如何使用时间 int 变量简化代码
How to simplify code using a temporal int variable
我希望代码 1 和代码 2 执行相同的操作
module testModule #( parameter LEN = 4,
parameter logic [0:0] OPTION = 1'b0 )
(
input Clk,
input [ 7:0][LEN-1:0] DataIn,
input [ 7:0][LEN-1:0] Factor,
output [15:0][LEN-1:0] DataOut_1,
output [15:0][LEN-1:0] DataOut_2
);
// CODE 1
always_ff @(posedge Clk) begin
for (int i = 0; i < LEN; i++) begin
if (OPTION == 1'b0) begin
DataOut_1[i] <= DataIn[i] * Factor[0];
end else begin
DataOut_1[i] <= DataIn[i] * Factor[i];
end
end
end
// CODE 2
always_ff @(posedge Clk) begin
for (int i = 0; i < LEN; i++) begin
int select = (OPTION == 1'b0) ? 0 : i;
DataOut_2[i] <= DataIn[i] * Factor[select];
end
end
endmodule
OPTION 可以是 0 或 1。
但是我在 CODE 2 中得到以下错误
int select = (OPTION == 1'b0) ? 0 : i;
带有初始值设定项的局部静态变量需要 'static' 关键字
静态变量初始值设定项中的自动变量非法
我不想简化 for 循环,因为在我的原始代码中我需要它
这个问题是 other 的变体,但我不想更改原始代码问题
你有几个问题:
- 您不能分配给 always 块内的网络。 bpoth,
DataOut_1/2
被声明为网络。因此,您至少应该将它们声明为 regs:
output reg [15:0][LEN-1:0] DataOut_1,
output reg [15:0][LEN-1:0] DataOut_2
- 模块中定义的所有变量本质上都是
static
。某些情况除外。 'for' 循环中定义的 i
是自动的。因此,您不能将它用作静态变量的初始值设定项。您需要将 'select' 声明为自动:
automatic int select = (OPTION == 1'b0) ? 0 : i;
我希望代码 1 和代码 2 执行相同的操作
module testModule #( parameter LEN = 4,
parameter logic [0:0] OPTION = 1'b0 )
(
input Clk,
input [ 7:0][LEN-1:0] DataIn,
input [ 7:0][LEN-1:0] Factor,
output [15:0][LEN-1:0] DataOut_1,
output [15:0][LEN-1:0] DataOut_2
);
// CODE 1
always_ff @(posedge Clk) begin
for (int i = 0; i < LEN; i++) begin
if (OPTION == 1'b0) begin
DataOut_1[i] <= DataIn[i] * Factor[0];
end else begin
DataOut_1[i] <= DataIn[i] * Factor[i];
end
end
end
// CODE 2
always_ff @(posedge Clk) begin
for (int i = 0; i < LEN; i++) begin
int select = (OPTION == 1'b0) ? 0 : i;
DataOut_2[i] <= DataIn[i] * Factor[select];
end
end
endmodule
OPTION 可以是 0 或 1。
但是我在 CODE 2 中得到以下错误
int select = (OPTION == 1'b0) ? 0 : i;
带有初始值设定项的局部静态变量需要 'static' 关键字
静态变量初始值设定项中的自动变量非法
我不想简化 for 循环,因为在我的原始代码中我需要它
这个问题是 other 的变体,但我不想更改原始代码问题
你有几个问题:
- 您不能分配给 always 块内的网络。 bpoth,
DataOut_1/2
被声明为网络。因此,您至少应该将它们声明为 regs:
output reg [15:0][LEN-1:0] DataOut_1,
output reg [15:0][LEN-1:0] DataOut_2
- 模块中定义的所有变量本质上都是
static
。某些情况除外。 'for' 循环中定义的i
是自动的。因此,您不能将它用作静态变量的初始值设定项。您需要将 'select' 声明为自动:
automatic int select = (OPTION == 1'b0) ? 0 : i;