// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The other end of the bus: a drive with a processor in it, answering the //! packets `cbus.zig` frames. //! //! A UniDisk 3.5 is not worked the way the drives in `disk2.zig` and //! `disk35.zig` are. Those are mechanisms — phase lines, a spindle, bits going //! past a head — and the machine does the reading itself. This one is told what //! to do in words: "the Protocol Converter ... is a set of assembly-language //! routines used to support external I/O devices, such as UniDisk 3.5. To //! ProDOS and Pascal 1.3, the Protocol Converter appears to be a block device" //! (Apple Computer, Inc., n.d.-b, p. 20). So what is modelled here is a //! correspondent rather than a mechanism: bytes arrive, a packet is assembled //! out of them, and a packet goes back. //! //! **The handshake, read out of the ROM.** With the port's base at `$c0e0` //! (`LDX #$60`, bank 1 `$ca9a`), the firmware selects the drive and starts the //! motor, then reads the sense line as bit seven of `$c0ee` up to fifty times //! over — `LDY #$32`, `LDA $c08e,X`, `BMI` — and gives up with a handshake //! error if it never comes up. So a device announces itself by holding that //! line high. The host then "raises REQ", which is phase zero, `LDA $c081,X` //! at bank 1 `$c8a5`, and begins shifting the packet out. //! //! **What is not yet settled** is what a reply must carry to satisfy the //! firmware beyond being a well formed packet: which type byte it should have, //! and what it wants in the status byte. Those are marked where they are //! guessed, and the way to settle them is to wire this to the machine and //! watch, which is how everything else here that is now known was found out. const std = @import("std"); const cbus = @import("cbus.zig"); /// The calls the Protocol Converter makes, as the manual numbers them in /// "Descriptions of the Protocol Converter Calls" (Apple Computer, Inc., /// n.d.-b, pp. 22-45). /// /// The first byte of a command packet's contents is one of these, which is how /// the captured `InitCMD` packet was recognised: its contents begin `$05`, and /// the reset code that runs as a IIc starts does `jsr MLIEntry` followed by /// `dfb InitCMD` "to force a reset of the PC Bus". pub const Command = 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 the type byte in a packet's header says it is. Captured from a IIc /// going about its business: a command goes out as `$80`, the block that /// follows a `WRITE BLOCK` goes out as `$82`, and what comes back is `$81`. pub const kind_command: u8 = 0x00; pub const kind_reply: u8 = 0x01; pub const kind_data: u8 = 0x02; /// A block, which is what this drive deals in rather than sectors: "READ /// BLOCK ($01) Reads one 512-byte block from a disk device, and writes it to /// memory" (Apple Computer, Inc., n.d.-b, p. 23). pub const block_size = 512; /// How much of a packet there can be. A block is 512 bytes and a UniDisk's is /// "always 524 bytes ($020C)" (Apple Computer, Inc., n.d.-b), and the encoding /// adds a byte of gathered high bits to every seven, so this is that with room /// over. pub const buffer_size = 1024; /// The error codes the manual lists, of which these are the ones a drive needs /// when it has no disk in it, is asked for a block it does not have, or is /// asked to write to a disk whose tab is across: "$2B NOWRITE The medium in the /// device is write protected", "$2D BADBLOCK The block number is outside the /// range allowed for the medium", "$2F OFFLINE Device off-line or no disk in /// drive" (Apple Computer, Inc., n.d.-b, "Summary of Error Codes", p. 51). pub const error_bad_command: u8 = 0x01; pub const error_no_write: u8 = 0x2b; pub const error_bad_control: u8 = 0x21; pub const error_bad_block: u8 = 0x2d; pub const error_offline: u8 = 0x2f; /// "$28 NODRIVE No device connected", which the manual lists among the errors /// `INIT` can return (Apple Computer, Inc., n.d.-b, p. 39). It is how the chain /// says where it stops: "The last bus resident in the device chain returns an /// exception, indicating that it is the last bus resident" (Apple Computer, /// Inc., 1987a, p. 158). pub const error_no_drive: u8 = 0x28; /// "$27 IOERROR I/O error" (Apple Computer, Inc., n.d.-b, p. 31), which is /// what an eject that left the disk where it was reports. pub const error_io: u8 = 0x27; /// What the drive says it is when asked for its Device Information Block. /// /// "Several device types and subtypes are assigned to existing SmartPort /// devices", and a UniDisk 3.5 is type $01 subtype $00 (Apple Computer, Inc., /// 1987a, p. 124). The subtype's bits say what a device can do beyond the /// ordinary — bit 7 extended SmartPort, bit 6 disk-switched errors, bit 5 set /// for non-removable media — and a UniDisk claims none of them, which is a /// removable disk with nothing added. pub const device_type: u8 = 0x01; pub const device_subtype: u8 = 0x00; /// "7-bit ASCII, uppercase, padded with spaces, eighth bit always = 0", in a /// field of sixteen (p. 21). pub const device_name = "UNIDISK 3.5"; /// "The firmware version field is a 2-byte field consisting of a number /// indicating the firmware version" (1987a, p. 124). Nothing here says what a /// real UniDisk puts in it, so this says nothing rather than inventing a /// version that could be checked against. pub const firmware_version: u16 = 0x0000; /// Where the device is in the conversation. pub const State = enum { /// Nothing going on. The sense line is up, saying a device is here. idle, /// A begin mark has been seen and bytes are being collected. receiving, /// A packet has arrived whole and an answer is framed, but the host has /// not yet been told so. The sense line is *down* in this state, which is /// the acknowledgement the sending firmware waits for. acknowledging, /// A packet has been answered and the reply is being handed over a byte /// at a time. replying, /// The answer is all handed over and the host is waiting to be told the /// exchange is finished, which is the sense line going down once more. finishing, }; pub const Unidisk = struct { /// Whether there is a drive on the end of the cable at all. Nothing /// answers when there is not, which is the case every earlier IIc is in. fitted: bool = false, /// The disk in the drive, as blocks. A UniDisk 3.5 holds sixteen hundred /// of them, which is the eight hundred kilobytes of a double sided /// 3.5-inch disk; nothing here insists on that number, and what is handed /// in is what the drive reports and serves. blocks: ?[]u8 = null, /// Whether the tab is across. A drive answers `NOWRITE` rather than /// quietly doing nothing, which is what lets a program say so. write_protected: bool = true, /// The block a `WRITE BLOCK` named, waiting for the data packet that /// carries its contents. Writing takes two packets: the command says /// which block, and the five hundred and twelve bytes follow in a packet /// of their own. pending_write: ?usize = null, /// Which drive on the chain this one is, once it has been told. /// /// It is not born knowing. The host hands the numbers out, and it does so /// by sending `Init` to one address after another: "The host then sends /// the ID definition command. Whenever a device receives this command /// (with Enable), it assigns the unit number embedded in the command /// string as its own unit number. Thereafter it will not respond to any /// command string with a unit number other than that given it in the ID /// definition command" (Apple Computer, Inc., 1987a, p. 157). So a drive /// answers exactly one address, and until it has been given one it answers /// none — a drive that answered every address would be an endless chain of /// drives, which is what happened before this was checked at all: /// seventy-one of them before the firmware gave up. /// /// Taking the number from the host rather than assuming it is the whole of /// what lets a machine other than an Apple IIc find one of these. A IIc /// starts at one. An Apple IIgs has devices of its own to number first and /// starts at **three**, which was read off its own startup: the packet it /// sends is the IIc's `Init` to the byte, save that its destination is /// `$83` where a IIc's is `$81`. A drive born as unit one hears that packet /// and says nothing, and the machine decides there is nothing on the port. unit: ?u8 = null, /// Whether there is nothing behind this drive on the cable. /// /// The last one has to say so, and answers the call that numbers it with /// an error rather than with success: "The last bus resident in the device /// chain returns an exception, indicating that it is the last bus /// resident" (Apple Computer, Inc., 1987a, p. 158). A real drive works it /// out for itself, by "sensing a signal, normally unused in Disk II /// operations, which is grounded by all intelligent devices"; here `Chain` /// knows what is plugged in and says. /// /// An Apple IIc does not need telling — it asks the next address, hears /// nothing, and stops. An Apple IIgs does: told that another drive follows /// and then finding none, it abandons the port altogether and puts up /// "Check startup device!" with a perfectly good disk on the end of the /// cable. end_of_chain: bool = false, /// Whether it is this drive's turn to be given a number. /// /// Only one drive on the chain can take a number at a time, and the wire /// arranges it: a bus reset leaves every resident unable to see the enable, /// and "upon completing the ID definition command, the bus resident /// reenables the phase 3 line, allowing the next resident to receive its ID /// definition command" (p. 158). So the numbering walks down the chain, one /// drive per `Init`, and `Chain` sets this on whichever drive is next. numbering: bool = false, state: State = .idle, /// What the host has sent us, as it arrives on the wire. incoming: [buffer_size]u8 = @splat(0), incoming_n: usize = 0, /// And what we are sending back, likewise. outgoing: [buffer_size]u8 = @splat(0), outgoing_n: usize = 0, outgoing_at: usize = 0, /// REQ, which is phase zero of the port and the host's half of the /// handshake: "Host signals data iminent". The firmware raises it before /// it sends (`LDA $c081,X`, bank 1 `$c8a5`) and again before it reads a /// reply (`$c9fd`), and drops it once the exchange is over (`$cd3f`). req: bool = false, /// The last command we understood, kept so a test can say what happened. last: ?Command = null, /// How many packets have been taken in and answered, how many blocks have /// been written, and how many times the medium has been laid down. received: u64 = 0, replied: u64 = 0, written: u64 = 0, formatted: u64 = 0, /// How many times a `CONTROL` call has ejected the disk. A session watches /// this so that what was written goes back to the file before the drive /// lets go of it. ejected: u64 = 0, /// Whether what is in `blocks` differs from what came off the file, so /// that a session knows there is something to write back. Set by anything /// that changes the medium and cleared by whoever saves it. dirty: bool = false, /// The sense line, which is bit seven of what `$c0ee` reads. /// /// The firmware polls it before it sends and takes it being up as "a /// device is there and ready". An empty port reads the same way on a /// machine with an ordinary drive, because a drive with no disk in it /// reports itself write protected — which is why a IIc with nothing /// attached still transmits a packet, and why one could be captured. /// /// It has to come *down* as well, though, and that is the whole of the /// acknowledgement. Having sent the end of a packet the firmware waits on /// this line falling — `LDA $c08e,X`, `BMI` back, at bank 1 `$c9c1`, which /// spins for as long as bit seven is set — and only then goes on to /// receive. So the drive drops the line once, to say the packet arrived, /// and puts it back up to say an answer is waiting. pub fn ready(self: *Unidisk) bool { if (!self.fitted) return false; // Twice in an exchange the line has to fall, and both are places the // firmware spins until it does. The first says the packet arrived, at // bank 1 `$c9c1`; the second says the answer is all handed over, at // `$cd39`, which is `LDA $c08e` and `BMI` back on itself. // // The second of those is a level and not a pulse, and the difference // matters: the firmware polls this line in more than one place, so a // line that falls once and comes straight back up has its fall taken // by whichever poll happens to be first. It stays down until REQ goes // away, which is what the host does immediately afterwards. switch (self.state) { .acknowledging => { self.state = .replying; return false; }, .finishing => return false, else => return true, } } /// REQ moving. The end of an exchange is the host dropping it, which is /// what puts the drive back to waiting for another command. pub fn request(self: *Unidisk, on: bool) void { self.req = on; if (!on and self.state == .finishing) self.state = .idle; } /// A byte from the host. pub fn write(self: *Unidisk, byte: u8) void { if (!self.fitted) return; switch (self.state) { // Nothing the host says is listened to while an answer is // waiting to be told about or handed over. .acknowledging, .replying, .finishing => {}, .idle => { // Sync is not part of the packet and is skipped: the begin // mark is what starts one. if (byte == cbus.packet_begin) { self.state = .receiving; self.incoming_n = 0; self.incoming[self.incoming_n] = byte; self.incoming_n += 1; } }, .receiving => { if (self.incoming_n >= self.incoming.len) { self.state = .idle; return; } self.incoming[self.incoming_n] = byte; self.incoming_n += 1; // A packet ends where its own header says it does, and not at // the first byte that looks like the end mark. // // Watching for `$c8` instead is wrong, and wrong in a way that // takes a whole boot to show up: every byte of a packet's // contents travels with its top bit set, so a content byte of // `$48` goes across as `$c8` and is indistinguishable from the // mark. A IIc booting reads block after block into ascending // buffers — `$0800`, `$0c00`, `$0e00`, and up — and the read // that would have gone to `$4800` carries `$48` as its buffer's // high byte. Ending the packet there leaves a fragment that // cannot decode, the drive says nothing, the firmware retries, // and it retried some six thousand times before giving up with // `UNABLE TO LOAD PRODOS`. if (self.expected()) |whole| { if (self.incoming_n >= whole) self.finish(); } }, } } /// How long the packet coming in is, once enough of it has arrived to /// say. Nothing before the counts have been seen. /// /// After the begin mark come the five header bytes and the two counts, /// then — if there are any leftover bytes — their gathered high bits and /// the leftovers themselves, then each group of seven behind its own /// gathered byte, and last the checksum's two bytes and the end mark. fn expected(self: *const Unidisk) ?usize { if (self.incoming_n < 8) return null; const odd: usize = self.incoming[6] & 0x7f; const groups: usize = self.incoming[7] & 0x7f; return 1 + 7 + (if (odd == 0) 0 else 1 + odd) + groups * 8 + 2 + 1; } /// The packet is whole: work out what was asked and put an answer ready. fn finish(self: *Unidisk) void { self.state = .idle; var contents: [buffer_size]u8 = undefined; const packet = cbus.decode(self.incoming[0..self.incoming_n], &contents) catch return; // Not ours: the chain is walked by address, and a drive that is not // the one being spoken to says nothing at all. const addressed: u8 = packet.header.destination & 0x7f; if (self.unit) |mine| { if (addressed != mine) return; } else { // Nameless still. The only thing that can be said to a drive in // that state is the one call that gives it a name, and only when // it is this drive's turn to hear one. if (!self.numbering) return; if (packet.payload.len == 0) return; if (packet.payload[0] != @intFromEnum(Command.init)) return; self.unit = addressed; } self.received += 1; if (packet.payload.len == 0) return; self.last = @enumFromInt(packet.payload[0]); self.reply(packet); } /// Frames an answer to a packet that arrived. /// /// The addressing is the packet's turned round: what was sent to us came /// from `$80`, which is the host, so that is where the answer goes, and it /// comes from the unit the host was addressing. /// /// **The type and status bytes here are a guess.** A reply plainly has to /// be a well formed packet, and the firmware reads its status byte into /// `$004d` where an error code would go, so nought is what a call that /// worked should leave there. Which type byte marks a reply, though, is not /// something the manual says or the listing has yet been read for, and /// watching the machine is what will settle it. fn reply(self: *Unidisk, to: cbus.Packet) void { var contents: []const u8 = &.{}; var status: u8 = 0x00; var told: [25]u8 = undefined; // A data packet is not a command: it is the block a `WRITE BLOCK` // asked for, arriving behind it. if (to.header.kind & 0x7f == kind_data) { if (self.pending_write) |block| { self.pending_write = null; if (self.blocks) |disk| { const at = block * block_size; if (at + block_size <= disk.len and to.payload.len >= block_size) { @memcpy(disk[at..][0..block_size], to.payload[0..block_size]); self.written += 1; self.dirty = true; } else status = error_bad_block; } else status = error_offline; } else status = error_bad_command; self.send(to, &.{}, status); return; } switch (@as(Command, @enumFromInt(to.payload[0]))) { // "Reads one 512-byte block from the disk device specified by the // unit-number parameter into memory starting at the address // specified by the data-buffer parameter", the parameter list // being the count, the unit, "Data buffer (low byte, high byte)" // and "Block number (low byte, mid byte, high byte)" (Apple // Computer, Inc., n.d.-b, p. 30). // // On the wire the unit is not among them, the packet's own // destination having said which drive is being spoken to already, // so what arrives is the command, the count, the buffer and the // block. Two captured from a IIc booting settle it beyond // argument: `01 03 00 08 00 00 00 ...` is the boot block, block // nought to `$0800`, and `01 03 00 0c 02 00 00 ...` is block two // to `$0c00`, which is a ProDOS boot block fetching the volume // directory. Read one place further along and that second one asks // for block `$c50000`, which is nothing. .read_block => { if (to.payload.len >= 7) { const block = @as(usize, to.payload[4]) | (@as(usize, to.payload[5]) << 8) | (@as(usize, to.payload[6]) << 16); if (self.blocks) |disk| { const at = block * block_size; if (at + block_size <= disk.len) { contents = disk[at..][0..block_size]; } else status = error_bad_block; } else status = error_offline; } else status = error_bad_command; }, // "Writes one 512-byte block from memory to a disk device" // (Apple Computer, Inc., n.d.-b, p. 24). The command names the // block and the contents come behind it in a packet of their own, // so all this can do is remember which block was meant and say it // is ready. .write_block => { if (to.payload.len >= 7) { const block = @as(usize, to.payload[4]) | (@as(usize, to.payload[5]) << 8) | (@as(usize, to.payload[6]) << 16); if (self.blocks == null) { status = error_offline; } else if (self.write_protected) { status = error_no_write; } else { self.pending_write = block; } } else status = error_bad_command; }, // "Returns status information about a particular device", and // what sort is the status code's business: the parameter list is // the count, the unit, a pointer to where the answer goes, and // that code (Apple Computer, Inc., n.d.-b, p. 25). On the wire the // unit is absent as ever, so the code is the fifth byte. .status => { const code: u8 = if (to.payload.len >= 5) to.payload[4] else 0; switch (code) { // "Status Code = $00, Return Device Status. The device // status consists of four bytes", the first being the // general status byte and the next three "the size in // 512-byte blocks", low byte first (p. 26). 0x00 => { told[0] = self.generalStatus(); const count: usize = if (self.blocks) |disk| disk.len / block_size else 0; told[1] = @truncate(count); told[2] = @truncate(count >> 8); told[3] = @truncate(count >> 16); contents = told[0..4]; }, // "Status codes $01 and $02 are not supported by the // UniDisk 3.5 and result in an error (BADCTL $21)" (p. 26), // the first being the device control block a UniDisk does // not have and the second the newline mode only a // character device has. 0x01, 0x02 => status = error_bad_control, // "Status Code = $03, Return Device Information Block. The // device's information block contains information // identifying the device, its type, and various other // attributes" (p. 21). Twenty-five bytes: the same general // status byte as code $00, the size again, then the name // in a field of sixteen and what the device is. 0x03 => { told[0] = self.generalStatus(); const count: usize = if (self.blocks) |disk| disk.len / block_size else 0; told[1] = @truncate(count); told[2] = @truncate(count >> 8); told[3] = @truncate(count >> 16); told[4] = device_name.len; @memset(told[5..21], ' '); @memcpy(told[5..][0..device_name.len], device_name); told[21] = device_type; told[22] = device_subtype; told[23] = @truncate(firmware_version); told[24] = @truncate(firmware_version >> 8); contents = &told; }, // "Status Code = $05, Return UniDisk 3.5 Status. This call // allows the diagnostic program to get more detailed // information about the cause of a read or write error, // and to examine the contents of the 65C02's registers // after a CONTROL Protocol Converter call with control // code = $05" (p. 22). Eight bytes, of which the last four // "are set only after a CONTROL call with control code = // $05, and are zero after any other call". // // Nothing here fails the way the error byte describes — // there is no surface to mis-read and no track to seek // wrongly — so it says no errors and no retries, which is // the truth about this drive rather than a stub. 0x05 => { told[0] = 0x00; told[1] = 0x00; // no soft error told[2] = 0x00; // and nothing was read twice told[3] = 0x0d; @memset(told[4..8], 0); // A, X, Y and P after an EXECUTE contents = told[0..8]; }, else => status = error_bad_control, } }, // "The FORMAT call prepares all blocks on the recording medium of // a block device for reading and writing. The formatting done by // this call is not linked to any operating system; for example, // bitmaps and catalogs are not written by this call" (Apple // Computer, Inc., n.d.-b, p. 34). So this lays the medium down and // stops there: what makes it a volume is the host writing a // directory afterwards, block by block, the ordinary way. .format => { if (self.blocks) |disk| { if (self.write_protected) { status = error_no_write; } else { @memset(disk, 0); self.formatted += 1; self.dirty = true; } } else status = error_offline; }, // "Control requests to unit number $00 are sent to the Protocol // Converter itself" (Apple Computer, Inc., n.d.-b, p. 36) — and // that is the machine's own firmware, not anything on the bus, so // what reaches a drive is always a request to the drive. .control => { const code: u8 = if (to.payload.len >= 5) to.payload[4] else 0; switch (code) { // "$00 Reset the device" (p. 36). Everything the wire is // in the middle of is dropped; the disk stays where it is, // a reset being a reset of the drive and not an eject. 0x00 => self.reset(), // "UniDisk 3.5 has no DCB, and returns an error (BADCTL // $21) in response to this call" (p. 21), and newline is a // character device's business. 0x01, 0x02 => status = error_bad_control, // "$03 Service device interrupt... To be used as needed // for interrupt-driven devices" (p. 36). A UniDisk is not // one: it "does not support this line, and so cannot // generate interrupts to the Protocol Converter" (p. 22), // so there is never anything to service. 0x03 => {}, // "Causes UniDisk 3.5 to auto-eject a disk. There are no // parameters in the control list, and no errors are // returned if the disk ejected correctly or there was no // disk in the drive. Error code $27 (I/O error) is // returned if the eject failed, that is, a disk is still // in the drive" (p. 37). // // Nothing here can fail to let go of a disk, so the error // is the one answer this cannot produce. 0x04 => { self.blocks = null; self.ejected += 1; }, // "Control codes $05 and higher are reserved; use of some // of these codes can cause your system to crash" (p. 38). // Saying so is better than crashing. else => status = error_bad_control, } }, // "The INIT call resets all intelligent devices attached to the // Protocol Converter. The Protocol Converter goes through an // initialization sequence, cold-resetting all devices and sending // each its unit number. This call is made automatically on // startup; an application should never have to make this call" // (Apple Computer, Inc., n.d.-b, p. 39). // // A drive that answers it at all is a drive that is there, which // is how the firmware finds out what is on the bus: it asks unit // one, then unit two, and takes the first silence as the end of // the chain. So the answer is the whole of the work, and the disk // stays where it is — this resets the device, not the medium. .init => if (self.end_of_chain) { status = error_no_drive; }, // "$01 BADCMD An unimplemented command was issued" (p. 29). These // four are how a *character* device is read and written, and this // is a block device: answering them with silence would have a // caller believe a stream had been opened. .open, .close, .read, .write => status = error_bad_command, else => status = error_bad_command, } self.send(to, contents, status); } /// The general status byte, which both `STATUS $00` and the Device /// Information Block begin with. /// /// The bit senses are the manual's, checked against the page rather than /// its OCR, and two of them run the opposite way to the rest: "3 — 0 = /// format allowed" and "2 — 0 = medium write protected" (Apple Computer, /// Inc., n.d.-b, p. 20). So a one in bit three would say formatting is /// *not* allowed, and it is; and a one in bit two says the medium is *not* /// protected. fn generalStatus(self: *const Unidisk) u8 { var general: u8 = 0x80; // bit 7: a block device general |= 0x20; // bit 5: reading allowed if (self.blocks != null) general |= 0x10; // bit 4: a disk is in if (!self.write_protected) general |= 0x40 | 0x04; // bits 6 and 2 return general; } /// Frames an answer and puts it ready to be handed over. fn send(self: *Unidisk, to: cbus.Packet, contents: []const u8, status: u8) void { const answer = cbus.encode(.{ .destination = 0x00, .source = to.header.destination & 0x7f, .kind = kind_reply, .auxiliary = 0x00, .status = status, }, contents, &self.outgoing) catch return; self.outgoing_n = answer.len; self.outgoing_at = 0; self.state = .acknowledging; self.replied += 1; } /// The next byte the host will read out of the data register. /// /// When there is nothing to say it reads as nought, which is what an /// unanswered port reads as anyway, so a host polling one that has nothing /// for it waits exactly as it would with no device there. pub fn read(self: *Unidisk) u8 { if (self.state != .replying) return 0; if (self.outgoing_at >= self.outgoing_n) { self.state = .finishing; return 0; } const byte = self.outgoing[self.outgoing_at]; self.outgoing_at += 1; if (self.outgoing_at >= self.outgoing_n) self.state = .finishing; return byte; } /// A bus reset, which is the harder of the two and takes the number away /// with everything else. /// /// It is not the same thing as the `CONTROL` call that resets a device: it /// is the phase-line combination on the wire, and it "flips a latch on all /// bus residents", after which the host numbers the chain again from the /// top down (Apple Computer, Inc., 1987a, p. 157). A device told to reset /// itself is still the device the host was talking to and still answers to /// its name. pub fn busReset(self: *Unidisk) void { self.unit = null; self.numbering = false; self.end_of_chain = false; self.reset(); } pub fn reset(self: *Unidisk) void { self.state = .idle; self.incoming_n = 0; self.outgoing_n = 0; self.outgoing_at = 0; } }; const testing = std.testing; /// The packet an Apple IIc really sent, captured from the data register during /// an ordinary power-on. See `cbus.zig`, where it is taken apart. const captured_init = [_]u8{ 0xff, 0x3f, 0xcf, 0xf3, 0xfc, 0xff, // the sync run 0xc3, // the begin mark 0x81, 0x80, 0x80, 0x80, 0x80, // destination, source, type, aux, status 0x82, 0x81, // two leftover bytes, one whole group 0x80, // the leftovers' gathered high bits 0x85, 0x82, // and the leftovers: $05 is InitCMD 0x80, // the group's gathered high bits 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // and its seven 0xaf, 0xea, // the checksum, in two 0xc8, // the end mark }; test "a drive nobody fitted says nothing and answers nothing" { var drive: Unidisk = .{}; try testing.expect(!drive.ready()); for (captured_init) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 0), drive.received); try testing.expectEqual(@as(u8, 0), drive.read()); } test "a drive that is there holds the sense line up" { // Which is the whole of how the firmware finds one: fifty reads of // `$c0ee`, and a `BMI` out of the loop the first time bit seven is set. var drive: Unidisk = .{ .fitted = true, .unit = 1 }; try testing.expect(drive.ready()); } test "the packet a real IIc sends is taken in and read as Init" { var drive: Unidisk = .{ .fitted = true, .unit = 1 }; for (captured_init) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 1), drive.received); try testing.expectEqual(Command.init, drive.last.?); } test "the sync in front of a packet does not start one" { // A begin mark is what starts a packet; everything before it is the run // that puts a receiver in step, and one of those bytes is `$c3`-less by // design. Feeding sync alone must leave the device idle. var drive: Unidisk = .{ .fitted = true, .unit = 1 }; for (cbus.sync) |byte| drive.write(byte); try testing.expectEqual(State.idle, drive.state); try testing.expectEqual(@as(u64, 0), drive.received); } test "what the drive answers with is itself a packet, and reads back" { var drive: Unidisk = .{ .fitted = true, .unit = 1 }; for (captured_init) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 1), drive.replied); // Read it out a byte at a time, the way the firmware does. var back: [buffer_size]u8 = undefined; var wire: [buffer_size]u8 = undefined; try testing.expect(!drive.ready()); // the acknowledgement, once var n: usize = 0; while (drive.state == .replying) : (n += 1) wire[n] = drive.read(); // And it stays down at the end until the host takes REQ away, rather // than coming back up under the next poll that happens along. try testing.expect(!drive.ready()); try testing.expect(!drive.ready()); drive.request(false); try testing.expectEqual(State.idle, drive.state); try testing.expect(drive.ready()); const answer = try cbus.decode(wire[0..n], &back); // Addressed back to the host, and from the unit that was addressed. try testing.expectEqual(@as(u8, 0x80), answer.header.destination); try testing.expectEqual(@as(u8, 0x81), answer.header.source); // And a status byte of nought, which is what a call that worked leaves // where the firmware looks for an error code. try testing.expectEqual(@as(u8, 0x80), answer.header.status); } test "a packet with a byte spoiled is not answered" { // The checksum is the only thing standing between a line with noise on it // and a device acting on a command nobody sent. var drive: Unidisk = .{ .fitted = true, .unit = 1 }; var spoiled = captured_init; spoiled[15] ^= 0x01; for (spoiled) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 0), drive.received); try testing.expectEqual(@as(u64, 0), drive.replied); } test "two packets one after another are both answered" { var drive: Unidisk = .{ .fitted = true, .unit = 1 }; for (captured_init) |byte| drive.write(byte); _ = drive.ready(); while (drive.state == .replying) _ = drive.read(); drive.request(false); for (captured_init) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 2), drive.received); try testing.expectEqual(@as(u64, 2), drive.replied); } test "a drive answers to its own address and stays quiet at every other" { // How the firmware learns what is on the bus: `Init` to unit one, then // unit two, and on until nothing answers. A drive that replied whatever // address it saw would be an endless chain — which is not a guess about // what the firmware does, but what it did, seventy-one units deep, before // this was checked. var drive: Unidisk = .{ .fitted = true, .unit = 1 }; for (captured_init) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 1), drive.received); try testing.expectEqual(@as(u64, 1), drive.replied); // The same packet re-addressed to unit two, which is somebody else. drive.request(false); _ = drive.ready(); while (drive.state == .replying) _ = drive.read(); drive.request(false); var wire: [64]u8 = undefined; const elsewhere = try cbus.encode(.{ .destination = 0x02, .source = 0x00, .kind = 0x00, .auxiliary = 0x00, .status = 0x00, }, &.{ 0x05, 0x02, 0, 0, 0, 0, 0, 0, 0 }, &wire); for (elsewhere) |byte| drive.write(byte); try testing.expectEqual(@as(u64, 1), drive.received); try testing.expectEqual(@as(u64, 1), drive.replied); } /// Frames a command packet for the drive, the way the firmware does. fn command(into: []u8, unit: u8, kind: u8, contents: []const u8) ![]u8 { return cbus.encode(.{ .destination = unit, .source = 0x00, .kind = kind, .auxiliary = 0x00, .status = 0x00, }, contents, into); } /// Hands a whole packet to the drive and takes its answer back, the way an /// exchange really goes: the line falls to say the packet arrived, comes up /// to say an answer is waiting, and falls again once it is all handed over. fn exchange(drive: *Unidisk, packet: []const u8, into: []u8) ![]u8 { for (packet) |byte| drive.write(byte); _ = drive.ready(); var n: usize = 0; while (drive.state == .replying) : (n += 1) into[n] = drive.read(); drive.request(false); return into[0..n]; } test "a block written to the drive is really in the image afterwards" { // Two packets make a write: the command names the block, and the five // hundred and twelve bytes follow in a packet of their own with `$82` in // its type byte. Both were read off a IIc saving a file. var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; // `WRITE BLOCK`, block two: the command, a count of three, the buffer the // host is writing from, and the block number low byte first. const say = try command(&wire, 0x01, kind_command, &.{ 0x02, 0x03, 0x00, 0x20, 0x02, 0x00, 0x00 }); _ = try exchange(&drive, say, &back); try testing.expectEqual(@as(?usize, 2), drive.pending_write); // And then the block itself. var contents: [block_size]u8 = undefined; for (&contents, 0..) |*b, i| b.* = @intCast((i * 7 + 3) & 0xff); var big: [1024]u8 = undefined; const data = try command(&big, 0x01, kind_data, &contents); _ = try exchange(&drive, data, &back); try testing.expectEqual(@as(u64, 1), drive.written); try testing.expectEqual(@as(?usize, null), drive.pending_write); try testing.expectEqualSlices(u8, &contents, image[2 * block_size ..][0..block_size]); // And nothing landed on the neighbours. try testing.expect(std.mem.allEqual(u8, image[block_size..][0..block_size], 0)); } test "a disk with its tab across says so instead of being written to" { var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = true }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; const say = try command(&wire, 0x01, kind_command, &.{ 0x02, 0x03, 0x00, 0x20, 0x02, 0x00, 0x00 }); const answer = try exchange(&drive, say, &back); // "$2B NOWRITE The medium in the device is write protected", which is what // lets a program say so rather than watch a write vanish. const reply = try cbus.decode(answer, &back); try testing.expectEqual(error_no_write | 0x80, reply.header.status); // And nothing is waiting for a block that must not be written. try testing.expectEqual(@as(?usize, null), drive.pending_write); try testing.expect(std.mem.allEqual(u8, &image, 0)); } test "the drive says what it is when asked for its status" { // "The device status consists of four bytes. The first is the general // status byte" and the next three are "the size in 512-byte blocks", low // byte first (Apple Computer, Inc., n.d.-b, p. 26). var image: [1600 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; // `STATUS`, a count of three, somewhere to put the answer, and status // code nought. const ask = try command(&wire, 0x01, kind_command, &.{ 0x00, 0x03, 0x00, 0xdc, 0x00 }); const answer = try exchange(&drive, ask, &back); var into: [64]u8 = undefined; const reply = try cbus.decode(answer, &into); try testing.expectEqual(@as(usize, 4), reply.payload.len); const general = reply.payload[0]; try testing.expect(general & 0x80 != 0); // a block device try testing.expect(general & 0x40 != 0); // writing allowed try testing.expect(general & 0x20 != 0); // reading allowed try testing.expect(general & 0x10 != 0); // a disk is in it // Two bits run the other way about, which is the manual's doing and not a // slip here: bit two clear says the medium *is* protected, and this one is // not; bit three *clear* says formatting is allowed, and it is. try testing.expect(general & 0x04 != 0); try testing.expect(general & 0x08 == 0); // Sixteen hundred blocks, low byte first, which is eight hundred // kilobytes. const blocks = @as(usize, reply.payload[1]) | (@as(usize, reply.payload[2]) << 8) | (@as(usize, reply.payload[3]) << 16); try testing.expectEqual(@as(usize, 1600), blocks); } test "a protected disk and an empty drive say so in the status byte" { var image: [4 * block_size]u8 = @splat(0); var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var into: [64]u8 = undefined; const ask = try command(&wire, 0x01, kind_command, &.{ 0x00, 0x03, 0x00, 0xdc, 0x00 }); var protected: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = true }; const first = try cbus.decode(try exchange(&protected, ask, &back), &into); try testing.expect(first.payload[0] & 0x40 == 0); // writing not allowed try testing.expect(first.payload[0] & 0x04 == 0); // and the medium is protected try testing.expect(first.payload[0] & 0x10 != 0); // but there is a disk in it var empty: Unidisk = .{ .fitted = true, .unit = 1 }; const second = try cbus.decode(try exchange(&empty, ask, &back), &into); try testing.expect(second.payload[0] & 0x10 == 0); // nothing in the drive try testing.expectEqual(@as(u8, 0), second.payload[1]); } test "the status codes a UniDisk does not answer are refused, not guessed at" { // "Status codes $01 and $02 are not supported by the UniDisk 3.5 and // result in an error (BADCTL $21)" (Apple Computer, Inc., n.d.-b, p. 26). var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var into: [64]u8 = undefined; for ([_]u8{ 0x01, 0x02 }) |code| { drive.reset(); const ask = try command(&wire, 0x01, kind_command, &.{ 0x00, 0x03, 0x00, 0xdc, code }); const reply = try cbus.decode(try exchange(&drive, ask, &back), &into); try testing.expectEqual(error_bad_control | 0x80, reply.header.status); } } test "formatting lays the medium down and writes no filesystem on it" { // "The FORMAT call prepares all blocks on the recording medium of a block // device for reading and writing", and explicitly not more than that: // "bitmaps and catalogs are not written by this call" (Apple Computer, // Inc., n.d.-b, p. 34). So what it leaves is a blank medium, and what // makes a volume of it is the host writing a directory afterwards. var image: [4 * block_size]u8 = @splat(0xa5); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var into: [64]u8 = undefined; const ask = try command(&wire, 0x01, kind_command, &.{ 0x03, 0x01 }); const reply = try cbus.decode(try exchange(&drive, ask, &back), &into); try testing.expectEqual(@as(u8, 0x80), reply.header.status); // no error try testing.expectEqual(@as(u64, 1), drive.formatted); try testing.expect(std.mem.allEqual(u8, &image, 0)); } test "a protected disk will not be formatted either" { var image: [4 * block_size]u8 = @splat(0xa5); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = true }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var into: [64]u8 = undefined; const ask = try command(&wire, 0x01, kind_command, &.{ 0x03, 0x01 }); const reply = try cbus.decode(try exchange(&drive, ask, &back), &into); try testing.expectEqual(error_no_write | 0x80, reply.header.status); try testing.expectEqual(@as(u64, 0), drive.formatted); // And the tab did its job: nothing on the surface moved. try testing.expect(std.mem.allEqual(u8, &image, 0xa5)); } /// The drives on the port, which share one wire. /// /// A UniDisk 3.5 has a connector on the back for the next drive along, and the /// firmware finds out how many there are by asking: `Init` to unit one, then to /// unit two, and on until nothing answers. So they all hear everything, and /// each answers only when its own address is on the packet. /// /// The sense line is the one place that has to be thought about rather than /// delegated. It is shared, and a drive pulls it *down*; so a drive that is /// busy takes the line down for everybody, and the line is only up when every /// drive that is there is ready. That is why this asks all of them rather than /// the first: an idle drive saying it is ready must not raise a line that a /// working one is holding down. pub const Chain = struct { drives: [2]Unidisk = .{ .{}, .{} }, /// Whether there is anything on the port at all. pub fn fitted(self: *const Chain) bool { for (&self.drives) |*drive| { if (drive.fitted) return true; } return false; } /// The sense line, which any one drive can hold down. pub fn ready(self: *Chain) bool { var present = false; var up = true; for (&self.drives) |*drive| { if (!drive.fitted) continue; present = true; // Every drive is asked, not just until one says no: asking is what // moves a drive on from having taken a packet to having an answer // ready, and skipping that would leave it stuck. if (!drive.ready()) up = false; } return present and up; } /// A byte from the host, which every drive hears. /// /// Which of them may *answer to a new number* is settled first, because /// that is a property of the chain rather than of any one drive: the /// nearest drive that has not been given a number yet is the one whose /// turn it is, every drive behind it being cut off from the enable until /// it is done. pub fn write(self: *Chain, byte: u8) void { var turn = true; var last: ?*Unidisk = null; for (&self.drives) |*drive| { if (!drive.fitted) continue; drive.numbering = turn and drive.unit == null; drive.end_of_chain = false; if (drive.unit == null) turn = false; last = drive; } if (last) |drive| drive.end_of_chain = true; for (&self.drives) |*drive| drive.write(byte); } /// And one going back, from whichever drive has something to say. pub fn read(self: *Chain) u8 { for (&self.drives) |*drive| { if (drive.fitted and drive.state == .replying) return drive.read(); } return 0; } pub fn request(self: *Chain, on: bool) void { for (&self.drives) |*drive| drive.request(on); } /// The bus reset, which every resident hears and which leaves them all /// nameless, waiting to be numbered again. pub fn reset(self: *Chain) void { for (&self.drives) |*drive| drive.busReset(); } }; test "the host hands out the numbers, and the chain takes them in order" { // A drive is not born knowing which one it is. The host numbers the chain // by sending `Init` to one address after another, each drive taking the // number in the first such call it is allowed to hear, and the last one // saying that it is the last. // // Which numbers those are is the host's business and not the same on every // machine: an Apple IIc starts at one and an Apple IIgs at three, having // kept the first two for the drives on its own port. So the numbers here // start at seven, which is neither, to make the point that nothing in the // drive assumes them. var chain: Chain = .{}; chain.drives[0] = .{ .fitted = true }; chain.drives[1] = .{ .fitted = true }; try testing.expectEqual(@as(?u8, null), chain.drives[0].unit); var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var into: [1024]u8 = undefined; // The first `Init` goes to the nearest drive without a number, and only to // it: the one behind is cut off from the enable until its turn comes. const first = try command(&wire, 0x07, kind_command, &.{@intFromEnum(Command.init)}); for (first) |byte| chain.write(byte); try testing.expectEqual(@as(?u8, 7), chain.drives[0].unit); try testing.expectEqual(@as(?u8, null), chain.drives[1].unit); // And it answered, with nought, because there is another behind it. _ = chain.ready(); var n: usize = 0; while (chain.drives[0].state == .replying) : (n += 1) into[n] = chain.read(); chain.request(false); try testing.expectEqual(@as(u8, 0x00), (try cbus.decode(into[0..n], &back)).header.status & 0x7f); // The next one takes the next number, and says it is the end of the chain. const second = try command(&wire, 0x08, kind_command, &.{@intFromEnum(Command.init)}); for (second) |byte| chain.write(byte); try testing.expectEqual(@as(?u8, 7), chain.drives[0].unit); try testing.expectEqual(@as(?u8, 8), chain.drives[1].unit); _ = chain.ready(); n = 0; while (chain.drives[1].state == .replying) : (n += 1) into[n] = chain.read(); chain.request(false); try testing.expectEqual(error_no_drive, (try cbus.decode(into[0..n], &back)).header.status & 0x7f); // A third asks for a drive that is not there, and nothing answers it — // every drive on the chain now has a number of its own and none of them is // this one. const third = try command(&wire, 0x09, kind_command, &.{@intFromEnum(Command.init)}); for (third) |byte| chain.write(byte); try testing.expectEqual(State.idle, chain.drives[0].state); try testing.expectEqual(State.idle, chain.drives[1].state); } test "a drive with no number yet answers nothing but the call that gives it one" { // Anything else said to a nameless drive is somebody else's business. The // firmware talks to devices it has already numbered on the same wire, and a // drive that answered those would be two drives at one address. var chain: Chain = .{}; var image: [4 * block_size]u8 = @splat(0x33); chain.drives[0] = .{ .fitted = true, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; const read = try command(&wire, 0x01, kind_command, &.{ 0x01, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00 }); for (read) |byte| chain.write(byte); try testing.expectEqual(@as(u64, 0), chain.drives[0].received); try testing.expectEqual(@as(?u8, null), chain.drives[0].unit); try testing.expect(chain.ready()); } test "a drive on the chain answers for itself and leaves the others alone" { var chain: Chain = .{}; var one: [4 * block_size]u8 = @splat(0x11); var two: [4 * block_size]u8 = @splat(0x22); chain.drives[0] = .{ .unit = 1, .fitted = true, .blocks = &one, .write_protected = false }; chain.drives[1] = .{ .unit = 2, .fitted = true, .blocks = &two, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var into: [1024]u8 = undefined; // Block nought from the second drive, which is the one with `$22` on it. const ask = try command(&wire, 0x02, kind_command, &.{ 0x01, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00 }); chain.write(ask[0]); for (ask[1..]) |byte| chain.write(byte); _ = chain.ready(); var n: usize = 0; while (chain.drives[1].state == .replying) : (n += 1) into[n] = chain.read(); chain.request(false); const reply = try cbus.decode(into[0..n], &back); try testing.expectEqual(@as(usize, block_size), reply.payload.len); try testing.expect(std.mem.allEqual(u8, reply.payload, 0x22)); // The first drive heard every byte of that and said nothing. try testing.expectEqual(@as(u64, 0), chain.drives[0].received); try testing.expectEqual(@as(u64, 1), chain.drives[1].received); } test "a busy drive holds the shared line down for the whole chain" { // Which is the point of asking all of them: the line is pulled low, so one // drive being busy is the line being down however ready the others are. var chain: Chain = .{}; var one: [4 * block_size]u8 = @splat(0); chain.drives[0] = .{ .unit = 1, .fitted = true, .blocks = &one, .write_protected = false }; chain.drives[1] = .{ .unit = 2, .fitted = true }; // Nothing going on: the line is up. try testing.expect(chain.ready()); // Give the first drive a packet, and it takes the line down to say so. var wire: [1024]u8 = undefined; const say = try command(&wire, 0x01, kind_command, &.{ 0x00, 0x03, 0x00, 0xdc, 0x00 }); for (say) |byte| chain.write(byte); try testing.expectEqual(State.acknowledging, chain.drives[0].state); try testing.expect(!chain.ready()); // An empty socket is not a drive holding anything down. chain.drives[1].fitted = false; chain.drives[0].reset(); try testing.expect(chain.ready()); } test "the drive says when the medium has changed, so that a session knows to write it back" { // What is in the drive lives in memory, and the file it came out of is // only caught up when something asks. That something needs to be told // there is anything to catch up on, and asking the drive to hand back // eight hundred kilobytes to compare would be a poor way to find out. var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var big: [1024]u8 = undefined; var back: [1024]u8 = undefined; // Reading changes nothing. const read = try command(&wire, 0x01, kind_command, &.{ 0x01, 0x03, 0x00, 0x20, 0x01, 0x00, 0x00 }); _ = try exchange(&drive, read, &back); try testing.expect(!drive.dirty); // A `WRITE BLOCK` on its own changes nothing either: the command only // says which block, and the contents come in the packet behind it. const say = try command(&wire, 0x01, kind_command, &.{ 0x02, 0x03, 0x00, 0x20, 0x02, 0x00, 0x00 }); _ = try exchange(&drive, say, &back); try testing.expect(!drive.dirty); const contents: [block_size]u8 = @splat(0x5a); const data = try command(&big, 0x01, kind_data, &contents); _ = try exchange(&drive, data, &back); try testing.expect(drive.dirty); // And laying the medium down is a change like any other. drive.dirty = false; const wipe = try command(&wire, 0x01, kind_command, &.{ 0x03, 0x01, 0x00 }); _ = try exchange(&drive, wipe, &back); try testing.expect(drive.dirty); } test "a disk with its tab across is not marked as wanting saving" { var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = true }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; const say = try command(&wire, 0x01, kind_command, &.{ 0x02, 0x03, 0x00, 0x20, 0x02, 0x00, 0x00 }); _ = try exchange(&drive, say, &back); const wipe = try command(&wire, 0x01, kind_command, &.{ 0x03, 0x01, 0x00 }); _ = try exchange(&drive, wipe, &back); try testing.expect(!drive.dirty); } test "the drive says what it is when asked for its information block" { // "Status Code = $03, Return Device Information Block" (Apple Computer, // Inc., n.d.-b, p. 21): twenty-five bytes saying what the device is, which // is how a host that knows about SmartPort finds out what it is talking to // rather than assuming. var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; const ask = try command(&wire, 0x01, kind_command, &.{ 0x00, 0x03, 0x00, 0x20, 0x03 }); const said = try exchange(&drive, ask, &back); var room: [1024]u8 = undefined; const answer = try cbus.decode(said, &room); try testing.expectEqual(@as(u8, 0x80), answer.header.status); try testing.expectEqual(@as(usize, 25), answer.payload.len); // The same general status byte the plain status call gives, then the size // again, then the name in a field of sixteen. try testing.expectEqual(drive.generalStatus(), answer.payload[0]); try testing.expectEqual(@as(u8, 4), answer.payload[1]); try testing.expectEqual(@as(u8, device_name.len), answer.payload[4]); try testing.expectEqualStrings("UNIDISK 3.5 ", answer.payload[5..21]); // "$01 $00 UniDisk 3.5" (Apple Computer, Inc., 1987a, p. 124). try testing.expectEqual(@as(u8, 0x01), answer.payload[21]); try testing.expectEqual(@as(u8, 0x00), answer.payload[22]); } test "the calls a block device does not have say so rather than saying nothing" { // OPEN, CLOSE, READ and WRITE are how a character device is worked, and // this is not one. Answering them with a status of nought would have a // caller believe a stream had been opened. var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var room: [1024]u8 = undefined; for ([_]u8{ 0x06, 0x07, 0x08, 0x09 }) |call| { const ask = try command(&wire, 0x01, kind_command, &.{ call, 0x01, 0x00 }); const said = try exchange(&drive, ask, &back); const answer = try cbus.decode(said, &room); try testing.expectEqual(@as(u8, 0x80 | error_bad_command), answer.header.status); } } test "a control call resets the drive, ejects the disk, or says the code is no good" { var image: [4 * block_size]u8 = @splat(0); var drive: Unidisk = .{ .fitted = true, .unit = 1, .blocks = &image, .write_protected = false }; var wire: [1024]u8 = undefined; var back: [1024]u8 = undefined; var room: [1024]u8 = undefined; // "$00 Reset the device", which leaves the disk where it is. const reset_it = try command(&wire, 0x01, kind_command, &.{ 0x04, 0x02, 0x00, 0x20, 0x00 }); var said = try exchange(&drive, reset_it, &back); var answer = try cbus.decode(said, &room); try testing.expectEqual(@as(u8, 0x80), answer.header.status); try testing.expect(drive.blocks != null); // "UniDisk 3.5 has no DCB, and returns an error (BADCTL $21)", and newline // belongs to character devices. for ([_]u8{ 0x01, 0x02, 0x05, 0xff }) |code| { const ask = try command(&wire, 0x01, kind_command, &.{ 0x04, 0x02, 0x00, 0x20, code }); said = try exchange(&drive, ask, &back); answer = try cbus.decode(said, &room); try testing.expectEqual(@as(u8, 0x80 | error_bad_control), answer.header.status); } // "$04 Eject Disk... Causes UniDisk 3.5 to auto-eject a disk." const eject = try command(&wire, 0x01, kind_command, &.{ 0x04, 0x02, 0x00, 0x20, 0x04 }); said = try exchange(&drive, eject, &back); answer = try cbus.decode(said, &room); try testing.expectEqual(@as(u8, 0x80), answer.header.status); try testing.expectEqual(@as(?[]u8, null), drive.blocks); try testing.expectEqual(@as(u64, 1), drive.ejected); // "No errors are returned if the disk ejected correctly or there was no // disk in the drive", so asking again is not an error either. said = try exchange(&drive, eject, &back); answer = try cbus.decode(said, &room); try testing.expectEqual(@as(u8, 0x80), answer.header.status); }