You are on page 1of 56

Computer architecture

The simplest processor

Wouter M. Koolen

Advanced Topics
23-2-12
Simple Processor
About me
Koolen

Introduction

Calculator
I Work in machine learning ... Immediates

I .. but generally interested in most of computer science Jumps

Data memory
I Fervent programmer
Procedure calls
I Computer architecture as a hobby Conclusion

I Author of SIM-PL simulator for digital hardware Advanced tricks

http://www.science.uva.nl/amstel/SIM-PL
Simple Processor
Source material
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
What do I expect of you / What would really help
Koolen

Introduction

Calculator
You ...
Immediates
I want to learn Jumps
I have seen a digital circuit (e.g. gate, adder, flip-flop). Data memory

Procedure calls
I wrote a program (e.g. if, goto, increment)
Conclusion
I are not mortally afraid of bits and hexadecimal Advanced tricks

I ...
Simple Processor
What do I expect of you / What would really help
Koolen

Introduction

Calculator
You ...
Immediates
I want to learn Jumps
I have seen a digital circuit (e.g. gate, adder, flip-flop). Data memory

Procedure calls
I wrote a program (e.g. if, goto, increment)
Conclusion
I are not mortally afraid of bits and hexadecimal Advanced tricks

I ...
I will provide trick questions to guide your thinking
And most importantly ...
Simple Processor
What do I expect of you / What would really help
Koolen

Introduction

Calculator
You ...
Immediates
I want to learn Jumps
I have seen a digital circuit (e.g. gate, adder, flip-flop). Data memory

Procedure calls
I wrote a program (e.g. if, goto, increment)
Conclusion
I are not mortally afraid of bits and hexadecimal Advanced tricks

I ...
I will provide trick questions to guide your thinking
And most importantly ...

You raise your hand when you get lost


Simple Processor
Goal of this lecture
Koolen

Introduction

Calculator

I present the simplest processor Immediates

Jumps

Data memory
Understanding
Procedure calls
I hardware design
Conclusion
I You can do it too
Advanced tricks
I Baseline for more complex designs
I Many (esoteric) designs found niches
I execution of software
I Programming (e.g. embedded devices)
I Compiler architecture
I The why of hardware eccentricities
Simple Processor
Goal of this lecture
Koolen
Pierce layers of abstraction
Introduction

Calculator

Immediates
Transistor Jumps

Data memory
Gate Procedure calls

Conclusion
Stateful circuit Boolean circuit
Advanced tricks

Memory Arithmetic circuit

Calculator

Processor
Assembly language Assembler

C Compiler
Simple Processor
Calculator
Koolen

Introduction

Calculator
Goal Immediates

I 16 variables (memory cells) Jumps

Data memory
I program (list of instructions)
Procedure calls
I 4 operations Conclusion

Advanced tricks
Simple Processor
Calculator
Koolen

Introduction

Calculator
Goal Immediates

I 16 variables (memory cells) Jumps

Data memory
I program (list of instructions)
Procedure calls
I 4 operations Conclusion

Advanced tricks

Instruction set
ADD, SUB, AND, COPY
Simple Processor
Calculator
Koolen

Introduction

Calculator
Goal Immediates

I 16 variables (memory cells) Jumps

Data memory
I program (list of instructions)
Procedure calls
I 4 operations Conclusion

Advanced tricks

Instruction set
ADD, SUB, AND, COPY

Example program
0: ADD $6, $3, $4 Set Reg6 to Reg3 + Reg4
1: SUB $7, $3, $4 Set Reg7 to Reg3 − Reg4
2: COPY $8, $6 Set Reg8 to Reg6
Simple Processor
Implementation: the hardware circuit
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
The driving force
Koolen

Introduction

Calculator

Purpose: Synchronise operation of components Immediates

Jumps

Data memory

Procedure calls

Conclusion

I Usually some kind of oscillating crystal Advanced tricks

I Clock

I High and low levels


I Positive (up) edge and negative (down) edge
I A clock cycle is: up – high – down – low
Simple Processor
Program counter
Koolen

Introduction

Calculator
Purpose: maintain the current position in the program Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

I One address
I Increment triggered by positive clock edge
Simple Processor
Instruction memory
Koolen

Introduction
Purpose: store the program
Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

I 16 bit address
I 14 bit data
Simple Processor
Registers
Koolen
Purpose: maintain the state of program variables
Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

I 16 registers named $0, $1, ..., $15


I 16 bits each
I Two read ports
I One write port (triggered by negative clock edge)
Simple Processor
Arithmetic logic unit (ALU)
Koolen

