title: 6502 CPU Emulation
body: <p>It's the weekend, so I relax from spending all week programming by writing a hobby project.</p>
  
  <p>I wrote the framework of a MOS 6502 CPU emulator yesterday, the registers, stack, memory and all the opcodes are implemented. (Link to source below)</p>
  
  <p>I can manually run a series of operations in the debugger I wrote, but I'd like to load a NES rom and just point the program counter at its instructions, I figured that this would be the fastest way to find flawed opcodes.</p>
  
  <p>I wrote a quick NES rom loader and loaded the ROM banks into the CPU memory.</p>
  
  <p>The problem is that I don't know how the opcodes are encoded. I know that the opcodes themselves follow a pattern of one byte per opcode that uniquely identifies the opcode, </p>
  
  <pre><code>0 - BRK
  1 - ORA (D,X)
  2 - COP b
  </code></pre>
  
  <p>etc</p>
  
  <p>However I'm not sure where I'm supposed to find the opcode argument. Is it the the byte directly following? In absolute memory, I suppose it might not be a byte but a short. </p>
  
  <p>Is anyone familiar with this CPU's memory model?</p>
  
  <p>EDIT: I realize that this is probably shot in the dark, but I was hoping there were some oldschool Apple and Commodore hackers lurking here.</p>
  
  <p><strong>EDIT:</strong> Thanks for your help everyone. After I implemented the proper changes to align each operation the CPU can load and run Mario Brothers. It doesn't do anything but loop waiting for Start, but its a good sign :)</p>
  
  <p>I uploaded the source:</p>
  
  <p><a href="https://archive.codeplex.com/?p=cpu6502" rel="nofollow noreferrer">https://archive.codeplex.com/?p=cpu6502</a></p>
  
  <p>If anyone has ever wondered how an emulator works, its pretty easy to follow. Not optimized in the least, but then again, I'm emulating a CPU that runs at 2mhz on a 2.4ghz machine :)</p>