G1.3.1 Overflow Flags for an Adder

MediumImplementIcarus · browser samples / server checksReview the topic

Build a 6-bit adder-subtractor with a carry flag and a signed-overflow flag that are right for every input.

Hardware context: The arithmetic block of a small processor and the brightness adder of a lamp dimmer. Build the 6-bit adder-subtractor whose carry and signed-overflow flags are right for every input.

A small processor has one arithmetic block. Its compare and branch instructions read two flags from it. If a flag is wrong, a loop that should stop keeps running.

This is an Implement problem. Write addsub6. When sub is 0 the result is a + b. When sub is 1 the result is a - b. Form it the way an adder does: a plus the inverted b plus one.

The flag c_out is the carry out of the top bit of that adder. In a subtraction it is 1 when no borrow was needed. The flag overflow is 1 when the result, read as a signed 6-bit number, is not the true sum or difference.

The check applies ten operations, one per nanosecond, and records the result and both flags after each.

Signals in the Timeline

SignalWidthRole
script130input of each check
Full specification
The module (keep the name and the ports)
module addsub6 (
  input  logic [5:0] a,
  input  logic [5:0] b,
  input  logic       sub,        // 0: a + b    1: a - b
  output logic [5:0] result,
  output logic       c_out,      // the carry out of bit 5 of the adder
  output logic       overflow    // 1 when the signed result does not fit in 6 bits
);
  // your design
endmodule
Outputsub = 0sub = 1
resultThe low 6 bits of a + b.The low 6 bits of a - b.
c_outThe carry out of bit 5 of a + b.The carry out of bit 5 of a + (inverted b) + 1.
overflow1 when a + b is outside -32 to 31.1 when a - b is outside -32 to 31.

Both operands are 6-bit patterns. Read as signed numbers they run from -32 to 31. The check lists them as signed numbers.

The result fields are named result_step0, c_out_step0 and overflow_step0 for the first step, up to step 9.

Examples

Example 1

Additions That Fit
Input
1 + 2; 10 + 5; -1 + 1; -10 + 3; 15 + 16; -16 + -16; 0 + 0; -1 + -1; 20 + -20; 7 + 8
Output
result_step0 = 3, c_out_step0 = no, overflow_step0 = no, result_step1 = 15, c_out_step1 = no, overflow_step1 = no, result_step2 = 0, c_out_step2 = yes, overflow_step2 = no, result_step3 = -7, c_out_step3 = no, overflow_step3 = no, result_step4 = 31, c_out_step4 = no, overflow_step4 = no, result_step5 = -32, c_out_step5 = yes, overflow_step5 = no, result_step6 = 0, c_out_step6 = no, overflow_step6 = no, result_step7 = -2, c_out_step7 = yes, overflow_step7 = no, result_step8 = 0, c_out_step8 = yes, overflow_step8 = no, result_step9 = 15, c_out_step9 = no, overflow_step9 = no

Every sum is inside -32 to 31, so overflow is 0 in all ten steps. The carry is still 1 in some of them, such as -1 + 1: a carry does not mean a signed error.

Example 2

Additions at the Ends of the Range
Input
31 + 1; -32 + -1; 31 + 31; -32 + -32; 16 + 16; -17 + -16; 30 + 1; -31 + -1; 31 + 0; -32 + 0
Output
result_step0 = -32, c_out_step0 = no, overflow_step0 = yes, result_step1 = 31, c_out_step1 = yes, overflow_step1 = yes, result_step2 = -2, c_out_step2 = no, overflow_step2 = yes, result_step3 = 0, c_out_step3 = yes, overflow_step3 = yes, result_step4 = -32, c_out_step4 = no, overflow_step4 = yes, result_step5 = 31, c_out_step5 = yes, overflow_step5 = yes, result_step6 = 31, c_out_step6 = no, overflow_step6 = no, result_step7 = -32, c_out_step7 = yes, overflow_step7 = no, result_step8 = 31, c_out_step8 = no, overflow_step8 = no, result_step9 = -32, c_out_step9 = no, overflow_step9 = no

31 + 1 is 32, one above the range: the result pattern reads -32 and overflow is 1 with no carry. -32 + -1 overflows the other way and also gives a carry.

Example 3

Subtractions That Fit
Input
5 - 3; 3 - 5; 0 - 0; -1 - -1; 10 - -10; -10 - 10; 0 - 1; 1 - 0; 31 - 31; -32 - -32
Output
result_step0 = 2, c_out_step0 = yes, overflow_step0 = no, result_step1 = -2, c_out_step1 = no, overflow_step1 = no, result_step2 = 0, c_out_step2 = yes, overflow_step2 = no, result_step3 = 0, c_out_step3 = yes, overflow_step3 = no, result_step4 = 20, c_out_step4 = no, overflow_step4 = no, result_step5 = -20, c_out_step5 = yes, overflow_step5 = no, result_step6 = -1, c_out_step6 = no, overflow_step6 = no, result_step7 = 1, c_out_step7 = yes, overflow_step7 = no, result_step8 = 0, c_out_step8 = yes, overflow_step8 = no, result_step9 = 0, c_out_step9 = yes, overflow_step9 = no

5 - 3 gives 2 with c_out at 1: no borrow. 3 - 5 gives -2 with c_out at 0: the unsigned subtraction needed a borrow. Overflow stays 0.

Constraints and Contract

  • Inputs are known 0/1 values. Each operation is held for one nanosecond and the outputs are sampled at its end.
  • Keep the module name and the ports. The design is combinational.

Hints

Hint 1

Decide first which 6-bit pattern the adder adds to a. For a subtraction it is the inverted b, and the extra one comes in as a carry.

Hint 2

To see the carry out, make the sum 7 bits wide. Put a 0 bit in front of each operand yourself, after any inversion.

Hint 3

Signed overflow needs the signs of the two numbers that enter the adder and the sign of the result. Check which pattern holds the sign of the second number in a subtraction.

What the grader checks: Six directed and ten seeded cases of ten operations each run on the block. The result and both flags are compared with a model written in JavaScript after every step. The check compares outputs only: delay and the structure of the adder are outside this problem.

16 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Additions That Fit

Input
1 + 2; 10 + 5; -1 + 1; -10 + 3; 15 + 16; -16 + -16; 0 + 0; -1 + -1; 20 + -20; 7 + 8
Expected output
result_step0 = 3, c_out_step0 = no, overflow_step0 = no, result_step1 = 15, c_out_step1 = no, overflow_step1 = no, result_step2 = 0, c_out_step2 = yes, overflow_step2 = no, result_step3 = -7, c_out_step3 = no, overflow_step3 = no, result_step4 = 31, c_out_step4 = no, overflow_step4 = no, result_step5 = -32, c_out_step5 = yes, overflow_step5 = no, result_step6 = 0, c_out_step6 = no, overflow_step6 = no, result_step7 = -2, c_out_step7 = yes, overflow_step7 = no, result_step8 = 0, c_out_step8 = yes, overflow_step8 = no, result_step9 = 15, c_out_step9 = no, overflow_step9 = no

Every sum is inside -32 to 31, so overflow is 0 in all ten steps. The carry is still 1 in some of them, such as -1 + 1: a carry does not mean a signed error.

Run samples executes all 3 public checks. Check all tests runs 16 checks on the server.

Icarus · browser samples / server checks