badline 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/CLAUDE.md +24 -6
  4. data/CONTRIBUTING.md +15 -9
  5. data/README.md +16 -5
  6. data/doc/media.md +28 -1
  7. data/doc/pinned-behaviour.md +31 -1
  8. data/exe/badline-ruby +1 -1
  9. data/lib/badline/c128/bus.rb +4 -17
  10. data/lib/badline/c128/drives.rb +108 -0
  11. data/lib/badline/c128/fast_serial.rb +63 -0
  12. data/lib/badline/c128/io_pages.rb +35 -0
  13. data/lib/badline/c128/mmu.rb +24 -7
  14. data/lib/badline/c128/modes.rb +17 -4
  15. data/lib/badline/c128/saved_state.rb +23 -0
  16. data/lib/badline/c128/z80.rb +49 -0
  17. data/lib/badline/c128/z80_pages.rb +118 -0
  18. data/lib/badline/c128/z80_turns.rb +106 -0
  19. data/lib/badline/c128.rb +25 -26
  20. data/lib/badline/cartridge/action_replay.rb +2 -2
  21. data/lib/badline/cartridge/comal80.rb +1 -7
  22. data/lib/badline/cartridge/dinamic.rb +0 -8
  23. data/lib/badline/cartridge/final_cartridge3.rb +6 -6
  24. data/lib/badline/cartridge/fun_play.rb +0 -8
  25. data/lib/badline/cartridge/game_system.rb +0 -8
  26. data/lib/badline/cartridge/kcs_power.rb +10 -7
  27. data/lib/badline/cartridge/mach5.rb +2 -4
  28. data/lib/badline/cartridge/magic_desk.rb +0 -8
  29. data/lib/badline/cartridge/ocean.rb +0 -8
  30. data/lib/badline/cartridge/pagefox.rb +2 -2
  31. data/lib/badline/cartridge/retro_replay.rb +7 -6
  32. data/lib/badline/cartridge/rex_utility.rb +2 -6
  33. data/lib/badline/cartridge/rgcd.rb +2 -2
  34. data/lib/badline/cartridge/simons_basic.rb +0 -5
  35. data/lib/badline/cartridge/super_games.rb +4 -2
  36. data/lib/badline/cartridge/westermann.rb +2 -6
  37. data/lib/badline/cartridge.rb +37 -9
  38. data/lib/badline/cia/fast_forward.rb +60 -0
  39. data/lib/badline/cia/serial.rb +12 -0
  40. data/lib/badline/cia/timer.rb +39 -0
  41. data/lib/badline/cia.rb +10 -5
  42. data/lib/badline/computer/attachments.rb +4 -50
  43. data/lib/badline/computer/true_drives.rb +80 -0
  44. data/lib/badline/computer.rb +4 -0
  45. data/lib/badline/core.rb +2 -0
  46. data/lib/badline/drive/core.rb +147 -0
  47. data/lib/badline/drive/fast_serial.rb +73 -0
  48. data/lib/badline/{drive1541 → drive}/idle.rb +18 -29
  49. data/lib/badline/drive/idle_cpu.rb +28 -0
  50. data/lib/badline/{drive1541 → drive}/orbit.rb +3 -5
  51. data/lib/badline/drive/serial_lines.rb +73 -0
  52. data/lib/badline/{drive1541 → drive}/sleep.rb +4 -5
  53. data/lib/badline/drive/vias.rb +84 -0
  54. data/lib/badline/drive/watch.rb +150 -0
  55. data/lib/badline/drive.rb +11 -0
  56. data/lib/badline/drive1541/bus.rb +5 -129
  57. data/lib/badline/drive1541/byte_ready.rb +48 -0
  58. data/lib/badline/drive1541/cpu.rb +1 -19
  59. data/lib/badline/drive1541/disk.rb +77 -20
  60. data/lib/badline/drive1541/disk_state.rb +7 -2
  61. data/lib/badline/drive1541/disk_via.rb +3 -0
  62. data/lib/badline/drive1541/mechanism.rb +48 -14
  63. data/lib/badline/drive1541/mechanism_state.rb +5 -4
  64. data/lib/badline/drive1541/rotation.rb +5 -5
  65. data/lib/badline/drive1541/serial_port.rb +4 -60
  66. data/lib/badline/drive1541.rb +14 -172
  67. data/lib/badline/drive1571/bus.rb +111 -0
  68. data/lib/badline/drive1571/cpu.rb +11 -0
  69. data/lib/badline/drive1571/serial_port.rb +37 -0
  70. data/lib/badline/drive1571/serial_via.rb +25 -0
  71. data/lib/badline/drive1571/wd1770.rb +120 -0
  72. data/lib/badline/drive1571.rb +175 -0
  73. data/lib/badline/drive1581/bus.rb +137 -0
  74. data/lib/badline/drive1581/cpu.rb +11 -0
  75. data/lib/badline/drive1581/disk.rb +135 -0
  76. data/lib/badline/drive1581/disk_state.rb +87 -0
  77. data/lib/badline/drive1581/mechanism.rb +117 -0
  78. data/lib/badline/drive1581/serial_port.rb +52 -0
  79. data/lib/badline/drive1581/slot.rb +78 -0
  80. data/lib/badline/drive1581/track.rb +100 -0
  81. data/lib/badline/drive1581/track_bytes.rb +75 -0
  82. data/lib/badline/drive1581/wd1772/registers.rb +89 -0
  83. data/lib/badline/drive1581/wd1772/sector_writes.rb +73 -0
  84. data/lib/badline/drive1581/wd1772/sectors.rb +150 -0
  85. data/lib/badline/drive1581/wd1772/state.rb +66 -0
  86. data/lib/badline/drive1581/wd1772/stepping.rb +109 -0
  87. data/lib/badline/drive1581/wd1772/tracks.rb +125 -0
  88. data/lib/badline/drive1581/wd1772/turn.rb +73 -0
  89. data/lib/badline/drive1581/wd1772.rb +216 -0
  90. data/lib/badline/drive1581.rb +233 -0
  91. data/lib/badline/frontend/app.rb +2 -4
  92. data/lib/badline/frontend/audio_device.rb +67 -0
  93. data/lib/badline/frontend/audio_sink.rb +16 -50
  94. data/lib/badline/frontend/boot.rb +18 -10
  95. data/lib/badline/frontend/buttons.rb +7 -7
  96. data/lib/badline/frontend/drive_led.rb +1 -1
  97. data/lib/badline/frontend/keys.rb +33 -8
  98. data/lib/badline/frontend/media_slots.rb +42 -0
  99. data/lib/badline/frontend/{confirmation.rb → menu/confirmation.rb} +4 -4
  100. data/lib/badline/frontend/{file_browser.rb → menu/file_browser.rb} +18 -34
  101. data/lib/badline/frontend/{menu_dialogs.rb → menu/menu_dialogs.rb} +3 -7
  102. data/lib/badline/frontend/{menu_media.rb → menu/menu_media.rb} +26 -51
  103. data/lib/badline/frontend/{menu_pages.rb → menu/menu_pages.rb} +7 -9
  104. data/lib/badline/frontend/{name_field.rb → menu/name_field.rb} +11 -24
  105. data/lib/badline/frontend/{pause_menu.rb → menu/pause_menu.rb} +13 -28
  106. data/lib/badline/frontend/menu/theme.rb +18 -0
  107. data/lib/badline/frontend/{filter_view.rb → player/filter_view.rb} +4 -4
  108. data/lib/badline/frontend/{info_view.rb → player/info_view.rb} +3 -3
  109. data/lib/badline/frontend/{player_window.rb → player/player_controller.rb} +17 -25
  110. data/lib/badline/frontend/{player_footer.rb → player/player_footer.rb} +8 -8
  111. data/lib/badline/frontend/{player_header.rb → player/player_header.rb} +5 -5
  112. data/lib/badline/frontend/{player_screen.rb → player/player_screen.rb} +4 -5
  113. data/lib/badline/frontend/{scope.rb → player/scope.rb} +1 -1
  114. data/lib/badline/frontend/{sid_view.rb → player/sid_view.rb} +16 -20
  115. data/lib/badline/frontend/player/theme.rb +18 -0
  116. data/lib/badline/frontend/{visualizer_view.rb → player/visualizer_view.rb} +8 -8
  117. data/lib/badline/frontend/sound.rb +19 -38
  118. data/lib/badline/frontend/timeline.rb +3 -14
  119. data/lib/badline/frontend.rb +24 -20
  120. data/lib/badline/iec_bus.rb +68 -6
  121. data/lib/badline/machine.rb +9 -6
  122. data/lib/badline/media/boot_disk.rb +50 -0
  123. data/lib/badline/media/not_disk.rb +8 -0
  124. data/lib/badline/media/true_drive.rb +48 -25
  125. data/lib/badline/media.rb +9 -3
  126. data/lib/badline/options/event.rb +2 -2
  127. data/lib/badline/options/family.rb +3 -9
  128. data/lib/badline/options/help.rb +3 -1
  129. data/lib/badline/options/table.rb +1 -1
  130. data/lib/badline/roms/README +18 -0
  131. data/lib/badline/roms/dos1571.rom +0 -0
  132. data/lib/badline/roms/dos1581.rom +0 -0
  133. data/lib/badline/sdl.rb +37 -20
  134. data/lib/badline/snapshot/state.rb +5 -4
  135. data/lib/badline/storage/d64_image.rb +3 -0
  136. data/lib/badline/storage/d71_image.rb +9 -4
  137. data/lib/badline/storage/g64_image.rb +7 -2
  138. data/lib/badline/storage/p00.rb +8 -0
  139. data/lib/badline/time_of_day.rb +9 -0
  140. data/lib/badline/version.rb +1 -1
  141. data/lib/badline/via/fast_forward.rb +3 -3
  142. data/lib/badline/vic20/attachments.rb +23 -3
  143. data/lib/badline/vic20/saved_state.rb +2 -0
  144. data/lib/badline/vic20.rb +3 -0
  145. data/lib/badline/z80/arithmetic.rb +117 -0
  146. data/lib/badline/z80/bit_page.rb +55 -0
  147. data/lib/badline/z80/block_io.rb +58 -0
  148. data/lib/badline/z80/block_transfers.rb +59 -0
  149. data/lib/badline/z80/bus_cycles.rb +111 -0
  150. data/lib/badline/z80/core.rb +113 -0
  151. data/lib/badline/z80/exchanges.rb +50 -0
  152. data/lib/badline/z80/extended_page.rb +89 -0
  153. data/lib/badline/z80/flags.rb +37 -0
  154. data/lib/badline/z80/interrupt_acceptance.rb +46 -0
  155. data/lib/badline/z80/jumps.rb +82 -0
  156. data/lib/badline/z80/loads.rb +98 -0
  157. data/lib/badline/z80/main_page.rb +134 -0
  158. data/lib/badline/z80/registers.rb +120 -0
  159. data/lib/badline/z80/saved_state.rb +69 -0
  160. data/lib/badline/z80/shifts.rb +64 -0
  161. data/lib/badline/z80.rb +11 -0
  162. metadata +89 -24
  163. /data/lib/badline/frontend/{menu_ports.rb → menu/menu_ports.rb} +0 -0
  164. /data/lib/badline/frontend/{player_commands.rb → player/player_commands.rb} +0 -0
  165. /data/lib/badline/frontend/{player_state.rb → player/player_state.rb} +0 -0
  166. /data/lib/badline/frontend/{sid_history.rb → player/sid_history.rb} +0 -0
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Drive1571
5
+ # What VIA 1's ports read from outside the chip. Port B carries the
6
+ # serial bus inputs and the device number jumpers, as on the 1541
7
+ # (Drive::SerialLines). Port A reads the mechanism on two inputs:
8
+ #
9
+ # PA0 track 0 sensor, low while the head sits on track 1
10
+ # PA7 BYTE READY, latched low from a whole byte until the CPU next
11
+ # reads or writes VIA 2
12
+ #
13
+ # The DOS steps the head out until PA0 reads low with the stepper on
14
+ # phase 0, and polls PA7 for each byte at 2 MHz, taking the byte from
15
+ # VIA 2's port A or putting the next one there after each. PA1 (fast serial
16
+ # direction), PA2 (side) and PA5 (1 or 2 MHz) are outputs, which
17
+ # Drive1571 follows (SerialVIA).
18
+ class SerialPort
19
+ include Drive::SerialLines
20
+
21
+ TRACK0 = 0x01
22
+ BYTE_READY = 0x80
23
+
24
+ def initialize(mechanism, device: 8, bus: IECBus.new)
25
+ @mechanism = mechanism
26
+ init_serial_lines(device, bus)
27
+ end
28
+
29
+ def read_a(_lines)
30
+ value = 0xff
31
+ value &= ~TRACK0 if @mechanism.half_track == Drive1541::Mechanism::MIN_HALF_TRACK
32
+ value &= ~BYTE_READY if @mechanism.byte_latched?
33
+ value
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Drive1571
5
+ # VIA 1, the one facing the serial bus. Its port A also selects the
6
+ # side, the clock rate and the fast serial direction, so the drive
7
+ # hears port A after every write and reset (Drive1571#port_a_written).
8
+ class SerialVIA < VIA
9
+ def initialize(start:, peripheral:, drive:)
10
+ @drive = drive
11
+ super(start:, peripheral:)
12
+ end
13
+
14
+ def reset!
15
+ super
16
+ @drive.port_a_written(port_a_output)
17
+ end
18
+
19
+ def poke(addr, value)
20
+ super
21
+ @drive.port_a_written(port_a_output)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Drive1571
5
+ # The WD1770 floppy controller at $2000, which DOS 3.0 drives for MFM
6
+ # disks. The 1571 here only ever holds GCR disks, so the controller
7
+ # never finds an MFM sector: it runs each command for its time and ends
8
+ # it as the WD177x data sheet does on a disk without one.
9
+ #
10
+ # $2000 status (read), command (write)
11
+ # $2001 track
12
+ # $2002 sector
13
+ # $2003 data
14
+ #
15
+ # BUSY sets as a command is written. A restore, seek or step (type I)
16
+ # moves the track register and ends a few milliseconds later, with
17
+ # TRACK 0 reported at track 0. A sector read or write, a read address
18
+ # or a read track ends with RECORD NOT FOUND after five turns of the
19
+ # disk, and a write track after one, with nothing written. A force
20
+ # interrupt ends the command at once. Time is the drive CPU's cycles.
21
+ class WD1770
22
+ BUSY = 0x01
23
+ TRACK0 = 0x04
24
+ NOT_FOUND = 0x10
25
+ MOTOR_ON = 0x80
26
+
27
+ # Cycles at 2 MHz, the 1571's clock in 1571 mode: a turn of the disk
28
+ # at 300 rpm, and a step with the head settling.
29
+ TURN = 400_000
30
+ STEP = 12_000
31
+
32
+ # The CPU whose cycles time the commands.
33
+ attr_writer :cpu
34
+
35
+ def initialize
36
+ @registers = [0, 0, 0, 0]
37
+ @status = 0
38
+ @due = 0
39
+ @result = 0
40
+ @cpu = nil
41
+ end
42
+
43
+ # Register +offset+, 0 to 3: the status, settled to the command's end
44
+ # once its time has run out.
45
+ def peek(offset)
46
+ return @registers[offset] unless offset.zero?
47
+
48
+ finish if @status.anybits?(BUSY) && @cpu.cycles >= @due
49
+ @status
50
+ end
51
+
52
+ def poke(offset, value)
53
+ offset.zero? ? command(value) : @registers[offset] = value
54
+ end
55
+
56
+ def save_state(out)
57
+ out.ints([*@registers, @status, @due, @result])
58
+ end
59
+
60
+ # A state from before the controller ran commands holds the four
61
+ # registers alone.
62
+ def load_state(input)
63
+ values = input.ints
64
+ @registers = values[0, 4]
65
+ @status = values[4] || 0
66
+ @due = values[5] || 0
67
+ @result = values[6] || 0
68
+ end
69
+
70
+ private
71
+
72
+ def command(value)
73
+ return interrupt if (value & 0xf0) == 0xd0
74
+
75
+ @registers[0] = value
76
+ value < 0x80 ? step(value) : search(value)
77
+ end
78
+
79
+ # A restore ($0x), seek ($1x) or step ($2x to $7x), its track register
80
+ # moved at once.
81
+ def step(value)
82
+ track = @registers[1]
83
+ target = case value >> 4
84
+ when 0 then 0
85
+ when 1 then @registers[3]
86
+ else track + step_direction(value)
87
+ end
88
+ @registers[1] = target.clamp(0, 255) if value < 0x20 || value.anybits?(0x10)
89
+ start(STEP * [(target - track).abs, 1].max, @registers[1].zero? ? TRACK0 : 0)
90
+ end
91
+
92
+ # Step in ($4x) moves toward higher tracks, step out ($6x) toward
93
+ # track 0, and step ($2x) the way the last one went.
94
+ def step_direction(value)
95
+ @direction = 1 if (value >> 5) == 2
96
+ @direction = -1 if (value >> 5) == 3
97
+ @direction || 1
98
+ end
99
+
100
+ # A sector or track command that finds no MFM sector.
101
+ def search(value)
102
+ value >= 0xf0 ? start(TURN, 0) : start(TURN * 5, NOT_FOUND)
103
+ end
104
+
105
+ def start(cycles, result)
106
+ @status = BUSY | MOTOR_ON
107
+ @due = @cpu.cycles + cycles
108
+ @result = result
109
+ end
110
+
111
+ def finish
112
+ @status = @result
113
+ end
114
+
115
+ def interrupt
116
+ @status &= ~BUSY & 0xff
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "badline/drive1571/cpu"
4
+ require "badline/drive1571/wd1770"
5
+ require "badline/drive1571/serial_port"
6
+ require "badline/drive1571/serial_via"
7
+ require "badline/drive1571/bus"
8
+
9
+ module Badline
10
+ # The 1571 disk drive, the C128D's built-in one: the 1541's 6502, two
11
+ # VIAs and GCR mechanism, with a second head, a 6526 CIA for fast
12
+ # serial, a WD1770 for MFM disks and DOS 3.0 in 32 KB of ROM. Drive::Core
13
+ # clocks it against the host and sleeps through the DOS's idle loop.
14
+ #
15
+ # VIA 1's port A picks the clock, 1 or 2 MHz on PA5, and the head on
16
+ # PA2, and reads the track 0 sensor and BYTE READY (SerialPort). The
17
+ # DOS resets at 2 MHz and drops to 1 MHz as a 1541 would run, which is
18
+ # how it stays until a C128 asks for fast serial. The disk turns at the
19
+ # same speed either way (Drive1541::Mechanism#clock_hz=).
20
+ #
21
+ # The CIA sits at $4000, on the IRQ line with both VIAs. Only its serial
22
+ # port is wired, for fast serial through buffers PA1 turns round
23
+ # (Drive::FastSerial). The drive sleeps only while the CIA is quiet
24
+ # (CIA#quiet?).
25
+ class Drive1571
26
+ include Drive::Core
27
+ include Drive::VIAs
28
+ include Drive::FastSerial
29
+
30
+ CLOCK_HZ = 1_000_000
31
+ FAST_CLOCK_HZ = 2_000_000
32
+
33
+ # Where the DOS 3.0 idle loop starts over, as DOS 2.6's does.
34
+ IDLE_LOOP = 0xebff
35
+
36
+ # VIA 1's port A outputs.
37
+ FAST_SERIAL_OUT = 0x02
38
+ SIDE = 0x04
39
+ FAST = 0x20
40
+
41
+ def cia
42
+ settle!
43
+ @cia
44
+ end
45
+
46
+ def fdc
47
+ settle!
48
+ @fdc
49
+ end
50
+
51
+ def model_name = "1571"
52
+
53
+ # +rom+ covers $8000-$FFFF, and defaults to the DOS image in the ROM
54
+ # path. +device+ is the number the jumpers on VIA 1's PB5 and PB6 set,
55
+ # 8 to 11.
56
+ def initialize(rom: nil, host_clock_hz: CLOCK_HZ, device: 8, debug: false)
57
+ @device = device
58
+ @clock_hz = CLOCK_HZ
59
+ init_fast_serial
60
+ @mechanism = Drive1541::Mechanism.new(slip: 2)
61
+ @serial_port = SerialPort.new(@mechanism, device:)
62
+ @via1 = SerialVIA.new(start: 0x1800, peripheral: @serial_port, drive: self)
63
+ @via2 = Drive1541::DiskVIA.new(start: 0x1c00, mechanism: @mechanism)
64
+ @cia = CIA.new(start: 0x4000)
65
+ @fdc = WD1770.new
66
+ @bus = Bus.new(rom: rom || ROM.load("dos1571.rom", 0x8000), via1: @via1, via2: @via2, cia: @cia, fdc: @fdc)
67
+ @cpu = CPU.new(@bus, debug:)
68
+ @fdc.cpu = @cpu
69
+ @idle_loop = IDLE_LOOP
70
+ @host_clock_hz = host_clock_hz
71
+ @phase = 0
72
+ @cycles = 0
73
+ @serial_bus = nil
74
+ init_idle(debug)
75
+ # CA1 powers up at the level of a released ATN, without an edge.
76
+ @via1.ca1 = false
77
+ @via1.reset!
78
+ connect(IECBus.new)
79
+ end
80
+
81
+ # The serial bus's RESET line reaches the CPU, both VIAs and the CIA.
82
+ # RAM keeps its contents.
83
+ def reset!
84
+ settle!
85
+ @via1.reset!
86
+ @via2.reset!
87
+ @cia.reset!
88
+ @cpu.reset!
89
+ end
90
+
91
+ # VIA 1 drives port A to +lines+: the side, the clock rate and the fast
92
+ # serial direction follow it.
93
+ def port_a_written(lines)
94
+ @mechanism.side = lines.anybits?(SIDE) ? 1 : 0
95
+ fast_serial_out = lines.anybits?(FAST_SERIAL_OUT)
96
+ turn_fast_serial(fast_serial_out) if fast_serial_out != @fast_serial_out
97
+ clock_hz = lines.anybits?(FAST) ? FAST_CLOCK_HZ : CLOCK_HZ
98
+ return if clock_hz == @clock_hz
99
+
100
+ @clock_hz = clock_hz
101
+ @mechanism.clock_hz = clock_hz
102
+ end
103
+
104
+ # Whether the drive runs at 2 MHz.
105
+ def fast? = @clock_hz == FAST_CLOCK_HZ
106
+
107
+ # VIA 1's port B, with the fast serial pins pulling DATA and SRQ
108
+ # (IECBus::DRIVE_FAST_DATA and DRIVE_FAST_SRQ) above it.
109
+ def serial_output = @via1.port_b_output | @fast_output
110
+
111
+ # The drive's whole state, as the 1541's (Drive1541#save_state), with
112
+ # the CIA and the WD1770's registers. The clock rate, the side and the
113
+ # fast serial direction follow from VIA 1's port A.
114
+ def save_state(out)
115
+ settle!
116
+ out.marker("DRIVE1571")
117
+ out.int(@phase).int(@cycles).boolean(@mechanism.so_pending)
118
+ @serial_port.save_state(out)
119
+ @via1.save_state(out)
120
+ @via2.save_state(out)
121
+ @cia.save_state(out)
122
+ @fdc.save_state(out)
123
+ @bus.save_state(out)
124
+ @cpu.save_state(out)
125
+ @mechanism.save_state(out)
126
+ end
127
+
128
+ def load_state(input)
129
+ settle!
130
+ input.marker("DRIVE1571")
131
+ @phase = input.int
132
+ @cycles = input.int
133
+ @mechanism.so_pending = input.boolean?
134
+ @serial_port.load_state(input)
135
+ @via1.load_state(input)
136
+ @via2.load_state(input)
137
+ @cia.load_state(input)
138
+ @fdc.load_state(input)
139
+ @bus.load_state(input)
140
+ @cpu.load_state(input)
141
+ @mechanism.load_state(input)
142
+ @fast_serial_out = @via1.port_a_output.anybits?(FAST_SERIAL_OUT)
143
+ port_a_written(@via1.port_a_output)
144
+ push_fast_output(@fast_serial_out ? fast_pins : 0)
145
+ forget_orbits
146
+ end
147
+
148
+ private
149
+
150
+ # One drive cycle, as the 1541's (Drive1541#step), with the CIA
151
+ # clocked beside the VIAs and pulling IRQ with them.
152
+ def step
153
+ so = @mechanism.take_so
154
+ @mechanism.cycle!
155
+ @via1.ca1 = @serial_port.atn_low?
156
+ @via1.cycle!
157
+ @via2.cycle!
158
+ @cia.cycle!
159
+ drive_fast_serial if @fast_serial_out
160
+ @cpu.irq = @via1.irq? || @via2.irq? || @cia.interrupted?
161
+ @cpu.so! if so
162
+ @cpu.cycle!
163
+ @cycles += 1
164
+ end
165
+
166
+ # The VIAs' quiet cycles while the CIA is quiet, and none otherwise.
167
+ def quiet_cycles
168
+ @cia.quiet? ? [@via1.quiet_cycles, @via2.quiet_cycles].min : 0
169
+ end
170
+
171
+ def fast_forward_more(cycles)
172
+ @cia.fast_forward(cycles)
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Drive1581
5
+ # The drive CPU's bus, as the 74LS139 decodes A13-A15 (1581 Service
6
+ # Manual, memory map and schematic sheet 1):
7
+ #
8
+ # $0000-$1FFF - 8 KB of RAM
9
+ # $2000-$3FFF - nothing: open bus
10
+ # $4000-$5FFF - the 8520, its sixteen registers mirrored
11
+ # $6000-$7FFF - the WD1772, its four registers mirrored
12
+ # $8000-$FFFF - the DOS ROM
13
+ #
14
+ # Writes to the ROM and to open bus go nowhere, and a read from open
15
+ # bus finds the last byte on the data bus, as on the 1541. A write to
16
+ # either of the 8520's ports or their direction registers moves the
17
+ # drive's lines (Drive1581#ports_written).
18
+ #
19
+ # While Idle records a pass of the DOS's idle loop, the bus watches it
20
+ # (watch!): the RAM it reads and writes, and whether it touched the
21
+ # 8520 or the WD1772, either of which makes the pass volatile. While
22
+ # Idle looks for an orbit, the bus guards the drive (guard!), and any
23
+ # access to either chip taints it.
24
+ class Bus
25
+ include Addressable
26
+
27
+ attr_reader :ram, :rom, :cia, :fdc, :data, :volatile, :touched, :tainted, :touched_timers
28
+
29
+ def initialize(rom:, cia:, fdc:, drive:)
30
+ addressable_at(0, length: 2**16)
31
+ @ram = Memory.new([], length: 0x2000, start: 0)
32
+ @rom = rom
33
+ @cia = cia
34
+ @fdc = fdc
35
+ @drive = drive
36
+ @data = 0
37
+ @recording = @guarding = @watching = false
38
+ @volatile = @tainted = false
39
+ @touched = {}
40
+ @touched_timers = 0
41
+ end
42
+
43
+ # The last byte on the data bus and the RAM. What a watched stretch
44
+ # touched is Idle's, and a restored bus isn't watching.
45
+ def save_state(out)
46
+ out.int(@data)
47
+ @ram.save_state(out)
48
+ end
49
+
50
+ def load_state(input)
51
+ @data = input.int
52
+ @ram.load_state(input)
53
+ @watching = false
54
+ end
55
+
56
+ def peek(addr)
57
+ return @data = @rom.peek(addr) if addr >= 0x8000
58
+ return @data = chip_peek(addr) if addr >= 0x2000
59
+
60
+ touch(addr) if @recording
61
+ @data = @ram.peek(addr)
62
+ end
63
+
64
+ def poke(addr, value)
65
+ @data = value
66
+ return if addr >= 0x8000
67
+ return chip_poke(addr, value) if addr >= 0x2000
68
+
69
+ touch(addr) if @recording
70
+ @ram.poke(addr, value)
71
+ end
72
+
73
+ # Starts watching: see volatile and touched.
74
+ def watch!
75
+ @recording = true
76
+ @volatile = false
77
+ @touched = {}
78
+ watching!
79
+ end
80
+
81
+ def unwatch!
82
+ @recording = false
83
+ watching!
84
+ end
85
+
86
+ # Starts guarding: see tainted.
87
+ def guard!
88
+ @guarding = true
89
+ @tainted = false
90
+ watching!
91
+ end
92
+
93
+ private
94
+
95
+ def chip_peek(addr)
96
+ watch_chip if @watching
97
+ if addr >= 0x6000
98
+ @fdc.peek(addr & 0x03)
99
+ elsif addr >= 0x4000
100
+ @cia.peek(0x4000 | (addr & 0x0f))
101
+ else
102
+ @data
103
+ end
104
+ end
105
+
106
+ def chip_poke(addr, value)
107
+ watch_chip if @watching
108
+ if addr >= 0x6000
109
+ @fdc.poke(addr & 0x03, value)
110
+ elsif addr >= 0x4000
111
+ @cia.poke(0x4000 | (addr & 0x0f), value)
112
+ @drive.ports_written if addr.nobits?(0x0c)
113
+ end
114
+ end
115
+
116
+ def touch(addr)
117
+ @touched[addr] = @ram.peek(addr) unless @touched.key?(addr)
118
+ end
119
+
120
+ def watching!
121
+ @watching = @recording || @guarding
122
+ end
123
+
124
+ def watch_chip
125
+ if @recording
126
+ @volatile = true
127
+ @recording = false
128
+ end
129
+ if @guarding
130
+ @tainted = true
131
+ @guarding = false
132
+ end
133
+ watching!
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Drive1581
5
+ # The drive's 6502, on the drive's Bus.
6
+ class CPU
7
+ include Badline::CPU::Core
8
+ include Drive::IdleCPU
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "badline/drive1581/disk_state"
4
+
5
+ module Badline
6
+ class Drive1581
7
+ # A 3.5" double-sided disk as the WD1772 sees it: a Track on each side
8
+ # of 80 cylinders, or none where it was never formatted.
9
+ #
10
+ # A .d81 image holds the 1581 DOS's logical blocks in order, 40 of 256
11
+ # bytes on each logical track 1 to 80 (1581 Service Manual, format
12
+ # organization). Logical track t is cylinder t - 1, and the DOS puts
13
+ # its sectors 0 to 19 on side 0 and 20 to 39 on side 1, two to each
14
+ # 512-byte physical sector, numbered from 1. So physical sector s on
15
+ # side h of cylinder c holds logical sectors 20h + 2(s - 1) and the one
16
+ # after it, and every cylinder of the image is a standard Track
17
+ # (Track.standard) on both sides. A block the image's error table
18
+ # marks 20 or 21 loses its sector's ID field, 27 spoils the ID's CRC,
19
+ # 22 loses the data field and 23 spoils its CRC.
20
+ #
21
+ # The WD1772 writes into the tracks, and flush stores the standard
22
+ # sectors of each one written back in the image, which writes its host
23
+ # file. A track formatted another way stays on the disk alone. The disk
24
+ # is write-protected when the image won't take writes.
25
+ class Disk
26
+ include State
27
+
28
+ CYLINDERS = 80
29
+ SIDES = 2
30
+
31
+ # Logical blocks a side of a cylinder holds.
32
+ SIDE_BLOCKS = 20
33
+
34
+ # A logical block's bytes, and how many a physical sector holds.
35
+ BLOCK_SIZE = 256
36
+ BLOCKS = Track::SECTOR_SIZE / BLOCK_SIZE
37
+
38
+ # A disk from the .d81 image at +path+. `read_only` opens the image
39
+ # write-protected.
40
+ def self.open(path, read_only: false)
41
+ new(Storage::D81Image.new(path, read_only:)).opened(path, read_only)
42
+ end
43
+
44
+ attr_reader :image
45
+
46
+ def initialize(image = nil)
47
+ @image = image
48
+ @tracks = Array.new(CYLINDERS * SIDES)
49
+ @written = {}
50
+ @changed = {}
51
+ @path = nil
52
+ @read_only = false
53
+ end
54
+
55
+ # The Track on +side+ of +cylinder+, or nil where there is none: past
56
+ # the image's cylinders, or on a disk without an image that the head
57
+ # never formatted.
58
+ def track(cylinder, side)
59
+ index = (cylinder * SIDES) + side
60
+ return @tracks[index] if @tracks[index] || !@image || cylinder >= CYLINDERS
61
+
62
+ @tracks[index] = image_track(cylinder, side)
63
+ end
64
+
65
+ # A write track laid +track+ down on +side+ of +cylinder+, or a write
66
+ # sector changed it.
67
+ def write(cylinder, side, track)
68
+ return if cylinder >= CYLINDERS
69
+
70
+ index = (cylinder * SIDES) + side
71
+ @tracks[index] = track
72
+ @written[index] = true
73
+ @changed[index] = true
74
+ end
75
+
76
+ # Whether the write-protect hole is open: the disk's image won't take
77
+ # writes, or a disk without an image was opened read-only.
78
+ def write_protected? = @image.nil? ? @read_only : !@image.writable?
79
+
80
+ # Whether any track was written since the last flush.
81
+ def written? = !@written.empty?
82
+
83
+ # Stores the standard sectors of each track written since the last
84
+ # flush in the image, all in one write to its host file.
85
+ def flush
86
+ written = @written.keys.sort
87
+ @written.clear
88
+ return if @image.nil? || written.empty?
89
+
90
+ @image.store_blocks(written.flat_map { |index| track_blocks(index / SIDES, index % SIDES) })
91
+ end
92
+
93
+ private
94
+
95
+ def image_track(cylinder, side)
96
+ track = Track.standard(cylinder, side) { |sector| physical_block(cylinder, side, sector) }
97
+ track.sectors.each { |sector| mark_errors(cylinder, side, sector) }
98
+ track.sectors.reject! { |sector| sector.id.nil? }
99
+ track
100
+ end
101
+
102
+ # The logical track and first sector physical sector +sector+ holds.
103
+ def logical(cylinder, side, sector) = [cylinder + 1, (side * SIDE_BLOCKS) + ((sector - 1) * BLOCKS)]
104
+
105
+ def physical_block(cylinder, side, sector)
106
+ track, first = logical(cylinder, side, sector)
107
+ Array.new(BLOCKS) { |i| @image.read_block(track, first + i) || [] }.flatten
108
+ end
109
+
110
+ def mark_errors(cylinder, side, sector)
111
+ track, first = logical(cylinder, side, sector.id.bytes[2])
112
+ errors = Array.new(BLOCKS) { |i| @image.block_error(track, first + i) }
113
+ sector.id.good = false if errors.include?(27)
114
+ sector.data.good = false if errors.include?(23)
115
+ sector.data = nil if errors.include?(22)
116
+ sector.id = nil if errors.include?(20) || errors.include?(21)
117
+ end
118
+
119
+ # [track, sector, data, nil] for DiskImage#store_blocks, for each
120
+ # logical block a standard sector of the track holds.
121
+ def track_blocks(cylinder, side)
122
+ track = @tracks[(cylinder * SIDES) + side]
123
+ Track::SECTORS.times.flat_map do |index|
124
+ data = track.sector([cylinder, side, index + 1, Track::SIZE_CODE])&.data
125
+ next [] unless data
126
+
127
+ logical_track, first = logical(cylinder, side, index + 1)
128
+ Array.new(BLOCKS) do |i|
129
+ [logical_track, first + i, data.bytes[i * BLOCK_SIZE, BLOCK_SIZE], nil]
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end