From 8ffa4057182470a94ea2ff6a02cd4ccae798da54 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 8 Sep 2026 13:45:27 -0500 Subject: [PATCH] a slow access on a IIgs costs what the phase says, not always three The Hardware Reference gives a best case and a worst and this modelled only the best: "if a single (8-bit) slow I/O read or write cycle is perfectly synchronized, it takes nearly three fast cycles to complete... if either a single or double slow cycle is not synchronized, the maximum delay for synchronization is one extra slow cycle" (Apple Computer, Inc., 1989a, p. 25). The mechanism behind those two sentences is that the processor is held until the Mega II will begin a cycle. So the cost is the distance to the next boundary plus a slow cycle: three when it arrives in step, four or five when it does not. A run of slow accesses stays in step because each one lands the clock back on a boundary, and a fast access in the middle is what knocks it out -- which is why an instruction that is all slow accesses costs the synchronised figure and one that mixes them does not. **The phase is discrete here and continuous on the machine**, and the comment says so rather than rounding quietly. A real IIgs runs 2.8 against 1.024, a ratio of 2.73, and its phase drifts through every value; this uses a whole 3 so that the processor and the video agree about how long a frame is, and a whole 3 gives the phase three values. What comes out is the shape of the variation rather than the exact sequence a particular machine would produce. Apple's own worst case is a round +3, being "up to one slow cycle" stated conservatively; the largest wait an integer 3:1 ratio can produce is 2. Two tests: every phase costing what it should, a run staying in step, a fast access knocking it out; and a machine held at 1.024 MHz having no synchronising to do at all, since then the whole machine is the Mega II's. Two existing tests wanted adjusting and both were asserting the old constant. A shadowed write now costs five rather than three because the read before it took the processor out of step, which is the case the manual gives a worst case for, so it asserts the range. And the IIgs 3.5-inch boot waits for its last line rather than expecting it on the spot: the machine is now honestly a little slower, so the menu finishes drawing a little later. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01Aieuyb2KR2DsKyZWPJpas6 --- README.md | 23 +++++++ src/memory16/apple2gs.zig | 126 ++++++++++++++++++++++++++++++++++---- src/system_tests.zig | 2 +- 3 files changed, 139 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e289242..c8ad639 100644 --- a/README.md +++ b/README.md @@ -4775,6 +4775,29 @@ The same three is used for the video's frame, so that the display and the processor agree with each other rather than one being exact and the other round. +**A slow access is not always three, though, and that took a second pass.** The +manual gives a best case and a worst: "if a single (8-bit) slow I/O read or +write cycle is perfectly synchronized, it takes nearly three fast cycles to +complete... if either a single or double slow cycle is not synchronized, the +maximum delay for synchronization is one extra slow cycle". The mechanism +behind those two sentences is simply that the processor is held until the Mega +II will begin a cycle: an access arriving on a boundary pays for the slow cycle +and nothing else, and one arriving in the middle of one waits out the rest +first. So the cost is the distance to the next boundary plus a slow cycle, +three to five fast cycles, and a run of slow accesses stays in step while a +fast one in the middle knocks it out — which is why an instruction that is all +slow accesses costs the synchronised figure and one that mixes them does not. + +The phase is discrete here and continuous on the machine, and that is worth +being plain about rather than quietly rounding. A real IIgs runs 2.8 against +1.024, a ratio of 2.73, so its phase drifts through every value; this uses a +whole three so that the processor and the video agree, and a whole three gives +the phase only three values. What comes out is the *shape* of the variation — +an access costing three, four or five depending on when it arrives — rather +than the exact sequence a particular machine would produce. Apple's own worst +case is a round +3, which is "up to one slow cycle" stated conservatively; the +largest wait an integer 3:1 ratio can produce is 2. + Chasing that turned up a bug worth naming, because it had been quietly poisoning the very measurement this section is written around: `$c100` to `$cfff` is slot ROM, and it was falling through to the soft-switch case where diff --git a/src/memory16/apple2gs.zig b/src/memory16/apple2gs.zig index 59deac9..ea90371 100644 --- a/src/memory16/apple2gs.zig +++ b/src/memory16/apple2gs.zig @@ -117,10 +117,8 @@ const block_firmware = blockdevice.firmware(block_slot); /// nine per cent slow in absolute terms. Nothing here measures absolute time /// yet. /// -/// A real one is variable: an access arriving out of step with the Mega II -/// waits up to a whole slow cycle longer, which the Hardware Reference gives -/// as the difference between the best and worst case. That is not modelled, -/// and a program timing itself finely against the video would notice. +/// A slow access is *not* always this long, and that is modelled: see +/// `slowAccess`. pub const slow_cycle = 3; /// An Apple IIgs. @@ -1743,18 +1741,56 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { /// previous Apple II computers will function as Apple IIGS /// peripherals only if the system speed is changed to 1.024 /// MHz before disk access is attempted". + /// What one access to the slow side of the machine costs, + /// which is not a constant. + /// + /// "If a single (8-bit) slow I/O read or write cycle is + /// perfectly synchronized, it takes nearly three fast cycles + /// to complete... If either a single or double slow cycle is + /// not synchronized, the maximum delay for synchronization is + /// one extra slow cycle, adding the equivalent of three fast + /// cycles to the count" (Apple Computer, Inc., 1989a, p. 25). + /// + /// The mechanism behind that sentence is simply that the + /// processor is held until the Mega II is ready to begin a + /// cycle: an access that arrives on a boundary pays for the + /// slow cycle and nothing else, and one that arrives in the + /// middle of one waits out the rest of it first. So the cost + /// is the distance to the next boundary plus a slow cycle, + /// which is between three and five fast cycles here. + /// + /// **The phase is discrete here and continuous on a real + /// machine**, and that is worth being plain about. A IIgs runs + /// 2.8 MHz against 1.024, a ratio of 2.73, so its phase drifts + /// through every value; this model uses a whole 3 so that the + /// processor and the video agree about how long a frame is -- + /// see `slow_cycle` -- and a whole 3 gives the phase only + /// three values. What comes out is the *shape* of the + /// variation, an access costing three, four or five cycles + /// depending on when it arrives, rather than the exact + /// sequence a particular machine would produce. Apple's own + /// worst case is a round +3, being "up to one slow cycle" + /// stated conservatively; the largest wait an integer 3:1 + /// ratio can produce is 2. + fn slowAccess(self: *Self) u8 { + const now = self.getCPU().clock.cycles; + const into: u8 = @intCast(now % slow_cycle); + const wait: u8 = if (into == 0) 0 else slow_cycle - into; + return wait + slow_cycle; + } + fn cost(self: *Self, address: u24, writing: bool) u8 { if (self.speed & speed_fast == 0) return slow_cycle; if (self.motor_detected & self.speed & motor_detect_bits != 0) return slow_cycle; const bank: u8 = @truncate(address >> 16); const within: u16 = @truncate(address); - if (bank == slow_bank or bank == slow_bank + 1) return slow_cycle; + if (bank == slow_bank or bank == slow_bank + 1) return self.slowAccess(); // A shadowed write has to be made twice, and the second // one is on the far side of the machine. The manual says // so outright: "only write instructions to the video // locations require the system to operate at 1.024 MHz". - if (writing and self.shadowed(bank, within)) return slow_cycle; + if (writing and self.shadowed(bank, within)) return self.slowAccess(); if (bank != 0x00 and bank != 0x01) return 1; if (within < io_from or within >= io_to) return 1; // And with the I/O space inhibited there is nothing slow @@ -1765,12 +1801,12 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { // The FPI's own registers, on the fast side of the // machine. Only meaningful below $c100, which the // range check above has already established. - 0x35, 0x36, 0x37 => if (within < switches_to) 1 else slow_cycle, + 0x35, 0x36, 0x37 => if (within < switches_to) 1 else self.slowAccess(), // The interrupt ROM, fast to read and slow to write. - 0x71...0x7f => if (within < switches_to and !writing) 1 else slow_cycle, + 0x71...0x7f => if (within < switches_to and !writing) 1 else self.slowAccess(), // In both chips: written slowly, read quickly. - 0x2d, 0x68 => if (within < switches_to and !writing) 1 else slow_cycle, - else => slow_cycle, + 0x2d, 0x68 => if (within < switches_to and !writing) 1 else self.slowAccess(), + else => self.slowAccess(), }; } @@ -2769,7 +2805,11 @@ test "a shadowed write is slow and a read of the same place is fast" { const writing = cpu.clock.read() - before - reading; try testing.expectEqual(@as(u64, 1), reading); - try testing.expectEqual(@as(u64, slow_cycle), writing); + // A slow cycle, plus however much of one was left to wait out: the fast + // read above has taken the processor out of step with the Mega II, which + // is exactly the case the Hardware Reference gives a worst case for. See + // `slowAccess`. + try testing.expect(writing >= slow_cycle and writing <= 2 * slow_cycle - 1); // With the shadowing turned off the write is as fast as the read: there is // no second trip to make. @@ -3664,3 +3704,67 @@ test "the border colour is the low nibble of $c034 and the clock keeps the rest" // rather than instead of it. try testing.expectEqual(@as(u8, 0x0f), cpu.memory.read(0x00c034, .data) & 0x0f); } + +test "a slow access costs a slow cycle plus however much of one was left" { + // "If a single (8-bit) slow I/O read or write cycle is perfectly + // synchronized, it takes nearly three fast cycles to complete... If either + // a single or double slow cycle is not synchronized, the maximum delay for + // synchronization is one extra slow cycle" (Apple Computer, Inc., 1989a, + // p. 25). The mechanism is that the processor is held until the Mega II + // will begin a cycle, so what it costs is the distance to that boundary + // plus the cycle itself. + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + cpu.memory.write(0x00c036, TestMemory.speed_fast, .data); + + // Arriving on a boundary is the synchronised case, and costs the slow + // cycle and nothing more. + for (0..slow_cycle) |phase| { + cpu.clock.cycles = 300 + phase; + const before = cpu.clock.read(); + _ = cpu.memory.read(0x00c000, .data); // the keyboard, on the slow side + const spent = cpu.clock.read() - before; + const expected: u64 = if (phase == 0) slow_cycle else slow_cycle + (slow_cycle - phase); + try testing.expectEqual(expected, spent); + // Never less than a slow cycle and never more than two, which is the + // manual's best and worst case. + try testing.expect(spent >= slow_cycle and spent < 2 * slow_cycle); + } + + // And a run of them stays in step, because each one lands the clock back + // on a boundary. This is why an instruction that is all slow accesses + // costs what the manual's synchronised case says and no more. + cpu.clock.cycles = 0; + for (0..4) |_| { + const before = cpu.clock.read(); + _ = cpu.memory.read(0x00c000, .data); + try testing.expectEqual(@as(u64, slow_cycle), cpu.clock.read() - before); + } + + // A fast access is what knocks it out of step, and the next slow one pays + // for it. Fast RAM in bank 2, which nothing shadows. + cpu.clock.cycles = 0; + _ = cpu.memory.read(0x020000, .data); + try testing.expectEqual(@as(u64, 1), cpu.clock.read()); + const before = cpu.clock.read(); + _ = cpu.memory.read(0x00c000, .data); + try testing.expectEqual(@as(u64, slow_cycle + slow_cycle - 1), cpu.clock.read() - before); +} + +test "a machine held at 1.024 MHz has no synchronising to do" { + // With the speed register asking for the slow clock the whole machine is + // the Mega II's, so there is no boundary to wait for: every access is one + // slow cycle whenever it arrives. + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + cpu.memory.write(0x00c036, 0x00, .data); // slow + + for (0..slow_cycle) |phase| { + cpu.clock.cycles = 500 + phase; + const before = cpu.clock.read(); + _ = cpu.memory.read(0x00c000, .data); + try testing.expectEqual(@as(u64, slow_cycle), cpu.clock.read() - before); + } +} diff --git a/src/system_tests.zig b/src/system_tests.zig index 8fefeec..1db66f9 100644 --- a/src/system_tests.zig +++ b/src/system_tests.zig @@ -737,7 +737,7 @@ test "an Apple IIgs boots an 800K disk from its 3.5-inch drive" { // booted is that program's own menu — text that is nowhere in the ROM and // can only have come off the disk, through the 3.5-inch drive. try system.waitFor("FastCopy", 300 * gs_second); - try system.expect("Copyright Apple Computer, Inc. 1987-90"); + try system.waitFor("Copyright Apple Computer, Inc. 1987-90", 60 * gs_second); } test "an Apple IIgs writes a file to its 3.5-inch disk and it is really there" { -- knotmirror 0.0.1