Find the first cycle where an ALU gives a wrong result or zero flag, and name the cause.
Hardware context: The ALU of a small controller that adds wide numbers in steps and branches on its flags. Find the first wrong cycle of an ALU and name the bug.
A small ALU computes y and a zero flag z. Its test ends with: ERROR: cycle 3 op 1 09 04 gave y=04 z=0, expected y=05 z=0. The bench is correct. The bug is in alu4.
Run the failing test to see its log and waveform. In each cycle the bench applies one operation, lets the ALU settle, and checks y and z.
Step 1: find the first cycle where y or z shows a value a correct ALU would not, and name the signal. Step 2: choose the cause from four possible bugs; each is real and fails in a different cycle. Step 3 is optional: fix alu4 and run the tests (lesson G4.11).
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 144 | input of each check |
Full specification
module alu4 (
input logic [1:0] op, // 0 ADD, 1 SUB, 2 AND, 3 OR
input logic [7:0] a, b,
output logic [7:0] y,
output logic z // 1 when y is zero
);
...
endmodule
| op | Operation | y |
|---|---|---|
| 0 | ADD | a + b |
| 1 | SUB | a − b |
| 2 | AND | a & b |
| 3 | OR | a | b |
| Cycle | Operation | a | b |
|---|---|---|---|
| 0 | ADD | ff | 01 |
| 1 | OR | 0f | 3c |
| 2 | AND | 0f | 3c |
| 3 | SUB | 09 | 04 |
| 4 | ADD | 80 | 80 |
| 5 | SUB | 10 | 10 |
| 6 | OR | 00 | 00 |
| 7 | SUB | 20 | 01 |
z is 1 when the 8-bit y is zero. Cycle N is the stretch where the cycle counter shows N. The fix tests apply eight operations, one per nanosecond, and record y and z after each.
Diagnose the Failing Run
The test below runs the buggy design. Read its log, then find where it first goes wrong in the waveform.
Log
Waveform
Click a signal at a cycle to select it. Or focus the waveform and use the arrow keys: Left and Right move one cycle, Up and Down one signal.
Step 3 is optional: fix the code in the editor, then run the tests.
Examples
Example 1
One of Each Operation- Input
ADD 03 04; SUB 09 04; AND 0f 3c; OR 0f 3c; ADD 00 00; SUB 05 05; AND f0 0f; OR 00 00
- Output
y_step0 = 0x7, z_step0 = no, y_step1 = 0x5, z_step1 = no, y_step2 = 0xc, z_step2 = no, y_step3 = 0x3f, z_step3 = no, y_step4 = 0x0, z_step4 = yes, y_step5 = 0x0, z_step5 = yes, y_step6 = 0x0, z_step6 = yes, y_step7 = 0x0, z_step7 = yes
3 + 4 = 07, 9 − 4 = 05, 0f AND 3c = 0c, 0f OR 3c = 3f. 0 + 0 and 5 − 5 give zero, so z = 1 there.
Example 2
Subtractions- Input
SUB 10 01; SUB 01 10; SUB 80 7f; SUB 00 00; SUB ff 01; SUB 3c 3c; SUB 20 01; SUB 07 08
- Output
y_step0 = 0xf, z_step0 = no, y_step1 = 0xf1, z_step1 = no, y_step2 = 0x1, z_step2 = no, y_step3 = 0x0, z_step3 = yes, y_step4 = 0xfe, z_step4 = no, y_step5 = 0x0, z_step5 = yes, y_step6 = 0x1f, z_step6 = no, y_step7 = 0xff, z_step7 = no
Each result is a − b in eight bits: 01 − 10 wraps to f1, and 3c − 3c is zero with z = 1.
Example 3
Zero Results with and Without a Carry- Input
ADD ff 01; ADD 80 80; SUB 42 42; AND aa 55; OR 00 00; ADD 00 00; ADD fe 02; SUB 00 00
- Output
y_step0 = 0x0, z_step0 = yes, y_step1 = 0x0, z_step1 = yes, y_step2 = 0x0, z_step2 = yes, y_step3 = 0x0, z_step3 = yes, y_step4 = 0x0, z_step4 = yes, y_step5 = 0x0, z_step5 = yes, y_step6 = 0x0, z_step6 = yes, y_step7 = 0x0, z_step7 = yes
ff + 01 and 80 + 80 carry out of bit 7, yet the 8-bit result is zero, so z = 1.
Constraints and Contract
- op, a and b are known 0/1 values.
- Keep the module name and the ports.
Hints
Hint 1
Work out y and z yourself for each cycle from the table, and compare both signals with the waveform from cycle 0.
Hint 2
The error message names one cycle, but another bug could show earlier. Check z as carefully as y: ff + 01 is a zero result with a carry.
Hint 3
In the first wrong cycle, compare the wrong value with the right one. Off by one, the result of another operation, and a wrong flag point to different causes.
What the grader checks: The failing run is simulated live with the shipped ALU. The correct and each buggy ALU run on the same eight-cycle bench. That gives the first bad cycle and signal of every option. The optional fix is compared with a model written in JavaScript on five directed and eight seeded scripts of eight operations.
13 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 3 steps and the final capstone