diff --git a/README.md b/README.md index 7100894..07d5a72 100644 --- a/README.md +++ b/README.md @@ -1213,6 +1213,50 @@ truncating the address to eight bits turned `$c123` into switch `$23`. Reads of slot firmware were being counted as reads of soft switches that had never been touched. +### The machine puts something on the screen + +Shadowing was the last thing between the firmware and the display, and putting +it in was the difference between a blank screen and **`Check startup device!`** +in the middle of it — which is the right answer from a machine whose disk port +is empty, and the first thing this model has ever drawn. + +The reason is worth setting out, because it is the whole of why the register +exists. The display buffers are in banks `$e0` and `$e1`, on the Mega II; every +program written before this machine existed puts its display in bank `$00`, on +the fast side; and shadowing is the wire between them. A write to a shadowed +address is made *twice*, once to fast RAM and once to the Mega II. A read is +not — "read instructions access the high-speed shadowed bank" — and that +asymmetry is the point, because it means old code keeps working at full speed +except for the writes. The manual is equally plain about the cost: "only write +instructions to the video locations require the system to operate at 1.024 +MHz", so a shadowed write is three fast cycles and a read of the same byte is +one. + +Which areas are shadowed is the Shadow register, and its bits are all +*inhibits*: text page 1, Hi-Res pages 1 and 2, the Super Hi-Res buffer, the +auxiliary Hi-Res pages and text page 2, one bit each, set to disable. A reset +clears the register, so a machine comes up shadowing everything. Two of them +are not simply an area: + +- Bit 4 names *banks* rather than a buffer. It takes the Hi-Res pages away from + the odd banks alone and leaves the even bank's alone, which the manual states + in as many words. +- Bit 3 is the Super Hi-Res buffer, a single 32K span from `$2000` to `$9fff` + in the odd bank — which means the two Hi-Res pages lie *underneath* its lower + half. The two paths are separate and either one enabled carries the write, so + inhibiting the Hi-Res pages does not stop a write at `$3000` reaching the + screen if Super Hi-Res shadowing is still on. + +Which of the Mega II's two halves a shadowed write lands in is the same +question as which half `$e1xxxx` reaches, and the Hardware Reference answers +both in one sentence: "if shadowing is enabled or the software is addressing +bank `$E0` or `$E1`, an odd-numbered bank address will access the Mega II +auxiliary memory automatically, without using the soft switches ... otherwise, +the Mega II ignores the bank bit". So one function decides it for both, which +is the point of having it. Note the two senses of *auxiliary* in play: the FPI +decides *whether* to shadow from the bank number it can see, and the Mega II +decides *where* it lands, and with the bank latch clear those two can disagree. + What is left to suspect is that nothing here raises an interrupt yet. ### What is left @@ -1581,11 +1625,16 @@ half of this project rather than the finished half. the slow side its 1 MHz, the shadowing of banks `$00` and `$01` into `$e0` and `$e1`, the VGC and its Super Hi-Res modes with a palette per scanline, the Ensoniq 5503 with its thirty-two oscillators, and ADB as a - microcontroller rather than a port. Nothing in the source cites it yet — - `src/cpu16/` is a processor and not a machine — but it is the map for - whether that ever changes. - [PDF at archive.org](https://dn760003.eu.archive.org/0/items/Apple_IIgs_Hardware_Reference_HiRes/Apple_IIgs_Hardware_Reference_HiRes.pdf) - · [details](https://archive.org/details/Apple_IIgs_Hardware_Reference_HiRes) + microcontroller rather than a port. `src/memory16/apple2gs.zig` is built + against it throughout, and chapter 2 in particular: the bank latch, the two + clock domains and the whole of shadowing come from it. Chapters 2 and 3 say + the same things from opposite ends — chapter 2 from the chips outward and + chapter 3 from the address map inward — and the pair is often what settles a + question neither one does alone. Read the `_djvu.txt`: the tables survive OCR + well enough to be trusted, and it is searchable in a way the PDF is not. + [details](https://archive.org/details/Apple_IIgs_Hardware_Reference_HiRes) + · [text](https://archive.org/stream/Apple_IIgs_Hardware_Reference_HiRes/Apple_IIgs_Hardware_Reference_HiRes_djvu.txt) + · [PDF](https://dn760003.eu.archive.org/0/items/Apple_IIgs_Hardware_Reference_HiRes/Apple_IIgs_Hardware_Reference_HiRes.pdf) - **Apple IIgs Firmware Reference.** Apple Computer, 1987, Addison-Wesley. The book the Hardware Reference keeps pointing at, and the one that has what diff --git a/src/memory16/apple2gs.zig b/src/memory16/apple2gs.zig index 852d4e3..c8b29dd 100644 --- a/src/memory16/apple2gs.zig +++ b/src/memory16/apple2gs.zig @@ -121,6 +121,36 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { /// Bit 0 of the New-Video register: the bank latch. const bank_latch: u8 = 0x01; + /// The Shadow register's bits. Every one of them is an + /// *inhibit*: set disables and clear enables, so a register of + /// zeroes — which is what a reset leaves — shadows everything. + const inhibit_text_1: u8 = 0x01; + const inhibit_hires_1: u8 = 0x02; + const inhibit_hires_2: u8 = 0x04; + const inhibit_super_hires: u8 = 0x08; + const inhibit_auxiliary_hires: u8 = 0x10; + const inhibit_text_2: u8 = 0x20; + + /// Bit 4 of the Speed register: shadow every RAM bank rather + /// than only the first two. The manual says not to, in as many + /// words — firmware running in another bank is corrupted by it + /// and the machine crashes — but the bit exists and this + /// answers it. + const shadow_every_bank: u8 = 0x10; + + /// The display buffers, at the addresses they have had since + /// 1977, and the Shadow register bit that turns each one off. + const text_1_from: u16 = 0x0400; + const text_1_to: u16 = 0x0800; + const text_2_to: u16 = 0x0c00; + const hires_1_from: u16 = 0x2000; + const hires_1_to: u16 = 0x4000; + const hires_2_to: u16 = 0x6000; + /// The Super Hi-Res buffer is a single 32K span in the + /// auxiliary bank, from $2000 to $9fff, and the two Hi-Res + /// pages sit underneath its lower half. + const super_hires_to: u16 = 0xa000; + /// What the speed register holds when the machine starts. /// /// Bit 7 set is 2.8 MHz. The Hardware Reference does not say @@ -358,6 +388,89 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { nothing, }; + /// Which of the Mega II's two halves an access reaches. + /// + /// With the bank latch set — bit 0 of the New-Video register — + /// the low bit of the bank address says which, and the old + /// soft switches have no vote. With it clear the Mega II + /// ignores the bank bit entirely and its own switches decide, + /// exactly as on a IIe. + /// + /// The Hardware Reference states the rule once and applies it + /// to both ways of reaching the slow side: "if shadowing is + /// enabled or the software is addressing bank $E0 or $E1, an + /// odd-numbered bank address will access the Mega II auxiliary + /// memory automatically, without using the soft switches ... + /// otherwise, the Mega II ignores the bank bit". So one + /// function serves the direct banks and the shadowed writes + /// alike, which is the point of having it. + fn megaHalf(self: *Self, bank: u8, within: u16, writing: bool) bool { + if (self.new_video & bank_latch != 0) return bank & 1 != 0; + return self.mega2.auxiliaryFor(within, writing); + } + + /// Whether a write here is duplicated into the Mega II. + /// + /// Reads are not: the Hardware Reference is explicit that + /// "read instructions access the high-speed shadowed bank", + /// and that only writes pay for the trip. Shadowing exists so + /// that a program written for a IIe can keep putting its + /// display in bank $00 and still be seen, while everything it + /// reads back comes at full speed from the fast side. + fn shadowed(self: *const Self, bank: u8, within: u16) bool { + // Banks $00 and $01 are always shadow-enabled. The rest + // are only when the Speed register says every bank is. + if (bank > 0x01 and self.speed & shadow_every_bank == 0) + return false; + + // The odd bank is the auxiliary one — the manual writes + // "all auxiliary (odd) banks" — and this is the FPI's + // decision, made from the bank number it can see, rather + // than the Mega II's about which half to land in. + const odd = bank & 1 != 0; + const inhibit = self.shadow; + + // Bit 4 takes the Hi-Res pages away from the odd banks + // alone and leaves the even banks' alone. + const hires_here = !odd or inhibit & inhibit_auxiliary_hires == 0; + // Under $6000 the Super Hi-Res buffer lies over the two + // Hi-Res pages, and the two paths are separate: either one + // enabled is enough to shadow the write. + const super_here = odd and inhibit & inhibit_super_hires == 0; + + return switch (within) { + // Text page 1, which is not only for old programs: + // the Control Panel writes here too. + text_1_from...text_1_to - 1 => inhibit & inhibit_text_1 == 0, + // Text page 2, which the original machine could not + // shadow at all — the bit is on the 1 MB board only. + text_1_to...text_2_to - 1 => inhibit & inhibit_text_2 == 0, + hires_1_from...hires_1_to - 1 => super_here or + (hires_here and inhibit & inhibit_hires_1 == 0), + hires_1_to...hires_2_to - 1 => super_here or + (hires_here and inhibit & inhibit_hires_2 == 0), + // The top two-thirds of the Super Hi-Res buffer, with + // no Hi-Res page underneath it. + hires_2_to...super_hires_to - 1 => super_here, + else => false, + }; + } + + /// Puts a shadowed write through to the Mega II as well. + /// + /// Every program written before this machine existed puts its + /// display in bank $00, and the screen is in bank $e0; this is + /// the wire between them. Without it a IIgs shows nothing at + /// all, which is exactly how the want of it was found — the + /// firmware wrote its startup text into bank $00 and the screen + /// stayed empty through eight million instructions. + fn shadowWrite(self: *Self, bank: u8, within: u16, value: u8) void { + if (!self.shadowed(bank, within)) return; + const auxiliary = self.megaHalf(bank, within, true); + if (self.mega2.halfTarget(auxiliary, within, true)) |byte| + byte.* = value; + } + /// Works out where an address lands, without touching /// anything: `peek`, `read` and `write` all go through this so /// that there is one map rather than three. @@ -417,13 +530,8 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { if (self.rom_image) |image| return .{ .rom = image.firmware()[within] }; return .nothing; } - // Which half, and this is the whole of the seam: - // with the latch set the bank address says, and - // with it clear the Mega II's own switches do. - const auxiliary = if (self.new_video & bank_latch != 0) - bank & 1 != 0 - else - self.mega2.auxiliaryFor(within, writing); + // Which half, and this is the whole of the seam. + const auxiliary = self.megaHalf(bank, within, writing); if (self.mega2.halfTarget(auxiliary, within, writing)) |byte| return .{ .ram = byte }; // Not RAM at the moment: the language card is @@ -648,6 +756,11 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { const bank: u8 = @truncate(address >> 16); const within: u16 = @truncate(address); if (bank == slow_bank or bank == slow_bank + 1) return slow_cycle; + // 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 (bank != 0x00 and bank != 0x01) return 1; if (within < io_from or within >= io_to) return 1; @@ -733,7 +846,14 @@ pub fn AppleIIgs(comptime options: Options) fn (type) type { else => |where| { self.getCPU().clock.sleep(spend); switch (where) { - .ram => |byte| byte.* = value, + .ram => |byte| { + byte.* = value; + self.shadowWrite( + @truncate(address >> 16), + @truncate(address), + value, + ); + }, // The cycle is spent and the bus carries the // access either way; ROM simply declines to // latch it. @@ -828,6 +948,10 @@ const TestCPU = @import("../cpu16.zig").Chip( AppleIIgs(.{}), ); +/// The model itself, for the tests that want to name one of its constants +/// rather than write the bit out as a number. +const TestMemory = @FieldType(TestCPU, "memory"); + /// A ROM image with nothing in it but the marks that say which bank is which, /// and a reset vector pointing somewhere a test can recognise. fn testRom(gpa: std.mem.Allocator, comptime order: iigsrom.Order) ![]u8 { @@ -1176,3 +1300,143 @@ test "the clock is reached a byte at a time, and the border rides along" { try testing.expectEqual(@as(u32, 0), cpu.memory.unimplemented[0x33]); try testing.expectEqual(@as(u32, 0), cpu.memory.unimplemented[0x34]); } + +test "a write to the old display in bank zero is shadowed onto the screen" { + // The whole reason shadowing exists. A program written for a IIe puts a + // character in text page 1 of bank $00; the screen is the Mega II's, in + // bank $e0; and this is the only thing that carries the one to the other. + // Without it the firmware's own startup message goes nowhere, which is how + // the want of this was noticed. + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + + cpu.memory.write(0x000400, 0xc1, .data); + try testing.expectEqual(@as(u8, 0xc1), cpu.memory.peek(0x000400)); + try testing.expectEqual(@as(u8, 0xc1), cpu.memory.mega2.peek(0x0400)); + + // And a reset leaves the register at zero, which shadows everything: both + // Hi-Res pages as well. + cpu.memory.write(0x002000, 0x3c, .data); + cpu.memory.write(0x004000, 0x5a, .data); + try testing.expectEqual(@as(u8, 0x3c), cpu.memory.mega2.peek(0x2000)); + try testing.expectEqual(@as(u8, 0x5a), cpu.memory.mega2.peek(0x4000)); + + // Memory outside the display buffers is not shadowed, and stays on the + // fast side where it belongs. + cpu.memory.write(0x001000, 0x99, .data); + try testing.expectEqual(@as(u8, 0x99), cpu.memory.peek(0x001000)); + try testing.expectEqual(@as(u8, 0x00), cpu.memory.mega2.peek(0x1000)); +} + +test "every bit of the Shadow register is an inhibit" { + // Set means off, which is the opposite way round from how the name reads, + // and is why a register of zeroes shadows the lot. + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + + cpu.memory.write(0x00c035, 0x01, .data); // text page 1 inhibited + cpu.memory.write(0x000400, 0x11, .data); + try testing.expectEqual(@as(u8, 0x11), cpu.memory.peek(0x000400)); + try testing.expectEqual(@as(u8, 0x00), cpu.memory.mega2.peek(0x0400)); + + // Hi-Res page 1 is still shadowed, its own bit being clear. + cpu.memory.write(0x002000, 0x22, .data); + try testing.expectEqual(@as(u8, 0x22), cpu.memory.mega2.peek(0x2000)); + + // And clearing the bit again lets text through. + cpu.memory.write(0x00c035, 0x00, .data); + cpu.memory.write(0x000401, 0x33, .data); + try testing.expectEqual(@as(u8, 0x33), cpu.memory.mega2.peek(0x0401)); +} + +test "the auxiliary Hi-Res bit takes the odd bank's pages and leaves the even one's" { + // Bit 4 is the odd one out: it does not name a buffer, it names the banks + // the other bits apply to. "Shadowing of Hi-Res graphics pages in the main + // bank remains unaffected." + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + + // With the latch set, bank $01 is the auxiliary half outright. + cpu.memory.write(0x00c029, 0x01, .data); + // Bit 4 set, and bit 3 too, so that the Super Hi-Res path does not carry + // the write instead. + cpu.memory.write(0x00c035, 0x18, .data); + + cpu.memory.write(0x012000, 0xaa, .data); + cpu.memory.write(0x002000, 0xbb, .data); + try testing.expectEqual(@as(u8, 0x00), cpu.memory.peek(0xe12000)); + try testing.expectEqual(@as(u8, 0xbb), cpu.memory.peek(0xe02000)); +} + +test "the Super Hi-Res buffer is the odd bank's alone, and lies over the Hi-Res pages" { + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + cpu.memory.write(0x00c029, 0x01, .data); // the bank latch + + // Above $6000 there is no Hi-Res page underneath, so only bit 3 answers + // for it — and only in the odd bank. + cpu.memory.write(0x016000, 0x77, .data); + cpu.memory.write(0x006000, 0x88, .data); + try testing.expectEqual(@as(u8, 0x77), cpu.memory.peek(0xe16000)); + try testing.expectEqual(@as(u8, 0x00), cpu.memory.peek(0xe06000)); + + // Under $6000 the two paths overlap and either one is enough: with the + // Hi-Res pages inhibited in the odd bank, the Super Hi-Res bit still + // carries the write. + cpu.memory.write(0x00c035, TestMemory.inhibit_auxiliary_hires, .data); + cpu.memory.write(0x013000, 0x99, .data); + try testing.expectEqual(@as(u8, 0x99), cpu.memory.peek(0xe13000)); + + // Inhibit that as well and nothing gets through. + cpu.memory.write( + 0x00c035, + TestMemory.inhibit_auxiliary_hires | TestMemory.inhibit_super_hires, + .data, + ); + cpu.memory.write(0x013001, 0x9a, .data); + try testing.expectEqual(@as(u8, 0x00), cpu.memory.peek(0xe13001)); +} + +test "banks above the first two are shadowed only when the Speed register says so" { + // There is no fast RAM up there on this machine yet, so this asks the + // decision rather than the memory. The manual's advice is not to. + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + + try testing.expect(cpu.memory.shadowed(0x00, 0x0400)); + try testing.expect(cpu.memory.shadowed(0x01, 0x0400)); + try testing.expect(!cpu.memory.shadowed(0x02, 0x0400)); + + cpu.memory.write(0x00c036, cpu.memory.speed | TestMemory.shadow_every_bank, .data); + try testing.expect(cpu.memory.shadowed(0x02, 0x0400)); +} + +test "a shadowed write is slow and a read of the same place is fast" { + // "Only write instructions to the video locations require the system to + // operate at 1.024 MHz." Reads come from the fast side, which is the whole + // point of shadowing rather than simply putting the display in $e0. + var cpu: TestCPU = undefined; + try cpu.init(testing.allocator, null); + defer cpu.deinit(); + + const before = cpu.clock.read(); + _ = cpu.memory.read(0x000400, .data); + const reading = cpu.clock.read() - before; + cpu.memory.write(0x000400, 0x00, .data); + const writing = cpu.clock.read() - before - reading; + + try testing.expectEqual(@as(u64, 1), reading); + try testing.expectEqual(@as(u64, slow_cycle), writing); + + // With the shadowing turned off the write is as fast as the read: there is + // no second trip to make. + cpu.memory.write(0x00c035, 0x3f, .data); + const quiet = cpu.clock.read(); + cpu.memory.write(0x000400, 0x00, .data); + try testing.expectEqual(@as(u64, 1), cpu.clock.read() - quiet); +}