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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The character generator: which dots a byte in the text page lights up.//!//! Everything else in this emulator is behaviour, worked out from what the//! hardware does. This is the one part that is *data*, because a character//! generator is a ROM full of pictures and there is nothing to derive: an 'A'//! is an 'A' because somebody at Apple drew one. The pictures are in//! `fonts/apple2.zig` and `fonts/apple2e.zig`, transcribed from the chips, and//! this is the logic that sits on top of them — which chip a machine has,//! which entry a screen byte reaches, and what the machine does to the dots on//! the way out.//!//! ## The two families//!//! An Apple II and a II Plus have `341-0036`: sixty-four shapes, uppercase and//! punctuation and nothing else. Inverse and flashing are not in it. They are//! an XOR gate on the way out of the shift register, which is why one bitmap//! serves all four quarters of the byte range — `$00-$3f` inverse, `$40-$7f`//! flashing, `$80-$ff` normal twice over. The blank scan line of each cell is//! at the *top*.//!//! A IIe has `342-0133-A`, or `342-0265-A` if it is an enhanced one, and both//! are two hundred and fifty-six entries. The inverse shapes are stored//! inverted, so the XOR gate is gone and a byte indexes the table directly —//! but only with the alternate character set switched in. That is the table//! these chips hold. With the primary set, which is what a machine comes up//! in, the video logic strips bit 6 from `$40-$7f` before it reaches the ROM,//! so those codes land on the inverse shapes at `$00-$3f`, and *then* XORs the//! result with the flash clock. So flashing exists in the primary set and not//! in the alternate one, which is exactly what the manual says and looks//! arbitrary until you see where the bit went. The blank scan line moved to//! the bottom so that lowercase could have descenders.//!//! The two IIe chips differ at `$40-$5f` and nowhere else. The original holds//! a second copy of the inverse uppercase shapes there — the copy the primary//! set reaches by clearing bit 6, and so a copy nothing ever displays from//! directly. The enhanced part spends those thirty-two entries on MouseText//! instead, which is why MouseText cost Apple nothing but a chip swap.//!//! ## The flash clock//!//! Flashing needs one more thing than a bitmap: a phase, and `flashing` below//! works it out from the machine's own cycle count rather than from the wall.//! That is what the hardware does — the flash comes out of the video counter,//! which is the same clock everything else in the machine runs on — and it//! means a machine run at ten times its speed flashes ten times as fast and a//! halted one stops flashing, which is what makes stepping through code//! bearable.//!//! How fast it goes is one of the few numbers here that no manual gives.//! Nothing depends on it: it is a cursor blinking, and the only requirement is//! that it is somewhere near twice a second, which is what this is.
const std = @import("std");
pub const apple2 = @import("fonts/apple2.zig");pub const apple2e = @import("fonts/apple2e.zig");
/// Dots across a character cell, and scan lines down it. Both chips agree.pub const width = apple2e.width;pub const height = apple2e.height;
/// One cell: eight scan lines, topmost first, with **bit 6 the leftmost dot**/// and a set bit meaning lit.////// That is a normalised form and not what either chip holds. A IIe's shift/// register clocks out least significant bit first through an inverting/// driver, so the ROM stores each row backwards and upside down; the/// transcription in `fonts/` turns them all the right way up so that one/// `pixel` serves every machine.pub const Glyph = apple2e.Glyph;
/// How many times a second a flashing character turns over and back.pub const flashes_per_second = 2;
/// The phase of the flash clock, from a machine's cycle count.pub fn flashing(cycles: u64, cycles_per_second: u64) bool { const half = cycles_per_second / (2 * flashes_per_second); return (cycles / @max(1, half)) & 1 == 1;}
/// Which chip is in the machine.pub const Set = enum { /// `341-0036`, in an Apple II and a II Plus. Uppercase only. apple2, /// `342-0133-A`, in an Apple IIe as it was first sold. apple2e, /// `342-0265-A`, in an enhanced IIe and in a IIc. The same, with MouseText /// where the original keeps a copy of the inverse uppercase. apple2e_enhanced,};
/// The dots a byte in the text page lights up on one of its eight scan lines.////// `alternate` is ALTCHRSET, which a machine older than the IIe has not got/// and should pass as false. `flash` is the phase of the flash clock: pass it/// true for half of each cycle and a flashing character will flash.pub fn glyph(set: Set, byte: u8, alternate: bool, flash: bool) Glyph { switch (set) { .apple2 => { const shape = apple2.glyph(byte); const dark = apple2.isInverse(byte) or (apple2.isFlashing(byte) and flash); return if (dark) apple2.invert(shape) else shape; }, .apple2e, .apple2e_enhanced => { const table = if (set == .apple2e_enhanced) &apple2e.enhanced else &apple2e.original; if (alternate) return table[byte]; const shape = table[apple2e.primaryRomIndex(byte)]; return if (apple2e.isFlashing(byte) and flash) apple2e.invert(shape) else shape; }, }}
/// One scan line of that, as seven dots with the leftmost in bit 0 — which is/// the order the video puts them out and the order `video.zig` wants them in,/// and the other way round from how `Glyph` holds them.pub fn row(set: Set, byte: u8, line: u3, alternate: bool, flash: bool) u7 { const shape = glyph(set, byte, alternate, flash); return @bitReverse(@as(u7, @truncate(shape[line])));}
const testing = std.testing;
test "the flash clock turns over twice a second" { const hz = 1_020_500; try testing.expect(!flashing(0, hz)); // A quarter of a second in it has turned over, and half a second in it is // back where it started. try testing.expect(flashing(hz / 4, hz)); try testing.expect(!flashing(hz / 2, hz)); try testing.expect(!flashing(hz, hz));}
test "a byte reaches the shape the manual says it does" { // $c1 is a normal 'A' on every machine here, and the shape is the same // five-by-seven cell in all three chips. const a_normal = glyph(.apple2e_enhanced, 0xc1, false, false); try testing.expectEqualSlices(u8, &.{ 0x08, 0x14, 0x22, 0x22, 0x3e, 0x22, 0x22, 0x00 }, &a_normal); // The II's blank line is at the top rather than the bottom, and it has no // room for descenders because of it. const a_older = glyph(.apple2, 0xc1, false, false); try testing.expectEqualSlices(u8, &.{ 0x00, 0x08, 0x14, 0x22, 0x22, 0x3e, 0x22, 0x22 }, &a_older);}
test "inverse is the same shape with the dots and the gaps swapped" { const normal = glyph(.apple2e_enhanced, 0xc1, true, false); const inverse = glyph(.apple2e_enhanced, 0x01, true, false); for (normal, inverse) |lit, dark| try testing.expectEqual(lit, ~dark & 0x7f);}
test "flashing exists in the primary set and not in the alternate one" { // $41 is a flashing 'A' in the primary set: the video strips bit 6, so it // lands on the inverse shape at $01, and then the flash clock decides // whether to turn it back over. const dim = glyph(.apple2e_enhanced, 0x41, false, false); const bright = glyph(.apple2e_enhanced, 0x41, false, true); for (dim, bright) |a, b| try testing.expectEqual(a, ~b & 0x7f); try testing.expectEqualSlices(u8, &glyph(.apple2e_enhanced, 0x01, true, false), &dim);
// With the alternate set the same byte is MouseText on an enhanced chip // and does not flash at all. const still = glyph(.apple2e_enhanced, 0x41, true, false); try testing.expectEqualSlices(u8, &still, &glyph(.apple2e_enhanced, 0x41, true, true));
// And on the original chip those thirty-two entries are the copy of the // inverse uppercase that the primary set was reaching all along. try testing.expectEqualSlices( u8, &glyph(.apple2e, 0x01, true, false), &glyph(.apple2e, 0x41, true, false), );}
test "the two IIe chips differ only where MouseText went" { for (apple2e.original, apple2e.enhanced, 0..) |old, new, code| { if (code >= 0x40 and code < 0x60) continue; try testing.expectEqualSlices(u8, &old, &new); } // And there they do differ, or the swap would have bought nothing. var same: usize = 0; for (0x40..0x60) |code| { if (std.mem.eql(u8, &apple2e.original[code], &apple2e.enhanced[code])) same += 1; } try testing.expect(same < 0x20);}
test "a row comes out with the leftmost dot in bit zero" { // Which is the order the shift register puts them out, and the order the // rest of the video works in — the opposite of how the tables hold them. // 'A' has its apex in the middle of the top line either way. try testing.expectEqual(@as(u7, 0b0001000), row(.apple2e_enhanced, 0xc1, 0, false, false)); // The second line is two dots either side of it, which is not symmetric // about the seven, so a reversal that did nothing would show up here. try testing.expectEqual(@as(u7, 0b0010100), row(.apple2e_enhanced, 0xc1, 1, false, false)); // 'L' is the plainest asymmetric shape there is: a stem on the left and a // foot along the bottom. try testing.expectEqual(@as(u7, 0b0000010), row(.apple2e_enhanced, 0xcc, 0, false, false)); try testing.expectEqual(@as(u7, 0b0111110), row(.apple2e_enhanced, 0xcc, 6, false, false));}