代码之家  ›  专栏  ›  技术社区  ›  justrajdeep

有人能解释系统verilog中的分叉和循环吗?

  •  0
  • justrajdeep  · 技术社区  · 8 年前

    我正在经历 SV LRM section 9.3.2 有了这个疑问,我粘贴下面的示例以供参考

    for(int j=1; j <=3; ++j)
        fork
            automatic int k = j;
            begin
                .... # use k here
            end
        join_none
    
    • 有人能告诉我到底发生了什么吗?

    • 如果我移动 automatic 分叉外的变量?

      for(int j=1; j <=3; ++j) begin
          automatic int k = j;
          fork
              begin
                  .... # use k here
              end
          join_none
      end
      
    • 如果我在叉子内移动循环会发生什么?

      fork
          for(int j=1; j <=3; ++j) begin
              automatic int k = j;
              begin
                  .... # use k here
              end
          end
       join_none
      

    提前谢谢。

    2 回复  |  直到 8 年前
        1
  •  4
  •   dave_59    8 年前

    将自动变量移到叉外,但仍在 begin/end for 循环具有相同的效果。循环的每次迭代都会获得其自身的 k 用当前循环值初始化 j .所以你得到了三份 k 值为1、2和3。由fork/join_none生成的每个进程都绑定到 k 由于fork/join_none在循环中,因此产生了三个进程。

    如果你移动 对于 在fork中循环,则只得到一个由 fork 其具有环。那么使用j或k并不重要,因为循环中的代码是按顺序执行的。

        2
  •  0
  •   Karan Shah    8 年前

    1、3的效果相同,每个线程 fork...join 将取j的适当值(j=1,2,3…将取所有值)。

    但对于第二种情况,由于您将值分配给 k 在线程之外,将为所有线程取k的最后一个值。

    下面是每个案例的示例代码。

    // Code 1
    for(int j=1; j <=3; ++j)
        fork
            automatic int k = j;
            begin
              $display("Current Value - %0d", k);
            end
        join_none
        wait_fork;
    
    // Output of Code 1
    Current Value - 1
    Current Value - 2
    Current Value - 3
    
    // Code 2
    for(int j=1; j <=3; ++j)
    begin
        automatic int k = j;
        fork
            begin
              $display("Current Value - %0d", k);
            end
        join_none
    end
    wait_fork;
    
    // Output of Code 2
    Current Value - 3
    Current Value - 3
    Current Value - 3
    
    // Code 3
    fork
        for(int j=1; j <=3; ++j)
          begin
            automatic int k = j;
            $display("Current Value - %0d", k);
          end
    join_none
    wait fork;
    
    // Output of Code 3
    Current Value - 1
    Current Value - 2
    Current Value - 3