From ea5370ff197c77b21568a10028cd397998ec24f8 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 1 Sep 2026 23:32:18 -0500 Subject: [PATCH] give the IIgs its disk port, or at least the chip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An Apple IIgs has an IWM where a IIe had a Disk II card: sixteen addresses at $c0e0, of which eight are the stepper phases, two the motor, two the drive select, and four are a pair of flip-flops called Q6 and Q7 that decide which of the chip's registers the next access reaches. Table E-15 gives the four states and src/iwm.zig implements them. The self-test wrote its own specification for the interesting part. It does this: TYA / STA $C0EF / TYA / EOR $C0EE / AND #$1F / BNE which is: write a value to an odd address, read an even one, and the low five bits must match. The mode register is write-only and the status register is how a program finds out what it wrote — its low five bits are the mode register's low five, and the manual says so in as many words. The handshake register is the part worth repeating. The first version had it read as zero, on the reasoning that there is no drive attached and so nothing to be ready for. That is a different claim and a false one: the IWM is a chip on the board whether or not anything is plugged into it, and a chip with nothing to do is ready by definition. Reading zero models a chip that is permanently busy, which is not a machine with an empty disk port but one with a broken port, and the firmware waits on it forever. The distinction between "there is nothing here" and "the thing that is here is not working" is the whole of the difference. With that the machine moves on twice more. What is left is dominated by one entry: 65,500 accesses to $c03d, the Ensoniq's sixty-four kilobytes of sound RAM being tested a byte at a time. After that it wants a drive with a disk in it, which is a larger thing than a register. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01FawbWzEu5hoLLZSMCehUGP --- README.md | 27 ++++- src/iwm.zig | 217 ++++++++++++++++++++++++++++++++++++++ src/memory16/apple2gs.zig | 14 +++ src/root.zig | 4 + 4 files changed, 258 insertions(+), 4 deletions(-) create mode 100644 src/iwm.zig diff --git a/README.md b/README.md index 70fa52d..732aaa7 100644 --- a/README.md +++ b/README.md @@ -1150,10 +1150,29 @@ This one is a US machine with one layout and one character set. With those two answered the machine goes a great deal further: sixty-three bytes of conversation with the desktop bus instead of fourteen, and it starts executing in bank `$00` and in the ROM's other bank rather than going round in -the same four instructions. What it wants next is a different machine -altogether — 65,500 accesses to `$c03d`, which is the Ensoniq's sixty-four -kilobytes of sound RAM being tested a byte at a time, and half a million to -`$c0ee` and `$c0ef`, which is the disk interface. +the same four instructions. + +Then it wanted the disk port, which on a IIgs is an IWM where a IIe had a card +— sixteen addresses, of which eight are the stepper phases, two the motor, two +the drive select, and four are a pair of flip-flops that decide which of the +chip's registers the next access reaches. That is in `src/iwm.zig`, and the +self-test's own loop is the specification: write to an odd address, read an +even one, and the low five bits must match, because the mode register is +write-only and the status register is how a program finds out what it wrote. + +One thing there is worth repeating, because it took a while to see. The first +version had the handshake register read as zero, reasoning that there is no +drive attached and so nothing to be ready for. But that is a different claim, +and a false one: the IWM is a chip on the board whether or not a drive is +plugged into it, and a chip with nothing to do is *ready*. Reading zero models +a chip that is permanently busy — not an empty disk port but a broken one — and +the firmware waits for it forever. An hour of "why is it still stuck" came down +to that distinction. + +What is left on the list is dominated by one thing: 65,500 accesses to `$c03d`, +which is the Ensoniq's sixty-four kilobytes of sound RAM being tested a byte at +a time. And beyond that the machine wants a drive with a disk in it, which is a +larger piece of work than a register. The two clocks are now two clocks, which fixes the timing that was knowably wrong. A IIgs runs its processor at 2.8 MHz and its Mega II at 1.024, so diff --git a/src/iwm.zig b/src/iwm.zig new file mode 100644 index 0000000..735e53c --- /dev/null +++ b/src/iwm.zig @@ -0,0 +1,217 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// 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: +//! +//! | 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 implemented is the chip: the switches, the mode register and the +//! status register that reads it back, and the handshake. What is not is a +//! drive, or a disk in one. So the data register reads as nothing, because +//! nothing is going past the head — but the handshake says the chip is ready, +//! because a chip with nothing to do is ready. That distinction cost a while: +//! an empty disk port and a broken one are different machines, and only one of +//! them leaves the firmware waiting forever. + +const std = @import("std"); + +/// The chip's sixteen addresses and the little state behind them. +pub const Iwm = struct { + /// The four stepper phases, one bit each. Walking them in order is how a + /// head is moved; nothing here has a head to move. + phases: u4 = 0, + /// Whether the spindle motor has been asked for. + motor: bool = false, + /// Which of the two drives is selected. + drive: u1 = 0, + /// 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, + + /// The sense line coming back from whatever is plugged in, which means + /// different things depending on which of the phases are asserted. With + /// nothing plugged in there is nothing to sense. + sense: 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) u8 { + switch (which) { + // The four stepper phases, off on even and on on odd. + 0x0, 0x2, 0x4, 0x6 => { + const phase: u2 = @intCast(which >> 1); + self.phases &= ~(@as(u4, 1) << phase); + }, + 0x1, 0x3, 0x5, 0x7 => { + const phase: u2 = @intCast(which >> 1); + self.phases |= @as(u4, 1) << phase; + }, + 0x8 => self.motor = false, + 0x9 => self.motor = true, + 0xa => self.drive = 0, + 0xb => self.drive = 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. Writing reaches + // the mode register, and only with the motor stopped: the manual is + // explicit that a write succeeds only once the one-second timer has + // run out. + if (writing) { + if (self.q7 and self.q6 and !self.motor) self.mode = value; + return 0; + } + if (self.q7) return self.handshake(); + if (self.q6) return self.status(); + return self.data(); + } + + /// 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: *const Iwm) u8 { + var value: u8 = self.mode & 0x1f; + if (self.sense) value |= 0x40; + if (self.motor) value |= 0x80; + return value; + } + + /// 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 is set here because the chip *is* ready. + /// That is worth spelling out, because the first version of this returned + /// zero on the grounds that there is no drive attached and nothing to be + /// ready for — and that is a different claim, and a false one. The IWM is + /// a chip on the board whether or not a drive is plugged into it, and a + /// chip with nothing to do is ready by definition. Returning zero models a + /// chip that is permanently busy, which is not a machine with an empty + /// disk port; it is a machine with a broken one, and the firmware waits + /// for it forever. + /// + /// Bit 6 says the last byte was written in time, which it was, there being + /// no disk turning to be late for. The scan of the manual's figure is too + /// mangled to say which way round its labels go, but the firmware settles + /// bit 7: it shifts this register left and loops while the carry is clear, + /// which is a machine waiting for bit 7 to come up. + pub fn handshake(self: *const Iwm) u8 { + _ = self; + return 0xc0; + } + + /// What the data register reads: the byte coming off the disk, of which + /// there is none. + pub fn data(self: *const Iwm) u8 { + _ = self; + return 0; + } + + pub fn reset(self: *Iwm) void { + self.* = .{}; + } +}; + +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); // motor off + _ = iwm.access(0xd, false, 0); // Q6 high + + var pattern: u8 = 0; + while (pattern < 0x20) : (pattern += 1) { + _ = iwm.access(0xf, true, pattern); // Q7 high: write the mode register + const read_back = iwm.access(0xe, false, 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); // Q6 high + _ = iwm.access(0x9, false, 0); // motor on + _ = iwm.access(0xf, true, 0x1f); + try testing.expectEqual(@as(u8, 0), iwm.mode); + + _ = iwm.access(0x8, false, 0); // motor off + _ = iwm.access(0xf, true, 0x1f); + 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 = .{}; + try testing.expectEqual(@as(u4, 0), iwm.phases); + _ = iwm.access(0x1, false, 0); + _ = iwm.access(0x5, false, 0); + try testing.expectEqual(@as(u4, 0b0101), iwm.phases); + _ = iwm.access(0x0, false, 0); + try testing.expectEqual(@as(u4, 0b0100), iwm.phases); + + _ = iwm.access(0xb, false, 0); + try testing.expectEqual(@as(u1, 1), iwm.drive); + _ = iwm.access(0x9, false, 0); + try testing.expect(iwm.motor); + // And the motor shows in the status register. + _ = iwm.access(0xd, false, 0); + try testing.expectEqual(@as(u8, 0x80), iwm.access(0xe, false, 0) & 0x80); +} + +test "which register an access reaches is Q6 and Q7" { + var iwm: Iwm = .{}; + _ = iwm.access(0x8, false, 0); // motor off + _ = iwm.access(0xd, false, 0); // Q6 high + _ = iwm.access(0xf, true, 0x15); // 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) & 0x1f); + // Q7 high, Q6 low: the handshake, which says it is ready — 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); + try testing.expectEqual(@as(u8, 0xc0), iwm.access(0xf, false, 0)); + // Q7 low, Q6 low: the data register, and there is no disk. + _ = iwm.access(0xe, false, 0); + try testing.expectEqual(@as(u8, 0x00), iwm.access(0xc, false, 0)); +} diff --git a/src/memory16/apple2gs.zig b/src/memory16/apple2gs.zig index 39e02f1..7792329 100644 --- a/src/memory16/apple2gs.zig +++ b/src/memory16/apple2gs.zig @@ -61,6 +61,7 @@ const iigsrom = @import("../iigsrom.zig"); const apple2e = @import("../memory/apple2e.zig"); const adb_module = @import("../adb.zig"); const rtc_module = @import("../rtc.zig"); +const iwm_module = @import("../iwm.zig"); pub const Options = bus_log.Options; pub const Cycle = bus_log.Cycle; @@ -161,6 +162,9 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { /// control register. rtc: rtc_module.Rtc, + /// The disk port, which is an IWM where a IIe had a card. + iwm: iwm_module.Iwm, + /// The New-Video register, `$c029`. /// /// Bit 7 turns Super Hi-Res on, bit 6 linearises its buffer, @@ -235,6 +239,7 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { .mega2 = undefined, .adb = .{}, .rtc = .{}, + .iwm = .{}, .rom = &.{}, .rom_image = null, .new_video = 0, @@ -269,6 +274,7 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { self.mega2.reset(); self.adb.reset(); self.rtc.reset(); + self.iwm.reset(); self.new_video = 0; self.shadow = 0; self.speed = speed_at_reset; @@ -497,6 +503,14 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { self.adb.setStatus(v); return v; } else return self.adb.status(self.mega2.keyWaiting()), + // The disk port. Every one of its sixteen addresses + // throws a switch, and which register the access then + // reaches is decided by two of those switches. + 0xe0...0xef => return self.iwm.access( + @truncate(which), + value != null, + value orelse 0, + ), // The clock chip: one byte at a time through a data // register and a control register, with the border // colour sharing the low nibble of the control one diff --git a/src/root.zig b/src/root.zig index 21427cc..a39a4b4 100644 --- a/src/root.zig +++ b/src/root.zig @@ -78,6 +78,9 @@ pub const adb = @import("adb.zig"); /// control register. pub const rtc = @import("rtc.zig"); +/// The Integrated Woz Machine: the disk port of an Apple IIgs. +pub const iwm = @import("iwm.zig"); + /// The 65816, which is its own core rather than an option on the one above. /// See the header of cpu16.zig for why the two are not one. pub const cpu16 = @import("cpu16.zig"); @@ -149,6 +152,7 @@ test { _ = iigsrom; _ = adb; _ = rtc; + _ = iwm; _ = memory16; _ = @import("cpu16/opcodes.zig"); _ = @import("cpu16/add.zig"); -- knotmirror 0.0.1