write.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # greaseweazle/tools/write.py
  2. #
  3. # Greaseweazle control script: Write Image to Disk.
  4. #
  5. # Written & released by Keir Fraser <keir.xen@gmail.com>
  6. #
  7. # This is free and unencumbered software released into the public domain.
  8. # See the file COPYING for more details, or visit <http://unlicense.org>.
  9. description = "Write a disk from the specified image file."
  10. import sys
  11. from greaseweazle.tools import util
  12. from greaseweazle import error, track
  13. from greaseweazle import usb as USB
  14. # Read and parse the image file.
  15. def open_image(args):
  16. cls = util.get_image_class(args.file)
  17. return cls.from_file(args.file)
  18. class Formatter:
  19. def __init__(self):
  20. self.length = 0
  21. def print(self, s):
  22. self.erase()
  23. self.length = len(s)
  24. print(s, end="", flush=True)
  25. def erase(self):
  26. l = self.length
  27. print("\b"*l + " "*l + "\b"*l, end="", flush=True)
  28. self.length = 0
  29. # write_from_image:
  30. # Writes the specified image file to floppy disk.
  31. def write_from_image(usb, args, image):
  32. # Measure drive RPM.
  33. # We will adjust the flux intervals per track to allow for this.
  34. drive = usb.read_track(2)
  35. del drive.list
  36. verified_count, not_verified_count = 0, 0
  37. for t in args.tracks:
  38. cyl, head = t.cyl, t.head
  39. track = image.get_track(cyl, head)
  40. if track is None and not args.erase_empty:
  41. continue
  42. print("\r%sing Track %u.%u..." %
  43. ("Writ" if track is not None else "Eras", cyl, head),
  44. end="", flush=True)
  45. usb.seek(t.physical_cyl, head)
  46. if track is None:
  47. usb.erase_track(drive.ticks_per_rev * 1.1)
  48. continue
  49. if args.precomp is not None:
  50. track.precomp = args.precomp.track_precomp(cyl)
  51. flux = track.flux_for_writeout()
  52. # @factor adjusts flux times for speed variations between the
  53. # read-in and write-out drives.
  54. factor = drive.ticks_per_rev / flux.index_list[0]
  55. # Convert the flux samples to Greaseweazle sample frequency.
  56. rem = 0.0
  57. flux_list = []
  58. for x in flux.list:
  59. y = x * factor + rem
  60. val = round(y)
  61. rem = y - val
  62. flux_list.append(val)
  63. # Encode the flux times for Greaseweazle, and write them out.
  64. formatter = Formatter()
  65. verified = False
  66. for retry in range(3):
  67. usb.write_track(flux_list = flux_list,
  68. cue_at_index = flux.index_cued,
  69. terminate_at_index = flux.terminate_at_index)
  70. try:
  71. no_verify = args.no_verify or track.verify is None
  72. except AttributeError: # track.verify undefined
  73. no_verify = True
  74. if no_verify:
  75. not_verified_count += 1
  76. verified = True
  77. break
  78. v_revs, v_ticks = track.verify_revs, 0
  79. if isinstance(v_revs, float):
  80. v_ticks = int(drive.ticks_per_rev * v_revs)
  81. v_revs = 2
  82. v_flux = usb.read_track(revs = v_revs, ticks = v_ticks)
  83. v_flux.scale(flux.time_per_rev / drive.time_per_rev)
  84. verified = track.verify.verify_track(v_flux)
  85. if verified:
  86. verified_count += 1
  87. break
  88. formatter.print(" Retry %d" % (retry + 1))
  89. formatter.erase()
  90. error.check(verified, "Failed to write Track %u.%u" % (cyl, head))
  91. print()
  92. if not_verified_count == 0:
  93. print("All tracks verified")
  94. else:
  95. if verified_count == 0:
  96. s = "No tracks verified "
  97. else:
  98. s = ("%d tracks verified; %d tracks *not* verified "
  99. % (verified_count, not_verified_count))
  100. s += ("(Reason: Verify %s)"
  101. % ("unavailable", "disabled")[args.no_verify])
  102. print(s)
  103. class PrecompSpec:
  104. def __str__(self):
  105. s = "Precomp %s" % track.Precomp.TYPESTRING[self.type]
  106. for e in self.list:
  107. s += ", %d-:%dns" % e
  108. return s
  109. def track_precomp(self, cyl):
  110. for c,s in reversed(self.list):
  111. if cyl >= c:
  112. return track.Precomp(self.type, s)
  113. return None
  114. def importspec(self, spec):
  115. self.list = []
  116. self.type = track.Precomp.MFM
  117. for x in spec.split(':'):
  118. k,v = x.split('=')
  119. if k == 'type':
  120. self.type = track.Precomp.TYPESTRING.index(v.upper())
  121. else:
  122. self.list.append((int(k), int(v)))
  123. self.list.sort()
  124. def __init__(self, spec):
  125. try:
  126. self.importspec(spec)
  127. except:
  128. raise ValueError
  129. def main(argv):
  130. parser = util.ArgumentParser(usage='%(prog)s [options] file')
  131. parser.add_argument("--device", help="greaseweazle device name")
  132. parser.add_argument("--drive", type=util.drive_letter, default='A',
  133. help="drive to write (A,B,0,1,2)")
  134. parser.add_argument("--tracks", type=util.TrackSet,
  135. help="which tracks to write")
  136. parser.add_argument("--erase-empty", action="store_true",
  137. help="erase empty tracks (default: skip)")
  138. parser.add_argument("--no-verify", action="store_true",
  139. help="disable verify")
  140. parser.add_argument("--precomp", type=PrecompSpec,
  141. help="write precompensation")
  142. parser.add_argument("file", help="input filename")
  143. parser.description = description
  144. parser.prog += ' ' + argv[1]
  145. args = parser.parse_args(argv[2:])
  146. try:
  147. usb = util.usb_open(args.device)
  148. image = open_image(args)
  149. tracks = util.TrackSet('c=0-81:h=0-1')
  150. if args.tracks is not None:
  151. tracks.update_from_trackspec(args.tracks.trackspec)
  152. args.tracks = tracks
  153. s = str(args.tracks)
  154. if args.precomp is not None:
  155. s += "; %s" % args.precomp
  156. print("Writing %s" % s)
  157. util.with_drive_selected(write_from_image, usb, args, image)
  158. except USB.CmdError as error:
  159. print("Command Failed: %s" % error)
  160. if __name__ == "__main__":
  161. main(sys.argv)
  162. # Local variables:
  163. # python-indent: 4
  164. # End: