I am trying to use gate level to model a JK flip-flop to show the Q and Qbar toggling effect. The problem is that when programmed into the device, only the Qbar is toggling while Q is constantly on.
How do I make both Q and Qbar toggle in sequence? So I can show the race condition.
My device is a BASYS3 from Digilent, and my code is:
module JK_gate(q,qbar,clk,j,k);
input j,k,clk;
output q,qbar;
wire nand1_out; // output from nand1
wire nand2_out; // output from nand2
wire new_clk;
clk_div wrapper(
.clk(clk),
.divided_clk(new_clk)
);
nand(nand1_out, j,new_clk,qbar);
nand(nand2_out, k,new_clk,q);
nand(q,qbar,nand1_out);
nand(qbar,q,nand2_out);
endmodule
The clock divider is:
module clk_div(
input wire clk,
output reg divided_clk = 0
);
localparam div = 50000000;
//division value = 100 MHz/(2 x desired frequency)
integer counter_val = 0;
always@ (posedge clk)
begin
if (counter_val == div) counter_val <= 0; //reset
else counter_val <= counter_val +1; //count +1
end
always@ (posedge clk)
begin
if (counter_val == div) divided_clk <= ~divided_clk; //flip the signal
else divided_clk <= divided_clk; //signal stays
end
endmodule
The simulation that I am trying to get to work is, for this I removed the clock divider in the original code to remove 1 layer of complexation.
module jk_sim;
reg j, k, clk;
wire q, qbar;
JK_gate U0(q,qbar,clk,j,k);
initial begin
j=1'b0;
k=1'b0;
clk=1;
#100
j=1'b1;
k=1'b0;
#100
j=1'b0;
k=1'b1;
#100
j=1'b1;
k=1'b1;
end
always #25 clk=~clk;
endmodule