Design and Implementation of a Simple 8-Bit CPU
Copyright © 1997, 2000 by Rex N. Fisher
5.0 Operation
5.1 The Instruction Cycle
The CPU executes instructions that are stored in memory. The process of retrieving instructions and executing them is called the instruction cycle. Because it is comprised of a fetch operation and an execute operation, it is often called the fetch-execute cycle.
The following steps are performed:
1. Fetch an opcode from memory.
2. Decode the opcode to identify the instruction.
3. Read the required operand(s) from memory.
4. Execute the instruction.
5. Return to Step 1.
Steps 1 and 2 are part of the fetch cycle, while steps 3, and 4 comprise the execution cycle. Steps 1, 2, and 4 are always performed, but it is not always necessary to retrieve additional operands from memory (Step 3).
5.2 Step-By-Step Example of Instruction Execution
Examining the complete instruction cycle for one instruction illustrates the operation of the P8 CPU. Assume the following conditions:
The instruction pointer (IP) contains 07h
The A register, or accumulator (A) contains 13h
Memory address 07h contains 68h (op code for "Subtract Direct From A")
Memory address 08h contains 1Ah
Memory address 1Ah contains 11h
The P8 instruction SUB 1Ah means "Subtract the data in memory location 1Ah from the A register and place the results in the A register". It has the opcode 68h. The second byte of the instruction (1Ah in this case) is the address where the operand will be found. The following pages describe the execution of this instruction.
Fetch Cycle
The first part of the instruction cycle fetches the opcode from memory, places it in the instruction register, and decodes it.
At the beginning of the fetch operation IR contains a 00h and MIP holds a 0h. This sets the control store's address to 000h. The CPU is ready to proceed with Step 1 of the fetch operation.
Step 1
The control bits in CS address 000h trigger the following datapath events:
Event Control Bits
AR <-- IP
!IP_OUT, AR_LOAD
External Address Bus <-- AR !AR_OUT
Assert MEMR
MEMR
The output of IP is enabled and the 07h in IP is placed on the inputs of AR. On CLK2, 07h is loaded into AR. The output of AR is enabled and 07h is placed on the external address bus. MEMR is asserted. See Figure 5.1. MIP increments the CS address to 001h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
|
+----+
|
|
+----+ |
|
| DR | |
|<----------------------------------------- | IN |<------+
|
| |
|
+----+
|
+----+
+----+
|
| IP |
| AR |
Ext Adr Bus
+-----------------> |
|---------+------->| |------------->
|
| 07 |
| | 07
| 07
|
+----+
| +----+
|
+----+ |
|
| OR | |
+-----------------> | |---------+
|
| |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 00 |
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |--> MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13 |
OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Fetch Step 1)
Step 2
The control bits in CS address 001h trigger the following datapath events:
Event Control Bits
External Address Bus <-- AR
!AR_OUT
Assert MEMR
MEMR
DR <-- Op Code
DR(IN)_LOAD
IP <-- IP + 1
IP_CLK_EN
The output of AR continues to be enabled and 07h remains on the external address bus. MEMR remains asserted. On CLK2, the 68h, which was in memory location 07h, is loaded into DR. IP is incremented to 08h. See Figure 5.2. MIP increments the CS address to 002h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | | 68
|
+----+
|
|
+----+ |
|
| DR | |
|<----------------------------------------- | IN |<------+
|
| 68 |
|
+----+
|
+----+
+----+
|
| IP |
| AR |
Ext Adr Bus
+-----------------> | |---------+------->| |------------->
|
| 08 |
| | 07 |
07
|
+----+
| +----+
|
+----+ |
|
| OR | |
+-----------------> | |---------+
|
| |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 00
|
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |--> MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13 |
OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Fetch Step 2)
Step 3
The control bits in CS address 002h trigger the following datapath events:
Event Control Bits
IR <--DR !DR(IN)_OUT, IR_LOAD
The output of DR is enabled and 68h is placed on the internal bus (IB). On CLK2, the 68h is loaded into IR, and "0" is placed on the active-low LOAD input of MIP. See Figure 5.3. MIP loads 0h and the CS address goes to 680h on CLK1. The instruction is "decoded" when the op code in IR is placed on the address bus of CS.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
|
+----+
|
|
+----+ |
|
| DR | |
|<----------------------------------------- | IN |<------+
|
| 68 |
|
+----+
| +----+
+----+
|
| IP |
| AR | Ext Adr Bus
+-----------------> |
|---------+------->| |------------->
|
| 08 |
| | 07
|
|
+----+ | +----+
|
+----+ |
|
| OR | |
+-----------------> |
|---------+
|
| |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> |
|----------------->| CONTROL | :
|
| 68 |
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |-->
MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13 |
OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Fetch Step 3)
Execute Cycle
The remaining steps of the instruction cycle retrieve the operand from memory, execute the ALU operation, and store the result back in the accumulator. In this instruction, the next byte after the op code contains the memory address where the data are located. Two memory accesses are, therefore, required -- once to get the address, and a second time to get the data.
Step 1
The control bits in CS address 680h trigger the following datapath events:
Event Control Bits
AR <-- IP
!IP_OUT, AR_LOAD
External Address Bus <-- AR !AR_OUT
Assert MEMR
MEMR
The output of IP is enabled and the 08h in IP is placed on the inputs of AR. On CLK2, 08h is loaded into AR. The output of AR is enabled and 08h is placed on the external address bus. MEMR is asserted. See Figure 5.4. MIP increments the CS address to 681h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
|
+----+
|
|
+----+ |
|
| DR | |
|<----------------------------------------- | IN |<------+
|
| 68 |
|
+----+
|
+----+
+----+
|
| IP |
| AR |
Ext Adr Bus
+-----------------> | |---------+------->| |------------->
|
| 08 |
| | 08
| 08
|
+----+
| +----+
|
+----+ |
|
| OR | |
+-----------------> | |---------+
|
| |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 68
|
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |--> MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13
| OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Execute Step 1)
Step 2
The control bits in CS address 681h trigger the following datapath events:
Event Control Bits
External Address Bus <-- AR
!AR_OUT
Assert MEMR
MEMR
DR <-- Byte # 2 (Address of Data) DR(IN)_LOAD
IP <-- IP + 1
IP_CLK_EN
The output of AR continues to be enabled and 08h remains on the external address bus. MEMR remains asserted. On CLK2, the 1Ah, which was in memory location 08h, is loaded into DR. IP is incremented to 09h. See Figure 5.5. MIP increments the CS address to 682h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
1A
|
+----+
|
|
+----+ |
|
| DR |
|
|<----------------------------------------- |
IN |<------+
|
| 1A |
|
+----+
|
+----+
+----+
|
| IP |
| AR | Ext Adr Bus
+-----------------> | |---------+------->| |------------->
|
| 09 |
| | 08 |
08
|
+----+
| +----+
|
+----+ |
|
| OR | |
+-----------------> | |---------+
|
| |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 68
|
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |--> MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13
| OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Execute Step 2)
Step 3
The control bits in CS address 682h trigger the following datapath events:
Event Control Bits
OR <--DR !DR(IN)_OUT, OR_LOAD
The output of DR is enabled and 1Ah is placed on the internal bus (IB). On CLK2, the 1Ah is loaded into the operand register (OR). See Figure 5.6. MIP increments the CS address to 683h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
|
+----+
|
|
+----+ |
|
| DR |
|
|<----------------------------------------- |
IN |<------+
|
| 1A |
|
+----+
| +----+
+----+
|
| IP |
| AR | Ext Adr Bus
+-----------------> |
|---------+------->| |------------->
|
| 09
| |
| 08 |
|
+----+ | +----+
|
+----+
|
|
| OR |
|
+-----------------> |
|---------+
|
| 1A |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 68
|
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |-->
MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13
| OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Execute Step 3)
Step 4
The control bits in CS address 683h trigger the following datapath events:
Event Control Bits
AR <-- OR
!OR_OUT,
AR_LOAD
External Address Bus <-- AR !AR_OUT
Assert MEMR
MEMR
The output of OR is enabled and the 1Ah in OR is placed on the inputs of AR. On CLK2, 1Ah is loaded into AR. The output of AR is enabled and 1Ah is placed on the external address bus. MEMR is asserted. See Figure 5.7. MIP increments the CS address to 684h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
|
+----+
|
|
+----+ |
|
| DR | |
|<----------------------------------------- | IN |<------+
|
| |
|
+----+
|
+----+
+----+
|
| IP |
| AR | Ext Adr Bus
+-----------------> | |---------+------->|
|------------->
|
|
| |
| 1A | 1A
|
+----+ |
+----+
|
+----+ |
|
| OR |
|
+-----------------> | |---------+
|
| 1A |
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 68
|
|
LOGIC | :
|
+----+
| |--> CN
|
+----+ | |--> MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13
| OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Execute Step 4)
Step 5
The control bits in CS address 684h trigger the following datapath events:
Event Control Bits
External Address Bus <-- AR
!AR_OUT
Assert MEMR
MEMR
DR <-- Operand
DR(IN)_LOAD
The output of AR continues to be enabled and 1Ah remains on the external address bus. MEMR remains asserted. On CLK2, the 11h, which was in memory location 1Ah, is loaded into DR. See Figure 5.8. MIP increments the CS address to 685h on CLK1.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
11
|
+----+
|
|
+----+ |
|
| DR
| |
|<----------------------------------------- |
IN |<------+
|
| 11 |
|
+----+
|
+----+
+----+
|
| IP |
| AR | Ext Adr Bus
+-----------------> | |---------+------->|
|------------->
|
| 09
| | | 1A |
1A
|
+----+ | +----+
|
+----+ |
|
| OR | |
+-----------------> | |---------+
|
| 1A
|
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> | |----------------->| CONTROL | :
|
| 68
|
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |--> MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ | +----+ +----------+
+-----------------> | ALU|--+
|
| |
|
+-->| |-------+ DEFINITIONS:
| |
+----+ |
| +----+ | +----+
| DR: Data Register
| | BUF| | | A |
| AR: Address Register
|<----| |<---+---| |<------+
IP: Instruction Pointer
| | | | 13
| OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Execute Step 5)
Step 6
The control bits in CS address 685h trigger the following datapath events:
Event Control Bits
ALU(B) <-- DR
!DR(IN)_OUT
ALU(A) <-- A
Always
Present
ALU(F) <-- ALU(A) - ALU(B) SUB
A <-- ALU(F)
A_LOAD
IR <-- 00h (FETCH)
IR_RESET
The output of DR is enabled and 11h is placed on the internal bus (IB) and the B input of the ALU. The contents of the accumulator are always present on the A input of the ALU. PAL 2 uses control bit 17 (SUB) to encode the four select bits, mode bit, and carry in bit of the ALU to perform a subtract operation. The results are immediately available on the F output of the ALU. On CLK2 the accumulator is loaded with the results of the subtraction. Control bit 0 (IR_RESET), which is "1", is inverted and latched by PAL 3. The resulting "0" is forwarded, through PAL 2, to the active-low CLR input of the instruction register (IR). This resets IR to 00h (opcode for FETCH). PAL 3 simultaneously latches a "0" on the active-low LOAD of the microinstruction pointer (MIP). See Figure 5.9. Because MIP resets synchronously, nothing happens until CLK1, when MIP is loaded with a 0h. This addresses the first microinstruction of FETCH at CS address 000h and the instruction cycle repeats.
+----+
| DR | Ext Data Bus
+-----------------------------------------> |OUT |-------+----->
|
| | |
|
+----+
|
|
+----+ |
|
| DR |
|
|<----------------------------------------- |
IN |<------+
|
| 11 |
|
+----+
|
+----+
+----+
|
| IP |
| AR | Ext Adr Bus
+-----------------> |
|---------+------->| |------------->
|
| 09
| | | 1A
|
|
+----+ | +----+
|
+----+ |
|
| OR | |
+-----------------> |
|---------+
|
| 1A
|
|
+----+
|
+----+
+----------+
|
| IR |
|
|--> C0
+-----------------> |
|----------------->| CONTROL | :
|
| 00 |
|
LOGIC | :
|
+----+
|
|--> CN
|
+----+ | |-->
MEMR
|
| Z | |
|--> MEMW
|
+--->|
|---->| |--> IOR
|
|
| | |
|--> IOW
|
+----+ |
+----+ +----------+
+-----------------> | ALU|--+
|
| |
| +-->| 02 |-------+ DEFINITIONS:
| | +----+ |
| +----+ |
+----+ | DR: Data
Register
| | BUF| |
| A | | AR:
Address Register
|<----| |<---+---|
|<------+ IP: Instruction Pointer
| | | | 02 |
OR: Operand Register
| +----+ +----+
IR: Instruction Register
| +----+ +----+
ALU: Arithmetic Logic
Unit
| | BUF| | R |
Z: Zero (Condition Bit)
Reg
|<----| |<------ | |<------+
A: A Register (Accumulator)
| | |
| | | R: R Register
| +----+ +----+
| BUF: 3-State Buffer
+--------------------------------+ C0-CN: Register Control Bits
MEMR:
Memory Read Cntrl Bit
MEMW:
Memory Write Cntrl Bit
IOR:
IO Read Control Bit
IOW:
IO Write Control Bit
/8:
8-Bit Parallel Bus
/1:
1-Bit Line
Figure 5.1: Instruction Cycle for SUB 1Ah (Execute Step 6)
[Back to Top][Back to Table of Contents][<==Previous Section][Next Section==>]