首页 > 其他分享 >SystemVerilog -- 3.0 SystemVerilog Loops

SystemVerilog -- 3.0 SystemVerilog Loops

时间:2024-05-03 20:33:27浏览次数:19  
标签:begin counter -- Counter Loops World array Hello SystemVerilog

SystemVerilog Loops

What are loops ?

loop是一段不断执行的代码。条件语句通常包含在循环中,以便在条件变为真时终止。如果loop永远运行,那么模拟将无限期挂起。

下表给出了 SystemVerilog 中不同类型的循环构造。

\ \
forever Runs the given set of statements forever
repeat Repeats the given set of statements for a given number of times
while Repeats the given set of statements as long as given condition is true
for Similar to while loop, but more condense and popular form
do while Repeats the given set of statements atleast once, and then loops as long as condition is true
foreach Used mainly to iterate through all elements in an array

forever

这是一个无限循环。请注意,除非在模块中包含时间延迟以提前模拟时间,否则您的模拟将挂起。while (1) forever

module tb;
  // This initial block has a forever loop which will "run forever"
  // Hence this block will never finish in simulation
  initial begin
    forever begin
      #5 $display ("Hello World !");
    end
  end

  // Because the other initial block will run forever, our simulation will hang!
  // To avoid that, we will explicity terminate simulation after 50ns using $finish
  initial 
    #50 $finish;
endmodule
请注意,如果不调用模拟,模拟将无限期地继续进行。`$finish`

模拟日志

ncsim> run
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Simulation complete via $finish(1) at time 50 NS + 0

repeat

用于在块中重复语句一定次数。下面显示的示例将显示该消息5次,并继续执行其余代码。

module tb;
  // This initial block will exexute a repeat statement that will run 5 times and exit
  initial begin
    // Repeat everything within begin and 5 times and exit "repeat" block
    repeat (5) begin
      $display ("Hello World !");
    end
  end
endmodule

模拟日志

ncsim> run
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
ncsim: *W,RNQUIE: Simulation is complete.

while

如果你知道verilog/C,你就已经知道了。只要条件为true,它就会重复该快。计数器最初为零,并递增直到达到10。

module tb;
  bit clk;

  always #10 clk = ~clk;
  initial begin
    bit [3:0] counter;

    $display ("Counter = %0d", counter);      // Counter = 0
    while (counter < 10) begin
      @(posedge clk);
      counter++;
      $display ("Counter = %0d", counter);    // Counter increments
    end
    $display ("Counter = %0d", counter);      // Counter = 10
    $finish;
  end
endmoudule

模拟日志

ncsim> run
Counter = 0
Counter = 1
Counter = 2
Counter = 3
Counter = 4
Counter = 5
Counter = 6
Counter = 7
Counter = 8
Counter = 9
Counter = 10
Counter = 10
Simulation complete via $finish(1) at time 190 NS + 0

for

与verilog/C类似,这允许您在同一行中提起起始值、条件和增量表达式。

module tb;
  bit clk;
  
  always #10 clk = ~clk;
  initial begin
    bit [3:0] counter;

    $display ("Counter = %0d", counter);      // Counter = 0
    for (counter = 2; counter < 14; counter = counter + 2) begin
      @(posedge clk);
      $display ("Counter = %0d", counter);    // Counter increments
    end
    $display ("Counter = %0d", counter);      // Counter = 14
    $finish;
  end
endmodule

模拟日志

ncsim> run
Counter = 0
Counter = 2
Counter = 4
Counter = 6
Counter = 8
Counter = 10
Counter = 12
Counter = 14
Simulation complete via $finish(1) at time 110 NS + 0

do while

这将首先执行代码,然后检查条件以查看是否应再次执行代码。

module tb;
  bit clk;
  
  always #10 clk = ~clk;
  initial begin
    $display ("Counter = %0d", counter);      // Counter = 0
    do begin
      @(posedge clk);
      counter ++;
      $display ("Counter = %0d", counter);    // Counter increments
    end while (counter < 5);
    $display ("Counter = %0d", counter);      // Counter = 14
    $finish;
  end
endmodule

模拟日志

ncsim> run
Counter = 0
Counter = 1
Counter = 2
Counter = 3
Counter = 4
Counter = 5
Counter = 5
Simulation complete via $finish(1) at time 90 NS + 0

