1

My desired functionality will be to add A, B, and cin (where A and B are 64 bits, and cin is one bit). So the actual sum (the actual result) can either be 64 bits or even 65 bits, right? So I want the output "sum" to be 64 bits, and then the carryout output "cout" would hold the most significant bit of the result. Tried doing this by an assign statement, but I guess that's not how you do it as it gives an error. Any other way?

module addsub(A, B, cin, sum, cout);
input [63:0] A, B;
input cin;
output reg [63:0] sum; 
output cout;


reg [64:0] actualsum; // the actual result of the addition, which I want to then "split" into cout & sum

always @(A or B or cin) begin
actualsum = A + B + cin;
sum <= actualsum[63:0];
cout <= actualsum[64];
end


endmodule
toolic
  • 57,801
  • 17
  • 75
  • 117
RhinoECE
  • 43
  • 7

1 Answers1

2

The compile error I got was due to a procedural assignment to cout (inside an always block). To fix that, you would declare cout as a reg.

Good coding practices recommend that you use blocking assignments (=) instead of nonblocking assignments (<=) for combinational logic.

A much simpler and more conventional way to code this is:

module addsub (
    input [63:0] A, B,
    input cin,
    output [63:0] sum, 
    output cout
);

assign {cout, sum} = A + B + cin;

endmodule
toolic
  • 57,801
  • 17
  • 75
  • 117