首页 > 解决方案 > 模块输入的宽度可以在模块内部确定吗?

问题描述

在 SystemVerilog IEEE Std 1800-2017 第 328 页中,显示了以下示例:

module ram_model (address, write, chip_select, data);

  parameter data_width = 8;
  parameter ram_depth = 256;
  localparam addr_width = clogb2(ram_depth);
  input [addr_width - 1:0] address;
  input write, chip_select;
  inout [data_width - 1:0] data;

  //define the clogb2 function
  function integer clogb2 (input [31:0] value);
  value = value - 1;
  for (clogb2 = 0; value > 0; clogb2 = clogb2 + 1)
    value = value >> 1;
  endfunction

  logic [data_width - 1:0] data_store[0:ram_depth - 1];

  //the rest of the ram model

endmodule: ram_model

由模块内部函数的输出“addr_width”确定的“地址”输入宽度有什么意义?

标签: verilogsystem-verilog

解决方案


该代码来自第 13.4.3 节常量函数

这里的关键是clogb2满足成为常数函数的所有标准。正如 Std 所述,常量函数调用在细化时进行评估。由于这发生在运行时之前,因此返回的值被视为常量。

并非所有功能都可以这种方式使用。

另请参阅 IEEE Std 1800-2017,第 23.10.4 节细化注意事项,以进一步讨论细化时间。


推荐阅读