diff --git a/README.md b/README.md index e6642bd..00ce3dd 100644 --- a/README.md +++ b/README.md @@ -407,9 +407,18 @@ machine vouching for itself. A disk whose tab is across answers `$2B NOWRITE` instead, which is what lets a program say so rather than watch a write vanish. The same trace showed the firmware asking for `STATUS`, which it never does -while booting. An empty answer satisfies it for now; what that call is supposed -to return — "number of blocks, ID string, device name, device type, device -firmware version" — is still not modelled. +while booting, and that call is now answered properly rather than with an empty +packet. Status code nought returns "four bytes", the first a general status byte +and the next three "the size in 512-byte blocks", low byte first. Two of that +byte's bits run the opposite way to the rest — "3 — 0 = format allowed" and +"2 — 0 = medium write protected" — which is the manual's own doing and was read +off the page rather than its OCR, that being exactly the kind of thing a scan +gets wrong. The codes a UniDisk does not answer are refused with `$21 BADCTL` +rather than guessed at, as the manual says they should be. + +Whether those two inverted bits were read right is not left to argument: with +`STATUS` answered, the machine still saves a file, which it would not do if it +had been told the disk was protected. The IIc Plus comes up. That machine has a chip no other IIc has — the multidrive interface glue, a custom part and two kilobytes of static RAM diff --git a/src/unidisk.zig b/src/unidisk.zig index e7a8955..f1f2ed4 100644 --- a/src/unidisk.zig +++ b/src/unidisk.zig @@ -80,6 +80,7 @@ pub const buffer_size = 1024; /// 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; @@ -290,6 +291,7 @@ pub const Unidisk = struct { fn reply(self: *Unidisk, to: cbus.Packet) void { var contents: []const u8 = &.{}; var status: u8 = 0x00; + var told: [4]u8 = undefined; // A data packet is not a command: it is the block a `WRITE BLOCK` // asked for, arriving behind it. @@ -357,6 +359,46 @@ pub const Unidisk = struct { } } 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 => { + // 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". So a one in + // bit three says formatting is *not* allowed, which is + // the truth here until `FORMAT` is modelled, and a one + // in bit two says the medium is *not* protected. + 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 + general |= 0x08; // bit 3: no formatting yet + const count: usize = if (self.blocks) |disk| disk.len / block_size else 0; + told = .{ + general, + @truncate(count), + @truncate(count >> 8), + @truncate(count >> 16), + }; + contents = &told; + }, + // "Status codes $01 and $02 are not supported by the + // UniDisk 3.5 and result in an error (BADCTL $21)" (p. 26). + 0x01, 0x02 => status = error_bad_control, + else => status = error_bad_control, + } + }, else => {}, } @@ -602,3 +644,76 @@ test "a disk with its tab across says so instead of being written to" { 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, .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 set says formatting is *not* allowed, which is the truth + // until `FORMAT` is modelled. + 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, .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 }; + 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, .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); + } +}