Should case variable be increment atomically?

时间:2019-03-19 14:54:45

标签: verilog fpga

I'm writing Verilog code which iterate through the states of an FSM in order to perform a calculation in steps. The application is done and running as desired, but for efficiency requirements I'm wondering the following.

I'm updating the case variable, "i", atomically as shown in excerpt 1 below, is this at all necessary to ensure it does not skip a step in my FSM?

Or is the method in excerpt 2 equally safe to run without a step being missed?

As excerpt two is twice as fast as excerpt one.

Before it is asked. Everything runs, everything is initialised prior to use, counters reset to 0 when finished and encased in a module.


Code for atomic update of "i"

always @(posedge clk) begin
  if( ITERATE ) i = i + 1; //Atomic increment of variable
  else begin
    case( i )
      0: ....
      .: .... 
      n: The case internals are not pertinent
    endcase
  end
  ITERATE = !ITERATE; //Trigger for atomic update of i
end

Code for non-atomic update of "i"

always @(posedge clk) begin
  case( i )
    0: ....
    .: .... 
    n: The case internals are not pertinent
  endcase
  i = i + 1; //Non-atomic increment of variable
end

1 个答案:

答案 0 :(得分:0)

在两种情况下,您的always块在​​clk的每个上升沿仅执行一次。他们将不会在相同的增量周期内进行重新评估(除非您自己在clk中有故障)。

在always块中的代码与其他编程语言一样按顺序执行。

因此,根据您的样本,第一个将在每个时钟沿增加“ i”,而第二个将在每个时钟沿将其递增。

  

我正在自动更新大小写变量“ i”,如下面的摘录1所示,这是否完全必要,以确保它不会跳过我的FSM中的任何步骤?

我不确定您的意思,但是i会逐个递增而不跳过,并且fsm中的case语句将有机会对两种情况下的'i'的每个值做出反应。由于您在第一个样本中使用了ITERATE,因此时间安排有所不同。同样,在第一个示例中,i将在case语句之前 递增,在第二个示例中,在之后递增。这是另一个区别。

顺便说一句,atomic一词不适用于任何情况。你在那是什么意思?

还请注意,您没有按照一般建议编写状态机,没有拆分状态和转换逻辑,也很可能在示例中滥用了阻塞分配。这些对于测试台来说可能是可接受的代码示例,但对于RTL可能不是很好。