1. Instruction Lifecycle Overview
Every instruction in our single-cycle CPU goes through these steps:
- Fetch: PC → Instruction Memory → IR (Instruction Register)
- Decode & Register Read: opcode → Control Unit; rs1/rs2 → Register File
- Execute / Address Calculation: ALU computes result or branch target
- Memory Access: for loads/stores, Data Memory read/write
- Write-Back: result (ALU or Mem) → Register File
- PC Update: PC + 4, branch target, or jump target
2. Case Study: R-Type Arithmetic (add x5, x6, x7
)
add x5, x6, x7
- Fetch: PC=0x1000 → IR=0x00C383B3
- Decode: opcode=0110011 → R-type controls (RegWrite=1, ALUSrc=0, ALUOp=10)
- rs1=x6=3, rs2=x7=4
- Execute: ALUOp=10, funct7=0, funct3=000 → ALUCtrl=ADD → 3 + 4 = 7
- Memory: no access (MemRead=0, MemWrite=0)
- Write-Back: x5 ← 7
- PC Update: PC ← PC + 4 = 0x1004
3. Case Study: Load Word (lw x8, 12(x9)
)
lw x8, 12(x9)
- Fetch: PC=0x1004 → IR=0x00C4C503
- Decode: opcode=0000011 → Load controls (RegWrite=1, MemRead=1, MemToReg=1, ALUSrc=1, ALUOp=00)
- rs1=x9=0x2000, imm=12
- Execute: ALUOp=00 → ALUCtrl=ADD → address = 0x2000 + 12 = 0x200C
- Memory: read Mem[0x200C] → 0x12345678
- Write-Back: x8 ← 0x12345678
- PC Update: PC ← 0x1008
4. Case Study: Store Word (sw x10, 8(x11)
)
sw x10, 8(x11)
- Fetch: PC=0x1008 → IR=0x00A5C023
- Decode: opcode=0100011 → Store controls (MemWrite=1, ALUSrc=1, ALUOp=00)
- rs1=x11, rs2=x10, imm=8
- Execute: ALU computes address = x11 + 8
- Memory: write x10 to Mem[address]
- PC Update: PC ← PC + 4 = 0x100C
5. Case Study: Conditional Branch (beq x1, x2, +12
)
Assume PC=0x100C, offset=+12
beq x1, x2, label # offset=12
- Fetch: IR=0x00C10263
- Decode: opcode=1100011 → Branch controls (Branch=1, ALUOp=01)
- rs1=x1, rs2=x2, imm=12
- Execute: ALU subtracts x1 − x2; Zero flag = (x1x2)
- PC Update: if Zero & Branch → PC = 0x100C + 12 = 0x1018 else → PC = 0x1010
6. Case Study: Jump and Link (jal x3, +16
)
Assume PC=0x1010, offset=16
jal x3, 16
- Fetch: IR=0x010001EF
- Decode: opcode=1101111 → Jump controls (Jump=1)
- Execute: calculate target = PC + 16
- Write-Back: x3 ← PC + 4 = 0x1014
- PC Update: PC ← 0x1020