badline 0.16.1 → 0.17.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 (143) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +60 -0
  3. data/CLAUDE.md +1 -1
  4. data/README.md +102 -12
  5. data/doc/media.md +5 -2
  6. data/doc/pinned-behaviour.md +61 -0
  7. data/doc/snapshot-format.md +376 -69
  8. data/doc/snapshots.md +148 -14
  9. data/lib/badline/cbm2/acia.rb +10 -0
  10. data/lib/badline/cbm2/bus.rb +263 -0
  11. data/lib/badline/cbm2/cpu.rb +58 -0
  12. data/lib/badline/cbm2/crtc_video.rb +133 -0
  13. data/lib/badline/cbm2/ieee_drive.rb +18 -0
  14. data/lib/badline/cbm2/ieee_layout.rb +69 -0
  15. data/lib/badline/cbm2/ieee_ports.rb +104 -0
  16. data/lib/badline/cbm2/ieee_traps.rb +81 -0
  17. data/lib/badline/cbm2/keyboard_ports.rb +60 -0
  18. data/lib/badline/cbm2/keyed_modules.rb +67 -0
  19. data/lib/badline/cbm2/keyed_state.rb +91 -0
  20. data/lib/badline/cbm2/model.rb +44 -0
  21. data/lib/badline/cbm2/p500/control_ports.rb +80 -0
  22. data/lib/badline/cbm2/p500/ieee_layout.rb +41 -0
  23. data/lib/badline/cbm2/p500/keyed_state.rb +85 -0
  24. data/lib/badline/cbm2/p500/model.rb +39 -0
  25. data/lib/badline/cbm2/p500/pages.rb +57 -0
  26. data/lib/badline/cbm2/p500.rb +262 -0
  27. data/lib/badline/cbm2/sid_page.rb +19 -0
  28. data/lib/badline/cbm2.rb +267 -0
  29. data/lib/badline/{plus4 → chips}/acia/frames.rb +1 -1
  30. data/lib/badline/chips/acia/keyed_state.rb +47 -0
  31. data/lib/badline/chips/acia.rb +307 -0
  32. data/lib/badline/chips/crtc.rb +3 -2
  33. data/lib/badline/chips/riot/fast_forward.rb +72 -0
  34. data/lib/badline/chips/riot/keyed_parts.rb +69 -0
  35. data/lib/badline/chips/riot/timer.rb +80 -0
  36. data/lib/badline/chips/riot.rb +162 -0
  37. data/lib/badline/chips/rriot/keyed_state.rb +30 -0
  38. data/lib/badline/chips/rriot.rb +132 -0
  39. data/lib/badline/chips/tpi/keyed_state.rb +38 -0
  40. data/lib/badline/chips/tpi.rb +233 -0
  41. data/lib/badline/chips/vic/bank.rb +60 -5
  42. data/lib/badline/chips/vic/sprite.rb +4 -3
  43. data/lib/badline/drives/datasette/keyed_state.rb +9 -1
  44. data/lib/badline/drives/datasette.rb +27 -3
  45. data/lib/badline/drives/model8050/disk.rb +163 -0
  46. data/lib/badline/drives/model8050/fdc_bus.rb +102 -0
  47. data/lib/badline/drives/model8050/idle.rb +177 -0
  48. data/lib/badline/drives/model8050/ieee_port.rb +147 -0
  49. data/lib/badline/drives/model8050/ip_bus.rb +93 -0
  50. data/lib/badline/drives/model8050/keyed_state.rb +160 -0
  51. data/lib/badline/drives/model8050/mechanism.rb +230 -0
  52. data/lib/badline/drives/model8050/parts.rb +37 -0
  53. data/lib/badline/drives/model8050.rb +243 -0
  54. data/lib/badline/frontend/app.rb +10 -3
  55. data/lib/badline/frontend/boot.rb +1 -1
  56. data/lib/badline/frontend/controls.rb +51 -15
  57. data/lib/badline/frontend/gamepads.rb +3 -3
  58. data/lib/badline/frontend/keys.rb +61 -2
  59. data/lib/badline/frontend/media_slots.rb +1 -1
  60. data/lib/badline/frontend/menu/menu_media.rb +14 -11
  61. data/lib/badline/frontend/menu/menu_pages.rb +18 -4
  62. data/lib/badline/frontend/menu/menu_ports.rb +40 -4
  63. data/lib/badline/frontend/menu/pause_menu.rb +1 -1
  64. data/lib/badline/frontend/pet_keyboard.rb +23 -7
  65. data/lib/badline/frontend/timeline.rb +19 -10
  66. data/lib/badline/kernal_trap/dos/disk_commands.rb +80 -0
  67. data/lib/badline/kernal_trap/dos/status.rb +3 -0
  68. data/lib/badline/kernal_trap/dos.rb +18 -5
  69. data/lib/badline/kernal_trap/ieee.rb +4 -2
  70. data/lib/badline/kernal_trap/ieee_layout.rb +95 -43
  71. data/lib/badline/machine/ieee_drives.rb +194 -0
  72. data/lib/badline/machine/settling.rb +4 -0
  73. data/lib/badline/machine.rb +23 -6
  74. data/lib/badline/media/cbm2_media.rb +29 -0
  75. data/lib/badline/media/program.rb +6 -3
  76. data/lib/badline/media/true_drive.rb +38 -9
  77. data/lib/badline/media.rb +15 -6
  78. data/lib/badline/options/family.rb +15 -3
  79. data/lib/badline/options/help.rb +16 -2
  80. data/lib/badline/options/table.rb +18 -4
  81. data/lib/badline/options/validation.rb +27 -3
  82. data/lib/badline/options.rb +21 -7
  83. data/lib/badline/pet/cassettes.rb +22 -1
  84. data/lib/badline/pet/ieee_drive.rb +5 -64
  85. data/lib/badline/pet/ieee_ports.rb +7 -0
  86. data/lib/badline/pet.rb +15 -4
  87. data/lib/badline/plus4/acia.rb +4 -298
  88. data/lib/badline/plus4/keyed_parts.rb +0 -44
  89. data/lib/badline/plus4.rb +1 -0
  90. data/lib/badline/roms/8050/dos-2.7.rom +0 -0
  91. data/lib/badline/roms/8050/fdc-901869-01.rom +0 -0
  92. data/lib/badline/roms/8050/fdc-901885-04.rom +0 -0
  93. data/lib/badline/roms/README +111 -0
  94. data/lib/badline/roms/cbm2/basic128-02b.rom +0 -0
  95. data/lib/badline/roms/cbm2/basic128.rom +0 -0
  96. data/lib/badline/roms/cbm2/basic256.rom +0 -0
  97. data/lib/badline/roms/cbm2/characters-600.rom +0 -0
  98. data/lib/badline/roms/cbm2/characters-700.rom +0 -0
  99. data/lib/badline/roms/cbm2/kernal-03b.rom +0 -0
  100. data/lib/badline/roms/cbm2/kernal-04a.rom +0 -0
  101. data/lib/badline/roms/cbm2/p500-basic.rom +0 -0
  102. data/lib/badline/roms/cbm2/p500-kernal.rom +0 -0
  103. data/lib/badline/snapshot/cbm2_setup.rb +53 -0
  104. data/lib/badline/snapshot/container.rb +7 -2
  105. data/lib/badline/snapshot/field_doc/markdown.rb +4 -2
  106. data/lib/badline/snapshot/field_doc.rb +38 -3
  107. data/lib/badline/snapshot/image.rb +62 -9
  108. data/lib/badline/snapshot/vice/cbm2.rb +93 -0
  109. data/lib/badline/snapshot/vice/cbm2_acia.rb +75 -0
  110. data/lib/badline/snapshot/vice/cbm2_crtc.rb +127 -0
  111. data/lib/badline/snapshot/vice/cbm2_mains.rb +49 -0
  112. data/lib/badline/snapshot/vice/cbm2_mem.rb +75 -0
  113. data/lib/badline/snapshot/vice/cbm2_restore.rb +204 -0
  114. data/lib/badline/snapshot/vice/cbm2_tpi.rb +58 -0
  115. data/lib/badline/snapshot/vice/cias.rb +13 -9
  116. data/lib/badline/snapshot/vice/p500.rb +96 -0
  117. data/lib/badline/snapshot/vice/p500_restore.rb +130 -0
  118. data/lib/badline/snapshot/vice/p500_vicii.rb +166 -0
  119. data/lib/badline/snapshot/vice/pet.rb +7 -3
  120. data/lib/badline/snapshot/vice/pet_cpu.rb +5 -2
  121. data/lib/badline/snapshot/vice/pet_crtc.rb +10 -15
  122. data/lib/badline/snapshot/vice/pet_drive.rb +180 -0
  123. data/lib/badline/snapshot/vice/pet_drive8050.rb +216 -0
  124. data/lib/badline/snapshot/vice/pet_restore.rb +70 -6
  125. data/lib/badline/snapshot/vice/pet_via.rb +6 -5
  126. data/lib/badline/snapshot/vice/sid_registers.rb +2 -1
  127. data/lib/badline/snapshot/vice/vicii_import.rb +10 -6
  128. data/lib/badline/snapshot/vice.rb +3 -0
  129. data/lib/badline/snapshot.rb +11 -3
  130. data/lib/badline/storage/crt_file.rb +7 -1
  131. data/lib/badline/storage/d64_image.rb +7 -0
  132. data/lib/badline/storage/d80_image.rb +17 -1
  133. data/lib/badline/storage/d81_image.rb +14 -0
  134. data/lib/badline/storage/disk_image/bam.rb +18 -3
  135. data/lib/badline/storage/disk_image/formatting.rb +69 -0
  136. data/lib/badline/storage/disk_image/writing.rb +35 -0
  137. data/lib/badline/storage/disk_image.rb +2 -0
  138. data/lib/badline/storage.rb +1 -0
  139. data/lib/badline/support/cbm2_checkpoint.rb +48 -0
  140. data/lib/badline/support/p500_checkpoint.rb +35 -0
  141. data/lib/badline/version.rb +1 -1
  142. data/lib/badline.rb +1 -0
  143. metadata +72 -2
