From 08e88254e263b9dcfe3fc70fb2bde41b558b72fa Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 31 Aug 2026 14:58:12 -0500 Subject: [PATCH] document every function Every function in src now carries a doc comment, and the instruction implementations carry references to both manuals. The references in the tree were to the MOS MCS6500 Family Programming Manual, not to the 6502 User's Manual, so the two are now named. Carr's book is a hardware interfacing text and puts its whole instruction reference in chapter 16, pages 245 to 277, which is a different shape from the MCS6500 chapter and section numbering already here. Left unlabelled the two would have read as the same scheme disagreeing with itself. Carr's chapter 16 has no entry for TAY, so that one says so rather than pointing at a page that does not discuss it, and the nine instructions that never had an MCS6500 reference still do not, since that manual was not to hand to check. The undocumented opcodes get descriptions but no references, being in neither book. Two things turned up while writing the comments. Registers.changed read self.cpu.timer, and neither field exists; like the 16-bit write fixed earlier it was never called, so it was never instantiated and never compiled. It now goes through getCPU and the clock, the way Flags.since always did, and has a test so it stays compiled. And decode.zig is documented as what it is: unreferenced, and older than the opcode table it would have to work against. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01FawbWzEu5hoLLZSMCehUGP --- src/clock.zig | 6 +++++ src/cpu.zig | 22 ++++++++++++++++++ src/cpu/add.zig | 19 ++++++++++++++++ src/cpu/addressing_modes.zig | 9 ++++++++ src/cpu/flags.zig | 38 +++++++++++++++++++++++++++++--- src/cpu/memory.zig | 17 ++++++++++++++ src/cpu/opcodes.zig | 6 +++++ src/cpu/opcodes/adc.zig | 12 +++++++++- src/cpu/opcodes/alr.zig | 6 +++++ src/cpu/opcodes/anc.zig | 6 +++++ src/cpu/opcodes/and.zig | 11 ++++++++- src/cpu/opcodes/ane.zig | 6 +++++ src/cpu/opcodes/arr.zig | 6 +++++ src/cpu/opcodes/asl.zig | 12 +++++++++- src/cpu/opcodes/bcc.zig | 11 ++++++++- src/cpu/opcodes/bcs.zig | 11 ++++++++- src/cpu/opcodes/beq.zig | 13 ++++++++++- src/cpu/opcodes/bit.zig | 14 +++++++++++- src/cpu/opcodes/bmi.zig | 12 +++++++++- src/cpu/opcodes/bne.zig | 12 +++++++++- src/cpu/opcodes/bpl.zig | 11 ++++++++- src/cpu/opcodes/brk.zig | 11 +++++++++ src/cpu/opcodes/bvc.zig | 11 ++++++++- src/cpu/opcodes/bvs.zig | 11 ++++++++- src/cpu/opcodes/clc.zig | 12 +++++++++- src/cpu/opcodes/cld.zig | 12 +++++++++- src/cpu/opcodes/cli.zig | 13 ++++++++++- src/cpu/opcodes/clv.zig | 12 +++++++++- src/cpu/opcodes/cmp.zig | 13 ++++++++++- src/cpu/opcodes/cpx.zig | 12 +++++++++- src/cpu/opcodes/cpy.zig | 12 +++++++++- src/cpu/opcodes/dcp.zig | 6 +++++ src/cpu/opcodes/dec.zig | 12 +++++++++- src/cpu/opcodes/dex.zig | 12 +++++++++- src/cpu/opcodes/dey.zig | 12 +++++++++- src/cpu/opcodes/eor.zig | 10 +++++++++ src/cpu/opcodes/inc.zig | 12 +++++++++- src/cpu/opcodes/inx.zig | 12 +++++++++- src/cpu/opcodes/iny.zig | 12 +++++++++- src/cpu/opcodes/isc.zig | 6 +++++ src/cpu/opcodes/jam.zig | 6 +++++ src/cpu/opcodes/jmp.zig | 12 ++++++++++ src/cpu/opcodes/jsr.zig | 11 +++++++++ src/cpu/opcodes/las.zig | 6 +++++ src/cpu/opcodes/lax.zig | 6 +++++ src/cpu/opcodes/lda.zig | 12 +++++++++- src/cpu/opcodes/ldx.zig | 12 +++++++++- src/cpu/opcodes/ldy.zig | 12 +++++++++- src/cpu/opcodes/lsr.zig | 13 ++++++++++- src/cpu/opcodes/lxa.zig | 6 +++++ src/cpu/opcodes/nop.zig | 19 +++++++++++----- src/cpu/opcodes/ora.zig | 9 ++++++++ src/cpu/opcodes/pha.zig | 11 ++++++++- src/cpu/opcodes/php.zig | 12 +++++++++- src/cpu/opcodes/pla.zig | 11 ++++++++- src/cpu/opcodes/plp.zig | 11 ++++++++- src/cpu/opcodes/rla.zig | 6 +++++ src/cpu/opcodes/rol.zig | 12 +++++++++- src/cpu/opcodes/ror.zig | 12 +++++++++- src/cpu/opcodes/rra.zig | 6 +++++ src/cpu/opcodes/rti.zig | 11 +++++++++ src/cpu/opcodes/rts.zig | 10 +++++++++ src/cpu/opcodes/sax.zig | 6 +++++ src/cpu/opcodes/sbc.zig | 10 +++++++++ src/cpu/opcodes/sbx.zig | 6 +++++ src/cpu/opcodes/sec.zig | 11 ++++++++- src/cpu/opcodes/sed.zig | 11 ++++++++- src/cpu/opcodes/sei.zig | 11 ++++++++- src/cpu/opcodes/sha.zig | 6 +++++ src/cpu/opcodes/shx.zig | 6 +++++ src/cpu/opcodes/shy.zig | 6 +++++ src/cpu/opcodes/slo.zig | 6 +++++ src/cpu/opcodes/sre.zig | 6 +++++ src/cpu/opcodes/sta.zig | 12 +++++++++- src/cpu/opcodes/stx.zig | 12 +++++++++- src/cpu/opcodes/sty.zig | 12 +++++++++- src/cpu/opcodes/tas.zig | 6 +++++ src/cpu/opcodes/tax.zig | 12 +++++++++- src/cpu/opcodes/tay.zig | 12 +++++++++- src/cpu/opcodes/tsx.zig | 12 +++++++++- src/cpu/opcodes/txa.zig | 10 +++++++++ src/cpu/opcodes/txs.zig | 13 ++++++++++- src/cpu/opcodes/tya.zig | 12 +++++++++- src/cpu/program_counter.zig | 17 ++++++++++++++ src/cpu/registers.zig | 27 ++++++++++++++++++++++- src/cpu/stack.zig | 15 +++++++++++++ src/decode.zig | 2 ++ src/graphics/apple2/textmode.zig | 3 +++ src/main.zig | 11 +++++++++ src/root.zig | 7 ++++++ src/single_step_tests.zig | 9 ++++++++ src/single_step_tests/Cycle.zig | 5 +++++ src/single_step_tests/RAM.zig | 5 +++++ 93 files changed, 943 insertions(+), 55 deletions(-) diff --git a/src/clock.zig b/src/clock.zig index 5f9c0c5..b5bb112 100644 --- a/src/clock.zig +++ b/src/clock.zig @@ -5,19 +5,25 @@ pub const FakeClock = struct { cycles: u64, + /// Starts the clock at zero. The speed is accepted and ignored: nothing + /// here waits on real time, it only counts. pub fn init(self: *Self, clock_speed: ?u128) void { _ = clock_speed; self.cycles = 0; } + /// How many cycles have elapsed since the last reset. pub fn read(self: *const Self) u64 { return self.cycles; } + /// Spends cycles. Every bus access calls this, so the count is a tally of + /// what the processor did rather than a delay. pub fn sleep(self: *Self, cycles: u8) void { self.cycles +%= cycles; } + /// Puts the cycle count back to zero. pub fn reset(self: *Self) void { self.cycles = 0; } diff --git a/src/cpu.zig b/src/cpu.zig index ebae61a..7ac60c6 100644 --- a/src/cpu.zig +++ b/src/cpu.zig @@ -31,10 +31,16 @@ pub const Options = struct { // address: u16, // }; +/// A 6502, parameterised over the clock it counts cycles against. Everything +/// the processor owns hangs off the returned struct, and each part finds its +/// way back to the whole through @fieldParentPtr, so the CPU can be built in +/// place without any pointer wiring. pub fn CPU(comptime Clock: type) type { return struct { const Self = @This(); + /// An unsigned byte, unchanged. The counterpart to signed, so that + /// tests can write either without caring. pub fn unsigned(value: u8) u8 { return value; } @@ -44,6 +50,7 @@ pub fn CPU(comptime Clock: type) type { try std.testing.expectEqual(@as(u8, 128), result); } + /// A signed byte as the processor stores it, in two's complement. pub fn signed(value: i8) u8 { return @bitCast(value); } @@ -58,6 +65,8 @@ pub fn CPU(comptime Clock: type) type { try std.testing.expectEqual(@as(u8, 5), result); } + /// A number from 0 to 99 packed into two BCD nibbles, for setting up + /// decimal mode tests. pub fn toDecimal(value: u8) !u8 { if (value < 0) return error.IllegalValue; if (value > 99) return error.IllegalValue; @@ -91,6 +100,7 @@ pub fn CPU(comptime Clock: type) type { try std.testing.expectEqual(@as(u8, 0b10011001), result); } + /// Two BCD nibbles unpacked back into a number. pub fn fromDecimal(value: u8) u8 { const tmp: packed struct(u8) { low: u4, @@ -133,6 +143,8 @@ pub fn CPU(comptime Clock: type) type { memory: Memory, + /// Brings up every part of the processor in place. Does not fetch the + /// reset vector; call reset for that. pub fn init(self: *Self, memory_size: ?usize, clock_speed: ?u128) !void { self.clock.init(clock_speed); self.addressing_modes.init(); @@ -143,10 +155,14 @@ pub fn CPU(comptime Clock: type) type { self.flags.init(); } + /// Nothing to release. Present so callers can defer it and stay correct + /// if that changes. pub fn deinit(self: *Self) void { _ = self; } + /// Does what the reset line does: reloads the program counter from the + /// vector and clears the registers, flags and stack pointer. pub fn reset(self: *Self) void { self.program_counter.reset(); self.registers.reset(); @@ -154,6 +170,12 @@ pub fn CPU(comptime Clock: type) type { self.stack.reset(); } + /// Fetches one opcode and runs it, spending its cycles on the clock as + /// it goes. + /// + /// Break and Jam are not failures but the two ways an instruction can + /// end without the processor going on to the next one. IllegalOpcode + /// cannot currently happen, since all 256 opcodes decode. pub fn executeInstruction(self: *Self) error{ IllegalOpcode, Break, Jam }!void { const opcode = self.program_counter.read(u8); const data = Opcodes.get(opcode) orelse return error.IllegalOpcode; diff --git a/src/cpu/add.zig b/src/cpu/add.zig index 9f38910..3fc6165 100644 --- a/src/cpu/add.zig +++ b/src/cpu/add.zig @@ -27,16 +27,20 @@ const Nibble = enum { high, }; +/// Overflow: set when two operands of the same sign produce a result of the +/// other sign. inline fn test_V(augend: u8, addend: u8, binary_sum: u8) bool { const arg_sign_eq = (augend ^ addend) & 0x80 == 0; const prod_sign_neq = (addend ^ binary_sum) & 0x80 != 0; return arg_sign_eq and prod_sign_neq; } +/// Negative: bit 7 of the result. inline fn test_N(a: u8) bool { return a & 0x80 == 0x80; } +/// Zero: whether the result is zero. inline fn test_Z(a: u8) bool { return a == 0; } @@ -46,6 +50,9 @@ const CarryResult = struct { decimal_carry_flag: bool, }; +/// Carry out of a nibble. Decimal mode carries at 9 rather than 15, and +/// subtraction always uses the binary answer because its correction happens +/// afterwards. inline fn compute_carry(tmp: u8, decimal_flag: bool, comptime operation: Operation) CarryResult { const binary_carry_flag = tmp > 0x0f; const decimal_carry_flag = tmp > 0x09; @@ -61,6 +68,9 @@ inline fn compute_carry(tmp: u8, decimal_flag: bool, comptime operation: Operati }; } +/// Pulls a nibble back into range after a decimal add or subtract, by 6 in the +/// direction of the operation. The 65C02 takes an extra step off the high +/// nibble that the 6502 does not. inline fn decimal_correction( a: u4, carry_result: CarryResult, @@ -93,6 +103,8 @@ const PartialResult = struct { carry_result: CarryResult, }; +/// Adds one nibble, giving both the raw binary answer and the decimal corrected +/// one, since the flags come from one and the result from the other. inline fn compute_partial( augend: u4, addend: u4, @@ -120,6 +132,13 @@ inline fn compute_partial( }; } +/// The whole of the 6502's addition and subtraction, a nibble at a time. +/// +/// Subtraction is addition of the complement, so the caller passes ~addend and +/// the operation only changes how carry and the decimal correction behave. The +/// flags come from the binary sum even in decimal mode, which is why both are +/// carried through: it is what makes N and V meaningless but predictable on a +/// decimal ADC. pub fn add(augend: u8, addend: u8, carry_flag: bool, decimal_flag: bool, comptime operation: Operation, comptime processor: Processor) Result { const augend_l: u4 = @truncate(augend); const augend_h: u4 = @truncate(augend >> 4); diff --git a/src/cpu/addressing_modes.zig b/src/cpu/addressing_modes.zig index cdc96b1..94b71a2 100644 --- a/src/cpu/addressing_modes.zig +++ b/src/cpu/addressing_modes.zig @@ -2,14 +2,20 @@ 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)); } @@ -81,6 +87,9 @@ pub fn AddressingModes(comptime CPU: type) type { 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, diff --git a/src/cpu/flags.zig b/src/cpu/flags.zig index 0c1c425..a1ee1d1 100644 --- a/src/cpu/flags.zig +++ b/src/cpu/flags.zig @@ -1,5 +1,10 @@ const std = @import("std"); +/// The processor status register. Each flag is kept separately rather than +/// packed, with the byte assembled only when something pushes or reads it, and +/// each carries the cycle it last changed on so the debugger can highlight it. +/// 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 Flags(comptime CPU: type) type { return struct { const Self = @This(); @@ -23,10 +28,13 @@ pub fn Flags(comptime CPU: type) type { _values: std.EnumArray(Name, bool), _changed: std.EnumArray(Name, u64), + /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("flags", self)); } + /// Clears every flag except that N and Z are set from the accumulator, + /// for a CPU that is not running yet. pub fn init(self: *Self) void { const cpu = self.getCPU(); @@ -44,6 +52,7 @@ pub fn Flags(comptime CPU: type) type { }; } + /// As init, but timestamps the flags against the running clock. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self._values = .init(.{ @@ -58,49 +67,59 @@ pub fn Flags(comptime CPU: type) type { self._changed = .initFill(cpu.clock.read()); } + /// How many cycles ago the flag last changed. pub fn since(self: *Self, comptime flag: Name) u64 { const cpu = self.getCPU(); return cpu.clock.read() - self._changed.get(flag); } + /// Whether the flag is set. pub fn isSet(self: *Self, comptime flag: Name) bool { return self._values.get(flag) == true; } + /// Whether the flag is clear. pub fn isClear(self: *Self, comptime flag: Name) bool { return self._values.get(flag) == false; } + /// Sets a flag. pub fn set(self: *Self, comptime flag: Name) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, true); } + /// Clears a flag. pub fn clear(self: *Self, comptime flag: Name) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, false); } + /// Inverts a flag. pub fn toggle(self: *Self, comptime flag: Name) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, !self._values.get(flag)); } + /// Sets a flag to a computed value. pub fn update(self: *Self, comptime flag: Name, value: bool) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, value); } + /// Reports a result through N and Z, which nearly every instruction + /// that produces a value does. pub fn updateNZ(self: *Self, value: u8) void { self.update(.N, value & 0x080 == 0x80); self.update(.Z, value == 0x00); } - /// Convenience function for ADC/SBC + /// Carry as a number to add in, for the arithmetic and rotate + /// instructions. pub fn carry(self: *const Self, comptime T: type) T { const info = @typeInfo(T); if (info != .int) @compileError("must be an integer"); @@ -129,7 +148,8 @@ pub fn Flags(comptime CPU: type) type { N: bool = false, }; - /// return the processor status flags as a u8 + /// The status register as a byte, with the unused bit 5 set as the + /// hardware always reads it. pub fn save(self: *const Self) u8 { var value: Value = undefined; inline for (@typeInfo(Name).@"enum".fields) |field| { @@ -139,7 +159,9 @@ pub fn Flags(comptime CPU: type) type { return @bitCast(value); } - /// return the processor status flags as a u8 with the B flag set + /// As save, but with the break flag set in the copy. BRK and PHP push + /// this rather than the live flag, which is never actually stored in + /// the register. pub fn saveBRK(self: *Self) u8 { var value: Value = undefined; inline for (@typeInfo(Name).@"enum".fields) |field| { @@ -150,6 +172,8 @@ pub fn Flags(comptime CPU: type) type { return @bitCast(value); } + /// Loads the status register from a byte, keeping the current break + /// flag because nothing can pull one back in. pub fn restore(self: *Self, value: u8) void { const cpu = self.getCPU(); var new: Value = @bitCast(value); @@ -160,6 +184,8 @@ pub fn Flags(comptime CPU: type) type { self._changed = .initFill(cpu.clock.read()); } + /// The status register as a byte, for tests and the debugger. Unlike + /// save it reports the break flag as it stands. pub fn dump(self: *Self) u8 { var new: Value = undefined; new._5 = true; @@ -169,6 +195,8 @@ pub fn Flags(comptime CPU: type) type { return @bitCast(new); } + /// Loads the status register from a byte wholesale, break flag + /// included, for tests setting up a starting state. pub fn load(self: *Self, value: u8) void { const new: Value = @bitCast(value); inline for (@typeInfo(Name).@"enum".fields) |field| { @@ -176,6 +204,8 @@ pub fn Flags(comptime CPU: type) type { } } + /// Whether every flag matches the given byte. Bit 5 is not compared, + /// having no flag behind it. pub fn compare(self: *const Self, value: u8) bool { const other: Value = @bitCast(value); inline for (@typeInfo(Name).@"enum".fields) |field| { @@ -184,6 +214,8 @@ pub fn Flags(comptime CPU: type) type { return true; } + /// Writes the flags as a row of letters, capital for set and lower case + /// for clear, with a dash for the unused bit. pub fn format(_: *Self, writer: *std.Io.Writer, value_: u8) !void { const value: Value = @bitCast(value_); const info = @typeInfo(Value); diff --git a/src/cpu/memory.zig b/src/cpu/memory.zig index 7549f42..b05ce27 100644 --- a/src/cpu/memory.zig +++ b/src/cpu/memory.zig @@ -2,6 +2,10 @@ const std = @import("std"); const memory_size = std.math.maxInt(u16) + 1; +/// The 64K address space, plus a record of which of it is read-only and a log +/// of the accesses the current instruction has made. 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 Memory(comptime CPU: type) type { return struct { const Self = @This(); @@ -27,16 +31,19 @@ pub fn Memory(comptime CPU: type) type { read_only: std.StaticBitSet(memory_size), changed: [memory_size]u64 = undefined, + /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("memory", self)); } + /// Appends an access to the bus log, dropping it if the log is full. fn record(self: *Self, address: u16, value: u8, direction: Access.Direction) void { if (self.bus_len == self.bus.len) return; self.bus[self.bus_len] = .{ .address = address, .value = value, .direction = direction }; self.bus_len += 1; } + /// Zeroes all of memory and marks all of it writable. pub fn init(self: *Self, size: ?usize) void { self.* = .{ .bus_len = 0, @@ -47,6 +54,7 @@ pub fn Memory(comptime CPU: type) type { }; } + /// As init, but keeps the configured size. pub fn reset(self: *Self) void { self.bus_len = 0; self.memory = [_]u8{0} ** memory_size; @@ -54,6 +62,8 @@ pub fn Memory(comptime CPU: type) type { self.changed = [_]u64{0} ** memory_size; } + /// Copies a program in and marks that range read-only, so writes to it + /// are driven onto the bus but never stored. pub fn loadROM(self: *Self, location: u16, program: []const u8) !void { const start: usize = @intCast(location); const end: usize = start + program.len; @@ -62,10 +72,13 @@ pub fn Memory(comptime CPU: type) type { self.read_only.setRangeValue(.{ .start = start, .end = end }, true); } + /// Loads a ROM so that its last byte sits at $ffff, which is where a + /// 6502 expects to find its vectors. pub fn loadROMAtEnd(self: *Self, program: []const u8) !void { try self.loadROM(@truncate(memory_size - program.len), program); } + /// Copies a program in and leaves it writable. pub fn loadProgram(self: *Self, location: u16, program: []const u8) !void { const start: usize = @intCast(location); const end: usize = start + program.len; @@ -73,6 +86,8 @@ pub fn Memory(comptime CPU: type) type { @memcpy(self.memory[start..end], program); } + /// Reads memory, spending a cycle per byte and logging each one. A word + /// is read low byte first. pub fn read(self: *Self, comptime T: type, offset: u16) T { const cpu = self.getCPU(); const info = @typeInfo(T); @@ -93,6 +108,8 @@ pub fn Memory(comptime CPU: type) type { } } + /// Writes memory, spending a cycle per byte and logging each one. A + /// word is written low byte first. pub fn write(self: *Self, comptime T: type, offset: u16, value: T) void { const cpu = self.getCPU(); const info = @typeInfo(T); diff --git a/src/cpu/opcodes.zig b/src/cpu/opcodes.zig index 5b6ea7c..5b17a88 100644 --- a/src/cpu/opcodes.zig +++ b/src/cpu/opcodes.zig @@ -1,5 +1,8 @@ const std = @import("std"); +/// The instruction table, built at comptime from the modules listed below. Each +/// contributes its opcodes, and a duplicate is a compile error rather than a +/// silently overwritten entry. pub fn Opcodes(comptime CPU: type) type { return struct { pub const Error = error{ @@ -7,6 +10,9 @@ pub fn Opcodes(comptime CPU: type) type { Jam, }; + /// Looks up an opcode. Never null now that every encoding is + /// implemented, but kept optional so an incomplete table stays + /// representable. pub fn get(opcode: u8) ?Data { return table[opcode]; } diff --git a/src/cpu/opcodes/adc.zig b/src/cpu/opcodes/adc.zig index 1be592d..e9792f5 100644 --- a/src/cpu/opcodes/adc.zig +++ b/src/cpu/opcodes/adc.zig @@ -1,6 +1,9 @@ const std = @import("std"); const add = @import("../add.zig").add; +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ADC"; @@ -47,11 +50,18 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (cpu: *CPU) error{}!void { const f = struct { // https://github.com/SourMesen/Mesen-S/blob/89199f298a95345a893d814525b6668567a6d490/Core/Cpu.Instructions.h#L4-L33 - /// 2.2.1 page 7 + /// Adds memory and the carry flag into the accumulator. Honours + /// decimal mode. + /// + /// MCS6500 Programming Manual 2.2.1 page 7 + /// Carr, 6502 User's Manual page 245 pub fn ADC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); diff --git a/src/cpu/opcodes/alr.zig b/src/cpu/opcodes/alr.zig index 618aac9..471fbaa 100644 --- a/src/cpu/opcodes/alr.zig +++ b/src/cpu/opcodes/alr.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ALR"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/anc.zig b/src/cpu/opcodes/anc.zig index 593de47..50061ff 100644 --- a/src/cpu/opcodes/anc.zig +++ b/src/cpu/opcodes/anc.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ANC"; @@ -14,6 +17,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/and.zig b/src/cpu/opcodes/and.zig index 81f38c6..bae6db8 100644 --- a/src/cpu/opcodes/and.zig +++ b/src/cpu/opcodes/and.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "AND"; @@ -44,9 +47,15 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (cpu: *CPU) error{}!void { const f = struct { - /// 2.2.4.1 page 20 + /// ANDs memory into the accumulator. + /// + /// MCS6500 Programming Manual 2.2.4.1 page 20 + /// Carr, 6502 User's Manual page 246 pub fn AND(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/ane.zig b/src/cpu/opcodes/ane.zig index ba36434..9e44ae7 100644 --- a/src/cpu/opcodes/ane.zig +++ b/src/cpu/opcodes/ane.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ANE"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/arr.zig b/src/cpu/opcodes/arr.zig index 5089c83..cc9a067 100644 --- a/src/cpu/opcodes/arr.zig +++ b/src/cpu/opcodes/arr.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ARR"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/asl.zig b/src/cpu/opcodes/asl.zig index 33f9f7a..e57c088 100644 --- a/src/cpu/opcodes/asl.zig +++ b/src/cpu/opcodes/asl.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ASL"; @@ -29,9 +32,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 10.2 page 149 + /// Shifts the accumulator or the addressed byte left one bit. + /// Bit 7 falls into carry and a zero comes in at the bottom. + /// + /// MCS6500 Programming Manual 10.2 page 149 + /// Carr, 6502 User's Manual page 247 pub fn ASL(cpu: *CPU) error{}!void { var address: ?u16 = null; const value = switch (comptime addressing_mode) { diff --git a/src/cpu/opcodes/bcc.zig b/src/cpu/opcodes/bcc.zig index bf57eb6..dec7039 100644 --- a/src/cpu/opcodes/bcc.zig +++ b/src/cpu/opcodes/bcc.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BCC"; @@ -9,9 +12,15 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 4.1.2.3 page 40 + /// Branches when carry is clear. + /// + /// MCS6500 Programming Manual 4.1.2.3 page 40 + /// Carr, 6502 User's Manual page 248 pub fn BCC(cpu: *CPU) error{}!void { if (addressing_mode != .Relative) return error.IllegalAddressingMode; const address = cpu.addressing_modes.decode(addressing_mode, .read); diff --git a/src/cpu/opcodes/bcs.zig b/src/cpu/opcodes/bcs.zig index 8d5b75c..bb22d71 100644 --- a/src/cpu/opcodes/bcs.zig +++ b/src/cpu/opcodes/bcs.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BCS"; @@ -9,9 +12,15 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 4.1.2.4 page 40 + /// Branches when carry is set. + /// + /// MCS6500 Programming Manual 4.1.2.4 page 40 + /// Carr, 6502 User's Manual page 248 pub fn BCS(cpu: *CPU) error{}!void { if (addressing_mode != .Relative) return error.IllegalAddressingMode; const address = cpu.addressing_modes.decode(addressing_mode, .read); diff --git a/src/cpu/opcodes/beq.zig b/src/cpu/opcodes/beq.zig index d76adb8..fd28b68 100644 --- a/src/cpu/opcodes/beq.zig +++ b/src/cpu/opcodes/beq.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BEQ"; @@ -8,10 +11,18 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { - /// 4.1.2.5 page 41 + /// Branches when the zero flag is set, that is when the last + /// result was zero. + /// + /// MCS6500 Programming Manual 4.1.2.5 page 41 + /// Carr, 6502 User's Manual page 249 pub fn BEQ(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isSet(.Z)) cpu.program_counter.branch(address); diff --git a/src/cpu/opcodes/bit.zig b/src/cpu/opcodes/bit.zig index f3903bc..6b7114a 100644 --- a/src/cpu/opcodes/bit.zig +++ b/src/cpu/opcodes/bit.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BIT"; @@ -13,9 +16,18 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 4.2.2.1 page 47 + /// Tests bits of memory against the accumulator without keeping + /// the result: bits 7 and 6 of the byte go straight into N and + /// V, and Z reports whether the AND came out zero. + /// + /// MCS6500 Programming Manual 4.2.2.1 page 47 + /// Carr, 6502 User's Manual page 249 pub fn BIT(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/bmi.zig b/src/cpu/opcodes/bmi.zig index f8ec730..3bd2ebd 100644 --- a/src/cpu/opcodes/bmi.zig +++ b/src/cpu/opcodes/bmi.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BMI"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { - /// 4.1.2.1 page 40 + /// Branches when the last result was negative. + /// + /// MCS6500 Programming Manual 4.1.2.1 page 40 + /// Carr, 6502 User's Manual page 250 pub fn BMI(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isSet(.N)) cpu.program_counter.branch(address); diff --git a/src/cpu/opcodes/bne.zig b/src/cpu/opcodes/bne.zig index bd9500c..b81c644 100644 --- a/src/cpu/opcodes/bne.zig +++ b/src/cpu/opcodes/bne.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BNE"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { - /// 4.1.2.6 page 41 + /// Branches when the zero flag is clear. + /// + /// MCS6500 Programming Manual 4.1.2.6 page 41 + /// Carr, 6502 User's Manual page 251 pub fn BNE(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isClear(.Z)) cpu.program_counter.branch(address); diff --git a/src/cpu/opcodes/bpl.zig b/src/cpu/opcodes/bpl.zig index cd1908c..55b10e0 100644 --- a/src/cpu/opcodes/bpl.zig +++ b/src/cpu/opcodes/bpl.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BPL"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { - /// 4.1.2.2 page 40 + /// Branches when the last result was positive. + /// + /// MCS6500 Programming Manual 4.1.2.2 page 40 + /// Carr, 6502 User's Manual page 251 pub fn BPL(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isClear(.N)) cpu.program_counter.branch(address); diff --git a/src/cpu/opcodes/brk.zig b/src/cpu/opcodes/brk.zig index 9f4d804..b607e8f 100644 --- a/src/cpu/opcodes/brk.zig +++ b/src/cpu/opcodes/brk.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BRK"; @@ -9,9 +12,17 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { + /// Forces an interrupt: pushes the program counter and the + /// status register, sets the interrupt disable and vectors + /// through $fffe. + /// + /// Carr, 6502 User's Manual page 252 pub fn BRK(cpu: *CPU) CPU.Opcodes.Error!void { // BRK is two bytes wide; the padding byte is fetched and // discarded diff --git a/src/cpu/opcodes/bvc.zig b/src/cpu/opcodes/bvc.zig index dc1ac7f..77782c6 100644 --- a/src/cpu/opcodes/bvc.zig +++ b/src/cpu/opcodes/bvc.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BVC"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { - /// 4.1.2.8 page 41 + /// Branches when overflow is clear. + /// + /// MCS6500 Programming Manual 4.1.2.8 page 41 + /// Carr, 6502 User's Manual page 252 pub fn BVC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isClear(.V)) cpu.program_counter.branch(address); diff --git a/src/cpu/opcodes/bvs.zig b/src/cpu/opcodes/bvs.zig index a31b51a..922b8c2 100644 --- a/src/cpu/opcodes/bvs.zig +++ b/src/cpu/opcodes/bvs.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BVS"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { - /// 4.1.2.7 page 41 + /// Branches when overflow is set. + /// + /// MCS6500 Programming Manual 4.1.2.7 page 41 + /// Carr, 6502 User's Manual page 253 pub fn BVS(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isSet(.V)) cpu.program_counter.branch(address); diff --git a/src/cpu/opcodes/clc.zig b/src/cpu/opcodes/clc.zig index 31d3ca2..bb0ac51 100644 --- a/src/cpu/opcodes/clc.zig +++ b/src/cpu/opcodes/clc.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CLC"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.0.2 page 25 + /// Clears the carry flag. + /// + /// MCS6500 Programming Manual 3.0.2 page 25 + /// Carr, 6502 User's Manual page 253 pub fn CLC(cpu: *CPU) !void { cpu.flags.clear(.C); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/cld.zig b/src/cpu/opcodes/cld.zig index f08331f..b104509 100644 --- a/src/cpu/opcodes/cld.zig +++ b/src/cpu/opcodes/cld.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CLD"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.3.2 page 27 + /// Clears decimal mode. + /// + /// MCS6500 Programming Manual 3.3.2 page 27 + /// Carr, 6502 User's Manual page 254 pub fn CLD(cpu: *CPU) !void { cpu.flags.clear(.D); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/cli.zig b/src/cpu/opcodes/cli.zig index b80906b..ec93503 100644 --- a/src/cpu/opcodes/cli.zig +++ b/src/cpu/opcodes/cli.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CLI"; @@ -8,10 +11,18 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.2.2 page 26 + /// Clears the interrupt disable, letting interrupts through + /// again. + /// + /// MCS6500 Programming Manual 3.2.2 page 26 + /// Carr, 6502 User's Manual page 254 pub fn CLI(cpu: *CPU) !void { cpu.flags.clear(.I); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/clv.zig b/src/cpu/opcodes/clv.zig index dbcb5bb..3c9efa1 100644 --- a/src/cpu/opcodes/clv.zig +++ b/src/cpu/opcodes/clv.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CLV"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.6.1 page 28 + /// Clears the overflow flag. + /// + /// MCS6500 Programming Manual 3.6.1 page 28 + /// Carr, 6502 User's Manual page 254 pub fn CLV(cpu: *CPU) CPU.Opcodes.Error!void { cpu.flags.clear(.V); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/cmp.zig b/src/cpu/opcodes/cmp.zig index 2981430..c70b2d0 100644 --- a/src/cpu/opcodes/cmp.zig +++ b/src/cpu/opcodes/cmp.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CMP"; @@ -44,9 +47,17 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 4.2.1 page 45 + /// Compares memory against the accumulator, setting the flags + /// from the subtraction but keeping neither operand. + /// + /// MCS6500 Programming Manual 4.2.1 page 45 + /// Carr, 6502 User's Manual page 255 pub fn CMP(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/cpx.zig b/src/cpu/opcodes/cpx.zig index 54ac15e..02cdcdb 100644 --- a/src/cpu/opcodes/cpx.zig +++ b/src/cpu/opcodes/cpx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CPX"; @@ -18,9 +21,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 7.8 page 99 + /// Compares memory against the X register. + /// + /// MCS6500 Programming Manual 7.8 page 99 + /// Carr, 6502 User's Manual page 256 pub fn CPX(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/cpy.zig b/src/cpu/opcodes/cpy.zig index d61606a..cd1f98a 100644 --- a/src/cpu/opcodes/cpy.zig +++ b/src/cpu/opcodes/cpy.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "CPY"; @@ -18,9 +21,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 7.9 page 99 + /// Compares memory against the Y register. + /// + /// MCS6500 Programming Manual 7.9 page 99 + /// Carr, 6502 User's Manual page 257 pub fn CPY(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/dcp.zig b/src/cpu/opcodes/dcp.zig index e6fcfb6..d9825b7 100644 --- a/src/cpu/opcodes/dcp.zig +++ b/src/cpu/opcodes/dcp.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "DCP"; @@ -39,6 +42,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// A DEC of the addressed byte, with the accumulator then diff --git a/src/cpu/opcodes/dec.zig b/src/cpu/opcodes/dec.zig index 384d4ff..d811d3d 100644 --- a/src/cpu/opcodes/dec.zig +++ b/src/cpu/opcodes/dec.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "DEC"; @@ -23,9 +26,16 @@ pub fn data(comptime CPU: type) type { .cycles = 7, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 10.8 page 155 + /// Decrements the addressed byte by one. + /// + /// MCS6500 Programming Manual 10.8 page 155 + /// Carr, 6502 User's Manual page 257 pub fn DEC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/dex.zig b/src/cpu/opcodes/dex.zig index 3a79000..e561f1c 100644 --- a/src/cpu/opcodes/dex.zig +++ b/src/cpu/opcodes/dex.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "DEX"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.6 page 98 + /// Decrements the X register by one. + /// + /// MCS6500 Programming Manual 7.6 page 98 + /// Carr, 6502 User's Manual page 258 pub fn DEX(cpu: *CPU) CPU.Opcodes.Error!void { const value = cpu.registers.get(.X) -% 1; cpu.registers.set(.X, value); diff --git a/src/cpu/opcodes/dey.zig b/src/cpu/opcodes/dey.zig index 14d7cb1..60cf6b5 100644 --- a/src/cpu/opcodes/dey.zig +++ b/src/cpu/opcodes/dey.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "DEY"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.7 page 98 + /// Decrements the Y register by one. + /// + /// MCS6500 Programming Manual 7.7 page 98 + /// Carr, 6502 User's Manual page 258 pub fn DEY(cpu: *CPU) CPU.Opcodes.Error!void { const value = cpu.registers.get(.Y) -% 1; cpu.registers.set(.Y, value); diff --git a/src/cpu/opcodes/eor.zig b/src/cpu/opcodes/eor.zig index ce96c50..803ebb2 100644 --- a/src/cpu/opcodes/eor.zig +++ b/src/cpu/opcodes/eor.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "EOR"; @@ -43,8 +46,15 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { + /// Exclusive-ORs memory into the accumulator. + /// + /// Carr, 6502 User's Manual page 259 pub fn EOR(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/inc.zig b/src/cpu/opcodes/inc.zig index 35e21b7..2a0ec59 100644 --- a/src/cpu/opcodes/inc.zig +++ b/src/cpu/opcodes/inc.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "INC"; @@ -23,9 +26,16 @@ pub fn data(comptime CPU: type) type { .cycles = 7, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 10.7 page 155 + /// Increments the addressed byte by one. + /// + /// MCS6500 Programming Manual 10.7 page 155 + /// Carr, 6502 User's Manual page 260 pub fn INC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/inx.zig b/src/cpu/opcodes/inx.zig index 09cd8f6..1269e01 100644 --- a/src/cpu/opcodes/inx.zig +++ b/src/cpu/opcodes/inx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "INX"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.4 page 97 + /// Increments the X register by one. + /// + /// MCS6500 Programming Manual 7.4 page 97 + /// Carr, 6502 User's Manual page 260 pub fn INX(cpu: *CPU) CPU.Opcodes.Error!void { const value = cpu.registers.get(.X) +% 1; cpu.registers.set(.X, value); diff --git a/src/cpu/opcodes/iny.zig b/src/cpu/opcodes/iny.zig index d7e0624..c139e99 100644 --- a/src/cpu/opcodes/iny.zig +++ b/src/cpu/opcodes/iny.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "INY"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.5 page 97 + /// Increments the Y register by one. + /// + /// MCS6500 Programming Manual 7.5 page 97 + /// Carr, 6502 User's Manual page 261 pub fn INY(cpu: *CPU) CPU.Opcodes.Error!void { const value = cpu.registers.get(.Y) +% 1; cpu.registers.set(.Y, value); diff --git a/src/cpu/opcodes/isc.zig b/src/cpu/opcodes/isc.zig index 5e0ac80..9da96a6 100644 --- a/src/cpu/opcodes/isc.zig +++ b/src/cpu/opcodes/isc.zig @@ -1,5 +1,8 @@ const add = @import("../add.zig").add; +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ISC"; @@ -41,6 +44,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// An INC of the addressed byte, with the incremented value diff --git a/src/cpu/opcodes/jam.zig b/src/cpu/opcodes/jam.zig index 47bcf43..de0bbf9 100644 --- a/src/cpu/opcodes/jam.zig +++ b/src/cpu/opcodes/jam.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "JAM"; @@ -64,6 +67,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/jmp.zig b/src/cpu/opcodes/jmp.zig index 1d89075..7127912 100644 --- a/src/cpu/opcodes/jmp.zig +++ b/src/cpu/opcodes/jmp.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "JMP"; @@ -14,8 +17,17 @@ pub fn data(comptime CPU: type) type { .cycles = 5, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { + /// Jumps to a new address. The indirect form carries the + /// hardware bug where a vector ending in $ff takes its high + /// byte from the start of the same page. + /// + /// Carr, 6502 User's Manual page 261 pub fn JMP(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); cpu.program_counter.jump(address); diff --git a/src/cpu/opcodes/jsr.zig b/src/cpu/opcodes/jsr.zig index fcbc4bc..7fa0824 100644 --- a/src/cpu/opcodes/jsr.zig +++ b/src/cpu/opcodes/jsr.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "JSR"; @@ -8,9 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 6, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Absolute) @compileError("illegal addressing mode"); const f = struct { + /// Calls a subroutine, pushing the address of its own last byte + /// rather than of the next instruction. + /// + /// Carr, 6502 User's Manual page 262 pub fn JSR(cpu: *CPU) error{}!void { // we can't use cpu.addressing_modes.decode because // reads/writes of the stack and program counter are diff --git a/src/cpu/opcodes/las.zig b/src/cpu/opcodes/las.zig index 100de8e..78955ef 100644 --- a/src/cpu/opcodes/las.zig +++ b/src/cpu/opcodes/las.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LAS"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .@"Absolute, Y") @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/lax.zig b/src/cpu/opcodes/lax.zig index bfc83cb..cd91bec 100644 --- a/src/cpu/opcodes/lax.zig +++ b/src/cpu/opcodes/lax.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LAX"; @@ -34,6 +37,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// An LDA and an LDX of the same byte at once. diff --git a/src/cpu/opcodes/lda.zig b/src/cpu/opcodes/lda.zig index 7f30285..ce05b43 100644 --- a/src/cpu/opcodes/lda.zig +++ b/src/cpu/opcodes/lda.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LDA"; @@ -43,9 +46,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 2.1.1 page 4 + /// Loads the accumulator from memory. + /// + /// MCS6500 Programming Manual 2.1.1 page 4 + /// Carr, 6502 User's Manual page 263 pub fn LDA(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/ldx.zig b/src/cpu/opcodes/ldx.zig index 14e232f..330b167 100644 --- a/src/cpu/opcodes/ldx.zig +++ b/src/cpu/opcodes/ldx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LDX"; @@ -28,9 +31,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 7.1 page 96 + /// Loads the X register from memory. + /// + /// MCS6500 Programming Manual 7.1 page 96 + /// Carr, 6502 User's Manual page 264 pub fn LDX(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/ldy.zig b/src/cpu/opcodes/ldy.zig index 16f175f..7301d5d 100644 --- a/src/cpu/opcodes/ldy.zig +++ b/src/cpu/opcodes/ldy.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LDY"; @@ -28,9 +31,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 7.1 page 96 + /// Loads the Y register from memory. + /// + /// MCS6500 Programming Manual 7.1 page 96 + /// Carr, 6502 User's Manual page 264 pub fn LDY(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/lsr.zig b/src/cpu/opcodes/lsr.zig index 326a941..a69ce36 100644 --- a/src/cpu/opcodes/lsr.zig +++ b/src/cpu/opcodes/lsr.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LSR"; @@ -28,9 +31,17 @@ pub fn data(comptime CPU: type) type { .cycles = 7, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 10.1 page 148 + /// Shifts the accumulator or the addressed byte right one bit. + /// Bit 0 falls into carry and a zero comes in at the top. + /// + /// MCS6500 Programming Manual 10.1 page 148 + /// Carr, 6502 User's Manual page 265 pub fn LSR(cpu: *CPU) error{}!void { var address: ?u16 = null; const value = switch (addressing_mode) { diff --git a/src/cpu/opcodes/lxa.zig b/src/cpu/opcodes/lxa.zig index b2b587f..9618fdb 100644 --- a/src/cpu/opcodes/lxa.zig +++ b/src/cpu/opcodes/lxa.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "LXA"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/nop.zig b/src/cpu/opcodes/nop.zig index fb8f4a9..e9ba6bc 100644 --- a/src/cpu/opcodes/nop.zig +++ b/src/cpu/opcodes/nop.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "NOP"; @@ -144,13 +147,19 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { const f = struct { - /// page B-19. The documented NOP is the implied one; the rest - /// are undocumented encodings that go through the motions of an - /// instruction and throw the result away. They still take the - /// cycles and still touch the bus, which is the only way to - /// tell them apart from each other. + /// Does nothing. The documented NOP is the implied one; the + /// rest are undocumented encodings that go through the motions + /// of an instruction and throw the result away. They still take + /// the cycles and still touch the bus, which is the only thing + /// telling them apart. + /// + /// MCS6500 Programming Manual appendix B page B-19 + /// Carr, 6502 User's Manual page 265 pub fn NOP(cpu: *CPU) !void { switch (comptime addressing_mode) { .Implied => cpu.program_counter.phantomRead(), diff --git a/src/cpu/opcodes/ora.zig b/src/cpu/opcodes/ora.zig index ebacf55..08fc43f 100644 --- a/src/cpu/opcodes/ora.zig +++ b/src/cpu/opcodes/ora.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ORA"; @@ -44,8 +47,14 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { + /// ORs memory into the accumulator. + /// + /// Carr, 6502 User's Manual page 266 pub fn ORA(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); diff --git a/src/cpu/opcodes/pha.zig b/src/cpu/opcodes/pha.zig index 7ad448d..ed698f3 100644 --- a/src/cpu/opcodes/pha.zig +++ b/src/cpu/opcodes/pha.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "PHA"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 8.5 page 117 + /// Pushes the accumulator onto the stack. + /// + /// MCS6500 Programming Manual 8.5 page 117 + /// Carr, 6502 User's Manual page 267 pub fn PHA(cpu: *CPU) CPU.Opcodes.Error!void { // the operand fetch every single byte instruction performs // and discards diff --git a/src/cpu/opcodes/php.zig b/src/cpu/opcodes/php.zig index 54059a7..05f96b8 100644 --- a/src/cpu/opcodes/php.zig +++ b/src/cpu/opcodes/php.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "PHP"; @@ -9,10 +12,17 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 8.11 page 122 + /// Pushes the status register onto the stack, with the break + /// flag set in the pushed copy. + /// + /// MCS6500 Programming Manual 8.11 page 122 + /// Carr, 6502 User's Manual page 267 pub fn PHP(cpu: *CPU) CPU.Opcodes.Error!void { // the operand fetch every single byte instruction performs // and discards diff --git a/src/cpu/opcodes/pla.zig b/src/cpu/opcodes/pla.zig index 9d70151..fa5da2e 100644 --- a/src/cpu/opcodes/pla.zig +++ b/src/cpu/opcodes/pla.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "PLA"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 8.6 page 118 + /// Pulls the accumulator off the stack. + /// + /// MCS6500 Programming Manual 8.6 page 118 + /// Carr, 6502 User's Manual page 268 pub fn PLA(cpu: *CPU) !void { // the discarded operand fetch, then the discarded read // of the stack slot the pointer is sitting on before it diff --git a/src/cpu/opcodes/plp.zig b/src/cpu/opcodes/plp.zig index 1eebfc8..613b8e1 100644 --- a/src/cpu/opcodes/plp.zig +++ b/src/cpu/opcodes/plp.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "PLP"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 8.12 page 123 + /// Pulls the status register off the stack. + /// + /// MCS6500 Programming Manual 8.12 page 123 + /// Carr, 6502 User's Manual page 268 pub fn PLP(cpu: *CPU) CPU.Opcodes.Error!void { // the discarded operand fetch, then the discarded read // of the stack slot the pointer is sitting on before it diff --git a/src/cpu/opcodes/rla.zig b/src/cpu/opcodes/rla.zig index cc2e985..d39ed91 100644 --- a/src/cpu/opcodes/rla.zig +++ b/src/cpu/opcodes/rla.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "RLA"; @@ -39,6 +42,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// A ROL of the addressed byte, with the result also ANDed diff --git a/src/cpu/opcodes/rol.zig b/src/cpu/opcodes/rol.zig index dd62a65..0ff8c5d 100644 --- a/src/cpu/opcodes/rol.zig +++ b/src/cpu/opcodes/rol.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ROL"; @@ -29,9 +32,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 10.3 page 149 + /// Rotates the accumulator or the addressed byte left one bit + /// through carry. + /// + /// MCS6500 Programming Manual 10.3 page 149 + /// Carr, 6502 User's Manual page 269 pub fn ROL(cpu: *CPU) error{}!void { var address: ?u16 = null; const value = switch (comptime addressing_mode) { diff --git a/src/cpu/opcodes/ror.zig b/src/cpu/opcodes/ror.zig index 0da50d4..cce171f 100644 --- a/src/cpu/opcodes/ror.zig +++ b/src/cpu/opcodes/ror.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ROR"; @@ -29,9 +32,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 10.4 page 150 + /// Rotates the accumulator or the addressed byte right one bit + /// through carry. + /// + /// MCS6500 Programming Manual 10.4 page 150 + /// Carr, 6502 User's Manual page 269 pub fn ROR(cpu: *CPU) error{}!void { var address: ?u16 = null; const value = switch (comptime addressing_mode) { diff --git a/src/cpu/opcodes/rra.zig b/src/cpu/opcodes/rra.zig index 5b18556..8db9d0b 100644 --- a/src/cpu/opcodes/rra.zig +++ b/src/cpu/opcodes/rra.zig @@ -1,5 +1,8 @@ const add = @import("../add.zig").add; +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "RRA"; @@ -41,6 +44,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// A ROR of the addressed byte, with the result also added to diff --git a/src/cpu/opcodes/rti.zig b/src/cpu/opcodes/rti.zig index 7c200dc..8bb731e 100644 --- a/src/cpu/opcodes/rti.zig +++ b/src/cpu/opcodes/rti.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "RTI"; @@ -9,9 +12,17 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { + /// Returns from an interrupt, pulling the status register and + /// then the program counter, which unlike RTS's was pushed + /// ready to use. + /// + /// Carr, 6502 User's Manual page 270 pub fn RTI(cpu: *CPU) error{}!void { // the discarded operand fetch, then the discarded read // of the stack slot the pointer is sitting on before it diff --git a/src/cpu/opcodes/rts.zig b/src/cpu/opcodes/rts.zig index 0168f77..5e69818 100644 --- a/src/cpu/opcodes/rts.zig +++ b/src/cpu/opcodes/rts.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "RTS"; @@ -9,9 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { + /// Returns from a subroutine, pulling the address JSR pushed + /// and stepping one past it. + /// + /// Carr, 6502 User's Manual page 271 pub fn RTS(cpu: *CPU) error{}!void { // the discarded operand fetch, then the discarded read of // the stack slot the pointer is sitting on diff --git a/src/cpu/opcodes/sax.zig b/src/cpu/opcodes/sax.zig index 0eb69a4..cea5a86 100644 --- a/src/cpu/opcodes/sax.zig +++ b/src/cpu/opcodes/sax.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SAX"; @@ -24,6 +27,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// Stores the accumulator ANDed with X. Touches no flags, diff --git a/src/cpu/opcodes/sbc.zig b/src/cpu/opcodes/sbc.zig index c3ab1ca..03e5ed6 100644 --- a/src/cpu/opcodes/sbc.zig +++ b/src/cpu/opcodes/sbc.zig @@ -1,5 +1,8 @@ const add = @import("../add.zig").add; +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SBC"; @@ -52,8 +55,15 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (cpu: *CPU) error{}!void { const f = struct { + /// Subtracts memory and the borrow from the accumulator. + /// Honours decimal mode. + /// + /// Carr, 6502 User's Manual page 271 pub fn SBC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); diff --git a/src/cpu/opcodes/sbx.zig b/src/cpu/opcodes/sbx.zig index 4af404f..85fa572 100644 --- a/src/cpu/opcodes/sbx.zig +++ b/src/cpu/opcodes/sbx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SBX"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/sec.zig b/src/cpu/opcodes/sec.zig index 596eab5..25fcafb 100644 --- a/src/cpu/opcodes/sec.zig +++ b/src/cpu/opcodes/sec.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SEC"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.0.1 page 24 + /// Sets the carry flag. + /// + /// MCS6500 Programming Manual 3.0.1 page 24 + /// Carr, 6502 User's Manual page 272 pub fn SEC(cpu: *CPU) error{}!void { cpu.flags.set(.C); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/sed.zig b/src/cpu/opcodes/sed.zig index 965bec8..18d59b5 100644 --- a/src/cpu/opcodes/sed.zig +++ b/src/cpu/opcodes/sed.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SED"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.3.1 page 26 + /// Sets decimal mode. + /// + /// MCS6500 Programming Manual 3.3.1 page 26 + /// Carr, 6502 User's Manual page 273 pub fn SED(cpu: *CPU) error{}!void { cpu.flags.set(.D); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/sei.zig b/src/cpu/opcodes/sei.zig index 22197b0..9365bee 100644 --- a/src/cpu/opcodes/sei.zig +++ b/src/cpu/opcodes/sei.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SEI"; @@ -9,10 +12,16 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 3.2.1 page 26 + /// Sets the interrupt disable, masking interrupts. + /// + /// MCS6500 Programming Manual 3.2.1 page 26 + /// Carr, 6502 User's Manual page 273 pub fn SEI(cpu: *CPU) error{}!void { cpu.flags.set(.I); // the operand fetch every single byte instruction performs diff --git a/src/cpu/opcodes/sha.zig b/src/cpu/opcodes/sha.zig index 37ded46..b2ddcec 100644 --- a/src/cpu/opcodes/sha.zig +++ b/src/cpu/opcodes/sha.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SHA"; @@ -14,6 +17,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// Unstable. As SHY, but storing the accumulator ANDed with X. diff --git a/src/cpu/opcodes/shx.zig b/src/cpu/opcodes/shx.zig index a9afaab..6f73c54 100644 --- a/src/cpu/opcodes/shx.zig +++ b/src/cpu/opcodes/shx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SHX"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .@"Absolute, Y") @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/shy.zig b/src/cpu/opcodes/shy.zig index 3e4949a..86ebaa8 100644 --- a/src/cpu/opcodes/shy.zig +++ b/src/cpu/opcodes/shy.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SHY"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .@"Absolute, X") @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/slo.zig b/src/cpu/opcodes/slo.zig index 2f620a5..fa16496 100644 --- a/src/cpu/opcodes/slo.zig +++ b/src/cpu/opcodes/slo.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SLO"; @@ -39,6 +42,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// An ASL of the addressed byte, with the result also ORed diff --git a/src/cpu/opcodes/sre.zig b/src/cpu/opcodes/sre.zig index fa1bd21..403a1f7 100644 --- a/src/cpu/opcodes/sre.zig +++ b/src/cpu/opcodes/sre.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "SRE"; @@ -39,6 +42,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { /// An LSR of the addressed byte, with the result also EORed diff --git a/src/cpu/opcodes/sta.zig b/src/cpu/opcodes/sta.zig index 3496bc0..5c9ae8e 100644 --- a/src/cpu/opcodes/sta.zig +++ b/src/cpu/opcodes/sta.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "STA"; @@ -38,9 +41,16 @@ pub fn data(comptime CPU: type) type { .cycles = 5, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 2.1.2 page 5 + /// Stores the accumulator into memory. + /// + /// MCS6500 Programming Manual 2.1.2 page 5 + /// Carr, 6502 User's Manual page 273 pub fn STA(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); cpu.memory.write(u8, address, cpu.registers.get(.A)); diff --git a/src/cpu/opcodes/stx.zig b/src/cpu/opcodes/stx.zig index 635179f..feb6da8 100644 --- a/src/cpu/opcodes/stx.zig +++ b/src/cpu/opcodes/stx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "STX"; @@ -18,9 +21,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 7.2 page 97 + /// Stores the X register into memory. + /// + /// MCS6500 Programming Manual 7.2 page 97 + /// Carr, 6502 User's Manual page 274 pub fn STX(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); cpu.memory.write(u8, address, cpu.registers.get(.X)); diff --git a/src/cpu/opcodes/sty.zig b/src/cpu/opcodes/sty.zig index 26e3f35..0c76db4 100644 --- a/src/cpu/opcodes/sty.zig +++ b/src/cpu/opcodes/sty.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "STY"; @@ -18,9 +21,16 @@ pub fn data(comptime CPU: type) type { .cycles = 4, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { const f = struct { - /// 7.3 page 97 + /// Stores the Y register into memory. + /// + /// MCS6500 Programming Manual 7.3 page 97 + /// Carr, 6502 User's Manual page 274 pub fn STY(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); cpu.memory.write(u8, address, cpu.registers.get(.Y)); diff --git a/src/cpu/opcodes/tas.zig b/src/cpu/opcodes/tas.zig index 8e9ec84..1e9fa1d 100644 --- a/src/cpu/opcodes/tas.zig +++ b/src/cpu/opcodes/tas.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TAS"; @@ -9,6 +12,9 @@ pub fn data(comptime CPU: type) type { }, }; + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .@"Absolute, Y") @compileError("illegal addressing mode"); const f = struct { diff --git a/src/cpu/opcodes/tax.zig b/src/cpu/opcodes/tax.zig index d180b93..dd8a203 100644 --- a/src/cpu/opcodes/tax.zig +++ b/src/cpu/opcodes/tax.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TAX"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.11 page 100 + /// Copies the accumulator into the X register. + /// + /// MCS6500 Programming Manual 7.11 page 100 + /// Carr, 6502 User's Manual page 275 pub fn TAX(cpu: *CPU) error{}!void { const value = cpu.registers.get(.A); cpu.registers.set(.X, value); diff --git a/src/cpu/opcodes/tay.zig b/src/cpu/opcodes/tay.zig index 0d06ff6..d3621cc 100644 --- a/src/cpu/opcodes/tay.zig +++ b/src/cpu/opcodes/tay.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TAY"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.13 page 101 + /// Copies the accumulator into the Y register. + /// + /// MCS6500 Programming Manual 7.13 page 101 + /// Carr, 6502 User's Manual has no entry for it pub fn TAY(cpu: *CPU) error{}!void { const value = cpu.registers.get(.A); cpu.registers.set(.Y, value); diff --git a/src/cpu/opcodes/tsx.zig b/src/cpu/opcodes/tsx.zig index 3540905..7e41c63 100644 --- a/src/cpu/opcodes/tsx.zig +++ b/src/cpu/opcodes/tsx.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TSX"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 8.9 page 122 + /// Copies the stack pointer into the X register. + /// + /// MCS6500 Programming Manual 8.9 page 122 + /// Carr, 6502 User's Manual page 276 pub fn TSX(cpu: *CPU) error{}!void { const value = cpu.stack.pointer; cpu.registers.set(.X, value); diff --git a/src/cpu/opcodes/txa.zig b/src/cpu/opcodes/txa.zig index ac8276f..8b31801 100644 --- a/src/cpu/opcodes/txa.zig +++ b/src/cpu/opcodes/txa.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TXA"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { // 7.12 page 100 + /// Copies the X register into the accumulator. + /// + /// Carr, 6502 User's Manual page 276 pub fn TXA(cpu: *CPU) error{}!void { if (addressing_mode != .Implied) return error.IllegalAddressingMode; const value = cpu.registers.get(.X); diff --git a/src/cpu/opcodes/txs.zig b/src/cpu/opcodes/txs.zig index f42c7b3..96edb3f 100644 --- a/src/cpu/opcodes/txs.zig +++ b/src/cpu/opcodes/txs.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TXS"; @@ -8,10 +11,18 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 8.8 page 120 + /// Copies the X register into the stack pointer. Alone among + /// the transfers it touches no flags. + /// + /// MCS6500 Programming Manual 8.8 page 120 + /// Carr, 6502 User's Manual page 277 pub fn TXS(cpu: *CPU) error{}!void { if (addressing_mode != .Implied) return error.IllegalAddressingMode; cpu.stack.pointer = cpu.registers.get(.X); diff --git a/src/cpu/opcodes/tya.zig b/src/cpu/opcodes/tya.zig index 7271442..04e8059 100644 --- a/src/cpu/opcodes/tya.zig +++ b/src/cpu/opcodes/tya.zig @@ -1,3 +1,6 @@ +/// Describes one instruction: its mnemonic, and every opcode that selects +/// it together with that opcode's addressing mode and cycle count. Opcodes +/// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "TYA"; @@ -8,10 +11,17 @@ pub fn data(comptime CPU: type) type { .cycles = 2, }, }; + + /// The implementation for a single addressing mode. Resolved at + /// comptime so the dispatch table holds a plain function pointer with + /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { - /// 7.14 page 101 + /// Copies the Y register into the accumulator. + /// + /// MCS6500 Programming Manual 7.14 page 101 + /// Carr, 6502 User's Manual page 275 pub fn TYA(cpu: *CPU) error{}!void { const value = cpu.registers.get(.Y); cpu.registers.set(.A, value); diff --git a/src/cpu/program_counter.zig b/src/cpu/program_counter.zig index 9b50abd..3c73fa4 100644 --- a/src/cpu/program_counter.zig +++ b/src/cpu/program_counter.zig @@ -1,19 +1,25 @@ const std = @import("std"); +/// The program counter, and the operand fetching that walks it forward. +/// 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 ProgramCounter(comptime CPU: type) type { return struct { const Self = @This(); value: u16 = 0, + /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("program_counter", self)); } + /// Sets the counter to zero, for a CPU that is not running yet. pub fn init(self: *Self) void { self.value = 0; } + /// Loads the counter from the vector at $fffe. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self.value = cpu.memory.read(u16, 0xfffe); @@ -27,6 +33,8 @@ pub fn ProgramCounter(comptime CPU: type) type { _ = cpu.memory.read(u8, self.value); } + /// Fetches an operand and steps past it. A word is read low byte first, + /// the order operands are stored in. pub fn read(self: *Self, comptime T: type) T { const cpu = self.getCPU(); const info = @typeInfo(T); @@ -52,16 +60,23 @@ pub fn ProgramCounter(comptime CPU: type) type { // Calculate the address that an immediate argument should be loaded // from. This does not actually return the data, just the address that // holds the data. + /// The address an immediate operand lives at, which is simply the + /// counter. Steps past it without reading, because the instruction does + /// the read itself. pub fn immediate(self: *Self) u16 { const result = self.value; self.value +%= 1; return result; } + /// Moves the counter. The operand fetch has already been paid for, so + /// this costs nothing on its own. pub fn jump(self: *Self, value: u16) void { self.value = value; } + /// Takes a branch, spending the cycle the chip uses to add the offset + /// and a second one when the target turns out to be in another page. pub fn branch(self: *Self, value: u16) void { const cpu = self.getCPU(); // the discarded read taken while the offset is added to the low @@ -74,6 +89,8 @@ pub fn ProgramCounter(comptime CPU: type) type { self.value = value; } + /// Where a branch would land, counted from the instruction after it. + /// Does not take the branch and costs nothing. pub fn calculateBranchLocation(self: *Self, offset: i8) u16 { return @intCast((@as(i17, @intCast(self.value)) +% offset) & 0xffff); } diff --git a/src/cpu/registers.zig b/src/cpu/registers.zig index 1dcd0e6..07788ca 100644 --- a/src/cpu/registers.zig +++ b/src/cpu/registers.zig @@ -1,5 +1,9 @@ const std = @import("std"); +/// The accumulator and the two index registers, each carrying the cycle it was +/// last written on. 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 Registers(comptime CPU: type) type { return struct { const Self = @This(); @@ -13,15 +17,20 @@ pub fn Registers(comptime CPU: type) type { _values: std.EnumArray(Name, u8), _changed: std.EnumArray(Name, u64), + /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("registers", self)); } + /// Clears all three registers without touching the flags, for a CPU + /// that is not running yet. pub fn init(self: *Self) void { self._values = .initFill(0); self._changed = .initFill(0); } + /// Clears all three registers and reports the new accumulator through + /// the N and Z flags, as a reset does. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self._values = .initFill(0); @@ -29,18 +38,34 @@ pub fn Registers(comptime CPU: type) type { cpu.flags.updateNZ(self._values.get(.A)); } + /// Writes a register and records when it changed, which the debugger + /// uses to highlight recent writes. pub fn set(self: *Self, comptime register: Name, value: u8) void { const cpu = self.getCPU(); self._changed.set(register, cpu.clock.read()); self._values.set(register, value); } + /// Reads a register. pub fn get(self: *Self, comptime register: Name) u8 { return self._values.get(register); } + /// How many cycles ago the register was last written. pub fn changed(self: *Self, comptime register: Name) u64 { - return self.cpu.timer.read() - self._changed.get(register); + const cpu = self.getCPU(); + return cpu.clock.read() - self._changed.get(register); } }; } + +test "changed" { + const clock = @import("../clock.zig"); + const CPU = @import("../cpu.zig").CPU(clock.FakeClock); + var cpu: CPU = undefined; + try cpu.init(null, null); + defer cpu.deinit(); + cpu.registers.set(.A, 0x69); + _ = cpu.memory.read(u8, 0x0000); + try std.testing.expectEqual(@as(u64, 1), cpu.registers.changed(.A)); +} diff --git a/src/cpu/stack.zig b/src/cpu/stack.zig index daa01cc..672ccfa 100644 --- a/src/cpu/stack.zig +++ b/src/cpu/stack.zig @@ -1,5 +1,9 @@ const std = @import("std"); +/// The stack pointer, and the pushes and pulls that move it. The stack lives in +/// page one and wraps within it rather than overflowing. 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 Stack(comptime CPU: type) type { return struct { const Self = @This(); @@ -9,14 +13,19 @@ pub fn Stack(comptime CPU: type) type { underflows: usize, overflows: usize, + /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("stack", self)); } + /// Puts the pointer at the top of the stack page and clears the wrap + /// counters. pub fn init(self: *Self) void { self.reset(); } + /// As init. The 6502 does not clear the stack on reset either; it only + /// moves the pointer. pub fn reset(self: *Self) void { self.pointer = 0xff; self.underflows = 0; @@ -30,6 +39,10 @@ pub fn Stack(comptime CPU: type) type { _ = cpu.memory.read(u8, page + self.pointer); } + /// Moves the pointer up and reads. A word comes back low byte first, + /// the order the processor pushed it in. Running off the end wraps + /// within the stack page and is counted rather than reported, because + /// that is what the hardware does. pub fn pop(self: *Self, comptime T: type) T { const cpu = self.getCPU(); const info = @typeInfo(T); @@ -53,6 +66,8 @@ pub fn Stack(comptime CPU: type) type { } } + /// Writes at the pointer and moves it down. A word goes out high byte + /// first. Running off the end wraps, as in pop. pub fn push(self: *Self, comptime T: type, value: T) void { const cpu = self.getCPU(); const info = @typeInfo(T); diff --git a/src/decode.zig b/src/decode.zig index 089f99a..9f32aee 100644 --- a/src/decode.zig +++ b/src/decode.zig @@ -2,6 +2,8 @@ const std = @import("std"); const CPU = @import("cpu.zig"); const table = @import("table.zig").table; +/// Formats the instruction at an address as assembly. Not currently referenced +/// by anything, and predates the present opcode table. pub fn format(buf: []u8, memory: []const u8, address: u16) ![]const u8 { const opcode = memory[address]; const data = table[opcode]; diff --git a/src/graphics/apple2/textmode.zig b/src/graphics/apple2/textmode.zig index 092f9a5..7c9c7f1 100644 --- a/src/graphics/apple2/textmode.zig +++ b/src/graphics/apple2/textmode.zig @@ -1,16 +1,19 @@ const std = @import("std"); const z2d = @import("z2d"); +/// Renders the Apple II 40 column text screen. Very much a work in progress. pub fn TextMode() type { return struct { const Self = @This(); font: z2d.Font, + /// Loads the bitmap font the glyphs are drawn from. pub fn init(self: *Self) !void { self.font = try .loadBuffer(@embedFile("pixel_code")); } + /// Draws the screen to a surface and writes it out as a PNG. pub fn generate(self: *Self, io: std.Io, alloc: std.mem.Allocator) !void { var surface: z2d.Surface = try .init( .image_surface_rgba, diff --git a/src/main.zig b/src/main.zig index 364a223..afea99b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -41,6 +41,8 @@ const App = struct { cpu: CPU, memory_page: u8 = 0, + /// Brings up the terminal and the processor in place, and loads the Apple + /// II+ ROM so there is something to step through. pub fn init( self: *App, io: std.Io, @@ -61,12 +63,16 @@ const App = struct { self.mouse = null; } + /// Puts the terminal back the way it was found. pub fn deinit(self: *App) void { self.cpu.deinit(); self.vx.deinit(self.alloc, self.writer); self.tty.deinit(); } + /// The event loop: drain whatever input has arrived, redraw, repeat. Each + /// pass gets a fresh arena, so drawing can allocate freely without + /// bookkeeping. pub fn run(self: *App) !void { var loop: vaxis.Loop(Event) = .init(self.io, &self.tty, &self.vx); try loop.installResizeHandler(); @@ -91,6 +97,8 @@ const App = struct { } } + /// Applies one input event. Space steps a single instruction, page up and + /// down move the memory view, and ctrl-c quits. pub fn update(self: *App, event: Event, alloc: std.mem.Allocator) !void { _ = alloc; switch (event) { @@ -124,6 +132,8 @@ const App = struct { } } + /// Paints the whole screen: the registers and flags, the disassembly around + /// the program counter, and a page of memory. pub fn draw(self: *App, alloc: std.mem.Allocator) !void { const non_printable_characters: vaxis.Style = .{ .fg = .{ @@ -612,6 +622,7 @@ const App = struct { } }; +/// Runs the debugger until the user quits. pub fn main(init: std.process.Init) !void { // Initialize our application var app: App = undefined; diff --git a/src/root.zig b/src/root.zig index 11fd232..8301c2f 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,6 +1,13 @@ +//! A cycle accurate 6502, and the pieces an emulator needs around it. +//! +//! CPU is parameterised over a clock so that timing can be counted or +//! driven; everything else hangs off it. + pub const clock = @import("clock.zig"); pub const CPU = @import("cpu.zig").CPU; +/// Which processor's behaviour to follow where the families disagree, +/// which for now means the decimal mode correction in add.zig. pub const Processor = enum { /// 6502 @"6502", diff --git a/src/single_step_tests.zig b/src/single_step_tests.zig index 1e430ce..85823bc 100644 --- a/src/single_step_tests.zig +++ b/src/single_step_tests.zig @@ -37,6 +37,15 @@ const Test = struct { var debug_allocator: std.heap.DebugAllocator(.{}) = .init; +/// Runs every opcode against the SingleStepTests corpus in 65x02/6502/v1. +/// +/// Each case gives a starting state, the state the hardware ended in, and every +/// access it made on the way. All three are checked: the architectural state, +/// the number of cycles, and the address, value and direction of each access. +/// The last is the sharpest of them, because a discarded read leaves no trace +/// in the registers but is exactly what a memory mapped device reacts to. +/// +/// Build with -Dssf= to run only some opcodes. pub fn main(init: std.process.Init) !void { const io = init.io; const alloc, const is_debug = allocator: { diff --git a/src/single_step_tests/Cycle.zig b/src/single_step_tests/Cycle.zig index 32dcad7..98d2273 100644 --- a/src/single_step_tests/Cycle.zig +++ b/src/single_step_tests/Cycle.zig @@ -9,11 +9,16 @@ address: u16, value: u8, direction: Direction, +/// Parses one access on the bus, as an [address, value, direction] triple. The +/// corpus stores these as arrays rather than objects, so the default object +/// based parser cannot be used. pub fn jsonParse(alloc: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) std.json.ParseError(@TypeOf(source.*))!@This() { const value = try std.json.innerParse(std.json.Value, alloc, source, options); return @This().jsonParseFromValue(alloc, value, options); } +/// Parses one access on the bus, as an [address, value, direction] triple from +/// an already parsed JSON value. pub fn jsonParseFromValue(_: std.mem.Allocator, json: std.json.Value, _: std.json.ParseOptions) std.json.ParseFromValueError!@This() { var address: u16 = undefined; var value: u8 = undefined; diff --git a/src/single_step_tests/RAM.zig b/src/single_step_tests/RAM.zig index 7a08a4b..17bcc53 100644 --- a/src/single_step_tests/RAM.zig +++ b/src/single_step_tests/RAM.zig @@ -3,11 +3,16 @@ const std = @import("std"); address: u16, value: u8, +/// Parses a memory location, as an [address, value] pair. The corpus stores +/// these as arrays rather than objects, so the default object based parser +/// cannot be used. pub fn jsonParse(alloc: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) std.json.ParseError(@TypeOf(source.*))!@This() { const value = try std.json.innerParse(std.json.Value, alloc, source, options); return @This().jsonParseFromValue(alloc, value, options); } +/// Parses a memory location, as an [address, value] pair from an already parsed +/// JSON value. pub fn jsonParseFromValue(_: std.mem.Allocator, json: std.json.Value, _: std.json.ParseOptions) std.json.ParseFromValueError!@This() { var address: u16 = undefined; var value: u8 = undefined; -- knotmirror 0.0.1