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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! Turning a machine's screen into pixels, and pixels into a file.//!//! This is the only file here that knows what a picture is. Everything else//! deals in what the machines deal in: `video.zig` hands back dots and palette//! numbers, `superhires.zig` hands back palette numbers again, and neither has//! any idea what colour is on a screen or what a file looks like. That is//! deliberate — a program embedding this to *run* a machine should not have to//! drag a graphics library along — so the dependency on z2d stops here.//!//! ## Why a library rather than the twenty lines it replaced//!//! There was a PNG writer here before, hand-rolled, and it was a nice piece of//! work: indexed output matching the hardware's own palette, stored deflate//! blocks so the length was arithmetic rather than a discovery, and therefore//! no allocator and nothing to fail but the writer. What it was not was a//! graphics library. A picture is a thing you want to *do* things to — scale//! it, put two of them side by side, draw on it, write it out compressed — and//! every one of those is somebody else's solved problem. z2d is that somebody.//!//! What is given up is the indexed file. z2d's surfaces are RGB, so what comes//! out is truecolour rather than an index and a palette chunk, which is one//! step further from what the hardware holds than the old writer was. In//! exchange the files are a tenth the size, because they are actually//! compressed.//!//! ## Scaling//!//! Neither machine has square pixels, so the default repeats every line. It//! *repeats* rather than interpolating, and that is a decision rather than//! laziness: the dots are what the machine made, and smoothing them is a//! judgement about how the picture should look that belongs to whoever is//! looking at it.
const std = @import("std");
const z2d = @import("z2d");
const superhires = @import("superhires.zig");const ntsc = @import("ntsc.zig");const video = @import("video.zig");const videx = @import("videx.zig");const ultraterm = @import("ultraterm.zig");
/// Which of a machine's displays to draw.////// An Apple IIgs has both and only one of them is on the screen at a time; ask/// the machine which with `superHiRes`. Everything older has only the first.pub const Display = enum { /// What the Mega II draws, and what every Apple II before the IIgs drew: /// 560 dots by 192, coloured by what four of them at a time do to a /// television. See `video.zig`. apple2, /// What the Video Graphics Controller draws: 640 by 200, with a palette /// per scan line. See `superhires.zig`. super_hires, /// What a Videx Videoterm draws, which is not the machine's video at all: /// 720 by 216 out of the card's own memory and its own character /// generator, and monochrome because the card has no colour in it. See /// `videx.zig`. videoterm, /// What a Videx UltraTerm draws: whatever its 6845 has been told to, up to /// a hundred and sixty columns, at two brightnesses. See `ultraterm.zig`. ultraterm,
pub fn width(self: Display) u16 { return switch (self) { .apple2 => video.width, .super_hires => superhires.width, .videoterm => videx.width, .ultraterm => 80 * ultraterm.glyph_dots, }; }
pub fn height(self: Display) u16 { return switch (self) { .apple2 => video.height, .super_hires => superhires.height, .videoterm => videx.height, .ultraterm => 24 * 9, }; }};
pub const Options = struct { /// How many times each dot is repeated across and down. See the header for /// why it repeats. across: u3 = 1, down: u3 = 2, /// Which display the dot stream is being looked at on. monitor: Monitor = .colour,};
/// The three displays an Apple II's dot stream could be looked at on.////// This is a property of the *screen*, not of the machine: what comes out of/// an Apple II is a stream of dots and a set of timing signals, and what those/// become depends entirely on what is plugged in. All three see the same/// stream.pub const Monitor = enum { /// A composite colour monitor, which turns the dot stream into colour by /// the rule in `video.zig`: four dots make a colour, drawn as a flat block /// with hard edges. colour, /// The same monitor, but demodulated rather than looked up -- which is /// what one actually did. Colours bleed into their neighbours and a lone /// dot is a smudge rather than a block, because chrominance is allowed a /// third of the bandwidth luminance is. See `ntsc.zig`. ntsc, /// A monochrome monitor, which shows the dots as dots. Text is much easier /// to read this way and it is how text was usually looked at. monochrome, /// The Apple IIc's Flat Panel Display, 1984: a seven-inch liquid crystal /// panel of 560 by 192 pixels, one bit deep, that clipped onto the machine /// and gave it a screen without a monitor. /// /// It is here as a *monitor* and not as hardware because that is all it /// is. It hung off the IIc's DB-15 video expansion connector, which /// carries timing and video signals and no registers at all -- `TEXT`, /// `14M`, `SYNC*`, `LDPS*`, `CREF`, `PRAS`, `SEROUT*` and `VIDD7` (Apple /// Computer, Inc., 1989b, Table 11-32, p. 376). There is no soft switch /// for it, no firmware, and nothing a program can ask about it: software /// on a IIc with a flat panel and software on a IIc with a television are /// the same software doing the same thing. /// /// Five hundred and sixty by a hundred and ninety-two is exactly the dot /// stream: eighty columns of seven dots, and the same hundred and /// ninety-two lines. So this is the most literal of the three -- one panel /// pixel per dot, no composite rule and no artefact colour, because there /// is no composite signal for artefacts to happen in. flat_panel,
pub fn other(self: Monitor) Monitor { return switch (self) { .monochrome => .colour, .colour => .ntsc, .ntsc => .flat_panel, .flat_panel => .monochrome, }; }
/// Whether the dots are drawn as dots rather than run through the colour /// rule, which both of the non-composite displays do. pub fn isMonochrome(self: Monitor) bool { return self == .monochrome or self == .flat_panel; }
pub fn describe(self: Monitor) []const u8 { return switch (self) { .monochrome => "a monochrome monitor", .colour => "a colour monitor", .ntsc => "a colour monitor, demodulated", .flat_panel => "the flat panel", }; }
pub fn parse(name: []const u8) ?Monitor { if (std.mem.eql(u8, name, "monochrome") or std.mem.eql(u8, name, "mono")) return .monochrome; if (std.mem.eql(u8, name, "colour") or std.mem.eql(u8, name, "color")) return .colour; if (std.mem.eql(u8, name, "flat-panel") or std.mem.eql(u8, name, "flat") or std.mem.eql(u8, name, "lcd")) return .flat_panel; if (std.mem.eql(u8, name, "ntsc") or std.mem.eql(u8, name, "composite")) return .ntsc; return null; }
/// What a dot looks like on it: the ink first, then the ground. /// /// **The flat panel's two colours are a choice and not a fact**, in /// exactly the way `video.palette` is a choice: Apple's service manual for /// the panel describes it as modules, connectors and screws and never says /// what it looked like, and the Technical Reference describes the /// connector rather than the display on the end of it. So there is no /// document to be faithful to here. What is here is a passive liquid /// crystal panel of 1984 as such panels were -- dark segments on a pale /// ground, which is the one thing that makes it look unlike a monitor /// rather than merely dimmer than one. Replace it if you have a panel in /// mind. pub fn ink(self: Monitor) [2]video.Rgb { return switch (self) { .flat_panel => .{ .{ .r = 0x1a, .g = 0x22, .b = 0x26 }, .{ .r = 0xa8, .g = 0xb8, .b = 0xa4 }, }, // A monitor is a dark tube with a lit phosphor on it. else => .{ video.palette[@intFromEnum(video.Colour.white)], video.palette[@intFromEnum(video.Colour.black)], }, }; }};
/// How big a surface is, in pixels.pub const Size = struct { width: i32, height: i32 };
/// How big a surface has to be for one of these.////// The two Apple displays are a fixed size because the hardware that draws/// them is: 560 by 192 and 640 by 200 are wired in. A Videoterm's is not — the/// 6845 decides it, and a program may decide otherwise — so ask `sizeOf`,/// which can see the machine. This answers with the standard setup, which is/// what the Videoterm's own firmware asks for.pub fn size(display: Display, options: Options) Size { return .{ .width = @as(i32, display.width()) * @max(1, options.across), .height = @as(i32, display.height()) * @max(1, options.down), };}
/// How big a surface has to be for what a particular machine is showing.////// The same as `size` for everything the machine draws itself, and the 6845's/// business for a card that draws its own.pub fn sizeOf(display: Display, machine: anytype, options: Options) Size { if (display == .ultraterm) { if (comptime @hasField(@TypeOf(machine.*), "ultraterm_card")) { const card = &machine.ultraterm_card; return .{ .width = @as(i32, ultraterm.pictureWidth(card)) * @max(1, options.across), .height = @as(i32, ultraterm.pictureHeight(card)) * @max(1, options.across), }; } } if (display == .videoterm) { if (comptime @hasField(@TypeOf(machine.*), "videx_card")) { const card = &machine.videx_card; return .{ .width = @as(i32, videx.pictureWidth(card)) * @max(1, options.across), .height = @as(i32, videx.pictureHeight(card)) * @max(1, options.across), }; } } return size(display, options);}
/// Draws a machine's screen into a surface, which must be at least the size/// `size` gives.pub fn paint(display: Display, machine: anytype, surface: *z2d.Surface, options: Options) void { switch (display) { .apple2 => paintVideo(machine, surface, options), .super_hires => paintSuperHiRes(machine, surface, options), .videoterm => paintVideoterm(machine, surface, options), .ultraterm => paintUltraTerm(machine, surface, options), }}
fn paintVideo(machine: anytype, surface: *z2d.Surface, options: Options) void { var line: video.Line = .{}; var decoded: [video.width]video.Rgb = undefined; const shown = options.monitor.ink(); for (0..video.height) |y| { _ = video.scanLine(machine, @intCast(y), &line); // Demodulating is a whole line's work rather than a dot's, because // every dot's colour depends on the seventeen around it. if (options.monitor == .ntsc) ntsc.decodeLine(&line.dots, &decoded); for (0..video.width) |x| { const colour = switch (options.monitor) { .monochrome, .flat_panel => shown[if (line.dots[x]) 0 else 1], .colour => video.palette[line.colours[x]], .ntsc => decoded[x], }; put(surface, x, y, colour, options); } }}
fn paintSuperHiRes(machine: anytype, surface: *z2d.Surface, options: Options) void { var table: [superhires.palettes * superhires.colours]video.Rgb = undefined; superhires.palette(machine, &table); // The monitor is one choice about one screen, so it reaches Super Hi-Res // the way it reaches everything else. A monochrome or flat-panel display // has no colour to give: each pixel arrives as its luminance, placed on // the line between the tube's dark and its lit phosphor, the same as the // legacy modes draw a dot. A colour or composite monitor takes the palette // as it is -- Super Hi-Res is already RGB, so there is nothing for the // NTSC path to demodulate. const monochrome = options.monitor.isMonochrome(); const ink = options.monitor.ink(); var line: superhires.Line = .{}; for (0..superhires.height) |y| { superhires.scanLine(machine, @intCast(y), &line); for (0..superhires.width) |x| { const colour = table[line.entries[x]]; put(surface, x, y, if (monochrome) monochromeTint(colour, ink) else colour, options); } }}
/// One colour as a monochrome monitor shows it: its luminance placed between/// the tube's dark end and its lit phosphor. The weights are Rec. 601's, which/// is what a black-and-white set made of a colour signal.fn monochromeTint(colour: video.Rgb, ink: [2]video.Rgb) video.Rgb { const luma = (@as(u32, colour.r) * 299 + @as(u32, colour.g) * 587 + @as(u32, colour.b) * 114) / 1000; return .{ .r = interpolate(ink[1].r, ink[0].r, luma), .g = interpolate(ink[1].g, ink[0].g, luma), .b = interpolate(ink[1].b, ink[0].b, luma), };}
/// `dark` at luminance zero, `lit` at luminance 255, straight between. `ink`/// hands back the lit phosphor first and the dark tube second, which is why/// the caller passes them the other way up.fn interpolate(dark: u8, lit: u8, luma: u32) u8 { return @intCast((@as(u32, dark) * (255 - luma) + @as(u32, lit) * luma) / 255);}
/// The Videoterm's own picture, which owes nothing to the machine's video.////// Monochrome whatever the options say: the card puts out one signal with no/// colour burst, and what colour the letters came out was a property of the/// tube. The cursor is drawn as showing, because a still picture of a blinking/// cursor has to pick a half of the blink and the half with a cursor in it is/// the one that says where the machine is.////// The card's dots are square where the Apple's are not, so this ignores the/// doubling that exists to undo the Apple's stretch, and draws one pixel per/// dot unless asked for more.fn paintVideoterm(machine: anytype, surface: *z2d.Surface, options: Options) void { // A machine with no slot to put the card in cannot be asked for it, and // asking is a compile-time question rather than a run-time one. if (comptime !@hasField(@TypeOf(machine.*), "videx_card")) return; const card = &machine.videx_card; const parts = machine.videxCharsets(); const square: Options = .{ .across = options.across, .down = @max(1, options.across), .monitor = .monochrome, }; const tall = videx.pictureHeight(card); const wide = videx.pictureWidth(card); var line: videx.Line = .{}; for (0..tall) |y| { videx.scanLine(card, parts.set, parts.alternate, @intCast(y), true, &line); for (0..wide) |x| { const colour = video.palette[if (line.dots[x]) @intFromEnum(video.Colour.white) else 0]; put(surface, x, y, colour, square); } }}
/// The UltraTerm's picture, which has three levels rather than two.////// Lowlight is drawn at half the white a highlight character gets. That is a/// choice about how to show it and not a measurement — the card drove the/// video less hard and what came out depended on the monitor — so it is put/// here, where the picture is made, rather than in `ultraterm.zig`, which says/// only which of the two a character asked for.fn paintUltraTerm(machine: anytype, surface: *z2d.Surface, options: Options) void { if (comptime !@hasField(@TypeOf(machine.*), "ultraterm_card")) return; const card = &machine.ultraterm_card; const white = video.palette[@intFromEnum(video.Colour.white)]; const dim: video.Rgb = .{ .r = white.r / 2, .g = white.g / 2, .b = white.b / 2 }; const black = video.palette[0]; const square: Options = .{ .across = options.across, .down = @max(1, options.across) };
const tall = ultraterm.pictureHeight(card); const wide = ultraterm.pictureWidth(card); var line: ultraterm.Line = .{}; for (0..tall) |y| { ultraterm.scanLine(card, &machine.ultraterm_charset, @intCast(y), true, &line); for (0..wide) |x| put(surface, x, y, switch (line.levels[x]) { .off => black, .low => dim, .high => white, }, square); }}
/// One dot of the machine, which is however many pixels of the picture the/// scale says.fn put(surface: *z2d.Surface, x: usize, y: usize, colour: video.Rgb, options: Options) void { const pixel: z2d.Pixel = .{ .rgb = .{ .r = colour.r, .g = colour.g, .b = colour.b } }; const across: usize = @max(1, options.across); const down: usize = @max(1, options.down); for (0..down) |again_down| { for (0..across) |again_across| { surface.putPixel( @intCast(x * across + again_across), @intCast(y * down + again_down), pixel, ); } }}
/// A picture kept between frames: the surface z2d draws into, and the same/// pixels as three bytes each.////// Three bytes rather than z2d's own four — its RGB pixel is a `u32` with a/// byte of padding — because that is what a terminal's graphics protocol/// takes, and because handing over the surface's buffer directly would send/// the padding down the wire as well. Something drawing a screen over and over/// wants both of those allocated once, which is what this is for; something/// writing a file once does not and should use `write`.pub const Buffer = struct { surface: z2d.Surface, /// The pixels, left to right and top to bottom, three bytes each. rgb: []u8, display: Display, options: Options,
pub fn init( gpa: std.mem.Allocator, display: Display, machine: anytype, options: Options, ) (z2d.Surface.Error || std.mem.Allocator.Error)!Buffer { const wanted = sizeOf(display, machine, options); var surface: z2d.Surface = try .init(.image_surface_rgb, gpa, wanted.width, wanted.height); errdefer surface.deinit(gpa); const count: usize = @intCast(wanted.width * wanted.height); return .{ .surface = surface, .rgb = try gpa.alloc(u8, count * 3), .display = display, .options = options, }; }
pub fn deinit(self: *Buffer, gpa: std.mem.Allocator) void { self.surface.deinit(gpa); gpa.free(self.rgb); self.* = undefined; }
pub fn width(self: *const Buffer) u16 { return @intCast(self.surface.getWidth()); }
pub fn height(self: *const Buffer) u16 { return @intCast(self.surface.getHeight()); }
/// Whether what this was allocated for is still what the machine is /// showing. /// /// It always is for the two Apple displays. A Videoterm's picture is the /// 6845's to decide and a program may decide otherwise while it runs, and /// then a buffer allocated for the old shape is the wrong one — the same /// situation as a monitor losing sync, and dealt with the same way, by /// starting again. pub fn fits(self: *const Buffer, machine: anytype) bool { const wanted = sizeOf(self.display, machine, self.options); return wanted.width == self.surface.getWidth() and wanted.height == self.surface.getHeight(); }
/// Draws the machine's screen, and flattens it into `rgb` on the way out. pub fn draw(self: *Buffer, machine: anytype) void { paint(self.display, machine, &self.surface, self.options); const wide: usize = self.width(); var at: usize = 0; for (0..self.height()) |y| { const row = self.surface.getStride(0, @intCast(y), wide); switch (row) { .rgb => |dots| for (dots) |dot| { self.rgb[at + 0] = dot.r; self.rgb[at + 1] = dot.g; self.rgb[at + 2] = dot.b; at += 3; }, // The surface is made here and is always an RGB one. else => unreachable, } } }};
/// Draws a machine's screen and writes it out as a PNG.pub fn write( io: std.Io, gpa: std.mem.Allocator, display: Display, machine: anytype, path: []const u8, options: Options,) !void { const wanted = sizeOf(display, machine, options); var surface: z2d.Surface = try .init(.image_surface_rgb, gpa, wanted.width, wanted.height); defer surface.deinit(gpa); paint(display, machine, &surface, options); try z2d.png_exporter.writeToPNGFile(io, surface, path, .{});}
const testing = std.testing;
test "a Videoterm's picture is the size its 6845 was told to make it" { // Not a machine, just enough of one: the size question is answered from // the card's registers and nothing else is consulted. var stub: struct { videx_card: videx.Card = .{ .fitted = true }, pub fn videxCharsets(_: *const @This()) struct { set: *const [videx.charset_size]u8, alternate: ?*const [videx.charset_size]u8, } { return .{ .set = &blank, .alternate = null }; } const blank: [videx.charset_size]u8 = @splat(0); } = .{};
const one: Options = .{ .across = 1, .down = 1 };
// A 6845 nobody has programmed puts out nothing, and a surface has to be // some size, so it is told the standard one. try testing.expectEqual(@as(i32, videx.width), sizeOf(.videoterm, &stub, one).width); try testing.expectEqual(@as(i32, videx.height), sizeOf(.videoterm, &stub, one).height);
// What the Videoterm's own firmware asks for: eighty by twenty-four, nine // lines to a cell. stub.videx_card.crtc.register[1] = 80; stub.videx_card.crtc.register[6] = 24; stub.videx_card.crtc.register[9] = 8; try testing.expectEqual(@as(i32, 720), sizeOf(.videoterm, &stub, one).width); try testing.expectEqual(@as(i32, 216), sizeOf(.videoterm, &stub, one).height);
// And what a Videoterm compatible board asks for: the same rows in a cell // a line taller, which is a picture two hundred and forty lines deep. This // is the case that used to be drawn into a surface too short for it. stub.videx_card.crtc.register[9] = 9; try testing.expectEqual(@as(i32, 240), sizeOf(.videoterm, &stub, one).height);
// The Apple's own displays are the hardware's size whatever a machine says. try testing.expectEqual(size(.apple2, one), sizeOf(.apple2, &stub, one));}
/// A machine with nothing in it but a screen, for the tests below: enough of/// one to answer both of the renderers, since both read out of the same two/// halves of memory.const Screen = struct { main: [0x10000]u8 = @splat(0), aux: [0x10000]u8 = @splat(0), showing: @import("screen.zig").Mode = .text,
pub fn colourBurst(self: *const Screen) bool { // A test screen is a later board: white text, coloured graphics. return !self.showing.isText(); } pub fn delaysHires(_: *const Screen) bool { return true; } pub fn mode(self: *const Screen) @import("screen.zig").Mode { return self.showing; } pub fn videoByte(self: *const Screen, auxiliary: bool, address: u16) u8 { return if (auxiliary) self.aux[address] else self.main[address]; } pub fn displayPage(_: *const Screen) u16 { return 0x0400; } pub fn hiresPage(_: *const Screen) u16 { return 0x2000; } pub fn columns(self: *const Screen) u16 { return if (self.showing.isDoubled()) 80 else 40; } pub fn characterSet(_: *const Screen) @import("font.zig").Set { return .apple2e_enhanced; } pub fn alternateCharacters(_: *const Screen) bool { return false; } pub fn flashing(_: *const Screen) bool { return false; }
fn text(self: *Screen, column: usize, byte: u8) void { self.main[@import("screen.zig").rowBase(0x0400, 0) + column] = byte; }
fn superColour(self: *Screen, which: u4, colour: u4, rgb: u12) void { const at = superhires.palette_table + @as(usize, which) * 32 + @as(usize, colour) * 2; self.aux[at] = @truncate(rgb); self.aux[at + 1] = @truncate(rgb >> 8); }};
test "a picture is the screen, at the size the scale asks for" { const gpa = testing.allocator; const machine = try gpa.create(Screen); defer gpa.destroy(machine); machine.* = .{ .showing = .low_resolution }; for (0..40) |c| machine.text(c, @intCast(c % 16 | (c % 16) << 4));
const options: Options = .{ .across = 2, .down = 3 }; const wanted = size(.apple2, options); try testing.expectEqual(@as(i32, video.width * 2), wanted.width); try testing.expectEqual(@as(i32, video.height * 3), wanted.height);
var surface: z2d.Surface = try .init(.image_surface_rgb, gpa, wanted.width, wanted.height); defer surface.deinit(gpa); paint(.apple2, machine, &surface, options);
// Every pixel is the colour the renderer said, repeated rather than // blended: the six pixels a dot becomes are all the same one. var line: video.Line = .{}; for (0..video.height) |y| { _ = video.scanLine(machine, @intCast(y), &line); for (0..video.width) |x| { const want = video.palette[line.colours[x]]; for (0..3) |down| { for (0..2) |across| { const got = surface.getPixel( @intCast(x * 2 + across), @intCast(y * 3 + down), ).?.rgb; try testing.expectEqual(want.r, got.r); try testing.expectEqual(want.g, got.g); try testing.expectEqual(want.b, got.b); } } } }}
test "monochrome is the dots rather than what four of them come to" { const gpa = testing.allocator; const machine = try gpa.create(Screen); defer gpa.destroy(machine); machine.* = .{ .showing = .text }; for (0..40) |c| machine.text(c, 0xcc); // a row of Ls
const options: Options = .{ .across = 1, .down = 1, .monitor = .monochrome }; const wanted = size(.apple2, options); var surface: z2d.Surface = try .init(.image_surface_rgb, gpa, wanted.width, wanted.height); defer surface.deinit(gpa); paint(.apple2, machine, &surface, options);
var line: video.Line = .{}; _ = video.scanLine(machine, 0, &line); var lit: usize = 0; for (0..video.width) |x| { const got = surface.getPixel(@intCast(x), 0).?.rgb; try testing.expectEqual(@as(u8, if (line.dots[x]) 0xff else 0), got.r); try testing.expectEqual(got.r, got.g); try testing.expectEqual(got.g, got.b); if (line.dots[x]) lit += 1; } // And there is something on the line, or the test above proves nothing. try testing.expect(lit > 0);}
test "Super Hi-Res comes out of its own palettes" { const gpa = testing.allocator; const machine = try gpa.create(Screen); defer gpa.destroy(machine); machine.* = .{}; machine.superColour(0, 1, 0xf00); machine.superColour(0, 2, 0x0f0); machine.aux[superhires.buffer] = 0x12;
const options: Options = .{ .across = 1, .down = 1 }; const wanted = size(.super_hires, options); try testing.expectEqual(@as(i32, 640), wanted.width); try testing.expectEqual(@as(i32, 200), wanted.height);
var surface: z2d.Surface = try .init(.image_surface_rgb, gpa, wanted.width, wanted.height); defer surface.deinit(gpa); paint(.super_hires, machine, &surface, options);
// The first byte is colours 1 and 2 of palette 0, each two pixels wide // because a 320-pixel line is drawn at half the dot clock. for (0..2) |x| { const got = surface.getPixel(@intCast(x), 0).?.rgb; try testing.expectEqual(@as(u8, 0xff), got.r); try testing.expectEqual(@as(u8, 0), got.g); } for (2..4) |x| { const got = surface.getPixel(@intCast(x), 0).?.rgb; try testing.expectEqual(@as(u8, 0), got.r); try testing.expectEqual(@as(u8, 0xff), got.g); }}
test "Super Hi-Res on a monochrome monitor is its luminance, not its colour" { const gpa = testing.allocator; const machine = try gpa.create(Screen); defer gpa.destroy(machine); machine.* = .{}; machine.superColour(0, 1, 0xf00); // pure red machine.superColour(0, 2, 0x0f0); // pure green machine.aux[superhires.buffer] = 0x12;
const options: Options = .{ .across = 1, .down = 1, .monitor = .monochrome }; const wanted = size(.super_hires, options); var surface: z2d.Surface = try .init(.image_surface_rgb, gpa, wanted.width, wanted.height); defer surface.deinit(gpa); paint(.super_hires, machine, &surface, options);
// Grey, the same in every channel, and green brighter than red because a // monochrome monitor weights it that way (Rec. 601: 299 red, 587 green). const red = surface.getPixel(0, 0).?.rgb; try testing.expectEqual(@as(u8, 76), red.r); // 255 * 299 / 1000 try testing.expectEqual(red.r, red.g); try testing.expectEqual(red.g, red.b); const green = surface.getPixel(2, 0).?.rgb; try testing.expectEqual(@as(u8, 149), green.r); // 255 * 587 / 1000 try testing.expectEqual(green.r, green.g); try testing.expectEqual(green.g, green.b); try testing.expect(green.r > red.r);}
test "the four displays paint the same dot stream four ways" { // What comes out of an Apple II is a stream of dots and some timing // signals. All three of these see exactly that stream; the difference is // entirely in what they make of it, which is why this is a property of the // screen and not of the machine. try testing.expect(!Monitor.colour.isMonochrome()); try testing.expect(Monitor.monochrome.isMonochrome()); // The flat panel is not a composite display at all -- it takes the dots // off the video expansion connector -- so there are no artefact colours // for it to show, and it draws the dots as dots like the monitor does. try testing.expect(Monitor.flat_panel.isMonochrome()); // Demodulated composite has colour in it, so it is not one of the two // that draw the dots as dots -- it is the other way of being a colour // monitor, and the more nearly right one. try testing.expect(!Monitor.ntsc.isMonochrome());}
test "a monitor is light on dark and the panel is dark on light" { // The one thing that makes a liquid crystal panel look unlike a monitor // rather than merely dimmer than one. Both colours are a choice -- see // `Monitor.ink` -- but which way round they go is the point of having it. const tube = Monitor.monochrome.ink(); const panel = Monitor.flat_panel.ink(); const brightness = struct { fn of(c: video.Rgb) u32 { return @as(u32, c.r) + c.g + c.b; } }; try testing.expect(brightness.of(tube[0]) > brightness.of(tube[1])); try testing.expect(brightness.of(panel[0]) < brightness.of(panel[1]));}
test "alt+m goes round all four rather than between two" { var at: Monitor = .monochrome; var seen: [4]bool = @splat(false); for (0..4) |_| { seen[@intFromEnum(at)] = true; at = at.other(); } for (seen) |was| try testing.expect(was); // And it comes back to where it started, so the cycle is a cycle. try testing.expectEqual(Monitor.monochrome, at);}
test "the flat panel is named by more than one thing somebody might type" { try testing.expectEqual(Monitor.flat_panel, Monitor.parse("flat-panel").?); try testing.expectEqual(Monitor.flat_panel, Monitor.parse("flat").?); try testing.expectEqual(Monitor.flat_panel, Monitor.parse("lcd").?); try testing.expectEqual(Monitor.monochrome, Monitor.parse("mono").?); try testing.expectEqual(Monitor.colour, Monitor.parse("color").?); try testing.expect(Monitor.parse("plasma") == null);}