verilog - systemverilog 中逻辑语句的非常量索引

标签 verilog system-verilog

我正在尝试创建一个 for 循环,在给定循环迭代的情况下将不同的值分配给逻辑数组。

因此,例如,假设我正在尝试实例化两个不同的积木,宽度均为 10,高度均为 5。我们还假设这些值中的每一个都是 10 位。对于两 block 砖,我有代码:

logic[19:0] Brick_Width;
logic[19:0] Brick_Height;

第一个砖 block 的宽度和高度将被分配到最高有效的 10 位,第二个砖 block 的宽度和高度将分配到最低有效的 10 位。

这是我目前拥有的代码:

int i = 19;
initial
begin
 for(i=19; i>=0; i=i-10)
 begin
  assign Brick_Width[i:i-9] = 10;
  assign Brick_Height[i:i-9] = 5;
 end
end

但是,我收到一条错误消息,指出“i”不是常量。关于我如何去做这件事有什么想法吗?

最佳答案

使用 : 运算符的常用范围选择必须有一个常量。您的意图可以通过所谓的位选择运算符来实现。

引用来自 LRM 1800-2012 的示例, 第 11.5 节如下:

logic [31: 0] a_vect;
logic [0 :31] b_vect;
logic [63: 0] dword;
integer sel;
a_vect[ 0 +: 8] // == a_vect[ 7 : 0]
a_vect[15 -: 8] // == a_vect[15 : 8]
b_vect[ 0 +: 8] // == b_vect[0 : 7]
b_vect[15 -: 8] // == b_vect[8 :15]
dword[8*sel +: 8] // variable part-select with fixed width

+:-: 运算符必须按照您的情况用于位切片或部分选择。这里,你想选择从ii-9的部分,所以必须使用-:运算符。比如Brick_Height[i-:9]=...

例如,

x +: Y, the start position is x and count up from x by Y. Y is necessarily a constant.
x -: Y, the start position is x and count down from x by Y. Y is necessarily a constant.

另外,initial block 中的 assign 语句使其成为连续过程赋值。在这种情况下,位选择将不起作用。为此,只需删除 assign statement。如下:

for(i=19; i>=0; i=i-10)
 begin
    Brick_Width[i-:9] = 10; // no assign, rest all the stuff is same
    Brick_Height[i-:9] = 5;
 end

或者使用生成 block ,如果连续驾驶是意图。

genvar i;
generate
for(i=19; i>=0; i=i-10)
begin
    assign Brick_Width[i-:9] = 10;
    assign Brick_Height[i-:9] = 5;
end
endgenerate

有关位选择的更多信息,请访问 this。链接。

边注:

指的是您问题中的以下短语:

For two bricks, I have the following code:

您必须有一个数组,例如logic[9:0] Brick [2]

关于verilog - systemverilog 中逻辑语句的非常量索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33864574/

相关文章:

verilog - 如果在 Verilog 中恰好是一个高,则连线高

verilog - 使用串联语句分配 localparam?

system-verilog - UVM RAL : Randomizing registers in a register model

Verilog,我可以为always block 内的多个位分配一个位值吗

syntax-error - 文本 “genvar”附近的test_bench_lb2.v(14)处的Verilog HDL语法错误;期待 “end”

verilog - 括号在用于包裹参数时有什么特殊意义吗?

Verilog 有符号乘法 : Multiplying numbers of different sizes?

verilog - EDAplayground错误: Execution interrupted or reached maximum runtime

oop - 将 "type"参数传递给函数

VIM 高亮匹配开始/结束