2 SystemVerilog Control Flow

目录

2 SystemVerilog Control Flow

2.1SystemVerilog Loops

2.1.1什么是循环?

2.1.2 forever

2.1.4 while与do while

2.1.5 for

2.1.6 foreach

2.2 SystemVerilog 'break' and 'continue'

2.3 SystemVerilog 'unique' and 'priority' if-else

2.3.1unique-if

2.3.2priority-if

2.4 case语句

2.5 Verilog Blocking & Non-Blocking

2.5.1Blocking

2.5.2Non-Blocking

2.6 SystemVerilog Event

2.6.1@(event_name)与.triggered之间的区别是什么?

2.6.2 Merging Events

2.7 SystemVerilog Function

2.7.1 Verilog Function

2.7.2 ANSI-C style declaration

2.7.3 Using declarations and directions

2.7.4按值传递参数

2.7.5按引用传递参数

2.8 Task

2.8.1 Static Task

2.8.2 Automatic Task

2.8.3 Global tasks

2.8.3 Task与Function 之间的区别

2.8.4 Disable Task


2 SystemVerilog Control Flow

2.1SystemVerilog Loops

2.1.1什么是循环?

循环是一遍又一遍的执行代码,当条件变为true时可以停止,如果循环永远进行,那模拟将无限期挂起。下表给出了Systemverilog中不同类型的循环结构:

2.1.2 forever

2.1.2.1语法

forever
    // single statement
forever begin
    // Multiple ststement
end

类似于下面Verilog中显示的代码。两者都运行无限的模拟时间,并且在其中存在一个延时元素很重要。

class Monitor;
    virtual task run();
        forever begin
            @(posedge vif.clk);
            if (vif.write & vif.sel)
            if (!vif.write & vif.sel)

        end
    endtask
endclass
module tb;
    Monitor mon;
    initial begin
        fork
            mon.run();
        join_none
    end
endmodule

2.1.3 repeat

一组给定的语句可以使用构造执行 N 次。

module tb;
    initial begin
        repeat (5) begin
            $display ("Repeat this statement");
        end
    end
endmodule
_______________________________________
Repeat this statement
Repeat this statement
Repeat this statement
Repeat this statement
Repeat this statement
_______________________________________

2.1.4 while与do while

两者都是循环构造,只要条件给定为真,他们就会执行给定的语句集合。

while:循环首先检查条件是否为真,如果条件为真,则执行语句。如果条件被证明是假的,那么循环就在那里结束

do while:循环首先执行一次语句,然后检查条件是否为真。如果条件为真,则执行语句集合,直到条件变为假。如果条件为假,那么循环再此结束。

因此两者之间的区别在于循环至少执行一次语句集。

2.1.4.1语法

while (<condition>) begin
// Multiple statements
end
_______________________________________
do begin
// Multiple statements
end while (<condition>);
_______________________________________

2.1.4.2示例

// while循环:
module tb;
    initial begin
        int cnt = 0;
        while (cnt < 5) begin
            $display("cnt = %0d", cnt);
            cnt++;
        end
    end
endmodule
/*sumilation log
cnt = 0
cnt = 1
cnt = 2
cnt = 3
cnt = 4
*/
_______________________________________
module tb;
    initial begin
        int cnt = 0;
        do begin
            $display("cnt = %0d", cnt);
            cnt++;
        end while (cnt < 5);
    end
endmodule
/*sumilation log
cnt = 0
cnt = 1
cnt = 2
cnt = 3
cnt = 4
*/
_______________________________________
module tb;
    initial begin
        int cnt = 0;
        do begin
            $display("cnt = %0d", cnt);
            cnt++;
        end while (cnt == 0);
    end
endmodule
/*sumilation log
cnt = 0
*/
_______________________________________

2.1.5 for

SystemVerilog 中的循环多次重复一组给定的语句,直到不满足 给定的表达式。与所有其他过程块一样,循环需要其中的多个语 句被 and 关键字括起来。

2.1.5.1语法

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

1. 初始化影响循环运行次数的变量

2. 在执行循环之前,请检查条件是否为 true

3. 修饰符在每次迭代结束时执行,并跳转到步骤 2

for ( [initialization]; <condition>; [modifier])
// Single statement
for ( [initialization]; <condition>; [modifier])
// Multiple statements
end

2.1.5.2 数组迭代

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

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
//
array[0] = apple
array[1] = orange
array[2] = pear
array[3] = blueberry
array[4] = lemon

2.1.5.3多次初始化

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 repaced by 0",i,array[i],j);
        end
    end
endmodule
//
array[0] = ap0le, 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

2.1.5.4添加多个修饰符

module tb;
    string    array[5] = '{'apple', 'orange', 'pear','blueberry','lemon'};
    initial begin
        for(int i = 0,j = array[i].len()-1;i<$size(array);i++,j--)begin
            array[i][j] = "0";
        $display("array[%0d] = %s,%0dth index repaced by 0",i,array[i],j);
        end
    end
endmodule

array[0] = apll0, 4th index replaced by 0
array[1] = ora0ge, 3th index replaced by 0
array[2] = pe0r, 2th index replaced by 0
array[3] = b0ueberry, 1th index replaced by 0
array[4] = 0emon, 0th index replaced by 0

2.1.6 foreach

SystemVerilog 数组是允许在单个变量中存储多个值的数据结 构。循环仅用于迭代此类数组,是最简单的方法。

2.1.6.1语法

循环从 0 开始遍历每个索引。如果循环中有多个语句,则必须 像所有其他过程块一样用 and 关键字将它们括起来

foreach(<variable>[<iterator>])
    // Single statement
foreach(<variable>[<iterator>]) begin
    // Multiple statements
end

2.1.6.2 一维数组

module
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SystemVerilog是一种硬件描述语言(HDL),它是对Verilog的扩展和增强。SystemVerilog提供了一些新的特性和功能,使得硬件设计和验证更加方便和高效。以下是SystemVerilog的一些主要特点: 1. 对象导向编程(OOP):SystemVerilog引入了类和对象的概念,使得硬件设计和验证可以更加模块化和可重用。通过使用类和对象,可以更好地组织和管理设计和验证代码。 2. 事务级建模(TLM):SystemVerilog引入了TLM的概念,使得设计和验证可以在更高的抽象级别上进行。TLM允许设计和验证人员以事务的方式进行交互,从而提高了开发效率和代码可读性。 3. 强大的数据类型支持:SystemVerilog提供了丰富的数据类型,包括整数、实数、枚举、结构体等。这些数据类型的支持使得设计和验证可以更加灵活和精确。 4. 接口和端口:SystemVerilog引入了接口和端口的概念,使得设计和验证可以更好地进行模块化和连接。通过使用接口和端口,可以更好地定义模块之间的通信和交互。 5. 断言和约束:SystemVerilog引入了断言和约束的概念,使得验证可以更加全面和准确。通过使用断言和约束,可以对设计进行静态和动态的验证,从而提高了验证的可靠性和效率。 6. UVM集成:SystemVerilog与UVM(通用验证方法)紧密集成,使得验证工程师可以更好地使用UVM进行验证。UVM提供了一套验证方法和工具,可以帮助验证工程师更好地组织和管理验证环境。 总之,SystemVerilog是一种功能强大的硬件描述语言,它提供了丰富的特性和功能,可以帮助设计和验证人员更好地进行硬件开发和验证工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值