title: verilog signed addition and subtraction
body: <p>I am having trouble understanding how to handle overflow when adding or subtracting signed numbers in verilog.</p>
  
  <p>When I did this with unsigned numbers, it was very simple:</p>
  
  <pre><code>input [15:0] A;
  input [15:0] B;
  input [3:0] S;
  output reg [15:0] AddAB;
  ...
  always@(*)
      begin
          {OFAdd, AddAB} &lt;= A + B;
          {OFSub, SubAB} &lt;= A - B;
  ...
          case(S)
              0:
              begin
                  Display &lt;= AddAB;
                  DisplayOF &lt;= OFAdd;
              end
              1:
              begin
                  Display &lt;= SubAB;
                  DisplayOF &lt;= OFSub;
              end
  ...
  </code></pre>
  
  <p>And I would get the proper output.
  But doing the same thing with signed inputs, I get the wrong value for my overflow</p>
  
  <pre><code>input signed [15:0] A;
  input signed [15:0] B;
  input [3:0] S;
  output reg signed [15:0] AddAB;
  ...
  always@(*)
      begin
          {OFAdd, AddAB} &lt;= A + B;
          {OFSub, SubAB} &lt;= A - B;
  ...
          case(S)
              0:
              begin
                  Display &lt;= AddAB;
                  DisplayOF &lt;= OFAdd;
              end
              1:
              begin
                  Display &lt;= SubAB;
                  DisplayOF &lt;= OFSub;
              end
  ...
  </code></pre>
  
  <p>For instance, if A =-21846 and B = 88, I get the proper value AddAB = -21758, but I get DisplayOF = 1. The same happens with subtraction. I get the proper value for SubAB but I get an overflow on DisplayOF when I shouldn't.</p>
  
  <p>If it makes any difference, my testbench looks basically like this</p>
  
  <pre><code>    A = 16'sb1010101010101010;
      B = 16'sd88;
      S = 4'd0;
      #10;
  
      A = 16'sb1010101010101010;
      B = 16'sd88;
      S = 4'd1;
      #10;
  </code></pre>
  
  <p>I'm not concerned with synthesizability, I just want to figure out how to deal with overflow on unary arithmetic on signed values.</p>
  
  <p>If anyone could point me in the right direction I would be very greatful, thank you!</p>