module DW01_add_tb; // Declare inputs and outputs reg [3:0] A; reg [3:0] B; reg CI; wire [3:0] SUM; wire CO; // Instantiate design under test DW01_add dut( .A(A), .B(B), .CI(CI), .SUM(SUM), .CO(CO) ); // Initialize inputs initial begin A = 4'b0000; B = 4'b0000; CI = 0; end // Adder test case task test_adder; // Test case 1: 0 + 0 with carry-in of 0 A = 4'b0000; B = 4'b0000; CI = 0; #10; if (SUM !== 4'b0000 || CO !== 1'b0) $display("Test failed! 0 + 0 should be 0 with carry-out of 0"); // Test case 2: 7 + 3 with carry-in of 0 A = 4'b0111; B = 4'b0011; CI = 0; #10; if (SUM !== 4'b1010 || CO !== 1'b0) $display("Test failed! 7 + 3 should be 10 with carry-out of 0"); // Test case 3: 5 + 11 with carry-in of 1 A = 4'b0101; B = 4'b1011; CI = 1; #10; if (SUM !== 4'b0001 || CO !== 1'b1) $display("Test failed! 5 + 11 should be 16 with carry-out of 1"); // Test case 4: 15+15 with carry-in of 1 A = 4'b1111; B = 4'b1111; CI = 1; #10; if (SUM !== 4'b1110 || CO !== 1'b1) $display("Overflow!!!"); endtask // Run test cases initial begin test_adder(); $finish; end endmodule // DW01_add_tb If I want to replace the delays, i.e, #10; in this piece of verilog code, by below code: integer cyc; initial cyc=1; always @ (posedge clk) begin cyc <= cyc+1; if(cyc==1)begin in_val1 <=4'd4; end else if (cyc==2) begin if (out_val1 !==8'b1) $stop; in_val <=4'd3; end else if (cyc==3)begin if(out_val1 !== 8'b10)$stop; in_val1 <= 4'd7; end else if(cyc==4) begin $write("ALL Finished \n"); $finish; end end in other words, using pound delay # is not allowed, how should I modify the code?
时间: 2024-03-28 14:42:18 浏览: 70
To replace the delays with the code you provided, you can use a counter to keep track of the number of clock cycles and use an if-else statement to control the input values at each cycle. Here's an example:
```
module DW01_add_tb;
// Declare inputs and outputs
reg [3:0] in_val1;
reg [3:0] in_val2;
wire [3:0] out_sum;
wire out_carry;
// Instantiate design under test
DW01_add dut(
.A(in_val1),
.B(in_val2),
.SUM(out_sum),
.CO(out_carry)
);
// Initialize inputs
initial begin
in_val1 = 4'b0000;
in_val2 = 4'b0000;
end
// Clock generator
reg clk = 0;
always #5 clk = ~clk;
// Counter to keep track of clock cycles
integer cyc = 0;
// Control input values without using pound delay #
always @(posedge clk) begin
cyc <= cyc + 1;
if (cyc == 1) begin
in_val1 <= 4'd4;
end else if (cyc == 2) begin
if (out_sum !== 4'b0001 || out_carry !== 1'b0) $stop;
in_val1 <= 4'd3;
end else if (cyc == 3) begin
if (out_sum !== 4'b0111 || out_carry !== 1'b0) $stop;
in_val1 <= 4'd7;
in_val2 <= 4'd5;
end else if (cyc == 4) begin
if (out_sum !== 4'b0000 || out_carry !== 1'b1) $stop;
in_val1 <= 4'd15;
in_val2 <= 4'd15;
end else if (cyc == 5) begin
if (out_sum !== 4'b1110 || out_carry !== 1'b1) $stop;
$write("ALL Finished\n");
$finish;
end
end
endmodule // DW01_add_tb
```
In this code, the clock generator generates a clock signal with a period of 10 time units (5 time units high, 5 time units low). The counter `cyc` keeps track of the number of clock cycles and is incremented on each positive edge of the clock. The if-else statement controls the input values at each clock cycle based on the value of `cyc`, and the output values are checked using an assertion. Finally, when all the test cases are completed, the simulation is finished using `$finish`.
阅读全文