@@ -0,0 +1,233 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "badline/chips/tpi/keyed_state"
4
+
5
+ module Badline
6
+ module Chips
7
+ # The MOS 6525 TPI (tri-port interface): three 8-bit ports, A, B and C,
8
+ # each with a direction register, a 1 making the pin an output. Its
9
+ # three address lines repeat the eight registers every 8 bytes:
10
+ #
11
+ # 0 - PRA
12
+ # 1 - PRB
13
+ # 2 - PRC, or in mode 1 the interrupt latches (bits 0-4) and the levels
14
+ # of IRQ, CA and CB (bits 5-7)
15
+ # 3 - DDRA
16
+ # 4 - DDRB
17
+ # 5 - DDRC, which in mode 1 is the interrupt mask, its bits 0-4 enabling
18
+ # I0-I4 with a 1, and reads back all eight bits written
19
+ # 6 - CR: bit 0 MC, the mode, bit 1 IP, the priority, bits 2-3 IE3 and
20
+ # IE4, the active edges of I3 and I4 (1 rising), bits 4-5 CA's mode
21
+ # and bits 6-7 CB's
22
+ # 7 - AIR, the active interrupt register, in mode 1
23
+ #
24
+ # A port reads its pins: an output's level, and what the peripheral
25
+ # lets through of an input, which floats high (read_a, read_b and
26
+ # read_c pull lines low, as a VIA's peripheral does).
27
+ #
28
+ # In mode 1 PC0-PC4 are the interrupt inputs I0-I4 (#interrupt_line).
29
+ # A falling edge on I0-I2, or the edge IE3 and IE4 pick on I3 and I4,
30
+ # sets the input's latch whether or not its mask bit is set. A write of
31
+ # 0 to a latch's bit of PRC clears it, and a 1 does nothing. PC5 is
32
+ # the IRQ output, open drain, low (#irq?) while the AIR holds a bit;
33
+ # PC6 is CA and PC7 CB (#ca?, #cb?).
34
+ #
35
+ # Without priority (IP 0), the AIR holds every unmasked latch that is
36
+ # set, and a read of it clears them and it. With priority (IP 1) the
37
+ # inputs rank I4 > I3 > I2 > I1 > I0, and the AIR holds the one highest
38
+ # unmasked latch that outranks the interrupt in service, the top of a
39
+ # five-level stack. A read of the AIR pushes it on the stack, clears it
40
+ # and its latch, and lets IRQ go; a write pops the stack, letting lower
41
+ # interrupts through again.
42
+ #
43
+ # CA: 00 set high by I3's active edge and low by a read of PRA, 01
44
+ # pulsed low by a read of PRA, 10 low, 11 high. CB: 00 set low by a
45
+ # write of PRB and high by I4's active edge, 01 pulsed low by a write of
46
+ # PRB, 10 low, 11 high. A pulse is shorter than a cycle, so CA and CB
47
+ # read high in mode 01.
48
+ #
49
+ # The rules are the MPS 6525 datasheet's (Commodore Semiconductor
50
+ # Group), pages 2-81 to 2-83.
51
+ class TPI
52
+ STACK_DEPTH = 5
53
+
54
+ attr_reader :peripheral, :latches, :control, :stack
55
+
56
+ def initialize(peripheral: nil)
57
+ @peripheral = peripheral
58
+ @lines = 0x1f
59
+ @stack = []
60
+ reset!
61
+ end
62
+
63
+ # The RES pin clears every register: mode 0, and every pin an input.
64
+ def reset!
65
+ @pra = @prb = @prc = 0
66
+ @ddra = @ddrb = @ddrc = 0
67
+ @control = 0
68
+ @latches = 0
69
+ @air = 0
70
+ @stack.clear
71
+ @ca = @cb = false
72
+ end
73
+
74
+ # The interrupt mask in mode 1: DDRC's bits 0-4.
75
+ def mask = @ddrc & 0x1f
76
+
77
+ # Whether IRQ is pulled low.
78
+ def irq? = @air != 0
79
+
80
+ # The levels of CA and CB in mode 1.
81
+ def ca? = @ca
82
+
83
+ def cb? = @cb
84
+
85
+ # The port lines as the chip drives them, inputs floating high.
86
+ def port_a_output = driven(@pra, @ddra)
87
+
88
+ def port_b_output = driven(@prb, @ddrb)
89
+
90
+ def port_c_output = mode1? ? pins_c_mode1 : driven(@prc, @ddrc)
91
+
92
+ # Something outside drives interrupt input +index+, I0 to I4, +high+
93
+ # or low. Its active edge sets its latch in mode 1.
94
+ def interrupt_line(index, high)
95
+ bit = 1 << index
96
+ was_high = @lines.anybits?(bit)
97
+ return if was_high == high
98
+
99
+ @lines = high ? @lines | bit : @lines & ~bit
100
+ return unless mode1? && high == rising_active?(index)
101
+
102
+ @latches |= bit
103
+ @ca = true if index == 3 && ca_mode.zero?
104
+ @cb = true if index == 4 && cb_mode.zero?
105
+ update_air
106
+ end
107
+
108
+ def peek(addr)
109
+ case addr & 0x07
110
+ when 0 then read_pra
111
+ when 1 then pins(@prb, @ddrb, @peripheral ? @peripheral.read_b(port_b_output) : 0xff)
112
+ when 2 then mode1? ? pins_c_mode1 : read_pins_c
113
+ when 3 then @ddra
114
+ when 4 then @ddrb
115
+ when 5 then @ddrc
116
+ when 6 then @control
117
+ else read_air
118
+ end
119
+ end
120
+
121
+ def poke(addr, value)
122
+ case addr & 0x07
123
+ when 0 then @pra = value
124
+ when 1 then write_prb(value)
125
+ when 2 then write_prc(value)
126
+ when 3 then @ddra = value
127
+ when 4 then @ddrb = value
128
+ when 5 then write_ddrc(value)
129
+ when 6 then write_control(value)
130
+ else pop_air
131
+ end
132
+ end
133
+
134
+ private
135
+
136
+ def mode1? = @control.anybits?(0x01)
137
+
138
+ def priority? = @control.anybits?(0x02)
139
+
140
+ def ca_mode = (@control >> 4) & 0x03
141
+
142
+ def cb_mode = (@control >> 6) & 0x03
143
+
144
+ # I3 and I4 latch on the edge IE3 and IE4 pick, the others falling.
145
+ def rising_active?(index)
146
+ case index
147
+ when 3 then @control.anybits?(0x04)
148
+ when 4 then @control.anybits?(0x08)
149
+ else false
150
+ end
151
+ end
152
+
153
+ def driven(register, direction) = (register & direction) | (~direction & 0xff)
154
+
155
+ def pins(register, direction, inputs) = (register & direction) | (inputs & ~direction & 0xff)
156
+
157
+ def read_pra
158
+ @ca = false if mode1? && ca_mode.zero?
159
+ pins(@pra, @ddra, @peripheral ? @peripheral.read_a(port_a_output) : 0xff)
160
+ end
161
+
162
+ def read_pins_c
163
+ pins(@prc, @ddrc, @peripheral ? @peripheral.read_c(port_c_output) : 0xff)
164
+ end
165
+
166
+ # The latches, IRQ's level, CA's and CB's.
167
+ def pins_c_mode1
168
+ @latches | (irq? ? 0 : 0x20) | (@ca ? 0x40 : 0) | (@cb ? 0x80 : 0)
169
+ end
170
+
171
+ def write_prb(value)
172
+ @prb = value
173
+ @cb = false if mode1? && cb_mode.zero?
174
+ end
175
+
176
+ def write_prc(value)
177
+ return @prc = value unless mode1?
178
+
179
+ @latches &= value | 0xe0
180
+ update_air
181
+ end
182
+
183
+ def write_ddrc(value)
184
+ @ddrc = value
185
+ update_air
186
+ end
187
+
188
+ def write_control(value)
189
+ @control = value
190
+ @ca = ca_mode.anybits?(0x01) unless ca_mode.zero?
191
+ @cb = cb_mode.anybits?(0x01) unless cb_mode.zero?
192
+ update_air
193
+ end
194
+
195
+ def read_air
196
+ value = @air
197
+ return value unless mode1?
198
+
199
+ @latches &= ~value
200
+ @air = 0
201
+ if priority?
202
+ @stack.shift if @stack.length == STACK_DEPTH
203
+ @stack << value
204
+ end
205
+ update_air
206
+ value
207
+ end
208
+
209
+ def pop_air
210
+ return unless mode1?
211
+
212
+ @stack.pop
213
+ update_air
214
+ end
215
+
216
+ # The AIR's bits from the latches, the mask and, with priority, the
217
+ # interrupt in service.
218
+ def update_air
219
+ pending = mode1? ? @latches & mask : 0
220
+ return @air = pending unless priority?
221
+
222
+ highest = highest_bit(pending)
223
+ @air = highest > (@stack.last || 0) ? highest : 0
224
+ end
225
+
226
+ def highest_bit(bits)
227
+ bit = 0x10
228
+ bit >>= 1 while bit.positive? && bits.nobits?(bit)
229
+ bit
230
+ end
231
+ end
232
+ end
233
+ end
@@ -10,6 +10,13 @@ module Badline
10
10
  # The machine's bus pushes in what the VIC sees through #connect,
