System Verilog 可以说是Verilog+C的结合,在Verilog的基础上,引入了面向对象的思想,使得更有利于实现对电路的验证。因此很多时候需要对比Verilog,结合C,对比SV(System Verilog)的区别即可。
本博文简单介绍SV的任务函数的基本使用

System Verilog——任务和函数 Part-I

  • Verilog中的task
    • task的声明
    • task的使用实例
      • 实例1
      • 实例2
    • 关键字automatic
  • System Verilog中的task
  • System Verilog中task使用实例
    • 实例1
    • 实例2

Verilog中的task

主要参考菜鸟教程中的6.2 Verilog 任务,《Verilog HDL A guide to digital design and synthesis 2nd》这里简单进行总结。

task的声明

任务在模块中任意位置定义,并在模块内任意位置引用,作用范围也局限于此模块。
模块内子程序出现下面任意一个条件时,则必须使用任务而不能使用函数。
1)子程序中包含时序控制逻辑,例如延迟,事件控制等
2)没有输入变量(因为函数必须要有一个输入变量,任务可以没有)
3)没有输出或输出端的数量大于 1
(简单提一下,这里的函数就是一个输入,产生一个输出,因此没有输入,或者输出多余一个变量的时候,要将其划分为代码块儿时,必须使用任务)
Verilog 任务声明格式如下:

task [automatic] task_id ;port_declaration ;procedural_statement ;
endtask
|
task [automatic] task_id (port_declaration );procedural_statement ;
endtask

[]:表示可选 |:表示或
任务中使用关键字 input、output 和 inout 对端口进行声明。input 、inout 型端口将变量从任务外部传递到内部,output、inout 型端口将任务执行完毕时的结果传回到外部调用语句的相应变量里。任务可以调用其他任务或函数。
这里使用的变量类型的声明,是用来传输变量的,与module里的端口声明本质上有所区别,端口上的是用来与外部信号连接的。

进行任务的逻辑设计时,可以把 input 声明的端口变量看做 wire 型,把 output 声明的端口变量看做 reg 型。但是不需要用 reg 对 output 端口再次说明。

对 output 信号赋值时也不要用关键字 assign。为避免时序错乱,建议 output 信号采用阻塞赋值。
实例1,该实例1的功能是声明一个bitwise_oper的任务,对输入的两个16bit变量a,b进行按位与,按位或和按位异或操作,两个十六位的计算的输出分别为ab_and,ab_or,ab_xor。同时还使用了一个delay参数进行延时。

//define task bitwise_oper
task bitwise_oper;
output [15:0] ab_and, ab_or, ab_xor; //outputs from the task
input [15:0] a, b; //inputs to the task
begin
#delay ab_and = a & b;
ab_or = a | b;
ab_xor = a ^ b;
end
endtask

或者使用如下形式进行声明

//define task bitwise_oper
task bitwise_oper (output [15:0] ab_and, ab_or, ab_xor,
input [15:0] a, b);
begin
#delay ab_and = a & b;
ab_or = a | b;
ab_xor = a ^ b;
end
endtask

task的使用实例

实例1

module operation;
parameter delay = 10;
reg [15:0] A, B;
reg [15:0] AB_AND, AB_OR, AB_XOR;
always @(A or B) //whenever A or B changes in value
begin
//invoke the task bitwise_oper. provide 2 input arguments A, B
//Expect 3 output arguments AB_AND, AB_OR, AB_XOR
//The arguments must be specified in the same order as they
//appear in the task declaration.bitwise_oper(AB_AND, AB_OR, AB_XOR, A, B);
end
//define task bitwise_oper
task bitwise_oper;
...
endtask

实例2

同样可以对模块内定义的变量进行赋值。如下实例可以对clock进行赋值,注意,该任务定义没有输入输出。

//Define a module that contains the task asymmetric_sequence
module sequence;
reg clock;
initial
init_sequence; //Invoke the task init_sequence
always
begin
asymmetric_sequence; //Invoke the task asymmetric_sequence
end
//Initialization sequence
task init_sequence;
begin
clock = 1'b0;
end
endtask
//define task to generate asymmetric sequence
//operate directly on the clock defined in the module.
task asymmetric_sequence;
begin
#12 clock = 1'b0;
#5 clock = 1'b1;
#3 clock = 1'b0;
#10 clock = 1'b1;
end
endtask
endmodule

关键字automatic

任务通常是静态的。所有声明的项都是静态分配的,它们在并发执行的任务的所有使用中共享。因此,如果从代码中的两个位置同时调用任务,则这些任务调用将对相同的任务变量进行操作。这种操作的结果很可能不正确。
为了避免此问题,在任务关键字前面添加了关键字automatic,以使任务重新进入。这种任务称为自动任务。自动任务中声明的所有项都会为每次调用动态分配。每个任务调用都在一个独立的空间中运行。因此,任务调用在任务变量的独立副本上运行。这将导致正确的操作。如果有可能从代码中的两个位置同时调用任务,建议使用自动任务。
实例略

System Verilog中的task

我们这里列出两者的不同之处,其他的与Verilog保持一致。

  • 默认端口方向:任何端口都视为输入,除非声明为其他类型。
    以下是端口类型
    input:copy value in at beginning(在开始时赋值)
    output copy value out at end
    inout:copy in at begin and out at end
    ref:pass reference
  • 默认数据类型:除非声明,否则都是logic类型
  • 无需使用begin end
  • 通过使用return语句变量,可以在endtask之前终止任务。
  • 变量:Systemverilog允许使用本地静态变量或本地动态变量。
  • 时间:Systemverilog允许一个任务是静态或者是自动的
  • 线网:线网型数据类型不能在端口列表中使用

System Verilog中task使用实例

实例1

module task_intro ();
initial begin#1 doInit(4,5);#1 doInit(9,6);#1 $finish;
end
task doInit (input bit [3:0] count, delay); automatic reg [7:0] a;if (count > 5) begin$display ("@%g Returning from task", $time);return;end#(delay) $display ("@%g Value passed is %d", $time, count);
endtask
endmodule

仿真输出为

@6 Value passed is  4
@7 Returning from task

实例2

module traffic_lights;
logic clock, red, amber, green;
parameter   on = 1, off = 0, red_tics = 350,amber_tics = 30, green_tics = 200;
// initialize colors
initial red = off;
initial amber = off;
initial green = off;
always begin // sequence to control the lightsred = on; // turn red light onlight(red, red_tics); // and wait.green = on; // turn green light onlight(green, green_tics); // and wait.amber = on; // turn amber light onlight(amber, amber_tics); // and wait.
end
// task to wait for 'tics' positive edge clocks
// before turning 'color' light off
task light (output color, input [31:0] tics);repeat (tics) @ (posedge clock);color = off; // turn light off.
endtask: light
always begin // waveform for the clock#100 clock = 0;#100 clock = 1;
end
endmodule: traffic_lights

System Verilog——任务和函数 Part-I相关推荐

  1. System Verilog自学笔记专栏概述博文目录

    本文用于梳理学习笔记目录下的博文笔记,概述会有笔者的System Verilog的学习反思以及相关随笔,望有同学发现问题及时提出~感谢 概述随笔 了解基本的概念,框架后,开始使用例子进行学习语言.需要 ...

  2. (119)System Verilog 父类与子类对象复制(自定义函数)详解

    (119)System Verilog 父类与子类对象复制(自定义函数)详解 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog 父类与子 ...

  3. (118)System Verilog 父类与子类对象复制(copy函数)详解

    (118)System Verilog 父类与子类对象复制(copy函数)详解 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog 父类与 ...

  4. (04)System Verilog 利用函数通用总线激励驱动方法

    (04)System Verilog 利用函数通用总线激励驱动方法 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog 利用函数通用总线激 ...

  5. System verilog随机系统函数$randomize使用方法

    1.1 System verilog随机系统函数$randomize使用方法 1.1.1 本节目录 1)本节目录: 2)本节引言: 3)FPGA简介: 4)System verilog随机系统函数$r ...

  6. System verilog随机系统函数$urandom_range使用方法

    1.1 System verilog随机系统函数$urandom_range使用方法 1.1.1 本节目录 1)本节目录: 2)本节引言: 3)FPGA简介: 4)System verilog随机系统 ...

  7. System verilog随机系统函数$urandom使用方法

    1.1 System verilog随机系统函数$urandom使用方法 1.1.1 本节目录 1)本节目录: 2)本节引言: 3)FPGA简介: 4)System verilog随机系统函数$ura ...

  8. System verilog随机系统函数$random使用方法

    1.1 System verilog随机系统函数$random使用方法 1.1.1 本节目录 1)本节目录: 2)本节引言: 3)FPGA简介: 4)System verilog随机系统函数$rand ...

  9. System Verilog学习小结

    System Verilog课程小结 第一周 问题1:设计人员和验证人员他们的协作关系体现在哪些地方? 1.设计人员和验证人员都需要围绕功能描述文档开展工作 2.设计只有经过充分量化验证,才有信心去流 ...

最新文章

  1. 推荐一个个人感觉比较有吸引力的网站
  2. 微服务之间调用经过网关吗_微服务网关入门
  3. [转]thinkphp 模板显示display和assign的用法
  4. 【错误记录】编译 Android 版本的 ijkplayer 报错 ( ./init-android.sh: 第 37 行: cd: android/contrib/: 没有那个文件或目录 )
  5. php sockent通信
  6. HTTP协议(学习笔记)
  7. Mac文件夹图标颜色自定义工具Color Folder
  8. 网站跨域访问解决方法
  9. 集体智慧编程 - 读书笔记
  10. Redis迭代查询详解及其使用:Scan命令、Sscan命令、Hscan命令、Zscan命令
  11. SVN分支/主干Merge操作小记
  12. 如何在各种非三星电脑上安装Samsung Notes三星笔记
  13. 制坯系列-Golang专题-chan
  14. CISP-PTE证书含金量大吗?一文看懂CISP-PTE值不值得考
  15. 【绝对好用】java poi 导入、导出excel(支持xsl、xslx)
  16. C语言学习笔记-有符号数和无符号数相加的问题
  17. 关于时间复杂度什么是时间复杂度
  18. Phonics 自然拼读法 ai ay ee ea ey ie igh oa ow ui ue 元音字母组合 Teacher:Lamb
  19. DB2使用特殊分隔符处理数据
  20. IText导出PDF添加图片,解决中文问题

热门文章

  1. Android Studio升级到3.0,抛出Aapt2Exception异常
  2. 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
  3. React Native图片缓存解决方案
  4. -1在numpy重塑中是什么意思?
  5. 在Java 8中,有没有一种简洁的方法可以迭代带有索引的流?
  6. python setup.py卸载
  7. oracle中sql语句 日期加减,SQL语句里对日期进行相加减
  8. 单麦克纳姆轮受力分析
  9. java vo转map_Java后端必备的开发规范
  10. html5a链接_html 超链接(a)详细讲解