SystemVerilog 控制流语句

news/2024/5/19 18:28:33 标签: Systemverilog
  • unique-if/unique0-if

  • 对于unique-if ,如果condition没有一个匹配且没有加else语句,则会报告一个错误;如果超过1个condition匹配,也会报告错误;
    unique0-if与unique-if的不同之处在于,如果没有一个condition匹配也不会报错

    module tb;
      int x = 4;
     
        initial begin
            // This if else if construct is declared to be "unique"
        // Error is not reported here because there is a "else"
            // clause in the end which will be triggered when none of
            // the conditions match
          unique if (x == 3) 
              $display ("x is %0d", x);
          else if (x == 5)
              $display ("x is %0d", x);
            else
              $display ("x is neither 3 nor 5");      
     
            // When none of the conditions become true and there
            // is no "else" clause, then an error is reported
          unique if (x == 3) 
              $display ("x is %0d", x);
          else if (x == 5)
              $display ("x is %0d", x);
        end
    endmodule
    

  • priority-if

  • 果condition没有一个匹配且没有加else语句,则会报告一个错误;如果有多个condition匹配,排在前面的优先级最高,且执行完最高的优先级后退出选择
     
    module tb;
    	int x = 4;
      
      	initial begin    
          	// Exits if-else block once the first match is found
          	priority if (x == 4)
          		$display ("x is %0d", x);
          else if (x != 5)
          		$display ("x is %0d", x);
      	end
    endmodule	
    
  • unique,unique0 case

  • 对于unique case ,如果case没有一个匹配,则会报告一个错误;如果超过1个condition匹配,也会报告错误,同时执行第一个匹配到的case;
    unique0-case与unique-case的不同之处在于,如果没有一个case匹配也不会报错
    module tb;
      bit [1:0]   abc;
     
      initial begin
        abc = 1;
     
        // None of the case items match the value in "abc"
        // A violation is reported here
        unique case (abc)
          0 : $display ("Found to be 0");
          2 : $display ("Found to be 2");
          //使用unique case此时多加default
        endcase
      end
    endmodule
    
  • priority case

  • 至少有一个条件选项的值与条件表达式匹配。如果有多个条件选项的值与条件表达式匹配,必须执行第一个匹配分支
	bit [2:0] a;
	priority case(a) // 值4,5,6,7会引起一个运行时警告
	   3'b00?: $display("0 or 1");
	   3'b0??: $display("2 or 3");
	endcase

forever

在SV中,always块不能存在于类和其他过程块中,所以用forever代替。格式如下:

	always begin
    // Multiple statements
  	end
	class Monitor;
	  virtual task run();
	    forever begin
	      @(posedge vif.clk);
	      if (vif.write & vif.sel)
	        // Capture write data
	      if (!vif.write & vif.sel)
	        // Capture read data
	    end
	  endtask
	endclass
	 
	module tb;
	  Monitor mon;
	 
	  // Start the monitor task and allow it to continue as 
	  // long as there is activity on the bus
	  initial begin
	    fork
	      mon.run();
	    join_none
	  end
	endmodule

        为了防止循环体在delta时间内产生无限循环,导致仿真挂起,forever循环体内部必须带有时序控制或者disable语句

break

类C语言的break声明立即结束循环操作。循环不会重新执行,除非执行流程重新到达循环的起点

1 // find first bit set within a range of bits
 2 always_comb begin
 3     first_bit = 0;
 4     for (int i=0; i<=63; i=i+1) begin
 5         if (i < start_range) continue;
 6         if (i > end_range) break; // exit loop
 7         if ( data[i] ) begin
 8             first_bit = i;
 9             break; // exit loop
10         end
11     end // end of the loop
12     ... // process data based on first bit set
13 end  

continue

        类C语言的continue声明跳转到循环的末尾并执行循环的控制。使用continue声明时,不需要对代码添加命名的begin…end块,而这在使用disable声明时是必要的。

1 logic [15:0] array [0:255];
2 always_comb begin
3     for (int i = 0; i <= 255; i++) begin : loop
4         if (array[i] == 0)
5           continue; // skip empty elements
6         transform_function(array[i]);
7     end // end of loop
8 end

return

        system verilog增加了类C语言的return声明,用于从一个非void函数中返回数值或者从一个void函数或任务返回。return声明可以在任务或函数执行流程的任意一点执行。当return声明执行后,任务或者函数立即退出而不需要执行到任务或者函数的末尾

1 task add_up_to_max (input [ 5:0] max,
2                     output [63:0] result);
3     result = 1;
4     if (max == 0) return; // exit task
5     for (int i=1; i<=63; i=i+1) begin
6         result = result + result;
7         if (i == max) return; // exit task
8     end
9 endtask

