Jason Sadler<p><strong>NES Emulator: CPU Instructions</strong></p><p>I’m <a href="https://www.sadlerjw.com/2025/07/31/writing-an-nes-emulator-in-swift/" rel="nofollow noopener" target="_blank">writing an NES emulator</a> in Swift!</p><p>The NES uses a <a href="https://en.wikipedia.org/wiki/MOS_Technology_6502" rel="nofollow noopener" target="_blank">6502</a> processor, which has 3 normal registers (<code>a</code>, <code>x</code>, and <code>y</code>) and defines its own instruction set of <a href="https://www.nesdev.org/wiki/Instruction_reference" rel="nofollow noopener" target="_blank">56 instructions</a>. That makes for a lot of little tiny implementations! Addition (<a href="https://www.nesdev.org/wiki/Instruction_reference#ADC" rel="nofollow noopener" target="_blank"><code>ADC</code></a>) and subtraction (<a href="https://www.nesdev.org/wiki/Instruction_reference#SBC" rel="nofollow noopener" target="_blank"><code>SBC</code></a>) are probably the most complex ones, with the latter taking me some two hours to figure out simply because it’s the first time since 2004(?) that I’ve had to think about binary math and <a href="https://en.wikipedia.org/wiki/Two%27s_complement" rel="nofollow noopener" target="_blank">two’s complements</a>. Luckily (or annoyingly depending on your point of view) many of the instructions are nearly identical, especially the six “transfer” instructions, which just copy values between different registers.</p><p>While the obvious thing might have been to implement each instruction as a small function on my <code>CPU</code> class, I wanted to break each out into its own file to avoid making it a multi-thousand-line mess. Having each instruction implemented as its own struct also gives me the flexibility to turn each into a little state machine if I decide to make my emulator clock-accurate in the future. For the time being, each instruction fully executes on its first clock tick, and then the CPU just spins for the <a href="https://www.nesdev.org/wiki/6502_cycle_times" rel="nofollow noopener" target="_blank">correct number of ticks</a> before executing the next instruction. So my current implementation is <em>duration</em>-accurate, but not <em>clock</em>-accurate.</p><p>The <a href="https://github.com/sadlerjw/SwiftNES/blob/9dcf155f4f54918bcf6afde4d5dc7ba8a27ef9b7/NES-iOS/Emulator/Instructions.swift#L8" rel="nofollow noopener" target="_blank"><code>Instruction</code> protocol</a> that each instruction conforms to just defines an <code>execute</code> method that takes the CPU as an argument. Since the instruction naturally needs to be able to fully access the CPU, one downside of this arrangement is that none of the CPU’s state can be declared private. The <code>NES</code> class is the only other code with a reference to the CPU, but it does expose it as public so that I can create some <a href="https://www.sadlerjw.com/wp-content/uploads/2025/08/Simulator-Screenshot-Clone-2-of-iPhone-16-Pro-2025-08-02-at-15.03.19.png" rel="nofollow noopener" target="_blank">debugging UI</a> with access to its registers. Maybe I can mitigate that with some module boundaries. We’ll see.</p><p>Because I have no idea what I’m doing (and because I hope the source can be a bit of a portfolio piece to show to potential employers), <a href="https://github.com/sadlerjw/SwiftNES/tree/main/NES-iOSTests" rel="nofollow noopener" target="_blank">I wrote unit tests</a> for each instruction and addressing mode.</p><p>One mistake I made is forgetting that of course some instructions<em> write</em> to memory! Since most of the instructions just read values and store results in registers, I had the CPU’s <code>tick</code> function use the current addressing mode to fetch the appropriate value and make it available to the instruction via a property on <code>CPU</code> before calling the instruction’s <code>execute</code> method. When I got to read-modify-write instructions like <a href="https://www.nesdev.org/wiki/Instruction_reference#ASL" rel="nofollow noopener" target="_blank"><code>ASL</code></a>, I let the instruction return a value from <code>execute</code> and then the tick function would use the addressing mode to write it back to where it came from.</p><p>But wait! There’s another complication! <a href="https://www.nesdev.org/wiki/Instruction_reference#JMP" rel="nofollow noopener" target="_blank"><code>JMP</code></a> actually needs to read <em>two</em> bytes from memory. (Addresses are 2 bytes long, but memory reads and writes only operate on a byte at a time.) This means I need to store the address from the addressing mode so that the instruction can read the extra byte from the <em>following</em> address.</p><p>I think sometime down the road instead of having the CPU’s <code>tick</code> method try to be smart and do these things, I’ll just pass the addressing mode into the instruction’s <code>execute</code> method and let it read and write exactly what it needs. Whoops!</p><p>I finally finished implementing all of the instructions and I’m on to the <a href="https://www.nesdev.org/wiki/PPU" rel="nofollow noopener" target="_blank">PPU</a>, starting with its memory-mapped registers that are exposed on the CPU’s bus. Reading and writing these registers cause side effects within the PPU! More interesting times ahead! Stay tuned for more.</p><p>If you’re interested in following along you can find the code <a href="https://github.com/sadlerjw/SwiftNES/" rel="nofollow noopener" target="_blank">on Github</a>.</p> <p><a rel="nofollow noopener" class="hashtag u-tag u-category" href="https://www.sadlerjw.com/tag/emulation/" target="_blank">#emulation</a> <a rel="nofollow noopener" class="hashtag u-tag u-category" href="https://www.sadlerjw.com/tag/nes/" target="_blank">#nes</a> <a rel="nofollow noopener" class="hashtag u-tag u-category" href="https://www.sadlerjw.com/tag/programming/" target="_blank">#programming</a> <a rel="nofollow noopener" class="hashtag u-tag u-category" href="https://www.sadlerjw.com/tag/swift/" target="_blank">#swift</a></p>