11
11
  # #map_character_rom and #map whenever it changes. Until then the bank
12
12
  # has a CIA 2, RAM, colour RAM and character ROM of its own.
13
+ #
14
+ # The P500 wires the VIC another way (#map_video_bus): its phi1
15
+ # reads go to the character ROM while VICDOTSEL is high, and its phi2
16
+ # reads to the 1K static video matrix while STATVID is high, VA12
17
+ # high picking the matrix and VA12 low reading 0. Otherwise both go to
18
+ # the DRAM's 16K bank that VA14 and VA15 pick (#video_bus_lines). The
19
+ # colour nibbles come from the colour RAM in every phi2 read.
13
20
  class Bank
14
21
  include Addressable
15
22
 
@@ -30,6 +37,11 @@ module Badline
30
37
  map(Memory.new([], length: 2**16, start: 0))
31
38
  @lines = 0b11
32
39
  @swapped = false
40
+ @video_bus = false
41
+ @matrix = @ram
42
+ @statvid = false
43
+ @dotsel = false
44
+ @video_base = 0
33
45
  end
34
46
 
35
47
  # CIA 2, whose port A picks the bank, and the colour RAM, read from
@@ -57,9 +69,31 @@ module Badline
57
69
  @base = base
58
70
  @starts = BANK_STARTS.map { |start| base + start }.freeze
59
71
  end
60
- @phi1_ultimax = phi1_ultimax
61
- @ultimax = ultimax
62
72
  @romh = romh
73
+ @phi1_other = phi1_ultimax
74
+ @phi2_other = ultimax
75
+ end
76
+
77
+ # The P500's video bus: +dram+ is segment 0's 64K, +matrix+ the
78
+ # static video matrix, read from its start address on, and
79
+ # +color_ram+ the colour RAM, read from +color_base+ on. The
80
+ # character ROM is the one #map_character_rom gives, read from
81
+ # +base+ on.
82
+ def map_video_bus(dram, matrix, color_ram, color_base)
83
+ @ram = dram
84
+ @matrix = matrix
85
+ @color_ram = color_ram
86
+ @color_base = color_base
87
+ @video_bus = true
88
+ @phi1_other = true
89
+ @phi2_other = true
90
+ end
91
+
92
+ # STATVID, VICDOTSEL and VA14-VA15, as the P500's TPIs drive them.
93
+ def video_bus_lines(statvid, dotsel, bank)
94
+ @statvid = statvid
95
+ @dotsel = dotsel
96
+ @video_base = bank << 14
63
97
  end
64
98
 
65
99
  # On the 8565, a port A write that swaps the two bank lines, driving
@@ -74,7 +108,7 @@ module Badline
74
108
  end
75
109
 
76
110
  def peek(offset)
77
- return ultimax_peek(offset) if @phi1_ultimax
111
+ return phi1_other(offset) if @phi1_other
78
112
 
79
113
  bits = bank_switch_register
80
114
  if (bits & @shadow_mask) == 0b01 && (offset & 0xf000) == 0x1000
@@ -88,7 +122,13 @@ module Badline
88
122
  # memory configuration the CPU does in that half. No cartridge drives
89
123
  # Ultimax mode in the first half alone, so otherwise it reads as #peek.
90
124
  def peek_phi2(offset)
91
- @ultimax ? ultimax_peek(offset) : peek(offset)
125
+ @phi2_other ? phi2_other(offset) : peek(offset)
126
+ end
127
+
128
+ # A sprite's s-access in phi2: on the P500's video bus as the other
129
+ # phi2 reads, and otherwise as #peek.
130
+ def peek_sprite(offset)
131
+ @video_bus ? phi2_other(offset) : peek(offset)
92
132
  end
93
133
 
94
134
  def peek_color(offset)
@@ -101,7 +141,7 @@ module Badline
101
141
 
102
142
  # True if the VIC reads the character ROM at this offset.
103
143
  def character_rom?(offset)
104
- !@phi1_ultimax && (bank_switch_register & @shadow_mask) == 0b01 && (offset & 0xf000) == 0x1000
144
+ !@phi1_other && (bank_switch_register & @shadow_mask) == 0b01 && (offset & 0xf000) == 0x1000
105
145
  end
