Verilog变量可以局部范围给一个总是阻塞吗?

mks*_*uth 8 scope verilog nonblocking blocking

我有时会发现在clocked always块中使用"局部变量"的阻塞赋值很有用.这可以帮助减少重复的代码.

为了避免在不同的always块中意外使用相同的变量(对于模拟来说可能是非确定性的),我想给它本地范围.有一个很好的综合方式吗?

就像是:

module sum3(
  input            clk,
  input      [7:0] in1,
  input      [7:0] in2,
  input      [7:0] in3,
  output reg [7:0] result,
  output reg [7:0] result_p1);

  begin :sum
    reg [7:0] sum_temp; // local variable
    always @(posedge clk) begin
      sum_temp   = in1 + in2 + in3;
      result    <= sum_temp;
      result_p1 <= sum_temp + 1;
    end
  end

endmodule
Run Code Online (Sandbox Code Playgroud)

(ModelSim似乎没问题,但Synplify似乎不喜欢它.)

Chi*_*ggs 10

我不确定普通Verilog中的语义,但根据SystemVerilog LRM第6.21节:

变量声明应位于程序块内的任何语句之前.

因此,以下是SystemVerilog中的合法语法:

module sum3(
  input            clk,
  input      [7:0] in1,
  input      [7:0] in2,
  input      [7:0] in3,
  output reg [7:0] result,
  output reg [7:0] result_p1);

  always @(posedge clk) begin : sum
    reg [7:0] sum_temp; // local variable (scope limited to process)
    sum_temp   = in1 + in2 + in3;
    result    <= sum_temp;
    result_p1 <= sum_temp + 1;
  end

endmodule
Run Code Online (Sandbox Code Playgroud)

请注意,我已将变量声明移动sum_temp到进程中,从而限制了范围并消除了对命名sum块的需要.这个编译在Modelsim和Riviera(EDA Playground上的例子).

如果您的工具不支持此语法,请提出错误!