Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The Cirrus Logic CS8900A, the Ethernet chip on an a2RetroSystems Uthernet//! (the original Uthernet, "Uthernet I").//!//! Where the W5100 on the Uthernet II carries its own TCP/IP stack, the CS8900A//! carries none: it is a *raw* Ethernet controller. A driver — IP65, Contiki,//! Marinetti — runs its own stack and hands the chip whole Ethernet frames to//! send, and takes whole frames the chip has received. That is the difference//! that decides the host end: the W5100 maps onto the host's own sockets, and//! the CS8900A maps onto a piece of wire, which here is a UDP multicast group//! other emulators share, exactly as `ltoudp.zig` does for LocalTalk.//!//! This is the chip and nothing of the host. It is the PacketPage memory, the//! eight I/O ports the Uthernet wires to a slot, and the transmit and receive//! frame windows, and it turns what a driver writes into a frame a session can//! take and a frame a session delivers into something the driver can read. The//! session (`ethernet.zig`) is what has the socket and moves the frames; see//! `pumpEthernet` there.//!//! ## The PacketPage architecture (CS8900A data sheet, §4)//!//! The chip's registers and its frame buffers do not each sit at an address of//! their own. They live in a four-kilobyte internal space, *PacketPage memory*,//! reached indirectly: a driver writes the PacketPage Pointer with an address//! and then reads or writes the PacketPage Data port, which lands on the two//! bytes the pointer names — and, with the pointer's auto-increment bit set,//! steps the pointer on by two. The map a driver works (§4.2)://!//! - `0x0000` the read-only Product Identification Code, `0x630E` followed by a//! revision, which is the signature a driver checks to know the chip is there.//! - `0x0100`–`0x011F` the configuration and control registers — the receiver//! control that says which frames to accept (`RxCTL`, `0x0104`), the line//! control that turns the transmitter and receiver on (`LineCTL`, `0x0112`),//! and the rest — and `0x0120`–`0x013F` the status registers, chief of them//! the Interrupt Status Queue.//! - `0x0158` the Individual Address, the six bytes of the MAC.//! - `0x0400` the Receive Frame Location and `0x0A00` the Transmit Frame//! Location, the windows a received frame is read out of and a frame to send//! is written into.//!//! ## The bus (§4.10)//!//! The Uthernet reaches the chip through its I/O-mode ports: eight sixteen-bit//! ports at the base, which are the slot's sixteen device selects. The chip is//! sixteen bits and the slot is eight, so each port is a low byte at an even//! address and a high byte at the odd one above it. The ports are the//! Receive/Transmit Data port (`0x0`), the TxCMD port (`0x4`), the TxLength port//! (`0x6`), the ISQ port (`0x8`), the PacketPage Pointer port (`0xA`) and the//! PacketPage Data port (`0xC`). A driver sends a frame by writing TxCMD then//! TxLength then streaming the frame to the data port, and takes a received one//! by reading the data port for its status, its length, and then its bytes.//!//! Like the other cards here, none of this has been checked against real//! software; it is checked against the data sheet and the sequence its drivers//! are documented to follow (Cirrus Logic, 2004).
const std = @import("std");
/// The PacketPage space is four kilobytes.pub const page_size = 0x1000;
// The PacketPage map (§4.2), the locations a driver works.const pp_product_id: u16 = 0x0000; // read-only, 0x630E and a revisionconst pp_rx_cfg: u16 = 0x0102; // Register 3const pp_rx_ctl: u16 = 0x0104; // Register 5: which frames to acceptconst pp_tx_cfg: u16 = 0x0106; // Register 7const pp_tx_cmd_read: u16 = 0x0108; // Register 9, the read-back copyconst pp_buf_cfg: u16 = 0x010a; // Register Bconst pp_line_ctl: u16 = 0x0112; // Register 13: transmitter/receiver onconst pp_self_ctl: u16 = 0x0114; // Register 15const pp_bus_ctl: u16 = 0x0116; // Register 17const pp_isq: u16 = 0x0120; // the Interrupt Status Queueconst pp_rx_event: u16 = 0x0124; // Register 4const pp_tx_event: u16 = 0x0128; // Register 8const pp_buf_event: u16 = 0x012c; // Register Cconst pp_bus_st: u16 = 0x0138; // Register 18: Rdy4TxNOW lives hereconst pp_ia: u16 = 0x0158; // the Individual Address (MAC), six bytesconst rx_frame: u16 = 0x0400; // the received frame: status, length, bytesconst tx_frame: u16 = 0x0a00; // the frame to send
/// The chip's EISA product number, read back at `pp_product_id`: the low half/// is `0x630E`, and a driver reads the `0x0E`/`0x63` to know a CS8900A answers.pub const product_id: u16 = 0x630e;
// The I/O-mode ports (§4.10), as byte offsets in the slot's device selects.const io_rxtx_data: u4 = 0x0; // the receive/transmit data portconst io_txcmd: u4 = 0x4; // bid to transmitconst io_txlength: u4 = 0x6; // how long the frame isconst io_isq: u4 = 0x8; // the interrupt status queueconst io_pp_pointer: u4 = 0xa; // the PacketPage addressconst io_pp_data: u4 = 0xc; // the PacketPage data window
// Bits a driver watches.const rx_event_rxok: u16 = 0x0100; // a good frame arrived (RxEvent bit 8)const bus_st_rdy4tx: u16 = 0x0100; // the transmit buffer is free (BusST bit 8)const isq_pointer_mask: u16 = 0x003f; // the low six bits of an ISQ word name the registerconst auto_increment: u16 = 0x8000; // the pointer's top bit: step on after a data access
/// The largest Ethernet frame, header and payload and CRC: 1518 bytes. The/// windows hold one each with room to spare.pub const max_frame = 1518;
pub const CS8900 = struct { /// The internal PacketPage memory the pointer addresses: the registers low /// down, the receive window at `0x0400`, the transmit window at `0x0A00`. page: [page_size]u8 = @splat(0), /// The PacketPage pointer, with its auto-increment bit in the top. pointer: u16 = 0,
/// How far through the receive window the data port has been read, and how /// far through the transmit window it has been written. The chip keeps /// these; a driver only ever streams. rx_read: u16 = rx_frame, tx_write: u16 = tx_frame, /// The length a driver set with TxLength, and whether a whole frame has /// been streamed in and is waiting for the session to send it. tx_length: u16 = 0, tx_pending: bool = false, /// Whether a received frame is sitting in the window unread, which is what /// the ISQ reports and the interrupt line follows. rx_ready: bool = false, /// The half-written value of each of the eight sixteen-bit ports, so a word /// written a byte at a time on the eight-bit bus lands whole. Indexed by /// port number (the offset's high three bits). latch: [8]u16 = @splat(0),
fn word(self: *const CS8900, at: u16) u16 { return @as(u16, self.page[at & (page_size - 1)]) | @as(u16, self.page[(at + 1) & (page_size - 1)]) << 8; }
fn setWord(self: *CS8900, at: u16, value: u16) void { self.page[at & (page_size - 1)] = @truncate(value); self.page[(at + 1) & (page_size - 1)] = @truncate(value >> 8); }
/// Brings the chip up as a driver expects to find it: the product code that /// says it is a CS8900A, and the transmit buffer already free. pub fn init() CS8900 { var self: CS8900 = .{}; self.setWord(pp_product_id, product_id); self.setWord(pp_product_id + 2, 0x0900); // a revision, the low bits ignored self.setWord(pp_bus_st, bus_st_rdy4tx); return self; }
pub fn reset(self: *CS8900) void { self.* = init(); }
// -- the bus: sixteen device selects, eight sixteen-bit ports ------------
/// The Uthernet's slot access. `offset` is the byte within the sixteen; the /// even byte of a port is its low half and the odd byte its high half. pub fn read(self: *CS8900, offset: u4) u8 { const port: u4 = offset & 0xe; // The port access — which for the data port advances a frame — happens // once, on the low byte, and its result is held so the high byte reads // the other half of the very same word rather than fetching again. if (offset & 1 == 0) { const value = self.readPort(port); self.latch[port >> 1] = value; return @truncate(value); } return @truncate(self.latch[port >> 1] >> 8); }
pub fn write(self: *CS8900, offset: u4, value: u8) void { const high = offset & 1 != 0; const port: u4 = offset & 0xe; var current = self.latch[port >> 1]; if (high) { current = (@as(u16, value) << 8) | (current & 0x00ff); } else { current = (current & 0xff00) | value; } self.latch[port >> 1] = current; // A sixteen-bit port takes effect when its high byte is written, which // is the order a driver writes a word in on this little-endian bus. if (high) self.writePort(port, current); }
fn readPort(self: *CS8900, port: u4) u16 { return switch (port) { io_rxtx_data => self.readData(), io_isq => self.readIsq(), io_pp_pointer => self.pointer, io_pp_data => self.readPacketPage(), else => 0, }; }
fn writePort(self: *CS8900, port: u4, value: u16) void { switch (port) { io_rxtx_data => self.writeData(value), io_txcmd => self.startTransmit(), io_txlength => self.tx_length = value, io_pp_pointer => self.pointer = value, io_pp_data => self.writePacketPage(value), else => {}, } }
// -- the PacketPage window -----------------------------------------------
fn readPacketPage(self: *CS8900) u16 { const at = self.pointer & 0x0fff; const value = self.word(at); if (self.pointer & auto_increment != 0) self.pointer +%= 2; return value; }
fn writePacketPage(self: *CS8900, value: u16) void { const at = self.pointer & 0x0fff; // The product code and the status registers are the chip's to set; a // write to them is dropped rather than being allowed to forge them. if (at != pp_product_id and at != pp_product_id + 2 and at != pp_isq and at != pp_rx_event and at != pp_bus_st) self.setWord(at, value); if (self.pointer & auto_increment != 0) self.pointer +%= 2; }
// -- the receive and transmit data port ----------------------------------
/// Reading the data port streams the received frame out of its window: the /// status word first, then the length, then the frame itself. fn readData(self: *CS8900) u16 { const value = self.word(self.rx_read); self.rx_read +%= 2; // Once the driver has read the length and the whole frame, the window // is free and the interrupt is done. if (self.rx_read >= rx_frame + 4 + self.rxFrameLength()) { self.rx_ready = false; self.setWord(pp_rx_event, 0); } return value; }
fn rxFrameLength(self: *const CS8900) u16 { return self.word(rx_frame + 2); }
/// Writing the data port streams the frame to send into its window; when as /// many bytes as TxLength said have arrived, the frame is ready to go. fn writeData(self: *CS8900, value: u16) void { if (self.tx_write + 1 < page_size) self.setWord(self.tx_write, value); self.tx_write +%= 2; if (self.tx_write >= tx_frame + self.tx_length) self.tx_pending = true; }
/// A bid to transmit: the buffer is offered, and the write pointer set to /// the head of the transmit window so the frame streams in from the start. fn startTransmit(self: *CS8900) void { self.tx_write = tx_frame; self.tx_pending = false; self.setWord(pp_bus_st, bus_st_rdy4tx); }
// -- the interrupt status queue ------------------------------------------
/// The ISQ reports the next pending event as the register's number in the /// low six bits with its bits above. A received frame is an RxEvent; with /// nothing pending the queue reads as zero (§5.1). fn readIsq(self: *CS8900) u16 { if (self.rx_ready) return pp_rx_event & isq_pointer_mask | rx_event_rxok; return 0; }
// -- what the session drives ---------------------------------------------
/// The MAC the driver set in the Individual Address registers, which is /// what a received frame is filtered against and what a sent one goes out /// from. pub fn mac(self: *const CS8900) [6]u8 { return self.page[pp_ia..][0..6].*; }
/// Sets the card's hardware address, its power-on default. A driver that /// writes the Individual Address registers itself overrides it; this is /// what the card comes up with, the way a real one's is in its EEPROM. pub fn setMac(self: *CS8900, address: [6]u8) void { @memcpy(self.page[pp_ia..][0..6], &address); }
/// The frame a driver has queued to send, or nothing. The session takes it /// and then calls `didSend`. pub fn sent(self: *CS8900) ?[]const u8 { if (!self.tx_pending) return null; const len = @min(self.tx_length, @as(u16, max_frame)); return self.page[tx_frame..][0..len]; }
pub fn didSend(self: *CS8900) void { self.tx_pending = false; self.setWord(pp_tx_event, 0x0100); // TxOK }
/// Whether the receive window is free for a frame to be delivered into. A /// frame is dropped rather than overwriting one the driver has not read. pub fn canReceive(self: *const CS8900) bool { return !self.rx_ready; }
/// Delivers a received frame into the window: the status word, then the /// length, then the frame, and raises the RxEvent the driver polls the ISQ /// for. The frame includes its own CRC space is not required — the length /// is the bytes given. pub fn receive(self: *CS8900, frame: []const u8) void { if (self.rx_ready) return; const len: u16 = @intCast(@min(frame.len, @as(usize, max_frame))); self.setWord(rx_frame, rx_event_rxok | 0x0004); // RxEvent: RxOK, and the register number self.setWord(rx_frame + 2, len); @memcpy(self.page[rx_frame + 4 ..][0..len], frame[0..len]); self.rx_read = rx_frame; self.rx_ready = true; self.setWord(pp_rx_event, rx_event_rxok); }
/// Whether the receiver has been turned on and told to accept frames, which /// is how the session knows the driver is listening rather than still /// setting the chip up. The receiver is on when LineCTL's receiver-enable /// bit is set. pub fn listening(self: *const CS8900) bool { return self.word(pp_line_ctl) & 0x0040 != 0; // SerRxOn }};
// -- tests ------------------------------------------------------------------
const testing = std.testing;
/// Writes a sixteen-bit port a byte at a time, low then high, the way a driver/// on the eight-bit bus does.fn writePort(chip: *CS8900, port: u4, value: u16) void { chip.write(port, @truncate(value)); chip.write(port + 1, @truncate(value >> 8));}
fn readPort(chip: *CS8900, port: u4) u16 { const low = chip.read(port); const high = chip.read(port + 1); return @as(u16, low) | @as(u16, high) << 8;}
test "the product code identifies the chip through the PacketPage window" { var chip = CS8900.init(); // Point the window at the product code and read it back, the way a driver // checks a CS8900A is really there. writePort(&chip, io_pp_pointer, pp_product_id); try testing.expectEqual(product_id, readPort(&chip, io_pp_data));}
test "the pointer steps on when the auto-increment bit is set" { var chip = CS8900.init(); // Write the MAC through the window with auto-increment, then read it back // the same way. writePort(&chip, io_pp_pointer, pp_ia | auto_increment); writePort(&chip, io_pp_data, 0x0201); writePort(&chip, io_pp_data, 0x0403); writePort(&chip, io_pp_data, 0x0605); try testing.expectEqual([6]u8{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }, chip.mac());}
test "a frame written to the data port is offered to the session, once" { var chip = CS8900.init(); try testing.expect(chip.sent() == null);
// Bid to transmit, say how long, and stream the frame in. writePort(&chip, io_txcmd, 0x00c9); writePort(&chip, io_txlength, 4); writePort(&chip, io_rxtx_data, 0x2211); writePort(&chip, io_rxtx_data, 0x4433);
const frame = chip.sent().?; try testing.expectEqualSlices(u8, &.{ 0x11, 0x22, 0x33, 0x44 }, frame); chip.didSend(); try testing.expect(chip.sent() == null);}
test "a delivered frame is read back through the data port and the ISQ reports it" { var chip = CS8900.init(); try testing.expectEqual(@as(u16, 0), readPort(&chip, io_isq));
chip.receive("hello"); try testing.expect(!chip.canReceive()); // The ISQ names the RxEvent register and its RxOK bit. try testing.expect(readPort(&chip, io_isq) & rx_event_rxok != 0);
// The data port gives the status, then the length, then the frame. _ = readPort(&chip, io_rxtx_data); // status try testing.expectEqual(@as(u16, 5), readPort(&chip, io_rxtx_data)); // length var got: [6]u8 = undefined; for (0..3) |i| { const w = readPort(&chip, io_rxtx_data); got[i * 2] = @truncate(w); got[i * 2 + 1] = @truncate(w >> 8); } try testing.expectEqualSlices(u8, "hello", got[0..5]); // Once it is read out the window is free again. try testing.expect(chip.canReceive());}
test "the receiver is not listening until LineCTL turns it on" { var chip = CS8900.init(); try testing.expect(!chip.listening()); writePort(&chip, io_pp_pointer, pp_line_ctl); writePort(&chip, io_pp_data, 0x00d3); // SerRxOn | SerTxOn and the rest try testing.expect(chip.listening());}