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

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.

• Hint: address the rom with {A, B} instead of A*B. If you're addressing the rom with A*B you're assuming you already have hardware to calculate A*B which makes the whole exercise kind of silly. – The Photon Apr 12 '15 at 4:48
• @ThePhoton Could you explain though, why it works when I switch the inputs with 8 bits? – Sagistic Apr 12 '15 at 4:53
• What's the error message you're seeing? I suspect something like the * operator causing promotion to 8 bits and that somehow messing things up (maybe due to sign extension?) but I don't know for sure. – The Photon Apr 12 '15 at 5:12
• @ThePhoton ; Nevermind that, I'm actually still stuck on your original suggestion. {A,B} is the concatenation operator right? So I tried changing the last line to assign P = ROM{A,B}; but, that's not correct. – Sagistic Apr 12 '15 at 5:45
• You need to change the rest of your code to match. But we're not going to do your homework for you. Just answer your specific questions and get you started. – The Photon Apr 12 '15 at 13:39

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.