diff --git a/README.md b/README.md index e6d91e6..3417711 100644 --- a/README.md +++ b/README.md @@ -1011,6 +1011,47 @@ the other side: bank 1 leaves five hundred and twelve zero bytes there, two pages of hole rather than one, because both belong to the chip. The window is modelled that wide now. +That leaves the chip's fourth latched output, and it is placed now too. +`IWMRES*` is a "latched output bit to reset IWM" (Table 11-15, p. 328), which +MG puts at a write to `$cc40` while hedging it twice over — "(probably) +/IWMRES", "set (or pulsed?)" — and observing that a latch would want a second +address to let it go, for which there is "no evidence" (MG, 2018). + +Counting every access the firmware makes in `$c800-$cfff` over a boot and a +whole 800K format settles the parts of that a firmware can settle. `$cc40` is +written exactly **twice** and never read. `$cc00` is not touched **at all**, so +whatever it may do, nothing on this machine does it, and there is nothing here +for a latch to be released by; it is modelled as a pulse. `$ce80`, which "affects +signal on ENB2" and which MG says the firmware "does not appear to use", is +likewise touched nought times in either direction. + +Both writes come from the same three instructions, at bank 1 `$cd1c`: `STA +$CC40`, `STA $CC80`, `STA $CCC0`, `RTS`. That is the disk port being put back +to a known state — reset the chip, swing the drive 2 select in to the drive in +the case and back out to the port — and four places call it: twice around the +device scan at `$e603` and `$e652`, once in the retry loop at `$e524`, and once +at `$efc6`. A routine that resets a chip and clears its routing, called where +one would expect to find it, is about as much corroboration as a firmware can +give a pin description. + +What the pulse reaches is the disk chip's own reset line and nothing further, +which took splitting `reset` in two. Everything it clears is a register or an +addressable latch on that chip — the mode register, the two state flip flops, +the four phases, the drive select and the enable — and the enable is dropped +through the latch rather than by assignment, so a turning drive hears about it. +What it leaves alone is everything across the cable and everything across the +machine: where a head was left is mechanical, and whether the port is talking +to a 3.5-inch drive is `$c031` on a IIgs and the glue chip's own `3.5DRIVE*` +here, neither of which is on this line. + +One thing the firmware does that nothing can explain is worth writing down +rather than leaving out. The routine at bank 1 `$ef02`, on its way into the +SmartPort firmware, writes `$81` once each to `$c840`, `$c880` and `$c8c0`. MG +lists all three under MIG control and says nothing about what they do, and +nothing here can say either: they fall in the half of the window shared with +the ROM, so the write is dropped whether the chip is watching or not, and no +test that could be written here would tell those two apart. + The four-megahertz accelerator is still to come, and what is worth saying is why it is not a switch. It is a cache. The machine has a 65C02 clocked at four megahertz beside two eight-kilobyte static RAMs, one holding data and one diff --git a/src/iwm.zig b/src/iwm.zig index 500e4f9..8204264 100644 --- a/src/iwm.zig +++ b/src/iwm.zig @@ -450,19 +450,41 @@ pub const Iwm = struct { self.three_and_a_half = value & 0x40 != 0; } - /// What the reset line does. The chip's own registers go back, and so do - /// the motor and the magnets, which are one addressable latch that the bus - /// clears — so a reset out of a boot that is going nowhere stops the drive - /// rather than leaving it turning. Where the head was left is mechanical - /// and stays where it is, and so does whatever is in the drives. - pub fn reset(self: *Iwm) void { + /// What the chip's own reset pin does, and nothing more. + /// + /// Everything cleared here is a register or an addressable latch *on the + /// chip*: the mode register, the two state flip flops, the four phase + /// lines, the drive select and the enable. Dropping the enable is what + /// stops a turning drive, and it is dropped through `enable` rather than + /// by assignment so that it reaches the mechanism the way the bus would. + /// + /// What is deliberately left alone is everything on the other side of the + /// cable and everything on the other side of the machine. Where a head was + /// left is mechanical. Whether the port is talking to a 3.5-inch drive is + /// not the chip's to say — it is `$c031` on a IIgs and the glue chip's + /// `3.5DRIVE*` on a IIc Plus, and neither of those is on this reset line. + /// + /// A IIc Plus reaches this on its own, without the machine being reset: + /// `IWMRES*` is one of the glue chip's latched outputs and a write to + /// `$cc40` pulses it. See `mig.zig`. + pub fn resetChip(self: *Iwm, cycles: u64) void { + // The mode register goes first, so that dropping the enable below is + // not held up by a one-second timer this has just cleared. self.mode = 0; self.q6 = false; self.q7 = false; - self.enabled = false; - self.selected = 0; - self.three_and_a_half = false; self.lines = @splat(false); + self.choose(0); + self.enable(false, cycles); + } + + /// What the machine's reset line does, which is the chip's own reset and + /// then everything the chip is wired to. The drives go back, the Disk + /// Interface register's kind-of-drive bit goes back with them, and what is + /// in a drive stays in it. + pub fn reset(self: *Iwm) void { + self.resetChip(0); + self.three_and_a_half = false; self.drives.reset(); self.disks35.reset(); self.unidisk.reset(); @@ -722,3 +744,43 @@ test "the handshake will not take another byte until the last one has gone down" try testing.expectEqual(@as(u8, 0x00), five.handshake(1000 + five.drives.turning - 1)); try testing.expectEqual(@as(u8, 0x80), five.handshake(1000 + five.drives.turning)); } + +test "the chip's own reset line puts the chip back and leaves the drives alone" { + var iwm: Iwm = .{}; + iwm.drives.fitted = 2; + + // Put the chip somewhere: a mode register, both state flip flops, a + // couple of phases, the second drive, and the motor running. + _ = iwm.access(0xd, false, 0, 0); // Q6 high + _ = iwm.access(0xf, true, 0x1f, 0); // and Q7, so a write reaches the mode + _ = iwm.access(0xb, false, 0, 0); // drive two + _ = iwm.access(0x1, false, 0, 0); // phase 0 on + _ = iwm.access(0x9, false, 0, 0); // motor on + try testing.expect(iwm.enabled); + try testing.expectEqual(@as(u1, 1), iwm.selected); + try testing.expect(iwm.q6 and iwm.q7); + try testing.expect(iwm.lines[0]); + + // Where the head was left is mechanical, so put it somewhere too and + // watch it stay. + iwm.drives.drives[0].quarter_track = 68; + // And the kind of drive on the port, which on the two machines that have + // this chip is not the chip's line at all. + iwm.three_and_a_half = true; + + iwm.resetChip(0); + + try testing.expectEqual(@as(u8, 0), iwm.mode); + try testing.expect(!iwm.q6 and !iwm.q7); + try testing.expectEqual(@as(u1, 0), iwm.selected); + try testing.expect(!iwm.lines[0]); + try testing.expect(!iwm.enabled); + // The enable was dropped through the latch rather than assigned, so the + // mechanism heard about it: with the mode register cleared first there is + // no one-second timer left to hold the motor on. + try testing.expect(!iwm.disks35.enabled); + + // And these two are on the other side of the reset line. + try testing.expectEqual(@as(u8, 68), iwm.drives.drives[0].quarter_track); + try testing.expect(iwm.three_and_a_half); +} diff --git a/src/memory8/apple2e.zig b/src/memory8/apple2e.zig index 19a38e5..b87bb90 100644 --- a/src/memory8/apple2e.zig +++ b/src/memory8/apple2e.zig @@ -2249,6 +2249,16 @@ fn Machine(comptime kind: Kind, comptime options: Options) fn (type) type { // wrecks the machine within a few hundred instructions. if (mig_chip.Mig.strobes(address)) { self.mig.strobe(address, is_write); + // `IWMRES*` is a wire out of the glue chip and + // into the disk chip's reset pin, so the pulse is + // taken here rather than inside either of them. + // Only the chip goes back: the drives are on the + // other side of the cable, and which kind of drive + // the port is talking to is the glue chip's own + // line and not on this one. + if (self.mig.takeIwmReset()) { + self.disk.resetChip(self.getCPU().clock.cycles); + } self.syncDiskPort(); return 0; } diff --git a/src/mig.zig b/src/mig.zig index 39abce4..9c6e027 100644 --- a/src/mig.zig +++ b/src/mig.zig @@ -39,6 +39,19 @@ //! and `HDSEL` "latched output bit used for 3.5-inch drives" — and stops //! there. The rest is read out of the ROM 5 firmware, and each piece says //! below where it came from. +//! +//! All four of those latched outputs are placed. What is left of the chip is +//! two things the firmware never touches and one it does. `$ce80` "affects +//! signal on ENB2" and the firmware "does not appear to use" it (MG, 2018); +//! counting every access in `$c800-$cfff` over a boot and a whole 800K format +//! agrees — not once, either way. Neither is `$cc00`, which is where a second +//! address for the supposedly latched `IWMRES*` would sit if there were one. +//! The one it does touch is `$c840`, `$c880` and `$c8c0`, written `$81` once +//! each from the routine at bank 1 `$ef02` on its way into the SmartPort +//! firmware. MG lists all three under MIG control and says nothing about what +//! they do, and nothing here can say either: they are in the half of the +//! window that is shared with the ROM, so a write to one is dropped whether +//! the chip is watching or not, and no test can tell the two apart. const std = @import("std"); @@ -200,6 +213,30 @@ pub const Mig = struct { /// say when it wants the port outside instead. internal_enabled: bool = true, + /// `IWMRES*`, "latched output bit to reset IWM" (Apple Computer, Inc., + /// 1989b, Table 11-15, p. 328), pulsed by a write to `$cc40`. + /// + /// This is a pulse and not a level, and the hedge belongs on it. MG has it + /// as "(probably) /IWMRES" and "set (or pulsed?) by W $CC40", and says the + /// manual calling it latched "would expect a $CC00/$CC40 combo, but no + /// evidence for that" (MG, 2018, MIG Address Space → MIG Control). The + /// firmware settles that much from this end: over a boot of ROM 5 and a + /// whole 800K format, `$cc00` is not read or written **once**, so if there + /// is a second address that lets the line go it is not one this machine + /// uses. Nothing is latched here. + /// + /// It is written by one routine and only one, three instructions at + /// bank 1 `$cd1c` — `STA $CC40`, `STA $CC80`, `STA $CCC0`, `RTS` — which + /// resets the chip and then swings the drive 2 select to the internal + /// drive and back out again. Four places call it: twice around the device + /// scan at `$e603` and `$e652`, once in the retry loop at `$e524`, and + /// once at `$efc6`. It is "put the disk port back to a known state", and + /// over the same boot and format it runs exactly twice. + /// + /// The flag is read and cleared by whoever is holding the IWM, since the + /// chip's outputs are wires and this one goes to a pin on another chip. + iwm_reset: bool = false, + /// Whichever of the eight an address in the chip's page falls in. fn registerOf(address: u16) Register { return @enumFromInt(@as(u3, @truncate(address >> 5))); @@ -286,11 +323,13 @@ pub const Mig = struct { /// because **the chip has no data bus pins at all** (MG, 2018, "Analysis") /// and so can never drive a byte. What it does is watch the bus and latch, /// which is why the firmware reaches them with writes: `$cc80` and `$ccc0` - /// are where the drive 2 select is pointed, and `$cc40` is "(probably) - /// /IWMRES" — hedged there, and hedged here, and not modelled. + /// are where the drive 2 select is pointed, and `$cc40` resets the IWM. pub fn strobe(self: *Mig, address: u16, is_write: bool) void { if (!is_write) return; switch (address & 0x0fe0) { + 0x0c40 => { + self.iwm_reset = true; + }, 0x0c80 => { self.internal_enabled = true; }, @@ -301,11 +340,19 @@ pub const Mig = struct { } } + /// Takes the `IWMRES*` pulse, if there has been one since it was last + /// asked. A pulse is a thing that happened rather than a state, so it is + /// consumed by reading it. + pub fn takeIwmReset(self: *Mig) bool { + defer self.iwm_reset = false; + return self.iwm_reset; + } + /// Whether an address is one of those strobes. The chip decodes A5 to A11 /// and nothing above, so this is a question about eleven address lines. pub fn strobes(address: u16) bool { return switch (address & 0x0fe0) { - 0x0c80, 0x0cc0 => true, + 0x0c40, 0x0c80, 0x0cc0 => true, else => false, }; } @@ -389,3 +436,53 @@ test "the chip has no A8 and so answers the same at $cf00 as at $ce00" { _ = mig.access(0xcea0, false, 0); try testing.expectEqual(@as(u6, 0), mig.page); } + +test "a write to $cc40 pulses the IWM's reset line, and taking the pulse clears it" { + var mig: Mig = .{}; + try testing.expect(!mig.iwm_reset); + try testing.expect(!mig.takeIwmReset()); + + mig.strobe(0xcc40, true); + try testing.expect(mig.iwm_reset); + // A pulse is a thing that happened, so it is gone once it has been + // acted on, and a second look finds nothing to do. + try testing.expect(mig.takeIwmReset()); + try testing.expect(!mig.takeIwmReset()); + + // It is a write and nothing else. Reads of these addresses are the ROM + // being executed -- there is code at bank 1 $cc40 -- and the chip must + // not take an instruction fetch for a command. + mig.strobe(0xcc40, false); + try testing.expect(!mig.iwm_reset); +} + +test "$cc00 is not one of the chip's addresses, and this firmware never asks it to be" { + // The manual calls IWMRES* latched, which would want a second address to + // let it go again, and $cc00 is where such a thing would sit. MG looked + // for one and found "no evidence for that" (MG, 2018). + // + // ROM 5 settles it from this end. Over a boot and a whole 800K format -- + // every access in $c800-$cfff counted with the auxiliary bank selected -- + // $cc00 is not read or written once, while $cc40 is written exactly twice + // and $cc80 and $ccc0 two hundred and twenty-five and twenty-four times. + // So whatever $cc00 may do, nothing in this machine does it. + try testing.expect(!Mig.strobes(0xcc00)); + + var mig: Mig = .{}; + mig.strobe(0xcc00, true); + try testing.expect(!mig.iwm_reset); + try testing.expect(mig.internal_enabled); +} + +test "the strobes are the three the chip decodes, and nothing next to them" { + // A5 to A11 and no more, so the question is about eleven address lines + // and the three chunks that answer are 32 bytes apart. + try testing.expect(Mig.strobes(0xcc40)); + try testing.expect(Mig.strobes(0xcc5f)); + try testing.expect(Mig.strobes(0xcc80)); + try testing.expect(Mig.strobes(0xccc0)); + try testing.expect(!Mig.strobes(0xcc20)); + try testing.expect(!Mig.strobes(0xcc60)); + try testing.expect(!Mig.strobes(0xcca0)); + try testing.expect(!Mig.strobes(0xcce0)); +}