// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The Corvus Omninet Transporter: the card that puts an Apple II on Corvus's //! network, and through it on a Corvus disk. //! //! It is not worked the way the disk cards here are. A Transporter has a //! processor of its own and is told what to do in whole commands: "A host //! computer issues a command to its Transporter by first formatting a command //! vector in its own memory ... and then sending the address of that vector to //! the Transporter. The command vector tells the Transporter what command to //! execute and how to execute it" (Corvus Systems, 1984, printed p. 30). The //! card then reaches into host memory itself — it is a DMA device — does the //! work, and writes a return code where the vector told it to. //! //! **The whole host interface is one register**, which is worth saying plainly //! because the manual does not say it: being written to be host-independent, it //! describes a READY line and a STROBE and leaves where they live to each //! computer's own manual. The card's slot firmware answers that, and answers it //! by working the address out rather than by carrying it. It finds which slot //! it is in the way slot firmware always has — `LDA #$60`, `STA $00`, //! `JSR $0000`, then the return address off the stack — and then, at `$c760` of //! the PROM as it sits in slot seven: //! //! LDA $01 ; the page it is running from, $c7 //! ASL ; four times over, so $c7 becomes $70 //! ASL //! ASL //! ASL //! ADC #$80 ; $f0, which with $c0 above it is $c0f0 //! //! So the card answers at `$c080` plus sixteen times its slot, and a command is //! three bytes written there: "the host sending a 24 bit address to the //! Transporter in the form of 3 bytes (most significant byte first)" (printed //! p. 33), with the READY line read back from the same address to know when the //! next one may go. //! //! What the other fifteen addresses of the card's page do is not known. The //! firmware never touches them and no manual here says, so this answers at the //! first and leaves the rest alone rather than inventing a decode. const std = @import("std"); /// The commands a Transporter answers, by the code in the first byte of a /// command vector (Corvus Systems, 1984, printed pp. 35-46). pub const Command = enum(u8) { who_am_i = 0x01, echo = 0x02, peek_poke = 0x08, end_receive = 0x10, initialize = 0x20, send_message = 0x40, setup_receive = 0xf0, _, }; /// The return codes, from the table the manual gathers them into (printed /// p. 47). A command's result record begins with one of these, and the host /// knows the command is done when it stops being `pending`. pub const ok: u8 = 0x00; /// "Transmit failure (no acknowledgement after maximum number of retries)", /// which is the honest answer on a network with nobody else on it. pub const no_acknowledgement: u8 = 0x80; /// "Bad socket number in command (must be 80, 90, A0 or B0)". pub const bad_socket: u8 = 0x84; /// "Bad node number in command (must be 0-7F or FF)". pub const bad_node: u8 = 0x86; /// "Received an ACK for an Echo command". pub const echo_acknowledged: u8 = 0xc0; /// "Socket set up successfully". pub const socket_set_up: u8 = 0xfe; /// What the host writes into the return code before issuing a command, and /// watches for the Transporter to change: "the host must write the value FF /// hexadecimal to the first byte ... of the result record" (printed p. 33). pub const pending: u8 = 0xff; /// The four sockets a Transporter has, which are named by these numbers and no /// others: "must be 80, 90, A0 or B0". pub fn validSocket(socket: u8) bool { return socket == 0x80 or socket == 0x90 or socket == 0xa0 or socket == 0xb0; } /// Node numbers run to `$7f`, with `$ff` meaning every node at once — a /// broadcast. Anything else is refused. pub fn validNode(node: u8) bool { return node <= 0x7f or node == 0xff; } pub const Card = struct { /// Whether there is a card in the slot at all. fitted: bool = false, /// Which node this Transporter is, which on the real card is a DIP switch /// on the board. "Who Am I returns the network node number of requesting /// Transporter (00-3F) as the return code" (printed p. 41). node: u8 = 0, /// The command vector's address as it arrives, most significant byte /// first, and how much of it has come so far. address: [3]u8 = @splat(0), have: usize = 0, /// What the last command was and how many have been run, which is what a /// test reads rather than the network. last: ?Command = null, commands: u64 = 0, /// The READY line, read as bit seven of the card's first address. /// /// A real Transporter drops it while it is busy — receiving a packet, or /// still working on the command before — and the manual is emphatic about /// what goes wrong when a host does not wait (printed p. 34). Nothing here /// takes any time, so it is always ready; when there is something on the /// other end of the wire that will stop being true. pub fn ready(self: *const Card) bool { return self.fitted; } /// What the card's first address reads as: the READY line in bit seven. pub fn status(self: *const Card) u8 { return if (self.ready()) 0x80 else 0x00; } /// One byte of a command vector address from the host. The third one /// completes it, and the command runs. /// /// `machine` is whatever the card is fitted to, and has to offer /// `blockGet` and `blockPut` — one byte of its memory in and one out — /// which is this card reaching into host memory the way the real one /// reaches in by DMA. pub fn write(self: *Card, machine: anytype, byte: u8) void { if (!self.fitted) return; if (self.have >= self.address.len) self.have = 0; self.address[self.have] = byte; self.have += 1; if (self.have == self.address.len) { self.have = 0; self.run(machine); } } /// Reads the command vector out of host memory and does what it says. fn run(self: *Card, machine: anytype) void { // Three bytes of address, most significant first. An Apple II has // sixteen bits of memory to be pointed at, so the top byte is only // ever nought here; it is read and dropped rather than ignored, // because a card that quietly used the low two would be a card that // worked by luck on a machine that had more. const vector: u16 = (@as(u16, self.address[1]) << 8) | self.address[2]; const code: Command = @enumFromInt(machine.blockGet(vector)); // Every command vector carries the address of its result record next, // in the same three bytes most significant first. const result: u16 = (@as(u16, machine.blockGet(vector +% 2)) << 8) | machine.blockGet(vector +% 3); self.last = code; self.commands += 1; const answer: u8 = switch (code) { // Both of these say who the Transporter is: Initialize returns "a // return code value equal to the host node number", and Who Am I // "returns the network node number of requesting Transporter". .initialize, .who_am_i => self.node, // A socket is armed without anybody else being there, so this can // answer truthfully. .setup_receive => blk: { const socket = machine.blockGet(vector +% 4); break :blk if (validSocket(socket)) socket_set_up else bad_socket; }, .end_receive => blk: { const socket = machine.blockGet(vector +% 4); break :blk if (validSocket(socket)) ok else bad_socket; }, // And these two want somebody on the wire. With nobody there the // right answer is not an error of ours but the one a real // Transporter gives after its retries run out, which is what a // network with one node on it looks like from the inside. .send_message => blk: { // The destination host is the vector's last byte, after the // socket, the data address, the data length and the control // length (printed p. 36). const host = machine.blockGet(vector +% 12); break :blk if (validNode(host)) no_acknowledgement else bad_node; }, .echo => blk: { const host = machine.blockGet(vector +% 4); break :blk if (validNode(host)) no_acknowledgement else bad_node; }, // Peek and Poke reach the Transporter's own memory, which this // does not model: there is no 6801 in here to have any. A Poke is // accepted and dropped and a Peek reads as nought, which is what // an unwritten byte of it would hold. .peek_poke => ok, else => ok, }; machine.blockPut(result, answer); } pub fn reset(self: *Card) void { self.have = 0; self.address = @splat(0); } }; const testing = std.testing; /// Sixty-four kilobytes of somewhere for the card to reach into, which is all /// a Transporter wants of the machine it is in. const Fake = struct { memory: [0x10000]u8 = @splat(0), pub fn blockGet(self: *Fake, at: u16) u8 { return self.memory[at]; } pub fn blockPut(self: *Fake, at: u16, byte: u8) void { self.memory[at] = byte; } }; /// Lays a command vector down at `$2000` with its result record at `$3000`, /// the way a host would, and hands the card the address a byte at a time. fn issue(card: *Card, machine: *Fake, vector: []const u8) u8 { const at: u16 = 0x2000; const result: u16 = 0x3000; @memcpy(machine.memory[at..][0..vector.len], vector); machine.memory[result] = pending; // Three bytes, most significant first. card.write(machine, 0x00); card.write(machine, @truncate(at >> 8)); card.write(machine, @truncate(at)); return machine.memory[result]; } /// A command vector: the code, then the result record's address in three /// bytes, then whatever else the command wants. fn commandFor(code: Command, rest: []const u8) [16]u8 { var out: [16]u8 = @splat(0); out[0] = @intFromEnum(code); out[1] = 0x00; out[2] = 0x30; // the result record at $3000, most significant byte first out[3] = 0x00; @memcpy(out[4..][0..rest.len], rest); return out; } test "a card nobody fitted is not ready and answers nothing" { var machine: Fake = .{}; var card: Card = .{}; try testing.expect(!card.ready()); try testing.expectEqual(@as(u8, 0x00), card.status()); const v = commandFor(.who_am_i, &.{}); _ = issue(&card, &machine, &v); try testing.expectEqual(@as(u64, 0), card.commands); } test "a fitted card holds its ready line up in bit seven" { // Which is the whole of how the firmware finds it: `BIT $c080,X` and a // `BPL` back on itself until bit seven is set. var card: Card = .{ .fitted = true }; try testing.expect(card.ready()); try testing.expectEqual(@as(u8, 0x80), card.status()); } test "three bytes make a command, and fewer make none" { var machine: Fake = .{}; var card: Card = .{ .fitted = true, .node = 0x12 }; const v = commandFor(.who_am_i, &.{}); @memcpy(machine.memory[0x2000..][0..v.len], &v); machine.memory[0x3000] = pending; card.write(&machine, 0x00); card.write(&machine, 0x20); try testing.expectEqual(@as(u64, 0), card.commands); try testing.expectEqual(pending, machine.memory[0x3000]); card.write(&machine, 0x00); try testing.expectEqual(@as(u64, 1), card.commands); try testing.expectEqual(@as(u8, 0x12), machine.memory[0x3000]); } test "Who Am I and Initialize both answer with the node number" { // "Who am I returns the network node number of requesting Transporter // (00-3F) as the return code", and Initialize returns "a return code value // equal to the host node number". var machine: Fake = .{}; var card: Card = .{ .fitted = true, .node = 0x2a }; for ([_]Command{ .who_am_i, .initialize }) |code| { const v = commandFor(code, &.{}); try testing.expectEqual(@as(u8, 0x2a), issue(&card, &machine, &v)); try testing.expectEqual(code, card.last.?); } } test "a socket is set up, and only the four that exist" { // "Bad socket number in command (must be 80, 90, A0 or B0)". var machine: Fake = .{}; var card: Card = .{ .fitted = true }; for ([_]u8{ 0x80, 0x90, 0xa0, 0xb0 }) |socket| { const v = commandFor(.setup_receive, &.{socket}); try testing.expectEqual(socket_set_up, issue(&card, &machine, &v)); } for ([_]u8{ 0x00, 0x81, 0xc0, 0xff }) |socket| { const v = commandFor(.setup_receive, &.{socket}); try testing.expectEqual(bad_socket, issue(&card, &machine, &v)); } } test "a message sent to a network with nobody on it is not acknowledged" { // Not an error of this card's but the answer a real Transporter gives once // its retries run out: "Transmit failure (no acknowledgement after maximum // number of retries)". A network with one node on it is what that looks // like from the inside. var machine: Fake = .{}; var card: Card = .{ .fitted = true }; // Send Message: socket, data address, data length, control length, then // the destination host as the last byte of the vector. var send = commandFor(.send_message, &.{ 0x80, 0x00, 0x40, 0x00, 0x00, 0x10, 0x00 }); send[12] = 0x05; // a node that is allowed to exist try testing.expectEqual(no_acknowledgement, issue(&card, &machine, &send)); // Echo carries its destination immediately after the result address. const echo = commandFor(.echo, &.{0x05}); try testing.expectEqual(no_acknowledgement, issue(&card, &machine, &echo)); } test "a node number that cannot exist is refused rather than tried" { // "Bad node number in command (must be 0-7F or FF)", `$ff` being every // node at once. var machine: Fake = .{}; var card: Card = .{ .fitted = true }; const bad = commandFor(.echo, &.{0x80}); try testing.expectEqual(bad_node, issue(&card, &machine, &bad)); const broadcast = commandFor(.echo, &.{0xff}); try testing.expectEqual(no_acknowledgement, issue(&card, &machine, &broadcast)); } test "the result record is written where the vector says, not somewhere fixed" { var machine: Fake = .{}; var card: Card = .{ .fitted = true, .node = 0x07 }; // A vector at $2000 pointing its result record at $4321 instead. machine.memory[0x2000] = @intFromEnum(Command.who_am_i); machine.memory[0x2001] = 0x00; machine.memory[0x2002] = 0x43; machine.memory[0x2003] = 0x21; machine.memory[0x4321] = pending; card.write(&machine, 0x00); card.write(&machine, 0x20); card.write(&machine, 0x00); try testing.expectEqual(@as(u8, 0x07), machine.memory[0x4321]); }