foreach

这最适合循环数组变量,因为您不必找到数组大小,将变量设置为从0开始到array_size-1,并在每次迭代时递增它。

module tb_top;
  bit [7:0] array [8];   // create a fixed size array

  initial begin
    // Assign a value to each loaction in the array
    foreach (array[index]) begin
      array[index] = index;
    end

    // Iterate through each location and print the value of current location
    foreach (array[index]) begin
      $display ("array[%0d] = 0x%0d", index, array[index]);
    end
  end
endmodule

模拟日志

ncsim> run
array[0] = 0x0
array[1] = 0x1
array[2] = 0x2
array[3] = 0x3
array[4] = 0x4
array[5] = 0x5
array[6] = 0x6
array[7] = 0x7
ncsim: *W,RNQUIE: Simulation is complete.

标签:begin,counter,--,Counter,Loops,World,array,Hello,SystemVerilog
From: https://www.cnblogs.com/sys-123456/p/18171543

相关文章

  • 容器化部署Tengine worker数量问题
    当容器化部署Tengine时,worker数量默认是cpu数量。https://tengine.taobao.org/document_cn/core_cn.html对应/etc/nginx/nginx.conf数量配置是4。容器中cpu数量是节点cpu数量,Nginx不需要这么多worker子进程,指定worker_processes是2即可。......
  • FreeRTOS任务通知
    FreeRTOS任务通知FreeRTOS新增了任务通知(TaskNotifictions)这个功能,可以使用任务通知来代替信号量、消息队列、事件标志组等这些东西。使用任务通知的话效率会更高,任务通知在FreeRTOS中是一个可选的功能,使用队列、信号量、事件标志组时都需另外创建一个结构体,通过中间的结......
  • 微机结构
    经过两个月的课程,陈老师为我们讲到微机结构,以下是我在课上学习到的内容。主要是总线、CPU、控制器、微机指令系统、存储器和I/O接口。计算机硬件的基本结构:运算器、存储器、控制器和输入输出设备。运算器和控制器合称为CPU(中央控制器),微机结构采用总线结构来实现相互之间的信息传......
  • 关于微机结构
    微机结构是计算机系统的重要组成部分,它包含了多个关键部分。中央处理器(CPU)是微机的核心,负责执行指令和运算。它由运算器、控制器等部分组成,决定了计算机的运算能力和效率。存储器是用于存储数据和程序的地方,包括随机存取存储器(RAM)和只读存储器(ROM)等,它们为计算机提供了临时和永久......
  • 关于操作系统
    操作系统是管理计算机硬件与软件资源的系统软件,它在计算机系统中起着至关重要的作用。操作系统负责协调和管理计算机的各种任务,如进程管理、内存管理、文件系统管理、设备管理等。它为用户和应用程序提供了一个友好的操作环境,使人们能够方便地使用计算机。同时,操作系统还确保了......
  • multiset用法总结
    multiset是库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存在重复的数。简单应用:通过一个程序来看如何使用multiset:#include<string>#include<iostream>#include<set>usin......
  • 基于SSM的仓库进销存系统毕业设计论文【范文】
    摘要随着信息技术的不断发展,企业对于仓储管理的要求日益提高。为了提升仓库管理的自动化和智能化水平,本研究设计并实现了一个基于Spring、SpringMVC和MyBatis(SSM)框架的在仓库进销存系统。该系统旨在为企业提供一个高效、准确、实时的库存管理解决方案,以优化库存控制,降低成本......
  • webapi中间件没有使用终结点中间件时的注意事项
    最小webapi默认的中间件配置是这样的app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();最小webapi没有使用app.UseRouting()和app.UseEndpoints。这种情况下我们添加的所有中间件其实都是位于终结点路由中间件EndpointRoutingMiddleware......
  • 南斗六星-系统设计
    需求的改进系统设计架构设计本次团队项目采用开发框架式SSM(Spring+SpringMVC+Mybatis)系统采用三层体系结构,分别是:表示层WEB应用层数据层利用SpringBoot进行项目的后端开发,前端使用React框架来开发工作架构大概是这样的:从买家的角度来看,系统应该是这样工作的:从......
  • uboot-学习笔记
    uboot引导程序的作用不同bootloader的对比系统启动自举过程阶段iROM读取流程......