Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! The Disk II card's own logic state sequencer, run from its own PROM.//!//! This is not part of the emulator and is not how it reads a disk. It is a//! second opinion, and the only one available that is not somebody's reading of//! a manual: the card had a 256-byte PROM at P6 wired up as, in Sather's words,//! "a little 2 MHz computer" (Sather, 1985, Chapter 9), and running it over a//! bit stream says what the hardware would have handed the processor. What the//! drive in `src/disk2.zig` hands it should be the same bytes.//!//! ## How the PROM is wired//!//! Four of the ROM's own data outputs come back round to its address inputs//! through flip-flops clocked at 2 MHz, which is what makes it a state machine//! at all. They are not in an order anyone would guess, and getting it wrong//! produces a table that looks plausible and reads nothing. Sather (1985,//! Chapter 9): "It uses a 256-byte ROM with four of its data outputs (D4—D7)//! connected through flip-flops to four of its address inputs (A5, A0, A6, and//! A7)."//!//! The other four address lines are the ones the machine and the disk drive//! move, and the same chapter names them: "A1 — QA, the MSB of the data//! register. A2 — SHIFT/LOAD, the `$c08c,x`/`$c08d,x` switch. A3 — READ/WRITE,//! the `$c08e,x`/`$c08f,x` switch. A4 — The read pulse from the disk drive."//!//! So the address is, from the top: D7, D6, D4, the read pulse, Q7, Q6, QA,//! and D5. The four partitioning bits divide the PROM into the card's four//! modes, which is a thing the contents confirm on sight: every one of the//! sixteen states in the Q7 clear, Q6 set quarter holds `$0a` — go to state//! zero, shift the write protect signal in — which is exactly what asking//! about the tab does.//!//! The read pulse arrives inverted, which the contents also settle: with it//! the other way up the sequencer walks its counting states while a pulse is//! present rather than while it is absent, and a track reads as nothing.
const std = @import("std");
/// Sixteen states, sixteen partitions, one byte each.pub const size = 256;
/// The six things the sequencer can tell the data register to do. The command/// nibble has sixteen values and only these six are used, "due to redundant/// states which command the data register to do the same thing".const Command = enum(u4) { clear = 0x0, nothing = 0x8, shift_in_zero = 0x9, shift_in_write_protect = 0xa, load = 0xb, shift_in_one = 0xd, _,};
/// The sequencer reading a disk, one bit cell at a time.pub const Sequencer = struct { rom: *const [size]u8, state: u4 = 0, register: u8 = 0,
fn address(self: Sequencer, pulse: u1, q7: u1, q6: u1) u8 { const s = self.state; const qa: u8 = (self.register >> 7) & 1; return (@as(u8, (s >> 3) & 1) << 7) | (@as(u8, (s >> 2) & 1) << 6) | (@as(u8, (s >> 0) & 1) << 5) | (@as(u8, pulse) << 4) | (@as(u8, q7) << 3) | (@as(u8, q6) << 2) | (qa << 1) | @as(u8, (s >> 1) & 1); }
/// One 2 MHz clock. Returns the byte if this is the tick that finished /// one, which is the tick QA goes from clear to set. pub fn clock(self: *Sequencer, pulse: u1) ?u8 { const byte = self.rom[self.address(pulse, 0, 0)]; const before = self.register & 0x80; switch (@as(Command, @enumFromInt(@as(u4, @truncate(byte))))) { .clear => self.register = 0, .shift_in_zero => self.register <<= 1, .shift_in_one => self.register = (self.register << 1) | 1, // Nothing is write protected here: this reads a file. .shift_in_write_protect => self.register >>= 1, .nothing, .load => {}, _ => {}, } self.state = @truncate(byte >> 4); if (before == 0 and self.register & 0x80 != 0) return self.register; return null; }};
/// Runs the sequencer over a track and hands back each byte as it finishes.////// `timing` is how long a bit cell is in eighths of a microsecond, which at/// 2 MHz is that many quarters of a sequencer clock: the standard 32 is eight/// clocks, and a disk written at 3.5 microseconds is seven. The read pulse is/// a microsecond wide, which is two of them, and arrives at the start of the/// cell.pub fn readTrack(rom: *const [size]u8, bits: []const u8, count: u32, timing: u8, into: []u8) usize { var sequencer: Sequencer = .{ .rom = rom }; const clocks = @max(1, (@as(u32, timing) + 2) / 4); var got: usize = 0; for (0..count) |at| { const bit: u1 = @truncate(bits[at / 8] >> @intCast(7 - at % 8)); for (0..clocks) |clk| { // Active low into A4, and a microsecond of it. const pulse: u1 = if (bit == 1 and clk < 2) 0 else 1; if (sequencer.clock(pulse)) |byte| { if (got < into.len) { into[got] = byte; got += 1; } } } } return got;}