// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! Apple's ProFile, the five-megabyte hard disk of 1981, on the interface //! card that put it in an Apple III's slot — modelled from the far side of //! the card, as SOS's driver drives it. //! //! The drive has a Z8 microprocessor of its own and talks to the host over an //! eight-bit parallel cable with a handshake of two lines, CMD from the host //! and BSY from the drive. The card is little more than a latch each way, an //! addressable latch for the control lines, and a status buffer; everything //! about the protocol is in SOS's ProFile driver (Apple Computer, Inc., //! 1983b), which is what this is built from and is cited by the driver's own //! page numbers: //! //! - The card's I/O, at `$C080` and up for slot 0, so `$C0C0` for slot 4: //! `WR_PORT .EQU 0C080 ;Write to Z8 RAM (byte at a time)`, `RD_PORT //! .EQU 0C081 ;Read from Z8 RAM`, `BUSY .EQU 0C082 ;Z8 not ready`, //! `CLR_PARITY .EQU 0C083 ;Clear parity error` (p. 3). In the BUSY //! register BSY is upside down in bit 7 — `WAITBSYHI` loops while `BUSY,X` //! is negative — the parity error is bit 6 (`CHKPARITY`: "get parity error //! - on bit 6"), and a set bit 0 is "No drive connected (power off)" //! (pp. 5, 6, 22). //! - The control lines, set by *reading* `$CN00` plus a code: `NOTCMD 0` / //! `SETCMD 4`, `SETWRT 1` / `SETRD 5`, `INTDSABL 2` / `INTENABL 6`, `RWLO //! 3` / `RWHI 7`, `RST 0C` / `CLRRST 8` (p. 4). Bit 2 of the code is the //! level and the other bits pick the line, which is an addressable latch: //! CMD, the direction the card's bus driver faces ("CRW"), the interrupt //! enable, the data strobe direction ("DATRW"), and the drive's reset. //! - The handshake, `SNDCMD` (p. 21): wait for BSY low, raise CMD, wait for //! BSY high, read the drive's response byte from `RD_PORT` and compare it //! with the one expected — `$01` for a command, `$02` when a read is done, //! `$03` or `$04` when a write's or write-verify's data is wanted, `$06` //! when the data has been taken — then write `$55` for yes or `$AA` for no //! to `WR_PORT`, drop CMD and wait for BSY low. //! - The command, six bytes down `WR_PORT`: "cmd, blockhi, blocklo, retries, //! retry threshold" with the block as three bytes (p. 22); the status, four //! bytes up `RD_PORT` (`GETSTAT`, p. 22), of which a set bit 0 of the //! first and bits of the second mean the operation failed; then, for a //! read, the 512 bytes of the block, and for a write the 512 bytes down //! followed by six check bytes (`TRANSDNE`, p. 15) the drive is left to //! make of what it will. //! //! The drive's own manual, the ProFile Level II Service Manual (Apple //! Computer, Inc., 1984c), is about fixing the thing rather than talking to //! it, but it settles what the driver leaves implicit: an Apple III's blocks //! are 512 bytes and a Lisa's 532 (p. 3.14), "reading block FFFFFF also //! returns status information" (p. 2.50), and the four status bytes are //! described bit by bit (pp. 2.58–2.59) — bit 0 of the first "if operation //! unsuccessful", which is the bit the driver tests, and bits 0 to 2 of the //! third "set by the SOS ProFile driver" itself for parity errors, bad //! responses and resets, which is why `GETSTAT` ORs its own flags into it. //! //! What the Z8 does between the handshakes is its own affair, and here it //! does it in no time: the drive is busy for exactly as long as the host //! takes to answer it. const std = @import("std"); const blockdevice = @import("blockdevice.zig"); /// `MAXBLOCK .EQU 2600 ;4.86 megabytes (+16K spares)` (p. 3): the blocks a /// ProFile has, and the size of an image of one. pub const blocks: u32 = 0x2600; /// The response codes, and the two answers the host gives them. pub const Response = enum(u8) { command = 0x01, read_done = 0x02, write_data = 0x03, write_verify_data = 0x04, write_done = 0x06, }; pub const accept: u8 = 0x55; pub const reject: u8 = 0xaa; /// The commands the driver sends: `WDGTRD 0`, `WDGTWRT 1`, `WDGTWRTVER 2`, /// `WDGTSTAT 3` (p. 4). pub const Command = enum(u8) { read = 0, write = 1, write_verify = 2, status = 3, _, }; /// The five lines behind the addressable latch, by the latch address the /// control code's bits 0, 1 and 3 make. pub const Line = enum(u3) { cmd = 0, direction = 1, interrupt = 2, strobe = 3, reset = 4, _, }; /// The first status byte's bit 0 is the drive's own error flag — "0 = 1 if /// operation unsuccessful" (Apple Computer, Inc., 1984c, p. 2.58) — and the /// bit the driver tests: `LDA STATUS1; AND #1; BNE LDAXIO` (1983b, p. 12). const status_error: u8 = 0x01; pub const Card = struct { /// The disk on the end of the cable, or nothing. volume: ?*blockdevice.Volume = null, /// Where the conversation is. phase: Phase = .idle, /// What the drive answers when CMD is next raised. response: Response = .command, /// The host's answer to the response, if it has given one this handshake. answer: ?u8 = null, /// The control lines as the latch holds them. cmd: bool = false, reading: bool = true, interrupts: bool = false, reset: bool = false, /// The six command bytes as they arrive. command: [6]u8 = @splat(0), received: u8 = 0, /// The block the command named, and what it asked for. block: u32 = 0, request: Command = .read, /// The bytes going up or down the cable: the status and the block for a /// read, the block and its check bytes for a write. buffer: [4 + blockdevice.block_size + 6]u8 = @splat(0), index: u16 = 0, length: u16 = 0, /// The four status bytes the drive reports after an operation. status: [4]u8 = @splat(0), parity_error: bool = false, pub const Phase = enum { /// BSY low, waiting for CMD. idle, /// CMD is up, BSY is up, the response byte is on offer. responding, /// The handshake said yes to a command; six bytes are expected. command, /// The drive has the block and is handing it up, status first. data_out, /// The drive is taking a block down, check bytes last. data_in, }; /// Whether anything is on the end of the cable. pub fn fitted(self: *const Card) bool { return self.volume != null; } /// Whether BSY is up, which it is only while a handshake is in /// progress: the driver waits for it to drop before it sends the /// command bytes and before it moves the data (`BSYACK`, p. 23). pub fn busy(self: *const Card) bool { return self.phase == .responding; } /// The BUSY register: BSY inverted in bit 7, the parity error in bit 6, /// and bit 0 set when there is no drive at all — `TYA; AND #1; BNE /// NODRV` at the driver's entry (p. 5). The other bits are the drive's /// to say and the driver's to ignore. pub fn statusRegister(self: *const Card) u8 { if (!self.fitted()) return 0x81; var out: u8 = 0; if (!self.busy()) out |= 0x80; if (self.parity_error) out |= 0x40; return out; } /// `RD_PORT`: the response byte during a handshake, the next byte of the /// status and block during a read, nothing otherwise. pub fn readPort(self: *Card) u8 { if (!self.fitted()) return 0xff; switch (self.phase) { .responding => return @intFromEnum(self.response), .data_out => { if (self.index >= self.length) return 0; const byte = self.buffer[self.index]; self.index += 1; // The last byte up is the end of the operation: the next // handshake is for a new command. if (self.index == self.length) self.response = .command; return byte; }, else => return 0, } } /// `WR_PORT`: the host's answer during a handshake, a command byte after /// one, a data byte during a write. pub fn writePort(self: *Card, value: u8) void { if (!self.fitted()) return; switch (self.phase) { .responding => self.answer = value, .command => { self.command[self.received] = value; self.received += 1; if (self.received == 6) self.takeCommand(); }, .data_in => { if (self.index < self.length) { self.buffer[self.index] = value; self.index += 1; } if (self.index == self.length) self.finishWrite(); }, else => {}, } } /// A control code, as the driver reads `$CN00` plus it: bit 2 is the /// level, bits 0, 1 and 3 the line. pub fn control(self: *Card, code: u8) void { const line: Line = @enumFromInt(@as(u3, @truncate((code & 0x03) | (code & 0x08) >> 1))); const high = code & 0x04 != 0; switch (line) { .cmd => self.setCmd(high), .direction => self.reading = high, .interrupt => self.interrupts = high, .strobe => {}, .reset => { self.reset = high; if (high) self.resetDrive(); }, _ => {}, } } /// `CLR_PARITY`, read or written. pub fn clearParity(self: *Card) void { self.parity_error = false; } /// The CMD line. Up, the drive answers with BSY and its response byte; /// down again, it acts on the host's answer — yes takes the conversation /// on to the next phase, no or nothing puts it back to idle — and drops /// BSY. fn setCmd(self: *Card, high: bool) void { if (high == self.cmd) return; self.cmd = high; if (!self.fitted()) return; if (high) { // Whatever the drive was doing, CMD up is a handshake, and the // response is where it stands: a fresh command after a finished // transfer, the data or status handshake in the middle of one. if (self.phase != .responding) { self.phase = .responding; self.answer = null; } return; } if (self.phase != .responding) return; const answered = self.answer orelse reject; self.answer = null; if (answered != accept) { self.phase = .idle; self.response = .command; return; } switch (self.response) { .command => { self.phase = .command; self.received = 0; }, .read_done => { // The status and the block go up, status first. self.phase = .data_out; self.index = 0; }, .write_data, .write_verify_data => { self.phase = .data_in; self.index = 0; self.length = blockdevice.block_size + 6; }, .write_done => { // The status goes up, and that is the end of it. self.phase = .data_out; self.index = 0; self.length = 4; }, } } /// Six bytes are in: `cmd, blockhi, blocklo, retries, retry threshold` /// with the block in three bytes, most significant first. fn takeCommand(self: *Card) void { self.request = @enumFromInt(self.command[0]); self.block = @as(u32, self.command[1]) << 16 | @as(u32, self.command[2]) << 8 | self.command[3]; self.status = @splat(0); switch (self.request) { .read => { self.fillFromBlock(); @memcpy(self.buffer[0..4], &self.status); self.length = 4 + blockdevice.block_size; self.response = .read_done; self.phase = .idle; }, .write => { self.response = .write_data; self.phase = .idle; }, .write_verify => { self.response = .write_verify_data; self.phase = .idle; }, // Anything else the drive does not know, including the `$FF` the // driver sends on its way out "so the ready light doesn't go // out" (p. 13): back to the beginning. else => { self.response = .command; self.phase = .idle; }, } } /// The block's bytes, or the drive's status block for the two block /// numbers past the end that mean that — `CMP #0FE ;RAM or status /// block?` (p. 9) — or an error for a block that is not there. fn fillFromBlock(self: *Card) void { const data = self.buffer[4..][0..blockdevice.block_size]; @memset(data, 0); if (self.block >= 0xfffffe) { self.statusBlock(data); return; } const volume = self.volume.?; if (self.block >= volume.image.len / blockdevice.block_size) { self.status[0] |= status_error; return; } @memcpy(data, volume.at(@intCast(self.block)).?); } /// The drive's own table, as read back at block `$FFFFFF` — "reading /// block FFFFFF also returns status information" (Apple Computer, Inc., /// 1984c, p. 2.50): its name, device number, firmware revision, size in /// blocks and bytes per block, then its spare and bad block tables, /// empty. The manual says the block exists and not how it is laid out; /// the layout here is the one the Lisa's ProFile software reads, and /// SOS's driver never asks for it. fn statusBlock(self: *const Card, data: *[blockdevice.block_size]u8) void { const volume = self.volume.?; const count: u32 = @intCast(volume.image.len / blockdevice.block_size); @memcpy(data[0..13], "PROFILE "); data[13] = 0x00; // device number, three bytes: a 5 MB ProFile data[14] = 0x00; data[15] = 0x00; data[16] = 0x03; // firmware revision data[17] = 0x98; data[18] = @truncate(count >> 16); data[19] = @truncate(count >> 8); data[20] = @truncate(count); data[21] = 0x02; // bytes per block, 532 data[22] = 0x14; } /// The block and its check bytes are all down: write it, if the disk /// allows, and offer the status. fn finishWrite(self: *Card) void { const volume = self.volume.?; if (self.block >= volume.image.len / blockdevice.block_size or volume.write_protected) { self.status[0] |= status_error; } else { @memcpy(volume.at(@intCast(self.block)).?, self.buffer[0..blockdevice.block_size]); volume.dirty = true; } @memcpy(self.buffer[0..4], &self.status); self.response = .write_done; self.phase = .idle; } /// Whether a byte would move if the host let the card have the bus: the /// pseudo-DMA of the driver's `DATRANS` (p. 14), which runs the ROM's /// page of `SBC #1 / BEQ` while the card moves one byte per cycle to or /// from the page the zero-page register names. pub fn transferring(self: *const Card) bool { return switch (self.phase) { .data_out, .data_in => self.index < self.length, else => false, }; } /// Whether that byte goes from the drive into memory. pub fn transferringOut(self: *const Card) bool { return self.phase == .data_out; } /// The slot's reset line: the card's latch drops every line and the /// drive starts over, and the disk stays on the cable. pub fn resetCard(self: *Card) void { self.* = .{ .volume = self.volume }; } /// The drive's reset line: back to idle, whatever was going on. fn resetDrive(self: *Card) void { self.phase = .idle; self.response = .command; self.answer = null; self.received = 0; self.index = 0; self.length = 0; self.parity_error = false; } }; const testing = std.testing; /// The driver's handshake, as a test does it. fn handshake(card: *Card, expected: Response) !void { try testing.expect(!card.busy()); card.control(0x04); // SETCMD try testing.expect(card.busy()); try testing.expectEqual(@intFromEnum(expected), card.readPort()); card.writePort(accept); card.control(0x00); // NOTCMD } test "a block is read the way SOS's driver reads it: two handshakes, six bytes down, status and block up" { var image: [16 * blockdevice.block_size]u8 = @splat(0); for (image[3 * blockdevice.block_size ..][0..blockdevice.block_size], 0..) |*b, i| b.* = @truncate(i * 3); var volume: blockdevice.Volume = .{ .image = &image, .write_protected = false }; var card: Card = .{ .volume = &volume }; try testing.expectEqual(@as(u8, 0x80), card.statusRegister()); try handshake(&card, .command); try testing.expect(!card.busy()); for ([_]u8{ @intFromEnum(Command.read), 0x00, 0x00, 0x03, 0x0a, 0x03 }) |b| card.writePort(b); try handshake(&card, .read_done); // Four clean status bytes, then the block. for (0..4) |_| try testing.expectEqual(@as(u8, 0), card.readPort()); try testing.expect(card.transferring()); for (0..blockdevice.block_size) |i| try testing.expectEqual(@as(u8, @truncate(i * 3)), card.readPort()); try testing.expect(!card.transferring()); // The driver's way out: a dummy handshake, an invalid command, CMD up // and down with no answer. The drive is idle at the end of it. try handshake(&card, .command); for ([_]u8{ 0xff, 0, 0, 0, 0, 0 }) |b| card.writePort(b); card.control(0x04); card.control(0x00); try testing.expect(!card.busy()); try testing.expectEqual(Response.command, card.response); } test "a block is written with three handshakes, and the disk keeps it" { var image: [16 * blockdevice.block_size]u8 = @splat(0); var volume: blockdevice.Volume = .{ .image = &image, .write_protected = false }; var card: Card = .{ .volume = &volume }; try handshake(&card, .command); for ([_]u8{ @intFromEnum(Command.write), 0x00, 0x00, 0x05, 0x00, 0x00 }) |b| card.writePort(b); try handshake(&card, .write_data); for (0..blockdevice.block_size) |i| card.writePort(@truncate(i ^ 0x5a)); // The six check bytes, which the drive takes and does nothing with. for (0..6) |_| card.writePort(0); try testing.expect(!card.busy()); try handshake(&card, .write_done); for (0..4) |_| try testing.expectEqual(@as(u8, 0), card.readPort()); try testing.expectEqual(@as(u8, 7 ^ 0x5a), image[5 * blockdevice.block_size + 7]); try testing.expect(volume.dirty); // A write-protected disk takes nothing and says so in the status. volume.write_protected = true; try handshake(&card, .command); for ([_]u8{ @intFromEnum(Command.write), 0x00, 0x00, 0x06, 0x00, 0x00 }) |b| card.writePort(b); try handshake(&card, .write_data); for (0..blockdevice.block_size + 6) |_| card.writePort(0xee); try handshake(&card, .write_done); try testing.expectEqual(status_error, card.readPort() & status_error); try testing.expectEqual(@as(u8, 0), image[6 * blockdevice.block_size]); } test "a rejected response, a bad block and a reset all put the drive back to idle" { var image: [16 * blockdevice.block_size]u8 = @splat(0); var volume: blockdevice.Volume = .{ .image = &image }; var card: Card = .{ .volume = &volume }; // The host says no. card.control(0x04); try testing.expectEqual(@intFromEnum(Response.command), card.readPort()); card.writePort(reject); card.control(0x00); try testing.expect(!card.busy()); // A block past the end reads as an error in the status. try handshake(&card, .command); for ([_]u8{ 0x00, 0x00, 0x10, 0x00, 0x00, 0x00 }) |b| card.writePort(b); try handshake(&card, .read_done); try testing.expectEqual(status_error, card.readPort() & status_error); // And the reset line, mid-transfer. card.control(0x0c); try testing.expect(!card.busy()); try testing.expect(!card.transferring()); card.control(0x08); // With nothing on the cable the status register says so. card.volume = null; try testing.expectEqual(@as(u8, 0x81), card.statusRegister()); }