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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The Video Graphics Controller's registers.//!//! The chip that draws an Apple IIgs's screen. It reads the Mega II's memory//! and puts a picture out of it, which means it can do everything a IIe's video//! could and several things it could not: colour text on a coloured background,//! a border, monochrome at the flick of a bit, Super Hi-Res with a palette per//! scanline, and a character generator that speaks eight languages.//!//! Five registers reach it from the processor, and four of them are here://!//! - `$c021`, Monochrome/Color. Bit 7 alone: set for black and white, clear//! for colour. Every other bit is reserved.//! - `$c022`, Screen Color. The text colour in the high nibble and the//! background in the low, from the same sixteen colours an Apple II has//! always had.//! - `$c029`, New-Video. Super Hi-Res, its linear buffer, colour or black and//! white Double Hi-Res — and bit 0, the *bank latch*, which is not about//! video at all: it decides whether the low bit of a bank address reaches//! the Mega II's auxiliary 64K directly or whether the old soft switches//! get to say. That bit is the seam between the two halves of the machine.//! - `$c02b`, Language Select. Which of eight character sets the generator//! uses, whether the video is NTSC or PAL, and which of a keyboard's two//! languages is currently selected.//!//! The fifth is the border colour, and it is not here: it shares `$c034` with//! the clock's control register, so it lives with the clock, and the machine//! hands it out as a dot through `borderColour`. That is where the hardware//! put it and there is no arguing with a shared address.//!//! What is *not* here is the drawing. These are the registers, and they read//! back what was written to them, which is what a program setting up a display//! needs. Turning them into a picture is `superhires.zig`'s job for this//! chip's own modes and `video.zig`'s for the ones it inherited.
const std = @import("std");
/// The video controller, as far as the processor can see it.pub const Vgc = struct { /// `$c021`. Only bit 7 means anything. monochrome: bool = false, /// The rest of `$c021`, kept so that a program reading the register back /// finds what it put there. The manual says not to modify these bits, /// which is not the same as saying they are not there. monochrome_reserved: u7 = 0,
/// `$c022`, both nibbles: the text colour and the background it sits on. /// /// What these hold at power-on the manual does not say, and this does not /// guess: they are zero, which is black on black. The firmware reads the /// user's choice out of the clock chip's battery RAM and writes it here, /// which is what makes the Control Panel's colour settings stick. text_colour: u4 = 0, background_colour: u4 = 0,
/// `$c029`. Bit 7 turns Super Hi-Res on, bit 6 linearises its buffer, bit 5 /// chooses colour or black and white for Double Hi-Res, and bit 0 is the /// bank latch. The bits between are reserved. new_video: u8 = 0,
/// Bits 7 to 5 of `$c02b`: which character set the generator uses. language: Language = .english_usa, /// Bit 4: set for PAL, clear for NTSC. A machine sold in a PAL country has /// this set, and its video is 50 Hz and 312 lines rather than 60 and 262. /// Nothing here changes the video timing when it does — the frame is still /// the Mega II's 262 lines — so this reads back and no more. pal: bool = false, /// Bit 3: which of the keyboard's two languages is selected. Clear is the /// primary one, which is the language in the field above; set is the /// secondary, which for every setting but the US one is US English. secondary_language: bool = false,
/// `$c023`, the VGC Interrupt register: a status bit and an enable bit for /// each of two interrupts the video controller can raise. /// /// The status bits are the hardware's to set and the enable bits are /// software's — "software can directly manipulate only the enable bits in /// the VGC Interrupt register; writing to the other bit positions has no /// effect" — and clearing a status bit is done at a *different* address, /// `$c032`, and by writing a **zero** into it. Which is worth saying twice, /// because every other clear-an-interrupt register in this machine is /// touched rather than written and this one takes a value with the bit /// down. one_second: bool = false, one_second_enabled: bool = false, scan_line: bool = false, scan_line_enabled: bool = false,
/// The character sets the generator can be asked for, in the order the /// Firmware Reference numbers them (Apple Computer, Inc., 1987a). Each has a /// second language behind it, reached with bit 3, and for all but the first /// that second language is US English; the US machine's is Dvorak instead. pub const Language = enum(u3) { english_usa = 0, english_uk = 1, french = 2, danish = 3, spanish = 4, italian = 5, german = 6, swedish = 7, };
/// The bank latch, bit 0 of the New-Video register. pub const bank_latch: u8 = 0x01;
/// And bit 7 of it, which is the one that matters most: with it set the /// old Apple II video is gone and the controller has the memory to itself. pub const super_hires: u8 = 0x80;
/// The VGC Interrupt register's bits, at `$c023`. The top three are status /// and read-only; the two enables are the only ones a program can write. pub const interrupting_bit: u8 = 0x80; pub const one_second_status: u8 = 0x40; pub const scan_line_status: u8 = 0x20; pub const one_second_enable: u8 = 0x04; pub const scan_line_enable: u8 = 0x02;
/// Whether the controller is asking for an interrupt, which is what bit 7 /// of its interrupt register reports: "this bit is set when the interrupt /// bit and the status bit are set for one or more of the interrupts". pub fn interrupting(self: *const Vgc) bool { return (self.one_second_enabled and self.one_second) or (self.scan_line_enabled and self.scan_line); }
pub fn interruptRegister(self: *const Vgc) u8 { var value: u8 = 0; if (self.interrupting()) value |= interrupting_bit; if (self.one_second) value |= one_second_status; if (self.scan_line) value |= scan_line_status; if (self.one_second_enabled) value |= one_second_enable; if (self.scan_line_enabled) value |= scan_line_enable; return value; }
/// Writing the interrupt register reaches the two enable bits and nothing /// else. A program cannot set a status bit by hand, and cannot clear one /// here either. pub fn setInterruptRegister(self: *Vgc, value: u8) void { self.one_second_enabled = value & one_second_enable != 0; self.scan_line_enabled = value & scan_line_enable != 0; }
/// `$c032`, the VGC Interrupt-Clear register, whose polarity is the other /// way round from everything near it: "to clear the scan-line and /// one-second status bits, write a 0 into the corresponding bit position /// ... writing a 1 into these positions or writing into the other bit /// positions has no effect". pub fn clearInterrupts(self: *Vgc, value: u8) void { if (value & one_second_status == 0) self.one_second = false; if (value & scan_line_status == 0) self.scan_line = false; }
pub fn monochromeRegister(self: *const Vgc) u8 { var value: u8 = self.monochrome_reserved; if (self.monochrome) value |= 0x80; return value; }
pub fn setMonochromeRegister(self: *Vgc, value: u8) void { self.monochrome = value & 0x80 != 0; self.monochrome_reserved = @truncate(value); }
pub fn screenColour(self: *const Vgc) u8 { return @as(u8, self.text_colour) << 4 | self.background_colour; }
pub fn setScreenColour(self: *Vgc, value: u8) void { self.text_colour = @truncate(value >> 4); self.background_colour = @truncate(value); }
pub fn languageSelect(self: *const Vgc) u8 { var value: u8 = @as(u8, @intFromEnum(self.language)) << 5; if (self.pal) value |= 0x10; if (self.secondary_language) value |= 0x08; return value; }
pub fn setLanguageSelect(self: *Vgc, value: u8) void { self.language = @enumFromInt(@as(u3, @truncate(value >> 5))); self.pal = value & 0x10 != 0; self.secondary_language = value & 0x08 != 0; // Bits 2, 1 and 0 must be written as zero, so there is nothing to // keep: a program reading the register back gets zeroes whatever it // wrote, which is what the hardware does with bits that are not there. }
/// What a reset does. Every one of these is a register in the chip rather /// than memory, so a reset takes the lot. pub fn reset(self: *Vgc) void { self.* = .{}; }};
const testing = std.testing;
test "the monochrome bit is bit seven and the rest reads back" { var vgc: Vgc = .{}; try testing.expect(!vgc.monochrome);
vgc.setMonochromeRegister(0x80); try testing.expect(vgc.monochrome); try testing.expectEqual(@as(u8, 0x80), vgc.monochromeRegister());
// The reserved bits are reserved, not absent: a read-modify-write that // sets bit 7 must not lose whatever else was there. vgc.setMonochromeRegister(0x05); try testing.expect(!vgc.monochrome); try testing.expectEqual(@as(u8, 0x05), vgc.monochromeRegister()); vgc.setMonochromeRegister(vgc.monochromeRegister() | 0x80); try testing.expectEqual(@as(u8, 0x85), vgc.monochromeRegister());}
test "the screen colour register is two nibbles" { // Text in the high one and background in the low, which is the order they // are written in and the opposite of the order they are read out loud. var vgc: Vgc = .{}; vgc.setScreenColour(0xf0); // white on black try testing.expectEqual(@as(u4, 0xf), vgc.text_colour); try testing.expectEqual(@as(u4, 0x0), vgc.background_colour); try testing.expectEqual(@as(u8, 0xf0), vgc.screenColour());
vgc.setScreenColour(0x1e); try testing.expectEqual(@as(u4, 0x1), vgc.text_colour); try testing.expectEqual(@as(u4, 0xe), vgc.background_colour);}
test "the language register names a character set, a video standard and a switch" { var vgc: Vgc = .{}; // A machine as it leaves the factory in the United States: English, NTSC, // primary language. try testing.expectEqual(@as(u8, 0x00), vgc.languageSelect());
// German, PAL, secondary language selected — which for a German machine // means US English. vgc.setLanguageSelect(0xd8); try testing.expectEqual(Vgc.Language.german, vgc.language); try testing.expect(vgc.pal); try testing.expect(vgc.secondary_language); try testing.expectEqual(@as(u8, 0xd8), vgc.languageSelect());
// The low three bits must be written as zero and are not kept. vgc.setLanguageSelect(0x47); try testing.expectEqual(Vgc.Language.french, vgc.language); try testing.expectEqual(@as(u8, 0x40), vgc.languageSelect());}
test "the bank latch is bit zero of the New-Video register" { // Which is the one bit in this chip that is not about video at all: it is // the seam between the fast side of the machine and the Mega II. var vgc: Vgc = .{}; try testing.expectEqual(@as(u8, 0), vgc.new_video & Vgc.bank_latch); vgc.new_video = 0xc1; try testing.expectEqual(Vgc.bank_latch, vgc.new_video & Vgc.bank_latch);}