我是Verilog的初学者,我想在我定义了以下两个宏的代码中打包和解压缩3-D输入和输出:
`define PACK_3D(PK_WIDTH,PK_HEIGHT, PK_DEPTH, PK_SRC, PK_DEST) \
genvar pk_idh; \
genvar pk_idd; \
generate for (pk_idd=0; pk_idd<(PK_DEPTH); pk_idd=pk_idd+1) begin \
generate for (pk_idh=0; pk_idh<(PK_HEIGHT); pk_idh=pk_idh+1) begin \
assign PK_DEST[pk_idd*(PK_HEIGHT*PK_WIDTH) + pk_idh*(PK_WIDTH) + (PK_WIDTH-1): pk_idd*(PK_HEIGHT*PK_WIDTH) + pk_idh*(PK_WIDTH)] = PK_SRC[pk_idd][pk_idh][(PK_WIDTH)-1):0]; \
end \
endgenerate \
end \
endgenerate
`define UNPACK_3D(PK_WIDTH, PK_HEIGHT, PK_DEPTH, PK_SRC, PK_DEST) \
genvar pk_idh; \
genvar pk_idd; \
generate for (pk_idd=0; pk_idd<(PK_DEPTH); pk_idd=pk_idd+1) begin \
generate for (pk_idh=0; pk_idh<(PK_HEIGHT); pk_idh=pk_idh+1) begin \
assign PK_DEST[pk_idd][pk_idh][(PK_WIDTH)-1):0] = PK_SRC[pk_idd*(PK_HEIGHT*PK_WIDTH) + pk_idh*(PK_WIDTH) + (PK_WIDTH-1): pk_idd*(PK_HEIGHT*PK_WIDTH) + pk_idh*(PK_WIDTH)]; \
end \
endgenerate \
end \
endgenerate
接下来,我构建了一个add模块来添加两个3-D矩阵并返回如下输出:
module add(clk, rst, g_input, e_input, o);
input clk,rst;
localparam num=4;
localparam h = 3;
localparam w = 3;
localparam d = 2;
input [2*num*h*w*d-1:0] g_input;
input [2*num*h*w*d-1:0] e_input;
output reg [2*num*h*w*d-1:0] o;
reg [2*num -1: 0] g_unpack[d-1:0][h-1:0][w-1:0];
reg [2*num -1: 0] e_unpack[d-1:0][h-1:0][w-1:0];
reg [2*num -1: 0] o_unpack[d-1:0][h-1:0][w-1:0];
`UNPACK_3D(w,h,d,g_input,g_unpack);
`UNPACK_3D(w,h,d,e_input,e_unpack);
integer i_d, i_h, i_w
always@* // always combinational block
begin
for (i_d = 0; i_d < d; i_d = i_d+1)
begin
for (i_h = 0; i_h < d; i_h = i_h+1)
begin
for (i_w = 0; i_w < d; i_w = i_w+1)
begin
o_unpack[i_d][i_h][i_w] = g_unpack[i_d][i_h][i_w] + e_unpack[i_d][i_h][i_w];
end
end
end
end
`PACK_3D(w,h,d,o_unpack,o);
endmodule
我从this discussion那里得到了这个主意。虽然当我尝试编译上面的代码时,我遇到编译错误:
Error: ./add.v:43: Syntax error at or near token 'generate'
in macro "UNPACK_3D"
called from file "./add.v" (line 43). (VER-294)
Error: ./add.v:43: Syntax error at or near token '('
in macro "UNPACK_3D"
called from file "./add.v" (line 43). (VER-294)
Error: ./add.v:44: Syntax error at or near token 'generate'
in macro "UNPACK_3D"
called from file "./add.v" (line 44). (VER-294)
Error: ./add.v:44: Syntax error at or near token '('
in macro "UNPACK_3D"
called from file "./add.v" (line 44). (VER-294)
Error: ./add.v:44: Syntax error at or near token '('
in macro "UNPACK_3D"
called from file "./add.v" (line 44). (VER-294)
Error: ./add.v:48: Syntax error at or near token 'always'. (VER-294)
Error: ./add.v:50: Syntax error at or near token ';'. (VER-294)
Error: ./add.v:52: Syntax error at or near token ';'. (VER-294)
Error: ./add.v:54: Syntax error at or near token ';'. (VER-294)
Error: ./add.v:62: Syntax error at or near token 'generate'
in macro "PACK_3D"
called from file "./add.v" (line 62). (VER-294)
Error: ./add.v:62: Syntax error at or near token '('
in macro "PACK_3D"
called from file "./add.v" (line 62). (VER-294)
Error: ./add.v:62: Syntax error at or near token '('
in macro "PACK_3D"
called from file "./add.v" (line 62). (VER-294)
*** Presto compilation terminated with 12 errors. ***
任何人都可以帮助解决此问题吗? 谢谢。
答案 0 :(得分:1)
您已嵌套generates
。
您应该只有一对generate..endgenerate
,并且两个for
都在其中。
此外,如果您实例化您的genvar的代码被声明两次(genvar pk_idh; genvar pk_idd;
),即使您在打包和解压缩之间使它们不同,也只能在每个模块中对每个宏调用一次。
我还建议您先尝试不使用宏的代码。然后,当语法正确且代码有效时,请尝试将其转换为宏。