There are several mistakes in the code.
1) The line if(clk)begin
and relevant end
should be removed, posedge clk
already describes trigger condition of the flip-flop.
2) A non-blocking assignment (<=
) is required for the sequential logic.
The always
block should be as follows:
always @ (posedge clk) begin
q <= inp;
end
3) Some simulators don't complain, but signal q
should be wire
in module rff_try_1
.
wire q;
Simulation
I simulated the code (after the modifications) on EDA Playground with the testbench below. Used Icarus Verilog 0.9.7 as simulator.
module tb();
reg clk = 1;
always clk = #5 ~clk;
reg inp;
wire q;
rff_try_1 dut(q, inp, clk);
initial begin
inp = 0;
#12;
inp = 1;
#27;
inp = 0;
#24;
inp = 1;
end
initial begin
$dumpfile("dump.vcd"); $dumpvars;
#200;
$finish;
end
endmodule
The signal q
is as expected as seen on the waveform.
