From e817ee5c7fb2ea47f5877c2e3ad65d71dbebfe0e Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 7 Sep 2026 21:44:48 -0500 Subject: [PATCH] the head follows SEL, so a IIc Plus formats both sides of every track The last thing standing between this machine and a formatted disk, and it was a mistake in the drive rather than anything to do with the IIc Plus. `disk35.zig` modelled the head as a latch that a *read* of register 1 or 3 sets, those two registers differing in nothing but the SEL bit. Reading is forgiving enough to hide that -- a firmware that finds itself on the wrong side reads an address field saying so and looks again -- so every read test passed throughout. Writing has no second chance. Over a IIc Plus's format all 1,457,979 bytes went to side nought, half the disk was never written, and the blame lay with 1,261 sense reads on register 1 that landed in the middle of runs of writing and quietly pulled the head back. Two tables on one page of *Inside Macintosh* settle it (Apple Computer, Inc., 1985c, p. III-35). The read table calls registers 1 and 3 "RDDATA0, read data, lower head" and "RDDATA1, read data, upper head" -- where the data comes *from*, with nothing said about a read leaving the drive on that head. The write table settles it from the other end: every control register is written with SEL at nought, "Next, set SEL to 0", so SEL is never spent on naming one and is free to be the head select the whole time. The circuit diagrams say it outright. On every Macintosh with a 3.5-inch drive the disk chip's own `HDSEL` output is marked *n.c.* and the line reaching the drive comes from VIA1 PA5 (Apple Computer, Inc., 1990, Figures 9-10 and 9-13, pp. 351 and 354, both checked against the page images). SEL is a plain latched output of the host -- bit 7 of `$c031` on a IIgs, the glue chip's `HDSEL` on a IIc Plus, a VIA pin on a Macintosh -- set by the firmware, held for as long as it works on one side, and connecting that head to the read amplifier and the write driver alike. This had been tried once before and appeared to fail, and the failure was borrowed: it was measured while `$ce40` and `$ce60` were modelled as one latch moved by a read or a write indifferently, so the firmware's writes to that pair dragged SEL about underneath its reads. With the two separated the measurement comes out the opposite way. Over a whole format SEL changes between one written byte and the next exactly 160 times -- twice per track for eighty tracks. It does not thrash. It sits still for a side and then moves. So `Drive.side` goes and `Controller.head()` returns SEL, and the format test comes in from the scratchpad where it has been waiting: it boots the System Disk, drives the System Utilities' Format a Disk to the drive on the back, swaps an unrecorded surface in when the program asks for the destination, and checks all 1600 blocks -- not a sample -- for a ProDOS volume of the name typed. The disk the machine booted from sits unprotected in the other drive throughout. The IIgs's own format test, every 3.5-inch read and write test, the 45 WOZ images and the four functional tests are unchanged by this. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01Aieuyb2KR2DsKyZWPJpas6 --- README.md | 152 +++++++++++++++++---------------- src/disk35.zig | 77 +++++++++++------ src/system_tests.zig | 195 +++++++++++++++++++++++++++++++++++++++++++ tools/bootgs.zig | 2 +- 4 files changed, 329 insertions(+), 97 deletions(-) diff --git a/README.md b/README.md index a3611c4..e6d91e6 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ has none of Apple's code on it. | `ii` | Apple II, 1977 | `apple2o.rom`, 12K | boots to the Monitor and Integer BASIC | | `ii+` | Apple II Plus, 1979 | `APPLE2.ROM`, 20K | boots to Applesoft; the same seven slots a IIe has | | `iie` | Apple IIe, 1983 and the 1985 enhanced | `APPLE2E.ROM` 32K, or `apple2e-unenhanced.rom` 16K | boots to Applesoft; boots disks; any card in any slot | -| `iic` | Apple IIc, all five ROM revisions | one of the five below | boots to Applesoft and off 5.25, 3.5 and UniDisk | +| `iic` | Apple IIc, all five ROM revisions | one of the five below | boots to Applesoft and off 5.25, 3.5 and UniDisk; reads, writes and formats | | `iii` | Apple III and III Plus, 1980 | `342-0031.bin`, 4K | boots SOS, Business BASIC, a ProFile, and Apple's own II emulation | | `iigs` | Apple IIgs, 1986 | `Apple IIGS ROM 01`, 128K, or a ROM 3, 256K | boots 5.25, 3.5 and a UniDisk on the port; reads, writes and formats | @@ -329,7 +329,7 @@ driver is in the machine's own ROM. | `$00` | `Apple IIc ROM 00 - 342-0033-A - 1985.bin`, 32K | UniDisk 3.5 support | boots 5.25 and a UniDisk 3.5 | | `$03` | `Apple IIc ROM 03 - 341-0445-A.bin`, 32K | the Memory Expansion machine | boots 5.25, memory card works | | `$04` | `Apple IIc ROM 04 - 341-0445-B.bin`, 32K | the last of them | boots 5.25, memory card works | -| `$05` | `Apple IIc ROM 05 - IIc Plus.bin`, 32K | the IIc Plus: 4 MHz, 3.5-inch drive in the case | boots its own drive, and either kind on the back | +| `$05` | `Apple IIc ROM 05 - IIc Plus.bin`, 32K | the IIc Plus: 4 MHz, 3.5-inch drive in the case | boots its own drive, and either kind on the back; formats a 3.5-inch disk | A IIc with the `$FF` ROM and no disk appears to hang, and that is what the hardware did: the firmware starts the drive and waits for a disk that is not @@ -928,8 +928,9 @@ addresses whose low bytes are the low bytes of soft switches, so a write allowed to fall through past them puts `$cc80` into the language card's sixteen and `$ccc0` into the memory expansion's. -A IIc Plus will now format a disk, and gets most of the way through one. What -was in the way was not the machine at all but the blank being offered to it. +A IIc Plus can be asked to format a disk, and getting it as far as trying was +the first half of the work. What was in the way then was not the machine at all +but the blank being offered to it. A disk with nothing on it was filled here with `$ff` and called self-sync, and that is wrong twice over: self-sync is what the *gaps on a formatted disk* @@ -942,74 +943,65 @@ IIgs copes with either and formats regardless. A IIc Plus, whose hunt for a noise it gives up on reading and lays the surface down instead, and the tests that make blanks now make them the way a drive would find them. -What it lays down is half a disk: eight hundred blocks of sixteen hundred, in -an alternating pattern twelve long, which is one track's sectors on the -outermost zone. It writes one side of every track and not the other. - -Where that happens is now known exactly. Every one of the million and a half -bytes goes to side nought, and it is not that the firmware never asks for the -other one — it chooses the upper head four hundred and nineteen times. The head -is chosen by *reading* register one or three, which selects that head as a side -effect, and those two registers differ in nothing but the SEL bit. During the -writing the lines sit on register one for seven hundred and thirty-one thousand -of the writes, and twelve hundred and fifty-nine sense reads land there while -they do — each one quietly pulling the head back to the lower side before a -byte goes down. - -So the question is not what selects a head but whether reading the sense line -should still be selecting one at a moment when the machine is writing, and that -wants settling against what the drive really does rather than against what -would make this test pass. `HDSEL`, the glue chip's "latched output bit used -for 3.5-inch drives", was the obvious suspect and is not the answer: driving -the head from the spare writes on the SEL pair stops the format finishing at -all. - -Reading the source that settles it points at this model rather than at the -machine. *Inside Macintosh* prints the table of which state-control lines -address which disk register, and the two in question are `RDDATA0`, "read data, -lower head", and `RDDATA1`, "read data, upper head" — the same four lines with -SEL the other way about (Apple Computer, Inc., 1985c). Apple describes them as -where the data *comes from*, and says nothing about reading one leaving the -drive on that head afterwards. This models it as a latch that a read sets, -which is a stronger claim than the table makes and is very likely an -over-reading of the article that was followed alongside it. A head that is -addressed rather than latched would be chosen by SEL at the moment of each -access, reading or writing, and could not be taken away by a stray sense read -while the machine was writing. - -That was tried, with the table open, and it does not hold as stated. Choosing -the head from SEL at the moment of each write scatters the bytes across both -sides and the format stops finishing at all, because SEL does not sit still -while a write goes on: the lines were measured alternating between two -registers throughout. So the drive must be holding the head somewhere across a -write rather than taking it from the lines byte by byte, and the latch here is -closer to right than the table alone suggests. - -What is left is narrower and stranger than it looked. The head is held, the -firmware does ask for the upper one, and something puts it back before any byte -is written. Where that happens is known to the instruction; why a real drive -would not do the same is not. The test stays out of the tree until it is. - -That last point has since been read from both ends, because it is the one that -decides whether the latch is reachable at all. Statically, no instruction in -either bank of the ROM names an address falling in any of the three unused -registers; what looks at first like a reference is a table of ascending bytes -in one place and a run of high-ASCII message text in another, each being -disassembled as though it were code. Dynamically, a IIc Plus booted with a -five-and-a-quarter-inch disk in the drive touches the three exactly nought -times. So the latch is not written through a register of its own, and where it -*is* written from was, at the time, unknown. - -It is known now, and it is not a disk register at all: `HDSEL` is the glue -chip's own latched output, and it moves on a **read** of `$ce40` or `$ce60` -(MG, 2018, MIG Address Space → MIG Control). That is why no search among the -IWM's registers could have found it, and it is why the earlier attempt failed -in the way it did — the head was driven from the spare *writes* on that pair, -which is the other latch on the same two addresses. This is a lead rather than -a fix: the format still lays down one side of every track, and whether a -`HDSEL` that the firmware sets deliberately can survive a sense read that this -model treats as choosing a head is the question that paragraph left open. The -test stays out of the tree. +What it laid down at first was half a disk: eight hundred blocks of sixteen +hundred, in an alternating pattern twelve long, which is one track's sectors on +the outermost zone. It wrote one side of every track and not the other. + +Every one of the million and a half bytes went to side nought, and it was not +that the firmware never asked for the other one — it chose the upper head four +hundred and nineteen times. The head was modelled here as a latch that a *read* +of register one or three sets, those two registers differing in nothing but the +SEL bit; and while the writing went on, twelve hundred and fifty-nine sense +reads landed on register one, each of them quietly pulling the head back to the +lower side before a byte went down. + +The latch was the mistake, and it was an over-reading of the source rather than +anything the source says. *Inside Macintosh* prints the table of which +state-control lines address which disk register, and the two in question are +`RDDATA0`, "read data, lower head", and `RDDATA1`, "read data, upper head" +(Apple Computer, Inc., 1985c, p. III-35). Apple describes them as where the +data *comes from*, and says nothing about reading one leaving the drive on that +head afterwards. The other table on the same page settles it from the far side: +every control register is written with SEL at nought — "Next, set SEL to 0" — +so SEL is never spent on naming one of them, and is free to be the head select +the whole time. + +The circuit diagrams say it outright. On every Macintosh with a 3.5-inch drive +the disk chip's own `HDSEL` output is marked *n.c.* and the line that actually +reaches the drive comes from VIA1 PA5 instead (Apple Computer, Inc., 1990, +Figures 9-10 and 9-13, pp. 351 and 354). SEL is a plain latched output of the +host — bit 7 of `$c031` on a IIgs, the glue chip's `HDSEL` on a IIc Plus, a VIA +pin on a Macintosh — which the firmware sets, holds for as long as it is +working on one side, and which connects that head to the read amplifier and the +write driver alike. + +Driving the head that way had been tried once before and appeared to fail, and +that is worth recording because the failure was borrowed from somewhere else. +It was measured while `$ce40` and `$ce60` were modelled as a single latch moved +by a read or a write indifferently, so the firmware's *writes* to that pair +were dragging SEL about underneath its reads. With the two latches separated +the measurement comes out the opposite way: over a whole format SEL changes +between one written byte and the next exactly a hundred and sixty times, which +is twice per track for eighty tracks. It does not thrash at all. It sits still +for a side and then moves. + +That also dissolves the question the three undocumented registers were being +searched for. Nothing writes the head latch through a register of its own — +statically no instruction in either ROM bank names one of the three, and +dynamically a booted IIc Plus touches them exactly nought times — because there +is no latch to write. Reading is forgiving enough to have hidden all of this: +a firmware that finds itself on the wrong side reads an address field saying so +and looks again, which is why every read test passed throughout. Writing has no +such second chance. + +So a IIc Plus formats a blank 3.5-inch disk end to end now, and the test that +says so is in the tree. It boots the System Disk, drives the System Utilities' +Format a Disk to the drive on the back, swaps an unrecorded surface in at the +moment the program asks for the destination disk, and then checks all sixteen +hundred blocks — not a sample — for a ProDOS volume carrying the name that was +typed. The disk the machine booted from sits unprotected in the other drive the +whole time, so a head or a drive select going astray would destroy it and be +seen. Reading the pins closely did settle one thing about the chip. Every address line it has is in its pinout — A5, A6 and A7 to pick the register, A9, A10 and @@ -6298,6 +6290,22 @@ them by — Apple published a great deal and dated little of it. [PDF](https://archive.org/download/Apple_IIgs_Hardware_Reference_HiRes/Apple_IIgs_Hardware_Reference_HiRes.pdf) is not. +- Apple Computer, Inc. (1990). *Guide to the Macintosh family hardware* (2nd + ed.). Addison-Wesley. + + + The successor to *Inside Macintosh*'s hardware chapter, and the source that + settled how a 3.5-inch drive picks a head. Chapter 9 draws the floppy + interface of every machine in the family as a circuit, and in each of those + drawings the disk chip's own `HDSEL` output is marked *n.c.* while the line + that reaches the drive comes from a VIA pin instead (Figures 9-10 and 9-13, + pp. 351 and 354). That is what says SEL is a plain latched output of the + host, held across a whole side's worth of writing, rather than something the + drive latches when a register is read — which is what `src/disk35.zig` had + and what left half of every formatted disk blank. The book covers the + Macintosh Plus through the IIfx, so it is also the last word on the SWIM + where the SWIM chip's own reference is thin. + - AppleWin. (n.d.). *AppleWin* [Computer software]. GitHub. diff --git a/src/disk35.zig b/src/disk35.zig index 31b04cb..b133e75 100644 --- a/src/disk35.zig +++ b/src/disk35.zig @@ -601,10 +601,6 @@ pub const Drive = struct { /// the drive steps whole ones when it is told to, and nothing outside it /// knows how that is done. track: u8 = 0, - /// Which of the two heads is reading. It is chosen by *reading* one of two - /// registers rather than by strobing anything, which is the one place in - /// this interface where asking a question has an effect. - side: u1 = 0, /// How far round the disk has turned, as a position in the encoded track. position: u16 = 0, /// Whether the spindle is running. On a Disk II the enable line and the @@ -753,18 +749,12 @@ pub const Controller = struct { if (!disk.fitted) return true; return switch (self.addressed()) { .direction_in => disk.outward, - // Choosing a head is done by asking which head is reading, which - // is the one place in this interface where a question has an - // effect. What comes back is a bit of the raw stream, and every - // byte that can be written to a disk has its top bit set. - .lower_head => blk: { - disk.side = 0; - break :blk true; - }, - .upper_head => blk: { - disk.side = 1; - break :blk true; - }, + // These two are not a choice but a place to look: they are the + // same signal off the two heads, and which one the lines are + // naming is which head SEL has already picked. What comes back is + // a bit of the raw stream, and every byte that can be written to + // a disk has its top bit set. See `head`. + .lower_head, .upper_head => true, .disk_in_place => disk.floppy == null, .step => cycles >= disk.settled_at, .write_protect => !self.writeProtected(), @@ -836,6 +826,36 @@ pub const Controller = struct { return (@as(u32, disk.position) * 120 / length) & 1 == 1; } + /// Which of the two heads is against the surface, which is SEL and + /// nothing else. + /// + /// SEL is not one of the phase lines: it is a latched output of the host — + /// bit 7 of `$c031` on a IIgs, `HDSEL` out of the glue chip on a IIc Plus, + /// VIA1 PA5 on a Macintosh, where the disk chip's own `HDSEL` pin is left + /// unconnected and the VIA's line goes to the drive instead (Apple + /// Computer, Inc., 1990, Figures 9-10 and 9-13, pp. 351 and 354). The + /// firmware sets it, holds it for as long as it is working on that side, + /// and the drive connects that head to the read amplifier and the write + /// driver alike. + /// + /// It is *also* the fourth bit of the register address, which is what made + /// this look like a latch: registers 1 and 3 differ in nothing else, and + /// Apple names them "RDDATA0, read data, lower head" and "RDDATA1, read + /// data, upper head" (Apple Computer, Inc., 1985c, p. III-35). Modelling + /// that as a read that *sets* the head is a stronger claim than the table + /// makes, and it is wrong in a way only writing shows: over a IIc Plus's + /// format the firmware holds SEL still for a whole track at a time and + /// changes it exactly twice per track, but sense reads on register 1 + /// land in the middle of every one of those runs. Latching pulled the head + /// back to the lower side before each byte went down and half the disk was + /// never written. Nothing in either the read table or the write table + /// needs a latch: every control register is written with SEL low (p. + /// III-35), so SEL is never spent on addressing one and is free to be the + /// head select the whole time. + pub fn head(self: *const Controller) u1 { + return @intFromBool(self.sel); + } + /// One byte past the head, reading. pub fn readNibble(self: *Controller, cycles: u64) u8 { self.spin(cycles); @@ -843,7 +863,7 @@ pub const Controller = struct { if (!disk.spindle) return 0; const floppy = disk.floppy orelse return 0; self.read += 1; - return floppy.byteAt(disk.track, disk.side, disk.position); + return floppy.byteAt(disk.track, self.head(), disk.position); } /// And one byte going down under it. @@ -853,7 +873,7 @@ pub const Controller = struct { if (!disk.spindle) return; const floppy = disk.floppy orelse return; self.written += 1; - floppy.writeAt(disk.track, disk.side, disk.position, value); + floppy.writeAt(disk.track, self.head(), disk.position, value); } /// Turns the disk by however much time has gone by, and never by nothing: @@ -1093,16 +1113,25 @@ test "the strobe is what makes the drive do something" { try testing.expect(!disk.spindle); } -test "asking which head is reading is what chooses one" { +test "SEL is what chooses a head, and asking which head is reading does not" { var controller: Controller = .{}; controller.enabled = true; - try testing.expectEqual(@as(u1, 0), controller.drive(0).side); + // SEL is the whole of it, and it is the same line whether the machine is + // about to read or about to write. + try testing.expectEqual(@as(u1, 0), controller.head()); + controller.sel = true; + try testing.expectEqual(@as(u1, 1), controller.head()); + controller.sel = false; + try testing.expectEqual(@as(u1, 0), controller.head()); + + // Naming register 1 or register 3 moves SEL, because SEL is that + // register's fourth address bit -- but the *read* changes nothing, which + // is the difference. A firmware holding the upper head and asking a + // question about the lower one keeps the upper head. select(&controller, .upper_head); + try testing.expectEqual(@as(u1, 1), controller.head()); _ = controller.sense(0); - try testing.expectEqual(@as(u1, 1), controller.drive(0).side); - select(&controller, .lower_head); - _ = controller.sense(0); - try testing.expectEqual(@as(u1, 0), controller.drive(0).side); + try testing.expectEqual(@as(u1, 1), controller.head()); } test "the disk turns only when the spindle does, and the head reads it" { diff --git a/src/system_tests.zig b/src/system_tests.zig index 74ffca4..47ce2a4 100644 --- a/src/system_tests.zig +++ b/src/system_tests.zig @@ -2078,3 +2078,198 @@ test "an Apple IIc Plus boots a five-and-a-quarter disk on the back of the machi // the MIG in reset and so cannot assert it. try testing.expect(!system.cpu.memory.disk.three_and_a_half); } +test "an Apple IIc Plus formats a blank 3.5-inch disk in the drive on the back" { + // The IIc Plus's half of the format proof. The IIgs lays a disk down + // through its own IWM; this machine lays one down through the glue chip's + // buffer, and every address field, data field and checksum on the surface + // is one the emulated drive wrote at this machine's direction. It also + // exercises the drive select under load: eighty tracks of writing all have + // to land in the drive on the back while the disk the machine booted from + // sits unprotected in the one in the case. + // + // It is the only test here that catches a head being chosen wrongly, and + // it is why the block-by-block check below covers all sixteen hundred + // rather than sampling. Reading is forgiving: a firmware that finds itself + // on the wrong side reads an address field that says so and looks again. + // Writing is not, and a model that latched the head on a register read + // laid every one of a million and a half bytes on side nought and left the + // odd-numbered halves of all eighty tracks blank. See `head` in + // `disk35.zig`. + const alloc = testing.allocator; + const rom = try catalogued(alloc, .roms, "Apple IIc ROM 05 - IIc Plus.bin"); + defer alloc.free(rom); + const file = try catalogued(alloc, .disks, "Apple II System Disk 3.2 on 800k image.2mg"); + defer alloc.free(file); + const opened = try diskfile.open(alloc, file, "system.2mg"); + defer opened.deinit(alloc); + const floppy = try alloc.create(disk35.Floppy); + defer alloc.destroy(floppy); + floppy.* = .{}; + try floppy.load(opened.image.data); + // The boot disk goes in unprotected on purpose, as the control: if the + // drive select went astray and the machine formatted what it booted from, + // nothing would stop it and the checks at the end would find it gone. + floppy.write_protected = false; + + // The blank: an unrecorded surface rather than an image, which is what + // self-sync reads as, with no address mark anywhere on it. + const blank = try alloc.create(disk35.Floppy); + defer alloc.destroy(blank); + blank.* = .{}; + // What an unrecorded surface reads as, which is noise and not a run of + // ones. The difference is the whole of why this test could not be written + // before: filled with `$ff` a blank disk hands the firmware self-sync for + // ever, and its hunt for a `D5` address mark at bank 1 `$e0cd` has no way + // out of that. Given noise the hunt fails the way it fails on a real blank + // disk, the firmware gives up on reading and lays the surface down + // instead. The same amplifier noise is what `disk2.zig` makes with + // `fakeBit` over unrecorded five-and-a-quarter surface, for the same + // reason and on the same authority (Morris, 2018). + var noise: u32 = 0x2545f491; + for (&blank.nibbles) |*byte| { + noise = noise *% 1664525 +% 1013904223; + byte.* = @truncate(noise >> 16); + } + blank.write_protected = false; + + // And a recorded disk to occupy the drive on the back while the machine + // starts, which the blank cannot do. Two things stand in the way of simply + // putting the blank there. This firmware hunts the `D5 AA 96` address mark + // in a loop at `$e0cd` that has no way out while the drive keeps handing + // it bytes, so an unrecorded surface in any drive at power-on holds the + // machine before it has even drawn its banner — the same shape as a Disk + // II with no disk in it, and no more a fault here than it is there. And + // ProDOS counts its devices once, as it starts, so a drive standing empty + // then is not one the Utilities can be pointed at afterwards. A disk that + // reads answers both: the machine boots past it and ProDOS counts it, and + // it is swapped for the blank at the point the program asks for the disk + // to be written. + // It carries a name of its own rather than the booted volume's, so that + // what the program says it is about to destroy names which disk it read. + const stand_in = try alloc.alloc(u8, disk35.image_size); + defer alloc.free(stand_in); + @memcpy(stand_in, opened.image.data[0..disk35.image_size]); + const header = 2 * prodos.block_size + 4; // the volume directory header + stand_in[header] = 0xf0 | @as(u8, "PLACEHOLDER".len); + @memcpy(stand_in[header + 1 ..][0.."PLACEHOLDER".len], "PLACEHOLDER"); + + { + const check = try prodos.Volume.open(stand_in); + var nb: [prodos.name_length]u8 = undefined; + try testing.expectEqualStrings("PLACEHOLDER", check.name(&nb)); + } + const placeholder = try alloc.create(disk35.Floppy); + defer alloc.destroy(placeholder); + placeholder.* = .{}; + try placeholder.load(stand_in); + placeholder.write_protected = false; + + var system = try powerOn(cpu8.CMOS, memory.AppleIIc(.{}), rom); + system.cpu.memory.disk.disks35.insert(0, floppy); + system.cpu.memory.disk.disks35.insert(1, placeholder); + system.cpu.reset(); + + // Boot the System Disk and take its Utilities to Format a Disk, three + // items down the first menu. Both disks are in before the machine starts, + // because ProDOS counts its devices once at boot: a drive that was empty + // then is not a drive the Utilities can be pointed at afterwards. + try system.waitFor("FastCopy", 600 * second); + try system.typeLine("1"); + system.run(60 * second); + try system.press(0x0a); + try system.press(0x0a); + try system.press(0x0a); + try system.press('\r'); + + // The Slot and Drive fields are not drawn until this screen is accepted, + // so the RETURN that draws them comes first; then U moves to the Drive + // field and a digit sets it. Drive 2 is the blank, on the back. + try system.waitFor("Where is your disk?", 60 * second); + try system.press('\r'); + try system.waitFor("Slot:", 60 * second); + try system.press(0x15); + try system.press('2'); + system.run(2 * second); + try system.expect("Drive: 2"); + try system.press('\r'); + + try system.waitFor("Select the Operating System", 60 * second); + try system.press('\r'); // ProDOS, the first of the two. + + try system.waitFor("Enter volume name", 60 * second); + for (0..prodos.name_length) |_| try system.press(0x7f); + try system.typeLine("IICPLUSFMT"); + + // Here is the swap the whole arrangement was for. The program asks for + // the destination disk before it reads it, which is the moment the disk + // that got the machine this far comes out of the drive on the back and + // the unrecorded one goes in — so everything written from here lands on a + // surface carrying nothing anybody put there, and there is no volume left + // on it for the program to stop and ask about destroying. + try system.waitFor("Please Place Destination Disk", 60 * second); + system.cpu.memory.disk.disks35.insert(1, blank); + try system.press('\r'); + + try system.waitFor("Formatting Complete", 900 * second); + + // Something really went down onto the surface. + try testing.expect(blank.dirty); + try testing.expect(system.cpu.memory.disk.disks35.written > 0); + + // Read the surface back. The buffer is handed in full of a byte no sector + // of a fresh ProDOS volume holds, and `save` leaves a sector it could not + // find exactly as given — so a block still full of it is one the machine + // never laid down. Checking every one covers the five zones, whose eighty + // tracks carry four different sector counts between them. + const sentinel = 0xe5; + const rendered = try alloc.alloc(u8, disk35.image_size); + defer alloc.free(rendered); + @memset(rendered, sentinel); + try blank.save(rendered); + + var missing: usize = 0; + var first_missing: usize = 0; + var block: usize = 0; + while (block * prodos.block_size < rendered.len) : (block += 1) { + const contents = rendered[block * prodos.block_size ..][0..prodos.block_size]; + if (std.mem.allEqual(u8, contents, sentinel)) { + if (missing == 0) first_missing = block; + missing += 1; + } + } + if (missing != 0) { + std.debug.print("{d} of {d} blocks were never written, from block {d}\n first missing: ", .{ + missing, rendered.len / prodos.block_size, first_missing, + }); + var shown: usize = 0; + var b: usize = 0; + while (b * prodos.block_size < rendered.len and shown < 24) : (b += 1) { + if (std.mem.allEqual(u8, rendered[b * prodos.block_size ..][0..prodos.block_size], sentinel)) { + std.debug.print("{d} ", .{b}); + shown += 1; + } + } + std.debug.print("\n", .{}); + return error.TheMachineLeftPartOfTheDiskUnformatted; + } + + // And what it laid down is a ProDOS volume: the name that was typed, the + // whole eight hundred kilobytes of it, and a directory that reads. + const volume = try prodos.Volume.open(rendered); + var name_buffer: [prodos.name_length]u8 = undefined; + try testing.expectEqualStrings("IICPLUSFMT", volume.name(&name_buffer)); + try testing.expectEqual(@as(u16, 1600), volume.blocks()); + try testing.expect(volume.freeBlocks() > 1500); + + // The disk in the case is untouched: it was writable throughout, so this + // is what says the machine formatted the drive it was told to. + const kept = try alloc.alloc(u8, disk35.image_size); + defer alloc.free(kept); + @memcpy(kept, opened.image.data[0..disk35.image_size]); + try floppy.save(kept); + const booted = try prodos.Volume.open(kept); + var booted_name: [prodos.name_length]u8 = undefined; + var path_buffer: [64]u8 = undefined; + const path = try std.fmt.bufPrint(&path_buffer, "/{s}/BASIC.SYSTEM", .{booted.name(&booted_name)}); + try testing.expect(booted.find(path) != null); +} diff --git a/tools/bootgs.zig b/tools/bootgs.zig index fb10456..52764a0 100644 --- a/tools/bootgs.zig +++ b/tools/bootgs.zig @@ -551,7 +551,7 @@ pub fn main(init: std.process.Init) !void { .{ if (mech.spindle) "on" else "off", mech.track, - mech.side, + disk35.head(), if (mech.floppy == null) "no disk in it" else "a disk in it", disk35.addressed().name(), }, -- knotmirror 0.0.1