Skip to content

Instantly share code, notes, and snippets.

@Optiroc
Created February 3, 2024 18:00
Show Gist options
  • Save Optiroc/20135302cd2b3692f9b0f41d59be4a8e to your computer and use it in GitHub Desktop.
Save Optiroc/20135302cd2b3692f9b0f41d59be4a8e to your computer and use it in GitHub Desktop.
Convert Sega Saturn save files between MiSTer and Mednafen format
#!/usr/bin/env python3
import argparse
import os
import struct
import sys
def read_chunks(f, length):
while True:
data = f.read(length)
if not data: break
yield data
def main():
parser = argparse.ArgumentParser()
parser.add_argument(dest="in_file", metavar="IN_FILE", help="input file")
parser.add_argument(dest="out_file", metavar="OUT_FILE", help="output file")
try:
args = parser.parse_args()
in_size = os.path.getsize(args.in_file)
if in_size == 0x10000:
print("converting Saturn_MiSTer save to Mednafen format...", args.in_file, "->", args.out_file)
out = bytes()
with open(args.in_file, "rb") as in_fh:
for _, word in enumerate(read_chunks(in_fh, 2)):
out += struct.pack("<B", word[1])
with open(args.out_file, "wb") as out_fh:
out_fh.write(out)
elif in_size == 0X8000:
print("converting Mednafen save to Saturn_MiSTer format...", args.in_file, "->", args.out_file)
out = bytes()
with open(args.in_file, "rb") as in_fh:
for _, byte in enumerate(read_chunks(in_fh, 1)):
out += struct.pack(">H", byte[0])
with open(args.out_file, "wb") as out_fh:
out_fh.write(out)
else:
raise IOError("unrecognized save format in file '{}'".format(args.in_file))
return 0
# except Exception as err:
except IOError as err:
print("error - {}".format(err))
sys.exit(1)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment