Handling Illegal Opcodes

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
zachmo47
Posts: 3
Joined: Wed Jun 09, 2021 2:10 am

Handling Illegal Opcodes

Post by zachmo47 »

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?
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Handling Illegal Opcodes

Post by Dwedit »

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.
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!
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Handling Illegal Opcodes

Post by Quietust »

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.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
zachmo47
Posts: 3
Joined: Wed Jun 09, 2021 2:10 am

Re: Handling Illegal Opcodes

Post by zachmo47 »

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.
Thank you that is really helpful!
User avatar
aquasnake
Posts: 515
Joined: Fri Sep 13, 2019 11:22 pm

Re: Handling Illegal Opcodes

Post by aquasnake »

Illegal operation code existed in the unlicensed game "Bio Hazard"(mapper15 version), i dont think option 1 is the right way for emulating
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Handling Illegal Opcodes

Post by tokumaru »

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.
NewRisingSun
Posts: 1510
Joined: Thu May 19, 2005 11:30 am

Re: Handling Illegal Opcodes

Post by NewRisingSun »

aquasnake wrote: Fri Jun 11, 2021 2:29 am Illegal operation code existed in the unlicensed game "Bio Hazard"(mapper15 version), i dont think option 1 is the right way for emulating
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).
zzo38
Posts: 1096
Joined: Mon Feb 07, 2011 12:46 pm

Re: Handling Illegal Opcodes

Post by zzo38 »

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.
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).

"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)
User avatar
aquasnake
Posts: 515
Joined: Fri Sep 13, 2019 11:22 pm

Re: Handling Illegal Opcodes

Post by aquasnake »

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
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Handling Illegal Opcodes

Post by tepples »

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).
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Handling Illegal Opcodes

Post by Oziphantom »

For emulation this is the source to use https://csdb.dk/release/?id=198357
Post Reply