0
0
VHDLprogramming~10 mins

ALU design in VHDL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ALU design
Input A, Input B, Control Signals
Select Operation
Perform Arithmetic or Logic
Output Result
Set Flags (Zero, Carry, Overflow)
The ALU takes two inputs and control signals to select an operation, performs the operation, outputs the result, and sets status flags.
Execution Sample
VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity ALU is
  Port ( A, B : in STD_LOGIC_VECTOR(3 downto 0);
         Op : in STD_LOGIC_VECTOR(2 downto 0);
         Result : out STD_LOGIC_VECTOR(3 downto 0);
         Zero : out STD_LOGIC);
end ALU;
This VHDL code defines an ALU entity with 4-bit inputs A and B, a 3-bit operation code, a 4-bit result output, and a zero flag output.
Execution Table
StepInputs (A, B, Op)Operation SelectedComputationResultZero Flag
1A=0011, B=0001, Op=000Addition3 + 101000
2A=0101, B=0011, Op=001Subtraction5 - 300100
3A=1111, B=0000, Op=010AND1111 AND 000000001
4A=1010, B=0101, Op=011OR1010 OR 010111110
5A=1001, B=0110, Op=100XOR1001 XOR 011011110
6A=0000, B=0000, Op=000Addition0 + 000001
Exit----Execution ends after all test cases
💡 All test cases executed, ALU operations demonstrated.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
A----0011010111111010100100000000
B----0001001100000101011000000000
Op----000001010011100000000
Result----0100001000001111111100000000
Zero----0010011
Key Moments - 3 Insights
Why does the Zero flag become '1' when the result is 0000?
The Zero flag is set to '1' when the ALU output is all zeros, indicating the result is zero, as shown in execution_table rows 3 and 6.
How does the ALU know which operation to perform?
The ALU uses the Op input as a control signal to select the operation, demonstrated in the 'Operation Selected' column of the execution_table.
Why is the result different for the same inputs with different Op codes?
Because the Op code changes the operation (add, subtract, AND, OR, XOR), the same inputs produce different results, as seen in rows 1 to 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Result when A=1010, B=0101, and Op=011?
A1010
B0101
C1111
D0000
💡 Hint
Check row 4 in the execution_table under Result.
At which step does the Zero flag first become '1'?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Look at the Zero Flag column in execution_table rows.
If Op=000 always means addition, what would be the Result for A=0100 and B=0011?
A0111
B0011
C0100
D0000
💡 Hint
Addition sums the two inputs; see how addition works in execution_table row 1.
Concept Snapshot
ALU Design in VHDL:
- Inputs: A, B (4-bit), Op (3-bit control)
- Output: Result (4-bit), Zero flag
- Op selects operation: 000=Add, 001=Sub, 010=AND, 011=OR, 100=XOR
- Result computed based on Op
- Zero flag set if Result is zero
- Simple, modular, and extensible design
Full Transcript
This visual execution traces a simple 4-bit ALU design in VHDL. The ALU takes two 4-bit inputs A and B, and a 3-bit operation code Op. Depending on Op, it performs addition, subtraction, AND, OR, or XOR. The result is output as a 4-bit vector, and a Zero flag is set if the result is zero. The execution table shows step-by-step inputs, selected operation, computation, result, and zero flag status for six test cases. Variable tracking shows how inputs and outputs change each step. Key moments clarify why the Zero flag is set and how Op controls the operation. The quiz tests understanding of results and flags from the table. The snapshot summarizes the ALU design essentials in VHDL.