ALU Module 3D Rendering

This portion of the CPU is responsible for all of the arithmetic, logic, and boolean functionality. It also includes a flags register as well as a conditional jump logic module. Internally it uses the A and B data buses to move local data around. It is also connected to the main 8-bit data bus to transfer values to the accumulator and operand registers as well as output the result of the given function. It is also used to restore the flags register during an interrupt return instruction.

ALU Control Module

FUNCTIONDESCRIPTIONFlags AffectedCONTROL
PASS A Outputs the accumulator(A) to the data bus0x20
INC Increment, A + 1CF, SF, OF, ZF0x25
DEC Decrement, A – 1CF, SF, OF, ZF0x26
ADD Addition, A + BCF, SF, OF, ZF0x21
ADC Addition w/ Carry, A + B + Carry FlagCF, SF, OF, ZF0x22
SUBSubtract, A – BCF, SF, OF, ZF0x23
SBBSubtract w/ Carry, A – B – Carry FlagCF, SF, OF, ZF0x24
NOTInvert A, ~A0x30
ANDBoolean AND, A & BSF, ZF, OF: Cleared, CF: Cleared0x27
NANDBoolean NAND, ~(A & B)SF, ZF, OF: Cleared, CF: Cleared0x35
ORBoolean OR, A | BSF, ZF, OF: Cleared, CF: Cleared0x28
NORBoolean NOR, ~(A | B)SF, ZF, OF: Cleared, CF: Cleared0x38
XORBoolean XOR, A ^ BSF, ZF, OF: Cleared, CF: Cleared0x29
XNORBoolean XNOR, ~(A ^ B)SF, ZF, OF: Cleared, CF: Cleared0x39
SHLShift Left, A << 1CF: Bit shifted out, SF, ZF, OF0x2a
SHRShift Right, A >> 1CF: Bit shifted out, SF, ZF, OF0x2b
ASLSigned Arithmetic Shift Left, A * 2CF: Bit shifted out, SF, ZF, OF0x2c
ASRSigned Arithmetic Shift Riht, A / 2CF: Bit shifted out, SF, ZF, OF0x2d
RORRotate RightCF: Bit shifted out, SF, ZF, OF0x2e
ROLRotate LeftCF: Bit shifted out, SF, ZF, OF0x2f
CMPCompare, same as SUB but does not latch Flags0x23*
TSTTest, same as AND but does not latch Flags0x27*
ALU Instructions

Accumulator and Operand Registers

  • Latch Accumulator – Loads the value present on the data bus into the Accumulator (A) Register to be used by the various ALU functions.

  • Latch Operand – Loads the value present on the data bus into the Operand (B) Register to be used by the various ALU functions.

Control Unit Schematic Drawing

ALU Control Unit Schematic
ALU Control Unit

Arithmetic Module

This module is built with two 4-bit Full Adders to perform addition on two 8-bit values. It uses two’s complement for Subtraction by XOR’ing the operand (B) with 1 and adding 1. It also has the ability to increment or decrement the accumulator.

Arithmetic Control Truth Table

InstructionMuxTwo’s CompCarry In
A + B000
A – B011
A + B + Carry00Carry Flag Value
A – B – Carry01Carry Flag Value
INC ( A + 1)111
DEC ( A – 1)100
Arithmetic Control Word Truth Table

Arithmetic Examples

FunctionABCFResult
ADD0b00110b0001n.c.0b0100
ADC0b00110b000110b0101
SUB0b00110b0001n.c.0b0010
SBB0b00110b000110b0001
INC0b0011n.c.n.c.0b0100
DEC0b0011n.c.n.c.0b0010
Arithmetic Examples

Arithmetic Schematic Drawing

Logic Gate Module

This module is responsible for executing logical bitwise operations. It contains only the AND, OR, and NOR functions. To acheive NAND, NOR, and XNOR the result is inverted at the ALU control unit.

Logic Gate Examples

FunctionABResult
AND0b01010b00110b0001
NAND*0b01010b00110b1110
OR0b01010b00110b0111
NOR*0b01010b00110b1000
XOR0b01010b00110b0110
XNOR*0b01010b00110b1001
Logic Gate Examples

*1 – Uses same function, however, the invert control bit is set to active. ie. and => nand

Logic Gate Schematic Drawing

Logic gate Schematic
Logic gate Schematic

Shift & Rotate Module

This module provides the ability to shift bits to the left or right. Can be done logically or arithmetically. The logical shift is unsigned (ignores MSB), whereas Arithmetic Shift is signed, and maintains the most significant bit. This is the equivalent of multiplying/dividing by 2.

Shift & Rotate Truth Table

FunctionShift RightShift LeftRotateArithmetic
SHL1011
SHR0111
ASL1010
ASR0110
ROR0101
ROL1001
Shift & Rotate Control Table

Shift & Rotate Examples

FunctionOperandResult
SHL0b100010010b00010010
SHR0b100010010b01000100
ASL0b100010010b10010010
ASR0b100010010b11000100
ROR0b100010010b11000100
ROL0b100010010b00010011
Shift & Rotate Examples

Shift & Rotate Schematic Drawing

Shift and Rotate Schematic
Shift and Rotate Schematic

Flags Register

Bit Order on Data Bus

BIT 3BIT 2BIT 1BIT 0
Zero (ZF)Overflow (OF)Sign (SF)Carry (CF)
Flags Bit Order on Data Bus
  • Flags Out– Asserts the flags register onto the data bus.
  • Flags In – Latches the flag’s register with calculated values based on the alu function’s result. This is active for all ALU functions except CMP and TST

  • Restore Flags – Used in conjunction with Flags In, this will latch the register with values asserted on the data bus. This is normally used when returning from an interrupt and popping the flags off of the stack.

Flags Regsiter Truth Table

Flags OutFlags InRestore FlagsDescription
11n.c.NOP
01n.c.Assert Flags to Data Bus
100Latch flags from ALU. Used with mostly all ALU functions.
101Latches/Restores flag data from 8-bit data bus. Normally used while returning from interrupt.
Flags Control Truth Table

Flags Register Schematic Drawing

Flags Register Schematic
Flags Register Schematic

Conditional Jump Logic

This module provides all of the conditions for calculating jumps based on ALU Flags. It uses a 4-bit control bus with enable, which is connected to the main CPU’s microcode control logic, the ALU flags and outputs a single control line which is LOW if a jump is active.

MnemonicDescriptionFlagsControl
jpJumpn/a0x0
jle / jngJump if Less Than or Equal / Jump Not GreaterZF = 1 or
SF <> OF
0x1
jg / jnleJump if Greater / Jump if Not Less Than or EqualZF = 0 and
SF = OF
0x2
jge / jnlJump if Greater Than or Equal / Jump if Not LowerSF = OF0x3
jl / jngeJump if Less Than / Jump if Not Greater Than or EqualSF <> OF0x4
ja / jnbeJump if Above / Jump if Not BelowCF = 0 and
ZF = 0
0x5
jbe / jnaJump if Below / Jump if Not AboveCF = 1 or
ZF = 1
0x6
jnb / jae / jncJump if not below / Jump if above or equal / Jump if not carryCF = 00x7
jb / jnae / jcJump if below / Jump if not above or equal / Jump if carryCF = 10x8
jne / jnzJump if not equal / Jump if not zeroZF = 00x9
je / jzJump if equal / Jump if zeroZF = 10xa
jnsJump if not signSF = 00xb
jsJump if signSF = 10xc
jnoJump if not overflowOF = 00xd
joJump if overflowOF = 10xe
Conditional Jump Instructions and Conditions

Conditional Jump Logic Schematic Drawing

Conditional Jump Logic
Conditional Jump Logic