4 bit x 4 bit using look up table (rom)

verilog

This is an introductory level verilog course. I'm trying to generate a 8 bit output from 4 bit multiplied with 4 bit. Here is the code I have so far.

    // 4 bit x 4 bit operator
    module bit4x4Operator(
    input [3:0]A,        //this code works when A and B are switched to 7:0
    input [3:0]B,
    output [7:0]P
    );
    reg [7:0] ROM[0:224];
    integer i,k;

    initial 
    begin
      for (i = 0; i < 16; i = i+1)
           for (k = 0; k < 16; k = k + 1)
              begin
                  ROM [(i * k)] = (i * k);
              end
     end
     assign P = ROM[(A * B)];

      endmodule

So, the assignment has to be done implementing the ROM[], and this code runs and outputs correctly if the input bits are changed to 7:0, but I'm not sure why that works, and why it can't be the way it is now.

Best Answer

assign P = ROM[(A * B)]; should be assign P = ROM[{A,B}]; The curly brackets ({}) is used to concatenate the bits. The square brackets ([]) are for indexing.

For ROM [(i * k)] = (i * k); you will want ROM [{i[3:0],k[3:0]}] = (i * k);

i and k are integers (equivalent to reg signed [31:0]), so {i,k} would be 64 bits wide with i starting at bit 32. The [3:0] is range slice if the integers that are meaningful for this indexing.

Note: you do not want to redefine i and k as reg [3:0] as this will give you an infinite loop 15 + 1 is 0 for a 4-bit reg and always less than 16.