Chapter 1
Chapter 1
and Microcontrollers
Ali Parsayan
[email protected]
1
Chapter 1
Z80
Microprocessor
2
Outline
8008 8088/6
4004 80386 80860
intel 8080 80186
4040 80486 pentium
8085 80286
Z8000
zilog Z80 Z8001
Z8002
6800 68006 68020
Motorola 6802 68008 68030
6809 68010 68040
Z80 Pins Assignment
27 30
M1 - A0
31
A1
19 32
MREQ - A2
20 33
System IORQ -
21 34
A3
Control Lines RD - A4
22 35
WR - A5
36
A6
28 37
RFSH - A7
38
A8 Address Bus
18 39
HALT - A9
40
A10
WAIT -
24
Z - 80 CPU 1
2
A11
CPU 16 3
A12
Control Lines INT - A13
17 4
NMI - A14
5
A15
26
RESET -
14
D0
25 15
Bus BUSRQ - D1
23 12
Control Lines BUSAK - D2
8
D3
7
D4 Data Bus
6 9
D5
11 10
+ 5V D6
29 13
GND D7
5
Z80 Pins Assignment
6
Z80 Microprocessor
• An Osborne Executive
portable computer, from 1982,
with:
o a Zilog Z80 4 MHz CPU,
o and a 2007 Apple iPhone
with a 412 MHz ARM11
CPU;
• The Executive has 100 times
the weight, almost 500 times
the volume, approximately 10
times the inflation adjusted
cost, and 1/1000 frequency of
the smartphone.
Z80 Pin Description
• A15-A0:
o Address bus (output, active high, 3-state).
o Used for accessing the memory and I/O ports.
• D7-D0:
o Data Bus (input/output, active high, 3-state).
o Used for data exchanges with memory, I/O and
interrupts.
• RD:
o Read (output, active Low, 3-state).
o Indicates that the CPU wants to read data from
memory or I/O.
• WR:
o Write (output, active Low, 3-state).
o Indicates that the CPU data bus holds valid
data to be stored at the addressed memory or
I/O location. 8
Z80 Pin Description
• MREQ:
o Memory Request (output, active Low, 3-state).
o Indicates memory read/write operation.
• IORQ:
o Input/Output Request (output, active Low,3-
state).
o Indicates I/O read/write operation.
• M1:
o Machine Cycle One (output, active Low).
o Together with MREQ indicates opcode fetch
cycle, and together with IORQ indicates an
Interrupt Ack cycle.
• RFSH:
o Refresh (output, active Low).
o Together with MREQ indicates refresh cycle and
Lower 7-bits address is refresh address to DRAM. 9
Z80 Pin Description
• INT:
o Interrupt Request (input, active Low).
o It is generated by I/O devices.
o Checked at the end of the current
instruction if IFF flag is enabled.
• NMI:
o Non-Maskable Interrupt (Input, negative
edge-triggered).
o Higher priority than INT and Recognized
at the end of the current instruction.
o Independent of the status of IFF.
o Forces the CPU to restart at location
0066H. 10
Z80 Pin Description
• BUSREQ:
o Bus Request (input, active Low).
o Higher priority than NMI.
o Recognized at the end of the current machine
cycle.
o Forces the CPU address bus, data bus, and
MREQ, IORQ, RD, and WR to high-imp.
• BUSACK:
o Bus Acknowledge (output, active Low).
o Indicates to the requesting device that address,
data, and control signals MREQ, IORQ, RD,
and WR have entered their high-impedance
states.
11
Z80 Pin Description
• RESET:
o Reset (input, active Low).
o RESET initializes the CPU as follows:
➢ Resets the IFF
➢ Clears the PC and registers I and R.
➢ Sets the interrupt status to Mode 0.
➢ During reset time, the address and
data bus go to a high-impedance state
➢ And all control output signals go to the
inactive state.
➢ Must be active for a minimum of three
full clock cycles before the reset
operation is complete.
12
Z80 Structure B
8 U
INTERNAL DATA BUS (8 BIT) F
F
E
R
DATA BUS
MUX MUX
A F
INSTRUCTION W'
TMP
Z' W Z
REGISTER
I R A' F'
B' C' B C
D' E' D E
H' L' H L ACT
DECODER
IX
IY
SP
ALU
CONTROLLER
SEQUENCER
PC
±k ±k
CONTROL
SECTION B ADDRESS BUS
16 U
INTERNAL ADDRESS BUS (16 BIT)
F
F
E
R
B CONTROL BUS
13 U
INTERNAL CONTROL BUS F
F
E
R 13
Z80 Programming Model
14
Register Set
• A: Accumulator Register
• F: Flag register
• Two sets of six general-purpose registers
o may be used individually as 8-bit A, F, B, C, D, E, H, and L (A’,
F’, B’, C’, D’, E’, H’, and L’)
o or in pairs as 16-bit registers AF, BC, DE, and HL (AF’, BC’,
DE’, and HL’)
• The Alternative registers (A’, F’, B’, C’, D’, E’, H’, and
L’) not visible to the programmer but can access via:
o EXX (BC)<->(BC') , (DE)<->(DE') , (HL)<->(HL’)
o EX AF, AF ’ (AF)<->(AF’)
o What is this instruction useful for? 15
Register Set
P
S Z X H X V N C
• S: Sign Flag (1: Negativ)
• Z: Zero Flag (1: Zero)
• H: Half Carry Flag (1: Carry from Bit 3 to Bit 4)**
• P: Parity Flag (1: Even)
• V: Overflow Flag (1: Overflow)
o 2-complement number representation
• N: Operation Flag (1: Previous Operation was subtraction)
o used in DAA-operation for BCD-arithmetic
• C: Carry Flag (1: Carry from Bit n-1 to Bit n, with n length of
operand)
DAA - Decimal Adjust Accumulator
• Adjusts the content of the Accumulator A for BCD addition and subtraction
operations such as ADD, ADC, SUB, SBC, and NEG according to the table:
before DAA after DAA
Op N C Bits 4-7 H Bits 0-3 A=A+.. C
0 0 0-9 0 0-9 00 0
0 0 0-8 0 A-F 06 0
0 0 0-9 1 0-3 06 0
0 0 A-F 0 0-9 60 1
ADD
0 0 9-F 0 A-F 66 1
ADC
0 0 A-F 1 0-3 66 1
0 1 0-2 0 0-9 60 1
0 1 0-2 0 A-F 66 1
0 1 0-3 1 0-3 66 1
1 0 0-9 0 0-9 00 0
SUB
1 0 0-8 1 6-F FA 0
SBC
1 1 7-F 0 0-9 A0 1
NEG 1 1 6-F 1 6-F 9A 1
Instruction Cycles,
Machine Cycles and “T-states”
• T-state = 1/f (f: Z80 Clock Frequency)
o f = 4 MHZ → T-state = 0.25 us
• Machine cycle is defined as the time required to
complete one operation of accessing memory,
accessing IO, etc.
• Instruction cycle is the time taken to complete the
execution of an instruction
19
Basic CPU Timing Example
20
Opcode Fetch Bus Timings (M1 Cycle)
21
M1 Refresh Cycle
• Takes 4T to 6Ts
• Z80 includes built in circuitry for refreshing
DRAM
• This simplifies the external interfacing hardware
• DRAM consists of MOS transistors, which store
Information as capacitive charges; each cell
needs to be periodically refreshed
• During T3 and T4 (when Z80 is performing
internal ops), the low order address is used to
supply a 7-bit address for refresh 22
The R register
24
Adding One Wait State to an M1 Cycle
25
Adding One Wait State
to Any Memory Cycle
26
Memory Read/Write Cycle
27
IO Read/Write Cycle
29
Interrupts
• Mode 0:
o An 8 bit opcode is Fetched from Data BUS and executed
o The source interrupt device must put 8 bit opcode at data bus
o 8 bit opcode usually is RST p instructions
• Mode 1:
o A jump is made to address 0038h
o No value is required at data bus
• Mode 2:
o A jump is made to address (register I × 256 + value from interrupting device that
puts at bus)
o I is high 8 bit of interrupt vector
o Value is low 8 bit of interrupt vector
31
Interrupt Modes
32
Interrupt Request/Acknowledge Cycle
34
Minimal Configuration of a
Z80 Microcomputer
Clock Memory Power
Generator (ROM, RAM) Supply
Address Bus
Input Out
Z - 80 CPU Data Bus Output
In
Control Bus (I/O)
35
Z80 Memory Connection
D7~D0 D7~D0
RAM
64 kb
A15~A0 A15~A0
Z80 RD WR CS
RD
WR
MREQ
Z80 Memory Connection
A15 MREQ
Z80 Memory Connection
Z80 OE CS RD WR CS
RD
WR
A15 MREQ
Z80 Memory Connection
Z80 OE CS RD WR CS RD WR CS RD WR CS
RD
WR
En
A14
A15
MREQ
S0
S1
Address Bit Map
Selects chip Selects location within chips
Z80 16k
OE CS RD WR CS RD WR CS RD WR CS
BFFFh
RD
C000h
RAM3
WR
En
A14
A15
MREQ
S0
16k
S1
FFFFh
Memory Map
0000h
• Empty Area can't write and read ROM
• rd op. returns FFh value (usually) 3FFFh
4000h
• wr op. can't store any value on it Empty
7FFFh
D7~D0 D7~D0 D7~D0 D7~D0
8000h
RAM2
ROM RAM RAM
16 kb 16 kb 16 kb
A13~A0 A13~A0 A13~A0 A13~A0
Z80 OE CS RD WR CS RD WR CS
BFFFh
RD
WR
C000h
En RAM3
A14
A15
MREQ
S0
S1
FFFFh
Memory Map
0000h
• Empty Area can't write and read ROM
• rd op. returns FFh value (usually) 3FFFh
4000h
• wr op. can't store any value on it Empty
7FFFh
D7~D0 D7~D0 D7~D0
8000h
ROM
RAM
RAM
16 kb 16 kb
A13~A0 A13~A0 A13~A0
Z80 OE CS RD WR CS
BFFFh
RD
WR
C000h
En Empty
A14
A15
MREQ
S0
S1
FFFFh
Full and Partial Decoding
• Partial Decoding
o When some of the address lines are connected the
memory/device to perform selection
o Using this type of decoding results into roll-over addresses
(fold back or shading).
o Roll-over address: any memory location has more than one
address 47
Partial Decoding
A15~A12 X RD WR CS
Z80
RD
CPU WR
MREQ 48
Partial Decoding 0000h
RAM
0FFFh
1000h
RAM’
1FFFh
o Every memory location has more than one address
2000h
o For example first RAM location has addresses: RAM’
2FFFh
o 0000h 3000h
RAM’
3FFFh
o 1000h
o 2000h Roll-over Address
o 3000h F000h
RAM’
o …………….
FFFFh
o …………….
RAM
AAAA AAAA AAAA AAAA 4 kb
A15 to A0 Memory A11~A0 A11~A0
1111 1198 7654 3210
(HEX) Chip A15~A12 X RD WR CS
5432 10
RAM CPU WR
A13 X OE CS RD WR CS
Z80
RD
CPU
WR
A15
A14
MREQ
50
Partial Decoding
• 8 roll-over address for ROM
• 4 roll-over address for RAM
MREQ
X0x1 1111 1111 1111
51
0000h 0000h
ROM
0FFFh
RAM’
1000h
ROM’
Partial Decoding Conflict 1FFFh
2000h
1FFFh
2000h
2FFFh
ROM’
RAM’
3000h
ROM’
D7~D0 D7~D0 D7~D0 3FFFh 3FFFh
ROM
4 kb
RAM
8 kb
4000h 4000h
ROM’
4FFFh
A12~A0 A11~A0 A12~A0
5000h
ROM’
OE CS RD WR CS 5FFFh 5FFFh
Z80 A13
X
CPU 6000h 6000h
ROM’
RD 6FFFh
WR
7000h
ROM’
7FFFh 7FFFh
A15
A14
MREQ
8000h F000h
RAM
9FFFh
AAAA AAAA AAAA AAAA A000h
Memory
1111 1198 7654 3210
Chip RAM’
5432 10
BFFFh
ROM
0xxx 1111 1111 1111
DFFFh
X0x0 0000 0000 0000 8k
E000h
RAM
X0x1 1111 1111 1111 FFFFh
FFFFh
0000h 0000h
ROM
0FFFh
1000h
ROM’
Partial Decoding 1FFFh
2000h
1FFFh
2000h
2FFFh
ROM’
3000h
ROM’
D7~D0 D7~D0 D7~D0 3FFFh 3FFFh
ROM
4 kb
RAM
8 kb
4000h 4000h
ROM’
4FFFh
A12~A0 A11~A0 A12~A0 RAM’
5000h
ROM’
Z80
CPU
A13
X
OE CS RD WR CS 5FFFh
6000h
Conflict
5FFFh
6000h
ROM’
RD 6FFFh
WR RAM’
7000h
ROM’
7FFFh 7FFFh
A15
A14
MREQ
8000h F000h
9FFFh
AAAA AAAA AAAA AAAA A000h
Memory
1111 1198 7654 3210
Chip
5432 10
BFFFh
ROM RAM
0xxx 1111 1111 1111
DFFFh
X1x0 0000 0000 0000 8k
E000h
RAM
X1x1 1111 1111 1111 RAM’
FFFFh
FFFFh
Full (exhaustive) Decoding
AAAA AAAA AAAA AAAA
Memory
1111 1198 7654 3210
Chip
5432 10
A12~A0 A12~A0
0000 0000 0000 0000 D7~D0
ROM 2764
0001 1111 1111 1111 EPROM
0010 0000 0000 0000 8k8
RAM
0010 0111 1111 1111 OE CE
D7~D0
RD
A13 C 0000h-07FFh
Y0
A12 B Y1 0800h-0FFFh
A11 A Y2 1000h-17FFh 7421
1800h-1FFFh
A10~A0 A10~A0
74138 Y3 D7~D0
2000h-27FFh 6116
Y4 RAM
A15 G2A Y5 2k8
A14 G2B Y6 RD WR CS
MREQ G1 Y7
RD WR
Partial Decoding
AAAA AAAA AAAA AAAA
Memory
1111 1198 7654 3210
Chip
5432 10
A12~A0 A12~A0
0000 0000 0000 0000 D7~D0
ROM 2764
0001 1111 1111 1111 EPROM
001x x000 0000 0000 8k8
RAM
001x x111 1111 1111 OE CE
D7~D0
RD
A15 C 0000h-1FFFh
Y0
A14 B Y1 2000h-3FFFh
A13 A Y2
A10~A0 A10~A0
74138 Y3 D7~D0
6116
Y4 RWM
MREQ G2A Y5 2k8
GND G2B Y6 RD WR CS
VCC G1 Y7
RD WR
1 Bit Memory With Separated I/O
D7-D0
D7 D1 D0
WR / RD CS WR / RD CS WR / RD CS
WR / RD
CS
56
What is The Memory (addr. bit) Map
A12~A0
D7~D0
2764
EPROM
8k8
OE CE
RD
A15 C 0000h-1FFFh
Y0
A14 B 2000h-3FFFh D7-D0
Y1 D0
D7 D1
A13 A Y2
VCC G1 Y7
WR WR
RD
Adding RAM & ROM
58
Minimum Z80 Computer System
59
Z80-µP-Family (Typical Environment)
PIO DMA
+5V
60
Z80 Input/Output
• Z80 at most could have 256 input port and 256 output
• 8 bit port address is placed on A7–A0 pin to select the I/O device
• OUT (n), A
o n is 8 bit port address
o Content of A is data
• OUT (C), r
o Content of C is a port address
o r is a data register
• IN A, (n)
o n is 8 bit port address
o Data is transferred to A
• IN r (C)
o Content of Reg C is a port address
o Input data is transferred to r (data reg) 61
Remember IO Read/Write Cycle
62
Z80 and Simple Output Port
A15
A14 OUT (03), A
:
A0
D7 D0 Q0
D6 D1 Q1
D5 D2 Q2
Z80 D4 D3 Q3
D3 D4 Q4
74LS373
D2 D5 Q5
D1 D6 Q6
D0 D7 Q7
LE OE
IORQ
WR
AAAAAAAA
IOWR 76543210
63
Z80 and Simple Output Port (74LS373)
64
Z80 and Simple Input Port
A15 5V
A14 IN A, (02)
:
A0
D7 Y0 A0
D6 Y1 A1
D5 Y2 A2
Z80 D4 Y3 A3
D3 Y4 74LS244 A4
D2 Y5 A5
D1 Y6 A6
D0 Y7 A7
G1 G2
IORQ
RD
AAAAAAAA
IORD 76543210
65
Z80 and Simple Input Port (74LS244)
66
Z80 Instructions Description
• Immediate
• Immediate Extended
• Modified Page Zero Addressing (rst p)
• Relative Addressing
o Jump Relative (2 byte)
➢ One Byte Op Code
➢ 8-Bit Two’s Complement Displacement (A+2)
• Extended Addressing
o Absolute jump
➢ One byte opcode
➢ 2 byte address
• Indexed Addressing
o (Index Register + Displacement) (IX+d)
o 2 byte opcode
o 1 byte displacement 68
Addressing Modes
• Register Addressing
o LD C, B
• Implied Addressing
o Op Code implies other operand(s)
o ADD E
• Register Indirect Addressing
o 16-bit CPU register pair as pointer (such as HL)
o ADD (HL)
• Bit Addressing
o Set, Reset, and test instructions.
o SET 3, A
o RES 7, B
69
Z80 Instructions
• Logical Instructions
o AND r, s
o OR r, s
o CPL r
o XOR r, s
o AND r, n
o AND r, (nn)
o AND r, (ss)
o AND r, (IX+n)
74
Z80 Instructions
• Jump Instructions
o JP z, Address
o JP nz, Address
o JP c, Address
o JP nc, Address
o …………………
o …………………
o …………………
o JR nn, Address (jump to PC + nn)
o DJNZ Address (B=B-1 if B=0 then jump)
75
Z80 Instructions
• Shift and Rotate Instructions
o RLCA rotate all ACC bits and put MSB in LSB and Carry
o RRCA rotate all ACC bits and put LSB in MSB and Carry
o RLA rotate all ACC bits and Carry
o RLA rotate all ACC bits and Carry
o RLC
o RRC
o ………..
o SLA shift all ACC bits and put ‘0’ in LSB and put MSB in
Carry
o SRA
o ………...
o …………
76
Z80 Instructions
77
Z80 Instructions
• I/O Instructions
o IN A, (nn)
o OUT (nn), A
o ………………
o ………………
o ………………
78
79
80
81
82
83
84
85
86
87
Question?
88