Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The Integrated Woz Machine, as far as its registers go.//!//! The chip an Apple IIgs has where a IIe had a Disk II card. It answers at the//! same sixteen addresses a card in slot six would, `$c0e0` to `$c0ef`, and the//! low four bits of the address are the whole of its interface: eight of them//! are the four stepper phases, two the motor, two the drive select, and the//! last four are two flip-flops called Q6 and Q7 which decide *which* of the//! chip's registers the next access reaches.//!//! Those two bits and the motor make four states, which Table E-15 of the//! Hardware Reference sets out (Apple Computer, Inc., 1989a)://!//! | Q7 | Q6 | motor | what an access reaches |//! | -- | -- | ----- | ---------------------- |//! | 0 | 0 | on | the data register |//! | 0 | 1 | any | the status register |//! | 1 | 0 | any | the handshake register |//! | 1 | 1 | off | the mode register |//!//! What is on the end of the cable is either kind of drive, and the Disk//! Interface register at `$c031` says which. That one bit changes what the//! sixteen addresses above mean: on a 5.25-inch drive the four phase lines are//! magnets and walk a head, and on a 3.5-inch drive they are an address and a//! strobe reaching into a mechanism that steps its own head. So the chip is//! the same chip and the switches are the same switches, and this routes them//! to whichever set of drives the register has chosen. See `disk35.zig` for//! what happens at the far end.
const std = @import("std");
const disk2 = @import("disk2.zig");const disk35 = @import("disk35.zig");const unidisk_device = @import("unidisk.zig");
/// The chip's sixteen addresses and the little state behind them.pub const Iwm = struct { /// The drives, and whatever is in them. /// /// An IWM is a Disk II card's mechanism on one chip: the same four /// magnets, the same spindle motor, the same two drives, and the same /// six-and-two encoded bytes going past the head. So these are the drives /// already modelled for the card rather than a second set of them — what /// the IWM adds is three registers a card never had, and those are below. drives: disk2.Controller = .{},
/// And the other kind, which is nothing like the same mechanism: two of /// them, sharing the same four lines and the same enable and select /// latches, and reached only when the Disk Interface register says so. disks35: disk35.Controller = .{},
/// An intelligent drive on the port, if one is plugged in. It is not a /// mechanism like the two above but a correspondent: the firmware sends it /// packets and it sends packets back. See `unidisk.zig`. /// /// It answers only when the second connector is the one chosen, which is /// where the firmware looks for it — `LDA $c08b,X` to choose it and `LDA /// $c089,X` to start the motor before it polls for the sense line at all /// (bank 1 `$c88e`). So a machine with nothing plugged in behaves exactly /// as it did. unidisk: unidisk_device.Chain = .{},
/// How the port's two selects reach the two 3.5-inch drives. /// /// On a IIgs each select is a drive and there is nothing to say. A IIc /// Plus puts its glue chip in between: the IWM's drive 2 select is an /// *input* to that chip, "routed to ENB2* when external drives are /// selected (W $CCC0), to INTEN* when internal drive is selected (W /// $CC80)" (MG, 2018). So which drive the second select reaches is a /// latch, and the firmware moves it — two hundred and seventy-nine writes /// to `$ccc0` over a boot of the drive on the back, and not one over a /// boot of the drive in the case. /// /// That is also the whole of what the drive select being "the wrong way /// about" was: the drive in the case is the one this port selects second /// because the chip sends the second select to it, not because anything is /// reversed. routing: enum { /// Each select is its own drive, which is every machine but a IIc Plus. direct, /// `INTEN*` asserted: the second select is the drive in the case, and /// the first goes out to the port. internal_on_two, /// `INTEN*` de-asserted: the chip sends the second select out to the /// port as well, so neither select reaches the drive in the case. /// /// A real port has two external drives here, one per select; there is /// one place for an external 3.5-inch drive in this model, so both /// selects find it. That is a simplification and not the hardware. external_only, } = .direct,
/// The enable latch, which is one latch on the chip whichever kind of /// drive it reaches. On a 5.25 it is the motor; on a 3.5 it is the light /// and the disk held in place, and the spindle is a separate command. enabled: bool = false,
/// Which of the two connectors is listening. selected: u1 = 0,
/// The four phase lines as they stand, kept here rather than only in /// whichever set of drives is listening, because there is a third thing on /// the port that reads them and it is neither. /// /// An intelligent drive is switched on by a *combination* of these rather /// than by a latch of its own: "Two functions are strictly hardware /// invoked: bus reset and bus enable. Both of these conditions are invoked /// through combinations of phase lines on the disk port that never occur /// under normal Disk II operation" -- enable being phase 3 and phase 1 /// both up, with phase 0 free because it "is used as a REQ handshake line /// cycled on a packet basis when the bus is enabled" (Apple Computer, /// Inc., 1987a, p. 157). lines: [4]bool = @splat(false),
/// The two flip-flops that choose the register. q6: bool = false, q7: bool = false,
/// The mode register: write-only, and only its low five bits mean /// anything. /// /// Bit 4 is the read-clock speed, bit 3 the bit-cell size — two /// microseconds for a 3.5-inch drive and four for a 5.25 — bit 2 the /// one-second timer that keeps a drive spinning after it is deselected, /// bit 1 the handshake protocol, and bit 0 whether a read byte stays valid /// for a whole byte time or about seven microseconds. Bits 5 and 6 are to /// be written as zero and bit 7 is reserved. mode: u8 = 0,
/// When the last byte was handed over to be written, which is what the /// handshake register counts from: the chip is ready for another once that /// one has had time to go down onto the disk. wrote_at: ?u64 = null,
/// Bit 6 of the Disk Interface register: which *kind* of drive the port is /// talking to. Set for a 3.5-inch drive and clear for a 5.25. /// /// This is not one of the chip's own registers — it is a register of the /// machine, at `$c031` rather than in the `$c0ex` block — but it belongs /// with the disk port because it is the disk port it configures, and /// because the sixteen switches above mean different things depending on /// it. On a 5.25 the four phases step a head; on a 3.5 they are an address /// and a strobe. /// /// Which bit this is was settled from the ROM, because the two manuals are /// wrong about it. The Hardware Reference (Apple Computer, Inc., 1989a) /// calls bit 7 "read/write head select" and bit 6 "disk drive select", and /// the Firmware Reference (Apple Computer, Inc., 1987a) agrees; Neil /// Parker's note on driving these drives (Parker, 1994) says flatly that /// both are mistaken. The firmware itself decides it. Before it touches a /// 3.5-inch drive at all it does `LDA #$40 / STA $C031`, which is bit 6 and /// not bit 7 — and its routine for naming one of the drive's registers /// reads `$c031`, masks with `$7f`, conditionally sets `$80` and writes it /// back, once per register selection. Bit 7 is not a head select. It is one /// of the four bits that name the register, and choosing a head is one of /// the things that can be named with it, which is how the manual came to /// describe a part of it as the whole. /// /// Bit 7 of the same register is that SEL line. It is kept with the drives /// it reaches, in `disks35.sel`, rather than here, because it means /// nothing to the chip. three_and_a_half: bool = false,
/// Throws the switch an address names, and answers with whatever that /// address reads. /// /// Every access throws its switch, read or write alike, which is how this /// family of interfaces has always worked: the address *is* the /// instruction. pub fn access(self: *Iwm, which: u4, writing: bool, value: u8, cycles: u64) u8 { switch (which) { // The four lines, released at the even address and energised at // the odd one. On a 5.25-inch drive walking them in order moves // the head; on a 3.5 three of them and SEL name a register in the // drive and the fourth strobes it. 0x0...0x7 => { const magnet: u2 = @truncate(which >> 1); const on = which & 1 == 1; self.lines[magnet] = on; // And the other combination, which is the bus reset: phase 0 // and phase 2 up with the other two down. It puts every // resident back to waiting for a packet, and the firmware uses // it before it hands out unit numbers. if (self.busReset()) self.unidisk.reset(); // Phase zero is REQ when there is an intelligent drive // listening, which is the host's half of the handshake. The // line is set above before this is asked, so that the access // that raises phase 1 or phase 3 is itself the one that // switches the bus on. if (self.speaking() and magnet == 0) self.unidisk.request(on); if (self.three_and_a_half) self.disks35.line(magnet, on, cycles) else self.drives.phase(magnet, on); }, 0x8 => self.enable(false, cycles), 0x9 => self.enable(true, cycles), 0xa => self.choose(0), 0xb => self.choose(1), 0xc => self.q6 = false, 0xd => self.q6 = true, 0xe => self.q7 = false, 0xf => self.q7 = true, }
// And then whichever register the state now selects. With both flip // flops set, a write reaches the mode register if the drive is off and // the disk if it is on — the manual is explicit that a write to the // mode register succeeds only once the one-second timer has run out. if (writing) { if (self.q6 and self.q7) { if (self.enabled) self.writeNibble(cycles, value) else self.mode = value; } return 0; } if (self.q7) return self.handshake(cycles); if (self.q6) return self.status(cycles); return self.data(cycles); }
/// Bit 2 of the mode register: whether a deselected drive keeps turning /// for a second. const one_second_timer: u8 = 0x04;
/// The enable latch. It is one latch, and which set of drives it reaches /// is what the Disk Interface register decides. fn enable(self: *Iwm, on: bool, cycles: u64) void { self.enabled = on; if (self.three_and_a_half) { self.disks35.enabled = on; } else { // Whether a 5.25-inch drive keeps turning for a second after it is // deselected is the machine's decision here rather than the // drive's: a Disk II card has that second soldered on, and a IIgs // puts it under software control. The Hardware Reference on bit 2 // of the mode register (Apple Computer, Inc., 1989a): "One-second // timer is enabled. When the current disk drive is deselected, the // drive will remain enabled for 1 second if this bit is set." self.drives.setMotor(on, self.mode & one_second_timer != 0, cycles); } }
/// Which of the two connectors is listening. A Disk II card always has two /// whether or not there is anything on the second; a 3.5-inch drive says /// for itself whether it is there, which is what register 15 is for. fn choose(self: *Iwm, which: u1) void { self.selected = which; self.disks35.selected = switch (self.routing) { .direct => which, .internal_on_two => ~which, .external_only => 1, }; self.drives.selected = if (which == 1 and self.drives.fitted <= 1) 0 else which; }
/// What the status register reads. /// /// Its low five bits are the mode register's low five, which is the whole /// point of it being readable at all: the mode register is write-only, and /// this is how a program finds out what it wrote. The machine's self-test /// does exactly that — writes a value, reads it back, and compares the low /// five bits. pub fn status(self: *Iwm, cycles: u64) u8 { var value: u8 = self.mode & 0x1f; if (self.enabled) value |= 0x20; if (self.sense(cycles)) value |= 0x80; return value; }
/// The sense line coming back from the drive, which is bit 6. /// /// The Hardware Reference (Apple Computer, Inc., 1989a) declines to say /// what it means: "multifunction input; use determined by disk device. /// (Used as a write-protect sense in some Apple disk drives.)" So the chip /// does not know either — it carries a line and whatever is on the end of /// it decides what that line is saying, and which of several things it is /// saying depends on which of the four phases are asserted when it is read. /// On the 5.25-inch drives modelled here it is the write-protect tab, and /// an empty drive reads as protected because there is nothing in it to /// write to. /// Whether the port is talking to the intelligent drive rather than to a /// mechanism. /// /// Three things have to be true, and the middle one is what keeps a /// machine with drives of its own from being deafened by a drive on the /// end of the cable. /// /// The first is that something is plugged in. The second is the bus /// enable, which is a combination of the phase lines and not a latch: /// phase 3 and phase 1 both up (Apple Computer, Inc., 1987a, p. 157, /// tabulated as `PH3 = 1, PH2 = X, PH1 = 1, PH0 = X`). Apple chose a /// combination that "never occur[s] under normal Disk II operation" /// precisely so that "a Disk II device and other bus residents [] stay out /// of each other's way" -- energising two opposing magnets at once being /// pointless on a drive whose head they move. /// /// The third is that the port is not addressing a 3.5-inch mechanism, on /// pin 4 of the connector -- "3.5- or 5.25-inch drive select" (Apple /// Computer, Inc., 1989a, Table 7-1, p. 152), which is bit 6 of `$c031`. /// An intelligent drive is neither of the two mechanisms that line chooses /// between, and both machines here put it down before they speak: every /// bus enable either of them raises has that bit clear. It is kept as a /// condition because a dumb 3.5-inch drive *is* addressed with phase 1 and /// phase 3 -- they are two of the four bits that name one of its registers /// -- so without it a machine with drives of its own would have two things /// answering at once. A real chain is spared that by Apple's ordering rule /// instead: "Apple 3.5 drives first, followed by UniDisk 3.5 drives, /// followed by Disk II drives" (Apple Computer, Inc., 1987a, Figure 6-1, /// p. 110), each drive passing on an enable the one before it did not use. /// /// What is *not* a condition, and was for a while, is which of the two /// enable lines is down. The book says one: "The Disk II device is enabled /// by the disk port signal /ENABLE2. The SmartPort must activate the /// /ENABLE2 line to communicate with intelligent bus residents" (p. 158). /// The machines say otherwise, and they disagree with each other. An Apple /// IIc does use `DR2` -- `LDA $c08b,X` and then `LDA $c089,X` (342-0033-A, /// bank 1, `$c88e`) -- which is what a machine with its own drive on `DR1` /// would do. An Apple IIgs, traced through its own startup, selects `DR2` /// to look for an Apple 3.5 drive, then goes back to **`DR1`** with `LDA /// $c0ea` before it raises phase 1 and phase 3 and starts the handshake. /// So the enable line is whichever one the host has spare, and it is the /// phase combination that arbitrates -- which is what the same page /// describes from the other side, each resident gating the enable onward /// "so that whenever any SmartPort bus resident is enabled (PHASE1 and /// PHASE3), any Disk II at the end of the chain is disabled". inline fn speaking(self: *const Iwm) bool { return self.unidisk.fitted() and !self.three_and_a_half and self.busEnabled(); }
/// The bus enable: phase 3 and phase 1 both up. inline fn busEnabled(self: *const Iwm) bool { return self.lines[1] and self.lines[3]; }
/// The other combination the same table gives: `PH3 = 0, PH2 = 1, PH1 = 0, /// PH0 = 1` is a bus reset (Apple Computer, Inc., 1987a, p. 157). /// /// Both of these were read back off the machine as well as out of the /// book, and the two agree. An Apple IIc's own firmware keeps three /// routines side by side, each preceded by `LDX #$60` so that the indexed /// addresses land on the port: `LDA $c081,X / LDA $c085,X` with a delay /// either side of it is the reset, `LDA $c083,X / LDA $c087,X` is the /// enable, and `LDA $c080,X / $c082,X / $c084,X / $c086,X` puts all four /// down again (342-0033-A, bank 1, `$ca60`, `$ca80` and `$ca8a`). /// /// A Disk II is not troubled by either, which is the point of choosing /// them: both energise opposing magnets, which moves no head anywhere, so /// no ordinary seek ever asks for one. inline fn busReset(self: *const Iwm) bool { return self.lines[0] and self.lines[2] and !self.lines[1] and !self.lines[3]; }
pub fn sense(self: *Iwm, cycles: u64) bool { if (self.speaking()) return self.unidisk.ready(); if (self.three_and_a_half) return self.disks35.sense(cycles); return self.drives.writeProtected(); }
/// What the handshake register reads: whether the chip will take another /// byte to write, and whether the last one was given to it in time. /// /// Bit 7 is the ready bit, and it says whether the *next* byte can be /// handed over yet. That is not the same as whether the chip is there. /// This returned a flat `$80` for a long while, on the reasoning that a /// chip with nothing to do is ready by definition — which is right for the /// first byte and wrong for every one after it. A byte takes as long to go /// down onto the disk as it takes to pass the head, and until it has, the /// chip is not ready for another. /// /// Saying otherwise is not a small lie. The firmware's track-writing loop /// is six instructions — `LDA $0f5c,y`, `BIT $c0ec`, `BPL` back to it, /// `STA $c0ed`, `DEY`, `BPL` — and the `BPL` is the whole of its timing: /// it hands over a byte and then waits here until the chip asks for /// another. With the answer always yes it never waits, and it writes a /// byte every twenty-five cycles onto a disk that turns one every /// forty-eight. Formatting laid down about two bytes for every place there /// was room for one, and the verify that follows found what you would /// expect. Reading and writing whole sectors were unaffected, because /// neither of them asks. /// /// The first byte after a quiet spell goes straight down: an idle chip /// really is ready, and the counting starts from the byte before. /// /// Bit 6 is the underrun flag, and it is clear. A real one sets it when /// the processor was too slow to keep the head fed and the track is /// spoiled; nothing here is ever too slow, because this is what makes it /// wait. /// /// The scan of the manual's figure for this register is too mangled to say /// which way round its labels go, so the firmware settled both bits, and /// it took two of its loops to do it. One shifts this register left and /// loops while the carry is clear, which is a machine waiting for bit 7 to /// come *up*. The other reads it, masks with $40 and loops while the /// result is non-zero, which is a machine waiting for bit 6 to go *down*. /// A register that satisfies both at once reads $80, so bit 6 set must /// mean the underrun happened rather than that it did not — which is the /// opposite of what this first said. pub fn handshake(self: *const Iwm, cycles: u64) u8 { const byte_time = if (self.three_and_a_half) self.disks35.turning else self.drives.turning; const last = self.wrote_at orelse return 0x80; if (cycles -| last < byte_time) return 0x00; return 0x80; }
/// What the data register reads: the byte going past the head. /// /// With nothing in the drive there is nothing going past and this reads as /// zero, which is not the same as the drive being broken — see the /// handshake above for why that distinction cost a while. pub fn data(self: *Iwm, cycles: u64) u8 { if (self.speaking()) return self.unidisk.read(); if (self.three_and_a_half) return self.disks35.readNibble(cycles); return self.drives.readNibble(cycles); }
/// And one byte going the other way, down onto the disk. pub fn writeNibble(self: *Iwm, cycles: u64, value: u8) void { self.wrote_at = cycles; if (self.speaking()) return self.unidisk.write(value); if (self.three_and_a_half) self.disks35.writeNibble(cycles, value) else self.drives.writeNibble(cycles, value); }
/// What the Disk Interface register at `$c031` reads. /// /// Only two bits, and the Hardware Reference is explicit about the rest /// (Apple Computer, Inc., 1989a): "when the Disk Interface register is /// read, 0's are returned in the unused positions (bits 5 through 0)". So /// this is one of the few registers in the machine where reading back /// something other than what was written is correct rather than a bug — /// which is also why the manual says to touch it only with a /// read-modify-write. pub fn diskRegister(self: *const Iwm) u8 { var value: u8 = 0; if (self.disks35.sel) value |= 0x80; if (self.three_and_a_half) value |= 0x40; return value; }
pub fn setDiskRegister(self: *Iwm, value: u8) void { self.disks35.sel = value & 0x80 != 0; self.three_and_a_half = value & 0x40 != 0; }
/// What the chip's own reset pin does, and nothing more. /// /// Everything cleared here is a register or an addressable latch *on the /// chip*: the mode register, the two state flip flops, the four phase /// lines, the drive select and the enable. Dropping the enable is what /// stops a turning drive, and it is dropped through `enable` rather than /// by assignment so that it reaches the mechanism the way the bus would. /// /// What is deliberately left alone is everything on the other side of the /// cable and everything on the other side of the machine. Where a head was /// left is mechanical. Whether the port is talking to a 3.5-inch drive is /// not the chip's to say — it is `$c031` on a IIgs and the glue chip's /// `3.5DRIVE*` on a IIc Plus, and neither of those is on this reset line. /// /// A IIc Plus reaches this on its own, without the machine being reset: /// `IWMRES*` is one of the glue chip's latched outputs and a write to /// `$cc40` pulses it. See `mig.zig`. pub fn resetChip(self: *Iwm, cycles: u64) void { // The mode register goes first, so that dropping the enable below is // not held up by a one-second timer this has just cleared. self.mode = 0; self.q6 = false; self.q7 = false; self.lines = @splat(false); self.choose(0); self.enable(false, cycles); }
/// What the machine's reset line does, which is the chip's own reset and /// then everything the chip is wired to. The drives go back, the Disk /// Interface register's kind-of-drive bit goes back with them, and what is /// in a drive stays in it. pub fn reset(self: *Iwm) void { self.resetChip(0); self.three_and_a_half = false; self.drives.reset(); self.disks35.reset(); self.unidisk.reset(); }};
const testing = std.testing;
test "the mode register reads back through the status register" { // The machine's own self-test, written out: with Q6 high, write a value to // an odd address and read it back from an even one, and the low five bits // must match. It is the only way to read a write-only register. var iwm: Iwm = .{}; _ = iwm.access(0x8, false, 0, 0); // motor off _ = iwm.access(0xd, false, 0, 0); // Q6 high
var pattern: u8 = 0; while (pattern < 0x20) : (pattern += 1) { _ = iwm.access(0xf, true, pattern, 0); // Q7 high: write the mode register const read_back = iwm.access(0xe, false, 0, 0); // Q7 low: read the status try testing.expectEqual(pattern, read_back & 0x1f); }}
test "the mode register cannot be written with the motor running" { var iwm: Iwm = .{}; _ = iwm.access(0xd, false, 0, 0); // Q6 high _ = iwm.access(0x9, false, 0, 0); // motor on _ = iwm.access(0xf, true, 0x1f, 0); try testing.expectEqual(@as(u8, 0), iwm.mode);
_ = iwm.access(0x8, false, 0, 0); // motor off _ = iwm.access(0xf, true, 0x1f, 0); try testing.expectEqual(@as(u8, 0x1f), iwm.mode);}
test "the address is the instruction" { // Every access throws its switch, read or write alike. var iwm: Iwm = .{}; const phases = &iwm.drives.drives[0].phases; try testing.expectEqualSlices(bool, &.{ false, false, false, false }, phases); _ = iwm.access(0x1, false, 0, 0); // phase 0 on _ = iwm.access(0x5, false, 0, 0); // phase 2 on try testing.expectEqualSlices(bool, &.{ true, false, true, false }, phases); _ = iwm.access(0x0, false, 0, 0); // and phase 0 off again try testing.expectEqualSlices(bool, &.{ false, false, true, false }, phases);
_ = iwm.access(0xb, false, 0, 0); try testing.expectEqual(@as(u1, 1), iwm.drives.selected); _ = iwm.access(0x9, false, 0, 0); try testing.expect(iwm.drives.motor); // And the enable shows in the status register, at bit 5. _ = iwm.access(0xd, false, 0, 0); try testing.expectEqual(@as(u8, 0x20), iwm.access(0xe, false, 0, 0) & 0x20);}
test "which register an access reaches is Q6 and Q7" { var iwm: Iwm = .{}; _ = iwm.access(0x8, false, 0, 0); // motor off _ = iwm.access(0xd, false, 0, 0); // Q6 high _ = iwm.access(0xf, true, 0x15, 0); // Q7 high, write the mode
// Q7 low, Q6 high: the status register, and the mode in its low bits. try testing.expectEqual(@as(u8, 0x15), iwm.access(0xe, false, 0, 0) & 0x1f); // Q7 high, Q6 low: the handshake. Ready, and nothing written late — a // chip with nothing to do is ready, and saying otherwise is a machine // whose disk port is broken rather than empty. _ = iwm.access(0xc, false, 0, 0); try testing.expectEqual(@as(u8, 0x80), iwm.access(0xf, false, 0, 0)); // Q7 low, Q6 low: the data register, and there is no disk. _ = iwm.access(0xe, false, 0, 0); try testing.expectEqual(@as(u8, 0x00), iwm.access(0xc, false, 0, 0));}
test "the handshake satisfies both of the firmware's waits at once" { // The two loops in the ROM are the only evidence for which way round these // two bits go, and between them they pin it. One shifts the register left // and loops while the carry is clear, so it wants bit 7 up. The other // masks with $40 and loops while the result is non-zero, so it wants bit 6 // down. Only one value ends both, and a machine answering anything else // hangs in one loop or the other — which is how both were found. var iwm: Iwm = .{}; _ = iwm.access(0xc, false, 0, 0); // Q6 low const value = iwm.access(0xf, false, 0, 0); // Q7 high: the handshake register
try testing.expectEqual(@as(u8, 0x80), value); try testing.expect(value & 0x80 != 0); // the ASL/BCC loop ends try testing.expect(value & 0x40 == 0); // the AND #$40/BNE loop ends}
test "the disk interface register has two bits and reads the rest as zero" { // "When the Disk Interface register is read, 0's are returned in the // unused positions (bits 5 through 0)" — so reading back something other // than what was written is right here, and is why the manual says to use a // read-modify-write on it. var iwm: Iwm = .{}; try testing.expectEqual(@as(u8, 0x00), iwm.diskRegister());
// Bit 6 is the one that chooses a 3.5-inch drive, which is the firmware's // answer rather than the manual's: `LDA #$40 / STA $C031` is what it does // before it touches one. iwm.setDiskRegister(0x40); try testing.expect(iwm.three_and_a_half); try testing.expect(!iwm.disks35.sel); try testing.expectEqual(@as(u8, 0x40), iwm.diskRegister());
iwm.setDiskRegister(0xff); try testing.expect(iwm.three_and_a_half); try testing.expect(iwm.disks35.sel); try testing.expectEqual(@as(u8, 0xc0), iwm.diskRegister());
// Both bits are cleared on reset, and a 5.25-inch drive is what a machine // comes up expecting. iwm.reset(); try testing.expect(!iwm.three_and_a_half); try testing.expectEqual(@as(u8, 0x00), iwm.diskRegister());}
test "the disk port reads what is going past the head" { // An IWM with a drive on the end of it is a Disk II's mechanism, so this // is the boot PROM's own loop in miniature: turn the motor on, select the // data register, and read until something recognisable goes by. var iwm: Iwm = .{}; const floppy = try testing.allocator.create(disk2.Floppy); defer testing.allocator.destroy(floppy); floppy.* = .{};
// A real disk rather than a track of filler, because filler is the one // thing a head cannot find its place in. A drive puts its head down in the // middle of a byte and stays there until a gap goes past: gaps are written // as self-sync, ten bits to the byte, and those extra bits are what push a // lost reader back into step. A track of plain $ff has no gaps in that // sense and nothing to lock on to. const image = try testing.allocator.alloc(u8, disk2.image_size); defer testing.allocator.free(image); @memset(image, 0); try floppy.load(image, .dos); iwm.drives.insert(0, floppy);
_ = iwm.access(0xe, false, 0, 0); // Q7 low _ = iwm.access(0xc, false, 0, 0); // Q6 low: the data register _ = iwm.access(0x9, false, 0, 0); // and the motor on
// A disk turns at the speed it turns at, so what the head is over depends // on the clock rather than on how often it is asked. This is the boot // PROM's own loop: read until the three bytes that mark the start of a // sector go by. The PROM polls every seven cycles and the register holds // a finished byte for about seven microseconds, so a byte is sometimes // read twice; the PROM's compare-and-restart shrugs that off, and so // does collapsing a byte read twice into one here, since no two of the // three marks are the same. var cycles: u64 = 0; var recent: [3]u8 = @splat(0); var found = false; for (0..20_000) |_| { cycles += 7; const value = iwm.access(0xc, false, 0, cycles); if (value & 0x80 == 0 or value == recent[2]) continue; recent = .{ recent[1], recent[2], value }; if (std.mem.eql(u8, &recent, &.{ 0xd5, 0xaa, 0x96 })) found = true; } try testing.expect(found);
// And the status register says the drive is enabled, at bit 5, and // carries the sense line at bit 7 -- which on these drives is the // write-protect tab. _ = iwm.access(0xd, false, 0, cycles); // Q6 high: the status register try testing.expectEqual(@as(u8, 0x20), iwm.access(0xe, false, 0, cycles) & 0x20); try testing.expectEqual(@as(u8, 0x80), iwm.access(0xe, false, 0, cycles) & 0x80);
floppy.write_protected = false; try testing.expectEqual(@as(u8, 0x00), iwm.access(0xe, false, 0, cycles) & 0x80);}
test "an empty drive reads as noise and as write protected" { // Which is not the same as a broken one: the handshake still says the chip // is ready. An empty port and a broken port are different machines. // // Nor is an empty drive a silent one. It used to read here as nought, and // that was wrong: the head hears the amplifier's own noise over surface // with nothing on it, and a machine that waits for a byte off an empty // drive is waiting for something that does arrive. See `readNibble` in // `disk2.zig` for what a IIc does with the difference. var iwm: Iwm = .{}; _ = iwm.access(0x9, false, 0, 0); _ = iwm.access(0xe, false, 0, 0); var finished: usize = 0; for (1..64) |i| { if (iwm.access(0xc, false, 0, i * 1000) & 0x80 != 0) finished += 1; } try testing.expect(finished > 0); try testing.expect(iwm.sense(0)); _ = iwm.access(0xc, false, 0, 0); try testing.expectEqual(@as(u8, 0x80), iwm.access(0xf, false, 0, 0));}
test "the disk interface register decides which drives the switches reach" { // The same sixteen addresses, and what they do depends on one bit // somewhere else entirely. var iwm: Iwm = .{};
// With a 5.25-inch drive on the end, phase 0 is a magnet. _ = iwm.access(0x1, false, 0, 0); try testing.expect(iwm.drives.drives[0].phases[0]); try testing.expect(!iwm.disks35.lines[0]);
// With a 3.5-inch drive on the end it is one bit of a register number. iwm.setDiskRegister(0x40); _ = iwm.access(0x0, false, 0, 0); // CA0 off _ = iwm.access(0x3, false, 0, 0); // CA1 on try testing.expectEqual(disk35.Register.motor_on, iwm.disks35.addressed());
// And strobing it starts the spindle, which the enable line does not. _ = iwm.access(0x9, false, 0, 0); // enable try testing.expect(iwm.disks35.enabled); try testing.expect(!iwm.disks35.drives[0].spindle); _ = iwm.access(0x7, false, 0, 0); // strobe up _ = iwm.access(0x6, false, 0, 0); // and down try testing.expect(iwm.disks35.drives[0].spindle);
// The sense line now answers the drive's questions rather than a tab. _ = iwm.access(0x2, false, 0, 0); // CA1 off: register 0, the direction _ = iwm.access(0xd, false, 0, 0); // Q6 high _ = iwm.access(0xe, false, 0, 0); // Q7 low: the status register try testing.expectEqual(@as(u8, 0x00), iwm.access(0xe, false, 0, 0) & 0x80);}
test "the handshake will not take another byte until the last one has gone down" { // Which is the whole of the firmware's timing when it formats a disk. Its // loop is six instructions and only one of them waits: `BIT $c0ec` and // `BPL` back to it, between fetching the byte and writing it. Told yes // every time it never waits at all, and writes about two bytes for every // place on the track there is room for one. var iwm: Iwm = .{}; iwm.three_and_a_half = true; const byte_time = iwm.disks35.turning;
// An idle chip is ready: there is nothing it is waiting to finish. try testing.expectEqual(@as(u8, 0x80), iwm.handshake(1000));
// Having taken one, it is not ready again until that byte has had as long // as it takes to pass the head. iwm.writeNibble(1000, 0xff); try testing.expectEqual(@as(u8, 0x00), iwm.handshake(1000)); try testing.expectEqual(@as(u8, 0x00), iwm.handshake(1000 + byte_time - 1)); try testing.expectEqual(@as(u8, 0x80), iwm.handshake(1000 + byte_time));
// And bit 6 stays clear whatever happens: a real one sets it when the // processor was too slow to keep the head fed, and nothing here is ever // too slow, because this is what makes it wait. try testing.expectEqual(@as(u8, 0x00), iwm.handshake(1000 + byte_time * 100) & 0x40);
// The 5.25-inch drives are paced by their own byte time, which is twice // as long: four microseconds to the bit rather than two. var five: Iwm = .{}; five.writeNibble(1000, 0xff); try testing.expectEqual(@as(u8, 0x00), five.handshake(1000 + five.drives.turning - 1)); try testing.expectEqual(@as(u8, 0x80), five.handshake(1000 + five.drives.turning));}
test "the chip's own reset line puts the chip back and leaves the drives alone" { var iwm: Iwm = .{}; iwm.drives.fitted = 2;
// Put the chip somewhere: a mode register, both state flip flops, a // couple of phases, the second drive, and the motor running. _ = iwm.access(0xd, false, 0, 0); // Q6 high _ = iwm.access(0xf, true, 0x1f, 0); // and Q7, so a write reaches the mode _ = iwm.access(0xb, false, 0, 0); // drive two _ = iwm.access(0x1, false, 0, 0); // phase 0 on _ = iwm.access(0x9, false, 0, 0); // motor on try testing.expect(iwm.enabled); try testing.expectEqual(@as(u1, 1), iwm.selected); try testing.expect(iwm.q6 and iwm.q7); try testing.expect(iwm.lines[0]);
// Where the head was left is mechanical, so put it somewhere too and // watch it stay. iwm.drives.drives[0].quarter_track = 68; // And the kind of drive on the port, which on the two machines that have // this chip is not the chip's line at all. iwm.three_and_a_half = true;
iwm.resetChip(0);
try testing.expectEqual(@as(u8, 0), iwm.mode); try testing.expect(!iwm.q6 and !iwm.q7); try testing.expectEqual(@as(u1, 0), iwm.selected); try testing.expect(!iwm.lines[0]); try testing.expect(!iwm.enabled); // The enable was dropped through the latch rather than assigned, so the // mechanism heard about it: with the mode register cleared first there is // no one-second timer left to hold the motor on. try testing.expect(!iwm.disks35.enabled);
// And these two are on the other side of the reset line. try testing.expectEqual(@as(u8, 68), iwm.drives.drives[0].quarter_track); try testing.expect(iwm.three_and_a_half);}