rubyboy 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rubyboy/cpu.rb CHANGED
@@ -50,509 +50,509 @@ module Rubyboy
50
50
 
51
51
  case opcode
52
52
  when 0x00 then 4 # NOP
53
- when 0x01 then ld16({ type: :register16, value: :bc }, { type: :immediate16 })
54
- when 0x02 then ld8({ type: :indirect, value: :bc }, { type: :register8, value: :a })
55
- when 0x03 then inc16({ type: :register16, value: :bc })
56
- when 0x04 then inc8({ type: :register8, value: :b })
57
- when 0x05 then dec8({ type: :register8, value: :b })
58
- when 0x06 then ld8({ type: :register8, value: :b }, { type: :immediate8 })
59
- when 0x07 then rlca
60
- when 0x08 then ld16({ type: :direct16 }, { type: :sp })
61
- when 0x09 then add16({ type: :register16, value: :hl }, { type: :register16, value: :bc })
62
- when 0x0a then ld8({ type: :register8, value: :a }, { type: :indirect, value: :bc })
63
- when 0x0b then dec16({ type: :register16, value: :bc })
64
- when 0x0c then inc8({ type: :register8, value: :c })
65
- when 0x0d then dec8({ type: :register8, value: :c })
66
- when 0x0e then ld8({ type: :register8, value: :c }, { type: :immediate8 })
67
- when 0x0f then rrca
53
+ when 0x01 then ld16(:bc, :immediate16, cycles: 12)
54
+ when 0x02 then ld8(:indirect_bc, :a, cycles: 8)
55
+ when 0x03 then inc16(:bc, cycles: 8)
56
+ when 0x04 then inc8(:b, cycles: 4)
57
+ when 0x05 then dec8(:b, cycles: 4)
58
+ when 0x06 then ld8(:b, :immediate8, cycles: 8)
59
+ when 0x07 then rlca(cycles: 4)
60
+ when 0x08 then ld16(:direct16, :sp, cycles: 20)
61
+ when 0x09 then add16(:hl, :bc, cycles: 8)
62
+ when 0x0a then ld8(:a, :indirect_bc, cycles: 8)
63
+ when 0x0b then dec16(:bc, cycles: 8)
64
+ when 0x0c then inc8(:c, cycles: 4)
65
+ when 0x0d then dec8(:c, cycles: 4)
66
+ when 0x0e then ld8(:c, :immediate8, cycles: 8)
67
+ when 0x0f then rrca(cycles: 4)
68
68
  when 0x10 then 4 # STOP
69
- when 0x11 then ld16({ type: :register16, value: :de }, { type: :immediate16 })
70
- when 0x12 then ld8({ type: :indirect, value: :de }, { type: :register8, value: :a })
71
- when 0x13 then inc16({ type: :register16, value: :de })
72
- when 0x14 then inc8({ type: :register8, value: :d })
73
- when 0x15 then dec8({ type: :register8, value: :d })
74
- when 0x16 then ld8({ type: :register8, value: :d }, { type: :immediate8 })
75
- when 0x17 then rla
76
- when 0x18 then jr
77
- when 0x19 then add16({ type: :register16, value: :hl }, { type: :register16, value: :de })
78
- when 0x1a then ld8({ type: :register8, value: :a }, { type: :indirect, value: :de })
79
- when 0x1b then dec16({ type: :register16, value: :de })
80
- when 0x1c then inc8({ type: :register8, value: :e })
81
- when 0x1d then dec8({ type: :register8, value: :e })
82
- when 0x1e then ld8({ type: :register8, value: :e }, { type: :immediate8 })
83
- when 0x1f then rra
84
- when 0x20 then jr(condition: !flags[:z])
85
- when 0x21 then ld16({ type: :register16, value: :hl }, { type: :immediate16 })
86
- when 0x22 then ld8({ type: :hl_inc }, { type: :register8, value: :a })
87
- when 0x23 then inc16({ type: :register16, value: :hl })
88
- when 0x24 then inc8({ type: :register8, value: :h })
89
- when 0x25 then dec8({ type: :register8, value: :h })
90
- when 0x26 then ld8({ type: :register8, value: :h }, { type: :immediate8 })
91
- when 0x27 then daa
92
- when 0x28 then jr(condition: flags[:z])
93
- when 0x29 then add16({ type: :register16, value: :hl }, { type: :register16, value: :hl })
94
- when 0x2a then ld8({ type: :register8, value: :a }, { type: :hl_inc })
95
- when 0x2b then dec16({ type: :register16, value: :hl })
96
- when 0x2c then inc8({ type: :register8, value: :l })
97
- when 0x2d then dec8({ type: :register8, value: :l })
98
- when 0x2e then ld8({ type: :register8, value: :l }, { type: :immediate8 })
99
- when 0x2f then cpl
100
- when 0x30 then jr(condition: !flags[:c])
101
- when 0x31 then ld16({ type: :sp }, { type: :immediate16 })
102
- when 0x32 then ld8({ type: :hl_dec }, { type: :register8, value: :a })
103
- when 0x33 then inc16({ type: :sp })
104
- when 0x34 then inc8({ type: :indirect, value: :hl })
105
- when 0x35 then dec8({ type: :indirect, value: :hl })
106
- when 0x36 then ld8({ type: :indirect, value: :hl }, { type: :immediate8 })
107
- when 0x37 then scf
108
- when 0x38 then jr(condition: flags[:c])
109
- when 0x39 then add16({ type: :register16, value: :hl }, { type: :sp })
110
- when 0x3a then ld8({ type: :register8, value: :a }, { type: :hl_dec })
111
- when 0x3b then dec16({ type: :sp })
112
- when 0x3c then inc8({ type: :register8, value: :a })
113
- when 0x3d then dec8({ type: :register8, value: :a })
114
- when 0x3e then ld8({ type: :register8, value: :a }, { type: :immediate8 })
115
- when 0x3f then ccf
116
- when 0x40 then ld8({ type: :register8, value: :b }, { type: :register8, value: :b })
117
- when 0x41 then ld8({ type: :register8, value: :b }, { type: :register8, value: :c })
118
- when 0x42 then ld8({ type: :register8, value: :b }, { type: :register8, value: :d })
119
- when 0x43 then ld8({ type: :register8, value: :b }, { type: :register8, value: :e })
120
- when 0x44 then ld8({ type: :register8, value: :b }, { type: :register8, value: :h })
121
- when 0x45 then ld8({ type: :register8, value: :b }, { type: :register8, value: :l })
122
- when 0x46 then ld8({ type: :register8, value: :b }, { type: :indirect, value: :hl })
123
- when 0x47 then ld8({ type: :register8, value: :b }, { type: :register8, value: :a })
124
- when 0x48 then ld8({ type: :register8, value: :c }, { type: :register8, value: :b })
125
- when 0x49 then ld8({ type: :register8, value: :c }, { type: :register8, value: :c })
126
- when 0x4a then ld8({ type: :register8, value: :c }, { type: :register8, value: :d })
127
- when 0x4b then ld8({ type: :register8, value: :c }, { type: :register8, value: :e })
128
- when 0x4c then ld8({ type: :register8, value: :c }, { type: :register8, value: :h })
129
- when 0x4d then ld8({ type: :register8, value: :c }, { type: :register8, value: :l })
130
- when 0x4e then ld8({ type: :register8, value: :c }, { type: :indirect, value: :hl })
131
- when 0x4f then ld8({ type: :register8, value: :c }, { type: :register8, value: :a })
132
- when 0x50 then ld8({ type: :register8, value: :d }, { type: :register8, value: :b })
133
- when 0x51 then ld8({ type: :register8, value: :d }, { type: :register8, value: :c })
134
- when 0x52 then ld8({ type: :register8, value: :d }, { type: :register8, value: :d })
135
- when 0x53 then ld8({ type: :register8, value: :d }, { type: :register8, value: :e })
136
- when 0x54 then ld8({ type: :register8, value: :d }, { type: :register8, value: :h })
137
- when 0x55 then ld8({ type: :register8, value: :d }, { type: :register8, value: :l })
138
- when 0x56 then ld8({ type: :register8, value: :d }, { type: :indirect, value: :hl })
139
- when 0x57 then ld8({ type: :register8, value: :d }, { type: :register8, value: :a })
140
- when 0x58 then ld8({ type: :register8, value: :e }, { type: :register8, value: :b })
141
- when 0x59 then ld8({ type: :register8, value: :e }, { type: :register8, value: :c })
142
- when 0x5a then ld8({ type: :register8, value: :e }, { type: :register8, value: :d })
143
- when 0x5b then ld8({ type: :register8, value: :e }, { type: :register8, value: :e })
144
- when 0x5c then ld8({ type: :register8, value: :e }, { type: :register8, value: :h })
145
- when 0x5d then ld8({ type: :register8, value: :e }, { type: :register8, value: :l })
146
- when 0x5e then ld8({ type: :register8, value: :e }, { type: :indirect, value: :hl })
147
- when 0x5f then ld8({ type: :register8, value: :e }, { type: :register8, value: :a })
148
- when 0x60 then ld8({ type: :register8, value: :h }, { type: :register8, value: :b })
149
- when 0x61 then ld8({ type: :register8, value: :h }, { type: :register8, value: :c })
150
- when 0x62 then ld8({ type: :register8, value: :h }, { type: :register8, value: :d })
151
- when 0x63 then ld8({ type: :register8, value: :h }, { type: :register8, value: :e })
152
- when 0x64 then ld8({ type: :register8, value: :h }, { type: :register8, value: :h })
153
- when 0x65 then ld8({ type: :register8, value: :h }, { type: :register8, value: :l })
154
- when 0x66 then ld8({ type: :register8, value: :h }, { type: :indirect, value: :hl })
155
- when 0x67 then ld8({ type: :register8, value: :h }, { type: :register8, value: :a })
156
- when 0x68 then ld8({ type: :register8, value: :l }, { type: :register8, value: :b })
157
- when 0x69 then ld8({ type: :register8, value: :l }, { type: :register8, value: :c })
158
- when 0x6a then ld8({ type: :register8, value: :l }, { type: :register8, value: :d })
159
- when 0x6b then ld8({ type: :register8, value: :l }, { type: :register8, value: :e })
160
- when 0x6c then ld8({ type: :register8, value: :l }, { type: :register8, value: :h })
161
- when 0x6d then ld8({ type: :register8, value: :l }, { type: :register8, value: :l })
162
- when 0x6e then ld8({ type: :register8, value: :l }, { type: :indirect, value: :hl })
163
- when 0x6f then ld8({ type: :register8, value: :l }, { type: :register8, value: :a })
164
- when 0x70 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :b })
165
- when 0x71 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :c })
166
- when 0x72 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :d })
167
- when 0x73 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :e })
168
- when 0x74 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :h })
169
- when 0x75 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :l })
170
- when 0x76 then halt
171
- when 0x77 then ld8({ type: :indirect, value: :hl }, { type: :register8, value: :a })
172
- when 0x78 then ld8({ type: :register8, value: :a }, { type: :register8, value: :b })
173
- when 0x79 then ld8({ type: :register8, value: :a }, { type: :register8, value: :c })
174
- when 0x7a then ld8({ type: :register8, value: :a }, { type: :register8, value: :d })
175
- when 0x7b then ld8({ type: :register8, value: :a }, { type: :register8, value: :e })
176
- when 0x7c then ld8({ type: :register8, value: :a }, { type: :register8, value: :h })
177
- when 0x7d then ld8({ type: :register8, value: :a }, { type: :register8, value: :l })
178
- when 0x7e then ld8({ type: :register8, value: :a }, { type: :indirect, value: :hl })
179
- when 0x7f then ld8({ type: :register8, value: :a }, { type: :register8, value: :a })
180
- when 0x80 then add8({ type: :register8, value: :b })
181
- when 0x81 then add8({ type: :register8, value: :c })
182
- when 0x82 then add8({ type: :register8, value: :d })
183
- when 0x83 then add8({ type: :register8, value: :e })
184
- when 0x84 then add8({ type: :register8, value: :h })
185
- when 0x85 then add8({ type: :register8, value: :l })
186
- when 0x86 then add8({ type: :indirect, value: :hl })
187
- when 0x87 then add8({ type: :register8, value: :a })
188
- when 0x88 then adc8({ type: :register8, value: :b })
189
- when 0x89 then adc8({ type: :register8, value: :c })
190
- when 0x8a then adc8({ type: :register8, value: :d })
191
- when 0x8b then adc8({ type: :register8, value: :e })
192
- when 0x8c then adc8({ type: :register8, value: :h })
193
- when 0x8d then adc8({ type: :register8, value: :l })
194
- when 0x8e then adc8({ type: :indirect, value: :hl })
195
- when 0x8f then adc8({ type: :register8, value: :a })
196
- when 0x90 then sub8({ type: :register8, value: :b })
197
- when 0x91 then sub8({ type: :register8, value: :c })
198
- when 0x92 then sub8({ type: :register8, value: :d })
199
- when 0x93 then sub8({ type: :register8, value: :e })
200
- when 0x94 then sub8({ type: :register8, value: :h })
201
- when 0x95 then sub8({ type: :register8, value: :l })
202
- when 0x96 then sub8({ type: :indirect, value: :hl })
203
- when 0x97 then sub8({ type: :register8, value: :a })
204
- when 0x98 then sbc8({ type: :register8, value: :b })
205
- when 0x99 then sbc8({ type: :register8, value: :c })
206
- when 0x9a then sbc8({ type: :register8, value: :d })
207
- when 0x9b then sbc8({ type: :register8, value: :e })
208
- when 0x9c then sbc8({ type: :register8, value: :h })
209
- when 0x9d then sbc8({ type: :register8, value: :l })
210
- when 0x9e then sbc8({ type: :indirect, value: :hl })
211
- when 0x9f then sbc8({ type: :register8, value: :a })
212
- when 0xa0 then and8({ type: :register8, value: :b })
213
- when 0xa1 then and8({ type: :register8, value: :c })
214
- when 0xa2 then and8({ type: :register8, value: :d })
215
- when 0xa3 then and8({ type: :register8, value: :e })
216
- when 0xa4 then and8({ type: :register8, value: :h })
217
- when 0xa5 then and8({ type: :register8, value: :l })
218
- when 0xa6 then and8({ type: :indirect, value: :hl })
219
- when 0xa7 then and8({ type: :register8, value: :a })
220
- when 0xa8 then xor8({ type: :register8, value: :b })
221
- when 0xa9 then xor8({ type: :register8, value: :c })
222
- when 0xaa then xor8({ type: :register8, value: :d })
223
- when 0xab then xor8({ type: :register8, value: :e })
224
- when 0xac then xor8({ type: :register8, value: :h })
225
- when 0xad then xor8({ type: :register8, value: :l })
226
- when 0xae then xor8({ type: :indirect, value: :hl })
227
- when 0xaf then xor8({ type: :register8, value: :a })
228
- when 0xb0 then or8({ type: :register8, value: :b })
229
- when 0xb1 then or8({ type: :register8, value: :c })
230
- when 0xb2 then or8({ type: :register8, value: :d })
231
- when 0xb3 then or8({ type: :register8, value: :e })
232
- when 0xb4 then or8({ type: :register8, value: :h })
233
- when 0xb5 then or8({ type: :register8, value: :l })
234
- when 0xb6 then or8({ type: :indirect, value: :hl })
235
- when 0xb7 then or8({ type: :register8, value: :a })
236
- when 0xb8 then cp8({ type: :register8, value: :b })
237
- when 0xb9 then cp8({ type: :register8, value: :c })
238
- when 0xba then cp8({ type: :register8, value: :d })
239
- when 0xbb then cp8({ type: :register8, value: :e })
240
- when 0xbc then cp8({ type: :register8, value: :h })
241
- when 0xbd then cp8({ type: :register8, value: :l })
242
- when 0xbe then cp8({ type: :indirect, value: :hl })
243
- when 0xbf then cp8({ type: :register8, value: :a })
244
- when 0xc0 then ret_if(!flags[:z])
245
- when 0xc1 then pop16(:bc)
246
- when 0xc2 then jp({ type: :immediate16 }, condition: !flags[:z])
247
- when 0xc3 then jp({ type: :immediate16 })
248
- when 0xc4 then call16({ type: :immediate16 }, condition: !flags[:z])
249
- when 0xc5 then push16(:bc)
250
- when 0xc6 then add8({ type: :immediate8 })
251
- when 0xc7 then rst(0x00)
252
- when 0xc8 then ret_if(flags[:z])
253
- when 0xc9 then ret
254
- when 0xca then jp({ type: :immediate16 }, condition: flags[:z])
255
- when 0xcc then call16({ type: :immediate16 }, condition: flags[:z])
256
- when 0xcd then call16({ type: :immediate16 })
257
- when 0xce then adc8({ type: :immediate8 })
258
- when 0xcf then rst(0x08)
259
- when 0xd0 then ret_if(!flags[:c])
260
- when 0xd1 then pop16(:de)
261
- when 0xd2 then jp({ type: :immediate16 }, condition: !flags[:c])
262
- when 0xd4 then call16({ type: :immediate16 }, condition: !flags[:c])
263
- when 0xd5 then push16(:de)
264
- when 0xd6 then sub8({ type: :immediate8 })
265
- when 0xd7 then rst(0x10)
266
- when 0xd8 then ret_if(flags[:c])
267
- when 0xd9 then reti
268
- when 0xda then jp({ type: :immediate16 }, condition: flags[:c])
269
- when 0xdc then call16({ type: :immediate16 }, condition: flags[:c])
270
- when 0xde then sbc8({ type: :immediate8 })
271
- when 0xdf then rst(0x18)
272
- when 0xe0 then ld8({ type: :ff00 }, { type: :register8, value: :a })
273
- when 0xe1 then pop16(:hl)
274
- when 0xe2 then ld8({ type: :ff00_c }, { type: :register8, value: :a })
275
- when 0xe5 then push16(:hl)
276
- when 0xe6 then and8({ type: :immediate8 })
277
- when 0xe7 then rst(0x20)
278
- when 0xe8 then add_sp_r8
279
- when 0xe9 then jp({ type: :register16, value: :hl })
280
- when 0xea then ld8({ type: :direct8 }, { type: :register8, value: :a })
281
- when 0xee then xor8({ type: :immediate8 })
282
- when 0xef then rst(0x28)
283
- when 0xf0 then ld8({ type: :register8, value: :a }, { type: :ff00 })
284
- when 0xf1 then pop16(:af)
285
- when 0xf2 then ld8({ type: :register8, value: :a }, { type: :ff00_c })
286
- when 0xf3 then di
287
- when 0xf5 then push16(:af)
288
- when 0xf6 then or8({ type: :immediate8 })
289
- when 0xf7 then rst(0x30)
290
- when 0xf8 then ld_hl_sp_r8
291
- when 0xf9 then ld16({ type: :sp }, { type: :register16, value: :hl })
292
- when 0xfa then ld8({ type: :register8, value: :a }, { type: :direct8 })
293
- when 0xfb then ei
294
- when 0xfe then cp8({ type: :immediate8 })
295
- when 0xff then rst(0x38)
69
+ when 0x11 then ld16(:de, :immediate16, cycles: 12)
70
+ when 0x12 then ld8(:indirect_de, :a, cycles: 8)
71
+ when 0x13 then inc16(:de, cycles: 8)
72
+ when 0x14 then inc8(:d, cycles: 4)
73
+ when 0x15 then dec8(:d, cycles: 4)
74
+ when 0x16 then ld8(:d, :immediate8, cycles: 8)
75
+ when 0x17 then rla(cycles: 4)
76
+ when 0x18 then jr(condition: true)
77
+ when 0x19 then add16(:hl, :de, cycles: 8)
78
+ when 0x1a then ld8(:a, :indirect_de, cycles: 8)
79
+ when 0x1b then dec16(:de, cycles: 8)
80
+ when 0x1c then inc8(:e, cycles: 4)
81
+ when 0x1d then dec8(:e, cycles: 4)
82
+ when 0x1e then ld8(:e, :immediate8, cycles: 8)
83
+ when 0x1f then rra(cycles: 4)
84
+ when 0x20 then jr(condition: !flag_z)
85
+ when 0x21 then ld16(:hl, :immediate16, cycles: 12)
86
+ when 0x22 then ld8(:hl_inc, :a, cycles: 8)
87
+ when 0x23 then inc16(:hl, cycles: 8)
88
+ when 0x24 then inc8(:h, cycles: 4)
89
+ when 0x25 then dec8(:h, cycles: 4)
90
+ when 0x26 then ld8(:h, :immediate8, cycles: 8)
91
+ when 0x27 then daa(cycles: 4)
92
+ when 0x28 then jr(condition: flag_z)
93
+ when 0x29 then add16(:hl, :hl, cycles: 8)
94
+ when 0x2a then ld8(:a, :hl_inc, cycles: 8)
95
+ when 0x2b then dec16(:hl, cycles: 8)
96
+ when 0x2c then inc8(:l, cycles: 4)
97
+ when 0x2d then dec8(:l, cycles: 4)
98
+ when 0x2e then ld8(:l, :immediate8, cycles: 8)
99
+ when 0x2f then cpl(cycles: 4)
100
+ when 0x30 then jr(condition: !flag_c)
101
+ when 0x31 then ld16(:sp, :immediate16, cycles: 12)
102
+ when 0x32 then ld8(:hl_dec, :a, cycles: 8)
103
+ when 0x33 then inc16(:sp, cycles: 8)
104
+ when 0x34 then inc8(:indirect_hl, cycles: 12)
105
+ when 0x35 then dec8(:indirect_hl, cycles: 12)
106
+ when 0x36 then ld8(:indirect_hl, :immediate8, cycles: 12)
107
+ when 0x37 then scf(cycles: 4)
108
+ when 0x38 then jr(condition: flag_c)
109
+ when 0x39 then add16(:hl, :sp, cycles: 8)
110
+ when 0x3a then ld8(:a, :hl_dec, cycles: 8)
111
+ when 0x3b then dec16(:sp, cycles: 8)
112
+ when 0x3c then inc8(:a, cycles: 4)
113
+ when 0x3d then dec8(:a, cycles: 4)
114
+ when 0x3e then ld8(:a, :immediate8, cycles: 8)
115
+ when 0x3f then ccf(cycles: 4)
116
+ when 0x40 then ld8(:b, :b, cycles: 4)
117
+ when 0x41 then ld8(:b, :c, cycles: 4)
118
+ when 0x42 then ld8(:b, :d, cycles: 4)
119
+ when 0x43 then ld8(:b, :e, cycles: 4)
120
+ when 0x44 then ld8(:b, :h, cycles: 4)
121
+ when 0x45 then ld8(:b, :l, cycles: 4)
122
+ when 0x46 then ld8(:b, :indirect_hl, cycles: 8)
123
+ when 0x47 then ld8(:b, :a, cycles: 4)
124
+ when 0x48 then ld8(:c, :b, cycles: 4)
125
+ when 0x49 then ld8(:c, :c, cycles: 4)
126
+ when 0x4a then ld8(:c, :d, cycles: 4)
127
+ when 0x4b then ld8(:c, :e, cycles: 4)
128
+ when 0x4c then ld8(:c, :h, cycles: 4)
129
+ when 0x4d then ld8(:c, :l, cycles: 4)
130
+ when 0x4e then ld8(:c, :indirect_hl, cycles: 8)
131
+ when 0x4f then ld8(:c, :a, cycles: 4)
132
+ when 0x50 then ld8(:d, :b, cycles: 4)
133
+ when 0x51 then ld8(:d, :c, cycles: 4)
134
+ when 0x52 then ld8(:d, :d, cycles: 4)
135
+ when 0x53 then ld8(:d, :e, cycles: 4)
136
+ when 0x54 then ld8(:d, :h, cycles: 4)
137
+ when 0x55 then ld8(:d, :l, cycles: 4)
138
+ when 0x56 then ld8(:d, :indirect_hl, cycles: 8)
139
+ when 0x57 then ld8(:d, :a, cycles: 4)
140
+ when 0x58 then ld8(:e, :b, cycles: 4)
141
+ when 0x59 then ld8(:e, :c, cycles: 4)
142
+ when 0x5a then ld8(:e, :d, cycles: 4)
143
+ when 0x5b then ld8(:e, :e, cycles: 4)
144
+ when 0x5c then ld8(:e, :h, cycles: 4)
145
+ when 0x5d then ld8(:e, :l, cycles: 4)
146
+ when 0x5e then ld8(:e, :indirect_hl, cycles: 8)
147
+ when 0x5f then ld8(:e, :a, cycles: 4)
148
+ when 0x60 then ld8(:h, :b, cycles: 4)
149
+ when 0x61 then ld8(:h, :c, cycles: 4)
150
+ when 0x62 then ld8(:h, :d, cycles: 4)
151
+ when 0x63 then ld8(:h, :e, cycles: 4)
152
+ when 0x64 then ld8(:h, :h, cycles: 4)
153
+ when 0x65 then ld8(:h, :l, cycles: 4)
154
+ when 0x66 then ld8(:h, :indirect_hl, cycles: 8)
155
+ when 0x67 then ld8(:h, :a, cycles: 4)
156
+ when 0x68 then ld8(:l, :b, cycles: 4)
157
+ when 0x69 then ld8(:l, :c, cycles: 4)
158
+ when 0x6a then ld8(:l, :d, cycles: 4)
159
+ when 0x6b then ld8(:l, :e, cycles: 4)
160
+ when 0x6c then ld8(:l, :h, cycles: 4)
161
+ when 0x6d then ld8(:l, :l, cycles: 4)
162
+ when 0x6e then ld8(:l, :indirect_hl, cycles: 8)
163
+ when 0x6f then ld8(:l, :a, cycles: 4)
164
+ when 0x70 then ld8(:indirect_hl, :b, cycles: 8)
165
+ when 0x71 then ld8(:indirect_hl, :c, cycles: 8)
166
+ when 0x72 then ld8(:indirect_hl, :d, cycles: 8)
167
+ when 0x73 then ld8(:indirect_hl, :e, cycles: 8)
168
+ when 0x74 then ld8(:indirect_hl, :h, cycles: 8)
169
+ when 0x75 then ld8(:indirect_hl, :l, cycles: 8)
170
+ when 0x76 then halt(cycles: 4)
171
+ when 0x77 then ld8(:indirect_hl, :a, cycles: 8)
172
+ when 0x78 then ld8(:a, :b, cycles: 4)
173
+ when 0x79 then ld8(:a, :c, cycles: 4)
174
+ when 0x7a then ld8(:a, :d, cycles: 4)
175
+ when 0x7b then ld8(:a, :e, cycles: 4)
176
+ when 0x7c then ld8(:a, :h, cycles: 4)
177
+ when 0x7d then ld8(:a, :l, cycles: 4)
178
+ when 0x7e then ld8(:a, :indirect_hl, cycles: 8)
179
+ when 0x7f then ld8(:a, :a, cycles: 4)
180
+ when 0x80 then add8(:b, cycles: 4)
181
+ when 0x81 then add8(:c, cycles: 4)
182
+ when 0x82 then add8(:d, cycles: 4)
183
+ when 0x83 then add8(:e, cycles: 4)
184
+ when 0x84 then add8(:h, cycles: 4)
185
+ when 0x85 then add8(:l, cycles: 4)
186
+ when 0x86 then add8(:indirect_hl, cycles: 8)
187
+ when 0x87 then add8(:a, cycles: 4)
188
+ when 0x88 then adc8(:b, cycles: 4)
189
+ when 0x89 then adc8(:c, cycles: 4)
190
+ when 0x8a then adc8(:d, cycles: 4)
191
+ when 0x8b then adc8(:e, cycles: 4)
192
+ when 0x8c then adc8(:h, cycles: 4)
193
+ when 0x8d then adc8(:l, cycles: 4)
194
+ when 0x8e then adc8(:indirect_hl, cycles: 8)
195
+ when 0x8f then adc8(:a, cycles: 4)
196
+ when 0x90 then sub8(:b, cycles: 4)
197
+ when 0x91 then sub8(:c, cycles: 4)
198
+ when 0x92 then sub8(:d, cycles: 4)
199
+ when 0x93 then sub8(:e, cycles: 4)
200
+ when 0x94 then sub8(:h, cycles: 4)
201
+ when 0x95 then sub8(:l, cycles: 4)
202
+ when 0x96 then sub8(:indirect_hl, cycles: 8)
203
+ when 0x97 then sub8(:a, cycles: 4)
204
+ when 0x98 then sbc8(:b, cycles: 4)
205
+ when 0x99 then sbc8(:c, cycles: 4)
206
+ when 0x9a then sbc8(:d, cycles: 4)
207
+ when 0x9b then sbc8(:e, cycles: 4)
208
+ when 0x9c then sbc8(:h, cycles: 4)
209
+ when 0x9d then sbc8(:l, cycles: 4)
210
+ when 0x9e then sbc8(:indirect_hl, cycles: 8)
211
+ when 0x9f then sbc8(:a, cycles: 4)
212
+ when 0xa0 then and8(:b, cycles: 4)
213
+ when 0xa1 then and8(:c, cycles: 4)
214
+ when 0xa2 then and8(:d, cycles: 4)
215
+ when 0xa3 then and8(:e, cycles: 4)
216
+ when 0xa4 then and8(:h, cycles: 4)
217
+ when 0xa5 then and8(:l, cycles: 4)
218
+ when 0xa6 then and8(:indirect_hl, cycles: 8)
219
+ when 0xa7 then and8(:a, cycles: 4)
220
+ when 0xa8 then xor8(:b, cycles: 4)
221
+ when 0xa9 then xor8(:c, cycles: 4)
222
+ when 0xaa then xor8(:d, cycles: 4)
223
+ when 0xab then xor8(:e, cycles: 4)
224
+ when 0xac then xor8(:h, cycles: 4)
225
+ when 0xad then xor8(:l, cycles: 4)
226
+ when 0xae then xor8(:indirect_hl, cycles: 8)
227
+ when 0xaf then xor8(:a, cycles: 4)
228
+ when 0xb0 then or8(:b, cycles: 4)
229
+ when 0xb1 then or8(:c, cycles: 4)
230
+ when 0xb2 then or8(:d, cycles: 4)
231
+ when 0xb3 then or8(:e, cycles: 4)
232
+ when 0xb4 then or8(:h, cycles: 4)
233
+ when 0xb5 then or8(:l, cycles: 4)
234
+ when 0xb6 then or8(:indirect_hl, cycles: 8)
235
+ when 0xb7 then or8(:a, cycles: 4)
236
+ when 0xb8 then cp8(:b, cycles: 4)
237
+ when 0xb9 then cp8(:c, cycles: 4)
238
+ when 0xba then cp8(:d, cycles: 4)
239
+ when 0xbb then cp8(:e, cycles: 4)
240
+ when 0xbc then cp8(:h, cycles: 4)
241
+ when 0xbd then cp8(:l, cycles: 4)
242
+ when 0xbe then cp8(:indirect_hl, cycles: 8)
243
+ when 0xbf then cp8(:a, cycles: 4)
244
+ when 0xc0 then ret_if(condition: !flag_z)
245
+ when 0xc1 then pop16(:bc, cycles: 12)
246
+ when 0xc2 then jp(:immediate16, condition: !flag_z)
247
+ when 0xc3 then jp(:immediate16, condition: true)
248
+ when 0xc4 then call16(:immediate16, condition: !flag_z)
249
+ when 0xc5 then push16(:bc, cycles: 16)
250
+ when 0xc6 then add8(:immediate8, cycles: 8)
251
+ when 0xc7 then rst(0x00, cycles: 16)
252
+ when 0xc8 then ret_if(condition: flag_z)
253
+ when 0xc9 then ret(cycles: 16)
254
+ when 0xca then jp(:immediate16, condition: flag_z)
255
+ when 0xcc then call16(:immediate16, condition: flag_z)
256
+ when 0xcd then call16(:immediate16, condition: true)
257
+ when 0xce then adc8(:immediate8, cycles: 8)
258
+ when 0xcf then rst(0x08, cycles: 16)
259
+ when 0xd0 then ret_if(condition: !flag_c)
260
+ when 0xd1 then pop16(:de, cycles: 12)
261
+ when 0xd2 then jp(:immediate16, condition: !flag_c)
262
+ when 0xd4 then call16(:immediate16, condition: !flag_c)
263
+ when 0xd5 then push16(:de, cycles: 16)
264
+ when 0xd6 then sub8(:immediate8, cycles: 8)
265
+ when 0xd7 then rst(0x10, cycles: 16)
266
+ when 0xd8 then ret_if(condition: flag_c)
267
+ when 0xd9 then reti(cycles: 16)
268
+ when 0xda then jp(:immediate16, condition: flag_c)
269
+ when 0xdc then call16(:immediate16, condition: flag_c)
270
+ when 0xde then sbc8(:immediate8, cycles: 8)
271
+ when 0xdf then rst(0x18, cycles: 16)
272
+ when 0xe0 then ld8(:ff00, :a, cycles: 12)
273
+ when 0xe1 then pop16(:hl, cycles: 12)
274
+ when 0xe2 then ld8(:ff00_c, :a, cycles: 8)
275
+ when 0xe5 then push16(:hl, cycles: 16)
276
+ when 0xe6 then and8(:immediate8, cycles: 8)
277
+ when 0xe7 then rst(0x20, cycles: 16)
278
+ when 0xe8 then add_sp_r8(cycles: 16)
279
+ when 0xe9 then jp_hl(cycles: 4)
280
+ when 0xea then ld8(:direct8, :a, cycles: 16)
281
+ when 0xee then xor8(:immediate8, cycles: 8)
282
+ when 0xef then rst(0x28, cycles: 16)
283
+ when 0xf0 then ld8(:a, :ff00, cycles: 12)
284
+ when 0xf1 then pop16(:af, cycles: 12)
285
+ when 0xf2 then ld8(:a, :ff00_c, cycles: 8)
286
+ when 0xf3 then di(cycles: 4)
287
+ when 0xf5 then push16(:af, cycles: 16)
288
+ when 0xf6 then or8(:immediate8, cycles: 8)
289
+ when 0xf7 then rst(0x30, cycles: 16)
290
+ when 0xf8 then ld_hl_sp_r8(cycles: 12)
291
+ when 0xf9 then ld16(:sp, :hl, cycles: 8)
292
+ when 0xfa then ld8(:a, :direct8, cycles: 16)
293
+ when 0xfb then ei(cycles: 4)
294
+ when 0xfe then cp8(:immediate8, cycles: 8)
295
+ when 0xff then rst(0x38, cycles: 16)
296
296
  when 0xcb # CB prefix
297
297
  opcode = read_byte_and_advance_pc
298
298
 
299
299
  case opcode
300
- when 0x00 then rlc8({ type: :register8, value: :b })
301
- when 0x01 then rlc8({ type: :register8, value: :c })
302
- when 0x02 then rlc8({ type: :register8, value: :d })
303
- when 0x03 then rlc8({ type: :register8, value: :e })
304
- when 0x04 then rlc8({ type: :register8, value: :h })
305
- when 0x05 then rlc8({ type: :register8, value: :l })
306
- when 0x06 then rlc8({ type: :indirect, value: :hl })
307
- when 0x07 then rlc8({ type: :register8, value: :a })
308
- when 0x08 then rrc8({ type: :register8, value: :b })
309
- when 0x09 then rrc8({ type: :register8, value: :c })
310
- when 0x0a then rrc8({ type: :register8, value: :d })
311
- when 0x0b then rrc8({ type: :register8, value: :e })
312
- when 0x0c then rrc8({ type: :register8, value: :h })
313
- when 0x0d then rrc8({ type: :register8, value: :l })
314
- when 0x0e then rrc8({ type: :indirect, value: :hl })
315
- when 0x0f then rrc8({ type: :register8, value: :a })
316
- when 0x10 then rl8({ type: :register8, value: :b })
317
- when 0x11 then rl8({ type: :register8, value: :c })
318
- when 0x12 then rl8({ type: :register8, value: :d })
319
- when 0x13 then rl8({ type: :register8, value: :e })
320
- when 0x14 then rl8({ type: :register8, value: :h })
321
- when 0x15 then rl8({ type: :register8, value: :l })
322
- when 0x16 then rl8({ type: :indirect, value: :hl })
323
- when 0x17 then rl8({ type: :register8, value: :a })
324
- when 0x18 then rr8({ type: :register8, value: :b })
325
- when 0x19 then rr8({ type: :register8, value: :c })
326
- when 0x1a then rr8({ type: :register8, value: :d })
327
- when 0x1b then rr8({ type: :register8, value: :e })
328
- when 0x1c then rr8({ type: :register8, value: :h })
329
- when 0x1d then rr8({ type: :register8, value: :l })
330
- when 0x1e then rr8({ type: :indirect, value: :hl })
331
- when 0x1f then rr8({ type: :register8, value: :a })
332
- when 0x20 then sla8({ type: :register8, value: :b })
333
- when 0x21 then sla8({ type: :register8, value: :c })
334
- when 0x22 then sla8({ type: :register8, value: :d })
335
- when 0x23 then sla8({ type: :register8, value: :e })
336
- when 0x24 then sla8({ type: :register8, value: :h })
337
- when 0x25 then sla8({ type: :register8, value: :l })
338
- when 0x26 then sla8({ type: :indirect, value: :hl })
339
- when 0x27 then sla8({ type: :register8, value: :a })
340
- when 0x28 then sra8({ type: :register8, value: :b })
341
- when 0x29 then sra8({ type: :register8, value: :c })
342
- when 0x2a then sra8({ type: :register8, value: :d })
343
- when 0x2b then sra8({ type: :register8, value: :e })
344
- when 0x2c then sra8({ type: :register8, value: :h })
345
- when 0x2d then sra8({ type: :register8, value: :l })
346
- when 0x2e then sra8({ type: :indirect, value: :hl })
347
- when 0x2f then sra8({ type: :register8, value: :a })
348
- when 0x30 then swap8({ type: :register8, value: :b })
349
- when 0x31 then swap8({ type: :register8, value: :c })
350
- when 0x32 then swap8({ type: :register8, value: :d })
351
- when 0x33 then swap8({ type: :register8, value: :e })
352
- when 0x34 then swap8({ type: :register8, value: :h })
353
- when 0x35 then swap8({ type: :register8, value: :l })
354
- when 0x36 then swap8({ type: :indirect, value: :hl })
355
- when 0x37 then swap8({ type: :register8, value: :a })
356
- when 0x38 then srl8({ type: :register8, value: :b })
357
- when 0x39 then srl8({ type: :register8, value: :c })
358
- when 0x3a then srl8({ type: :register8, value: :d })
359
- when 0x3b then srl8({ type: :register8, value: :e })
360
- when 0x3c then srl8({ type: :register8, value: :h })
361
- when 0x3d then srl8({ type: :register8, value: :l })
362
- when 0x3e then srl8({ type: :indirect, value: :hl })
363
- when 0x3f then srl8({ type: :register8, value: :a })
364
- when 0x40 then bit8(0, { type: :register8, value: :b })
365
- when 0x41 then bit8(0, { type: :register8, value: :c })
366
- when 0x42 then bit8(0, { type: :register8, value: :d })
367
- when 0x43 then bit8(0, { type: :register8, value: :e })
368
- when 0x44 then bit8(0, { type: :register8, value: :h })
369
- when 0x45 then bit8(0, { type: :register8, value: :l })
370
- when 0x46 then bit8(0, { type: :indirect, value: :hl })
371
- when 0x47 then bit8(0, { type: :register8, value: :a })
372
- when 0x48 then bit8(1, { type: :register8, value: :b })
373
- when 0x49 then bit8(1, { type: :register8, value: :c })
374
- when 0x4a then bit8(1, { type: :register8, value: :d })
375
- when 0x4b then bit8(1, { type: :register8, value: :e })
376
- when 0x4c then bit8(1, { type: :register8, value: :h })
377
- when 0x4d then bit8(1, { type: :register8, value: :l })
378
- when 0x4e then bit8(1, { type: :indirect, value: :hl })
379
- when 0x4f then bit8(1, { type: :register8, value: :a })
380
- when 0x50 then bit8(2, { type: :register8, value: :b })
381
- when 0x51 then bit8(2, { type: :register8, value: :c })
382
- when 0x52 then bit8(2, { type: :register8, value: :d })
383
- when 0x53 then bit8(2, { type: :register8, value: :e })
384
- when 0x54 then bit8(2, { type: :register8, value: :h })
385
- when 0x55 then bit8(2, { type: :register8, value: :l })
386
- when 0x56 then bit8(2, { type: :indirect, value: :hl })
387
- when 0x57 then bit8(2, { type: :register8, value: :a })
388
- when 0x58 then bit8(3, { type: :register8, value: :b })
389
- when 0x59 then bit8(3, { type: :register8, value: :c })
390
- when 0x5a then bit8(3, { type: :register8, value: :d })
391
- when 0x5b then bit8(3, { type: :register8, value: :e })
392
- when 0x5c then bit8(3, { type: :register8, value: :h })
393
- when 0x5d then bit8(3, { type: :register8, value: :l })
394
- when 0x5e then bit8(3, { type: :indirect, value: :hl })
395
- when 0x5f then bit8(3, { type: :register8, value: :a })
396
- when 0x60 then bit8(4, { type: :register8, value: :b })
397
- when 0x61 then bit8(4, { type: :register8, value: :c })
398
- when 0x62 then bit8(4, { type: :register8, value: :d })
399
- when 0x63 then bit8(4, { type: :register8, value: :e })
400
- when 0x64 then bit8(4, { type: :register8, value: :h })
401
- when 0x65 then bit8(4, { type: :register8, value: :l })
402
- when 0x66 then bit8(4, { type: :indirect, value: :hl })
403
- when 0x67 then bit8(4, { type: :register8, value: :a })
404
- when 0x68 then bit8(5, { type: :register8, value: :b })
405
- when 0x69 then bit8(5, { type: :register8, value: :c })
406
- when 0x6a then bit8(5, { type: :register8, value: :d })
407
- when 0x6b then bit8(5, { type: :register8, value: :e })
408
- when 0x6c then bit8(5, { type: :register8, value: :h })
409
- when 0x6d then bit8(5, { type: :register8, value: :l })
410
- when 0x6e then bit8(5, { type: :indirect, value: :hl })
411
- when 0x6f then bit8(5, { type: :register8, value: :a })
412
- when 0x70 then bit8(6, { type: :register8, value: :b })
413
- when 0x71 then bit8(6, { type: :register8, value: :c })
414
- when 0x72 then bit8(6, { type: :register8, value: :d })
415
- when 0x73 then bit8(6, { type: :register8, value: :e })
416
- when 0x74 then bit8(6, { type: :register8, value: :h })
417
- when 0x75 then bit8(6, { type: :register8, value: :l })
418
- when 0x76 then bit8(6, { type: :indirect, value: :hl })
419
- when 0x77 then bit8(6, { type: :register8, value: :a })
420
- when 0x78 then bit8(7, { type: :register8, value: :b })
421
- when 0x79 then bit8(7, { type: :register8, value: :c })
422
- when 0x7a then bit8(7, { type: :register8, value: :d })
423
- when 0x7b then bit8(7, { type: :register8, value: :e })
424
- when 0x7c then bit8(7, { type: :register8, value: :h })
425
- when 0x7d then bit8(7, { type: :register8, value: :l })
426
- when 0x7e then bit8(7, { type: :indirect, value: :hl })
427
- when 0x7f then bit8(7, { type: :register8, value: :a })
428
- when 0x80 then res8(0, { type: :register8, value: :b })
429
- when 0x81 then res8(0, { type: :register8, value: :c })
430
- when 0x82 then res8(0, { type: :register8, value: :d })
431
- when 0x83 then res8(0, { type: :register8, value: :e })
432
- when 0x84 then res8(0, { type: :register8, value: :h })
433
- when 0x85 then res8(0, { type: :register8, value: :l })
434
- when 0x86 then res8(0, { type: :indirect, value: :hl })
435
- when 0x87 then res8(0, { type: :register8, value: :a })
436
- when 0x88 then res8(1, { type: :register8, value: :b })
437
- when 0x89 then res8(1, { type: :register8, value: :c })
438
- when 0x8a then res8(1, { type: :register8, value: :d })
439
- when 0x8b then res8(1, { type: :register8, value: :e })
440
- when 0x8c then res8(1, { type: :register8, value: :h })
441
- when 0x8d then res8(1, { type: :register8, value: :l })
442
- when 0x8e then res8(1, { type: :indirect, value: :hl })
443
- when 0x8f then res8(1, { type: :register8, value: :a })
444
- when 0x90 then res8(2, { type: :register8, value: :b })
445
- when 0x91 then res8(2, { type: :register8, value: :c })
446
- when 0x92 then res8(2, { type: :register8, value: :d })
447
- when 0x93 then res8(2, { type: :register8, value: :e })
448
- when 0x94 then res8(2, { type: :register8, value: :h })
449
- when 0x95 then res8(2, { type: :register8, value: :l })
450
- when 0x96 then res8(2, { type: :indirect, value: :hl })
451
- when 0x97 then res8(2, { type: :register8, value: :a })
452
- when 0x98 then res8(3, { type: :register8, value: :b })
453
- when 0x99 then res8(3, { type: :register8, value: :c })
454
- when 0x9a then res8(3, { type: :register8, value: :d })
455
- when 0x9b then res8(3, { type: :register8, value: :e })
456
- when 0x9c then res8(3, { type: :register8, value: :h })
457
- when 0x9d then res8(3, { type: :register8, value: :l })
458
- when 0x9e then res8(3, { type: :indirect, value: :hl })
459
- when 0x9f then res8(3, { type: :register8, value: :a })
460
- when 0xa0 then res8(4, { type: :register8, value: :b })
461
- when 0xa1 then res8(4, { type: :register8, value: :c })
462
- when 0xa2 then res8(4, { type: :register8, value: :d })
463
- when 0xa3 then res8(4, { type: :register8, value: :e })
464
- when 0xa4 then res8(4, { type: :register8, value: :h })
465
- when 0xa5 then res8(4, { type: :register8, value: :l })
466
- when 0xa6 then res8(4, { type: :indirect, value: :hl })
467
- when 0xa7 then res8(4, { type: :register8, value: :a })
468
- when 0xa8 then res8(5, { type: :register8, value: :b })
469
- when 0xa9 then res8(5, { type: :register8, value: :c })
470
- when 0xaa then res8(5, { type: :register8, value: :d })
471
- when 0xab then res8(5, { type: :register8, value: :e })
472
- when 0xac then res8(5, { type: :register8, value: :h })
473
- when 0xad then res8(5, { type: :register8, value: :l })
474
- when 0xae then res8(5, { type: :indirect, value: :hl })
475
- when 0xaf then res8(5, { type: :register8, value: :a })
476
- when 0xb0 then res8(6, { type: :register8, value: :b })
477
- when 0xb1 then res8(6, { type: :register8, value: :c })
478
- when 0xb2 then res8(6, { type: :register8, value: :d })
479
- when 0xb3 then res8(6, { type: :register8, value: :e })
480
- when 0xb4 then res8(6, { type: :register8, value: :h })
481
- when 0xb5 then res8(6, { type: :register8, value: :l })
482
- when 0xb6 then res8(6, { type: :indirect, value: :hl })
483
- when 0xb7 then res8(6, { type: :register8, value: :a })
484
- when 0xb8 then res8(7, { type: :register8, value: :b })
485
- when 0xb9 then res8(7, { type: :register8, value: :c })
486
- when 0xba then res8(7, { type: :register8, value: :d })
487
- when 0xbb then res8(7, { type: :register8, value: :e })
488
- when 0xbc then res8(7, { type: :register8, value: :h })
489
- when 0xbd then res8(7, { type: :register8, value: :l })
490
- when 0xbe then res8(7, { type: :indirect, value: :hl })
491
- when 0xbf then res8(7, { type: :register8, value: :a })
492
- when 0xc0 then set8(0, { type: :register8, value: :b })
493
- when 0xc1 then set8(0, { type: :register8, value: :c })
494
- when 0xc2 then set8(0, { type: :register8, value: :d })
495
- when 0xc3 then set8(0, { type: :register8, value: :e })
496
- when 0xc4 then set8(0, { type: :register8, value: :h })
497
- when 0xc5 then set8(0, { type: :register8, value: :l })
498
- when 0xc6 then set8(0, { type: :indirect, value: :hl })
499
- when 0xc7 then set8(0, { type: :register8, value: :a })
500
- when 0xc8 then set8(1, { type: :register8, value: :b })
501
- when 0xc9 then set8(1, { type: :register8, value: :c })
502
- when 0xca then set8(1, { type: :register8, value: :d })
503
- when 0xcb then set8(1, { type: :register8, value: :e })
504
- when 0xcc then set8(1, { type: :register8, value: :h })
505
- when 0xcd then set8(1, { type: :register8, value: :l })
506
- when 0xce then set8(1, { type: :indirect, value: :hl })
507
- when 0xcf then set8(1, { type: :register8, value: :a })
508
- when 0xd0 then set8(2, { type: :register8, value: :b })
509
- when 0xd1 then set8(2, { type: :register8, value: :c })
510
- when 0xd2 then set8(2, { type: :register8, value: :d })
511
- when 0xd3 then set8(2, { type: :register8, value: :e })
512
- when 0xd4 then set8(2, { type: :register8, value: :h })
513
- when 0xd5 then set8(2, { type: :register8, value: :l })
514
- when 0xd6 then set8(2, { type: :indirect, value: :hl })
515
- when 0xd7 then set8(2, { type: :register8, value: :a })
516
- when 0xd8 then set8(3, { type: :register8, value: :b })
517
- when 0xd9 then set8(3, { type: :register8, value: :c })
518
- when 0xda then set8(3, { type: :register8, value: :d })
519
- when 0xdb then set8(3, { type: :register8, value: :e })
520
- when 0xdc then set8(3, { type: :register8, value: :h })
521
- when 0xdd then set8(3, { type: :register8, value: :l })
522
- when 0xde then set8(3, { type: :indirect, value: :hl })
523
- when 0xdf then set8(3, { type: :register8, value: :a })
524
- when 0xe0 then set8(4, { type: :register8, value: :b })
525
- when 0xe1 then set8(4, { type: :register8, value: :c })
526
- when 0xe2 then set8(4, { type: :register8, value: :d })
527
- when 0xe3 then set8(4, { type: :register8, value: :e })
528
- when 0xe4 then set8(4, { type: :register8, value: :h })
529
- when 0xe5 then set8(4, { type: :register8, value: :l })
530
- when 0xe6 then set8(4, { type: :indirect, value: :hl })
531
- when 0xe7 then set8(4, { type: :register8, value: :a })
532
- when 0xe8 then set8(5, { type: :register8, value: :b })
533
- when 0xe9 then set8(5, { type: :register8, value: :c })
534
- when 0xea then set8(5, { type: :register8, value: :d })
535
- when 0xeb then set8(5, { type: :register8, value: :e })
536
- when 0xec then set8(5, { type: :register8, value: :h })
537
- when 0xed then set8(5, { type: :register8, value: :l })
538
- when 0xee then set8(5, { type: :indirect, value: :hl })
539
- when 0xef then set8(5, { type: :register8, value: :a })
540
- when 0xf0 then set8(6, { type: :register8, value: :b })
541
- when 0xf1 then set8(6, { type: :register8, value: :c })
542
- when 0xf2 then set8(6, { type: :register8, value: :d })
543
- when 0xf3 then set8(6, { type: :register8, value: :e })
544
- when 0xf4 then set8(6, { type: :register8, value: :h })
545
- when 0xf5 then set8(6, { type: :register8, value: :l })
546
- when 0xf6 then set8(6, { type: :indirect, value: :hl })
547
- when 0xf7 then set8(6, { type: :register8, value: :a })
548
- when 0xf8 then set8(7, { type: :register8, value: :b })
549
- when 0xf9 then set8(7, { type: :register8, value: :c })
550
- when 0xfa then set8(7, { type: :register8, value: :d })
551
- when 0xfb then set8(7, { type: :register8, value: :e })
552
- when 0xfc then set8(7, { type: :register8, value: :h })
553
- when 0xfd then set8(7, { type: :register8, value: :l })
554
- when 0xfe then set8(7, { type: :indirect, value: :hl })
555
- when 0xff then set8(7, { type: :register8, value: :a })
300
+ when 0x00 then rlc8(:b, cycles: 8)
301
+ when 0x01 then rlc8(:c, cycles: 8)
302
+ when 0x02 then rlc8(:d, cycles: 8)
303
+ when 0x03 then rlc8(:e, cycles: 8)
304
+ when 0x04 then rlc8(:h, cycles: 8)
305
+ when 0x05 then rlc8(:l, cycles: 8)
306
+ when 0x06 then rlc8(:indirect_hl, cycles: 16)
307
+ when 0x07 then rlc8(:a, cycles: 8)
308
+ when 0x08 then rrc8(:b, cycles: 8)
309
+ when 0x09 then rrc8(:c, cycles: 8)
310
+ when 0x0a then rrc8(:d, cycles: 8)
311
+ when 0x0b then rrc8(:e, cycles: 8)
312
+ when 0x0c then rrc8(:h, cycles: 8)
313
+ when 0x0d then rrc8(:l, cycles: 8)
314
+ when 0x0e then rrc8(:indirect_hl, cycles: 16)
315
+ when 0x0f then rrc8(:a, cycles: 8)
316
+ when 0x10 then rl8(:b, cycles: 8)
317
+ when 0x11 then rl8(:c, cycles: 8)
318
+ when 0x12 then rl8(:d, cycles: 8)
319
+ when 0x13 then rl8(:e, cycles: 8)
320
+ when 0x14 then rl8(:h, cycles: 8)
321
+ when 0x15 then rl8(:l, cycles: 8)
322
+ when 0x16 then rl8(:indirect_hl, cycles: 16)
323
+ when 0x17 then rl8(:a, cycles: 8)
324
+ when 0x18 then rr8(:b, cycles: 8)
325
+ when 0x19 then rr8(:c, cycles: 8)
326
+ when 0x1a then rr8(:d, cycles: 8)
327
+ when 0x1b then rr8(:e, cycles: 8)
328
+ when 0x1c then rr8(:h, cycles: 8)
329
+ when 0x1d then rr8(:l, cycles: 8)
330
+ when 0x1e then rr8(:indirect_hl, cycles: 16)
331
+ when 0x1f then rr8(:a, cycles: 8)
332
+ when 0x20 then sla8(:b, cycles: 8)
333
+ when 0x21 then sla8(:c, cycles: 8)
334
+ when 0x22 then sla8(:d, cycles: 8)
335
+ when 0x23 then sla8(:e, cycles: 8)
336
+ when 0x24 then sla8(:h, cycles: 8)
337
+ when 0x25 then sla8(:l, cycles: 8)
338
+ when 0x26 then sla8(:indirect_hl, cycles: 16)
339
+ when 0x27 then sla8(:a, cycles: 8)
340
+ when 0x28 then sra8(:b, cycles: 8)
341
+ when 0x29 then sra8(:c, cycles: 8)
342
+ when 0x2a then sra8(:d, cycles: 8)
343
+ when 0x2b then sra8(:e, cycles: 8)
344
+ when 0x2c then sra8(:h, cycles: 8)
345
+ when 0x2d then sra8(:l, cycles: 8)
346
+ when 0x2e then sra8(:indirect_hl, cycles: 16)
347
+ when 0x2f then sra8(:a, cycles: 8)
348
+ when 0x30 then swap8(:b, cycles: 8)
349
+ when 0x31 then swap8(:c, cycles: 8)
350
+ when 0x32 then swap8(:d, cycles: 8)
351
+ when 0x33 then swap8(:e, cycles: 8)
352
+ when 0x34 then swap8(:h, cycles: 8)
353
+ when 0x35 then swap8(:l, cycles: 8)
354
+ when 0x36 then swap8(:indirect_hl, cycles: 16)
355
+ when 0x37 then swap8(:a, cycles: 8)
356
+ when 0x38 then srl8(:b, cycles: 8)
357
+ when 0x39 then srl8(:c, cycles: 8)
358
+ when 0x3a then srl8(:d, cycles: 8)
359
+ when 0x3b then srl8(:e, cycles: 8)
360
+ when 0x3c then srl8(:h, cycles: 8)
361
+ when 0x3d then srl8(:l, cycles: 8)
362
+ when 0x3e then srl8(:indirect_hl, cycles: 16)
363
+ when 0x3f then srl8(:a, cycles: 8)
364
+ when 0x40 then bit8(0, :b, cycles: 8)
365
+ when 0x41 then bit8(0, :c, cycles: 8)
366
+ when 0x42 then bit8(0, :d, cycles: 8)
367
+ when 0x43 then bit8(0, :e, cycles: 8)
368
+ when 0x44 then bit8(0, :h, cycles: 8)
369
+ when 0x45 then bit8(0, :l, cycles: 8)
370
+ when 0x46 then bit8(0, :indirect_hl, cycles: 12)
371
+ when 0x47 then bit8(0, :a, cycles: 8)
372
+ when 0x48 then bit8(1, :b, cycles: 8)
373
+ when 0x49 then bit8(1, :c, cycles: 8)
374
+ when 0x4a then bit8(1, :d, cycles: 8)
375
+ when 0x4b then bit8(1, :e, cycles: 8)
376
+ when 0x4c then bit8(1, :h, cycles: 8)
377
+ when 0x4d then bit8(1, :l, cycles: 8)
378
+ when 0x4e then bit8(1, :indirect_hl, cycles: 12)
379
+ when 0x4f then bit8(1, :a, cycles: 8)
380
+ when 0x50 then bit8(2, :b, cycles: 8)
381
+ when 0x51 then bit8(2, :c, cycles: 8)
382
+ when 0x52 then bit8(2, :d, cycles: 8)
383
+ when 0x53 then bit8(2, :e, cycles: 8)
384
+ when 0x54 then bit8(2, :h, cycles: 8)
385
+ when 0x55 then bit8(2, :l, cycles: 8)
386
+ when 0x56 then bit8(2, :indirect_hl, cycles: 12)
387
+ when 0x57 then bit8(2, :a, cycles: 8)
388
+ when 0x58 then bit8(3, :b, cycles: 8)
389
+ when 0x59 then bit8(3, :c, cycles: 8)
390
+ when 0x5a then bit8(3, :d, cycles: 8)
391
+ when 0x5b then bit8(3, :e, cycles: 8)
392
+ when 0x5c then bit8(3, :h, cycles: 8)
393
+ when 0x5d then bit8(3, :l, cycles: 8)
394
+ when 0x5e then bit8(3, :indirect_hl, cycles: 12)
395
+ when 0x5f then bit8(3, :a, cycles: 8)
396
+ when 0x60 then bit8(4, :b, cycles: 8)
397
+ when 0x61 then bit8(4, :c, cycles: 8)
398
+ when 0x62 then bit8(4, :d, cycles: 8)
399
+ when 0x63 then bit8(4, :e, cycles: 8)
400
+ when 0x64 then bit8(4, :h, cycles: 8)
401
+ when 0x65 then bit8(4, :l, cycles: 8)
402
+ when 0x66 then bit8(4, :indirect_hl, cycles: 12)
403
+ when 0x67 then bit8(4, :a, cycles: 8)
404
+ when 0x68 then bit8(5, :b, cycles: 8)
405
+ when 0x69 then bit8(5, :c, cycles: 8)
406
+ when 0x6a then bit8(5, :d, cycles: 8)
407
+ when 0x6b then bit8(5, :e, cycles: 8)
408
+ when 0x6c then bit8(5, :h, cycles: 8)
409
+ when 0x6d then bit8(5, :l, cycles: 8)
410
+ when 0x6e then bit8(5, :indirect_hl, cycles: 12)
411
+ when 0x6f then bit8(5, :a, cycles: 8)
412
+ when 0x70 then bit8(6, :b, cycles: 8)
413
+ when 0x71 then bit8(6, :c, cycles: 8)
414
+ when 0x72 then bit8(6, :d, cycles: 8)
415
+ when 0x73 then bit8(6, :e, cycles: 8)
416
+ when 0x74 then bit8(6, :h, cycles: 8)
417
+ when 0x75 then bit8(6, :l, cycles: 8)
418
+ when 0x76 then bit8(6, :indirect_hl, cycles: 12)
419
+ when 0x77 then bit8(6, :a, cycles: 8)
420
+ when 0x78 then bit8(7, :b, cycles: 8)
421
+ when 0x79 then bit8(7, :c, cycles: 8)
422
+ when 0x7a then bit8(7, :d, cycles: 8)
423
+ when 0x7b then bit8(7, :e, cycles: 8)
424
+ when 0x7c then bit8(7, :h, cycles: 8)
425
+ when 0x7d then bit8(7, :l, cycles: 8)
426
+ when 0x7e then bit8(7, :indirect_hl, cycles: 12)
427
+ when 0x7f then bit8(7, :a, cycles: 8)
428
+ when 0x80 then res8(0, :b, cycles: 8)
429
+ when 0x81 then res8(0, :c, cycles: 8)
430
+ when 0x82 then res8(0, :d, cycles: 8)
431
+ when 0x83 then res8(0, :e, cycles: 8)
432
+ when 0x84 then res8(0, :h, cycles: 8)
433
+ when 0x85 then res8(0, :l, cycles: 8)
434
+ when 0x86 then res8(0, :indirect_hl, cycles: 16)
435
+ when 0x87 then res8(0, :a, cycles: 8)
436
+ when 0x88 then res8(1, :b, cycles: 8)
437
+ when 0x89 then res8(1, :c, cycles: 8)
438
+ when 0x8a then res8(1, :d, cycles: 8)
439
+ when 0x8b then res8(1, :e, cycles: 8)
440
+ when 0x8c then res8(1, :h, cycles: 8)
441
+ when 0x8d then res8(1, :l, cycles: 8)
442
+ when 0x8e then res8(1, :indirect_hl, cycles: 16)
443
+ when 0x8f then res8(1, :a, cycles: 8)
444
+ when 0x90 then res8(2, :b, cycles: 8)
445
+ when 0x91 then res8(2, :c, cycles: 8)
446
+ when 0x92 then res8(2, :d, cycles: 8)
447
+ when 0x93 then res8(2, :e, cycles: 8)
448
+ when 0x94 then res8(2, :h, cycles: 8)
449
+ when 0x95 then res8(2, :l, cycles: 8)
450
+ when 0x96 then res8(2, :indirect_hl, cycles: 16)
451
+ when 0x97 then res8(2, :a, cycles: 8)
452
+ when 0x98 then res8(3, :b, cycles: 8)
453
+ when 0x99 then res8(3, :c, cycles: 8)
454
+ when 0x9a then res8(3, :d, cycles: 8)
455
+ when 0x9b then res8(3, :e, cycles: 8)
456
+ when 0x9c then res8(3, :h, cycles: 8)
457
+ when 0x9d then res8(3, :l, cycles: 8)
458
+ when 0x9e then res8(3, :indirect_hl, cycles: 16)
459
+ when 0x9f then res8(3, :a, cycles: 8)
460
+ when 0xa0 then res8(4, :b, cycles: 8)
461
+ when 0xa1 then res8(4, :c, cycles: 8)
462
+ when 0xa2 then res8(4, :d, cycles: 8)
463
+ when 0xa3 then res8(4, :e, cycles: 8)
464
+ when 0xa4 then res8(4, :h, cycles: 8)
465
+ when 0xa5 then res8(4, :l, cycles: 8)
466
+ when 0xa6 then res8(4, :indirect_hl, cycles: 16)
467
+ when 0xa7 then res8(4, :a, cycles: 8)
468
+ when 0xa8 then res8(5, :b, cycles: 8)
469
+ when 0xa9 then res8(5, :c, cycles: 8)
470
+ when 0xaa then res8(5, :d, cycles: 8)
471
+ when 0xab then res8(5, :e, cycles: 8)
472
+ when 0xac then res8(5, :h, cycles: 8)
473
+ when 0xad then res8(5, :l, cycles: 8)
474
+ when 0xae then res8(5, :indirect_hl, cycles: 16)
475
+ when 0xaf then res8(5, :a, cycles: 8)
476
+ when 0xb0 then res8(6, :b, cycles: 8)
477
+ when 0xb1 then res8(6, :c, cycles: 8)
478
+ when 0xb2 then res8(6, :d, cycles: 8)
479
+ when 0xb3 then res8(6, :e, cycles: 8)
480
+ when 0xb4 then res8(6, :h, cycles: 8)
481
+ when 0xb5 then res8(6, :l, cycles: 8)
482
+ when 0xb6 then res8(6, :indirect_hl, cycles: 16)
483
+ when 0xb7 then res8(6, :a, cycles: 8)
484
+ when 0xb8 then res8(7, :b, cycles: 8)
485
+ when 0xb9 then res8(7, :c, cycles: 8)
486
+ when 0xba then res8(7, :d, cycles: 8)
487
+ when 0xbb then res8(7, :e, cycles: 8)
488
+ when 0xbc then res8(7, :h, cycles: 8)
489
+ when 0xbd then res8(7, :l, cycles: 8)
490
+ when 0xbe then res8(7, :indirect_hl, cycles: 16)
491
+ when 0xbf then res8(7, :a, cycles: 8)
492
+ when 0xc0 then set8(0, :b, cycles: 8)
493
+ when 0xc1 then set8(0, :c, cycles: 8)
494
+ when 0xc2 then set8(0, :d, cycles: 8)
495
+ when 0xc3 then set8(0, :e, cycles: 8)
496
+ when 0xc4 then set8(0, :h, cycles: 8)
497
+ when 0xc5 then set8(0, :l, cycles: 8)
498
+ when 0xc6 then set8(0, :indirect_hl, cycles: 16)
499
+ when 0xc7 then set8(0, :a, cycles: 8)
500
+ when 0xc8 then set8(1, :b, cycles: 8)
501
+ when 0xc9 then set8(1, :c, cycles: 8)
502
+ when 0xca then set8(1, :d, cycles: 8)
503
+ when 0xcb then set8(1, :e, cycles: 8)
504
+ when 0xcc then set8(1, :h, cycles: 8)
505
+ when 0xcd then set8(1, :l, cycles: 8)
506
+ when 0xce then set8(1, :indirect_hl, cycles: 16)
507
+ when 0xcf then set8(1, :a, cycles: 8)
508
+ when 0xd0 then set8(2, :b, cycles: 8)
509
+ when 0xd1 then set8(2, :c, cycles: 8)
510
+ when 0xd2 then set8(2, :d, cycles: 8)
511
+ when 0xd3 then set8(2, :e, cycles: 8)
512
+ when 0xd4 then set8(2, :h, cycles: 8)
513
+ when 0xd5 then set8(2, :l, cycles: 8)
514
+ when 0xd6 then set8(2, :indirect_hl, cycles: 16)
515
+ when 0xd7 then set8(2, :a, cycles: 8)
516
+ when 0xd8 then set8(3, :b, cycles: 8)
517
+ when 0xd9 then set8(3, :c, cycles: 8)
518
+ when 0xda then set8(3, :d, cycles: 8)
519
+ when 0xdb then set8(3, :e, cycles: 8)
520
+ when 0xdc then set8(3, :h, cycles: 8)
521
+ when 0xdd then set8(3, :l, cycles: 8)
522
+ when 0xde then set8(3, :indirect_hl, cycles: 16)
523
+ when 0xdf then set8(3, :a, cycles: 8)
524
+ when 0xe0 then set8(4, :b, cycles: 8)
525
+ when 0xe1 then set8(4, :c, cycles: 8)
526
+ when 0xe2 then set8(4, :d, cycles: 8)
527
+ when 0xe3 then set8(4, :e, cycles: 8)
528
+ when 0xe4 then set8(4, :h, cycles: 8)
529
+ when 0xe5 then set8(4, :l, cycles: 8)
530
+ when 0xe6 then set8(4, :indirect_hl, cycles: 16)
531
+ when 0xe7 then set8(4, :a, cycles: 8)
532
+ when 0xe8 then set8(5, :b, cycles: 8)
533
+ when 0xe9 then set8(5, :c, cycles: 8)
534
+ when 0xea then set8(5, :d, cycles: 8)
535
+ when 0xeb then set8(5, :e, cycles: 8)
536
+ when 0xec then set8(5, :h, cycles: 8)
537
+ when 0xed then set8(5, :l, cycles: 8)
538
+ when 0xee then set8(5, :indirect_hl, cycles: 16)
539
+ when 0xef then set8(5, :a, cycles: 8)
540
+ when 0xf0 then set8(6, :b, cycles: 8)
541
+ when 0xf1 then set8(6, :c, cycles: 8)
542
+ when 0xf2 then set8(6, :d, cycles: 8)
543
+ when 0xf3 then set8(6, :e, cycles: 8)
544
+ when 0xf4 then set8(6, :h, cycles: 8)
545
+ when 0xf5 then set8(6, :l, cycles: 8)
546
+ when 0xf6 then set8(6, :indirect_hl, cycles: 16)
547
+ when 0xf7 then set8(6, :a, cycles: 8)
548
+ when 0xf8 then set8(7, :b, cycles: 8)
549
+ when 0xf9 then set8(7, :c, cycles: 8)
550
+ when 0xfa then set8(7, :d, cycles: 8)
551
+ when 0xfb then set8(7, :e, cycles: 8)
552
+ when 0xfc then set8(7, :h, cycles: 8)
553
+ when 0xfd then set8(7, :l, cycles: 8)
554
+ when 0xfe then set8(7, :indirect_hl, cycles: 16)
555
+ when 0xff then set8(7, :a, cycles: 8)
556
556
  else
557
557
  raise "unknown opcode: 0xcb 0x#{'%02x' % opcode}"
558
558
  end
@@ -592,27 +592,33 @@ module Rubyboy
592
592
  end
593
593
 
594
594
  def print_log(opcode)
595
- puts "PC: 0x#{'%04x' % @pc}, Opcode: 0x#{'%02x' % opcode}, AF: 0x#{'%04x' % @registers.read16(:af)}, BC: 0x#{'%04x' % @registers.read16(:bc)}, HL: 0x#{'%04x' % @registers.read16(:hl)}, SP: 0x#{'%04x' % @sp}"
595
+ puts "PC: 0x#{'%04x' % @pc}, Opcode: 0x#{'%02x' % opcode}, AF: 0x#{'%04x' % @registers.af}, BC: 0x#{'%04x' % @registers.bc}, HL: 0x#{'%04x' % @registers.hl}, SP: 0x#{'%04x' % @sp}"
596
596
  end
597
597
 
598
- def flags
599
- f_value = @registers.read8(:f)
600
- {
601
- z: f_value[7] == 1,
602
- n: f_value[6] == 1,
603
- h: f_value[5] == 1,
604
- c: f_value[4] == 1
605
- }
598
+ def flag_z
599
+ @registers.f[7] == 1
606
600
  end
607
601
 
608
- def update_flags(z: flags[:z], n: flags[:n], h: flags[:h], c: flags[:c])
602
+ def flag_n
603
+ @registers.f[6] == 1
604
+ end
605
+
606
+ def flag_h
607
+ @registers.f[5] == 1
608
+ end
609
+
610
+ def flag_c
611
+ @registers.f[4] == 1
612
+ end
613
+
614
+ def update_flags(z: flag_z, n: flag_n, h: flag_h, c: flag_c)
609
615
  f_value = 0x00
610
616
  f_value |= 0x80 if z
611
617
  f_value |= 0x40 if n
612
618
  f_value |= 0x20 if h
613
619
  f_value |= 0x10 if c
614
620
 
615
- @registers.write8(:f, f_value)
621
+ @registers.f = f_value
616
622
  end
617
623
 
618
624
  def bool_to_integer(bool)
@@ -632,10 +638,10 @@ module Rubyboy
632
638
  byte > 127 ? byte - 256 : byte
633
639
  end
634
640
 
635
- def rlca
636
- a_value = @registers.read8(:a)
641
+ def rlca(cycles:)
642
+ a_value = @registers.a
637
643
  a_value = ((a_value << 1) | (a_value >> 7)) & 0xff
638
- @registers.write8(:a, a_value)
644
+ @registers.a = a_value
639
645
  update_flags(
640
646
  z: false,
641
647
  n: false,
@@ -643,13 +649,13 @@ module Rubyboy
643
649
  c: a_value[0] == 1
644
650
  )
645
651
 
646
- 4
652
+ cycles
647
653
  end
648
654
 
649
- def rrca
650
- a_value = @registers.read8(:a)
655
+ def rrca(cycles:)
656
+ a_value = @registers.a
651
657
  a_value = ((a_value >> 1) | (a_value << 7)) & 0xff
652
- @registers.write8(:a, a_value)
658
+ @registers.a = a_value
653
659
  update_flags(
654
660
  z: false,
655
661
  n: false,
@@ -657,14 +663,14 @@ module Rubyboy
657
663
  c: a_value[7] == 1
658
664
  )
659
665
 
660
- 4
666
+ cycles
661
667
  end
662
668
 
663
- def rra
664
- a_value = @registers.read8(:a)
669
+ def rra(cycles:)
670
+ a_value = @registers.a
665
671
  cflag = a_value[0] == 1
666
- a_value = ((a_value >> 1) | (bool_to_integer(flags[:c]) << 7)) & 0xff
667
- @registers.write8(:a, a_value)
672
+ a_value = ((a_value >> 1) | (bool_to_integer(flag_c) << 7)) & 0xff
673
+ @registers.a = a_value
668
674
  update_flags(
669
675
  z: false,
670
676
  n: false,
@@ -672,14 +678,14 @@ module Rubyboy
672
678
  c: cflag
673
679
  )
674
680
 
675
- 4
681
+ cycles
676
682
  end
