Beruflich Dokumente
Kultur Dokumente
initial begin
d2 = 0; d1 = 0; d0 = 0 ;
end
module tb_graycounter;
reg tclk,tq2,tq1,tq0,tx ;
wire td2,td1,td0 ;
reg dut_error;
reg td2_compare, td1_compare , td0_compare;
graycounter tb_graycounter(tclk,tx,tq2,tq1,tq0,td2,td1,td0);
event reset_tx;
event terminate_sim;
initial
begin
$display ("########");
tclk = 0 ;
tq2 = 0 ;
tq1 = 0 ;
tq0 = 0 ;
tx = 0 ;
dut_error = 0;
end
always
#5 tclk = ~tclk;
initial
begin
$dumpfile ("tb_graycounter.vcd");
$dumpvars;
end
initial
@ (terminate_sim) begin
$display ("Terminating simulation");
if (dut_error == 0) begin
$display ("Simulation Result : PASSED");
end
else begin
$display ("Simulation Result : FAILED");
end
$display ("#########");
#1 $finish;
end
event reset_done;
initial
forever begin
@ (reset_tx);
@ (posedge tclk)
$display ("Applying tx");
tx = 1;
@ (posedge tclk)
tx = 0;
$display ("Came out of tx");
-> reset_done;
end
initial begin
#10 -> reset_tx;
@ (reset_done);
@ (posedge tclk);
tx = 1;
repeat (5)
begin
@ (negedge tclk);
end
tx = 0;
#5 -> terminate_sim;
end
initial begin
td2_compare = 1'b0 ; td1_compare = 1'b0 ; td0_compare = 1'b0 ;
#5
td2_compare = 1'b0 ; td1_compare = 1'b0 ; td0_compare = 1'b1 ;
#20
td2_compare = 1'b1 ; td1_compare = 1'b0 ; td0_compare = 1'b0 ;
#10
td2_compare = 1'b0 ; td1_compare = 1'b0 ; td0_compare = 1'b1 ;
#10
td2_compare = 1'b1 ; td1_compare = 1'b0 ; td0_compare = 1'b0 ;
end
endmodule
simulation of grey counter
Explanation :
The verilog code implements a 3 bit gray counter . The grey counter changes only one bit of its
output sequence given the input sequence.
The present state is represented by q2,q1, and q0 whereas the next state is represented by d2,d1,d0 .
The input to d filp flop is X.
The test bench creates three events : reset , terminate_simulation and reset_done.
td2_compare,td1_compare and td0_compare represent the expected output while td2,td1, and td0
represent the actual output.