ines-parser 1.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ines_parser/__init__.py +46 -0
- ines_parser/cli/__init__.py +0 -0
- ines_parser/cli/ines_scan_roms.py +535 -0
- ines_parser/cli/ines_split_rom.py +346 -0
- ines_parser/mappers.py +147 -0
- ines_parser/parser.py +492 -0
- ines_parser-1.2.0.dist-info/METADATA +117 -0
- ines_parser-1.2.0.dist-info/RECORD +12 -0
- ines_parser-1.2.0.dist-info/WHEEL +5 -0
- ines_parser-1.2.0.dist-info/entry_points.txt +3 -0
- ines_parser-1.2.0.dist-info/licenses/LICENSE +22 -0
- ines_parser-1.2.0.dist-info/top_level.txt +1 -0
ines_parser/parser.py
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
#!/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
iNES ROM header parser with full NES 2.0 support
|
|
4
|
+
Based on the iNES and NES 2.0 file format specifications
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from typing import Optional, Dict, Any, List
|
|
9
|
+
from .mappers import (
|
|
10
|
+
get_mapper_name,
|
|
11
|
+
get_mapper_alternate_names,
|
|
12
|
+
get_mapper_notes,
|
|
13
|
+
get_mapper_info,
|
|
14
|
+
is_known_mapper,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# iNES format constants
|
|
19
|
+
INES_HEADER_SIZE = 16
|
|
20
|
+
INES_TRAINER_SIZE = 512
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class INESFormat(Enum):
|
|
24
|
+
"""iNES format version"""
|
|
25
|
+
UNKNOWN = "Unknown"
|
|
26
|
+
ARCHAIC = "Archaic iNES"
|
|
27
|
+
INES_0_7 = "iNES 0.7"
|
|
28
|
+
INES = "iNES"
|
|
29
|
+
NES_2_0 = "NES 2.0"
|
|
30
|
+
UNIF = "UNIF"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Mirroring(Enum):
|
|
34
|
+
"""Nametable mirroring type"""
|
|
35
|
+
HORIZONTAL = "H" # Vertical arrangement
|
|
36
|
+
VERTICAL = "V" # Horizontal arrangement
|
|
37
|
+
FOUR_SCREEN = "F" # Alternative/4-screen
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TVSystem(Enum):
|
|
41
|
+
"""TV system type"""
|
|
42
|
+
NTSC = "NTSC"
|
|
43
|
+
PAL = "PAL"
|
|
44
|
+
DUAL = "Dual"
|
|
45
|
+
DENDY = "Dendy"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ConsoleType(Enum):
|
|
49
|
+
"""Console type for NES 2.0"""
|
|
50
|
+
NES_FAMICOM = "NES/Famicom"
|
|
51
|
+
VS_SYSTEM = "VS System"
|
|
52
|
+
PLAYCHOICE_10 = "PlayChoice-10"
|
|
53
|
+
EXTENDED = "Extended"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class CPUTiming(Enum):
|
|
57
|
+
"""CPU/PPU timing mode"""
|
|
58
|
+
RP2C02_NTSC = "RP2C02 (NTSC NES)"
|
|
59
|
+
RP2C07_PAL = "RP2C07 (Licensed PAL NES)"
|
|
60
|
+
MULTIPLE_REGION = "Multiple-region"
|
|
61
|
+
UA6538_DENDY = "UA6538 (Dendy)"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class INESHeader:
|
|
65
|
+
"""Parsed iNES header data with full NES 2.0 support"""
|
|
66
|
+
|
|
67
|
+
def __init__(self, header_bytes: bytes):
|
|
68
|
+
if len(header_bytes) < 16:
|
|
69
|
+
raise ValueError(f"Header too short: {len(header_bytes)} bytes (expected 16)")
|
|
70
|
+
|
|
71
|
+
self.raw_header = header_bytes[:16]
|
|
72
|
+
self.format = self._detect_format()
|
|
73
|
+
|
|
74
|
+
if self.format == INESFormat.UNIF:
|
|
75
|
+
return
|
|
76
|
+
|
|
77
|
+
if self.format == INESFormat.UNKNOWN:
|
|
78
|
+
return
|
|
79
|
+
|
|
80
|
+
# Parse common fields (bytes 4-6)
|
|
81
|
+
self.prg_rom_size_lsb = header_bytes[4]
|
|
82
|
+
self.chr_rom_size_lsb = header_bytes[5]
|
|
83
|
+
|
|
84
|
+
# Parse Flags 6
|
|
85
|
+
flags6 = header_bytes[6]
|
|
86
|
+
self.mirroring = self._parse_mirroring(flags6)
|
|
87
|
+
self.has_battery = bool(flags6 & 0x02)
|
|
88
|
+
self.has_trainer = bool(flags6 & 0x04)
|
|
89
|
+
self.four_screen = bool(flags6 & 0x08)
|
|
90
|
+
mapper_low = (flags6 & 0xF0) >> 4
|
|
91
|
+
|
|
92
|
+
# Parse Flags 7
|
|
93
|
+
flags7 = header_bytes[7]
|
|
94
|
+
self.console_type = self._parse_console_type(flags7)
|
|
95
|
+
self.is_vs_unisystem = bool(flags7 & 0x01)
|
|
96
|
+
self.is_playchoice_10 = bool(flags7 & 0x02)
|
|
97
|
+
mapper_mid = (flags7 & 0xF0) >> 4
|
|
98
|
+
|
|
99
|
+
# Initial mapper (will be extended for NES 2.0)
|
|
100
|
+
self.mapper = (mapper_mid << 4) | mapper_low
|
|
101
|
+
self.submapper = None
|
|
102
|
+
|
|
103
|
+
# Parse extended fields based on format
|
|
104
|
+
if self.format == INESFormat.NES_2_0:
|
|
105
|
+
self._parse_nes2_0(header_bytes)
|
|
106
|
+
else:
|
|
107
|
+
# iNES 1.0 size calculation
|
|
108
|
+
self.prg_rom_size = self.prg_rom_size_lsb * 16 * 1024
|
|
109
|
+
self.chr_rom_size = self.chr_rom_size_lsb * 8 * 1024
|
|
110
|
+
self._parse_ines(header_bytes)
|
|
111
|
+
|
|
112
|
+
# Initialize NES 2.0 specific fields to None for iNES 1.0
|
|
113
|
+
if self.format != INESFormat.NES_2_0:
|
|
114
|
+
self.submapper = None
|
|
115
|
+
self.prg_ram_size = None
|
|
116
|
+
self.prg_nvram_size = None
|
|
117
|
+
self.chr_ram_size = None
|
|
118
|
+
self.chr_nvram_size = None
|
|
119
|
+
self.cpu_timing = None
|
|
120
|
+
self.vs_ppu_type = None
|
|
121
|
+
self.vs_hw_type = None
|
|
122
|
+
self.extended_console_type = None
|
|
123
|
+
self.misc_rom_count = None
|
|
124
|
+
self.expansion_device = None
|
|
125
|
+
|
|
126
|
+
def _detect_format(self) -> INESFormat:
|
|
127
|
+
"""Detect iNES format version according to specification"""
|
|
128
|
+
if self.raw_header.startswith(b"UNIF"):
|
|
129
|
+
return INESFormat.UNIF
|
|
130
|
+
|
|
131
|
+
if not self.raw_header.startswith(b"NES\x1a"):
|
|
132
|
+
return INESFormat.UNKNOWN
|
|
133
|
+
|
|
134
|
+
flags7 = self.raw_header[7]
|
|
135
|
+
|
|
136
|
+
# Check for NES 2.0: byte 7 AND $0C = $08
|
|
137
|
+
if (flags7 & 0x0C) == 0x08:
|
|
138
|
+
return INESFormat.NES_2_0
|
|
139
|
+
|
|
140
|
+
# Check for Archaic iNES: byte 7 AND $0C = $04
|
|
141
|
+
if (flags7 & 0x0C) == 0x04:
|
|
142
|
+
return INESFormat.ARCHAIC
|
|
143
|
+
|
|
144
|
+
# Check for iNES: byte 7 AND $0C = $00, and bytes 12-15 are all 0
|
|
145
|
+
if ((flags7 & 0x0C) == 0x00 and
|
|
146
|
+
self.raw_header[12] == 0 and
|
|
147
|
+
self.raw_header[13] == 0 and
|
|
148
|
+
self.raw_header[14] == 0 and
|
|
149
|
+
self.raw_header[15] == 0):
|
|
150
|
+
return INESFormat.INES
|
|
151
|
+
|
|
152
|
+
# Otherwise, iNES 0.7 or archaic iNES
|
|
153
|
+
return INESFormat.INES_0_7
|
|
154
|
+
|
|
155
|
+
def _parse_mirroring(self, flags6: int) -> Mirroring:
|
|
156
|
+
"""Parse mirroring from Flags 6"""
|
|
157
|
+
if flags6 & 0x08: # Four-screen / alternative nametable
|
|
158
|
+
return Mirroring.FOUR_SCREEN
|
|
159
|
+
elif flags6 & 0x01: # Horizontal arrangement (vertical mirroring)
|
|
160
|
+
return Mirroring.VERTICAL
|
|
161
|
+
else: # Vertical arrangement (horizontal mirroring)
|
|
162
|
+
return Mirroring.HORIZONTAL
|
|
163
|
+
|
|
164
|
+
def _parse_console_type(self, flags7: int) -> ConsoleType:
|
|
165
|
+
"""Parse console type from Flags 7"""
|
|
166
|
+
console_bits = flags7 & 0x03
|
|
167
|
+
if console_bits == 0:
|
|
168
|
+
return ConsoleType.NES_FAMICOM
|
|
169
|
+
elif console_bits == 1:
|
|
170
|
+
return ConsoleType.VS_SYSTEM
|
|
171
|
+
elif console_bits == 2:
|
|
172
|
+
return ConsoleType.PLAYCHOICE_10
|
|
173
|
+
else:
|
|
174
|
+
return ConsoleType.EXTENDED
|
|
175
|
+
|
|
176
|
+
def _parse_ines(self, header_bytes: bytes):
|
|
177
|
+
"""Parse iNES-specific fields (bytes 8-10)"""
|
|
178
|
+
# Flags 8: PRG RAM size
|
|
179
|
+
self.prg_ram_size_units = header_bytes[8]
|
|
180
|
+
if self.prg_ram_size_units == 0:
|
|
181
|
+
self.prg_ram_size = 8 * 1024 # 8 KiB for compatibility
|
|
182
|
+
else:
|
|
183
|
+
self.prg_ram_size = self.prg_ram_size_units * 8 * 1024
|
|
184
|
+
|
|
185
|
+
# Flags 9: TV system
|
|
186
|
+
flags9 = header_bytes[9]
|
|
187
|
+
self.tv_system = TVSystem.PAL if (flags9 & 0x01) else TVSystem.NTSC
|
|
188
|
+
|
|
189
|
+
# Flags 10: Extended TV system and bus conflicts
|
|
190
|
+
flags10 = header_bytes[10]
|
|
191
|
+
tv_bits = flags10 & 0x03
|
|
192
|
+
if tv_bits == 2:
|
|
193
|
+
self.tv_system = TVSystem.PAL
|
|
194
|
+
elif tv_bits in [1, 3]:
|
|
195
|
+
self.tv_system = TVSystem.DUAL
|
|
196
|
+
|
|
197
|
+
self.has_prg_ram = not bool(flags10 & 0x10)
|
|
198
|
+
self.has_bus_conflicts = bool(flags10 & 0x20)
|
|
199
|
+
|
|
200
|
+
def _parse_rom_size_nes2_0(self, lsb: int, msb_nibble: int, unit_size: int) -> int:
|
|
201
|
+
"""
|
|
202
|
+
Parse ROM size for NES 2.0 format.
|
|
203
|
+
Supports both simple notation and exponent-multiplier notation.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
lsb: LSB byte (byte 4 for PRG, byte 5 for CHR)
|
|
207
|
+
msb_nibble: MSB nibble from byte 9 (bits 0-3 for PRG, bits 4-7 for CHR)
|
|
208
|
+
unit_size: Size of one unit (16384 for PRG, 8192 for CHR)
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
Size in bytes
|
|
212
|
+
"""
|
|
213
|
+
if msb_nibble <= 0x0E:
|
|
214
|
+
# Simple notation: (msb_nibble << 8) | lsb units
|
|
215
|
+
size_units = (msb_nibble << 8) | lsb
|
|
216
|
+
return size_units * unit_size
|
|
217
|
+
else:
|
|
218
|
+
# Exponent-multiplier notation: msb_nibble = 0xF
|
|
219
|
+
# Format: 1111 EEEE EEMM
|
|
220
|
+
# Exponent is bits 2-7 of lsb, multiplier is bits 0-1 of lsb
|
|
221
|
+
exponent = (lsb >> 2) & 0x3F
|
|
222
|
+
multiplier_raw = lsb & 0x03
|
|
223
|
+
multiplier = multiplier_raw * 2 + 1 # 1, 3, 5, or 7
|
|
224
|
+
|
|
225
|
+
return (2 ** exponent) * multiplier
|
|
226
|
+
|
|
227
|
+
def _parse_nes2_0(self, header_bytes: bytes):
|
|
228
|
+
"""Parse NES 2.0-specific fields (full implementation)"""
|
|
229
|
+
# Byte 8: Mapper MSB and Submapper
|
|
230
|
+
flags8 = header_bytes[8]
|
|
231
|
+
mapper_highest = (flags8 & 0x0F) << 8
|
|
232
|
+
self.mapper = self.mapper | mapper_highest
|
|
233
|
+
self.submapper = (flags8 & 0xF0) >> 4
|
|
234
|
+
|
|
235
|
+
# Byte 9: PRG-ROM and CHR-ROM size MSB
|
|
236
|
+
flags9 = header_bytes[9]
|
|
237
|
+
prg_msb = flags9 & 0x0F
|
|
238
|
+
chr_msb = (flags9 & 0xF0) >> 4
|
|
239
|
+
|
|
240
|
+
# Calculate ROM sizes using NES 2.0 formula
|
|
241
|
+
self.prg_rom_size = self._parse_rom_size_nes2_0(
|
|
242
|
+
self.prg_rom_size_lsb, prg_msb, 16 * 1024
|
|
243
|
+
)
|
|
244
|
+
self.chr_rom_size = self._parse_rom_size_nes2_0(
|
|
245
|
+
self.chr_rom_size_lsb, chr_msb, 8 * 1024
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
# Byte 10: PRG-RAM/EEPROM size
|
|
249
|
+
flags10 = header_bytes[10]
|
|
250
|
+
prg_ram_shift = flags10 & 0x0F
|
|
251
|
+
prg_nvram_shift = (flags10 & 0xF0) >> 4
|
|
252
|
+
|
|
253
|
+
if prg_ram_shift == 0:
|
|
254
|
+
self.prg_ram_size = 0
|
|
255
|
+
else:
|
|
256
|
+
self.prg_ram_size = 64 << prg_ram_shift
|
|
257
|
+
|
|
258
|
+
if prg_nvram_shift == 0:
|
|
259
|
+
self.prg_nvram_size = 0
|
|
260
|
+
else:
|
|
261
|
+
self.prg_nvram_size = 64 << prg_nvram_shift
|
|
262
|
+
|
|
263
|
+
# Byte 11: CHR-RAM size
|
|
264
|
+
flags11 = header_bytes[11]
|
|
265
|
+
chr_ram_shift = flags11 & 0x0F
|
|
266
|
+
chr_nvram_shift = (flags11 & 0xF0) >> 4
|
|
267
|
+
|
|
268
|
+
if chr_ram_shift == 0:
|
|
269
|
+
self.chr_ram_size = 0
|
|
270
|
+
else:
|
|
271
|
+
self.chr_ram_size = 64 << chr_ram_shift
|
|
272
|
+
|
|
273
|
+
if chr_nvram_shift == 0:
|
|
274
|
+
self.chr_nvram_size = 0
|
|
275
|
+
else:
|
|
276
|
+
self.chr_nvram_size = 64 << chr_nvram_shift
|
|
277
|
+
|
|
278
|
+
# Byte 12: CPU/PPU Timing
|
|
279
|
+
flags12 = header_bytes[12]
|
|
280
|
+
timing_mode = flags12 & 0x03
|
|
281
|
+
if timing_mode == 0:
|
|
282
|
+
self.cpu_timing = CPUTiming.RP2C02_NTSC
|
|
283
|
+
self.tv_system = TVSystem.NTSC
|
|
284
|
+
elif timing_mode == 1:
|
|
285
|
+
self.cpu_timing = CPUTiming.RP2C07_PAL
|
|
286
|
+
self.tv_system = TVSystem.PAL
|
|
287
|
+
elif timing_mode == 2:
|
|
288
|
+
self.cpu_timing = CPUTiming.MULTIPLE_REGION
|
|
289
|
+
self.tv_system = TVSystem.DUAL
|
|
290
|
+
else: # timing_mode == 3
|
|
291
|
+
self.cpu_timing = CPUTiming.UA6538_DENDY
|
|
292
|
+
self.tv_system = TVSystem.DENDY
|
|
293
|
+
|
|
294
|
+
# Byte 13: Vs. System Type or Extended Console Type
|
|
295
|
+
flags13 = header_bytes[13]
|
|
296
|
+
if self.console_type == ConsoleType.VS_SYSTEM:
|
|
297
|
+
self.vs_ppu_type = flags13 & 0x0F
|
|
298
|
+
self.vs_hw_type = (flags13 & 0xF0) >> 4
|
|
299
|
+
self.extended_console_type = None
|
|
300
|
+
elif self.console_type == ConsoleType.EXTENDED:
|
|
301
|
+
self.extended_console_type = flags13 & 0x0F
|
|
302
|
+
self.vs_ppu_type = None
|
|
303
|
+
self.vs_hw_type = None
|
|
304
|
+
else:
|
|
305
|
+
self.vs_ppu_type = None
|
|
306
|
+
self.vs_hw_type = None
|
|
307
|
+
self.extended_console_type = None
|
|
308
|
+
|
|
309
|
+
# Byte 14: Miscellaneous ROMs
|
|
310
|
+
flags14 = header_bytes[14]
|
|
311
|
+
self.misc_rom_count = flags14 & 0x03
|
|
312
|
+
|
|
313
|
+
# Byte 15: Default Expansion Device
|
|
314
|
+
flags15 = header_bytes[15]
|
|
315
|
+
self.expansion_device = flags15 & 0x3F
|
|
316
|
+
|
|
317
|
+
# Bus conflicts not applicable in NES 2.0
|
|
318
|
+
self.has_bus_conflicts = False
|
|
319
|
+
|
|
320
|
+
def is_valid(self) -> bool:
|
|
321
|
+
"""Check if this is a valid, parseable iNES file"""
|
|
322
|
+
return self.format not in [INESFormat.UNKNOWN, INESFormat.UNIF, INESFormat.ARCHAIC]
|
|
323
|
+
|
|
324
|
+
def get_mapper_name(self) -> str:
|
|
325
|
+
"""Get the primary mapper name from database"""
|
|
326
|
+
return get_mapper_name(self.mapper)
|
|
327
|
+
|
|
328
|
+
def get_mapper_info(self) -> Dict[str, Any]:
|
|
329
|
+
"""Get complete mapper information"""
|
|
330
|
+
return get_mapper_info(self.mapper)
|
|
331
|
+
|
|
332
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
333
|
+
"""Convert header to dictionary for easy access"""
|
|
334
|
+
if not self.is_valid():
|
|
335
|
+
return {
|
|
336
|
+
'format': self.format.value,
|
|
337
|
+
'valid': False
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
mapper_info = self.get_mapper_info()
|
|
341
|
+
|
|
342
|
+
result = {
|
|
343
|
+
'format': self.format.value,
|
|
344
|
+
'valid': True,
|
|
345
|
+
'prg_rom_size': self.prg_rom_size,
|
|
346
|
+
'chr_rom_size': self.chr_rom_size,
|
|
347
|
+
'prg_rom_size_kib': self.prg_rom_size // 1024,
|
|
348
|
+
'chr_rom_size_kib': self.chr_rom_size // 1024,
|
|
349
|
+
'mapper': self.mapper,
|
|
350
|
+
'mapper_name': mapper_info['name'],
|
|
351
|
+
'mapper_alternates': mapper_info['alternates'],
|
|
352
|
+
'mapper_notes': mapper_info['notes'],
|
|
353
|
+
'mirroring': self.mirroring.value,
|
|
354
|
+
'has_battery': self.has_battery,
|
|
355
|
+
'has_trainer': self.has_trainer,
|
|
356
|
+
'four_screen': self.four_screen,
|
|
357
|
+
'is_vs_unisystem': self.is_vs_unisystem,
|
|
358
|
+
'is_playchoice_10': self.is_playchoice_10,
|
|
359
|
+
'console_type': self.console_type.value,
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
# Add format-specific fields
|
|
363
|
+
if hasattr(self, 'prg_ram_size') and self.prg_ram_size is not None:
|
|
364
|
+
result['prg_ram_size'] = self.prg_ram_size
|
|
365
|
+
if hasattr(self, 'tv_system'):
|
|
366
|
+
result['tv_system'] = self.tv_system.value
|
|
367
|
+
|
|
368
|
+
# NES 2.0 specific fields
|
|
369
|
+
if self.format == INESFormat.NES_2_0:
|
|
370
|
+
if self.submapper is not None:
|
|
371
|
+
result['submapper'] = self.submapper
|
|
372
|
+
if self.prg_nvram_size is not None:
|
|
373
|
+
result['prg_nvram_size'] = self.prg_nvram_size
|
|
374
|
+
if self.chr_ram_size is not None:
|
|
375
|
+
result['chr_ram_size'] = self.chr_ram_size
|
|
376
|
+
if self.chr_nvram_size is not None:
|
|
377
|
+
result['chr_nvram_size'] = self.chr_nvram_size
|
|
378
|
+
if self.cpu_timing is not None:
|
|
379
|
+
result['cpu_timing'] = self.cpu_timing.value
|
|
380
|
+
if self.vs_ppu_type is not None:
|
|
381
|
+
result['vs_ppu_type'] = self.vs_ppu_type
|
|
382
|
+
if self.vs_hw_type is not None:
|
|
383
|
+
result['vs_hw_type'] = self.vs_hw_type
|
|
384
|
+
if self.extended_console_type is not None:
|
|
385
|
+
result['extended_console_type'] = self.extended_console_type
|
|
386
|
+
if self.misc_rom_count is not None:
|
|
387
|
+
result['misc_rom_count'] = self.misc_rom_count
|
|
388
|
+
if self.expansion_device is not None:
|
|
389
|
+
result['expansion_device'] = self.expansion_device
|
|
390
|
+
|
|
391
|
+
# iNES 1.0 specific fields
|
|
392
|
+
if hasattr(self, 'has_bus_conflicts'):
|
|
393
|
+
result['has_bus_conflicts'] = self.has_bus_conflicts
|
|
394
|
+
|
|
395
|
+
return result
|
|
396
|
+
|
|
397
|
+
def __str__(self) -> str:
|
|
398
|
+
"""Human-readable string representation (minimal info)"""
|
|
399
|
+
if self.format == INESFormat.UNIF:
|
|
400
|
+
return "UNIF format (not supported)"
|
|
401
|
+
if self.format == INESFormat.UNKNOWN:
|
|
402
|
+
return "Not an iNES file"
|
|
403
|
+
if self.format == INESFormat.ARCHAIC:
|
|
404
|
+
return "Archaic iNES (not supported)"
|
|
405
|
+
|
|
406
|
+
# Minimal output: mapper name, PRG size, and CHR size if present
|
|
407
|
+
mapper_name = self.get_mapper_name()
|
|
408
|
+
parts = [f"mapper: {self.mapper} ({mapper_name})"]
|
|
409
|
+
parts.append(f"PRG: {self.prg_rom_size // 1024} KiB")
|
|
410
|
+
|
|
411
|
+
if self.chr_rom_size > 0:
|
|
412
|
+
parts.append(f"CHR: {self.chr_rom_size // 1024} KiB")
|
|
413
|
+
|
|
414
|
+
return ", ".join(parts)
|
|
415
|
+
|
|
416
|
+
def detailed_str(self) -> str:
|
|
417
|
+
"""Detailed human-readable string representation"""
|
|
418
|
+
if not self.is_valid():
|
|
419
|
+
return str(self)
|
|
420
|
+
|
|
421
|
+
mapper_info = self.get_mapper_info()
|
|
422
|
+
|
|
423
|
+
# Full output with all fields
|
|
424
|
+
parts = [
|
|
425
|
+
f"mapper: {self.mapper} ({mapper_info['name']})",
|
|
426
|
+
f"mirroring: {self.mirroring.value}",
|
|
427
|
+
f"PRG ROM: {self.prg_rom_size // 1024} KiB",
|
|
428
|
+
f"CHR ROM: {self.chr_rom_size // 1024} KiB"
|
|
429
|
+
]
|
|
430
|
+
|
|
431
|
+
# Mapper info
|
|
432
|
+
if mapper_info['alternates']:
|
|
433
|
+
parts.append(f"Alt names: {', '.join(mapper_info['alternates'])}")
|
|
434
|
+
if mapper_info['notes']:
|
|
435
|
+
parts.append(f"Notes: {mapper_info['notes']}")
|
|
436
|
+
|
|
437
|
+
# NES 2.0 specific fields
|
|
438
|
+
if self.format == INESFormat.NES_2_0:
|
|
439
|
+
if self.submapper is not None:
|
|
440
|
+
parts.append(f"Submapper: {self.submapper}")
|
|
441
|
+
if self.prg_ram_size and self.prg_ram_size > 0:
|
|
442
|
+
parts.append(f"PRG RAM: {self.prg_ram_size // 1024} KiB")
|
|
443
|
+
if self.prg_nvram_size and self.prg_nvram_size > 0:
|
|
444
|
+
parts.append(f"PRG NVRAM: {self.prg_nvram_size // 1024} KiB")
|
|
445
|
+
if self.chr_ram_size and self.chr_ram_size > 0:
|
|
446
|
+
parts.append(f"CHR RAM: {self.chr_ram_size // 1024} KiB")
|
|
447
|
+
if self.chr_nvram_size and self.chr_nvram_size > 0:
|
|
448
|
+
parts.append(f"CHR NVRAM: {self.chr_nvram_size // 1024} KiB")
|
|
449
|
+
if self.cpu_timing:
|
|
450
|
+
parts.append(f"CPU Timing: {self.cpu_timing.value}")
|
|
451
|
+
if self.console_type == ConsoleType.VS_SYSTEM:
|
|
452
|
+
if self.vs_ppu_type is not None:
|
|
453
|
+
parts.append(f"VS PPU Type: {self.vs_ppu_type}")
|
|
454
|
+
if self.vs_hw_type is not None:
|
|
455
|
+
parts.append(f"VS HW Type: {self.vs_hw_type}")
|
|
456
|
+
if self.extended_console_type is not None:
|
|
457
|
+
parts.append(f"Extended Console: {self.extended_console_type}")
|
|
458
|
+
if self.misc_rom_count and self.misc_rom_count > 0:
|
|
459
|
+
parts.append(f"Misc ROMs: {self.misc_rom_count}")
|
|
460
|
+
|
|
461
|
+
# Add optional fields
|
|
462
|
+
if self.has_trainer:
|
|
463
|
+
parts.append("Trainer: Yes")
|
|
464
|
+
if self.has_battery:
|
|
465
|
+
parts.append("Battery: Yes")
|
|
466
|
+
if self.is_vs_unisystem:
|
|
467
|
+
parts.append("VS Unisystem")
|
|
468
|
+
if self.is_playchoice_10:
|
|
469
|
+
parts.append("PlayChoice-10")
|
|
470
|
+
if hasattr(self, 'tv_system'):
|
|
471
|
+
parts.append(f"TV System: {self.tv_system.value}")
|
|
472
|
+
if hasattr(self, 'has_bus_conflicts') and self.format != INESFormat.NES_2_0:
|
|
473
|
+
parts.append(f"Bus Conflicts: {self.has_bus_conflicts}")
|
|
474
|
+
|
|
475
|
+
return ", ".join(parts)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def parse_ines_header(header_bytes: bytes) -> Optional[INESHeader]:
|
|
479
|
+
"""
|
|
480
|
+
Parse iNES header from bytes
|
|
481
|
+
|
|
482
|
+
Args:
|
|
483
|
+
header_bytes: First 16+ bytes of the ROM file
|
|
484
|
+
|
|
485
|
+
Returns:
|
|
486
|
+
INESHeader object or None if parsing failed
|
|
487
|
+
"""
|
|
488
|
+
try:
|
|
489
|
+
return INESHeader(header_bytes)
|
|
490
|
+
except (ValueError, TypeError, IndexError):
|
|
491
|
+
# Treat malformed or incomplete headers as parse failures.
|
|
492
|
+
return None
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ines-parser
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: A Python library and utilities for parsing iNES format NES ROM files
|
|
5
|
+
Author: Leonid Kabanov
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ponchick/ines-parser
|
|
8
|
+
Project-URL: Documentation, https://github.com/ponchick/ines-parser#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/ponchick/ines-parser.git
|
|
10
|
+
Project-URL: Issues, https://github.com/ponchick/ines-parser/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/ponchick/ines-parser/releases
|
|
12
|
+
Keywords: nes,ines,rom,parser,retro,gaming,emulation
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Games/Entertainment
|
|
21
|
+
Classifier: Topic :: System :: Archiving
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Provides-Extra: archive
|
|
26
|
+
Requires-Dist: libarchive-c>=4.0; extra == "archive"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# iNES Parser
|
|
30
|
+
|
|
31
|
+
Lightweight Python library and CLI tools for working with NES ROMs in iNES/NES 2.0 format.
|
|
32
|
+
|
|
33
|
+
Supported versions: Python 3.10-3.14.
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install ines-parser
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Optional extra for archive formats (`.7z`/`.zip`/`.rar`): `pip install "ines-parser[archive]"`. Otherwise the library has no extra dependencies.
|
|
42
|
+
|
|
43
|
+
## What It Does
|
|
44
|
+
|
|
45
|
+
- parses ROM headers (`mapper`, PRG/CHR sizes, mirroring, and more)
|
|
46
|
+
- scans ROM directories with filters
|
|
47
|
+
- splits ROM files into `PRG` and `CHR` binaries
|
|
48
|
+
|
|
49
|
+
## CLI
|
|
50
|
+
|
|
51
|
+
Tools live in **`ines_parser/cli/`** as **`ines_scan_roms.py`** and **`ines_split_rom.py`**. The names match **`pip install`** entry points on your `PATH`: **`ines_scan_roms.py`** and **`ines_split_rom.py`**.
|
|
52
|
+
|
|
53
|
+
**Without installing**, from the repository root you can use either `-m` or the path to the same files:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
python -m ines_parser.cli.ines_scan_roms /path/to/roms
|
|
57
|
+
python ines_parser/cli/ines_scan_roms.py /path/to/roms
|
|
58
|
+
|
|
59
|
+
python -m ines_parser.cli.ines_split_rom game.nes
|
|
60
|
+
python ines_parser/cli/ines_split_rom.py game.nes
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**After `pip install`,** run the same logical names as commands (they are small wrappers, not copies of the sources):
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
ines_scan_roms.py /path/to/roms
|
|
67
|
+
ines_split_rom.py game.nes
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Scan ROMs
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
ines_scan_roms.py /path/to/roms
|
|
74
|
+
ines_scan_roms.py /path/to/roms --mapper 4
|
|
75
|
+
ines_scan_roms.py /path/to/roms --show-all
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Useful filters:
|
|
79
|
+
|
|
80
|
+
- `--mapper N`
|
|
81
|
+
- `--mirroring H|V|F`
|
|
82
|
+
- `--has-trainer`
|
|
83
|
+
- `--min-prg KiB`, `--max-prg KiB`
|
|
84
|
+
- `--min-chr KiB`, `--max-chr KiB`
|
|
85
|
+
|
|
86
|
+
### Split ROM (PRG / CHR)
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
ines_split_rom.py game.nes
|
|
90
|
+
ines_split_rom.py roms.7z
|
|
91
|
+
ines_split_rom.py game.nes --force
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
By default, existing output files are not overwritten without confirmation.
|
|
95
|
+
|
|
96
|
+
## Library Usage
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from ines_parser import parse_ines_header
|
|
100
|
+
|
|
101
|
+
with open("game.nes", "rb") as f:
|
|
102
|
+
header = parse_ines_header(f.read(16))
|
|
103
|
+
|
|
104
|
+
if header and header.is_valid():
|
|
105
|
+
print(header) # compact output
|
|
106
|
+
print(header.detailed_str()) # full output
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Format Docs
|
|
110
|
+
|
|
111
|
+
- Details: `docs/iNES.md`
|
|
112
|
+
- Spec: [NESdev iNES](https://www.nesdev.org/wiki/INES)
|
|
113
|
+
- NES 2.0: [NESdev NES 2.0](https://www.nesdev.org/wiki/NES_2.0)
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT, see `LICENSE`.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
ines_parser/__init__.py,sha256=uD6e-XaaRSMFQjEgrGGABgUOWJiojvG8Mr3oQPaLXng,858
|
|
2
|
+
ines_parser/mappers.py,sha256=Pqq9PgiFrK6aLwDk1Q-LjrL4VuC-veuROIuX-VXRfxc,4705
|
|
3
|
+
ines_parser/parser.py,sha256=JSdwy_mXXYQW8bNwXHiUlDptWMHKFVv2NlWU_4vX464,18086
|
|
4
|
+
ines_parser/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
ines_parser/cli/ines_scan_roms.py,sha256=O4c_tqpFhx4FWyNhHC8aLNjt48KGn19Pp_oPurUAqzI,18199
|
|
6
|
+
ines_parser/cli/ines_split_rom.py,sha256=lOkbdSVTSY5FtHihQj8GixpsvUodeFtHtC-jQ2GgRdQ,12152
|
|
7
|
+
ines_parser-1.2.0.dist-info/licenses/LICENSE,sha256=Vi3cItkblr6fZwGbNlp_HnBaMFwXSWYPkrVQLXX3LCs,1057
|
|
8
|
+
ines_parser-1.2.0.dist-info/METADATA,sha256=W6jP5qanaKLgVA9P00rQKFHVUR5dzwcEBef_-uo0XEo,3439
|
|
9
|
+
ines_parser-1.2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
10
|
+
ines_parser-1.2.0.dist-info/entry_points.txt,sha256=LGUoN33uwfrW05JRmVjB9T0H-0luA-Ho4SHTUfgqvlw,130
|
|
11
|
+
ines_parser-1.2.0.dist-info/top_level.txt,sha256=MU-M-ICxJCJ-E5lHa0sddEAm8Cl4VcXwCN0rBAWgth8,12
|
|
12
|
+
ines_parser-1.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ines_parser
|