Problems

18. Small ALU with flags

MediumWrite the moduleAbout 25 min
combinationalarithmeticalusigned

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

Module alu4

PortDirBitsWhat it is
ain4First operand.
bin4Second operand.
opin3Operation select, 0 to 7.
yout4Result.
carryout1Carry, borrow or shifted-out bit.
zeroout11 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.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Verilogalu4.v
14px
Loading the editor…

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.

Small ALU with flags: Verilog interview question | breadboardhub