write.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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
  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. flux = track.flux_for_writeout()
  50. # @factor adjusts flux times for speed variations between the
  51. # read-in and write-out drives.
  52. factor = drive.ticks_per_rev / flux.index_list[0]
  53. # Convert the flux samples to Greaseweazle sample frequency.
  54. rem = 0.0
  55. flux_list = []
  56. for x in flux.list:
  57. y = x * factor + rem
  58. val = round(y)
  59. rem = y - val
  60. flux_list.append(val)
  61. # Encode the flux times for Greaseweazle, and write them out.
  62. formatter = Formatter()
  63. verified = False
  64. for retry in range(3):
  65. usb.write_track(flux_list = flux_list,
  66. cue_at_index = flux.index_cued,
  67. terminate_at_index = flux.terminate_at_index)
  68. try:
  69. no_verify = args.no_verify or track.verify is None
  70. except AttributeError: # track.verify undefined
  71. no_verify = True
  72. if no_verify:
  73. not_verified_count += 1
  74. verified = True
  75. break
  76. v_revs, v_ticks = track.verify_revs, 0
  77. if isinstance(v_revs, float):
  78. v_ticks = int(drive.ticks_per_rev * v_revs)
  79. v_revs = 2
  80. v_flux = usb.read_track(revs = v_revs, ticks = v_ticks)
  81. v_flux.scale(flux.time_per_rev / drive.time_per_rev)
  82. verified = track.verify.verify_track(v_flux)
  83. if verified:
  84. verified_count += 1
  85. break
  86. formatter.print(" Retry %d" % (retry + 1))
  87. formatter.erase()
  88. error.check(verified, "Failed to write Track %u.%u" % (cyl, head))
  89. print()
  90. if not_verified_count == 0:
  91. print("All tracks verified")
  92. else:
  93. if verified_count == 0:
  94. s = "No tracks verified "
  95. else:
  96. s = ("%d tracks verified; %d tracks *not* verified "
  97. % (verified_count, not_verified_count))
  98. s += ("(Reason: Verify %s)"
  99. % ("unavailable", "disabled")[args.no_verify])
  100. print(s)
  101. def main(argv):
  102. parser = util.ArgumentParser(usage='%(prog)s [options] file')
  103. parser.add_argument("--device", help="greaseweazle device name")
  104. parser.add_argument("--drive", type=util.drive_letter, default='A',
  105. help="drive to write (A,B,0,1,2)")
  106. parser.add_argument("--tracks", type=util.TrackSet,
  107. help="which tracks to write")
  108. parser.add_argument("--erase-empty", action="store_true",
  109. help="erase empty tracks (default: skip)")
  110. parser.add_argument("--no-verify", action="store_true",
  111. help="disable verify")
  112. parser.add_argument("file", help="input filename")
  113. parser.description = description
  114. parser.prog += ' ' + argv[1]
  115. args = parser.parse_args(argv[2:])
  116. try:
  117. usb = util.usb_open(args.device)
  118. image = open_image(args)
  119. tracks = util.TrackSet('c=0-81:h=0-1')
  120. if args.tracks is not None:
  121. tracks.update_from_trackspec(args.tracks.trackspec)
  122. args.tracks = tracks
  123. print("Writing %s" % (args.tracks))
  124. util.with_drive_selected(write_from_image, usb, args, image)
  125. except USB.CmdError as error:
  126. print("Command Failed: %s" % error)
  127. if __name__ == "__main__":
  128. main(sys.argv)
  129. # Local variables:
  130. # python-indent: 4
  131. # End: