From 820fec36d23dd10a8dfffe548ccef6985a2bf8e4 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 1 Sep 2026 20:15:13 -0500 Subject: [PATCH] give the 65816 its stack and its register transfers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 228 encodings against 195: the seventeen register-to-register moves and register increments, and the sixteen that push and pull. Two things the recordings settled. The stack is two stacks, and which one an instruction gets depends on which machine it came from. Emulating, the pointer is held inside page one, so a pull from $01ff reads $000100 — that is the 6502's stack and a program written for that machine expects it. The instructions this processor added were never promised any such thing and step straight out of the page: PLD from $01ff reads $000200, and PHD from $0100 puts its second byte at $0000ff. So the confinement is per instruction rather than per access, and it is applied at the start of each instruction and when the register is read, never in between — which is how the pointer can leave page one during a push and still read back inside it afterwards. XBA takes two cycles of its own rather than one, and PER's internal cycle holds the last operand byte's address, which is the same rule REP, SEP and the direct page penalty all follow. The transfers are where the widths get particular: which flag governs is the destination's and not the source's, so TXA is as wide as M says and TAX as wide as X says, and the four that move whole sixteen-bit registers — TCD, TDC, TCS, TSC — ignore both because what they move was never eight bits wide. TXS and TCS set no flags; XBA sets its two from the low half after the swap, eight bits of it whatever M says. 4,560,000 cases, every register, every cycle count and every cycle of every bus trace. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01FawbWzEu5hoLLZSMCehUGP --- README.md | 37 +++++++++++------- src/cpu16.zig | 4 ++ src/cpu16/opcodes.zig | 43 ++++++++++++++++++--- src/cpu16/opcodes/dex.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/dey.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/inx.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/iny.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/pea.zig | 37 ++++++++++++++++++ src/cpu16/opcodes/pei.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/per.zig | 42 ++++++++++++++++++++ src/cpu16/opcodes/pha.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/phb.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/phd.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/phk.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/php.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/phx.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/phy.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/pla.zig | 50 ++++++++++++++++++++++++ src/cpu16/opcodes/plb.zig | 41 ++++++++++++++++++++ src/cpu16/opcodes/pld.zig | 41 ++++++++++++++++++++ src/cpu16/opcodes/plp.zig | 39 +++++++++++++++++++ src/cpu16/opcodes/plx.zig | 50 ++++++++++++++++++++++++ src/cpu16/opcodes/ply.zig | 50 ++++++++++++++++++++++++ src/cpu16/opcodes/tax.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/tay.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/tcd.zig | 40 ++++++++++++++++++++ src/cpu16/opcodes/tcs.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/tdc.zig | 40 ++++++++++++++++++++ src/cpu16/opcodes/tsc.zig | 40 ++++++++++++++++++++ src/cpu16/opcodes/tsx.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/txa.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/txs.zig | 38 +++++++++++++++++++ src/cpu16/opcodes/txy.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/tya.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/tyx.zig | 47 +++++++++++++++++++++++ src/cpu16/opcodes/xba.zig | 44 +++++++++++++++++++++ src/cpu16/stack.zig | 80 +++++++++++++++++++++++++++------------ 37 files changed, 1560 insertions(+), 42 deletions(-) create mode 100644 src/cpu16/opcodes/dex.zig create mode 100644 src/cpu16/opcodes/dey.zig create mode 100644 src/cpu16/opcodes/inx.zig create mode 100644 src/cpu16/opcodes/iny.zig create mode 100644 src/cpu16/opcodes/pea.zig create mode 100644 src/cpu16/opcodes/pei.zig create mode 100644 src/cpu16/opcodes/per.zig create mode 100644 src/cpu16/opcodes/pha.zig create mode 100644 src/cpu16/opcodes/phb.zig create mode 100644 src/cpu16/opcodes/phd.zig create mode 100644 src/cpu16/opcodes/phk.zig create mode 100644 src/cpu16/opcodes/php.zig create mode 100644 src/cpu16/opcodes/phx.zig create mode 100644 src/cpu16/opcodes/phy.zig create mode 100644 src/cpu16/opcodes/pla.zig create mode 100644 src/cpu16/opcodes/plb.zig create mode 100644 src/cpu16/opcodes/pld.zig create mode 100644 src/cpu16/opcodes/plp.zig create mode 100644 src/cpu16/opcodes/plx.zig create mode 100644 src/cpu16/opcodes/ply.zig create mode 100644 src/cpu16/opcodes/tax.zig create mode 100644 src/cpu16/opcodes/tay.zig create mode 100644 src/cpu16/opcodes/tcd.zig create mode 100644 src/cpu16/opcodes/tcs.zig create mode 100644 src/cpu16/opcodes/tdc.zig create mode 100644 src/cpu16/opcodes/tsc.zig create mode 100644 src/cpu16/opcodes/tsx.zig create mode 100644 src/cpu16/opcodes/txa.zig create mode 100644 src/cpu16/opcodes/txs.zig create mode 100644 src/cpu16/opcodes/txy.zig create mode 100644 src/cpu16/opcodes/tya.zig create mode 100644 src/cpu16/opcodes/tyx.zig create mode 100644 src/cpu16/opcodes/xba.zig diff --git a/README.md b/README.md index a893431..06d6c6f 100644 --- a/README.md +++ b/README.md @@ -751,11 +751,12 @@ passes. It is unfinished but no longer a skeleton. The registers, flags, stack, program counter and the table builder are there, the addressing modes are decoded for every mode anything but the jumps uses — twenty-two of the -twenty-five — and 195 of the 256 encodings are implemented: the loads and -stores, the logic and the comparisons, the arithmetic, the shifts and rotates -and the rest of the read-modify-writes, the flag instructions, and the three -that change what kind of machine this is. What is left is the stack, the jumps -and calls, the block moves and the interrupt sequence. +twenty-five — and 228 of the 256 encodings are implemented: the loads and +stores, the logic and the comparisons, the arithmetic, the read-modify-writes, +the register transfers, the stack, the flag instructions, and the three that +change what kind of machine this is. The twenty-eight left are the jumps and +calls, the branches, the block moves, and the interrupt sequence with `BRK` and +`COP`. The decoder is where this processor stops resembling the older one. A direct page that is not on a page boundary costs a cycle every time it is used. An @@ -769,10 +770,10 @@ cycle by cycle: ``` $ zig build corpus16 -5120000 tests: 3900000 succeeded, 0 failed, 0 skipped, 1220000 not implemented -instructions: 195 of 256 encodings implemented, 3900000 cases run, 1950000 of them native +5120000 tests: 4560000 succeeded, 0 failed, 0 skipped, 560000 not implemented +instructions: 228 of 256 encodings implemented, 4560000 cases run, 2280000 of them native bus traces: every cycle matches -cycle counts: all 3900000 cases match +cycle counts: all 4560000 cases match ``` Its corpus is [a separate set][sst816], twenty thousand cases per opcode rather @@ -860,6 +861,17 @@ three cycles of a read-modify-write so that nothing else on the bus can come between them. Every one of the thirty-four read-modify-write encodings failed on that one character until it was modelled. +**The stack is two stacks, and which one you get depends on the +instruction.** While emulating, the pointer is held inside page one, so pulling +from `$01ff` reads `$000100` — that is the 6502's stack and a program written +for that machine expects it. But the instructions this processor *added* were +never promised any such thing, and step straight out of the page: `PLD` from +`$01ff` reads `$000200`, and pushing two bytes from `$0100` puts the second at +`$0000ff`. So the confinement is per instruction rather than per access, and it +is applied at the start of each one and when the register is read, never in +between. The register still reads back inside page one afterwards, which is how +both facts can be true at once. + **And the 6502's pointer wrap is gone.** The older machine wrapped a zero page pointer inside the zero page, so a pointer at `$ff` took its high byte from `$00`, and the natural guess is that this one keeps that while emulating. It @@ -1081,13 +1093,12 @@ machine around them is not. should have, which is the cycle it costs. - **The 65816 is a second core, and unfinished.** It is not one of the processors above and cannot be: `src/cpu16/` is its own core with its own - 24-bit memory models, for the reasons given under The 65816. 195 of its 256 + 24-bit memory models, for the reasons given under The 65816. 228 of its 256 encodings are implemented and exact against its own recordings — the loads and stores, the logic, the comparisons, the arithmetic including decimal at - both widths, the read-modify-writes, and the instructions that change what - kind of machine it is. The stack, the jumps and calls, the block moves and - the interrupt sequence are not written, and no machine here can hold one - yet. + both widths, the read-modify-writes, the register transfers and the stack. + The jumps and calls, the branches, the block moves and the interrupt + sequence are not written, and no machine here can hold one yet. ## Layout diff --git a/src/cpu16.zig b/src/cpu16.zig index 79e79a5..00886e7 100644 --- a/src/cpu16.zig +++ b/src/cpu16.zig @@ -268,6 +268,10 @@ pub fn Chip( self.clock.sleep(1); return; } + // Emulating, the stack pointer is pulled back into page one + // before each instruction rather than after every access: what a + // 16-bit push does to it in between is the pointer's own business. + self.stack.normalize(); const opcode = self.program_counter.opcode(); const entry = switch (comptime dispatch) { .branch => &Opcodes.table[opcode], diff --git a/src/cpu16/opcodes.zig b/src/cpu16/opcodes.zig index b2cd82e..e9100a8 100644 --- a/src/cpu16/opcodes.zig +++ b/src/cpu16/opcodes.zig @@ -25,40 +25,73 @@ const std = @import("std"); pub fn Opcodes(comptime CPU: type) type { return struct { const instructions = .{ + @import("opcodes/@and.zig"), @import("opcodes/adc.zig"), @import("opcodes/asl.zig"), - @import("opcodes/@and.zig"), @import("opcodes/bit.zig"), @import("opcodes/clc.zig"), @import("opcodes/cld.zig"), @import("opcodes/cli.zig"), + @import("opcodes/clv.zig"), @import("opcodes/cmp.zig"), @import("opcodes/cpx.zig"), @import("opcodes/cpy.zig"), @import("opcodes/dec.zig"), - @import("opcodes/clv.zig"), + @import("opcodes/dex.zig"), + @import("opcodes/dey.zig"), @import("opcodes/eor.zig"), @import("opcodes/inc.zig"), + @import("opcodes/inx.zig"), + @import("opcodes/iny.zig"), @import("opcodes/lda.zig"), @import("opcodes/ldx.zig"), @import("opcodes/ldy.zig"), @import("opcodes/lsr.zig"), @import("opcodes/nop.zig"), @import("opcodes/ora.zig"), + @import("opcodes/pea.zig"), + @import("opcodes/pei.zig"), + @import("opcodes/per.zig"), + @import("opcodes/pha.zig"), + @import("opcodes/phb.zig"), + @import("opcodes/phd.zig"), + @import("opcodes/phk.zig"), + @import("opcodes/php.zig"), + @import("opcodes/phx.zig"), + @import("opcodes/phy.zig"), + @import("opcodes/pla.zig"), + @import("opcodes/plb.zig"), + @import("opcodes/pld.zig"), + @import("opcodes/plp.zig"), + @import("opcodes/plx.zig"), + @import("opcodes/ply.zig"), @import("opcodes/rep.zig"), - @import("opcodes/sec.zig"), - @import("opcodes/sed.zig"), - @import("opcodes/sei.zig"), @import("opcodes/rol.zig"), @import("opcodes/ror.zig"), @import("opcodes/sbc.zig"), + @import("opcodes/sec.zig"), + @import("opcodes/sed.zig"), + @import("opcodes/sei.zig"), @import("opcodes/sep.zig"), @import("opcodes/sta.zig"), @import("opcodes/stx.zig"), @import("opcodes/sty.zig"), @import("opcodes/stz.zig"), + @import("opcodes/tax.zig"), + @import("opcodes/tay.zig"), + @import("opcodes/tcd.zig"), + @import("opcodes/tcs.zig"), + @import("opcodes/tdc.zig"), @import("opcodes/trb.zig"), @import("opcodes/tsb.zig"), + @import("opcodes/tsc.zig"), + @import("opcodes/tsx.zig"), + @import("opcodes/txa.zig"), + @import("opcodes/txs.zig"), + @import("opcodes/txy.zig"), + @import("opcodes/tya.zig"), + @import("opcodes/tyx.zig"), + @import("opcodes/xba.zig"), @import("opcodes/xce.zig"), }; diff --git a/src/cpu16/opcodes/dex.zig b/src/cpu16/opcodes/dex.zig new file mode 100644 index 0000000..92341ca --- /dev/null +++ b/src/cpu16/opcodes/dex.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xca, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Takes one off index register X. + pub fn DEX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.X, T) -% 1; + cpu.registers.setNarrow(.X, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.DEX; + } + }; +} diff --git a/src/cpu16/opcodes/dey.zig b/src/cpu16/opcodes/dey.zig new file mode 100644 index 0000000..e3062e3 --- /dev/null +++ b/src/cpu16/opcodes/dey.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x88, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Takes one off index register Y. + pub fn DEY(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.Y, T) -% 1; + cpu.registers.setNarrow(.Y, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.DEY; + } + }; +} diff --git a/src/cpu16/opcodes/inx.zig b/src/cpu16/opcodes/inx.zig new file mode 100644 index 0000000..f803492 --- /dev/null +++ b/src/cpu16/opcodes/inx.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xe8, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Adds one to index register X. + pub fn INX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.X, T) +% 1; + cpu.registers.setNarrow(.X, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.INX; + } + }; +} diff --git a/src/cpu16/opcodes/iny.zig b/src/cpu16/opcodes/iny.zig new file mode 100644 index 0000000..24f2f88 --- /dev/null +++ b/src/cpu16/opcodes/iny.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xc8, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Adds one to index register Y. + pub fn INY(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.Y, T) +% 1; + cpu.registers.setNarrow(.Y, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.INY; + } + }; +} diff --git a/src/cpu16/opcodes/pea.zig b/src/cpu16/opcodes/pea.zig new file mode 100644 index 0000000..12284ad --- /dev/null +++ b/src/cpu16/opcodes/pea.zig @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PEA"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xf4, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes the two bytes after the opcode. Not an address it reads from: the operand itself is what goes on the stack, which is how a constant is put there for a subroutine to find. + pub fn PEA(cpu: *CPU) !void { + cpu.stack.push(u16, .unconfined, cpu.program_counter.read(u16)); + } + }; + return f.PEA; + } + }; +} diff --git a/src/cpu16/opcodes/pei.zig b/src/cpu16/opcodes/pei.zig new file mode 100644 index 0000000..7434b72 --- /dev/null +++ b/src/cpu16/opcodes/pei.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PEI"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xd4, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes the word the direct page operand points at, which is how a pointer already in the direct page is handed on. + pub fn PEI(cpu: *CPU) !void { + const address = cpu.addressing_modes.decode(.@"Direct Page", .read); + cpu.stack.push(u16, .unconfined, cpu.addressing_modes.readData(.@"Direct Page", u16, address)); + } + }; + return f.PEI; + } + }; +} diff --git a/src/cpu16/opcodes/per.zig b/src/cpu16/opcodes/per.zig new file mode 100644 index 0000000..5f241cd --- /dev/null +++ b/src/cpu16/opcodes/per.zig @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PER"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x62, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes an address worked out from where the instruction is: the signed word after the opcode, added to what the program counter holds afterwards. Position-independent code is what this is for. + pub fn PER(cpu: *CPU) !void { + const at = cpu.program_counter.address(); + const displacement = cpu.program_counter.read(u16); + // The cycle spent adding keeps the last operand byte's + // address on the bus, as every other one here does. + cpu.memory.internal(at +% 1); + cpu.stack.push(u16, .unconfined, cpu.program_counter.value +% displacement); + } + }; + return f.PER; + } + }; +} diff --git a/src/cpu16/opcodes/pha.zig b/src/cpu16/opcodes/pha.zig new file mode 100644 index 0000000..46a4964 --- /dev/null +++ b/src/cpu16/opcodes/pha.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x48, + .addressing_mode = .Stack, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + const f = struct { + /// Pushes the accumulator. + pub fn PHA(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.m) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + // The cycle before the push is the chip's own; the + // push itself is one cycle a byte, high half first. + cpu.program_counter.internal(); + cpu.stack.push(T, .page_one, cpu.registers.getNarrow(.A, T)); + } + }; + return f.PHA; + } + }; +} diff --git a/src/cpu16/opcodes/phb.zig b/src/cpu16/opcodes/phb.zig new file mode 100644 index 0000000..8348138 --- /dev/null +++ b/src/cpu16/opcodes/phb.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PHB"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x8b, + .addressing_mode = .Stack, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes the data bank register. + pub fn PHB(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.stack.push(u8, .unconfined, cpu.registers.data_bank); + } + }; + return f.PHB; + } + }; +} diff --git a/src/cpu16/opcodes/phd.zig b/src/cpu16/opcodes/phd.zig new file mode 100644 index 0000000..47bce25 --- /dev/null +++ b/src/cpu16/opcodes/phd.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PHD"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x0b, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes the direct page register, both halves of it. + pub fn PHD(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.stack.push(u16, .unconfined, cpu.registers.direct); + } + }; + return f.PHD; + } + }; +} diff --git a/src/cpu16/opcodes/phk.zig b/src/cpu16/opcodes/phk.zig new file mode 100644 index 0000000..f093667 --- /dev/null +++ b/src/cpu16/opcodes/phk.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PHK"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x4b, + .addressing_mode = .Stack, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes the program bank register, which is how a program finds out which bank it is running in. + pub fn PHK(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.stack.push(u8, .unconfined, cpu.registers.program_bank); + } + }; + return f.PHK; + } + }; +} diff --git a/src/cpu16/opcodes/php.zig b/src/cpu16/opcodes/php.zig new file mode 100644 index 0000000..1460ab8 --- /dev/null +++ b/src/cpu16/opcodes/php.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x08, + .addressing_mode = .Stack, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pushes the status register. + pub fn PHP(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.stack.push(u8, .page_one, cpu.flags.dump()); + } + }; + return f.PHP; + } + }; +} diff --git a/src/cpu16/opcodes/phx.zig b/src/cpu16/opcodes/phx.zig new file mode 100644 index 0000000..d958a59 --- /dev/null +++ b/src/cpu16/opcodes/phx.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PHX"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xda, + .addressing_mode = .Stack, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + const f = struct { + /// Pushes index register X. + pub fn PHX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + // The cycle before the push is the chip's own; the + // push itself is one cycle a byte, high half first. + cpu.program_counter.internal(); + cpu.stack.push(T, .page_one, cpu.registers.getNarrow(.X, T)); + } + }; + return f.PHX; + } + }; +} diff --git a/src/cpu16/opcodes/phy.zig b/src/cpu16/opcodes/phy.zig new file mode 100644 index 0000000..15599af --- /dev/null +++ b/src/cpu16/opcodes/phy.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PHY"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x5a, + .addressing_mode = .Stack, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + const f = struct { + /// Pushes index register Y. + pub fn PHY(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + // The cycle before the push is the chip's own; the + // push itself is one cycle a byte, high half first. + cpu.program_counter.internal(); + cpu.stack.push(T, .page_one, cpu.registers.getNarrow(.Y, T)); + } + }; + return f.PHY; + } + }; +} diff --git a/src/cpu16/opcodes/pla.zig b/src/cpu16/opcodes/pla.zig new file mode 100644 index 0000000..3082ad3 --- /dev/null +++ b/src/cpu16/opcodes/pla.zig @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x68, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + const f = struct { + /// Pulls the accumulator. + pub fn PLA(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.m) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + // Two cycles of the chip's own before anything is + // read: one after the opcode, one while the pointer moves. + cpu.program_counter.internal(); + cpu.program_counter.internal(); + const value = cpu.stack.pop(T, .page_one); + cpu.registers.setNarrow(.A, T, value); + cpu.flags.updateNZ(T, value); + } + }; + return f.PLA; + } + }; +} diff --git a/src/cpu16/opcodes/plb.zig b/src/cpu16/opcodes/plb.zig new file mode 100644 index 0000000..76c3a71 --- /dev/null +++ b/src/cpu16/opcodes/plb.zig @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PLB"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xab, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pulls the data bank register. + pub fn PLB(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.program_counter.internal(); + const value = cpu.stack.pop(u8, .unconfined); + cpu.registers.data_bank = value; + cpu.flags.updateNZ(u8, value); + } + }; + return f.PLB; + } + }; +} diff --git a/src/cpu16/opcodes/pld.zig b/src/cpu16/opcodes/pld.zig new file mode 100644 index 0000000..4fecbac --- /dev/null +++ b/src/cpu16/opcodes/pld.zig @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PLD"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x2b, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pulls the direct page register, both halves of it. + pub fn PLD(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.program_counter.internal(); + const value = cpu.stack.pop(u16, .unconfined); + cpu.registers.direct = value; + cpu.flags.updateNZ(u16, value); + } + }; + return f.PLD; + } + }; +} diff --git a/src/cpu16/opcodes/plp.zig b/src/cpu16/opcodes/plp.zig new file mode 100644 index 0000000..837164f --- /dev/null +++ b/src/cpu16/opcodes/plp.zig @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x28, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Pulls the status register. Emulating, the width flags come back set whatever was pushed, because there they are not writable. + pub fn PLP(cpu: *CPU) !void { + cpu.program_counter.internal(); + cpu.program_counter.internal(); + cpu.flags.load(cpu.stack.pop(u8, .page_one)); + } + }; + return f.PLP; + } + }; +} diff --git a/src/cpu16/opcodes/plx.zig b/src/cpu16/opcodes/plx.zig new file mode 100644 index 0000000..b2aa5d6 --- /dev/null +++ b/src/cpu16/opcodes/plx.zig @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PLX"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xfa, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + const f = struct { + /// Pulls index register X. + pub fn PLX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + // Two cycles of the chip's own before anything is + // read: one after the opcode, one while the pointer moves. + cpu.program_counter.internal(); + cpu.program_counter.internal(); + const value = cpu.stack.pop(T, .page_one); + cpu.registers.setNarrow(.X, T, value); + cpu.flags.updateNZ(T, value); + } + }; + return f.PLX; + } + }; +} diff --git a/src/cpu16/opcodes/ply.zig b/src/cpu16/opcodes/ply.zig new file mode 100644 index 0000000..0e95858 --- /dev/null +++ b/src/cpu16/opcodes/ply.zig @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "PLY"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x7a, + .addressing_mode = .Stack, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Stack) @compileError("illegal addressing mode"); + const f = struct { + /// Pulls index register Y. + pub fn PLY(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + // Two cycles of the chip's own before anything is + // read: one after the opcode, one while the pointer moves. + cpu.program_counter.internal(); + cpu.program_counter.internal(); + const value = cpu.stack.pop(T, .page_one); + cpu.registers.setNarrow(.Y, T, value); + cpu.flags.updateNZ(T, value); + } + }; + return f.PLY; + } + }; +} diff --git a/src/cpu16/opcodes/tax.zig b/src/cpu16/opcodes/tax.zig new file mode 100644 index 0000000..a2ddb8a --- /dev/null +++ b/src/cpu16/opcodes/tax.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xaa, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies the accumulator into index register X. + pub fn TAX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.A, T); + cpu.registers.setNarrow(.X, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TAX; + } + }; +} diff --git a/src/cpu16/opcodes/tay.zig b/src/cpu16/opcodes/tay.zig new file mode 100644 index 0000000..27c8f9d --- /dev/null +++ b/src/cpu16/opcodes/tay.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xa8, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies the accumulator into index register Y. + pub fn TAY(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.A, T); + cpu.registers.setNarrow(.Y, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TAY; + } + }; +} diff --git a/src/cpu16/opcodes/tcd.zig b/src/cpu16/opcodes/tcd.zig new file mode 100644 index 0000000..89a7d1d --- /dev/null +++ b/src/cpu16/opcodes/tcd.zig @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "TCD"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x5b, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Copies the whole accumulator into the direct page register, sixteen bits of it whatever M says. + pub fn TCD(cpu: *CPU) !void { + const value = cpu.registers.get(.A); + cpu.registers.direct = value; + cpu.flags.updateNZ(u16, value); + cpu.program_counter.internal(); + } + }; + return f.TCD; + } + }; +} diff --git a/src/cpu16/opcodes/tcs.zig b/src/cpu16/opcodes/tcs.zig new file mode 100644 index 0000000..f415fd4 --- /dev/null +++ b/src/cpu16/opcodes/tcs.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "TCS"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x1b, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Copies the whole accumulator into the stack pointer, and touches no flag. + pub fn TCS(cpu: *CPU) !void { + cpu.stack.set(cpu.registers.get(.A)); + cpu.program_counter.internal(); + } + }; + return f.TCS; + } + }; +} diff --git a/src/cpu16/opcodes/tdc.zig b/src/cpu16/opcodes/tdc.zig new file mode 100644 index 0000000..24c5807 --- /dev/null +++ b/src/cpu16/opcodes/tdc.zig @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "TDC"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x7b, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Copies the direct page register into the whole accumulator. + pub fn TDC(cpu: *CPU) !void { + const value = cpu.registers.direct; + cpu.registers.set(.A, value); + cpu.flags.updateNZ(u16, value); + cpu.program_counter.internal(); + } + }; + return f.TDC; + } + }; +} diff --git a/src/cpu16/opcodes/tsc.zig b/src/cpu16/opcodes/tsc.zig new file mode 100644 index 0000000..350bbfb --- /dev/null +++ b/src/cpu16/opcodes/tsc.zig @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "TSC"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x3b, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Copies the stack pointer into the whole accumulator. + pub fn TSC(cpu: *CPU) !void { + const value = cpu.stack.get(); + cpu.registers.set(.A, value); + cpu.flags.updateNZ(u16, value); + cpu.program_counter.internal(); + } + }; + return f.TSC; + } + }; +} diff --git a/src/cpu16/opcodes/tsx.zig b/src/cpu16/opcodes/tsx.zig new file mode 100644 index 0000000..7cf7efb --- /dev/null +++ b/src/cpu16/opcodes/tsx.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xba, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies the stack pointer into index register X. + pub fn TSX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value: T = @truncate(cpu.stack.get()); + cpu.registers.setNarrow(.X, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TSX; + } + }; +} diff --git a/src/cpu16/opcodes/txa.zig b/src/cpu16/opcodes/txa.zig new file mode 100644 index 0000000..9b91d7f --- /dev/null +++ b/src/cpu16/opcodes/txa.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x8a, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies index register X into the accumulator. + pub fn TXA(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.m) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.X, T); + cpu.registers.setNarrow(.A, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TXA; + } + }; +} diff --git a/src/cpu16/opcodes/txs.zig b/src/cpu16/opcodes/txs.zig new file mode 100644 index 0000000..1a81565 --- /dev/null +++ b/src/cpu16/opcodes/txs.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x9a, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Copies index register X into the stack pointer, and touches no flag. Emulating, only the low half arrives: the high one is not the program's to choose. + pub fn TXS(cpu: *CPU) !void { + cpu.stack.set(cpu.registers.get(.X)); + cpu.program_counter.internal(); + } + }; + return f.TXS; + } + }; +} diff --git a/src/cpu16/opcodes/txy.zig b/src/cpu16/opcodes/txy.zig new file mode 100644 index 0000000..8456345 --- /dev/null +++ b/src/cpu16/opcodes/txy.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "TXY"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x9b, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies index register X into index register Y. + pub fn TXY(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.X, T); + cpu.registers.setNarrow(.Y, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TXY; + } + }; +} diff --git a/src/cpu16/opcodes/tya.zig b/src/cpu16/opcodes/tya.zig new file mode 100644 index 0000000..f4d8d54 --- /dev/null +++ b/src/cpu16/opcodes/tya.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0x98, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies index register Y into the accumulator. + pub fn TYA(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.m) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.Y, T); + cpu.registers.setNarrow(.A, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TYA; + } + }; +} diff --git a/src/cpu16/opcodes/tyx.zig b/src/cpu16/opcodes/tyx.zig new file mode 100644 index 0000000..dd30a84 --- /dev/null +++ b/src/cpu16/opcodes/tyx.zig @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "TYX"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xbb, + .addressing_mode = .Implied, + .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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + const f = struct { + /// Copies index register Y into index register X. + pub fn TYX(cpu: *CPU) !void { + if (comptime widths) |w| { + if (comptime w.x) at(cpu, u8) else at(cpu, u16); + } else { + if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); + } + } + + fn at(cpu: *CPU, comptime T: type) void { + const value = cpu.registers.getNarrow(.Y, T); + cpu.registers.setNarrow(.X, T, value); + cpu.flags.updateNZ(T, value); + cpu.program_counter.internal(); + } + }; + return f.TYX; + } + }; +} diff --git a/src/cpu16/opcodes/xba.zig b/src/cpu16/opcodes/xba.zig new file mode 100644 index 0000000..df0b99e --- /dev/null +++ b/src/cpu16/opcodes/xba.zig @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie +// +// SPDX-License-Identifier: MIT + +/// 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 = "XBA"; + pub const opcodes = [_]CPU.Opcodes.Init{ + .{ + .opcode = 0xeb, + .addressing_mode = .Implied, + .cycles = 3, + }, + }; + + /// 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, + comptime widths: ?CPU.Widths, + ) *const fn (*CPU) CPU.Opcodes.Error!void { + if (addressing_mode != .Implied) @compileError("illegal addressing mode"); + _ = widths; + const f = struct { + /// Swaps the two halves of the accumulator, which is how the hidden one is reached. The flags come from what ends up in the low half, eight bits of it whatever M says. + pub fn XBA(cpu: *CPU) !void { + const value = cpu.registers.get(.A); + const swapped = value >> 8 | value << 8; + cpu.registers.set(.A, swapped); + cpu.flags.updateNZ(u8, @truncate(swapped)); + // Two cycles of the chip's own, not one: swapping the + // halves is work, and it is done in the second. + cpu.program_counter.internal(); + cpu.program_counter.internal(); + } + }; + return f.XBA; + } + }; +} diff --git a/src/cpu16/stack.zig b/src/cpu16/stack.zig index 1c25f07..3dc68ff 100644 --- a/src/cpu16/stack.zig +++ b/src/cpu16/stack.zig @@ -7,14 +7,23 @@ const std = @import("std"); /// The 65816's stack pointer, which is sixteen bits wide and can put the stack /// anywhere in bank zero. /// -/// Except in emulation mode, where it cannot. There the high half is forced to -/// $01 and the stack is back in page one where a 6502 keeps it, and this is -/// not merely applied when something is pushed: the register itself reads back -/// with $01 in its high half for as long as the emulation line is set. The -/// SingleStepTests recordings show it plainly — a case that begins in +/// Except in emulation mode, where it mostly cannot — and the "mostly" took +/// the recordings to pin down. There the high half is forced to $01 and the +/// stack is back in page one where a 6502 keeps it, and that is not merely +/// applied when something is pushed: the register reads back with $01 in its +/// high half for as long as the emulation line is set. A case that begins in /// emulation with S at $6646 and executes a NOP, which touches no stack at /// all, ends with S reading $0146. /// +/// What is not forced is the arithmetic in the middle of an instruction. Push +/// two bytes from $0100 while emulating and the second one goes to $0000ff, +/// not back up to $0001ff: the pointer steps across the boundary and is pulled +/// back into page one afterwards. A 6502 never had the chance to notice, +/// because none of its instructions pushed two bytes; the ones that do here — +/// PHD, PEA, PEI, PER and PLD — are this processor's own and were never +/// promised the older machine's behaviour. So the confinement happens at the +/// start of each instruction and when the register is read, and nowhere else. +/// /// The stack lives in bank zero whatever the data bank says. pub fn Stack(comptime CPU: type) type { return struct { @@ -63,34 +72,57 @@ pub fn Stack(comptime CPU: type) type { if (cpu.flags.emulation) self.raw = emulation_page | (self.raw & 0x00ff); } - /// Steps the pointer, wrapping within page one in emulation mode and - /// across the whole of bank zero otherwise. - fn step(self: *Self, comptime direction: enum { up, down }) void { + /// Whether an instruction's use of the stack is held inside page one + /// while emulating. + /// + /// This is per instruction, not per access, and the recordings are + /// what said so. The ones the 6502 had keep its stack: pull from $01ff + /// and the next byte comes from $000100, because that is what a + /// program written for that machine will expect. The ones this + /// processor added were never promised anything of the sort, and step + /// straight out of the page — PLD from $01ff reads $000200. + pub const Confinement = enum { + /// The 6502's stack: the low half moves and the high half does + /// not. + page_one, + /// The whole of bank zero, whatever mode this is. + unconfined, + }; + + /// Steps the pointer, under whichever rule the instruction follows. + fn step(self: *Self, comptime where: Confinement, comptime direction: enum { up, down }) void { const cpu = self.getCPU(); - const delta: u16 = switch (direction) { + const delta: u8 = switch (direction) { .up => 1, - .down => 0xffff, + .down => 0xff, }; - if (cpu.flags.emulation) { - const low: u8 = @truncate(self.raw); - self.raw = emulation_page | (low +% @as(u8, @truncate(delta))); + if (where == .page_one and cpu.flags.emulation) { + self.raw = (self.raw & 0xff00) | (@as(u8, @truncate(self.raw)) +% delta); } else { - self.raw = self.raw +% delta; + self.raw = self.raw +% switch (direction) { + .up => 1, + .down => @as(u16, 0xffff), + }; } } /// Writes at the pointer and moves it down. A word goes out high byte /// first, as on the 6502. - pub fn push(self: *Self, comptime T: type, value: T) void { + /// + /// The address is the pointer as it stands, not as it reads back: + /// within an instruction an unconfined push is free to step out of + /// page one even while emulating, and `normalize` puts it back before + /// the next instruction starts. + pub fn push(self: *Self, comptime T: type, comptime where: Confinement, value: T) void { const cpu = self.getCPU(); switch (@typeInfo(T).int.bits) { 8 => { - cpu.memory.write(self.get(), value, .data); - self.step(.down); + cpu.memory.write(self.raw, value, .data); + self.step(where, .down); }, 16 => { - self.push(u8, @truncate(value >> 8)); - self.push(u8, @truncate(value)); + self.push(u8, where, @truncate(value >> 8)); + self.push(u8, where, @truncate(value)); }, else => @compileError("must be a u8 or a u16"), } @@ -98,16 +130,16 @@ pub fn Stack(comptime CPU: type) type { /// Moves the pointer up and reads. A word comes back low byte first, /// the order it was pushed in. - pub fn pop(self: *Self, comptime T: type) T { + pub fn pop(self: *Self, comptime T: type, comptime where: Confinement) T { const cpu = self.getCPU(); switch (@typeInfo(T).int.bits) { 8 => { - self.step(.up); - return cpu.memory.read(self.get(), .data); + self.step(where, .up); + return cpu.memory.read(self.raw, .data); }, 16 => { - const low: u16 = self.pop(u8); - const high: u16 = self.pop(u8); + const low: u16 = self.pop(u8, where); + const high: u16 = self.pop(u8, where); return high << 8 | low; }, else => @compileError("must be a u8 or a u16"), -- knotmirror 0.0.1