677
683
 
678
- def rla
679
- a_value = @registers.read8(:a)
684
+ def rla(cycles:)
685
+ a_value = @registers.a
680
686
  cflag = a_value[7] == 1
681
- a_value = ((a_value << 1) | bool_to_integer(flags[:c])) & 0xff
682
- @registers.write8(:a, a_value)
687
+ a_value = ((a_value << 1) | bool_to_integer(flag_c)) & 0xff
688
+ @registers.a = a_value
683
689
  update_flags(
684
690
  z: false,
685
691
  n: false,
@@ -687,62 +693,62 @@ module Rubyboy
687
693
  c: cflag
688
694
  )
689
695
 
690
- 4
696
+ cycles
691
697
  end
692
698
 
693
- def daa
694
- a_value = @registers.read8(:a)
695
- if flags[:n]
696
- a_value -= 0x06 if flags[:h]
697
- a_value -= 0x60 if flags[:c]
699
+ def daa(cycles:)
700
+ a_value = @registers.a
701
+ if flag_n
702
+ a_value -= 0x06 if flag_h
703
+ a_value -= 0x60 if flag_c
698
704
  else
699
- if flags[:c] || a_value > 0x99
705
+ if flag_c || a_value > 0x99
700
706
  a_value += 0x60
701
707
  update_flags(c: true)
702
708
  end
703
- a_value += 0x06 if flags[:h] || (a_value & 0x0f) > 0x09
709
+ a_value += 0x06 if flag_h || (a_value & 0x0f) > 0x09
704
710
  end
705
711
 
706
- @registers.write8(:a, a_value)
712
+ @registers.a = a_value
707
713
  update_flags(
708
- z: @registers.read8(:a) == 0,
714
+ z: @registers.a == 0,
709
715
  h: false
710
716
  )
711
717
 
712
- 4
718
+ cycles
713
719
  end
714
720
 
715
- def cpl
716
- @registers.write8(:a, ~@registers.read8(:a))
721
+ def cpl(cycles:)
722
+ @registers.a = ~@registers.a
717
723
  update_flags(
718
724
  n: true,
719
725
  h: true
720
726
  )
721
727
 
722
- 4
728
+ cycles
723
729
  end
724
730
 
725
- def scf
731
+ def scf(cycles:)
726
732
  update_flags(
727
733
  n: false,
728
734
  h: false,
729
735
  c: true
730
736
  )
731
737
 
732
- 4
738
+ cycles
733
739
  end
734
740
 
735
- def ccf
741
+ def ccf(cycles:)
736
742
  update_flags(
737
743
  n: false,
738
744
  h: false,
739
- c: !flags[:c]
745
+ c: !flag_c
740
746
  )
741
747
 
742
- 4
748
+ cycles
743
749
  end
744
750
 
745
- def inc8(x)
751
+ def inc8(x, cycles:)
746
752
  value = (get_value(x) + 1) & 0xff
747
753
  set_value(x, value)
748
754
  update_flags(
@@ -751,16 +757,16 @@ module Rubyboy
751
757
  h: (value & 0x0f) == 0
752
758
  )
753
759
 
754
- x[:type] == :register8 ? 4 : 12
760
+ cycles
755
761
  end
756
762
 
757
- def inc16(x)
763
+ def inc16(x, cycles:)
758
764
  set_value(x, get_value(x) + 1)
759
765
 
760
- 8
766
+ cycles
761
767
  end
762
768
 
763
- def dec8(x)
769
+ def dec8(x, cycles:)
764
770
  value = (get_value(x) - 1) & 0xff
765
771
  set_value(x, value)
766
772
  update_flags(
@@ -769,35 +775,35 @@ module Rubyboy
769
775
  h: (value & 0x0f) == 0x0f
770
776
  )
771
777
 
772
- x[:type] == :register8 ? 4 : 12
778
+ cycles
773
779
  end
774
780
 
775
- def dec16(x)
781
+ def dec16(x, cycles:)
776
782
  set_value(x, get_value(x) - 1)
777
783
 
778
- 8
784
+ cycles
779
785
  end
780
786
 
781
- def add8(x)
782
- a_value = @registers.read8(:a)
787
+ def add8(x, cycles:)
788
+ a_value = @registers.a
783
789
  x_value = get_value(x)
784
790
 
785
791
  hflag = (a_value & 0x0f) + (x_value & 0x0f) > 0x0f
786
792
  cflag = a_value + x_value > 0xff
787
793
 
788
794
  a_value += x_value
789
- @registers.write8(:a, a_value)
795
+ @registers.a = a_value
790
796
  update_flags(
791
- z: @registers.read8(:a) == 0,
797
+ z: @registers.a == 0,
792
798
  n: false,
793
799
  h: hflag,
794
800
  c: cflag
795
801
  )
796
802
 
797
- x[:type] == :register8 ? 4 : 8
803
+ cycles
798
804
  end
799
805
 
800
- def add16(x, y)
806
+ def add16(x, y, cycles:)
801
807
  x_value = get_value(x)
802
808
  y_value = get_value(y)
803
809
 
@@ -811,10 +817,10 @@ module Rubyboy
811
817
  c: cflag
812
818
  )
813
819
 
814
- 8
820
+ cycles
815
821
  end
816
822
 
817
- def add_sp_r8
823
+ def add_sp_r8(cycles:)
818
824
  byte = to_signed_byte(read_byte_and_advance_pc)
819
825
 
820
826
  hflag = (@sp & 0x0f) + (byte & 0x0f) > 0x0f
@@ -829,17 +835,17 @@ module Rubyboy
829
835
  c: cflag
830
836
  )
831
837
 
832
- 16
838
+ cycles
833
839
  end
834
840
 
835
- def sub8(x)
836
- a_value = @registers.read8(:a)
841
+ def sub8(x, cycles:)
842
+ a_value = @registers.a
837
843
  x_value = get_value(x)
838
844
 
839
845
  hflag = (x_value & 0x0f) > (a_value & 0x0f)
840
846
  cflag = x_value > a_value
841
847
  a_value -= x_value
842
- @registers.write8(:a, a_value)
848
+ @registers.a = a_value
843
849
  update_flags(
844
850
  z: a_value == 0,
845
851
  n: true,
@@ -847,50 +853,50 @@ module Rubyboy
847
853
  c: cflag
848
854
  )
849
855
 
850
- x[:type] == :register8 ? 4 : 8
856
+ cycles
851
857
  end
852
858
 
853
- def adc8(x)
854
- a_value = @registers.read8(:a)
859
+ def adc8(x, cycles:)
860
+ a_value = @registers.a
855
861
  x_value = get_value(x)
856
- c_value = bool_to_integer(flags[:c])
862
+ c_value = bool_to_integer(flag_c)
857
863
 
858
864
  hflag = (a_value & 0x0f) + (x_value & 0x0f) + c_value > 0x0f
859
865
  cflag = a_value + x_value + c_value > 0xff
860
866
  a_value += x_value + c_value
861
- @registers.write8(:a, a_value)
867
+ @registers.a = a_value
862
868
  update_flags(
863
- z: @registers.read8(:a) == 0,
869
+ z: @registers.a == 0,
864
870
  n: false,
865
871
  h: hflag,
866
872
  c: cflag
867
873
  )
868
874
 
869
- x[:type] == :register8 ? 4 : 8
875
+ cycles
870
876
  end
871
877
 
872
- def sbc8(x)
873
- a_value = @registers.read8(:a)
878
+ def sbc8(x, cycles:)
879
+ a_value = @registers.a
874
880
  x_value = get_value(x)
875
- c_value = bool_to_integer(flags[:c])
881
+ c_value = bool_to_integer(flag_c)
876
882
 
877
883
  hflag = (x_value & 0x0f) + c_value > (a_value & 0x0f)
878
884
  cflag = x_value + c_value > a_value
879
885
  a_value -= x_value + c_value
880
- @registers.write8(:a, a_value)
886
+ @registers.a = a_value
881
887
  update_flags(
882
- z: @registers.read8(:a) == 0,
888
+ z: @registers.a == 0,
883
889
  n: true,
884
890
  h: hflag,
885
891
  c: cflag
886
892
  )
887
893
 
888
- x[:type] == :register8 ? 4 : 8
894
+ cycles
889
895
  end
890
896
 
891
- def and8(x)
892
- a_value = @registers.read8(:a) & get_value(x)
893
- @registers.write8(:a, a_value)
897
+ def and8(x, cycles:)
898
+ a_value = @registers.a & get_value(x)
899
+ @registers.a = a_value
894
900
  update_flags(
895
901
  z: a_value == 0,
896
902
  n: false,
@@ -898,12 +904,12 @@ module Rubyboy
898
904
  c: false
899
905
  )
900
906
 
901
- x[:type] == :register8 ? 4 : 8
907
+ cycles
902
908
  end
903
909
 
904
- def or8(x)
905
- a_value = @registers.read8(:a) | get_value(x)
906
- @registers.write8(:a, a_value)
910
+ def or8(x, cycles:)
911
+ a_value = @registers.a | get_value(x)
912
+ @registers.a = a_value
907
913
  update_flags(
908
914
  z: a_value == 0,
909
915
  n: false,
@@ -911,12 +917,12 @@ module Rubyboy
911
917
  c: false
912
918
  )
913
919
 
914
- x[:type] == :register8 ? 4 : 8
920
+ cycles
915
921
  end
916
922
 
917
- def xor8(x)
918
- a_value = @registers.read8(:a) ^ get_value(x)
919
- @registers.write8(:a, a_value)
923
+ def xor8(x, cycles:)
924
+ a_value = @registers.a ^ get_value(x)
925
+ @registers.a = a_value
920
926
  update_flags(
921
927
  z: a_value == 0,
922
928
  n: false,
@@ -924,58 +930,49 @@ module Rubyboy
924
930
  c: false
925
931
  )
926
932
 
927
- x[:type] == :register8 ? 4 : 8
933
+ cycles
928
934
  end
929
935
 
930
- def push16(register16)
931
- value = @registers.read16(register16)
936
+ def push16(register16, cycles:)
932
937
  @sp -= 2
933
- write_word(@sp, value)
938
+ write_word(@sp, get_value(register16))
934
939
 
935
- 16
940
+ cycles
936
941
  end
937
942
 
938
- def pop16(register16)
939
- @registers.write16(register16, read_word(@sp))
943
+ def pop16(register16, cycles:)
944
+ set_value(register16, read_word(@sp))
940
945
  @sp += 2
941
946
 
942
- 12
947
+ cycles
943
948
  end
944
949
 
945
- def halt
950
+ def halt(cycles:)
946
951
  @halted = true
947
952
 
948
- 4
953
+ cycles
949
954
  end
950
955
 
951
- def ld8(x, y)
956
+ def ld8(x, y, cycles:)
952
957
  value = get_value(y)
953
958
  set_value(x, value)
954
959
 
955
- return 4 if x[:type] == :register8 && y[:type] == :register8
956
- return 12 if x[:type] == :indirect && y[:type] == :immediate8
957
- return 12 if x[:type] == :ff00 || y[:type] == :ff00
958
- return 16 if x[:type] == :direct8 || y[:type] == :direct8
959
-
960
- 8
960
+ cycles
961
961
  end
962
962
 
963
- def ld16(x, y)
963
+ def ld16(x, y, cycles:)
964
964
  value = get_value(y)
965
965
  set_value(x, value)
966
966
 
967
- return 8 if y[:type] == :register16
968
- return 20 if y[:type] == :sp
969
-
970
- 12
967
+ cycles
971
968
  end
972
969
 
973
- def ld_hl_sp_r8
970
+ def ld_hl_sp_r8(cycles:)
974
971
  byte = to_signed_byte(read_byte_and_advance_pc)
975
972
 
976
973
  hflag = (@sp & 0x0f) + (byte & 0x0f) > 0x0f
977
974
  cflag = (@sp & 0xff) + (byte & 0xff) > 0xff
978
- @registers.write16(:hl, @sp + byte)
975
+ @registers.hl = @sp + byte
979
976
  update_flags(
980
977
  z: false,
981
978
  n: false,
@@ -983,24 +980,24 @@ module Rubyboy
983
980
  c: cflag
984
981
  )
985
982
 
986
- 12
983
+ cycles
987
984
  end
988
985
 
989
- def ei
986
+ def ei(cycles:)
990
987
  @ime_delay = true
991
988
 
992
- 4
989
+ cycles
993
990
  end
994
991
 
995
- def di
992
+ def di(cycles:)
996
993
  @ime_delay = false
