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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! A ProDOS block device: a card that reads and writes whole 512 byte blocks.//!//! This is the easy kind of disk. A Disk II hands the machine a stream of bits//! and leaves it to work out where the sectors are; a block device is asked for//! block 1234 and produces it, so there is no encoding, no head to move and no//! timing to get right. It is also how anything larger than a floppy was ever//! attached to one of these machines.//!//! The card is its own firmware. Unlike the Disk II and the serial card, whose//! ROMs are Apple's, the protocol here is published and short enough to write,//! so the firmware below is ours and the card works with nothing but an image//! file. ProDOS finds it by four bytes in the card's page, calls the entry//! point the last byte of that page points at, and passes what it wants in the//! zero page.//!//! See the ProDOS 8 Technical Reference Manual for the protocol (Apple//! Computer, Inc., n.d.-d, Chapter 6).
const std = @import("std");
/// A block is always this. It is two of the 256 byte sectors a floppy uses,/// which is why a ProDOS floppy is 280 blocks rather than 560 sectors.pub const block_size = 512;
/// What ProDOS puts in the zero page before it calls the driver.pub const zero_page = struct { pub const command = 0x42; pub const unit = 0x43; pub const buffer = 0x44; // and 0x45 pub const block = 0x46; // and 0x47};
/// The four things a driver can be asked to do.pub const Command = enum(u8) { status = 0, read = 1, write = 2, format = 3, _,};
/// The SmartPort commands. The first six are a block device's; the last four/// belong to a character device, which this is not.////// A command with bit 6 set is the extended form, which takes four-byte/// addresses instead of two. This card does not offer them and does not claim/// to: $CnFB is where a card says it is an extended SmartPort, the IIgs's own/// 3.5-inch firmware puts $80 there, and this one leaves it at nought.pub const SmartPort = enum(u8) { status = 0x00, read_block = 0x01, write_block = 0x02, format = 0x03, control = 0x04, init = 0x05, open = 0x06, close = 0x07, read = 0x08, write = 0x09, _,};
/// What a SmartPort call answers with. Nought is success and the carry comes/// back clear; anything else sets it.pub const SmartPortResult = enum(u8) { none = 0x00, /// A command this device does not do. bad_command = 0x01, /// The parameter list does not begin with the number of parameters the /// command takes, which is the first thing every call checks. bad_parameter_count = 0x04, /// No such unit. Nought is the interface itself and only a status call /// may name it; this card has two drives after that. bad_unit = 0x11, /// A status or control code this device has nothing to say about. bad_code = 0x21, io = 0x27, write_protected = 0x2b, /// The drive is there and empty, which is not the same as not being there. offline = 0x2f,};
/// What a device says about itself in the top byte of a status reply.////// The Apple 3.5-inch drive's answer through the IIgs's own firmware is the/// model for the shape of this; which bits mean what is the SmartPort/// protocol's.pub const status_block_device: u8 = 0x80;pub const status_write_allowed: u8 = 0x40;pub const status_read_allowed: u8 = 0x20;pub const status_online: u8 = 0x10;pub const status_format_allowed: u8 = 0x08;pub const status_write_protected: u8 = 0x04;pub const status_interrupting: u8 = 0x02;pub const status_open: u8 = 0x01;
/// The errors ProDOS knows about, of the ones this can produce.pub const Failure = enum(u8) { none = 0x00, /// Something went wrong reading or writing. io = 0x27, /// There is no such device: nothing in the drive. offline = 0x28, /// The volume is write protected. protected = 0x2b,};
/// The card's registers, in the sixteen addresses its slot answers at.////// Reading `execute` does the whole transfer and gives back an error code,/// which is the trick that makes the firmware short: the card moves the five/// hundred and twelve bytes itself, as a real one does with its own hardware,/// rather than the processor copying them a byte at a time.pub const Register = enum(u3) { execute = 0, /// The same for a SmartPort call, which the card does the whole of: the /// firmware hands over where the three bytes after the caller's JSR are /// and this reads back an error code. smartport = 1, command = 2, unit = 3, buffer_low = 4, buffer_high = 5, block_low = 6, block_high = 7,};
/// A volume: an image file, held as blocks.pub const Volume = struct { /// The whole image. Its length in blocks is what the machine is told when /// it asks after the device. image: []u8, /// Whether the machine may write to it. write_protected: bool = true, /// Whether anything has been written since it was loaded. dirty: bool = false,
/// How many whole blocks there are, capped at what ProDOS can address: /// the block number it passes is sixteen bits, so thirty-two megabytes is /// the most any one volume can be. pub fn blocks(self: Volume) u16 { const whole = self.image.len / block_size; return @intCast(@min(whole, 0xffff)); }
pub fn at(self: Volume, block: u16) ?[]u8 { const start = @as(usize, block) * block_size; if (start + block_size > self.image.len) return null; return self.image[start..][0..block_size]; }};
/// The card itself, which is four registers and two drives.////// Two, because ProDOS counts in drives per slot and every real controller/// has both: a slot answers for `,S7,D1` and `,S7,D2`, and a card with only/// one of them is the odd one out. It is not a detail that can be waved away/// either. ProDOS 8's own startup, having found the volume it booted from,/// writes *two* entries into its device list for the booting slot — the unit/// it booted and that unit with bit 7 flipped — and unstacks what it pushed on/// the assumption that it found both. A card declaring one volume leaves that/// arithmetic short by one, and the return that follows lands in the weeds./// With one drive it boots an Apple IIe by luck and a IIgs not at all; with/// two it boots both, which is what settled the question.////// A drive with nothing in it is offline, which is what a controller with one/// disk attached says about the other.pub const Card = struct { volumes: [2]?*Volume = @splat(null), /// How many transfers have been asked for. A real card has a light on it /// that flickers while the heads are busy, and this is what a front end /// needs to draw one: the number changing between one frame and the next /// is the light being on. accesses: u64 = 0, command: u8 = 0, unit: u8 = 0, buffer: u16 = 0, block: u16 = 0,
pub fn write(self: *Card, register: Register, value: u8) void { switch (register) { .command => self.command = value, .unit => self.unit = value, .buffer_low => self.buffer = (self.buffer & 0xff00) | value, .buffer_high => self.buffer = (self.buffer & 0x00ff) | (@as(u16, value) << 8), .block_low => self.block = (self.block & 0xff00) | value, .block_high => self.block = (self.block & 0x00ff) | (@as(u16, value) << 8), .execute, .smartport => {}, } }
/// What a read of one of the registers gives back, other than execute, /// which the machine has to do the transfer for. /// Whether there is a card in the slot at all, which is whether either /// drive has anything in it. An empty controller is not a controller: its /// page reads as nothing and the boot scan goes past. pub fn fitted(self: *const Card) bool { return self.volumes[0] != null or self.volumes[1] != null; }
/// Which drive the unit register names. ProDOS numbers a unit `DSSS0000`, /// so the top bit is the drive and the rest is the slot the caller already /// knows. pub fn chosen(self: *const Card) ?*Volume { return self.volumes[self.unit >> 7]; }
pub fn read(self: *const Card, register: Register) u8 { return switch (register) { .command => self.command, .unit => self.unit, .buffer_low => @truncate(self.buffer), .buffer_high => @truncate(self.buffer >> 8), .block_low => @truncate(self.block), .block_high => @truncate(self.block >> 8), .execute, .smartport => 0, }; }};
/// What the card was asked to do through ProDOS's own interface, done.////// The bytes move straight between the image and the machine's memory without/// the processor spending a cycle on them, which is what a real card does with/// its own hardware and why a block device is so much faster than a floppy./// `machine` is whatever the card is fitted to, and has to offer `blockGet`/// and `blockPut`: one byte of its memory in and one out.pub fn run(card: *Card, machine: anytype) u8 { card.accesses +%= 1; const volume = card.chosen() orelse return @intFromEnum(Failure.offline); switch (@as(Command, @enumFromInt(card.command))) { .status => { card.block = volume.blocks(); return 0; }, .read => return @intFromEnum(readInto(machine, volume, card.block, card.buffer)), .write => return @intFromEnum(writeFrom(machine, volume, card.block, card.buffer)), .format => return 0, _ => return @intFromEnum(Failure.io), }}
fn readInto(machine: anytype, volume: *Volume, block: u16, buffer: u16) Failure { const data = volume.at(block) orelse return .io; for (data, 0..) |byte, i| machine.blockPut(buffer +% @as(u16, @intCast(i)), byte); return .none;}
fn writeFrom(machine: anytype, volume: *Volume, block: u16, buffer: u16) Failure { if (volume.write_protected) return .protected; const data = volume.at(block) orelse return .io; for (data, 0..) |*byte, i| byte.* = machine.blockGet(buffer +% @as(u16, @intCast(i))); volume.dirty = true; return .none;}
/// A SmartPort call, which the card does the whole of.////// The firmware's entry point is twenty-odd bytes and can only manage the part/// that has to be done on the stack: it takes the return address the caller's/// JSR left, which points at the three bytes after it — a command and a/// pointer to its parameters — and hands that over in the buffer registers./// Everything else is here. The card reads those three bytes itself, does the/// work, puts the return address back three further on so the firmware's RTS/// steps over them, leaves the number of bytes moved in the block registers/// for the X and Y the protocol asks for, and leaves the error where the/// command register was so the firmware can pick it up without asking for the/// call to be made twice.pub fn smartPort(card: *Card, machine: anytype) u8 { card.accesses +%= 1; const at = card.buffer; const command: SmartPort = @enumFromInt(machine.blockGet(at +% 1)); const list: u16 = @as(u16, machine.blockGet(at +% 2)) | (@as(u16, machine.blockGet(at +% 3)) << 8); card.buffer = at +% 3; card.block = 0;
const result = call(card, machine, command, list); card.command = @intFromEnum(result); return card.command;}
fn call(card: *Card, machine: anytype, command: SmartPort, list: u16) SmartPortResult { const parameters = machine.blockGet(list); switch (command) { .status => { if (parameters != 3) return .bad_parameter_count; const unit = machine.blockGet(list +% 1); const into: u16 = @as(u16, machine.blockGet(list +% 2)) | (@as(u16, machine.blockGet(list +% 3)) << 8); return status(card, machine, unit, into, machine.blockGet(list +% 4)); }, .read_block, .write_block => { if (parameters != 3) return .bad_parameter_count; const drive = unitDrive(machine.blockGet(list +% 1)) orelse return .bad_unit; const buffer: u16 = @as(u16, machine.blockGet(list +% 2)) | (@as(u16, machine.blockGet(list +% 3)) << 8); // Three bytes of block number, of which this card can only reach // the low two: a volume is capped at what ProDOS can address. const block: u32 = @as(u32, machine.blockGet(list +% 4)) | (@as(u32, machine.blockGet(list +% 5)) << 8) | (@as(u32, machine.blockGet(list +% 6)) << 16); const volume = card.volumes[drive] orelse return .offline; if (block > 0xffff) return .io; const failure = if (command == .read_block) readInto(machine, volume, @intCast(block), buffer) else writeFrom(machine, volume, @intCast(block), buffer); if (failure != .none) return switch (failure) { .protected => .write_protected, else => .io, }; card.block = block_size; return .none; }, // Nothing to do, and saying so is the truthful answer: the image is // already as many blocks as it is going to be, and a card with no // state to clear has nothing to put back. .format, .init => { if (parameters != 1) return .bad_parameter_count; const drive = unitDrive(machine.blockGet(list +% 1)) orelse return .bad_unit; if (card.volumes[drive] == null) return .offline; return .none; }, .control => { if (parameters != 3) return .bad_parameter_count; const unit = machine.blockGet(list +% 1); if (unit != 0 and unitDrive(unit) == null) return .bad_unit; // Nought is reset, which this has nothing to reset. The rest are // a device's own and this device has none. return if (machine.blockGet(list +% 4) == 0) .none else .bad_code; }, // The four that belong to a character device, and the extended forms, // which have bit 6 set and four-byte addresses. This is a block device // and does not claim to be an extended one. .open, .close, .read, .write => return .bad_command, _ => return .bad_command, }}
/// Which of the card's drives a unit number names, or none if it names/// something this card has not got. Unit nought is the interface itself.fn unitDrive(unit: u8) ?u1 { return switch (unit) { 1 => 0, 2 => 1, else => null, };}
/// How many drives the card answers for, which is both of them whether or not/// there is anything in them: a drive with no disk is offline, and offline is/// something a drive says rather than something it fails to be.pub const smartport_devices = 2;
/// What the card calls itself, and what kind of thing it says it is.////// The shape is the IIgs's own, read out of its ROM at `$ff58fd`, where the/// constant part of the 3.5-inch drive's information block sits: a length, a/// sixteen character name padded with spaces, a type, a subtype and two bytes/// of version. Its answer is `08 "DISK 3.5 " 01 c0 00 10`, which is/// what says that $01 is the type of an Apple 3.5-inch disk and that the/// subtype's top two bits are the extended and removable flags — the IIgs's/// SmartPort is extended and its disks come out, and both are set.////// This card is neither, so its subtype is nought. Its *type* is a choice/// rather than a fact: $01 is the only code this project can point at an/// oracle for, and $02 is the ProFile-shaped hard disk by the usual reckoning,/// which is what this is. Nothing that has been run against it looks.pub const smartport_name = "HARD DISK";pub const smartport_type: u8 = 0x02;pub const smartport_subtype: u8 = 0x00;pub const smartport_version = [2]u8{ 0x00, 0x10 };
fn status(card: *Card, machine: anytype, unit: u8, into: u16, code: u8) SmartPortResult { // Unit nought is the interface rather than a drive, and the only thing to // say about it is how many drives there are behind it. if (unit == 0) { if (code != 0) return .bad_code; machine.blockPut(into, smartport_devices); // No interrupt, no vendor, version nought: eight bytes, of which this // card only has anything to put in the first. for (1..8) |i| machine.blockPut(into +% @as(u16, @intCast(i)), 0); card.block = 8; return .none; }
const drive = unitDrive(unit) orelse return .bad_unit; const volume = card.volumes[drive]; var general: u8 = status_block_device | status_read_allowed; var blocks: u16 = 0; if (volume) |disk| { general |= status_online; if (!disk.write_protected) general |= status_write_allowed else general |= status_write_protected; blocks = disk.blocks(); } else { general |= status_write_allowed; }
switch (code) { // The device's own status: what it will do, and how big it is. 0x00 => { machine.blockPut(into, general); machine.blockPut(into +% 1, @truncate(blocks)); machine.blockPut(into +% 2, @truncate(blocks >> 8)); machine.blockPut(into +% 3, 0); card.block = 4; return .none; }, // The information block: the same, and then what the device calls // itself. 0x03 => { machine.blockPut(into, general); machine.blockPut(into +% 1, @truncate(blocks)); machine.blockPut(into +% 2, @truncate(blocks >> 8)); machine.blockPut(into +% 3, 0); machine.blockPut(into +% 4, smartport_name.len); for (0..16) |i| { const letter: u8 = if (i < smartport_name.len) smartport_name[i] else ' '; machine.blockPut(into +% 5 +% @as(u16, @intCast(i)), letter); } machine.blockPut(into +% 21, smartport_type); machine.blockPut(into +% 22, smartport_subtype); machine.blockPut(into +% 23, smartport_version[0]); machine.blockPut(into +% 24, smartport_version[1]); card.block = 25; return .none; }, else => return .bad_code, }}
/// The card's firmware, which answers at $Cn00.////// Four of these bytes are what ProDOS looks for, and they are the low halves/// of instructions rather than data, which is the same trick the Disk II's own/// PROM plays: $Cn01 is $20, $Cn03 is $00 and $Cn05 is $03 say "a block/// device", and $Cn07 says which kind. The last two bytes of the page are the/// rest of what ProDOS wants: what the device can do, and where to call it.////// $Cn07 is the interesting one, and it cost a while to get right. $3c there/// is a Disk II, which this is not. $00 is a *SmartPort*, which this is also/// not — and that is not a reading of the manuals but of the machines: the/// IIgs's own 3.5-inch firmware in slot 5 has $00 there, and so does the IIc's,/// and so does every real card to hand, all of which answer SmartPort calls at/// three bytes past their ProDOS entry point. ProDOS 8 believes them. From/// version 1.9 on it makes a SmartPort STATUS call to `$CnFF + 3` as soon as/// it has finished its first ProDOS call to a device that claims it, and a/// card with nothing there sends the machine into whatever the page holds/// after its driver — which for this one was zeroes, and a IIgs into its/// monitor after ProDOS had printed its banner.////// So this card does not claim to be one. It is a block device and says so,/// and $Cn07 holds a value that is neither of the two kinds it is not. What it/// really ought to grow is a SmartPort interface, since that is what a card of/// this sort had by 1986 and what GS/OS would rather talk to; until it does,/// saying so would be a lie that ProDOS calls.////// The slot is baked into the page, so there is a page for every slot: see/// `firmwares` below, which is what a machine indexes.pub fn firmware(comptime slot: u4) [256]u8 { const io = 0xc080 + @as(u16, slot) * 0x10; const lo: u8 = @truncate(io); const hi: u8 = @truncate(io >> 8); const page: u8 = 0xc0 + @as(u8, slot); const boot_at = 0x20; const driver_at = 0x50; const smartport_at = 0x80;
var rom: [256]u8 = @splat(0);
// $Cn00. The four bytes ProDOS reads are the immediate halves of these, // which is the same trick the Disk II's own PROM plays with its signature. const head = [_]u8{ 0xa2, 0x20, // LDX #$20 0xa0, 0x00, // LDY #$00 0xa2, 0x03, // LDX #$03 0xa9, 0x00, // LDA #$00 $Cn07: a SmartPort, which // a Disk II ($3c) is not 0x4c, boot_at, page, // JMP boot // $Cn0b, which is where $CnFF points and what ProDOS calls. 0x4c, driver_at, page, // JMP driver // $Cn0e, three bytes further on, which is where SmartPort is called // and why the two above it are jumps rather than the routines. 0x4c, smartport_at, page, // JMP smartport }; @memcpy(rom[0..head.len], &head);
// Booting: read block nought into $0800 and jump into it, which is what // every ProDOS volume expects. Its first block is its boot loader, and it // wants the slot in X. const boot = [_]u8{ 0xa9, 0x01, 0x8d, lo + 2, hi, // LDA #$01 : STA command read 0xa9, 0x00, 0x8d, lo + 3, hi, // LDA #$00 : STA unit 0x8d, lo + 4, hi, // STA buffer low $00 0x8d, lo + 6, hi, // STA block low block 0 0x8d, lo + 7, hi, // STA block high 0xa9, 0x08, 0x8d, lo + 5, hi, // LDA #$08 : STA buffer high $0800 0xad, lo, hi, // LDA execute 0xd0, 0x05, // BNE nothing no volume 0xa2, @as(u8, slot) * 0x10, // LDX #$n0 0x4c, 0x01, 0x08, // JMP $0801 0x4c, 0x00, 0xe0, // nothing: JMP $e000 back to BASIC }; @memcpy(rom[boot_at..][0..boot.len], &boot);
// The driver. Copy what ProDOS left in the zero page into the card, tell // it to go, and turn the error code into the carry flag ProDOS reads the // answer from. A status call also wants the size, which comes back in the // block registers. const driver = [_]u8{ 0xa5, zero_page.command, 0x8d, lo + 2, hi, // LDA $42 : STA command 0xa5, zero_page.unit, 0x8d, lo + 3, hi, // LDA $43 : STA unit 0xa5, zero_page.buffer, 0x8d, lo + 4, hi, // LDA $44 : STA buffer low 0xa5, zero_page.buffer + 1, 0x8d, lo + 5, hi, // LDA $45 : STA buffer high 0xa5, zero_page.block, 0x8d, lo + 6, hi, // LDA $46 : STA block low 0xa5, zero_page.block + 1, 0x8d, lo + 7, hi, // LDA $47 : STA block high 0xad, lo, hi, // LDA execute 0xd0, 0x08, // BNE failed 0xae, lo + 6, hi, // LDX block low how many blocks 0xac, lo + 7, hi, // LDY block high 0x18, // CLC it worked 0x60, // RTS 0x38, // failed: SEC 0x60, // RTS }; @memcpy(rom[driver_at..][0..driver.len], &driver);
// The SmartPort entry. Everything a hand-written routine has to do here is // stack work the card cannot do for itself: take the return address the // caller's JSR left, which points at the three bytes after it, and put it // back three further on so the RTS steps over them. The call itself is the // card's, and so is working out what those three bytes said. const smartport = [_]u8{ 0x68, // PLA where the three 0x8d, lo + 4, hi, // STA buffer low bytes are, less one 0x68, // PLA 0x8d, lo + 5, hi, // STA buffer high 0xad, lo + 1, hi, // LDA smartport do it 0xad, lo + 5, hi, // LDA buffer high the return, moved on 0x48, // PHA 0xad, lo + 4, hi, // LDA buffer low 0x48, // PHA 0xae, lo + 6, hi, // LDX block low bytes moved, which 0xac, lo + 7, hi, // LDY block high is what X and Y are 0xad, lo + 2, hi, // LDA command where the error is 0xc9, 0x01, // CMP #$01 carry set unless 0x60, // RTS it worked }; @memcpy(rom[smartport_at..][0..smartport.len], &smartport);
comptime { // The regions must not run into one another, which is the one thing // hand written machine code will do silently. std.debug.assert(head.len <= boot_at); std.debug.assert(boot_at + boot.len <= driver_at); std.debug.assert(driver_at + driver.len <= smartport_at); std.debug.assert(smartport_at + smartport.len <= 0xfe); // The SmartPort entry has to be exactly three bytes past the ProDOS // one, which is what makes the pair at the end of the head jumps // rather than the routines themselves. std.debug.assert(head[0x0b] == 0x4c and head[0x0e] == 0x4c); }
// What the device can do: status, read and write, two volumes, and it is // not removable. The ProDOS 8 Technical Reference on the driver's $CnFE // (Apple Computer, Inc., n.d.-d, Section 6.3): bits 5 and 4 are the number // of volumes less one, so $10 is the second drive and $07 the three things // it will do. rom[0xfe] = 0x17; // And where to call it. rom[0xff] = 0x0b; return rom;}
/// One page for each slot, since a card's ROM has its own slot's addresses/// assembled into it and which slot that is cannot be known until a machine is/// built. Indexed by the slot number, so there are eight of them and the first/// is never read: slot 0 is not a slot, and two hundred and fifty-six wasted/// bytes are cheaper than an off-by-one waiting to happen.pub const firmwares: [8][256]u8 = blk: { var all: [8][256]u8 = undefined; for (0..all.len) |slot| all[slot] = firmware(@intCast(slot)); break :blk all;};
const testing = std.testing;
test "a volume is measured in whole blocks" { var image: [block_size * 4 + 3]u8 = @splat(0); const volume: Volume = .{ .image = &image }; try testing.expectEqual(@as(u16, 4), volume.blocks()); try testing.expect(volume.at(0) != null); try testing.expect(volume.at(3) != null); // The odd bytes on the end are not a block, and neither is anything past // the end of the image. try testing.expect(volume.at(4) == null); try testing.expect(volume.at(9999) == null);}
test "the firmware says what ProDOS looks for" { const rom = firmware(7); // The four bytes that say what kind of device this is: a block device, // and a SmartPort rather than a Disk II, which has $3c in the last of // them where this has $00. try testing.expectEqual(@as(u8, 0x20), rom[1]); try testing.expectEqual(@as(u8, 0x00), rom[3]); try testing.expectEqual(@as(u8, 0x03), rom[5]); try testing.expectEqual(@as(u8, 0x00), rom[7]); // And the pair of jumps that put the SmartPort entry exactly three bytes // past the ProDOS one. try testing.expectEqual(@as(u8, 0x4c), rom[0x0b]); try testing.expectEqual(@as(u8, 0x4c), rom[0x0e]); // Read, write and status, and two drives: bits 5 and 4 are the number of // volumes less one. try testing.expectEqual(@as(u8, 0x17), rom[0xfe]); // And the entry point, which is a jump. try testing.expectEqual(@as(u8, 0x0b), rom[0xff]); try testing.expectEqual(@as(u8, 0x4c), rom[0x0b]);}
test "the card holds what it was asked for" { var card: Card = .{}; card.write(.command, 2); card.write(.unit, 0x70); card.write(.buffer_low, 0x00); card.write(.buffer_high, 0x20); card.write(.block_low, 0x34); card.write(.block_high, 0x12); try testing.expectEqual(@as(u8, 2), card.command); try testing.expectEqual(@as(u16, 0x2000), card.buffer); try testing.expectEqual(@as(u16, 0x1234), card.block); try testing.expectEqual(@as(u8, 0x34), card.read(.block_low)); try testing.expectEqual(@as(u8, 0x12), card.read(.block_high));}
test "a card has two drives, and an empty one is offline" { // ProDOS counts in drives per slot, and the card says it has both so that // `,S7,D2` is a place a second image can go — and so that ProDOS's own // startup, which writes two device-list entries for the slot it booted // from, finds what it is expecting. var image: [block_size]u8 = @splat(0); var volume: Volume = .{ .image = &image }; var card: Card = .{}; try testing.expect(!card.fitted()); try testing.expectEqual(@as(?*Volume, null), card.chosen());
card.volumes[0] = &volume; try testing.expect(card.fitted()); // A unit number is DSSS0000, so the top bit is the drive. card.write(.unit, 0x70); try testing.expectEqual(@as(?*Volume, &volume), card.chosen()); card.write(.unit, 0xf0); try testing.expectEqual(@as(?*Volume, null), card.chosen());
// And the other way round, which is a card with only its second drive // connected: still a card, and its first drive still empty. card.volumes[0] = null; card.volumes[1] = &volume; try testing.expect(card.fitted()); try testing.expectEqual(@as(?*Volume, &volume), card.chosen()); card.write(.unit, 0x70); try testing.expectEqual(@as(?*Volume, null), card.chosen());}
/// A machine with nothing in it but memory, for exercising the card's side of/// a call without a processor in the way.const TestMachine = struct { memory: [0x10000]u8 = @splat(0),
pub fn blockGet(self: *TestMachine, at: u16) u8 { return self.memory[at]; }
pub fn blockPut(self: *TestMachine, at: u16, byte: u8) void { self.memory[at] = byte; }};
/// Sets up a call the way the firmware's entry point does: the command and a/// pointer to its parameters go inline after a JSR, and what the card is/// handed is where those three bytes are, less one.fn smartPortCall(card: *Card, machine: *TestMachine, command: SmartPort, list: u16) SmartPortResult { const at: u16 = 0x0300; machine.memory[at] = @intFromEnum(command); machine.memory[at + 1] = @truncate(list); machine.memory[at + 2] = @truncate(list >> 8); card.buffer = at - 1; return @enumFromInt(smartPort(card, machine));}
test "a SmartPort call steps the caller over the three bytes after it" { // Which is the whole of what the firmware cannot do for itself, and the // reason the card is handed the return address at all. var machine: TestMachine = .{}; var card: Card = .{}; _ = smartPortCall(&card, &machine, .init, 0x0400); try testing.expectEqual(@as(u16, 0x0302), card.buffer);}
test "unit nought is the interface and says how many drives are behind it" { var machine: TestMachine = .{}; var card: Card = .{};
machine.memory[0x0400] = 3; // three parameters machine.memory[0x0401] = 0; // unit nought machine.memory[0x0402] = 0x00; // the status list goes at $0500 machine.memory[0x0403] = 0x05; machine.memory[0x0404] = 0; // status code nought
try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .status, 0x0400)); try testing.expectEqual(@as(u8, 2), machine.memory[0x0500]); // Eight bytes, which is what X and Y come back holding. try testing.expectEqual(@as(u16, 8), card.block);
// And it is the only thing there is to say about the interface. machine.memory[0x0404] = 3; try testing.expectEqual(SmartPortResult.bad_code, smartPortCall(&card, &machine, .status, 0x0400));}
test "a drive says what it will do and how big it is" { var machine: TestMachine = .{}; var card: Card = .{}; var image: [block_size * 4]u8 = @splat(0); var volume: Volume = .{ .image = &image, .write_protected = false }; card.volumes[0] = &volume;
machine.memory[0x0400] = 3; machine.memory[0x0401] = 1; // the first drive machine.memory[0x0402] = 0x00; machine.memory[0x0403] = 0x05; machine.memory[0x0404] = 0;
try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .status, 0x0400)); try testing.expectEqual( status_block_device | status_read_allowed | status_write_allowed | status_online, machine.memory[0x0500], ); // Four blocks, in three bytes, low first. try testing.expectEqual(@as(u8, 4), machine.memory[0x0501]); try testing.expectEqual(@as(u8, 0), machine.memory[0x0502]); try testing.expectEqual(@as(u8, 0), machine.memory[0x0503]); try testing.expectEqual(@as(u16, 4), card.block);
// A write protected disk says both halves of that: writing is not allowed // and the medium is the reason. volume.write_protected = true; try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .status, 0x0400)); try testing.expectEqual( status_block_device | status_read_allowed | status_online | status_write_protected, machine.memory[0x0500], );
// The second drive is there and empty, which is not the same as not being // there: it answers, and says it is offline. machine.memory[0x0401] = 2; try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .status, 0x0400)); try testing.expectEqual(@as(u8, 0), machine.memory[0x0500] & status_online); try testing.expectEqual(@as(u8, 0), machine.memory[0x0501]);
// And there is no third. machine.memory[0x0401] = 3; try testing.expectEqual(SmartPortResult.bad_unit, smartPortCall(&card, &machine, .status, 0x0400));}
test "the information block is the shape the IIgs's own firmware uses" { // Read out of its ROM at $ff58fd, where the 3.5-inch drive's constant part // sits: a length, sixteen characters padded with spaces, a type, a subtype // and two bytes of version. var machine: TestMachine = .{}; var card: Card = .{}; var image: [block_size * 300]u8 = @splat(0); var volume: Volume = .{ .image = &image, .write_protected = false }; card.volumes[0] = &volume;
machine.memory[0x0400] = 3; machine.memory[0x0401] = 1; machine.memory[0x0402] = 0x00; machine.memory[0x0403] = 0x05; machine.memory[0x0404] = 3; // the information block
try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .status, 0x0400)); try testing.expectEqual(@as(u16, 25), card.block); try testing.expectEqual(@as(u8, 300 & 0xff), machine.memory[0x0501]); try testing.expectEqual(@as(u8, 300 >> 8), machine.memory[0x0502]); try testing.expectEqual(@as(u8, smartport_name.len), machine.memory[0x0504]); try testing.expectEqualSlices(u8, smartport_name, machine.memory[0x0505..][0..smartport_name.len]); // Padded with spaces to sixteen, which is what the IIgs does. for (machine.memory[0x0505 + smartport_name.len .. 0x0515]) |letter| try testing.expectEqual(@as(u8, ' '), letter); try testing.expectEqual(smartport_type, machine.memory[0x0515]); try testing.expectEqual(smartport_subtype, machine.memory[0x0516]); try testing.expectEqual(smartport_version[0], machine.memory[0x0517]); try testing.expectEqual(smartport_version[1], machine.memory[0x0518]);}
test "blocks go in and out through SmartPort as well as through ProDOS" { var machine: TestMachine = .{}; var card: Card = .{}; var image: [block_size * 4]u8 = @splat(0); for (0..4) |block| @memset(image[block * block_size ..][0..block_size], @intCast(block + 1)); var volume: Volume = .{ .image = &image, .write_protected = false }; card.volumes[0] = &volume;
machine.memory[0x0400] = 3; machine.memory[0x0401] = 1; // the first drive machine.memory[0x0402] = 0x00; // into $2000 machine.memory[0x0403] = 0x20; machine.memory[0x0404] = 2; // block 2, in three bytes machine.memory[0x0405] = 0; machine.memory[0x0406] = 0;
try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .read_block, 0x0400)); try testing.expectEqual(@as(u8, 3), machine.memory[0x2000]); try testing.expectEqual(@as(u8, 3), machine.memory[0x21ff]); // Five hundred and twelve bytes moved, which is what X and Y report. try testing.expectEqual(@as(u16, block_size), card.block);
machine.memory[0x2000] = 0x5a; try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .write_block, 0x0400)); try testing.expectEqual(@as(u8, 0x5a), image[2 * block_size]); try testing.expect(volume.dirty);
// A write protected disk says so rather than quietly not writing. volume.write_protected = true; machine.memory[0x2000] = 0x11; try testing.expectEqual( SmartPortResult.write_protected, smartPortCall(&card, &machine, .write_block, 0x0400), ); try testing.expectEqual(@as(u8, 0x5a), image[2 * block_size]);
// A block past the end of the image is an error and not a wrong answer. machine.memory[0x0404] = 9; try testing.expectEqual(SmartPortResult.io, smartPortCall(&card, &machine, .read_block, 0x0400));
// And an empty drive is offline. machine.memory[0x0401] = 2; machine.memory[0x0404] = 0; try testing.expectEqual(SmartPortResult.offline, smartPortCall(&card, &machine, .read_block, 0x0400));}
test "what this device does not do, it says it does not do" { var machine: TestMachine = .{}; var card: Card = .{}; var image: [block_size]u8 = @splat(0); var volume: Volume = .{ .image = &image }; card.volumes[0] = &volume;
// The four calls that belong to a character device. This is not one. for ([_]SmartPort{ .open, .close, .read, .write }) |command| try testing.expectEqual(SmartPortResult.bad_command, smartPortCall(&card, &machine, command, 0x0400));
// The extended forms, which have bit 6 set. This card does not claim to be // an extended SmartPort and does not answer them. try testing.expectEqual( SmartPortResult.bad_command, smartPortCall(&card, &machine, @enumFromInt(0x41), 0x0400), );
// Every call checks the parameter count first, and this one is wrong. machine.memory[0x0400] = 2; try testing.expectEqual( SmartPortResult.bad_parameter_count, smartPortCall(&card, &machine, .read_block, 0x0400), );
// Reset is the one control code there is anything to say about. machine.memory[0x0400] = 3; machine.memory[0x0401] = 1; machine.memory[0x0404] = 0; try testing.expectEqual(SmartPortResult.none, smartPortCall(&card, &machine, .control, 0x0400)); machine.memory[0x0404] = 7; try testing.expectEqual(SmartPortResult.bad_code, smartPortCall(&card, &machine, .control, 0x0400));}
test "there is a page of firmware for every slot, and each is that slot's" { // A machine takes its page out of `firmwares` by slot rather than calling // the generator, so the two have to be the same thing. They are the same // expression, which is exactly why this is worth asserting: a generator // that quietly ignored its argument would give eight identical pages and // nothing else here would notice until a card in slot 5 drove slot 7. inline for (0..firmwares.len) |slot| { try testing.expectEqualSlices(u8, &firmware(slot), &firmwares[slot]); } // And they really do differ: the card's own registers are at $C080 + N*16, // so the low byte of that address moves with the slot. try testing.expect(!std.mem.eql(u8, &firmwares[5], &firmwares[7]));}