Assign values to all columns in 2D array in one statement

verilog

I would like to assign value to an array. The array is 2 dimensional it has 16 rows and 16 col. Each element is 2 bits wide.

I would like to initialize each row of an array like so:

Initialize the 4th row of the array, then each value should be stored in respective column, so 0 will be in 15th col, 2 will be in 12th col, etc. I do not want to store values column by column. I've tried implementing it below but my code is erroring.

module test2;

    reg [1:0] mem [15:0][15:0];

    wire [1:0] icon;

    assign icon = mem[4][4];

    mem[4][15:0] = 16'h0002000000000000;

    $strobe("%b", icon);

endmodule;

Best Answer

mem[4][15:0] = 16'h0002000000000000; and $strobe("%b", icon); need to be in an assign statement, initial block, or always block.

mem is also delcared incorrectly if you want to assign as mem[4][15:0]. it should be declared as double packed, single unpacked multi-dimensional arrays: reg [15:0][1:0] mem [15:0];

The following compiles and runs without errors:

module test2;
    reg  [15:0][1:0] mem [15:0];
    wire [1:0] icon;
    assign icon = mem[4][4];

    initial begin
        mem[4][15:0] = 16'h0002000000000000;
        $strobe("%b", icon);
    end
endmodule;
Related Topic