SystemVerilog for loop

SystemVerilog中的for循环多次重复给定的语句集,直到不满足给定的表达式为止。 像所有其他过程块一样,for循环要求其中的多个语句由begin和end关键字括起来。

Syntax

For循环使用三步方法来控制其语句的执行:

【1】初始化影响循环运行次数的变量
【2】在执行循环之前,请检查条件是否为真
【3】修饰符在每次迭代结束时执行,并跳至步骤2。、

 for ( [initialization]; <condition>; [modifier])
    //单一陈述
 
  for ( [initialization]; <condition>; [modifier]) begin
    // 多个陈述
  end

Example#1-数组迭代

在此示例中,我们将遍历字符串数组并打印出其内容。数组用5种不同的水果名称初始化。for循环初始化声明了一个名为i的局部变量,该局部变量表示数组中任何元素的索引。 条件表达式检查i是否小于数组的大小。 修饰符增加i的值,以便for循环的每次迭代都在不同的索引上运行。

module tb;
  string array [5] = '{"apple", "orange", "pear", "blueberry", "lemon"};
 
  initial begin
    for (int i = 0; i < $size(array); i++)
      $display ("array[%0d] = %s", i, array[i]);
  end
endmodule
 
Simulation Log
ncsim> run
array[0] = apple
array[1] = orange
array[2] = pear
array[3] = blueberry
array[4] = lemon
ncsim: *W,RNQUIE: Simulation is complete.

Example#2-多次初始化

for循环的第一部分可以进行多次初始化。 在下面显示的代码中,一旦输入for循环,变量i和j都将初始化。 为了使示例有趣,将j指向的每个字符串的索引替换为0。

module tb;
  string array [5] = '{"apple", "orange", "pear", "blueberry", "lemon"};
 
  initial begin
      for (int i = 0, j = 2; i < $size(array); i++) begin
      array[i][j] = "0";
        $display ("array[%0d] = %s, %0dth index replaced by 0", i, array[i], j);
    end
  end
endmodule
 
Simulation Log
 
ncsim> run
array[0] = ap<span class="code-hl-yellow">0</span>le, 2th index replaced by 0
array[1] = or0nge, 2th index replaced by 0
array[2] = pe0r, 2th index replaced by 0
array[3] = bl0eberry, 2th index replaced by 0
array[4] = le0on, 2th index replaced by 0
ncsim: *W,RNQUIE: Simulation is complete.
 

参考文献:
【1】https://www.chipverify.com/systemverilog/systemverilog-for-loop#example-3-adding-multiple-modifiers

发布了91 篇原创文章 · 获赞 7 · 访问量 5253

猜你喜欢

转载自blog.csdn.net/qq_43042339/article/details/104465271