// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); const log = std.log.scoped(.addressing_modes); /// Works out where an instruction's operand lives, and spends the cycles the /// chip spends getting there. Embedded in CPU, and finds its way back to it /// with @fieldParentPtr rather than holding a pointer, so the whole processor /// can be built in place. pub fn AddressingModes(comptime CPU: type) type { return struct { const Self = @This(); /// Nothing to set up; the decoder holds no state of its own. pub fn init(self: *Self) void { _ = self; } /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("addressing_modes", self)); } /// What the instruction is going to do with the address it gets back. /// Indexed addressing always drives a read of the partially indexed /// address, but on a read instruction that access is only a cycle of /// its own when the index carried out of the low byte, because /// otherwise it is the data read itself. /// What the instruction is going to do with the address it gets back. /// /// A read only pays for the phantom access when the index carried out /// of the low byte, because otherwise that access is the data read /// itself. A write always pays. A modify is both, and which of the two /// it pays like is one of the places the processors disagree: the NMOS /// part always pays, the CMOS part only when the index carried, which /// is why shifting an indexed address is a cycle shorter on it more /// often than not. pub const Access = enum { read, write, modify, /// Whether the phantom access happens whatever the index did. fn unconditional(self: Access) bool { return switch (self) { .read => false, .write => true, .modify => !CPU.processor.cmos(), }; } }; /// The "phantom" read the chip performs while it works out the real /// high byte. /// /// Where it reads is the other thing the two disagree about. The NMOS /// part drives the address it has built so far, whose high byte has /// not been fixed up and which is therefore an address nobody meant; /// the CMOS part drives the last byte of the instruction instead, /// which is harmless. That matters to a machine with hardware in it: /// the NMOS part's phantom read can touch a soft switch that was never /// addressed. fn phantomRead( self: *Self, comptime access: Access, base_address_high: u8, base_address_low: u8, index: u8, ) void { const cpu = self.getCPU(); const indexed, const carried = @addWithOverflow(base_address_low, index); if (comptime !access.unconditional()) { if (carried == 0) return; } _ = switch (CPU.processor) { .@"6502", .RP2A03 => cpu.memory.read(u8, @as(u16, base_address_high) << 8 | indexed), .@"65C02", .R65C02, .W65C02 => cpu.memory.read(u8, cpu.program_counter.value -% 1), }; } /// The pieces of an indexed address. The unstable stores need them /// because both the value they write and where they write it depend /// on the address the chip built before it fixed up the high byte. pub const Indexed = struct { base_high: u8, low: u8, carried: bool, /// Where the store lands. When indexing carried, the chip never /// gets around to fixing the high byte up and the value being /// written ends up there instead. pub fn target(self: Indexed, value: u8) u16 { return @as(u16, if (self.carried) value else self.base_high) << 8 | self.low; } }; /// Decode an indexed addressing mode and hand back its parts instead /// of just the address, performing the phantom read a store performs. pub fn decodeIndexed(self: *Self, comptime addressing_mode: CPU.AddressingModes) Indexed { const cpu = self.getCPU(); const base_low, const base_high = switch (addressing_mode) { .@"Absolute, X", .@"Absolute, Y" => base: { const low = cpu.program_counter.read(u8); break :base .{ low, cpu.program_counter.read(u8) }; }, .@"(Indirect), Y" => base: { const pointer = cpu.program_counter.read(u8); const low = cpu.memory.read(u8, pointer); break :base .{ low, cpu.memory.read(u8, pointer +% 1) }; }, else => @compileError("not an indexed addressing mode"), }; const index = switch (addressing_mode) { .@"Absolute, X" => cpu.registers.get(.X), else => cpu.registers.get(.Y), }; const low, const carried = @addWithOverflow(base_low, index); _ = cpu.memory.read(u8, @as(u16, base_high) << 8 | low); return .{ .base_high = base_high, .low = low, .carried = carried == 1 }; } /// Works out the address an instruction operates on, spending the /// cycles the chip spends getting there. The access kind decides /// whether an indexed mode pays for its phantom read unconditionally. pub fn decode( self: *Self, comptime addressing_mode: CPU.AddressingModes, comptime access: Access, ) u16 { const cpu = self.getCPU(); return switch (addressing_mode) { .Absolute => addr: { break :addr cpu.program_counter.read(u16); }, .@"Absolute, X" => addr: { const base_address_low: u8 = cpu.program_counter.read(u8); const base_address_high: u8 = cpu.program_counter.read(u8); const index = cpu.registers.get(.X); self.phantomRead(access, base_address_high, base_address_low, index); break :addr (@as(u16, base_address_high) << 8 | base_address_low) +% index; }, .@"Absolute, Y" => addr: { const base_address_low: u8 = cpu.program_counter.read(u8); const base_address_high: u8 = cpu.program_counter.read(u8); const index = cpu.registers.get(.Y); self.phantomRead(access, base_address_high, base_address_low, index); break :addr (@as(u16, base_address_high) << 8 | base_address_low) +% index; }, .Implied => @compileError("illegal addressing mode"), // The shapes of the encodings the CMOS part does not decode. // Nothing here works out an address, because they have none: // whatever fetching they do, they do for themselves. .@"No Operand", .@"Two Operands" => @compileError("no address to work out"), // Two addresses rather than one, so the instruction fetches // them itself: there is nothing useful to hand back here. .@"Zero Page, Relative" => @compileError("two addresses, not one"), .@"(Indirect)" => addr: { const pointer = cpu.program_counter.read(u8); const low = cpu.memory.read(u8, pointer); const high = cpu.memory.read(u8, pointer +% 1); break :addr @as(u16, high) << 8 | low; }, .@"(Absolute, X)" => addr: { const base = cpu.program_counter.read(u16); // A cycle spent indexing, with the low byte of the operand // on the bus rather than anything to do with the address. _ = cpu.memory.read(u8, cpu.program_counter.value -% 2); const indirect = base +% cpu.registers.get(.X); const low = cpu.memory.read(u8, indirect); const high = cpu.memory.read(u8, indirect +% 1); break :addr @as(u16, high) << 8 | low; }, .Immediate => addr: { break :addr cpu.program_counter.immediate(); }, .Indirect => addr: { const indirect = cpu.program_counter.read(u16); if (comptime CPU.processor.cmos()) { // The CMOS part gets this right, and the way it does is // worth seeing: it makes the NMOS part's mistake first // and then corrects it. The low byte, then a read of // the address the old chip would have taken the high // byte from, and then a read of the one it should be, // which is the cycle this costs that the old one did // not. const low = cpu.memory.read(u8, indirect); _ = cpu.memory.read(u8, (indirect & 0xff00) | ((indirect +% 1) & 0x00ff)); const high = cpu.memory.read(u8, indirect +% 1); break :addr @as(u16, high) << 8 | low; } if (indirect & 0x00ff == 0x00ff) { // https://github.com/f0lg0/6502/blob/a6308d9950012a1ded0326e2f14b874c1d5f16b8/src/cpu8/instructions.c#L396-L420 // If the low byte of the supplied address is 0xFF, // then to read the high byte of the actual address we // need to cross a page boundary. This doesn't actually // work on the chip as designed, instead it wraps back // around in the same page, yielding an invalid actual // address. See: https://www.nesdev.com/6502bugs.txt const low = cpu.memory.read(u8, indirect); const high = cpu.memory.read(u8, indirect & 0xff00); break :addr @as(u16, high) << 8 | low; } else { break :addr cpu.memory.read(u16, indirect); } }, .@"(Indirect, X)" => addr: { var base_address_low: u8 = cpu.program_counter.read(u8); _ = cpu.memory.read(u8, base_address_low); // "phantom" read base_address_low +%= cpu.registers.get(.X); const address_low = cpu.memory.read(u8, base_address_low); const address_high = cpu.memory.read(u8, base_address_low +% 1); const address = @as(u16, address_high) << 8 | address_low; break :addr address; }, .@"(Indirect), Y" => addr: { const intermediate_address_low: u8 = cpu.program_counter.read(u8); const base_address_low = cpu.memory.read(u8, intermediate_address_low); const base_address_high = cpu.memory.read(u8, intermediate_address_low +% 1); const index = cpu.registers.get(.Y); self.phantomRead(access, base_address_high, base_address_low, index); break :addr (@as(u16, base_address_high) << 8 | base_address_low) +% index; }, .Relative => addr: { const offset: i8 = @bitCast(cpu.program_counter.read(u8)); break :addr cpu.program_counter.calculateBranchLocation(offset); }, .@"Zero Page" => addr: { break :addr cpu.program_counter.read(u8); }, .@"Zero Page, X" => addr: { const base_address_low = cpu.program_counter.read(u8); _ = cpu.memory.read(u8, base_address_low); // "phantom" read break :addr base_address_low +% cpu.registers.get(.X); }, .@"Zero Page, Y" => addr: { const base_address_low = cpu.program_counter.read(u8); _ = cpu.memory.read(u8, base_address_low); // "phantom" read break :addr base_address_low +% cpu.registers.get(.Y); }, }; } }; } test "Absolute" { const CPU = @import("../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); cpu.program_counter.value = 0x0080; try cpu.memory.loadProgram(0x0080, &[_]u8{ 0x50, 0x16 }); const actual = cpu.addressing_modes.decode(.Absolute, .read); try std.testing.expectEqual(@as(u16, 0x1650), actual); try std.testing.expectEqual(@as(u16, 0x0082), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 2), cpu.clock.cycles); }