I am modelling a JK FLIP Flop using Verilog. I am getting the output q as x in all the cases. I used case statement for various jk combinations. I hardly found any issue with the code.
Design
module jk_ff ( input j, input k, input clk, output q);
reg q;
always @ (posedge clk)
case ({j,k})
2'b00 : q <= q;
2'b01 : q <= 1'b0;
2'b10 : q <= 1'b1;
2'b11 : q <= ~q;
endcase
endmodule
test bench
module tb_jk;
reg j;
reg k;
reg clk;
always #5 clk = ~clk;
jk_ff jk0 ( .j(j),
.k(k),
.clk(clk),
.q(q));
initial begin
$dumpfile("test.vcd");
$dumpvars;
j <= 0;
k <= 0;
#5 j <= 0;
k <= 1;
#20 j <= 1;
k <= 0;
#20 j <= 1;
k <= 1;
#20 $finish;
end
initial
$monitor ("j=%0d k=%0d q=%0d", j, k, q);
endmodule
logs
vu2swz@PPDP01:~$ iverilog jk_ff.v
vu2swz@PPDP01:~$ ./a.out
VCD info: dumpfile test.vcd opened for output.
j=0 k=0 q=x
j=0 k=1 q=x
j=1 k=0 q=x
j=1 k=1 q=x
The output is showing only x instead of 0 1 and Q.
When I ran your simulation and looked at waveforms, I saw that
clkwas always unknown (x). You declaredclkasreg, which means that it defaults tox. Your assignment (clk = ~clk) does not change the value ofclkbecause~clkevaluates to~x, which is stillx.You need to initialize
clkto a known value in your testbench. For example, change:to:
This shows:
There are 2 ways to debug this problem. As I mentioned, one way is to look at waveforms; you already create the VCD file. This is the most efficient way. The
iverilogsite says GTKWave "is the preferred waveform viewer for Icarus Verilog".Another way is to add the
clksignal to your$monitorstatement:Then you would have seen: