badline 0.4.2 → 0.5.1

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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/CLAUDE.md +32 -27
  4. data/CONTRIBUTING.md +5 -0
  5. data/README.md +55 -24
  6. data/doc/pinned-behaviour.md +223 -0
  7. data/exe/badline-ruby +7 -4
  8. data/lib/badline/address_bus/ultimax_pages.rb +38 -0
  9. data/lib/badline/address_bus.rb +19 -29
  10. data/lib/badline/audio/jukebox.rb +6 -4
  11. data/lib/badline/audio/machine_player.rb +10 -6
  12. data/lib/badline/cartridge/kcs_power.rb +3 -3
  13. data/lib/badline/cia.rb +21 -3
  14. data/lib/badline/computer.rb +86 -7
  15. data/lib/badline/cpu.rb +39 -0
  16. data/lib/badline/drive1541/bus.rb +197 -0
  17. data/lib/badline/drive1541/disk.rb +256 -0
  18. data/lib/badline/drive1541/disk_via.rb +33 -0
  19. data/lib/badline/drive1541/gcr.rb +57 -0
  20. data/lib/badline/drive1541/idle.rb +136 -0
  21. data/lib/badline/drive1541/mechanism.rb +277 -0
  22. data/lib/badline/drive1541/orbit.rb +81 -0
  23. data/lib/badline/drive1541/rotation.rb +111 -0
  24. data/lib/badline/drive1541/sector_reader.rb +76 -0
  25. data/lib/badline/drive1541/serial_port.rb +67 -0
  26. data/lib/badline/drive1541/sleep.rb +114 -0
  27. data/lib/badline/drive1541/track.rb +98 -0
  28. data/lib/badline/drive1541.rb +234 -0
  29. data/lib/badline/gui/application.rb +15 -3
  30. data/lib/badline/gui/drive_led_pane.rb +29 -0
  31. data/lib/badline/gui.rb +1 -0
  32. data/lib/badline/iec_bus.rb +109 -0
  33. data/lib/badline/kernal_trap/channel.rb +9 -1
  34. data/lib/badline/kernal_trap/serial.rb +9 -3
  35. data/lib/badline/media/true_drive.rb +54 -0
  36. data/lib/badline/media.rb +22 -1
  37. data/lib/badline/memory.rb +3 -0
  38. data/lib/badline/options.rb +24 -4
  39. data/lib/badline/region.rb +35 -10
  40. data/lib/badline/reu/dma.rb +228 -0
  41. data/lib/badline/reu/ram.rb +65 -0
  42. data/lib/badline/reu/trigger.rb +32 -0
  43. data/lib/badline/reu.rb +276 -0
  44. data/lib/badline/roms/README +10 -0
  45. data/lib/badline/roms/dos1541.rom +0 -0
  46. data/lib/badline/sdl/functions.rb +1 -0
  47. data/lib/badline/storage/d64_image.rb +18 -9
  48. data/lib/badline/storage/d81_image.rb +1 -0
  49. data/lib/badline/storage/disk_image/catalog.rb +52 -0
  50. data/lib/badline/storage/disk_image/writing.rb +16 -0
  51. data/lib/badline/storage/disk_image.rb +2 -0
  52. data/lib/badline/storage/g64_image.rb +222 -0
  53. data/lib/badline/storage/host_directory.rb +21 -1
  54. data/lib/badline/storage/listing.rb +118 -0
  55. data/lib/badline/storage/t64.rb +14 -0
  56. data/lib/badline/storage.rb +2 -0
  57. data/lib/badline/traps.rb +4 -0
  58. data/lib/badline/version.rb +1 -1
  59. data/lib/badline/via/control_lines.rb +3 -0
  60. data/lib/badline/via/fast_forward.rb +53 -0
  61. data/lib/badline/via/shift_register.rb +8 -3
  62. data/lib/badline/via/timer1.rb +36 -0
  63. data/lib/badline/via/timer2.rb +31 -0
  64. data/lib/badline/via.rb +2 -0
  65. data/lib/badline/vic/display_state.rb +7 -7
  66. data/lib/badline/vic/sprite/internal_bus.rb +11 -7
  67. data/lib/badline/vic/sprite/shifter.rb +10 -15
  68. data/lib/badline/vic/sprite/timing.rb +60 -0
  69. data/lib/badline/vic/sprite.rb +14 -12
  70. data/lib/badline/vic/sprites.rb +2 -2
  71. data/lib/badline/vic.rb +86 -39
  72. data/lib/badline.rb +3 -0
  73. metadata +28 -1
@@ -6,7 +6,12 @@ module Badline
6
6
  # device 8 go to a virtual drive instead of out over the IEC lines, so
7
7
  # OPEN/CHKIN/CHRIN reach a CBM DOS, and so do the block reads that
8
8
  # loaders make through them. Other devices fall through to the ROM.
9
+ #
10
+ # With +device+ nil the traps answer no device, and every frame goes
11
+ # out over the bus, to a true drive if one is plugged in.
9
12
  class Serial < Routine
13
+ attr_accessor :device
14
+
10
15
  ROUTINES = {
11
16
  0xed09 => :talk,
12
17
  0xed0c => :listen,
@@ -29,9 +34,10 @@ module Badline
29
34
 
30
35
  NO_DATA = [0x0d, EOI | READ_TIMEOUT].freeze
31
36
 
32
- def initialize(cpu:, bus:, drive:)
37
+ def initialize(cpu:, bus:, drive:, device: DEVICE)
33
38
  super(cpu:, bus:)
34
39
  @drive = drive
40
+ @device = device
35
41
  @listening = false
36
42
  @talking = false
37
43
  @listen_channel = nil
@@ -66,13 +72,13 @@ module Badline
66
72
  # in the secondary address that follows.
67
73
  def talk
68
74
  @talk_channel = nil
69
- @talking = @cpu.a == DEVICE
75
+ @talking = @cpu.a == @device
70
76
  return_to_caller if @talking
71
77
  end
72
78
 
73
79
  def listen
74
80
  @listen_channel = nil
75
- @listening = @cpu.a == DEVICE
81
+ @listening = @cpu.a == @device
76
82
  return_to_caller if @listening
77
83
  end
78
84
 
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ module Media
5
+ # Device 8 as a true 1541 instead of the KERNAL traps. Once a Drive1541
6
+ # is plugged in as device 8, disk media go into it rather than into the
7
+ # traps, so a LOAD runs the DOS and every byte crosses the serial bus.
8
+ # A .g64 holds the disk's raw GCR, which only a true drive reads, so it
9
+ # plugs one in when there's none yet. The 1541 reads only .d64 and .g64
10
+ # images, so the traps' other disk media, a .d71, .d81 or .t64 or a host
11
+ # directory, raise Error.
12
+ module TrueDrive
13
+ class Error < ArgumentError; end
14
+
15
+ class << self
16
+ # Plugs a Drive1541 in as device 8, unless one is there already, and
17
+ # returns it.
18
+ def plug(computer)
19
+ drive(computer) || Drive1541.new.tap { |drive| computer.attach_drive1541(drive) }
20
+ end
21
+
22
+ # The Drive1541 on device 8, or nil.
23
+ def drive(computer)
24
+ drive = computer.drive1541
25
+ drive if drive && drive.device == KernalTrap::Routine::DEVICE
26
+ end
27
+
28
+ # Whether the path goes in a true drive: a .g64 always, and other
29
+ # disk media, which the traps would mount, when a true drive is
30
+ # device 8.
31
+ def takes?(computer, path)
32
+ return true if File.extname(path).casecmp?(".g64")
33
+
34
+ !drive(computer).nil? && (File.directory?(path) || MOUNT_TYPES.key?(File.extname(path).downcase))
35
+ end
36
+
37
+ # Puts the .d64 or .g64 in the drive on device 8, plugging one in
38
+ # when there's none, and takes out what was mounted through the
39
+ # traps, so LOAD and SAVE reach the drive too. `read_only` puts it
40
+ # in write-protected.
41
+ def insert(computer, path, read_only: false)
42
+ unless %w[.d64 .g64].include?(File.extname(path).downcase)
43
+ raise Error, "#{path} is not a .d64 or .g64 image, which the 1541 reads"
44
+ end
45
+
46
+ disk = Drive1541::Disk.open(path, read_only:)
47
+ computer.unmount
48
+ plug(computer).insert(disk)
49
+ "Inserted #{path} in the 1541 as device 8"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
data/lib/badline/media.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "badline/media/true_drive"
4
+
3
5
  module Badline
4
6
  module Media
5
7
  AUTOSTART = %(lO"*",8,1\rrun\r)
@@ -19,8 +21,14 @@ module Badline
19
21
  # the Retro Replay's flash mode, and `disk: { read_only: true }` mounts
20
22
  # a disk image write-protected, so nothing the program does writes to
21
23
  # its file. Media they don't apply to ignore them.
24
+ #
25
+ # A .g64 plugs in a true drive as device 8 (TrueDrive), and with one
26
+ # there, a .d64 goes into it instead of the KERNAL traps too. The
27
+ # autostart then loads through it.
22
28
  def attach(computer, path, autostart: true, song: nil, **options)
23
- if File.directory?(path)
29
+ if TrueDrive.takes?(computer, path)
30
+ attach_true_drive(computer, path, options.fetch(:disk, {}), autostart:)
31
+ elsif File.directory?(path)
24
32
  computer.mount(Storage::HostDirectory.new(path))
25
33
  "Mounted #{path} as device 8"
26
34
  elsif File.extname(path).downcase == ".crt"
@@ -40,7 +48,14 @@ module Badline
40
48
  # while the machine runs, without loading anything. The drive keeps
41
49
  # its RAM and its status. `read_only` inserts a disk image
42
50
  # write-protected.
51
+ #
52
+ # A .g64 goes in a true 1541, which is plugged in as device 8 when
53
+ # there's none yet (TrueDrive). Once a true 1541 is device 8, a .d64
54
+ # goes in its drive as well, and other disks, which a 1541 can't
55
+ # read, raise TrueDrive::Error, an ArgumentError.
43
56
  def insert_disk(computer, path, read_only: false)
57
+ return TrueDrive.insert(computer, path, read_only:) if TrueDrive.takes?(computer, path)
58
+
44
59
  raise ArgumentError, "#{path} is not a disk image or a directory" unless disk?(path)
45
60
 
46
61
  computer.mount(open_storage(path, { read_only: }))
@@ -98,6 +113,12 @@ module Badline
98
113
  "Inserted #{path} in the datasette"
99
114
  end
100
115
 
116
+ def attach_true_drive(computer, path, disk, autostart:)
117
+ message = TrueDrive.insert(computer, path, read_only: disk.fetch(:read_only, false))
118
+ computer.type_text(AUTOSTART) if autostart
119
+ message
120
+ end
121
+
101
122
  def attach_storage(computer, path, disk, autostart:)
102
123
  computer.mount(open_storage(path, disk))
103
124
  computer.type_text(AUTOSTART) if autostart
@@ -22,6 +22,9 @@ module Badline
22
22
  (addr...(addr + length)).to_a.map { |a| peek(a) }
23
23
  end
24
24
 
25
+ # Every byte, as a copy.
26
+ def snapshot = @storage.dup
27
+
25
28
  def write(addr, bytes)
26
29
  Array(bytes).each_with_index { |b, i| poke(addr + i, b) }
27
30
  end
@@ -21,9 +21,13 @@ module Badline
21
21
  badline-ruby --headless [options] tune.sid
22
22
  badline-ruby [options] tune.sid --audio-out FILE
23
23
 
24
- Media can be a .prg/.p00 program, a .d64/.d71/.d81 disk image,
25
- a .t64 tape archive, a .tap tape, a .crt cartridge, a .sid tune, or
26
- a directory to mount as device 8. It opens in the emulator window.
24
+ Media can be a .prg/.p00 program, a .d64/.d71/.d81 disk image, a
25
+ .g64 disk image for the true 1541, a .t64 tape archive, a .tap
26
+ tape, a .crt cartridge, a .sid tune, or a directory to mount as
27
+ device 8. It opens in the emulator window. Device 8 answers through
28
+ traps on the KERNAL's disk routines, unless --true-drive puts an
29
+ emulated 1541 there, which runs its own DOS and reads .d64 and .g64
30
+ images only.
27
31
 
28
32
  --headless plays a .sid tune on the host's audio device without the
29
33
  window, and --audio-out renders it to 16-bit PCM instead. The
@@ -43,7 +47,7 @@ module Badline
43
47
 
44
48
  BANNER
45
49
 
46
- attr_reader :media_path, :audio_out, :song, :seconds, :songlengths, :sid_model, :filter_chunk
50
+ attr_reader :media_path, :audio_out, :song, :seconds, :songlengths, :sid_model, :filter_chunk, :reu
47
51
 
48
52
  alias tune_path media_path
49
53
 
@@ -54,6 +58,8 @@ module Badline
54
58
  @read_only = false
55
59
  @sound = false
56
60
  @verbose = false
61
+ @true_drive = false
62
+ @ntsc = false
57
63
  @headless = false
58
64
  @jit = true
59
65
  @quiet = false
@@ -99,6 +105,10 @@ module Badline
99
105
 
100
106
  def verbose? = @verbose
101
107
 
108
+ def true_drive? = @true_drive
109
+
110
+ def ntsc? = @ntsc
111
+
102
112
  def quiet? = @quiet
103
113
 
104
114
  def tui? = @tui
@@ -168,6 +178,16 @@ module Badline
168
178
  opts.on("--sound", "Play the SID through the host's audio device (F10 mutes)") do
169
179
  window_only("--sound") { @sound = true }
170
180
  end
181
+ opts.on("--true-drive", "Put a true 1541 on device 8 instead of the KERNAL traps") do
182
+ window_only("--true-drive") { @true_drive = true }
183
+ end
184
+ opts.on("--reu SIZE", %w[128 256 512 1024 2048 4096 8192 16384],
185
+ "Plug in an REU of SIZE K: 128, 256, 512 (a 1750) or up to 16384") do |size|
186
+ window_only("--reu") { @reu = size.to_i }
187
+ end
188
+ opts.on("--ntsc", "Run an NTSC C64, with the 6567R8 VIC-II, instead of a PAL one") do
189
+ window_only("--ntsc") { @ntsc = true }
190
+ end
171
191
  opts.on("--verbose", "Print the display, sound and gamepad setup and the frame timing") do
172
192
  window_only("--verbose") { @verbose = true }
173
193
  end
@@ -6,8 +6,9 @@ module Badline
6
6
  # The timing and geometry a video standard gives the machine.
7
7
  module Region
8
8
  # The CPU clock, the VIC's raster, where the beam is blanked, the display
9
- # window's edges, the part of the raster a front end shows, and the
10
- # mains frequency that feeds the CIAs' TOD pins.
9
+ # window's edges, the part of the raster a front end shows, the mains
10
+ # frequency that feeds the CIAs' TOD pins, and where the VIC's sprite
11
+ # fetches and X counter sit in the line.
11
12
  #
12
13
  # Columns are the VIC's cycles within a line, counted from 0, and a
13
14
  # column draws 8 pixels, so the raster is `cycles_per_line * 8` pixels
@@ -16,8 +17,15 @@ module Badline
16
17
  # is the larger. `display_x_bounds` holds the display window's
17
18
  # [left, right] pixels per CSEL state, 38 columns then 40, and `crop`
18
19
  # the [left, top, width, height] a front end shows.
20
+ #
21
+ # `sprite_cycle` is Bauer's cycle for sprite 0's p-access, and each
22
+ # sprite after it takes two more. `sprite_display_cycle` is the cycle
23
+ # that turns a sprite's display on or off. `x_hold` is how many pixels
24
+ # the X counter spends past its run, repeating $184-$187, and a raster
25
+ # of more than 512 pixels needs them.
19
26
  Profile = Data.define(:name, :clock_hz, :cycles_per_line, :lines_per_frame, :hblank, :vblank,
20
- :display_x_bounds, :crop, :mains_hz)
27
+ :display_x_bounds, :crop, :mains_hz, :sprite_cycle, :sprite_display_cycle,
28
+ :x_hold)
21
29
 
22
30
  # The 6569 of a PAL C64: 63 cycles by 312 lines at 985,248 Hz, with
23
31
  # 50 Hz mains.
@@ -25,19 +33,36 @@ module Badline
25
33
  name: :pal, clock_hz: 985_248, cycles_per_line: 63, lines_per_frame: 312,
26
34
  hblank: [61, 9].freeze, vblank: [300, 15].freeze,
27
35
  display_x_bounds: [[135, 438].freeze, [128, 447].freeze].freeze,
28
- crop: [96, 20, 384, 272].freeze, mains_hz: 50
36
+ crop: [96, 20, 384, 272].freeze, mains_hz: 50,
37
+ sprite_cycle: 58, sprite_display_cycle: 58, x_hold: 0
29
38
  )
30
39
 
31
40
  # The 6567R8 of an NTSC C64: 65 cycles by 263 lines at 1,022,727 Hz,
32
- # with 60 Hz mains, and lines 13-40 blanked (Bauer). The machine does
33
- # not run it yet: the blanked columns and the display window are PAL's
34
- # stand-ins, and the crop leaves out lines 0-12, which show below the
35
- # last line.
41
+ # with 60 Hz mains. The sprite fetches and the compares ahead of them
42
+ # run a cycle later than on the 6569 (the spritesteal, spritex and
43
+ # phi1timing testprogs), and the X counter pauses near $184, as Bauer's
44
+ # 6567R8 diagram shows, reading $184-$187 for 8 pixels more. The
45
+ # blanked lines are the ones VICE's NTSC view leaves out, 12-27. It shows lines 0-11 below
46
+ # line 262, and the crop leaves those out.
36
47
  NTSC = Profile.new(
37
48
  name: :ntsc, clock_hz: 1_022_727, cycles_per_line: 65, lines_per_frame: 263,
38
- hblank: [61, 9].freeze, vblank: [13, 40].freeze,
49
+ hblank: [63, 9].freeze, vblank: [12, 27].freeze,
50
+ display_x_bounds: [[135, 438].freeze, [128, 447].freeze].freeze,
51
+ crop: [96, 28, 384, 235].freeze, mains_hz: 60,
52
+ sprite_cycle: 59, sprite_display_cycle: 59, x_hold: 8
53
+ )
54
+
55
+ # The 6567R56A of the first NTSC C64s: 64 cycles by 262 lines at
56
+ # 1,022,727 Hz. Its sprite fetches run a cycle later than the 6569's,
57
+ # as on the 6567R8, but it turns the display on in the same cycle as
58
+ # the 6569, and its X counter runs straight through 512 pixels, one
59
+ # coordinate for each.
60
+ NTSC_OLD = Profile.new(
61
+ name: :ntscold, clock_hz: 1_022_727, cycles_per_line: 64, lines_per_frame: 262,
62
+ hblank: [62, 9].freeze, vblank: [13, 27].freeze,
39
63
  display_x_bounds: [[135, 438].freeze, [128, 447].freeze].freeze,
40
- crop: [96, 41, 384, 222].freeze, mains_hz: 60
64
+ crop: [96, 28, 384, 234].freeze, mains_hz: 60,
65
+ sprite_cycle: 59, sprite_display_cycle: 58, x_hold: 0
41
66
  )
42
67
 
43
68
  # Whether a column or line lies in a [first, last] blanking span.
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class REU
5
+ # One transfer between the C64 and the REU's RAM, clocked a cycle at a
6
+ # time while the REC holds the bus. The REC only reads the C64's bus
7
+ # while BA is high, so a read followed by BA low waits for BA to rise.
8
+ # After a write it goes on through the first BA-low cycle and stops at
9
+ # the second. See doc/pinned-behaviour.md, REU DMA.
10
+ class DMA
11
+ # Transfer types, as the command register's bottom two bits give
12
+ # them.
13
+ STASH = 0
14
+ FETCH = 1
15
+ SWAP = 2
16
+ VERIFY = 3
17
+
18
+ # What the REC did on the cycle before.
19
+ IDLE = 0
20
+ READ = 1
21
+ WRITE = 2
22
+
23
+ # Where the transfer stopped, what it left for the length register,
24
+ # and the status bits it ended with.
25
+ attr_reader :c64, :expansion, :length, :events
26
+
27
+ def initialize(ram, span, bus)
28
+ @ram = ram
29
+ @span = span
30
+ @bus = bus
31
+ @c64 = @expansion = @length = @events = 0
32
+ @holding = false
33
+ end
34
+
35
+ # Whether the REC held the bus on the last cycle clocked, which it
36
+ # does while it waits for BA too, since the CPU stays halted.
37
+ def holds_bus? = @holding
38
+
39
+ # A length of 0 moves 64K. control is the address control register,
40
+ # which can hold either address where it is.
41
+ def start(type, c64, expansion, length, control)
42
+ @type = type
43
+ @c64 = c64
44
+ @expansion = expansion
45
+ @length = length.zero? ? 0x10000 : length
46
+ @c64_step = control.anybits?(FIX_C64) ? 0 : 1
47
+ @expansion_step = control.anybits?(FIX_EXPANSION) ? 0 : 1
48
+ @events = 0
49
+ @holding = true
50
+ @last = IDLE
51
+ @waiting = false
52
+ @ba_low_after_writes = 0
53
+ @stopped_after_write = false
54
+ @swap_write_due = false
55
+ @swap_byte = 0
56
+ @held_read = 0
57
+ @moved_all = false
58
+ @extra_cycle = false
59
+ end
60
+
61
+ # One cycle of the transfer. The cycle the REC lets go of the bus on
62
+ # is the CPU's.
63
+ def cycle!(ba_low)
64
+ note_ba(ba_low)
65
+ settle_held_read(ba_low) if @held_read.positive?
66
+ return if @waiting && ba_low
67
+
68
+ @waiting = false
69
+ @moved_all ? wind_down : move(ba_low)
70
+ end
71
+
72
+ private
73
+
74
+ # The second BA-low cycle in a row after writes stops the REC, and so
75
+ # does any BA-low cycle after a read. It then waits for BA to rise.
76
+ def note_ba(ba_low)
77
+ if @last == READ
78
+ @waiting = ba_low
79
+ elsif @last == WRITE
80
+ @ba_low_after_writes = ba_low ? @ba_low_after_writes + 1 : 0
81
+ @stopped_after_write = @ba_low_after_writes == 2
82
+ if @stopped_after_write
83
+ @waiting = true
84
+ @ba_low_after_writes = 0
85
+ end
86
+ end
87
+ @last = IDLE
88
+ end
89
+
90
+ # A fetch or a swap whose last write ran into BA, or a verify that
91
+ # found a difference before its last byte, takes one more cycle before
92
+ # the REC lets go of the bus.
93
+ def wind_down
94
+ if @extra_cycle || (@stopped_after_write && (@type == FETCH || @type == SWAP))
95
+ @extra_cycle = @stopped_after_write = false
96
+ @last = READ
97
+ return
98
+ end
99
+
100
+ @holding = false
101
+ settle
102
+ end
103
+
104
+ def move(ba_low)
105
+ case @type
106
+ when STASH then stash
107
+ when FETCH then fetch
108
+ when SWAP then swap(ba_low)
109
+ else verify
110
+ end
111
+ end
112
+
113
+ # The REU's data bus latch keeps the last byte the REC stored.
114
+ def stash
115
+ byte = read_c64
116
+ @ram.poke(@expansion, byte)
117
+ @ram.latch = byte
118
+ step
119
+ @last = READ
120
+ end
121
+
122
+ def fetch
123
+ byte = @ram.peek(@expansion)
124
+ @ram.latch = byte
125
+ write_c64(byte)
126
+ step
127
+ @last = WRITE
128
+ end
129
+
130
+ # Two cycles a byte: the C64's byte is read and stored while the
131
+ # REU's is fetched, and the REU's is written to the C64 on the next.
132
+ def swap(ba_low)
133
+ if @swap_write_due
134
+ @swap_write_due = false
135
+ write_c64(@swap_byte)
136
+ step
137
+ @last = WRITE
138
+ return
139
+ end
140
+
141
+ @swap_byte = @ram.peek(@expansion)
142
+ @swap_write_due = true
143
+ @last = READ
144
+ return swap_read_on_ba if ba_low
145
+
146
+ @ram.poke(@expansion, read_c64)
147
+ end
148
+
149
+ # A swap's read on the first BA-low cycle after a write, while AEC is
150
+ # still high. On the last byte the read is made there, the write
151
+ # follows on the next cycle whatever BA does, and the REC lets go.
152
+ # Otherwise the read stays open and takes what is on the bus two
153
+ # cycles later, or on the cycle BA rises if that comes first. Pinned
154
+ # by REU/reutiming2/e5-m2, f3-m2 and f4-m2.
155
+ def swap_read_on_ba
156
+ return @held_read = 2 unless @length == 1
157
+
158
+ @ram.poke(@expansion, read_c64)
159
+ @last = IDLE
160
+ @ba_low_after_writes = 0
161
+ end
162
+
163
+ def settle_held_read(ba_low)
164
+ @held_read -= 1
165
+ return if @held_read.positive? && ba_low
166
+
167
+ @held_read = 0
168
+ @ram.poke(@expansion, read_c64)
169
+ end
170
+
171
+ # A difference ends the transfer, with one cycle more if bytes were
172
+ # left to compare.
173
+ def verify
174
+ same = @ram.peek(@expansion) == read_c64
175
+ step
176
+ @last = READ
177
+ return if same
178
+
179
+ @events |= FAULT
180
+ @extra_cycle = @length.positive?
181
+ @moved_all = true
182
+ end
183
+
184
+ # The REC reaches C64 memory as the CPU sees it, but at $00 and $01
185
+ # it reaches the RAM under the CPU's port.
186
+ def read_c64
187
+ @c64 < 0x02 ? @bus.ram.peek(@c64) : @bus.peek(@c64)
188
+ end
189
+
190
+ def write_c64(byte)
191
+ @c64 < 0x02 ? @bus.ram.poke(@c64, byte) : @bus.poke(@c64, byte)
192
+ end
193
+
194
+ # The REC counts the expansion address in its 19 low bits, from the
195
+ # top of its span back to 0, and leaves the bank bits above them as
196
+ # they were.
197
+ def step
198
+ @c64 = (@c64 + @c64_step) & 0xffff
199
+ low = (@expansion & 0x7ffff) + @expansion_step
200
+ @expansion = (@expansion & 0xf80000) | (low == @span ? 0 : low)
201
+ @length -= 1
202
+ @moved_all = true if @length.zero?
203
+ end
204
+
205
+ # The length register is left at 1 after a whole block. A fetch has
206
+ # already fetched the byte after its last into the latch.
207
+ def settle
208
+ return settle_verify if @type == VERIFY
209
+
210
+ @length += 1
211
+ @events |= END_OF_BLOCK
212
+ @ram.latch = @ram.peek(@expansion) if @type == FETCH
213
+ end
214
+
215
+ # A verify that compared every byte ends the block. One that found a
216
+ # difference on the byte before the last compares the last too, and
217
+ # ends the block if that one is the same.
218
+ def settle_verify
219
+ if @length.zero?
220
+ @length = 1
221
+ @events |= END_OF_BLOCK
222
+ elsif @length == 1 && @ram.peek(@expansion) == read_c64
223
+ @events |= END_OF_BLOCK
224
+ end
225
+ end
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class REU
5
+ # The REU's DRAM, as the REC addresses it. The REC counts 19 address
6
+ # bits, 17 on the 1700, and the 1764 fills only the first 256K of
7
+ # them: past that a read finds the latch on the REU's data bus, which
8
+ # holds the last byte the REC moved. A unit past 512K takes its upper
9
+ # address lines from the bank register.
10
+ class RAM
11
+ BANK_SIZE = 0x10000
12
+
13
+ # Where a bank of a 1764 that has just been switched on reads
14
+ # inverted, over the first 128K of each 256K. Over the second 128K
15
+ # it is the rest of the bank that reads inverted. Measured from
16
+ # REU/raminitpattern/dumpfile-256k-x1541.bin, a real 1764's dump.
17
+ INVERTED = [0x2a00...0x5400, 0x8000...0xac00, 0xd600...0x10000].freeze
18
+
19
+ attr_reader :size
20
+ attr_accessor :latch
21
+
22
+ def initialize(size, span)
23
+ @size = size
24
+ @mask = (size > 0x80000 ? size : span) - 1
25
+ @banks = Array.new(size / BANK_SIZE) { [] }
26
+ @power_on = [[], []]
27
+ @latch = 0xff
28
+ end
29
+
30
+ def peek(addr)
31
+ addr &= @mask
32
+ addr < @size ? bank(addr >> 16)[addr & 0xffff] : @latch
33
+ end
34
+
35
+ def poke(addr, value)
36
+ addr &= @mask
37
+ bank(addr >> 16)[addr & 0xffff] = value if addr < @size
38
+ end
39
+
40
+ private
41
+
42
+ # A bank takes its power-on contents the first time it is touched,
43
+ # so an REU costs only the memory a program uses.
44
+ def bank(number)
45
+ bank = @banks[number]
46
+ return bank unless bank.empty?
47
+
48
+ half = (number >> 1) & 1
49
+ @power_on[half] = power_on_bank(half == 1) if @power_on[half].empty?
50
+ @banks[number] = @power_on[half].dup
51
+ end
52
+
53
+ # Pairs of $FF and $00 bytes, starting with a single $FF, and each
54
+ # odd page the inverse of the even one before it.
55
+ def power_on_bank(upper)
56
+ Array.new(BANK_SIZE) do |offset|
57
+ byte = (offset + 1).anybits?(2) ? 0x00 : 0xff
58
+ byte ^= 0xff if offset.anybits?(0x100)
59
+ inverted = INVERTED.any? { |range| range.cover?(offset) }
60
+ inverted == upper ? byte : byte ^ 0xff
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class REU
5
+ # Stands in for the page at $FF00 in the write table, passing each
6
+ # write on to what is mapped there and telling the REC when $FF00 is
7
+ # written, which starts an armed transfer.
8
+ class Trigger
9
+ ADDRESS = 0xff00
10
+
11
+ def initialize(reu)
12
+ @reu = reu
13
+ @target = nil
14
+ end
15
+
16
+ # Puts the trigger in front of target and returns it.
17
+ def wrap(target)
18
+ @target = target
19
+ self
20
+ end
21
+
22
+ def peek(addr)
23
+ @target.peek(addr)
24
+ end
25
+
26
+ def poke(addr, value)
27
+ @target.poke(addr, value)
28
+ @reu.ff00_written if addr == ADDRESS
29
+ end
30
+ end
31
+ end
32
+ end