G4.7.1 Adder-Subtractor with One Control Bit

MediumImplementIcarus · browser samples / server checksReview the topic

Turn one 8-bit adder into a block that adds, computes a − b or b − a, and flags signed overflow.

Hardware context: The one 8-bit adder of a small processor data path, which must also subtract. Build an adder-subtractor that also reverse-subtracts, around one fixed adder.

A small processor has room for one 8-bit adder in its data path. It must add, subtract, and subtract the other way round for a reverse-subtract instruction. The adder is a fixed block from the cell library, so only the logic around it can change.

This is an Implement problem. Write addsub8 around one instance of the read-only add8, named u_add. With sub = 0 the output y is a + b. With sub = 1 it is a − b, or b − a when rev = 1. c_out is the adder's own carry out. v flags a signed result that does not fit in eight bits.

The check applies seven operations, one per nanosecond. After each it records y, c_out and v. It also reads the inputs and outputs of u_add, to see that the adder itself does the work, and counts the adders. The minus operator is not allowed.

Signals in the Timeline

SignalWidthRole
script126input of each check
Full specification
The module (keep the name and the ports)
module addsub8 (
  input  logic [7:0] a, b,
  input  logic       sub,        // 0: y = a + b    1: subtract
  input  logic       rev,        // with sub = 1: 0 gives a - b, 1 gives b - a
  output logic [7:0] y,
  output logic       c_out,      // the adder's carry out; on subtract, 1 means no borrow
  output logic       v           // 1 when the signed result does not fit in 8 bits
);
  // your design
endmodule
add8.sv: the adder (read-only)
// add8.sv: the adder (read-only). Instantiate it once, as u_add. Every copy adds 1 to tb.n_add.
module add8 (
  input  logic [7:0] a, b,
  input  logic       cin,
  output wire  [7:0] sum,
  output wire        cout
);
  assign {cout, sum} = a + b + cin;
  initial tb.n_add++;
endmodule

subrevy
0anya + b
10a − b
11b − a
OutputRule
yThe result in eight bits, wrapping around.
c_outThe carry out of u_add. After a subtraction it is 1 when no borrow was needed.
v1 when the true signed result is above 127 or below −128.
u_addExactly one add8, named u_add. It adds the two operands in the order of the subtraction, with the second one inverted and a carry in of 1. y and c_out are its own outputs.

The result fields are y_stepK, c_out_stepK, v_stepK and adder_ok_stepK for the steps 0 to 6, and adder_count_wrong. adder_ok is 1 when u_add sees the operands and carry in of the rule above, and drives y and c_out.

Examples

Example 1

Small Additions and Subtractions
Input
5 + 3; 5 - 3; 3 - 5; 10 + 20; 20 - 10; 0 - 0; 0 + 0
Output
y_step0 = 0x8, c_out_step0 = no, v_step0 = no, adder_ok_step0 = yes, y_step1 = 0x2, c_out_step1 = yes, v_step1 = no, adder_ok_step1 = yes, y_step2 = 0xfe, c_out_step2 = no, v_step2 = no, adder_ok_step2 = yes, y_step3 = 0x1e, c_out_step3 = no, v_step3 = no, adder_ok_step3 = yes, y_step4 = 0xa, c_out_step4 = yes, v_step4 = no, adder_ok_step4 = yes, y_step5 = 0x0, c_out_step5 = yes, v_step5 = no, adder_ok_step5 = yes, y_step6 = 0x0, c_out_step6 = no, v_step6 = no, adder_ok_step6 = yes, adder_count_wrong = no

5 + 3 = 8 and 5 − 3 = 2. 3 − 5 wraps to fe, with c_out = 0: a borrow.

Example 2

Both Orders of Subtraction
Input
9 - 4; 4 - 9 (b - a); 30 - 100 (b - a); 100 - 30 (b - a); 7 - 7 (b - a); 1 - 0 (b - a); 0 - 1 (b - a)
Output
y_step0 = 0x5, c_out_step0 = yes, v_step0 = no, adder_ok_step0 = yes, y_step1 = 0xfb, c_out_step1 = no, v_step1 = no, adder_ok_step1 = yes, y_step2 = 0xba, c_out_step2 = no, v_step2 = no, adder_ok_step2 = yes, y_step3 = 0x46, c_out_step3 = yes, v_step3 = no, adder_ok_step3 = yes, y_step4 = 0x0, c_out_step4 = yes, v_step4 = no, adder_ok_step4 = yes, y_step5 = 0x1, c_out_step5 = yes, v_step5 = no, adder_ok_step5 = yes, y_step6 = 0xff, c_out_step6 = no, v_step6 = no, adder_ok_step6 = yes, adder_count_wrong = no

With rev = 1 the same inputs give b − a: 9 and 4 give 05 one way and fb the other.

Example 3

Rev Does Nothing on an Add
Input
50 + 70; 50 + 70; 127 + 1; -128 + -1; 3 + 4; 0 + 0; -1 + 1
Output
y_step0 = 0x78, c_out_step0 = no, v_step0 = no, adder_ok_step0 = yes, y_step1 = 0x78, c_out_step1 = no, v_step1 = no, adder_ok_step1 = yes, y_step2 = 0x80, c_out_step2 = no, v_step2 = yes, adder_ok_step2 = yes, y_step3 = 0x7f, c_out_step3 = yes, v_step3 = yes, adder_ok_step3 = yes, y_step4 = 0x7, c_out_step4 = no, v_step4 = no, adder_ok_step4 = yes, y_step5 = 0x0, c_out_step5 = no, v_step5 = no, adder_ok_step5 = yes, y_step6 = 0x0, c_out_step6 = yes, v_step6 = no, adder_ok_step6 = yes, adder_count_wrong = no

With sub = 0, rev is ignored: a + b and b + a are the same sum.

Constraints and Contract

  • Inputs are known 0/1 values. Each operation is held for 3 ns: the outputs are sampled after 1 ns, then the check briefly forces the adder's outputs.
  • Keep the module name and the ports, and name the adder instance u_add.

Hints

Hint 1

b − a is a − b with the operands swapped. Pick the two operands with a mux before the adder: the number you subtract from goes to the adder's a input.

Hint 2

XOR the second operand with sub, and feed sub to the carry in as well. With sub = 1 that is the inverted operand plus 1.

Hint 3

Judge overflow on what the adder really adds: its two operands. Overflow is when they share a sign and y has the other sign.

What the grader checks: Seven directed and ten seeded cases of seven operations each cover both orders of subtraction. y, c_out and v are compared with a model written in JavaScript after every step. The check also reads the inputs and outputs of u_add and counts the adders. The minus operator is refused before the run.

17 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 1 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

Small Additions and Subtractions

Input
5 + 3; 5 - 3; 3 - 5; 10 + 20; 20 - 10; 0 - 0; 0 + 0
Expected output
y_step0 = 0x8, c_out_step0 = no, v_step0 = no, adder_ok_step0 = yes, y_step1 = 0x2, c_out_step1 = yes, v_step1 = no, adder_ok_step1 = yes, y_step2 = 0xfe, c_out_step2 = no, v_step2 = no, adder_ok_step2 = yes, y_step3 = 0x1e, c_out_step3 = no, v_step3 = no, adder_ok_step3 = yes, y_step4 = 0xa, c_out_step4 = yes, v_step4 = no, adder_ok_step4 = yes, y_step5 = 0x0, c_out_step5 = yes, v_step5 = no, adder_ok_step5 = yes, y_step6 = 0x0, c_out_step6 = no, v_step6 = no, adder_ok_step6 = yes, adder_count_wrong = no

5 + 3 = 8 and 5 − 3 = 2. 3 − 5 wraps to fe, with c_out = 0: a borrow.

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

Icarus · browser samples / server checks