module full_adder_8bit( input [7:0] a, input [7:0] b, input cin, output [7:0] sum, output cout ); wire [3:0] c; wire [3:0] s; // 低4位全加器 full_adder fa0 (.a(a[3:0]), .b(b[3:0]), .cin(cin), .s(s[0]), .c(c[0])); // 高4位全加器 full_adder fa1 (.a(a[7:4]), .b(b[7:4]), .cin(c[0]), .s(s[1]), .c(c[1])); // 最高位进位信号,等于高4位全加器的进位信号 assign c[2] = c[1]; // 低4位和高4位结果相加 full_adder fa2 (.a(s[3:0]), .b(s[3:0]), .cin(c[2]), .s(sum[3:0]), .c(c[3])); // 最高位全加器 full_adder fa3 (.a(a[7]), .b(b[7]), .cin(c[3]), .s(sum[7]), .c(cout)); endmodule module full_adder( input a, b, cin, output s, cout ); assign s = a ^ b ^ cin; assign cout = (a & b) | (a & cin) | (b & cin); endmodule写出其对应的完整的测试激励
时间: 2024-01-23 20:03:21 浏览: 29
下面是 full_adder_8bit 模块的完整测试激励代码:
```
module full_adder_8bit_tb;
// Inputs
reg [7:0] a;
reg [7:0] b;
reg cin;
// Outputs
wire [7:0] sum;
wire cout;
// Instantiate the Unit Under Test (UUT)
full_adder_8bit uut (
.a(a),
.b(b),
.cin(cin),
.sum(sum),
.cout(cout)
);
initial begin
// Initialize Inputs
a = 8'b00000000;
b = 8'b00000000;
cin = 0;
// Wait for 100 ns for global reset to finish
#100;
// Add 1 + 1 with no carry-in
a = 8'b00000001;
b = 8'b00000001;
cin = 1'b0;
#10;
$display("1 + 1 = %d, cout = %d", sum, cout);
// Add 255 + 1 with no carry-in
a = 8'b11111111;
b = 8'b00000001;
cin = 1'b0;
#10;
$display("255 + 1 = %d, cout = %d", sum, cout);
// Add 127 + 1 with carry-in
a = 8'b01111111;
b = 8'b00000001;
cin = 1'b1;
#10;
$display("127 + 1 (with carry-in) = %d, cout = %d", sum, cout);
// Add 255 + 255 with no carry-in
a = 8'b11111111;
b = 8'b11111111;
cin = 1'b0;
#10;
$display("255 + 255 = %d, cout = %d", sum, cout);
// Add 127 + 127 with carry-in
a = 8'b01111111;
b = 8'b01111111;
cin = 1'b1;
#10;
$display("127 + 127 (with carry-in) = %d, cout = %d", sum, cout);
// Add -1 + 1 with no carry-in
a = 8'b11111111;
b = 8'b00000001;
cin = 1'b0;
#10;
$display("-1 + 1 = %d, cout = %d", sum, cout);
// Add -128 + 127 with no carry-in
a = 8'b10000000;
b = 8'b01111111;
cin = 1'b0;
#10;
$display("-128 + 127 = %d, cout = %d", sum, cout);
// Add -128 + 128 with no carry-in
a = 8'b10000000;
b = 8'b10000000;
cin = 1'b0;
#10;
$display("-128 + 128 = %d, cout = %d", sum, cout);
// Add -128 + 128 with carry-in
a = 8'b10000000;
b = 8'b10000000;
cin = 1'b1;
#10;
$display("-128 + 128 (with carry-in) = %d, cout = %d", sum, cout);
// Add -1 + -1 with no carry-in
a = 8'b11111111;
b = 8'b11111111;
cin = 1'b0;
#10;
$display("-1 + -1 = %d, cout = %d", sum, cout);
// Add -128 + -128 with carry-in
a = 8'b10000000;
b = 8'b10000000;
cin = 1'b1;
#10;
$display("-128 + -128 (with carry-in) = %d, cout = %d", sum, cout);
$finish;
end
endmodule
```
这个测试激励可以测试 full_adder_8bit 模块的多种情况,包括正数相加、负数相加、有进位和无进位的情况,以及边界情况。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)