Build an 8-bit ALU with add and subtract with carry, three logic operations and the flags Z, N, C and V.
Hardware context: The ALU of a small controller that adds wide numbers in steps and branches on its flags. Build an ALU with ADC and SBC and all four flags.
A small controller adds and subtracts numbers wider than 8 bits in several steps. Its ALU therefore has ADC and SBC: add and subtract with the carry of the step before. SBC works as on ARM processors: it computes a + ~b + cin, so cin = 1 means "no borrow".
This is an Implement problem. Write alu8f with one shared adder, a logic unit and an output mux. Give every operation its result and all four flags. Opcode 7 is reserved: y = 0, with C and V at 0.
The check applies six operations, one per nanosecond, and records y and the flags after each. The check also synthesizes the module on the site's flow and rejects any latch.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 120 | input of each check |
Full specification
module alu8f (
input logic [2:0] op, // 0 ADD, 1 ADC, 2 SUB, 3 SBC, 4 AND, 5 OR, 6 XOR, 7 reserved
input logic [7:0] a, b,
input logic cin, // carry in, used by ADC and SBC only
output logic [7:0] y,
output logic z, n, c, v // zero, negative, carry, signed overflow
);
// your design
endmodule
| op | Name | y | C | V |
|---|---|---|---|---|
| 0 | ADD | a + b | carry out | signed overflow |
| 1 | ADC | a + b + cin | carry out | signed overflow |
| 2 | SUB | a + ~b + 1 = a − b | carry out (1 = no borrow) | signed overflow |
| 3 | SBC | a + ~b + cin | carry out (1 = no borrow) | signed overflow |
| 4, 5, 6 | AND, OR, XOR | a & b, a | b, a ^ b | 0 | 0 |
| 7 | reserved | 0 | 0 | 0 |
| Flag | Rule, for every operation |
|---|---|
| z | 1 when the 8-bit y is zero. |
| n | Bit 7 of y. |
| v | For 0 to 3: a, the adder's b operand and its carry in, added as signed numbers, fall outside −128 to 127. |
The result fields are y_stepK, z_stepK, n_stepK, c_stepK and v_stepK for the steps 0 to 5.
Examples
Example 1
Additions- Input
ADD 12 34 cin=0; ADD 7f 01 cin=0; ADD ff 01 cin=0; ADD 80 80 cin=0; ADD 00 00 cin=0; ADD c0 50 cin=0
- Output
y_step0 = 0x46, z_step0 = no, n_step0 = no, c_step0 = no, v_step0 = no, y_step1 = 0x80, z_step1 = no, n_step1 = yes, c_step1 = no, v_step1 = yes, y_step2 = 0x0, z_step2 = yes, n_step2 = no, c_step2 = yes, v_step2 = no, y_step3 = 0x0, z_step3 = yes, n_step3 = no, c_step3 = yes, v_step3 = yes, y_step4 = 0x0, z_step4 = yes, n_step4 = no, c_step4 = no, v_step4 = no, y_step5 = 0x10, z_step5 = no, n_step5 = no, c_step5 = yes, v_step5 = no
7f + 01 = 80 sets N and V: two positive numbers gave a negative one. ff + 01 = 00 sets Z and C.
Example 2
Subtractions- Input
SUB 05 07 cin=0; SUB 07 05 cin=0; SUB 42 42 cin=0; SUB 80 01 cin=0; SUB 7f ff cin=0; SUB 00 01 cin=0
- Output
y_step0 = 0xfe, z_step0 = no, n_step0 = yes, c_step0 = no, v_step0 = no, y_step1 = 0x2, z_step1 = no, n_step1 = no, c_step1 = yes, v_step1 = no, y_step2 = 0x0, z_step2 = yes, n_step2 = no, c_step2 = yes, v_step2 = no, y_step3 = 0x7f, z_step3 = no, n_step3 = no, c_step3 = yes, v_step3 = yes, y_step4 = 0x80, z_step4 = no, n_step4 = yes, c_step4 = no, v_step4 = yes, y_step5 = 0xff, z_step5 = no, n_step5 = yes, c_step5 = no, v_step5 = no
05 − 07 borrows, so C = 0. 07 − 05 does not, so C = 1. 80 − 01 overflows: −128 − 1 gives 7f.
Example 3
Logic Operations- Input
AND f0 3c cin=0; OR f0 0c cin=0; XOR ff 0f cin=0; AND 0f f0 cin=0; OR 00 00 cin=0; XOR aa aa cin=0
- Output
y_step0 = 0x30, z_step0 = no, n_step0 = no, c_step0 = no, v_step0 = no, y_step1 = 0xfc, z_step1 = no, n_step1 = yes, c_step1 = no, v_step1 = no, y_step2 = 0xf0, z_step2 = no, n_step2 = yes, c_step2 = no, v_step2 = no, y_step3 = 0x0, z_step3 = yes, n_step3 = no, c_step3 = no, v_step3 = no, y_step4 = 0x0, z_step4 = yes, n_step4 = no, c_step4 = no, v_step4 = no, y_step5 = 0x0, z_step5 = yes, n_step5 = no, c_step5 = no, v_step5 = no
AND, OR and XOR set Z and N from y, and leave C and V at 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: no clock and no stored value.
Hints
Hint 1
Decode the opcode into two adder controls: invert b or not, and which carry in to use (0, 1 or cin). Then one adder serves all four arithmetic operations.
Hint 2
Judge V on what the adder really adds: a and b after the XOR row. An unsigned operand such as cin makes a whole sum unsigned. Z and N come from y.
Hint 3
Give C and V a value of 0 before the case statement, and the case a default arm. Then the reserved opcode has a value too, and nothing needs a latch.
What the grader checks: Eight directed and ten seeded cases of six operations each cover every opcode, with and without carry in. y and the four flags are compared with a model written in JavaScript after every step. The synthesis check rejects a latch.
The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.
18 checks on Icarus Verilog + Yosys · server. 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 3 steps and the final capstone