// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The Motorola MC6850 ACIA, the serial chip on a Passport MIDI card. //! //! It is the plainer cousin of the 6551 on a Super Serial Card (`serial.zig`): //! two addresses rather than four, and no baud-rate generator of its own. To //! the program it is two memory locations — a control register to write and a //! status register to read at the first, a transmit register to write and a //! receive register to read at the second — and the whole of driving it is //! polling the status register for two bits: transmit-register-empty, that a //! byte can go out, and receive-register-full, that one has come in. //! //! The chip cannot time itself. Its transmit and receive clocks are pins, and //! the control register only says whether to divide what arrives there by one, //! sixteen or sixty-four. On a Passport that clock is the 6840's first timer, //! wired for half a megahertz, so the ACIA divided by sixteen runs at MIDI's //! 31,250 baud — which is `clock_hz` here, the frequency on those pins, and the //! divisor is the control register's low two bits. The byte time matters for //! the same reason it does on the 6551: MIDI is a real serial line and a //! program feeding it counts on a byte taking as long as it takes. //! //! The register layout is the MC6850 data sheet's, and the addresses are the //! Passport manual's (Bethke & Borowicz, 1985, §4.5). const std = @import("std"); /// How many bytes wait to go out before one is dropped. Something outside /// empties this whenever it looks, so it only covers the gap between looks. pub const capacity = 256; /// The two the chip answers at: the control/status pair, and the data pair. /// Which of a pair is meant is settled by whether it is a read or a write. pub const Register = enum(u1) { control_status = 0, data = 1, }; /// The counter-divide bits, the low two of the control register. Eleven is not /// a divisor at all but a master reset. pub const divide_1: u8 = 0b00; pub const divide_16: u8 = 0b01; pub const divide_64: u8 = 0b10; pub const master_reset: u8 = 0b11; pub const Acia = struct { /// The control register: the divide, the word shape, and the interrupt and /// transmitter control. Write only; a read of its address is the status. control: u8 = 0, /// The byte that came in and has not been read yet, and whether one arrived /// on top of it. received: ?u8 = null, overrun: bool = false, /// Bytes on their way out, oldest first, and the cycle the one being /// clocked out finishes on. Until then transmit-register-empty is false. sending: [capacity]u8 = undefined, /// The cycle each queued byte was handed to the chip, kept alongside it so /// a MIDI recorder can time-stamp what the machine sends. sending_at: [capacity]u64 = undefined, sending_len: usize = 0, busy_until: u64 = 0, /// The frequency on the transmit and receive clock pins, which is the /// 6840's first timer on a Passport: half a megahertz for MIDI. clock_hz: u64 = 500_000, /// How fast the machine runs, for turning a byte time into cycles. cycles_per_second: u64 = 1_020_500, /// The baud rate: the clock on the pins divided by the control register's /// low two bits. A master-reset code is treated as divide-by-one, since it /// is not a running configuration. pub fn baud(self: Acia) u64 { return switch (@as(u2, @truncate(self.control))) { divide_1, master_reset => self.clock_hz, divide_16 => self.clock_hz / 16, divide_64 => self.clock_hz / 64, }; } /// How many bits a byte takes on the wire: the start bit, seven or eight of /// word, a parity bit unless the word select says none, and one or two /// stop bits. (MC6850 data sheet, the word-select table.) pub fn bitsPerByte(self: Acia) u64 { return switch (@as(u3, @truncate(self.control >> 2))) { 0b000 => 1 + 7 + 1 + 2, // 7 even 2 0b001 => 1 + 7 + 1 + 2, // 7 odd 2 0b010 => 1 + 7 + 1 + 1, // 7 even 1 0b011 => 1 + 7 + 1 + 1, // 7 odd 1 0b100 => 1 + 8 + 0 + 2, // 8 none 2 0b101 => 1 + 8 + 0 + 1, // 8 none 1 — MIDI 0b110 => 1 + 8 + 1 + 1, // 8 even 1 0b111 => 1 + 8 + 1 + 1, // 8 odd 1 }; } /// How long one byte takes to clock out, in machine cycles. pub fn cyclesPerByte(self: Acia) u64 { const rate = @max(1, self.baud()); return self.cycles_per_second * self.bitsPerByte() / rate; } fn ready(self: Acia, cycle: u64) bool { return cycle >= self.busy_until; } /// The status register (MC6850 data sheet). Data-carrier-detect and /// clear-to-send are answered as present, the way the Passport wires them: /// there is always something on the MIDI line's other end. pub fn status(self: Acia, cycle: u64) u8 { var out: u8 = 0; if (self.received != null) out |= 0x01; // receive register full if (self.ready(cycle)) out |= 0x02; // transmit register empty if (self.overrun) out |= 0x20; if (self.interrupting(cycle)) out |= 0x80; return out; } /// Whether the chip is pulling its interrupt line. The receiver interrupts /// when its control bit is set and a byte is waiting; the transmitter when /// its two control bits ask for it and there is room for another byte. pub fn interrupting(self: Acia, cycle: u64) bool { const receiver = self.control & 0x80 != 0 and self.received != null; const transmitter = self.control & 0x60 == 0x20 and self.ready(cycle); return receiver or transmitter; } pub fn read(self: *Acia, register: Register, cycle: u64) u8 { return switch (register) { .control_status => self.status(cycle), .data => taken: { const byte = self.received orelse 0; self.received = null; self.overrun = false; break :taken byte; }, }; } pub fn write(self: *Acia, register: Register, value: u8, cycle: u64) void { switch (register) { .control_status => { self.control = value; // A master-reset code clears the receiver and the errors. if (value & 0x03 == master_reset) { self.received = null; self.overrun = false; } }, .data => { if (self.sending_len < capacity) { self.sending[self.sending_len] = value; self.sending_at[self.sending_len] = cycle; self.sending_len += 1; } self.busy_until = @max(cycle, self.busy_until) + self.cyclesPerByte(); }, } } /// Hands the chip a byte from outside, saying whether there was room: a /// byte on top of one not yet read is an overrun, and the new one survives. 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; } pub fn receiving(self: Acia) bool { return self.received == null; } /// What the machine has sent, oldest first, and dropping the first `count`. pub fn sent(self: *const Acia) []const u8 { return self.sending[0..self.sending_len]; } /// The cycle each of those bytes went out on, in step with `sent`. pub fn sentAt(self: *const Acia) []const u64 { return self.sending_at[0..self.sending_len]; } 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]); std.mem.copyForwards(u64, self.sending_at[0 .. self.sending_len - taking], self.sending_at[taking..self.sending_len]); self.sending_len -= taking; } pub fn reset(self: *Acia) void { self.* = .{ .clock_hz = self.clock_hz, .cycles_per_second = self.cycles_per_second }; } }; const testing = std.testing; test "divide by sixteen off a half-megahertz clock is MIDI's baud rate" { var acia: Acia = .{}; acia.write(.control_status, 0b0001_0101, 0); // 8 none 1, divide by 16 try testing.expectEqual(@as(u64, 31_250), acia.baud()); try testing.expectEqual(@as(u64, 10), acia.bitsPerByte()); // Ten bits at 31,250 baud is 320 microseconds, about 327 machine cycles. try testing.expectEqual(@as(u64, 1_020_500 * 10 / 31_250), acia.cyclesPerByte()); // Divide by one is the whole clock; sixty-four is the slow end. acia.write(.control_status, 0b0001_0100, 0); try testing.expectEqual(@as(u64, 500_000), acia.baud()); acia.write(.control_status, 0b0001_0110, 0); try testing.expectEqual(@as(u64, 7_812), acia.baud()); } test "a byte is held out for as long as it takes at the baud rate" { var acia: Acia = .{}; acia.write(.control_status, 0b0001_0101, 0); // MIDI: 8N1 divide 16 const byte_cycles = acia.cyclesPerByte(); // Empty to begin with, so a byte can go. try testing.expect(acia.status(0) & 0x02 != 0); acia.write(.data, 0x90, 0); // a MIDI note-on status byte try testing.expectEqualSlices(u8, &[_]u8{0x90}, acia.sent()); // Busy until the byte has clocked out. try testing.expect(acia.status(byte_cycles - 1) & 0x02 == 0); try testing.expect(acia.status(byte_cycles) & 0x02 != 0); // A second byte queues behind the first and pushes the finish out. acia.write(.data, 0x3c, 10); try testing.expectEqualSlices(u8, &[_]u8{ 0x90, 0x3c }, acia.sent()); acia.consume(2); try testing.expectEqual(@as(usize, 0), acia.sent().len); } test "a byte comes in, is read once, and a second on top of it is an overrun" { var acia: Acia = .{}; try testing.expect(acia.receiving()); try testing.expect(acia.receive(0x3c)); try testing.expect(!acia.receiving()); // Receive-register-full is set. try testing.expect(acia.status(0) & 0x01 != 0); // A second byte before the first is read is an overrun. try testing.expect(!acia.receive(0x40)); try testing.expect(acia.status(0) & 0x20 != 0); // Reading the data register hands back the survivor and clears the flags. try testing.expectEqual(@as(u8, 0x40), acia.read(.data, 0)); try testing.expect(acia.status(0) & 0x21 == 0); try testing.expect(acia.receiving()); } test "the interrupt line follows the control register's two enables" { var acia: Acia = .{}; // Receiver interrupt on ($C7 set), a byte waiting: it interrupts. acia.write(.control_status, 0b1001_0101, 0); try testing.expect(!acia.interrupting(0)); _ = acia.receive(0x7f); try testing.expect(acia.interrupting(0)); _ = acia.read(.data, 0); try testing.expect(!acia.interrupting(0)); // Transmitter interrupt on (bits 6,5 = 01): it interrupts whenever there is // room, which is the moment the last byte has gone. acia.write(.control_status, 0b0011_0101, 0); try testing.expect(acia.interrupting(0)); // empty, so room acia.write(.data, 0x90, 0); try testing.expect(!acia.interrupting(acia.cyclesPerByte() - 1)); try testing.expect(acia.interrupting(acia.cyclesPerByte())); } test "a master reset clears the receiver" { var acia: Acia = .{}; _ = acia.receive(0x50); acia.write(.control_status, 0b0000_0011, 0); // divide bits = master reset try testing.expect(acia.receiving()); try testing.expect(acia.status(0) & 0x01 == 0); }