-2

Create a Verilog module called eightbit palu which has two 8-bit inputs, a and b, and one 2-bit input, sel. The outputs of this module are an 8-bit signal f, and a 1-bit signal ovf. The value of these outputs should change based on the sel signal value which determines the operation.

**s[1:0]   f[7:0]         ovf           Description**

 0 0     a + b (add)    overflow         a plus b

 0 1     b (inv)           0             Bitwise inversion of b

 1 0     a · b (and)       0             Bitwise AND of a and b

 1 1      a | b (or)       0             Bitwise OR of a and b

I have the above assignment and here's what I have so far in verilog:

module eightbit_palu(input [7:0]a,
                     input [7:0]b,
                     input [1:0]sel,
                     output [7:0]f,
                     output ovf);
     reg (f, ovf);
     always @ (a, b, sel);
     case(sel)
         2’b00: f = a + b;
         2’b01: f = ~b;
         2’b10: f = a & b;
         2’b11: f = a | b;
     endcase
endmodule

I'm new to verilog so I'm not sure if this is right or what I should do about the overflow value. Any tips/suggestions?

4
  • OR is |, not /. And I assume you want ovf to be 1 iff the operation is addition and the addition has "signed overflow", is that right?
    – harold
    Jan 31, 2016 at 18:05
  • The addition gives you an extra bit actually. If the two top bits are not the same, then the truncation would change the value if it's interpreted as a 2's complement integer (ie, it overflows). You do need the extra bit for that obviously.
    – harold
    Jan 31, 2016 at 18:12
  • but wouldn't the two bits always be 0 because the addition only happens in the case that sel is 0 0 ?
    – smd
    Jan 31, 2016 at 18:15
  • @smd Not sel. The top bits of the result. btw the answer you accepted checks for unsigned overflow.
    – harold
    Jan 31, 2016 at 19:05

1 Answer 1

0

If you extend the length of the adder's output by an extra bit, then that bit will be set if a carry is performed. The following snippet is synthesized (at least by XST) as an 8-bit carry-out adder:

2'b00: {ovf, f} = a + b;

This is a concatenation of overflow and result in the LHS and assignment to that concatenation.

Don't forget to set a default value to ovf, to prevent synthesis of a latch (which is almost never appropriate):

always @ (a, b, sel) begin
     ovf = 0; // default
     case(sel)
         2'b00: {ovf, f} = a + b;
         2'b01: f = ~b;
         2'b10: f = a & b;
         2'b11: f = a | b;
     endcase
end
5
  • so the rest of it looks like it should do what's asked?
    – smd
    Jan 31, 2016 at 18:40
  • @toolic I just simulated it using iverilog.com and got the errors 6: syntax error 6: error: syntax error in reg variable list. 9: error: unmatched character (hex �) 9: error: unmatched character (hex �) 9: error: unmatched character (hex �) 9: syntax error 9: error: invalid module item. a: syntax error a: error: unmatched character (hex �) a: error: invalid module item. b: syntax error b: error: unmatched character (hex �) b: error: invalid module item. c: syntax error c: error: unmatched character (hex �) c: error: invalid module item. d: syntax error
    – smd
    Jan 31, 2016 at 18:44
  • 1
    @smd Your code includes "smart quote" marks. Use normal apostrophes instead, as I have in my example. ( and ' are different). Additionally, I suggest that, if you are able to, that you use a full-featured FPGA toolkit (e.g. ISE, Vivado, Diamond, or Quartus) and a physical FPGA, to better understand the limitations of this architecture. There are many things that iverilog will let you do, that would be completely nonsensical, impossible, or inappropriate on physical hardware, and there are many elements to the physical hardware (e.g. IOs, transceivers) that are not applicable to iverilog.
    – nanofarad
    Jan 31, 2016 at 18:49
  • I still received the error "6: error: syntax error in reg variable list." Do I need to include [7:0] in the reg list?
    – smd
    Jan 31, 2016 at 18:52
  • 1
    @smd No, it's because line 6 is competely wrong. Don't use parentheses on it (should be reg f, ovf instead)
    – nanofarad
    Jan 31, 2016 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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