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  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SystemVerilog手册是一份详尽的参考资料,包含了SystemVerilog编程语言的规范和用法。SystemVerilog是一种硬件描述语言,主要用于编写数字电路的设计、验证和仿真。 SystemVerilog手册包含了SystemVerilog语言的各种构造,例如模块、端口、信号、变量、数据类型、运算符、语句和过程等,并且详细阐述了这些构造的语法和用法,包括各种高级特性,如生成块、宏、接口和类的继承等。 此外,SystemVerilog手册还包含了SystemVerilog编程中的一些常见技巧和最佳实践,并提供了大量的示例代码和应用案例。手册中还附带了SystemVerilog编译和仿真工具的用户手册,让读者能够更好地了解和使用这些工具。 SystemVerilog手册是SystemVerilog编程语言的权威参考资料,无论是初学者还是有经验的工程师,都可以从中获得有用的信息和知识,并在设计、验证和仿真数字电路时提高自己的能力和效率。 ### 回答2: SystemVerilog手册是一本用于学习SystemVerilog语言的参考书,它提供了广泛的语言概述、规范和示例代码,对于想要深入学习SystemVerilog的工程师以及入门者都非常有用。 手册的主要内容包括SystemVerilog语言的基本概念和语法、数据类型和运算符、控制流语句、结构体和联合体、接口和继承等。手册还介绍了SystemVerilog的测试工具和如何使用它们进行验证。 在手册中,还包括了SystemVerilog的各种特性和扩展,如SystemVerilog Assertions(SVA)、Constrained Random Verification(CRV)、Coverage、Direct Programming Interface(DPI)等。这些特性和扩展都可以帮助工程师更好地进行设计和验证工作。 总之,SystemVerilog手册不仅能够帮助工程师快速入门SystemVerilog语言,而且还能够提供深入到高级语言特性的学习资料,让读者能够更加灵活地进行工程设计和验证。 ### 回答3: SystemVerilog是一种硬件设计语言,旨在帮助工程师们更好地设计复杂的数字电路系统。SystemVerilog手册是工程师们学习和使用SystemVerilog语言的重要参考资料之一。 SystemVerilog手册包含了SystemVerilog语言的完整文档,其中包括语言的语法、数据类型、操作符、控制流、任务和函数、模块、接口、生成块、时序控制等内容。此外,手册还提供了大量的代码示例和应用场景,使读者更好地理解和掌握SystemVerilog语言。 SystemVerilog手册不仅对于数字电路设计工程师是非常有用的参考资料,对于其他领域的工程师也同样有着重要的意义。例如,软件工程师通过学习SystemVerilog语言可以更好地理解底层的硬件设计原理,并且可以实现软硬件协同设计,提高系统的性能和可靠性。 总之,SystemVerilog手册是工程师们系统学习和使用SystemVerilog语言的必备参考资料,对于提高硬件设计和软硬件协同设计的能力有着重要的意义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值