// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! An AppleMouse II card: where the mouse is, which buttons are down, and the //! firmware a program calls to ask. //! //! The card is the only ordinary one that pulls the interrupt line: told to, //! it interrupts when the mouse moves, when a button goes down, or once a //! frame at vertical blanking. Everything else in this machine leaves the line //! alone. //! //! A program finds the card by the five bytes in its ROM, and calls it through //! a table of eight entry points. What comes back does not come back in //! registers: the card leaves the position and the state of the buttons in the //! screen holes, the odd bytes of the text page that no character is displayed //! from, one set of them for each slot. //! //! The Apple II mouse technical notes and the Apple IIe Technical Reference are //! where the arrangement below comes from (Apple Computer, Inc., 1985b, //! 1985-1990). const std = @import("std"); const testing = std.testing; /// The five bytes a program looks for, and where in the page they sit. The /// first four are the Pascal 1.1 way of saying what a card is; the last says /// which sort of pointing device. pub const identifiers = [_]struct { at: u8, byte: u8 }{ .{ .at = 0x05, .byte = 0x38 }, .{ .at = 0x07, .byte = 0x18 }, .{ .at = 0x0b, .byte = 0x01 }, .{ .at = 0x0c, .byte = 0x20 }, .{ .at = 0xfb, .byte = 0xd6 }, }; /// Where the table of entry points begins. Each byte in it is the low half of /// a routine's address in the card's page. pub const entries_at = 0x12; /// The eight routines, in the order the table holds them. pub const Entry = enum(u8) { /// Turns the mouse on or off and says what it may interrupt for. set = 0x12, /// Called from an interrupt handler: says whether this card caused it. serve = 0x13, /// Puts where the mouse is into the screen holes. read = 0x14, /// Puts it back to nought. clear = 0x15, /// Takes the position from the screen holes, which is how a program moves /// the pointer itself. position = 0x16, /// Sets how far the mouse may go, which the caller leaves in slot nought's /// screen holes. clamp = 0x17, /// Puts it in the corner of wherever it has been clamped to. home = 0x18, /// Back to how it starts: nought to 1023 both ways, and the middle of /// nowhere in particular. init = 0x19, /// Where in the card's page the byte for this routine sits. pub fn address(self: Entry) u8 { return @intFromEnum(self); } }; /// Where a slot's screen holes are. The text page has eight bytes in each of /// its lines that no character comes from, and the cards were given them. pub const hole = struct { pub const x_low = 0x0478; pub const y_low = 0x04f8; pub const x_high = 0x0578; pub const y_high = 0x05f8; pub const status = 0x0778; pub const mode = 0x07f8; }; /// What a program may ask to be interrupted for, which is the byte it hands to /// the first routine. pub const mode_on = 0x01; pub const mode_move_interrupt = 0x02; pub const mode_button_interrupt = 0x04; pub const mode_vbl_interrupt = 0x08; pub const mode_known = 0x0f; /// What the card says in the status hole. The low half is which interrupt has /// happened, the high half is where the buttons are now and where they were /// when the position was last read. pub const status_button1_was = 0x01; pub const status_moved_interrupt = 0x02; pub const status_button_interrupt = 0x04; pub const status_vbl_interrupt = 0x08; pub const status_button1_now = 0x10; pub const status_moved = 0x20; pub const status_button0_was = 0x40; pub const status_button0_now = 0x80; /// How far the mouse may go before it is told otherwise. pub const default_clamp_low = 0; pub const default_clamp_high = 1023; /// What the firmware asks the card to do. The card does the work, as the one /// on a real card's own processor does, and the firmware is the few bytes that /// pass the question along. pub const Command = enum(u4) { set = 0, serve = 1, read = 2, clear = 3, position = 4, clamp = 5, home = 6, init = 7, /// What the last one came to: nought for the carry clear that means it /// worked, anything else for the carry set that means it did not. result = 8, _, }; pub const Card = struct { /// Whether there is a card in the slot at all. fitted: bool = false, x: i16 = 0, y: i16 = 0, low_x: i16 = default_clamp_low, high_x: i16 = default_clamp_high, low_y: i16 = default_clamp_low, high_y: i16 = default_clamp_high, /// Where the buttons are, and where they were when the position was last /// read, which is how a program tells a click from a hold. button: [2]bool = @splat(false), button_was: [2]bool = @splat(false), /// What it has been told it may interrupt for. mode: u8 = 0, /// Whether it has moved since the position was last read. moved: bool = false, /// Which interrupts are waiting to be owned up to. pending: u8 = 0, /// What the last command came to. result: u8 = 0, /// Whether the mouse is switched on, which is the only bit that matters /// when it is not. pub fn on(self: *const Card) bool { return self.mode & mode_on != 0; } /// Whether the card is pulling the interrupt line, which it does until a /// handler asks it to own up. /// /// Not conditional on the mouse being switched on, because one of the /// three interrupts is not the mouse's. Nothing the mouse raises can be /// waiting here while it is off: `setMode` takes those away as it goes /// off, and neither `moveTo` nor `setButton` raises one meanwhile. pub inline fn interrupting(self: *const Card) bool { return self.fitted and self.pending != 0; } /// The byte the card leaves in the status hole. pub fn status(self: *const Card) u8 { var byte: u8 = self.pending; if (self.button[1]) byte |= status_button1_now; if (self.button_was[1]) byte |= status_button1_was; if (self.button[0]) byte |= status_button0_now; if (self.button_was[0]) byte |= status_button0_was; if (self.moved) byte |= status_moved; return byte; } /// Where the host puts the mouse, in the units the program clamped it to. /// /// A mouse that is switched off does not move: "the mouse position is not /// tracked; any mouse motion is ignored" (Apple Computer, Inc., 1985-1990, /// No. 3). That is about the hand on the desk rather than about the /// program, and the same note is careful to say so — `positionTo`, /// `clear`, `home` and `clamp` all move it whatever the mode says, because /// "PosMouse and ClearMouse will change the mouse position, ClampMouse /// will set new clamp values". pub fn moveTo(self: *Card, x: i16, y: i16) void { if (!self.on()) return; const to_x = std.math.clamp(x, self.low_x, self.high_x); const to_y = std.math.clamp(y, self.low_y, self.high_y); if (to_x == self.x and to_y == self.y) return; self.x = to_x; self.y = to_y; self.moved = true; if (self.mode & mode_move_interrupt != 0) { self.pending |= status_moved_interrupt; } } pub fn moveBy(self: *Card, dx: i16, dy: i16) void { self.moveTo(self.x +| dx, self.y +| dy); } pub fn setButton(self: *Card, which: u1, down: bool) void { if (self.button[which] == down) return; self.button[which] = down; // Only a press is worth interrupting for; letting go is not. if (down and self.on() and self.mode & mode_button_interrupt != 0) { self.pending |= status_button_interrupt; } } /// Called when the video reaches the bottom of a frame, which is the third /// thing the card can be told to interrupt for. /// /// It is the one that does not need the mouse switched on. The low bit of /// the mode is a switch on the mouse rather than on the card, and the /// distinction is written down: "button and movement interrupts are not /// generated, regardless of the other mouse mode bits. Pure VBL interrupts /// can still be generated, however, if bit 3 is set", and, in case that /// left room for doubt, "a mode byte of $08 (mouse off but VBL interrupt /// on) will generate VBL interrupts" (Apple Computer, Inc., 1985-1990, /// No. 3). pub fn blanked(self: *Card) void { if (self.mode & mode_vbl_interrupt != 0) { self.pending |= status_vbl_interrupt; } } /// Turns the mouse on or off. A mode with bits in it the card does not /// know is refused, which is what the carry says on the way out. /// /// Switching the mouse off takes away the two interrupts it can no longer /// raise and leaves the one it still can, which is vertical blanking if /// the mode still asks for it. Nothing else is reset: "turning the mouse /// off and on with the mode byte does not reset any mouse values, /// including position" (Apple Computer, Inc., 1985-1990, No. 3). pub fn setMode(self: *Card, mode: u8) void { if (mode & ~@as(u8, mode_known) != 0) { self.result = 1; return; } self.mode = mode; if (!self.on()) { self.pending &= if (mode & mode_vbl_interrupt != 0) status_vbl_interrupt else 0; } self.result = 0; } /// Owning up: a handler asks whether this card was the one, and the answer /// takes the interrupt off the line. pub fn serve(self: *Card) void { self.result = if (self.pending != 0) 0 else 1; self.pending = 0; } /// Reading the position hands back the status as it stood, and then moves /// the buttons' now into their before and forgets that it moved. What a /// program is told is therefore what has happened since it last asked, /// which is what lets it see each press and each movement once. pub fn readPosition(self: *Card) u8 { const reported = self.status(); self.button_was = self.button; self.moved = false; self.result = 0; return reported; } pub fn clear(self: *Card) void { self.x = 0; self.y = 0; self.moved = false; self.result = 0; } pub fn home(self: *Card) void { self.x = self.low_x; self.y = self.low_y; self.moved = false; self.result = 0; } /// Where a program says the mouse is, which it may do at any time. pub fn positionTo(self: *Card, x: i16, y: i16) void { self.x = std.math.clamp(x, self.low_x, self.high_x); self.y = std.math.clamp(y, self.low_y, self.high_y); self.moved = false; self.result = 0; } /// How far it may go. Nought clamps the one way and one the other, and a /// clamp that is the wrong way round is left alone. pub fn clamp(self: *Card, which: u8, low: i16, high: i16) void { if (low > high) { self.result = 1; return; } if (which == 0) { self.low_x = low; self.high_x = high; self.x = std.math.clamp(self.x, low, high); } else { self.low_y = low; self.high_y = high; self.y = std.math.clamp(self.y, low, high); } self.result = 0; } /// How it starts, and what the eighth routine puts it back to. pub fn initialise(self: *Card) void { const fitted = self.fitted; self.* = .{ .fitted = fitted }; self.result = 0; } }; /// The card's ROM, which is ours: what a mouse card has to answer to is /// published, and none of Apple's own code is needed to answer it. /// /// The five bytes a program looks for are at the offsets it looks at, and are /// ordinary instructions, three of which are branches that all land on the /// same place. That is how the real card's page begins, and why those offsets /// were chosen. pub fn firmware(comptime slot: u4) [256]u8 { const io = 0xc080 + @as(u16, slot) * 0x10; const lo: u8 = @truncate(io); const hi: u8 = @truncate(io >> 8); const page: u8 = 0xc0 + @as(u8, slot); const entry_at = 0x20; const first_routine = 0x30; const routine_size = 6; const finish_at = first_routine + 8 * routine_size; var rom: [256]u8 = @splat(0); const head = [_]u8{ 0x2c, 0x58, 0xff, // $Cn00 BIT $ff58 0x70, 0x1b, // $Cn03 BVS $Cn20 0x38, // $Cn05 SEC the first of the five 0x90, 0x18, // $Cn06 BCC $Cn20 and the second, the $18 0xb8, // $Cn08 CLV 0x50, 0x15, // $Cn09 BVC $Cn20 0x01, 0x20, // $Cn0b ORA ($20,X) the third and the fourth }; @memcpy(rom[0..head.len], &head); // The table a program calls through: the low half of each routine's // address, in the order the eight are always in. for (0..8) |i| rom[entries_at + i] = first_routine + @as(u8, @intCast(i)) * routine_size; // Whatever runs into $Cn20 has nothing to do: a mouse is not something to // print through or read a line from. rom[entry_at] = 0x60; // RTS // The eight, each of them a word with the card and then the same ending. for (0..8) |i| { const command: u8 = @intCast(i); const at = first_routine + i * routine_size; // Setting the mode and setting the clamps are told something in the // accumulator, so they write; the rest only ask. const writes = command == @intFromEnum(Command.set) or command == @intFromEnum(Command.clamp); const routine = [_]u8{ if (writes) 0x8d else 0xad, lo + command, hi, // STA/LDA the card 0x4c, finish_at, page, // JMP finish }; @memcpy(rom[at..][0..routine.len], &routine); } // The ending: the carry the caller reads the answer from. const finish = [_]u8{ 0xad, lo + @intFromEnum(Command.result), hi, // LDA result 0xd0, 0x02, // BNE bad 0x18, // CLC it worked 0x60, // RTS 0x38, // bad: SEC 0x60, // RTS }; @memcpy(rom[finish_at..][0..finish.len], &finish); // And the byte that says which sort of device this is. for (identifiers) |identifier| rom[identifier.at] = identifier.byte; comptime { // The identifiers must not land in anything else: the head ends // before the last of them, and the rest are past everything written. for (identifiers) |identifier| { std.debug.assert(identifier.at < head.len or identifier.at >= finish_at + finish.len); } std.debug.assert(head.len <= entries_at); std.debug.assert(entries_at + 8 <= entry_at); std.debug.assert(entry_at < first_routine); std.debug.assert(finish_at + finish.len <= 0xfb); } return rom; } /// One page for each slot, since a card's ROM has its own slot's addresses /// assembled into it and which slot that is cannot be known until a machine is /// built. Indexed by the slot number, so there are eight of them and the first /// is never read: slot 0 is not a slot, and two hundred and fifty-six wasted /// bytes are cheaper than an off-by-one waiting to happen. pub const firmwares: [8][256]u8 = blk: { var all: [8][256]u8 = undefined; for (0..all.len) |slot| all[slot] = firmware(@intCast(slot)); break :blk all; }; test "the firmware says what a program looks for" { const rom = firmware(4); // The five bytes, at the five offsets, which is the whole of how a // program decides a slot holds a mouse. for (identifiers) |identifier| { try testing.expectEqual(identifier.byte, rom[identifier.at]); } // The three branches in the head all land on the same place, which is why // those offsets could be given away to the identifiers. try testing.expectEqual(@as(u8, 0x20), 0x05 + rom[0x04]); try testing.expectEqual(@as(u8, 0x20), 0x08 + rom[0x07]); try testing.expectEqual(@as(u8, 0x20), 0x0b + rom[0x0a]); // The table is eight entry points, each inside the page and none of them // the same as another. var seen: [8]u8 = undefined; for (0..8) |i| { const at = rom[entries_at + i]; try testing.expect(at >= 0x20); for (seen[0..i]) |before| try testing.expect(before != at); seen[i] = at; // Each is a word with the card and then a jump to the same ending. try testing.expect(rom[at] == 0xad or rom[at] == 0x8d); try testing.expectEqual(@as(u8, 0x4c), rom[at + 3]); } // Setting the mode and the clamps are told something; the rest only ask. const table = struct { fn of(where: [256]u8, command: Command) u8 { return where[where[entries_at + @as(usize, @intFromEnum(command))]]; } }; try testing.expectEqual(@as(u8, 0x8d), table.of(rom, .set)); try testing.expectEqual(@as(u8, 0x8d), table.of(rom, .clamp)); try testing.expectEqual(@as(u8, 0xad), table.of(rom, .read)); } test "the mouse stays inside what it has been clamped to" { var card: Card = .{}; card.setMode(mode_on); card.moveTo(500, 600); try testing.expectEqual(@as(i16, 500), card.x); try testing.expectEqual(@as(i16, 600), card.y); // The corners it starts with, which are nought to 1023 both ways. card.moveTo(-100, 5000); try testing.expectEqual(@as(i16, 0), card.x); try testing.expectEqual(@as(i16, 1023), card.y); // A window, and a position outside it is pulled in when it is set. card.clamp(0, 100, 200); try testing.expectEqual(@as(i16, 100), card.x); card.clamp(1, 300, 400); try testing.expectEqual(@as(i16, 400), card.y); card.moveTo(1000, 1000); try testing.expectEqual(@as(i16, 200), card.x); try testing.expectEqual(@as(i16, 400), card.y); // The corner it homes to is the near one of that window. card.home(); try testing.expectEqual(@as(i16, 100), card.x); try testing.expectEqual(@as(i16, 300), card.y); // A window the wrong way round is refused. card.clamp(0, 500, 100); try testing.expectEqual(@as(u8, 1), card.result); try testing.expectEqual(@as(i16, 100), card.low_x); } test "the status says where the buttons are and where they were" { var card: Card = .{}; card.setMode(mode_on); // Nothing has happened. try testing.expectEqual(@as(u8, 0), card.status()); // A press shows as down now, and as not having been down before, until // the position is read: that is what tells a program a click is new. card.setButton(0, true); try testing.expectEqual(status_button0_now, card.status() & (status_button0_now | status_button0_was)); _ = card.readPosition(); try testing.expectEqual( status_button0_now | status_button0_was, card.status() & (status_button0_now | status_button0_was), ); // And letting go shows the other way about. card.setButton(0, false); try testing.expectEqual(status_button0_was, card.status() & (status_button0_now | status_button0_was)); // The second button has its own pair of bits. card.setButton(1, true); try testing.expect(card.status() & status_button1_now != 0); try testing.expect(card.status() & status_button1_was == 0); // And moving shows until it is read. card.moveTo(10, 10); try testing.expect(card.status() & status_moved != 0); _ = card.readPosition(); try testing.expect(card.status() & status_moved == 0); } test "it pulls the interrupt line only for what it was told to" { var card: Card = .{ .fitted = true }; // Switched off and asked for nothing, nothing it does matters. card.moveTo(10, 10); card.setButton(0, true); card.blanked(); try testing.expect(!card.interrupting()); // On, but told to interrupt for nothing. card.setMode(mode_on); card.setButton(0, false); card.moveTo(20, 20); card.setButton(0, true); card.blanked(); try testing.expect(!card.interrupting()); // Told to interrupt when it moves. card.setMode(mode_on | mode_move_interrupt); card.moveTo(30, 30); try testing.expect(card.interrupting()); try testing.expect(card.status() & status_moved_interrupt != 0); // Owning up takes it off the line, and says it was this card. card.serve(); try testing.expectEqual(@as(u8, 0), card.result); try testing.expect(!card.interrupting()); // Asked again, it says it was not. card.serve(); try testing.expectEqual(@as(u8, 1), card.result); // A press, and the end of a frame, are the other two. card.setMode(mode_on | mode_button_interrupt); card.setButton(0, false); card.setButton(0, true); try testing.expect(card.status() & status_button_interrupt != 0); card.serve(); card.setMode(mode_on | mode_vbl_interrupt); card.blanked(); try testing.expect(card.status() & status_vbl_interrupt != 0); card.serve(); // Switching the mouse off drops whatever was waiting. card.setMode(mode_on | mode_vbl_interrupt); card.blanked(); try testing.expect(card.interrupting()); card.setMode(0); try testing.expect(!card.interrupting()); // And a mode with bits in it the card does not know is refused. card.setMode(0xf0); try testing.expectEqual(@as(u8, 1), card.result); try testing.expectEqual(@as(u8, 0), card.mode); } test "a mouse switched off still interrupts at the end of a frame" { // The low bit of the mode reads like a switch on the card and is not one: // it switches off the mouse, and the end of a frame is not the mouse's. // "A mode byte of $08 (mouse off but VBL interrupt on) will generate VBL // interrupts" (Apple Computer, Inc., 1985-1990, No. 3). var card: Card = .{ .fitted = true }; card.setMode(mode_vbl_interrupt); card.blanked(); try testing.expect(card.interrupting()); try testing.expect(card.status() & status_vbl_interrupt != 0); card.serve(); // The other two stay away however the mode asks for them, which is the // other half of the same paragraph: "button and movement interrupts are // not generated, regardless of the other mouse mode bits". card.setMode(mode_vbl_interrupt | mode_move_interrupt | mode_button_interrupt); card.moveTo(40, 40); card.setButton(0, true); try testing.expect(!card.interrupting()); // Nor does the position follow the hand while it is off, which is the // first thing that paragraph says: "the mouse position is not tracked; // any mouse motion is ignored". The program's own routines are not the // hand, and still move it. try testing.expectEqual(@as(i16, 0), card.x); try testing.expectEqual(@as(i16, 0), card.y); card.positionTo(40, 40); try testing.expectEqual(@as(i16, 40), card.x); card.home(); try testing.expectEqual(card.low_x, card.x); // And a frame still reaches the line with those bits set as well. card.blanked(); try testing.expect(card.interrupting()); try testing.expectEqual(@as(u8, status_vbl_interrupt), card.pending); card.serve(); // Taking the vertical blanking bit away while the mouse is off drops what // was waiting, since nothing left can raise it. card.setMode(mode_vbl_interrupt); card.blanked(); try testing.expect(card.interrupting()); card.setMode(0); try testing.expect(!card.interrupting()); } test "starting again puts everything back" { var card: Card = .{ .fitted = true }; card.setMode(mode_on | mode_move_interrupt); card.clamp(0, 10, 20); card.moveTo(15, 500); card.setButton(1, true); card.initialise(); try testing.expectEqual(@as(i16, 0), card.x); try testing.expectEqual(@as(i16, 0), card.y); try testing.expectEqual(@as(i16, default_clamp_low), card.low_x); try testing.expectEqual(@as(i16, default_clamp_high), card.high_x); try testing.expectEqual(@as(u8, 0), card.mode); try testing.expect(!card.button[1]); // But it is still in the machine. try testing.expect(card.fitted); } test "there is a page of firmware for every slot, and each is that slot's" { // See the same test in `blockdevice.zig`: the machine indexes this table // rather than calling the generator, so a generator that ignored its // argument would go unnoticed. inline for (0..firmwares.len) |slot| { try testing.expectEqualSlices(u8, &firmware(slot), &firmwares[slot]); } try testing.expect(!std.mem.eql(u8, &firmwares[4], &firmwares[2])); }