track.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # greaseweazle/track.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 binascii
  8. from bitarray import bitarray
  9. from greaseweazle.flux import Flux
  10. # A pristine representation of a track, from a codec and/or a perfect image.
  11. class MasterTrack:
  12. @property
  13. def bitrate(self):
  14. return len(self.bits) / self.time_per_rev
  15. # bits: Track bitcell data (bitarray)
  16. # time_per_rev: Time per revolution, in seconds (float)
  17. # bit_ticks: Per-bitcell time values, in unitless 'ticks'
  18. # splice: Location of the track splice, in bitcells, after the index.
  19. # weak: List of (start, length) weak ranges
  20. def __init__(self, bits, time_per_rev, bit_ticks=None, splice=0, weak=[]):
  21. self.bits = bits
  22. self.time_per_rev = time_per_rev
  23. self.bit_ticks = bit_ticks
  24. self.splice = splice
  25. self.weak = weak
  26. def __str__(self):
  27. s = "\nMaster Track: splice at %d\n" % self.splice
  28. s += (" %d bits, %.1f kbit/s"
  29. % (len(self.bits), self.bitrate))
  30. if self.bit_ticks:
  31. s += " (variable)"
  32. s += ("\n %.1f ms / rev (%.1f rpm)"
  33. % (self.time_per_rev * 1000, 60 / self.time_per_rev))
  34. if len(self.weak) > 0:
  35. s += "\n %d weak range" % len(self.weak)
  36. if len(self.weak) > 1: s += "s"
  37. s += ": " + ", ".join(str(n) for _,n in self.weak) + " bits"
  38. #s += str(binascii.hexlify(self.bits.tobytes()))
  39. return s
  40. def flux_for_writeout(self):
  41. # We're going to mess with the track data, so take a copy.
  42. bits = self.bits.copy()
  43. bitlen = len(bits)
  44. # Also copy the bit_ticks array (or create a dummy one), and remember
  45. # the total ticks that it contains.
  46. bit_ticks = self.bit_ticks.copy() if self.bit_ticks else [1] * bitlen
  47. ticks_to_index = sum(bit_ticks)
  48. # Weak regions need special processing for correct flux representation.
  49. for s,n in self.weak:
  50. e = s + n
  51. assert s+n <= bitlen
  52. if n < 2:
  53. continue
  54. if n < 400:
  55. # Short weak regions are written with no flux transitions.
  56. bits[s:e] = bitarray([0]) * n
  57. else:
  58. # Long weak regions we present a fuzzy clock bit in an
  59. # otherwise normal byte (16 bits MFM). The byte may be
  60. # interpreted as
  61. # MFM 0001001010100101 = 12A5 = byte 0x43, or
  62. # MFM 0001001010010101 = 1295 = byte 0x47
  63. pattern = bitarray(endian="big")
  64. pattern.frombytes(b"\x12\xA5")
  65. bits[s:e] = (pattern * (n//16+1))[:n]
  66. for i in range(0, n-10, 16):
  67. x, y = bit_ticks[s+i+10], bit_ticks[s+i+11]
  68. bit_ticks[s+i+10], bit_ticks[s+i+11] = x+y*0.5, y*0.5
  69. # To prevent corrupting a preceding sync word by effectively
  70. # starting the weak region early, we start with a 1 if we just
  71. # clocked out a 0.
  72. bits[s] = not (bits[-1] if s == 0 else bits[s-1])
  73. # Similarly modify the last bit of the weak region.
  74. bits[e-1] = not(bits[e-2] or bits[e%bitlen])
  75. splice_at_index = self.splice < 4 or bitlen - self.splice < 4
  76. if splice_at_index:
  77. # Splice is at the index (or within a few bitcells of it).
  78. # We stretch the track with extra bytes of filler, in case the
  79. # drive motor spins slower than expected and we need more filler
  80. # to get us to the index pulse (where the write will terminate).
  81. # Thus if the drive spins slow, the track gets a longer footer.
  82. pos = bitlen-4 if self.splice < 4 else self.splice-4
  83. tick_pattern = bit_ticks[pos-32:pos]
  84. fill_pattern = bits[pos-32:pos]
  85. # We stretch by 10 percent, which is way more than enough.
  86. for i in range(bitlen // (10*32)):
  87. bit_ticks[pos:pos+32] = tick_pattern
  88. bits[pos:pos+32] = fill_pattern
  89. pos += 32
  90. else:
  91. # Splice is not at the index. We will write more than one
  92. # revolution, and terminate the second revolution at the splice.
  93. # For the first revolution we repeat the track header *backwards*
  94. # to the very start of the write. This is in case the drive motor
  95. # spins slower than expected and the write ends before the original
  96. # splice position.
  97. # Thus if the drive spins slow, the track gets a longer header.
  98. bit_ticks += bit_ticks[:self.splice-4]
  99. bits += bits[:self.splice-4]
  100. pos = self.splice+4
  101. fill_pattern = bits[pos:pos+32]
  102. while pos >= 32:
  103. pos -= 32
  104. bits[pos:pos+32] = fill_pattern
  105. # Convert the stretched track data into flux.
  106. bit_ticks_i = iter(bit_ticks)
  107. flux_list = []
  108. flux_ticks = 0
  109. for bit in bits:
  110. flux_ticks += next(bit_ticks_i)
  111. if bit:
  112. flux_list.append(flux_ticks)
  113. flux_ticks = 0
  114. if flux_ticks:
  115. flux_list.append(flux_ticks)
  116. # Package up the flux for return.
  117. flux = Flux([ticks_to_index], flux_list,
  118. ticks_to_index / self.time_per_rev)
  119. flux.terminate_at_index = splice_at_index
  120. return flux
  121. # Local variables:
  122. # python-indent: 4
  123. # End: