Title

SystemVerilog -- 3.0 SystemVerilog Threads

SystemVerilog Threads

What are SystemVerilog threads or processes ?

thread 或 process 是作为单独实体执行的任何一段代码。在 verilog 中,每个initialalways块都作为单独的 thread 生成,这些 threads 从 0 time 开始并行运行。block 还会创建并运行的不同 threads。fork join

What are different fork - join styles ?

我们在 SystemVerilog 中有三种不同的风格。fork join

/ /
fork join Finshes when all child threads are over
fork join_any Finshes when any child thread ges over
fork join_none Finshes when soon after child threads are spawned

Where is this used in the testbench ?

验证环境中的组件可能需要能够同行运行多个tasks。例如,一个 process 可能会等待某些事情发生,而另一个 process 继续执行其他一些 tasks。他们都通过fork ... join作为单独的线程生成。例如,checker 可以并行生成不同的tasks,以捕获和验证来自测试平台不同部分的数据。 fork ... join

What is the limitation of a Verilog fork join ?

只有在 fork-join 内生成的所有 threads 都完成时,才会执行 a 之后的代码。因此,checker 必须等到在 fork-join 中生成的所有 thrads 完成,然后才能继续。 fork ... join

fork join Example

SystemVerilog 会等到所有 forked processes 都完成。 fork join

module tb_top;
  initial begin
    #1 $display ("[%0t ns] Start fork ...", $time);

    // Main Process: Fork these processes in parallel and wait untill all of them finish
    fork
      // Thread1 : Print this statement after 5ns from start of fork
      #5 $display ("[%0t ns] Thread1: Orange is named after orange", $time);
      // Thread2 : Print these two statements after the given delay from start of fork
      begin
      #2 $display ("[%0t ns] Thread2: Apple keeps the doctor away", $time);
      #4 $display ("[%0t ns] Thread2: But not anymore", $time);
      end
      // Thread3 : Print this statement after 10ns from start of fork
      #10 $display ("[%0t ns] Thread3: Banana is a good fruit", $time);
    join

    // Main Process: Continue with rest of statements once fork-join is over
    $display ("[%0t ns] After Fork-Join", $time);
  end
endmodule

模拟日志

ncsim> run
[1 ns] Start fork ...
[3 ns] Thread2: Apple keeps the doctor away
[6 ns] Thread1: Orange is named after orange
[7 ns] Thread2: But not anymore
[11 ns] Thread3: Banana is a good fruit
[11 ns] After Fork-Join
ncsim: *W,RNQUIE: Simulation is complete.

fork join_any Example

SystemVerilog 会等待,直到任何一个 forked processes 是完成的。fork join_any

module tb_top;
  initial begin
    #1 $display ("[%0t ns] Start fork ...", $time);

    // Main Process: Fork these processes in parallel and wait untill any one of them finish
    fork
      // Thread1 : Print this statement after 5ns from start of fork
      #5 $display ("[%0t ns] Thread1: Orange is named after orange", $time);
      // Thread2 : Print these two statements after the given delay from start of fork
      begin
      #2 $display ("[%0t ns] Thread2: Apple keeps the doctor away", $time);
      #4 $display ("[%0t ns] Thread2: But not anymore", $time);
      end
      // Thread3 : Print this statement after 10ns from start of fork
      #10 $display ("[%0t ns] Thread3: Banana is a good fruit", $time);
      
    join_any

    // Main Process: Continue with rest of statements once fork-join is exited
    $display ("[%0t ns] After Fork-Join", $time);
  end
endmodule

模拟日志

ncsim> run
[1 ns] Start fork ...
[3 ns] Thread2: Apple keeps the doctor away
[6 ns] Thread1: Orange is named after orange
[6 ns] After Fork-Join
[7 ns] Thread2: But not anymore
[11 ns] Thread3: Banana is a good fruit
ncsim: *W,RNQUIE: Simulation is complete.

fork join_none Example

SystemVerilog 不会等待并立即退出block,允许 forked processes 在后台运行。main thread 恢复执行block之后的语句。 fork join_none fork join_none

module tb_top;
  initial begin
    #1 $display ("[%0t ns] Start fork ...", $time);

    // Main Process: Fork these processes in parallel and exits immediately
    fork
      // Thread1 : Print this statement after 5ns from start of fork
      #5 $display ("[%0t ns] Thread1: Orange is named after orange", $time);
      // Thread2 : Print these two statements after the given delay from start of fork
      begin
      #2 $display ("[%0t ns] Thread2: Apple keeps the doctor away", $time);
      #4 $display ("[%0t ns] Thread2: But not anymore", $time);
      end
      // Thread3 : Print this statement after 10ns from start of fork
      #10 $display ("[%0t ns] Thread3: Banana is a good fruit", $time);
      
    join_none

    // Main Process: Continue with rest of statements once fork-join is exited
    $display ("[%0t ns] After Fork-Join", $time);
  end
endmodule

模拟日志

ncsim> run
[1 ns] Start fork ...
[1 ns] After Fork-Join
[3 ns] Thread2: Apple keeps the doctor away
[6 ns] Thread1: Orange is named after orange
[7 ns] Thread2: But not anymore
[11 ns] Thread3: Banana is a good fruit
ncsim: *W,RNQUIE: Simulation is complete.

posted on 2024-05-12 21:46  松—松  阅读(3)  评论(0编辑  收藏  举报

导航