Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # By Pedro Gimeno, 2020-06-26
- import sys, struct
- def main():
- f = open(sys.argv[1], 'rb')
- try:
- f.seek(0, 2)
- flen = f.tell()
- f.seek(0, 0)
- hdr = f.read(256)
- fmt, trkflags, id, crap = struct.unpack('<B170s11s74s', hdr)
- if id != b'DIFC HEADER':
- raise Exception('Invalid file format - not a .dim file')
- trkflags = bytearray(trkflags)
- print("DIM format: %d" % fmt)
- if fmt == 0x00:
- sectors = 8
- sectorlen = 1024
- fmt = 0x20 # ?
- elif fmt == 0x01 or fmt == 0x03:
- sectors = 9
- sectorlen = 1024
- fmt = 0x20 # ?
- elif fmt == 0x02:
- sectors = 15
- sectorlen = 512
- fmt = 0x20 # ?
- elif fmt == 0x09:
- sectors = 18
- sectorlen = 512
- fmt = 0x20 # ?
- elif fmt == 0x11:
- sectors = 26
- sectorlen = 256
- fmt = 0x20 # ?
- else:
- raise Exception('Unrecognized format: 0x%02X' % fmt)
- secdata = []
- d88_offsets = [0x2B0]
- for i, v in enumerate(trkflags):
- if v:
- for s in range(sectors):
- secdata.append(f.read(sectorlen))
- if i < 164: # d88 only supports 164 tracks, dim supports 170
- d88_offsets.append(d88_offsets[-1] + (0x10 + sectorlen) * sectors)
- else:
- raise Exception('Data present in a track number not supported by D88, aborting')
- if f.tell() != flen:
- print(f.tell(), flen)
- raise Exception('Inconsistent sector size? Found %d tracks of %d sectors, each %d bytes'
- % (len(secdata), sectors, sectorlen))
- finally:
- f.close()
- f = open(sys.argv[2], 'wb')
- try:
- hdr = struct.pack('<17s9sBBI', b'DIM2D88', b'', 0, fmt, d88_offsets[-1])
- d88_offsets = d88_offsets[:-1]
- d88_offsets = d88_offsets + [0] * (164 - len(d88_offsets))
- f.write(hdr)
- f.write(struct.pack('<164I', *d88_offsets))
- tracknum = 0
- secptr = 0
- seclen_code = 1 if sectorlen == 256 else 2 if sectorlen == 512 else 3
- for v in trkflags[:164]:
- if v:
- h = tracknum & 1
- c = tracknum >> 1
- for s in range(sectors):
- hdr = struct.pack('<BBBBHBBB5sH', c, h, s+1, seclen_code, sectors,
- 0x00, # ? Density - 0x00 = Double, 0x40 = single
- 0, 0, b'', sectorlen)
- f.write(hdr)
- f.write(secdata[secptr])
- secptr += 1
- tracknum += 1
- finally:
- f.close()
- print("Successfully converted!")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement