Skip to content

Instantly share code, notes, and snippets.

@Nisto
Created September 13, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nisto/181017e480ed2551a66fec4a5f113bb4 to your computer and use it in GitHub Desktop.
Save Nisto/181017e480ed2551a66fec4a5f113bb4 to your computer and use it in GitHub Desktop.
mkpsfvab.py
import os
import sys
import struct
import zlib
import glob
#
# CHANGE THESE VARIABLES AS NEEDED
#
SEQ_ADDR = 0x80100000
VH_ADDR = 0x80120000
VB_ADDR = 0x80140000
PSF_TAGS = bytes("[TAG]%s\n" % "\n".join([
"utf8=1",
"game=...",
"artist=...",
"genre=...",
"year=...",
"copyright=...",
"psfby=..."
]), encoding="utf-8")
def writemem(exebuf, addr, data):
text_start = struct.unpack("<I", exebuf[0x18:0x1C])[0]
offset = 0x800 + (addr - text_start)
exebuf[offset:offset+len(data)] = data
psf_header_t = struct.Struct("<4sIII")
def exe2psf(exebuf):
zbuf = zlib.compress(exebuf, 9)
header = psf_header_t.pack(
b"PSF\x01", 0, len(zbuf), zlib.crc32(zbuf)
)
return header + zbuf + PSF_TAGS
def main(argc=len(sys.argv), argv=sys.argv):
if argc != 2:
input("Usage: %s <input_dir>" % argv[0])
return 1
dir_in = os.path.realpath(argv[1])
if not os.path.isdir(dir_in):
input("ERROR: Directory path invalid")
return 1
dir_out = os.path.join(dir_in, "out")
if not os.path.isdir(dir_out):
os.makedirs(dir_out)
drv_path = os.path.join(dir_in, "driver.exe")
if not os.path.isfile(drv_path):
input("ERROR: Driver not found!")
return 1
with open(drv_path, "rb") as drv:
drvbuf = bytearray( drv.read() )
seq_glob = os.path.join(dir_in, "*.seq")
for seq_path in glob.glob(seq_glob):
stem = os.path.splitext(seq_path)[0]
vh_path = "%s.vh" % stem
if not os.path.isfile(vh_path):
input("ERROR: VH not found for %s" % seq_path)
return 1
vb_path = "%s.vb" % stem
if not os.path.isfile(vb_path):
input("ERROR: VB not found for %s" % seq_path)
return 1
exebuf = drvbuf[:]
with open(seq_path, "rb") as seq:
writemem( exebuf, SEQ_ADDR, seq.read() )
with open(vh_path, "rb") as vh:
writemem( exebuf, VH_ADDR, vh.read() )
with open(vb_path, "rb") as vb:
writemem( exebuf, VB_ADDR, vb.read() )
name = os.path.basename(stem)
psf_path = os.path.join(dir_out, "%s.psf" % name)
with open(psf_path, "wb") as psf:
psf.write( exe2psf(exebuf) )
return 0
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment