Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. ## BizHawk CDL to IDC converter - Sega GameGear/MasterSystem
  2. ## By : ehw
  3.  
  4. import sys
  5.  
  6. ##Variables:
  7.    
  8. inputfile = ''
  9. outputfile = ''
  10.  
  11. cdl_ROMseg_addr = 0x2A ##might not be the same every time, and for future versions
  12. cdl_ROMseg_endbyte = '\x08\x4d\x61\x69' ###.Mai, or, the beginning of the Main RAM segment.
  13.  
  14. ## MAIN CODE START
  15.  
  16. def main(argv):
  17.     print("BizHawk CDL to IDC converter - Sega GameGear/MasterSystem\n")
  18.     print("By : ehw\n")
  19.     print("\t Usage: smsgg_cdl2idc.py input.cdl output.idc\n")
  20.  
  21. ##2.) Check and make sure the user specified both input/output files
  22.     if(len(sys.argv) < 2):
  23.         print("\t Usage: smsgg_cdl2idc.py input.cdl output.idc\n")
  24.         sys.exit(2)
  25. ##3.) Once the check has passed, get and then parse the cdl file
  26.     inputfile = open(sys.argv[1], 'rb')
  27.     outputfile = open(sys.argv[2], 'w')
  28.     init_idc(outputfile)
  29.     parse_cdl(inputfile, outputfile)
  30.    
  31.     print("Program End")
  32.     inputfile.close();
  33.     outputfile.close();
  34.     sys.exit(2)
  35.    
  36. ## MAIN CODE END
  37. def parse_cdl(inputfile, outputfile):
  38.     ##Flags:
  39.     ExecFirst_flag = '\x01'
  40.     ExecOperand_flag = '\x02'  
  41.     Data_flag= '\x04'
  42.  
  43.     ExecFirst_Count = 0
  44.     Data_Count = 0
  45.     ExecOperand_Count = 0
  46.     Unknown_Count = 0
  47.  
  48.     ##1.) Get the ROM segment size from the CDL file
  49.     rom_size = get_romseg_size(inputfile, cdl_ROMseg_endbyte, cdl_ROMseg_addr)
  50.  
  51.     ##2.) Set the offset to the ROM segment
  52.     print("Conversion Process Start!")
  53.     print("Parsing ROM segment at {:x}".format(cdl_ROMseg_addr))
  54.     inputfile.seek(cdl_ROMseg_addr)
  55.    
  56.     ##3.) Scan through the cdl file one byte at a time,
  57.     currentAddress = cdl_ROMseg_addr
  58.     while(currentAddress != cdl_ROMseg_addr+rom_size):
  59.         if(inputfile.read(1) == ExecFirst_flag):
  60.             outputfile.write("MakeCode(0x{:x});\n".format(currentAddress-cdl_ROMseg_addr))
  61.             ExecFirst_Count += 1
  62.         if(inputfile.read(1) == Data_flag):
  63.             outputfile.write("MakeData(0x{:x}, FF_BYTE, 0x1, 0);\n".format(currentAddress-cdl_ROMseg_addr))
  64.             Data_Count += 1
  65.         if(inputfile.read(1) == ExecOperand_flag):
  66.             outputfile.write("MakeCode(0x{:x});\n".format(currentAddress-cdl_ROMseg_addr))
  67.             ExecOperand_Count += 1
  68.         if(inputfile.read(1) == '\x00'):
  69.             Unknown_Count += 1
  70.         currentAddress += 1
  71.         inputfile.seek(currentAddress)
  72.  
  73.     print("ROM segment parsing complete!")
  74.     ##DO Main RAM STUFF HERE
  75.     cdl_RAMseg_addr = currentAddress + 13 ##since currentAddress is now at the end of the ROM segment, we can just add 12 to get to the next RAM segment since the segment header size is fixed
  76.     ram_size = 8192 ##ram is always the same size no matter what
  77.  
  78.     ##Set the offset in the filestream to the beginning of the RAM segment
  79.     inputfile.seek(cdl_RAMseg_addr)
  80.     currentAddress = cdl_RAMseg_addr
  81.     print("Parsing Main RAM segment at {:x}".format(cdl_RAMseg_addr))
  82.     ##Note: I can probably truncate this to make it more compact but the write out to
  83.     ## the idc file is going to be different for RAM depending on the usage of the game
  84.     ## so more different flags might be set in ram in some games than others. Also,
  85.     ## the write out to the idc file needs to add an additional string for RAM space
  86.     ##
  87.     ##Note 2: The RAM segment in IDA Pro might need to be created with the proper base
  88.     ## address before this script is run.
  89.     while(currentAddress != cdl_RAMseg_addr+ram_size):
  90.         if(inputfile.read(1) == ExecFirst_flag):
  91.             outputfile.write("MakeCode(0xFF{0:0{1}x});\n".format(currentAddress-cdl_RAMseg_addr,4))
  92.             ExecFirst_Count += 1
  93.         if(inputfile.read(1) == Data):
  94.             outputfile.write("MakeData(0xFF{0:0{1}x}, FF_BYTE, 0x1, 0);\n".format(currentAddress-cdl_RAMseg_addr,4))
  95.             Data_Count += 1
  96.         if(inputfile.read(1) == ExecOperand_flag):
  97.             outputfile.write("MakeCode(0xFF{0:0{1}x});\n".format(currentAddress-cdl_RAMseg_addr,4))
  98.             ExecOperand_Count += 1
  99.         if(inputfile.read(1) == '\x00'):
  100.             Unknown_Count += 1
  101.         currentAddress += 1
  102.         inputfile.seek(currentAddress)
  103.  
  104.     print("Main RAM segment parsing complete!")
  105.  
  106.  
  107.     outputfile.write("}") ##close the idc script, we're done
  108.     print("Conversion completed!\n")
  109.     print("Identified Data:\n")
  110.     print("\t ExecFirst : {:x} bytes".format(ExecFirst_Count))
  111.     print("\t ExecOperand : {:x} bytes".format(ExecOperand_Count))
  112.     print("\t Data : {:x} bytes".format(Data_Count))
  113.     print("\t Unknown : {:x} bytes".format(Unknown_Count))
  114.  
  115.     return
  116.  
  117. def get_romseg_size(inputfile, cdl_ROMseg_endbyte, cdl_ROMseg_addr):
  118.     cdl_ROMseg_size = 0x00
  119.  
  120.     currentAddress = cdl_ROMseg_addr
  121.     inputfile.seek(currentAddress)
  122.  
  123.     print("...Calculating ROM Segment size...Please Wait...\n")
  124.     while(inputfile.read(4) != cdl_ROMseg_endbyte):
  125.         cdl_ROMseg_size += 1 #add 1 to the total size
  126.         currentAddress += 1 #move to the next byte
  127.         inputfile.seek(currentAddress) #set as current address, necessary?
  128.  
  129.     print('ROM Size calculated! The size is {:x} bytes\n'.format(cdl_ROMseg_size))
  130.     return cdl_ROMseg_size
  131.  
  132. def init_idc(outputfile):
  133.     outputfile.write("#include <idc.idc>\n")
  134.     outputfile.write("static main() {\n")
  135.     ##create RAM segments just in case they havent been made by the user? might take this out, I dont know what the proper address spaces would be yet
  136.     outputfile.write('SegRename(0x000000, "ROM");\n')
  137.     outputfile.write('SegCreate(0xFF0000, 0xFFFFFF,0,0,0,2);\n')
  138.     outputfile.write('SegRename(0xFF0000, "RAM");\n')
  139.     return
  140.  
  141. if __name__ == "__main__":
  142.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement