microprocessor systems and instrumentation soe2121

200
1 Microprocessor Systems and Instrumentation SOE2121

Upload: duante

Post on 19-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Microprocessor Systems and Instrumentation SOE2121. Microprocessor Systems and Instrumentation SOE2121 Number Systems. Machine code: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Microprocessor Systems and Instrumentation SOE2121

1

Microprocessor Systems and Instrumentation

SOE2121

Page 2: Microprocessor Systems and Instrumentation SOE2121

2

Microprocessor Systems and Instrumentation

SOE2121

Number Systems

Page 3: Microprocessor Systems and Instrumentation SOE2121

3

Machine code:

01010011010100101010010010101001010010010000111100010100110010010100101000100010010101010010010010100101000010101010100101110001011100010010101000101001000100101001010100100101000100010010100010101001001010010100001010101010010111000101110001001010100010100100010010100101010010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100110010010100010001001100100101000100010011001001010001000100110010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100101000101010001111100100101001001010010100001000100010010111

Page 4: Microprocessor Systems and Instrumentation SOE2121

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

Page 5: Microprocessor Systems and Instrumentation SOE2121

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

Page 6: Microprocessor Systems and Instrumentation SOE2121

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

Page 7: Microprocessor Systems and Instrumentation SOE2121

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

Page 8: Microprocessor Systems and Instrumentation SOE2121

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

Page 9: Microprocessor Systems and Instrumentation SOE2121

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

Page 10: Microprocessor Systems and Instrumentation SOE2121

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

Page 11: Microprocessor Systems and Instrumentation SOE2121

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

Page 12: Microprocessor Systems and Instrumentation SOE2121

12

Brain Exercises (Neurobics)

* Binary counting on fingers (and toes after ?)

* Binary Arithmetic - + Addition - Subtraction / Division * Multiplication

* Hexadecimal Arithmetic - + Addition - Subtraction / Division * Multiplication

Page 13: Microprocessor Systems and Instrumentation SOE2121

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

Page 14: Microprocessor Systems and Instrumentation SOE2121

14

Converting between Binary and Hexadecimal

Binary: 0010 1100 32+8+4=44

Hex: 2 C 32+12=44

Page 15: Microprocessor Systems and Instrumentation SOE2121

15

Converting between Binary and Hexadecimal

111O1OO111O1 = 111O 1OO1 11O1 E 9 D 12AB = 1 2 A B OOO1 OO1O 1O1O 1O11

Page 16: Microprocessor Systems and Instrumentation SOE2121

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 +

-----

Page 17: Microprocessor Systems and Instrumentation SOE2121

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

Page 18: Microprocessor Systems and Instrumentation SOE2121

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

Page 19: Microprocessor Systems and Instrumentation SOE2121

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

Page 20: Microprocessor Systems and Instrumentation SOE2121

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)

Page 21: Microprocessor Systems and Instrumentation SOE2121

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

Page 22: Microprocessor Systems and Instrumentation SOE2121

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

Page 23: Microprocessor Systems and Instrumentation SOE2121

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

Page 24: Microprocessor Systems and Instrumentation SOE2121

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)

Page 25: Microprocessor Systems and Instrumentation SOE2121

25

Binary Coded Decimal:

Range of BCD numbers in one byte 00 - 99

Reading a BCD number in hex gives the decimal value

Page 26: Microprocessor Systems and Instrumentation SOE2121

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

Page 27: Microprocessor Systems and Instrumentation SOE2121

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

Page 28: Microprocessor Systems and Instrumentation SOE2121

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

Page 29: Microprocessor Systems and Instrumentation SOE2121

29

The Byte

7 6 5 4 3 2 1 0

Most Significant Bit Least Significant Bit

Page 30: Microprocessor Systems and Instrumentation SOE2121

30

Which is Most Significant?

A typical lecturer’s salary might be:

£93,878

Most Significant Digit Least Significant Digit

