Hello. I'm interested in making a 6502 emulator just for fun.
I have a question around a 6502 machine code.
On the web, we may find a 6502 machine code table like this:
http://www.llx.com/~nparker/a2/opcodes.htmlBy the way, if you make an evaluator to simulate an 8-bit processor in a higher language such as C, Python, Lisp or whatever, you've got to write 256 CASE statements at maximum.
This seems to suck.
I mean, for instance, if you wrote LDAs, you've got to write like this in psedo code:
CASE (OPCODE)
0xA9 -> LDA with Immediate
0xA5 -> LDA with Zeropage
0xB5 -> LDA with Zeropage, X
0xAD -> LDA with Absolute
0xBD -> LDA with Absolute, X
0xB9 -> LDA with Absolute, Y
0xA1 -> LDA with (Indirect, X)
0xB1 -> LDA with (Indirect), Y
You've got to do this to all function(in the higher language you choose).
Those hexadecimal numbers don't mean any sense.
So I wondered if there is any rule to combine "Function" and "Addressing Mode".
As long as I know, 6502 has 13 addressing modes and basically 50 .... how can I say?, uh, Function? modes.
13 addressing modes x 50 "Functions" gives 650 opcodes "Theoretically". Of course, 6502 is a 8-bit processor, so this is too many. A lot of function lacks some addressing modes, so, total number of "OPCODE" comes below 256.
I checked some OPCODES in binay. For instance, LDA with Addressing Mode becomes like this:
LDA with Immediate -> 10101001
LDA with Zeropage -> 10100101
LDA with Zeropage, X -> 10110101
LDA with Absolute -> 10101101
LDA with Absolute, X -> 10111101
LDA with Absolute, Y -> 10111001
LDA with (Indirect, X) -> 10100001
LDA with (Indirect), Y -> 10110001
The common part of LDAs is this:
101xxx01
So I guessed 101xxx01 is the "Function" part of LDAs' opcode.
However, there is a problem. xxx represents 3bit and 3bit cannot express "13" addressing modes(At least, you must have 4bit to express all 13 addressing modes).
Here's another problem. You may see in, for instance, LDXs.
LDX with Immediate -> 10100010
LDX with Zeropage -> 10100110
LDX with Zeropage, Y -> 10110110
LDX with Absolute -> 10101110
LDX with Absolute, Y -> 10111110
If the theory stated above were correct, the common part of LDX becomes:
101xxx10
and xxx implies Addressing Mode of 6502.
Even though Zeropage seems representing 001, Immediate of LDA and that of LDX are different:
Immediate of LDA -> 010
Immediate of LDX -> 000
Here's the other problem. Let's check STXs.
STX with Zeropage -> 10000110
STX with Zeropage, Y -> 10010110
STX with Absolute -> 10001110
Common part of STXs seems to be:
10xxx110
I mean, the bit field which must be represented (in the assumption) is different.
So I'm complete lost.
Please tell me if there is any rule to produce a certain OPCODE, by combining "Function" and "Addressing Mode"; otherwise, there is just no such rule behind OPCODEs.
Thanks, regards.