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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! Reading the text screen the way somebody sitting in front of it would.//!//! The machine's screen is not stored in the order it is shown and its bytes//! are not ASCII, so anything outside the emulator that wants to know what is//! on screen has the same two problems to solve. They are solved here once://! the models hand this their memory and their switches, and it hands back//! characters in reading order.//!//! See the Apple IIe Technical Reference Manual for the character set and the//! display switches, and its figure 2-5 for the table this decoding comes from//! (Apple Computer, Inc., 1985b, Chapter 2).
const std = @import("std");
/// Twenty-four rows of forty or eighty, whichever the machine is showing.pub const rows = 24;
/// What the display is doing.////// Only the text modes can be read back as characters; the rest are here so/// that something embedding this can tell when there is no text to read and/// draw the screen itself instead. `video.zig` draws the graphics ones.pub const Mode = enum { /// Forty columns of text. text, /// Eighty columns of text, the auxiliary bank supplying the even ones. text_80, /// Forty by forty-eight blocks of colour. low_resolution, /// The same, with the bottom four rows of text left showing. low_resolution_mixed, /// Eighty by forty-eight, out of both halves of memory. double_low_resolution, /// The same, mixed. The text at the bottom of a doubled screen can only /// be eighty columns, since it is the same circuit fetching both. double_low_resolution_mixed, /// Two hundred and eighty by a hundred and ninety-two. high_resolution, /// The same, with the bottom four rows of text left showing. high_resolution_mixed, /// Five hundred and sixty by a hundred and ninety-two, out of both halves. double_high_resolution, /// The same, mixed. double_high_resolution_mixed,
/// Whether this mode is one whose screen can be read as characters. pub fn isText(self: Mode) bool { return switch (self) { .text, .text_80 => true, else => false, }; }
/// Whether the bottom four rows of the screen are text over graphics. pub fn isMixed(self: Mode) bool { return switch (self) { .low_resolution_mixed, .double_low_resolution_mixed, .high_resolution_mixed, .double_high_resolution_mixed, => true, else => false, }; }
/// Whether the graphics are dots rather than blocks. pub fn isHighResolution(self: Mode) bool { return switch (self) { .high_resolution, .high_resolution_mixed, .double_high_resolution, .double_high_resolution_mixed, => true, else => false, }; }
/// Whether the auxiliary half of memory is on the screen too, which is /// what doubles the horizontal resolution. pub fn isDoubled(self: Mode) bool { return switch (self) { .text_80, .double_low_resolution, .double_low_resolution_mixed, .double_high_resolution, .double_high_resolution_mixed, => true, else => false, }; }
/// How many columns of text the mode shows, or none if it shows none. /// /// A mixed mode shows four rows of it, and this does not say so: the /// question here is how wide a row is, and a caller drawing a mixed screen /// already knows which rows are text from `video.scanLine`. pub fn columns(self: Mode) ?u16 { return switch (self) { .text => 40, .text_80 => 80, else => null, }; }};
/// How a character is drawn. The machine has no colour: it draws inverse and/// flashing by swapping the dots for the gaps, so these are the only three/// things a character can be.pub const Video = enum { normal, inverse, flashing };
pub const Glyph = struct { character: u8, video: Video,};
/// Decodes a text page byte.////// The eight runs of thirty-two each carry a slice of ASCII and a way of/// drawing it: uppercase and punctuation twice over in inverse, again in/// flashing, and twice more in normal, with lowercase only in the last run./// The IIe's alternate character set trades the flashing runs for inverse/// lowercase, which is why it has to be known.////// One character is not ASCII: where ASCII has delete, the machine's character/// generator has a solid block, and the firmware uses it as the cursor. It/// comes back as $7f, the code the machine itself holds, leaving whoever is/// reading to decide how to show a block.////// (Apple Computer, Inc., 1985b, Chapter 2, Figure 2-5)pub fn decode(byte: u8, alternate: bool) Glyph { const bits: u8 = byte & 0x1f; const uppercase = 0x40 + bits; // @ through _ const symbols = 0x20 + bits; // space through ? const lowercase = 0x60 + bits; // ` through DEL return switch (byte) { 0x00...0x1f => .{ .character = uppercase, .video = .inverse }, 0x20...0x3f => .{ .character = symbols, .video = .inverse }, 0x40...0x5f => .{ .character = uppercase, .video = if (alternate) .inverse else .flashing, }, 0x60...0x7f => if (alternate) .{ .character = lowercase, .video = .inverse } else .{ .character = symbols, .video = .flashing }, 0x80...0x9f => .{ .character = uppercase, .video = .normal }, 0xa0...0xbf => .{ .character = symbols, .video = .normal }, 0xc0...0xdf => .{ .character = uppercase, .video = .normal }, 0xe0...0xff => .{ .character = lowercase, .video = .normal }, };}
/// Where a row of the display starts in memory.////// The text page is not stored in the order it is shown. Three rows eight/// apart share each 128 byte block, which is how 960 characters fit into a/// kilobyte and why reading the screen means working the address out rather/// than walking through memory.pub fn rowBase(page: u16, row: u16) u16 { return page + 0x80 * (row % 8) + 0x28 * (row / 8);}
/// The byte behind one character position.////// In eighty columns the two halves of memory are read alternately, the/// auxiliary one supplying the even columns, so each pair of columns comes/// from the same address in the two halves.pub fn byteAt(machine: anytype, columns: u16, line: u16, column: u16) u8 { if (columns == 80) return machine.videoByte(column % 2 == 0, line + column / 2); return machine.videoByte(false, line + column);}
/// Writes what is on the text screen, row by row in reading order, with/// nothing at all between the rows: twenty-four lots of forty characters, or/// of eighty, and the caller knows which from the mode.////// Nothing is written for a screen showing graphics, since there is no text on/// it to write. Ask the mode first if the difference matters.pub fn write(machine: anytype, writer: *std.Io.Writer) std.Io.Writer.Error!void { const columns = machine.mode().columns() orelse return; const page = machine.displayPage(); const alternate = machine.alternateCharacters(); for (0..rows) |row| { const line = rowBase(page, @intCast(row)); for (0..columns) |column| { const byte = byteAt(machine, columns, line, @intCast(column)); try writer.writeByte(decode(byte, alternate).character); } }}
const testing = std.testing;
test "the character set is decoded as the manual sets it out" { // Normal video, the top two runs, which are where ordinary printing goes. try testing.expectEqual(Glyph{ .character = 'A', .video = .normal }, decode(0xc1, false)); try testing.expectEqual(Glyph{ .character = 'a', .video = .normal }, decode(0xe1, false)); try testing.expectEqual(Glyph{ .character = ' ', .video = .normal }, decode(0xa0, false));
// Inverse, and flashing, which is what the older firmware's cursor is. try testing.expectEqual(Glyph{ .character = 'A', .video = .inverse }, decode(0x01, false)); try testing.expectEqual(Glyph{ .character = 'A', .video = .flashing }, decode(0x41, false));
// The alternate set trades the flashing runs for inverse lowercase. try testing.expectEqual(Glyph{ .character = 'A', .video = .inverse }, decode(0x41, true)); try testing.expectEqual(Glyph{ .character = 'a', .video = .inverse }, decode(0x61, true));
// And the one that is not ASCII: the block the firmware leaves where the // cursor is, which comes back as the code the machine holds. try testing.expectEqual(Glyph{ .character = 0x7f, .video = .normal }, decode(0xff, false));}
test "three rows eight apart share a block" { // (Apple Computer, Inc., 1985b, Table 2-4) try testing.expectEqual(@as(u16, 0x0400), rowBase(0x0400, 0)); try testing.expectEqual(@as(u16, 0x0480), rowBase(0x0400, 1)); try testing.expectEqual(@as(u16, 0x0780), rowBase(0x0400, 7)); try testing.expectEqual(@as(u16, 0x0428), rowBase(0x0400, 8)); try testing.expectEqual(@as(u16, 0x0450), rowBase(0x0400, 16)); try testing.expectEqual(@as(u16, 0x07d0), rowBase(0x0400, 23)); // The second page is the same shape a kilobyte further on. try testing.expectEqual(@as(u16, 0x0800), rowBase(0x0800, 0)); try testing.expectEqual(@as(u16, 0x0bd0), rowBase(0x0800, 23));}
test "a mode says whether there is text to read and how much" { try testing.expect(Mode.text.isText()); try testing.expect(Mode.text_80.isText()); try testing.expect(!Mode.high_resolution.isText()); try testing.expectEqual(@as(?u16, 40), Mode.text.columns()); try testing.expectEqual(@as(?u16, 80), Mode.text_80.columns()); try testing.expectEqual(@as(?u16, null), Mode.low_resolution_mixed.columns());}
test "a mode says what kind of graphics it is" { try testing.expect(!Mode.text.isMixed()); try testing.expect(Mode.low_resolution_mixed.isMixed()); try testing.expect(Mode.double_high_resolution_mixed.isMixed());
try testing.expect(Mode.high_resolution.isHighResolution()); try testing.expect(Mode.double_high_resolution_mixed.isHighResolution()); try testing.expect(!Mode.low_resolution.isHighResolution());
// Eighty-column text is doubled too, and for the same reason: it is the // auxiliary half of memory on the screen beside the main one. try testing.expect(Mode.text_80.isDoubled()); try testing.expect(Mode.double_low_resolution.isDoubled()); try testing.expect(!Mode.high_resolution.isDoubled());}