Skip to content

Instantly share code, notes, and snippets.

@Screwtapello
Created April 16, 2012 12:25
Show Gist options
  • Save Screwtapello/2398367 to your computer and use it in GitHub Desktop.
Save Screwtapello/2398367 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import os
import re
from xml.etree import ElementTree as ET
SAVE_TYPE_RE = re.compile(r"""
EEPROM_V\d{3} |
SRAM_V\d{3} |
SRAM_F_V\d{3} |
FLASH_V\d{3} |
FLASH512_V\d{3} |
FLASH1M_V\d{3}
""", re.VERBOSE)
def pick_file_type(filename):
with open(filename, 'rb') as handle:
data = handle.read()
results = set(SAVE_TYPE_RE.findall(data))
if len(results) == 0:
return None
elif len(results) > 1:
raise RuntimeError("Multiple save-RAM codes:", results)
return results.pop()
def kbit_to_bytes(kbit):
return kbit * 1024 / 8
def element_for_savetype(savetype):
res = ET.Element("ram")
if savetype.startswith("EEPROM"):
res.set('type', "EEPROM")
res.set('size', str(kbit_to_bytes(64)))
elif savetype.startswith("SRAM"):
res.set('type', "SRAM")
res.set('size', str(kbit_to_bytes(256)))
elif savetype.startswith("FLASH1M"):
res.set('type', "FlashROM")
res.set('id', "0x09c2")
res.set('size', str(kbit_to_bytes(1024)))
elif savetype.startswith("FLASH"):
# FLASH and FLASH512
res.set('type', "FlashROM")
res.set('id', "0x09c2")
res.set('size', str(kbit_to_bytes(512)))
else:
raise ValueError("Unrecognised save type", savetype)
return res
def build_manifest(savetype):
res = ET.Element("cartridge")
if savetype is not None:
res.append(element_for_savetype(savetype))
return res
def write_manifest_for_file(filename):
base, ext = os.path.splitext(filename)
manifest_name = base + ".xml"
savetype = pick_file_type(filename)
manifest_xml = ET.ElementTree(build_manifest(savetype))
manifest_xml.write(manifest_name)
if __name__ == "__main__":
write_manifest_for_file(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment