write.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 usb as USB
  13. # Read and parse the image file.
  14. def open_image(args):
  15. image_class = util.get_image_class(args.file)
  16. if hasattr(image_class, 'from_filename'):
  17. image = image_class.from_filename(args.file)
  18. else:
  19. with open(args.file, "rb") as f:
  20. image = image_class.from_file(f.read())
  21. return image
  22. # write_from_image:
  23. # Writes the specified image file to floppy disk.
  24. def write_from_image(usb, args, image):
  25. # @drive_ticks is the time in Greaseweazle ticks between index pulses.
  26. # We will adjust the flux intervals per track to allow for this.
  27. flux = usb.read_track(2)
  28. drive_ticks = (flux.index_list[0] + flux.index_list[1]) / 2
  29. del flux
  30. for cyl in range(args.scyl, args.ecyl+1):
  31. for side in range(0, args.nr_sides):
  32. track = image.get_track(cyl, side, writeout=True)
  33. if track is None and not args.erase_empty:
  34. continue
  35. print("\r%sing Track %u.%u..." %
  36. ("Writ" if track is not None else "Eras", cyl, side), end="")
  37. usb.seek((cyl, cyl*2)[args.double_step], side)
  38. if track is None:
  39. usb.erase_track(drive_ticks * 1.1)
  40. continue
  41. flux = track.flux_for_writeout()
  42. # @factor adjusts flux times for speed variations between the
  43. # read-in and write-out drives.
  44. factor = drive_ticks / flux.index_list[0]
  45. # Convert the flux samples to Greaseweazle sample frequency.
  46. rem = 0.0
  47. flux_list = []
  48. for x in flux.list:
  49. y = x * factor + rem
  50. val = int(round(y))
  51. rem = y - val
  52. flux_list.append(val)
  53. # Encode the flux times for Greaseweazle, and write them out.
  54. usb.write_track(flux_list, flux.terminate_at_index)
  55. print()
  56. def main(argv):
  57. parser = util.ArgumentParser()
  58. parser.add_argument("--drive", type=util.drive_letter, default='A',
  59. help="drive to write (A,B,0,1,2)")
  60. parser.add_argument("--scyl", type=int, default=0,
  61. help="first cylinder to write")
  62. parser.add_argument("--ecyl", type=int, default=81,
  63. help="last cylinder to write")
  64. parser.add_argument("--single-sided", action="store_true",
  65. help="single-sided write")
  66. parser.add_argument("--double-step", action="store_true",
  67. help="double-step drive heads")
  68. parser.add_argument("--erase-empty", action="store_true",
  69. help="erase empty tracks (default: skip)")
  70. parser.add_argument("file", help="input filename")
  71. parser.add_argument("device", nargs="?", help="serial device")
  72. parser.description = description
  73. parser.prog += ' ' + argv[1]
  74. args = parser.parse_args(argv[2:])
  75. args.nr_sides = 1 if args.single_sided else 2
  76. try:
  77. usb = util.usb_open(args.device)
  78. image = open_image(args)
  79. util.with_drive_selected(write_from_image, usb, args, image)
  80. except USB.CmdError as error:
  81. print("Command Failed: %s" % error)
  82. if __name__ == "__main__":
  83. main(sys.argv)
  84. # Local variables:
  85. # python-indent: 4
  86. # End: