-
Notifications
You must be signed in to change notification settings - Fork 1
/
math8.rb
362 lines (339 loc) · 12.3 KB
/
math8.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
module Z80ops
ADDA_A = 0x87
ADDA_B = 0x80
ADDA_C = 0x81
ADDA_D = 0x82
ADDA_E = 0x83
ADDA_H = 0x84
ADDA_L = 0x85
ADDA_HL = 0x86
ADDA_N = 0xC6
ADCA_A = 0x8F
ADCA_B = 0x88
ADCA_C = 0x89
ADCA_D = 0x8A
ADCA_E = 0x8B
ADCA_H = 0x8C
ADCA_L = 0x8D
ADCA_HL = 0x8E
ADCA_N = 0xCE
SUBA_A = 0x97
SUBA_B = 0x90
SUBA_C = 0x91
SUBA_D = 0x92
SUBA_E = 0x93
SUBA_H = 0x94
SUBA_L = 0x95
SUBA_HL = 0x96
SUBA_N = 0xD6
SBCA_A = 0x9F
SBCA_B = 0x98
SBCA_C = 0x99
SBCA_D = 0x9A
SBCA_E = 0x9B
SBCA_H = 0x9C
SBCA_L = 0x9D
SBCA_HL = 0x9E
SBCA_N = 0xDE
ANDA_A = 0xA7
ANDA_B = 0xA0
ANDA_C = 0xA1
ANDA_D = 0xA2
ANDA_E = 0xA3
ANDA_H = 0xA4
ANDA_L = 0xA5
ANDA_HL = 0xA6
ANDA_N = 0xE6
#todo, ops with IX, IY
end
class Z80cpu
def add_flags(a, old_x, old_y, carry)
f = 0 # Clear flags
f = f | SIGN if (a & SIGN) != 0 # sign
f = f | ZERO if (a & 0xFF) == 0 # Check for zero
f = f | HC if ((old_x & 0xF) + (old_y & 0xF) + carry) > 15 #half carry
f = f | CARRY if old_x + old_y + carry > 255 # carry
f = f | OVER if ((old_x & SIGN) == (old_y & SIGN)) && ((a & SIGN) != (old_x & SIGN)) # Check for overflow
f
end
def sub_flags(a, old_x, old_y, carry)
f = SUB
f = f | SIGN if (a & SIGN) != 0 # sign
f = f | ZERO if (a & 255) == 0 # Check for zero
f = f | HC if ((old_y & 0xF) + carry ) > (old_x & 0xF) #half carry
f = f | CARRY if (old_y + carry) > old_x
f = f | OVER if ((old_x & SIGN) != (old_y & SIGN)) && ((a & SIGN) != (old_x & SIGN))
f
end
def logic_flags(a)
f = HC
f = f | SIGN if (a & SIGN) != 0 # sign
f = f | ZERO if (a & 255) == 0 # Check for zero
f = f | OVER if !a.to_s(2).count('1').odd?
f
end
def adda_a(carry = 0)
a = @registers.a
x = @registers.a
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_b(carry = 0)
a = @registers.a
x = @registers.b
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_c(carry = 0)
a = @registers.a
x = @registers.c
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_d(carry = 0)
a = @registers.a
x = @registers.d
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_e(carry = 0)
a = @registers.a
x = @registers.e
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_h(carry = 0)
a = @registers.a
x = @registers.h
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_l(carry = 0)
a = @registers.a
x = @registers.l
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def adda_n(carry = 0)
a = @registers.a
x = @memory.rb[@registers.pc]
@registers.pc = @registers.pc + 1
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 2
@registers.t = 7 # 2 M taken
end
def adda_hl(carry = 0)
a = @registers.a
x = @memory.rb[((@registers.h << 8) & 0xF0) | ((@registers.l << 8) & 0x0F)]
@registers.a += x + carry # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 2
@registers.t = 7 # 2 M taken
end
def adca_a; adda_a(@registers.f & Z80cpu.CARRY); end
def adca_b; adda_b(@registers.f & Z80cpu.CARRY); end
def adca_c; adda_c(@registers.f & Z80cpu.CARRY); end
def adca_d; adda_d(@registers.f & Z80cpu.CARRY); end
def adca_e; adda_e(@registers.f & Z80cpu.CARRY); end
def adca_h; adda_h(@registers.f & Z80cpu.CARRY); end
def adca_l; adda_l(@registers.f & Z80cpu.CARRY); end
def adca_n; adda_n(@registers.f & Z80cpu.CARRY); end
def adca_h1; adda_hl(@registers.f & Z80cpu.CARRY); end
def suba_a(carry = 0)
a = @registers.a
x = @registers.a
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_b(carry = 0)
a = @registers.a
x = @registers.b
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_c(carry = 0)
a = @registers.a
x = @registers.c
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_d(carry = 0)
a = @registers.a
x = @registers.d
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_e(carry = 0)
a = @registers.a
x = @registers.e
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_h(carry = 0)
a = @registers.a
x = @registers.h
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_l(carry = 0)
a = @registers.a
x = @registers.l
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def suba_n(carry = 0)
a = @registers.a
x = @memory.rb[@registers.pc]
@registers.pc = @registers.pc + 1
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 2
@registers.t = 7 # 2 M taken
end
def suba_hl(carry = 0)
a = @registers.a
x = @memory.rb[((@registers.h << 8) & 0xF0) | ((@registers.l << 8) & 0x0F)]
@registers.a -= (x + carry) # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = sub_flags(@registers.a,a,x,carry) # calculate flags
@registers.m = 2
@registers.t = 7 # 2 M taken
end
def sbca_a; suba_a(@registers.f & Z80cpu.CARRY); end
def sbca_b; suba_b(@registers.f & Z80cpu.CARRY); end
def sbca_c; suba_c(@registers.f & Z80cpu.CARRY); end
def sbca_d; suba_d(@registers.f & Z80cpu.CARRY); end
def sbca_e; suba_e(@registers.f & Z80cpu.CARRY); end
def sbca_h; suba_h(@registers.f & Z80cpu.CARRY); end
def sbca_l; suba_l(@registers.f & Z80cpu.CARRY); end
def sbca_n; suba_n(@registers.f & Z80cpu.CARRY); end
def sbca_hl; suba_hl(@registers.f & Z80cpu.CARRY); end
def anda_a(carry = 0)
a = @registers.a
x = @registers.a
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_b(carry = 0)
a = @registers.a
x = @registers.b
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_c(carry = 0)
a = @registers.a
x = @registers.c
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_d(carry = 0)
a = @registers.a
x = @registers.d
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_e(carry = 0)
a = @registers.a
x = @registers.e
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_h(carry = 0)
a = @registers.a
x = @registers.h
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_l(carry = 0)
a = @registers.a
x = @registers.l
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 1
@registers.t = 4 # 1 M taken
end
def anda_n(carry = 0)
a = @registers.a
x = @memory.rb[@registers.pc]
@registers.pc = @registers.pc + 1
@registers.a = a & x
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = logic_flags(@registers.a) # calculate flags
@registers.m = 2
@registers.t = 7 # 2 M taken
end
def anda_hl(carry = 0)
a = @registers.a
x = @memory.rb[((@registers.h << 8) & 0xF0) | ((@registers.l << 8) & 0x0F)]
@registers.a = a & x # Perform addition
@registers.a = @registers.a & 255 # Mask to 8-bits
@registers.f = add_flags(@registers.a) # calculate flags
@registers.m = 2
@registers.t = 7 # 2 M taken
end
end