0

Is this true to say that the following Code-1 and Code-2 are equivalent in Verilog:

Code 1

always@(posedge Clock or B or C) 
 begin
   if (B)
    A <= 0;
   else if (C)
    A <= 1; 
 end 

Code 2

 always@(posedge Clock or B or C) 
 begin
   if (B)
    A <= 0;
   else if (C)
    A <= 1; 
   else
    A <= A;
 end 

Is the same thing also true in VHDL?

Code-3

process (clk, preset, reset) 
 begin  
  if (preset = ‘1’) then 
   ff <= ‘1’; 
  elsif (reset = ‘1’) then 
   ff <= ‘0’; 
  elsif (clk=’1’ and clk’event) then 
   ff <= ff_d; 
 endif; 
end process

Code-4

process (clk, preset, reset) 
 begin  
  if (preset = ‘1’) then 
   ff <= ‘1’; 
  elsif (reset = ‘1’) then 
   ff <= ‘0’; 
  elsif (clk=’1’ and clk’event) then 
   ff <= ff_d;
  else
   ff <= ff; 
 endif; 
end process

Could we also say Code-3 and Code-4 are the same?

How about the same thing about a combinatorial logic ? Can we say the followings are equivalent (Verilog example)?

code-5

  always @ ( * ) begin
    if ( Trigger ) begin
      A = Pass ;
    end
  end

code-6

  always @ ( * ) begin
    if ( Trigger ) begin
      A = Pass ;
    end
    else begin 
     A = A;
    end
  end

Could we also say Code-5 and Code-6 are the same? f

Rudy01
  • 1,027
  • 5
  • 18
  • 32
  • Codes 3 and 4 are the same. –  Mar 12 '14 at 15:16
  • Use `always@(posedge Clock)` NOT `always@(posedge Clock or B or C)` – Morgan Mar 12 '14 at 15:57
  • See this question for 1 vs. 2, with the caveat that as Morgan pointed out you should never write your code this way: http://stackoverflow.com/questions/11007729/better-way-of-coding-a-d-flip-flop – nguthrie Mar 12 '14 at 16:09
  • I think @Rudy01 meant to use asynchronous set/reset Codes 1 and 2; `always@(posedge Clock or posedge B or posedge C)` – Greg Mar 12 '14 at 16:27
  • Related question: [better way of coding a D flip-flop](http://stackoverflow.com/questions/11007729/better-way-of-coding-a-d-flip-flop). This question also includes vhdl, so it is not a full duplicate. – Greg Mar 12 '14 at 16:32
  • @Greg: I've always wondered what happens if we are missing a condition. I have always coded up asynchronous set/reset just like code-3, however I am thinking that code-4 is actually what the tool implements (is this correct)! I am assuming every time a condition is missing, the tool will holds the signal value, regardless whether it is a sequential or combinatorial process ! But it would be okay in the sequential process, because it is a EDGE sensitive process, however it will NOT be okay in a combinatorial process since it is level sensitive ! – Rudy01 Mar 12 '14 at 16:37

2 Answers2

4
  1. Code-1 and Code-2 are the same, but messy.
  2. Brian Drummond has answered that Code-3 and Code-4 are the same.
  3. Code-5 and Code-6 are the same, and both contain the same mistake.

Code-1 and Code-2 update based Gregs comments
The use of always@(posedge Clock or B or C) is messy because it combines edge triggered and level triggered.

You may want a combinatorial block always @*, the * gives you an automatically compiled sensitivity list (less bugs, easier refactoring). While learning there is sometimes the idea of purposefully giving an incomplete sensitivity list to get the simulation behaviour you want, Do not do this for RTL. When synthesised you will get the behaviour of always @*.

To imply sequential or flip-flop use edge triggered ie

// Flip-flop sync or no reset
always @(posedge clk) begin 

//Flip-flop with async active-low reset.
always @(posedge clk or negedge rst_n) begin

//Flip-flop with asyn active-low reset and async set
always @(posedge clk or negedge rst_n or posedge set) begin

As Greg has also noted from at least 2001 comma , separated lists can be used instead of or.

Code-5 and Code-6 imply combinatorial blocks, which maintain state this implies a latch.

Latches are not inherently bad but require a touch of caution therefore accidental ones are often cause for concern and many bugs.

A flip-flop is two latches one after the other with a clock inversion. This means that only 1 of the 2 latches is open at a given point. A single latch when enabled is transparent.

If a latch is open and closes on near the updating data (posedge of a clock), timing uncertainties mean that you may latch the old data or the new data.

A typical way to control this is to have open the latch on the first half of the clock and close it for the second half. This ensures the Latch hold the Output value at the time when the input is to be updated. Often using a full cycle enable to operate a clock gate to generate the enable signal for the latch.

Morgan
  • 19,934
  • 8
  • 58
  • 84
  • Okay I see the point. Code-5 and Code-6 are bad, since it will create latch. I also agree that I should not use always@(posedge Clock or B or C), and instead I should have to separate processes one that has always@(posedge Clock) and the other always(B or C). But I have seen people use this style of coding a lot, when I look at other people's code, and that is why I thought it is a better of coding. But apparently it is not. – Rudy01 Mar 12 '14 at 16:29
  • I just want to add that Codes 1&2 infer messy logic because `B` and `C` are value change sensitive, not edge sensitive. Synchronous set/reset should be `always @(posedge Clock)`. Asynchronous `always @(posedge Clock or posedge B or posedge C)` or `always @(posedge Clock, posedge B, posedge C)`. In IEEE Std 1364-2001 section 9.7.4 _Event or operator_, "The keyword `or` or a comma character (`,`) is used as an event logical or operator. A combination of these can be used in the same event expression. Comma-separated sensitivity lists shall be synonymous to or-separated sensitivity lists." – Greg Mar 12 '14 at 16:53
  • @Rudy01, Don't use `always@(B or C)`, use `always @*` or `always @(*)` instead. All three are sensitivity lists for combination logic, however the latter two are self constructing lists. This will safe you if additional signals should have been defined in the list. – Greg Mar 12 '14 at 16:59
  • @Greg feel free to edit my answers in the future if you can improve or add to them. If I do not agree with the edit I can always revert. I do not mind if they become community wikis either if they help create good answers. – Morgan Mar 12 '14 at 17:52
0

The basic answer to your question is true of both VHDL and Verilog. If you trace every possible path through your 'process' (and everything essentially decomposes to a 'process', in both VHDL and Verilog), and a 'signal' is not assigned to in one or more paths, then that signal holds its value in those paths. It has to, because you haven't assigned to it. It doesn't make any difference if you explicitly add an assignment which says 'assign the signal to itself', because this is the same as not assigning to the signal at all (assuming that your code isn't sensitive to delta-delays, you haven't done anything fancy with manually-specified delays, and so on). It doesn't make any difference if the process is interpreted as being clocked or combinatorial.

One of the comments refers to an SO thread where someone suggests that a form which an explicit assignment may synthesise to a gated-clock rather than a gated-data implementation. I don't buy this, and I'd like to see an example.

Having said all that, you need to stick with conventional synthesis templates to get something that you know will synthesise as intended, rather than just simulating correctly. Note also that your snippets #1 and #2 are useless, and are not equivalent to #3/#4 - there's no data input. and there's nothing wrong with latches, as long as your tool flow can cope with them (ie. do timing analysis on them). And you don't need al lthe brackets in the VHDL, use rising_edge, etc.

EML
  • 9,619
  • 6
  • 46
  • 78