8259 programmable interrupt controller
Study notes on the Intel 8259 Programmable Interrupt Controller (PIC): what it does, how it is programmed (ICWs/OCWs), cascading masters/slaves, vector mapping and typical x86 initialization examples.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Two sentences: The 8259 Programmable Interrupt Controller (PIC) is the classic hardware device used to collect interrupt requests (IRQs) from devices, prioritize them, and present a single interrupt request (INTR) to the CPU. An engineer uses the 8259 when designing or understanding interrupt-driven I/O, interrupt vector mapping, cascading multiple PICs, and when writing low-level OS or boot code that initializes interrupt hardware.
Key ideas
Definition: INT (PIC output) / INTR (CPU input) — the 8259 asserts INT, which drives the CPU’s INTR pin to signal that an interrupt is pending.
Definition: INTA (Interrupt Acknowledge) — the CPU bus cycle (and line)(s) used to tell the PIC to place the interrupt vector on the data bus; for the 8086 family the CPU generates two INTA cycles for each interrupt.
Definition: Master / Slave — when two 8259s are connected, the primary is the master (handles IRQ0..IRQ7 to the CPU) and a second is a slave (its INT output is connected to one of the master's IRQ inputs—commonly IR2). The slave provides IRQ8..IRQ15 logically to the CPU via the master. In cascade, the master drives CAS0–CAS2 to select a slave during INTA; tie SP/EN high on the master and low on slaves.
Definition: Vector base (ICW2) — the programmable base vector number the PIC uses; the PIC maps IRQ n to the interrupt vector number (VectorBase + n). The CPU uses that vector number to index its Interrupt Vector Table (IVT) or vector table.
Initialization and control words (high level):
- ICWs (Initialization Command Words) set up PIC mode and vector base. The usual sequence is ICW1, ICW2, ICW3 (if cascade), and ICW4 (if needed). ICWs are required at reset or when reprogramming. Key ICW1 bits: LTIM selects level-triggered (1) vs edge-triggered (0) mode; SNGL selects single (1) vs cascaded (0); IC4=1 indicates that ICW4 will follow.
- OCWs (Operation Command Words) control runtime functions. Important ones:
- OCW1 — the Interrupt Mask Register (IMR). Bit=1 masks (disables) that IRQ; bit=0 enables it.
- OCW2 — used for End of Interrupt (EOI), rotation and priority commands. A typical non-specific EOI code is issued after ISR completes to inform the PIC service is done. In OCW2, set EOI=1 for EOI; with SL=0 it is non-specific EOI (common value 0x20), with SL=1 and L2–L0 it is specific EOI; R and L bits control rotate and priority.
- OCW3 — used to read status registers (IRR or ISR) and for polling. Use RIS to select IRR (RIS=0) or ISR (RIS=1) before reading the data port; set POLL=1 to enable polling mode.
Priority and servicing flow (step-by-step) — what to draw and label:
- Draw two 8259 boxes side by side labeled MASTER and SLAVE. For MASTER label pins IR0..IR7 and INT (output to CPU INTR). For SLAVE label IR0..IR7 and INT (connected to MASTER's IR2). Also draw CPU box with INTA lines from the CPU, CAS0–CAS2 lines from MASTER to SLAVE, and show data bus connection between PICs and CPU.
- Show device lines connected to IR pins (e.g., Timer -> IR0, Keyboard -> IR1, Slave-> master IR2, etc.).
- When a device asserts IRQ, PIC sets IRR (Interrupt Request Register), resolves priority (IRR vs ISR), and when CPU acknowledges (INTA), PIC places interrupt vector (VectorBase + IRQ) on the data bus.
Interrupt servicing sequence (detailed steps, assume x86-style INTA):
- Device asserts IRQ line to PIC.
- PIC sets the corresponding bit in IRR (Interrupt Request Register).
- When prioritized request reaches top, the 8259 asserts INT (driving the CPU’s INTR pin).
- CPU performs bus cycle to acknowledge (sends INTA pulses). For x86, CPU typically performs two INTA cycles.
- On acknowledgment, PIC places the interrupt vector number on the data bus during the last INTA cycle.
- CPU uses the vector number to fetch the ISR pointer from the IVT (in real mode: IVT base at physical address 0, vector entry size 4 bytes: segment:offset). CPU jumps to ISR.
- ISR runs; on completion it sends an EOI (End of Interrupt) to the PIC (usually by writing an OCW2 value), so PIC clears the corresponding bit in the ISR (In-Service Register) and can allow lower priority requests.
Important assumptions universities expect:
- The PIC maps IRQ n to (VectorBase + n) — so ICW2 determines VectorBase.
- IMR bits: 1 means masked (disabled), 0 means enabled (this is a common source of confusion; memorise it).
- In a cascaded pair, slave outputs connect into one master's IRQ input; the master must be told in ICW3 which IR is connected to a slave, and the slave must be told its cascade identity. Default fixed priority order is IR0 (highest) to IR7 (lowest) on each PIC; rotating priority can be enabled with OCW2.
Note: bit-by-bit definitions of ICW1/ICW2/ICW3/ICW4 and OCW bits are often asked — see verify list for exact bitfields I want you (reviewer) to confirm.
Formulas
VectorNumber = VectorBase + IRQ — VectorBase is the value loaded in ICW2; IRQ is the local IRQ number (0..7 for a PIC). Applies when PIC is in normal mapping mode.
IVT_Address (bytes) = VectorNumber × 4 — for x86 real mode where each IVT entry is 4 bytes (offset:2, segment:2). Units: bytes. Use this to calculate where CPU fetches ISR pointer.
GlobalIRQ (for slave line i) = 8 + i — slave-local IRQ lines 0..7 map to logical IRQ8..IRQ15. The master IR used for the cascade (commonly IR2) does not change this numbering; it only carries the slave’s request.
IMR_bit = 1 means IRQ masked (disabled); IMR_bit = 0 means IRQ enabled — IMR is OCW1 written to PIC data port.
Worked examples
- Vector and IVT address for IRQ13 with remapped PICs
- Given: Master VectorBase = 0x20 (32 decimal), Slave VectorBase = 0x28 (40 decimal). IRQ13 is a slave IRQ (since IRQ8..15 are slave).
- Step 1: Determine whether IRQ13 is on slave: yes (13 >= 8).
- Step 2: Compute slave-local IRQ = 13 - 8 = 5.
- Step 3: VectorNumber = SlaveVectorBase + slave-local IRQ = 40 + 5 = 45.
- Step 4: IVT_Address = 45 × 4 = 180 bytes = 0xB4.
- Final answer: IRQ13 maps to vector 45; IVT entry starts at byte address 180 (0xB4) in real mode.
- Typical initialization bytes for PC-style two-PIC setup (master + slave) and enabling only Timer and Keyboard
- Common initialization sequence (IO port usage on IBM PC compatibles): master command port = 0x20, master data port = 0x21; slave command = 0xA0, slave data = 0xA1.
- Typical ICW sequence (values commonly used):
- Master: ICW1 = 0x11, ICW2 = 0x20, ICW3 = 0x04, ICW4 = 0x01
- Slave: ICW1 = 0x11, ICW2 = 0x28, ICW3 = 0x02, ICW4 = 0x01 Explanation: ICW2 sets vector bases (0x20 and 0x28). In ICW3 master value 0x04 = b00000100 indicates a slave connected on master's IR2; slave ICW3 = 0x02 indicates its cascade identity is 2.
- After ICWs, set IMR (OCW1) to enable only Timer (IRQ0) and Keyboard (IRQ1) on master, mask everything else: IMR_master = b11111100 = 0xFC (bits 0 and 1 are 0 to enable IRQ0 and IRQ1). If no slave devices, IMR_slave = 0xFF (all masked). If you intend to accept any slave IRQs, unmask IR2 on the master (set bit 2 = 0).
- Final answers: Initialization bytes as above; IMR_master written as 0xFC, IMR_slave 0xFF.
- Example: Sending End of Interrupt (EOI)
- After ISR for IRQ1 completes, CPU writes non-specific EOI to master command port. Common OCW2 for non-specific EOI is 0x20. So out 0x20, 0x20 signals PIC that service for the highest priority in-service IRQ is completed. For an interrupt from the slave (IRQ8–IRQ15), send a non-specific EOI to the slave command port (0xA0) first, then to the master (0x20).
Common mistakes
- Mixing up the master/slave connection: slave INT output must be connected to a single master IR pin (commonly IR2) and ICW3 values must reflect that.
- Forgetting to send EOI after servicing an interrupt; this leaves ISR bit set and further interrupts at same or lower priority are blocked.
- Confusing vector number with IVT byte address — remember IVT entry size is 4 bytes in x86 real mode.
- Incorrect order or values when writing ICWs and OCWs; ICW1 must be written before ICW2, etc.
For your exam
Kinds of questions and what full marks require:
- Definition/short note: Define 8259 PIC, IMR, ICW/OCW, INTR/INTA. A full answer gives concise definitions and roles.
- Diagram and explanation: Draw cascaded master-slave connection. A full answer draws two 8259 boxes, labels IR0..IR7, shows slave INT connected to master IR2, CPU INTR and INTA lines, and steps for interrupt flow.
- Derivation/step-by-step: Explain how interrupt vector is formed and how CPU finds ISR. Full marks require listing VectorNumber = VectorBase + IRQ and IVT_Address = VectorNumber×4 (for x86 real mode) and the servicing sequence (IRR→INTR→INTA→ISR fetch→EOI).
- Numerical/programming: Given vector bases and IRQ number compute vector and IVT address; or provide IO port writes to initialize PICs and IMR value for a given mask. Full marks show each arithmetic step and correct final hex values.
- Short program: Give assembly or pseudo-code sequence to initialize PIC pair and set masks. Full answer lists command/data ports, ICW/OCW bytes and their order and explains purpose of each write.
Quick check
- What does OCW1 control?
- If master VectorBase = 0x30 and IRQ4 occurs on master, what is the VectorNumber?
- In a cascaded PIC, which master IR is typically used for the slave connection on PCs?
Answers: OCW1 controls the Interrupt Mask Register (IMR); VectorNumber = 0x30 + 4 = 0x34 (52 decimal); typically master IR2 is used for the slave cascade.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?