Introduction

Calculator
Purpose: performs computation Immediates

Jumps
S0 S1 output Data memory

0 0 A+B Procedure calls

0 1 A−B Conclusion

Advanced tricks
1 0 A&B
1 1 B

I 2 bits to select operation


Simple Processor
Calculator: the circuit
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
In action
Koolen

Say $3 initially contains 7, while $4 contains 5. Introduction

Calculator
0: ADD $6, $3, $4 Set Reg6 to Reg3 + Reg4
Immediates
1: SUB $7, $3, $4 Set Reg7 to Reg3 - Reg4
Jumps
2: COPY $8, $6 Set Reg8 to Reg6 Data memory

Procedure calls

1 Conclusion
1
Advanced tricks
11

F FFFF
FFFF 3FFF
FFFF

F FFFF

F
FFFF
Simple Processor
In action
Koolen

Say $3 initially contains 7, while $4 contains 5. Introduction

Calculator
0: ADD $6, $3, $4 Set Reg6 to Reg3 + Reg4
Immediates
1: SUB $7, $3, $4 Set Reg7 to Reg3 - Reg4
Jumps
2: COPY $8, $6 Set Reg8 to Reg6 Data memory

Procedure calls

0 Conclusion
0
Advanced tricks
00

3 0007
0000 0346
000C

4 0005

6
000C
Simple Processor
In action
Koolen

Say $3 initially contains 7, while $4 contains 5. Introduction

Calculator
0: ADD $6, $3, $4 Set Reg6 to Reg3 + Reg4
Immediates
1: SUB $7, $3, $4 Set Reg7 to Reg3 - Reg4
Jumps
2: COPY $8, $6 Set Reg8 to Reg6 Data memory

Procedure calls

1 Conclusion
0
Advanced tricks
01

3 0007
0001 1347
0002

4 0005

7
0002
Simple Processor
In action
Koolen

Say $3 initially contains 7, while $4 contains 5. Introduction

Calculator
0: ADD $6, $3, $4 Set Reg6 to Reg3 + Reg4
Immediates
1: SUB $7, $3, $4 Set Reg7 to Reg3 - Reg4
Jumps
2: COPY $8, $6 Set Reg8 to Reg6 Data memory

Procedure calls

1 Conclusion
1
Advanced tricks
11

0 FFFF
0002 3068
000C

6 000C

8
000C
Simple Processor
Summary
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory
Calculator
Procedure calls
I 16 memory cells Conclusion
I 4 operations Advanced tricks

I Executes program (list of instructions) sequentially


I Timing
Simple Processor
(Trick) questions
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

I Why does an instruction take 14 bits of memory?


I Is COPY $1, $1 safe?
I What about ADD $1, $1, $1?
I Can we increment a register? How?
I Can we multiply 2 registers? How?
I Can we execute an if statement? How?
Simple Processor
Immediates
Koolen

Introduction

Calculator

Immediates
Goal Jumps
Use of immediates (constants) in instructions Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
Immediates
Koolen

Introduction

Calculator

Immediates
Goal Jumps
Use of immediates (constants) in instructions Data memory

Procedure calls
Instruction set
Conclusion
ADD, SUB, AND, COPY, ADDI, SUBI, ANDI, LOADI Advanced tricks
Simple Processor
Immediates
Koolen

Introduction

Calculator

Immediates
Goal Jumps
Use of immediates (constants) in instructions Data memory

Procedure calls
Instruction set
Conclusion
ADD, SUB, AND, COPY, ADDI, SUBI, ANDI, LOADI Advanced tricks

Example program
0: LOADI $1, 0x3000 Load 3000hex in Reg1
1: LOADI $2, 0x2000 Load 2000hex in Reg2
2: SUB $3, $1, $2 Set Reg3 to Reg1 - Reg2
3: ADDI $4, $3, 0x200 Set Reg4 to Reg3 + 0200hex
Simple Processor
Implementing immediates
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

Do we use an immediate?
Immediate value
Simple Processor
Multiplexer
Koolen

Introduction

Calculator

Immediates

Jumps
Purpose: channel chooser
Data memory

Procedure calls
S output
Conclusion

0 A Advanced tricks

1 B

I 1 bit to select which input is passed on


Simple Processor
Immediates in action
Koolen

0: LOADI $1, 0x3000 Load 3000hex in Reg1 Introduction


1: LOADI $2, 0x2000 Load 2000hex in Reg2 Calculator

2: SUB $3, $1, $2 Set Reg3 to Reg1 - Reg2 Immediates

