// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The far end of a IIgs's LocalTalk cable: a bridge between the frames the //! SCC sends and receives and the datagrams other emulators exchange. //! //! LocalTalk is a bus. Every node on the cable hears every frame, and the //! link layer — LLAP, the LocalTalk Link Access Protocol — arranges who //! speaks when with a handshake: a node with a directed frame to send first //! sends a lapRTS to the destination and waits for a lapCTS back before it //! sends the data, and a node coming up chooses a number for itself and //! sends lapENQ to that number to find out whether anyone already has it //! (ZiLOG, 2001, Chapter 6, "Technical Considerations When Implementing //! LocalTalk Link Access Protocol"). Inside Macintosh's account of the layer //! is from the software's side — a frame is a destination, a source, "an //! eight-bit quantity, contained in the frame's header, that identifies" the //! protocol above it, and then data (Apple Computer, Inc., 1985d, p. II-264) //! — and the values of the four control types are not written down in //! either; they are what this machine's own firmware sends and compares //! against, at `$ff/31b8` and `$ff/2ef2` in ROM 01. //! //! The convention emulators use to put that bus on an ordinary network is //! LocalTalk over UDP, from Mini vMac (Fort, Pratt & Mitchelmore, 2012): each //! LLAP frame goes out as one UDP datagram to the multicast group //! 239.192.76.84 on port 1954, with four bytes in front of it that identify //! the sender so that a node can recognise its own frames when the group //! hands them back to it. The frame check sequence stays off the wire — the //! datagram is the bytes the link layer wrote, and every receiver's chip puts //! a check of its own on the end. And the handshake stays off it too: TashTalk's //! daemon, which puts real LocalTalk hardware on the same group, does "not //! retransmit RTS/CTS frame[s]" (lampmerchant, 2021), since a datagram cannot //! collide and the handshake is about collisions. A bridge therefore answers //! the machine's lapRTS with a lapCTS of its own, as a bridge on a real cable //! would, and the machine goes on to send its data. //! //! This file is the bridge without the socket: which frames cross, which are //! answered, which are ignored. What carries the datagrams is the front end's //! business, and a test's. const std = @import("std"); const scc_module = @import("scc.zig"); const Scc = scc_module.Scc; /// The multicast group LocalTalk over UDP uses, whose last two octets are /// "LT", and its port. pub const group = [4]u8{ 239, 192, 76, 84 }; pub const port: u16 = 1954; /// The LLAP control frame types, named as Zilog's note names them and /// numbered as the firmware does. pub const Frame = struct { pub const enq: u8 = 0x81; pub const ack: u8 = 0x82; pub const rts: u8 = 0x84; pub const cts: u8 = 0x85; /// Every node. pub const broadcast: u8 = 0xff; /// The three bytes every LLAP frame begins with. pub const header = 3; }; /// The four bytes in front of every datagram: whoever sent it, as an opaque /// number, so that a sender can tell its own datagrams from everyone else's /// when the group delivers them back. pub const tag_len = 4; /// The most a datagram can be: the tag and the longest frame LocalTalk /// allows, without its check. pub const longest = tag_len + Scc.Frame.longest - 2; /// Puts the tag in front of a frame's bytes. pub fn wrap(tag: u32, frame: []const u8, into: *[longest]u8) []const u8 { std.mem.writeInt(u32, into[0..tag_len], tag, .big); @memcpy(into[tag_len .. tag_len + frame.len], frame); return into[0 .. tag_len + frame.len]; } /// Takes a datagram apart. Nothing shorter than a tag and a header is a /// frame at all. pub fn unwrap(datagram: []const u8) ?struct { tag: u32, frame: []const u8 } { if (datagram.len < tag_len + Frame.header) return null; return .{ .tag = std.mem.readInt(u32, datagram[0..tag_len], .big), .frame = datagram[tag_len..], }; } /// The bridge: one machine's end of the cable, and what it does with each /// frame in either direction. pub const Bridge = struct { /// This node's tag on every datagram it sends. tag: u32, /// Which of the chip's channels the cable is on, once the firmware has /// said by putting it into SDLC mode. channel: ?Scc.Which = null, /// The node number the machine last sent from, which is what it has /// settled on, and when anything last crossed in either direction. node: ?u8 = null, last_activity: u64 = 0, /// How much has gone by, for the status line and for tests. sent: u64 = 0, received: u64 = 0, /// Requests to send this end answered itself with a clear to send. answered: u64 = 0, /// Handshake frames that stayed off the network. handshakes_dropped: u64 = 0, /// Datagrams that came back carrying this end's own tag. echoes: u64 = 0, /// Datagrams that arrived while the chip had no room to queue them. overflowed: u64 = 0, /// Which channel the network is on: whichever the firmware has put /// into SDLC mode, the printer port first if somehow both are. pub fn attach(self: *Bridge, scc: *const Scc) ?Scc.Which { if (scc.a.sdlc()) { self.channel = .a; } else if (scc.b.sdlc()) { self.channel = .b; } else { self.channel = null; } return self.channel; } /// Takes the frames the machine has sent off the chip and does what the /// cable would: a lapRTS to one node is answered with that node's lapCTS, /// a lapCTS goes nowhere, and everything else goes to the group through /// `sink`, which is anything with `send(bytes: []const u8) void`. pub fn drain(self: *Bridge, scc: *Scc, now: u64, sink: anytype) void { const which = self.attach(scc) orelse return; const channel = scc.channelOf(which); const frames = channel.sentFrames(); var buffer: [longest]u8 = undefined; for (frames) |frame| { const bytes = frame.payload(); if (bytes.len < Frame.header) continue; self.last_activity = now; self.node = bytes[1]; switch (bytes[2]) { Frame.rts => { self.handshakes_dropped += 1; // A broadcast dialogue has no clear to send: the sender // waits out the gap and goes ahead. A directed one is // answered here, from the node it was for. if (bytes[0] == Frame.broadcast) continue; const cts = [_]u8{ bytes[1], bytes[0], Frame.cts }; _ = scc.receiveFrame(which, &cts, true, now); self.answered += 1; }, Frame.cts => self.handshakes_dropped += 1, else => { sink.send(wrap(self.tag, bytes, &buffer)); self.sent += 1; }, } } channel.consumeFrames(frames.len); } /// A datagram from the group: the machine's own come back and are /// ignored, and everyone else's go onto the chip's wire. pub fn deliver(self: *Bridge, scc: *Scc, datagram: []const u8, now: u64) void { const parts = unwrap(datagram) orelse return; if (parts.tag == self.tag) { self.echoes += 1; return; } const which = self.attach(scc) orelse return; if (!scc.receiveFrame(which, parts.frame, true, now)) { self.overflowed += 1; return; } self.received += 1; self.last_activity = now; } /// Whether anything has crossed in the last while, for a light. pub fn lit(self: *const Bridge, now: u64, within: u64) bool { return self.last_activity != 0 and now -| self.last_activity < within; } }; const testing = std.testing; /// A sink that keeps what it was given. const Kept = struct { datagrams: [8][longest]u8 = undefined, lengths: [8]usize = @splat(0), count: usize = 0, fn send(self: *Kept, bytes: []const u8) void { @memcpy(self.datagrams[self.count][0..bytes.len], bytes); self.lengths[self.count] = bytes.len; self.count += 1; } fn get(self: *const Kept, index: usize) []const u8 { return self.datagrams[index][0..self.lengths[index]]; } }; /// A chip with its printer port on the network, the way the firmware sets /// it up, and its transmitter and driver on. fn networked() Scc { var scc: Scc = .{}; const table = [_]u8{ 0x04, 0x20, 0x03, 0xdd, 0x05, 0xeb, 0x0c, 0x06, 0x0d, 0x00 }; for (table) |byte| _ = scc.access(0b01, true, byte, 0); return scc; } /// Sends a frame through the chip the way the firmware does, and lets it /// finish. fn sendThrough(scc: *Scc, bytes: []const u8, from: u64) u64 { var now = from; _ = scc.access(0b01, true, 0x80, now); // Reset Tx CRC _ = scc.access(0b11, true, bytes[0], now); _ = scc.access(0b01, true, 0xc0, now); // Reset Tx Underrun/EOM latch for (bytes[1..]) |byte| { while (scc.access(0b01, false, 0, now) & Scc.tx_buffer_empty == 0) now += 12; _ = scc.access(0b11, true, byte, now); } while (!scc.a.allSent(now)) { now += 12; scc.advance(now); } return now; } test "a datagram is a tag and a frame, and nothing shorter is a datagram" { var buffer: [longest]u8 = undefined; const datagram = wrap(0x01020304, &.{ 0xff, 0x05, 0x81 }, &buffer); try testing.expectEqualSlices(u8, &.{ 1, 2, 3, 4, 0xff, 0x05, 0x81 }, datagram); const parts = unwrap(datagram).?; try testing.expectEqual(@as(u32, 0x01020304), parts.tag); try testing.expectEqualSlices(u8, &.{ 0xff, 0x05, 0x81 }, parts.frame); try testing.expect(unwrap(&.{ 1, 2, 3, 4, 0xff, 0x05 }) == null); } test "an enquiry goes to the group without its check, and a clear to send never does" { var scc = networked(); var bridge: Bridge = .{ .tag = 0xdeadbeef }; var kept: Kept = .{}; var now = sendThrough(&scc, &.{ 0x07, 0x07, Frame.enq }, 1000); bridge.drain(&scc, now, &kept); try testing.expectEqual(@as(usize, 1), kept.count); try testing.expectEqualSlices(u8, &.{ 0xde, 0xad, 0xbe, 0xef, 0x07, 0x07, 0x81 }, kept.get(0)); try testing.expectEqual(@as(?u8, 0x07), bridge.node); try testing.expectEqual(@as(usize, 0), scc.a.sentFrames().len); now = sendThrough(&scc, &.{ 0x03, 0x07, Frame.cts }, now + 1000); bridge.drain(&scc, now, &kept); try testing.expectEqual(@as(usize, 1), kept.count); try testing.expectEqual(@as(u64, 1), bridge.handshakes_dropped); } test "a request to send one node is answered by that node's clear to send" { // The handshake is about collisions, and a datagram cannot collide, so it // stays here: the bridge answers as the far node would, and the machine's // data frame is what crosses. var scc = networked(); var bridge: Bridge = .{ .tag = 1 }; var kept: Kept = .{}; _ = scc.access(0b01, true, 0x06, 0); _ = scc.access(0b01, true, 0x07, 0); // this is node 7 var now = sendThrough(&scc, &.{ 0x03, 0x07, Frame.rts }, 1000); bridge.drain(&scc, now, &kept); try testing.expectEqual(@as(usize, 0), kept.count); try testing.expectEqual(@as(u64, 1), bridge.answered); // And the answer arrives on the chip, addressed to node 7 from node 3. // The opening flag and the three bytes have arrived; the check has not, // and the FIFO is three deep, so this is the moment to read. const bit = scc.a.cyclesPerBit(scc.cycles_per_second); now += 8 * bit * 4 + 1; scc.advance(now); try testing.expect(scc.readRegister(.a, 0) & Scc.rx_available != 0); try testing.expectEqual(@as(u8, 0x07), scc.access(0b11, false, 0, now)); try testing.expectEqual(@as(u8, 0x03), scc.access(0b11, false, 0, now)); try testing.expectEqual(Frame.cts, scc.access(0b11, false, 0, now)); // A broadcast request has no answer to wait for. now = sendThrough(&scc, &.{ Frame.broadcast, 0x07, Frame.rts }, now + 5000); bridge.drain(&scc, now, &kept); try testing.expectEqual(@as(u64, 1), bridge.answered); try testing.expectEqual(@as(usize, 0), kept.count); } test "a datagram from the group goes onto the wire, and one's own comes back ignored" { var scc = networked(); var bridge: Bridge = .{ .tag = 0x11223344 }; _ = scc.access(0b01, true, 0x06, 0); _ = scc.access(0b01, true, 0x07, 0); // this is node 7 bridge.deliver(&scc, &.{ 0x11, 0x22, 0x33, 0x44, 0x07, 0x03, Frame.ack }, 1000); try testing.expectEqual(@as(u64, 1), bridge.echoes); try testing.expectEqual(@as(u64, 0), bridge.received); try testing.expect(!scc.a.frameArriving()); bridge.deliver(&scc, &.{ 0x55, 0x66, 0x77, 0x88, 0x07, 0x03, Frame.ack }, 1000); try testing.expectEqual(@as(u64, 1), bridge.received); try testing.expect(scc.a.frameArriving()); const bit = scc.a.cyclesPerBit(scc.cycles_per_second); scc.advance(1000 + 8 * bit * 4 + 1); try testing.expectEqual(@as(u8, 0x07), scc.access(0b11, false, 0, scc.now)); try testing.expectEqual(@as(u8, 0x03), scc.access(0b11, false, 0, scc.now)); try testing.expectEqual(Frame.ack, scc.access(0b11, false, 0, scc.now)); try testing.expect(bridge.lit(scc.now, 1_000_000)); } test "the bridge is on whichever port the firmware put on the network" { var scc: Scc = .{}; var bridge: Bridge = .{ .tag = 1 }; try testing.expect(bridge.attach(&scc) == null); _ = scc.access(0b00, true, 0x04, 0); _ = scc.access(0b00, true, 0x20, 0); // channel B into SDLC try testing.expectEqual(@as(?Scc.Which, .b), bridge.attach(&scc)); }