106
146
 
107
147
  def start
@@ -110,6 +150,21 @@ module Badline
110
150
 
111
151
  private
112
152
 
153
+ def phi1_other(offset)
154
+ return ultimax_peek(offset) unless @video_bus
155
+ return @character_rom.peek(@character_base + (offset & 0x0fff)) if @dotsel
156
+
157
+ @ram.peek(@video_base + offset)
158
+ end
159
+
160
+ def phi2_other(offset)
161
+ return ultimax_peek(offset) unless @video_bus
162
+ return @ram.peek(@video_base + offset) unless @statvid
163
+ return 0 if offset.nobits?(0x1000)
164
+
165
+ @matrix.peek(@matrix.start + (offset & 0x03ff))
166
+ end
167
+
113
168
  # In Ultimax mode the cartridge ROMH replaces the character ROM
114
169
  # shadow, visible at $3000-$3FFF of the window. The MAX is in
115
170
  # Ultimax mode with no cartridge too.
@@ -322,12 +322,13 @@ module Badline
322
322
  end
323
323
 
324
324
  # The three s-accesses step MC through the sprite's block, wrapping
325
- # within it.
325
+ # within it. The first and third fall in phi2 and the second in
326
+ # phi1 (Bank#peek_sprite).
326
327
  def row_bits(counter)
327
328
  base = pointer * 64
328
- (@bank.peek(base + counter) << 16) |
329
+ (@bank.peek_sprite(base + counter) << 16) |
329
330
  (@bank.peek(base + ((counter + 1) & MC_MASK)) << 8) |
330
- @bank.peek(base + ((counter + 2) & MC_MASK))
331
+ @bank.peek_sprite(base + ((counter + 2) & MC_MASK))
331
332
  end
332
333
 
333
334
  def pointer = @bank.peek(@registers.screen_base + 0x3f8 + index)
@@ -10,7 +10,8 @@ module Badline
10
10
  # bytes and how far it has played. A tape that isn't in leaves tape.* out. A datasette that
11
11
  # times the read line's level adds when in the countdown the line
12
12
  # rises, and whether a recording's low half wave is on the tape. The
13
- # sense and flag handlers don't fire on load.
13
+ # sense and flag handlers don't fire on load, and the running handler
14
+ # does when the load starts or stops the tape.
14
15
  module KeyedState
15
16
  def save_fields(out)
16
17
  out.bool("playing", @playing).bool("motor", @motor).int("countdown", @countdown)
@@ -33,6 +34,13 @@ module Badline
33
34
  @write_high = input.bool("write_high", @write_high)
34
35
  @rise_at = input.int("rise_at", @rise_at)
35
36
  @low_recorded = input.bool("low_recorded", @low_recorded)
37
+ load_tape(input)
38
+ update_running
39
+ end
40
+
41
+ private
42
+
43
+ def load_tape(input)
36
44
  bytes = input.bytes("tape.bytes", "".b).bytes
37
45
  return @tape = nil if bytes.empty?
38
46
 
@@ -14,7 +14,6 @@ module Badline
14
14
  # What inserting a tape raises on a machine without a cassette port.
15
15
  class Missing < ArgumentError; end
16
16
  attr_reader :tape
17
- attr_writer :motor
18
17
 
19
18
  def initialize(tape = nil, level: false)
20
19
  @tape = tape
@@ -30,6 +29,8 @@ module Badline
30
29
  @low_recorded = false
31
30
  @flag_handler = nil
32
31
  @sense_handler = nil
32
+ @running_handler = nil
33
+ @running = false
33
34
  end
34
35
 
35
36
  # Falling edges on the tape read line.
@@ -42,6 +43,17 @@ module Badline
42
43
  @sense_handler = handler
43
44
  end
44
45
 
46
+ # Whether the tape runs (#running?), each time that changes, a
47
+ # snapshot's load included.
48
+ def on_running_change(&handler)
49
+ @running_handler = handler
50
+ end
51
+
52
+ def motor=(on)
53
+ @motor = on
54
+ update_running
55
+ end
56
+
45
57
  # Whether the machine has a datasette at its cassette port. The SX-64
46
58
  # has none: its keys stay up and it takes no tape.
47
59
  def connected? = @connected
@@ -56,11 +68,13 @@ module Badline
56
68
 
57
69
  @tape = tape
58
70
  rewind
71
+ update_running
59
72
  end
60
73
 
61
74
  def eject
62
75
  stop!
63
76
  @tape = nil
77
+ update_running
64
78
  end
65
79
 
66
80
  def rewind
@@ -117,7 +131,8 @@ module Badline
117
131
 
118
132
  def motor? = @motor
119
133
 
120
- def running? = @playing && @motor && !@tape.nil?
134
+ # Whether the tape moves: a key down, the motor on and a tape in.
135
+ def running? = @running
121
136
 
122
137
  # The tape read line's level, on a datasette built with +level: true+.
123
138
  # Each pulse holds it low for its first half and high for its second,
@@ -131,7 +146,7 @@ module Badline
131
146
  end
132
147
 
133
148
  def cycle!
134
- return unless running?
149
+ return unless @running
135
150
  return @since += 1 if @recording
136
151
 
137
152
  next_pulse if @countdown.zero?
@@ -175,9 +190,18 @@ module Badline
175
190
 
176
191
  finish_recording unless down
177
192
  @playing = down
193
+ update_running
178
194
  @sense_handler&.call
179
195
  end
180
196
 
197
+ def update_running
198
+ running = @playing && @motor && !@tape.nil?
199
+ return if running == @running
200
+
201
+ @running = running
202
+ @running_handler&.call
203
+ end
204
+
181
205
  # Releasing the keys ends a recording, and writes the tape out.
182
206
  def finish_recording
183
207
  return unless @recording
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ module Drives
5
+ class Model8050
6
+ # A .d80 or .d82 image as the floppy controller finds it on the disk:
7
+ # each track a ring of byte cells, each cell ten bit cells the GCR ROM
8
+ # turns into a data byte (Track::SYNC for a run of ones), so the
9
+ # mechanism needs no GCR. A track is laid out as the DOS's format code
10
+ # writes it ($C000-$C363 of DOS 2.7, run on the controller), sector
11
+ # after sector:
12
+ #
13
+ # 4 sync, $08, checksum, sector, track, ID 2, ID 1, $0F,
14
+ # 19 x $0F, 5 sync, $07, 256 data bytes, checksum, then $00
15
+ #
16
+ # the header checksum the EOR of the track, sector and IDs, and the
17
+ # data block's that of its bytes. The $00 gaps share out what the
18
+ # track has left over.
19
+ #
20
+ # Tracks come off the image as the head first reaches them. What the
21
+ # head writes goes back to the image when the motor stops (flush):
22
+ # every data block found on a written track whose header and checksum
23
+ # read cleanly.
24
+ class Disk
25
+ SYNC = 0x100
26
+
27
+ attr_reader :image
28
+
29
+ def initialize(image)
30
+ @image = image
31
+ @tracks = {}
32
+ @written = {}
33
+ end
34
+
35
+ def write_protected? = @image.read_only?
36
+
37
+ # The image's host file.
38
+ def path = @image.path
39
+
40
+ # How many tracks a side holds: 77.
41
+ def side_tracks = Storage::D80Image::SIDE_TRACKS
42
+
43
+ def sides = @image.is_a?(Storage::D82Image) ? 2 : 1
44
+
45
+ # The cells of +track+, 1 to 77 on the first side and 78 to 154 on
46
+ # the second, or nil past the disk's last.
47
+ def track(number)
48
+ return nil unless number.between?(1, side_tracks * sides)
49
+
50
+ @tracks[number] ||= build(number)
51
+ end
52
+
53
+ # The cells a track of the zone holds in one turn at 300 rpm: the
54
+ # 200,000 cycles of a turn over the zone's ten bit cells a byte at
55
+ # 6 MHz / (16 - zone).
56
+ def self.cells_in(zone) = 120_000 / (16 - zone)
57
+
58
+ # The zone a track is recorded in, 3 outermost to 0 innermost.
59
+ def self.zone_of(track)
60
+ cylinder = ((track - 1) % Storage::D80Image::SIDE_TRACKS) + 1
61
+ 3 - Storage::D80Image::ZONE_LAST_TRACKS.count { |last| cylinder > last }
62
+ end
63
+
64
+ # The head wrote to +track+.
65
+ def written(number)
66
+ @written[number] = true
67
+ end
68
+
69
+ # Stores the blocks the head wrote in the image.
70
+ def flush
71
+ return if @written.empty?
72
+
73
+ blocks = []
74
+ @written.each_key { |number| blocks.concat(read_blocks(@tracks[number])) }
75
+ @written.clear
76
+ @image.store_blocks(blocks) unless blocks.empty?
77
+ end
78
+
79
+ private
80
+
81
+ def build(number)
82
+ id1, id2 = disk_id
83
+ cells = []
84
+ sectors = @image.sectors_in(number)
85
+ sectors.times do |sector|
86
+ data = @image.read_block(number, sector)
87
+ cells.push(SYNC, SYNC, SYNC, SYNC, 0x08, number ^ sector ^ id1 ^ id2, sector, number, id2, id1)
88
+ cells.concat(Array.new(20, 0x0f))
89
+ cells.push(SYNC, SYNC, SYNC, SYNC, SYNC, 0x07)
90
+ cells.concat(data)
91
+ cells << checksum(data)
92
+ end
93
+ pad(cells, sectors, self.class.cells_in(self.class.zone_of(number)))
94
+ end
95
+
96
+ # Shares the $00 gap out after each sector.
97
+ def pad(cells, sectors, length)
98
+ sector_length = cells.length / sectors
99
+ gap = (length - cells.length) / sectors
100
+ padded = []
101
+ sectors.times do |sector|
102
+ padded.concat(cells[sector * sector_length, sector_length])
103
+ padded.concat(Array.new(sector == sectors - 1 ? length - padded.length : gap, 0x00))
104
+ end
105
+ padded
106
+ end
107
+
108
+ def checksum(bytes)
109
+ sum = 0
110
+ bytes.each { |byte| sum ^= byte }
111
+ sum
112
+ end
113
+
114
+ # The ID the header block holds at $18.
115
+ def disk_id
116
+ header = @image.read_block(*@image.header_block)
117
+ [header[0x18], header[0x19]]
118
+ end
119
+
120
+ # Every block whose header and data read cleanly off +cells+, as
121
+ # [track, sector, data, nil].
122
+ def read_blocks(cells)
123
+ blocks = []
124
+ index = 0
125
+ length = cells.length
126
+ while (index = next_block(cells, index, 0x08))
127
+ header = Array.new(5) { |offset| cells[(index + offset) % length] }
128
+ index = next_block(cells, index, 0x07)
129
+ break unless index
130
+
131
+ block = data_block(cells, index, header)
132
+ blocks << block if block
133
+ break if index + 260 >= length
134
+ end
135
+ blocks
136
+ end
137
+
138
+ # The cell after the next sync run followed by +mark+, from
139
+ # +index+, or nil.
140
+ def next_block(cells, index, mark)
141
+ length = cells.length
142
+ while index < length
143
+ return index + 2 if cells[index] == SYNC && cells[(index + 1) % length] == mark
144
+
145
+ index += 1
146
+ end
147
+ nil
148
+ end
149
+
150
+ def data_block(cells, index, header)
151
+ checksum, sector, track, id2, id1 = header
152
+ return nil unless checksum == (track ^ sector ^ id1 ^ id2)
153
+
154
+ length = cells.length
155
+ data = Array.new(256) { |offset| cells[(index + offset) % length] }
156
+ return nil if data.include?(SYNC) || checksum(data) != cells[(index + 256) % length]
157
+
158
+ [track, sector, data, nil]
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end