-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnes_rom_extract.py
executable file
·42 lines (37 loc) · 1.18 KB
/
nes_rom_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# Author: Bryan Cain (Plombo)
# Date: August 2010
# Updated: December 28, 2010
# Extracts an NES ROM from a 00000001.app file from an NES Virtual Console game.
import sys
from cStringIO import StringIO
# returns a file-like object
def extract_nes_rom(app1):
romoffset = 0
while True:
buf = app1.read(8192)
if buf.find('NES\x1a') >= 0: # Found NES ROM
romoffset += buf.find('NES\x1a')
break
elif len(buf) != 8192: # End of file, and no NES rom found
app1.close()
return None
else: romoffset += 8192
# NES ROM found; calculate size and extract it (FIXME: size calculation doesn't work)
app1.seek(romoffset)
#size = 16 + 128 # 16-byte header, 128-byte title data (footer)
#size += 16 * 1024 * ord(app1.read(1)) # next byte: number of PRG banks, 16KB each
#size += 8 * 1024 * ord(app1.read(1)) # next byte: number of CHR banks, 8KB each
app1.seek(romoffset)
rom = StringIO(app1.read())
return rom
if __name__ == '__main__':
if len(sys.argv) != 3:
sys.exit('Usage: %s input.app output.nes' % sys.argv[0])
f = open(sys.argv[1], 'rb')
rom = extract_nes_rom(f)
f.close()
f2 = open(sys.argv[2], 'wb')
f2.write(rom.read())
f2.close()
print 'Done!'