1
Microprocessor Systems and Instrumentation
SOE2121
2
Microprocessor Systems and Instrumentation
SOE2121
Number Systems
3
Machine code:
01010011010100101010010010101001010010010000111100010100110010010100101000100010010101010010010010100101000010101010100101110001011100010010101000101001000100101001010100100101000100010010100010101001001010010100001010101010010111000101110001001010100010100100010010100101010010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100110010010100010001001100100101000100010011001001010001000100110010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100101000101010001111100100101001001010010100001000100010010111
4
Machine code:
01010001 01010010 10100100 10101001 01001001 00001111 00010100 11001001 01001010 00100010 01010101 00100100 10100101 00001010 10101001 01110001 01110001 00101010 00101001 00010010 10010101 00100101 00010001 00101000 10101001 00101001 01000010 10101010 01011100 01011100 01001010 10001010 01000100 10100101 01001001 01000100 01001010 00101010 01001010 01010000 10101010 10010111 00010111 00010010 10100010 10010001 00101001 01010010 01010001 00010011 00100101 00010001 00110010 01010001 00010011 00100101 00010001 00110010 01010001 00010010 10001010 10010010 10010100 00101010 10100101 11000101 11000100 10101000 10100100 01001010
5
Machine code:
DB 33 37 56 5E 4B 67 85 44 34 77 62 A3 D9 FF 03 74 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 76 F5 E2 AB 72 97 43...
6
The Decimal System
Decimal Number 38O7: 3 x 1OOO ( = 1O3) 3OOO 8 x 1OO ( = 1O2) 8OO O x 1O ( = 1O1) OO 7 x 1 ( = 1OO) 7 ---- Total: 38O7
7
The Binary System
Binary Number 1O11: 1 x 8 ( = 23) 8 O x 4 ( = 22) O 1 x 2 ( = 21) 2 1 x 1 ( = 2O) 1 -- Total: 11
8
The Binary System
Bit value: 128 64 32 16 8 4 2 1 Power of 2: 27 26 25 24 23 22 21 2O
Number: 1 0 0 1 0 1 1 0
9
The Decimal and Binary Systems
Decimal Number 38O7: 3 x 1OOO ( = 1O3) 3OOO 8 x 1OO ( = 1O2) 8OO O x 1O ( = 1O1) OO 7 x 1 ( = 1OO) 7 ---- Total: 38O7
Binary Number 1O11: 1 x 8 ( = 23) 8 O x 4 ( = 22) O 1 x 2 ( = 21) 2 1 x 1 ( = 2O) 1 -- Total: 11
10
The Hexadecimal System
Hexadecimal Number 21AF: (Base 16) 2 x 4O96 ( = 163) 8192 1 x 256 ( = 162) 256 A x 16 ( = 161) 16O F x 1 ( = 16O) 15 ---- Total: 8623
11
Number Systems
Decimal Numbers (Base 1O) 1O symbols: O 1 2 3 4 5 6 7 8 9 Binary Numbers (Base 2) 2 symbols: O 1 Hexadecimal Numbers(Base 16) 16 symbols: O 1 2 3 4 5 6 7 8 9 A B C D E F
12
Brain Exercises (Neurobics)
* Binary counting on fingers (and toes after ?)
* Binary Arithmetic - + Addition - Subtraction / Division * Multiplication
* Hexadecimal Arithmetic - + Addition - Subtraction / Division * Multiplication
13
HEX BINARY DECIMAL
O OOOO O 1 OOO1 1 2 OO1O 2 3 OO11 3 4 O1OO 4 5 O1O1 5 6 O11O 6 7 O111 7 8 1OOO 8 9 1OO1 9 A 1O1O 1O B 1O11 11 C 11OO 12 D 11O1 13 E 111O 14 F 1111 15
14
Converting between Binary and Hexadecimal
Binary: 0010 1100 32+8+4=44
Hex: 2 C 32+12=44
15
Converting between Binary and Hexadecimal
111O1OO111O1 = 111O 1OO1 11O1 E 9 D 12AB = 1 2 A B OOO1 OO1O 1O1O 1O11
16
Number conversions - Hex to Decimal:
Convert 1234 hex to decimal:
163 162 161 160
4096 256 16 1
1 2 3 41 x 4096 = 4096
2 x 256 = 512
3 x 16 = 48
4 x 1 = 4 +
-----
17
Number conversions - Decimal to Binary:
Convert 1234 decimal to binary:
210 1024 - 1 1234 - 1024 = 210
29 512 - 0 28 256 - 0 27 128 - 1 210 - 128 = 82 26 64 - 1 82 - 64 = 18 25 32 - 0 24 16 - 1 18 - 16 = 2 23 8 - 0 22 4 - 0 21 2 - 1 2 - 2 = 0 20 1 - 0
So 1234 decimal = 100 1101 0010 binary
18
Number conversions - Binary to Hex:
eg binary number 11010011101011010
First divide up FROM THE RIGHT into groups of 4 bits:
1 1010 0111 0101 1010
Then use the hex table (or better your brain) to write the hex values:
1 1010 0111 0101 1010 1 A 7 5 A
Note: learn 0-9 and remember A=1010 and C=1100
19
HEX BINARY DECIMAL
O OOOO O 1 OOO1 1 2 OO1O 2 3 OO11 3 4 O1OO 4 5 O1O1 5 6 O11O 6 7 O111 7 8 1OOO 8 9 1OO1 9 A 1O1O 1O B 1O11 11 C 11OO 12 D 11O1 13 E 111O 14 F 1111 15
20
Negative Numbers - Twos Complement form:
To form a negative number, for example -3, first invert (ones complement) the positive number of the same magnitude (+3), then add one:
00000011 +3invert to give
11111100 ones complement00000001+ add one--------11111101 result is -3 (Hex FD)
21
Negative Numbers:
Most negative number -128
Most positive number +127
Note: bit 7 carries the sign information 1=negative 0=positive
7 0
00000001
7 0
11111110
22
Negative Numbers:
0111 1111 7F +1270111 1110 7E +126 | 0000 0011 03 +30000 0010 02 +20000 0001 01 +10000 0000 00 01111 1111 FF -11111 1110 FE -21111 1101 FD -3 |1000 0001 81 -127
1000 0000 80 -128
23
Signed and Unsigned Arithmetic:
Why does it work for both unsigned numbers (0 to 255) and signed numbers (-128 to +127)?
Unsigned Data Signed
253 FD -3 1 01+ +1
--- --- --- 254 FE -2
24
Binary Coded Decimal:
Each decimal digit is encoded as a 4 bit binary number:
O OOOO 1 OOO1 2 OO1O 3 OO11 4 O1OO 5 O1O1 6 O11O 7 O111 8 1OOO 9 1OO1
eg decimal 42 0100 00104 2
What is this value in Hexadecimal? (Note 42 in binary is 0010 1010
Hex 2A)
25
Binary Coded Decimal:
Range of BCD numbers in one byte 00 - 99
Reading a BCD number in hex gives the decimal value
26
HEX BINARY DECIMAL
O OOOO O 1 OOO1 1 2 OO1O 2 3 OO11 3 4 O1OO 4 5 O1O1 5 6 O11O 6 7 O111 7 8 1OOO 8 9 1OO1 9 A 1O1O 1O B 1O11 11 C 11OO 12 D 11O1 13 E 111O 14 F 1111 15
27
ASCII Table:
MSD->0 1 2 3 4 5 6 7
LSD
0 NUL DLE SP 0 @ P ` p
1 SOH DC1 ! 1 A Q a q
2 STX DC2 " 2 B R b r
3 ETX DC3 # 3 C S c s
4 EOT DC4 $ 4 D T d t
5 ENQ NAK % 5 E U e u
6 ACK SYN & 6 F V f v
7 BEL ETB ' 7 G W g w
8 BS CAN ( 8 H X h x
9 HT EM ) 9 I Y i y
A LF SUB * : J Z j z
B VT ESC + ; K [ k {
C FF FS , < L ` l |
D CR GS - = M ] m }
E SO RS . > N ^ n ~
F SI VS / ? O _ o DEL
28
The Byte
7 6 5 4 3 2 1 0
1 Byte = 8 bits Contains one of 256 possible patterns OOOOOOOO OOOOOOO1 OOOOOO1O OOOOOO11 OOOOO1OO |
11111111 Range $OO-$FF = O to 255
29
The Byte
7 6 5 4 3 2 1 0
Most Significant Bit Least Significant Bit
30
Which is Most Significant?
A typical lecturer’s salary might be:
£93,878
Most Significant Digit Least Significant Digit
31
Hexadecimal Representation:
Binary Hex Meaning Value
0100 0001 41 Unsigned Binary 65 Decimal
0100 0001 41 ASCII code A
0100 0001 41 BCD number 41 Decimal
1111 1111 FF Signed Binary -1 Decimal
1111 1111 FF Unsigned Binary 255 Decimal
1010 0101 A5 Opcode LDA
7 6 5 4 3 2 1 0
32
Brain Exercises (Neurobics)
* Binary counting on fingers (and toes after ?)
* Binary Arithmetic - + Addition - Subtraction / Division * Multiplication
* Hexadecimal Arithmetic - + Addition - Subtraction / Division * Multiplication
33
IntroductionIntroduction
Load and StoreLoad and Store
TransferTransfer
34
Not Recommended Book:
65O2 Assembly Language Programming
by L A Leventhal
Pub: Osborne/McGraw-Hill
35
Assembly Language
Advantages:
Fastest possible program (on a particular processor)
Smallest size program (cheaper ROM)Smallest RAM requirement (cheaper RAM)
Disadvantages:
Much longer software development timePrograms more difficult to debugPrograms not portable
36
Assembly Language: When is it used?
MASS PRODUCTION
Where the lowest possible production cost is required and longer more expensive software development time is acceptable. eg microwave oven, mobile phone
HIGH SPEED APPLICATIONS
Where a high level language would not respond quickly enough. eg high speed data acquisition system
Notes: faster processors, critical bits in assembler, smallest physical size eg Pacemaker where power consumption is also a consideration.
37
Microprocessor Systems and Instrumentation
SOE2121
Number Systems
38
Machine code:
01010011010100101010010010101001010010010000111100010100110010010100101000100010010101010010010010100101000010101010100101110001011100010010101000101001000100101001010100100101000100010010100010101001001010010100001010101010010111000101110001001010100010100100010010100101010010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100110010010100010001001100100101000100010011001001010001000100110010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100101000101010001111100100101001001010010100001000100010010111
39
Machine code:
01010001 01010010 10100100 10101001 01001001 00001111 00010100 11001001 01001010 00100010 01010101 00100100 10100101 00001010 10101001 01110001 01110001 00101010 00101001 00010010 10010101 00100101 00010001 00101000 10101001 00101001 01000010 10101010 01011100 01011100 01001010 10001010 01000100 10100101 01001001 01000100 01001010 00101010 01001010 01010000 10101010 10010111 00010111 00010010 10100010 10010001 00101001 01010010 01010001 00010011 00100101 00010001 00110010 01010001 00010011 00100101 00010001 00110010 01010001 00010010 10001010 10010010 10010100 00101010 10100101 11000101 11000100 10101000 10100100 01001010
40
Machine code:
DB 33 37 56 5E 4B 67 85 44 34 77 62 A3 D9 FF 03 74 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 99 55 64 EE E7 F2 83 82 99 55 76 F5 E2 AB 72 97 43...
41
HEX BINARY DECIMAL
O OOOO O 1 OOO1 1 2 OO1O 2 3 OO11 3 4 O1OO 4 5 O1O1 5 6 O11O 6 7 O111 7 8 1OOO 8 9 1OO1 9 A 1O1O 1O B 1O11 11 C 11OO 12 D 11O1 13 E 111O 14 F 1111 15
42
The Byte
7 6 5 4 3 2 1 0
1 Byte = 8 bits Contains one of 256 possible patterns OOOOOOOO OOOOOOO1 OOOOOO1O OOOOOO11 OOOOO1OO |
11111111 Range $OO-$FF = O to 255
43
The Byte
7 6 5 4 3 2 1 0
Most Significant Bit Least Significant Bit
44
Which is Most Significant?
A typical lecturer’s salary might be:
£93,878
Most Significant Digit Least Significant Digit
45
The Byte
7 6 5 4 3 2 1 0
1 Byte = 8 bits Contains one of 256 possible patterns OOOOOOOO OOOOOOO1 OOOOOO1O OOOOOO11 OOOOO1OO |
11111111 Range $OO-$FF = O to 255
46
Hexadecimal Representation:
Binary Hex Meaning Value
0100 0001 41 Unsigned Binary 65 Decimal
0100 0001 41 ASCII code A
0100 0001 41 BCD number 41 Decimal
1111 1111 FF Signed Binary -1 Decimal
1111 1111 FF Unsigned Binary 255 Decimal
1010 0101 A5 Opcode LDA
7 6 5 4 3 2 1 0
47
6502 Programmer’s Model
00000001
NV-BDIZC
A
X
PC
SP
PS
Y
7 0
15 8
FFFF
020001FF
0100
00FF
0000
Microprocessor
Memory
STACK
PAGE ZERO
7 0
48
Memory
65536 bytes numbered
in decimal: O - 65535 in hex: OOOO - FFFF in binary: OOOO OOOO OOOO OOOO - 1111 1111 1111 1111
The number of each memory location is known as its ADDRESS 2 Bytes are required to hold an address which is 16 bits
49
Instruction Formats
One byte instructions:
Two byte instructions:
Three byte instructions:
OPCODE
OPCODE
OPCODE
OPERAND
OPER AND
50
Load and Store Instructions
51
The LDA Instruction
Copy the contents of memory location 8O into the A register: LDA 8O This instruction uses zero page addressing since address $8O is on page zero
52
The LDA Instruction and the Instruction Set Sheet
Assembly Language: LDA 8O Machine Code: A5 8O Instruction Set Sheet Entry: A5 3 Opcode: A5 Time: 3 clock cycles Length: 2 bytes
53
The LDA Instruction and the Instruction Set Sheet
+---------------------------------------------+ | |Immed| ABS | ZP |Effect on | | Operation |len=2|len=3|len=2|the flags:| | |OP n|OP n|OP n| NV-BDIZC |+-----+----------------+-----+-----+-----+----------+
| LDA | A := M |A9 2|AD 4|A5 3| N.....Z. |
Assembly Language: LDA 8O Machine Code: A5 8O Instruction Set Sheet Entry: A5 3 Opcode: A5 Time: 3 clock cycles Length: 2 bytes
54
The LDA Instruction and the Instruction Set Sheet
Assembly Language: LDA 8O Machine Code: A5 8O Instruction Set Sheet Entry: A5 3 Opcode: A5 Time: 3 clock cycles Length: 2 bytes
Effect on the Flags: N.....Z.
N and Z changed depending on the value that is loaded
The N flag becomes the sign bit (7) of the value loaded The Z flag is 1 if the value loaded is zero The Z flag is O if the value loaded is not zero
55
Load and Store instructions
Absolute Zero Page
LDA 1234 LDA 8O
STA 1234 STA 8O
LDX 1234 LDX 8O
STX 1234 STX 8O
LDY 1234 LDY 8O
STY 1234 STY 8O
56
Immediate Addressing
Instruction Machine code: LDA #AA A9 AA LDX #FF A2 FF LDY #O1 AO O1
57
Don’t mix these up!
Zero Page: LDA OO Immediate: LDA #OO
58
Transfer Instructions
59
Transfer instructions:
TAX Copy register A to register X The old value in X is lost Register A is not affected
60
Transfer instructions:
TAX TAY TXA TYA
(TXS TSX)
All 1 byte Long Implied Addressing 2 Clock Cycles Flags as for LDA
61
ArithmeticInstructions
ADC SBC
62
Negative Numbers
63
Negative Numbers - Twos Complement form:
To form a negative number, for example -3, first invert (ones complement) the positive number of the same magnitude (+3), then add one:
00000011 +3invert to give
11111100 ones complement00000001+ add one--------11111101 result is -3 (Hex FD)
64
Negative Numbers:
Most negative number -128
Most positive number +127
Note: bit 7 carries the sign information 1=negative 0=positive
7 0
00000001
7 0
11111110
65
Signed and Unsigned Arithmetic:
Why does it work for both unsigned numbers (0 to 255) and signed numbers (-128 to +127)?
Unsigned Data Signed
253 FD -3 1 01+ +1
--- --- --- 254 FE -2
66
ADC
67
Addition:
CLC
LDA 8O ADC 81 STA 82
68
Two byte arithmetic:
How do we do it in decimal?
3625 +
---61
69
Addition and Subtraction:
CLC SEC LDA 8O LDA 8O ADC 81 SBC 81 STA 82 STA 82
70
Two byte arithmetic:
LSB LSB LSB MSBMSBMSB
First Number: Second Number: Result:
80 81 82 83 84 85
CLC SEC LDA 8O LSBs LDA 8O LSBs ADC 82 SBC 82 STA 84 STA 84 LDA 81 MSBs LDA 81 MSBs ADC 83 SBC 83 STA 85 STA 85
Addition: Subtraction:
71
Decimal Arithmetic
72
6502 Programmer’s Model
00000001
NV-BDIZC
A
X
PC
SP
PS
Y
7 0
15 8
FFFF
020001FF
0100
00FF
0000
Microprocessor
Memory
STACK
PAGE ZERO
7 0
73
Decimal Arithmetic (BCD):
SED SED CLC SEC LDA 8O LDA 8O ADC 81 SBC 81 STA 82 STA 82 CLD CLD
Addition: Subtraction:
74
Logical Logical InstructionsInstructions
75
Logical Operations:
76
Logical Operations:
And: AND
77
Logical Operations:
And: AND
Or: ORA
78
Logical Operations:
And: AND
Or: ORA
Exclusive Or: EOR
79
AND
80
The AND Instruction: LDA #A9 1O1O 1OO1 (A) AND #OF OOOO 1111 (M) --------- Result: OOOO 1OO1 Any O in the mask will clear the corresponding bit in A to O Any 1 in the mask will leave the corresponding bit in A unchanged
81
OR
82
The ORA Instruction: LDA #A9 1O1O 1OO1 (A) ORA #OF OOOO 1111 (M) --------- Result: 1010 1111 Any 1 in the mask will set the corresponding bit in A to 1 Any 0 in the mask will leave the corresponding bit in A unchanged
83
EOR
84
The EOR Instruction: LDA #A9 1O1O 1OO1 (A) EOR #OF OOOO 1111 (M) --------- Result: 1010 0110 Any 1 in the mask will invert the corresponding bit in A (so 1 becomes 0 and 0 becomes 1) Any 0 in the mask will leave the corresponding bit in A unchanged
85
Testing Testing individual bits individual bits
in a bytein a byte
86
Testing Individual Bits in a Byte: To test bit 2 of location $8O LDA 8O byte to test AND #O4 mask OOOO O1OO
87
The Byte
7 6 5 4 3 2 1 0
0 0 0 0 0 1 0 0
Hex 04
?
88
Testing Individual Bits in a Byte: eg - to test bit 2 of location $8O LDA 8O byte to test AND #O4 mask OOOO O1OO
89
Testing Individual Bits in a Byte: eg - to test bit 2 of location $8O LDA 8O byte to test AND #O4 mask OOOO O1OO $8O contains $B7 A 1O11 O111 M OOOO O1OO --------- OOOO O1OO Z flag =O
90
Testing Individual Bits in a Byte: eg - to test bit 2 of location $8O LDA 8O byte to test AND #O4 mask OOOO O1OO $8O contains $B7 $8O contains $69 A 1O11 O111 A O11O 1OO1 M OOOO O1OO M OOOO O1OO --------- --------- OOOO O1OO OOOO OOOO Z flag =O Z flag =1
91
The BIT Instruction: (this is rather a funny
instruction) eg LDA #O1 mask for AND BIT 8O byte to test
Z is set by the result of the AND:
1OO1 1OO1 OOOO OOO1 --------- OOOO OOO1 ie non-zero so Z =O
1 0 0 1 1 0 0 1
7 6 5 4 3 2 1 0
N V
92
Logical instructions - Example:
LDA #33 Result is: OO11 OO11 33 AND #OF ORA #9O EOR #8O EOR #FF
93
Logical instructions - Example:
LDA #33 Result is: OO11 OO11 33 AND #OF Result is: OOOO OO11 O3 ORA #9O EOR #8O EOR #FF
94
Logical instructions - Example:
LDA #33 Result is: OO11 OO11 33 AND #OF Result is: OOOO OO11 O3 ORA #9O Result is: 1OO1 OO11 93 EOR #8O EOR #FF
95
Logical instructions - Example:
LDA #33 Result is: OO11 OO11 33 AND #OF Result is: OOOO OO11 O3 ORA #9O Result is: 1OO1 OO11 93 EOR #8O Result is: OOO1 OO11 13 EOR #FF
96
Logical instructions - Example:
LDA #33 Result is: OO11 OO11 33 AND #OF Result is: OOOO OO11 O3 ORA #9O Result is: 1OO1 OO11 93 EOR #8O Result is: OOO1 OO11 13 EOR #FF Result is: 111O 11OO EC
97
Shift and Rotate
Instructions
98
Shift and Rotate Instructions:
ASL
99
Shift and Rotate Instructions:
ASL LSR
100
Shift and Rotate Instructions:
ASL LSR ROL
101
Shift and Rotate Instructions:
ASL LSR ROL ROR
102
The ASL instruction: The bits in the byte are shifted one position to the left
A zero is shifted into bit O Bit 7 is shifted out into the carry flag
eg ASL 42 ASL 21OF ASL A New Addressing Mode!
0
C 7 0
103
The LSR instruction: The bits in the byte are shifted one position to the right
A zero is shifted into bit 7 Bit 0 is shifted out into the carry flag
eg LSR 42 LSR 21OF LSR A
0
C7 0
104
The ASL instruction:
C 7 0
0 01000000
105
The ASL instruction:
C 7 0
0 00100000
106
The ASL instruction:
C 7 0
0 00010000
107
The ASL instruction:
C 7 0
0 00001000
108
The ASL instruction:
C 7 0
0 00000100
109
The ASL instruction:
C 7 0
0 00000010
110
The ASL instruction:
C 7 0
0 00000001
111
The ASL instruction:
C 7 0
1 00000000
112
The ASL instruction:
C 7 0
0 00000000
113
The ROL instruction: The bits in the byte are shifted one position to the left The original value of the carry is shifted into bit O Bit 7 is shifted out into the carry flag
C 7 0
114
The ROR instruction: The bits in the byte are shifted one position to the right The original value of the carry is shifted into bit 7 Bit 0 is shifted out into the carry flag
C 7 0
115
eg LSR 80 msb
ROR 81 lsb
80 81
1 0 0 1 1 0 0 1
7 6 5 4 3 2 1 0
1 0 0 1 1 0 0 1
7 6 5 4 3 2 1 0
1
0
2 Byte Shift
C
116
Jumping and Branching and
Comparing
117
Unconditional Jump - JMP
O25O CLC O251 LDA 4O O253 ADC 41 O255 STA 42 --O257 JMP O25E | O25A LDA 8O | O25C STA 81 --O25E LDA FOFA O261 STA 35
118
Conditional Branch Instructions: BNE Branch if not equal to zero Z =OBEQ Branch if equal to zero Z =1 BCC Branch if carry clear C =OBCS Branch if carry set C =1 BPL Branch if plus (positive) N =OBMI Branch if minus (negative) N =1 (BVC Branch if overflow clear V =O)(BVS Branch if overflow set V =1)
119
Conditional Branch Instructions:
O25O LDA 8O --O252 BNE O256 | O254 LDA 81 --O256 STA 82 O258 BRK O25O A5 (LDA) -4 = FC O251 8O -3 = FD O252 DO (BNE) -2 = FE O253 O2 -1 = FF O254 A5 (LDA) O = OO O255 81 +1 = O1 O256 85 (LDA) +2 = O2 O257 82 +3 = O3 O258 OO (BRK) +4 = O4
120
Delay Loop: 10s
O4OO LDA #12 ;$12 = 18 decimal O4O2 STA 82 O4O4 LDA #OO O4O6 STA 8O O4O8 STA 81 O4OA DEC 8O O4OC BNE O4OA ;inner loop O4OE DEC 81 O41O BNE O4OA ;next loop O412 DEC 82 O414 BNE O4OA ;outer loop O416 RTS
121
ComparingCMP CPX CPY
122
Compare Instructions: CMP CPX CPY Examples:
CMP 8O CPX 8O CPY 8O CMP 1234 CPX 1234 CPY 1234 CMP #O3 CPX #O3 CPY #O3 The Z and C flags are changed after a compare instruction as follows: A = M then Z =1 (BEQ will branch) A <> M then Z =O (BNE will branch) A >= M then C =1 (BCS will branch) A < M then C =O (BCC will branch)
123
To compare the contents of $5O and $51:
O25O LDA 5O first number O252 CMP 51 second number O254 BEQ O26O branch if first=second O256 BCS O27O branch if first>second O258 BCC O28O branch if first<second O25A | O26O here if first=second | O27O here if first>second | O28O here if first<second
124
Loops and Indexed
addressing
125
Incrementing and
Decrementing
126
Incrementing and Decrementing: Memory Locations: INC 8O DEC 2F INC O412 DEC 12BA Registers: INX DEX INY DEY
127
Car Mileometer
What happens if you drive forward one more mile?
9 9 9 9 9
128
Car Mileometer
What happens if you reverse one mile?
0 0 0 0 0
129
Decrementing X
Instruction Value of X after instruction
04 DEX O3 DEX O2 DEX O1 DEX OO (Z flag set)
DEX FF DEX FE DEX FD
130
Incrementing X
Instruction Value of X after instruction
FC INX FD INX FE INX FF INX OO (Z flag set)
INX 01 INX 02 INX 03
131
Looping
132
Loop Example: add the contents of locations 80 to 85
O25O LDA #OO A will hold sum O252 LDX #OO X - loop counter O254 CLC -O255 ADC 8O,X Add next byte | O257 INX | O258 CPX #O6 Have we finished? -O25A BNE O255 If not continue O25C STA 8A Store the sum O25E BRK
80 81 82 83 84 85 86 87
133
Indexed Addressin
g
134
Indexed Addressing:
Zero Page,X ADC 8O,X
Zero Page,Y LDX 8O,Y
Absolute,X LDA 123A,X
Absolute,Y LDA 456B,Y
135
Delay Loop: 10ms O3OO LDY #OA;outer loop 1O times O3O2 LDX #C8;inner loop 2OO times O3O4 DEX O3O5 BNE O3O4 O3O7 DEY O3O8 BNE O3O2 O3OA RTS
136
Delay Loop: 10s
O4OO LDA #12 ;$12 = 18 decimal O4O2 STA 82 O4O4 LDA #OO O4O6 STA 8O O4O8 STA 81 O4OA DEC 8O O4OC BNE O4OA ;inner loop O4OE DEC 81 O41O BNE O4OA ;next loop O412 DEC 82 O414 BNE O4OA ;outer loop O416 RTS
137
The NOP Instruction
NOP does nothing 1 byte 2 clock cycles
138
Subroutines and the
Stack
139
Subroutines:
0200 LDA 94
AND #07
JSR 05000207 LDA 94 0500 LDA 80
ORA #80 AND #0F
JSR 0500 RTS020E LDX #08
JSR 05000213 STA 60,X
DEX
|
140
Subroutines:
JSR 0500
RTS
141
The Stack:
SP 01 FF
A1 01FF
35 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
142
Push and Pull Instructions: Register A:
PHA store on stack
PLA get from stack
Flag Register:
PHP store on stack
PLP get from stack
143
SubroutinesSubroutines
LDA #30LDA #30STA 51STA 51CLCCLCADC #42ADC #42LSR ALSR A
JSR 0400JSR 0400CMP #05CMP #05BNE 0294BNE 0294JMP 0280JMP 0280LDA 50LDA 50ADC #42ADC #42LSR ALSR ACMP #05CMP #05BNE 0294BNE 0294JMP 0280JMP 0280LDA 50LDA 50STA 62STA 62ROR 7EROR 7ELDA #30LDA #30STA 51STA 51CLCCLCADC #42ADC #42LSR ALSR ACMP #05CMP #05BNE 0294BNE 0294JMP 0280JMP 0280LDA 50LDA 50STA 62STA 62
LDA #30LDA #30STA 51STA 51CLCCLCADC #42ADC #42LSR ALSR A
JSR 0500JSR 0500STA 9ASTA 9ALSR ALSR ASTA 3BSTA 3B
RTSRTS
0400
LSR ALSR ASTA 6FSTA 6FSTA 77STA 77
RTSRTS
0500
SP 01 FF
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0250
0262 0413
* Before First JSR
*
144
The Stack:
SP 01 FF
A1 01FF
35 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
145
The Stack:
Store the value AA on the stack
SP 01 FF
A1 01FF
35 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
146
The Stack:
Store the value AA on the stack
SP 01 FE
AA 01FF
35 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
147
The Stack:
Store the value F3 on the stack
SP 01 FE
AA 01FF
35 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
148
The Stack:
Store the value F3 on the stack
SP 01 FD
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
149
The Stack:
The Stack pointer always contains theaddress of the next free location
SP 01 FD
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
150
The Stack:
Get a byte from the stack
SP 01 FD
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
151
The Stack:
Get a byte from the stackThe Stack Pointer is firstincremented to point to thelast value stored
SP 01 FE
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
152
The Stack:
Get a byte from the stack:
We get the value F3 -the last value stored01FE is now the next free location
SP 01 FE
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
0100
|
153
What happens when a subroutine is called: before JSR
0260 JSR 05000263 LDA #00
0500 LDX #030502 TAY0503 RTS
SP 01 FF
AA 01FF
F3 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
154
What happens when a subroutine is called: after JSR
0260 JSR 05000263 LDA #00
0500 LDX #030502 TAY0503 RTS
SP 01 FD
02 01FF
62 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
155
What happens when a subroutine is called: after RTS
0260 JSR 05000263 LDA #00
0500 LDX #030502 TAY0503 RTS
SP 01 FF
02 01FF
62 01FE
29 01FD
81 01FC
12 01FB
7B 01FA
5D 01F9
156
InterruptsInterrupts
157
InterruptsInterrupts
Main Program:Main Program: LDA #30LDA #30
STA 51STA 51CLCCLCADC #42ADC #42LSR ALSR ACMP #05CMP #05BNE 0294BNE 0294JMP 0280JMP 0280 PHAPHALDA 50LDA 50 LDA #33LDA #33STA 62STA 62 STA 51STA 51ROR 7EROR 7E LDA A000LDA A000LDA #30LDA #30 STA 80STA 80STA 51STA 51 LDA A001LDA A001CLCCLC STA 81STA 81
ADC #42ADC #42 INC 9AINC 9ALSR ALSR A DEC 9BDEC 9BCMP #05CMP #05 PLAPLABNE 0294BNE 0294 RTIRTIJMP 0280JMP 0280LDA 50LDA 50STA 62STA 62ROR 7EROR 7ELDA #30LDA #30STA 51STA 51CLCCLCADC #42ADC #42LSR ALSR ACMP #05CMP #05BNE 0294BNE 0294JMP 0280JMP 0280LDA 50LDA 50STA 62STA 62
Interrupt Interrupt occurs hereoccurs here
Interrupt Routine:Interrupt Routine:
When an interrupt occurs, the return address is stored on the stack, in the same way as for a subroutine call. The contents of the flag register is also stored on the stack.
158
6502 Interrupt inputs:
___IRQ
___NMI
___RES
6502 microprocessor
5v
0v
10K
159
6502 Vectors:
___ NMI $FFFA + $FFFB ___ RES $FFFC + $FFFD ___ IRQ $FFFE + $FFFF
160
Vectors:
FFFFFFFEFFFDFFFCFFFBFFFA
Memory7 0
IRQ LSB
IRQ MSB
RES MSB
RES LSB
NMI MSB
NMI LSB
161
6502 Programmer’s Model
00000001
NV-BDIZC
A
X
PC
SP
PS
Y
7 0
15 8
FFFF
020001FF
0100
00FF
0000
Microprocessor
Memory
STACK
PAGE ZERO
7 0
162
The Interrupt Flag I:
CLI set I = O - Enable IRQ interrupts
SEI set I = 1 - Disable IRQ interrupts
163
Saving registers used in interrupt routines:
PHA ; save A (no need to save the flags) TXA PHA ; save X TYA PHA ; save Y | {interrupt routine instructions} | PLA TAY ; restore Y PLA TAX ; restore X PLA ; restore A RTI
164
The BRK Instruction:
BRK
165
6502 Programmer’s Model
00000001
NV-BDIZC
A
X
PC
SP
PS
Y
7 0
15 8
FFFF
020001FF
0100
00FF
0000
Microprocessor
Memory
STACK
PAGE ZERO
7 0
166
Checking the B BRK Flag:
PHA save A PHP get flags in A PLA AND #1O test bit 4 (B flag) BNE O81O if set it was BRK interrupt
| else continue with normal IRQ | routine from external input | RTI
167
On Tuesday we did:
Indirect Indirect AddressingAddressing
168
Direct and Indirect addressing:
2340 2341 2342 2343 2344 2345 2346
One of these houses contains the treasure (the data)
If I know the address (2342) then...
169
Direct addressing:
2340 2341 2342 2343 2344 2345 2346
I go directly to address 2342 where the data (the treasure) is stored
Treasure!
170
Direct and Indirect addressing:
2340 2341 2342 2343 2344 2345 2346
This time I don’t know where the treasure is stored,but I do know that I can find the address by goingto another house (2346)
So I go to address 2346 where...
171
Indirect addressing:
2340 2341 2342 2343 2344 2345 2346
I get the data - the address where the treasure is -address 2342.
I can then go to address 2342 and get the data there - the treasure.
2342
172
Indirect addressing:
2340 2341 2342 2343 2344 2345 2346
I go to address 2342 where the data (the treasure)is stored
Treasure!
173
Indirect Indirect Addressing Addressing gives us the gives us the
address of the addressaddress of the address
that we wantthat we want
174
Indirect Indirect
JMP JMP
175
Direct JMP:
JMP O8O7
Indirect JMP:
JMP (O8O7)
176
Indirect JMP:
JMP (O8O7)
results in a jump to location $1234
Memory7 0
0808
0807
12
34
177
Indexed Indexed Indirect Indirect
AddressingAddressing
178
Indexed indirect or pre-indexed indirect addressing:
(IND,X) eg LDA (8O,X) Indirect indexed or post-indexed indirect addressing:
(IND),Y eg LDA (8O),Y Both these addressing modes are only available with zero page addresses for IND
179
Indexed indirect or pre-indexed indirect addressing:
For (IND,X) addressing, the current value of X is added to IND. This gives the address of the address to be used. Indirect indexed or post-indexed indirect addressing: For (IND),Y addressing, IND gives the address of the address to be indexed. This address is then indexed by Y giving the address to be used.
180
(IND,X) example: If: The X register contains 5 Location $85 contains $78 Location $86 contains $56 and the following instruction is executed: LDA (8O,X) then the contents of X is added to $8O giving $85. The address actually used is found in locations $85 and $86 ie $5678. So that using these values, LDA (8O,X) is equivalent to LDA 5678
181
Indexed Indirect Addressing (I,X)
eg if X = 5
LDA (80,X)
result:LDA (85)
LDA 5678
Memory7 0
86
85
56
78
182
(IND),Y example: If: The Y register contains 7 Location $8O contains $34 Location $81 contains $12 and the following instruction is executed: LDA (8O),Y then the address stored in $8O and $81 is to be indexed by Y, equivalent to LDA 1234,Y. Final address=$1234 + Y (7) = $123B. Using these values,
LDA (8O),Y is equivalent to LDA 123B
183
Indexed Indirect Addressing (I),Y
eg if Y = 7
LDA (80),Y
result:LDA 1234,Y
LDA 123B
Memory7 0
81
80
12
34
184
Indexed Addressing:
LDA 0600,X range of X is 00 - FF so
this instruction can address locations 0600 - 06FF
Indexed Indirect Addressing:
LDA (20),Y
Using just Y this instruction can address locations 0600 - 06FF
BUT
7 0
21
20
06
00
185
Indexed Addressing:
LDA 0600,X range of X is 00 - FF so
this instruction can address locations 0600 - 06FF
Indexed Indirect Addressing:
LDA (20),Y
Using just Y this instruction can address locations 0600 - 06FF BUT Location 21 can then beincremented so the instruction will then address locations 0700-07FF.
So we can address as much of memoryas we want to.
7 0
21
20
07
00
186
Input/OutputInput/Output
ProgrammingProgramming
Using the 6522 Using the 6522 VIAVIA
187
The VIA:
188
FFFF
0000
Microprocessor System Memory Map:
7 0
RAM
I/O
ROM
189
FFFF
0000
Microprocessor System Memory Map:
7 0
RAM
I/O
ROM
LDA 0300LDA F000LDA A000
190
The VIA:
VIA
__CS
191
The VIA:
VIA
__CS
A0
A1
A2
A3
192
The VIA:
VIA
__CS
A0
A1
A2
A3
A0 to A3 so 0000-1111
ie 16 locations
193
VIA Registers (AIM addresses):
A000 Port B Data Register (DRB)A001 Port A Data Register (DRA)A002 Port B Data Direction Register (DDRB)A003 Port A Data Direction Register (DDRA)A004 Timer 1 LSBA005 Timer 1 MSBA006 Timer 1A007 Timer 1A008 Timer 2 LSBA009 Timer 2 MSBA00A Shift RegisterA00B Auxiliary Control Register (ACR)A00C Peripheral Control Register (PCR)A00D Interrupt Flag Register (IFR)A00E Interrupt Enable Register (IER)A00F Port A Data Register (No handshake)
194
The VIA:
VIA
PA0PA1PA2
PA3PA4PA5PA6PA7
CA2
CA1
PB0PB1PB2
PB3PB4PB5PB6PB7
CB2
CB1
Port A
Port B
195
Simple use of the VIA:Simple use of the VIA:
LDA #00LDA #00 Set Port A as all inputsSet Port A as all inputs
STA A003STA A003 Data direction register AData direction register ALDA #FFLDA #FF Set Port B as all Set Port B as all
outputsoutputs
STA A002STA A002 Data direction register BData direction register B
LDA A001LDA A001 Read Port ARead Port A
STA A000STA A000 Write to Port BWrite to Port B
JMP JMP
196
The VIA:
VIA
PA0
PB0
5v
0v
Buffer
0v 2
20
10K
197
Using the CB2 Control Line:
PCR7 PCR6 PCR5 Mode: for operation of CB2 control line
0 0 0 Input: IFR3 set by neg edge on CB2, cleared by read or write to DRB
0 0 1 Input: IFR3 set by neg edge on CB2, NOT cleared by read/write to DRB
0 1 0 Input: IFR3 set by pos edge on CB2, cleared by read or write to DRB
0 1 1 Input: IFR3 set by pos edge on CB2, NOT cleared by read/write to DRB
1 0 0 Output: CB2 low on write to DRB, CB2 high by active edge on CB1
1 0 1 Output: CB2 low for one clock cycle following write to DRB
1 1 0 Output: CB2 held low
1 1 1 Output: CB2 held high
198
HandshakingHandshaking: Microprocessor system and a Printer: Microprocessor system and a Printer
VIA
Printer
PB0
PB1
PB2
PB3
PB4
PB5
PB6
PB7
CB2
CB1 READY
STROBE
DO
D1
D2
D3
D4
D5
D6
D7
1. Byte is output from VIA on Port B2. STROBE pulse is output on CB23. Program waits for READY line to go High
199
VIA Timer:VIA Timer:
C3 50 MSB LSB
C350 = 50,000
ie 50ms if system clock is 1 MHz
200
The The EndEnd
!!