Page 31: Microprocessor Systems and Instrumentation SOE2121

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

Page 32: Microprocessor Systems and Instrumentation SOE2121

32

Brain Exercises (Neurobics)

* Binary counting on fingers (and toes after ?)

* Binary Arithmetic - + Addition - Subtraction / Division * Multiplication

* Hexadecimal Arithmetic - + Addition - Subtraction / Division * Multiplication

Page 33: Microprocessor Systems and Instrumentation SOE2121

33

IntroductionIntroduction

Load and StoreLoad and Store

TransferTransfer

Page 34: Microprocessor Systems and Instrumentation SOE2121

34

Not Recommended Book:

65O2 Assembly Language Programming

by L A Leventhal

Pub: Osborne/McGraw-Hill

Page 35: Microprocessor Systems and Instrumentation SOE2121

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

Page 36: Microprocessor Systems and Instrumentation SOE2121

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.

Page 37: Microprocessor Systems and Instrumentation SOE2121

37

Microprocessor Systems and Instrumentation

SOE2121

Number Systems

Page 38: Microprocessor Systems and Instrumentation SOE2121

38

Machine code:

01010011010100101010010010101001010010010000111100010100110010010100101000100010010101010010010010100101000010101010100101110001011100010010101000101001000100101001010100100101000100010010100010101001001010010100001010101010010111000101110001001010100010100100010010100101010010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100110010010100010001001100100101000100010011001001010001000100110010010100010001001010001010100100101001010000101010101001011100010111000100101010001010010001001010010101001001010001000100101000101010001111100100101001001010010100001000100010010111

Page 39: Microprocessor Systems and Instrumentation SOE2121

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

Page 40: Microprocessor Systems and Instrumentation SOE2121

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

Page 41: Microprocessor Systems and Instrumentation SOE2121

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

Page 42: Microprocessor Systems and Instrumentation SOE2121

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

Page 43: Microprocessor Systems and Instrumentation SOE2121

43

The Byte

7 6 5 4 3 2 1 0

Most Significant Bit Least Significant Bit

Page 44: Microprocessor Systems and Instrumentation SOE2121

44

Which is Most Significant?

A typical lecturer’s salary might be:

£93,878

Most Significant Digit Least Significant Digit

Page 45: Microprocessor Systems and Instrumentation SOE2121

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

Page 46: Microprocessor Systems and Instrumentation SOE2121

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

Page 47: Microprocessor Systems and Instrumentation SOE2121

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

Page 48: Microprocessor Systems and Instrumentation SOE2121

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

Page 49: Microprocessor Systems and Instrumentation SOE2121

49

Instruction Formats

One byte instructions:

Two byte instructions:

Three byte instructions:

OPCODE

OPCODE

OPCODE

OPERAND

OPER AND

Page 50: Microprocessor Systems and Instrumentation SOE2121

50

Load and Store Instructions

Page 51: Microprocessor Systems and Instrumentation SOE2121

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

Page 52: Microprocessor Systems and Instrumentation SOE2121

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

Page 53: Microprocessor Systems and Instrumentation SOE2121

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

Page 54: Microprocessor Systems and Instrumentation SOE2121

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

Page 55: Microprocessor Systems and Instrumentation SOE2121

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

Page 56: Microprocessor Systems and Instrumentation SOE2121

56

Immediate Addressing

Instruction Machine code: LDA #AA A9 AA LDX #FF A2 FF LDY #O1 AO O1

Page 57: Microprocessor Systems and Instrumentation SOE2121

57

Don’t mix these up!

Zero Page: LDA OO Immediate: LDA #OO

Page 58: Microprocessor Systems and Instrumentation SOE2121

58

Transfer Instructions

Page 59: Microprocessor Systems and Instrumentation SOE2121

59

Transfer instructions:

TAX Copy register A to register X The old value in X is lost Register A is not affected

Page 60: Microprocessor Systems and Instrumentation SOE2121

60

Transfer instructions:

