4

I.e. are the different addressing modes encoded somehow in the opcodes? Can they be extracted programmatically or does this info only exist in the documentation of the 6502? I'm writing an emulator and I'm not concerned with performance. It would be nice to have a function that takes an opcode and returns the addressing mode, if possible.

So far I've not come across any indication that there's a pattern in the codes, except that all zero page instructions seem to have their third bit set.

Andreas
  • 2,665
  • 2
  • 29
  • 38

1 Answers1

5

Yes there is. The addressing mode is encoded in 3 bits at positions 4-2 in the opcode byte (i.e. xxxAAAxx). Decoding the addressing mode is dependent on the other bits, but they conform to a regular pattern (mostly) which can be dropped through a lookup table to determine the mode for each instruction type.

This page has a full description of the various patterns and how to decode in each case.

Eight-Bit Guru
  • 9,756
  • 2
  • 48
  • 62
  • That's great! I had four different pages with descriptions of the instruction set, and none of them mentioned anything of the bit level semantics. – Andreas Sep 16 '14 at 14:31
  • Good luck with your emulator; the first version of my Sharp6502 emulator worked at the opcode level, and managed 70MHz on a Pentium 4. The second version operated at the bit-decoder level, and clocked at 122MHz on the same hardware - so it's entirely possible to get spectacular performance (in comparison to the 1MHz silicon) when emulating at this level, even when writing in a managed language like C#. – Eight-Bit Guru Sep 16 '14 at 19:51
  • Thank you. I realized that it's not obvious to me how to leverage this bit information for code readability (or performance) when working in Java. Seems to get more messy than just using a giant flat switch-statement. Can't seem to find your emulator on Google, it would've been interesting to look at. – Andreas Sep 22 '14 at 11:47
  • The code was on Google Code but has been taken down whilst I find a new home for it. Here's the (long out of date) blog: http://legacysystem.blogspot.co.uk/ You can certainly use switch-statements, although I combined that with a bit-shift operation to isolate the elements of the opcode and then do a simple numeric switch rather than using a huge mnemonic switch. – Eight-Bit Guru Sep 22 '14 at 19:07
  • Really really interesting stuff. – Juan Antonio Gomez Moriano Nov 24 '14 at 02:14