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.

Five questions this page answers
Question
1What are the parts inside a processor?
2Which instructions cause a bus transfer, and how many transfers does each one cost?
3Why does arithmetic work on registers and not on memory?
4What does a jump do, and what does a branch add to it?
5How 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 doesBus transferWhen
Fetch an instructiona read, at the address in the PCat the start of every instruction
Loada read, at the address in the instructiononly for a load instruction
Storea write, at the address in the instructiononly 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

New words on this page
WordMeaning
JumpAn instruction that writes a new address into the PC.
BranchA jump with a condition. It is taken if the condition holds. If not, the PC steps on as usual.
Instruction setThe 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.

The instruction set of the teaching processor
InstructionWhat it doesKindBus transfers
HALTStop.0fetch only
LOAD r, nCopy the content of address n into register r.1fetch, then one read
ADD r, sAdd register s to register r.2fetch only
STORE r, nCopy register r to address n.3fetch, then one write
SUB r, sSubtract register s from register r.4fetch only
JUMPNZ r, nA branch: if register r is not 0, the PC becomes n.5fetch 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

Inside the Processor, and Its One DoorStep through the picture. Each step is one kind of work. Watch which steps use the bus.
  • Processor
  • Bus
  • Memory
  • Peripheral
Inside the Processor, and Its One DoorStep through the picture. Each step is one kind of work. Watch which steps use the bus. component Program counter(PC) the address of thenext instruction component Decode splits the instructioninto its fields component Registers r0 tor3 four numbers,reached at once component Add and subtract works on tworegisters component Bus the only door: onetransfer at a time component Memory addresses 0 to 13 component Lamp register address 14 fetch: read at the PCthe instructionLOAD: the value readSTORE: the value towritetwo registersthe resultJUMPNZ: a new PCmemory addressesthe lamp address
  1. component Program counter (PC)the address of the next instruction
    • fetch: read at the PC → Bus
  2. component Busthe only door: one transfer at a time
    • the instruction → Decode
    • LOAD: the value read → Registers r0 to r3
    • memory addresses → Memory
    • the lamp address → Lamp register
  3. component Memoryaddresses 0 to 13
  4. component Decodesplits the instruction into its fields
    • JUMPNZ: a new PC → Program counter (PC)
  5. component Registers r0 to r3four numbers, reached at once
    • STORE: the value to write → Bus
    • two registers → Add and subtract
  6. component Add and subtractworks on two registers
    • the result → Registers r0 to r3
  7. component Lamp registeraddress 14
  1. Fetch

    The processor reads the box whose address is in the PC. That is one bus transfer, a read. Every instruction starts this way.

  2. Decode

    The instruction is split into its fields: the kind, a register number, and an address or a second register.

  3. LOAD

    A second bus transfer, a read. The value goes into one register.

  4. ADD and SUB

    Two registers go in and one result comes back. The bus is not used at all.

  5. STORE

    A second bus transfer, a write. Here the address is 14, so the decoder picks the lamp register.

  6. 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.

The program
BoxContentInstructionMeaning
00x110CLOAD r1, 12r1 = the count
10x120DLOAD r2, 13r2 = 1
20x310ESTORE r1, 14show the count on the lamps
30x4102SUB r1, r2one fewer
40x5102JUMPNZ r1, 2not zero yet: back to box 2
50x0000HALTstop

Now be the processor. Each row is one instruction. The last column is the address of the next fetch.

Nine instructions, by hand
PCInstructionSecond transferAfterwardsNext PC
0LOAD r1, 12read address 12r1 = 21
1LOAD r2, 13read address 13r2 = 12
2STORE r1, 14write address 14lamps = 23
3SUB r1, r2noner1 = 14
4JUMPNZ r1, 2noner1 is 1, so the branch is taken2
2STORE r1, 14write address 14lamps = 13
3SUB r1, r2noner1 = 04
4JUMPNZ r1, 2noner1 is 0, so the PC steps on5
5HALTnonethe 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

  1. Rule 1: Every instruction starts with a fetch.

    The fetch is a bus read at the address in the PC.

  2. 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.

  3. Rule 3: Arithmetic works on registers.

    A value from memory must be loaded first. A result that must last is stored afterwards.

  4. Rule 4: The PC steps on by one, unless a jump writes it.

    A branch writes it only when its condition holds.

  5. 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.

Printed by the lab, one row for each instruction. After "fetch" comes the instruction as four hex digits. The right-hand part names the second transfer, or says "no bus".
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 stores

Compare 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 choiceThe alternativeWhat the choice buys, and costs
Arithmetic on registers onlyAdd a memory box directly, as S0.1 didMost instructions need no second transfer, so they are quick. Values must be loaded first.
A jump is an ordinary instructionSpecial wiring for every loopAny program can loop or choose. The PC is the only thing that changes.
Every instruction has the same sizeShort and long instructionsThe fetch always reads one box, and decode is simple. Some bits go unused.
One door, the busSeparate wires to memory and to each deviceAdding 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 page4 registers, 6 instructions
ap_rv3232 registers, about 40 instructions

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.

  1. 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.

  2. 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.

  3. 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.

  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.

  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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 stores