TAX TAY TXA TYA

(TXS TSX)

All 1 byte Long Implied Addressing 2 Clock Cycles Flags as for LDA

Page 61: Microprocessor Systems and Instrumentation SOE2121

61

ArithmeticInstructions

ADC SBC

Page 62: Microprocessor Systems and Instrumentation SOE2121

62

Negative Numbers

Page 63: Microprocessor Systems and Instrumentation SOE2121

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)

Page 64: Microprocessor Systems and Instrumentation SOE2121

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

Page 65: Microprocessor Systems and Instrumentation SOE2121

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

Page 66: Microprocessor Systems and Instrumentation SOE2121

66

ADC

Page 67: Microprocessor Systems and Instrumentation SOE2121

67

Addition:

CLC

LDA 8O ADC 81 STA 82

Page 68: Microprocessor Systems and Instrumentation SOE2121

68

Two byte arithmetic:

How do we do it in decimal?

3625 +

---61

Page 69: Microprocessor Systems and Instrumentation SOE2121

69

Addition and Subtraction:

CLC SEC LDA 8O LDA 8O ADC 81 SBC 81 STA 82 STA 82

Page 70: Microprocessor Systems and Instrumentation SOE2121

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:

Page 71: Microprocessor Systems and Instrumentation SOE2121

71

Decimal Arithmetic

Page 72: Microprocessor Systems and Instrumentation SOE2121

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

Page 73: Microprocessor Systems and Instrumentation SOE2121

73

Decimal Arithmetic (BCD):

SED SED CLC SEC LDA 8O LDA 8O ADC 81 SBC 81 STA 82 STA 82 CLD CLD

Addition: Subtraction:

Page 74: Microprocessor Systems and Instrumentation SOE2121

74

Logical Logical InstructionsInstructions

Page 75: Microprocessor Systems and Instrumentation SOE2121

75

Logical Operations:

Page 76: Microprocessor Systems and Instrumentation SOE2121

76

Logical Operations:

And: AND

Page 77: Microprocessor Systems and Instrumentation SOE2121

77

Logical Operations:

And: AND

Or: ORA

Page 78: Microprocessor Systems and Instrumentation SOE2121

78

Logical Operations:

And: AND

Or: ORA

Exclusive Or: EOR

Page 79: Microprocessor Systems and Instrumentation SOE2121

79

AND

Page 80: Microprocessor Systems and Instrumentation SOE2121

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

Page 81: Microprocessor Systems and Instrumentation SOE2121

81

OR

Page 82: Microprocessor Systems and Instrumentation SOE2121

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

Page 83: Microprocessor Systems and Instrumentation SOE2121

83

EOR

Page 84: Microprocessor Systems and Instrumentation SOE2121

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

Page 85: Microprocessor Systems and Instrumentation SOE2121

85

Testing Testing individual bits individual bits

in a bytein a byte

Page 86: Microprocessor Systems and Instrumentation SOE2121

86

Testing Individual Bits in a Byte: To test bit 2 of location $8O LDA 8O byte to test AND #O4 mask OOOO O1OO

Page 87: Microprocessor Systems and Instrumentation SOE2121

87

The Byte

7 6 5 4 3 2 1 0

0 0 0 0 0 1 0 0

Hex 04

?

Page 88: Microprocessor Systems and Instrumentation SOE2121

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

Page 89: Microprocessor Systems and Instrumentation SOE2121

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

Page 90: Microprocessor Systems and Instrumentation SOE2121

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

Page 91: Microprocessor Systems and Instrumentation SOE2121

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

Page 92: Microprocessor Systems and Instrumentation SOE2121

92

Logical instructions - Example:

LDA #33 Result is: OO11 OO11 33 AND #OF ORA #9O EOR #8O EOR #FF

Page 93: Microprocessor Systems and Instrumentation SOE2121

93

Logical instructions - Example:

LDA #33 Result is: OO11 OO11 33 AND #OF Result is: OOOO OO11 O3 ORA #9O EOR #8O EOR #FF

Page 94: Microprocessor Systems and Instrumentation SOE2121

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

Page 95: Microprocessor Systems and Instrumentation SOE2121

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

Page 96: Microprocessor Systems and Instrumentation SOE2121

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

Page 97: Microprocessor Systems and Instrumentation SOE2121

97

Shift and Rotate

Instructions

Page 98: Microprocessor Systems and Instrumentation SOE2121

98

Shift and Rotate Instructions:

ASL

Page 99: Microprocessor Systems and Instrumentation SOE2121

99

Shift and Rotate Instructions:

ASL LSR

Page 100: Microprocessor Systems and Instrumentation SOE2121

100

Shift and Rotate Instructions:

ASL LSR ROL

Page 101: Microprocessor Systems and Instrumentation SOE2121

101

Shift and Rotate Instructions:

ASL LSR ROL ROR

Page 102: Microprocessor Systems and Instrumentation SOE2121

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

Page 103: Microprocessor Systems and Instrumentation SOE2121

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

Page 104: Microprocessor Systems and Instrumentation SOE2121

104

The ASL instruction:

C 7 0

0 01000000

Page 105: Microprocessor Systems and Instrumentation SOE2121

105

The ASL instruction:

C 7 0

0 00100000

Page 106: Microprocessor Systems and Instrumentation SOE2121

106

The ASL instruction:

C 7 0

0 00010000

Page 107: Microprocessor Systems and Instrumentation SOE2121

107

The ASL instruction:

C 7 0

0 00001000

Page 108: Microprocessor Systems and Instrumentation SOE2121

108

The ASL instruction:

C 7 0

0 00000100

Page 109: Microprocessor Systems and Instrumentation SOE2121

109

The ASL instruction:

C 7 0

0 00000010

Page 110: Microprocessor Systems and Instrumentation SOE2121

110

The ASL instruction:

C 7 0

0 00000001

Page 111: Microprocessor Systems and Instrumentation SOE2121

111

The ASL instruction:

C 7 0

1 00000000

Page 112: Microprocessor Systems and Instrumentation SOE2121

112

The ASL instruction:

C 7 0

0 00000000

Page 113: Microprocessor Systems and Instrumentation SOE2121

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

Page 114: Microprocessor Systems and Instrumentation SOE2121

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

Page 115: Microprocessor Systems and Instrumentation SOE2121

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

Page 116: Microprocessor Systems and Instrumentation SOE2121

116

Jumping and Branching and

Comparing

Page 117: Microprocessor Systems and Instrumentation SOE2121

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

Page 118: Microprocessor Systems and Instrumentation SOE2121

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)

Page 119: Microprocessor Systems and Instrumentation SOE2121

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

Page 120: Microprocessor Systems and Instrumentation SOE2121

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

Page 121: Microprocessor Systems and Instrumentation SOE2121

121

ComparingCMP CPX CPY

Page 122: Microprocessor Systems and Instrumentation SOE2121

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)

Page 123: Microprocessor Systems and Instrumentation SOE2121

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

Page 124: Microprocessor Systems and Instrumentation SOE2121

124

Loops and Indexed

addressing

Page 125: Microprocessor Systems and Instrumentation SOE2121

125

Incrementing and

Decrementing

Page 126: Microprocessor Systems and Instrumentation SOE2121

126

Incrementing and Decrementing: Memory Locations: INC 8O DEC 2F INC O412 DEC 12BA Registers: INX DEX INY DEY

Page 127: Microprocessor Systems and Instrumentation SOE2121

127

Car Mileometer

What happens if you drive forward one more mile?

9 9 9 9 9

Page 128: Microprocessor Systems and Instrumentation SOE2121

128

Car Mileometer

What happens if you reverse one mile?

0 0 0 0 0

Page 129: Microprocessor Systems and Instrumentation SOE2121

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

Page 130: Microprocessor Systems and Instrumentation SOE2121

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

Page 131: Microprocessor Systems and Instrumentation SOE2121

131

Looping

Page 132: Microprocessor Systems and Instrumentation SOE2121

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

Page 133: Microprocessor Systems and Instrumentation SOE2121

133

Indexed Addressin

g

Page 134: Microprocessor Systems and Instrumentation SOE2121

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

Page 135: Microprocessor Systems and Instrumentation SOE2121

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

Page 136: Microprocessor Systems and Instrumentation SOE2121

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

Page 137: Microprocessor Systems and Instrumentation SOE2121

137

The NOP Instruction

NOP does nothing 1 byte 2 clock cycles

Page 138: Microprocessor Systems and Instrumentation SOE2121

138

Subroutines and the

Stack

Page 139: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 140: Microprocessor Systems and Instrumentation SOE2121

140

Subroutines:

JSR 0500

RTS

Page 141: Microprocessor Systems and Instrumentation SOE2121

141

The Stack:

SP 01 FF

A1 01FF

35 01FE

29 01FD

81 01FC

12 01FB

7B 01FA

5D 01F9

0100

|

Page 142: Microprocessor Systems and Instrumentation SOE2121

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

Page 143: Microprocessor Systems and Instrumentation SOE2121

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

*

Page 144: Microprocessor Systems and Instrumentation SOE2121

144

The Stack:

SP 01 FF

A1 01FF

35 01FE

29 01FD

81 01FC

12 01FB

7B 01FA

5D 01F9

0100

|

Page 145: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 146: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 147: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 148: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 149: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 150: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 151: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 152: Microprocessor Systems and Instrumentation SOE2121

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

|

Page 153: Microprocessor Systems and Instrumentation SOE2121

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

Page 154: Microprocessor Systems and Instrumentation SOE2121

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

Page 155: Microprocessor Systems and Instrumentation SOE2121

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

Page 156: Microprocessor Systems and Instrumentation SOE2121

156

InterruptsInterrupts

Page 157: Microprocessor Systems and Instrumentation SOE2121

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.

Page 158: Microprocessor Systems and Instrumentation SOE2121

158

6502 Interrupt inputs:

___IRQ

___NMI

___RES

6502 microprocessor

5v

0v

10K

Page 159: Microprocessor Systems and Instrumentation SOE2121

159

6502 Vectors:

___ NMI $FFFA + $FFFB ___ RES $FFFC + $FFFD ___ IRQ $FFFE + $FFFF

Page 160: Microprocessor Systems and Instrumentation SOE2121

160

Vectors:

FFFFFFFEFFFDFFFCFFFBFFFA

Memory7 0

IRQ LSB

IRQ MSB

RES MSB

RES LSB

NMI MSB

NMI LSB

Page 161: Microprocessor Systems and Instrumentation SOE2121

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

Page 162: Microprocessor Systems and Instrumentation SOE2121

162

The Interrupt Flag I:

CLI set I = O - Enable IRQ interrupts

SEI set I = 1 - Disable IRQ interrupts

Page 163: Microprocessor Systems and Instrumentation SOE2121

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

Page 164: Microprocessor Systems and Instrumentation SOE2121

164

The BRK Instruction:

BRK

Page 165: Microprocessor Systems and Instrumentation SOE2121

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

Page 166: Microprocessor Systems and Instrumentation SOE2121

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

Page 167: Microprocessor Systems and Instrumentation SOE2121

167

On Tuesday we did:

Indirect Indirect AddressingAddressing

Page 168: Microprocessor Systems and Instrumentation SOE2121

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

Page 169: Microprocessor Systems and Instrumentation SOE2121

169

Direct addressing:

2340 2341 2342 2343 2344 2345 2346

I go directly to address 2342 where the data (the treasure) is stored

Treasure!

Page 170: Microprocessor Systems and Instrumentation SOE2121

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

Page 171: Microprocessor Systems and Instrumentation SOE2121

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

