// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The Apple IIgs's built-in clock, and the border colour it shares a register //! with. //! //! The clock chip keeps the date and time and a little battery-backed RAM that //! the machine stores its settings in — which is why a IIgs remembers its //! preferences with the power off and an Apple IIe does not. It is reached one //! byte at a time through two registers, and the Hardware Reference sets that //! handshake out exactly (Apple Computer, Inc., 1989a): //! //! - `$c033` is the data register, one byte in either direction. //! - `$c034` is the control register. Bit 7 starts a transfer and the chip //! clears it when the transfer is done, so a program starts one and then //! polls that bit for zero. Bit 6 says which way the byte is going, from //! the chip or to it. Bit 5 is set on the last byte of a sequence and //! clear on every other, which the manual warns is not optional: leaving //! it clear at the end corrupts what is in the chip. //! //! To write, a program puts the byte in the data register and then starts the //! transfer; to read, it starts the transfer and then takes the byte out. //! //! The low four bits of that same control register are the border colour, and //! have nothing to do with the clock at all. They are here because they are //! there, and because a program setting the border must not disturb a transfer //! and the other way about — which is the reason the manual says to touch this //! register only with a read-modify-write. //! //! What the bytes *mean* is in neither manual. Both describe the handshake and //! then decline the command set: "to remain compatible with future Apple II //! products, use the firmware calls to read and write data to the RTC". So the //! firmware is the only description of this chip there is, and what is decoded //! here was read off the conversation it has at power-on rather than out of a //! book. //! //! That conversation is the largest this machine has with anything: three //! thousand and seventy-six exchanges before it has run a third of a million //! instructions, in groups of three, every one of them a write. Two command //! bytes and a data byte: //! //! ``` //! w3f w70 w13 w3f w74 w24 w3f w78 wb9 w3f w7c w8e //! ``` //! //! The second bytes step by four and take exactly thirty-two values from `$00` //! to `$7c`; the first bytes take six consecutive values from `$3a` to `$3f`. //! Which gives the format outright — five address bits in one and three in the //! other: //! //! ``` //! first byte: 0 0 1 1 1 a7 a6 a5 //! second byte: 0 a4 a3 a2 a1 a0 0 0 //! ``` //! //! Decoded that way the hundred and seventy groups in a capture name addresses //! `$56` through `$ff`, **strictly ascending by one**, with no group left over. //! A firmware walking its parameter RAM a byte at a time is the only thing //! that looks like, and getting a contiguous run out of an eight-bit address //! split across two bytes is not something a wrong guess does. //! //! Which way a byte is going is *not* read out of the command: the control //! register's bit 6 says, and that much is documented — "bit 6 = 0 = write to //! clock chip, 1 = read from clock". So this does not have to guess at how the //! command encodes a direction, and does not. //! //! The clock registers proper -- the four bytes of seconds since 1904 -- are //! decoded too; see `clock_prefix`, which was read out of the firmware's own //! routine for asking the time. So a program asking gets the machine's time, //! and a test checks that it is one number and that it keeps counting. //! //! The boot ends with `w37 w55` and `w31 w00`, and neither `$37` nor `$31` is //! one of the parameter-RAM commands. `$31` is known: it is the chip's test //! register, "write only", whose two high bits "should always be set to 0 //! during normal operation" (Apple Computer, Inc., 1985, p. 45) -- and `$00` //! is what the firmware writes to it, so this is the boot putting the chip in //! its normal state and is taken as such. `$35`, the write-protect register on //! the same page, is decoded by the same token though no boot has been seen to //! send it. `$37` is in nobody's table and is counted rather than guessed at, //! in the way this model counts everything else it has not understood -- and so //! is the `$55` behind it, since a command this does not know is a command //! whose data byte it cannot tell from the next command. const std = @import("std"); /// The clock chip's two registers, and the border colour in with them. pub const Rtc = struct { pub const ram_size = 0x100; /// How far through a command the chip is: two bytes name an address and a /// third carries the byte. pub const Phase = enum { command, address, data, clock, /// One of the two write-only registers has been named and its byte is next. control, }; /// The first byte of a parameter-RAM command: `z0111aaa`, so `$38` to /// `$3f` and again at `$b8` to `$bf`, one pair for each thirty-two bytes /// of the RAM. /// /// The top bit is the direction, and it was found rather than assumed. The /// first seven hundred exchanges of a boot are `wbe w70 r00` and `wbf w00 /// r00` — the same commands with bit 7 set, each followed by a *read* /// transfer — and the thousand after them are `w3f w70 w13` with bit 7 /// clear and a write. So the firmware reads the whole parameter RAM, finds /// nothing in it, and writes it out with the checksums, which is exactly /// the sequence the Firmware Reference (Apple Computer, Inc., 1987a) /// describes for its `TOREADBR` vector. /// /// It is not what this acts on, though. Which way a byte goes is the /// control register's bit 6, and that much is documented; the command's /// top bit has agreed with it in every exchange observed, and there is no /// reason to model the same fact twice. const command_prefix: u8 = 0x38; const command_mask: u8 = 0x78; /// And the second: `0aaaaa00`. const address_mask: u8 = 0x83; /// The clock proper, which is four more commands: `z000rr01`, where `rr` /// picks one of the four bytes of a seconds counter and `z` says which /// way it is going. So `$01 $05 $09 $0d` write it and `$81 $85 $89 $8d` /// read it, low byte first. /// /// This came off the machine too, and out of the ROM rather than out of a /// trace, because nothing in a boot ever asks the time. The firmware's own /// routine for reading it is at `$fe/b5a0` and counts the commands out /// rather than listing them: /// /// ``` /// LDA #$FD /// CLC /// ADC #$04 ; $01, and $05, and $09, and $0d /// PHA /// ORA #$80 ; which for a read is $81, $85, $89, $8d /// ``` /// /// It does the whole of that twice and compares the two answers, and /// starts again if they differ — which is what a program reading a /// counter that is still counting has to do, and is the clearest possible /// evidence that these four bytes are one number rather than four. const clock_mask: u8 = 0x73; const clock_prefix: u8 = 0x01; /// The chip's two write-only registers, "00110001" and "00110101" (Apple /// Computer, Inc., 1985, p. 45). The firmware writes the first at the end /// of every boot, with nought. const test_register: u8 = 0x31; const write_protect_register: u8 = 0x35; /// What the counter counts from. Apple's clocks count seconds from the /// start of 1904, which is far enough back that a machine built in 1986 /// could not reach the end of thirty-two bits within its own lifetime — /// though it does reach it in 2040, which is a problem for somebody. pub const epoch_1904 = 2_082_844_800; /// Bit 7 of the control register: set to start a transfer, and the chip /// clears it when the transfer is over. This one is never busy — it /// finishes as it starts — so the bit is never seen set, which is what a /// program polling it wants and gets in one poll rather than several. /// Kept as a field so that a slower model can be made of it later. busy: bool = false, /// Bit 6: the direction of the last transfer, and readable as it was /// written. reading: bool = false, /// Bit 5: whether the last byte was the last of its sequence. last_byte: bool = false, /// Bit 4 is reserved, and the manual says not to modify it. What is /// written to it is kept, so that a program reading the register back /// finds what it put there. reserved: bool = false, /// The low four bits: the colour of the border around the display. border: u4 = 0, /// The data register at `$c033`. data: u8 = 0, /// The time, as seconds since the start of 1904. Battery-backed like the /// parameter RAM, and like it a reset does not touch it. seconds: u32 = 0, /// The machine cycle the count above was true at, and how many of those /// go by in a second. It keeps time by the machine's own clock rather than /// the wall, so a machine run at ten times its speed sees ten times as /// many hours go by and a halted one sees none — which is how the clock /// card in the 8-bit machines works, and for the same reason: there is one /// clock in here and everything is paced by it. counted_at: u64 = 0, cycles_per_second: u64 = 0, /// The battery-backed parameter RAM, which is what a IIgs remembers its /// settings in and an Apple IIe has not got. The Hardware Reference is firm /// about whose it is (Apple Computer, Inc., 1989a): "the parameter RAM in /// the RTC is used for system parameters, and is not available to, nor /// should it be used by, programs other than the system". ram: [ram_size]u8 = @splat(0), /// How far through a command the chip is. Two bytes name an address and a /// third carries the byte, so a transfer means different things depending /// on which of the three it is. phase: Phase = .command, /// Which of the two write-only registers is being written, while `phase` /// is `.control`, and what was last written to each. control_register: u8 = 0, test_bits: u8 = 0, write_protect: u8 = 0, /// The top three address bits, from the first of the two command bytes. group: u3 = 0, /// Which of the four bytes of the counter a clock command named. register: u2 = 0, /// The address those two bytes named. at: u8 = 0, /// How many commands went by that were not one of the eight this /// understands. The to-do list, in the same shape as the machine's. unrecognised: u32 = 0, /// How many bytes have been exchanged, which says whether anything is /// talking to the chip at all. transfers: u32 = 0, /// The last few bytes that went by, and which way. For whoever comes to /// work out the command set: the shape of a conversation is most of the /// way to reading it. trace: [48]Exchange = @splat(.{ .value = 0, .reading = false, .last = false }), traced: usize = 0, pub const Exchange = struct { value: u8, reading: bool, last: bool, }; /// The bytes of the parameter RAM the Control Panel's Slots menu comes /// down to, one per slot: zero is the machine's own firmware for that /// slot and one is "Your card". Slot 7's own firmware is AppleTalk, so /// zero there is what turns the network on; and the AppleTalk firmware /// takes the printer port when slot 1 reads one and the modem port /// otherwise, which is the only thing it looks at to choose. /// /// Read off the firmware rather than a manual. ROM 01 seeds the /// parameter RAM from a table at `$ff/898a` whose bytes `$21` to `$27` /// are `00 00 00 00 00 00 01` — six built-in slots and a seventh left to /// a card, which is the Slot register's `$80` the same firmware writes — /// and its AppleTalk code decides between `$c038` and `$c039` on byte /// `$21` of the copy it keeps at `$e1/02c0`. pub const slot_1: u8 = 0x21; pub const slot_7: u8 = 0x27; /// ROM 3 gave slots 1 and 2 a third choice, AppleTalk itself, and keeps /// slot 1's answer to that question in a byte of its own past the end /// of ROM 01's table: its AppleTalk boot at `$ff/f920` takes the port /// from a variable that `$ff/baed` fills from byte `$22` when that says /// two and otherwise from byte `$58`. Two here is the printer port on /// the network. pub const slot_1_appletalk: u8 = 0x58; /// And the byte after them is the Startup Slot: zero to scan, or the /// slot to start from. Seven with AppleTalk in it is a machine that /// boots off the network. pub const startup_slot: u8 = 0x28; /// The parameter RAM is checksummed, and the firmware falls back to its /// own defaults when the sum does not agree: "if Battery RAM is damaged /// and inoperative (or the battery dies), the firmware will /// automatically use the Apple standard values to bring up the system" /// (Apple Computer, Inc., 1987a, Appendix G). The sum is the last four /// bytes, and no manual gives it, so this is the firmware's own routine /// at `$ff/b61d` in ROM 01 read out: /// /// ``` /// LDX #$FA /// REP #$20 /// CLC /// LDA #$0000 /// ROL ; the carry it takes in is the one CPX left, which /// ADC $02C0,X ; inside the loop is always clear /// DEX /// CPX #$FF /// BNE loop /// TAX /// EOR #$AAAA /// ``` /// /// Two hundred and fifty-one overlapping little-endian words, top down, /// each rotated into the running sum. `$fc` and `$fd` get the sum and /// `$fe` and `$ff` get it with every other bit flipped. The same routine /// is what checks it at power-on, so a RAM this has sealed is one the /// firmware keeps. pub fn checksum(ram: *const [ram_size]u8) u16 { var sum: u16 = 0; var at: usize = 0xfa; while (true) : (at -= 1) { const word = @as(u16, ram[at]) | @as(u16, ram[at + 1]) << 8; sum = std.math.rotl(u16, sum, 1) +% word; if (at == 0) break; } return sum; } /// Writes the checksum in, so that the firmware accepts what is there. pub fn seal(ram: *[ram_size]u8) void { const sum = checksum(ram); ram[0xfc] = @truncate(sum); ram[0xfd] = @truncate(sum >> 8); ram[0xfe] = @truncate(sum ^ 0xaaaa); ram[0xff] = @truncate((sum ^ 0xaaaa) >> 8); } /// Whether the firmware would accept what is there. pub fn sealed(ram: *const [ram_size]u8) bool { const sum = checksum(ram); return ram[0xfc] == @as(u8, @truncate(sum)) and ram[0xfd] == @as(u8, @truncate(sum >> 8)) and ram[0xfe] == @as(u8, @truncate(sum ^ 0xaaaa)) and ram[0xff] == @as(u8, @truncate((sum ^ 0xaaaa) >> 8)); } /// Sets the clock, and starts it counting from this cycle. pub fn set(self: *Rtc, seconds: u32, cycles: u64) void { self.seconds = seconds; self.counted_at = cycles; } /// Brings the count up to date. The machine calls this before it lets /// anything talk to the chip, which is the only time the answer matters. pub fn tick(self: *Rtc, cycles: u64) void { if (self.cycles_per_second == 0) return; const passed = (cycles -| self.counted_at) / self.cycles_per_second; if (passed == 0) return; self.seconds +%= @truncate(passed); self.counted_at += passed * self.cycles_per_second; } /// What the control register reads as. pub fn control(self: *const Rtc) u8 { var value: u8 = self.border; if (self.busy) value |= 0x80; if (self.reading) value |= 0x40; if (self.last_byte) value |= 0x20; if (self.reserved) value |= 0x10; return value; } /// Writes the control register, which is also how a transfer is started. pub fn setControl(self: *Rtc, value: u8) void { self.border = @truncate(value); self.reserved = value & 0x10 != 0; self.reading = value & 0x40 != 0; self.last_byte = value & 0x20 != 0; if (value & 0x80 == 0) return; // A transfer, and which way it goes is the control register's to say // rather than the command's. const sent = self.data; if (self.reading) { self.data = switch (self.phase) { .data => self.ram[self.at], .clock => @truncate(self.seconds >> @as(u5, self.register) * 8), // Only a completed command has a byte behind it. Anything // else gets nothing, which is the truth rather than an // invention. else => 0, }; self.phase = .command; } else self.accept(sent); self.trace[self.traced % self.trace.len] = .{ .value = if (self.reading) self.data else sent, .reading = self.reading, .last = self.last_byte, }; self.traced +|= 1; self.transfers +|= 1; // And it is over as soon as it began, so bit 7 reads back clear. self.busy = false; } /// Takes a byte the machine sent, which is one of three things depending /// on how far through a command the chip is. fn accept(self: *Rtc, byte: u8) void { switch (self.phase) { .command => { if (byte & command_mask == command_prefix) { self.group = @truncate(byte); self.phase = .address; return; } if (byte & clock_mask == clock_prefix) { self.register = @truncate(byte >> 2); self.phase = .clock; return; } // The two write-only registers in the chip's own table: the // test register at $31, whose top bits "should always be set // to 0 during normal operation", and the write-protect register // at $35 (Apple Computer, Inc., 1985, p. 45). Each takes one // byte, which is kept so that a reader can see what was sent // and otherwise does nothing: the test bits would only // "interfere with normal clock counting", and write protection // has never been seen asked for by a IIgs, so enforcing it // would be modelling a thing nothing does. if (byte == test_register or byte == write_protect_register) { self.control_register = byte; self.phase = .control; return; } // Neither. A boot sends $37 once and never looks at it again, // and this does not know what it is, so it says so rather // than acting on a guess. self.unrecognised +|= 1; }, .control => { if (self.control_register == test_register) self.test_bits = byte else self.write_protect = byte; self.phase = .command; }, .clock => { // One byte of the counter, and the count starts again from // here: setting a clock is setting it, not adding to it. const shift: u5 = @as(u5, self.register) * 8; self.seconds &= ~(@as(u32, 0xff) << shift); self.seconds |= @as(u32, byte) << shift; self.phase = .command; }, .address => { self.phase = .command; if (byte & address_mask != 0) { self.unrecognised +|= 1; return; } self.at = @as(u8, self.group) << 5 | (byte >> 2 & 0x1f); self.phase = .data; }, .data => { self.ram[self.at] = byte; self.phase = .command; }, } } /// What the reset line does. The parameter RAM keeps what it held, which /// is the whole point of it: it is the one thing in this machine with a /// battery behind it, and a reset is not a flat battery. pub fn reset(self: *Rtc) void { // Everything behind the battery survives: the parameter RAM, the // time, and how fast the machine's clock runs. A reset is not a flat // battery. const kept = self.ram; const seconds = self.seconds; const counted_at = self.counted_at; const rate = self.cycles_per_second; self.* = .{}; self.ram = kept; self.seconds = seconds; self.counted_at = counted_at; self.cycles_per_second = rate; } }; const testing = std.testing; test "a write puts the byte in first and starts the transfer after" { var rtc: Rtc = .{}; rtc.data = 0x24; // $a0: start, writing, last byte of the sequence. rtc.setControl(0xa0); try testing.expectEqual(@as(u32, 1), rtc.transfers); try testing.expectEqual(@as(u8, 0x24), rtc.trace[0].value); try testing.expect(!rtc.trace[0].reading); try testing.expect(rtc.trace[0].last); // And bit 7 reads back clear, which is how a program knows it is done. try testing.expectEqual(@as(u8, 0x00), rtc.control() & 0x80); } test "a read starts the transfer first and takes the byte after" { var rtc: Rtc = .{}; // $e0: start, reading, last byte. rtc.setControl(0xe0); try testing.expect(rtc.trace[0].reading); // Nothing in the chip, so nothing comes out. Not a made-up date. try testing.expectEqual(@as(u8, 0x00), rtc.data); try testing.expectEqual(@as(u8, 0x00), rtc.control() & 0x80); } test "the border colour shares the register and is not disturbed by a transfer" { var rtc: Rtc = .{}; rtc.setControl(0x06); // border six, no transfer try testing.expectEqual(@as(u4, 6), rtc.border); try testing.expectEqual(@as(u32, 0), rtc.transfers); try testing.expectEqual(@as(u8, 0x06), rtc.control()); // A transfer that keeps the same low nibble keeps the border. rtc.data = 0x5a; rtc.setControl(0xa6); try testing.expectEqual(@as(u4, 6), rtc.border); try testing.expectEqual(@as(u32, 1), rtc.transfers); try testing.expectEqual(@as(u8, 0x26), rtc.control()); } test "the direction and last-byte bits read back as they were written" { // The manual is emphatic that the last-byte bit matters, so a program that // sets it must be able to see it did. var rtc: Rtc = .{}; rtc.setControl(0x40); try testing.expectEqual(@as(u8, 0x40), rtc.control()); rtc.setControl(0x20); try testing.expectEqual(@as(u8, 0x20), rtc.control()); rtc.setControl(0x00); try testing.expectEqual(@as(u8, 0x00), rtc.control()); } test "two command bytes name a byte of parameter RAM, and a third carries it" { // The format read off the firmware's own conversation: five address bits // in the second byte and three in the first. // // first byte: z 0 1 1 1 a7 a6 a5 // second byte: 0 a4 a3 a2 a1 a0 0 0 var rtc: Rtc = .{}; // $3f names the top group, $7c the last of its thirty-two, so $ff. rtc.data = 0x3f; rtc.setControl(0x80); try testing.expectEqual(Rtc.Phase.address, rtc.phase); rtc.data = 0x7c; rtc.setControl(0x80); try testing.expectEqual(Rtc.Phase.data, rtc.phase); try testing.expectEqual(@as(u8, 0xff), rtc.at); rtc.data = 0x8e; rtc.setControl(0x80); try testing.expectEqual(@as(u8, 0x8e), rtc.ram[0xff]); // And the chip is back to expecting a command. try testing.expectEqual(Rtc.Phase.command, rtc.phase); try testing.expectEqual(@as(u32, 0), rtc.unrecognised); // The lowest group and the lowest address in it, which is $00. rtc.data = 0x38; rtc.setControl(0x80); rtc.data = 0x00; rtc.setControl(0x80); try testing.expectEqual(@as(u8, 0x00), rtc.at); rtc.data = 0x5a; rtc.setControl(0x80); try testing.expectEqual(@as(u8, 0x5a), rtc.ram[0x00]); // Every one of the eight groups and thirty-two addresses within it comes // out where it should, which is what a decode split across two bytes has // to get right to be believed at all. for (0..0x100) |i| { const address: u8 = @intCast(i); rtc.data = Rtc.command_prefix | (address >> 5); rtc.setControl(0x80); rtc.data = (address & 0x1f) << 2; rtc.setControl(0x80); try testing.expectEqual(address, rtc.at); rtc.data = address ^ 0xa5; rtc.setControl(0x80); } for (0..0x100) |i| { try testing.expectEqual(@as(u8, @intCast(i)) ^ 0xa5, rtc.ram[i]); } try testing.expectEqual(@as(u32, 0), rtc.unrecognised); } test "the same command with its top bit set reads instead of writing" { // Which was found rather than assumed: the first seven hundred exchanges // of a boot are `wbf w00 r00` -- the command with bit 7 set, then a read // transfer -- and the thousand after them are `w3f w70 w13` with it clear. // The firmware reads the whole parameter RAM, finds nothing, and writes it // back out with checksums. var rtc: Rtc = .{}; rtc.ram[0x91] = 0x42; // $b8 | (0x91 >> 5) = $bc, and (0x91 & 0x1f) << 2 = $44. rtc.data = 0xbc; rtc.setControl(0x80); // start, writing rtc.data = 0x44; rtc.setControl(0x80); try testing.expectEqual(@as(u8, 0x91), rtc.at); // And now a read transfer, which is the control register's bit 6 rather // than anything in the command: the chip hands the byte over. rtc.setControl(0xc0); try testing.expectEqual(@as(u8, 0x42), rtc.data); try testing.expectEqual(@as(u32, 0), rtc.unrecognised); // A read with no address behind it gets nothing, which is the truth // rather than an invention. rtc.setControl(0xc0); try testing.expectEqual(@as(u8, 0x00), rtc.data); } test "a command this does not know is counted rather than acted on" { // The boot ends with `w37 w55` and `w31 w00`. The second pair is the test // register being put in its normal state and is taken as such; the first // is in nobody's table, so it is counted in the same way the machine // counts a soft switch it has not got -- and so is the $55 behind it, // since a command this does not know is a command whose data byte it // cannot tell from the next command. var rtc: Rtc = .{}; for ([_]u8{ 0x37, 0x55, 0x31, 0x00 }) |byte| { rtc.data = byte; rtc.setControl(0x80); } try testing.expectEqual(@as(u32, 2), rtc.unrecognised); try testing.expectEqual(@as(u8, 0x00), rtc.test_bits); try testing.expectEqual(Rtc.Phase.command, rtc.phase); // And none of it was mistaken for an address: nothing was written. for (rtc.ram) |b| try testing.expectEqual(@as(u8, 0), b); // A second byte that is not of the address form is caught too, and puts // the chip back to expecting a command rather than leaving it half way // through one. rtc.data = 0x3f; rtc.setControl(0x80); rtc.data = 0x55; // not 0aaaaa00 rtc.setControl(0x80); try testing.expectEqual(@as(u32, 3), rtc.unrecognised); try testing.expectEqual(Rtc.Phase.command, rtc.phase); } test "the parameter RAM has a battery behind it and a reset is not a flat one" { var rtc: Rtc = .{}; rtc.data = 0x3f; rtc.setControl(0x80); rtc.data = 0x7c; rtc.setControl(0x80); rtc.data = 0x8e; rtc.setControl(0x80); rtc.border = 6; rtc.reset(); try testing.expectEqual(@as(u8, 0x8e), rtc.ram[0xff]); // kept try testing.expectEqual(@as(u4, 0), rtc.border); // and not } test "the clock is four commands and one number" { // $01 $05 $09 $0d write the four bytes of a seconds counter and $81 $85 // $89 $8d read them, low byte first. Which is not in any manual: it is // counted out by the firmware's own routine at $fe/b5a0, `LDA #$FD` then // `ADC #$04` four times over, with `ORA #$80` for the read. var rtc: Rtc = .{}; const when: u32 = 0x8e_b9_24_13; var register: u8 = 0; while (register < 4) : (register += 1) { // The command byte first, then the byte it carries. rtc.data = 0x01 + register * 4; rtc.setControl(0xa0); rtc.data = @truncate(when >> @intCast(register * 8)); rtc.setControl(0xa0); } try testing.expectEqual(when, rtc.seconds); // And back out the same way, with the top bit set on the command -- though // it is the control register's bit 6 that this acts on, as ever. register = 0; while (register < 4) : (register += 1) { rtc.data = 0x81 + register * 4; rtc.setControl(0xa0); rtc.setControl(0xe0); // start, reading try testing.expectEqual(@as(u8, @truncate(when >> @intCast(register * 8))), rtc.data); } try testing.expectEqual(@as(u32, 0), rtc.unrecognised); } test "the clock keeps the machine's time rather than the wall's" { var rtc: Rtc = .{}; rtc.cycles_per_second = 1_000_000; rtc.set(100, 0); // Nothing has gone by yet. rtc.tick(999_999); try testing.expectEqual(@as(u32, 100), rtc.seconds); // And then a second has, and then ten more. rtc.tick(1_000_000); try testing.expectEqual(@as(u32, 101), rtc.seconds); rtc.tick(11_000_000); try testing.expectEqual(@as(u32, 111), rtc.seconds); // The part of a second left over is not thrown away: it counts towards // the next one rather than the clock losing time every time it is asked. rtc.tick(11_999_999); rtc.tick(12_000_000); try testing.expectEqual(@as(u32, 112), rtc.seconds); } test "a reset is not a flat battery" { var rtc: Rtc = .{}; rtc.cycles_per_second = 1_000_000; rtc.set(12345, 0); rtc.ram[7] = 0x42; rtc.reset(); try testing.expectEqual(@as(u32, 12345), rtc.seconds); try testing.expectEqual(@as(u8, 0x42), rtc.ram[7]); try testing.expectEqual(@as(u64, 1_000_000), rtc.cycles_per_second); } test "the parameter RAM's checksum is the firmware's own and a sealed RAM is kept" { // What ROM 01 writes when it finds nothing: its default table, then $ff // to the end, then the four bytes its routine at $ff/b61d makes of that. // The sum below is the one it wrote on this model, and ROM 3 agrees on // the method: its own defaults come out as $2d36 and $879c. var ram: [Rtc.ram_size]u8 = @splat(0xff); const defaults = [_]u8{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0d, 0x06, 0x02, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x06, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x0f, 0x06, 0x06, 0x00, 0x05, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0a, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; @memcpy(ram[0..defaults.len], &defaults); try testing.expectEqual(@as(u16, 0x2413), Rtc.checksum(&ram)); try testing.expect(!Rtc.sealed(&ram)); Rtc.seal(&ram); try testing.expectEqualSlices(u8, &.{ 0x13, 0x24, 0xb9, 0x8e }, ram[0xfc..]); try testing.expect(Rtc.sealed(&ram)); // Slot 7 is a card by default, and the firmware's AppleTalk is what // zero there turns on. try testing.expectEqual(@as(u8, 1), ram[Rtc.slot_7]); ram[Rtc.slot_7] = 0; try testing.expect(!Rtc.sealed(&ram)); } test "the two write-only registers take a byte each and keep it" { // "00110001 Test register (write only)" and "00110101 Write-protect // register (write only)" (Apple Computer, Inc., 1985, p. 45). Each is a // command followed by one byte, and knowing that is what keeps the byte // from being read as the next command. var rtc: Rtc = .{}; for ([_]u8{ 0x31, 0xc0, 0x35, 0x80 }) |byte| { rtc.data = byte; rtc.setControl(0x80); } try testing.expectEqual(@as(u32, 0), rtc.unrecognised); try testing.expectEqual(@as(u8, 0xc0), rtc.test_bits); try testing.expectEqual(@as(u8, 0x80), rtc.write_protect); // Kept, not acted on: protection is never asked for by a IIgs, so a write // to the parameter RAM still lands. for ([_]u8{ 0x38, 0x04, 0x5a }) |byte| { rtc.data = byte; rtc.setControl(0x80); } try testing.expectEqual(@as(u8, 0x5a), rtc.ram[1]); }