SystemVerilog 基础语法
SystemVerilog 同时覆盖 RTL 设计与验证。验证代码常用 logic、动态数组、关联数组、类、约束和断言。
四态与二态类型
bit是二态类型,只保存0/1。logic是四态类型,可保存0/1/X/Z。$isunknown(expr)在表达式任一位包含X或Z时返回1。
logic [3:0] value = 4'b10x1;
initial $display("unknown=%0d", $isunknown(value)); // 1关联数组
关联数组适合稀疏索引。下面的索引依次为 1, 2, 4, 8:
module assoc_array_tb;
bit [63:0] assoc[bit [63:0]], idx = 1;
initial begin
repeat (8) begin
assoc[idx] = idx;
idx = idx << 1;
end
foreach (assoc[i])
$display("assoc[%h] = %h", i, assoc[i]);
if (assoc.first(idx))
do
$display("assoc[%h] = %h", idx, assoc[idx]);
while (assoc.next(idx));
void'(assoc.first(idx));
assoc.delete(idx);
end
endmodule在 QuestaSim 中运行
把文件加入 project 后执行
vlog -sv assoc_array_tb.sv,再执行vsim work.assoc_array_tb和run -all。work.assoc_array_tb只有在编译成功后才会出现。
断言小例子
assert property (@(posedge clk) req |=> gnt)
else $error("grant did not follow request");断言的周期含义与 物理时序约束 处在不同抽象层次。