disable

        disable :用于在多进程的场景下终止一个或多个进程
        disable语句可以用在task或者块中去终止指定的task或块,包括终止disable语句所在的块或者task。disable也可以用在function中去终止task或者块,但不能用于终止function。当在function中用dsiable语句终止了一个task或者块,而这个task或者块刚好又是这个function的caller, 这种情况的结果是未知的。

task proc_a;
    begin
    ...
    ...
    if (a == 0)
        disable proc_a; // return if true
    ...
    ...
    end
endtask

event

        event是一个静态对象句柄,用于在两个或多个同时活动的进程之间进行同步。 一个进程将触发事件,另一个进程将等待事件。

【1】可以赋值或与其他事件变量进行比较
【2】可以赋值为空
【3】当赋值给另一个事件时,两个变量都指向同一个同步对象
【4】可以传递给队列,函数和任务

如何触发并等待事件?

【1】可以使用->或->>运算符触发命名事件
【2】进程可以使用@运算符或.triggered等待事件

module tb;
 
  //创建一个事件变量,进程可用于触发和等待
  event event_a;
 
  // 线程1:使用“->”运算符触发事件
  initial begin
    #20 ->event_a;
    $display ("[%0t] Thread1: triggered event_a", $time);
  end
 
  // 线程2:使用“ @”运算符等待事件
  initial begin
    $display ("[%0t] Thread2: waiting for trigger ", $time);
    @(event_a);
    $display ("[%0t] Thread2: received event_a trigger ", $time);
  end
 
  // 线程3:使用“ .triggered”等待事件
  initial begin
    $display ("[%0t] Thread3: waiting for trigger ", $time);
    wait(event_a.triggered);
    $display ("[%0t] Thread3: received event_a trigger", $time);
  end
endmodule  


http://www.niftyadmin.cn/n/5044336.html

相关文章

人工智能机器学习-飞桨神经网络与深度学习

飞桨神经网络与深度学习-机器学习 目录 飞桨神经网络与深度学习-机器学习 1.机器学习概述 2.机器学习实践五要素 2.1.数据 2.2.模型 2.3.学习准则 2.4.优化算法 2.5.评估标准 3.实现简单的线性回归模型 3.1.数据集构建 3.2.模型构建 3.3.损失函数 3.4.模型优化 3…

2.(vue3.x+vite)组件注册并调用

前端技术社区总目录(订阅之前请先查看该博客) 关联博客 1.(vue3.x+vite)封装组件 一:umd调用方式 1:引入umd.js <script src="./public/myvue5.umd.js"></script>2:编写代码调用 (1)umd方式,根据“5

php如何把数组元素反转-array_reverse使用要点

在PHP中&#xff0c;你可以使用array_reverse()函数来反转数组的元素顺序。该函数会返回一个新的数组&#xff0c;其中的元素顺序与原始数组相反。 以下是使用array_reverse()函数反转数组的示例代码&#xff1a; <?php $originalArray array(a, b, c, d); $reversedArr…

高压放大器电源有什么作用和用途

高压放大器是一种专门用于放大高压信号的电子设备。它可以将低幅度的输入信号放大成高幅度的输出信号&#xff0c;用于驱动高压负载或处理高压信号。然而&#xff0c;高压放大器需要特定的电能来运行&#xff0c;而这就是电源的作用。 高压放大器电源的主要作用是为高压放大器提…

商品信息底部工具类实现

<!-- 商品轮播图开始 --> <view class="product_swiper"><swiper autoplay circular indicator-dots><swiper-item wx:for="{{productObj.productSwiperImageList}}"wx:key="id"><navigator><image src="…

mysqld_exporter监控MySQL服务

一、MySQL授权 1、登录MySQL服务器对监控使用的账号授权 CREATE USER exporterlocalhost IDENTIFIED BY 123456 WITH MAX_USER_CONNECTIONS 3; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO exporterlocalhost; flush privileges;2、上传mysqld_exporter安装包&#…

mysql如果存在一行数据,主库和从库主键相同而其他列值不同,源端Update或者delete该行,从库会update和delete这一行吗

从库会update或delete&#xff0c;而且会update成和主库完全相同的一行。这一点主从复制和CDC复制的一样的&#xff0c;都是以主键为查询基准的。复制程序很聪明&#xff0c;如果一张表存在主键的话&#xff0c;update或着delete该表的话&#xff0c;从库的复制程序会通过主键索…

有可以放到井下进行采样的自动采样器吗

利用自动采样器进行水样采集可以说节省很大的人力物力&#xff0c;但是有时为了采到更具代表性的水样&#xff0c;同样我们也需要对沟渠、深井、排污口等特殊场景进行采样。 像这些狭小的空间领域采样就有点困难&#xff0c;对现场工作人员就带来了一些难题。 所以也需要一款可…