CS 152 Computer Architecture and Engineering
CS252A Graduate Computer Architecture Lecture 2 - Simple Machine Implementations: Microcode

Chris Fletcher
UC Berkeley, Spring 2026

Timeline of (Implemented) Machines

Fixed functionabacus, compass Semi-programmableno branchescalculators Programmablehard to reprogramENIAC Stored programTuring machine,EDVAC, EDSAC Stored program+ ISA decoupledIBM 360

ISA to Microarchitecture Mapping

ISA styleTypical implementation
AccumulatorHardwired, unpipelined
CISCMicrocoded
RISCHardwired, pipelined
VLIWFixed-latency in-order parallel pipelines
JVMSoftware interpretation

Why Learn Microprogramming?

Plan: specify RV32I → build a single-bus datapath → control it with microcode → shrink the microcode ROM

RISC-V ISA

RV32I Processor State

XLEN = 32 bits x00 (hardwired) x1 x2 x3 ⋮ x30 x31 pc
  • Program counter pc
    • 32-bit byte address of the current instruction
  • 32 integer registers x0–x31
    • Each 32 bits wide
    • x0 (zero) always reads 0
    • Writes to x0 are ignored

RISC-V Instruction Encoding

RV32I Base Instruction Formats

312524201915141211760 funct7rs2rs1funct3rdopcode imm[11:0]rs1funct3rdopcode imm[11:5]rs2rs1funct3imm[4:0]opcode 12imm[10:5]rs2rs1funct3imm[4:1]11opcode imm[31:12]rdopcode 20imm[10:1]11imm[19:12]rdopcode Rows: R, I, S, B, U, J. Yellow = immediate bits. rd, rs1, rs2 never move; sign bit is always bit 31; opcode[1:0] = 11.

Example: Decoding a B-type Immediate

