# The floppy: IWM, the 3.5" drives, speed, format, images The hardest part to get exact, as the Disk II is for the Apple II (read [apple2-python's disk-ii.md](../../apple2-python/docs/disk-ii.md): the IWM is the Disk II's logic state sequencer folded into one chip). Primary sources: Apple's IWM Device Specification rev. 19 (IWMSPEC, 1982; the DigiBarn scan, read by OCR: see the licence note in [sources.md](sources.md)), the IWM undocumented-features memo (IWMUNDOC, 1984), IM3's "Disk Interface" (III-33..36), the 400K drive specification 699-0285-A (DRV400), the 800K ERS 699-0321 (DRV800), the drive command and status code table (DRVCMD, a later community-style document, unsigned). ## The IWM - **Pins and clocks**: 28-pin DIP; PHASE0-3, A0-A3, /DEV, D0-D7, FCLK (7 or 8 MHz), Q3 (2 MHz, synchronous mode only), RDDATA, SENSE, WRDATA, /WRREQ, /ENBL1, /ENBL2, /RESET (DOC: IWMSPEC pp. 1, 5-6). On the Mac FCLK = C8M, 7.8336 MHz; on the SE C16M with the "divide by two" bit set by software (DOC: GMFH ch. 14). On the Mac the PHASE lines are the drive's CA0, CA1, CA2 and LSTRB (DOC: IM3 III-34). - **State register**: eight write-only state bits addressed by A3-A1, each set to A0 by an access: 0-3 PHASE0-3, 4 Motor-On, 5 Drive-Sel (/ENBL1 or /ENBL2), 6 L6 (Q6), 7 L7 (Q7); all reset by /RESET (DOC: IWMSPEC p. 7). On the Mac A3-A0 come from address lines A12-A9, so register n is at $DFE1FF + 512 n; even n turns a line off, odd turns it on (DOC: IM3 III-44..45 names them ph0L..q7H). - **Register selected by L7, L6, Motor-On**, and **by the new state if the access changes it**: L7=0 L6=0 Motor-On=0 "read all ones", L7=0 L6=0 Motor-On=1 read the data register, L7=0 L6=1 read status, L7=1 L6=0 read the write-handshake register, L7=1 L6=1 Motor-On=0 write the mode register, L7=1 L6=1 Motor-On=1 write the data register. "Registers are read during any operation in which A0 is a zero"; written when L6 and L7 are both (being) set and A0 is 1 (DOC: IWMSPEC p. 7). IWMUNDOC adds: a read must come from a bit-clearing address, a write go to a bit-setting one, and reading status from any address but L6's clear location fails (DOC: IWMUNDOC). - **Mode register** (write only, reset to 0): bit 0 latch mode, 1 asynchronous handshake, 2 one-second timer **disable**, 3 fast mode (2 µs cells), 4 8 MHz clock, 5 test mode, 6 MZ-reset, 7 reserved (DOC: IWMSPEC p. 8). Arithmetic (ours): in 8M fast mode a bit cell is 16 FCLK periods; with FCLK = 7.8336 MHz that is 2.042 µs, **489.6 kbit/s, exactly DRV400's "speed 1"**; so the Mac must set fast and 8M (bits 3 and 4), and the commonly quoted mode value $1F (latch, asynchronous, timer disabled, fast, 8M) fits. OBSERVED (MAME 0.285 `macplus`, cycle 5,740,341 onwards): the ROM reads $DFF1FF (motor off), $DFFBFF (Q6 on), $DFFDFF (Q7 off: status $80), writes **$1F** at $DFFFFF (Q7 on: the mode register, Motor-On off), and reads status back as **$9F** (SENSE high, mode $1F). - **Status register**: bits 0-4 the mode bits, 5 = an enable is active, 6 MZ, 7 = SENSE (DOC: IWMSPEC p. 9). The Mac reads drive registers through SENSE in bit 7 (DOC: IM3 III-35). **Handshake register**: 7 = write buffer ready, 6 = write state (0 after an underrun), 0-5 read as ones (ibid.). - **One-second timer**: with mode bit 2 clear, the selected enable stays low for 2^23 ±100 FCLK periods after Motor-On is cleared (8,388,608: 1.07 s at 7.8336 MHz, "about 1 second"); with latch mode set the count is not guaranteed (DOC: IWMSPEC p. 8, read from the page image). - **Reading**: falling transitions are synchronised to CLK and discriminated into bit cells by a digital one-shot; each transition restarts the windows. Clock periods between falling transitions: 7M modes 7-20 gives "1", 21-34 "01", 35-48 "001"; 8M modes 8-23, 24-39, 40-55; counted in FCLK periods in fast mode and FCLK/2 in slow mode (fast 8M: 1.0-2.875, 3.0-4.75, 5.0-6.875 µs at 8 MHz); beyond the last row the table is silent (U12) (DOC: IWMSPEC p. 4, read from the page image). Bits shift in at the LSB; a nibble is complete when a 1 reaches the MSB; it is latched into the data register and the shift register cleared (DOC: IWMSPEC pp. 3-4). In asynchronous mode the data register is cleared 14 FCLK periods after a valid read (MSB set, /DEV low for at least one FCLK) (ibid.; OCR-damaged on p. 3, to read from the page image). Latch mode keeps the MSB latched during /DEV low. - **Writing**: the write state starts on the transition from write-protect-sense (L6=1, L7=0) to write-load; in asynchronous mode the shift register is loaded every 8 bit cells starting seven CLK periods after the write state begins; an underrun (no data written in time) disables /WRREQ and clears the handshake's bit 6 until L7 is cleared (DOC: IWMSPEC pp. 2-3). For 9 FCLK periods after a shift-register load, writes to the write latch are ignored (DOC: IWMUNDOC: found on the Liron card; a 6502 indexed store's false read loaded garbage). - **Bit cell** 4 µs (slow) or 2 µs (fast); "in 7M and slow mode the bit cell time will be 28 FCLK clock input periods", 8M slow 32, 8M fast 16 (DOC: IWMSPEC p. 2). ## The drive interface The Mac addresses the drive's internal registers with CA2, CA1, CA0 (IWM PHASE2-0) and SEL (VIA PA5, vHeadSel), reads the selected one through SENSE (status bit 7, with L6 set, L7 clear), and writes one by setting CA0=CA1=1, SEL=0, then CA1/CA0 to the register and CA2 to the value, and pulsing LSTRB for 1 µs to 1 ms (DOC: IM3 III-34..35). | CA2 CA1 CA0 SEL | Read | | CA1 CA0 SEL (write, CA2 = value) | Write | | --- | --- | --- | --- | --- | | 0000 | DIRTN (step direction) | | 000 | DIRTN: 0 inwards (to 79), 1 outwards | | 0001 | CSTIN (0 = disk in place) | | 010 | STEP: 0 steps one track; reads back 1 after ~12 ms | | 0010 | STEP | | 100 | MOTORON: 0 on, 1 off (only with a disk in place) | | 0011 | WRTPRT (0 = locked) | | 110 | EJECT: 1 ejects; hold LSTRB at least 1/2 s | | 0100 | MOTORON | | | | | 0101 | TK0 (0 at track 0, valid 12 ms after the step) | | | | | 0111 | TACH (60 pulses a revolution) | | | | | 1000 / 1001 | RDDATA0 / RDDATA1 (lower / upper head) | | | | | 1100 | SIDES (1 = double-sided) | | | | | 1111 | DRVIN (0 = drive connected; floats to 1) | | | | (DOC: IM3 III-35..36.) DRVCMD lists the same codes with SEL first and more of them (eject latch, ready, revised, MFM-mode, from the SWIM3 driver's names); its own text hedges ("apparently", "it looks like"): tier 3-5, a detector for the 800K/SuperDrive extras. ## The drives and speed | Drive | Speed | Sectors per track (zones of 16 tracks) | | --- | --- | --- | | 400K single-sided (Sony OA-D34V based; 128K, 512K, external) | set by the Mac's PWM: continuously variable 390-605 rpm; 394/429/472/525/590 rpm (speed 1, 489.6 kbit/s) or 402/438/482/536/603 (speed 2, 500 kbit/s) | 12, 11, 10, 9, 8 | | 800K double-sided (512Ke, Plus, SE) | its own: 394, 429, 472, 525, 590 rpm | the same, both sides | (DOC: DRV400 sector-format appendix and §2.17; DRV800 speed table; IM4 IV-251.) 80 tracks; 800 blocks a side; double-sided blocks interleave by cylinder (blocks 0-11 side 0 track 0, 12-23 side 1 track 0); 2:1 sector interleave (DOC: DRV400). TACH gives 60 pulses a revolution; the speed changes take up to 150 ms to settle (DOC: DRV400). The 400K drive's speed is the closed loop described in [video-and-sound.md](video-and-sound.md). ## The format (GCR 6-and-2) - Header: sync (self-sync FF patterns), `D5 AA 96`, track (low 6 bits), sector, side (bit 5 = side, bit 0 = track bit 6), format (bit 5 = double-sided, bits 0-4 interleave, 2 for standard), checksum (XOR of the four), `DE AA`, pad (DOC: DRV400). - Data: `D5 AA AD`, sector, 524 bytes (12 tag + 512) encoded into 699 code bytes with a running 3-byte checksum (rotate CSUMC, add with carry across the three, XOR the data bytes), 4 checksum code bytes, `DE AA`, pad; 710 bytes in all (DOC: DRV400; tashnotes `gcr_checksum.md` restates it, CC BY-SA, cited not copied). - The **sector images** (raw 400K/800K, Disk Copy 4.2) hold only the 512-byte sectors (DC42 can add the 12-byte tags): the board must synthesise a track (gaps, sync, the encoding) for them, as apple2-python does for DSK. Only **MOOF** (Applesauce: bit streams or flux per track, 2 µs cells) carries what a protected disk needs (DOC: MOOF; CiderPress II notes). ## The image formats | Format | Holds | Board use | | --- | --- | --- | | raw 409,600 / 819,200 bytes (".dsk", ".img", "DiskDup") | sectors, no tags | synthesise tracks; MFS (sig $D2D7) or HFS ($4244) at byte 1024 | | Disk Copy 4.2 (84-byte header, data and tag checksums) | sectors + optional tags, the format byte (0 = 400K, 1 = 800K) | the same, with tags (CiderPress II `DiskCopy-notes.md`) | | DART | compressed DC images | later | | MOOF 1.0 | per track bit stream (TRKS) and optional FLUX, INFO with disk type (1 SSDD 400K, 2 DSDD 800K, 3 DSHD) | the native format for a cycle-level drive | | A2R 2/3 (Applesauce raw flux) | captures | later (via a converter) | ## The design (proposed) The IWM steps per FCLK while an enable is low (and catches up otherwise); the drive turns a MOOF bit stream, or a synthesised one, under the head at the current rpm, giving a flux transition at each 1; RDDATA pulses go to the IWM's synchroniser. Weak-bit and no-flux behaviour follows the Applesauce/MOOF notes as a named, seeded model (as apple2-python proposes for the MC3470), tagged HYPOTHESIS. ## Oracles MAME's `macplus` with an 800K raw image, logging IWM reads at $DFE1FF + 512 n, is the first detector; Snow (MIT) and Clock Signal (MIT) are readable detectors for the IWM and the drive (their `swim/iwm.rs`, `drive.rs`, `IWM.cpp`, `MacintoshDoubleDensityDrive.cpp`, `DriveSpeedAccumulator.cpp`). No hardware-tier capture of an IWM is in hand; the protection corpus would be MOOF images (none in the census: open decision 7).