997
994
  @ime = false
998
995
 
999
- 4
996
+ cycles
1000
997
  end
1001
998
 
1002
- def cp8(x)
1003
- a_value = @registers.read8(:a)
999
+ def cp8(x, cycles:)
1000
+ a_value = @registers.a
1004
1001
  x_value = get_value(x)
1005
1002
 
1006
1003
  hflag = (x_value & 0x0f) > (a_value & 0x0f)
@@ -1012,34 +1009,38 @@ module Rubyboy
1012
1009
  c: cflag
1013
1010
  )
1014
1011
 
1015
- x[:type] == :register8 ? 4 : 8
1012
+ cycles
1016
1013
  end
1017
1014
 
1018
- def rst(addr)
1015
+ def rst(addr, cycles:)
1019
1016
  @sp -= 2
1020
1017
  write_word(@sp, @pc)
1021
1018
  @pc = addr
1022
1019
 
1023
- 16
1020
+ cycles
1024
1021
  end
1025
1022
 
1026
- def jr(condition: true)
1023
+ def jr(condition:)
1027
1024
  value = to_signed_byte(read_byte_and_advance_pc)
1028
1025
  @pc += value if condition
1029
1026
 
1030
1027
  condition ? 12 : 8
1031
1028
  end
1032
1029
 
1033
- def jp(x, condition: true)
1030
+ def jp(x, condition:)
1034
1031
  addr = get_value(x)
1035
1032
  @pc = addr if condition
1036
1033
 
1037
- return 4 if x[:type] == :register16
1038
-
1039
1034
  condition ? 16 : 12
1040
1035
  end
1041
1036
 
1042
- def call16(x, condition: true)
1037
+ def jp_hl(cycles:)
1038
+ @pc = @registers.hl
1039
+
1040
+ cycles
1041
+ end
1042
+
1043
+ def call16(x, condition:)
1043
1044
  addr = get_value(x)
1044
1045
  if condition
1045
1046
  @sp -= 2
@@ -1050,25 +1051,25 @@ module Rubyboy
1050
1051
  condition ? 24 : 12
1051
1052
  end
1052
1053
 
1053
- def ret
1054
+ def ret(cycles:)
1054
1055
  @pc = read_word(@sp)
1055
1056
  @sp += 2
1056
1057
 
1057
- 16
1058
+ cycles
1058
1059
  end
1059
1060
 
1060
- def ret_if(condition)
1061
- ret if condition
1061
+ def ret_if(condition:)
1062
+ ret(cycles: 16) if condition
1062
1063
 
1063
1064
  condition ? 20 : 8
1064
1065
  end
1065
1066
 
1066
- def reti
1067
+ def reti(cycles:)
1067
1068
  @ime = true
1068
- ret
1069
+ ret(cycles:)
1069
1070
  end
1070
1071
 
1071
- def rlc8(x)
1072
+ def rlc8(x, cycles:)
1072
1073
  value = get_value(x)
1073
1074
  value = (value << 1) | (value >> 7)
1074
1075
  set_value(x, value)
@@ -1079,10 +1080,10 @@ module Rubyboy
1079
1080
  c: value[0] == 1
1080
1081
  )
1081
1082
 
1082
- x[:type] == :register8 ? 8 : 16
1083
+ cycles
1083
1084
  end
1084
1085
 
1085
- def rrc8(x)
1086
+ def rrc8(x, cycles:)
1086
1087
  value = get_value(x)
1087
1088
  value = (value >> 1) | (value << 7)
1088
1089
  set_value(x, value)
@@ -1093,13 +1094,13 @@ module Rubyboy
1093
1094
  c: value[7] == 1
1094
1095
  )
1095
1096
 
1096
- x[:type] == :register8 ? 8 : 16
1097
+ cycles
1097
1098
  end
1098
1099
 
1099
- def rl8(x)
1100
+ def rl8(x, cycles:)
1100
1101
  value = get_value(x)
1101
1102
  cflag = value[7] == 1
1102
- value = ((value << 1) | bool_to_integer(flags[:c])) & 0xff
1103
+ value = ((value << 1) | bool_to_integer(flag_c)) & 0xff
1103
1104
  set_value(x, value)
1104
1105
  update_flags(
1105
1106
  z: value == 0,
@@ -1108,13 +1109,13 @@ module Rubyboy
1108
1109
  c: cflag
1109
1110
  )
1110
1111
 
1111
- x[:type] == :register8 ? 8 : 16
1112
+ cycles
1112
1113
  end
1113
1114
 
1114
- def rr8(x)
1115
+ def rr8(x, cycles:)
1115
1116
  value = get_value(x)
1116
1117
  cflag = value[0] == 1
1117
- value = (value >> 1) | (bool_to_integer(flags[:c]) << 7)
1118
+ value = (value >> 1) | (bool_to_integer(flag_c) << 7)
1118
1119
  set_value(x, value)
1119
1120
  update_flags(
1120
1121
  z: value == 0,
@@ -1123,10 +1124,10 @@ module Rubyboy
1123
1124
  c: cflag
1124
1125
  )
1125
1126
 
1126
- x[:type] == :register8 ? 8 : 16
1127
+ cycles
1127
1128
  end
1128
1129
 
1129
- def sla8(x)
1130
+ def sla8(x, cycles:)
1130
1131
  value = get_value(x)
1131
1132
  cflag = value[7] == 1
1132
1133
  value <<= 1
@@ -1139,10 +1140,10 @@ module Rubyboy
1139
1140
  c: cflag
1140
1141
  )
1141
1142
 
1142
- x[:type] == :register8 ? 8 : 16
1143
+ cycles
1143
1144
  end
1144
1145
 
1145
- def sra8(x)
1146
+ def sra8(x, cycles:)
1146
1147
  value = get_value(x)
1147
1148
  cflag = value[0] == 1
1148
1149
  value = (value >> 1) | (value[7] << 7)
@@ -1154,10 +1155,10 @@ module Rubyboy
1154
1155
  c: cflag
1155
1156
  )
1156
1157
 
1157
- x[:type] == :register8 ? 8 : 16
1158
+ cycles
1158
1159
  end
1159
1160
 
1160
- def swap8(x)
1161
+ def swap8(x, cycles:)
1161
1162
  value = get_value(x)
1162
1163
  value = ((value & 0x0f) << 4) | ((value & 0xf0) >> 4)
1163
1164
  set_value(x, value)
@@ -1168,10 +1169,10 @@ module Rubyboy
1168
1169
  c: false
1169
1170
  )
1170
1171
 
1171
- x[:type] == :register8 ? 8 : 16
1172
+ cycles
1172
1173
  end
1173
1174
 
1174
- def srl8(x)
1175
+ def srl8(x, cycles:)
1175
1176
  value = get_value(x)
1176
1177
  cflag = value[0] == 1
1177
1178
  value >>= 1
@@ -1183,10 +1184,10 @@ module Rubyboy
1183
1184
  c: cflag
1184
1185
  )
1185
1186
 
1186
- x[:type] == :register8 ? 8 : 16
1187
+ cycles
1187
1188
  end
1188
1189
 
1189
- def bit8(n, x)
1190
+ def bit8(n, x, cycles:)
1190
1191
  value = get_value(x)
1191
1192
  update_flags(
1192
1193
  z: value[n] == 0,
@@ -1194,65 +1195,89 @@ module Rubyboy
1194
1195
  h: true
1195
1196
  )
1196
1197
 
1197
- x[:type] == :register8 ? 8 : 12
1198
+ cycles
1198
1199
  end
1199
1200
 
1200
- def res8(n, x)
1201
+ def res8(n, x, cycles:)
1201
1202
  value = get_value(x)
1202
1203
  value &= ((~(1 << n)) & 0xff)
1203
1204
  set_value(x, value)
1204
1205
 
1205
- x[:type] == :register8 ? 8 : 16
1206
+ cycles
1206
1207
  end
1207
1208
 
1208
- def set8(n, x)
1209
+ def set8(n, x, cycles:)
1209
1210
  value = get_value(x)
1210
1211
  value |= (1 << n)
1211
1212
  set_value(x, value)
1212
1213
 
1213
- x[:type] == :register8 ? 8 : 16
1214
+ cycles
1214
1215
  end
1215
1216
 
1216
1217
  def get_value(operand)
1217
- case operand[:type]
1218
- when :register8 then @registers.read8(operand[:value])
1219
- when :register16 then @registers.read16(operand[:value])
1218
+ case operand
1219
+ when :a then @registers.a
1220
+ when :b then @registers.b
1221
+ when :c then @registers.c
1222
+ when :d then @registers.d
1223
+ when :e then @registers.e
1224
+ when :h then @registers.h
1225
+ when :l then @registers.l
1226
+ when :f then @registers.f
1227
+ when :af then @registers.af
1228
+ when :bc then @registers.bc
1229
+ when :de then @registers.de
1230
+ when :hl then @registers.hl
1220
1231
  when :sp then @sp
1221
1232
  when :immediate8 then read_byte_and_advance_pc
1222
1233
  when :immediate16 then read_word_and_advance_pc
1223
1234
  when :direct8 then read_byte(read_word_and_advance_pc)
1224
1235
  when :direct16 then read_word(read_word_and_advance_pc)
1225
- when :indirect then read_byte(@registers.read16(operand[:value]))
1226
1236
  when :ff00 then read_byte(0xff00 + read_byte_and_advance_pc)
1227
- when :ff00_c then read_byte(0xff00 + @registers.read8(:c))
1237
+ when :ff00_c then read_byte(0xff00 + @registers.c)
1228
1238
  when :hl_inc
1229
- value = read_byte(@registers.read16(:hl))
1230
- @registers.increment16(:hl)
1239
+ value = read_byte(@registers.hl)
1240
+ @registers.hl += 1
1231
1241
  value
1232
1242
  when :hl_dec
1233
- value = read_byte(@registers.read16(:hl))
1234
- @registers.decrement16(:hl)
1243
+ value = read_byte(@registers.hl)
1244
+ @registers.hl -= 1
1235
1245
  value
1246
+ when :indirect_hl then read_byte(@registers.hl)
1247
+ when :indirect_bc then read_byte(@registers.bc)
1248
+ when :indirect_de then read_byte(@registers.de)
1236
1249
  else raise "unknown operand: #{operand}"
1237
1250
  end
1238
1251
  end
1239
1252
 
1240
1253
  def set_value(operand, value)
1241
- case operand[:type]
1242
- when :register8 then @registers.write8(operand[:value], value)
1243
- when :register16 then @registers.write16(operand[:value], value)
1254
+ case operand
1255
+ when :a then @registers.a = value
1256
+ when :b then @registers.b = value
1257
+ when :c then @registers.c = value
1258
+ when :d then @registers.d = value
1259
+ when :e then @registers.e = value
1260
+ when :h then @registers.h = value
1261
+ when :l then @registers.l = value
1262
+ when :f then @registers.f = value
1263
+ when :af then @registers.af = value
1264
+ when :bc then @registers.bc = value
1265
+ when :de then @registers.de = value
1266
+ when :hl then @registers.hl = value
1244
1267
  when :sp then @sp = value & 0xffff
1245
1268
  when :direct8 then write_byte(read_word_and_advance_pc, value)
1246
1269
  when :direct16 then write_word(read_word_and_advance_pc, value)
1247
- when :indirect then write_byte(@registers.read16(operand[:value]), value)
1248
1270
  when :ff00 then write_byte(0xff00 + read_byte_and_advance_pc, value)
1249
- when :ff00_c then write_byte(0xff00 + @registers.read8(:c), value)
1271
+ when :ff00_c then write_byte(0xff00 + @registers.c, value)
1250
1272
  when :hl_inc
1251
- write_byte(@registers.read16(:hl), value)
1252
- @registers.increment16(:hl)
1273
+ write_byte(@registers.hl, value)
1274
+ @registers.hl += 1
1253
1275
  when :hl_dec
1254
- write_byte(@registers.read16(:hl), value)
1255
- @registers.decrement16(:hl)
1276
+ write_byte(@registers.hl, value)
1277
+ @registers.hl -= 1
1278
+ when :indirect_hl then write_byte(@registers.hl, value)
1279
+ when :indirect_bc then write_byte(@registers.bc, value)
1280
+ when :indirect_de then write_byte(@registers.de, value)
1256
1281
  when :immediate8, :immediate16 then raise 'immediate type is read only'
1257
1282
  else raise "unknown operand: #{operand}"
1258
1283
  end