// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! Working out how an Apple IIgs system ROM image is laid out. //! //! Nothing loads one of these yet: there is a 65816 in `cpu16.zig` and no //! machine to put it in. This is here because identifying the image turned out //! to be the first real piece of work in that direction, and because the //! answer is not what you would guess. //! //! A IIgs ROM covers the top banks of the address space — two of them for a //! 128K ROM 00 or ROM 01, four for a 256K ROM 3 — and **the banks are not //! always in the same order in the file**. ROM 01 puts the high bank first; //! ROM 00 puts it last. Worse, two dumps of the same part disagree: the copies //! of ROM 3's `341-0748` and `341-0749` that circulate with "(new)" in the //! name are the opposite way round from the ones without. So the order cannot //! be taken from the size, the name or the part number, and has to be found in //! the image. //! //! It can be. Bank `$ff` holds the Apple II firmware, which has not moved in //! decades and is unmistakable: the reset vector is `$fa62`, the Monitor's //! reset entry; `$fbb3` and `$fbc0` are `$06` and `$e0`, which are the bytes a //! IIe answers with when a program asks what machine it is on; and `$fded` is //! `6c 36 00`, `COUT` jumping through the output vector, on every machine in //! the family since 1977. All four together are not going to appear by //! accident, and the bank that has them is bank `$ff`. //! //! The rest follows: the banks run in order from there, and whether that order //! is up or down is decided by which end of the image the firmware is at. const std = @import("std"); /// A bank of the address space is sixty-four kilobytes, and a ROM image is a /// whole number of them. pub const bank_size = 0x10000; /// The bank that holds the Apple II firmware. Always the top of the address /// space, whatever end of the file it happens to be at. pub const firmware_bank: u8 = 0xff; /// Which way the banks run through the file. pub const Order = enum { /// The lowest bank first, so the firmware is at the end. ROM 00, and the /// 256K images. ascending, /// The highest bank first, so the firmware is at the start. ROM 01. descending, }; pub const Error = error{ /// Not a whole number of banks, or not a number of banks a ROM comes in. WrongSize, /// No part of it looks like the Apple II firmware. Either it is not a /// system ROM at all, or it is one half of a split one: the `341-0728` /// file that carries banks `$fc` and `$fd` of a ROM 3 is a real ROM image /// and has none of the firmware in it. NoFirmwareBank, /// The firmware is in the middle of the image rather than at one end, /// which no dump seen so far does and which leaves no way to say which /// order the rest are in. FirmwareBankNotAtAnEnd, }; /// A ROM image whose layout has been worked out. pub const Image = struct { bytes: []const u8, order: Order, /// The lowest bank the image holds. The highest is always `$ff`. lowest_bank: u8, /// How many banks are in it. pub fn count(self: Image) u8 { return @intCast(self.bytes.len / bank_size); } /// The sixty-four kilobytes of one bank, or null if the image has no such /// bank in it. pub fn bank(self: Image, which: u8) ?[]const u8 { if (which < self.lowest_bank or which > firmware_bank) return null; const from_lowest: usize = which - self.lowest_bank; const index = switch (self.order) { .ascending => from_lowest, .descending => self.count() - 1 - from_lowest, }; return self.bytes[index * bank_size ..][0..bank_size]; } /// The bank with the Apple II firmware in it, which is the one a machine /// maps into bank zero to start from. pub fn firmware(self: Image) []const u8 { return self.bank(firmware_bank).?; } }; /// Whether a sixty-four kilobyte block is the one with the firmware in it. /// /// Four marks, all of which have to be there. Any one of them could turn up by /// chance in a bank of code; four at fixed addresses will not. fn isFirmware(block: []const u8) bool { const reset = @as(u16, block[0xfffc]) | @as(u16, block[0xfffd]) << 8; return reset == 0xfa62 and block[0xfbb3] == 0x06 and block[0xfbc0] == 0xe0 and std.mem.eql(u8, block[0xfded..0xfdf0], &.{ 0x6c, 0x36, 0x00 }); } /// Works out how an image is laid out. pub fn identify(bytes: []const u8) Error!Image { if (bytes.len == 0 or bytes.len % bank_size != 0) return error.WrongSize; const banks = bytes.len / bank_size; // Two for a ROM 00 or 01, four for a whole ROM 3. Nothing else has been // made, and guessing at a size nobody shipped would be guessing. if (banks != 2 and banks != 4) return error.WrongSize; var found: ?usize = null; for (0..banks) |i| { if (isFirmware(bytes[i * bank_size ..][0..bank_size])) { // Two blocks that both look like the firmware would mean the marks // are not the marks; say so rather than picking one. if (found != null) return error.FirmwareBankNotAtAnEnd; found = i; } } const at = found orelse return error.NoFirmwareBank; const order: Order = if (at == banks - 1) .ascending else if (at == 0) .descending else return error.FirmwareBankNotAtAnEnd; return .{ .bytes = bytes, .order = order, .lowest_bank = firmware_bank - @as(u8, @intCast(banks - 1)), }; } /// Builds an image for the tests: `banks` banks of nothing, with the firmware /// marks written into the one at `at`. fn synthesise(gpa: std.mem.Allocator, banks: usize, at: usize) ![]u8 { const bytes = try gpa.alloc(u8, banks * bank_size); @memset(bytes, 0); const block = bytes[at * bank_size ..][0..bank_size]; block[0xfffc] = 0x62; block[0xfffd] = 0xfa; block[0xfbb3] = 0x06; block[0xfbc0] = 0xe0; @memcpy(block[0xfded..0xfdf0], &[_]u8{ 0x6c, 0x36, 0x00 }); return bytes; } test "the firmware at the end means the banks run up" { const bytes = try synthesise(std.testing.allocator, 2, 1); defer std.testing.allocator.free(bytes); const image = try identify(bytes); try std.testing.expectEqual(Order.ascending, image.order); try std.testing.expectEqual(@as(u8, 0xfe), image.lowest_bank); try std.testing.expectEqual(@as(u8, 2), image.count()); // Bank $ff is the second half, and $fe the first. try std.testing.expectEqual(bytes[bank_size..].ptr, image.bank(0xff).?.ptr); try std.testing.expectEqual(bytes.ptr, image.bank(0xfe).?.ptr); try std.testing.expectEqual(@as(?[]const u8, null), image.bank(0xfd)); } test "the firmware at the start means they run down" { // Which is ROM 01, and the reason none of this can be assumed from the // size: it is the same 128K as ROM 00 and the other way round. const bytes = try synthesise(std.testing.allocator, 2, 0); defer std.testing.allocator.free(bytes); const image = try identify(bytes); try std.testing.expectEqual(Order.descending, image.order); try std.testing.expectEqual(@as(u8, 0xfe), image.lowest_bank); try std.testing.expectEqual(bytes.ptr, image.bank(0xff).?.ptr); try std.testing.expectEqual(bytes[bank_size..].ptr, image.bank(0xfe).?.ptr); } test "a whole ROM 3 is four banks" { const bytes = try synthesise(std.testing.allocator, 4, 3); defer std.testing.allocator.free(bytes); const image = try identify(bytes); try std.testing.expectEqual(Order.ascending, image.order); try std.testing.expectEqual(@as(u8, 0xfc), image.lowest_bank); try std.testing.expectEqual(@as(u8, 4), image.count()); for (0..4) |i| { const which: u8 = @intCast(0xfc + i); try std.testing.expectEqual(bytes[i * bank_size ..].ptr, image.bank(which).?.ptr); } try std.testing.expectEqual(@as(?[]const u8, null), image.bank(0xfb)); } test "half of a split ROM 3 has no firmware in it, and says so" { // The 341-0728 file is banks $fc and $fd and is a perfectly good ROM // image; it simply is not one that can be identified this way, and // pretending otherwise would put the firmware somewhere it is not. const bytes = try std.testing.allocator.alloc(u8, 2 * bank_size); defer std.testing.allocator.free(bytes); @memset(bytes, 0); try std.testing.expectError(error.NoFirmwareBank, identify(bytes)); } test "a size no ROM comes in is refused" { try std.testing.expectError(error.WrongSize, identify(&.{})); const odd = try std.testing.allocator.alloc(u8, bank_size + 1); defer std.testing.allocator.free(odd); try std.testing.expectError(error.WrongSize, identify(odd)); const three = try synthesise(std.testing.allocator, 3, 2); defer std.testing.allocator.free(three); try std.testing.expectError(error.WrongSize, identify(three)); }