3: ADDI $4, $3, 0x200 Set Reg4 to Reg3 + 0200hex Jumps

Data memory

Procedure calls
3 Conclusion
0 1
FFFF Advanced tricks
3

F FFFF
FFFF 7FFFFFFF 1 FFFF
FFFF
FFFF
F FFFF

F
FFFF
Simple Processor
Immediates in action
Koolen

0: LOADI $1, 0x3000 Load 3000hex in Reg1 Introduction


1: LOADI $2, 0x2000 Load 2000hex in Reg2 Calculator

2: SUB $3, $1, $2 Set Reg3 to Reg1 - Reg2 Immediates

3: ADDI $4, $3, 0x200 Set Reg4 to Reg3 + 0200hex Jumps

Data memory

Procedure calls
3 Conclusion
0 0
3000 Advanced tricks
3

0 FFFF
0000 30013000 0 3000
3000
3000
0 FFFF

1
3000
Simple Processor
Immediates in action
Koolen

0: LOADI $1, 0x3000 Load 3000hex in Reg1 Introduction


1: LOADI $2, 0x2000 Load 2000hex in Reg2 Calculator

2: SUB $3, $1, $2 Set Reg3 to Reg1 - Reg2 Immediates

3: ADDI $4, $3, 0x200 Set Reg4 to Reg3 + 0200hex Jumps

Data memory

Procedure calls
3 Conclusion
0 0
2000 Advanced tricks
3

0 FFFF
0001 30022000 0 2000
2000
2000
0 FFFF

2
2000
Simple Processor
Immediates in action
Koolen

0: LOADI $1, 0x3000 Load 3000hex in Reg1 Introduction


1: LOADI $2, 0x2000 Load 2000hex in Reg2 Calculator

2: SUB $3, $1, $2 Set Reg3 to Reg1 - Reg2 Immediates

3: ADDI $4, $3, 0x200 Set Reg4 to Reg3 + 0200hex Jumps

Data memory

Procedure calls
1 Conclusion
0 1
0000 Advanced tricks
1

1 3000
0002 51230000 1 1000
0000
2000
2 2000

3
1000
Simple Processor
Immediates in action
Koolen

0: LOADI $1, 0x3000 Load 3000hex in Reg1 Introduction


1: LOADI $2, 0x2000 Load 2000hex in Reg2 Calculator

2: SUB $3, $1, $2 Set Reg3 to Reg1 - Reg2 Immediates

3: ADDI $4, $3, 0x200 Set Reg4 to Reg3 + 0200hex Jumps

Data memory

Procedure calls
0 Conclusion
0 0
0200 Advanced tricks
0

3 1000
0003 03040200 0 1200
0200
0200
0 FFFF

4
1200
Simple Processor
Summary
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls
I A little more hardware ...
Conclusion
I ... to allow immediates as last argument Advanced tricks

I Immediates part of instruction


Simple Processor
(Trick) questions
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

I How many bits do we need for each instruction?


I Can we increment a register? How?
I Can we execute ADDII $1, 0x1, 0x2? How?
I Can we multiply 2 registers? How?
I Can we execute an if statement? How?
Simple Processor
Conditional execution and jumps
Koolen

Goal Introduction

Implement if/else, switch, for, while, goto ... Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
Conditional execution and jumps
Koolen

Goal Introduction

Implement if/else, switch, for, while, goto ... Calculator

Immediates

Jumps
Instruction set
Data memory
ADD(I), SUB(I), AND(I), COPY, LOADI, BRA, BZ, BEQ
Procedure calls

Conclusion

Advanced tricks
Simple Processor
Conditional execution and jumps
Koolen

Goal Introduction

Implement if/else, switch, for, while, goto ... Calculator

Immediates

Jumps
Instruction set
Data memory
ADD(I), SUB(I), AND(I), COPY, LOADI, BRA, BZ, BEQ
Procedure calls

Example program Conclusion

Advanced tricks
LOADI a, 8 # a = 8;
LOADI b, 4 # b = 4;
LOADI r, 0 # r = 0;
loop:
BZ b, end # while (b != 0) {
ADD r, r, a # r += a;
SUBI b, b, 1 # --b;
BRA loop # }
end:
Simple Processor
Implementing jumps
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

Do we jump?
Where do we jump to?
Do we write to register?
Simple Processor
Arithmetic logic unit (ALU)
Koolen

Introduction

Calculator
Purpose: performs computation and tests
Immediates

S0 S1 output Jumps

