0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – The Photon Apr 12 '15 at 4:48
  • \$\begingroup\$ @ThePhoton Could you explain though, why it works when I switch the inputs with 8 bits? \$\endgroup\$ – Sagistic Apr 12 '15 at 4:53
  • \$\begingroup\$ 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. \$\endgroup\$ – The Photon Apr 12 '15 at 5:12
  • \$\begingroup\$ @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. \$\endgroup\$ – Sagistic Apr 12 '15 at 5:45
  • \$\begingroup\$ 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. \$\endgroup\$ – The Photon Apr 12 '15 at 13:39
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.