// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The 6551 ACIA, which is what a Super Serial Card mostly is. //! //! Four registers and a shift register each way. A program writes a byte to //! the data register and the chip clocks it out a bit at a time at whatever //! rate the control register asks for, saying it is busy meanwhile; a byte //! arriving fills the receive register and says so until it is read. Almost //! everything written for these cards polls the status register for those two //! bits, which is why they are the part worth getting right. //! //! The time a byte takes is not a detail. Software that drives a serial port //! by counting, and there is a good deal of it, is written against the rate it //! asked for, so a card that hands over bytes the instant they are asked for //! is a card that runs those programs at the wrong speed. //! //! See the Super Serial Card user's manual for the card and its firmware (Apple //! Computer, Inc., 1985a), and the 6551 data sheet for the register layout //! (Synertek, n.d.). const std = @import("std"); /// How many bytes wait to go out before anything is dropped. The machine /// writes one at a time and something outside empties this whenever it looks, /// so it only has to cover the gap between looks. pub const capacity = 256; /// Which register one of the card's four addresses is. pub const Register = enum(u2) { /// Read what came in, or write what is to go out. data = 0, /// Read how things stand, or write anything at all to reset the chip. status = 1, /// What the chip is doing: the data terminal ready line, the interrupts, /// and the parity. command = 2, /// How it is doing it: the rate, the word length and the stop bits. control = 3, }; pub const Acia = struct { /// The rate and the shape of a word. control: u8 = 0, /// The terminal ready line, the interrupt enables and the parity. command: u8 = 0, /// The byte that came in and has not been read yet. received: ?u8 = null, /// A byte arrived before the last one was read, and one of them is gone. overrun: bool = false, /// Bytes on their way out, oldest first. sending: [capacity]u8 = undefined, sending_len: usize = 0, /// The cycle the byte being clocked out will be finished on. Until then /// the chip says it is busy, which is what a program waits for. busy_until: u64 = 0, /// How fast the machine runs, which is what turns a rate into cycles. cycles_per_second: u64 = 1_020_500, /// The rate the control register's low four bits ask for, in bits a /// second. Zero means an outside clock, which nothing here provides, so it /// is treated as the fastest the card offers. /// /// The 6551 data sheet's control register (Synertek, n.d.). pub fn baud(self: Acia) u32 { return switch (@as(u4, @truncate(self.control))) { 0b0000 => 19200, // an external clock, which there is not one of 0b0001 => 50, 0b0010 => 75, 0b0011 => 110, 0b0100 => 135, 0b0101 => 150, 0b0110 => 300, 0b0111 => 600, 0b1000 => 1200, 0b1001 => 1800, 0b1010 => 2400, 0b1011 => 3600, 0b1100 => 4800, 0b1101 => 7200, 0b1110 => 9600, 0b1111 => 19200, }; } /// How many bits go down the wire for one byte: the start bit, the word /// itself, a parity bit if one was asked for, and the stop bits. pub fn bitsPerByte(self: Acia) u32 { const word: u32 = switch (@as(u2, @truncate(self.control >> 5))) { 0b00 => 8, 0b01 => 7, 0b10 => 6, 0b11 => 5, }; const stop: u32 = if (self.control & 0x80 != 0) 2 else 1; const parity: u32 = if (self.command & 0x20 != 0) 1 else 0; return 1 + word + parity + stop; } /// How long one byte takes, in cycles. pub fn cyclesPerByte(self: Acia) u64 { return self.cycles_per_second * self.bitsPerByte() / self.baud(); } /// Whether the byte being sent has finished going. fn ready(self: Acia, cycle: u64) bool { return cycle >= self.busy_until; } /// What the status register says (Synertek, n.d., Figure 8), and note that /// the two modem lines are the wrong way up: a nought means the other end /// is there. pub fn status(self: Acia, cycle: u64) u8 { var out: u8 = 0; if (self.interrupting(cycle)) out |= 0x80; // Data set ready and carrier detect, both answered as present: there // is always something on the other end of this one. if (self.ready(cycle)) out |= 0x10; // room for another byte if (self.received != null) out |= 0x08; // one waiting to be read if (self.overrun) out |= 0x04; return out; } /// Whether the chip is asking for attention. /// /// The command register's bit 1 turns the receiver's interrupt off, and /// bits 2 and 3 say what the transmitter does, of which one setting asks /// for an interrupt when there is room for another byte. pub fn interrupting(self: Acia, cycle: u64) bool { const receiver = self.command & 0x02 == 0 and self.received != null; const transmitter = self.command & 0x0c == 0x04 and self.ready(cycle); return receiver or transmitter; } pub fn read(self: *Acia, register: Register, cycle: u64) u8 { return switch (register) { .data => taken: { const byte = self.received orelse 0; self.received = null; self.overrun = false; break :taken byte; }, .status => self.status(cycle), .command => self.command, .control => self.control, }; } pub fn write(self: *Acia, register: Register, value: u8, cycle: u64) void { switch (register) { .data => { // Straight into the queue. The chip says it is busy for as // long as the byte would take at the rate that was asked for, // which is what a program counting on the rate is waiting for. if (self.sending_len < capacity) { self.sending[self.sending_len] = value; self.sending_len += 1; } self.busy_until = @max(cycle, self.busy_until) + self.cyclesPerByte(); }, // Writing here at all is a reset, whatever the value. It clears // the receiver and the errors and leaves the rate alone. .status => { self.received = null; self.overrun = false; self.command &= 0xe0; }, .command => self.command = value, .control => self.control = value, } } /// Hands the machine a byte from outside. Says whether there was room: a /// byte arriving on top of one not yet read is an overrun, and the new one /// is what survives, which is what the chip does. pub fn receive(self: *Acia, byte: u8) bool { const had_room = self.received == null; if (!had_room) self.overrun = true; self.received = byte; return had_room; } /// Whether there is room for another byte from outside. pub fn receiving(self: Acia) bool { return self.received == null; } /// What the machine has sent, oldest first. pub fn sent(self: *const Acia) []const u8 { return self.sending[0..self.sending_len]; } /// Drops the first `count` of those. pub fn consume(self: *Acia, count: usize) void { const taking = @min(count, self.sending_len); std.mem.copyForwards(u8, self.sending[0 .. self.sending_len - taking], self.sending[taking..self.sending_len]); self.sending_len -= taking; } /// What the reset line does, which is what a write to the status register /// does and a little more: the rate goes back to nothing as well. pub fn reset(self: *Acia) void { self.* = .{ .cycles_per_second = self.cycles_per_second }; } }; const testing = std.testing; test "the rate and the shape of a word decide how long a byte takes" { var acia: Acia = .{}; // Nine thousand six hundred bits a second, eight bits, no parity, one // stop: ten bits a byte, so a byte every 1064 microseconds or so. acia.control = 0b0001_1110; try testing.expectEqual(@as(u32, 9600), acia.baud()); try testing.expectEqual(@as(u32, 10), acia.bitsPerByte()); try testing.expectEqual(@as(u64, 1_020_500 * 10 / 9600), acia.cyclesPerByte()); // Three hundred bits a second takes thirty-two times as long. acia.control = 0b0000_0110; try testing.expectEqual(@as(u32, 300), acia.baud()); try testing.expectEqual(@as(u64, 1_020_500 * 10 / 300), acia.cyclesPerByte()); // Seven bits with two stop bits and parity is eleven. acia.control = 0b1010_1110; acia.command = 0x20; try testing.expectEqual(@as(u32, 11), acia.bitsPerByte()); } test "a byte takes the time the rate says it does" { var acia: Acia = .{ .control = 0b0001_1110 }; // 9600, eight bits const takes = acia.cyclesPerByte(); try testing.expect(acia.status(0) & 0x10 != 0); // room for one acia.write(.data, 'A', 0); // Busy until the byte has had time to go. try testing.expect(acia.status(0) & 0x10 == 0); try testing.expect(acia.status(takes - 1) & 0x10 == 0); try testing.expect(acia.status(takes) & 0x10 != 0); // What was written comes out the other side. try testing.expectEqualSlices(u8, "A", acia.sent()); acia.consume(1); try testing.expectEqual(@as(usize, 0), acia.sent().len); // Two in a row take twice as long: the second waits for the first. acia.write(.data, 'B', takes); acia.write(.data, 'C', takes); try testing.expect(acia.status(takes * 2) & 0x10 == 0); try testing.expect(acia.status(takes * 3) & 0x10 != 0); try testing.expectEqualSlices(u8, "BC", acia.sent()); } test "a byte arriving is announced until it is read" { var acia: Acia = .{}; try testing.expect(acia.status(0) & 0x08 == 0); try testing.expect(acia.receiving()); try testing.expect(acia.receive('Z')); try testing.expect(acia.status(0) & 0x08 != 0); try testing.expect(!acia.receiving()); try testing.expectEqual(@as(u8, 'Z'), acia.read(.data, 0)); try testing.expect(acia.status(0) & 0x08 == 0); // One arriving on top of another is an overrun, and the newer survives. try testing.expect(acia.receive('a')); try testing.expect(!acia.receive('b')); try testing.expect(acia.status(0) & 0x04 != 0); try testing.expectEqual(@as(u8, 'b'), acia.read(.data, 0)); try testing.expect(acia.status(0) & 0x04 == 0); } test "writing to the status register resets the chip" { var acia: Acia = .{ .control = 0b0001_1110 }; _ = acia.receive('x'); acia.write(.status, 0, 0); try testing.expect(acia.received == null); // The rate is left alone by a programmed reset. try testing.expectEqual(@as(u32, 9600), acia.baud()); } test "the chip asks for attention only when it was asked to" { var acia: Acia = .{ .control = 0b0001_1110 }; // Receiver interrupts are on when bit 1 is clear. acia.command = 0x00; try testing.expect(!acia.interrupting(0)); _ = acia.receive('q'); try testing.expect(acia.interrupting(0)); try testing.expect(acia.status(0) & 0x80 != 0); // Turned off, it keeps quiet about the same byte. acia.command = 0x02; try testing.expect(!acia.interrupting(0)); _ = acia.read(.data, 0); // The transmitter asks when there is room, for the one setting that says // so. acia.command = 0x06; try testing.expect(acia.interrupting(0)); acia.command = 0x0a; try testing.expect(!acia.interrupting(0)); }