diff --git a/README.md b/README.md index 3417711..33bbf54 100644 --- a/README.md +++ b/README.md @@ -329,7 +329,7 @@ driver is in the machine's own ROM. | `$00` | `Apple IIc ROM 00 - 342-0033-A - 1985.bin`, 32K | UniDisk 3.5 support | boots 5.25 and a UniDisk 3.5 | | `$03` | `Apple IIc ROM 03 - 341-0445-A.bin`, 32K | the Memory Expansion machine | boots 5.25, memory card works | | `$04` | `Apple IIc ROM 04 - 341-0445-B.bin`, 32K | the last of them | boots 5.25, memory card works | -| `$05` | `Apple IIc ROM 05 - IIc Plus.bin`, 32K | the IIc Plus: 4 MHz, 3.5-inch drive in the case | boots its own drive, and either kind on the back; formats a 3.5-inch disk | +| `$05` | `Apple IIc ROM 05 - IIc Plus.bin`, 32K | the IIc Plus: 4 MHz, 3.5-inch drive in the case | boots its own drive, and either kind on the back; formats a 3.5-inch disk; answers the accelerator's commands, at 1 MHz | A IIc with the `$FF` ROM and no disk appears to hang, and that is what the hardware did: the firmware starts the drive and waits for a disk that is not @@ -1052,34 +1052,40 @@ nothing here can say either: they fall in the half of the window shared with the ROM, so the write is dropped whether the chip is watching or not, and no test that could be written here would tell those two apart. -The four-megahertz accelerator is still to come, and what is worth saying is -why it is not a switch. It is a cache. The machine has a 65C02 clocked at four -megahertz beside two eight-kilobyte static RAMs, one holding data and one -holding a tag for every byte of it, and a gate array between them — the CGGA — -which gives the processor its fast clock only while the tag says the byte it -wants is already there. On a miss, or with the accelerator disabled, the array -synchronises the processor to the rest of the machine, fetches the byte from -system RAM, fills the cache behind it, and runs at the 1.023 MHz every other -IIc runs at (Apple Computer, Inc., 1989b, p. 329). Which parts of the address -space may be cached is itself programmable, and the state it comes up in is a -map of the machine's own compromises: the firmware for ports 1, 2, 5 and 6 and -the code for the speaker and the paddles are marked uncacheable and stay slow, -because speeding up ports 5 and 6 stops the disk drives working and speeding up -the paddles stops them reading at all; ports 3, 4 and 7 may run fast (p. 626). +The four-megahertz accelerator is not modelled as a speed, and what is worth +saying is why it could not be one. It is a cache. The machine has a 65C02 +clocked at four megahertz beside two eight-kilobyte static RAMs, one holding +data and one holding a tag for every byte of it, and a gate array between them +— the CGGA — which gives the processor its fast clock only while the tag says +the byte it wants is already there. On a miss, or with the accelerator +disabled, the array synchronises the processor to the rest of the machine, +fetches the byte from system RAM, fills the cache behind it, and runs at the +1.023 MHz every other IIc runs at (Apple Computer, Inc., 1989b, p. 329). Which +parts of the address space may be cached is itself programmable, and the state +it comes up in is a map of the machine's own compromises: the firmware for +ports 1, 2, 5 and 6 and the code for the speaker and the paddles are marked +uncacheable and stay slow, because speeding up ports 5 and 6 stops the disk +drives working and speeding up the paddles stops them reading at all; ports 3, +4 and 7 may run fast (p. 626). So the speed is not a setting that can be read back. The manual says so — "there is no way to determine the state of the system's speed at any given time" (p. 626) — and what this emulator runs at is the speed a miss gives, -which is the speed the machine falls back to anyway. - -The firmware does run, all the same. Reset reaches it through a thunk at -`$c7c7` that flips the ROM bank and jumps to `$fd00` in the second one, and -what it finds there borrows the glue chip's RAM for scratch, which is one more +which is the speed the machine falls back to anyway. A program cannot tell the +difference by asking; it can only tell by timing itself, which is exactly what +Apple warns will change under it. + +**The interface, though, is all here**, because it is firmware rather than +silicon, and a system test drives every command of Appendix H on Apple's own +ROM. Reset reaches that firmware through a thunk at `$c7c7` that flips the ROM +bank and jumps to `$fd00` in the second one, and what it finds there borrows +the glue chip's RAM for scratch — it steps the window to page 2 and saves the +whole of zero page `$00-$07` into it before doing anything, which is one more thing that would not work without that chip. The hardware it drives is -write-only: `$c05a` takes `$a5` to unlock the array and `$5a` four times over -to lock it again, `$c05a` and `$c05b` take `$08` to disable and enable it, and -`$c05c` through `$c05f` take four bytes copied out of a table at `$fe8c` saying -what may be cached. Nothing in either bank ever loads from any of those six +write-only: `$c05a` takes `$a5` to lock the array and `$5a` four times over to +unlock it, `$c05a` and `$c05b` take `$08` to disable and enable it, and `$c05c` +through `$c05f` take four bytes copied out of a table at `$fe8c` saying what +may be cached. Nothing in either bank ever loads from any of those six addresses. They are borrowed from the switches every other IIc keeps there — `$c05a` and `$c05b` are its vertical-blanking interrupt controls, and `$c05e` and `$c05f` its double-high-resolution switch, which is why the manual warns @@ -1087,10 +1093,38 @@ that running a command changes the state of double hi-res, and why its own sample code saves that switch and puts it back (p. 626). There is a Read Accelerator call in spite of all that, and how it answers is -the giveaway: it does not ask the chip. The firmware keeps a two-byte shadow of -everything it has written in the glue chip's RAM at `$ce02`, sets or clears a -bit of it beside every knock, and hands that back (Apple Computer, Inc., 1989b, -p. 631). +the giveaway: it does not ask the chip. The chip has no way to be asked. The +firmware keeps a two-byte shadow of everything it has written in the glue +chip's RAM at `$ce02`, sets or clears a bit of it beside every knock, and hands +that back (p. 631). Enable and Disable move one bit of it — high byte bit 3, +"CGGA enable (1 = disabled)" — which the same table says the Write command may +not touch, for exactly that reason. + +Reading it back on this machine gives `$67` low and `$00` high, and the low +byte is not a coincidence. It is the byte at bank 1 `$fe8a`, which the cold +reset path at `$fd96` loads into X and hands to the routine that writes the +registers, so what comes out of Read is the ROM's own power-on default having +gone all the way through the chip's window and back. Read against Table H-1 it +says ports 1, 2, 3, 6 and 7 fast, ports 4 and 5 slow, speaker slow (p. 631) — +which is **not** the default the same appendix describes in prose two pages +earlier, "ROM code for ports 1, 2, 5, and 6 ... cannot be cached ... The code +for ports 3, 4, and 7 can be" (p. 626). The two agree about the speaker and, +by way of the high byte at `$fe8b`, about the paddles, and disagree about every +port. The ROM is what the machine does, so the ROM is what the test asserts, +and the disagreement is Apple's and is left standing rather than resolved. + +One more thing the appendix has wrong, and it matters to anybody writing to it. +Read Accelerator takes a *pointer* to a two-byte buffer, and the firmware +dereferences it — `STA ($03),Y` at `$fe1d`. Write Accelerator is documented +with the same "pointer to buffer" parameter list and the same sample code (p. +633), and does not: `$fe33` is `LDA $03 / STA $ce02 / TAX`, and that X goes +straight to the register at `$c05c`. **Write takes the control word itself, +pushed where the pointer is documented to go.** Following the manual pushes a +buffer address into the CGGA as if it were a control word, and since a +buffer's high byte will usually have reserved bits set, the result is the crash +the appendix warns about three pages earlier — "writing a 1 to any CGGA control +word bits that are reserved can cause the system to crash" (p. 626). That is +how this was found. Nothing else needs a ROM. The block device in slot 7, the clock card, the mouse card and the Mockingboard all answer with firmware written for this diff --git a/src/system_tests.zig b/src/system_tests.zig index 47ce2a4..d334fe6 100644 --- a/src/system_tests.zig +++ b/src/system_tests.zig @@ -2273,3 +2273,104 @@ test "an Apple IIc Plus formats a blank 3.5-inch disk in the drive on the back" const path = try std.fmt.bufPrint(&path_buffer, "/{s}/BASIC.SYSTEM", .{booted.name(&booted_name)}); try testing.expect(booted.find(path) != null); } + +test "an Apple IIc Plus answers the accelerator's own commands" { + // The four-megahertz cache is not modelled and cannot be -- see the README + // -- but its *interface* is a firmware one, and all of that is here. Every + // command in Appendix H goes through the entry point at `$c7c7`, which + // flips the ROM bank, runs code in the other one, and keeps its state in + // the glue chip's buffer. So this exercises the bank thunk, the chip's RAM + // window and its page stepping, and the six write-only addresses at + // `$c05a-$c05f` all at once, on Apple's own ROM. + // + // Each command is its own `JSR`, which is how Apple's own sample shell + // makes them (Apple Computer, Inc., 1989b, p. 634): "push the command + // parameters onto the stack, then execute a JSR instruction to the + // accelerator entry point, $C7C7" (p. 627). A command that is understood + // returns `$00` in the accumulator. + const alloc = testing.allocator; + const rom = try catalogued(alloc, .roms, "Apple IIc ROM 05 - IIc Plus.bin"); + defer alloc.free(rom); + + // No disk anywhere, which on this machine reaches Applesoft by way of a + // complaint rather than by hanging: it has an intelligent port and asks + // it, where a II Plus with a Disk II card spins for ever. + var system = try powerOn(cpu8.CMOS, memory.AppleIIc(.{}), rom); + system.cpu.reset(); + try system.waitFor("UNABLE TO FIND A BOOTABLE DISK", 120 * second); + try system.waitFor("\n]", 30 * second); + + // One routine per command, sixteen bytes apart, each leaving the + // accumulator it was answered with in a byte of its own. + const routines = [_]struct { u16, []const u8, []const u8 }{ + .{ 0x300, "unlock", &[_]u8{ 0xa9, 0x04, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x90, 0x03, 0x60 } }, + .{ 0x310, "read", &[_]u8{ 0xa9, 0x03, 0x48, 0xa9, 0x80, 0x48, 0xa9, 0x05, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x91, 0x03, 0x60 } }, + .{ 0x320, "disable", &[_]u8{ 0xa9, 0x02, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x92, 0x03, 0x60 } }, + .{ 0x330, "read", &[_]u8{ 0xa9, 0x03, 0x48, 0xa9, 0x84, 0x48, 0xa9, 0x05, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x93, 0x03, 0x60 } }, + .{ 0x340, "enable", &[_]u8{ 0xa9, 0x01, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x94, 0x03, 0x60 } }, + .{ 0x350, "read", &[_]u8{ 0xa9, 0x03, 0x48, 0xa9, 0x88, 0x48, 0xa9, 0x05, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x95, 0x03, 0x60 } }, + .{ 0x360, "write", &[_]u8{ 0xa9, 0x40, 0x48, 0xa9, 0x00, 0x48, 0xa9, 0x06, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x96, 0x03, 0x60 } }, + .{ 0x370, "read", &[_]u8{ 0xa9, 0x03, 0x48, 0xa9, 0x8c, 0x48, 0xa9, 0x05, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x97, 0x03, 0x60 } }, + .{ 0x3a0, "write back", &[_]u8{ 0xa9, 0x00, 0x48, 0xa9, 0x67, 0x48, 0xa9, 0x06, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x98, 0x03, 0x60 } }, + .{ 0x3b0, "lock", &[_]u8{ 0xa9, 0x03, 0x48, 0x20, 0xc7, 0xc7, 0x8d, 0x99, 0x03, 0x60 } }, + }; + for (routines) |routine| { + for (routine[2], 0..) |byte, i| system.cpu.memory.poke(@intCast(routine[0] + i), byte); + } + // The buffers and the answers, filled with a byte no command writes. + for (0x380..0x39a) |address| system.cpu.memory.poke(@intCast(address), 0xee); + + var line: [16]u8 = undefined; + for (routines, 0..) |routine, i| { + try system.typeLine(try std.fmt.bufPrint(&line, "CALL {d}", .{routine[0]})); + system.run(second); + // The Monitor's prompt would mean the machine had fallen out of + // Applesoft, which is where a command it did not survive would leave + // it -- and it would leave every later check meaningless. + if (try system.shows("\n*")) { + std.debug.print("the machine fell into the Monitor at step {d} ({s})\n", .{ i, routine[1] }); + try system.report(); + return error.TheMachineDidNotSurviveTheCommand; + } + try testing.expectEqual(@as(u8, 0x00), system.cpu.memory.peek(@intCast(0x390 + i))); + } + + const word = struct { + fn at(sys: anytype, address: u16) [2]u8 { + return .{ sys.cpu.memory.peek(address), sys.cpu.memory.peek(address + 1) }; + } + }.at; + + // The control word this machine comes up holding. Its low byte is not a + // guess: it is `$67`, the byte at bank 1 `$fe8a`, which the reset path at + // `$fd96` loads into X and hands to the routine that writes the chip's + // registers -- so reading it back through the firmware is the ROM's own + // power-on default coming out the other end. + // + // Read literally, Table H-1 makes that `$67` "ports 1, 2, 3, 6 and 7 fast, + // ports 4 and 5 slow, speaker slow" (Apple Computer, Inc., 1989b, p. 631), + // which is not the default the same appendix describes in prose two pages + // earlier -- "ROM code for ports 1, 2, 5, and 6 ... cannot be cached ... + // The code for ports 3, 4, and 7 can be" (p. 626). The two agree on the + // speaker and, through the high byte, on the paddles, and disagree about + // every port. The ROM is what the machine does, so the ROM is what is + // asserted here; the discrepancy is Apple's and is recorded rather than + // resolved. + try testing.expectEqual([2]u8{ 0x67, 0x00 }, word(&system, 0x380)); + + // Disable and Enable move one documented bit and nothing else: high byte + // bit 3, "CGGA enable (1 = disabled)" (Table H-1). It is the one bit the + // table says the Write command may not touch, and the firmware keeps it in + // its shadow rather than in the chip, because the chip has no way to be + // asked. + try testing.expectEqual([2]u8{ 0x67, 0x08 }, word(&system, 0x384)); + try testing.expectEqual([2]u8{ 0x67, 0x00 }, word(&system, 0x388)); + + // And Write takes a word and Read gives it back. Everything slow. + try testing.expectEqual([2]u8{ 0x00, 0x00 }, word(&system, 0x38c)); + + // The machine is still the machine afterwards: Applesoft answers, which is + // what says none of this left it somewhere it cannot come back from. + try system.typeLine("PRINT 6*7"); + try system.waitFor("42", 5 * second); +}