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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! Runs an Apple IIgs ROM against the model in memory16/apple2gs.zig and says//! what the machine asked for that it has not got.//!//! There is no corpus for a machine, and this is the nearest thing to one: the//! ROM does a self-test when the power comes on, and if the model is honest//! about what is missing rather than inventing answers, the ROM's own//! behaviour is the specification for what to build next. What comes out is a//! tally of soft switches, most-asked-for first, which is the order worth//! working in — whatever the firmware is stuck in a loop on is at the top.//!//! It is also how the model gets checked at all. A machine that reaches native//! mode and stays in ROM is doing something; one that ends up executing bank//! $00 full of zeroes is not, and the time-by-bank line says which.
const std = @import("std");const zig65 = @import("zig65");const ltoudp = @import("ltoudp");
const CPU = zig65.cpu16.Chip(.{}, zig65.clock.FakeClock, zig65.memory16.AppleIIgs(.{}));
/// One frame, one line: the cycle, which way it went, and its bytes.fn logFrame(io: std.Io, file: std.Io.File, cycle: u64, direction: []const u8, bytes: []const u8) void { var line: [3 * zig65.scc.Scc.Frame.longest + 48]u8 = undefined; var w: std.Io.Writer = .fixed(&line); w.print("{d} {s}", .{ cycle, direction }) catch return; for (bytes) |byte| w.print(" {x:0>2}", .{byte}) catch return; w.writeByte('\n') catch return; file.writeStreamingAll(io, w.buffered()) catch {};}
/// Turns `\r` in an argument into a carriage return, since a shell will not./// Nothing else is escaped: this is a way to press return, not a language.fn unescape(text: []const u8, into: []u8) []const u8 { var length: usize = 0; var at: usize = 0; while (at < text.len and length < into.len) : (length += 1) { if (text[at] == '\\' and at + 1 < text.len and text[at + 1] == 'r') { into[length] = '\r'; at += 2; } else { into[length] = text[at]; at += 1; } } return into[0..length];}
pub fn main(init: std.process.Init) !void { const io = init.io; var args: std.process.Args.Iterator = .init(init.minimal.args); _ = args.skip(); // The positional arguments, with `--audio PATH` picked out of them // wherever it appears, so that adding a switch does not disturb the order // of what was already there. var positional: [8][]const u8 = undefined; var given: usize = 0; var audio: ?[]const u8 = null; var fast_banks: ?u16 = null; var appletalk = false; var startup_slot: ?u8 = null; // Addresses to count the processor arriving at, for finding out whether // a routine runs at all before working out why it does what it does. var watched: [16]u32 = undefined; var watched_hits: [16]u64 = @splat(0); var watched_first: [16]u64 = @splat(0); var watching: usize = 0; // A window of the machine's path: every address the processor visits // from one cycle on, up to a count, printed as it goes. var trace_from: ?u64 = null; var trace_at: ?u32 = null; // Every access to the serial chip, written as it happens rather than // kept: the ring in the chip holds the end of the conversation and a // firmware talking to it at full speed has a great deal more to say. var scc_log_path: ?[]const u8 = null; // Whether to put the machine on the LocalTalk group, so that whatever // else is on it can answer the firmware. var localtalk = false; var localtalk_log_path: ?[]const u8 = null; var localtalk_interface: ?[4]u8 = null; var trace_count: usize = 2000; while (args.next()) |arg| { if (std.mem.eql(u8, arg, "--audio")) { audio = args.next() orelse ""; } else if (std.mem.eql(u8, arg, "--banks")) { fast_banks = std.fmt.parseInt(u16, args.next() orelse "2", 10) catch 2; } else if (std.mem.eql(u8, arg, "--appletalk")) { appletalk = true; } else if (std.mem.eql(u8, arg, "--watch")) { var list = std.mem.splitScalar(u8, args.next() orelse "", ','); while (list.next()) |item| { if (watching == watched.len) break; watched[watching] = std.fmt.parseInt(u32, item, 16) catch continue; watching += 1; } } else if (std.mem.eql(u8, arg, "--trace-from")) { trace_from = std.fmt.parseInt(u64, args.next() orelse "0", 10) catch 0; } else if (std.mem.eql(u8, arg, "--localtalk")) { localtalk = true; } else if (std.mem.eql(u8, arg, "--localtalk-log")) { localtalk_log_path = args.next() orelse ""; } else if (std.mem.eql(u8, arg, "--localtalk-interface")) { localtalk_interface = zig65.ethernet.parseAddress(args.next() orelse ""); } else if (std.mem.eql(u8, arg, "--scc-log")) { scc_log_path = args.next() orelse ""; } else if (std.mem.eql(u8, arg, "--trace-at")) { trace_at = std.fmt.parseInt(u32, args.next() orelse "0", 16) catch 0; } else if (std.mem.eql(u8, arg, "--trace-count")) { trace_count = std.fmt.parseInt(usize, args.next() orelse "2000", 10) catch 2000; } else if (std.mem.eql(u8, arg, "--startup-slot")) { startup_slot = std.fmt.parseInt(u8, args.next() orelse "7", 10) catch 7; } else if (given < positional.len) { positional[given] = arg; given += 1; } } if (given == 0) { std.debug.print( \\usage: bootgs ROM [INSTRUCTIONS] [DISK] [SCREEN] [KEYS] \\ [--audio PATH] [--banks N] \\ [--appletalk] [--startup-slot N] \\ [--localtalk] [--localtalk-interface ADDR] [--localtalk-log PATH] \\ [--scc-log PATH] [--watch ADDR[,ADDR...]] \\ [--trace-at ADDR | --trace-from CYCLE] [--trace-count N] \\ \\Runs a IIgs system ROM against the model and reports what it wanted. \\INSTRUCTIONS defaults to two million, which is enough to get well \\into the firmware's self-test. DISK is a 5.25-inch image to put in \\the built-in drive, which is what the firmware goes looking for \\once it has finished testing itself. \\ \\--audio writes what the Ensoniq played to PATH, as a WAV, which \\is how the sound is listened to without a terminal in the way. \\--banks says how much fast RAM the machine has, in 64K banks: \\2 is a machine with nothing in its expansion slot. \\ \\--appletalk sets the Control Panel to the built-in AppleTalk once \\the firmware has written its defaults, and --startup-slot N sets \\the Startup Slot: 7 is what makes the firmware bring the network \\up by itself. --localtalk puts the machine on the LocalTalk over \\UDP group, through the interface at ADDR if one is named, and \\--localtalk-log writes every frame that crosses. \\ \\--scc-log writes every access to the serial chip as it happens. \\--watch counts the processor arriving at each address, in hex. \\--trace-at prints every instruction from the first arrival at \\ADDR, --trace-from from CYCLE, for --trace-count of them. \\ , .{}); std.process.exit(2); } const path = positional[0]; const steps = if (given > 1) try std.fmt.parseInt(usize, positional[1], 10) else 2_000_000; const disk_path: ?[]const u8 = if (given > 2) positional[2] else null; const screen_path: ?[]const u8 = if (given > 3) positional[3] else null; const keys: ?[]const u8 = if (given > 4) positional[4] else null;
const gpa = std.heap.smp_allocator; const rom = try std.Io.Dir.cwd().readFileAlloc(io, path, gpa, .unlimited); defer gpa.free(rom);
var cpu: CPU = undefined; try cpu.init(gpa, null); defer cpu.deinit(); try cpu.memory.loadROM(rom);
// A disk in whichever built-in drive it fits. The image has to be // nibblised before a head can read anything off it, which is what the two // Floppy.load routines do; for a 5.25 the sector order also comes from the // file's extension, because nothing inside a .dsk or .po says which it is. const floppy = try gpa.create(zig65.disk2.Floppy); defer gpa.destroy(floppy); floppy.* = .{}; const floppy35 = try gpa.create(zig65.disk35.Floppy); defer gpa.destroy(floppy35); floppy35.* = .{}; if (disk_path) |disk| { const file = try std.Io.Dir.cwd().readFileAlloc(io, disk, gpa, .unlimited); defer gpa.free(file); // A 2IMG says which way round its sectors are; a ShrinkIt archive is // unpacked and the disk taken out of it; a bare image leaves the // question to the file's name. const opened = try zig65.diskfile.open(gpa, file, disk); defer opened.deinit(gpa); const image = opened.image; const order = image.order() orelse return error.NibbleImage; // Write enabled, because nothing here is ever saved back: what is in // the drive is a copy in memory, and a machine that cannot write to // its disk cannot be made to do very much. if (floppy35.load(image.data)) |_| { floppy35.write_protected = image.write_protected; cpu.memory.iwm.disks35.insert(0, floppy35); } else |_| { try floppy.load(image.data, order); floppy.write_protected = image.write_protected; cpu.memory.iwm.drives.insert(0, floppy); } }
// The clock, set from the host once. After that it keeps time by the // machine's own cycles, as everything else here does. The host's clock is // read as UTC and this machine knows nothing of time zones, so a file // saved on it is stamped in UTC. { const nanoseconds = std.Io.Timestamp.now(io, .real).nanoseconds; const unix: i64 = @intCast(@divFloor(nanoseconds, std.time.ns_per_s)); cpu.memory.rtc.set(@intCast(unix + zig65.rtc.Rtc.epoch_1904), 0); }
// A memory expansion card, if one was asked for. The firmware sizes // memory at power-on by probing each bank, so fitting one is nothing more // than letting the RAM answer. if (fast_banks) |count| try cpu.memory.expand(count); cpu.reset();
// AppleTalk is a Control Panel setting, and the Control Panel lives in // the parameter RAM, so this does what an owner did: lets the machine // come up once and write its defaults, sets slot 7 to the built-in // AppleTalk and slot 1 to "Your card" so that the printer port is free // for it, and starts the machine again. if (appletalk or startup_slot != null) { var ran: usize = 0; while (ran < 4_000_000 and !zig65.rtc.Rtc.sealed(&cpu.memory.rtc.ram)) : (ran += 1) { cpu.executeInstruction() catch break; } if (!zig65.rtc.Rtc.sealed(&cpu.memory.rtc.ram)) { std.debug.print("bootgs: the firmware never wrote its parameter RAM, so there is no Control Panel to set\n", .{}); return; } // Which port AppleTalk takes is said in slot 1's byte, and the two // ROMs say it differently: ROM 01 gives slot 1 two choices, printer // port or "Your card", and a card there is what frees the port for // AppleTalk; ROM 3 gives it a third, AppleTalk itself, and refuses // the network unless one of the two ports says so. Either firmware // throws the whole parameter RAM away and writes its defaults over // a value it does not know, so the third choice is tried first and // the second is what a ROM 01 gets after it has done that. const slot_1_choices = [_]u8{ 2, 1 }; for (slot_1_choices) |choice| { if (appletalk) { cpu.memory.rtc.ram[zig65.rtc.Rtc.slot_7] = 0; cpu.memory.rtc.ram[zig65.rtc.Rtc.slot_1] = choice; cpu.memory.rtc.ram[zig65.rtc.Rtc.slot_1_appletalk] = choice; } if (startup_slot) |slot| cpu.memory.rtc.ram[zig65.rtc.Rtc.startup_slot] = slot; zig65.rtc.Rtc.seal(&cpu.memory.rtc.ram); cpu.reset(); if (!appletalk) break; // Long enough for the firmware to have checked it and, if it // objected, to have written its defaults back. ran = 0; while (ran < 400_000 and cpu.memory.rtc.ram[zig65.rtc.Rtc.slot_7] == 0) : (ran += 1) { cpu.executeInstruction() catch break; } if (cpu.memory.rtc.ram[zig65.rtc.Rtc.slot_7] == 0) break; } }
var buf: [8192]u8 = undefined; var out = std.Io.File.stdout().writer(io, &buf); const w = &out.interface; try w.print("reset to {x:0>2}:{x:0>4}\n", .{ cpu.registers.program_bank, cpu.program_counter.value });
// Where it spends its time, by bank, so a machine that has run off the // rails is obvious rather than merely unproductive. var banks: [256]u64 = @splat(0); // And by address within the bank, because a machine that is answering // every switch it asks about and still not getting anywhere is stuck on // something that is not a switch, and the only way to find out what is to // see where it is going round. const visits = try gpa.alloc(u32, 1 << 16); defer gpa.free(visits); @memset(visits, 0); var last: u24 = 0; var stuck: u64 = 0; // The last few addresses, with their banks. The tally above throws the // bank away so that a routine shows up whichever bank it ran in, which is // what you want for a profile and exactly what you do not want when the // question is which bank a loop is going round in. var recent: [32]u24 = @splat(0); var recorded: usize = 0;
// The sound, if it was asked for. A IIgs has one speaker, so one channel. const sample_rate = 44_100; var recorder: ?zig65.ensoniq.Recorder = if (audio) |where| .{ .file = try zig65.wav.Writer.create(io, gpa, where, sample_rate, 1), .cycles_per_second = @TypeOf(cpu.memory).cycles_per_frame * 60, .rate = sample_rate, .started = cpu.clock.cycles, } else null; defer if (recorder) |rec| if (rec.file) |f| f.finish(io, gpa); if (recorder) |*rec| cpu.memory.sound = rec;
var scc_log: ?std.Io.File = null; defer if (scc_log) |file| file.close(io); var scc_log_buf: [16384]u8 = undefined; var scc_log_writer: ?std.Io.File.Writer = null; if (scc_log_path) |log_path| { scc_log = try std.Io.Dir.cwd().createFile(io, log_path, .{}); scc_log_writer = scc_log.?.writer(io, &scc_log_buf); } defer if (scc_log_writer) |*lw| lw.interface.flush() catch {}; var scc_logged: usize = 0;
var network: ?ltoudp.Network = null; if (localtalk) { network = ltoudp.Network.open(@truncate(@as(u96, @bitCast(std.Io.Timestamp.now(io, .real).nanoseconds))), localtalk_interface) catch |err| { std.debug.print("bootgs: cannot join the LocalTalk group: {s}\n", .{@errorName(err)}); return; }; } defer if (network) |*net| net.close(); var localtalk_log: ?std.Io.File = null; defer if (localtalk_log) |file| file.close(io); if (localtalk_log_path) |log_path| localtalk_log = try std.Io.Dir.cwd().createFile(io, log_path, .{}); var pumped: usize = 0;
for (0..steps) |_| { const at = cpu.program_counter.address(); banks[at >> 16] += 1; // The network, looked at every so often, as the front end looks at // it once a frame: what the machine sent goes out, and what came in // goes onto the chip's wire and is said. pumped += 1; if (pumped == 2048) { pumped = 0; if (network) |*net| { const scc = &cpu.memory.scc; if (localtalk_log) |file| if (net.bridge.attach(scc)) |which| { for (scc.channelOf(which).sentFrames()) |frame| logFrame(io, file, cpu.clock.cycles, "out", frame.payload()); }; net.bridge.drain(scc, cpu.clock.cycles, net); var buffer: [zig65.localtalk.longest]u8 = undefined; for (0..8) |_| { const datagram = net.receive(&buffer) orelse break; const before = net.bridge.received; net.bridge.deliver(scc, datagram, cpu.clock.cycles); if (localtalk_log) |file| if (net.bridge.received != before) { logFrame(io, file, cpu.clock.cycles, "in ", datagram[zig65.localtalk.tag_len..]); }; } } } if (scc_log_writer) |*lw| { const scc = &cpu.memory.scc; while (scc_logged < scc.traced) : (scc_logged += 1) { const e = scc.trace[scc_logged % scc.trace.len]; const lwi = &lw.interface; if (e.data) try lwi.print("{d} {s} data {s} {x:0>2}\n", .{ e.cycle, @tagName(e.channel), if (e.reading) "->" else "<-", e.value }) else try lwi.print("{d} {s} {s}{d} {s} {x:0>2}\n", .{ e.cycle, @tagName(e.channel), if (e.reading) "RR" else "WR", e.register, if (e.reading) "->" else "<-", e.value }); } } if (trace_at) |address| { if (at == address) { trace_from = cpu.clock.cycles; trace_at = null; } } if (trace_from) |from| { if (cpu.clock.cycles >= from and trace_count != 0) { trace_count -= 1; std.debug.print("{d} {x:0>6} A={x:0>4} X={x:0>4} Y={x:0>4} P={x:0>2}\n", .{ cpu.clock.cycles, at, cpu.registers.get(.A), cpu.registers.get(.X), cpu.registers.get(.Y), cpu.flags.dump(), }); } } for (watched[0..watching], 0..) |address, i| { if (at == address) { if (watched_hits[i] == 0) watched_first[i] = cpu.clock.cycles; watched_hits[i] += 1; } } visits[@as(u16, @truncate(at))] +|= 1; // Runs of the same address collapse, so that a machine sitting in a // one-instruction loop still shows how it got there rather than // filling the whole trace with where it stopped. if (recorded == 0 or recent[(recorded - 1) % recent.len] != at) { recent[recorded % recent.len] = at; recorded += 1; } if (at == last) stuck += 1 else stuck = 0; last = at; cpu.executeInstruction() catch |err| { try w.print("stopped: {s} at {x:0>6}\n", .{ @errorName(err), at }); break; }; if (cpu.stopped) { try w.print("the processor stopped itself at {x:0>6}\n", .{at}); break; } } // And then whatever was to be typed, which is the only way to make this // machine do something it was not going to do on its own. There is one // keyboard latch and no queue behind it, and the firmware clears the latch // on its way in to reading a line, so each key waits until the machine is // sitting in a read loop asking for one and coming away empty. if (keys) |text| { var typed: usize = 0; var unescaped: [256]u8 = undefined; const wanted = unescape(text, &unescaped); for (wanted) |key| { var waited: usize = 0; while (cpu.memory.mega2.strobe or cpu.memory.mega2.empty_polls < 64) { cpu.executeInstruction() catch {}; waited += 1; if (waited > 20_000_000) break; } if (waited > 20_000_000) break; cpu.memory.mega2.pressKey(key); typed += 1; } // And a while afterwards for whatever was typed to happen. for (0..steps / 2) |_| cpu.executeInstruction() catch {}; try w.print("typed {d} of {d} keys\n", .{ typed, wanted.len }); }
try w.print("after {d} instructions and {d} cycles, at {x:0>2}:{x:0>4}\n", .{ steps, cpu.clock.cycles, cpu.registers.program_bank, cpu.program_counter.value, }); try w.print("emulation={} m={} x={} i={}\n", .{ cpu.flags.emulation, cpu.flags.isSet(.M), cpu.flags.isSet(.X), cpu.flags.isSet(.I), }); for (watched[0..watching], 0..) |address, i| { try w.print("watched {x:0>6}: reached {d} times", .{ address, watched_hits[i] }); if (watched_hits[i] != 0) try w.print(", first at cycle {d}", .{watched_first[i]}); try w.writeAll("\n"); } try w.writeAll("time by bank: "); for (banks, 0..) |n, b| if (n != 0) try w.print("${x:0>2}={d} ", .{ b, n }); try w.writeAll("\n");
// The handful of addresses it kept coming back to. var order: [16]u32 = @splat(0); var top: [16]u16 = @splat(0); for (visits, 0..) |count, address| { if (count <= order[15]) continue; var i: usize = 15; while (i > 0 and order[i - 1] < count) : (i -= 1) { order[i] = order[i - 1]; top[i] = top[i - 1]; } order[i] = count; top[i] = @intCast(address); } try w.writeAll("most visited addresses:"); for (top, order) |address, count| { if (count == 0) break; try w.print(" ${x:0>4}={d}", .{ address, count }); } try w.writeAll("\n\n");
// Everything the machine said to the desktop bus, which is a // conversation and reads as one. const said = @min(cpu.memory.adb.commands, cpu.memory.adb.trace.len); if (said != 0) { try w.print("\n{d} bytes to the desktop bus:", .{cpu.memory.adb.commands}); for (cpu.memory.adb.trace[0..said]) |b| try w.print(" {x:0>2}", .{b}); try w.writeAll("\n"); }
{ try w.writeAll("\nthe last few addresses, in order, without repeats:\n "); const shown = @min(recorded, recent.len); const from = if (recorded > recent.len) recorded % recent.len else 0; for (0..shown) |i| { if (i != 0 and i % 8 == 0) try w.writeAll("\n "); try w.print(" {x:0>2}:{x:0>4}", .{ recent[(from + i) % recent.len] >> 16, @as(u16, @truncate(recent[(from + i) % recent.len])) }); } try w.writeAll("\n"); }
try w.print( "\ninterrupts: enabled=${x:0>2} pending=${x:0>2} line={} after {d} frames\n", .{ cpu.memory.interrupts.enable, cpu.memory.interrupts.pending(), cpu.irq, cpu.memory.frames }, );
{ const m = &cpu.memory.mega2; try w.print( "\nvideo: col80={} store80={} page2={} text={} new_video=${x:0>2}\n", .{ m.col80, m.store80, m.page2, m.text, cpu.memory.vgc.new_video }, ); // How the text page is split between the two halves, which is the // difference between eighty-column text and forty-column text with // every other column thrown away. The two counts being roughly equal // is what eighty columns looks like from here. var main_bytes: usize = 0; var aux_bytes: usize = 0; for (0x400..0x800) |a| { if (m.halfTarget(false, @intCast(a), false)) |b| if (b.* != 0) { main_bytes += 1; }; if (m.halfTarget(true, @intCast(a), false)) |b| if (b.* != 0) { aux_bytes += 1; }; } try w.print("text page 1: {d} non-zero bytes in main, {d} in auxiliary\n", .{ main_bytes, aux_bytes }); }
// The registers that decide what the fast side even *is* at any moment. // A machine that has run off the rails has usually done it by ending up // with the wrong thing mapped somewhere, and these four say what is. { const c = cpu.memory.mega2.card_switches; try w.print( "\nfast side: language card reading {s}, bank {d}, writes {s}; shadow=${x:0>2} speed=${x:0>2} state=${x:0>2}\n", .{ if (c.read_ram) "RAM" else "ROM", @as(u8, if (c.bank2) 2 else 1), if (c.write_ram) "on" else "off", cpu.memory.shadow, cpu.memory.speed, cpu.memory.state(), }, ); }
// What the disk port did, which is the difference between a machine that // never looked and one that looked and found nothing. { const iwm = &cpu.memory.iwm; const drive = &iwm.drives.drives[iwm.drives.selected]; try w.print( "\ndisk port: talking to the {s} drives, drive {d}, enable {s}, mode ${x:0>2}\n", .{ if (iwm.three_and_a_half) "3.5-inch" else "5.25-inch", @as(u8, iwm.selected) + 1, if (iwm.enabled) "on" else "off", iwm.mode, }, ); try w.print( " 5.25-inch: motor {s}, head at track {d}, {s}\n", .{ if (iwm.drives.motor) "on" else "off", drive.track(), if (drive.floppy == null) "no disk in it" else "a disk in it", }, ); const disk35 = &iwm.disks35; const mech = &disk35.drives[disk35.selected]; try w.print( " 3.5-inch: spindle {s}, head at track {d} side {d}, {s}, register {s}\n", .{ if (mech.spindle) "on" else "off", mech.track, disk35.head(), if (mech.floppy == null) "no disk in it" else "a disk in it", disk35.addressed().name(), }, ); try w.print( " 3.5-inch: {d} commands strobed into the drive, {d} bytes read off the surface, {d} written to it\n", .{ disk35.commanded, disk35.read, disk35.written }, ); }
// And everything the machine said to the clock chip, which is the same // kind of evidence as the desktop bus conversation above: neither manual // gives this chip's command set, so what the firmware sends is the only // description of it there is. { const rtc = &cpu.memory.rtc; const exchanges = @min(rtc.traced, rtc.trace.len); if (exchanges != 0) { try w.print("\n{d} exchanges with the clock chip; the last {d}:\n ", .{ rtc.transfers, exchanges }); const from = if (rtc.traced > rtc.trace.len) rtc.traced % rtc.trace.len else 0; for (0..exchanges) |i| { const e = rtc.trace[(from + i) % rtc.trace.len]; try w.print(" {s}{x:0>2}{s}", .{ if (e.reading) "r" else "w", e.value, if (e.last) "." else "", }); } try w.writeAll("\n"); } var set: usize = 0; for (rtc.ram) |b| { if (b != 0) set += 1; } try w.print( " {d} of {d} bytes of parameter RAM written, {d} commands not understood\n", .{ set, rtc.ram.len, rtc.unrecognised }, ); // And what it holds, since which byte is which is what the Control // Panel's settings come down to. for (0..rtc.ram.len / 16) |row| { try w.print(" {x:0>2}:", .{row * 16}); for (rtc.ram[row * 16 ..][0..16]) |b| try w.print(" {x:0>2}", .{b}); try w.writeAll("\n"); } const stamp = zig65.clockcard.Time.fromEpoch( @as(i64, rtc.seconds) - zig65.rtc.Rtc.epoch_1904, ); try w.print(" the clock says {d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2}:{d:0>2}\n", .{ stamp.year, stamp.month, stamp.day, stamp.hour, stamp.minute, stamp.second, }); }
if (network) |*net| { try w.print("\nLocalTalk: node {?d}, {d} frames sent to the group ({d} the system refused), {d} received, {d} requests answered, {d} echoes ignored, {d} dropped for want of room\n", .{ net.bridge.node, net.bridge.sent, net.failed_sends, net.bridge.received, net.bridge.answered, net.bridge.echoes, net.bridge.overflowed, }); }
// What went out on the network, which is the AppleTalk firmware's work // seen from the wire: each frame's bytes, with the check the chip put on // the end. { const scc = &cpu.memory.scc; inline for (.{ zig65.scc.Scc.Which.a, zig65.scc.Scc.Which.b }) |which| { const port = scc.channelOf(which); const frames = port.sentFrames(); if (frames.len != 0 or port.frames_undriven != 0) { try w.print("\n{d} frames waiting on port {s}'s wire, {d} sent with the driver off:\n", .{ frames.len, @tagName(which), port.frames_undriven }); for (frames) |frame| { try w.writeAll(" "); for (frame.slice()) |byte| try w.print(" {x:0>2}", .{byte}); try w.print(" ({s})\n", .{if (zig65.scc.Scc.fcsAgrees(frame.slice())) "check agrees" else "check wrong"}); } } } }
// What the machine said to the serial chip, register by register, which // is the whole of what there is to go on for the AppleTalk firmware. { const scc = &cpu.memory.scc; const exchanges = @min(scc.traced, scc.trace.len); if (exchanges != 0) { try w.print("\n{d} accesses to the serial chip; the last {d}:\n", .{ scc.traced, exchanges }); const from = if (scc.traced > scc.trace.len) scc.traced % scc.trace.len else 0; for (0..exchanges) |i| { const e = scc.trace[(from + i) % scc.trace.len]; if (e.data) try w.print(" {d:>12} {s} data {s} {x:0>2}\n", .{ e.cycle, @tagName(e.channel), if (e.reading) "->" else "<-", e.value }) else try w.print(" {d:>12} {s} {s}{d} {s} {x:0>2}\n", .{ e.cycle, @tagName(e.channel), if (e.reading) "RR" else "WR", e.register, if (e.reading) "->" else "<-", e.value }); } } }
try cpu.memory.report(w);
// A picture of the screen, if one was asked for. It comes off the Mega II, // which is the machine's video whichever half of it did the writing: an // Apple IIgs draws its old-style screen with a real Apple IIe, and this is // that IIe being asked the same questions src/video.zig asks any of them. if (screen_path) |into| { // Which of the machine's two displays is on the screen. They are // different chips drawing different pictures on different grids, and // the New-Video register's top bit is what says which one has the // memory. const display: zig65.picture.Display = if (cpu.memory.superHiRes()) .super_hires else .apple2; try zig65.picture.write(io, gpa, display, &cpu.memory.mega2, into, .{}); try w.print("\nwrote the {s} screen to {s}\n", .{ @tagName(display), into }); }
// And what is on the text screen, which is the Mega II's to draw and so // is real the moment the firmware writes to it. Forty or eighty columns // of nothing is a machine that has not got there yet; anything else is // the first thing this has ever shown. try w.print("\ntext screen ({d} columns): ", .{cpu.memory.mega2.columns()}); var text: std.Io.Writer.Allocating = .init(gpa); defer text.deinit(); try cpu.memory.mega2.writeText(&text.writer); const shown = text.written(); const columns: usize = cpu.memory.mega2.columns();
// Nothing at all means the machine is not showing text: `screen.write` // writes nothing for a screen showing graphics, and a machine that has got // as far as drawing something has got further than one still printing at // you, so it is worth saying rather than crashing on an empty screen. if (shown.len == 0) { try w.print("not text at all — the machine is showing {s}\n", .{ @tagName(cpu.memory.mega2.mode()), }); try w.flush(); return; }
// A screen of one repeated character is memory nobody has written to, and // saying so in a line is more use than twenty-four identical rows. var uniform = true; for (shown) |c| if (c != shown[0]) { uniform = false; }; if (uniform) { try w.print("all '{c}', so nothing has been written to it\n", .{shown[0]}); } else { try w.writeAll("\n"); var row: usize = 0; while (row * columns < shown.len) : (row += 1) { const line = shown[row * columns ..][0..@min(columns, shown.len - row * columns)]; try w.print(" |{s}|\n", .{line}); } }
try w.flush();}