Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python3
"""
Converts between QD and FDS disk images
"""
import itertools
import struct
from sys import argv
def convert_to_fds(disk):
"""
Convert a .qd to .fds
"""
del disk[0x38:0x3a] # delete 01 block chk
# fn = d[0x39] # get num of files from 02 block
pos = 0x3a
try:
while disk[pos + 2] == 3: # if there's any more files, like in Doki Doki Panic
del disk[pos:pos + 2] # delete 03 block chk
print(disk[pos + 3:pos + 11].decode('1252'))
filesize, = struct.unpack('<H', disk[pos + 0xD:pos + 0xF])
del disk[pos + 0x10:pos + 0x12] # delete 04 block chk
pos = pos + 0x11 + filesize
except IndexError:
pass
del disk[pos:pos + 2] # delete final 04 block chk
if len(disk) > 65500:
disk = disk[:65500]
else:
for _ in itertools.repeat(None, 65500 - len(disk)):
disk.append(0)
return disk
def convert_to_qd(disk):
"""
Convert a .fds to .qd
"""
disk.insert(0x38, 0) # add 01 block chk
disk.insert(0x39, 0) # add 01 block chk
# fn = d[0x3b] # get num of files from 02 block
pos = 0x3c
try:
while disk[pos] == 3: # if there's any more files, like in Doki Doki Panic
disk.insert(pos, 0)
disk.insert(pos + 1, 0)
print(disk[pos + 5:pos + 13].decode('1252'))
filesize, = struct.unpack('<H', disk[pos + 0xF:pos + 0x11])
disk.insert(pos + 0x12, 0)
disk.insert(pos + 0x13, 0)
pos = pos + 0x14 + 1 + filesize
except IndexError:
pass
disk.insert(pos, 0) # add final 04 block chk
disk.insert(pos + 1, 0)
if len(disk) > 0x10000:
disk = disk[:0x10000]
else:
for _ in itertools.repeat(None, 0x10000 - len(disk)):
disk.append(0)
return disk
ext = ''
disk_file = open(argv[1], 'rb').read()
if len(disk_file) == 0x20000:
side1 = disk_file[:0x10000]
side2 = disk_file[0x10000:]
side1 = convert_to_fds(side1)
side2 = convert_to_fds(side2)
disk_file = side1 + side2
ext = '.fds'
elif len(disk_file) == 0x10000:
disk_file = convert_to_fds(bytearray(disk_file))
ext = '.fds'
elif len(disk_file) == 131000:
side1 = disk_file[:65500]
side2 = disk_file[65500:]
side1 = convert_to_qd(bytearray(side1))
side2 = convert_to_qd(bytearray(side2))
disk_file = side1 + side2
ext = '.qd'
elif len(disk_file) == 65500:
disk_file = convert_to_qd(bytearray(disk_file))
ext = '.qd'
open(argv[1] + ext, 'wb').write(disk_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.