Roscoe
Roscoe

Reputation: 13

How to access values in a 2D array in Verilog?

I need to use a 2D array for a coordinate system in a module, and I've created the test code below to test creating and accessing values in the array. This code should turn on an LED led output when the led output is 1'b1, but currently the LED stays off (I've troubleshooted all other code besides the 2D array stuff, it works when I use a 1D array here).

input clk;
reg [7:0] check[9:0];
reg ledreg;
output led;

initial begin
    check[0][0] = 1'b1;
    ledreg = 1'b0;
end

always @(posedge clk) begin
    if (check[0][0] == 1'b1) begin
        ledreg = 1'b1;
    end
end

assign led = ledreg;

I'm not sure if my array initialization syntax is off reg [7:0] check[9:0] or the value checking syntax is off check[0][0] == 1'b1, or if this is a SystemVerilog feature that doesn't work with just Verilog (I don't have SystemVerilog but this code compiles without error, so I don't think that's it).

How do I check a value in a 2D array so that I can do things when it has a certain value?

Upvotes: 1

Views: 14708

Answers (2)

Ariyan Sharifi
Ariyan Sharifi

Reputation: 63

for declaring a 2D reg in verilog this is more regular

reg [7:0] check[0:9]; //insted of reg [7:0] check[9:0]

and this mean you have 10 * 8bit regs

and you can assess to first 8bit by check[0] and you can access to 3rd bit of first element by check[0][3]

and yes you can ;)

Upvotes: 2

venkat pasumarti
venkat pasumarti

Reputation: 128

your assigning a value in initial block that is why it is not accepting it. i have edit your code and check it

 module led(clk,led,address
        );
         input clk;
         input [3:0] address;
    reg [7:0] check[9:0];
    reg ledreg;
    output led;

    //  initial begin
    //    check[0] = 1;
    //    ledreg = 1'b0;
    //  end

        always@(posedge clk) begin
            check[0][0] = 1'b1;

        if ( check[0][0] == 1'b1) begin

            ledreg = 1'b1;
        end
        end
    assign led = ledreg;
    endmodule

this mite help you i have included an input signal for the 2D array.

module led(clk,led,address
    );
     input clk;
     input [3:0] address;
reg [7:0] check[9:0];
reg ledreg;
output led;

//  initial begin
//    check[0] = 1;
//    ledreg = 1'b0;
//  end

    always@(posedge clk) begin
        check[address] = 8'b11111111;

    if ( check[0][0] == 1'b1) begin

        ledreg = 1'b1;
    end
    end

    assign led = ledreg;


endmodule

the above code will assign data for the bite in the first row of the multi dimension array and then u can access it.

Upvotes: -1

Related Questions