track.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, aligned to the write splice (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 @ %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 0 < s < e < bitlen
  52. pattern = bitarray(endian="big")
  53. if n < 400:
  54. # Short weak regions are written with no flux transitions.
  55. # Actually we insert a flux transition every 32 bitcells, else
  56. # we risk triggering Greaseweazle's No Flux Area generator.
  57. pattern.frombytes(b"\x80\x00\x00\x00")
  58. bits[s:e] = (pattern * (n//32+1))[:n]
  59. else:
  60. # Long weak regions we present a fuzzy clock bit in an
  61. # otherwise normal byte (16 bits MFM). The byte may be
  62. # interpreted as
  63. # MFM 0001001010100101 = 12A5 = byte 0x43, or
  64. # MFM 0001001010010101 = 1295 = byte 0x47
  65. pattern.frombytes(b"\x12\xA5")
  66. bits[s:e] = (pattern * (n//16+1))[:n]
  67. for i in range(0, n-10, 16):
  68. x, y = bit_ticks[s+i+10], bit_ticks[s+i+11]
  69. bit_ticks[s+i+10], bit_ticks[s+i+11] = x+y*0.5, y*0.5
  70. # To prevent corrupting a preceding sync word by effectively
  71. # starting the weak region early, we start with a 1 if we just
  72. # clocked out a 0.
  73. bits[s] = not bits[s-1]
  74. # Similarly modify the last bit of the weak region.
  75. bits[e-1] = not(bits[e-2] or bits[e])
  76. # Rotate data to start at the index (writes are always aligned there).
  77. index = -self.splice % bitlen
  78. if index != 0:
  79. bits = bits[index:] + bits[:index]
  80. bit_ticks = bit_ticks[index:] + bit_ticks[:index]
  81. splice_at_index = index < 4 or bitlen - index < 4
  82. if splice_at_index:
  83. # Splice is at the index (or within a few bitcells of it).
  84. # We stretch the track with extra bytes of filler, in case the
  85. # drive motor spins slower than expected and we need more filler
  86. # to get us to the index pulse (where the write will terminate).
  87. # Thus if the drive spins slow, the track gets a longer footer.
  88. pos = (self.splice - 4) % bitlen
  89. # We stretch by 10 percent, which is way more than enough.
  90. rep = bitlen // (10 * 32)
  91. bit_ticks = bit_ticks[:pos] + bit_ticks[pos-32:pos] * rep
  92. bits = bits[:pos] + bits[pos-32:pos] * rep
  93. else:
  94. # Splice is not at the index. We will write more than one
  95. # revolution, and terminate the second revolution at the splice.
  96. # For the first revolution we repeat the track header *backwards*
  97. # to the very start of the write. This is in case the drive motor
  98. # spins slower than expected and the write ends before the original
  99. # splice position.
  100. # Thus if the drive spins slow, the track gets a longer header.
  101. bit_ticks += bit_ticks[:self.splice-4]
  102. bits += bits[:self.splice-4]
  103. pos = self.splice+4
  104. fill_pattern = bits[pos:pos+32]
  105. while pos >= 32:
  106. pos -= 32
  107. bits[pos:pos+32] = fill_pattern
  108. # Convert the stretched track data into flux.
  109. bit_ticks_i = iter(bit_ticks)
  110. flux_list = []
  111. flux_ticks = 0
  112. for bit in bits:
  113. flux_ticks += next(bit_ticks_i)
  114. if bit:
  115. flux_list.append(flux_ticks)
  116. flux_ticks = 0
  117. if flux_ticks:
  118. flux_list.append(flux_ticks)
  119. # Package up the flux for return.
  120. flux = Flux([ticks_to_index], flux_list,
  121. ticks_to_index / self.time_per_rev)
  122. flux.terminate_at_index = splice_at_index
  123. return flux
  124. # Local variables:
  125. # python-indent: 4
  126. # End: