// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! A parallel printer card, of the kind that went in slot 1. //! //! This is the simplest card the Apple II had. There is no chip on it worth //! the name: eight data lines, a latch to hold what was last put on them, and //! a strobe that goes out when the latch is written. The Apple II Parallel //! Printer Interface Card's manual describes the whole of the hardware in one //! sentence — "if data is stored at location `$C080 + $N0`, where N is the //! slot number, then the data will appear on Printer Board Data Lines DP0-DP7, //! and will remain until the next STORE instruction to that location is //! executed" — and gives the way to drive it from BASIC, which is //! `POKE -16256 + N*16, DATA`. //! //! So a byte written to `$c090` for slot 1 is a byte the printer has. Nothing //! waits, because nothing here is a printer that could be busy: what is on the //! other end of the cable is whatever the caller attached, and a file does not //! run out of paper. //! //! ## The firmware //! //! The card also carried 256 bytes of ROM, which is what `PR#1` reaches: the //! Monitor points its output vector at `$Cn00` and every character the machine //! prints arrives there. Woz wrote the original and fitted a good deal into it //! — line widths, tabs, an escape character for talking to the card itself. //! What is here is ours rather than his, on the same footing as the clock card //! and the block device: what the card has to do is published, so nothing has //! to be supplied for it to work. //! //! It does the two things the manual says the card does. It strips the high //! bit, because the Apple II sets it on every character it prints and a //! printer wants seven-bit ASCII — the real card had a jumper for this and //! this is the setting anybody printing text used. And it adds a line feed //! after a carriage return, because "the Apple II adds a Carriage Return to //! the end of every line, and the Parallel Printer Card adds a Line Feed //! character to the end of each line". //! //! Neither happens to a byte poked straight at the latch. The hardware is //! eight bits wide and passes what it is given, which is what a program //! dumping the graphics screen depends on; the stripping is the firmware's //! doing, as it was on the card. const std = @import("std"); /// A carriage return and the line feed the card puts after it, as the machine /// sends them: the high bit is still on until the firmware takes it off. pub const carriage_return: u8 = 0x0d; pub const line_feed: u8 = 0x0a; /// The card. /// /// Where the bytes go is a `std.Io.Writer` the caller attaches, and the caller /// owns it: this neither opens nor closes anything, and a card with nothing /// attached is a card whose printer is switched off. That is not an error — /// the machine cannot tell, and neither could a real one. pub const Card = struct { /// Whether there is a card in the slot at all. fitted: bool = false, /// The printer on the end of the cable, if anything is. /// /// Buffering, flushing and closing belong to whoever attached it. Nothing /// here flushes, because a printer card that flushed after every character /// would be a card that wrote a system call per character. out: ?*std.Io.Writer = null, /// What was last put on the data lines, which is what stays there until /// something writes again — including when the address is read, since the /// latch is what is on the bus. latch: u8 = 0, /// How many bytes have gone out, which is the only thing a front end needs /// in order to say the printer is doing something. sent: u64 = 0, /// The first thing that went wrong writing, if anything did. /// /// Kept rather than returned because this is reached from a store /// instruction, which has nowhere to put an error and no business stopping /// the machine over one. Whoever attached the writer looks at this. trouble: ?anyerror = null, /// A byte on the data lines, and the strobe that goes with it. pub fn send(self: *Card, byte: u8) void { self.latch = byte; self.sent +|= 1; const out = self.out orelse return; out.writeAll(&.{byte}) catch |err| { if (self.trouble == null) self.trouble = err; }; } /// What reading the card's address gives, which is whatever the latch is /// still holding. There is no status to read: a real card's acknowledge /// line is the printer saying it has taken the byte, and this one always /// has. pub fn read(self: *const Card) u8 { return self.latch; } }; /// The card's 256 bytes of ROM, for a card in `slot`. /// /// `PR#n` points the machine's output vector at `$Cn00`, so every character /// printed arrives here with the character in the accumulator. It has to come /// back the same way: the Monitor's own output routine leaves the character in /// the accumulator and a good deal of software counts on that, and X and Y /// belong to whoever was printing. pub fn firmware(comptime slot: u4) [256]u8 { const data = 0xc080 + @as(u16, slot) * 0x10; const lo: u8 = @truncate(data); const hi: u8 = @truncate(data >> 8); var rom: [256]u8 = @splat(0); const code = [_]u8{ 0x48, // $Cn00 PHA keep the character to hand back 0x29, 0x7f, // AND #$7f the Apple sets bit 7; a printer 0x8d, lo, hi, // STA data wants it off 0xc9, 0x0d, // CMP #$0d was that the end of a line? 0xd0, 0x05, // BNE done 0xa9, 0x0a, // LDA #$0a then the card adds the line 0x8d, lo, hi, // STA data feed the Apple did not send 0x68, // done: PLA the character, as it arrived 0x60, // RTS }; @memcpy(rom[0..code.len], &code); return rom; } /// One page for each slot, since a card's ROM has its own slot's addresses /// assembled into it and which slot that is cannot be known until a machine is /// built. Indexed by the slot number, so there are eight of them and the first /// is never read: slot 0 is not a slot, and two hundred and fifty-six wasted /// bytes are cheaper than an off-by-one waiting to happen. pub const firmwares: [8][256]u8 = blk: { var all: [8][256]u8 = undefined; for (0..all.len) |slot| all[slot] = firmware(@intCast(slot)); break :blk all; }; const testing = std.testing; test "a byte written to the latch is a byte the printer has" { var buffer: [64]u8 = undefined; var writer: std.Io.Writer = .fixed(&buffer); var card: Card = .{ .fitted = true, .out = &writer }; card.send('H'); card.send('i'); try testing.expectEqualStrings("Hi", writer.buffered()); try testing.expectEqual(@as(u64, 2), card.sent); // And the latch holds the last one, which is what reading the address // gives back. try testing.expectEqual(@as(u8, 'i'), card.read()); } test "a card with nothing attached is a printer switched off" { // Not an error: the machine cannot tell, and neither could a real one. var card: Card = .{ .fitted = true }; card.send('x'); try testing.expectEqual(@as(u64, 1), card.sent); try testing.expectEqual(@as(u8, 'x'), card.read()); try testing.expectEqual(@as(?anyerror, null), card.trouble); } test "a writer that fills up says so once and goes on being written to" { // The card is reached from a store instruction, which has nowhere to put // an error, so the first one is kept and the machine carries on. var buffer: [2]u8 = undefined; var writer: std.Io.Writer = .fixed(&buffer); var card: Card = .{ .fitted = true, .out = &writer }; card.send('a'); card.send('b'); try testing.expectEqual(@as(?anyerror, null), card.trouble); card.send('c'); try testing.expect(card.trouble != null); // Still counting, still latching: the machine is not told. card.send('d'); try testing.expectEqual(@as(u64, 4), card.sent); try testing.expectEqual(@as(u8, 'd'), card.read()); } test "the firmware sends the character and hands it back" { // Which is the whole of what a card in the output vector has to do: the // Monitor's own output routine leaves the character in the accumulator, // and software counts on that. const rom = firmware(1); try testing.expectEqual(@as(u8, 0x48), rom[0]); // PHA try testing.expectEqual(@as(u8, 0x68), rom[15]); // PLA try testing.expectEqual(@as(u8, 0x60), rom[16]); // RTS // The address it stores to is $c090 for slot 1, which is the $C080 + N*16 // the manual gives. try testing.expectEqual(@as(u8, 0x8d), rom[3]); try testing.expectEqual(@as(u8, 0x90), rom[4]); try testing.expectEqual(@as(u8, 0xc0), rom[5]); // And slot 2 would be $c0a0, so the firmware is built for the slot it is // in rather than assuming one. const other = firmware(2); try testing.expectEqual(@as(u8, 0xa0), other[4]); } test "the branch over the line feed lands on the way out" { // Worth checking by arithmetic rather than by eye: a branch that is one // out sends a stray byte to the printer on every line. const rom = firmware(1); try testing.expectEqual(@as(u8, 0xd0), rom[8]); // BNE const from: usize = 10; // the instruction after the branch const target = from + rom[9]; try testing.expectEqual(@as(usize, 15), target); // the PLA } test "there is a page of firmware for every slot, and each is that slot's" { // See the same test in `blockdevice.zig`. The test above already shows // that slot 1 stores to $c090 and slot 2 to $c0a0; this says that the // table a machine actually reads is built from the same generator. inline for (0..firmwares.len) |slot| { try testing.expectEqualSlices(u8, &firmware(slot), &firmwares[slot]); } try testing.expectEqual(@as(u8, 0x90), firmwares[1][4]); try testing.expectEqual(@as(u8, 0xa0), firmwares[2][4]); }