1 1 111111 1110 0  =  −4 (two's complement)

Check Your Understanding

1. Quick decoding check: in all 32-bit RV32I formats, which bit holds the immediate's sign bit, and what are opcode[1:0] always equal to?
Bit 31 holds the sign bit; opcode[1:0] is always 11.
2. What would happen if the processor executes ADD x0, x1, x2?
x0 stays 0: it is hardwired to zero and writes to it have no effect.

Control versus Datapath

Control Datapath PC Inst.Reg. Regs ALU Control lines Inst., Cond? MainMemory Addr Data Busy?
  • Datapath: where state is stored and arithmetic happens
  • Control: sequences datapath operations
    • Inputs: instruction/opcode, Condition? from the ALU, Busy? from memory
    • Outputs: control lines

Technology Influence: Why Microcode?

Microcoded CPU

Control µPC Decoder Microcode ROM(control store) Next state Busy?, Opcode, Cond? Control lines Datapath Main Memory holds user program inmacroinstructions(x86, RISC-V) Address Data memory control

Pay Close Attention!

The µPC is NOT the PC!

PCµPC
KindArchitectural: visible to softwareMicroarchitectural: invisible to software
Lives inDatapathControl
Points toCurrent RISC-V instruction in main memoryCurrent row of the microcode ROM
StepsOnce per instructionMany times per instruction (one per cycle)

Single-Bus Datapath for Microcoded RISC-V

32-bit bus InstructionReg. InstLd Opcode → Control Immediate ImmEnImmSel Register RAM x0 … x31+ PC at addr 32 RegSel: rs1|rs2|rd|32 RegEn RegW rs1, rs2, rd A B ALd BLd ALU ALUEn ALUOpCondition? → Control MA MALd MainMemory MemEn MemW Busy? → Control

One source drives the bus per cycle (*En); any number of destinations latch on the rising edge (*Ld).

Datapath Components and Signals

  1. Instruction Reg. (IR): latches bus when InstLd=1; sends opcode to control, fields to datapath
  2. Immediate: sign-extends the field picked by ImmSel (ImmI/S/B/U/J); drives bus when ImmEn=1
  3. Register RAM: x0–x31 plus PC at address 32; RegSel = rs1|rs2|rd|32
    • Read: RegW=0, RegEn=1  ·  Write: RegW=1, RegEn=0
  4. A, B, ALU: ALd, BLd load operands; ALU does ALUOp, outputs Condition?, drives bus when ALUEn=1
  5. MA + Main Memory: MALd latches address; read when MemEn=1, write when MemW=1; asserts Busy? until done

Register-Transfer Notation

Register transferControl signals asserted
MA:=PCRegSel=PC; RegW=0; RegEn=1; MALd=1
B:=Reg[rs2]RegSel=rs2; RegW=0; RegEn=1; BLd=1
Reg[rd]:=A+BALUop=Add; ALUEn=1; RegSel=rd; RegW=1

Check Your Understanding

1. True or False: the µPC and the PC hold the same address during instruction execution.
False. PC is architectural and points to the current RISC-V instruction in memory; µPC is microarchitectural and points to the current microinstruction in the ROM.
2. Quick translation: write the non-zero control signals for B:=ImmI.
ImmSel=ImmI; ImmEn=1; BLd=1 (all other enables, loads, writes = 0).

RISC-V Instruction Execution Phases

  • Instruction fetch
  • Instruction decode
  • Register fetch
  • ALU operations
  • Optional memory operations
  • Optional register writeback
  • Calculate next instruction address

Instruction fetch microcode:

MA,A:=PCsend PC to memory, save in A
PC:=A+4increment PC
wait for memoryspin while Busy?
IR:=Memlatch instruction
dispatch on opcode

In the ROM table later, PC:=A+4 is scheduled after IR:=Mem so the bus is free during the memory wait.

Microcode: ALU, ALUI, and Load

ALU (R-type, e.g. ADD)ALUI (I-type, e.g. ADDI)LW (load word)
A:=Reg[rs1]A:=Reg[rs1]A:=Reg[rs1]
B:=Reg[rs2]B:=ImmIB:=ImmI
Reg[rd]:=ALUOp(A,B)Reg[rd]:=ALUOp(A,B)MA:=A+B
goto fetchgoto fetchwait for memory
Reg[rd]:=Mem
goto fetch

Microcode: JAL and Branch

JAL (entering with A = PCorig+4)

Reg[rd]:=Areturn address
A:=A-4recover PCorig
B:=ImmJ
PC:=A+B
goto fetch

Skipping A:=A-4 would offset the target by +4.

Branch

A:=Reg[rs1]
B:=Reg[rs2]
if !ALUOp(A,B) goto fetchnot taken: PC already +4
A:=PCtaken path
A:=A-4recover PCorig
B:=ImmB
PC:=A+B
goto fetch

Payoff: Complex Instructions, Same Datapath

RowMicroinstructionNext
MMA0MA:=Reg[rs1]next
MMA1A:=Memspin
MMA2MA:=Reg[rs2]next
MMA3B:=Memspin
MMA4MA:=Reg[rd]next
MMA5Mem:=ALUOp(A,B)spin
MMA6(idle)fetch

Check Your Understanding

1. Trace the pitfall: in Branch, why must the microcode execute A:=A-4 after A:=PC when the branch is taken?
PC was already incremented to PC+4 during fetch, but ImmB is relative to the branch's own address; A:=A-4 recovers it.
2. Yes or No: does adding M[rd] = M[rs1] + M[rs2] require changing the single-bus datapath?
No. The datapath does the three memory accesses and the add sequentially over several cycles; only new microcode rows are needed.

Pure ROM Implementation

µPC Opcode Cond? Busy? Address ROM Data Next µPC Control signals

$|\mu\text{address}| = |\mu\text{PC}| + |\text{opcode}| + 1 + 1$

$|\text{data}| = |\mu\text{PC}| + |\text{control}| = |\mu\text{PC}| + 18$

$\text{Size} = 2^{|\mu\text{address}|} \times |\text{data}|$

Pure ROM Contents: Mostly Don't-Cares

µPCOpcodeCond?Busy?ControlNext µPC
fetch0XXXMA,A:=PCfetch1
fetch1XX1fetch1
fetch1XX0IR:=Memfetch2
fetch2ALUXXPC:=A+4ALU0
fetch2ALUIXXPC:=A+4ALUI0
fetch2LWXXPC:=A+4LW0
ALU0XXXA:=Reg[rs1]ALU1
ALU1XXXB:=Reg[rs2]ALU2
ALU2XXXReg[rd]:=ALUOp(A,B)fetch0

Pure ROM Size for Our RISC-V Machine

$2^{13} \times 24 = 196{,}608 \text{ bits} = 24\text{ KiB}$

Reducing Control Store Size

Single-Bus RISC-V Microcode Engine

µPC Address (µPC only) µcode ROM64 rows Controls µPC jump (3b) µPC Jump LogicCond?, Busy? mux µPC+1fetch0Decode(Opcode)
µPC jumpMeaning
nextµPC + 1
spinµPC if Busy?, else µPC + 1
fetchfetch0
dispatchDecode(Opcode)
ftruefetch0 if Cond? true, else µPC + 1
ffalsefetch0 if Cond? false, else µPC + 1

Encoded ROM Contents

AddressControlJump
fetch0MA,A:=PCnext
fetch1IR:=Memspin
fetch2PC:=A+4dispatch
ALU0A:=Reg[rs1]next
ALU1B:=Reg[rs2]next
ALU2Reg[rd]:=ALUOp(A,B)fetch
Branch0A:=Reg[rs1]next
Branch1B:=Reg[rs2]next
Branch2A:=PCffalse
Branch3A:=A-4next
Branch4B:=ImmBnext
Branch5PC:=A+Bfetch
  • No X columns left
  • Branch2 tests Cond? on Reg[rs1], Reg[rs2] during the cycle; A latches PC only at the clock edge

$64 \times (3 + 18) = 1{,}344$ bits

= 168 bytes, >146× smaller

Horizontal vs. Vertical Microcode

Horizontal µcodeVertical µcode
WidthWider µinstructionsNarrower µinstructions
Ops per stepMultiple parallel operationsTypically one datapath op
BranchesFolded into each µinstructionSeparate branch µinstructions
Steps per instructionFewerMore
EncodingSparseCompact

Nanocoding

  • Exploits recurring control patterns
    • e.g., A:=Reg[rs1] in both ALU0 and ALUI0
  • µPC indexes a narrow µcode ROM
    • Outputs a next-state jump, or
    • A nanoaddress into a wide nanoinstruction ROM
  • Each unique horizontal pattern stored once
Motorola 68000
  • 17-bit µcode ROM entries
  • 10-bit µjump or 9-bit nanoinstruction pointer
  • 68-bit nanoinstruction ROM
  • Decodes into 196 control signals

Check Your Understanding

1. Quick computation: a pure-ROM controller has 5 bits of µPC, a 4-bit opcode, 1 Cond? bit, 1 Busy? bit, and 15 control signals. Total ROM size?
$|\mu\text{address}| = 5+4+1+1 = 11$, $|\text{data}| = 5+15 = 20$, so $2^{11} \times 20 = 40{,}960$ bits.
2. Concept check: why doesn't Branch2 (A:=PC with ffalse) corrupt A before Condition? is checked?
Registers update on the rising edge. During Branch2, A still holds Reg[rs1], so the ALU computes Cond? on rs1/rs2; A latches PC only at the end of the cycle, if µPC falls through to Branch3.

Microprogramming in IBM 360

Microprogramming Thrived in the 60s and 70s

Except for the cheapest and fastest machines, almost every computer was microprogrammed:

  1. ROMs were far faster than DRAM/core memory
  2. For complex ISAs, microcoded control was simpler and cheaper than hardwired logic
  3. New instructions (e.g., floating point) needed no datapath changes
  4. Fixing controller bugs meant swapping a ROM card
  5. Compatibility across wide model families was cheap and easy

Why Microprogramming Retreated (1980s)

  1. Runaway complexity: ISAs like the DEC VAX 11-780 needed huge microprograms with subroutines and call stacks inside µcode
  2. ROM vs. bugs: giant bug-free microprograms were nearly impossible → Writable Control Store (WCS) in fast MOS SRAM (B1700, QMachine, Intel i432)
  3. VLSI erased the ROM/RAM speed gap: logic and fast SRAM caches on the same chip
  4. Better compilers made exotic CISC instructions unnecessary
  5. Pipelining, caches, buffers made multi-cycle microcoded execution of simple register-register ops too slow → hardwired pipelined RISC

User-Writable Control Store Failed

Microprogramming Today

Check Your Understanding

1. Causal check: name two reasons User-Writable Control Store failed in minicomputers.
Any two: no tools, tiny control-store space, ISA-tailored datapaths, expensive context switches (WCS is processor state), protection problems, restartable microcode for page faults.
2. Yes or No: do modern x86 CPUs execute every instruction through microcode?
No. Common instructions use hardwired control; microcode handles rare/complex instructions and boot-time bug or security patches.