SystemVerilog常用关键词与函数

SystemVerilog作为Verilog的扩展,引入了许多新的语言特性和验证方法。为了方便查阅,下面按类别将核心的语言要素整理出来。可以先通过下面的表格对整体框架有个了解,再在后续章节中查阅具体内容。

分类 核心内容 快速应用场景
🔑 基础关键词 always_comb, logic, enum, interface, program 定义组合/时序逻辑回路、4状态数据类型、创建有限状态机、封装模块间通信端口、构建测试平台结构
🔧 系统任务/函数 $display, $monitor, $time, $finish 仿真信息输出、监控信号变化、获取仿真时刻、控制仿真流程的终止
🎲 随机化 rand / randc 约束随机值生成、启用/禁用随机变量
📜 断言 (SVA) assert, assume, cover 检查设计属性是否满足预期、为形式验证提供约束、监控功能覆盖点是否被触发
🔄 进程与线程 fork / join, disable 创建和管理多个并发执行的线程、根据条件终止一个命名块或任务
🔌 DPI接口 import "DPI-C" 调用C语言编写的函数或导出SystemVerilog函数供C语言使用

🔑 1. 基础关键词 (Keywords)

SystemVerilog的关键字(保留字)是语言的基石,用于定义各种硬件结构和行为。这部分源自IEEE Std 1800-2012标准,所有关键字均为小写。

  • 数据类型与结构化类型bit, logic, reg, int, integer, longint, shortint, byte, enum, struct, union, packed

  • 硬件结构描述module, endmodule, interface, endinterface, program, endprogram, package, endpackage, primitive, endprimitive

  • 过程块与逻辑行为always, always_comb, always_ff, always_latch, initial, final

  • 并发与并行编程fork, join, join_any, join_none, begin, end

  • 控制流与循环if, else, case, casex, casez, for, while, repeat, foreach, break, continue, return

  • 类与面向对象编程class, endclass, new, this, extends, super, virtual, pure, typedef

  • 随机化与约束rand, randc, constraint, solve, dist, randcase, randsequence

  • 断言与形式验证assert, assume, cover, property, endproperty, sequence, expect

  • 其他重要关键字automatic, static, local, protected, extern, import, export, bind, alias

完整关键词列表 (1800-2012版):
accept_on, alias, always, always_comb, always_ff, always_latch, and, assert, assign, assume, automatic, before, begin, bind, bins, binsof, bit, break, buf, bufif0, bufif1, byte, case, casex, casez, cell, chandle, checker, class, clocking, cmos, config, const, constraint, context, continue, cover, covergroup, coverpoint, cross, deassign, default, defparam, design, disable, dist, do, edge, else, end, endcase, endchecker, endclass, endclocking, endconfig, endfunction, endgenerate, endgroup, endinterface, endmodule, endpackage, endprimitive, endprogram, endproperty, endspecify, endsequence, endtable, endtask, enum, event, eventually, expect, export, extends, extern, final, first_match, for, force, foreach, forever, fork, forkjoin, function, generate, genvar, global, highz0, highz1, if, iff, ifnone, ignore_bins, illegal_bins, implements, implies, import, incdir, include, initial, inout, input, inside, instance, int, integer, interconnect, interface, intersect, join, join_any, join_none, large, let, liblist, library, local, localparam, logic, longint, macromodule, matches, medium, modport, module, nand, negedge, nettype, new, nexttime, nmos, nor, noshowcancelled, not, notif0, notif1, null, or, output, package, packed, parameter, pmos, posedge, primitive, priority, program, property, protected, pull0, pull1, pulldown, pullup, pulsestyle_ondetect, pulsestyle_onevent, pure, rand, randc, randcase, randsequence, rcmos, real, realtime, ref, reg, reject_on, release, repeat, restrict, return, rnmos, rpmos, rtran, rtranif0, rtranif1, s_always, s_eventually, s_nexttime, s_until, s_until_with, scalared, sequence, shortint, shortreal, showcancelled, signed, small, soft, solve, specify, specparam, static, string, strong, strong0, strong1, struct, super, supply0, supply1, sync_accept_on, sync_reject_on, table, tagged, task, this, throughout, time, timeprecision, timeunit, tran, tranif0, tranif1, tri, tri0, tri1, triand, trior, trireg, type, typedef, union, unique, unique0, unsigned, until, until_with, untyped, use, uwire, var, vectored, virtual, void, wait, wait_order, wand, weak, weak0, weak1, while, wildcard, wire, with, within, wor, xnor, xor

8†L11-L41\]\[9†L4-L29


⚙️ 2. 系统任务与函数 ($ Tasks and Functions)

系统任务和函数以$开头,在仿真中执行特定操作。下表列出了常用任务/函数及其简要说明。

分类 名称 说明
显示与监控 $display 打印信息,自动换行
$write 打印信息,不换行
$strobe 在仿真结束时刻打印信息
$monitor 监控变量变化并打印
$monitoron / $monitoroff 开关监控
仿真控制 $stop 暂停仿真
$finish 结束仿真
$exit 退出仿真
时间/值获取 $time 返回当前仿真时间
$realtime 返回实数时间
$random 返回32位有符号随机数
$urandom 返回32位无符号随机数
文件I/O $fopen 打开文件
$fclose 关闭文件
$fread 从文件读取数据
$readmemb / $readmemh 将文件数据读入存储器
其他 $size 返回数组/向量的位数
$countones 统计信号中'1'的数量
$assertoff 禁用断言

注意:上述列表为常用子集,更多用法请参考语言标准手册(如IEEE Std 1800-2017第20、21章)。


🎲 3. 随机化 (Randomization)

随机化是SystemVerilog约束随机验证(CRV)的核心,主要包括生成随机值和控制随机过程。

  • 随机变量修饰符rand (每次随机化在取值范围内均匀分布) 与 randc (该变量所有可能值都被遍历过后才允许重复)

  • 对象随机化方法randomize() (对所有rand/randc变量求解随机值), pre_randomize() (在randomize()前自动调用,可用于设置非随机变量), post_randomize() (在randomize()后自动调用,可用于打印信息或后处理)

  • 内嵌约束randomize() with {...}

  • 作用域随机化函数std::randomize()

  • 概率分布函数$urandom_range(), $dist_uniform(), $dist_normal(), $dist_exponential(), $dist_poisson()

  • 随机过程控制rand_mode() 控制某个变量是否参与随机化


📜 4. 断言 (Assertions - SVA)

SystemVerilog断言(SVA)用于描述设计属性,供仿真和形式验证工具检查。

类型 关键字 说明
基础断言 assert 检查一个属性是否永远成立
assume 为验证设定假设
cover 监控属性是否被触发
序列操作符 property ... endproperty 定义一个属性
sequence ... endsequence 定义一个序列
@(posedge clk) 指定时钟事件
##n 固定周期延迟
` ->/
内置系统函数 $rose() 检测信号上升沿
$fell() 检测信号下降沿
$stable() 检查信号是否保持稳定
$past() 获取过去第 N 个周期的值
$changed() 检测信号是否发生变化
$onehot(), $onehot0() 检查总线是否为一热码
$isunknown() 检查是否存在 x 或 z 状态

🔄 5. 进程控制 (Processes)

  • 并发执行fork ... join, join_any, join_none

  • 顺序执行begin ... end

  • 终止进程disable


🔌 6. 直接编程接口 (DPI - Direct Programming Interface)

DPI是SystemVerilog与C语言等外部编程语言之间的桥梁,使开发者能够整合和复用已有的C/C++模型或函数库。

  • 导入函数 (Import) :用于调用C语言实现的函数。语法示例:import "DPI-C" function void my_c_func(input int a);

  • 导出函数 (Export) :用于将SystemVerilog函数导出,供C语言调用。语法示例:export "DPI-C" function my_sv_func;


⚠️ 注意事项

  1. 仿真与综合的差异 :请注意,上述语言特性并非都可用于最终的硬件综合。always_combunique/priority case等关键词和硬件结构可用于综合,但大多数系统任务/函数(如$display)、随机化方法和DPI主要用于仿真验证,不可综合。

  2. 权威参考来源 :以上所有关键词和功能均定义于IEEE Std 1800标准。最新的标准文档 IEEE 1800-2023 是你工作中最准确、详尽的终极参考。

相关推荐
啄缘之间12 天前
8.【学习】工业级详细接口约束&覆盖率
开发语言·笔记·学习·uvm·sv
Ether IC Verifier12 天前
SystemVerilog 数据类型详解
php·systemverilog·uvm·ic验证
liuluyang5301 个月前
UVM 工厂机制 完整可编译运行 Demo
uvm·uvm工厂机制
liuluyang5301 个月前
UVM工厂机制
uvm·工厂机制
liuluyang5301 个月前
UVM工厂机制(二)
uvm·工厂机制
liuluyang5301 个月前
SV主要关键词详解
fpga开发·uvm·sv
liuluyang5302 个月前
clk_mux_seq sv改进
fpga开发·uvm
谷公子的藏经阁2 个月前
DVCon 2025 论文精华导读及下载链接
ai·论文·systemverilog·uvm·dvcon
蓝天下的守望者4 个月前
SystemVerilog中 `timescale的使用问题
systemverilog·uvm·vcs