2
\$\begingroup\$

Is it possible to write the following code using a for loop in Verilog?

i = 0: sum[0] <= data[0];
i = 1: sum[1] <= data[0] + data[1];
i = 2: sum[2] <= data[0] + data[1] + data[2];
i = 3: sum[3] <= data[0] + data[1] + data[2] + data[3];
.
.
i = 7: sum[7] <= data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7];
ocrdu
9,34123 gold badges33 silver badges43 bronze badges
asked Oct 30, 2022 at 8:23
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can use a couple of for loops and a function:

logic [3:0] sum [8];
logic [7:0] data;
function logic [3:0] sum_data (int n);
 sum_data = data[0];
 for (int j=1; j<=n; j++) begin
 sum_data += data[j];
 end
endfunction
// ...
for (int i=0; i<8; i++) begin
 sum[i] <= sum_data(i);
end

Functions are synthesizable.

answered Oct 30, 2022 at 10:51
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.