How MOS 6502 Illegal Opcodes Work – Michael Steil

https://news.ycombinator.com/rss Hits: 7
Summary

The original NMOS version of the MOS 6502, used in computers like the Commodore 64, the Apple II and the Nintendo Entertainment System (NES), is well-known for its illegal opcodes: Out of 256 possible opcodes, 151 are defined by the architecture, but many of the remaining 105 undefined opcodes do useful things. Many articles have been written to test and document these, but I am not aware of any article that tries to explain where exactly they come from. I’ll do this here. The Block Diagram Every 6502 data sheet comes with a block diagram, but these are of no use, because they are oversimplified, partially incorrect, and don’t explain how instruction decoding works. The following more detailed diagram is a lot more useful: (Original from Apple II things) The Decode ROM (PLA) There is no need to understand the whole diagram. The important part is on the left: The instruction register, which holds the opcode, and the current clock cycle within the instruction (T0 to T6) get fed into a 130×21 bit decode ROM, i.e. a ROM with 130 lines of 21 bits each. On the die shot, this is the green area on the bottom. (Original from Molecular Expressions) While other CPUs from the same era used microcode to interpret the instruction, the 6502 had this 130×21 bit PLA. All lines of the PLA compare the instruction and the current clock cycle, and if they match, the line fires. A little simplified, every line looks like this: ON bits OFF bits timing 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 T6 T5 T4 T3 T2 T1 (See the diagrams at http://impulzus.sch.bme.hu/6502/ for details; partial English translation of the website here). “ON bits” specifies, which bits need to be set for this line to fire. “OFF bits” specifies, which bits need to be clear for this line to fire. The opcode table of the 6502 is laid out in a way that you can find easy rules to generalize the effects of similar opcodes. For example, the branch opcodes are encoded like this: %aab10000 where “aa” is the condition (00=N, 01=V, 10=C, ...

First seen: 2025-04-23 14:46

Last seen: 2025-04-23 20:47