title: Fetching variable length opcodes and CPU timing
body: <p>I'm currently trying to write a NES emulator in C++ as a summer programming project to get ready for fall term next school year (I haven't coded in a while).  I've already written a Chip8 emulator, so I thought the next step would be to try and write a NES emulator.</p>
  
  <p>Anyways, I'm getting stuck.  I'm using <a href="http://www.obelisk.demon.co.uk/6502/reference.html" rel="nofollow">this</a> website for my opcode table and I'm running into a road block.  On the Chip8, all opcodes were two bytes long, so they were easy to fetch.  However, the NES seems to have either 2 or 3 byte opcodes depending on what addressing mode the CPU is in.  I can't think of any easy way to figure out how many bytes I need to read for each opcode (my only idea was to create really long if statements that check the first byte of the opcode to see how many more bytes to read).</p>
  
  <p>I'm also having trouble with figuring how to count cycles.  How do I create a clock within a programming language so that everything is in sync?</p>
  
  <p>On an unrelated side note, since the NES is little-endian, do I need to read programCounter + 1 and then read programCounter to get the correct opcode?</p>