1 `timescale 1ns / 1ps 2 module add_4_beha( 3 a, 4 b, 5 cin, 6 sum 7 ); 8 input [3:0] a; 9 input [3:0] b; 10 input cin; 11 output sum; 12 wire [3:0]a; 13 wire [3:0]b; 14 wire cin; 15 reg [4:0] sum; 16 17 always @ (a or b or cin) 18 begin 19 sum = a + b + cin; 20 end 21 endmodule
测试testbench:
`timescale 1ns / 1ps module add_4_beha_tb; reg [3:0] a,b; reg cin; wire [4:0] sum; initial $monitor ("a = %b, b = %b, cin = %b, sum = %b",a, b, cin, sum); initial begin #0 a = 4'b1100; b = 4'b0100; cin = 1'b0; #10 a = 4'b1100; b = 4'b0110; cin = 1'b1; #10 a = 4'b0010; b = 4'b0101; cin = 1'b1; #10 a = 4'b1000; b = 4'b1010; cin = 1'b0; #10 $stop; end add_4_beha inst( .a(a), .b(b), .cin(cin), .sum(sum) ); endmodule
不知道你有没有发现规律,这里的电路很复杂,但是描述语言不管内部结构,直接描述出其行为。
在测试单元中,直接简单的赋值,似乎更简单。我抓不到硬件的奥秘!