design notes of microprocessor pabs - İytetolgaayav/courses/ceng311/design_notes...design notes of...
TRANSCRIPT
Design Notes of Microprocessor
µPabs
Student’s Handbook(Ver. 08.a)
Asst. Prof. Dr. Tolga AYAV
Department of Computer EngineeringIzmir Institute of Technology
September 2008
2
Contents
1 A Simple Microprocessor: µPabs 31.1 Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 Datapath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.2.2 ALU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.2.3 Internal Program Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Control Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Software 112.1 High-Level Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.2 Assembly and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3 Instruction Pipelining 17
A Test Bench and Processor 19
B Datapath 21
C ALU 23
D Registers 25
E Internal Program Memory (ROM) 27
F Multiplexers, Addsub circuitry, Full Adder 29
G Control Unit 31
H Simulation 33
i
ii CONTENTS
CONTENTS 1
Preface
This handbook is intended to provide the students of Computer Engineering in Izmir Instituteof Technology with understanding of the basic principles of digital logic design and assistancein the related courses in the curriculum:
1. CENG214, Logic Design
2. CENG311, Computer Architecture
3. CENG314, Embedded Computer Systems
4. CENG383, Real-Time Systems
5. CENG384, Microprocessors
6. CENG451, Advanced Digital System Design
7. CENG452, Building Software for Embedded Systems
8. CENG563, Real-Time and Embedded System Design
The main purpose of this document in fact is to give the students some intuition about thefollowing question:
Question 1 What is really happening inside a computer system?
In other words, starting from typing printf("value:%d",*p); (we know C programming verywell), we must understand compiling, assembling, linking, loading the machine code, executionthe machine code on the processor and how processors execute this code: from the functions oflogic circuits to even the movement of electrons in the transistors of integrated circuits. This isa very long way to learn and these topics are covered in at least 8-10 undergraduate courses incomputer and electronics programs.
This document aims to give a very short and abstract answer to the above question. It startsfrom designing a simple microprocessor called µPabs . Design of a microprocessor can be seenas an ultimate in digital logic design efforts. VHDL codes of µPabs are also given in appendicesso that students can better understand the functions of each part of the microprocessor andthey can even realize the processor on a FPGA board. Compiling, assembling and linking areintroduced shortly to give the complete understanding of the whole computer system.
Although it is not the aim of this book to cover the aforementioned topics completely,students may still find many things they are wondering are missing, too short or incomplete.Nonetheless, I hope this will be a good starting point for their deeper research as well as theirstudy of computer architecture.
Tolga AYAV03/09/2008
2 CONTENTS
Chapter 1
A Simple Microprocessor: µPabs
Figure 1.1: µPabs.
µPabs is a simple 8-bit processor. Since it has an internal program memory and I/O ports,we can call it microcontroller as well. The specifications of µPabs are:
• 18 pins: 16 I/O, Reset and Clock pins
• 8-bit internal address and data buses
• 256x8 bit internal program memory
• 32x8 bit register file
• 8-bit input and 8-bit output ports
• 8 instructions with single cycle operation
A general diagram of µPabs showing its input and output pins is given in figure 1.1.Figure 1.2 demonstrates the behavior of µPabs using a GCL-like (Guarded Command Lan-
guage) pseudo-code (See [NN92] for the description of GCL). In this code, ir, pc and accdenote 8-bit instruction register, program counter and accumulator respectively. imem[α] is the8-bit value stored in “α + 1”th location of the program memory where α is 8-bit address value.reg[β] denotes “β + 1”th register where β is 5-bit register address. Please note that acc is aspecial register and physically same with reg[0]. ir consists of two parts such that ir.o holdsthe most significant 3 bits as opcode and ir.a holds the least significant 5 bits as an operandsuch as register address, memory address or immediate depending on the corresponding opcode.ir.a.s, however represents the most significant bit of ir.a as the sign bit.
[g1 → a1 [] g2 → a2 · · · [] gn → an] is a selective construct such that one action amonga1 · · · an whose guard is true will be selected and executed. *[...] depicts a repetitive con-struct.
3
4 CHAPTER 1. A SIMPLE MICROPROCESSOR: µPABS
*[FETCH: ir, pc := imem[pc], pc+1;DECODE,EXECUTE: [ ir.o=sta → reg[ir.a] := acc;
[] ir.o=lda → acc := reg[ir.a];[] ir.o=movi → acc := ir.a;[] ir.o=inp → reg[ir.a] := input;[] ir.o=outp → output := reg[ir.a];[] ir.o=jnz → [ acc = 0 → [ ir.a.s = false → pc := pc + ir.a;
[] ir.a.s = true → pc := pc - ir.a;]
[] ¬ acc = 0 → skip;]
[] ir.o=adda → acc := acc + reg[ir.a];[] ir.o=suba → acc := acc - reg[ir.a];[] ir.o=other → skip;]
]
Figure 1.2: Pseudo-code of µPabs in guarded command language
Question 2 Write a simulator for µPabs in C language (see the pseudo-code given in figure1.2). Your simulator should take an assembly program as input and execute it. During thesimulation, registers and other critical values will be shown on the screen.
1.1 Instruction Set
µPabs ’s limited instruction set has only eight instructions. These commands are given intable 1.1. To encode eight instructions the operation code (opcode) requires 3 bits, giving useight different combinations. As shown in the encoding column, the three most significant bitsrepresent the opcode of the instructions. For example, the opcode for sta is 000 and the opcodefor outp is 110 and so on. [PH05]
Table 1.1: Instruction set of µPabsInstruction Encoding Operation Comment
sta reg 000rrrrr F[reg] ← Acc store accumulatorlda reg 001rrrrr Acc ← F[reg] load accumulator
movi imm5 010iiiii Acc←imm5 move immediateinp reg 011rrrrr F[reg] ← input read from input portoutp reg 100rrrrr output ← F[reg] write to output portjnz add5 101saaaa if(A!=0 and s=0) then PC←PC+aaaa jump if
if(A!=0 and s=1) then PC←PC-aaaa Acc is not zeroadda reg 110rrrrr Acc ← Acc + F[reg] summationsuba reg 111rrrrr Acc ← Acc - F[reg] subtraction
Notations:
1.2. DATAPATH 5
Acc = Accumulator, i.e. F[0]F[0-31] = 32x8 bits register file
PC = Program counter registeradd5 = 5 bits for specifying a memory addressimm5 = 5 bit immediate valuereg = 5 bits for specifying a register address
1.2 Datapath
The datapath is responsible for manipulating data. It includes (1) functional units such asadders, shifters, multipliers, ALUs, and comparators, (2) registers and other memory elementsfor the temporary storage of data, and (3) buses, multiplexers, and tri-state buffers for thetransfer of data between the different components in the datapath, and the external world.External data enters the datapath through the data input lines. Results from the datapathoperations are provided through the data output lines. These signals serve as the primaryinput/output data ports for the microprocessor. In the following subsections, we will see thecomponents of the datapath in detail.
1.2.1 Registers
µPabs has three separate registers, program counter (PC), instruction register (IR), outputregister (OR) and a register file consisting of 32 general purpose registers.
Program Counter
Program counter (PC) contains the memory location of where the next instruction is stored.Each time an instruction is fetched from a memory location pointed to by the PC, normally thePC must be incremented to the next memory location for the next instruction. Alternatively,if the instruction is a jump instruction, the PC must be loaded with a new memory addressinstead.
Figure 1.3: Program Counter (PC) register and PC Next Logic.
Instruction Register and Output Register
Instruction register (IR) stores the instruction being fetched from the program memory. Outputregister holds the value driven from the output port. The structure of these registers are entirelyidentical to PC register.
6 CHAPTER 1. A SIMPLE MICROPROCESSOR: µPABS
Register File
Register file contains 32 registers. The block diagram of the register file is seen in figure 1.5.For further detail on its working, please see the code given in figure D.2 of appendix D.
Question 3 Implement program counter (PC), instruction register (IR) and output register inVHDL (See [Hwa04] for VHDL). Make a simulation in Modelsim to make sure that they runproperly.
1.2.2 ALU
The arithmetic logic unit (ALU) is one of the main components inside a microprocessor. It isresponsible for performing arithmetic and logic operations, such as addition, subtraction, logicalAND, and logical OR. µPabs ’s ALU performs only two actions: addition and subtraction. OurALU has two input ports, A and B, one output port F and a selection input s, as seen in figure1.5. We can define the function of ALU as:
F = f(s, A,B)
F = s′1s′0A + s′1s0(A + B) + s1s0(A + B′ + 1)
To implement ALU we will use a generic circuit consisting of a set of full adders augmented
Figure 1.4: Implementation of ALU.
Table 1.2: ALU operationss1 s0 Operation Name Operation xi (LE) yi (AE) c0 (CE)0 0 Pass Pass A to output ai 0 00 1 Addition A + B ai bi 01 1 Subtraction A−B ai b′i 11 0 - - - - -
with arithmetic and logic extenders. As we can see from the figure 1.4, the two combinationalcircuits in front of the full adder (FA) are labeled LE and AE. The logic extender (LE) is formanipulating all logical operations (note that µPabs does not have logical operations in fact);whereas, the arithmetic extender (AE) is for manipulating all arithmetic operations. The LEperforms the actual logical operations on the two primary operands, ai and bi, before passing theresult to the first operand, xi, of the FA. On the other hand, the AE only modifies the secondoperand, bi, and passes it to the second operand, yi, of the FA where the actual arithmeticoperation is performed. To perform additions and subtractions, we only need to modify yi (thesecond operand to the FA) so that all operations can be done with additions. The combinational
1.2. DATAPATH 7
circuit labeled CE (for carry extender) is for modifying the primary carry-in signal, c0, so thatarithmetic operations are performed correctly. Therefore, we can find out:
c0 = s1
yi = s1 ⊕ (s0 ∧ bi)xi = ai (1.1)
Question 4 Find out the formulae given with (1.1), using common digital design techniquessuch as truth tables, karnaugh maps or other simplification methods. The function of ALU isgiven in table 1.2.
Figure 1.5: ALU and Register File.
The implementation of ALU in VHDL can be seen in appendix C.
1.2.3 Internal Program Memory
It is the memory where the program code to be executed is stored. In each fetch cycle, one codeis fetched from the memory and placed into the instruction register. The VHDL coding of thememory is given in appendix E.
Figure 1.6: Internal Program Memory.
8 CHAPTER 1. A SIMPLE MICROPROCESSOR: µPABS
1.3 Control Unit
The control unit inside the microprocessor is a finite state machine. By stepping through asequence of states, the control unit controls the operations of the datapath. For each statethat the control unit is in, the output logic that is inside the control unit will generate allof the appropriate control signals for the datapath to perform one data operation. Thesedata operations are referred to as register-transfer operations. Each register-transfer operationconsists of reading a value from a register, modifying the value by one or more functional units,and finally, writing the modified value back into the same or a different register.
The block diagram of our control unit is given in figure 1.7. Figure 1.8 shows the FSM ofµPabs .
Figure 1.7: Control Unit.
Figure 1.8: FSM diagram for the control unit.
Question 5 Please complete the next-state diagram of the control unit given in the table 1.3and design the control unit using J-K flip-flops.
1.3. CONTROL UNIT 9
Tab
le1.
3:N
ext-
Stat
eD
iagr
amfo
rth
eC
ontr
olU
nit
(inc
ompl
ete)
clk
rese
tA
eq
IR
7−
5st
ate
ALU
Sel
Ase
lIR
load
PC
load
Olo
ad
jmpM
ux
opfe
tch
we
wri
teA
cc
rbe
next
state
↑1
xxxx
xxx
xx
00
00
00
0x
start
↑0
xxxx
start
xx
xx
00
00
10
0x
fetc
h↑
0x
xxx
fetc
hxx
xx
11
00
10
0x
decode
↑0
xss
sdecode
xx
xx
00
00
10
0x
state
(sss
)↑
0x
xxx
store
00
11
00
00
01
00
fetc
h↑
00
xxx
jnz
xx
xx
00
01
00
00
fetc
h↑
01
xxx
jnz
xx
xx
01
01
00
00
fetc
h↑
0x
xxx
in00
01
00
00
01
01
fetc
h
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
10 CHAPTER 1. A SIMPLE MICROPROCESSOR: µPABS
Figure 1.9: µPabs .
Chapter 2
Software
2.1 High-Level Programming
Let’s define the following high-level programming language Cabsfor µPabs :
S ::= var x : int8 variable definition| x:=E assignment| skip no operation| read(x) input read| write(x) output write| S1;S2 sequencing| if B then S1 else S2 conditional| while B do S iteration
whereE ::= E1 opE2 | x | C op ∈ {+, −}B ::= (x ! = C)int8 depicts the type of 8-bit integerC is 5-bit constant value.
Let’s write a program for our processor such that we read input port (n), find out∑n
i=1 iand write the result to the output port at the end. The following would be our program:
var t, n : int8;t := 0;read (n);while n!=0 do
t := t + n;n := n - 1;
write (t);
Figure 2.1: Example algorithm written in the abstract language
Compilation is another topic and beyond the scope of this document. Please see figure 2.3for detail. Here, we will assume that a compiler can generate the intermediary assembly codegiven in figure 2.4.
11
12 CHAPTER 2. SOFTWARE
Figure 2.2: Program generation from compilation through loading.
Question 6 Please note that Cabs is very limited language such that it is well suited to thehardware. For example, it supports two mathematical operations and only 5-bit constant values.Discuss if we could use multiplication, i.e., op ∈ {+,−, ∗}. How can the compiler translate thefollowing line to the assembly of µPabs ?
t := t ∗ n;
Could we also generalize 5-bit constants to 8-bit? If so, how would you translate the followingline to the assembly of µPabs ?
if t! = 231 then t := t + 1;
Question 7 You can see the basic steps of compilation process in figure 2.3. Try to develop acompiler for our Cabs language using lex and yacc tools.
2.2 Assembly and Linking
Assembly and linking are the last steps in the compilation process - they turn a list of instruc-tions into an image of the program’s bits in memory. Figure 2.2 highlights the role of assemblersand linkers in the compilation process. This process is often hidden from us by compilationcommands that do everything required to generate an executable program. As the figure shows,most compilers do not directly generate machine code, but instead create the instruction-levelprogram in the form of humanreadable assembly language. Generating assembly language ratherthan binary instructions frees the compiler writer from details extraneous to the compilationprocess, which include the instruction format as well as the exact addresses of instructions anddata. The assembler’s job is to translate symbolic assembly language statements into bit-levelrepresentations of instructions known as object code. The assembler takes care of instructionformats and does part of the job of translating labels into addresses. However, since the programmay be built from many files, the final steps in determining the addresses of instructions anddata are performed by the linker, which produces an executable binary file. That file may notnecessarily be located in the CPU’s memory, however, unless the linker happens to create theexecutable directly in RAM. The program that brings the program into memory for executionis called a loader.
Since we do not have any compiler to compile the high-level source code to the assemblyformat of µPabs , we will do it by hand. The assembly output is seen in figure 2.4.
Question 8 Read the assembly code given in figure 2.4 carefully. What can you say about themaximum value of n in the algorithm? Where does the limitation come from?
2.2. ASSEMBLY AND LINKING 13
Figure 2.3: Compilation process.
Question 9 Re-write the assembly code given with figure 2.4 in MIPS assembly format.
The simplest form of the assembler assumes that the starting address of the assembly languageprogram has been specified by the programmer. The addresses in such a program are known asabsolute addresses. However, in many cases, particularly when we are creating an executableout of several component files, we do not want to specify the starting addresses for all themodules before assembly. If we did, we would have to determine before assembly not only thelength of each program in memory but also the order in which they would be linked into theprogram. Most assemblers therefore allow us to use relative addresses by specifying at the startof the file that the origin of the assembly language module is to be computed later. Addresseswithin the module are then computed relative to the start of the module. The linker is thenresponsible for translating relative addresses into absolute addresses.
Assemblers
When translating assembly code into object code, the assembler must translate opcodes andformat the bits in each instruction, and translate labels into addresses. In this section, wereview the translation of assembly language into binary. Labels make the assembly process morecomplex, but they are the most important abstraction provided by the assembler. Labels letthe programmer (a human programmer or a compiler generating assembly code) avoid worryingabout the absolute locations of instructions and data. Label processing requires making twopasses through the assembly source code as follows:
1. The first pass scans the code to determine the address of each label.
14 CHAPTER 2. SOFTWARE
INP BMOVI 1STA CMOVI 0STA D
L1: LDA DADDA BSTA DLDA BSUBA CSTA BJNZ L1OUTP D
Figure 2.4: Example assembly program for µPabs .
2. The second pass assembles the instructions using the label values computed in the firstpass.
The name of each symbol and its address is stored in a symbol table that is built during thefirst pass. The symbol table is built by scanning from the first instruction to the last (For themoment, we assume that we know the absolute address of the first instruction in the program).During scanning, the current location in memory is kept in a program location counter (PLC).Despite the similarity in name to a program counter, the PLC is not used to execute the program,only to assign memory locations to labels. For example, the PLC always makes exactly one passthrough the program, whereas the program counter makes many passes over code in a loop.Thus, at the start of the first pass, the PLC is set to the program’s starting address and theassembler looks at the first line. After examining the line, the assembler updates the PLC tothe next location (since our architecture is one byte long, the PLC would be incremented byone) and looks at the next instruction. If the instruction begins with a label, a new entry ismade in the symbol table, which includes the label name and its value. The value of the labelis equal to the current value of the PLC. At the end of the first pass, the assembler rewinds tothe beginning of the assembly language file to make the second pass. During the second pass,when a label name is found, the label is looked up in the symbol table and its value substitutedinto the appropriate place in the instruction. In our program, the only label L1 is replaced with“10111”.
Linking
Many assembly language programs are written as several smaller pieces rather than as a singlelarge file. Breaking a large program into smaller files helps delineate program modularity. Ifthe program uses library routines, those will already be preassembled, and assembly languagesource code for the libraries may not be available for purchase. A linker allows a program to bestitched together out of several smaller pieces. The linker operates on the object files createdby the assembler and modifies the assembled code to make the necessary links between files.Some labels will be both defined and used in the same file. Other labels will be defined in asingle file but used elsewhere. The place in the file where a label is defined is known as an entrypoint. The place in the file where the label is used is called an external reference. The main
2.2. ASSEMBLY AND LINKING 15
job of the loader is to resolve external references based on available entry points. As a resultof the need to know how definitions and references connect, the assembler passes to the linkernot only the object file but also the symbol table. Even if the entire symbol table is not keptfor later debugging purposes, it must at least pass the entry points. External references areidentified in the object code by their relative symbol identifiers.
The linker proceeds in two phases. First, it determines the absolute address of the start ofeach object file. The order in which object files are to be loaded is given by the user, either byspecifying parameters when the loader is run or by creating a load map file that gives the orderin which files are to be placed in memory. Given the order in which files are to be placed inmemory and the length of each object file, it is easy to compute the absolute starting address ofeach file. At the start of the second phase, the loader merges all symbol tables from the objectfiles into a single, large table. It then edits the object files to change relative addresses intoabsolute addresses. This is typically performed by having the assembler write extra bits intothe object file to identify the instructions and fields that refer to labels. If a label cannot befound in the merged symbol table, it is undefined and an error message is sent to the user.
After assembling and linking the program, we have the following machine code:
011 00001010 00001000 00010010 00000000 00011001 00011110 00001000 00011001 00001111 00010000 00001101 10111100 00011
Figure 2.5: Machine code of our example assembly program for µPabs .
Question 10 VHDL synthesizer sometimes produces an error like “...all logic was removedfrom the design...”. What does it mean?
Question 11 Simulate the example program in Modelsim. How many clock cycles does it takefor µPabs to execute this program?
16 CHAPTER 2. SOFTWARE
Chapter 3
Instruction Pipelining
Pipelining, a standard feature in RISC processors, is much like an assembly line. Because theprocessor works on different steps of the instruction at the same time, more instructions can beexecuted in a shorter period of time.
A useful method of demonstrating this is the laundry analogy. Let’s say that there are fourloads of dirty laundry that need to be washed, dried, and folded. We could put the the firstload in the washer for 30 minutes, dry it for 40 minutes, and then take 20 minutes to fold theclothes. Then pick up the second load and wash, dry, and fold, and repeat for the third andfourth loads. Supposing we started at 6 PM and worked as efficiently as possible, we would stillbe doing laundry until midnight. However, a smarter approach to the problem would be to putthe second load of dirty laundry into the washer after the first was already clean and whirlinghappily in the dryer. Then, while the first load was being folded, the second load would dry,and a third load could be added to the pipeline of laundry. Using this method, the laundrywould be finished by 9:30.
µPabs ’s execution consists of 3 stages: fetch, decode and execution cycle. At first glance, apipelining in µPabs would have this form: To apply pipelining to µPabs , we may need additional
Figure 3.1: Pipelining in µPabs .
registers. µPabs has single-cycle operations and this makes pipelining easier. The only commandthat may complicate pipelining is jnz. If a jump occurs during the execution of jnz, thenpipelining mechanism must take into account this and start to fetch from the new location.
Question 12 Modify µPabs architecture to perform pipelining (we can call the modified micro-processor as µPabs). Reconstruct the next-state table of the control unit given in 1.3. Modifythe VHDL codes and simulate µPabs) in Modelsim. Please notice the execution time difference.
Question 13 For both µPabs and µPabs, find out a formula to calculate the execution time ofany given program.
17
18 CHAPTER 3. INSTRUCTION PIPELINING
Appendix A
Test Bench and Processor
Figure A.1: VHDL code for the test bench.
Figure A.2: VHDL code for µPabs .
19
20 APPENDIX A. TEST BENCH AND PROCESSOR
Appendix B
Datapath
Figure B.1: VHDL code for datapath.
21
22 APPENDIX B. DATAPATH
Appendix C
ALU
Figure C.1: ALU.
23
24 APPENDIX C. ALU
Appendix D
Registers
Figure D.1: VHDL code for Program Counter (PC) register.
25
26 APPENDIX D. REGISTERS
Figure D.2: VHDL code for register file.
Appendix E
Internal Program Memory (ROM)
Figure E.1: Opcode definitions.
27
28 APPENDIX E. INTERNAL PROGRAM MEMORY (ROM)
Figure E.2: Program memory.
Appendix F
Multiplexers, Addsub circuitry, FullAdder
Figure F.1: 2x1 4-bit Multiplexer.
Figure F.2: 4x1 8-bit Multiplexer.
29
30 APPENDIX F. MULTIPLEXERS, ADDSUB CIRCUITRY, FULL ADDER
Figure F.3: 8-bit addsub circuit.
Figure F.4: Full Adder.
Appendix G
Control Unit
31
32 APPENDIX G. CONTROL UNIT
Figure G.1: Control Unit.
Appendix H
Simulation
Figure H.1: Simulation in Modelsim.
33
34 APPENDIX H. SIMULATION
Bibliography
[Hwa04] Enoch O. Hwang, Digital logic and microprocessor design with vhdl, 2004.
[NN92] H.R. Nielson and F. Nielson, Semantics with applications — a formal introduction,1992.
[PH05] David A. Patterson and John L. Hennessy, Computer organization and design, 3rd ed.,Morgan Kaufmann, 2005.
35