// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The Universal Parallel Interface Card, which is what an Apple III printed //! through when it did not print through the serial port on its back. //! //! Apple's product A3L0009, and the second card this machine has anywhere to //! put — the first being the ProFile's interface. It is a parallel port with //! rather more to it than the Apple II's parallel card: that one is a latch and //! a strobe and nothing else, and this one has two output ports, an input port, //! a control register that sets the strobe's width and the polarity of three //! separate signals, two status flip-flops, and an interrupt on either of them. //! //! Eight addresses, from `$c080 + s0` where `s` is the slot, which is the same //! convention an Apple II uses (Apple Computer, Inc., 1981, "Summary of //! Hardware Addresses", p. 42, and the address-decode table on p. 35): //! //! ```text //! $c080 + s0 PORTA the output latch, IC 6C, on connector lines 20-27 //! $c081 + s0 PORTB the other output latch, IC 6A, on lines 1-8 //! $c082 + s0 STROBE a write sends the strobe //! $c083 + s0 READB a read takes the input port, IC 6B //! $c084 + s0 STATUS a read gives three signals //! $c085 + s0 DATARDY sets the data ready output //! $c086 + s0 CTRLREG the control register, IC 5B //! $c087 + s0 CLEAR clears the handshake and turns autostrobe off //! ``` //! //! ## What the far end is //! //! A file, and a file is never busy. So the acknowledgement a printer would //! send back comes back at once: a strobe puts a byte out and the card has its //! answer before the next instruction. That is the same choice `parallel.zig` //! makes for the Apple II's card and for the same reason — what is on the other //! end of the cable is whatever the caller attached, and a file does not run //! out of paper. A driver that waits for `ACK` therefore never waits, which is //! the fast case of real behaviour rather than a special case beside it. //! //! ## What is not here //! //! The card's own ROM at `$Cs00`. Under SOS nothing reads it: a driver is a //! file on the boot disk, loaded into memory by the operating system, and the //! ROM is there for Apple II emulation mode, where the card is driven the way //! an Apple II card is. What *is* modelled about that space is its side effect, //! because it has one: touching it turns autostrobe on. const std = @import("std"); /// The eight addresses, as the manual's decode table names them. pub const Register = enum(u3) { port_a = 0, port_b = 1, strobe = 2, read_b = 3, status = 4, data_ready = 5, control = 6, clear = 7, }; /// The bits of the control register (Apple Computer, Inc., 1981, p. 36). /// /// The bottom three set the strobe's width in microseconds, which nothing here /// measures: this card's timing is the file's, and a strobe that is two /// microseconds or fourteen puts the same byte in the same place. They are /// kept because they are written, and because a driver reads its own copy back /// out of them. pub const control_strobe_minus_2: u8 = 0x01; pub const control_strobe_plus_4: u8 = 0x02; pub const control_strobe_plus_8: u8 = 0x04; /// "Set ACKNOWLEDGE and DATA READY ACKNOWLEDGE INPUT polarities; 1 = positive". pub const control_ack_positive: u8 = 0x08; /// "Set DATA READY OUTPUT polarity; 1 = positive". pub const control_data_ready_positive: u8 = 0x10; /// "Set STROBE polarity, 1 = positive". pub const control_strobe_positive: u8 = 0x20; /// "1 = enable DATA READY ACKNOWLEDGE Interrupt Request". pub const control_data_ready_interrupt: u8 = 0x40; /// "1 = enable ACKNOWLEDGE Interrupt Request". pub const control_ack_interrupt: u8 = 0x80; /// The bits a read of STATUS gives back. pub const status_ack: u8 = 0x80; pub const status_data_ready: u8 = 0x40; pub const status_ack_signal: u8 = 0x01; pub const Card = struct { /// Output Port A's latch, IC 6C: "this data remains on the 40 pin /// connector lines 20-27 until PORTA is written to again". These are the /// lines a printer's data arrives on. port_a: u8 = 0, /// Output Port B's latch, IC 6A, on lines 1-8. Nothing a printer uses. port_b: u8 = 0, /// What the peripheral is driving on the input port, IC 6B. Nothing here /// drives it, so it is what a caller put there. input: u8 = 0, /// The control register, IC 5B, a 74LS273. /// /// **Write only.** "The current status of the control register must always /// be maintained in software (CTRLWRD) since there is no way to read the /// contents of CTRLREG" — so a read of `$c086 + s0` is not this, and the /// caller is told to float the bus there instead. control: u8 = 0, /// The ACKNOWLEDGE status, which a read of STATUS reports in bit 7: /// "turned on by ACKNOWLEDGE INPUT, turned off by STROBE signal or reset". acknowledged: bool = false, /// The DATA READY status, bit 6. data_ready: bool = false, /// Autostrobe, IC 1B, a 74LS74 flip-flop: with it on, a write to PORTA /// sends a strobe as well as latching. "Autostrobe is turned on when /// address space Csxx is accessed. It can be turned off by a read from or /// a write to CLEAR". It is "primarily intended to provide automatic /// strobes for Apple II Emulation mode", so under SOS it stays off and the /// driver strobes for itself. autostrobe: bool = false, /// Where a strobed byte goes: whatever the caller attached, which on the /// command line is a file. Null is a card with nothing plugged into it, /// which still latches and still strobes and still acknowledges. out: ?*std.Io.Writer = null, /// 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 so that /// a failing file is reported once rather than at every byte. trouble: ?anyerror = null, /// Whether the card is pulling the interrupt line: either status whose /// interrupt the control register has enabled. /// /// "If the interrupts are enabled (by setting bits 7 and 6 of the control /// register), then ACK status (DL7) and/or DATA READY status (DL6) will /// result in an IRQ signal." pub fn interrupting(self: *const Card) bool { if (self.acknowledged and self.control & control_ack_interrupt != 0) return true; if (self.data_ready and self.control & control_data_ready_interrupt != 0) return true; return false; } /// One of the sixteen device select addresses, of which this card decodes /// the low eight. Null where the card drives nothing and the bus floats. pub fn access(self: *Card, offset: u4, is_write: bool, value: u8) ?u8 { // Only eight addresses are decoded; A3 is not in the decode table, so // the top half of the slot's sixteen is nothing at all. if (offset >= 8) return null; switch (@as(Register, @enumFromInt(@as(u3, @truncate(offset))))) { .port_a => { if (!is_write) return null; self.port_a = value; if (self.autostrobe) self.sendStrobe(); return 0; }, // "A read or a write to PORTB will latch whatever is on the data // lines... For a read operation, the data on the lines is // undefined" -- so a read latches whatever the bus happened to // hold, and gives back nothing this can name. .port_b => { if (is_write) self.port_b = value; return if (is_write) 0 else null; }, .strobe => { if (is_write) self.sendStrobe(); return if (is_write) 0 else null; }, .read_b => return if (is_write) 0 else self.input, .status => { if (is_write) return 0; var byte: u8 = 0; if (self.acknowledged) byte |= status_ack; if (self.data_ready) byte |= status_data_ready; // "Mirrors the ACKNOWLEDGE INPUT, but always is positive true // logic (uses polarity)" -- the same line as bit 7, put the // right way up whatever the control register's polarity bit // says, which is what makes it worth having twice. if (self.acknowledged) byte |= status_ack_signal; return byte; }, // "A write or read to DATARDY sets the Data Ready flip-flop", and // the decode table calls this address "Set DATA READY OUTPUT". // // The manual also says, in the same entry, that DL6 "is turned // off" here, which contradicts both of those and its own account // of DL6 under STATUS -- "turned on by a read or write to // DATARDY". Two statements against one, and the decode table is a // table of what the hardware does, so this sets it and the // disagreement is written down rather than quietly resolved. .data_ready => { self.data_ready = true; return 0; }, .control => { if (!is_write) return null; // there is no way to read it self.control = value; return 0; }, // "A read or write to CLEAR clears the handshaking flip-flop", // which the decode table spells out as "Clear DATA READY OUTPUT // and ACKNOWLEDGE INPUT", "and also disables the autostrobe // feature". .clear => { self.data_ready = false; self.acknowledged = false; self.autostrobe = false; return 0; }, } } /// Touching the card's ROM space, `$Cs00` to `$CsFF`, which is not a read /// of anything here but does turn autostrobe on. pub fn touchFirmware(self: *Card) void { self.autostrobe = true; } /// The strobe: a byte goes out, and the acknowledgement comes back. /// /// "When the STROBE signal is generated, it causes the ACKNOWLEDGE status /// to be cleared", and then the peripheral's ACKNOWLEDGE INPUT sets it /// again. Both happen here, because the far end is a file: it has taken /// the byte by the time the processor could ask. fn sendStrobe(self: *Card) void { self.acknowledged = false; if (self.out) |writer| { writer.writeByte(self.port_a) catch |err| { if (self.trouble == null) self.trouble = err; }; } self.sent +|= 1; self.acknowledged = true; } /// "The contents of the latch are set to zero whenever the IORES line goes /// low. This occurs when the power is turned on, or when a is /// done." pub fn reset(self: *Card) void { self.control = 0; self.acknowledged = false; self.data_ready = false; self.autostrobe = false; } }; const testing = std.testing; test "a byte written to Port A and strobed is a byte the printer has" { var buffer: [16]u8 = undefined; var writer = std.Io.Writer.fixed(&buffer); var card: Card = .{ .out = &writer }; // "A write to PORTA will latch whatever is on the data lines into IC 6C", // and nothing goes out until the strobe: a latch is not a printer. _ = card.access(@intFromEnum(Register.port_a), true, 'H'); try testing.expectEqual(@as(u64, 0), card.sent); _ = card.access(@intFromEnum(Register.strobe), true, 0); try testing.expectEqual(@as(u64, 1), card.sent); _ = card.access(@intFromEnum(Register.port_a), true, 'i'); _ = card.access(@intFromEnum(Register.strobe), true, 0); try testing.expectEqualStrings("Hi", writer.buffered()); // The latch keeps what was put in it, so a second strobe with no new byte // sends the same one again. _ = card.access(@intFromEnum(Register.strobe), true, 0); try testing.expectEqualStrings("Hii", writer.buffered()); } test "the strobe clears the acknowledgement and the printer gives it back" { var card: Card = .{}; // "Turned off by STROBE signal or reset", then turned on by the // ACKNOWLEDGE INPUT -- which for a file is at once, because a file is // never busy. So a driver that waits for ACK never waits. _ = card.access(@intFromEnum(Register.port_a), true, 0x41); _ = card.access(@intFromEnum(Register.strobe), true, 0); const status = card.access(@intFromEnum(Register.status), false, 0).?; try testing.expectEqual(status_ack, status & status_ack); // Bit 0 is the same line the right way up whatever the polarity says, // which is what makes it worth reporting twice. try testing.expectEqual(status_ack_signal, status & status_ack_signal); } test "the control register can be written and not read" { var card: Card = .{}; // "There is no way to read the contents of CTRLREG", so a read of it is // not the register: nothing drives the bus and the caller floats it. _ = card.access(@intFromEnum(Register.control), true, control_ack_interrupt | control_strobe_positive); try testing.expectEqual(control_ack_interrupt | control_strobe_positive, card.control); try testing.expect(card.access(@intFromEnum(Register.control), false, 0) == null); } test "an enabled status pulls the interrupt line and a disabled one does not" { var card: Card = .{}; // "If the interrupts are enabled (by setting bits 7 and 6 of the control // register), then ACK status (DL7) and/or DATA READY status (DL6) will // result in an IRQ signal." _ = card.access(@intFromEnum(Register.port_a), true, 0); _ = card.access(@intFromEnum(Register.strobe), true, 0); try testing.expect(card.acknowledged); try testing.expect(!card.interrupting()); // nothing enabled yet _ = card.access(@intFromEnum(Register.control), true, control_ack_interrupt); try testing.expect(card.interrupting()); // And clearing the handshake takes it away again. _ = card.access(@intFromEnum(Register.clear), false, 0); try testing.expect(!card.interrupting()); // The other status, on its own bit. _ = card.access(@intFromEnum(Register.control), true, control_data_ready_interrupt); _ = card.access(@intFromEnum(Register.data_ready), true, 0); try testing.expect(card.data_ready); try testing.expect(card.interrupting()); } test "autostrobe comes on with the card's ROM space and off with CLEAR" { var buffer: [8]u8 = undefined; var writer = std.Io.Writer.fixed(&buffer); var card: Card = .{ .out = &writer }; // Off to begin with, so a write to PORTA only latches. _ = card.access(@intFromEnum(Register.port_a), true, 'a'); try testing.expectEqual(@as(u64, 0), card.sent); // "Autostrobe is turned on when address space Csxx is accessed." card.touchFirmware(); _ = card.access(@intFromEnum(Register.port_a), true, 'b'); try testing.expectEqualStrings("b", writer.buffered()); // "It can be turned off by a read from or a write to CLEAR." _ = card.access(@intFromEnum(Register.clear), true, 0); _ = card.access(@intFromEnum(Register.port_a), true, 'c'); try testing.expectEqualStrings("b", writer.buffered()); } test "only eight of the sixteen addresses are decoded" { var card: Card = .{}; // A3 is not in the manual's decode table, so the top half of the slot's // sixteen is nothing at all and the bus floats there. var offset: u4 = 8; while (true) : (offset += 1) { try testing.expect(card.access(offset, false, 0) == null); if (offset == 15) break; } } test "a card with nothing plugged into it still works the handshake" { // No writer: the card latches, strobes and acknowledges exactly as it // would with a printer on the end, because none of that is the printer's // doing. Only the byte has nowhere to go. var card: Card = .{}; _ = card.access(@intFromEnum(Register.port_a), true, 0x7f); _ = card.access(@intFromEnum(Register.strobe), true, 0); try testing.expectEqual(@as(u64, 1), card.sent); try testing.expect(card.acknowledged); try testing.expect(card.trouble == null); }