18. Small ALU with flags
Build a combinational 4-bit ALU. op picks the operation:
0 ADD: y = a + b (low 4 bits); carry = the carry out of bit 3. 1 SUB: y = a - b (low 4 bits); carry = 1 when a borrow was needed (a < b as unsigned numbers). 2 AND: y = a & b. 3 OR: y = a | b. 4 XOR: y = a ^ b. 5 SLT: y = 1 when a < b as signed two's complement numbers (-8 to 7), else 0. 6 SHL: y = a shifted left by one, 0 entering at bit 0; carry = the bit shifted out (a[3]). 7 SHR: y = a shifted right by one, logical (0 entering at bit 3); carry = the bit shifted out (a[0]).
carry is 0 for AND, OR, XOR and SLT. zero is 1 when y is 0, for every operation. There is no clock.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
a | in | 4 | First operand. |
b | in | 4 | Second operand. |
op | in | 3 | Operation select, 0 to 7. |
y | out | 4 | Result. |
carry | out | 1 | Carry, borrow or shifted-out bit. |
zero | out | 1 | 1 when y is 0. |
Example
a = 9, b = 7, ADD: 16 does not fit, so y = 0, carry = 1, zero = 1. a = 1110 (-2), b = 0011 (3), SLT: y = 1.
Written hints
AI hint
Submissions
No submissions yet. Submit your answer and it shows here. Sign in to keep them.
Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.
Press Build to check your code with Yosys. Errors and warnings show here, and a clean build draws the logic diagram.
Build your design to drive it by hand and see its signals here.