Hardware Interrupts: Thorne: 21.1, 21.3 Thorne: 21.1, 21.3 (Irvine Edition IV: Section 16.4)
Hardware Interrupts: Thorne: 21.1, 21.3 Thorne: 21.1, 21.3 (Irvine Edition IV: Section 16.4)
Hardware Interrupts: Thorne: 21.1, 21.3 Thorne: 21.1, 21.3 (Irvine Edition IV: Section 16.4)
SYSC3006
SYSC3006
SYSC3006
Address Bus
Data Bus
Data Port
Status Port
Hardware Interrupts
require a hardware
signal
g from the device
to the processor.
Control Port
SYSC3006
Hardware Interrupts
The interrupted processing doesnt know it was interrupted
The processor just:
1 temporarily suspended current thread of control
1.temporarily
2.ran Interrupt Service Routine (ISR, later!)
3.resumed suspended thread of control
Polling:
P lli
CPU asks
k the
h peripheral
i h l devices
d i
whether
h h there
h is
i
anything to do now ?
Programming is sequential : Next instruction is determined
b the
by
h fetch-execute
f h
cycle
l andd by
b controll transfer
f
instructions.
Interrupts: Device tells CPU it is time to do something NOW
External hardware signals spontaneously cause transfer of
control (cause interruption in default sequential program).
No busy waiting, no polling.
Programming now requires an event-driven mindset
SYSC3006
; m-1
; m-2
; m-3
; m-4
ISR PROC
MOV AX,1
IRET
; i-1
; i-2
i2
ISR ENDP
main ENDP
SYSC3006
If interrupts happen
CPU is used by ISR whenever the interrupt occurs.
time
m-1
m
1 m
m-2
2 ii-1
1 ii-2
2 m
m-3
3 m-4
m 4 m-1
m 1 ii-1
1 ii-2
2 m
m-2
2 m-3
m 3 m-4
m 4 m-1
m 1m
m-2
2 m-3
m 3 m-4
m 4
time
Interrupt
p signal
g
SYSC3006
main
ISR
m-1
1
m-2
Interrupt
signal
i-1
ii-2
2
m-3
m-4
m-1
m
1
i-1
i-2
m-2
m-3
m-4
m-1
SYSC3006
10
NMI INTR
bus
Outside the
computer system
SYSC3006
11
Interrupt Maskability
NonMaskable
k bl interrupts
i
cannot ever be
b ignored
i
d by
b the
h CPU
8086 Example : NMI is non-maskable
Used for catastrophic
p errors (e.g.
( g RAM failure
Power failure, etc).
An Interrupt request can be pending: the signal is active but it has not
yyet serviced
For maskable interrupts, it may or may not be serviced
until/if it is enabled
SYSC3006
12
SYSC3006
13
SYSC3006
14
0:0
IP
0:2
CS
0:4
IP
0:6
CS
Address of type 0
Address of type 1
IP at low
CS at high
0 : 3FC
0 : 3FD
0 : 3FE
0 : 3FF
SYSC3006
IP
Address of type 255
CS
15
SYSC3006
16
17
. . .
Device 2
NMI
bus
SYSC3006
18
interrupt
controller
INTR
. . .
device
bus
SYSC3006
19
20
return IP
return CS
fl
flags
SYSC3006
Return
Address
21
return IP
return CS
flags
arguments
SYSC3006
22
Installing an ISR
SYSC3006
23
EQU
EQU
40
; install as vector
myint_type * 4
PROC FAR
ISR
ENDP
IP of ISR
AX, 0
ES, AX
; ES vector table segment
ES:[myvector] , OFFSET myisr
segment override for
destination segment !
ES:[myvector+2] , @code
CS of ISR
24
SYSC3006
25
MOV AX, 0
MOV ES, AX
; ES vector table segment
MOV AX, ES:[myvector]
MOV old_vector_offset, AX
Save original vector table!
MOV AX, ES:[myvector+2]
MOV old_vector_segment,
ld
t
t AX
MOV ES:[myvector] , OFFSET myisr
MOV ES:[myvector+2] , @code
...
SYSC3006
26
INTR
IR0
8259 IR1
PIC IR2
IR3
IR4
IR5
IR6
IR7
Interrupting
Device
. . .
Interrupting
Device
IR7
SYSC3006
27
CPU
Interrupting Device
Slave
PIC
Master
PIC
INTR
Interrupting Device
bus
Interrupt type is read from data bus.
SYSC3006
28
8086
processor
IR0
8259 IR1
PIC IR2
IR3
IR4
IR5
IR6
IR7
INTR
Timer
Keyboard
. . .
LPT1
Pi t
Printer
T
Type-08h
08h
Type-09h
Type-0Fh
Interrupt type
bus
29
PC Example : Keyboard
Assume interrupts are enabled (assume IF = 1)
When the keyboard hardware logic asserts IR1 at PIC, the PIC
generates INTR signal to the processor
During interrupt acknowledge, PIC identifies interrupt Interrupts
disabled
source as type 9
when ISR
The CPU executes the INT 9h behaviour
begins
Saves the flags
execution
Clears IF and TF (Disabling interrupts at processor)
Saves CS and IP
Reads the interrupt-type = 9h from the Data bus and
vectors to the ISR pointed to by the double word at
address of 0 : 9h*4 in Vector Table (memory)
Execution of ISR 9 is caused by hardware interrupt mechanism
No software
so twa e involved
vo ved in the invocation oof ISR
S 9!
SYSC3006
30
31
Interrupt Priority
Interrupting Devices are assigned priorities
Higher priority devices take precedence over lower priority
devices
Priority is applied whenever interrupts coincide
When multiple interrupts occur at the same time
When
h new interrupts
i
occur while
hil still
ill processing
i the
h ISR
S off
previous interrupts.
Typically, interrupt controllers manage priority issues.
In PCs
The devices have pre-configured connections to PIC
Timer is always IR0 and Keyboard is always IR1
DOS programs the 8259A to assign priority based on the
device connection
IR0 == highest priority and IR7 == lowest priority
SYSC3006
IF=0
IF=1
IF=0
IF=1
IF=0
IF=1
IRET
IRET
IR1(= 09h)
IR0 (= 08h)
IF=0
Main
IRET
IR2 (= 0Ah)
IR1 (= 09h)
SYSC3006
33
IF 1
IF=1
IF=1
IF=0
IF=0
IF=1
IF=1
IR1
IR0
IF 0
IF=0
IF 0
IF=0
IRET
IF=1
IF=0
IF
0
IF=1
IF=1
INT 9
STI
STI
IR1
IRET
STI IRET
IR0
When is this ?
34
IR0
IR1
IR0
IR1
35
Pending Interrupts
Terminology: A Pending Interrupt is an interrupt signal that
i latched
is
l t h d somewhere
h in
i the
th system,
t
but
b t has
h nott yett been
b
acknowledged by the processor
Interrupts can be pending at device and / or at the PIC
Example : The Intel 8259 has an internal 8-bit register
one bit pper IR input
p
When IR is asserted, the associated bit is set
When the interrupt on IR is acknowledged, the associated bit
is cleared
In summary, the PIC has 1-bit memory for each IR
It can remember up to 1 pending interrupt for each IR
Total 8 different IRs
SYSC3006
36
End-of-Interrupt (EOI)
After sending an interrupt to processor, PIC needs to know
when it is safe to generate a lower priority interrupt
the PIC requires feedback from the CPU
End Of Interrupt (EOI) is a command sent to PIC from the CPU
It is not part of the INTA cycle; it is not done in hardware
It is a software command; ie. something your program must
do.
do
SYSC3006
37
PIC Programmer
Programmerss Model
bit = 1
mask at processor
IF = 0
38
SYSC3006
39
SYSC3006
40
The keyboard will not send the next scan code until the previous
scan code has been acknowledged
T acknowledge
To
k
l d scan code:
d
PB bit 7 must be toggled from 0 1 and then 1 0
41
42
SYSC3006
43
Requirements
prints uppercase chars representing keys pressed
ALT,
ALT SHIFT,
SHIFT CTRL keys
k
(and
( d a few
f others)
th ) are nott
managed
exit the program by resetting
ISR ignores key released scan codes
uses lookup table to convert key released scan code to
uppercase ASCII representation
SYSC3006
44
45
EQU
EQU
0AH
0DH
SYSC3006
46
.code
Keyboard : Code Fragments
MAIN PROC
CLI
; Disable ints while installing ISR
; Install the Keyboard ISR at Interrupt Type = 9
; Enable keyboard
y
interrupts
p at the PIC
STI
; Enable interrupts at the processor
FOR_EVER:
; press reset to exit
CALL GET_CHAR
; returns ASCII in AL
PUSH AX
; save char & pass parameter
CALL DISPLAY_CHAR ; displays char in AL
POP
AX
; restore char & clear stack
CMP
AL , CR
; check for Enter key
JNZ
REPEAT_LOOP
MOV
AL , LF
; if Enter do LF too !
PUSH AX
CALL DISPLAY_CHAR
DISPLAY CHAR
ADD
SP, 2
REPEAT_LOOP:
JMP
FOR_EVER
MAIN ENDP
SYSC3006
47
SYSC3006
48
AL , 60H
; AL = scan code
49
SYSC3006
51
52
counts
ticks
oscillator signal at
known frequency
Timer
Component
Timing Component :
Input Signal : oscillator circuit generates ticks at a known
frequency e.g. square wave at some frequency
It counts ticks (edge-to-edge) on the input signal
Since frequency of ticks is known, then counting a fixed
number
b represents the
h passage off a known
k
amount off time
i
SYSC3006
53
8253
timer
i
0
Input Clock
(crystal oscillator)
timer 1
timer 2
16-bit counter
16-bit counter
16-bit
16
bit counter
SYSC3006
54
0 or 1
55
56
57
g
There are three 8-bit Data Registers
Timer 0 Counter Register 40H
Timer 1 Counter Register 41H
Timer 2 Counter Register 42H
Question : How can 8-bit data registers be used to initialise 16bit internal counters ?
SYSC3006
58
write-only
timer
select
l t
SC1
0
0
1
read/load
sequence
SC0
0
1
0
mode
timer select
select timer 0
select timer 1
select timer 2
SYSC3006
59
RL0
1
0
1
read/load sequence
read/load LSB only
read/load MSB only
read/load LSB first,
first then MSB
M2 M1 M0 mode (6 modes)
x
1 1 mode
d 3 square wave generator where
h x = dont
d care
(other modes in ELEC-4601 )
BCD (binary coded decimal)
0 16 bit binary count
1 BCD
C cou
count (4
( decimal
dec
digits)
d g s)
SYSC3006
60
61
62
SYSC3006
63
20Hz Real
Real-Time
Time Clock : Before Programming
1. Develop the Program Architecture
Two software components - main program and timerISR
will share a 32-bit unsigned count variable
Why 32-bit ?
timerISR increments count everyy tick
Main program reads count whenever it needs to
Main Program
Initialize system
do work (usually a loop)
{
current= getTicks()
}
exit
timerISR
count
SYSC3006
count++
64
load
mode
BCD
36H
65
20H Real-Time
20Hz
R l Ti
Clock
Cl k : Before
B f
programming
i
SYSC3006
66
SYSC3006
67
68
69
70
Event-driven thinking !
timerisr PROC FAR
STI
; Re-enable ints
PUSH DS
; Save EVERY register used by ISR
PUSH DX
What are implications of this?
PUSH AX
Is it really needed?
MOV AX , @data
MOV DS , AX
ADD count_low,1
eventJNC t1
di
driven
thinking !
INC count_high
t1:
CLI
; Lock out all ints until IRET
MOV AL , EOI
; Send EOI to PIC
MOV DX , PIC_COMMAND_REG
OUT DX , AL
POP AX
; Restore registers
POP DX
POP DS
IRET
timerisr
i
i ENDP
71
20H Real-Time
20Hz
R l Ti
Clock
Cl k : Code
C d F
Fragments
t
get_ticks
get
ticks PROC NEAR
CLI
; Lock out ints while accessing shared data
; (ensure mutual exclusion)
eventdriven
thinking !
MOV DX , count_high
MOV AX , count_low
count low
STI
RET
get_ticks ENDP
; Re-enable ints
SYSC3006
72
Timer 0 tick
Timer 0 tick
Timer 0 tick
Wh t is
What
i this
thi value
l ?
SYSC3006
73
main
ISR
iinit
i
main work
count++
count++
What is this value ?
What does count
represent ?
SYSC3006
74
75
SYSC3006
DX = 0010H
AX = 0000H
76
SYSC3006
77
Polled Input/Output
CPU controls the synchronization
y
with device
Execution is simple : sequential control flow
Polled transfer rates are usually reasonable
Maximum
i
transfer
f rate is
i the
h time
i to execute the
h check
h k (once
(
the device is ready) and then transfer
Polled latencyy is usuallyy unpredictable
p
Depends on the other works being done between checks
unless CPU works only on polling, in which case CPU is
dedicated to I/O and cannot perform any other work.
work
What if other interesting events happen on other devices ?
SYSC3006
78
Interrupt-Driven Input/Output
Device signals CPU of a new I/O event
Event-driven programming
In general, interrupts improve the latency of a system
Interrupt latency is deterministic
Interrupt transfer rates includes interrupt overhead
Can be slower than polled
Depends . If amount of data transferred per interrupt
exceeds the overhead of switching tasks
Advantage : CPU can be doing other work whenever there is no
I/O event.
Software
S ft
structure
t t
remains
i clean
l
andd task-oriented.
t k i t d
Although the time to complete the other work is marginally
increased, depending on the number of interrupts.
SYSC3006
79