Verilog JK - help pls

OP (Company: NON) #7323859
Rate this post
• ▲ useful
▼ not useful

What is the problem that signal is not defined? How do you define an initial value for the output?

1
module JK_Latch(Q, Qbar, J, K, Enable);
2

3
input J, K, Enable;
4
output Q, Qbar;
5

6
wire J_E, K_E;
7

8
and(J_E, J, Enable, Qbar);
9
and(K_E, K, Enable, Q);
10

11
nor(Q, Qbar, K_E);
12
nor(Qbar, Q, J_E);
13

14
endmodule
15

16
module TB_JK_Latch();
17

18
wire q, qbar;
19
reg j, k, e;
20

21
JK_Latch JK0(q, qbar, j, k, e);
22
initial
23
begin
24
    $monitor($time," j =%b, k=%b, q=%b, qbar=%b, enable=%b\n",j,k,q,qbar,e);
25
    
26
    j=1; k=0; e=1;
27
       
28
    #5 j=0; k=1;
29
    #5 j=0; k=0;
30
    #5 j=1; k=0;
31
    #5 j=1; k=1;
32
    
33
    #5 j=1; k=1; e=0;
34
    #5 j=0; k=1;
35
    #5 j=0; k=0;
36
    #5 j=1; k=0;
37
    #5 j=1; k=1;
38
end
39
endmodule
#7328428
Rate this post
• ▲ useful
▼ not useful

Daniel C. wrote:

What is the problem that signal is not defined?

There is a permanent mutual dependency betwenn Q and Qbar that can not be overridden by the input signals. So, since Q and Qbar are undefined in the beginning, they remain undefined forever in the simulation. In real hardware, they would fall into a random state which is mainly determined by physical parameters of the technology.

How do you define an initial value for the output?

By using the initial statement. But only in the simulation. initial will not synthesize. Or you could extend your design by a reset signal that forces Q or Qbar to a logic level.

I hope you are doing this just for learning and playing around in the simulator. In real hardware designs, you would avoid latches as far as possible. And if you really would need one, you would use a D-latch. And if you really really would need a JK-latch, you would never use a self-build combinatorial feedback latch but take a primitive from a vendor library or emulate it using a D-latch. Actually, since more than 30 years, JK FFs and latches are used for stressing students, so unless you are a student, you should forget about JK type memories.

And please stop using gate primitives in your design. Use boolean expressions instead:

nor(Qbar, Q, J_E) is equivalent to assign Qbar = ~(Q | J_E)

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.