SystemVerilog 基础语法

SystemVerilog 同时覆盖 RTL 设计与验证。验证代码常用 logic、动态数组、关联数组、类、约束和断言。

四态与二态类型

  • bit 是二态类型,只保存 0/1
  • logic 是四态类型,可保存 0/1/X/Z
  • $isunknown(expr) 在表达式任一位包含 XZ 时返回 1
logic [3:0] value = 4'b10x1;
initial $display("unknown=%0d", $isunknown(value)); // 1

关联数组

关联数组适合稀疏索引。下面的索引依次为 1, 2, 4, 8

assoc_array_tb.sv
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_tbrun -allwork.assoc_array_tb 只有在编译成功后才会出现。

断言小例子

assert property (@(posedge clk) req |=> gnt)
  else $error("grant did not follow request");

断言的周期含义与 物理时序约束 处在不同抽象层次。

页面导航