A Processor in One Picture
A processor is a few registers, a program counter and one loop. It touches the rest of the chip only through bus transfers.
You Will Be Able To
- Name the parts inside a processor and the job of each.
- Say which instructions cause a bus transfer, and which do not.
- Explain how a jump lets a program repeat steps and make decisions.
- Lesson
- S0.6
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
The machine of S0.1 could add two numbers and stop. It could not count, repeat a step or make a choice. Two changes fix that, and the result is a processor in the shape that real chips use.
Before You Start
You need S0.1: registers, the PC, and fetch, decode, execute. You need S0.2: devices at addresses, and hexadecimal. You need S0.3: a transfer and the manager.
| Question | |
|---|---|
| 1 | What are the parts inside a processor? |
| 2 | Which instructions cause a bus transfer, and how many transfers does each one cost? |
| 3 | Why does arithmetic work on registers and not on memory? |
| 4 | What does a jump do, and what does a branch add to it? |
| 5 | How does the processor tell memory from a device? |
The Idea: Registers Inside, Transfers Outside
A processor keeps a handful of registers and does all its arithmetic on them. Memory and devices are outside, at the far end of the bus. The processor is the manager of that bus. It reaches the outside in only three ways.
| What the processor does | Bus transfer | When |
|---|---|---|
| Fetch an instruction | a read, at the address in the PC | at the start of every instruction |
| Load | a read, at the address in the instruction | only for a load instruction |
| Store | a write, at the address in the instruction | only for a store instruction |
Everything else happens inside, with no transfer. That is the first change from S0.1: arithmetic now works on registers only. A bus transfer costs time, as S0.3 to S0.5 showed. A register is a few flip-flops whose value is there at once.
The second change is the jump: an instruction that writes a new address into the PC. The next fetch then comes from that address. A jump that looks at a register first, and jumps only if a condition holds, is called a branch. With a branch, a program can repeat steps until a count runs out. It can also choose between two paths.
New Words
| Word | Meaning |
|---|---|
| Jump | An instruction that writes a new address into the PC. |
| Branch | A jump with a condition. It is taken if the condition holds. If not, the PC steps on as usual. |
| Instruction set | The full list of instructions one processor understands, and how each is written as a number. |
The teaching processor on this page has four registers, r0 to r3, and six instructions. A memory box now holds sixteen bits, so an instruction is four hex digits.
| Instruction | What it does | Kind | Bus transfers |
|---|---|---|---|
| HALT | Stop. | 0 | fetch only |
| LOAD r, n | Copy the content of address n into register r. | 1 | fetch, then one read |
| ADD r, s | Add register s to register r. | 2 | fetch only |
| STORE r, n | Copy register r to address n. | 3 | fetch, then one write |
| SUB r, s | Subtract register s from register r. | 4 | fetch only |
| JUMPNZ r, n | A branch: if register r is not 0, the PC becomes n. | 5 | fetch only |
An instruction has four hex digits. They are the kind, the register number, a spare 0, and then n or s. So 0x110C is LOAD r1, 12.
One Picture: The Parts and the Door
- Processor
- Bus
- Memory
- Peripheral
- Fetch
The processor reads the box whose address is in the PC. That is one bus transfer, a read. Every instruction starts this way.
- Decode
The instruction is split into its fields: the kind, a register number, and an address or a second register.
- LOAD
A second bus transfer, a read. The value goes into one register.
- ADD and SUB
Two registers go in and one result comes back. The bus is not used at all.
- STORE
A second bus transfer, a write. Here the address is 14, so the decoder picks the lamp register.
- JUMPNZ
No bus transfer. If the named register is not 0, the address in the instruction is written into the PC.
Read the diagram as text
- Program counter (PC): the address of the next instruction
- Decode: splits the instruction into its fields
- Registers r0 to r3: four numbers, reached at once
- Add and subtract: works on two registers
- Bus: the only door: one transfer at a time
- Memory: addresses 0 to 13
- Lamp register: address 14
- Program counter (PC) → Bus: fetch: read at the PC
- Bus → Decode: the instruction
- Bus → Registers r0 to r3: LOAD: the value read
- Registers r0 to r3 → Bus: STORE: the value to write
- Registers r0 to r3 → Add and subtract: two registers
- Add and subtract → Registers r0 to r3: the result
- Decode → Program counter (PC): JUMPNZ: a new PC
- Bus → Memory: memory addresses
- Bus → Lamp register: the lamp address
A teaching processor. Real ones have more registers and more kinds of instruction, and the same parts.
Count the arrows that touch the bus. There are three: fetch, load and store. Nothing else leaves the processor.
The Smallest Example, Worked by Hand: A Loop
This program counts down from 2 and shows each count on the lamps. Boxes 0 to 5 hold the program. Box 12 holds the count, 2. Box 13 holds the number 1.
| Box | Content | Instruction | Meaning |
|---|---|---|---|
| 0 | 0x110C | LOAD r1, 12 | r1 = the count |
| 1 | 0x120D | LOAD r2, 13 | r2 = 1 |
| 2 | 0x310E | STORE r1, 14 | show the count on the lamps |
| 3 | 0x4102 | SUB r1, r2 | one fewer |
| 4 | 0x5102 | JUMPNZ r1, 2 | not zero yet: back to box 2 |
| 5 | 0x0000 | HALT | stop |
Now be the processor. Each row is one instruction. The last column is the address of the next fetch.
| PC | Instruction | Second transfer | Afterwards | Next PC |
|---|---|---|---|---|
| 0 | LOAD r1, 12 | read address 12 | r1 = 2 | 1 |
| 1 | LOAD r2, 13 | read address 13 | r2 = 1 | 2 |
| 2 | STORE r1, 14 | write address 14 | lamps = 2 | 3 |
| 3 | SUB r1, r2 | none | r1 = 1 | 4 |
| 4 | JUMPNZ r1, 2 | none | r1 is 1, so the branch is taken | 2 |
| 2 | STORE r1, 14 | write address 14 | lamps = 1 | 3 |
| 3 | SUB r1, r2 | none | r1 = 0 | 4 |
| 4 | JUMPNZ r1, 2 | none | r1 is 0, so the PC steps on | 5 |
| 5 | HALT | none | the processor stops |
Six boxes of program ran as nine instructions, because boxes 2 to 4 ran twice. A branch that sends the PC back makes a program repeat like this. Count the bus transfers: nine fetches, two loads and two stores.
The Rules
Rule 1: Every instruction starts with a fetch.
The fetch is a bus read at the address in the PC.
Rule 2: Only a load or a store makes a second transfer.
A load is a bus read. A store is a bus write. Each names one address.
Rule 3: Arithmetic works on registers.
A value from memory must be loaded first. A result that must last is stored afterwards.
Rule 4: The PC steps on by one, unless a jump writes it.
A branch writes it only when its condition holds.
Rule 5: The processor sees only addresses.
It cannot tell memory from a device. The decoder on the bus picks the owner, as in S0.2.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy. Registers as the operands of arithmetic, and load and store as the only data transfers: section 2.3. Branches: section 2.7. The parts of a processor: sections 4.1 and 4.3.
The four registers, the six instructions and their four-digit form are our own teaching processor. No real processor uses them.
The RISC-V names near the end of this page come from the RISC-V rule book, which is public. Our reference library does not hold it, so those facts are marked as RISC-V facts.
Now Run It
The lab at the end of this page is this processor in SystemVerilog. Its bus is cut down to two routines, bus_read and bus_write. One call is one transfer. The lab does not model the clock cycles a transfer takes: S0.3 to S0.5 covered those.
pc=0 fetch 110c LOAD r1, 12 bus read 12: r1=2
pc=1 fetch 120d LOAD r2, 13 bus read 13: r2=1
pc=2 fetch 310e STORE r1, 14 bus write 14: 2
pc=3 fetch 4102 SUB r1, r2 no bus: r1=1
pc=4 fetch 5102 JUMPNZ r1, 2 no bus: next pc=2
pc=2 fetch 310e STORE r1, 14 bus write 14: 1
pc=3 fetch 4102 SUB r1, r2 no bus: r1=0
pc=4 fetch 5102 JUMPNZ r1, 2 no bus: next pc=5
pc=5 fetch 0000 HALT
bus transfers: 9 fetches, 2 loads, 2 storesCompare it with your hand table. Rule 1 at work: every row begins with a fetch. Rule 2 at work: only the LOAD and STORE rows name a second transfer. Rule 4 at work: the first JUMPNZ row ends with next pc=2, and the second with next pc=5.
The last row counts the transfers: 9 fetches, 2 loads and 2 stores. Of 13 transfers, 9 are fetches. That is why S0.5 put memory on the fast bus.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Arithmetic on registers only | Add a memory box directly, as S0.1 did | Most instructions need no second transfer, so they are quick. Values must be loaded first. |
| A jump is an ordinary instruction | Special wiring for every loop | Any program can loop or choose. The PC is the only thing that changes. |
| Every instruction has the same size | Short and long instructions | The fetch always reads one box, and decode is simple. Some bits go unused. |
| One door, the bus | Separate wires to memory and to each device | Adding a device changes nothing in the processor. |
The Trap: A Loop That Never Ends
A colleague sets the count in box 12 to 0, to switch the lamps off. The chip seems to stop responding. Nothing is broken. The processor is very busy.
Follow the first round. The STORE writes 0 to the lamps. The SUB takes 1 from 0. A register of sixteen bits cannot hold a number below 0, so it wraps round to 65,535. That is not 0, so the branch is taken. The loop now counts all the way down from 65,535.
We ran it. The lab with a count of 0 ended with 196,611 fetches and 65,536 stores. A loop that tests after it subtracts runs at least once, and a count of 0 means 65,536 rounds.
At Scale
This section looks ahead. The processor of ap_soc is named ap_rv32. It follows RISC-V, the public rule book of S0.1, and it is this page grown larger.
Registers
RISC-V has 32 registers, x0 to x31, each 32 bits wide. Register x0 always reads as 0, which gives every program a free zero.
The same six jobs
Our LOAD is the RISC-V instruction lw, short for "load word". STORE is sw. ADD and SUB are add and sub. JUMPNZ is close to bne, "branch if not equal", which compares two registers.
The PC counts bytes
A RISC-V instruction is 32 bits wide, which is four bytes. Each address names one byte, so the PC steps on by 4.
The fast bus
In ap_soc every fetch, load and store is an AHB transfer. A load from a device register crosses the bridge, as in S0.5.
More than one instruction at a time
A real processor starts the next fetch before the current instruction has finished. A later module of this track builds that.
The register count, the register x0, the instruction names and the instruction width are RISC-V facts, taken from that rule book. The count of about 40 instructions is its smallest 32-bit instruction set.
Try It
Run the lab. Match each printed row to one row of your hand table.
Change the count in box 12 to 4. Predict the three numbers in the last row.
Make the loop count up: replace SUB by ADD and predict what the lamps show in the first five rounds.
Change the STORE to address 11. Predict what the lamps show at the end, and which box changes.
pc=0 fetch 110c LOAD r1, 12 bus read 12: r1=2 pc=1 fetch 120d LOAD r2, 13 bus read 13: r2=1 pc=2 fetch 310e STORE r1, 14 bus write 14: 2 pc=3 fetch 4102 SUB r1, r2 no bus: r1=1 pc=4 fetch 5102 JUMPNZ r1, 2 no bus: next pc=2 pc=2 fetch 310e STORE r1, 14 bus write 14: 1 pc=3 fetch 4102 SUB r1, r2 no bus: r1=0 pc=4 fetch 5102 JUMPNZ r1, 2 no bus: next pc=5 pc=5 fetch 0000 HALT bus transfers: 9 fetches, 2 loads, 2 storesCheck Yourself
Common Mistakes
Wrong Think of ADD as reaching into memory.
On this processor, and on most real ones, arithmetic sees registers only.
Fix Load the value first. Add. Store the result if it must last.
Wrong Forget the fetch when counting bus transfers.
Every instruction costs one read before it does anything.
Fix Count one fetch for each instruction, plus one transfer for each load or store.
Wrong Start a countdown loop with a count of 0.
The loop subtracts before it tests, so 0 wraps round to the largest number.
Fix Test the count before the first round, or make sure it is never 0.
Remember
- A processor is a PC, a few registers, an adder and a decoder, running the loop of S0.1.
- Every instruction costs one fetch; only a load or a store adds a second bus transfer.
- Arithmetic works on registers, because a register needs no bus transfer.
- A jump writes the PC; a branch does so only when its condition holds. That makes loops and choices.
- The processor sees addresses only. The decoder decides between memory and a device.