Data memory
0 0 A+B Procedure calls
0 1 A−B Conclusion
1 0 A&B Advanced tricks
1 1 B

I 2 bits to select operation


I Zero bit is set when output is zero
Simple Processor
Summary
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory
I A bunch more hardware Procedure calls
I Hardware executes a branch when Conclusion

I Instruction is a branch instruction Advanced tricks


I Test succeeds (ALU outputs zero)
I Target of jump is encoded in instruction
Simple Processor
(Trick) questions
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

I How many bits do we need for each instruction?


I How can we test for (with A and B registers)

A=0 A=1 A=B A 6= B A<B


I Can we multiply 2 registers? How?
I Can we execute an if statement? How?
I Can we execute an if/else statement? How?
I Is Branch the negation of RegWrite?
Simple Processor
More/main memory
Koolen

Introduction

Calculator

Immediates
Goal Jumps
Add memory. We add 216 = 65536 variables Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
More/main memory
Koolen

Introduction

Calculator

Immediates
Goal Jumps
Add memory. We add 216 = 65536 variables Data memory

Procedure calls
Instruction set Conclusion
ADD(I), SUB(I), AND(I), COPY, LOADI, BRA, BZ, BEQ, Advanced tricks
LW, SW
Simple Processor
More/main memory
Koolen

Introduction

Calculator

Immediates
Goal Jumps
Add memory. We add 216 = 65536 variables Data memory

Procedure calls
Instruction set Conclusion
ADD(I), SUB(I), AND(I), COPY, LOADI, BRA, BZ, BEQ, Advanced tricks
LW, SW

Example program
LW $1, 10, $6 # Load mem[Reg6 + 10] into Reg1
SW $2, 10, $6 # Store $2 into mem[Reg6 + 10]
Simple Processor
Implementing main memory
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

Do we read from/write to memory?


Write what where? Value read?
Simple Processor
Summary
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls
I We simply “bolted on” some memory Conclusion
I Both in hardware Advanced tricks

I And in software
Simple Processor
(Trick) questions
Koolen

Introduction

Calculator

Immediates

Jumps
I How many bits do we need for each instruction?
Data memory
I Why not simply increase the number of registers? Procedure calls

I Can we perform a computation (say A + B) and write Conclusion

the result to memory using a single instruction? Advanced tricks

I Can we execute an if statement? How?


I What if we want more than 216 = 65536 variables?
I Can a program modify itself? (polymorphic code)
Simple Processor
Procedure calls
Koolen
Goal Introduction
Re-use blocks of code Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks
Simple Processor
Procedure calls
Koolen
Goal Introduction
Re-use blocks of code Calculator

Instruction set Immediates

ADD(I), SUB(I), AND(I), COPY, LOADI, BRA, BZ, BEQ, Jumps

Data memory
LW, SW, CALL, RETURN
Procedure calls
Example program Conclusion

LOADI $arg1, 1 Advanced tricks

LOADI $arg2, 2
LOADI $arg3, 3
CALL $ra, Add3

#--------------- Add3 procedure -----------------


Add3:
ADD $val1, $arg1, $arg2
ADD $val1, $val1, $arg3
RETURN $ra
Simple Processor
Implementing procedure calls
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

Conclusion

Advanced tricks

Do we CALL/RETURN?
Store PC+1 in register (for CALL)
Load PC from register (for RETURN)
Simple Processor
Summary
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls
I We allow store and load of PC
Conclusion
I Increment to return to instruction after call Advanced tricks

I Contract (calling convention) between caller and callee


Simple Processor
(Trick) questions
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

I How can we compute the PC at a given instruction? Procedure calls

Conclusion
I Can we implement a dispatch table? (function pointer)
Advanced tricks
I Can a procedure call another procedure?
I What about recursion?
Simple Processor
Conclusion
Koolen

Introduction

Calculator

Immediates

Jumps

Data memory

Procedure calls

I We built a general purpose processor Conclusion

Advanced tricks
I In incremental steps
Simple Processor
Advanced tricks
Koolen

Introduction
I Asynchronous design
Calculator
No clock
Immediates
I Caching Jumps
Fast small memory on top of slow big memory Data memory

I Register stacks Procedure calls

Conclusion
Accelerated procedure calls
Advanced tricks
I Floating point arithmetic, multimedia, encryption
Upgrade the ALU
I Very large instruction word (VLIW)
Multiple independent ALUs
I Pipelining
Execute multiple (sub-)instructions simultaneously
I Multi-core/processor
Multiple processors attached to a single main memory

You might also like