Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ======================================================================
- # GEMS extract
- # Extracts GEMS sound data from ROM with the help of
- # a snapshot of the z80 while it was running
- # (saved as a .ram file)
- #
- # Usage: gemsextract.py ROM_FILE
- # ram file must have the same filename as ROM
- # ======================================================================
- import sys
- import os.path
- # ======================================================================
- # -------------------------------------------------
- # Settings
- # -------------------------------------------------
- # Guess the size of each sound data sections since there's no way
- # to detect the size of each one
- #
- # change these values as you like.
- SIZE_GUESS_PAT = 0x20000
- SIZE_GUESS_ENV = 0x20000
- SIZE_GUESS_SEQ = 0x20000
- SIZE_GUESS_SMP = 0x80000
- # ======================================================================
- # -------------------------------------------------
- # Init
- # -------------------------------------------------
- if len(sys.argv) == 1:
- print("ARGS: inputfile [pattnumloop]")
- exit()
- if os.path.exists(sys.argv[1]) == False:
- print("File not found")
- exit()
- MASTERNAME = sys.argv[1][:-4]
- input_file = open(sys.argv[1],"rb")
- input_ram = open(MASTERNAME+".ram","rb")
- out_pat = open("output/"+MASTERNAME+"_PBANK.bin","wb")
- out_env = open("output/"+MASTERNAME+"_MBANK.bin","wb")
- out_seq = open("output/"+MASTERNAME+"_SBANK.bin","wb")
- out_smp = open("output/"+MASTERNAME+"_DBANK.bin","wb")
- out_drv = open("output/"+MASTERNAME+"_Z80.bin","wb")
- # ======================================================================
- # -------------------------------------------------
- # Start
- # -------------------------------------------------
- # search and extract driver
- romPos=0
- romSearch=True
- print("Searching for driver in ROM...")
- while romSearch:
- input_file.seek(romPos)
- a = ord(input_file.read(1)) << 24
- b = ord(input_file.read(1)) << 16
- c = ord(input_file.read(1)) << 8
- d = ord(input_file.read(1))
- romPos += 1
- e = a+b+c+d
- if e == 0xF3ED5631: # magic value
- input_file.seek(-4,1) # 2 bytes next
- print("FOUND driver at:",hex(input_file.tell())+"\n")
- drvStart=input_file.tell()
- romSearch=False
- # search end
- romSearch=True
- print("Searching for driver in ROM...")
- while romSearch:
- input_file.seek(romPos)
- a = ord(input_file.read(1)) << 24
- b = ord(input_file.read(1)) << 16
- c = ord(input_file.read(1)) << 8
- d = ord(input_file.read(1))
- romPos += 1
- e = a+b+c+d
- if e == 0x77007CC9:
- drvEnd=input_file.tell()
- romSearch=False
- #print(hex(drvStart),hex(drvEnd))
- drvLen=drvEnd-drvStart
- input_file.seek(drvStart)
- for a in range(drvLen):
- a = ord(input_file.read(1)) & 0xFF
- out_drv.write(bytes([a]))
- # search por pointers
- ramPos=0
- ramSearch=True
- print("Searching for pointers in Z80RAM...")
- while ramSearch:
- input_ram.seek(ramPos)
- a = ord(input_ram.read(1)) << 16
- b = ord(input_ram.read(1)) << 8
- c = ord(input_ram.read(1))
- ramPos += 1
- d = a+b+c
- if d == 0x10F9C3: # magic value
- input_ram.seek(2,True) # 2 bytes next
- print("FOUND pointers at:",hex(input_ram.tell()))
- ramSearch=False
- a = ord(input_ram.read(1))&0xFF
- b = (ord(input_ram.read(1))&0xFF) << 8
- c = (ord(input_ram.read(1))&0xFF) << 16
- addrPAT = a|b|c
- a = ord(input_ram.read(1))&0xFF
- b = (ord(input_ram.read(1))&0xFF) << 8
- c = (ord(input_ram.read(1))&0xFF) << 16
- addrENV = a|b|c
- a = ord(input_ram.read(1))&0xFF
- b = (ord(input_ram.read(1))&0xFF) << 8
- c = (ord(input_ram.read(1))&0xFF) << 16
- addrSEQ = a|b|c
- a = ord(input_ram.read(1))&0xFF
- b = (ord(input_ram.read(1))&0xFF) << 8
- c = (ord(input_ram.read(1))&0xFF) << 16
- addrSMP = a|b|c
- print("PATCH: ",hex(addrPAT))
- print("ENVELOPE:",hex(addrENV))
- print("SEQUENCE:",hex(addrSEQ))
- print("SAMPLES: ",hex(addrSMP))
- input_file.seek(addrPAT)
- for b in range(SIZE_GUESS_PAT):
- a = ord(input_file.read(1))&0xFF
- out_pat.write(bytes([a]))
- input_file.seek(addrENV)
- for b in range(SIZE_GUESS_ENV):
- a = ord(input_file.read(1))&0xFF
- out_env.write(bytes([a]))
- input_file.seek(addrSEQ)
- for b in range(SIZE_GUESS_SEQ):
- a = ord(input_file.read(1))&0xFF
- out_seq.write(bytes([a]))
- input_file.seek(addrSMP)
- for b in range(SIZE_GUESS_SMP):
- a = ord(input_file.read(1))&0xFF
- out_smp.write(bytes([a]))
- # ----------------------------
- # End
- # ----------------------------
- input_file.close()
- input_ram.close()
- out_pat.close()
- out_env.close()
- out_seq.close()
- out_smp.close()
- out_drv.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement