Verilog Generates are designed for solving this issue. It would be easier if you can use unpacked arrays for ports.
Statements need to be contained in an initial
or always
, to imply the output is a flip-flop use always @(posedge clk)
for a combinatorial circuit use always @*
.
module example (
parameter N = 10
)(
input clk,
input [31:0] a [0:N],
input [31:0] b [0:N],
output reg [31:0] c [0:N]
);
genvar i;
generate
for(i=0; i<=N; i++) begin
always @(posedge clk) begin
c[i] <= a[i] + b[i];
end
end
endgenerate
endmodule
Generates are good when you need to parameterise module instance, the above can be rewritten with a plain for loop.
integer i;
always @(posedge clk) begin
for(i=0; i<=N; i++) begin
c[i] <= a[i] + b[i];
end
end
For synthesizable code the for loop must be able to be statically unrolled.