-
Notifications
You must be signed in to change notification settings - Fork 0
/
romloader.py
61 lines (47 loc) · 2.66 KB
/
romloader.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import hardware
class romLoader:
nesSystem = 0
def __init__(self, nes):
self.nesSystem = nes
def loadRom(self, romPath):
try:
romFile = open(romPath, 'rb')
except ValueError:
print "File I/O Error. Check the path of the rom file."
romFile.seek(4) #skip first 4 bytes of crapola (the NES> bit)
#set prgCount, chrCount and control bytes
self.nesSystem.rom.prgCount = ord(romFile.read(1)) #number of 16kb rom banks
self.nesSystem.rom.chrCount = ord(romFile.read(1)) #number of 8kb vrom banks
self.nesSystem.rom.controlByte1 = romFile.read(1)
self.nesSystem.rom.controlByte2 = romFile.read(1)
#Shift the lower bits to the rightmost nibble, OR the two BYTEs together to get our mapper number
#print self.nesSystem.rom.controlByte1
self.nesSystem.rom.mapperNumber = (ord(self.nesSystem.rom.controlByte2) & 0xf0) | ((ord(self.nesSystem.rom.controlByte1) & 0xf0) >> 4)
romFile.seek(16)
#get trainer
if ord(self.nesSystem.rom.controlByte1) & self.nesSystem.rom.TRAINER_MASK:
self.nesSystem.rom.trainerData = romFile.read(self.nesSystem.rom.TRAINER_SIZE)
#prg rom
#read prg count * prg size (different roms have different sized banks). Ord() each item
self.nesSystem.rom.prgData = romFile.read(self.nesSystem.rom.PRG_SIZE * self.nesSystem.rom.prgCount)
self.nesSystem.rom.prgData = map(ord, self.nesSystem.rom.prgData)
#chr rom
self.nesSystem.rom.chrData = romFile.read(self.nesSystem.rom.CHR_SIZE * self.nesSystem.rom.chrCount)
self.nesSystem.rom.chrData = map(ord, self.nesSystem.rom.chrData)
# for index, x in enumerate(self.nesSystem.rom.chrData):
# print hex(index), hex(x)
print romPath, " ......LOADED"
print "Mapper Number: ", self.nesSystem.rom.mapperNumber
print "PRG Count: ", self.nesSystem.rom.prgCount
print "CHR Count: ", self.nesSystem.rom.chrCount
print "ControlByte 1: ", ord(self.nesSystem.rom.controlByte1)
print "ControlByte 2: ", ord(self.nesSystem.rom.controlByte2)
print "Size of PRG Data: ", len(self.nesSystem.rom.prgData)
print "Size of CHR Data: ", len(self.nesSystem.rom.chrData)
print "\n"
def printPrg(self, count):
for x in range(count):
print hex(ord(self.nesSystem.rom.prgData[x]))
def printChr(self, count):
for x in range(count):
print hex(ord(self.nesSystem.rom.chrData[x]))