Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The Apple IIc Plus's multidrive interface glue: a custom chip and two//! kilobytes of static RAM, sitting in the hole its firmware leaves at `$ce00`.//!//! "The MIG is actually two ICs: a custom integrated circuit (the Mc7 chip)//! and a 2 KB static RAM. The static RAM is used as a RAM buffer to support//! the high data rate of the 3.5-inch disk drives. The MIG chip provides the//! interface (the 'glue logic') that implements the RAM buffer for the//! internal disk drive and some interface logic for other disk drives" (Apple//! Computer, Inc., 1989b, p. 327). It is the one piece of a IIc Plus that no//! earlier IIc has, and nothing in the machine works without it: bank one of//! the ROM leaves `$ce00` to `$ceff` filled with zeros because that space is//! this chip, and the firmware puts its own jump vectors in the RAM and jumps//! through them.//!//! **What the manual says about this chip is not entirely right, and the//! corrections come from MG (2018)**, whose page is the only account of it//! that is any good and which was checked against the schematic and against//! working emulation. Three of them matter here. `EN2X*`, which Apple calls//! "input that indicates an access to address $C0nx is occuring", is in fact//! "connected to the drive 2 enable line of the IWM". `RESET*`, described as//! "System reset or ROM address 14", literally means that: ROM address 14 is//! tied to the chip's reset line, so "the MIG is held reset whenever the ROM//! is in the main bank". And the two latched outputs the disk needs are//! reached by *reading* one address and *writing* another, which is why this//! had them confused for a long time.//!//! The processor sees a thirty-two byte window on a two kilobyte buffer. The//! RAM takes "system address lines" A0 to A4 and six more, RAMA5 to RAMA10,//! from the MIG itself (Apple Computer, Inc., 1989b, Table 11-24, p. 346), so//! the chip holds the page and the processor only ever sees a window of it.//! Which page that is, and what the latched outputs do, the manual does not//! say: Table 11-15 (p. 328) names the pins — `IWMRES*` "latched output bit to//! reset IWM", `INTEN*` "latched output bit to enable internal drive",//! `3.5 DRIVE*` "latched output bit to select 5.25-inch or 3.5-inch drives"//! and `HDSEL` "latched output bit used for 3.5-inch drives" — and stops//! there. The rest is read out of the ROM 5 firmware, and each piece says//! below where it came from.//!//! All four of those latched outputs are placed. What is left of the chip is//! two things the firmware never touches and one it does. `$ce80` "affects//! signal on ENB2" and the firmware "does not appear to use" it (MG, 2018);//! counting every access in `$c800-$cfff` over a boot and a whole 800K format//! agrees — not once, either way. Neither is `$cc00`, which is where a second//! address for the supposedly latched `IWMRES*` would sit if there were one.//! The one it does touch is `$c840`, `$c880` and `$c8c0`, written `$81` once//! each from the routine at bank 1 `$ef02` on its way into the SmartPort//! firmware. MG lists all three under MIG control and says nothing about what//! they do, and nothing here can say either: they are in the half of the//! window that is shared with the ROM, so a write to one is dropped whether//! the chip is watching or not, and no test can tell the two apart.
const std = @import("std");
/// How much static RAM is beside the chip: "a 2 KB static RAM" (Apple/// Computer, Inc., 1989b, p. 327).pub const ram_size = 2048;
/// How much of it the processor sees at once. The RAM takes A0 to A4 from the/// system and RAMA5 to RAMA10 from the MIG (Apple Computer, Inc., 1989b,/// Table 11-24, p. 346), so the window is five address lines wide.pub const window_size = 32;
/// How many windows the buffer holds, which is what the six lines the chip/// supplies can reach.pub const pages = ram_size / window_size;
/// Where the chip answers, and how wide. The three address lines above the/// window — A5, A6 and A7 — divide the space into eight registers, and A9,/// A10 and A11 are what place it (Apple Computer, Inc., 1989b, Figure 11-13/// and Table 11-15, pp. 327-328).////// Those six are every address line the chip has. **A8 is not among them**,/// so the chip cannot tell `$ce00` from `$cf00` and answers across both/// pages, each register appearing at two addresses. The firmware's ROM says/// the same thing from the other side: bank 1 leaves `$ce00-$cfff` as five/// hundred and twelve zero bytes, two pages of hole rather than one, because/// both are the chip's rather than the ROM's.pub const base = 0xce00;pub const size = 0x200;
/// Which of the eight the address falls in.const Register = enum(u3) { /// `$ce00` to `$ce1f`: the window on the RAM buffer. window = 0, /// `$ce20`: read to step the page on. The firmware reads this far more /// than anything else — thirty-four bare `BIT`s in bank one — and the /// copying loop at `$ca71` is what says what it does: it reads a byte of /// the window, touches this twice, and writes the byte back to the same /// window address, which only moves anything if the page underneath has /// changed. Read out of the ROM 5 firmware. step = 1, /// `$ce40`. **Two different latches, depending on which way the access /// goes**, which is the thing this had wrong for a long time: /// /// - read: reset `HDSEL` /// - write: assert `3.5DRIVE*` /// /// (MG, 2018, "MIG RAM / Control"). It was modelled as one line moved by /// either, and the observation that pointed at the truth was already /// written down here: over a boot this address is read sixty-one times and /// written six, `$ce60` read seven times and written six, and making the /// line move on a read alone still booted the drive in the case. The reads /// were carrying `HDSEL`, the writes something else entirely, and the /// something else is the line that decides whether the port outside is /// talking to a 3.5-inch drive or a five-and-a-quarter one. /// /// `HDSEL` is the fourth bit of the address that names one of a 3.5-inch /// drive's registers — a IIgs takes it from bit 7 of `$c031`, which a IIc /// Plus does not have. The routine at `$ea15` touches this and then reads /// register `$9`, and the one at `$e4c7` touches `$ce60` and reads register /// `$b`, which are the same three phase lines with the fourth the other way /// about. select_low = 2, /// `$ce60`, the other half of the pair: a read sets `HDSEL`, a write /// de-asserts `3.5DRIVE*`. See `select_low`. select_high = 3, /// `$ce80`. Never referenced. See `seven`. four = 4, /// `$cea0`: read to put the page back to nought. The firmware touches this /// before it uses the window for anything it means to find again — the /// SmartPort dispatch at `$e446` does it before building the vector it /// then jumps through at `$e489`. Read out of the ROM 5 firmware. home = 5, /// `$cec0`. Never referenced. See `seven`. six = 6, /// `$cee0`, and the two beside it at `$ce80` and `$cec0`: the three the /// firmware never uses. /// /// This was measured twice over, because one of the chip's latched /// outputs — `3.5 DRIVE*`, "latched output bit to select 5.25-inch or /// 3.5-inch drives" (Apple Computer, Inc., 1989b, Table 11-15, p. 328) — /// has to be written from somewhere, and these three are what is left. /// Neither reading found it. Statically, no instruction in either bank /// names an address that falls in any of the three; the references that /// look like it are a table of ascending bytes and a run of high-ASCII /// message text being disassembled as code. Dynamically, a IIc Plus /// booted with a five-and-a-quarter-inch disk in the drive touches /// registers nought, one, two, three and five and touches these three /// exactly nought times. /// /// So `3.5 DRIVE*` is not written through a register of its own, and a /// five-and-a-quarter-inch drive on the back of a IIc Plus stays out of /// reach. Driving it from the spare writes on the SEL pair was tried: it /// turns the drive's motor and spins the surface under the head, which /// nothing here had managed before, but the head never steps and no /// machine boots, so it is a coincidence rather than the answer and is /// not what this models. seven = 7,};
pub const Mig = struct { /// The buffer itself. "Do not attempt to use the MIG RAM for program data" /// (Apple Computer, Inc., 1989b, p. 346) — it belongs to the disk. ram: [ram_size]u8 = @splat(0),
/// The six address lines the chip supplies, RAMA5 to RAMA10. page: u6 = 0,
/// `HDSEL`, the fourth bit of the address that names a 3.5-inch drive's /// register. Reset by a **read** of `$ce40`, set by a **read** of `$ce60` /// (MG, 2018). sel: bool = false,
/// `3.5DRIVE*`, "latched output bit to select 5.25 in. or 3.5 in. drives" /// (Apple Computer, Inc., 1989b, Table 11-15, p. 328) — asserted by a /// **write** to `$ce40` and de-asserted by a **write** to `$ce60` (MG, /// 2018). /// /// It is what a IIgs keeps in bit 6 of `$c031`, and it is the reason a IIc /// Plus could not boot a five-and-a-quarter disk here: the port was pinned /// to 3.5 for the life of the machine, so the phase lines were always read /// as a register address and never as four magnets. /// /// **Routed to the external connector only.** "In the IIc Plus, the /// internal 3.5" drive does not need the 3.5DRIVE signal asserted in order /// to operate" (MG, 2018), so the drive in the case is a 3.5-inch drive /// whatever this says, and this decides what is on the other end of the /// cable. /// /// **It comes out of reset de-asserted**, so a machine nobody has told /// otherwise is talking to a five-and-a-quarter drive, and that follows /// from the reset line rather than from a preference. /// /// The chip is held in reset for as long as the main ROM bank is selected, /// and the code that boots a five-and-a-quarter disk is in that bank: the /// two-instruction read loop at `$c65e`, `LDA $C08C,X` and `BPL` back. So /// while that loop runs this latch cannot be anything but its reset value, /// and a real IIc Plus boots such a disk — therefore the reset value is the /// one that means five-and-a-quarter. The 3.5-inch driver lives in the /// other bank, where the chip is not held, and asserts the line itself. external_35: bool = false,
/// `INTEN*`, "latched output bit to enable internal drive" — asserted by a /// write to `$cc80` and de-asserted by a write to `$ccc0`. /// /// This is the latch nothing here could place, and it is what the drive /// select being the wrong way about was really about. The IWM's drive 2 /// select is an *input* to this chip: pin 11 is "input from IWM drive 2 /// select", "routed to ENB2* when external drives are selected (W $CCC0), /// to INTEN* when internal drive is selected (W $CC80)" (MG, 2018). So the /// drive in the case is the one this port selects second because the chip /// sends the second select to it, not because anything is reversed. /// /// **It comes out of reset asserted**, which is not in the page and was /// read off the machine: over a whole boot of the drive in its case, ROM 5 /// writes neither `$cc80` nor `$ccc0` even once, and that drive is /// nonetheless what answers. So the chip powers up pointing the second /// select at the drive in the case, and the firmware only has anything to /// say when it wants the port outside instead. internal_enabled: bool = true,
/// `IWMRES*`, "latched output bit to reset IWM" (Apple Computer, Inc., /// 1989b, Table 11-15, p. 328), pulsed by a write to `$cc40`. /// /// This is a pulse and not a level, and the hedge belongs on it. MG has it /// as "(probably) /IWMRES" and "set (or pulsed?) by W $CC40", and says the /// manual calling it latched "would expect a $CC00/$CC40 combo, but no /// evidence for that" (MG, 2018, MIG Address Space → MIG Control). The /// firmware settles that much from this end: over a boot of ROM 5 and a /// whole 800K format, `$cc00` is not read or written **once**, so if there /// is a second address that lets the line go it is not one this machine /// uses. Nothing is latched here. /// /// It is written by one routine and only one, three instructions at /// bank 1 `$cd1c` — `STA $CC40`, `STA $CC80`, `STA $CCC0`, `RTS` — which /// resets the chip and then swings the drive 2 select to the internal /// drive and back out again. Four places call it: twice around the device /// scan at `$e603` and `$e652`, once in the retry loop at `$e524`, and /// once at `$efc6`. It is "put the disk port back to a known state", and /// over the same boot and format it runs exactly twice. /// /// The flag is read and cleared by whoever is holding the IWM, since the /// chip's outputs are wires and this one goes to a pin on another chip. iwm_reset: bool = false,
/// Whichever of the eight an address in the chip's page falls in. fn registerOf(address: u16) Register { return @enumFromInt(@as(u3, @truncate(address >> 5))); }
/// Where in the buffer the window is pointing. fn at(self: *const Mig, address: u16) usize { return @as(usize, self.page) * window_size + (address & (window_size - 1)); }
/// One access to the chip's page. Reading and writing the window reach the /// buffer; the rest are the chip's own registers, and touching one is /// enough — these are switches, not memory, so a read does as much as a /// write. pub fn access(self: *Mig, address: u16, is_write: bool, value: u8) u8 { switch (registerOf(address)) { .window => { const where = self.at(address); if (is_write) self.ram[where] = value; return self.ram[where]; }, .step => { // The byte written here is not thrown away: `$ce20` is the // address one past the window, so it belongs to the next // page, and reaching it is what carries the page on. The // firmware's data loop is what says so — it fills a sector // with `LDX #$e0 / STA $cd21,X`, and `$cd21 + $ff` is `$ce20`, // so every thirty-second store lands here and must keep its // byte or the sector loses one in thirty-two and fails its // checksum. Read out of the ROM 5 firmware at `$e191`. self.page +%= 1; const where = @as(usize, self.page) * window_size; if (is_write) self.ram[where] = value; return self.ram[where]; }, .home => { self.page = 0; return 0; }, .select_low => { if (is_write) { self.external_35 = true; } else { self.sel = false; } return 0; }, .select_high => { if (is_write) { self.external_35 = false; } else { self.sel = true; } return 0; }, else => return 0, } }
/// The reset line reaches the chip: `IOSEL*` is "tied to RESET*" (Apple /// Computer, Inc., 1989b, Table 11-15, p. 328). What it puts back is the /// page and the SEL line; the buffer is memory and keeps what it held. pub fn reset(self: *Mig) void { self.page = 0; self.hold(); }
/// The reset line held down, which is what the main ROM bank being /// selected does: ROM address 14 is tied to it (MG, 2018). /// /// The latched outputs go back; the buffer and the page do not. The buffer /// is a separate chip with its own supply, and the firmware fills it in one /// bank and reads it in the other, which it could not do if the window /// moved underneath it. pub fn hold(self: *Mig) void { self.sel = false; self.external_35 = false; self.internal_enabled = true; }
/// The control strobes that are not in the chip's own page. /// /// These are outside `$ce00` and the ROM still answers a read of them, /// because **the chip has no data bus pins at all** (MG, 2018, "Analysis") /// and so can never drive a byte. What it does is watch the bus and latch, /// which is why the firmware reaches them with writes: `$cc80` and `$ccc0` /// are where the drive 2 select is pointed, and `$cc40` resets the IWM. pub fn strobe(self: *Mig, address: u16, is_write: bool) void { if (!is_write) return; switch (address & 0x0fe0) { 0x0c40 => { self.iwm_reset = true; }, 0x0c80 => { self.internal_enabled = true; }, 0x0cc0 => { self.internal_enabled = false; }, else => {}, } }
/// Takes the `IWMRES*` pulse, if there has been one since it was last /// asked. A pulse is a thing that happened rather than a state, so it is /// consumed by reading it. pub fn takeIwmReset(self: *Mig) bool { defer self.iwm_reset = false; return self.iwm_reset; }
/// Whether an address is one of those strobes. The chip decodes A5 to A11 /// and nothing above, so this is a question about eleven address lines. pub fn strobes(address: u16) bool { return switch (address & 0x0fe0) { 0x0c40, 0x0c80, 0x0cc0 => true, else => false, }; }};
const testing = std.testing;
test "the window is thirty-two bytes on a two kilobyte buffer" { try testing.expectEqual(@as(usize, 64), pages); var mig: Mig = .{}; // The window's own address lines are the low five. _ = mig.access(0xce00, true, 0xaa); _ = mig.access(0xce1f, true, 0xbb); try testing.expectEqual(@as(u8, 0xaa), mig.ram[0]); try testing.expectEqual(@as(u8, 0xbb), mig.ram[31]);}
test "stepping moves the window on by a page and home puts it back" { var mig: Mig = .{}; _ = mig.access(0xce00, true, 0x11); _ = mig.access(0xce20, false, 0); // step try testing.expectEqual(@as(u6, 1), mig.page); _ = mig.access(0xce00, true, 0x22); // Two different pages, so two different bytes of the buffer. try testing.expectEqual(@as(u8, 0x11), mig.ram[0]); try testing.expectEqual(@as(u8, 0x22), mig.ram[32]); _ = mig.access(0xcea0, false, 0); // home try testing.expectEqual(@as(u6, 0), mig.page); try testing.expectEqual(@as(u8, 0x11), mig.access(0xce00, false, 0));}
test "a byte written one past the window belongs to the next page" { var mig: Mig = .{}; // What the firmware's sector loop does: thirty-one bytes into the window, // then a thirty-second that lands on `$ce20` and carries the page on. The // byte must survive, or a sector loses one in thirty-two. _ = mig.access(0xce1f, true, 0x7e); _ = mig.access(0xce20, true, 0x7f); try testing.expectEqual(@as(u6, 1), mig.page); try testing.expectEqual(@as(u8, 0x7e), mig.ram[31]); try testing.expectEqual(@as(u8, 0x7f), mig.ram[32]); // And the window now shows it as the first byte of the new page. try testing.expectEqual(@as(u8, 0x7f), mig.access(0xce00, false, 0));}
test "the two select addresses put the SEL line up and down" { var mig: Mig = .{}; _ = mig.access(0xce60, false, 0); try testing.expect(mig.sel); _ = mig.access(0xce40, false, 0); try testing.expect(!mig.sel); _ = mig.access(0xce60, false, 0); mig.reset(); try testing.expect(!mig.sel);}
test "the chip has no A8 and so answers the same at $cf00 as at $ce00" { // Every address line the chip is given is in its pinout: A5, A6 and A7 // pick the register, A9, A10 and A11 place the whole thing (Apple // Computer, Inc., 1989b, Figure 11-13, p. 327). A8 is not there, so it // cannot be decoded, and the second page is the first one again rather // than anything of its own. var mig: Mig = .{};
// SEL, reached at the high page, moves as it does at the low one. _ = mig.access(0xcf60, false, 0); try testing.expect(mig.sel); _ = mig.access(0xcf40, false, 0); try testing.expect(!mig.sel);
// And so does the window: a byte put in through one page is the same // byte read back through the other, at the same place in the buffer. _ = mig.access(0xcf05, true, 0xa3); try testing.expectEqual(@as(u8, 0xa3), mig.ram[5]); try testing.expectEqual(@as(u8, 0xa3), mig.access(0xce05, false, 0));
// Stepping and homing are the same switches through either page, which // is what would go wrong if A8 were being decoded after all. _ = mig.access(0xcf20, false, 0); try testing.expectEqual(@as(u6, 1), mig.page); _ = mig.access(0xcea0, false, 0); try testing.expectEqual(@as(u6, 0), mig.page);}
test "a write to $cc40 pulses the IWM's reset line, and taking the pulse clears it" { var mig: Mig = .{}; try testing.expect(!mig.iwm_reset); try testing.expect(!mig.takeIwmReset());
mig.strobe(0xcc40, true); try testing.expect(mig.iwm_reset); // A pulse is a thing that happened, so it is gone once it has been // acted on, and a second look finds nothing to do. try testing.expect(mig.takeIwmReset()); try testing.expect(!mig.takeIwmReset());
// It is a write and nothing else. Reads of these addresses are the ROM // being executed -- there is code at bank 1 $cc40 -- and the chip must // not take an instruction fetch for a command. mig.strobe(0xcc40, false); try testing.expect(!mig.iwm_reset);}
test "$cc00 is not one of the chip's addresses, and this firmware never asks it to be" { // The manual calls IWMRES* latched, which would want a second address to // let it go again, and $cc00 is where such a thing would sit. MG looked // for one and found "no evidence for that" (MG, 2018). // // ROM 5 settles it from this end. Over a boot and a whole 800K format -- // every access in $c800-$cfff counted with the auxiliary bank selected -- // $cc00 is not read or written once, while $cc40 is written exactly twice // and $cc80 and $ccc0 two hundred and twenty-five and twenty-four times. // So whatever $cc00 may do, nothing in this machine does it. try testing.expect(!Mig.strobes(0xcc00));
var mig: Mig = .{}; mig.strobe(0xcc00, true); try testing.expect(!mig.iwm_reset); try testing.expect(mig.internal_enabled);}
test "the strobes are the three the chip decodes, and nothing next to them" { // A5 to A11 and no more, so the question is about eleven address lines // and the three chunks that answer are 32 bytes apart. try testing.expect(Mig.strobes(0xcc40)); try testing.expect(Mig.strobes(0xcc5f)); try testing.expect(Mig.strobes(0xcc80)); try testing.expect(Mig.strobes(0xccc0)); try testing.expect(!Mig.strobes(0xcc20)); try testing.expect(!Mig.strobes(0xcc60)); try testing.expect(!Mig.strobes(0xcca0)); try testing.expect(!Mig.strobes(0xcce0));}