Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # SPDX-License-Identifier: CC0-1.0
- from pathlib import Path
- import os
- import sys
- import argparse
- import shutil
- import struct
- def split(i, parts):
- '''Splits a file in parts
- The parts might be of different sizes. But they must be all
- continuous
- '''
- for name, size in parts:
- buf = i.read(size)
- assert len(buf) == size
- with open(name, 'wb+') as o:
- o.write(buf)
- def split_ranges(i, parts):
- for name, ranges in parts:
- with open(name, 'wb+') as o:
- for start, end in ranges:
- i.seek(start)
- buf = i.read(end-start)
- assert i.tell() == end
- assert len(buf) == end-start
- o.write(buf)
- def deinterleave16(i, parts, limit = -1):
- with open(parts[0], 'wb+') as o0, open(parts[1], 'wb+') as o1:
- count = 0
- word = i.read(2) if count != limit else None
- while word:
- o0.write(word[0:1])
- o1.write(word[1:2])
- count += 1
- word = i.read(2) if count != limit else None
- def deinterleave32(i, parts):
- with open(parts[0], 'wb+') as o0, open(parts[1], 'wb+') as o1, \
- open(parts[2], 'wb+') as o2, open(parts[3], 'wb+') as o3:
- word = i.read(4)
- while word:
- o0.write(word[0:1])
- o1.write(word[1:2])
- o2.write(word[2:3])
- o3.write(word[3:4])
- word = i.read(4)
- def mkflash(target, prefix):
- target.seek(-1, os.SEEK_END)
- region = target.read(1)
- fn = prefix + f'flash0_blank_region{region[0]:02x}.u1053'
- with open(fn, 'wb+') as o:
- o.write(region)
- for _ in range(0x0F_FFFF):
- o.write(b'\xFF')
- def mkempty(fn, size):
- with open(fn, 'wb+') as o:
- o.seek(size-1)
- o.write(b'\x00')
- def pixel_de4_to_rai4(i, fn):
- with open(fn, 'wb+') as o:
- data_big = i.read(4)
- times = 0
- low = b''
- high = b''
- while data_big:
- pixels, = struct.unpack('<I', data_big)
- rom_pixels = ((pixels & 0x0000_0001) << 7 # -15 + 8
- | (pixels & 0x0000_0002) << 2 # -10 + 8
- | (pixels & 0x0000_0004) << 13 # -5 - 8
- | (pixels & 0x0000_0008) << 8 # 0 - 8
- | (pixels & 0x0000_0100) >> 2 # -6 + 8
- | (pixels & 0x0000_0200) >> 7 # -1 + 8
- | (pixels & 0x0000_0400) << 4 # 4 - 8
- | (pixels & 0x0000_0800) >> 1 # 9 - 8
- | (pixels & 0x0001_0000) >> 11 # 3 + 8
- | (pixels & 0x0002_0000) >> 16 # 8 + 8
- | (pixels & 0x0004_0000) >> 5 # 13 - 8
- | (pixels & 0x0008_0000) >> 10 # 18 - 8
- | (pixels & 0x0100_0000) >> 20 # 12 + 8
- | (pixels & 0x0200_0000) >> 25 # 17 + 8
- | (pixels & 0x0400_0000) >> 14 # 22 - 8
- | (pixels & 0x0800_0000) >> 19) # 27 - 8
- assert not (rom_pixels & ~0xFFFF)
- packed = struct.pack('<H', rom_pixels)
- if (times % 4) < 2:
- low += packed
- else:
- high += packed
- data_big = i.read(4)
- times += 1
- if times == 64:
- o.write(low)
- o.write(high)
- low = b''
- high = b''
- times = 0
- assert times < 64
- def encrypt_spi(pixels, key1, key2, key3, tn = 0):
- # to mame style (Big Endian)
- mame = ((pixels & 0x00_00FF) << 16
- | (pixels & 0x00_FF00)
- | (pixels & 0xFF_0000) >> 16)
- xor = mame ^ key3
- suma = tn + key1
- # no carry last
- assert not (key2 & 0x80_0000)
- e = 0
- carry = 0
- for i in range(24):
- bit = ((xor >> i) & 1) ^ ((suma >> i) & 1) ^ carry
- bitsum = bit + ((suma >> i) & 1) + carry
- carry = (bitsum >> 1) if ((key2 >> i) & 1) else 0
- e |= ((bit & 1) << i)
- #Reorder bits
- reordered = ((e & 0x80_0000) >> 5
- | (e & 0x40_0000) >> 3
- | (e & 0x20_0000) >> 12
- | (e & 0x10_0020) >> 15
- | (e & 0x08_0000) >> 9
- | (e & 0x04_0000) >> 1
- | (e & 0x02_0000) >> 1
- | (e & 0x01_0000) << 4
- | (e & 0x00_8000) << 6
- | (e & 0x00_4000) << 8
- | (e & 0x00_2000) >> 7
- | (e & 0x00_1000) >> 1
- | (e & 0x00_0800) << 4
- | (e & 0x00_0400) << 4
- | (e & 0x00_0200) >> 5
- | (e & 0x00_0100) << 15
- | (e & 0x00_0080) >> 7
- | (e & 0x00_0040) >> 5
- | (e & 0x00_0020) << 2
- | (e & 0x00_0010) << 4
- | (e & 0x00_0008) << 10
- | (e & 0x00_0004) << 10
- | (e & 0x00_0002) << 2
- | (e & 0x00_0001) << 2)
- return struct.pack('>I', reordered)[1:]
- def pixel_de5_to_spi6(inp, fn, fix0=False, fix1=False):
- with open(fn, 'wb+') as out:
- group = 0
- data = inp.read(4)
- while data:
- pixels, = struct.unpack('<I', data)
- # Pixel color fixes
- for i in range(4):
- n = i*8
- if fix0 and pixels >> n & 0xFF == 0x1F:
- pixels = pixels | 0x20 << n
- # Fix for rdft_gfxdata1.bin
- if fix1 and 61_456 <= group and group <= 61_487:
- pixels = pixels | 0x2020_2020
- # Change format
- spi6_pixels2 = ((pixels & 0x0000_0001) << 16 # 0 + 16 + 0
- | (pixels & 0x0000_0002) << 19 # -1 + 20 + 0
- | (pixels & 0x0000_0004) << 6 # -2 + 8 + 0
- | (pixels & 0x0000_0008) << 9 # -3 + 12 + 0
- | (pixels & 0x0000_0010) >> 4 # -4 + 0 + 0
- | (pixels & 0x0000_0020) >> 1 # -5 + 4 + 0
- | (pixels & 0x0000_0100) << 9 # -8 + 16 + 1
- | (pixels & 0x0000_0200) << 12 # -9 + 20 + 1
- | (pixels & 0x0000_0400) >> 1 # -10 + 8 + 1
- | (pixels & 0x0000_0800) << 2 # -11 + 12 + 1
- | (pixels & 0x0000_1000) >> 11 # -12 + 0 + 1
- | (pixels & 0x0000_2000) >> 8 # -13 + 4 + 1
- | (pixels & 0x0001_0000) << 2 # -16 + 16 + 2
- | (pixels & 0x0002_0000) << 5 # -17 + 20 + 2
- | (pixels & 0x0004_0000) >> 8 # -18 + 8 + 2
- | (pixels & 0x0008_0000) >> 5 # -19 + 12 + 2
- | (pixels & 0x0010_0000) >> 18 # -20 + 0 + 2
- | (pixels & 0x0020_0000) >> 15 # -21 + 4 + 2
- | (pixels & 0x0100_0000) >> 5 # -24 + 16 + 3
- | (pixels & 0x0200_0000) >> 2 # -25 + 20 + 3
- | (pixels & 0x0400_0000) >> 15 # -26 + 8 + 3
- | (pixels & 0x0800_0000) >> 12 # -27 + 12 + 3
- | (pixels & 0x1000_0000) >> 25 # -28 + 0 + 3
- | (pixels & 0x2000_0000) >> 22) # -29 + 4 + 3
- assert not (spi6_pixels2 & ~0xFF_FFFF)
- packed32 = struct.pack('<I', spi6_pixels2)
- packed = packed32[:-1]
- out.write(packed)
- group += 1
- data = inp.read(4)
- def raidenb(prefix):
- try:
- os.mkdir(prefix + 'raidenb')
- except FileExistsError:
- pass
- with open('raiden/raiden_maincpu.bin', 'rb') as f:
- maincpu_parts_low = [
- prefix + 'raidenb/1.u0253',
- prefix + 'raidenb/2.u0252',
- ]
- maincpu_parts_high = [
- prefix + 'raidenb/3__(raidenb).u022',
- prefix + 'raidenb/4__(raidenb).u023',
- ]
- deinterleave16(f, maincpu_parts_low, 0x01_0000)
- deinterleave16(f, maincpu_parts_high)
- with open('raiden/raiden_subcpu.bin', 'rb') as f:
- sub_parts = [
- prefix + 'raidenb/5__(raidenb).u042',
- prefix + 'raidenb/6__(raidenb).u043',
- ]
- deinterleave16(f, sub_parts)
- with open('raiden/raiden_audiocpu.bin', 'rb') as f:
- audiocpu_parts = [
- (prefix + 'raidenb/rai6.u212', [(0, 0x8000),
- (0x01_0000, 0x01_8000)]),
- ]
- split_ranges(f, audiocpu_parts)
- with open('raiden/raiden_gfx1.bin', 'rb') as f:
- gfx1_parts = [
- (prefix + 'raidenb/9', 0x8000),
- (prefix + 'raidenb/10', 0x8000)
- ]
- split(f, gfx1_parts)
- with open('raiden/raiden_gfxdata2.bin', 'rb') as f:
- gfx2 = prefix + 'raidenb/sei420'
- pixel_de4_to_rai4(f, gfx2)
- with open('raiden/raiden_gfxdata3.bin', 'rb') as f:
- gfx3 = prefix + 'raidenb/sei430'
- pixel_de4_to_rai4(f, gfx3)
- with open('raiden/raiden_gfxdata4.bin', 'rb') as f:
- gfx4 = prefix + 'raidenb/sei440'
- pixel_de4_to_rai4(f, gfx4)
- shutil.copyfile('raiden/raiden_okim6295.bin', prefix + 'raidenb/7.u203')
- # plds
- mkempty(prefix + 'raidenb/ep910pc-1.bin', 0x0884)
- mkempty(prefix + 'raidenb/ep910pc-2.bin', 0x0884)
- # TMP plds?
- mkempty(prefix + 'raidenb/jj3010.u0116', 0x0100)
- def rdft(prefix):
- try:
- os.mkdir(prefix + 'rdft')
- except FileExistsError:
- pass
- with open('rdft/rdft_i386.bin', 'rb') as f:
- maincpu_parts = [
- prefix + 'rdft/gd_1.211',
- prefix + 'rdft/gd_2.212',
- prefix + 'rdft/gd_3.210',
- prefix + 'rdft/gd_4.29'
- ]
- deinterleave32(f, maincpu_parts)
- with open('rdft/rdft_gfxdata1.bin', 'rb') as f:
- pixel_de5_to_spi6(f, prefix + 'rdft/rdft_gfxdata1.bin.spi', fix0=True,
- fix1=True)
- with open('rdft/rdft_gfxdata2.bin', 'rb') as f:
- pixel_de5_to_spi6(f, prefix + 'rdft/rdft_gfxdata2.bin.spi')
- with open('rdft/rdft_gfxdata3.bin', 'rb') as f:
- pixel_de5_to_spi6(f, prefix + 'rdft/rdft_gfxdata3.bin.spi')
- with open('rdft/rdft_soundrom.bin', 'rb') as f:
- sound01_parts = [
- (prefix + 'rdft/gd_pcm.217', 0x20_0000),
- (prefix + 'rdft/gd_8.216', 0x08_0000),
- ]
- split(f, sound01_parts)
- with open(prefix + 'rdft/gd_1.211', 'rb') as f:
- mkflash(f, prefix + 'rdft/')
- def rdft2(prefix):
- try:
- os.mkdir(prefix + 'rdft2')
- except FileExistsError:
- pass
- with open('rdft2/rdft2_i386.bin', 'rb') as f:
- maincpu_parts = [
- prefix + 'rdft2/prg0.tun',
- prefix + 'rdft2/prg1.bin',
- prefix + 'rdft2/prg2.bin',
- prefix + 'rdft2/prg3.bin'
- ]
- deinterleave32(f, maincpu_parts)
- with open('rdft2/rdft2_soundrom.bin', 'rb') as f:
- sound01_parts = [
- (prefix + 'rdft2/pcm.u0217', 0x20_0000),
- (prefix + 'rdft2/sound1.u0222', 0x08_0000),
- ]
- split(f, sound01_parts)
- with open(prefix + 'rdft2/prg0.tun', 'rb') as f:
- mkflash(f, prefix + 'rdft2/')
- def convert():
- outdir = 'mame/'
- try:
- os.mkdir(outdir)
- except FileExistsError:
- pass
- raidenb(outdir)
- rdft(outdir)
- rdft2(outdir)
- if __name__ == '__main__':
- convert()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement