# The 8253 PIT, the 8259A PIC, the 8237A DMA controller and DRAM refresh Source names as in [cpu-8088.md](cpu-8088.md); **T** = Tandy 1000 SX Technical Reference; **JR** = PCjr Technical Reference. OBSERVED values are from the MAME recipe ([mame-oracle.md](mame-oracle.md)), 5150 BIOS rev 3 (10/27/82). ## The 8253-5 PIT (ports 40h-43h) | Channel | Clock | Gate | Output | Use | Programmed by the BIOS | | --- | --- | --- | --- | --- | --- | | 0 | 1.19318 MHz | tied high (not found in the text; HYPOTHESIS, U7: the netlist decides) | IRQ0 | time of day, 18.2 Hz | `36h` (ch0, LSB+MSB, mode 3), count 0 = 65536 (A81 p.201 / A-11; OBSERVED at 3.248 s) | | 1 | 1.19318 MHz | as ch0 | DRQ0 of the 8237 | DRAM refresh request | `54h` (ch1, LSB only, mode 2), count 18 (A81 p.197 / A-7; OBSERVED `43h<-54h`, `41h<-12h` at 0.0569 s) | | 2 | 1.19318 MHz | PPI PB0 | ANDed with PPI PB1 to the speaker driver; read back on PPI PC5; cassette write | speaker, cassette | `B6h`, count 533h for the 1 kHz beep (A81 p.208 / A-18) | - **Contradiction C1:** the Aug 1981 prose swaps channels 0 and 1 ("Channel 0 is used to time and request refresh cycles", A81 p.20 / 2-4); A83 p.16-17 / 1-4-1-5 and XT p.16-17 correct it, and the 1981 BIOS listing itself programs channel 1 for refresh. Follow the BIOS and A83. - Refresh arithmetic (DOC + arithmetic): 18 PIT ticks x 4 CPU clocks = **72 CPU clocks = 15.09 us** between refresh requests, matching "once every 72 clocks or approximately 15" (A81 p.24 / 2-8, whose "m sec" is a typo for us). The time-of-day tick: 65536 / 1,193,182 = 54.925 ms (OBSERVED: EOIs to port 20h every 0.0549242-0.0549265 s in MAME). - Modes (I83 8253 sheet, PDF p.672-673 / 6-142-6-143): mode 0 output low after the mode set, high at terminal count; mode 1 retriggerable one-shot; mode 2 rate generator, output low for one input clock, a reload "between output pulses" takes effect on the next period; mode 3 square wave, odd counts give "(N + 1)/2 counts" high and "(N - 1)/2" low; mode 4 software strobe; mode 5 hardware strobe. Gate summary in Figure 6 (PDF p.673). Writing a counter during counting: "Write 1st byte stops the current counting. Write 2nd byte starts the new count" (mode 0, PDF p.672). The 8253 has no read-back command (the 8254's, PDF p.685, is an 8254 addition). - **What the data sheet does not settle** (U1): the exact clock on which a written count is loaded, the counter's value read in the clock after loading, the mode 3 output phase after a mid-period reload, and the 8253-vs-8254 differences in these. The PC's sample players and tracker routines (PWM through ch2, ch0 reprogrammed at audio rates) depend on them. reenigne's `8088/8253/` and `8088/pit/` hold test programs written for the real XT; their results are not in the tree. This is a tier-1 gap to close with xtserver-style runs (open decision 7). ## The 8259A PIC (ports 20h-21h) - IRQs (A83 p.19 / 1-11; XT p.21 / 1-9): 0 timer, 1 keyboard, 2 reserved (CGA vertical retrace can be jumpered, A81 p.76 / 2-60), 3 secondary serial, 4 primary serial, 5 fixed disk (XT), 6 diskette, 7 printer. NMI is separate (below). - BIOS initialisation (A81 p.198 / A-8; OBSERVED at 2.736 s): ICW1 `13h` "EDGE, SNGL, ICW4", ICW2 `08h` "INT TYPE 8 (8-F)", ICW4 `09h` "BUFFRD,8086 MODE" (buffered mode, slave: the `-SP/-EN` pin drives the data transceiver, circuit_5150.txt sheet 1). EOI is `20h` to port 20h. - Edge triggered: "An Interrupt Request is generated by raising an IRQ line (Low to High) and holding it high until it is acknowledged" (A81 p.27 / 2-11). - Data sheet rules the board must keep (I83 8259A sheet): the two-INTA 8086 sequence (PDF p.148-149); with no request present at the acknowledge "the 8259A will issue an interrupt level 7" (PDF p.148): the spurious IRQ7 that real PCs show when a request line drops early; OCW3 IRR/ISR reads and the poll command (PDF p.156); special mask, rotation, auto-EOI (PDF p.150-156). - **Second family user:** the Irem M72 board's uPD71059C (8259-compatible, i8086-python `docs/timing.md`), queued as `irem-python` in batch 4. See [reuse.md](reuse.md). ## The 8237A DMA controller (ports 00h-0Fh, page registers 80h-83h) - Part: "DMA CHIP 8237-2" (A81 p.39 / 2-23); the XT lists 8237A-5 (XT p.20 / 1-8). MAME names the part AM9517A at 4.772727 MHz (OBSERVED, `-listxml`); IBM's manuals do not state its clock (U2; the schematic decides). - Channels: 0 refresh (DACK0 "to refresh system dynamic memory", A81 p.28 / 2-12); 1 free (SDLC adapter, A83 p.284); 2 diskette; 3 fixed disk on the XT (A83 p.209 / 1-201). Only DRQ1-3 reach the slots. - Page registers: the BIOS writes 81h for channel 2 and 82h for channel 3, and initialises 83h (A81 p.196 / A-6, p.228-229 / A-38-39). IBM gives no table; Tandy's says 81 = ch2, 82 = ch3, 83 = "Ch 0 - 1", 80 unused (T p.50-51). Channel 0 has no page register of its own on the 5150 (HYPOTHESIS from the above; U17: the netlist decides). - BIOS refresh setup (A81 p.196-197 / A-6-A-7; OBSERVED): command `04h` (disable), master clear, ch0 count FFFFh, mode `58h` = "CH 0, READ, AUTOINIT" (single transfer), command `00h`, unmask ch0; modes 41h, 42h, 43h for channels 1-3. - States (I83 8237A sheet, PDF p.114 / 2-90): SI idle, S0 hold requested, S1-S4 the working states, Sw waits between S2 or S3 and S4 by READY; memory-to-memory uses S11-S14 and S21-S24. Compressed timing removes S3 (PDF p.116). The IBM BIOS never sets memory-to-memory or compressed timing (its command byte is 00h). ## DRAM refresh: the timing corner - Mechanism (DOC, cpu-8088.md): PIT ch1 pulses DRQ0 every 72 CPU clocks; the 8237 requests the bus; the board's DMA-wait logic waits for the CPU's bus to be passive and unlocked, then holds the CPU's next bus cycle in wait states (`-DMA WAIT` into the 8284A RDY1) while the 8237 runs a memory-read cycle with no I/O target, which refreshes a DRAM row. - **Contradiction C2: four or five clocks per refresh.** A81 p.20 / 2-4: refresh cycles "take four clocks or 840 ns"; A81 p.24 / 2-8: "require five clocks"; A83 p.25 / 1-17 and XT p.26 / 1-14: "four clocks". The CPU sees more than the 8237's own states: the hand-off (S0 until the CPU is passive), the 8237's S1-S4 and the return all cost the CPU, and the cost depends on where in the CPU's own bus activity the request lands. That dependence is why refresh makes the PC's cycle counts look irregular and why demos such as 8088 MPH reprogram or synchronise it. The judge is hardware: XTCE's test cases ran on a real 5160 with refresh period and phase as parameters (cpu-8088.md), and SingleStepTests runs without refresh. - "approximately 7% of the bus bandwidth" (A81 p.24 / 2-8): 5 / 72 = 6.9 %, so that sentence is computed with five clocks. - On the PCjr there is no 8237 at all: "Memory refresh is provided by the 6845 CRT Controller and gate array" (JR p.43 / 2-17), and the CPU gets "an average of two wait states" on RAM because video shares it (JR p.39 / 2-13). The Tandy 1000 has a custom chip "equivalent of an 8237A-5" that also refreshes (T p.33 / 24). ## NMI (port A0h) Write 80h to A0h to enable NMI, 00h to disable; masked at power-on (A81 p.39 / 2-23). Sources: memory parity (PPI PC7), I/O channel check (PC6), the 8087 (A83 p.43-44). The BIOS handler prints PARITY CHECK 1 or 2 and halts (A81 p.200 / A-10). On the PCjr the NMI is the keyboard's (ppi-keyboard.md).