1 `timescale 1ns / 1ps 2 module or3( 3 x1, 4 x2, 5 x3, 6 z1 7 ); 8 input x1, x2, x3; 9 output z1; 10 11 wire x1, x2 , x3; 12 reg z1; 13 always @ (x1 or x2 or x3) 14 begin 15 z1 = x1 | x2 | x3; 16 end 17 endmodule
1 `timescale 1ns / 1ps 2 module or3_tb; 3 4 reg x1, x2, x3; 5 wire z1; 6 initial 7 begin : apply_stimulus 8 reg [3:0] invec; 9 for(invec = 0; invec < 8; invec = invec + 1) 10 begin 11 {x1, x2 ,x3} = invec [3:0]; 12 #10 $display ("x1x2x3 = %b , z1 = %b ",{x1, x2, x3}, z1); 13 end 14 end 15 or3 inst( 16 .x1(x1), 17 .x2(x2), 18 .x3(x3), 19 .z1(z1) 20 ); 21 endmodule
这就是三输入或门,输出结果为:
这里需要记录的是:for循环哪里要格外小心,这里x1x2x3是相当于3比特的矢量,为什么要申请4比特的invec呢?
因为,这里如果invec也是3比特的,for循环中就会陷入一个死循环,永远出不来。1000.100