-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZ80macros.h
505 lines (400 loc) · 11.6 KB
/
Z80macros.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* Copyright (c) Günter Woigk 1996 - 2019
mailto:[email protected]
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of the copyright holder not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission. The copyright holder makes no
representations about the suitability of this software for any purpose.
It is provided "as is" without express or implied warranty.
THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
Z80 Emulator
originally based on fMSX; Copyright (C) Marat Fayzullin 1994,1995
macro definitions
*/
// access z80 registers:
#define IFF1 registers.iff1 // actually used irpt flip flop
#define IFF2 registers.iff2 // copy of iff1 during nmi processing
#define RR registers.r // 7 bit DRAM refresh counter
#define RI registers.i // hi byte of interrupt vector: i register
#define RA registers.a
#define RF registers.f
#define RB registers.b
#define RC registers.c
#define RD registers.d
#define RE registers.e
#define RH registers.h
#define RL registers.l
#define RA2 registers.a2
#define RF2 registers.f2
#define XH registers.xh
#define XL registers.xl
#define YH registers.yh
#define YL registers.yl
#define PCH registers.pch
#define PCL registers.pcl
#define SPH registers.sph
#define SPL registers.spl
#define BC registers.bc
#define DE registers.de
#define HL registers.hl
#define BC2 registers.bc2
#define DE2 registers.de2
#define HL2 registers.hl2
#define IX registers.ix
#define IY registers.iy
#define PC registers.pc
#define SP registers.sp
// ===================================
// default implementations for macros:
// ===================================
// read/write data:
// increment cpu cycle counter
#ifndef INCR_CC
#define INCR_CC(N) cc += (N)
#endif
// increment refresh register
#ifndef INCR_R
#define INCR_R() r += 1
#endif
// increment instruction counter
#ifndef INCR_IC
#define INCR_IC() /*nop*/
#endif
// read byte from memory
#ifndef PEEK
#define PEEK(DEST,ADDR) { INCR_CC(3); DEST = peek(ADDR); }
#endif
// write byte into memory
#ifndef POKE
#define POKE(ADDR,BYTE) { INCR_CC(3); poke(ADDR,BYTE); }
#endif
// read instruction byte at PC (M1 cycle)
#ifndef GET_INSTR
#define GET_INSTR(R) { INCR_CC(4); INCR_R(); INCR_IC(); R = peek(pc++); }
#endif
// read 2nd instruction byte after 0xCB opcode
#ifndef GET_CB_OP
#define GET_CB_OP(R) { INCR_CC(4); INCR_R(); R = peek(pc++); }
#endif
// read 2nd instruction byte after 0xED opcode
#ifndef GET_ED_OP
#define GET_ED_OP(R) { INCR_CC(4); INCR_R(); R = peek(pc++); }
#endif
// read 2nd instruction byte after IX or IY opcode prefix
#ifndef GET_XY_OP
#define GET_XY_OP(R) { INCR_CC(4); INCR_R(); R = peek(pc++); }
#endif
// read 3rd instruction byte after IX or IY prefix and 0xCB opcode
#ifndef GET_XYCB_OP
#define GET_XYCB_OP(R) { INCR_CC(5); R = peek(pc++); }
#endif
// read byte at PC
#ifndef GET_N
#define GET_N(R) { INCR_CC(3); R = peek(pc++); }
#endif
// dummy read byte at PC
#ifndef SKIP_N
#define SKIP_N() { INCR_CC(3); peek(pc++); }
#endif
// increment cpu cycle counter
#ifndef SKIP_1CC
#define SKIP_1CC(RR) { INCR_CC(1); }
#endif
#ifndef SKIP_2X1CC
#define SKIP_2X1CC(RR) { INCR_CC(2); }
#endif
#ifndef SKIP_4X1CC
#define SKIP_4X1CC(RR) { INCR_CC(4); }
#endif
#ifndef SKIP_5X1CC
#define SKIP_5X1CC(RR) { INCR_CC(5); }
#endif
#ifndef SKIP_7X1CC
#define SKIP_7X1CC(RR) { INCR_CC(7); }
#endif
// output byte to address
// this calls the Z80 member function handle_output(cc,addr,byte)
// the default implementation of this function iterates over all other Items 'behind' the Z80.
// #define Z80_NO_handle_output in Z80options.h to use your own implementation.
#ifndef OUTPUT
#define OUTPUT(ADDR,BYTE) { INCR_CC(4); this->handle_output(cc-2,ADDR,BYTE); }
#endif
// input byte from address
// this calls the Z80 member function handle_input(cc,addr)
// the default implementation of this function iterates over all other Items 'behind' the Z80.
// #define Z80_NO_handle_input in Z80options.h to use your own implementation.
#ifndef INPUT
#define INPUT(ADDR,DEST) { INCR_CC(4); DEST = this->handle_input(cc-2,ADDR); }
#endif
// call each item's update(cc) member function.
// this calls the Z80 member function handle_update(cc).
// the default implementation of this function iterates over all other Items 'behind' the Z80.
// #define Z80_NO_handle_update in Z80options.h to use your own implementation.
#ifndef UPDATE
#define UPDATE() { cc_next_update = this->handle_update(cc, cc_exit); }
#endif
// =====================================
// call-backs at some intersting points:
// =====================================
// Z80 constructor
#ifndef Z80_INFO_CTOR
#define Z80_INFO_CTOR /* nop */
#endif
// Z80 destructor
#ifndef Z80_INFO_DTOR
#define Z80_INFO_DTOR /* nop */
#endif
// processing interrupt:
#ifndef Z80_INFO_IRPT /* cpu cycle of irpt ack is cc-2 */
#define Z80_INFO_IRPT() /* nop */
#endif
// processing NMI:
#ifndef Z80_INFO_NMI /* cpu cycle of nmi ack is cc-2 */
#define Z80_INFO_NMI() /* nop */
#endif
// execute RETI
#ifndef Z80_INFO_RETI
#define Z80_INFO_RETI /* nop */
#endif
// execute RETN
#ifndef Z80_INFO_RETN
#define Z80_INFO_RETN /* nop */
#endif
// execute HALT
#ifndef Z80_INFO_HALT
#define Z80_INFO_HALT /* nop */
#endif
// executing an illegal instruction:
#ifndef Z80_INFO_ILLEGAL
#define Z80_INFO_ILLEGAL(CC,PC) /* nop */
#endif
// pop value from stack
// probably needed for a single stepper / debugger
#ifndef Z80_INFO_POP
#define Z80_INFO_POP /* nop */
#endif
// execute return opcode
// probably needed for a single stepper / debugger
#ifndef Z80_INFO_RET
#define Z80_INFO_RET /* nop */
#endif
// excute EX HL,(SP)
// probably needed for a single stepper / debugger
#ifndef Z80_INFO_EX_HL_xSP
#define Z80_INFO_EX_HL_xSP /* nop */
#endif
// execute RST 0 opdode
#ifndef Z80_INFO_RST00
#define Z80_INFO_RST00 /* nop */
#endif
// execute RST 8 opdode
#ifndef Z80_INFO_RST08
#define Z80_INFO_RST08 /* nop */
#endif
// execute RST 0x10 opdode
#ifndef Z80_INFO_RST10
#define Z80_INFO_RST10 /* nop */
#endif
// execute RST 0x18 opdode
#ifndef Z80_INFO_RST18
#define Z80_INFO_RST18 /* nop */
#endif
// execute RST 0x20 opdode
#ifndef Z80_INFO_RST20
#define Z80_INFO_RST20 /* nop */
#endif
// execute RST 0x28 opdode
#ifndef Z80_INFO_RST28
#define Z80_INFO_RST28 /* nop */
#endif
// execute RST 0x30 opdode
#ifndef Z80_INFO_RST30
#define Z80_INFO_RST30 /* nop */
#endif
// execute RST 0x38 opdode
#ifndef Z80_INFO_RST38
#define Z80_INFO_RST38 /* nop */
#endif
// execute EI opcode
#ifndef Z80_INFO_EI
#define Z80_INFO_EI /* nop */
#endif
// execute LD R,A opcode
#ifndef Z80_INFO_LD_R_A
#define Z80_INFO_LD_R_A /* nop */
#endif
// execute LD I,A opcode
#ifndef Z80_INFO_LD_I_A
#define Z80_INFO_LD_I_A /* nop */
#endif
// --------------------------------------------------------------------
// ---- INSTRUCTION MACROS --------------------------------------------
// no user serviceable parts inside.
// --------------------------------------------------------------------
#define GET_NN(RR) { GET_N(RR); GET_N(wm); RR += 256*wm; }
#define POP(R) { PEEK(R,SP); SP++; }
#define PUSH(R) { --SP; POKE(SP,R); }
/* RLC ... SRL: set/clr C, Z, P, S;
clear N=0, H=0
pres. none
*/
#define M_RLC(R) \
rf = R>>7; \
R = (R<<1)+rf; \
rf |= zlog_flags[R]
#define M_RRC(R) \
rf = R&0x01; \
R = (R>>1)+(rf<<7); \
rf |= zlog_flags[R]
#define M_RL(R) \
if (R&0x80) \
{ R = (R<<1)+(rf&0x01); \
rf = zlog_flags[R]+C_FLAG; \
} else \
{ R = (R<<1)+(rf&0x01); \
rf = zlog_flags[R]; \
}
#define M_RR(R) \
if (R&0x01) \
{ R = (R>>1)+(rf<<7); \
rf = zlog_flags[R]+C_FLAG; \
} else \
{ R = (R>>1)+(rf<<7); \
rf = zlog_flags[R]; \
}
#define M_SLA(R) \
rf = R>>7; \
R <<= 1; \
rf |= zlog_flags[R]
#define M_SRA(R) \
rf = R&0x01; \
R = (R&0x80)+(R>>1); \
rf |= zlog_flags[R]
#define M_SLL(R) \
rf = R>>7; \
R = (R<<1)+1; \
rf |= zlog_flags[R]
#define M_SRL(R) \
rf = R&0x01; \
R >>= 1; \
rf |= zlog_flags[R]
/* BIT: set/clr Z
clear N=0, H=1
pres C
takes other flags from corresponding bits in tested byte!
*/
#define M_BIT(N,R) \
rf = (rf&C_FLAG) + \
(R&(S_FLAG+P_FLAG)) + \
H_FLAG + \
((R&N)?0:Z_FLAG)
/* ADD ... CP: set/clr Z, S, V, C, N, H
pres none
*/
#define M_ADD(R) \
wm = ra+R; \
rf = wmh + (wml?0:Z_FLAG) + (wml&S_FLAG) \
+ (~(ra^R)&(wml^ra)&0x80?V_FLAG:0) \
+ ((ra^R^wml)&H_FLAG); \
ra = wml
#define M_SUB(R) \
wm = ra-R; \
rf = -wmh + (wml?0:Z_FLAG) + (wml&S_FLAG) \
+ ((ra^R)&(wml^ra)&0x80?V_FLAG:0) \
+ ((ra^R^wml)&H_FLAG) + N_FLAG; \
ra = wml
#define M_ADC(R) \
wm = ra+R+(rf&C_FLAG); \
rf = wmh + (wml?0:Z_FLAG) + (wml&S_FLAG) \
+ (~(ra^R)&(wml^ra)&0x80?V_FLAG:0) \
+ ((ra^R^wml)&H_FLAG); \
ra = wml
#define M_SBC(R) \
wm = ra-R-(rf&C_FLAG); \
rf = -wmh + (wml?0:Z_FLAG) + (wml&S_FLAG) \
+ ((ra^R)&(wml^ra)&0x80?V_FLAG:0) \
+ ((ra^R^wml)&H_FLAG) + N_FLAG; \
ra = wml
#define M_CP(R) \
wm = ra-R; \
rf = -wmh + (wml?0:Z_FLAG) + (wml&S_FLAG) \
+ ((ra^R)&(wml^ra)&0x80?V_FLAG:0) \
+ ((ra^R^wml)&H_FLAG) + N_FLAG;
/* AND ... XOR: set/clr Z, P, S
clear C=0, N=0, H=0/1 (OR,XOR/AND)
pres none
*/
#define M_AND(R) \
ra &= R; \
rf = H_FLAG|zlog_flags[ra]
#define M_OR(R) \
ra |= R; \
rf = zlog_flags[ra]
#define M_XOR(R) \
ra ^= R; \
rf = zlog_flags[ra]
/* INC ... DEC: set/clr Z,P,S,H
clear N=0/1 (INC/DEC)
pres C
*/
#define M_INC(R) \
R++; \
rf = (rf&C_FLAG) + \
(R?0:Z_FLAG) + \
(R&S_FLAG) + \
(R==0x80?V_FLAG:0) + \
(R&0x0F?0:H_FLAG)
#define M_DEC(R) \
R--; \
rf = (rf&C_FLAG) + \
(R?0:Z_FLAG) + \
(R&S_FLAG) + \
(R==0x7F?V_FLAG:0) + \
(((R+1)&0x0F)?0:H_FLAG) + \
N_FLAG
/* ADDW: set/clr C
clear N=0
pres Z, P, S
unkn H
*/
#define M_ADDW(R1,R2) \
rf &= ~(N_FLAG+C_FLAG); \
rf |= ((uint32)R1+(uint32)R2)>>16; \
R1 += R2;
/* ADCW, SBCW: set/clr C,Z,V,S
clear N=0/1 (ADC/SBC)
unkn H
pres none
*/
#define M_ADCW(R) \
wm = HL+R+(rf&C_FLAG); \
rf = (((uint32)HL+(uint32)R+(rf&C_FLAG))>>16)\
+ (wm?0:Z_FLAG) + (wmh&S_FLAG) \
+ (~(HL^R)&(wm^HL)&0x8000?V_FLAG:0);\
HL = wm
#define M_SBCW(R) \
wm = HL-R-(rf&C_FLAG); \
rf = (((uint32)HL-(uint32)R-(rf&C_FLAG))>>31)\
+ (wm?0:Z_FLAG) + (wmh&S_FLAG) \
+ ((HL^R)&(wm^HL)&0x8000?V_FLAG:0) \
+ N_FLAG; \
HL = wm
/* IN set/clr Z, P, S, H
clear N=0
pres C
*/
#define M_IN(R) \
INPUT(BC,R); \
rf = (rf&C_FLAG) + zlog_flags[R]