Detecting an NES rom based on file contents.

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

simonwjackson
Posts: 7
Joined: Tue Nov 12, 2013 7:05 am

Detecting an NES rom based on file contents.

Post by simonwjackson »

Hey everyone!

I am writing a rom manager, and i'm a little stumped by this issue.

Is there any data inside of an NES rom that will accurately identify it as an NES rom?
Maybe something in the header?
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Detecting an NES rom based on file contents.

Post by rainwarrior »

Traditionally you would use the entire data of the file hashed down to a single large number.

http://en.wikipedia.org/wiki/Cyclic_redundancy_check

For a .NES file it's probably best to skip the header, though, since your reasons for identifying the ROM programmatically probably have to do with a bad or inadequate header.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Detecting an NES rom based on file contents.

Post by Dwedit »

If you're just asking about the magic number at the beginning of a file, it's "NES", 0x1A. That will identify a iNES format file.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
simonwjackson
Posts: 7
Joined: Tue Nov 12, 2013 7:05 am

Re: Detecting an NES rom based on file contents.

Post by simonwjackson »

Thanks for the reply.

The only problem with this is that I would need to check it against a database. A rom might be a work in progress, a very obscure demo, custom patch, etc.

However, the goal is to identify IF it is a NES rom but not necessarily what game it is.

I was thinking that maybe there is some tiny piece of data that is in every NES ROM that I could check against.

Any ideas are welcome! :)
simonwjackson
Posts: 7
Joined: Tue Nov 12, 2013 7:05 am

Re: Detecting an NES rom based on file contents.

Post by simonwjackson »

Dwedit wrote:If you're just asking about the magic number at the beginning of a file, it's "NES", 0x1A. That will identify a iNES format file.
That might be what i'm looking for. I'll give this a shot!
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Detecting an NES rom based on file contents.

Post by rainwarrior »

Oh, sorry, I misunderstood the question. Yes, I think Dwedit's answer should be enough. Normally they also have the .NES file extension... How come you need to identify it from the file contents?
simonwjackson
Posts: 7
Joined: Tue Nov 12, 2013 7:05 am

Re: Detecting an NES rom based on file contents.

Post by simonwjackson »

rainwarrior wrote:Oh, sorry, I misunderstood the question. Yes, I think Dwedit's answer should be enough. Normally they also have the .NES file extension... How come you need to identify it from the file contents?
Better usability :)

I'm going to assume that the user may have (incorrectly) renamed/removed the file extension

This is also an issue with other roms/images that use the same extension.
For example, I would need to do something similar for gamecube and PS2
roms that use the same file extension .ISO
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Detecting an NES rom based on file contents.

Post by tepples »

PlayStation and PlayStation 2 games should use ISO because they are images of ISO 9660 (CD) or ISO/IEC 13346 (DVD) file systems. I was under the impression that GameCube games should use a different extension because they use a different file system. I've seen .gcm in use.

The problem is similar to distinguishing a program written in Python 2 from a program written in Python 3. The "open" action under Windows looks only at the file name suffix, and it took Python Software Foundation years to get a proper handler for .py files under Windows that distinguishes Python 2 from Python 3 by the #! line.
simonwjackson
Posts: 7
Joined: Tue Nov 12, 2013 7:05 am

Re: Detecting an NES rom based on file contents.

Post by simonwjackson »

tepples wrote:PlayStation and PlayStation 2 games should use ISO because they are images of ISO 9660 (CD) or ISO/IEC 13346 (DVD) file systems. I was under the impression that GameCube games should use a different extension because they use a different file system. I've seen .gcm in use.
This is the root of my problem. Some roms (or disk images) may use the same file extension for different platforms.
Dolphin can read: .gcm, .ISO, ..
PCSX2 can read: .bin, .ISO, ..

So I'm looking for ways to read the files contents and look for parters to determine the type of rom (or disk image)
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Detecting an NES rom based on file contents.

Post by Quietust »

simonwjackson wrote:I'm going to assume that the user may have (incorrectly) renamed/removed the file extension
If the file doesn't have the correct extension, then most (if not all) emulators won't even list it in their Open dialogs (for those that have Open dialogs, which is most of them), so that assumption probably isn't meaningful. Some emulators (such as my own) also check the extension to determine how to load the file, in which case an incorrect extension would prevent them from being loaded at all.

For other platforms, there's certainly the issue of one extension possibly having multiple meanings (especially Genesis ROMs which idiotically use the .BIN extension), but that's not a problem with the NES.
Last edited by Quietust on Tue Nov 12, 2013 8:52 am, edited 2 times in total.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
snarfblam
Posts: 143
Joined: Fri May 13, 2011 7:36 pm

Re: Detecting an NES rom based on file contents.

Post by snarfblam »

I wrote a program that needed to distinguish different kinds of ROMs. I didn't deal with any disk based systems, so I can't help you there. For most ROMs, I just looked for some kind of magic number or standard string. You could go further and start verifying header data, but in my case this didn't seem necessary.

For SNES, there isn't any magic number or standard string that I was aware of. (SMC headers have become much less popular, and if added back on, they may consist of all zeros instead of the correct SMC values.) Instead I verified the internal checksum if the ROM wasn't exceedingly large.

In cases where more than one possible system was identified (or zero), I would disambiguate by file extension. If that failed, I simply asked the user to manually specify the system. But my program only worked on one ROM at a time. If it saves you any trouble, the utility is here, as well as a link to the source, and includes an HTML file that details platform detection.
simonwjackson
Posts: 7
Joined: Tue Nov 12, 2013 7:05 am

Re: Detecting an NES rom based on file contents.

Post by simonwjackson »

Awesome, thanks for the link! This really helps!
darkhog
Posts: 192
Joined: Tue Jun 28, 2011 2:39 pm

Re: Detecting an NES rom based on file contents.

Post by darkhog »

Quietust wrote:
simonwjackson wrote:I'm going to assume that the user may have (incorrectly) renamed/removed the file extension
If the file doesn't have the correct extension, then most (if not all) emulators won't even list it in their Open dialogs (for those that have Open dialogs, which is most of them), so that assumption probably isn't meaningful. Some emulators (such as my own) also check the extension to determine how to load the file, in which case an incorrect extension would prevent them from being loaded at all.

For other platforms, there's certainly the issue of one extension possibly having multiple meanings (especially Genesis ROMs which idiotically use the .BIN extension), but that's not a problem with the NES.
Tell that to Linux/OSX/Other flavor of Unix users where file extensions are just for user's convenience and shouldn't be used to detect file types (10th paragraph, possibly more later on). So let's say, Mario rom can be called (in file system)
mario.nes
mario.rom
mario
(or if someone is particularly crazy)
mario.bin.doc.xls

So detecting by header structure (should look like iNES header) rather than file extension has certain benefits as you can see unless you are ABSOLUTELY sure you'll NEVER port it on unix-like system.
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Detecting an NES rom based on file contents.

Post by blargg »

Find a file with lots of occurrences of the pairs $15 $40 and $07 $20 :)
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: Detecting an NES rom based on file contents.

Post by Bregalad »

I'd be surprised in PS1 images uses ISO, it's very common for PS1 games to use MODE2 sectors (that is, store meaningful data in what is error correction data in MODE1), so it can't be represented by ISO images. CUE/BIN or CUE/IMG is often used.
Post Reply