badline 0.11.0 → 0.11.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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/CLAUDE.md +20 -3
  4. data/CONTRIBUTING.md +8 -3
  5. data/README.md +6 -2
  6. data/lib/badline/c128/drives.rb +95 -0
  7. data/lib/badline/c128/fast_serial.rb +62 -0
  8. data/lib/badline/c128/mmu.rb +15 -0
  9. data/lib/badline/c128/saved_state.rb +1 -0
  10. data/lib/badline/c128.rb +9 -7
  11. data/lib/badline/cia/fast_forward.rb +42 -0
  12. data/lib/badline/cia/serial.rb +12 -0
  13. data/lib/badline/cia/timer.rb +39 -0
  14. data/lib/badline/cia.rb +2 -0
  15. data/lib/badline/computer/attachments.rb +6 -0
  16. data/lib/badline/drive/core.rb +172 -0
  17. data/lib/badline/{drive1541 → drive}/idle.rb +18 -19
  18. data/lib/badline/drive/idle_cpu.rb +28 -0
  19. data/lib/badline/{drive1541 → drive}/orbit.rb +2 -1
  20. data/lib/badline/drive/serial_lines.rb +73 -0
  21. data/lib/badline/{drive1541 → drive}/sleep.rb +4 -3
  22. data/lib/badline/drive/watch.rb +150 -0
  23. data/lib/badline/drive.rb +9 -0
  24. data/lib/badline/drive1541/bus.rb +5 -129
  25. data/lib/badline/drive1541/byte_ready.rb +48 -0
  26. data/lib/badline/drive1541/cpu.rb +1 -19
  27. data/lib/badline/drive1541/disk.rb +77 -20
  28. data/lib/badline/drive1541/disk_state.rb +7 -2
  29. data/lib/badline/drive1541/disk_via.rb +3 -0
  30. data/lib/badline/drive1541/mechanism.rb +48 -14
  31. data/lib/badline/drive1541/mechanism_state.rb +5 -4
  32. data/lib/badline/drive1541/rotation.rb +5 -5
  33. data/lib/badline/drive1541/serial_port.rb +4 -60
  34. data/lib/badline/drive1541.rb +13 -172
  35. data/lib/badline/drive1571/bus.rb +111 -0
  36. data/lib/badline/drive1571/cpu.rb +11 -0
  37. data/lib/badline/drive1571/serial_port.rb +37 -0
  38. data/lib/badline/drive1571/serial_via.rb +25 -0
  39. data/lib/badline/drive1571/wd1770.rb +34 -0
  40. data/lib/badline/drive1571.rb +225 -0
  41. data/lib/badline/frontend/app.rb +1 -1
  42. data/lib/badline/frontend/drive_led.rb +1 -1
  43. data/lib/badline/frontend/menu_media.rb +3 -3
  44. data/lib/badline/frontend/menu_pages.rb +2 -1
  45. data/lib/badline/iec_bus.rb +54 -4
  46. data/lib/badline/machine.rb +7 -5
  47. data/lib/badline/media/true_drive.rb +26 -23
  48. data/lib/badline/options/help.rb +2 -1
  49. data/lib/badline/options/table.rb +1 -1
  50. data/lib/badline/roms/README +9 -0
  51. data/lib/badline/roms/dos1571.rom +0 -0
  52. data/lib/badline/storage/d64_image.rb +3 -0
  53. data/lib/badline/storage/d71_image.rb +9 -4
  54. data/lib/badline/storage/g64_image.rb +7 -2
  55. data/lib/badline/time_of_day.rb +9 -0
  56. data/lib/badline/version.rb +1 -1
  57. data/lib/badline/via/fast_forward.rb +3 -3
  58. data/lib/badline/vic20/attachments.rb +6 -0
  59. data/lib/badline/z80/arithmetic.rb +117 -0
  60. data/lib/badline/z80/bit_page.rb +55 -0
  61. data/lib/badline/z80/block_io.rb +58 -0
  62. data/lib/badline/z80/block_transfers.rb +59 -0
  63. data/lib/badline/z80/bus_cycles.rb +111 -0
  64. data/lib/badline/z80/core.rb +111 -0
  65. data/lib/badline/z80/exchanges.rb +50 -0
  66. data/lib/badline/z80/extended_page.rb +89 -0
  67. data/lib/badline/z80/flags.rb +37 -0
  68. data/lib/badline/z80/interrupt_acceptance.rb +46 -0
  69. data/lib/badline/z80/jumps.rb +82 -0
  70. data/lib/badline/z80/loads.rb +98 -0
  71. data/lib/badline/z80/main_page.rb +134 -0
  72. data/lib/badline/z80/registers.rb +120 -0
  73. data/lib/badline/z80/shifts.rb +64 -0
  74. data/lib/badline/z80.rb +11 -0
  75. metadata +36 -4
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # The ALU's 8- and 16-bit arithmetic. Bits 5 and 3 of F come from the
6
+ # result, except for CP, which takes them from the operand, and the
7
+ # 16-bit operations, which take them from the high byte.
8
+ module Arithmetic
9
+ private
10
+
11
+ # The eight operations an opcode's bits 5-3 pick: ADD ADC SUB SBC AND
12
+ # XOR OR CP.
13
+ def alu(operation, value)
14
+ case operation
15
+ when 0 then add_a(value, 0)
16
+ when 1 then add_a(value, @f & CF)
17
+ when 2 then @a = subtract(value, 0)
18
+ when 3 then @a = subtract(value, @f & CF)
19
+ when 4 then logic(@a & value, HF)
20
+ when 5 then logic(@a ^ value, 0)
21
+ when 6 then logic(@a | value, 0)
22
+ else compare(value)
23
+ end
24
+ end
25
+
26
+ def add_a(value, carry)
27
+ result = @a + value + carry
28
+ assign_flags(SZ53[result & 0xff] | (result >> 8) | ((@a ^ value ^ result) & HF) |
29
+ (((@a ^ value ^ 0x80) & (@a ^ result) & 0x80) >> 5))
30
+ @a = result & 0xff
31
+ end
32
+
33
+ def subtract(value, carry)
34
+ result = @a - value - carry
35
+ assign_flags(SZ53[result & 0xff] | NF | ((result >> 8) & CF) | ((@a ^ value ^ result) & HF) |
36
+ (((@a ^ value) & (@a ^ result) & 0x80) >> 5))
37
+ result & 0xff
38
+ end
39
+
40
+ def compare(value)
41
+ subtract(value, 0)
42
+ assign_flags((@f & ~XYF) | (value & XYF))
43
+ end
44
+
45
+ def logic(result, half)
46
+ @a = result
47
+ assign_flags(SZ53P[result] | half)
48
+ end
49
+
50
+ def increment(value)
51
+ result = (value + 1) & 0xff
52
+ assign_flags((@f & CF) | SZ53[result] | (result == 0x80 ? PF : 0) | (result.nobits?(0x0f) ? HF : 0))
53
+ result
54
+ end
55
+
56
+ def decrement(value)
57
+ result = (value - 1) & 0xff
58
+ assign_flags((@f & CF) | NF | SZ53[result] | (result == 0x7f ? PF : 0) | (value.nobits?(0x0f) ? HF : 0))
59
+ result
60
+ end
61
+
62
+ def add16(left, right)
63
+ result = left + right
64
+ @wz = (left + 1) & 0xffff
65
+ assign_flags((@f & (SF | ZF | PF)) | ((result >> 8) & XYF) | (((left ^ right ^ result) >> 8) & HF) |
66
+ (result >> 16))
67
+ result & 0xffff
68
+ end
69
+
70
+ def adc16(right)
71
+ left = hl
72
+ result = left + right + (@f & CF)
73
+ @wz = (left + 1) & 0xffff
74
+ self.hl = result & 0xffff
75
+ assign_flags(word_flags(result) | (((left ^ right ^ result) >> 8) & HF) |
76
+ (((left ^ right ^ 0x8000) & (left ^ result) & 0x8000) >> 13))
77
+ end
78
+
79
+ def sbc16(right)
80
+ left = hl
81
+ result = left - right - (@f & CF)
82
+ @wz = (left + 1) & 0xffff
83
+ self.hl = result & 0xffff
84
+ assign_flags(word_flags(result) | NF | (((left ^ right ^ result) >> 8) & HF) |
85
+ (((left ^ right) & (left ^ result) & 0x8000) >> 13))
86
+ end
87
+
88
+ def word_flags(result)
89
+ ((result >> 8) & (SF | XYF)) | (result.nobits?(0xffff) ? ZF : 0) | ((result >> 16) & CF)
90
+ end
91
+
92
+ def daa
93
+ low = @a & 0x0f
94
+ carry = @f.anybits?(CF) || @a > 0x99 ? CF : 0
95
+ correction = (carry.zero? ? 0 : 0x60) | (@f.anybits?(HF) || low > 9 ? 0x06 : 0)
96
+ subtracting = @f.anybits?(NF)
97
+ half = daa_half(low, subtracting)
98
+ @a = (subtracting ? @a - correction : @a + correction) & 0xff
99
+ assign_flags(SZ53P[@a] | (@f & NF) | carry | half)
100
+ end
101
+
102
+ def daa_half(low, subtracting)
103
+ if subtracting
104
+ @f.anybits?(HF) && low < 6 ? HF : 0
105
+ else
106
+ low > 9 ? HF : 0
107
+ end
108
+ end
109
+
110
+ def neg
111
+ value = @a
112
+ @a = 0
113
+ @a = subtract(value, 0)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # The CB page: rotates and shifts (quarter 0), BIT (1), RES (2) and SET
6
+ # (3) of the bit the row names, on the column's register or (HL). After
7
+ # DD or FD the operand is always (IX+d): the displacement comes before
8
+ # the opcode, and an opcode that names a register other than (HL) also
9
+ # copies the result into it.
10
+ module BitPage
11
+ private
12
+
13
+ def execute_bits(opcode)
14
+ operation = opcode >> 6
15
+ number = (opcode >> 3) & 7
16
+ index = opcode & 7
17
+ return bits_in_memory(operation, number, hl) if index == 6
18
+
19
+ value = register(index)
20
+ if operation == 1
21
+ bit(number, value, value)
22
+ else
23
+ set_register(index, bit_result(operation, number, value))
24
+ end
25
+ end
26
+
27
+ def execute_index_bits
28
+ displaced
29
+ opcode = fetch_byte
30
+ @cycles += 2
31
+ result = bits_in_memory(opcode >> 6, (opcode >> 3) & 7, @wz)
32
+ index = opcode & 7
33
+ set_plain_register(index, result) unless index == 6 || (opcode >> 6) == 1
34
+ end
35
+
36
+ def bits_in_memory(operation, number, address)
37
+ value = read_byte(address)
38
+ @cycles += 1
39
+ return bit(number, value, @wz >> 8) if operation == 1
40
+
41
+ result = bit_result(operation, number, value)
42
+ write_byte(address, result)
43
+ result
44
+ end
45
+
46
+ def bit_result(operation, number, value)
47
+ case operation
48
+ when 0 then shift(number, value)
49
+ when 2 then value & ~(1 << number) & 0xff
50
+ else value | (1 << number)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # INI, IND, OUTI, OUTD and their repeating forms. B counts the bytes
6
+ # and sets S, Z, 5 and 3. The byte's bit 7 sets N, and adding it to C
7
+ # plus or minus one for INI and IND, or to L for OUTI and OUTD, sets H
8
+ # and C on a carry out and P/V from the parity of its low 3 bits
9
+ # exclusive-ored with B.
10
+ module BlockIO
11
+ private
12
+
13
+ def block_input(step, repeat)
14
+ @cycles += 1
15
+ port = bc
16
+ value = input(port)
17
+ @wz = (port + step) & 0xffff
18
+ @b = (@b - 1) & 0xff
19
+ write_byte(hl, value)
20
+ self.hl = (hl + step) & 0xffff
21
+ block_io_flags(value, value + ((@c + step) & 0xff), repeat)
22
+ end
23
+
24
+ def block_output(step, repeat)
25
+ @cycles += 1
26
+ value = read_byte(hl)
27
+ @b = (@b - 1) & 0xff
28
+ output(bc, value)
29
+ self.hl = (hl + step) & 0xffff
30
+ @wz = (bc + step) & 0xffff
31
+ block_io_flags(value, value + @l, repeat)
32
+ end
33
+
34
+ def block_io_flags(value, sum, repeat)
35
+ carry = sum > 0xff ? HF | CF : 0
36
+ assign_flags(SZ53[@b] | ((value >> 6) & NF) | carry | PARITY[(sum & 7) ^ @b])
37
+ return unless repeat && !@b.zero?
38
+
39
+ repeat_block
40
+ repeat_io_flags(value, carry)
41
+ end
42
+
43
+ def repeat_io_flags(value, carry)
44
+ flags = @f & ~HF
45
+ if carry.zero?
46
+ flags ^= PARITY[@b & 7] ^ PF
47
+ elsif value.anybits?(0x80)
48
+ flags ^= PARITY[(@b - 1) & 7] ^ PF
49
+ flags |= HF if @b.nobits?(0x0f)
50
+ else
51
+ flags ^= PARITY[(@b + 1) & 7] ^ PF
52
+ flags |= HF if @b.allbits?(0x0f)
53
+ end
54
+ assign_flags(flags)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # LDI, LDD, CPI, CPD and their repeating forms. A repeating form that
6
+ # goes round again winds the program counter back to its ED and spends
7
+ # 5 T-states more, and bits 5 and 3 then come from the program
8
+ # counter's high byte.
9
+ module BlockTransfers
10
+ private
11
+
12
+ # ED A0-BB: the column picks LD, CP, IN or OUT, bit 3 counts down, and
13
+ # bit 4 repeats.
14
+ def block_operation(opcode)
15
+ step = opcode.nobits?(0x08) ? 1 : -1
16
+ repeat = opcode.anybits?(0x10)
17
+ case opcode & 3
18
+ when 0 then block_load(step, repeat)
19
+ when 1 then block_compare(step, repeat)
20
+ when 2 then block_input(step, repeat)
21
+ else block_output(step, repeat)
22
+ end
23
+ end
24
+
25
+ def block_load(step, repeat)
26
+ value = read_byte(hl)
27
+ write_byte(de, value)
28
+ @cycles += 2
29
+ self.hl = (hl + step) & 0xffff
30
+ self.de = (de + step) & 0xffff
31
+ self.bc = (bc - 1) & 0xffff
32
+ sum = value + @a
33
+ assign_flags((@f & (SF | ZF | CF)) | (sum & XF) | ((sum & 0x02) << 4) | (bc.zero? ? 0 : PF))
34
+ repeat_block if repeat && !bc.zero?
35
+ end
36
+
37
+ def block_compare(step, repeat)
38
+ value = read_byte(hl)
39
+ @cycles += 5
40
+ result = (@a - value) & 0xff
41
+ half = (@a ^ value ^ result) & HF
42
+ adjusted = result - (half >> 4)
43
+ self.hl = (hl + step) & 0xffff
44
+ self.bc = (bc - 1) & 0xffff
45
+ @wz = (@wz + step) & 0xffff
46
+ assign_flags((@f & CF) | NF | half | (SZ53[result] & (SF | ZF)) | (adjusted & XF) | ((adjusted & 0x02) << 4) |
47
+ (bc.zero? ? 0 : PF))
48
+ repeat_block if repeat && !bc.zero? && !result.zero?
49
+ end
50
+
51
+ def repeat_block
52
+ @cycles += 5
53
+ @pc = (@pc - 2) & 0xffff
54
+ @wz = (@pc + 1) & 0xffff
55
+ assign_flags((@f & ~XYF) | ((@pc >> 8) & XYF))
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # The machine cycles that reach the bus. An opcode fetch (M1) takes 4
6
+ # T-states and refreshes a row of DRAM, which counts up the low 7 bits
7
+ # of R. A memory read or write takes 3, and an I/O read or write 4,
8
+ # one of them the wait state the Z80 inserts itself. Each calls the bus
9
+ # on its first T-state, and T-states the CPU spends inside lengthen
10
+ # the machine cycle before them.
11
+ module BusCycles
12
+ private
13
+
14
+ def fetch_opcode
15
+ opcode = @bus.fetch(@pc)
16
+ @pc = (@pc + 1) & 0xffff
17
+ refresh
18
+ @cycles += 4
19
+ opcode
20
+ end
21
+
22
+ # A halted CPU fetches from the address after HALT, again and again,
23
+ # and leaves the program counter there.
24
+ def idle_fetch
25
+ @bus.fetch(@pc)
26
+ refresh
27
+ @cycles += 4
28
+ end
29
+
30
+ def refresh
31
+ @r = (@r & 0x80) | ((@r + 1) & 0x7f)
32
+ end
33
+
34
+ def read_byte(address)
35
+ value = @bus.read(address)
36
+ @cycles += 3
37
+ value
38
+ end
39
+
40
+ def write_byte(address, value)
41
+ @bus.write(address, value)
42
+ @cycles += 3
43
+ end
44
+
45
+ def read_word(address)
46
+ low = read_byte(address)
47
+ low | (read_byte((address + 1) & 0xffff) << 8)
48
+ end
49
+
50
+ def write_word(address, value)
51
+ write_byte(address, value & 0xff)
52
+ write_byte((address + 1) & 0xffff, value >> 8)
53
+ end
54
+
55
+ def input(port)
56
+ value = @bus.input(port)
57
+ @cycles += 4
58
+ value
59
+ end
60
+
61
+ def output(port, value)
62
+ @bus.output(port, value)
63
+ @cycles += 4
64
+ end
65
+
66
+ # The byte after the opcode.
67
+ def fetch_byte
68
+ value = read_byte(@pc)
69
+ @pc = (@pc + 1) & 0xffff
70
+ value
71
+ end
72
+
73
+ def fetch_word
74
+ low = fetch_byte
75
+ low | (fetch_byte << 8)
76
+ end
77
+
78
+ def push(value)
79
+ @sp = (@sp - 1) & 0xffff
80
+ write_byte(@sp, value >> 8)
81
+ @sp = (@sp - 1) & 0xffff
82
+ write_byte(@sp, value & 0xff)
83
+ end
84
+
85
+ def pop
86
+ low = read_byte(@sp)
87
+ high = read_byte((@sp + 1) & 0xffff)
88
+ @sp = (@sp + 2) & 0xffff
89
+ low | (high << 8)
90
+ end
91
+
92
+ # The address of an (HL) operand, or of (IX+d), whose displacement is
93
+ # read from after the opcode and added in 5 T-states inside.
94
+ def operand_address
95
+ return hl if @prefix.zero?
96
+
97
+ displaced
98
+ @cycles += 5
99
+ @wz
100
+ end
101
+
102
+ # Reads the displacement of an (IX+d) or (IY+d) operand and leaves
103
+ # the address in WZ.
104
+ def displaced
105
+ offset = fetch_byte
106
+ offset -= 256 if offset > 127
107
+ @wz = (index_register + offset) & 0xffff
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "badline/z80/flags"
4
+ require "badline/z80/registers"
5
+ require "badline/z80/bus_cycles"
6
+ require "badline/z80/arithmetic"
7
+ require "badline/z80/shifts"
8
+ require "badline/z80/loads"
9
+ require "badline/z80/exchanges"
10
+ require "badline/z80/jumps"
11
+ require "badline/z80/main_page"
12
+ require "badline/z80/bit_page"
13
+ require "badline/z80/extended_page"
14
+ require "badline/z80/block_transfers"
15
+ require "badline/z80/block_io"
16
+ require "badline/z80/interrupt_acceptance"
17
+
18
+ module Badline
19
+ class Z80
20
+ # The Z80 core, on whatever bus +bus+ is. The bus answers
21
+ # fetch(address) for an opcode fetch (M1), read(address) and
22
+ # write(address, value) for memory, input(port) and output(port, value)
23
+ # for I/O, and acknowledge for the byte a device puts on the data bus
24
+ # when the CPU takes a maskable interrupt.
25
+ #
26
+ # #step! runs one instruction, prefixes and all, or takes an interrupt,
27
+ # or idles one opcode fetch while halted. #cycles counts T-states, and
28
+ # each bus call happens while it holds the T-state its machine cycle
29
+ # starts on.
30
+ module Core
31
+ include Flags
32
+ include Registers
33
+ include BusCycles
34
+ include Arithmetic
35
+ include Shifts
36
+ include Loads
37
+ include Exchanges
38
+ include Jumps
39
+ include MainPage
40
+ include BitPage
41
+ include ExtendedPage
42
+ include BlockTransfers
43
+ include BlockIO
44
+ include InterruptAcceptance
45
+
46
+ attr_reader :bus, :cycles, :nmi
47
+ attr_accessor :int
48
+
49
+ def initialize(bus)
50
+ @bus = bus
51
+ @cycles = 0
52
+ @a = @f = 0xff
53
+ @b = @c = @d = @e = @h = @l = 0xff
54
+ @af_alt = @bc_alt = @de_alt = @hl_alt = 0xffff
55
+ @ix = @iy = @sp = 0xffff
56
+ @wz = @q = @last_q = 0
57
+ @prefix = 0
58
+ @int = @nmi = @nmi_pending = false
59
+ reset!
60
+ end
61
+
62
+ # The RESET line: the program counter, I, R and the interrupt mode go
63
+ # to 0, and interrupts are disabled.
64
+ def reset!
65
+ @pc = @i = @r = @im = 0
66
+ @iff1 = @iff2 = false
67
+ @halted = @after_ei = @after_ld_a_ir = false
68
+ @nmi_pending = false
69
+ end
70
+
71
+ # The NMI line. Asserting it latches an interrupt, taken after the
72
+ # current instruction, and holding it does nothing more.
73
+ def nmi=(asserted)
74
+ @nmi_pending = true if asserted && !@nmi
75
+ @nmi = asserted
76
+ end
77
+
78
+ # The WAIT line: a bus that holds it during an access lengthens that
79
+ # machine cycle by +states+ T-states.
80
+ def wait(states)
81
+ @cycles += states
82
+ end
83
+
84
+ def step!
85
+ if @nmi_pending
86
+ take_nmi
87
+ elsif @int && @iff1 && !@after_ei
88
+ take_int
89
+ else
90
+ @after_ei = @after_ld_a_ir = false
91
+ @last_q = @q
92
+ @q = 0
93
+ @prefix = 0
94
+ @halted ? idle_fetch : execute(fetch_opcode)
95
+ end
96
+ nil
97
+ end
98
+
99
+ def inspect
100
+ "Cycles: #{@cycles}, PC: #{hex(@pc)}, SP: #{hex(@sp)}, AF: #{hex(af)}, BC: #{hex(bc)}, " \
101
+ "DE: #{hex(de)}, HL: #{hex(hl)}, IX: #{hex(@ix)}, IY: #{hex(@iy)}"
102
+ end
103
+
104
+ private
105
+
106
+ def hex(value)
107
+ value.to_s(16).rjust(4, "0")
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # EX AF,AF', EXX, EX DE,HL and EX (SP),HL. Only the last swaps IX or IY
6
+ # after a prefix.
7
+ module Exchanges
8
+ private
9
+
10
+ def exchange_alternates
11
+ bc_alt = @bc_alt
12
+ de_alt = @de_alt
13
+ hl_alt = @hl_alt
14
+ @bc_alt = bc
15
+ @de_alt = de
16
+ @hl_alt = hl
17
+ self.bc = bc_alt
18
+ self.de = de_alt
19
+ self.hl = hl_alt
20
+ end
21
+
22
+ def exchange_af
23
+ af_alt = @af_alt
24
+ @af_alt = af
25
+ self.af = af_alt
26
+ end
27
+
28
+ def exchange_de_hl
29
+ d = @d
30
+ e = @e
31
+ @d = @h
32
+ @e = @l
33
+ @h = d
34
+ @l = e
35
+ end
36
+
37
+ def exchange_stack_top
38
+ low = read_byte(@sp)
39
+ high = read_byte((@sp + 1) & 0xffff)
40
+ @cycles += 1
41
+ value = hl_or_index
42
+ write_byte((@sp + 1) & 0xffff, value >> 8)
43
+ write_byte(@sp, value & 0xff)
44
+ @cycles += 2
45
+ @wz = low | (high << 8)
46
+ self.hl_or_index = @wz
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ # The interrupt mode each of ED 46, 4E, 56, 5E, 66, 6E, 76 and 7E sets.
6
+ # 4E and 6E set mode 0 on the NMOS Z80.
7
+ INTERRUPT_MODES = [0, 0, 1, 2, 0, 0, 1, 2].freeze
8
+
9
+ # The ED page: its second quarter, the block instructions, and 8
10
+ # T-states of nothing for the rest.
11
+ module ExtendedPage
12
+ private
13
+
14
+ def execute_extended(opcode)
15
+ case opcode >> 6
16
+ when 1 then extended_operation(opcode)
17
+ when 2 then block_operation(opcode) if (opcode & 0x24) == 0x20
18
+ end
19
+ end
20
+
21
+ def extended_operation(opcode)
22
+ row = (opcode >> 3) & 7
23
+ case opcode & 7
24
+ when 0 then input_register(row)
25
+ when 1 then output_register(row)
26
+ when 2 then add_pair_with_carry(row)
27
+ when 3 then row.nobits?(1) ? store_word(pair(row >> 1)) : set_pair(row >> 1, load_word)
28
+ when 4 then neg
29
+ when 5 then return_from_interrupt
30
+ when 6 then @im = INTERRUPT_MODES[row]
31
+ else special_register(row)
32
+ end
33
+ end
34
+
35
+ # IN r,(C), and IN F,(C) at index 6, which sets the flags alone.
36
+ def input_register(index)
37
+ port = bc
38
+ value = input(port)
39
+ @wz = (port + 1) & 0xffff
40
+ assign_flags((@f & CF) | SZ53P[value])
41
+ set_register(index, value) unless index == 6
42
+ end
43
+
44
+ # OUT (C),r, and at index 6 OUT (C),0.
45
+ def output_register(index)
46
+ port = bc
47
+ output(port, index == 6 ? 0 : register(index))
48
+ @wz = (port + 1) & 0xffff
49
+ end
50
+
51
+ def add_pair_with_carry(row)
52
+ row.nobits?(1) ? sbc16(pair(row >> 1)) : adc16(pair(row >> 1))
53
+ @cycles += 7
54
+ end
55
+
56
+ # RETN and RETI both restore IFF1 from IFF2.
57
+ def return_from_interrupt
58
+ @iff1 = @iff2
59
+ return_from_call
60
+ end
61
+
62
+ # LD I,A, LD R,A, LD A,I, LD A,R, RRD and RLD, and two that do
63
+ # nothing.
64
+ def special_register(row)
65
+ case row
66
+ when 0 then @i = load_ir(@a)
67
+ when 1 then @r = load_ir(@a)
68
+ when 2 then @a = load_a_from_ir(@i)
69
+ when 3 then @a = load_a_from_ir(@r)
70
+ when 4 then rotate_digits(false)
71
+ when 5 then rotate_digits(true)
72
+ end
73
+ end
74
+
75
+ def load_ir(value)
76
+ @cycles += 1
77
+ value
78
+ end
79
+
80
+ # P/V copies IFF2, and an interrupt taken next clears it again.
81
+ def load_a_from_ir(value)
82
+ @cycles += 1
83
+ assign_flags((@f & CF) | SZ53[value] | (@iff2 ? PF : 0))
84
+ @after_ld_a_ir = true
85
+ value
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Badline
4
+ class Z80
5
+ CF = 0x01
6
+ NF = 0x02
7
+ PF = 0x04
8
+ XF = 0x08
9
+ HF = 0x10
10
+ YF = 0x20
11
+ ZF = 0x40
12
+ SF = 0x80
13
+
14
+ # Bits 5 and 3, which most instructions copy from a result.
15
+ XYF = YF | XF
16
+
17
+ # S, Z, Y and X as a result byte sets them.
18
+ SZ53 = Array.new(256) { |v| (v & (SF | XYF)) | (v.zero? ? ZF : 0) }.freeze
19
+
20
+ # P/V as parity: set when a byte has an even number of bits set.
21
+ PARITY = Array.new(256) { |v| (0x6996 >> ((v ^ (v >> 4)) & 0x0f)).nobits?(1) ? PF : 0 }.freeze
22
+
23
+ SZ53P = Array.new(256) { |v| SZ53[v] | PARITY[v] }.freeze
24
+
25
+ # The flags, which the core keeps in F as the chip does. Every
26
+ # instruction that sets them through the ALU also latches them in Q,
27
+ # which SCF and CCF read on the next instruction.
28
+ module Flags
29
+ private
30
+
31
+ def assign_flags(value)
32
+ @f = value
33
+ @q = value
34
+ end
35
+ end
36
+ end
37
+ end