// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! What every 16-bit memory model has in common: a record of what went past on //! the address bus. //! //! Separate from the 8-bit `memory/bus.zig` rather than shared with it, and //! not only because the address is three bytes here. The 65816 says more about //! each cycle than the 6502 has pins to say. It carries VDA and VPA, which //! together tell whether the address on the bus is data, an instruction byte, //! both, or nothing at all, and the SingleStepTests recordings are written in //! those terms: a cycle with none of them asserted has no value recorded //! against it, because the machine that made the recording did not enable RAM //! for it. An emulator that logged every cycle as a read of some byte could //! not be compared against that. const std = @import("std"); /// What a memory model should do beyond being memory. pub const Options = struct { /// Keep a record of what goes past on the address bus. /// /// Off by default, as in the 8-bit models: it costs a compare and a store /// on every access, and only something checking which addresses an /// instruction touched has any use for it. bus_log: bool = false, }; /// What the processor was doing with the address bus during a cycle, which is /// what VDA and VPA say between them and what decides whether a value was /// transferred at all. pub const Cycle = enum { /// VDA and VPA both asserted: the opcode itself, which is at once the /// instruction byte and the thing being fetched. opcode, /// VPA alone: an operand byte, read from the program bank because it is /// part of the instruction. operand, /// VDA alone: data, at wherever the instruction worked out. data, /// VDA with VPB: a vector being pulled. VPB is what a machine watches to /// know an interrupt is being answered. vector, /// Neither: an internal cycle. The chip is doing arithmetic of its own and /// the address bus holds whatever it last held, so a value is driven by /// nobody and the recordings write null where a byte would go. internal, /// Neither either, but with the write line down and a byte on the bus. /// /// This is the middle cycle of a read-modify-write, and it is the NMOS /// 6502's habit surviving into a chip that no longer needs it: that part /// drove the unmodified value back before it drove the modified one, and /// this one still does, with none of the address lines asserted so that /// nothing latches it. The recordings do give it a value, which is why it /// cannot be lumped in with the cycle above. internal_write, /// Nothing at all: the processor has let go of the bus. /// /// This is what WAI and STP leave behind them. Every other cycle here has /// an address on it even when nothing is being transferred, because the /// chip is still driving one; a stopped processor is not, and the /// recordings write null for the address as well as the value and leave /// every one of the eight signal characters clear — the read line /// included, which no other cycle does. stopped, /// Whether anything was actually transferred, which is what decides /// whether the cycle has a value to compare. pub fn transfers(self: Cycle) bool { return self != .internal and self != .internal_write and self != .stopped; } /// Valid data address. pub fn vda(self: Cycle) bool { return self == .opcode or self == .data or self == .vector; } /// Valid program address. pub fn vpa(self: Cycle) bool { return self == .opcode or self == .operand; } /// Vector pull. pub fn vpb(self: Cycle) bool { return self == .vector; } }; /// One access on the address bus. /// /// The mode and width lines are recorded alongside the address because the /// chip drives them as outputs on every cycle and the recordings check them: /// they are how a machine outside the processor can tell an eight-bit access /// from a sixteen-bit one, which matters to anything watching the bus. pub const Access = struct { pub const Direction = enum { read, write }; address: u24, /// What was transferred, meaningless when the cycle transferred nothing. value: u8, direction: Direction, cycle: Cycle, /// The emulation line, as it stood during the cycle. e: bool, /// The accumulator width line: set means eight bits. m: bool, /// The index width line: set means eight bits. x: bool, /// The memory lock. Held down across the read, the think and the write of /// a read-modify-write, which is how a machine with more than one thing on /// the bus is told not to come between them: the whole point of such an /// instruction is that nobody sees the byte half changed. mlb: bool, /// The eight signal characters the corpus writes for a cycle, in its /// order: VDA, VPA, VPB, read or write, E, M, X and the memory lock. pub fn signals(self: Access) [8]u8 { // A processor that has let go of the bus is not driving the read line // either, which is the one place the direction does not show. if (self.cycle == .stopped) return .{ '-', '-', '-', '-', '-', '-', '-', '-' }; return .{ if (self.cycle.vda()) 'd' else '-', if (self.cycle.vpa()) 'p' else '-', if (self.cycle.vpb()) 'v' else '-', if (self.direction == .read) 'r' else 'w', if (self.e) 'e' else '-', if (self.m) 'm' else '-', if (self.x) 'x' else '-', if (self.mlb) 'l' else '-', }; } }; /// How many accesses a log keeps. /// /// Much longer than the 8-bit log, and not because ordinary instructions are /// longer: the longest is eight cycles. It is the block moves. Those repeat /// themselves by backing the program counter over their own three bytes, so a /// recording of one is a recording of however many times it went round, and /// the corpus runs them for a hundred cycles. This is room for that with a /// little to spare. pub const capacity = 128; /// A record of the accesses an instruction made, or nothing at all. The /// disabled version has the same shape and does nothing, so a model needs no /// conditionals of its own. pub fn Log(comptime enabled: bool) type { return if (enabled) struct { const Self = @This(); entries: [capacity]Access = undefined, len: usize = 0, pub fn record(self: *Self, access: Access) void { if (self.len == capacity) return; self.entries[self.len] = access; self.len += 1; } /// Starts a fresh record, which is what a caller wants between one /// instruction and the next. pub fn clear(self: *Self) void { self.len = 0; } } else struct { const Self = @This(); pub fn record(_: *Self, _: Access) void {} pub fn clear(_: *Self) void {} }; } test "the signal characters are the ones the corpus writes" { const opcode_fetch: Access = .{ .address = 0xbd0c7f, .value = 0xea, .direction = .read, .cycle = .opcode, .e = true, .m = true, .x = true, .mlb = false, }; try std.testing.expectEqualStrings("dp-remx-", &opcode_fetch.signals()); const internal: Access = .{ .address = 0xbd0c80, .value = 0, .direction = .read, .cycle = .internal, .e = true, .m = true, .x = true, .mlb = false, }; try std.testing.expectEqualStrings("---remx-", &internal.signals()); // Native, with a sixteen-bit accumulator and eight-bit indexes, reading an // operand out of the program bank. const operand: Access = .{ .address = 0x653d5a, .value = 0x06, .direction = .read, .cycle = .operand, .e = false, .m = false, .x = true, .mlb = false, }; try std.testing.expectEqualStrings("-p-r--x-", &operand.signals()); // And the read of a read-modify-write, which holds the bus. const locked: Access = .{ .address = 0x0021cb, .value = 0x74, .direction = .read, .cycle = .data, .e = true, .m = true, .x = true, .mlb = true, }; try std.testing.expectEqualStrings("d--remxl", &locked.signals()); }