// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! Super Hi-Res: the Apple IIgs's own graphics, and nothing like the old ones. //! //! `video.zig` draws what the Mega II draws, which is what an Apple II drew: //! a stream of dots at four times the colour subcarrier, coloured by what //! four of them at a time do to a television. None of that is true here. Super //! Hi-Res is the Video Graphics Controller's, it has actual pixels with actual //! colour numbers in them, and the numbers mean whatever a palette in memory //! says they mean. It is a different chip drawing a different picture on a //! different grid, which is why it is a different file. //! //! The buffer is thirty-two kilobytes of the auxiliary half of the slow RAM — //! bank `$e1`, `$2000` to `$9fff` — and it holds three things: //! //! | Where | What | //! | ---------------- | ------------------------------------------------- | //! | `$2000`-`$9cff` | 200 lines of 160 bytes | //! | `$9d00`-`$9dc7` | one scan-line control byte per line | //! | `$9e00`-`$9fff` | sixteen palettes of sixteen twelve-bit colours | //! //! ## A palette per line //! //! The control byte is the interesting part, and it is why this is drawn a //! scan line at a time rather than a frame at a time. Every line chooses, //! independently: which of the sixteen palettes it uses, whether it is 320 //! pixels or 640, whether Color Fill is on, and whether the beam reaching it //! raises an interrupt. So a picture can have far more than sixteen colours in //! it, and a program can change the palette *underneath the beam* and have //! more again. //! //! ## 320, and Color Fill //! //! Two pixels to a byte, four bits each, and the four bits are a colour number //! straight into the line's palette. With Color Fill on, colour `$0` stops //! being a colour and means "the same as the pixel to my left" — so a line of //! `A 0 0 0 0 0 B 0 0 0 0 0 C` is three runs of solid colour and cost three //! bytes to write rather than eighteen. Fifteen colours a line rather than //! sixteen, and the manual is careful to say that the first pixel of a line //! must not be `$0`, because there is nothing to its left and "an undetermined //! color results". //! //! ## 640, and the four groups //! //! Four pixels to a byte, two bits each — which would be four colours, except //! that it is not. The sixteen colours of the palette are divided into four //! groups of four, and *which group a pixel uses depends on where it is in the //! byte*: the first pixel takes the third group, the second the fourth, the //! third the first, and the fourth the second. So sixteen colours are still //! available along a line even though any one pixel can only be four of them, //! and 640 mode gets its colour by putting the right value in the right column //! (Apple Computer, Inc., 1989a, Table 4-21). //! //! That rotation looks arbitrary until you notice what it is for: two adjacent //! pixels on a 640 screen are half a dot each, and half a dot is what makes //! colour on a composite monitor. The groups are arranged so that the pairs //! that dither well sit next to each other. //! //! ## What is not here //! //! The border, which is drawn but not here: it is a colour round the *outside* //! rather than part of the picture, it shares an address with the clock, and a //! front end paints it round whatever this draws. And the fifty-sixth //! scan line's worth of bytes past the control bytes, which the manual //! reserves and says not to touch. const std = @import("std"); const video = @import("video.zig"); /// The widest a line can be. A 320-pixel line is drawn at half the dot clock, /// so it covers the same width of screen with each pixel twice as wide, and /// that is what `scanLine` fills in: 640 either way, so that everything /// downstream is one shape. pub const width = 640; /// And two hundred lines rather than the old video's hundred and ninety-two. pub const height = 200; /// Where the pixels are, in bank `$e1`, and how much of it each line takes. pub const buffer = 0x2000; pub const bytes_per_line = 160; /// Where the scan-line control bytes are: one per line, at `$9d00` plus the /// line number. pub const control_bytes = 0x9d00; /// And the palettes: sixteen of them, sixteen colours each, two bytes a /// colour. pub const palette_table = 0x9e00; pub const palettes = 16; pub const colours = 16; /// What one line's control byte says about it. pub const Control = packed struct(u8) { /// Which of the sixteen palettes the line uses. palette: u4, /// Reserved: the manual says to write zero. reserved: u1, /// Colour `$0` means "the same as the pixel to my left". 320 only; in 640 /// mode this bit does nothing. fill: bool, /// Raise the scan-line interrupt when the beam reaches this line. interrupt: bool, /// 640 pixels rather than 320. wide: bool, pub fn of(byte: u8) Control { return @bitCast(byte); } }; /// One line of the display. pub const Line = struct { /// Which colour each of the 640 dot positions is, as a number from 0 to /// 255: the palette in the high nibble and the colour in the low, which is /// exactly where it came from and is a number a whole frame can share. entries: [width]u8 = @splat(0), /// How many pixels the line really has, which is 320 or 640. The entries /// above are always 640 — a 320-pixel line has each of its pixels written /// twice, because that is what the dot clock does to it. pixels: u16 = 320, /// What its control byte said. control: Control = .of(0), }; /// Draws one line. /// /// The machine has to answer `videoByte(auxiliary, address)`, which is the /// same thing the old video asks of it; everything here is in the auxiliary /// half, which is bank `$e1`. pub fn scanLine(machine: anytype, y: u16, into: *Line) void { const control: Control = .of(machine.videoByte(true, control_bytes + y)); into.control = control; into.pixels = if (control.wide) 640 else 320; const line = buffer + @as(u16, y) * bytes_per_line; const high: u8 = @as(u8, control.palette) << 4; if (control.wide) { for (0..bytes_per_line) |at| { const byte = machine.videoByte(true, line + @as(u16, @intCast(at))); for (0..4) |pixel| { // Which four of the sixteen a pixel may be depends on where it // sits in the byte: third group, fourth, first, second. const group: u8 = @intCast((pixel + 2) % 4 * 4); const value: u8 = byte >> @intCast(6 - pixel * 2) & 0x03; into.entries[at * 4 + pixel] = high | group | value; } } return; } var last: u8 = 0; for (0..bytes_per_line) |at| { const byte = machine.videoByte(true, line + @as(u16, @intCast(at))); for (0..2) |pixel| { var value: u8 = if (pixel == 0) byte >> 4 else byte & 0x0f; // Colour zero with Color Fill on is not a colour: it is the pixel // to the left over again. At the start of a line there is nothing // to the left, and the manual says so rather than saying what // happens; what happens here is colour zero. if (control.fill and value == 0) value = last else last = value; const entry = high | value; into.entries[(at * 2 + pixel) * 2] = entry; into.entries[(at * 2 + pixel) * 2 + 1] = entry; } } } /// Reads all sixteen palettes: 256 colours, indexed the way `Line.entries` /// indexes them. /// /// A colour is twelve bits in two, four bits to a channel, and it is stored /// low byte first with green and blue in it and red in the byte above. Each /// nibble is repeated to widen it, so `$f` becomes `$ff` and `$0` stays `$0`. pub fn palette(machine: anytype, into: *[palettes * colours]video.Rgb) void { for (0..palettes) |which| { for (0..colours) |colour| { const at: u16 = @intCast(palette_table + which * colours * 2 + colour * 2); const low = machine.videoByte(true, at); const top = machine.videoByte(true, at + 1); into[which * colours + colour] = .{ .r = (top & 0x0f) * 0x11, .g = (low >> 4) * 0x11, .b = (low & 0x0f) * 0x11, }; } } } /// Whether the beam reaching this line should raise the scan-line interrupt. pub fn interruptsOn(machine: anytype, y: u16) bool { if (y >= height) return false; return Control.of(machine.videoByte(true, control_bytes + y)).interrupt; } const testing = std.testing; /// Thirty-two kilobytes of bank $e1 and nothing else, for the tests below. const Bare = struct { ram: [0x10000]u8 = @splat(0), pub fn videoByte(self: *const Bare, auxiliary: bool, address: u16) u8 { std.debug.assert(auxiliary); return self.ram[address]; } fn setColour(self: *Bare, which: u4, colour: u4, rgb: u12) void { const at = palette_table + @as(usize, which) * 32 + @as(usize, colour) * 2; self.ram[at] = @truncate(rgb); self.ram[at + 1] = @truncate(rgb >> 8); } }; test "a control byte is four things and a palette number" { const control: Control = .of(0b1110_0101); try testing.expect(control.wide); try testing.expect(control.interrupt); try testing.expect(control.fill); try testing.expectEqual(@as(u1, 0), control.reserved); try testing.expectEqual(@as(u4, 5), control.palette); // And nothing at all, which is what an unwritten buffer holds: 320 // pixels, palette zero, no fill and no interrupt. const empty: Control = .of(0); try testing.expect(!empty.wide); try testing.expect(!empty.interrupt); try testing.expect(!empty.fill); try testing.expectEqual(@as(u4, 0), empty.palette); } test "320 mode is two pixels a byte, each of them twice as wide" { var machine: Bare = .{}; var line: Line = .{}; machine.ram[control_bytes] = 0x03; // 320, palette 3 machine.ram[buffer] = 0x59; scanLine(&machine, 0, &line); try testing.expectEqual(@as(u16, 320), line.pixels); try testing.expectEqual(@as(u4, 3), line.control.palette); // The high nibble is the left pixel, and each covers two of the 640. try testing.expectEqual(@as(u8, 0x35), line.entries[0]); try testing.expectEqual(@as(u8, 0x35), line.entries[1]); try testing.expectEqual(@as(u8, 0x39), line.entries[2]); try testing.expectEqual(@as(u8, 0x39), line.entries[3]); } test "Color Fill makes colour zero mean the pixel to the left" { var machine: Bare = .{}; var line: Line = .{}; machine.ram[control_bytes] = 0x20; // 320, fill on, palette 0 // A 0 0 0 B 0 0 0, which the manual gives as the whole point of the mode. machine.ram[buffer + 0] = 0xa0; machine.ram[buffer + 1] = 0x00; machine.ram[buffer + 2] = 0xb0; machine.ram[buffer + 3] = 0x00; scanLine(&machine, 0, &line); for (0..8) |x| try testing.expectEqual(@as(u8, 0x0a), line.entries[x]); for (8..16) |x| try testing.expectEqual(@as(u8, 0x0b), line.entries[x]); // With fill off the same bytes are a colour and then colour zero. machine.ram[control_bytes] = 0x00; scanLine(&machine, 0, &line); try testing.expectEqual(@as(u8, 0x0a), line.entries[0]); try testing.expectEqual(@as(u8, 0x00), line.entries[2]); } test "640 mode takes its four groups in the order the manual gives" { // "The first pixel in each horizontal line can select one of 4 colors from // the third group of 4 in the palette. The second pixel selects from the // fourth group. The third pixel selects from the first group, and the // fourth pixel selects from the second group." var machine: Bare = .{}; var line: Line = .{}; machine.ram[control_bytes] = 0x80; // 640, palette 0 machine.ram[buffer] = 0b11_11_11_11; // value 3 in all four scanLine(&machine, 0, &line); try testing.expectEqual(@as(u16, 640), line.pixels); try testing.expectEqual(@as(u8, 0x0b), line.entries[0]); // third group: $8-$b try testing.expectEqual(@as(u8, 0x0f), line.entries[1]); // fourth: $c-$f try testing.expectEqual(@as(u8, 0x03), line.entries[2]); // first: $0-$3 try testing.expectEqual(@as(u8, 0x07), line.entries[3]); // second: $4-$7 // And the two bits are read from the top of the byte down. machine.ram[buffer] = 0b01_00_00_00; scanLine(&machine, 0, &line); try testing.expectEqual(@as(u8, 0x09), line.entries[0]); try testing.expectEqual(@as(u8, 0x0c), line.entries[1]); } test "a colour is twelve bits, low byte first, with red on its own" { var machine: Bare = .{}; machine.setColour(0, 1, 0xf00); // red machine.setColour(0, 2, 0x0f0); // green machine.setColour(0, 3, 0x00f); // blue machine.setColour(15, 15, 0xfff); var table: [palettes * colours]video.Rgb = undefined; palette(&machine, &table); try testing.expectEqual(video.Rgb{ .r = 0xff, .g = 0, .b = 0 }, table[1]); try testing.expectEqual(video.Rgb{ .r = 0, .g = 0xff, .b = 0 }, table[2]); try testing.expectEqual(video.Rgb{ .r = 0, .g = 0, .b = 0xff }, table[3]); try testing.expectEqual(video.Rgb{ .r = 0xff, .g = 0xff, .b = 0xff }, table[255]); try testing.expectEqual(video.Rgb{ .r = 0, .g = 0, .b = 0 }, table[0]); } test "every line chooses for itself" { var machine: Bare = .{}; var line: Line = .{}; for (0..height) |y| { // Alternating 320 and 640, and a different palette every line. machine.ram[control_bytes + y] = @as(u8, if (y % 2 == 0) 0 else 0x80) | @as(u8, @truncate(y % 16)); } for (0..height) |y| { scanLine(&machine, @intCast(y), &line); try testing.expectEqual(@as(u16, if (y % 2 == 0) 320 else 640), line.pixels); try testing.expectEqual(@as(u4, @truncate(y % 16)), line.control.palette); } }