1

As Verilog does not allow to pass the array as input to module, so how can we flatten any array in Verilog. Suppose I have this array:

parameter [31:0] A [0:31];
wire [31:0] B

I want to pass this into any module like:

module1 M1 (.input(A), .output (B));

How can I achieve this?

6
  • Sorry about my misunderstading, I am not clear about flatten array. Can you describe it more. On the other hand, if you map parameter, it should be module #(.para(A)) M1 (.output(B)); . I do not think map parameter as InputOutput is legal. Commented Feb 20, 2015 at 7:44
  • As my best understand, you should use generate to convert A to one dimensional array to map. Commented Feb 20, 2015 at 7:47
  • @KhanhN.Dang By flattening I mean converting it into 1D. For example I have array {1,2,3,4,5}; I want to pass each member one by one into module. Hope it helps to understand the problem Commented Feb 20, 2015 at 9:48
  • I would like to go with the method given below. If I have large array of thousand members then it will be taking a lot of clock cycles... But it would be good to know about array passing on clock for knowledge. Commented Feb 20, 2015 at 12:57
  • If you have large array containing thousands elements each of multiple bits you will have a very large bus. It is not likely to fit on an FPGA and closing timing would be very problematic. Commented Feb 20, 2015 at 13:19

1 Answer 1

4

This verilog restriction is just a pain in... and etc... but we have to deal with it.

You can map the 2D array onto a 1D array like this :

wire [32*32-1:0]One_D_array;
integer i;
for (i=0; i<32; i=i+1) assign One_D_array[32*i+31:32*i] = A[i];

Then in your module, you can recreate the 2D array with the inverted for loop :

wire [31:0]local_2D_array[0:31];
integer i;
for (i=0;i<32;i=i+1) assign local_2D_array[i] = input[32*i+31:32*i];

The synthesis tool will handle it as wire remapping, so no LUT/FLIP_FLOP will be used. This is the easiest workaround I found for this limitation.

Sign up to request clarification or add additional context in comments.

6 Comments

SystemVerilog can have array style ports.
Indeed. This answer applies for verilog only.
I am trying to implement 1st part of code that is making 2D into 1D. Here is code: module Array_passing_testing( input clk ); parameter [31:0] A[0:31]= {1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8}; wire [32*32-1:0]One_D_array; genvar i; for (i=0; i<7; i=i+1) assign #10 One_D_array[32*i-1:32*i] = A[i]; endmodule But it is giving following error: Part-select direction is opposite from prefix index direction.
Sorry for bad formatting. I tried to edit it but 5 minutes had passed and 4 spaces for codes didn't work
@AwaisHussain try reversing the bit range instead of [32*i-1:32*i] try [32*i:32*i-1].
|

Your Answer

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.