Verilog:以下代码会产生竞争条件吗?

标签 verilog race-condition

我正在尝试使用 Verilog HDL 制作管道处理器。我意识到我的代码中可能存在一些竞争条件。因此,我将编写一个 sudo 代码,并想询问您其中是否存在竞争条件以及如何避免它:

module A(input wire reset, input wire clock, output reg a_reg_o);
   always @(posedge clock)
   begin
      if(reset == 1'h1)
      begin
         a_reg_o = 1'h0;
      end
      else
      begin
         a_reg_o = 1'h1;
      end
   end
endmodule

module B(input wire reset, input wire clock, input a_i);
   reg b;

   always @(posedge clock)
   begin
      if(reset == 1'h1)
      begin
         b = 1'h0;
      end
      else
      begin
         if(a_i == 1'h1)
         begin
            b = 1'h1;
         end
         else
         begin
            b = 1'h0;
         end
      end
   end
endmodule

module Main(input wire reset, input wire clock);
   wire a_o;
   A a(reset, clock, a_o);
   B b(reset, clock, a_o)
endmodule

想象一下我触发了重置信号。在时钟的第一个正沿之后,寄存器 a_reg_o 将变为 0,模块 B 中的寄存器 b 也将变为 0(尚无竞争条件)。现在我松开重置按钮并将其设置为负值。在时钟的下一个正沿,寄存器 a_reg_o 将变为 1,但是模块 B 中的寄存器 b 又如何呢?会是: 1. 零,因为它还没有看到 a_i 的变化。 2. 这取决于模块(A 和 B)总延迟(即竞争条件)。

谢谢。

最佳答案

是的,可能存在竞争条件,因为您不知道网络 a_o 是否首先由模块 A 驱动,然后由模块 B 捕获,或者反之亦然。

因此,您应该为此使用非阻塞分配,因为这将确保无论执行哪个模块,模块 B 都将始终具有先前的 net a_o 值。

您可以通过以下链接找到有关此非阻塞分配的更多信息。 http://www.sunburst-design.com/papers/CummingsSNUG2000SJ_NBA.pdf

关于Verilog:以下代码会产生竞争条件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691174/

相关文章:

verilog - 在 Verilog 中使用静态值

ASP.NET 竞争条件

c - 如何检测 sqlite3 是否创建了数据库文件?

Verilog,FPGA,使用单元化寄存器

java - AtomicReference 无法避免 java 多线程中的竞争条件

C++11 std::thread 和虚函数绑定(bind)

java - 拥有 'setter'/实例变量会导致单例 DAO 出现竞争条件吗?

verilog - 如何在系统verilog中进行位扩展?

Verilog ** 符号

verilog - 使用参数在Verilog中创建常量