5
\$\begingroup\$

Will that create a latch?

always @ (posedge clk) begin
 if (enable) begin
   myvar1[63:0] <= some value;
   myvar2[63:0] <=some value2;
   myvar3[63:0] <= value3;
 end else begin
    myvar1[63:0] <=othervalue
 end
end

Should everything that is on the if, be on the else clause as well? I have understood that leaving an if without an else can cause a latch, but does it also mean that every signal on the if should be on the else? If that's the case, should I write something like this:

myvar2[63:0] <= myvar2[63:0];

or not?

\$\endgroup\$
10
\$\begingroup\$

No, this code should not infer latches. It is the case in all the synthesizers that I saw.

The reason is that a synthesis tool will infer flip-flop for any signal which is assigned inside a sequential block of code (@(posedge clk)). For all the branches which do not have an explicit assignments, the synthesis tool just muxes the output to the input, but since it is initially a flop, there is no concern.

What you're saying about latches applies to conbinational logic. For example, the following code infers latches for both myvar2 and myvar3:

always @* begin
 if (enable) begin
   myvar1[63:0] = some value;
   myvar2[63:0] =some value2;
   myvar3[63:0] = value3;
 end else begin
    myvar1[63:0] =othervalue
 end
end

Synthesis tries to mux the output to the input, but this requires a "memory" element to be in place in order to prevent a "loop". Latches controlled by enable signal will be inferred.

Hope this helps.

\$\endgroup\$
0
2
\$\begingroup\$

There's no concern about the contents of your else clause, because you've already created latches (registers) for all of those signals anyway, by putting them in a process that's triggered on a clock edge.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ I think OP is using the term latch to mean a level-sensitive latch, as opposed to an edge-triggered flip-flop. \$\endgroup\$ – The Photon Oct 8 '13 at 15:51
  • \$\begingroup\$ @ThePhoton: In that case, it would have to be an always @* block. \$\endgroup\$ – Dave Tweed Oct 8 '13 at 19:21
  • 2
    \$\begingroup\$ There's a lot of material out there that says "if you don't set all the same reg's in the else clause as you set in the if clause, it might infer a latch." Without explaining that distinction. I think that is what OP is asking about. \$\endgroup\$ – The Photon Oct 8 '13 at 19:36
  • \$\begingroup\$ @ThePhoton: I think you're reading too much into the question. Vasiliy said the same thing I did, but in more detail, and the OP seemed happy with that answer. \$\endgroup\$ – Dave Tweed Oct 8 '13 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.