1

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?

2
  • 1
    the code looks correct. is clk togging in your code? please make sure that it does. Also, please use non-blocking assignment <= in the posedge logic. Commented Jul 20, 2017 at 12:51
  • Are you running a simulation or directly of an FPGA? I remember trying to answer a prior question that turned out their FPGA reduced to read a single bit for a 2D array. Solution was to buffer the data into 1D array then read the bit. And FYI, you should initiate your entire array, not just the bits you use; add a for-loop assigning each entry to 0s at the top of initial block. Commented Jul 20, 2017 at 19:28

2 Answers 2

2

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 ;)

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

Comments

-1

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.

2 Comments

this suggestion makes no sense. it is as good as saying assing led = 1; and remove everything else.
Yes But in my second Program i have given a address, we can able to store values where want in the array and we can use then. i am not concerned about the making led =1 i have concerned about only retrieving values from array.

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.