# Input/Output
- Ports
- GPIO = General Purpose Input Output
- UART
- [[mem-org|Memory]] layout for IO
- **Flat** - Directly assess devices at a memory address, efficient but less
secure. More prevalent in modern system.
- **Segmented**: Memories divided into separate segments, some are dedicated
for devices. Better isolation.
## Device
- Components - three (sets) of registers
- Control - configure
- Status - interrogate
- Data - interact
- Reading from a previously written register does not necessarily yield the same
result!
## Interrupt
- Polling vs Interrupt
- Polling - heavy lifting is on CPU
- Interrupt - move a bit of intelligence to device, async and aperiodic
- Pins (usually low enable)
- `IRQ` pin: maskable interrupt request
- `XIRQ` pin: un-maskable, always handled
- PIC = Programmable Interrupt Controller, decodes which device interrupts.
Typically handles all the maskable interrupts.
- Multiple IRQ lines and priority
- Nested Interrupt
- Procedure
- Device -> event
- Device "raises" interrupt
- CPU finish instruction
- Save critical state: PC, regs, stack pointer, flags, CCR (condition code
regs), usually pushed to stack.
- Jump to exception handler, Vector Table (a list of addresses of functions
and devices)
- ISR = Interrupt Service Routine, part of device driver
- Invoked in supervisor mode, hence we want ISR to be "really short and sweet"
- Save registers (writing `asm` in C...)
- Read status or data, save them to buffer
- RTI = Return from the Interrupt (unlike in C, RTS = Return from the
Subroutine)