Lesson 41 +40 XP

Modes of Transfer: Programmed I/O, Interrupt-Initiated I/O & Priority Interrupt

Modes of Transfer: Programmed I/O, Interrupt-Initiated I/O & Priority Interrupt

Data can move between memory and a peripheral in three ways, which differ in how much of the CPU's time they use:

  1. Programmed I/O: the CPU runs a program that checks the device and moves every byte itself.
  2. Interrupt-initiated I/O: the device tells the CPU (interrupts it) when it is ready, so the CPU does not wait.
  3. Direct memory access (DMA): the interface moves data straight into memory, and the CPU is only told when the whole block is done (lesson 42).

This lesson covers the first two and how a CPU chooses between several devices that interrupt at the same time.

1. Programmed I/O

In programmed I/O every transfer is done by CPU instructions. To input a byte, the CPU repeatedly reads the interface's status register until a flag bit says new data is ready, then reads the data register and stores the byte in memory.

Flowchart for programmed inputFlowchart for programmed input

The CPU stays in this loop the whole time, which wastes it when the device is slow.

SolvedExample 1 · Wasted time in programmed I/O (Mano)

A device transfers 100 bytes per second, and the CPU executes one instruction in 1 µs. How many times does the CPU check the flag between two bytes?

  1. Time between bytes = 1 / 100 s = 10 ms = 10000 µs.
  2. Each check of the flag takes about one instruction, 1 µs.
  3. Checks per byte = 10000 / 1 = 10000.
Answerabout 10000 checks per byte, all of them wasted CPU time
SolvedExample 2 · Fraction of CPU time used

A keyboard produces 10 characters per second. The CPU needs 20 µs to read and store one character once the flag is set. If it used interrupts instead of polling, what fraction of its time would input take?

  1. Useful work per second = 10 × 20 µs = 200 µs.
  2. Fraction = 200 µs / 1 s = 0.0002 = 0.02%.
Answer0.02% with interrupts, against 100% with a busy-wait loop

2. Interrupt-Initiated I/O

Instead of polling, the CPU enables the interface's interrupt and continues with other work. When the flag is set, the interface sends an interrupt request. The CPU then:

  1. finishes the current instruction,
  2. saves the return address (the PC) on the stack or in a fixed place,
  3. branches to an interrupt service routine (ISR) that does the transfer,
  4. returns to the interrupted program when the ISR ends.

There are two ways to find the ISR:

  • Non-vectored interrupt: the ISR is at a fixed address in memory (the basic computer uses location 1, lesson 30).
  • Vectored interrupt: the interrupting device sends a vector address (VAD) that tells the CPU where its own ISR begins.
Programmed I/OInterrupt-initiated I/O
Who checks readinessthe CPU (polling loop)the interface (raises an interrupt)
CPU time wastedhigh: busy-waitinglow: no waiting
Responseas soon as the loop sees the flagafter the current instruction
Hardwaresimpleneeds interrupt logic

3. Priority Interrupt

Several devices may request an interrupt at the same moment. A priority interrupt system decides which one is served first and also stops a lower-priority device from interrupting a higher-priority ISR. Fast devices (such as disks) get high priority; slow ones (such as keyboards) get low priority.

Polling (Software Priority)

All devices share one interrupt line. The common ISR tests the devices one by one in priority order and branches to the first one with its flag set. It is flexible but slow when there are many devices.

Daisy Chain (Serial Hardware Priority)

Daisy-chain priority interruptDaisy-chain priority interrupt

All devices share the interrupt request line. They are connected in series by the interrupt acknowledge line, highest priority nearest the CPU:

  • The CPU answers a request with INTACK, which enters device 1 as PI (priority in).
  • A device with PI = 1 that is not requesting passes it on: PO = 1.
  • A device with PI = 1 that is requesting blocks it (PO = 0) and puts its vector address on the data bus.
  • A device with PI = 0 does nothing; a higher device is being served.
PIRF (request)POEnable VAD
0000
0100
1010
1101

So PO = PI · overline{RF} and Enable = PI · RF.

The three-device chain below gives device 1 vector 16, device 2 vector 32 and device 3 vector 48:

VERILOG
// Daisy-chain priority: the CPU's acknowledge (PI of device 1) passes
// down the chain; a requesting device blocks it (PO = 0) and puts its
// vector address on the data bus.
module daisy_stage (
    input pi,                  // priority in
    input request,
    output po,                 // priority out to the next device
    output enable_vad          // this device drives the bus
);
    assign po = pi & ~request;
    assign enable_vad = pi & request;
endmodule

module daisy_chain (
    input ack,                 // interrupt acknowledge from the CPU
    input [2:0] req,           // req[0] is device 1, nearest the CPU
    output intr,               // wired-OR interrupt line
    output [7:0] bus
);
    wire p1, p2, p3;
    wire [2:0] en;
    daisy_stage d1 (ack, req[0], p1, en[0]);
    daisy_stage d2 (p1, req[1], p2, en[1]);
    daisy_stage d3 (p2, req[2], p3, en[2]);
    assign intr = |req;
    assign bus = en[0] ? 8'd16 : en[1] ? 8'd32 : en[2] ? 8'd48 : 8'hzz;
endmodule

module tb;
    reg ack;
    reg [2:0] req;
    wire intr;
    wire [7:0] bus;
    daisy_chain u (ack, req, intr, bus);
    integer k;
    initial begin
        ack = 1;
        $display("dev3 dev2 dev1 | INT | vector on bus");
        for (k = 0; k < 8; k = k + 1) begin
            req = k;
            #1 $display("  %b    %b    %b  |  %b  | %0d", req[2], req[1], req[0], intr, bus);
        end
        $finish;
    end
endmodule

Whenever several devices request together, the vector belongs to the one nearest the CPU:

OUTPUT
dev3 dev2 dev1 | INT | vector on bus
  0    0    0  |  0  | z
  0    0    1  |  1  | 16
  0    1    0  |  1  | 32
  0    1    1  |  1  | 16
  1    0    0  |  1  | 48
  1    0    1  |  1  | 16
  1    1    0  |  1  | 32
  1    1    1  |  1  | 16

Parallel Priority Interrupt (Priority Encoder)

Parallel priority interrupt hardwareParallel priority interrupt hardware

Each device sets its own bit in an interrupt register. A mask register has one bit per device, which the program sets or clears to enable or disable that device.

The AND of the two goes to a priority encoder, which outputs the number xy of the highest-priority active input and sets the interrupt status IST. If the interrupt enable IEN is also set, the CPU gets the request INTR, and the vector address is formed from x and y.

Priority encoder truth table (I0 has the highest priority):

I0I1I2I3xyIST
1xxx001
01xx011
001x101
0001111
0000xx0
SolvedExample 3 · Boolean functions of the priority encoder

Find x, y and IST from the truth table.

  1. x = 1 in the rows for I2 and I3, which both need I0 = 0 and I1 = 0: x = overline{I0} · overline{I1}.
  2. y = 1 in the rows for I1 (overline{I0} I1) and I3 (overline{I0} overline{I1} overline{I2} I3). The second term simplifies with the don't-care row: y = overline{I0} I1 + overline{I0} · overline{I2}.
  3. IST is 1 when any input is 1: IST = I0 + I1 + I2 + I3.
Answerx = overline{I0} overline{I1}, y = overline{I0} I1 + overline{I0} overline{I2}, IST = I0 + I1 + I2 + I3

In this example the vector address is xy followed by two 0s, so the four devices jump to locations 0, 4, 8 and 12:

VERILOG
// Parallel priority interrupt (Mano): interrupt register AND mask
// register feed a priority encoder; I0 has the highest priority.
module priority_interrupt (
    input [3:0] irq,           // interrupt register bits I3..I0
    input [3:0] mask,          // 1 = enabled
    input ien,                 // interrupt enable flip-flop
    output x,
    output y,
    output ist,                // interrupt status
    output intr,               // interrupt request to the CPU
    output [7:0] vad           // vector address
);
    wire [3:0] i = irq & mask;
    assign x = ~i[0] & ~i[1];
    assign y = (~i[0] & i[1]) | (~i[0] & ~i[2]);
    assign ist = |i;
    assign intr = ist & ien;
    assign vad = {4'b0000, x, y, 2'b00};   // 0, 4, 8 or 12
