forked from anotherlin/z80emu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
macros.h
378 lines (332 loc) · 20.7 KB
/
macros.h
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* macros.h
* Helper macros definitions.
*
* Copyright (c) 2012-2017 Lin Ke-Fong
*
* This code is free, do whatever you want with it.
*/
/* Shortcuts for flags and registers. */
#define SZC_FLAGS (Z80_S_FLAG | Z80_Z_FLAG | Z80_C_FLAG)
#define YX_FLAGS (Z80_Y_FLAG | Z80_X_FLAG)
#define SZ_FLAGS (Z80_S_FLAG | Z80_Z_FLAG)
#define SZPV_FLAGS (Z80_S_FLAG | Z80_Z_FLAG | Z80_PV_FLAG)
#define SYX_FLAGS (Z80_S_FLAG | Z80_Y_FLAG | Z80_X_FLAG)
#define HC_FLAGS (Z80_H_FLAG | Z80_C_FLAG)
#define A (state->registers.byte[Z80_A])
#define F (state->registers.byte[Z80_F])
#define B (state->registers.byte[Z80_B])
#define C (state->registers.byte[Z80_C])
#define AF (state->registers.word[Z80_AF])
#define BC (state->registers.word[Z80_BC])
#define DE (state->registers.word[Z80_DE])
#define HL (state->registers.word[Z80_HL])
#define SP (state->registers.word[Z80_SP])
#define HL_IX_IY *((unsigned short *) registers[6])
/* Opcode decoding macros. Y() is bits 5-3 of the opcode, Z() is bits 2-0,
* P() bits 5-4, and Q() bits 4-3.
*/
#define Y(opcode) (((opcode) >> 3) & 0x07)
#define Z(opcode) ((opcode) & 0x07)
#define P(opcode) (((opcode) >> 4) & 0x03)
#define Q(opcode) (((opcode) >> 3) & 0x03)
/* Registers and conditions are decoded using tables in encodings.h. S() is
* for the special cases "LD H/L, (IX/Y + d)" and "LD (IX/Y + d), H/L".
*/
#define R(r) *((unsigned char *) (registers[(r)]))
#define S(s) *((unsigned char *) state->register_table[(s)])
#define RR(rr) *((unsigned short *) registers[(rr) + 8])
#define SS(ss) *((unsigned short *) registers[(ss) + 12])
#define CC(cc) ((F ^ XOR_CONDITION_TABLE[(cc)]) \
& AND_CONDITION_TABLE[(cc)])
#define DD(dd) CC(dd)
/* Macros to read constants, displacements, or addresses from code. */
#define READ_N(n) \
{ \
Z80_FETCH_BYTE(pc, (n)); \
pc++; \
elapsed_cycles += 3; \
}
#define READ_NN(nn) \
{ \
Z80_FETCH_WORD(pc, (nn)); \
pc += 2; \
elapsed_cycles += 6; \
}
#define READ_D(d) \
{ \
Z80_FETCH_BYTE(pc, (d)); \
(d) = (signed char) (d); \
pc++; \
elapsed_cycles += 3; \
}
/* Macros to read and write data. */
#define READ_BYTE(address, x) \
{ \
Z80_READ_BYTE((address), (x)); \
elapsed_cycles += 3; \
}
#define WRITE_BYTE(address, x) \
{ \
Z80_WRITE_BYTE((address), (x)); \
elapsed_cycles += 3; \
}
#define READ_WORD(address, x) \
{ \
Z80_READ_WORD((address), (x)); \
elapsed_cycles += 6; \
}
#define WRITE_WORD(address, x) \
{ \
Z80_WRITE_WORD((address), (x)); \
elapsed_cycles += 6; \
}
/* Indirect (HL) and indexed (IX + d) or (IY + d) memory operands read and
* write macros.
*/
#define READ_INDIRECT_HL(x) \
{ \
if (registers == state->register_table) { \
\
READ_BYTE(HL, (x)); \
\
} else { \
\
int d; \
\
READ_D(d); \
d += HL_IX_IY; \
READ_BYTE(d, (x)); \
\
elapsed_cycles += 5; \
\
} \
}
#define WRITE_INDIRECT_HL(x) \
{ \
if (registers == state->register_table) { \
\
WRITE_BYTE(HL, (x)); \
\
} else { \
\
int d; \
\
READ_D(d); \
d += HL_IX_IY; \
WRITE_BYTE(d, (x)); \
\
elapsed_cycles += 5; \
\
} \
}
/* Stack operation macros. */
#define PUSH(x) \
{ \
SP -= 2; \
WRITE_WORD(SP, (x)); \
}
#define POP(x) \
{ \
READ_WORD(SP, (x)); \
SP += 2; \
}
/* Exchange macro. */
#define EXCHANGE(a, b) \
{ \
int t; \
\
t = (a); \
(a) = (b); \
(b) = t; \
}
/* 8-bit arithmetic and logic operations. */
#define ADD(x) \
{ \
int a, z, c, f; \
\
a = A; \
z = a + (x); \
\
c = a ^ (x) ^ z; \
f = c & Z80_H_FLAG; \
f |= SZYX_FLAGS_TABLE[z & 0xff]; \
f |= OVERFLOW_TABLE[c >> 7]; \
f |= z >> (8 - Z80_C_FLAG_SHIFT); \
\
A = z; \
F = f; \
}
#define ADC(x) \
{ \
int a, z, c, f; \
\
a = A; \
z = a + (x) + (F & Z80_C_FLAG); \
\
c = a ^ (x) ^ z; \
f = c & Z80_H_FLAG; \
f |= SZYX_FLAGS_TABLE[z & 0xff]; \
f |= OVERFLOW_TABLE[c >> 7]; \
f |= z >> (8 - Z80_C_FLAG_SHIFT); \
\
A = z; \
F = f; \
}
#define SUB(x) \
{ \
int a, z, c, f; \
\
a = A; \
z = a - (x); \
\
c = a ^ (x) ^ z; \
f = Z80_N_FLAG | (c & Z80_H_FLAG); \
f |= SZYX_FLAGS_TABLE[z & 0xff]; \
c &= 0x0180; \
f |= OVERFLOW_TABLE[c >> 7]; \
f |= c >> (8 - Z80_C_FLAG_SHIFT); \
\
A = z; \
F = f; \
}
#define SBC(x) \
{ \
int a, z, c, f; \
\
a = A; \
z = a - (x) - (F & Z80_C_FLAG); \
\
c = a ^ (x) ^ z; \
f = Z80_N_FLAG | (c & Z80_H_FLAG); \
f |= SZYX_FLAGS_TABLE[z & 0xff]; \
c &= 0x0180; \
f |= OVERFLOW_TABLE[c >> 7]; \
f |= c >> (8 - Z80_C_FLAG_SHIFT); \
\
A = z; \
F = f; \
}
#define AND(x) \
{ \
F = SZYXP_FLAGS_TABLE[A &= (x)] | Z80_H_FLAG; \
}
#define OR(x) \
{ \
F = SZYXP_FLAGS_TABLE[A |= (x)]; \
}
#define XOR(x) \
{ \
F = SZYXP_FLAGS_TABLE[A ^= (x)]; \
}
#define CP(x) \
{ \
int a, z, c, f; \
\
a = A; \
z = a - (x); \
\
c = a ^ (x) ^ z; \
f = Z80_N_FLAG | (c & Z80_H_FLAG); \
f |= SZYX_FLAGS_TABLE[z & 0xff] & SZ_FLAGS; \
f |= (x) & YX_FLAGS; \
c &= 0x0180; \
f |= OVERFLOW_TABLE[c >> 7]; \
f |= c >> (8 - Z80_C_FLAG_SHIFT); \
\
F = f; \
}
#define INC(x) \
{ \
int z, c, f; \
\
z = (x) + 1; \
c = (x) ^ z; \
\
f = F & Z80_C_FLAG; \
f |= c & Z80_H_FLAG; \
f |= SZYX_FLAGS_TABLE[z & 0xff]; \
f |= OVERFLOW_TABLE[(c >> 7) & 0x03]; \
\
(x) = z; \
F = f; \
}
#define DEC(x) \
{ \
int z, c, f; \
\
z = (x) - 1; \
c = (x) ^ z; \
\
f = Z80_N_FLAG | (F & Z80_C_FLAG); \
f |= c & Z80_H_FLAG; \
f |= SZYX_FLAGS_TABLE[z & 0xff]; \
f |= OVERFLOW_TABLE[(c >> 7) & 0x03]; \
\
(x) = z; \
F = f; \
}
/* 0xcb prefixed logical operations. */
#define RLC(x) \
{ \
int c; \
\
c = (x) >> 7; \
(x) = ((x) << 1) | c; \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define RL(x) \
{ \
int c; \
\
c = (x) >> 7; \
(x) = ((x) << 1) | (F & Z80_C_FLAG); \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define RRC(x) \
{ \
int c; \
\
c = (x) & 0x01; \
(x) = ((x) >> 1) | (c << 7); \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define RR_INSTRUCTION(x) \
{ \
int c; \
\
c = (x) & 0x01; \
(x) = ((x) >> 1) | ((F & Z80_C_FLAG) << 7); \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define SLA(x) \
{ \
int c; \
\
c = (x) >> 7; \
(x) <<= 1; \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define SLL(x) \
{ \
int c; \
\
c = (x) >> 7; \
(x) = ((x) << 1) | 0x01; \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define SRA(x) \
{ \
int c; \
\
c = (x) & 0x01; \
(x) = ((signed char) (x)) >> 1; \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}
#define SRL(x) \
{ \
int c; \
\
c = (x) & 0x01; \
(x) >>= 1; \
F = SZYXP_FLAGS_TABLE[(x) & 0xff] | c; \
}