Handling Illegal Opcodes
Moderator: Moderators
Handling Illegal Opcodes
Hello! I've been trying to write my own NES emulating using JS but one thing I'm unsure about is when an illegal opcode is encountered how much do I need to increment the program counter otherwise it'll just get stuck trying to run the same illegal opcode?
Re: Handling Illegal Opcodes
You should not be encountering illegal opcodes. Real games uses these very rarely (but a few do). If you encounter illegal instructions, it usually means you are doing something wrong.
First thing to confirm is that you are starting the program at where the Reset Vector says to begin, and not the first byte of ROM.
First thing to confirm is that you are starting the program at where the Reset Vector says to begin, and not the first byte of ROM.
Last edited by Dwedit on Wed Jun 09, 2021 10:00 am, edited 2 times in total.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Re: Handling Illegal Opcodes
How you handle illegal opcodes is entirely up to you, but there are several methods to choose from:
1. Immediately halt emulation and don't allow it to resume. If you hit one of the "HLT"/"KIL" opcodes (02/22/42/62 or 12/32/52/72/92/B2/D2/F2), this is technically the "correct" way of responding.
2. Treat the opcode as a single-byte NOP instruction and skip it. This might work for some cases, but it's a bad idea because the 6502 treats most of the them as multibyte.
3. Determine the expected length of the opcode encountered, then treat it as an N-byte NOP instruction and skip it. This should work for most licensed games.
4. Actually emulate what the instruction would've done on a real NES. Most of them are fairly easy to do, but a few might be extremely complicated or unpredictable (like XAA) and might be candidates for just treating as multibyte NOPs due to the extreme unlikelihood of actually encountering them.
There's a wiki page which lists the addressing mode (and thus byte length) of all 256 opcodes, and you'll need that if you decide to go with options #3 and/or #4.
1. Immediately halt emulation and don't allow it to resume. If you hit one of the "HLT"/"KIL" opcodes (02/22/42/62 or 12/32/52/72/92/B2/D2/F2), this is technically the "correct" way of responding.
2. Treat the opcode as a single-byte NOP instruction and skip it. This might work for some cases, but it's a bad idea because the 6502 treats most of the them as multibyte.
3. Determine the expected length of the opcode encountered, then treat it as an N-byte NOP instruction and skip it. This should work for most licensed games.
4. Actually emulate what the instruction would've done on a real NES. Most of them are fairly easy to do, but a few might be extremely complicated or unpredictable (like XAA) and might be candidates for just treating as multibyte NOPs due to the extreme unlikelihood of actually encountering them.
There's a wiki page which lists the addressing mode (and thus byte length) of all 256 opcodes, and you'll need that if you decide to go with options #3 and/or #4.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
P.S. If you don't get this note, let me know and I'll write you another.
Re: Handling Illegal Opcodes
Thank you that is really helpful!Quietust wrote: ↑Wed Jun 09, 2021 9:58 am How you handle illegal opcodes is entirely up to you, but there are several methods to choose from:
1. Immediately halt emulation and don't allow it to resume. If you hit one of the "HLT"/"KIL" opcodes (02/22/42/62 or 12/32/52/72/92/B2/D2/F2), this is technically the "correct" way of responding.
2. Treat the opcode as a single-byte NOP instruction and skip it. This might work for some cases, but it's a bad idea because the 6502 treats most of the them as multibyte.
3. Determine the expected length of the opcode encountered, then treat it as an N-byte NOP instruction and skip it. This should work for most licensed games.
4. Actually emulate what the instruction would've done on a real NES. Most of them are fairly easy to do, but a few might be extremely complicated or unpredictable (like XAA) and might be candidates for just treating as multibyte NOPs due to the extreme unlikelihood of actually encountering them.
There's a wiki page which lists the addressing mode (and thus byte length) of all 256 opcodes, and you'll need that if you decide to go with options #3 and/or #4.
Re: Handling Illegal Opcodes
Illegal operation code existed in the unlicensed game "Bio Hazard"(mapper15 version), i dont think option 1 is the right way for emulating
Re: Handling Illegal Opcodes
Definitely not right in the sense that it's not what a real console would do, but It's a quick solution that will work for the vast majority of licensed games.
-
- Posts: 1510
- Joined: Thu May 19, 2005 11:30 am
Re: Handling Illegal Opcodes
Please keep in mind that the mapper 15 version is a bad mapper hack that works properly in no emulator for long. Good dumps use mappers 227 (rev0) and 178 (rev1).
Re: Handling Illegal Opcodes
My own recommendation is option 4 for all stable opcodes (whether or not any game currently uses them), and option 1 for unstable opcodes (also display an error message in this case).Quietust wrote: ↑Wed Jun 09, 2021 9:58 am 1. Immediately halt emulation and don't allow it to resume. If you hit one of the "HLT"/"KIL" opcodes (02/22/42/62 or 12/32/52/72/92/B2/D2/F2), this is technically the "correct" way of responding.
2. Treat the opcode as a single-byte NOP instruction and skip it. This might work for some cases, but it's a bad idea because the 6502 treats most of the them as multibyte.
3. Determine the expected length of the opcode encountered, then treat it as an N-byte NOP instruction and skip it. This should work for most licensed games.
4. Actually emulate what the instruction would've done on a real NES. Most of them are fairly easy to do, but a few might be extremely complicated or unpredictable (like XAA) and might be candidates for just treating as multibyte NOPs due to the extreme unlikelihood of actually encountering them.
There's a wiki page which lists the addressing mode (and thus byte length) of all 256 opcodes, and you'll need that if you decide to go with options #3 and/or #4.
"Stable" opcodes means the behaviour is well-defined and allows the program to continue, so that includes many of the unofficial opcodes too. See http://www.oxyron.de/html/opcodes02.html for a table of opcodes. Cells marked "KIL", and those with blue or red text, are unstable; everything else is stable (as far as I know; you can reply with a correction if I am wrong about any of this please). (Note that LAX is only unstable with an immediate operand; LAX with other operands is stable.)
(Free Hero Mesh - FOSS puzzle game engine)
Re: Handling Illegal Opcodes
There is a difference between 6502 and nes. The CPU part of 2a03 is a subset of 6502. Of course, some of the opcodes are invalid. If they are not defined, it is not the original behavior of 6502. If only the PC pointer increases on the physical console, the same should be on emulators
Re: Handling Illegal Opcodes
The stable unofficial opcodes behave the same way in the NES as they do in the Atari 2600, Commodore 64, and unenhanced Apple II (at least with decimal mode off).
-
- Posts: 1565
- Joined: Tue Feb 07, 2017 2:03 am
Re: Handling Illegal Opcodes
For emulation this is the source to use https://csdb.dk/release/?id=198357