endmodule

module tb;
    reg [3:0] irq, mask;
    reg ien;
    wire x, y, ist, intr;
    wire [7:0] vad;
    priority_interrupt u (irq, mask, ien, x, y, ist, intr, vad);
    task show;
        $display("I3..I0=%b mask=%b IEN=%b | xy=%b%b IST=%b INTR=%b VAD=%0d",
                 irq, mask, ien, x, y, ist, intr, vad);
    endtask
    initial begin
        ien = 1; mask = 4'b1111;
        irq = 4'b0000; #1 show;
        irq = 4'b1000; #1 show;
        irq = 4'b1100; #1 show;
        irq = 4'b1010; #1 show;
        irq = 4'b1011; #1 show;
        mask = 4'b1100; #1 show;   // I1 and I0 masked off
        ien = 0;  #1 show;         // CPU has disabled interrupts
        $finish;
    end
endmodule

The encoder picks the lowest-numbered active input; masking I0 and I1 lets I3 through, and IEN = 0 blocks the request to the CPU:

OUTPUT
I3..I0=0000 mask=1111 IEN=1 | xy=11 IST=0 INTR=0 VAD=12
I3..I0=1000 mask=1111 IEN=1 | xy=11 IST=1 INTR=1 VAD=12
I3..I0=1100 mask=1111 IEN=1 | xy=10 IST=1 INTR=1 VAD=8
I3..I0=1010 mask=1111 IEN=1 | xy=01 IST=1 INTR=1 VAD=4
I3..I0=1011 mask=1111 IEN=1 | xy=00 IST=1 INTR=1 VAD=0
I3..I0=1011 mask=1100 IEN=1 | xy=11 IST=1 INTR=1 VAD=12
I3..I0=1011 mask=1100 IEN=0 | xy=11 IST=1 INTR=0 VAD=12

The Interrupt Cycle

At the end of each instruction, if IEN = 1 and IST = 1, the CPU goes through the interrupt cycle:

MicrooperationMeaning
SP ← SP - 1make room on the stack
M[SP] ← PCpush the return address
INTACK ← 1acknowledge; the interrupting device's VAD goes on the bus
PC ← VADbranch to the vector address
IEN ← 0disable further interrupts

The ISR usually starts by saving the processor registers, then sets the mask register to allow only higher-priority devices and sets IEN again (so a higher device can interrupt it). It ends by clearing IEN, restoring the registers and mask, setting IEN and returning (pop PC).

MethodKindSpeedFlexibility
Pollingsoftwareslow (tests one device at a time)priority changed by rewriting the program
Daisy chainserial hardwarefastpriority fixed by position in the chain
Parallel (encoder)parallel hardwarefastestmask register changes which devices may interrupt
💡 Tip

Types of interrupts: external (from I/O devices, timers, power failure), internal or traps (from the program: overflow, divide by zero, invalid opcode) and software interrupts (a special instruction, used for supervisor calls).

Key points to remember

  • Programmed I/O busy-waits on a flag; interrupt I/O lets the device signal readiness.
  • Vectored interrupt: the device sends the ISR address (VAD).
  • Daisy chain: PO = PI · RF'; the device nearest the CPU wins.
  • Priority encoder: x = I0' I1', y = I0' I1 + I0' I2', IST = OR of all inputs; INTR = IST · IEN.
  • Interrupt cycle: push PC, INTACK, PC ← VAD, IEN ← 0.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Explain the modes of data transfer. Compare programmed I/O and interrupt-initiated I/O. (5 Marks)
  • Q2. What is a priority interrupt? Explain the daisy-chain method with a diagram. (10 Marks)
  • Q3. Draw the parallel priority interrupt hardware and give the truth table and Boolean functions of the priority encoder. (10 Marks)
  • Write: interrupt and mask registers, encoder table, x, y, IST, IEN, VAD.
  • Q4. What happens during the interrupt cycle? List its microoperations. (5 Marks)
  • Q5. Differentiate between vectored and non-vectored interrupts. (5 Marks)