scp.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # greaseweazle/scp.py
  2. #
  3. # Written & released by Keir Fraser <keir.xen@gmail.com>
  4. #
  5. # This is free and unencumbered software released into the public domain.
  6. # See the file COPYING for more details, or visit <http://unlicense.org>.
  7. import struct
  8. from greaseweazle.flux import Flux
  9. class SCP:
  10. # 40MHz
  11. sample_freq = 40000000
  12. def __init__(self, start_cyl, nr_sides):
  13. self.start_cyl = start_cyl
  14. self.nr_sides = nr_sides
  15. self.nr_revs = None
  16. self.track_list = []
  17. @classmethod
  18. def from_file(cls, dat):
  19. header = struct.unpack("<3s9BI", dat[0:16])
  20. (sig, _, _, nr_revs, s_trk, e_trk, flags, _, ss, _, _) = header
  21. assert sig == b"SCP"
  22. nr_sides = 1 if ss else 2
  23. trk_offs = struct.unpack("<168I", dat[16:0x2b0])
  24. scp = cls(s_trk // nr_sides, nr_sides)
  25. scp.nr_revs = nr_revs
  26. for trknr in range(s_trk, e_trk+1):
  27. trk_off = trk_offs[trknr]
  28. if trk_off == 0:
  29. scp.track_list.append((None, None))
  30. # Parse the SCP track header and extract the flux data.
  31. thdr = dat[trk_off:trk_off+4+12*nr_revs]
  32. sig, tnr, _, _, s_off = struct.unpack("<3sB3I", thdr[:16])
  33. assert sig == b"TRK"
  34. assert tnr == trknr
  35. _, e_nr, e_off = struct.unpack("<3I", thdr[-12:])
  36. tdat = dat[trk_off+s_off:trk_off+e_off+e_nr*2]
  37. scp.track_list.append((thdr, tdat))
  38. return scp
  39. def get_track(self, cyl, side, writeout=False):
  40. if side >= self.nr_sides:
  41. return None
  42. if cyl < self.start_cyl:
  43. return None
  44. off = (cyl - self.start_cyl) * self.nr_sides + side
  45. if off >= len(self.track_list):
  46. return None
  47. tdh, dat = self.track_list[off]
  48. if not dat:
  49. return None
  50. tdh = tdh[4:]
  51. # Writeout requires only a single revolution
  52. if writeout:
  53. tdh = tdh[:12]
  54. _, nr, _ = struct.unpack("<3I", tdh)
  55. dat = dat[:nr*2]
  56. index_list = []
  57. while tdh:
  58. ticks, _, _ = struct.unpack("<3I", tdh[:12])
  59. index_list.append(ticks)
  60. tdh = tdh[12:]
  61. # Decode the SCP flux data into a simple list of flux times.
  62. flux_list = []
  63. val = 0
  64. for i in range(0, len(dat), 2):
  65. x = dat[i]*256 + dat[i+1]
  66. if x == 0:
  67. val += 65536
  68. continue
  69. flux_list.append(val + x)
  70. val = 0
  71. return Flux(index_list, flux_list, SCP.sample_freq)
  72. # append_track:
  73. # Converts a Flux object into a Supercard Pro Track and appends it to
  74. # the current image-in-progress.
  75. def append_track(self, flux):
  76. nr_revs = len(flux.index_list) - 1
  77. if not self.nr_revs:
  78. self.nr_revs = nr_revs
  79. else:
  80. assert self.nr_revs == nr_revs
  81. factor = SCP.sample_freq / flux.sample_freq
  82. trknr = self.start_cyl * self.nr_sides + len(self.track_list)
  83. tdh = struct.pack("<3sB", b"TRK", trknr)
  84. dat = bytearray()
  85. len_at_index = rev = 0
  86. to_index = flux.index_list[0]
  87. rem = 0.0
  88. for x in flux.list:
  89. # Are we processing initial samples before the first revolution?
  90. if rev == 0:
  91. if to_index >= x:
  92. # Discard initial samples
  93. to_index -= x
  94. continue
  95. # Now starting the first full revolution
  96. rev = 1
  97. to_index += flux.index_list[rev]
  98. # Does the next flux interval cross the index mark?
  99. while to_index < x:
  100. # Append to the TDH for the previous full revolution
  101. tdh += struct.pack("<III",
  102. int(round(flux.index_list[rev]*factor)),
  103. (len(dat) - len_at_index) // 2,
  104. 4 + nr_revs*12 + len_at_index)
  105. # Set up for the next revolution
  106. len_at_index = len(dat)
  107. rev += 1
  108. if rev > nr_revs:
  109. # We're done: We simply discard any surplus flux samples
  110. self.track_list.append((tdh, dat))
  111. return
  112. to_index += flux.index_list[rev]
  113. # Process the current flux sample into SCP "bitcell" format
  114. to_index -= x
  115. y = x * factor + rem
  116. val = int(round(y))
  117. if (val & 65535) == 0:
  118. val += 1
  119. rem = y - val
  120. while val >= 65536:
  121. dat.append(0)
  122. dat.append(0)
  123. val -= 65536
  124. dat.append(val>>8)
  125. dat.append(val&255)
  126. # Header for last track(s) in case we ran out of flux timings.
  127. while rev <= nr_revs:
  128. tdh += struct.pack("<III",
  129. int(round(flux.index_list[rev]*factor)),
  130. (len(dat) - len_at_index) // 2,
  131. 4 + nr_revs*12 + len_at_index)
  132. len_at_index = len(dat)
  133. rev += 1
  134. self.track_list.append((tdh, dat))
  135. def get_image(self):
  136. s_trk = self.start_cyl * self.nr_sides
  137. e_trk = s_trk + len(self.track_list) - 1
  138. # Generate the TLUT and concatenate all the tracks together.
  139. trk_offs = bytearray(s_trk * 4)
  140. trk_dat = bytearray()
  141. for tdh, dat in self.track_list:
  142. trk_offs += struct.pack("<I", 0x2b0 + len(trk_dat))
  143. trk_dat += tdh + dat
  144. trk_offs += bytes(0x2a0 - len(trk_offs))
  145. # Calculate checksum over all data (except 16-byte image header).
  146. csum = 0
  147. for x in trk_offs:
  148. csum += x
  149. for x in trk_dat:
  150. csum += x
  151. # Generate the image header.
  152. header = struct.pack("<3s9BI",
  153. b"SCP", # Signature
  154. 0, # Version
  155. 0x80, # DiskType = Other
  156. self.nr_revs, s_trk, e_trk,
  157. 0x01, # Flags = Index
  158. 0, # 16-bit cell width
  159. 1 if self.nr_sides == 1 else 0,
  160. 0, # 25ns capture
  161. csum & 0xffffffff)
  162. # Concatenate it all together and send it back.
  163. return header + trk_offs + trk_dat
  164. # Local variables:
  165. # python-indent: 4
  166. # End: