When interacting with IO devices that provide or require data at relatively low data transfer rates, the CPU time is wasted while waiting the IO device to be ready to send or receive next data.
To not waste time, a technique called interrupt processing has been developed. In this technique, CPU does not need yto check if the device is ready, but the devices inform the ready situation by interrupting the CPU.
For example, if a keyboard is connected to the system, as soon as a key is pressed, a signal interrupts the processor. The following shows a time line of a system where a typist typing data through a keyboard, a printer prints data and another program executing.
Interrupts can be classified into three groups:
One problem with interrupt processing is to know the source of interrupt. For this reason every unique source of interrupt has a unique number called interrupt number or type.
In 80x86 processors, an interrupt is either a hardware generated or software generated CALL. Either an hardware or software signal will interrupt the program by calling an Interrupt Service Procedure(ISP) or Interrupt Handler.
For hardware interrupt three pins are available: INTR(Interrupt), INTA'(Interrupt Acknowledgement) and NMI(Non Maskable Interrupt).
The following is the internal interrupts in 8086
There are five interrupt instruction in the 80x86 CPU's.
INT n, INT 3, INTO, BOUND, IRET
An interrupt vector contains the address( segment and offset) of an ISP.
In real mode, an interrupt vector is a 4-byte far address stored in the first 1024 bytes of memory. For each interrupt type a vector is assigned, and there are 256 different vectors. I.e. there can be 256 unique source of interrupts in 80x86. The address of an interrupt vector is found as follows:
The address of vector n = n*4
For example the vector address of 8 is 8*4=20h
The first five interrupt types are used for internal or special interrupt sources.
In protected mode, the vector table is replaced by an interrupt descriptor table that uses 8-byte descriptors for each of the interrupts.
Intel reserves the first 32 interrupt vectors for internal interrupts. The rest is available to user.
After the execution of each instruction, the microprocessor checks for an interrupt request according to the types in the following order.
Whenever an interrupt is encountered, CPU does the followings:
CS
IP
In fact an interrupt can be simulated by the following instructions:
| PUSHF |
| CLI |
| CALL FAR PTR [n*4] |
First find the vector address for this interrupt. The vector address is
. Therefore, the four bytes begginning from the address
contains the beginning address of the ISP. Assume it contains the value
44325621h. Then the address of ISP is 2156h:3244h.
For example, if the BOUND SI,DATA instruction executes, then the word M[DS:DATA] contains the lower boundary and the word M[DS:DATA+2] contains the upper boundary.
The following program is an initializer and interrupt handler for the interrupt 9(keyboard interrupt in PC)
stack segment
st dw 256 dup(?)
stack ends
data segment
oldsr dd ?
oldss dw ?
oldsp dw ?
data ends
code segment
.386
assume cs:code,ds:data
main:
mov ax,data
mov ds,ax
mov ax,0
mov es,ax
;store old interrupt number
mov bx,9
shl bx,2 ;multiply by 4 and get vector number
mov ecx,es:[bx]
mov oldisr, ecx ;store old vector
;put new interrupt number
mov cx,offset myisr-for-int9
mov es:[bx],cx ;store offset address of new isr
mov ax,cs
mov es:[bx+2],ax ; store segment address of new isr
; stay resident
mov ax,3100h
mov dx,100 ;reserve sufficient amount of memory for this program to be resident
int 21h ; return to dos but this program will stay resident
myisr-for-int9:
; set SS and SP, if you want to use your stack segment
push ax ; ax is modified, so save
push ds ; ds is to be modified, so save it
mov ax,data
mov ds,ax ; ds now points to our data segment
mov oldss,ss ; save old ss
mov ax,stack
mov ss,ax ; ss now points to our stack segment
mov oldsp,sp ; save old sp
mov sp,offset st
add sp,256 ; set sp to top of stack
; call old ISR
pushf
call far ptr oldisr ; simulate an interrupt
;do what you want
.
.
mov sp,oldsp
mov ax,oldss
mov ss,ax
pop ds
pop ax
iret
code ends
end main
The INT signal of the microprocessor is used to request interrupt. This signal can be maskable by the I flag.
The problem in the hardware interrupt is to get the interrupt type.
When device A requested an interrupt it sets the IR flag in it and thus raises the INT line of the bus master. When bus master recognizes the interrupt, it issues an interrupt acknowledgement cycle to read the interrupt vector.In this figure, a three state buffer puts the vector 0Fh to the data bus when the INTA' signal is activated. Then bus master begins to run the ISP associated with this vector.
The IR flag in the device A must be cleared after the interrupt recognized. Otherwise it will continue to give the interrupt. There may be many methods to achieve this.
The INTA' line is directly connected to the IR flag or device A to clear it.
Since there is only one INT line, there are two problems that must be solved by the bus master:
The first problem can be solved with a technique known as polling. The CPU can read the value of the IR flag in each device via an input port. So at the beginning of ISP the following algorithm is used.
By this way the second problem can also be solved. Poll the devices according to an order. Then the device first polled will be serviced first.
In this case, assume a unique vector is assigned to every interrupting device. Then the problem to be solved is the handling of simultaneous interrupts.
The following circuit shows a solution.
For each combination of request a new interrupt vector is generated.
The disadvantage of this method is that the maximum number of interrupt source is 8.
In daisy chained interrupt structures, the priority among the devices are resolved in a serial fashion.
If din=0 and irq=1 then dout=1
If din=0 and irq=0 then dout=0
If
din=1 then dout=1.
In this scheme, only one device releases its vector on the bus during an interrupt acknowledge.
As the name implies, it is not maskable. I.e. can not be disabled. So it is often used for parity errors and other major system faults such as power failures. The following is a sample power failure detection circuit.
Question: Why the NMI interrupt signal recognized on the rising edge, although INT signal is a level detected signal.
The following figure shows a 8259 decoded at port 0400H and 0402H.
Each interrupt input has a mask bit and an in-service bit. If the mask bit of an input is set, then it is disabled. The mask bits can be set or reset by the programmer. The in-service bit of an interrupt input is set whenever an interrupt from this input is accepted. The in-service bit is cleared by an EOI(End of Interrupt) command.
When the chip gets an interrupt in one of its or more than one input do the followings:
There are four initialization command words for the 8259 that are selected
when the
pin is a logic 1. There are two ways of
initialization:
send ICW1,ICW2,ICW3 in the order
send ICW1,ICW2,ICW3,ICW4 in the order.
The second way is used if more than one 8259 are used in cascade mode.
OCW1,OCW2 and OCW3 are used to direct the operation of the 8259A after it is
initialized. OCW1 is selected when
. OCW2
is selected when
,
and
. OCW3
is selected when
,
,
and
. The following is an explanation of each words.
Assume a keyboard is connected to the IR2 line of a 8259 located at 300h. Write an initialization and ISP for this such that the interrupt vector of the keyboard is 56h and the keyboard port is at 600h.
When a slave gives an interrupt, the master informs the slave by CAS3-CAS0 signals to release its vector. If a slave is connected to IRn input of the master, then the slave's id must be n.