1.定义
两个一位数的相加,称为半加器。
2.真值表
x为第一个加数,y为第二个加数,c为进位,s为x+y右边一位和。
x y c s
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
3.逻辑表达式
与门
异或
4.verilog 程序
4.1 数据流描述方式
module add_half(
input x,
input y,
output c,
output s
):
assign c=x&y;
assign s=x^y;
endmodule
4.2 门级描述方式
module add_half(
input x,
input y,
output c,
output s
);
and(c,x,y);
xor(s,x,y);
endmodule
4.3 行为描述方式
module add_half(
input x,
input y,
output c,
output s
);
always@(a or b)
begin
case({a,b})
2'b00:begin s=0;c=0; end
2'b01:begin s=1;c=0; end
2'b10:begin s=1;c=0; end
2'b11:begin s=0;c=1; end
endcase
end
5.RTL 逻辑图
6.仿真
6.1 testbench
`timescale 1ns / 1ps
module sim_add_half(
);
reg x,y;
reg clk;
wire s,c;
initial#初始化数据
begin
#1
x=0;
y=0;
clk=0;
end
always #5 clk=~clk;
always@(posedge clk)
begin
x={$random}%2;
y={$random}%2;
end
add_half u1(x,y,c,s); #调用原文件
endmodule
6.2 仿真图