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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The language card's switches, which more than one machine has.//!//! Sixteen kilobytes of RAM behind the twelve at `$d000` to `$ffff`, where the//! Monitor and Applesoft live in ROM. Sixteen fits into twelve because the four//! kilobytes at `$d000` are two four-kilobyte banks in one window, and three//! soft switches decide which of it all is in view: whether reads come from the//! RAM or the ROM, whether writes reach the RAM, and which of the two banks the//! `$d000` window is showing.//!//! All sixteen addresses from `$c080` to `$c08f` throw those switches, and the//! address bits *are* the switch, which is why so many of them do the same//! thing://!//! - Bit 3 chooses the bank. Clear — `$c080` to `$c087` — is bank 2, and set//! is bank 1, which is the opposite way round from how the numbers read and//! is a trap the manuals fall into as well. The IIgs Hardware Reference//! compounds it: Table 3-2 documents the State register's `LCBNK2` as//! selecting "language-card RAM bank 1" when set and "bank 2" when clear//! (Apple Computer, Inc., 1989a, p. 40), while its chapter on the language//! card numbers the two four-kilobyte *blocks* the other way — "block 1//! occupies address space from $C000 to $CFFF", and "if bit 3 is 0, the//! first bank of RAM is used" (p. 36). Read together: the block the//! register's bit selects when set is the one address bit 3 clear//! selects, which the switches and `RDLCBNK2` call bank 2. Both manuals//! are consistent once you notice they are counting different things,//! and this was checked against the printed pages rather than their OCR.//! - Bit 0 set enables writing, and clear disables it.//! - Reading comes from the RAM when bits 0 and 1 agree, and from the ROM//! when they differ.//!//! And write-enabling takes **two consecutive reads**. One read arms it and the//! second does it; a write to the address disarms it instead. That is the//! `pre_write` latch below, and it exists so that a stray read-modify-write —//! which reads the address and then writes it — cannot enable writing to the//! card by accident. An emulator without it lets programs enable the card in//! one access, which works until it meets a program that relies on not being//! able to.//!//! Two machines here have one of these. An Apple IIe has one, in main memory//! and again in auxiliary. An Apple IIgs has the IIe's, inside its Mega II,//! and the same three switches also govern the fast side's RAM behind the ROM//! in banks `$00` and `$01`: **one set of switches, held in two chips.** The//! Hardware Reference says the State register that carries them exists "in//! both the FPI and the Mega II ICs" and is written at the slow clock, so that//! both chips take the write, and every `$c08x` access is an I/O cycle both//! chips see.//!//! For a long while this model gave the fast side a card of its own, moved//! only by switches touched in banks `$00` and `$01`, on the strength of a ROM//! routine at `$ff/1df7` that sets its data bank register to `$e1` before//! throwing `$c083` twice. That read the routine wrong. The data bank is set//! to `$e1` for the two `STA $da4a` and `STA $da4b` that follow, which save the//! State and Speed registers into the Mega II's card RAM; the switch is simply//! made from the bank the routine is already in. And the hang that seemed to//! confirm the reading — the firmware executing RAM full of zeroes after the//! switch — was the interrupt vectors being pulled from language-card RAM,//! which a IIgs never does (see `decodeVector` in `src/memory16/apple2gs.zig`).//! With that fixed, one card boots the ROM and GS/OS both; two cards left GS/OS//! reading the ROM where it had just put its own code.
const std = @import("std");
/// The three switches and the latch behind the second of them.pub const LanguageCard = struct { /// Which four-kilobyte bank the `$d000` window shows. Set — which is what /// a reset leaves — is the bank the switches call 2, reached at `$c080` /// through `$c087`. bank2: bool = true, /// Whether reads from `$d000` upwards come from the card's RAM rather than /// the ROM. Clear at reset, which is how a machine finds its reset vector. read_ram: bool = false, /// Whether writes reach the RAM. write_ram: bool = true, /// Whether a read has armed the write enable. See the header: it takes two /// consecutive reads, and a write disarms it. pre_write: bool = false,
/// Throws whichever switches the low four bits of the address name. /// /// `is_write` says whether the access was a write, which matters only for /// the arming latch: a write can disable writing but never enable it. pub fn switched(self: *LanguageCard, low: u4, is_write: bool) void { self.bank2 = low & 0x08 == 0; self.read_ram = (low & 0x01) == (low >> 1) & 0x01; if (low & 0x01 == 0) { self.write_ram = false; self.pre_write = false; } else if (is_write) { self.pre_write = false; } else { if (self.pre_write) self.write_ram = true; self.pre_write = true; } }
/// Whether an access to the card's window reaches RAM at all, which is a /// different question for reading and for writing: the ROM can be in the /// window for reads while writes still go into the RAM behind it. pub fn reaches(self: *const LanguageCard, writing: bool) bool { return if (writing) self.write_ram else self.read_ram; }
pub fn reset(self: *LanguageCard) void { self.* = .{}; }};
const testing = std.testing;
test "the address bits are the switch" { var card: LanguageCard = .{};
// $c088: bank 1, read RAM, no writing. card.switched(0x8, false); try testing.expect(!card.bank2); try testing.expect(card.read_ram); try testing.expect(!card.write_ram);
// $c082: bank 2, read ROM, no writing. Bits 0 and 1 differ. card.switched(0x2, false); try testing.expect(card.bank2); try testing.expect(!card.read_ram);
// $c081: bank 2, read ROM, and writing after two reads. card.switched(0x1, false); try testing.expect(!card.read_ram); // $c083: bank 2, read RAM. Bits 0 and 1 agree. card.switched(0x3, false); try testing.expect(card.read_ram);}
test "write-enabling takes two consecutive reads" { // One read arms it and the second does it, so that a read-modify-write — // which reads the address and then writes it — cannot enable the card by // accident. var card: LanguageCard = .{}; card.switched(0x0, false); // $c080: writing off, and the latch cleared try testing.expect(!card.write_ram);
card.switched(0x1, false); // one read arms try testing.expect(!card.write_ram); card.switched(0x1, false); // and the second enables try testing.expect(card.write_ram);
// A write in the middle disarms instead. card.switched(0x0, false); // off again card.switched(0x1, false); // armed card.switched(0x1, true); // and a write takes the arming away try testing.expect(!card.write_ram); card.switched(0x1, true); try testing.expect(!card.write_ram);
// An even address disables writing whatever was armed. card.switched(0x1, false); card.switched(0x1, false); try testing.expect(card.write_ram); card.switched(0x2, false); try testing.expect(!card.write_ram);}
test "a reset leaves the card reading ROM" { // Which is how a machine finds its reset vector at all: the vector is in // the ROM at $fffc, and the card must not be in the way of it. var card: LanguageCard = .{}; card.switched(0x3, false); card.switched(0x3, false); try testing.expect(card.read_ram);
card.reset(); try testing.expect(!card.read_ram); try testing.expect(!card.reaches(false)); try testing.expect(card.reaches(true));}