Advertisement
Guest User

dim288.py (convert disks for misterfpga sharp x68 core)

a guest
May 20th, 2024
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. # By Pedro Gimeno, 2020-06-26
  2. import sys, struct
  3.  
  4. def main():
  5. f = open(sys.argv[1], 'rb')
  6. try:
  7. f.seek(0, 2)
  8. flen = f.tell()
  9. f.seek(0, 0)
  10. hdr = f.read(256)
  11. fmt, trkflags, id, crap = struct.unpack('<B170s11s74s', hdr)
  12. if id != b'DIFC HEADER':
  13. raise Exception('Invalid file format - not a .dim file')
  14.  
  15. trkflags = bytearray(trkflags)
  16.  
  17. print("DIM format: %d" % fmt)
  18. if fmt == 0x00:
  19. sectors = 8
  20. sectorlen = 1024
  21. fmt = 0x20 # ?
  22. elif fmt == 0x01 or fmt == 0x03:
  23. sectors = 9
  24. sectorlen = 1024
  25. fmt = 0x20 # ?
  26. elif fmt == 0x02:
  27. sectors = 15
  28. sectorlen = 512
  29. fmt = 0x20 # ?
  30. elif fmt == 0x09:
  31. sectors = 18
  32. sectorlen = 512
  33. fmt = 0x20 # ?
  34. elif fmt == 0x11:
  35. sectors = 26
  36. sectorlen = 256
  37. fmt = 0x20 # ?
  38. else:
  39. raise Exception('Unrecognized format: 0x%02X' % fmt)
  40.  
  41. secdata = []
  42. d88_offsets = [0x2B0]
  43. for i, v in enumerate(trkflags):
  44. if v:
  45. for s in range(sectors):
  46. secdata.append(f.read(sectorlen))
  47. if i < 164: # d88 only supports 164 tracks, dim supports 170
  48. d88_offsets.append(d88_offsets[-1] + (0x10 + sectorlen) * sectors)
  49. else:
  50. raise Exception('Data present in a track number not supported by D88, aborting')
  51.  
  52. if f.tell() != flen:
  53. print(f.tell(), flen)
  54. raise Exception('Inconsistent sector size? Found %d tracks of %d sectors, each %d bytes'
  55. % (len(secdata), sectors, sectorlen))
  56.  
  57. finally:
  58. f.close()
  59.  
  60. f = open(sys.argv[2], 'wb')
  61. try:
  62. hdr = struct.pack('<17s9sBBI', b'DIM2D88', b'', 0, fmt, d88_offsets[-1])
  63. d88_offsets = d88_offsets[:-1]
  64. d88_offsets = d88_offsets + [0] * (164 - len(d88_offsets))
  65. f.write(hdr)
  66. f.write(struct.pack('<164I', *d88_offsets))
  67. tracknum = 0
  68. secptr = 0
  69. seclen_code = 1 if sectorlen == 256 else 2 if sectorlen == 512 else 3
  70. for v in trkflags[:164]:
  71. if v:
  72. h = tracknum & 1
  73. c = tracknum >> 1
  74. for s in range(sectors):
  75. hdr = struct.pack('<BBBBHBBB5sH', c, h, s+1, seclen_code, sectors,
  76. 0x00, # ? Density - 0x00 = Double, 0x40 = single
  77. 0, 0, b'', sectorlen)
  78. f.write(hdr)
  79. f.write(secdata[secptr])
  80. secptr += 1
  81.  
  82. tracknum += 1
  83. finally:
  84. f.close()
  85.  
  86. print("Successfully converted!")
  87.  
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement