Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import struct, sys
p = ("\x2f\x43", "\x43\x2f")
pnewdata = "\x57\x43"
penddata = "\x57\x2f"
def add_kernel_byte(c):
s = pnewdata
c = ord(c)
csum = 1
for i in xrange(8):
bit = (c >> i) & 1
s += p[bit]
csum ^= bit
s += p[csum]
return s
def add_loader_byte(c):
s = ""
c = ord(c)
for i in xrange(8):
bit = (c >> i) & 1
s += "\x27\x45"[bit]
return s
def add_kernel_block(data, headersize, pausesize):
s = "\x00" + struct.pack("<I", pausesize)[0:3]
s += "\x2f" * headersize
for c in data:
s += add_kernel_byte(c)
s += penddata
s += "\x2f" * 0x300
for c in data.replace("\x89\x88\x87\x86\x85\x84\x83\x82\x81", "\x09\x08\x07\x06\x05\x04\x03\x02\x01"):
s += add_kernel_byte(c)
s += penddata
return s
def add_loader_block(data, pausesize):
s = "\x00" + struct.pack("<I", pausesize)[0:3]
s += "\x27"
for i in xrange(64):
s += add_loader_byte("\x01")
for c in data:
s += add_loader_byte(c)
return s
f = open("P0000152.A44", "rb")
f.read(512)
tapdata = ""
while True:
header = f.read(10)
if len(header) == 0:
break
dunno1, dunno2, dunno3, sz, dunno4 = struct.unpack(">HHHHH", header)
print "%x %x %x %x %x" % (dunno1, dunno2, dunno3, sz, dunno4)
if sz < 10:
print "TOO SMALL"
break
data = f.read(sz - 10)
if dunno4 == 0xff00:
pausesize = 150000 * dunno1
tapdata += add_kernel_block(data, dunno3, pausesize)
elif dunno4 == 0x1c00:
pausesize = 5000 * dunno1
tapdata += add_loader_block(data, pausesize)
else:
print "UNKNOWN LOADER", hex(dunno4)
exit()
tapdata += "\x00" + struct.pack("<I", 150000 * 0x10)[0:3]
of = open("output.tap", "wb")
of.write("C64-TAPE-RAW\x01\x00\x00\x00" + struct.pack("<I", len(tapdata)) + tapdata)
of.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment