Skip to content

Instantly share code, notes, and snippets.

@SukkoPera
Last active July 19, 2023 14:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SukkoPera/4a9b7f3ce8edffea8bb8ffadbc6eee68 to your computer and use it in GitHub Desktop.
Save SukkoPera/4a9b7f3ce8edffea8bb8ffadbc6eee68 to your computer and use it in GitHub Desktop.
Split an Amiga ROM file into even/odd words
#!/usr/bin/env python
import os
import sys
import re
REGEXP = "AMIGA"
BUFSIZE = 4096
ODD_SUFFIX = "_odd_hi_uXa"
EVEN_SUFFIX = "_even_low_uXb"
if len (sys.argv) != 2:
print "Usage: %s <romfile>" % sys.argv[0]
sys.exit (1)
fn_in = sys.argv[1]
split = os.path.splitext (fn_in)
fn_odd = split[0] + ODD_SUFFIX + split[1]
fn_even = split[0] + EVEN_SUFFIX + split[1]
print ("Writing to %s and %s..." % (fn_odd, fn_even))
warningIssued = False
with open (fn_in) as fin, open (fn_odd, "w") as fodd, open (fn_even, "w") as feven:
while True:
buf = fin.read (BUFSIZE)
if not buf:
break
if not warningIssued and re.search (REGEXP, buf, re.IGNORECASE):
print ("WARNING: ROM does not look byteswapped, be careful!")
warningIssued = True
pairs = zip (buf[::2], buf[1::2])
odd = pairs[0::2]
even = pairs[1::2]
for pair in odd:
fodd.write (bytearray (pair))
for pair in even:
feven.write (bytearray (pair))
@krickert
Copy link

Like 13 - missing parens. Like 26 - problem with python because it defaults to utf-8 codec. I'm not a python guy though, so trying to figure out how to not make the split be on text but on binary instead

@SukkoPera
Copy link
Author

SukkoPera commented Jul 19, 2023

This was written for Python 2, try running it through the 2to3 tool.

Change w to wb and add rb in the other call for binary mode.

@krickert
Copy link

Thanks! I'll try in a bit. I used a tool written in C on OSX for now. It also does CRC checksums of a lot of the ROMs provided, so it'll ensure that it's not a wrong one. Pretty cool stuff.

@SukkoPera
Copy link
Author

Yeah, this was a quiiiick'n'dirty hack I wrote maaaany years ago (5+), I'm sure there's better stuff around but I could not find anything at the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment