#!/usr/bin/env python3

# Copyright 2015 <NAME>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Set the correct checksum bytes on the ROM header

#Header format

# TMR SEGA     8B
# Reserverd    2B
# Checksum     2B
# Product code 2.5B
# Version      0.5B
# Region       0.5B
# ROM size     0.5B

import os
import sys

USAGE = '''\
Usage: fix-checksum.py INPUT_FILE [HEADER_POSITION]
Where HEADER_POSITION can be 8, 16, 32 (default)\
'''

# ------------
# --- Main ---
# ------------
if __name__ == '__main__':
  if len(sys.argv) < 2:
    print(USAGE)
    exit()
  
  path_in   = sys.argv[1]
  mode = "32"
  try:
    mode = sys.argv[2]
  except:
    pass
  print("Checksum mode: %sKB" % mode)
  buff = None
  
  with open(path_in, 'rb') as f:
    f_size = os.stat(path_in).st_size
    print("ROM size: %dKB (%dB)" % (int(round(f_size/1024.0)), int(f_size)))
    buff = f.read()
    f.close()
  
  size = len(buff)
  header_location = None
  header_size = None
  
  if   size >= 1024*32:
    header_location = (32*1024) - 16
    header_size = 0x4C
  elif size >= 1024*16:
    header_location = (16*1024) - 16
    header_size = 0x4B
  elif size >= 1024*8:
    header_location = (8*1024) - 16
    header_size = 0x4A
  else:
    #Add padding to the ROM to fill 8K
    buff = buff + bytes("?" * ((8*1024) - len(buff) - 16), 'ASCII')
    header_location = (8*1024) - 16
    header_size = 0x4A
    
    
  sum = 0
  
  if mode == '8':
    header_location = (8*1024) - 16
    header_size = 0x4A
  elif mode == '16':
    header_location = (16*1024) - 16
    header_size = 0x4B
  elif mode == '32':
    header_location = (32*1024) - 16
    header_size = 0x4C
  else:
    print("Unknown mode, using default")
    pass
  
  for b in buff[:header_location]:
    sum = (sum + b) & 0xFFFF
  
  header = bytes("TMR SEGA", 'ASCII') #TMR sega
  header = header + bytes((0,0))      #Reserved
  header = header + bytes( (sum & 0xFF, (sum >> 8)& 0xFF) ) #Checksum
  header = header + bytes( (0,0,0) ) #Product code & version
  header = header + bytes((header_size,))
  
  with open(path_in, 'wb') as f:
    f.write(buff[:header_location])
    f.write(header)
    f.write(buff[header_location + 16:])
    f.close()

#!/usr/bin/env python3

# Copyright 2015 <NAME>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Set the correct checksum bytes on the ROM header

#Header format

# TMR SEGA     8B
# Reserverd    2B
# Checksum     2B
# Product code 2.5B
# Version      0.5B
# Region       0.5B
# ROM size     0.5B

import os
import sys

USAGE = '''\
Usage: fix-checksum.py INPUT_FILE [HEADER_POSITION]
Where HEADER_POSITION can be 8, 16, 32 (default)\
'''

# ------------
# --- Main ---
# ------------
if __name__ == '__main__':
  if len(sys.argv) < 2:
    print(USAGE)
    exit()
  
  path_in   = sys.argv[1]
  mode = "32"
  try:
    mode = sys.argv[2]
  except:
    pass
  print("Checksum mode: %sKB" % mode)
  buff = None
  
  with open(path_in, 'rb') as f:
    f_size = os.stat(path_in).st_size
    print("ROM size: %dKB (%dB)" % (int(round(f_size/1024.0)), int(f_size)))
    buff = f.read()
    f.close()
  
  size = len(buff)
  header_location = None
  header_size = None
  
  if   size >= 1024*32:
    header_location = (32*1024) - 16
    header_size = 0x4C
  elif size >= 1024*16:
    header_location = (16*1024) - 16
    header_size = 0x4B
  elif size >= 1024*8:
    header_location = (8*1024) - 16
    header_size = 0x4A
  else:
    #Add padding to the ROM to fill 8K
    buff = buff + bytes("?" * ((8*1024) - len(buff) - 16), 'ASCII')
    header_location = (8*1024) - 16
    header_size = 0x4A
    
    
  sum = 0
  
  if mode == '8':
    header_location = (8*1024) - 16
    header_size = 0x4A
  elif mode == '16':
    header_location = (16*1024) - 16
    header_size = 0x4B
  elif mode == '32':
    header_location = (32*1024) - 16
    header_size = 0x4C
  else:
    print("Unknown mode, using default")
    pass
  
  for b in buff[:header_location]:
    sum = (sum + b) & 0xFFFF
  
  header = bytes("TMR SEGA", 'ASCII') #TMR sega
  header = header + bytes((0,0))      #Reserved
  header = header + bytes( (sum & 0xFF, (sum >> 8)& 0xFF) ) #Checksum
  header = header + bytes( (0,0,0) ) #Product code & version
  header = header + bytes((header_size,))
  
  with open(path_in, 'wb') as f:
    f.write(buff[:header_location])
    f.write(header)
    f.write(buff[header_location + 16:])
    f.close()

#!/usr/bin/env python3 # Copyright 2015 <NAME> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #Set the correct checksum bytes on the ROM header #Header format # TMR SEGA     8B # Reserverd    2B # Checksum     2B # Product code 2.5B # Version      0.5B # Region       0.5B # ROM size     0.5B \
 Usage: fix-checksum.py INPUT_FILE [HEADER_POSITION]
 Where HEADER_POSITION can be 8, 16, 32 (default)\ # ------------ # --- Main --- # ------------ #Add padding to the ROM to fill 8K #TMR sega #Reserved #Checksum #Product code & version