1

I'm unable to wrap my head around Example 10-3 in SystemVerilog For Design book by Stuart Sutherland (and co.).

See line 232 of :

https://code.google.com/p/vak-opensource/source/browse/trunk/hardware/systemverilog/utopia-example/squat.sv?r=185

Here is the snippet of code. My question will follow.

  bit [0:NumRx-1] RoundRobin;

  always_ff @(posedge clk, posedge reset) begin: FSM
    bit breakVar;
    if (reset) begin: reset_logic
      Rxready <= '1;
      Txvalid <= '0;
      Txsel_out <= '0;
      SquatState <= wait_rx_valid;
      forward <= 0;
      RoundRobin = 1;
    end: reset_logic
    else begin: FSM_sequencer
      unique case (SquatState)

        wait_rx_valid: begin: rx_valid_state
          Rxready <= '1;
          breakVar = 1;
          for (int j=0; j<NumRx; j+=1) begin: loop1
            for (int i=0; i<NumRx; i+=1) begin: loop2
              if (Rxvalid[i] && RoundRobin[i] && breakVar)
                begin: match
                  ATMcell <= RxATMcell[i];
                  Rxready[i] <= 0;
                  SquatState <= wait_rx_not_valid;
                  breakVar = 0;
                end: match
            end: loop2
            if (breakVar)
              RoundRobin={RoundRobin[1:$bits(RoundRobin)-1],
                          RoundRobin[0]};
          end: loop1
        end: rx_valid_state

Specifically, my question is about the blocking assignment for breakVar and RoundRobin. I read somewhere that the variables are locally evaluated, but I can't picture in terms of gates how the logic is synthesized. Does RoundRobin get synthesized to a state register?

Most guidelines state to never mix blocking and nonblocking assignments. Is there a better way to represent something like this? Is it okay now in SystemVerilog designs to mix both types of assignments given that it is in an always_ff block?

0

2 Answers 2

6
+50

You should never mix blocking and nonblocking assignments to the same variable. breakVar is a temporary variable that will be synthesized into combinatorial logic because it is always written to first, then read. There is no state to be saved. RoundRobin is a local variable that is being used as both an intermediate and state variable. But because it is only accessed from within the always_ff block, there is no danger of a race condition.


A temporary variable is just a symbolic way to represent a piece of an equation. Here is a different but simpler example:

always_ff @(posedge clock)
   begin
   full = (counter == 10);
   brimming = (counter > 7);
   hold <= brimming && !full;
   if (full) 
      counter <= counter + 1;
   else 
      counter < = 0;
end

This is equivalent to writing the following (but might be harder to understand)

always_ff @(posedge clock)
   begin
   hold <= (counter > 7) && !(counter == 10);
   if (counter == 10) 
      counter <= counter + 1;
   else 
      counter < = 0;
end

In the two examples above, counter will always be synthesized as a register because it is read before written. It won't matter if we used a blocking or nonblocking assignment because we never read counter after writing it. There is no race condition within this always_ff block using blocking assignment but there could be if there was another always_ff block trying to read it. Since full and brimming are written before being read, they do not have to be registered.

To summarize, a variable get synthesized as a register if any of these conditions are true

  1. A variable is read before being written within the same always block. Note that even if a non-blocking assignment statement appears first, the read happens first because the write get scheduled to happen later.
  2. Due to conditional or looping statements, a variable is sometimes read without being written
  3. A variable is written in an always_ff block and read outside the block.
5
  • I'm not sure that's correct - RoundRobin in this case is a register, should really be using non-blocking assignment.
    – Chiggs
    Jun 13, 2014 at 14:34
  • I was half-correct - RoundRobin stores state but is also read after being written in the loop and so requires a blocking assignment... My comment was slightly misleading, but you spotted the deliberate mistake ;)
    – Chiggs
    Jun 13, 2014 at 15:32
  • 1
    The 2 always block approach is cleaner, though it requires a little more typing. One always_comb for all blocking assignments including next_state values, and an always_ff for non-blocking assigning the states to their next_state. This allows see the D pin in wave from which is useful for debug. I find it makes ECOs easier to do and documented. According to Cliff's SNUG2003 paper on FSM, the 2-always approach gives better area and timing but I think it is really tool/version dependent.
    – Greg
    Jun 13, 2014 at 17:24
  • @dave_59, could you please elaborate a little on how a temporary variable is synthesized?
    – user12311164
    Oct 14, 2020 at 7:47
  • 1
    Updated answer.
    – dave_59
    Oct 17, 2020 at 22:18
0

Totally agreed with @jonathan answer.

You should always split your logic elements in always_comb block and sequential elements in always_ff block.

  • If you write a code that is so closely stitched together ( both combi and sequential elements in same block) even though it is correct and compliant with system verilog spec, some older versions of simulator or newer simulators being developed may infer it in wrong way.

  • Your code will not be clean and comprehensible to others.

  • Also by writing in above style you are just compacting the lines of code, even though the logic remains same. There is no sense in writing a compact code if it hampers the readability of the code.

Now as far as blocking and non-blocking statements usage is concerned, I think that debate is closed now. It is now more a rule than a guideline to use blocking statements in always_comb block and non-blocking in always_ff block.

However the answer to all your questions are explained in this superb paper by Clifford E Cummings Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!

And if you are new to verilog/system verilog design I suggest you read all their papers, They are very useful and sets up a good base for a RTL Design Engineer.

Also it may be too much to tell here but if you are looking how to segregate your code in combi and sequential block you can have a look at code generated by bluespec

The signal names are difficult to comprehend in one go, but if you look closely the code is very neat logically and does not leave anything on the whims of simulation and synthesis tools.

3
  • The paper you have quoted is about Verilog not SystemVerilog.
    – user12311164
    Oct 17, 2020 at 6:23
  • @ShashankVM SystemVerilog is a superset of Verilog, same concepts apply. Oct 17, 2020 at 8:43
  • compliant with system verilog spec, some older versions of simulator or newer simulators being developed may infer it in wrong way then that's a bug and order the vendor to fix it ASAP, they must follow the specs or they call it something else. That's also a sufficient legal fault to invalidate any purchase of such a simulator. (done it and we won)
    – Alexis
    Apr 1, 2022 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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