I am teaching myself some Verilog with the help of icarus and the great course materials available online at MIT.
I set myself a little task to model a D flip-flop with NAND gates, and a test bench, this is what I came up with.
===
(file d_flip.v)
module d_flip(
input clk,
input d,
output q,
output q_bar
);
wire a, b;
assign a = !(clk && d);
assign b = !(clk && a);
assign q = !(a && q_bar);
assign q_bar = !(b && q );
endmodule
===
(file d_flip_tb.v)
module d_flip_tb;
reg d, clk;
wire q, q_bar;
d_flip uut(
.d(d),
.clk(clk),
.q(q),
.q_bar(q_bar)
);
initial begin
assign d = 1'b0;
assign clk = 1'b0;
$display("time = %t: q=%b, q_bar=%b",$time, q, q_bar);
#100;
assign clk = 1'b1;
//#10;
$display("time = %t: q=%b, q_bar = %b",$time, q, q_bar);
#10;
assign d = 1'b1;
assign clk = 1'b0;
//#10;
$display("time = %t: q=%b, q_bar = %b",$time, q, q_bar);
#10;
assign clk = 1'b1;
//#10;
$display("time = %t: q=%b, q_bar = %b",$time, q, q_bar);
end
Now when I run, i get this:
time = 0: q=x, q_bar=x
time = 100: q=x, q_bar = x
time = 110: q=0, q_bar = 1
time = 120: q=0, q_bar = 1
If I uncomment the extra time steps before each $display, I get this (which is what I expected):
time = 0: q=x, q_bar=x
time = 110: q=0, q_bar = 1
time = 130: q=0, q_bar = 1
time = 150: q=1, q_bar = 0
Why do I need to have an extra time step? it's as if the simulator assumes some delay or something ... ?
thanks dudes! fun stuff.