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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! A Videx Videoterm: eighty columns on a machine that has forty.//!//! An Apple II draws its text out of the machine's own memory with the//! machine's own character generator, and it draws forty columns of it. This//! card draws its own. It carries a 6845 CRT controller, two kilobytes of//! screen memory the processor cannot see all of at once, and a character//! generator in a socket of its own, and it puts a picture on a monitor that//! has nothing to do with what the Apple's video is doing. That is why an//! Apple II+ with one of these could run Pascal and word processors that//! wanted eighty columns, on a machine whose own video could not.//!//! The processor reaches four things, and the manual sets out all of them.//!//! - **The device select group, `$C080 + slot * 16`.** Sixteen addresses,//! and the low four bits of the one touched are the whole interface://! "the very lowest bit, bit 0, controls whether a register is being//! accessed (0) or the contents of the register is being accessed (1) ...//! the next two bits, bits 2 and 3, determine which page will be//! selected" (Videx, 1982, pp. 5-9). Bit 1 asks for an eight-dot cell//! rather than a nine-dot one, and the card "automatically interprets//! this as 0 (9 cells), so there is no reason to change this".//!//! Every access does all of it. A read of `$C0B0` on a card in slot 3//! "selects for slot 3, a register access and page zero all at once",//! which is why the firmware can select a page with a `PEEK` and why//! writing a 6845 register number to `$C0B0` also puts page zero in the//! window as a side effect.//!//! - **The firmware, `$C800` to `$CBFF`.** One kilobyte of it, in the//! expansion ROM space every card shares. The `$Cn00` page a card is//! scanned at is the top quarter of that same kilobyte, which is where//! the Pascal 1.1 signature and the four entry points sit.//!//! - **The window, `$CC00` to `$CDFF`.** Five hundred and twelve bytes of//! the two kilobytes of screen memory. The other three quarters are//! reached by selecting another page first: "the 2048 byte RAM area is//! subdivided into four 512-byte segments, each called a page" (Videx,//! 1982, pp. 5-12).//!//! - **`$CE00` to `$CFFF`,** which "is not presently used".//!//! What the 6845 does with the two kilobytes is the part worth understanding.//! It walks them from wherever its start address register points, eighty//! bytes to a row and twenty-four rows, and it wraps at two kilobytes rather//! than stopping. Eighty by twenty-four is 1,920 bytes, so there are 128 to//! spare, and scrolling is not a copy: the firmware adds eighty to the start//! address and the whole screen moves up, leaving the row that fell off the//! top to be overwritten as the new bottom row. A model that scrolled by//! moving bytes about would draw the right picture and be wrong about//! everything a program could see.//!//! The character generator is a 2K part holding 128 glyphs of sixteen rows//! each, seven dots wide in the low seven bits of each row, and the card//! shows nine of those sixteen rows because that is the cell the 6845 is set//! up for. A second socket takes an alternate set, which is what the//! inverse-video and lower-case modifications put in it.//!//! Neither ROM is here. Both are Videx's, as the boot PROM in `disk2.zig` is//! Apple's, so the machine that fits the card is the machine that was given//! the parts, and holds them; what is modelled here is the card around them.
const std = @import("std");const testing = std.testing;
/// What the firmware sets the 6845 up to draw, and what everything here/// assumes when asked what is on the screen.pub const columns = 80;pub const rows = 24;
/// Nine scan lines to a character cell, which is what the Videoterm's own/// firmware asks for. It is R9 + 1 and R9 is a register, so it is a default/// rather than a property of the card: the manual's table of defaults is/// headed "the standard VIDEOTERM default values used with its various/// character cell matrix sizes" (Videx, 1982, pp. 5-6), and a Videoterm/// compatible board's firmware really does ask for ten. Ask `Card.cellLines`/// for what a card is currently set to; this is what the picture is sized/// for.pub const cell_lines = 9;
/// Seven dots of a character generator row are the glyph, and the cell they/// sit in is nine wide: "bit 1 controls whether an eight or nine cell matrix/// width is being used. VIDEOTERM automatically interprets this as 0 (9/// cells)" (Videx, 1982, pp. 5-9). The two spare dots are the gap to the next/// character, which is why the letters do not touch.pub const glyph_dots = 7;pub const cell_dots = 9;
/// The picture, in dots: eighty cells of nine across, twenty-four rows of nine/// down. Neither number is a choice made here — both fall out of what the/// firmware puts in the 6845.////// It is the *Videoterm's* firmware, though, and a Videoterm compatible board/// asks for something else: the Space 84's sets R9 to nine, which is a/// ten-line cell and twenty-four rows of it is two hundred and forty lines./// So these are what the standard setup comes to and not what any given card/// is showing — ask `pictureWidth` and `pictureHeight` for that.pub const width = columns * cell_dots;pub const height = rows * cell_lines;
/// The largest picture this will describe.////// R1 is eight bits and R6 is seven, so the registers allow a screen far/// larger than any monitor of the period could show and far larger than two/// kilobytes of memory could fill. A surface has to be allocated before/// anything is drawn, so a program with nonsense in its registers is bounded/// here rather than allowed to ask for an arbitrarily large one.pub const max_columns = 160;pub const max_rows = 48;
/// Two kilobytes of screen memory, in four pages of half a kilobyte.pub const ram_size = 2048;pub const window_size = 512;pub const page_count = ram_size / window_size;
/// The character generator: 128 glyphs of sixteen rows.pub const charset_size = 2048;pub const charset_rows = 16;pub const glyph_count = charset_size / charset_rows;
/// The firmware, one kilobyte of it, and the quarter of it that answers at/// `$Cn00` when the machine scans the slot.pub const firmware_size = 1024;pub const slot_page_at = firmware_size - 256;
/// The 6845, as much of one as a Videoterm uses.////// Eighteen registers reached through two addresses: a number is written to/// the address register and the value follows to the data register. R0 to R13/// are write-only on the real part and R16 and R17 are read-only, which is/// not a detail a program can ignore — a program that reads back what it/// wrote to R1 gets nothing, and the firmware keeps its own copy of the start/// address in the Apple's scratchpad rather than asking the chip.pub const Crtc = struct { /// The register the next data access reaches. Five bits: the part has /// eighteen registers and ignores anything above them. address: u5 = 0,
/// R0 to R17. R14 and R15 are the cursor and are read/write; R16 and R17 /// are the light pen and are read-only, and nothing on this card ever /// strobes one. register: [18]u8 = @splat(0),
/// Which registers a program can read back. The 6845 answers zero for the /// rest rather than what was written. fn readable(at: u5) bool { return at >= 14 and at <= 17; }
pub fn write(self: *Crtc, value: u8) void { if (self.address < self.register.len) self.register[self.address] = value; }
pub fn read(self: *const Crtc) u8 { if (self.address >= self.register.len) return 0; return if (readable(self.address)) self.register[self.address] else 0; }
/// Where the top left character of the screen is, from R12 and R13. Only /// the low fourteen bits are an address, and on this card only the low /// eleven of those reach memory, since there are two kilobytes of it. pub fn startAddress(self: *const Crtc) u16 { const high: u16 = @as(u16, self.register[12] & 0x3f) << 8; return high | self.register[13]; }
/// Where the cursor is, from R14 and R15, in the same units. pub fn cursorAddress(self: *const Crtc) u16 { const high: u16 = @as(u16, self.register[14] & 0x3f) << 8; return high | self.register[15]; }
/// The first and last scan lines of the cell the cursor fills, from R10 /// and R11. R10 also carries how the cursor blinks in bits 6 and 5, which /// is why the start line is masked out of it. pub fn cursorLines(self: *const Crtc) struct { first: u5, last: u5 } { return .{ .first = @truncate(self.register[10] & 0x1f), .last = @truncate(self.register[11] & 0x1f), }; }
/// How the cursor blinks, which is bits 6 and 5 of R10: not displayed at /// all, displayed and steady, or blinking at one of two rates. pub const Blink = enum { steady, hidden, fast, slow };
pub fn blink(self: *const Crtc) Blink { return switch (@as(u2, @truncate(self.register[10] >> 5))) { 0b00 => .steady, 0b01 => .hidden, 0b10 => .fast, // one blink in sixteen fields 0b11 => .slow, // one in thirty-two, which is what the firmware asks for }; }};
/// The card.pub const Card = struct { /// Whether one is in the machine at all. A slot with no card in it answers /// nothing, and the machine's own ROM keeps the addresses. fitted: bool = false,
/// The screen memory. Not cleared to zero: a card that has just been /// powered on holds whatever it holds, and the firmware clears it. ram: [ram_size]u8 = @splat(' '),
/// Which 512-byte page of that the window shows. Set by bits 2 and 3 of /// every device select access, whether the access wanted a page or not. page: u2 = 0,
/// The CRT controller. crtc: Crtc = .{},
/// Whether the last device select asked for an eight-dot cell rather than /// the nine the card is built around. Kept because the bit exists and a /// program can set it; nothing here draws differently for it, which is /// what the manual says the card does with it. eight_dot: bool = false,
/// One of the sixteen device select addresses was touched. The offset is /// the low four bits of it, and everything the card does with an access it /// does here, whichever direction the access was going. /// /// Returns what a read of that address gives back, which is the 6845 for /// an odd offset and the floating bus for an even one. Nothing here models /// a floating bus, so an even offset answers zero. pub fn access(self: *Card, offset: u4, writing: bool, value: u8) u8 { // Bits 2 and 3 are the page, and they are the page on every access. self.page = @truncate(offset >> 2); self.eight_dot = offset & 0x02 != 0;
if (offset & 0x01 == 0) { // The address register. Write-only on the part: reading it back is // not something a 6845 offers. if (writing) self.crtc.address = @truncate(value & 0x1f); return 0; } if (writing) { self.crtc.write(value); return 0; } return self.crtc.read(); }
/// A read of the `$CC00` window, at an offset within its 512 bytes. pub fn windowRead(self: *const Card, offset: u9) u8 { return self.ram[self.windowAt(offset)]; }
/// A write to it. pub fn windowWrite(self: *Card, offset: u9, value: u8) void { self.ram[self.windowAt(offset)] = value; }
/// How many scan lines are in a character cell, from R9. Nine on a /// Videoterm, and not a constant of the hardware: the register is five /// bits and a program may put anything in it. pub fn cellLines(self: *const Card) u8 { return @as(u8, self.crtc.register[9]) + 1; }
/// How many rows and columns are being displayed, from R6 and R1. Also /// not constants: twenty-four by eighty is what the firmware asks for. pub fn rowsShown(self: *const Card) u8 { return self.crtc.register[6]; }
pub fn columnsShown(self: *const Card) u8 { return self.crtc.register[1]; }
/// Where in the two kilobytes an offset into the window lands, which is /// the page and the offset side by side. The machine needs this as well as /// the card does: the window is memory, so the memory model hands back a /// pointer into it rather than going through a read and a write. pub fn windowAt(self: *const Card, offset: u9) u11 { return (@as(u11, self.page) << 9) | offset; }
/// Where in the screen memory a row and column of the display comes from. /// /// The 6845 starts at its start address and counts forward, eighty to a /// row, and this card gives it eleven bits of address, so it wraps at two /// kilobytes. That wrap is the whole of how the screen scrolls. pub fn addressOf(self: *const Card, row: usize, column: usize) u11 { const from: u16 = self.crtc.startAddress(); const wide: usize = @max(1, self.columnsShown()); const forward: u16 = @intCast(row * wide + column); return @truncate(from +% forward); }
/// The character at a place on the screen, as the byte in memory. pub fn characterAt(self: *const Card, row: usize, column: usize) u8 { return self.ram[self.addressOf(row, column)]; }
/// One scan line of one character cell, as seven dots with the leftmost in /// bit 6, which is how the character generator holds them. /// /// The generator is the machine's to hold rather than the card's, the way /// a Disk II's boot PROM is: it is a part in a socket and the model that /// fits the card is the model that was given the part. A line past the /// ninth is blank, and so is one past the sixteen rows the generator /// holds. /// /// Bit 7 of the byte in memory picks the alternate set, which is a second /// part in the other socket. A card with only one answers from that one /// whichever the byte asks for, because a socket with nothing in it cannot /// draw anything and a blank screen is a worse lie than the wrong glyph. pub fn dotsAt( self: *const Card, set: *const [charset_size]u8, alternate: ?*const [charset_size]u8, row: usize, column: usize, line: usize, ) u8 { if (line >= self.cellLines() or line >= charset_rows) return 0; const byte = self.characterAt(row, column); const from = if (byte & 0x80 != 0) alternate orelse set else set; const glyph: usize = byte & 0x7f; return from[glyph * charset_rows + line] & 0x7f; }
/// Whether the cursor is in this cell, and on which of its scan lines. /// Says nothing about blinking: that is the caller's clock, not the card's. pub fn cursorIn(self: *const Card, row: usize, column: usize) ?struct { first: u5, last: u5 } { if (self.crtc.blink() == .hidden) return null; if (self.addressOf(row, column) != @as(u11, @truncate(self.crtc.cursorAddress()))) return null; const lines = self.crtc.cursorLines(); return .{ .first = lines.first, .last = lines.last }; }};
/// How wide the picture is, in dots: the columns the 6845 is displaying, each/// in a cell of nine dots or eight as the device select last asked.////// A card nobody has programmed has R1 at zero and puts out no picture at all,/// which is true of the part and useless as a surface size, so this answers/// with the shape the Videoterm's own firmware sets up until something has/// asked for another.pub fn pictureWidth(card: *const Card) u16 { const across = card.columnsShown(); if (across == 0) return width; const dots: u16 = if (card.eight_dot) cell_dots - 1 else cell_dots; return @min(max_columns, across) * dots;}
/// And how tall: the rows it is displaying, each of the scan lines R9 asks for.pub fn pictureHeight(card: *const Card) u16 { const down = card.rowsShown(); if (down == 0) return height; return @min(max_rows, down) * @as(u16, card.cellLines());}
/// One scan line of the picture, as dots.////// Monochrome, and not because anything has been simplified: the card puts out/// a single video signal with no colour burst anywhere near it, and what/// colour the letters came out was the phosphor's business rather than the/// card's.pub const Line = struct { dots: [max_columns * cell_dots]bool = @splat(false),};
/// Draws one line of the card's display.////// `y` runs from 0 at the top to 215 at the bottom, nine lines to each of the/// twenty-four rows. The character generator is passed in because it is a part/// in a socket rather than something the card knows, and the alternate is the/// second socket, empty on a card nobody modified.////// The cursor is drawn here rather than left to the caller, because on the/// real card it is drawn here: the 6845 raises its cursor output for the scan/// lines R10 and R11 name, and what that does to the video is invert them./// Whether it is blinking at the moment is the caller's clock and not the/// card's, which is what `blinked` says.pub fn scanLine( card: *const Card, set: *const [charset_size]u8, alternate: ?*const [charset_size]u8, y: u16, blinked: bool, into: *Line,) void { into.* = .{}; if (y >= pictureHeight(card)) return; // How tall a cell is and how many of them there are is the 6845's to say, // not this file's. A card told to draw a ten-line cell — which a // Videoterm compatible board's firmware does — has rows in different // places, and a picture drawn as though they were nine lines apart would // be wrong from the second row down. const tall: u16 = @max(1, card.cellLines()); const row = y / tall; const line = y % tall; if (row >= card.rowsShown()) return; const across: usize = @min(max_columns, card.columnsShown());
for (0..across) |column| { const glyph = card.dotsAt(set, alternate, row, column, line); // The cursor is the whole cell inverted, on the scan lines the 6845 // was told to put it on. R10 above R11 is a cursor that wraps rather // than one that is nothing, which is what the part does with it. const cursor = on: { if (!blinked) break :on false; const where = card.cursorIn(row, column) orelse break :on false; const at: u5 = @intCast(line); break :on if (where.first <= where.last) at >= where.first and at <= where.last else at >= where.first or at <= where.last; }; const wide: usize = if (card.eight_dot) cell_dots - 1 else cell_dots; for (0..wide) |dot| { // The glyph is the leftmost seven of the cell; the rest is the gap // to the next character, and only the cursor ever fills it. const lit = dot < glyph_dots and glyph & (@as(u8, 0x40) >> @intCast(dot)) != 0; into.dots[column * wide + dot] = lit != cursor; } }}
/// A card set up the way the firmware sets one up, for the tests that draw.////// A 6845 out of reset displays nothing: every register is zero, which is no/// columns and no rows. That is the part's business and not a shortcoming, so/// the tests that want a screen program one first, exactly as the firmware/// does on the way up — these are the sixteen bytes it writes.fn configured() Card { var card: Card = .{ .fitted = true }; const written = [_]u8{ 122, columns, 94, 47, 34, 0, rows, 29, 0, cell_lines - 1, 0xe0, 8, 0, 0, 0, 0, }; for (written, 0..) |value, register| { card.crtc.address = @intCast(register); card.crtc.write(value); } card.crtc.address = 0; return card;}
test "a 6845 nobody has programmed displays nothing at all" { // Not a special case in the model: R1 and R6 are zero out of reset, which // is no columns and no rows, and there is nothing to draw until a program // says otherwise. var card: Card = .{ .fitted = true }; card.ram = @splat('A'); try testing.expectEqual(@as(u8, 0), card.columnsShown()); try testing.expectEqual(@as(u8, 0), card.rowsShown());
var set: [charset_size]u8 = @splat(0xff); var line: Line = .{}; scanLine(&card, &set, null, 0, false, &line); for (line.dots) |dot| try testing.expect(!dot);}
test "the low bits of a device select say everything at once" { var card: Card = .{ .fitted = true };
// "Access of $C0B0 selects for slot 3, a register access and page zero all // at once" (Videx, 1982, pp. 5-9). The offset is the low nibble, so that // is offset 0. _ = card.access(0x0, true, 12); try testing.expectEqual(@as(u5, 12), card.crtc.address); try testing.expectEqual(@as(u2, 0), card.page);
// And the odd address is the register's contents. _ = card.access(0x1, true, 0x07); try testing.expectEqual(@as(u8, 0x07), card.crtc.register[12]);
// Bits 2 and 3 are the page, on any access at all — including one whose // business was a 6845 register. _ = card.access(0xc, true, 0); try testing.expectEqual(@as(u2, 3), card.page); _ = card.access(0x4, false, 0); try testing.expectEqual(@as(u2, 1), card.page); _ = card.access(0x8, false, 0); try testing.expectEqual(@as(u2, 2), card.page);
// Bit 1 is the cell width, which the card is built for one of. _ = card.access(0x2, false, 0); try testing.expect(card.eight_dot); _ = card.access(0x0, false, 0); try testing.expect(!card.eight_dot);}
test "the window is a quarter of the memory, and the page says which" { var card: Card = .{ .fitted = true }; card.ram = @splat(0);
// Page zero is the first 512 bytes. _ = card.access(0x0, false, 0); card.windowWrite(0, 'A'); card.windowWrite(511, 'B'); try testing.expectEqual(@as(u8, 'A'), card.ram[0]); try testing.expectEqual(@as(u8, 'B'), card.ram[511]);
// And page three is the last, which is the only way to reach it. _ = card.access(0xc, false, 0); card.windowWrite(0, 'C'); try testing.expectEqual(@as(u8, 'C'), card.ram[1536]); try testing.expectEqual(@as(u8, 'C'), card.windowRead(0));
// The same offset in a different page is a different byte. _ = card.access(0x0, false, 0); try testing.expectEqual(@as(u8, 'A'), card.windowRead(0));}
test "only the cursor and the light pen read back" { var card: Card = .{ .fitted = true };
// R1 is the number of columns and a program cannot ask what it is. _ = card.access(0x0, true, 1); _ = card.access(0x1, true, 80); try testing.expectEqual(@as(u8, 80), card.crtc.register[1]); try testing.expectEqual(@as(u8, 0), card.access(0x1, false, 0));
// R14 and R15 are the cursor, and they do. _ = card.access(0x0, true, 14); _ = card.access(0x1, true, 0x02); try testing.expectEqual(@as(u8, 0x02), card.access(0x1, false, 0)); _ = card.access(0x0, true, 15); _ = card.access(0x1, true, 0x30); try testing.expectEqual(@as(u8, 0x30), card.access(0x1, false, 0)); try testing.expectEqual(@as(u16, 0x0230), card.crtc.cursorAddress());}
test "the screen scrolls by moving where it starts, not by moving bytes" { var card = configured(); card.ram = @splat(' ');
// A character in the first cell of the screen. card.ram[0] = 'X'; try testing.expectEqual(@as(u8, 'X'), card.characterAt(0, 0));
// Scrolling is one addition. The firmware adds a row's worth to the start // address, and what was the second row is now the first. card.ram[columns] = 'Y'; card.crtc.register[12] = 0; card.crtc.register[13] = columns; try testing.expectEqual(@as(u8, 'Y'), card.characterAt(0, 0));
// The byte that was on screen is still in memory, untouched. Nothing was // copied anywhere, which is what makes this cheap on a 1 MHz machine. try testing.expectEqual(@as(u8, 'X'), card.ram[0]);}
test "the memory wraps at two kilobytes, which is what puts a scrolled row back at the bottom" { var card = configured(); card.ram = @splat(' ');
// Start near the end of the two kilobytes and the bottom of the screen // comes off the beginning of it. const start = ram_size - columns; card.crtc.register[12] = @intCast(start >> 8); card.crtc.register[13] = @truncate(start);
// The first row is the last eighty bytes of memory. try testing.expectEqual(@as(u11, @intCast(start)), card.addressOf(0, 0)); // And the second has wrapped to the beginning. try testing.expectEqual(@as(u11, 0), card.addressOf(1, 0)); // The last row of twenty-four, likewise, without ever leaving the memory. try testing.expectEqual(@as(u11, @intCast((start + 23 * columns) % ram_size)), card.addressOf(23, 0));}
test "a glyph is seven dots of nine rows out of a sixteen row cell" { // A character generator with one glyph in it: an 'A' shape in glyph $41, // seven rows of it and the rest of the sixteen blank, which is how the // real part is laid out. var set: [charset_size]u8 = @splat(0); const a = [_]u8{ 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42 }; @memcpy(set[0x41 * charset_rows ..][0..a.len], &a);
var card = configured(); card.ram = @splat(' '); card.ram[0] = 'A';
for (a, 0..) |want, line| try testing.expectEqual(want, card.dotsAt(&set, null, 0, 0, line));
// The cell is nine rows and the glyph is seven, so the last two are blank. try testing.expectEqual(@as(u8, 0), card.dotsAt(&set, null, 0, 0, 7)); try testing.expectEqual(@as(u8, 0), card.dotsAt(&set, null, 0, 0, 8)); // And there is no tenth: the 6845 is not asked for one. try testing.expectEqual(@as(u8, 0), card.dotsAt(&set, null, 0, 0, 9));}
test "the high bit asks for the other socket, and settles for the one part if there is one" { var one: [charset_size]u8 = @splat(0); var two: [charset_size]u8 = @splat(0); one[0x41 * charset_rows] = 0x18; two[0x41 * charset_rows] = 0x7e;
var card = configured(); card.ram = @splat(' '); card.ram[0] = 'A'; card.ram[1] = 'A' | 0x80;
// With both parts fitted the high bit chooses between them. try testing.expectEqual(@as(u8, 0x18), card.dotsAt(&one, &two, 0, 0, 0)); try testing.expectEqual(@as(u8, 0x7e), card.dotsAt(&one, &two, 0, 1, 0));
// With one part fitted, both come out of it. try testing.expectEqual(@as(u8, 0x18), card.dotsAt(&one, null, 0, 1, 0));}
test "a scan line is eighty cells of nine dots, and the glyph is seven of them" { var set: [charset_size]u8 = @splat(0); // A glyph that is a solid seven-dot bar on its first row. set[0x41 * charset_rows] = 0x7f;
var card = configured(); card.ram = @splat(' '); card.ram[0] = 'A';
var line: Line = .{}; scanLine(&card, &set, null, 0, false, &line); // Seven dots lit, and then the two that are the gap to the next character. for (0..glyph_dots) |x| try testing.expect(line.dots[x]); try testing.expect(!line.dots[7]); try testing.expect(!line.dots[8]); // The next cell is a space, so nothing. for (cell_dots..cell_dots * 2) |x| try testing.expect(!line.dots[x]);
// The second scan line of the same row is the glyph's second row, which is // blank, and a line past the twenty-fourth row is nothing at all. scanLine(&card, &set, null, 1, false, &line); for (0..glyph_dots) |x| try testing.expect(!line.dots[x]); scanLine(&card, &set, null, height, false, &line); for (line.dots) |dot| try testing.expect(!dot);}
test "the cursor inverts the cell rather than being a glyph of its own" { var set: [charset_size]u8 = @splat(0); set[0x41 * charset_rows] = 0x7f;
var card = configured(); card.ram = @splat(' '); card.ram[0] = 'A'; // The firmware's own cursor: the whole nine-line cell, blinking. card.crtc.register[10] = 0xe0; card.crtc.register[11] = 0x08;
var line: Line = .{}; // Not on this half of the blink, so the letter is the letter. scanLine(&card, &set, null, 0, false, &line); try testing.expect(line.dots[0]); try testing.expect(!line.dots[7]);
// On it, and every dot of the cell is the other way about — including the // two that are the gap, which is what makes it a block. scanLine(&card, &set, null, 0, true, &line); try testing.expect(!line.dots[0]); try testing.expect(line.dots[7]); try testing.expect(line.dots[8]); // And only that cell. try testing.expect(!line.dots[cell_dots]);}
test "a taller cell puts the rows somewhere else, and R9 is what says so" { var set: [charset_size]u8 = @splat(0); // A bar on the glyph's first row, so the top line of each row is lit. set[0x41 * charset_rows] = 0x7f;
var card = configured(); card.ram = @splat(' '); card.ram[0] = 'A'; // first row card.ram[columns] = 'A'; // second row
// As the Videoterm's own firmware sets it up, the second row starts nine // scan lines down. var line: Line = .{}; scanLine(&card, &set, null, cell_lines, false, &line); try testing.expect(line.dots[0]);
// A Videoterm compatible board's firmware asks for ten instead, and then // the same row is a line further down and nothing is where it was. This is // why the cell height cannot be a constant here: it is a register, and one // that real firmware really does write something else into. card.crtc.address = 9; card.crtc.write(9); try testing.expectEqual(@as(u8, 10), card.cellLines()); scanLine(&card, &set, null, cell_lines, false, &line); try testing.expect(!line.dots[0]); scanLine(&card, &set, null, 10, false, &line); try testing.expect(line.dots[0]);}
test "the cursor is where the 6845 says, and it says whether to draw it at all" { var card = configured();
// Row 1, column 5, which is address 85 with the screen starting at zero. card.crtc.register[14] = 0; card.crtc.register[15] = columns + 5; // The firmware's own setting: blinking slowly, filling the whole cell. card.crtc.register[10] = 0xe0; card.crtc.register[11] = 0x08;
try testing.expect(card.cursorIn(0, 0) == null); const here = card.cursorIn(1, 5) orelse return error.TestUnexpectedResult; try testing.expectEqual(@as(u5, 0), here.first); try testing.expectEqual(@as(u5, 8), here.last); try testing.expectEqual(Crtc.Blink.slow, card.crtc.blink());
// Bits 6 and 5 of R10 as 01 is the cursor turned off, and then it is // nowhere rather than somewhere invisible. card.crtc.register[10] = 0x20; try testing.expectEqual(Crtc.Blink.hidden, card.crtc.blink()); try testing.expect(card.cursorIn(1, 5) == null);
// The cursor moves with the screen, because it is an address in the same // memory and the same wrap applies. card.crtc.register[10] = 0xe0; card.crtc.register[13] = columns; try testing.expect(card.cursorIn(1, 5) == null); try testing.expect(card.cursorIn(0, 5) != null);}