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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! Drawing the graphics screen, one scan line at a time.//!//! `screen.zig` reads the text screen as characters, because that is what a//! program wanting to know what the machine *said* is asking for. This is the//! other question: what the machine is *showing*. The two are different enough//! to be separate files — one hands back ASCII and one hands back dots — and//! they ask the machine for the same things: the switches, and bytes out of//! either half of memory.//!//! ## A scan line, not a frame//!//! The unit here is one displayed line, because that is the unit the hardware//! produces and because it is the only division where the awkward cases stop//! being special cases. Mixed mode is four rows of text after a hundred and//! sixty lines of graphics rather than a flag. A program that throws a display//! switch halfway down the screen — which real software does, for split//! screens — comes out right rather than wrong. And it leaves room for the//! Apple IIgs, whose Super Hi-Res picks a palette per line.//!//! ## One stream of dots, coloured afterwards//!//! An Apple II has no colours in it. It has a shift register clocked at//! 14.318 MHz, and what comes out of it is a stream of dots, on or off. Colour//! happens in the monitor, because that dot rate is four times the NTSC colour//! subcarrier, so a pattern that repeats every four dots looks to a television//! like a colour. Which colour depends on *which* four-dot pattern and on//! where in the four the beam is.//!//! So the middle of everything here is a line of 560 dots, and every mode//! writes into it://!//! - Hi-Res is 280 dots, each written twice, and the top bit of a byte//! delays its seven dots by one position — half a dot — which is what//! turns violet into blue and green into orange. Seven is an odd number,//! so the parity of the dots flips at every byte boundary and one colour//! across a line takes *two* byte values alternating: `$55 $2a $55 $2a` is//! a violet line and `$2a $55 $2a $55` a green one. That is why the//! manual's tables of Hi-Res colours have four columns of byte values in//! them rather than one.//! - Double Hi-Res is 560 dots outright, seven to a byte, taken alternately//! from the auxiliary half and the main half.//! - Lo-Res is fourteen dots per block carrying the colour nibble as a//! repeating four-dot pattern, which is what the hardware actually//! serialises. Colouring it then gives the nibble back, and the blocks//! interfere at their edges exactly as they do on a real machine, because//! fourteen is three and a half colour cycles rather than four.//!//! Then one rule colours all of it. The Hardware Reference (Apple Computer,//! Inc., 1989) states it for Double Hi-Res and it is the same rule for//! everything: "Color is determined by any 4 adjacent dots along a line. Think//! of a 4-dot-wide window moving across the screen: at any given time, the//! color displayed will correspond to the 4-bit value ... that corresponds to//! the window's position ... divide the display column number by 4, and use the//! remainder."//!//! That is `colourise` below: the four dots at and after each position, read//! with the leftmost as the low bit, rotated left by the position's remainder.//! The manual's own table is too badly scanned to read the rotation off, so//! the direction was fixed from the things about Hi-Res that everybody knows —//! a `$55 $2a` line is violet and a `$2a $55` line is green, and with the high//! bits set they are blue and orange — and those four are what the tests below//! check. Getting all four right with one rotation is not something a wrong//! direction does.//!//! Text goes through the same line of dots as everything else, out of the//! character generator in `font.zig`. In forty columns a character is seven//! dots drawn twice as wide, which is fourteen positions; in eighty it is seven//! positions and the two halves of memory alternate. Whether the dots then get//! coloured is the machine's to say, through `colourBurst`: a colour monitor//! colours what it is shown only while the colour burst is on the signal, and//! on every Apple II board from Revision 1 on, and on the IIe, the burst is//! killed in TEXT mode so that text is white rather than "distracting green//! and violet" (Sather, 1983, p. 8-5; Sather, 1985, p. 8-4). It is not killed//! under mixed graphics, so the four text rows at the bottom of a graphics//! screen are fringed — and on a Revision 0 board, and on a IIgs with its//! Monochrome/Color register clear, everything is. Where the burst is present//! the dots are coloured by the rule below, and `Line.dots` is what a//! monochrome monitor shows either way.//!//! A text row still says it is one. A caller that can draw characters better//! than a bitmap can — a terminal, for instance — should, and reading the//! screen as characters stays in `screen.zig`: a test asking what is on the//! screen wants letters, not pixels, and always will.//!//! References: the Apple IIe Technical Reference, chapter 2, "The Video Display//! Generator", for the memory maps and the four-dot window (Apple Computer,//! Inc., 1985); and the Apple IIgs Hardware Reference, chapter 4, for the same//! in the machine that came later, and for the colour names (Apple Computer,//! Inc., 1989).
const std = @import("std");
const font = @import("font.zig");const screen = @import("screen.zig");
/// Dot positions across the screen. Every mode divides into this: Lo-Res is/// forty blocks of fourteen, Hi-Res two hundred and eighty dots of two, and/// Double Hi-Res five hundred and sixty of one.pub const width = 560;
/// And lines down it. The bottom thirty-two are text in mixed mode.pub const height = 192;
/// How many dots one colour takes, which is the whole reason there is any/// colour at all: four dots is one cycle of the NTSC colour subcarrier.pub const dots_per_colour = 4;
/// The sixteen colours, in the order the hardware numbers them — which is the/// order a Lo-Res nibble is in, and the order the four-dot window comes out in/// (Apple Computer, Inc., 1989a, Table 4-14).pub const Colour = enum(u4) { black = 0, deep_red = 1, dark_blue = 2, purple = 3, dark_green = 4, dark_grey = 5, medium_blue = 6, light_blue = 7, brown = 8, orange = 9, light_grey = 10, pink = 11, light_green = 12, yellow = 13, aquamarine = 14, white = 15,};
pub const Rgb = struct { r: u8, g: u8, b: u8 };
/// What those sixteen look like — and this table is a choice, not a fact.////// There are no colours in an Apple II to be faithful to. There is a composite/// signal, and what comes out of it depends on the monitor: the Hardware/// Reference gives the sixteen their names and then says, in a footnote to the/// table, that the colour "varies, depending on the controls on the monitor"./// So it names them and declines to say what they are, which is the honest/// thing for a manual to do and no use at all to something that has to put/// pixels somewhere.////// What is here is four bits a channel, which is the depth the Apple IIgs's/// palette has, expanded by repeating each nibble. Replace it if you have a/// monitor in mind.pub const palette: [16]Rgb = expand(.{ 0x000, 0xd03, 0x009, 0xd2d, 0x072, 0x555, 0x22f, 0x6af, 0x852, 0xf62, 0xaaa, 0xf98, 0x1d1, 0xff9, 0x4f9, 0xfff,});
fn expand(twelve: [16]u12) [16]Rgb { var out: [16]Rgb = undefined; for (twelve, 0..) |value, i| { // A nibble repeated is the right way to widen it: $f becomes $ff and // $0 stays $0, so full and empty stay full and empty. const r: u8 = @truncate(value >> 8); const g: u8 = @truncate(value >> 4 & 0xf); const b: u8 = @truncate(value & 0xf); out[i] = .{ .r = r * 0x11, .g = g * 0x11, .b = b * 0x11 }; } return out;}
/// One line of the display, in both of the ways it can be looked at.////// They are the same signal: `dots` is what a monochrome monitor shows, and/// `colours` is what a colour one makes of it. Neither is derived from the/// other after the fact — the dots are what the machine puts out and the/// colours are what four of them at a time come to.pub const Line = struct { dots: [width]bool = @splat(false), colours: [width]u4 = @splat(0),};
/// What a scan line turned out to be. The line is filled in either way; this/// says what it is filled in *with*, so that a caller which can draw/// characters better than a bitmap can knows when to.pub const Scanned = union(enum) { /// Graphics. dots, /// Text: which of the twenty-four rows it is, and which of the eight scan /// lines within that row. text: struct { row: u8, line: u3 },};
/// Draws one line of the display, and says what kind of line it was.////// `y` runs from 0 at the top to 191 at the bottom. The machine has to be able/// to answer `mode`, `videoByte`, `displayPage`, `hiresPage` and `doubled`;/// see `screen.zig` for the first three, which it shares.pub fn scanLine(machine: anytype, y: u16, into: *Line) Scanned { const mode = machine.mode(); const is_text = mode.isText() or (mode.isMixed() and y >= height - 4 * 8);
into.dots = @splat(false); var painted = false; if (is_text) { painted = text(machine, y, into); } else if (mode.isHighResolution()) { if (mode.isDoubled()) doubleHighResolution(machine, y, into) else highResolution(machine, y, into); } else { if (mode.isDoubled()) doubleLowResolution(machine, y, into) else lowResolution(machine, y, into); } // A machine that colours its own text has said what each dot is; the // rest are a signal a monitor makes something of. if (!painted) { if (machine.colourBurst()) colourise(into) else monochrome(into); } if (is_text) return .{ .text = .{ .row = @intCast(y / 8), .line = @truncate(y) } }; return .dots;}
/// One scan line of a row of characters.////// Forty columns or eighty, whichever the machine is showing, and the machine/// says which rather than the mode: eighty-column text can sit at the bottom/// of a forty-column graphics screen, because what decides the width is 80COL/// and what decides the graphics is elsewhere.////// A character is seven dots either way. In forty columns each is drawn twice/// as wide, which is fourteen of the 560 positions; in eighty it is seven, and/// the two halves of memory alternate with the auxiliary one first. The/// character generator is the same chip and the same bitmap in both — only the/// dot clock changes.fn text(machine: anytype, y: u16, into: *Line) bool { const columns = machine.columns(); const base = screen.rowBase(machine.displayPage(), y / 8); const line: u3 = @truncate(y); const flash = machine.flashing(); // A machine whose character generator is RAM its software loads — an // Apple III — hands over the dots itself; the rest name a chip. const Machine = switch (@typeInfo(@TypeOf(machine))) { .pointer => |p| p.child, else => @TypeOf(machine), }; const soft = comptime @hasDecl(Machine, "glyphRow"); const set = if (comptime soft) {} else machine.characterSet(); const alternate = if (comptime soft) {} else machine.alternateCharacters(); // And a machine that keeps a foreground and background colour for each // character — an Apple III in its colour text mode — paints them here, // since no colour monitor could make them out of the dots. const coloured = comptime @hasDecl(Machine, "textColour"); var painted = false; const each: usize = @as(usize, width) / columns; for (0..columns) |column| { const byte = screen.byteAt(machine, columns, base, @intCast(column)); const dots = if (comptime soft) machine.glyphRow(byte, line, flash) else font.row(set, byte, line, alternate, flash); const at = column * each; const colour = if (comptime coloured) machine.textColour(columns, base, @intCast(column)) else null; for (0..font.width) |bit| { const on = dots >> @intCast(bit) & 1 != 0; into.dots[at + bit * (each / font.width)] = on; // In forty columns the same dot fills two positions, which is what // "drawn twice as wide" means and is why forty-column text is // fringed the way Hi-Res is rather than twice as badly. if (each == 2 * font.width) into.dots[at + bit * 2 + 1] = on; if (colour) |c| { painted = true; into.colours[at + bit * (each / font.width)] = if (on) c.foreground else c.background; if (each == 2 * font.width) into.colours[at + bit * 2 + 1] = if (on) c.foreground else c.background; } } } return painted;}
/// Where a Hi-Res line starts in memory.////// The Hi-Res page is interleaved twice over: eight lines of a row are eight/// kilobyte-apart blocks, three rows share each block, and the eight groups of/// three are forty bytes apart. It is the text page's trick done again one/// level deeper, and for the same reason — it falls out of counting the beam/// with a shift register rather than an adder. Technical reference, figure 2-7.pub fn hiresBase(page: u16, y: u16) u16 { return page + 0x400 * (y & 7) + 0x80 * (y >> 3 & 7) + 0x28 * (y >> 6);}
/// Two hundred and eighty dots from forty bytes, each dot written twice.////// Bit 7 of a byte is not a dot. It delays the other seven by one position of/// the 560 — half a dot — which shifts them into the other half of the colour/// cycle and so trades violet for blue and green for orange. The position it/// vacates carries on showing whatever was there, because the shift register/// has not been given anything new to put out; that is the `previous` below./// On a board without the delay — the first Apple IIs, before Revision 1 —/// bit 7 does nothing, and Hi-Res has violet, green and white and no more/// (Sather, 1983, p. 8-19).fn highResolution(machine: anytype, y: u16, into: *Line) void { const base = hiresBase(machine.hiresPage(), y); const delays = machine.delaysHires(); for (0..40) |column| { const byte = machine.videoByte(false, base + @as(u16, @intCast(column))); const at = column * 14; const delayed = delays and byte & 0x80 != 0; if (delayed) into.dots[at] = at > 0 and into.dots[at - 1]; for (0..7) |bit| { const on = byte >> @intCast(bit) & 1 != 0; const first = at + 2 * bit + @intFromBool(delayed); into.dots[first] = on; if (first + 1 < width) into.dots[first + 1] = on; } }}
/// Five hundred and sixty dots from eighty bytes, one dot each.////// The two halves of memory are read alternately and the auxiliary one goes/// first, which is the same interleaving eighty-column text uses and for the/// same reason: it is the same circuit fetching them. Bit 7 is not used at/// all here — there is no half-dot delay, because there is no half dot left.fn doubleHighResolution(machine: anytype, y: u16, into: *Line) void { const base = hiresBase(machine.hiresPage(), y); for (0..40) |column| { const address = base + @as(u16, @intCast(column)); for ([2]bool{ true, false }, 0..) |auxiliary, half| { const byte = machine.videoByte(auxiliary, address); const at = column * 14 + half * 7; for (0..7) |bit| into.dots[at + bit] = byte >> @intCast(bit) & 1 != 0; } }}
/// Which nibble of a text-page byte is showing on this line.////// A Lo-Res block is half a text row: the low nibble is the top four scan/// lines of the cell and the high nibble the bottom four, which is how/// forty-eight rows of blocks fit into twenty-four rows of memory.fn lowNibble(byte: u8, y: u16) u4 { return if (y >> 2 & 1 == 0) @truncate(byte) else @truncate(byte >> 4);}
/// Forty blocks of fourteen dots, each carrying its colour as a pattern.////// The nibble is not looked up in a table and painted on. It is put out as a/// four-dot pattern, over and over, which is what the hardware does with it —/// and `colourise` then reads it back as the same nibble, because a repeating/// four-dot pattern is exactly what one colour is. Two things fall out of/// doing it this way rather than by table. A Lo-Res screen on a monochrome/// monitor comes out as the vertical stripes it really is. And because a block/// is fourteen dots rather than sixteen, the pattern does not restart in step/// with the block, so neighbouring colours disturb each other at the join,/// which is a thing real Lo-Res does and is not a bug here.fn lowResolution(machine: anytype, y: u16, into: *Line) void { const base = screen.rowBase(machine.displayPage(), y / 8); for (0..40) |column| { const byte = machine.videoByte(false, base + @as(u16, @intCast(column))); const nibble = lowNibble(byte, y); for (0..14) |dot| { const at = column * 14 + dot; into.dots[at] = nibble >> @intCast(at % dots_per_colour) & 1 != 0; } }}
/// The same again, eighty blocks of seven, out of both halves of memory.////// Nothing here rotates the auxiliary half's colours, which the books about/// Double Lo-Res say has to be done. It does not have to be done: the/// rotation is what happens when the colour is looked up in a table and/// painted on, and it has to be undone afterwards. Put the nibble out as the/// pattern the hardware puts out and the phase looks after itself, because/// seven dots is not a whole number of colour cycles and the next block starts/// wherever the last one left off.fn doubleLowResolution(machine: anytype, y: u16, into: *Line) void { const base = screen.rowBase(machine.displayPage(), y / 8); for (0..40) |column| { const address = base + @as(u16, @intCast(column)); for ([2]bool{ true, false }, 0..) |auxiliary, half| { const nibble = lowNibble(machine.videoByte(auxiliary, address), y); for (0..7) |dot| { const at = column * 14 + half * 7 + dot; into.dots[at] = nibble >> @intCast(at % dots_per_colour) & 1 != 0; } } }}
/// Turns a line of dots into a line of colours.////// Four adjacent dots make the colour, the leftmost of them being the low bit,/// and which of the four positions the window starts on rotates it. That/// rotation is the phase of the colour subcarrier, and it is why the same four/// dots one position along are a different colour.////// Off the right-hand end there are no dots, and the last three positions are/// coloured as though the screen went on in black. A real machine puts the/// blanking interval there, which is the same thing./// What a colour monitor shows with no colour burst to lock to: the dots,/// white on black. The same picture a monochrome monitor gives, which is why/// `dots` and `colours` agree on such a line.pub fn monochrome(into: *Line) void { for (0..width) |x| into.colours[x] = if (into.dots[x]) @intFromEnum(Colour.white) else @intFromEnum(Colour.black);}
pub fn colourise(into: *Line) void { for (0..width) |x| { var window: u4 = 0; for (0..dots_per_colour) |i| { if (x + i < width and into.dots[x + i]) window |= @as(u4, 1) << @intCast(i); } const phase: u2 = @truncate(x); into.colours[x] = std.math.rotl(u4, window, phase); }}
const testing = std.testing;
/// A machine with nothing in it but a screen, for the tests below.const Bare = struct { main: [0x10000]u8 = @splat(0), aux: [0x10000]u8 = @splat(0), showing: screen.Mode = .high_resolution, page: u16 = 0x2000, alternate: bool = false, flash: bool = false, /// Whether the colour burst is on the signal and whether bit 7 delays a /// Hi-Res byte: both true, as on any board after the first, unless a test /// is about the first. burst: bool = true, delays: bool = true,
pub fn mode(self: *const Bare) screen.Mode { return self.showing; } pub fn colourBurst(self: *const Bare) bool { return self.burst; } pub fn delaysHires(self: *const Bare) bool { return self.delays; } pub fn videoByte(self: *const Bare, auxiliary: bool, address: u16) u8 { return if (auxiliary) self.aux[address] else self.main[address]; } pub fn hiresPage(self: *const Bare) u16 { return self.page; } pub fn displayPage(_: *const Bare) u16 { return 0x0400; } pub fn columns(self: *const Bare) u16 { return if (self.showing.isDoubled()) 80 else 40; } pub fn characterSet(_: *const Bare) font.Set { return .apple2e_enhanced; } pub fn alternateCharacters(self: *const Bare) bool { return self.alternate; } pub fn flashing(self: *const Bare) bool { return self.flash; }
/// Fills a Hi-Res line with a pair of bytes alternating, which is what /// one colour across a line takes: seven dots to a byte is odd, so the /// parity flips at every boundary and the second byte has to put it back. pub fn fillHires(self: *Bare, y: u16, even: u8, odd: u8) void { const base = hiresBase(0x2000, y); for (0..40) |c| self.main[base + c] = if (c % 2 == 0) even else odd; }};
test "the sixteen colours widen from four bits a channel" { try testing.expectEqual(Rgb{ .r = 0, .g = 0, .b = 0 }, palette[@intFromEnum(Colour.black)]); try testing.expectEqual(Rgb{ .r = 0xff, .g = 0xff, .b = 0xff }, palette[@intFromEnum(Colour.white)]); // Every nibble widens to a byte by repeating, so nothing lands off the end. for (palette) |colour| { try testing.expectEqual(colour.r >> 4, colour.r & 0xf); try testing.expectEqual(colour.g >> 4, colour.g & 0xf); try testing.expectEqual(colour.b >> 4, colour.b & 0xf); }}
test "the four hi-res colours everybody knows come out right" { // This is what fixes the direction of the rotation in `colourise`, since // the manual's own table is too badly scanned to read it off. $55 is // violet and $2a is green; setting the high bit delays them half a dot // and they become blue and orange. One rotation direction gives all four // and the other gives none of them. var machine: Bare = .{}; var line: Line = .{};
const cases = [_]struct { even: u8, odd: u8, colour: Colour }{ .{ .even = 0x55, .odd = 0x2a, .colour = .purple }, .{ .even = 0x2a, .odd = 0x55, .colour = .light_green }, .{ .even = 0xd5, .odd = 0xaa, .colour = .medium_blue }, .{ .even = 0xaa, .odd = 0xd5, .colour = .orange }, .{ .even = 0x7f, .odd = 0x7f, .colour = .white }, .{ .even = 0x00, .odd = 0x00, .colour = .black }, }; for (cases) |case| { machine.fillHires(0, case.even, case.odd); try testing.expectEqual(Scanned.dots, scanLine(&machine, 0, &line)); // Away from the edges, where the pattern has settled, every dot // position is the same colour: this is a screen filled with it. for (100..500) |x| { testing.expectEqual(@intFromEnum(case.colour), line.colours[x]) catch |err| { std.debug.print("bytes ${x:0>2}/${x:0>2} at dot {d}: got {d}\n", .{ case.even, case.odd, x, line.colours[x], }); return err; }; } }}
test "the high bit of a hi-res byte delays its dots by one position" { var machine: Bare = .{}; var line: Line = .{}; machine.fillHires(0, 0x55, 0x55); _ = scanLine(&machine, 0, &line); // $55 lights dots 0, 2, 4 and 6 of seven, each two positions wide. try testing.expect(line.dots[14] and line.dots[15]); try testing.expect(!line.dots[16] and !line.dots[17]);
machine.fillHires(0, 0xd5, 0xd5); _ = scanLine(&machine, 0, &line); // The same seven dots, one position later. try testing.expect(line.dots[15] and line.dots[16]); try testing.expect(!line.dots[17] and !line.dots[18]);}
test "a hi-res line is where the interleaving says it is" { // Technical reference, figure 2-7: eight lines a kilobyte apart, three // rows sharing each block, and the groups forty bytes on. try testing.expectEqual(@as(u16, 0x2000), hiresBase(0x2000, 0)); try testing.expectEqual(@as(u16, 0x2400), hiresBase(0x2000, 1)); try testing.expectEqual(@as(u16, 0x3c00), hiresBase(0x2000, 7)); try testing.expectEqual(@as(u16, 0x2080), hiresBase(0x2000, 8)); try testing.expectEqual(@as(u16, 0x2028), hiresBase(0x2000, 64)); try testing.expectEqual(@as(u16, 0x3fd0), hiresBase(0x2000, 191)); // And the second page is the same shape eight kilobytes on. try testing.expectEqual(@as(u16, 0x4000), hiresBase(0x4000, 0)); try testing.expectEqual(@as(u16, 0x5fd0), hiresBase(0x4000, 191));
// Every one of the 192 lines lands somewhere different, and inside the // page, which is the property the formula exists for. var seen: [0x2000]bool = @splat(false); for (0..height) |y| { const base = hiresBase(0x2000, @intCast(y)); try testing.expect(base >= 0x2000 and base + 40 <= 0x4000); try testing.expect(!seen[base - 0x2000]); seen[base - 0x2000] = true; }}
test "a lo-res nibble comes back as the colour it is" { // Which is the point of putting the nibble out as a pattern rather than // looking it up: colouring the pattern gives the nibble back. var machine: Bare = .{}; machine.showing = .low_resolution; var line: Line = .{};
var colour: u8 = 0; while (colour < 16) : (colour += 1) { const base = screen.rowBase(0x0400, 0); for (0..40) |c| machine.main[base + c] = colour | colour << 4; _ = scanLine(&machine, 0, &line); for (100..500) |x| try testing.expectEqual(@as(u4, @truncate(colour)), line.colours[x]); // And the bottom half of the cell shows the high nibble, which here // is the same one. _ = scanLine(&machine, 4, &line); for (100..500) |x| try testing.expectEqual(@as(u4, @truncate(colour)), line.colours[x]); }
// The two nibbles are different halves of the row: low on top. const base = screen.rowBase(0x0400, 0); for (0..40) |c| machine.main[base + c] = 0xf0; _ = scanLine(&machine, 0, &line); for (100..500) |x| try testing.expectEqual(@as(u4, 0), line.colours[x]); _ = scanLine(&machine, 4, &line); for (100..500) |x| try testing.expectEqual(@as(u4, 15), line.colours[x]);}
test "mixed mode says which rows are text rather than drawing them" { var machine: Bare = .{}; machine.showing = .high_resolution_mixed; var line: Line = .{};
try testing.expectEqual(Scanned.dots, scanLine(&machine, 159, &line)); switch (scanLine(&machine, 160, &line)) { .dots => return error.TestUnexpectedResult, .text => |where| { try testing.expectEqual(@as(u8, 20), where.row); try testing.expectEqual(@as(u3, 0), where.line); }, } switch (scanLine(&machine, 191, &line)) { .dots => return error.TestUnexpectedResult, .text => |where| { try testing.expectEqual(@as(u8, 23), where.row); try testing.expectEqual(@as(u3, 7), where.line); }, }
// Above them it is graphics all the way up, and the row before the text // starts is the last one drawn. try testing.expectEqual(Scanned.dots, scanLine(&machine, 0, &line)); machine.showing = .high_resolution; for (0..height) |y| { try testing.expectEqual(Scanned.dots, scanLine(&machine, @intCast(y), &line)); }}
test "double hi-res takes both halves of memory, auxiliary first" { var machine: Bare = .{}; machine.showing = .double_high_resolution; var line: Line = .{};
const base = hiresBase(0x2000, 0); machine.aux[base] = 0x7f; // seven dots on, from the auxiliary half machine.main[base] = 0x00; // and seven off from the main one _ = scanLine(&machine, 0, &line); for (0..7) |x| try testing.expect(line.dots[x]); for (7..14) |x| try testing.expect(!line.dots[x]);
// All 560 of them are separate dots: there is no doubling here and no // half-dot delay, so bit 7 is not a dot at all. machine.aux[base] = 0x80; machine.main[base] = 0x80; _ = scanLine(&machine, 0, &line); for (0..14) |x| try testing.expect(!line.dots[x]);}
test "double lo-res needs no rotating of the auxiliary half" { // A block is seven dots, which is not a whole number of colour cycles, so // the phase carries from one block into the next by itself. var machine: Bare = .{}; machine.showing = .double_low_resolution; var line: Line = .{};
var colour: u8 = 0; while (colour < 16) : (colour += 1) { const base = screen.rowBase(0x0400, 0); for (0..40) |c| { machine.main[base + c] = colour | colour << 4; machine.aux[base + c] = colour | colour << 4; } _ = scanLine(&machine, 0, &line); for (100..500) |x| try testing.expectEqual(@as(u4, @truncate(colour)), line.colours[x]); }}
test "text is seven dots a character, twice as wide in forty columns" { var machine: Bare = .{}; machine.showing = .text; var line: Line = .{};
// 'L' is the plainest asymmetric shape there is, and $cc is a normal one. const base = screen.rowBase(0x0400, 0); machine.main[base] = 0xcc; switch (scanLine(&machine, 0, &line)) { .dots => return error.TestUnexpectedResult, .text => |where| try testing.expectEqual(@as(u8, 0), where.row), } // The stem of the L: one dot in from the left, and two positions wide // because forty-column text draws each of its seven dots twice. try testing.expect(!line.dots[0] and !line.dots[1]); try testing.expect(line.dots[2] and line.dots[3]); try testing.expect(!line.dots[4] and !line.dots[5]); // And its foot, on the seventh of the eight scan lines. _ = scanLine(&machine, 6, &line); for (2..12) |x| try testing.expect(line.dots[x]);
// In eighty columns the same character is seven positions rather than // fourteen, and the auxiliary half supplies the even columns. machine.showing = .text_80; machine.aux[base] = 0xcc; machine.main[base] = 0xa0; // a space, in the odd column _ = scanLine(&machine, 6, &line); for (1..6) |x| try testing.expect(line.dots[x]); for (7..14) |x| try testing.expect(!line.dots[x]);}
test "a text screen is fringed while the colour burst is on the signal" { // Which is what a Revision 0 Apple II showed, and what every machine // shows in the four text rows under mixed graphics: a dot two positions // wide is a colour, and the strokes of a character are two positions wide. var machine: Bare = .{}; machine.showing = .text; var line: Line = .{}; const base = screen.rowBase(0x0400, 0); for (0..40) |c| machine.main[base + c] = 0xcc; // a row of Ls _ = scanLine(&machine, 0, &line);
var coloured = false; for (line.colours) |colour| { if (colour != @intFromEnum(Colour.black) and colour != @intFromEnum(Colour.white)) coloured = true; } try testing.expect(coloured); // The dots themselves are the black and white a monochrome monitor shows, // which is what `dots` is for. try testing.expect(line.dots[2]); try testing.expect(!line.dots[0]);}
test "with the colour burst killed a text screen is white, as Revision 1 made it" { // "A Color Burst Killer was added in Revision 1 to eliminate the COLOR // BURST in TEXT MODE" (Sather, 1983, p. 8-5), and the IIe kept it: "The // COLOR BURST does not occur if the TEXT soft switch is set" (Sather, // 1985, p. 8-4). The same row of Ls, and every position is black or white. var machine: Bare = .{ .burst = false }; machine.showing = .text; var line: Line = .{}; const base = screen.rowBase(0x0400, 0); for (0..40) |c| machine.main[base + c] = 0xcc; _ = scanLine(&machine, 0, &line); for (line.colours, line.dots) |colour, dot| { try testing.expectEqual(if (dot) @intFromEnum(Colour.white) else @intFromEnum(Colour.black), colour); }}
test "a board without the delay draws bit 7 as nothing" { // Revision 0: "280 programmable dots per scan with violet, green, or white // coloring" and no more (Sather, 1983, p. 8-19). The bytes that are blue // and orange on a later board are violet and green on this one, because // the bit that would have moved them does nothing. var machine: Bare = .{ .delays = false }; var line: Line = .{}; machine.fillHires(0, 0xd5, 0xaa); // $55 and $2a with bit 7 set: blue elsewhere _ = scanLine(&machine, 0, &line); var seen: u16 = 0; for (line.colours[0..28]) |colour| seen |= @as(u16, 1) << colour; try testing.expect(seen & (1 << @intFromEnum(Colour.purple)) != 0); try testing.expect(seen & (1 << @intFromEnum(Colour.medium_blue)) == 0);}
test "flashing turns a character over and the alternate set does not" { var machine: Bare = .{}; machine.showing = .text; var line: Line = .{}; const base = screen.rowBase(0x0400, 0); machine.main[base] = 0x4c; // a flashing 'L' in the primary set
_ = scanLine(&machine, 6, &line); const dim = line.dots[2]; machine.flash = true; _ = scanLine(&machine, 6, &line); try testing.expect(line.dots[2] != dim);
// With the alternate set that byte is MouseText and does not flash. machine.alternate = true; machine.flash = false; _ = scanLine(&machine, 6, &line); const still = line.dots; machine.flash = true; _ = scanLine(&machine, 6, &line); try testing.expectEqualSlices(bool, &still, &line.dots);}