SystemVerilog const:只读约束的三种形态

const 表示“初始化后不可修改”。它用于表达只读配置、事务快照和方法不改变对象状态的约束。

1. const 变量

1
2
3
4
5
6
7
8
class config;
const int MAX_LEN = 64;
bit [31:0] base_addr;

function new(bit [31:0] a);
base_addr = a;
endfunction
endclass

const 成员必须在声明时或构造中初始化,之后不可赋值。

2. const 方法

1
2
3
function bit [31:0] get_base() const;
return base_addr; // 不修改任何成员
endfunction

const 方法内不能修改对象字段,编译器会检查。

3. const 参数

1
2
3
function void send(const ref packet p);
// p 只读
endfunction

4. 常见误区

  1. 试图修改 const 变量:编译错误。
  2. const 方法中调用非 const 方法:可能违反只读约束。
  3. 把 const 当作运行时不变:它主要是编译期约束。

小结

const 表达“只读意图”。配置对象、事务快照和查询方法都应尽量声明为 const,让工具替你守住边界。

参考文献:ChipVerify SystemVerilog Tutorial — SystemVerilog Const 章节(https://chipverify.com/systemverilog/systemverilog-const)。本文为原创中文讲解。