Check Yourself

Predict the outputThis is the countdown program with a count of 3. The last line counts the bus transfers of the whole run. What is that line?
// A processor with four registers. It reaches memory and the lamps only through the bus.
module tb;
  logic clk = 0;
  always #5 clk = ~clk;

  // ---- Memory and one device, reached only through the bus ----
  logic [15:0] mem [0:13];     // addresses 0 to 13: a box now holds sixteen bits
  logic [15:0] lamps = 0;      // address 14: the lamp register

  // The bus, reduced to two routines. One call is one transfer.
  // The decoder of S0.2 is inside them: 0 to 13 is memory, 14 is the lamps.
  function logic [15:0] bus_read(input logic [3:0] addr);
    return (addr <= 13) ? mem[addr] : lamps;
  endfunction
  task bus_write(input logic [3:0] addr, input logic [15:0] data);
    if (addr <= 13) mem[addr] = data;
    else            lamps     = data;
  endtask

  // ---- The processor ----
  localparam HALT = 0, LOAD = 1, ADD = 2, STORE = 3, SUB = 4, JUMPNZ = 5;   // the six kinds
  logic [3:0]  pc = 0;         // the program counter
  logic [15:0] r [0:3];        // four registers: r0, r1, r2 and r3
  logic [15:0] word;           // the instruction just fetched
  logic [3:0]  kind, reg_no, arg, next_pc;
  int          fetches = 0, loads = 0, stores = 0;   // counts the bus transfers

  initial begin
    for (int i = 0; i < 14; i++) mem[i] = 0;
    for (int i = 0; i < 4; i++)  r[i]   = 0;
    mem[0]  = 16'h110C;   // LOAD r1, 12     r1 = the count
    mem[1]  = 16'h120D;   // LOAD r2, 13     r2 = 1
    mem[2]  = 16'h310E;   // STORE r1, 14    show the count on the lamps
    mem[3]  = 16'h4102;   // SUB r1, r2      one fewer
    mem[4]  = 16'h5102;   // JUMPNZ r1, 2    not zero yet: back to box 2
    mem[5]  = 16'h0000;   // HALT
    mem[12] = 3;          // data: the count
    mem[13] = 1;          // data: the constant 1
    forever begin
      @(posedge clk);
      word = bus_read(pc);  fetches++;             // FETCH: a bus read at the address in the PC
      kind = word[15:12];                          // DECODE: four hex digits
      reg_no = word[11:8];
      arg  = word[3:0];
      next_pc = pc + 1;                            // unless a jump says otherwise
      case (kind)                                  // EXECUTE
        LOAD:   begin r[reg_no] = bus_read(arg);  loads++;      // a second transfer: a bus read
                  $display("pc=%0d fetch %h  LOAD r%0d, %0d     bus read %0d: r%0d=%0d", pc, word, reg_no, arg, arg, reg_no, r[reg_no]); end
        STORE:  begin bus_write(arg, r[reg_no]);  stores++;     // a second transfer: a bus write
                  $display("pc=%0d fetch %h  STORE r%0d, %0d    bus write %0d: %0d", pc, word, reg_no, arg, arg, r[reg_no]); end
        ADD:    begin r[reg_no] = r[reg_no] + r[arg[1:0]];      // registers only
                  $display("pc=%0d fetch %h  ADD r%0d, r%0d      no bus: r%0d=%0d", pc, word, reg_no, arg, reg_no, r[reg_no]); end
        SUB:    begin r[reg_no] = r[reg_no] - r[arg[1:0]];      // registers only
                  $display("pc=%0d fetch %h  SUB r%0d, r%0d      no bus: r%0d=%0d", pc, word, reg_no, arg, reg_no, r[reg_no]); end
        JUMPNZ: begin if (r[reg_no] != 0) next_pc = arg;        // a jump writes the PC
                  $display("pc=%0d fetch %h  JUMPNZ r%0d, %0d   no bus: next pc=%0d", pc, word, reg_no, arg, next_pc); end
        default: begin
                  $display("pc=%0d fetch %h  HALT", pc, word);
                  $display("bus transfers: %0d fetches, %0d loads, %0d stores", fetches, loads, stores);
                  $finish; end                     // $finish ends the simulation
      endcase
      pc = next_pc;
    end
  end
endmodule
ConceptWhich instruction makes two bus transfers?
ConceptRegister r1 holds 0 when JUMPNZ r1, 2 runs at address 4. Which address is fetched next?
ConceptWhy does this processor do arithmetic on registers and not on memory boxes?
ConceptThe processor runs STORE r1, 14. How does it know that address 14 is a device?

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.

Practice

Report a problem with this page