Page 172: Microprocessor Systems and Instrumentation SOE2121

172

Indirect addressing:

2340 2341 2342 2343 2344 2345 2346

I go to address 2342 where the data (the treasure)is stored

Treasure!

Page 173: Microprocessor Systems and Instrumentation SOE2121

173

Indirect Indirect Addressing Addressing gives us the gives us the

address of the addressaddress of the address

that we wantthat we want

Page 174: Microprocessor Systems and Instrumentation SOE2121

174

Indirect Indirect

JMP JMP

Page 175: Microprocessor Systems and Instrumentation SOE2121

175

Direct JMP:

JMP O8O7

Indirect JMP:

JMP (O8O7)

Page 176: Microprocessor Systems and Instrumentation SOE2121

176

Indirect JMP:

JMP (O8O7)

results in a jump to location $1234

Memory7 0

0808

0807

12

34

Page 177: Microprocessor Systems and Instrumentation SOE2121

177

Indexed Indexed Indirect Indirect

AddressingAddressing

Page 178: Microprocessor Systems and Instrumentation SOE2121

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

Page 179: Microprocessor Systems and Instrumentation SOE2121

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.

Page 180: Microprocessor Systems and Instrumentation SOE2121

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

Page 181: Microprocessor Systems and Instrumentation SOE2121

181

Indexed Indirect Addressing (I,X)

eg if X = 5

LDA (80,X)

result:LDA (85)

LDA 5678

Memory7 0

86

85

56

78

Page 182: Microprocessor Systems and Instrumentation SOE2121

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

Page 183: Microprocessor Systems and Instrumentation SOE2121

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

Page 184: Microprocessor Systems and Instrumentation SOE2121

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

Page 185: Microprocessor Systems and Instrumentation SOE2121

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

Page 186: Microprocessor Systems and Instrumentation SOE2121

186

Input/OutputInput/Output

ProgrammingProgramming

Using the 6522 Using the 6522 VIAVIA

Page 187: Microprocessor Systems and Instrumentation SOE2121

187

The VIA:

Page 188: Microprocessor Systems and Instrumentation SOE2121

188

FFFF

0000

Microprocessor System Memory Map:

7 0

RAM

I/O

ROM

Page 189: Microprocessor Systems and Instrumentation SOE2121

189

FFFF

0000

Microprocessor System Memory Map:

7 0

RAM

I/O

ROM

LDA 0300LDA F000LDA A000

Page 190: Microprocessor Systems and Instrumentation SOE2121

190

The VIA:

VIA

__CS

Page 191: Microprocessor Systems and Instrumentation SOE2121

191

The VIA:

VIA

__CS

A0

A1

A2

A3

Page 192: Microprocessor Systems and Instrumentation SOE2121

192

The VIA:

VIA

__CS

A0

A1

A2

A3

A0 to A3 so 0000-1111

ie 16 locations

Page 193: Microprocessor Systems and Instrumentation SOE2121

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)

Page 194: Microprocessor Systems and Instrumentation SOE2121

194

The VIA:

VIA

PA0PA1PA2

PA3PA4PA5PA6PA7

CA2

CA1

PB0PB1PB2

PB3PB4PB5PB6PB7

CB2

CB1

Port A

Port B

Page 195: Microprocessor Systems and Instrumentation SOE2121

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

Page 196: Microprocessor Systems and Instrumentation SOE2121

196

The VIA:

VIA

PA0

PB0

5v

0v

Buffer

0v 2

20

10K

Page 197: Microprocessor Systems and Instrumentation SOE2121

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

Page 198: Microprocessor Systems and Instrumentation SOE2121

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

Page 199: Microprocessor Systems and Instrumentation SOE2121

199

VIA Timer:VIA Timer:

C3 50 MSB LSB

C350 = 50,000

ie 50ms if system clock is 1 MHz

Page 200: Microprocessor Systems and Instrumentation SOE2121

200

The The EndEnd

!!