chapter 1 microprocessor architecture
TRANSCRIPT
Dr. Mohamed Mahmoud
http://www.cae.tntech.edu/~mmahmoud/[email protected]
Chapter 1
Microprocessor architecture
ECE 3130
The slides are copyright protected. It is not permissible to use them without
a permission from Dr Mahmoud
Outline
1.1 Number Systems
1.2 Computer hardware organization
1.3 Memory system operation
1.4 Processor
1.5 Program Execution
1.6 HCS12 Microcontroller
- The binary number system has two digits 0 and 1
- The hexadecimal number system uses 16 digits: 0, 1, .., 9, A,B, C,.., F
- The decimal number system uses 10 digits: 0, 1, .., 9
- Computer hardware uses binary numbers to perform alloperations.
- Humans are used to decimal number system.
- Hexadecimal numbers have shorter representations than thebinary system.
- Conversion between the different number systems is needed.
1 - 1
- A prefix is used to indicate the base of a number.
1 - 2
- Unsigned numbers are always positive
- Signed number
- Most significant bit (B) = 1 negative, otherwise positive
- Convert %1000101 to Hexadecimal
0100 0101 = $45
- Convert $4F to Binary
$4F = 0100 1111
B010 0000
Number like 10 can be binary, hexadecimal and decimal. We use a prefix to define it.
- Unsigned 8-bit number can have numbers from 0 to 255%00000000 to %11111111
- Signed 8-bit number can have numbers from -128 to +127to %10000000 to %01111111 1 - 3
1- Unsigned number
%1111 = 1 + 2 + 4 + 8 = 15
%0111 = 1 + 2 + 4 = 7
Unsigned N-bit number can have numbers from 0 to 2N-1
2- Signed number
%1111 is a negative number.
To convert to decimal, calculate the two’s complement
The two’s complement = one’s complement +1 = %0000 + 1=%0001 = 1 then %1111 = -1
%0111 is a positive number = 1 + 2 + 4 = 7.
- Signed N-bit number can have numbers from -2N-1 to 2N-1-1- Unsigned 3-bit number can represent numbers from 0 (=
%000) to 7 (=111)- Signed 3-bit number can represent numbers from -4 (= %100)
to 3 (= %011)
1 - 4
Computer does not know whether a bit sequence represents a signed or an unsigned number ->It is the programmer’s responsibility!
Signed
Outline
1.1 Number Systems
1.2 Computer hardware organization
1.3 Memory system operation
1.4 Processor
1.5 Program Execution
1.6 HCS12 Microcontroller
- A Computer consists of hardware and software
- Software is computer program.
- The hardware consists of processor, input devices, outputdevices and memory.
Computer Hardware Organization
1 - 5
1- Processor
- Also called Microprocessor (MP) or Central Processing Unit(CPU).
- Executes programs.
- Performs: (1) all of the computational operations and (2) thecoordination of the usage of resources of a computer
- Ex. can a memory and an input device write data on ashared bus simultaneously? NO. Control signals are issuedby processor to coordinate the devices operations.
2- Input devices
- Used to enter the programs to be executed and the data tobe processed into the computer.
- Examples: keyboard, keypad, switches, sensors, scanners,bar code readers, etc.
1 - 6
3- Output devices
- The results of the operations done by the processor should bedisplayed or printed on a media so that the user can see them.
- Examples: seven segment displays, light emitting diode(LED), liquid crystal displays (LCD), a monitor, etc.
4- Memory
- Programs to be executed and data to be processed are storedin memory so that the processor can readily access them.
5- Buses
- Address bus: The set of conductor wires that carry addresssignals.
- Data bus: The set of conductor wires that carry data signals
- Control bus: The set of conductor wires that carry controlsignals
1 - 7
Outline
1.1 Number Systems
1.2 Computer hardware organization
1.3 Memory system operation
1.4 Processor
1.5 Program Execution
1.6 HCS12 Microcontroller
- Each memory location has address and content.
- The content is one byte (8 bits).
- If a memory chip has 16-bit address, then the chip has 216 =64 Kbytes because 16 bits can point at 216 locations.
- Byte = 8 bits, Word = 2 bytes = 16 bits, Nibble = 4 bits
- Kilobyte (KB) = 210 bytes =1024 bytes
- Megabyte (MB) = K2 Bytes = 220 bytes = 1,048,576 bytes 1 - 8
$FF$00$3F
$AF
0123
Address: location number Content: A byte stored in a location
A block diagram for the memory
1 - 9
- The memory data bits are connected to the data bus.
- The memory address bits are connected to the address bus.
- RD (Read) in the MP is connected to OE (output enable) inthe memory and WR (write) is connected to WE (writeenable)
- RD and WR are two control signals issued by the CPU toinform the memory to read or write.
216 locations
16 bits8 bits
8 bits
Read Operation- The processor places the address of the memory location thatit intends to read on the address bus and asserts the RDsignal. (RD = 1 and WR = 0)
- After some time, the data stored in the location referred bythe address will be copied from the memory to the data busto be read by the MP. The content of the memory locationdoes not change.
1 - 10
Write Operation- The processor places the address of the location that itintends to write on the address bus.
- The processor also places the data that it intends to write onthe data bus and asserts the WE signal (RD = 0 and WR =1)
- The memory chip stores the data on the data bus in theselected memory location after some time. The content ofthe memory location changes.
Outline
1.1 Number Systems
1.2 Computer hardware organization
1.3 Memory system operation
1.4 Processor
1.5 Program Execution
1.6 HCS12 Microcontroller
- A processor consists of arithmetic logic unit (ALU),control unit, and registers.
- The ALU performs the arithmetic and logic operationsrequested by the program.
- A simple ALU that can perform only four operations (addition,subtraction, AND, OR) is shown in Figure 1.1.
1 - 11
- All four operations are performed simultaneously by differentcircuits whereas the instruction opcode tells the multiplexer toselect one of the four units’ outputs as the result
- The adder is used to perform addition and subtractionoperations
- The ALU is more complicated if the processor designer wantsto implement more operations directly in the hardware.
1 - 12I311
Select Output000110
I0
I1
I2
Remember how multiplexer works:
MUX output
I0
I1I2
I3
Inputs
Selector
The ALU operations:-
1- Opcode = 00
- The comparator output = 0, MUX2 output = CIN, MUX1 output =B, the adder inputs are n-bit A and n-bit B.
- The result of the adder is A + B + CIN, CIN is the input carry.
- MUX3 selects the output of the adder and the carry.
1 - 13
00
A + B+ CIN
0
0
1 - 14
01
A + B’ + 1= A - B
1
1
2- Opcode = 01
- The comparator output = 1, MUX2 output = 1, MUX1 output =B’ (one’ complement of B), the adder inputs are n-bit A and n-bit B’.
- The result of the adder is A + B’ + 1 = A - B. Remember B’ + 1= - B. B’ + 1 is the two’s complement of B.
- MUX3 selects the output of the adder and the carry (calledborrow).
3- Opcode = 10
MUX3 selects the output of the AND. It is the n bits resultedfrom performing logic AND operation for A and B.
4- Opcode = 11
MUX3 selects the output of the OR. It is the n bits resultedfrom performing logic OR operation for A and B.
Remember:-
X can be zero or one
1 - 15
The ALU in Fig. 1.1 can be expanded to perform more instructions
The instruction set of a microprocessor is the fixed set of operations the computer can be programmed to perform on data.
These instructions are executed on hardware, i.e., there is a hardware circuit that executes them.
Instruction Set
1 - 16
2- Registers
- A register is a storage area inside the CPU.
- A register is used to hold data or address during the executionof an instruction.
- Registers provide fast access to operands for program execution.
- Unlike memory, registers are not used to store the program orlarge data.
- Accessing memory is much slower than accessing registers
1 - 17
3- Control Unit
- The control unit executes the instructions by telling the ALU whatoperation it has to do (opcode) and issuing control signals toread/write from memory.
Outline
1.1 Number Systems
1.2 Computer hardware organization
1.3 Memory system operation
1.4 Processor
1.5 Program Execution
1.6 HCS12 Microcontroller
- Stored in memory - Executable by the CPU
Compiler vs assembler
1 - 18
for (i=1, i<10, i++){
Y = Y+i;}
00100001000111100001110100111100
Compiler
Machine code
- Stored in memory - Executable by the CPU
Written by programmers
ldaa $10adda $20staa $30
00100001000111100001110100111100
assembler
Machine code
Written by programmers
High-level programming language source code
Low-level programming languagesource code
Compiler vs assembler
- Compiler is more complex than assembler
- Each assembly language instruction is translated to one machinelanguage instruction
- But, one statement written in high level language may betranslated into tens or hundreds of machine languageinstructions and this translation is not usually optimal
- The CPU can execute ONLY machine codes.
- Obviously, programming by the machine code is hard tounderstand, enter, debug, and maintain for human humans.That is why we use assembly language that use English words.
1 - 19
$0000
1 - 20
Executing a program
ld 0x20, #0ld 0x21, #20ld ptr, #0x2000loop: ld A,@ptrand A, #0x03bnz nextinc 0x20next: dbnz 0x21,loop
1- The assembler converts the assembly program to machine code and stores it in the memory, e.g., starting from location $0000.
- Some instructions take more than a byte.
-A program counter (PC) is a register that is used to keep track of theaddress of the instruction to be executed next.
2- Two phases to execute each instruction:-
2.1 Instruction fetch:
- Read the instruction from the memory location pointed by PC and store it in the instruction register (IR)
- May need more than one reading operation if the instruction is more than one byte.
- Update PC to point at the address of the next instruction.
- Both PC and IR are two registers in the CPU.
2.2 Instruction Execution
- The control unit decodes the instruction and issues appropriate control signals to execute it.
- It may need to read/write the memory.
1 - 21
1 - 22
- The clock signal is a square waveform.
- Instruction execution is timed by the clock signal.
- At each cycle, some operations are performed.
- Executing one instruction may need more than one cycle.
1 - 23
Instruction ld $20,#0 (machine code 75 20 00)
Place 0 in data memory at 0x20
1- Fetch instruction:
- The Processor places contents of PC ($0000) on the address busto read the instruction to be executed.
-It reads the first byte (opcode) = $75. PC is incremented to$0001.
-From the opcode, the control unit recognizes that it needs to readthe following two bytes after the opcode. It reads $20 and $00 andholds them in IR. PC is incremented after each read operation tobe 3 to point at the next executed instruction.
2- Execute the instruction:
- Address bus ← $20 and Data bus ← $00
- WR =1 to write 0 into the memory location $20.
1 - 24
Initially PC = 0
PC = PC +1 after reading the byte pointed by 0 (or 75)
Instruction ld $21,#$14 (machine code 75 21 14)
- Similar to previous instruction.- PC = $0006 and write $14 to memory location $20.
-------------------------------------------------------
Instruction ld ptr,#$2000 (machine code 90 20 00)
- Load $2000 into the ptr register
1- Fetch instruction:- The address bus ← PC value ($06), request to read.- Opcode = $90 and PC = $07.- From the opcode, we need to read the following twolocations. Read $20 and $00 and PC = 0x09.
2- Execute instruction:ptr ← 2000
1 - 25
Interesting videohttps://www.youtube.com/watch?v=cNN_tTXABUA
1 - 26
The machine code of the program
Functional blocks required to run a program:
– A place to store the program, operands, and computation results –memory and registers
– A way to keep track of which instruction is to be executed next –program counter (PC)
– A place to temporarily “stage” an instruction while it is being executed – instruction register (IR)
– A way to perform arithmetic and logic operations – arithmetic logic unit (ALU)
– A way to coordinate and sequence the functions of the machine to execute the instructions – control unit
1 - 27
Summary
Instruction Execution Cycle
– One or more read cycles to fetch the instruction from memory.
– One or more read cycles to fetch the memory operand(s) (optional)
– Perform the operation specified by the instruction
– One or more write cycles to write back the result to either a register or a memory location (optional)
1 - 28
Memory is used to store:
- Instructions: binary codes prescribing instructions
- Data: to be manipulated
- Stack: will be explained later
Instruction, data and stack areas in memory must beseparated.
Any overlapping means, the program will crash
It is the programmer’s responsibility.1 - 29
Outline
1.1 Number Systems
1.2 Computer hardware organization
1.3 Memory system operation
1.4 Processor
1.5 Program Execution
1.6 HCS12 Microcontroller
Microprocessor VS Microcontroller
- A microprocessor (MP) needs external devices such asmemory, I/O ports, etc to run a program.
- A microcontroller (MCU) contains a microprocessor, memorytimer, and I/O ports within a single chip.
- Microcontrollers have been used in almost every product thatrequires a certain amount of intelligence.
- Microcontrollers have been used in printers, modems, MP3players, cars, home appliances such as washing machines,microwave ovens, etc.
1 - 30
1 - 31
Embedded Systems
- An embedded system is a special purpose computer systemdesigned to perform a dedicated function.
- Examples of embedded systems: MP3 players, traffic lights,digital watches
– Runs “firmware” which is stored on read only memory chips
Interesting applicationsSmart mirror: https://www.youtube.com/watch?v=xBDrevuomfoSmart spoon: https://www.youtube.com/watch?v=WiVQcgmIi08Smart baby bottle: https://www.youtube.com/watch?v=J7J94oZ8bqs
The HCS12 Features
- 16-bit CPU (the operations can be done on 16 bit data)
- Maximum operating clock frequency is 25 MHz
- 2KB to 14KB of on-chip SRAM (12KB in MC9S12DG256 )
- 4K bytes of EEPROM in MC9S12DG256
- 32 KB to 512KB on-chip flash memory (256 KB inMC9S12DG256 )
- Timer and Pulse counters
- I/O pins to interface to I/O devices
- Serial communication
- A/D 1 - 32
HCS12 microcontroller
1 - 33
- Static random access memory (SRAM) is volatile.
1 - 34
Flash memory:
- Non-volatile memory does not lose the information whenthere is no power, but volatile memory does.
- Electrically erasable and programmable many times by theMP.
- The most widely used nonvolatile memory technology.
- In embedded systems, the program is stored in anonvolatile memory. Why?
HCS12 registers:
- Registers are referred to by their names whereas memorylocations are referred to by their addresses
- Used to perform general purpose operations such as arithmetic,logic, and program flow control.
- All the registers are 16 bits except the accumulators A and Bwhich are 8 bits each.
- A and B can be concatenated to form a single 16-bit registeraccumulator referred as the D accumulator
- When A changes B is unaffected and vice versa, but when Dchanges both A and B change.
1 - 35
The HCS12 CPU Registers
7 00 7A B
15 0D
8-bit accumulator A and Bor16-bit double accumulator D
15 0X
15 0Y
Index register X
Index register Y
15 0SP Stack pointer
15 0PC Program counter
Condition code register
CarryOverflowZeroNegativeI Interrupt maskHalf-Carry (from bit 3)X Interrupt MaskStop Disable
Figure 1.10 HCS12 CPU registers.
S X H I N Z V C
1 - 36
- Index registers X and Y: 16 bits. Used to hold addresses. Theyare also used in several arithmetic instructions.
- Stack pointer (SP): 16 bits. A stack is a first-in-first-out datastructure. SP points to the top byte of the stack as shown infigure.
- Program counter (PC): 16 bits holds the address of the nextinstruction to be executed.
Lower address
Higher address
Top of stack SP
Pop
Push
- Condition code register (CCR):
8-bit register used to
- Flags are automatically updatedafter executing each instructionto reflect the status of theresult.
- Used to make a condition inconditional branch.
1 - 37
- Each bit in the CCR register is independent and has a differentindication.
- Zero flag (Z): set when the result is zero
- Negative flag (N): set whenever the result is negative, i.e., mostsignificant bit of the result is 1.
- Carry/borrow flag (C): set when addition/subtraction generates acarry/borrow.
- Overflow flag (V): Set when:
the addition of two positive numbers results in anegative number
or
the addition of two negative numbers results in apositive number. 1 - 38
Overflow
Overflow cannot occur when adding numbers of opposite sign why?
Problem: Registers have fixed number of bits which means they canstore a maximum number
Overflow occurs when two numbers are added or subtracted and thecorrect result is outside the range that can a register hold thegiven result is not correct
1 - 39
Addition:
C = 1 there is an overflow if the numbers are unsigned.
V = 1 there is an overflow if the numbers are signed.
Subtraction: A - BThere is no unsigned overflow but there is signed overflow why?C = 1, when there is a borrow or B > A, C is called borrow flagV =1, when(-ve) - (+ve) = (+ve) this is equivalent to (–ve) + (-ve) = (+ve)(+ve) - (-ve) = (-ve) this is equivalent to (+ve) + (+ve) = (-ve)
Signed numbers: -1 +1 = 0 , no overflow and the result is correct
Unsigned numbers: 255 +1 = 0, incorrect, the correct result is 256, overflow because the max. unsigned number for 8 bit number is 255
1010 1010+ 0101 0101
1111 1111C = 0, V = 0, Z = 0, N = 1
C = 1, V = 0, Z = 1, N =01111 1111
+ 0000 00010000 0000 1
0 1111 1111+ 0 0000 0001
1 0000 0000
By using more bits (9 bits or more) instead of 8 bits, the result is correct, no overflow
Signed numbers: -86 + 85 = -1 , no overflow and the result is correct
Unsigned numbers: 170 + 85 = 255, no overflow and the result is correct 1 - 40
1 - 41
0111 1111+ 0000 0001
1000 0000C = 0, V = 1, Z = 0, N = 1
Unsigned numbers: 127 + 1 = 128, no overflow and the result is correct
Signed numbers: 127 + 1 = -128 , there is overflow, the result is incorrect, the max. positive number in 8 bits is 127 that is less than the correct answer 128. If we use 9 bit addition, the result will be correct because 128 can be represented in 9 bits.
0 0111 1111+ 0 0000 0001
0 1000 0000C = 0, V = 0, Z = 0, N = 0
1010 1100+ 1000 1010
0011 0110C = 1, V = 1, Z = 0, N = 0
1
Unsigned numbers: 172 + 138 = 54 should be 310, overflow, 255 is the max. number for 8 bit number
Signed numbers: -84 + (-118) = 54, should be -202, overflow, the max. negative number in 8 bits is -128 that is less than the correct answer
0111 1010+ 1010 0100
If we use 9 bit addition, the result will be correct.
0 1010 1100+ 0 1000 1010
1 0011 0110C = 0Unsigned numbers:
1 1010 1100+ 1 1000 1010
1 0011 0110V = 0Signed numbers:
1 - 42
Subtraction: A – B = A + the two’s complement of B
0111 1010- 0101 1100
V = 0, C = 0, N = 0, Z =0
0001 11101
C is called borrow flag and it is set when we need to borrow from the most significant byte
Unsigned numbers: 122 - 92 = 30 correctSigned numbers: 122 - 92 = 30 correctV = 0 because (+ve) – (+ve) no overflow
0101 1100+ 0111 0110
0101 1100- 1000 1010 V = 1, C = 1, N = 1, Z =0
1101 0010
Unsigned numbers:
- There is a borrow, 92 – 138 = 210- What happened is (92+256) -138 = 210, where 256 is the borrow- If we do muti-byte subtraction, the result (210) is right and we should
subtract one from the next byte.- If you want to get the absolute difference 46, subtract the small number
from the bigger one or 1000 1010 - 0101 1100 = 46
Signed numbers:-92 – (-118) = -46 should be 210, V = 1 means the numbers should be represented in more bits
0 0101 1100+ 0 0111 0110
0 0101 1100- 1 1000 1010
V = 0
0 1101 0010 1 - 43
There is one set of flags whether the numbers are signed or not, e.g. N flag will consider the numbers signed even if they are not
Questions
Mohamed Mahmoud