-
Notifications
You must be signed in to change notification settings - Fork 3
/
z80.c
4373 lines (4010 loc) · 194 KB
/
z80.c
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* z80.c
* Portable Z80 emulator V2.7
*
* Copyright (C) 1998,1999,2000 Juergen Buchmueller, all rights reserved.
*
* - This source code is released as freeware for non-commercial purposes.
* - You are free to use and redistribute this code in modified or
* unmodified form, provided you list me in the credits.
* - If you modify this source code, you must add a notice to each modified
* source file that it has been changed. If you're a nice person, you
* will clearly mark each change too. :)
* - If you wish to use this for commercial purposes, please contact me at
* - The author of this copywritten work reserves the right to change the
* terms of its usage and license at any time, including retroactively
* - This entire notice must remain in the source code.
*
* Changes in 2.7:
* - removed z80_vm specific code, it's not needed (and never was).
* Changes in 2.6:
* - BUSY_LOOP_HACKS needed to call change_pc16() earlier, before
* checking the opcodes at the new address, because otherwise they
* might access the old (wrong or even NULL) banked memory region.
* Thanks to Sean Young for finding this nasty bug.
* Changes in 2.5:
* - Burning cycles always adjusts the ICount by a multiple of 4.
* - In REPEAT_AT_ONCE cases the R register wasn't incremented twice
* per repetition as it should have been. Those repeated opcodes
* could also underflow the ICount.
* - Simplified TIME_LOOP_HACKS for BC and added two more for DE + HL
* timing loops. I think those hacks weren't endian safe before too.
* Changes in 2.4:
* - z80_reset zaps the entire context, sets IX and IY to 0xffff(!) and
* sets the Z flag. With these changes the Tehkan World Cup driver
* _seems_ to work again.
* Changes in 2.3:
* - External termination of the execution loop calls z80_burn() and
* z80_vm_burn() to burn an amount of cycles (R adjustment)
* - Shortcuts which burn CPU cycles (BUSY_LOOP_HACKS and TIME_LOOP_HACKS)
* now also adjust the R register depending on the skipped opcodes.
* Changes in 2.2:
* - Fixed bugs in CPL, SCF and CCF instructions flag handling.
* - Changed variable EA and ARG16() function to UINT32; this
* produces slightly more efficient code.
* - The DD/FD XY CB opcodes where XY is 40-7F and Y is not 6/E
* are changed to calls to the X6/XE opcodes to reduce object size.
* They're hardly ever used so this should not yield a speed penalty.
* New in 2.0:
* - Optional more exact Z80 emulation (#define Z80_EXACT 1) according
* to a detailed description by Sean Young which can be found at:
* http://www.msxnet.org/tech/Z80/z80undoc.txt
*****************************************************************************/
#include "cpuintrf.h"
#include "z80.h"
#include "shared.h"
extern void cpu_writemem16(int address, int data);
extern void cpu_writeport(int port, int data);
extern int cpu_readport(int port);
unsigned char *cpu_readmap[8];
unsigned char *cpu_writemap[8];
#define cpu_readmem16(a) cpu_readmap[(a) >> 13][(a) & 0x1FFF]
#define cpu_readop(a) cpu_readmap[(a) >> 13][(a) & 0x1FFF]
#define cpu_readop_arg(a) cpu_readmap[(a) >> 13][(a) & 0x1FFF]
/* execute main opcodes inside a big switch statement */
#ifndef BIG_SWITCH
#define BIG_SWITCH 1
#endif
/* big flags array for ADD/ADC/SUB/SBC/CP results */
#define BIG_FLAGS_ARRAY 1
/* Set to 1 for a more exact (but somewhat slower) Z80 emulation */
#define Z80_EXACT 1
/* repetitive commands (ldir,cpdr etc.) repeat at
once until cycles used up or B(C) counted down. */
#define REPEAT_AT_ONCE 1
/* on JP and JR opcodes check for tight loops */
#define BUSY_LOOP_HACKS 1
/* check for delay loops counting down BC */
#define TIME_LOOP_HACKS 1
#undef BIG_FLAGS_ARRAY
#define BIG_FLAGS_ARRAY 0
#define CF 0x01
#define NF 0x02
#define PF 0x04
#define VF PF
#define XF 0x08
#define HF 0x10
#define YF 0x20
#define ZF 0x40
#define SF 0x80
#define INT_IRQ 0x01
#define NMI_IRQ 0x02
#define _PPC Z80.PREPC.d /* previous program counter */
#define _PCD Z80.PC.d
#define _PC Z80.PC.w.l
#define _SPD Z80.SP.d
#define _SP Z80.SP.w.l
#define _AFD Z80.AF.d
#define _AF Z80.AF.w.l
#define _A Z80.AF.b.h
#define _F Z80.AF.b.l
#define _BCD Z80.BC.d
#define _BC Z80.BC.w.l
#define _B Z80.BC.b.h
#define _C Z80.BC.b.l
#define _DED Z80.DE.d
#define _DE Z80.DE.w.l
#define _D Z80.DE.b.h
#define _E Z80.DE.b.l
#define _HLD Z80.HL.d
#define _HL Z80.HL.w.l
#define _H Z80.HL.b.h
#define _L Z80.HL.b.l
#define _IXD Z80.IX.d
#define _IX Z80.IX.w.l
#define _HX Z80.IX.b.h
#define _LX Z80.IX.b.l
#define _IYD Z80.IY.d
#define _IY Z80.IY.w.l
#define _HY Z80.IY.b.h
#define _LY Z80.IY.b.l
#define _I Z80.I
#define _R Z80.R
#define _R2 Z80.R2
#define _IM Z80.IM
#define _IFF1 Z80.IFF1
#define _IFF2 Z80.IFF2
#define _HALT Z80.HALT
int z80_ICount;
static Z80_Regs Z80;
Z80_Regs *Z80_Context = &Z80;
static UINT32 EA;
int after_EI = 0;
static UINT8 SZ[256]; /* zero and sign flags */
static UINT8 SZ_BIT[256]; /* zero, sign and parity/overflow (=zero) flags for BIT opcode */
static UINT8 SZP[256]; /* zero, sign and parity flags */
static UINT8 SZHV_inc[256]; /* zero, sign, half carry and overflow flags INC r8 */
static UINT8 SZHV_dec[256]; /* zero, sign, half carry and overflow flags DEC r8 */
#include "z80daa.h"
#if BIG_FLAGS_ARRAY
//#include <signal.h>
static UINT8 *SZHVC_add = 0;
static UINT8 *SZHVC_sub = 0;
#endif
#if Z80_EXACT
/* tmp1 value for ini/inir/outi/otir for [C.1-0][io.1-0] */
static UINT8 irep_tmp1[4][4] = {
{0,0,1,0},{0,1,0,1},{1,0,1,1},{0,1,1,0}
};
/* tmp1 value for ind/indr/outd/otdr for [C.1-0][io.1-0] */
static UINT8 drep_tmp1[4][4] = {
{0,1,0,0},{1,0,0,1},{0,0,1,0},{0,1,0,1}
};
/* tmp2 value for all in/out repeated opcodes for B.7-0 */
static UINT8 breg_tmp2[256] = {
0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,
1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1
};
#endif
static UINT8 cc_op[0x100] = {
4,10, 7, 6, 4, 4, 7, 4, 4,11, 7, 6, 4, 4, 7, 4,
8,10, 7, 6, 4, 4, 7, 4,12,11, 7, 6, 4, 4, 7, 4,
7,10,16, 6, 4, 4, 7, 4, 7,11,16, 6, 4, 4, 7, 4,
7,10,13, 6,11,11,10, 4, 7,11,13, 6, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
7, 7, 7, 7, 7, 7, 4, 7, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
5,10,10,10,10,11, 7,11, 5, 4,10, 0,10,10, 7,11,
5,10,10,11,10,11, 7,11, 5, 4,10,11,10, 0, 7,11,
5,10,10,19,10,11, 7,11, 5, 4,10, 4,10, 0, 7,11,
5,10,10, 4,10,11, 7,11, 5, 6,10, 4,10, 0, 7,11};
static UINT8 cc_cb[0x100] = {
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8};
static UINT8 cc_dd[0x100] = {
4, 4, 4, 4, 4, 4, 4, 4, 4,15, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4,15, 4, 4, 4, 4, 4, 4,
4,14,20,10, 9, 9, 9, 4, 4,15,20,10, 9, 9, 9, 4,
4, 4, 4, 4,23,23,19, 4, 4,15, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
9, 9, 9, 9, 9, 9,19, 9, 9, 9, 9, 9, 9, 9,19, 9,
19,19,19,19,19,19, 4,19, 4, 4, 4, 4, 9, 9,19, 4,
4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4,14, 4,23, 4,15, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4,10, 4, 4, 4, 4, 4, 4};
// dd/fd cycles are identical
#define cc_fd cc_dd
static UINT8 cc_xxcb[0x100] = {
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23};
static UINT8 cc_ed[0x100] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
12,12,15,20, 8, 8, 8, 9,12,12,15,20, 8, 8, 8, 9,
12,12,15,20, 8, 8, 8, 9,12,12,15,20, 8, 8, 8, 9,
12,12,15,20, 8, 8, 8,18,12,12,15,20, 8, 8, 8,18,
12,12,15,20, 8, 8, 8, 8,12,12,15,20, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
16,16,16,16, 8, 8, 8, 8,16,16,16,16, 8, 8, 8, 8,
16,16,16,16, 8, 8, 8, 8,16,16,16,16, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
static void take_interrupt(void);
#define PROTOTYPES(tablename,prefix) \
static __inline__ void prefix##_00(void); static __inline__ void prefix##_01(void); static __inline__ void prefix##_02(void); static __inline__ void prefix##_03(void); \
static __inline__ void prefix##_04(void); static __inline__ void prefix##_05(void); static __inline__ void prefix##_06(void); static __inline__ void prefix##_07(void); \
static __inline__ void prefix##_08(void); static __inline__ void prefix##_09(void); static __inline__ void prefix##_0a(void); static __inline__ void prefix##_0b(void); \
static __inline__ void prefix##_0c(void); static __inline__ void prefix##_0d(void); static __inline__ void prefix##_0e(void); static __inline__ void prefix##_0f(void); \
static __inline__ void prefix##_10(void); static __inline__ void prefix##_11(void); static __inline__ void prefix##_12(void); static __inline__ void prefix##_13(void); \
static __inline__ void prefix##_14(void); static __inline__ void prefix##_15(void); static __inline__ void prefix##_16(void); static __inline__ void prefix##_17(void); \
static __inline__ void prefix##_18(void); static __inline__ void prefix##_19(void); static __inline__ void prefix##_1a(void); static __inline__ void prefix##_1b(void); \
static __inline__ void prefix##_1c(void); static __inline__ void prefix##_1d(void); static __inline__ void prefix##_1e(void); static __inline__ void prefix##_1f(void); \
static __inline__ void prefix##_20(void); static __inline__ void prefix##_21(void); static __inline__ void prefix##_22(void); static __inline__ void prefix##_23(void); \
static __inline__ void prefix##_24(void); static __inline__ void prefix##_25(void); static __inline__ void prefix##_26(void); static __inline__ void prefix##_27(void); \
static __inline__ void prefix##_28(void); static __inline__ void prefix##_29(void); static __inline__ void prefix##_2a(void); static __inline__ void prefix##_2b(void); \
static __inline__ void prefix##_2c(void); static __inline__ void prefix##_2d(void); static __inline__ void prefix##_2e(void); static __inline__ void prefix##_2f(void); \
static __inline__ void prefix##_30(void); static __inline__ void prefix##_31(void); static __inline__ void prefix##_32(void); static __inline__ void prefix##_33(void); \
static __inline__ void prefix##_34(void); static __inline__ void prefix##_35(void); static __inline__ void prefix##_36(void); static __inline__ void prefix##_37(void); \
static __inline__ void prefix##_38(void); static __inline__ void prefix##_39(void); static __inline__ void prefix##_3a(void); static __inline__ void prefix##_3b(void); \
static __inline__ void prefix##_3c(void); static __inline__ void prefix##_3d(void); static __inline__ void prefix##_3e(void); static __inline__ void prefix##_3f(void); \
static __inline__ void prefix##_40(void); static __inline__ void prefix##_41(void); static __inline__ void prefix##_42(void); static __inline__ void prefix##_43(void); \
static __inline__ void prefix##_44(void); static __inline__ void prefix##_45(void); static __inline__ void prefix##_46(void); static __inline__ void prefix##_47(void); \
static __inline__ void prefix##_48(void); static __inline__ void prefix##_49(void); static __inline__ void prefix##_4a(void); static __inline__ void prefix##_4b(void); \
static __inline__ void prefix##_4c(void); static __inline__ void prefix##_4d(void); static __inline__ void prefix##_4e(void); static __inline__ void prefix##_4f(void); \
static __inline__ void prefix##_50(void); static __inline__ void prefix##_51(void); static __inline__ void prefix##_52(void); static __inline__ void prefix##_53(void); \
static __inline__ void prefix##_54(void); static __inline__ void prefix##_55(void); static __inline__ void prefix##_56(void); static __inline__ void prefix##_57(void); \
static __inline__ void prefix##_58(void); static __inline__ void prefix##_59(void); static __inline__ void prefix##_5a(void); static __inline__ void prefix##_5b(void); \
static __inline__ void prefix##_5c(void); static __inline__ void prefix##_5d(void); static __inline__ void prefix##_5e(void); static __inline__ void prefix##_5f(void); \
static __inline__ void prefix##_60(void); static __inline__ void prefix##_61(void); static __inline__ void prefix##_62(void); static __inline__ void prefix##_63(void); \
static __inline__ void prefix##_64(void); static __inline__ void prefix##_65(void); static __inline__ void prefix##_66(void); static __inline__ void prefix##_67(void); \
static __inline__ void prefix##_68(void); static __inline__ void prefix##_69(void); static __inline__ void prefix##_6a(void); static __inline__ void prefix##_6b(void); \
static __inline__ void prefix##_6c(void); static __inline__ void prefix##_6d(void); static __inline__ void prefix##_6e(void); static __inline__ void prefix##_6f(void); \
static __inline__ void prefix##_70(void); static __inline__ void prefix##_71(void); static __inline__ void prefix##_72(void); static __inline__ void prefix##_73(void); \
static __inline__ void prefix##_74(void); static __inline__ void prefix##_75(void); static __inline__ void prefix##_76(void); static __inline__ void prefix##_77(void); \
static __inline__ void prefix##_78(void); static __inline__ void prefix##_79(void); static __inline__ void prefix##_7a(void); static __inline__ void prefix##_7b(void); \
static __inline__ void prefix##_7c(void); static __inline__ void prefix##_7d(void); static __inline__ void prefix##_7e(void); static __inline__ void prefix##_7f(void); \
static __inline__ void prefix##_80(void); static __inline__ void prefix##_81(void); static __inline__ void prefix##_82(void); static __inline__ void prefix##_83(void); \
static __inline__ void prefix##_84(void); static __inline__ void prefix##_85(void); static __inline__ void prefix##_86(void); static __inline__ void prefix##_87(void); \
static __inline__ void prefix##_88(void); static __inline__ void prefix##_89(void); static __inline__ void prefix##_8a(void); static __inline__ void prefix##_8b(void); \
static __inline__ void prefix##_8c(void); static __inline__ void prefix##_8d(void); static __inline__ void prefix##_8e(void); static __inline__ void prefix##_8f(void); \
static __inline__ void prefix##_90(void); static __inline__ void prefix##_91(void); static __inline__ void prefix##_92(void); static __inline__ void prefix##_93(void); \
static __inline__ void prefix##_94(void); static __inline__ void prefix##_95(void); static __inline__ void prefix##_96(void); static __inline__ void prefix##_97(void); \
static __inline__ void prefix##_98(void); static __inline__ void prefix##_99(void); static __inline__ void prefix##_9a(void); static __inline__ void prefix##_9b(void); \
static __inline__ void prefix##_9c(void); static __inline__ void prefix##_9d(void); static __inline__ void prefix##_9e(void); static __inline__ void prefix##_9f(void); \
static __inline__ void prefix##_a0(void); static __inline__ void prefix##_a1(void); static __inline__ void prefix##_a2(void); static __inline__ void prefix##_a3(void); \
static __inline__ void prefix##_a4(void); static __inline__ void prefix##_a5(void); static __inline__ void prefix##_a6(void); static __inline__ void prefix##_a7(void); \
static __inline__ void prefix##_a8(void); static __inline__ void prefix##_a9(void); static __inline__ void prefix##_aa(void); static __inline__ void prefix##_ab(void); \
static __inline__ void prefix##_ac(void); static __inline__ void prefix##_ad(void); static __inline__ void prefix##_ae(void); static __inline__ void prefix##_af(void); \
static __inline__ void prefix##_b0(void); static __inline__ void prefix##_b1(void); static __inline__ void prefix##_b2(void); static __inline__ void prefix##_b3(void); \
static __inline__ void prefix##_b4(void); static __inline__ void prefix##_b5(void); static __inline__ void prefix##_b6(void); static __inline__ void prefix##_b7(void); \
static __inline__ void prefix##_b8(void); static __inline__ void prefix##_b9(void); static __inline__ void prefix##_ba(void); static __inline__ void prefix##_bb(void); \
static __inline__ void prefix##_bc(void); static __inline__ void prefix##_bd(void); static __inline__ void prefix##_be(void); static __inline__ void prefix##_bf(void); \
static __inline__ void prefix##_c0(void); static __inline__ void prefix##_c1(void); static __inline__ void prefix##_c2(void); static __inline__ void prefix##_c3(void); \
static __inline__ void prefix##_c4(void); static __inline__ void prefix##_c5(void); static __inline__ void prefix##_c6(void); static __inline__ void prefix##_c7(void); \
static __inline__ void prefix##_c8(void); static __inline__ void prefix##_c9(void); static __inline__ void prefix##_ca(void); static __inline__ void prefix##_cb(void); \
static __inline__ void prefix##_cc(void); static __inline__ void prefix##_cd(void); static __inline__ void prefix##_ce(void); static __inline__ void prefix##_cf(void); \
static __inline__ void prefix##_d0(void); static __inline__ void prefix##_d1(void); static __inline__ void prefix##_d2(void); static __inline__ void prefix##_d3(void); \
static __inline__ void prefix##_d4(void); static __inline__ void prefix##_d5(void); static __inline__ void prefix##_d6(void); static __inline__ void prefix##_d7(void); \
static __inline__ void prefix##_d8(void); static __inline__ void prefix##_d9(void); static __inline__ void prefix##_da(void); static __inline__ void prefix##_db(void); \
static __inline__ void prefix##_dc(void); static __inline__ void prefix##_dd(void); static __inline__ void prefix##_de(void); static __inline__ void prefix##_df(void); \
static __inline__ void prefix##_e0(void); static __inline__ void prefix##_e1(void); static __inline__ void prefix##_e2(void); static __inline__ void prefix##_e3(void); \
static __inline__ void prefix##_e4(void); static __inline__ void prefix##_e5(void); static __inline__ void prefix##_e6(void); static __inline__ void prefix##_e7(void); \
static __inline__ void prefix##_e8(void); static __inline__ void prefix##_e9(void); static __inline__ void prefix##_ea(void); static __inline__ void prefix##_eb(void); \
static __inline__ void prefix##_ec(void); static __inline__ void prefix##_ed(void); static __inline__ void prefix##_ee(void); static __inline__ void prefix##_ef(void); \
static __inline__ void prefix##_f0(void); static __inline__ void prefix##_f1(void); static __inline__ void prefix##_f2(void); static __inline__ void prefix##_f3(void); \
static __inline__ void prefix##_f4(void); static __inline__ void prefix##_f5(void); static __inline__ void prefix##_f6(void); static __inline__ void prefix##_f7(void); \
static __inline__ void prefix##_f8(void); static __inline__ void prefix##_f9(void); static __inline__ void prefix##_fa(void); static __inline__ void prefix##_fb(void); \
static __inline__ void prefix##_fc(void); static __inline__ void prefix##_fd(void); static __inline__ void prefix##_fe(void); static __inline__ void prefix##_ff(void); \
static void (*tablename[0x100])(void) = { \
prefix##_00,prefix##_01,prefix##_02,prefix##_03,prefix##_04,prefix##_05,prefix##_06,prefix##_07, \
prefix##_08,prefix##_09,prefix##_0a,prefix##_0b,prefix##_0c,prefix##_0d,prefix##_0e,prefix##_0f, \
prefix##_10,prefix##_11,prefix##_12,prefix##_13,prefix##_14,prefix##_15,prefix##_16,prefix##_17, \
prefix##_18,prefix##_19,prefix##_1a,prefix##_1b,prefix##_1c,prefix##_1d,prefix##_1e,prefix##_1f, \
prefix##_20,prefix##_21,prefix##_22,prefix##_23,prefix##_24,prefix##_25,prefix##_26,prefix##_27, \
prefix##_28,prefix##_29,prefix##_2a,prefix##_2b,prefix##_2c,prefix##_2d,prefix##_2e,prefix##_2f, \
prefix##_30,prefix##_31,prefix##_32,prefix##_33,prefix##_34,prefix##_35,prefix##_36,prefix##_37, \
prefix##_38,prefix##_39,prefix##_3a,prefix##_3b,prefix##_3c,prefix##_3d,prefix##_3e,prefix##_3f, \
prefix##_40,prefix##_41,prefix##_42,prefix##_43,prefix##_44,prefix##_45,prefix##_46,prefix##_47, \
prefix##_48,prefix##_49,prefix##_4a,prefix##_4b,prefix##_4c,prefix##_4d,prefix##_4e,prefix##_4f, \
prefix##_50,prefix##_51,prefix##_52,prefix##_53,prefix##_54,prefix##_55,prefix##_56,prefix##_57, \
prefix##_58,prefix##_59,prefix##_5a,prefix##_5b,prefix##_5c,prefix##_5d,prefix##_5e,prefix##_5f, \
prefix##_60,prefix##_61,prefix##_62,prefix##_63,prefix##_64,prefix##_65,prefix##_66,prefix##_67, \
prefix##_68,prefix##_69,prefix##_6a,prefix##_6b,prefix##_6c,prefix##_6d,prefix##_6e,prefix##_6f, \
prefix##_70,prefix##_71,prefix##_72,prefix##_73,prefix##_74,prefix##_75,prefix##_76,prefix##_77, \
prefix##_78,prefix##_79,prefix##_7a,prefix##_7b,prefix##_7c,prefix##_7d,prefix##_7e,prefix##_7f, \
prefix##_80,prefix##_81,prefix##_82,prefix##_83,prefix##_84,prefix##_85,prefix##_86,prefix##_87, \
prefix##_88,prefix##_89,prefix##_8a,prefix##_8b,prefix##_8c,prefix##_8d,prefix##_8e,prefix##_8f, \
prefix##_90,prefix##_91,prefix##_92,prefix##_93,prefix##_94,prefix##_95,prefix##_96,prefix##_97, \
prefix##_98,prefix##_99,prefix##_9a,prefix##_9b,prefix##_9c,prefix##_9d,prefix##_9e,prefix##_9f, \
prefix##_a0,prefix##_a1,prefix##_a2,prefix##_a3,prefix##_a4,prefix##_a5,prefix##_a6,prefix##_a7, \
prefix##_a8,prefix##_a9,prefix##_aa,prefix##_ab,prefix##_ac,prefix##_ad,prefix##_ae,prefix##_af, \
prefix##_b0,prefix##_b1,prefix##_b2,prefix##_b3,prefix##_b4,prefix##_b5,prefix##_b6,prefix##_b7, \
prefix##_b8,prefix##_b9,prefix##_ba,prefix##_bb,prefix##_bc,prefix##_bd,prefix##_be,prefix##_bf, \
prefix##_c0,prefix##_c1,prefix##_c2,prefix##_c3,prefix##_c4,prefix##_c5,prefix##_c6,prefix##_c7, \
prefix##_c8,prefix##_c9,prefix##_ca,prefix##_cb,prefix##_cc,prefix##_cd,prefix##_ce,prefix##_cf, \
prefix##_d0,prefix##_d1,prefix##_d2,prefix##_d3,prefix##_d4,prefix##_d5,prefix##_d6,prefix##_d7, \
prefix##_d8,prefix##_d9,prefix##_da,prefix##_db,prefix##_dc,prefix##_dd,prefix##_de,prefix##_df, \
prefix##_e0,prefix##_e1,prefix##_e2,prefix##_e3,prefix##_e4,prefix##_e5,prefix##_e6,prefix##_e7, \
prefix##_e8,prefix##_e9,prefix##_ea,prefix##_eb,prefix##_ec,prefix##_ed,prefix##_ee,prefix##_ef, \
prefix##_f0,prefix##_f1,prefix##_f2,prefix##_f3,prefix##_f4,prefix##_f5,prefix##_f6,prefix##_f7, \
prefix##_f8,prefix##_f9,prefix##_fa,prefix##_fb,prefix##_fc,prefix##_fd,prefix##_fe,prefix##_ff \
}
PROTOTYPES(Z80op,op);
PROTOTYPES(Z80cb,cb);
PROTOTYPES(Z80dd,dd);
PROTOTYPES(Z80ed,ed);
PROTOTYPES(Z80fd,fd);
PROTOTYPES(Z80xxcb,xxcb);
/****************************************************************************/
/* Burn an odd amount of cycles, that is instructions taking something */
/* different from 4 T-states per opcode (and R increment) */
/****************************************************************************/
static __inline__ void BURNODD(int cycles, int opcodes, int cyclesum)
{
if( cycles > 0 )
{
_R += (cycles / cyclesum) * opcodes;
z80_ICount -= (cycles / cyclesum) * cyclesum;
}
}
/***************************************************************
* define an opcode function
***************************************************************/
#define OP(prefix,opcode) static __inline__ void prefix##_##opcode(void)
/***************************************************************
* adjust cycle count by n T-states
***************************************************************/
#define CY(cycles) z80_ICount -= cycles
/***************************************************************
* execute an opcode
***************************************************************/
#define EXEC(prefix,opcode) \
{ \
unsigned op = opcode; \
CY(cc_##prefix[op]); \
(*Z80##prefix[op])(); \
}
#if BIG_SWITCH
#define EXEC_INLINE(prefix,opcode) \
{ \
unsigned op = opcode; \
CY(cc_##prefix[op]); \
switch(op) \
{ \
case 0x00:prefix##_##00();break; case 0x01:prefix##_##01();break; case 0x02:prefix##_##02();break; case 0x03:prefix##_##03();break; \
case 0x04:prefix##_##04();break; case 0x05:prefix##_##05();break; case 0x06:prefix##_##06();break; case 0x07:prefix##_##07();break; \
case 0x08:prefix##_##08();break; case 0x09:prefix##_##09();break; case 0x0a:prefix##_##0a();break; case 0x0b:prefix##_##0b();break; \
case 0x0c:prefix##_##0c();break; case 0x0d:prefix##_##0d();break; case 0x0e:prefix##_##0e();break; case 0x0f:prefix##_##0f();break; \
case 0x10:prefix##_##10();break; case 0x11:prefix##_##11();break; case 0x12:prefix##_##12();break; case 0x13:prefix##_##13();break; \
case 0x14:prefix##_##14();break; case 0x15:prefix##_##15();break; case 0x16:prefix##_##16();break; case 0x17:prefix##_##17();break; \
case 0x18:prefix##_##18();break; case 0x19:prefix##_##19();break; case 0x1a:prefix##_##1a();break; case 0x1b:prefix##_##1b();break; \
case 0x1c:prefix##_##1c();break; case 0x1d:prefix##_##1d();break; case 0x1e:prefix##_##1e();break; case 0x1f:prefix##_##1f();break; \
case 0x20:prefix##_##20();break; case 0x21:prefix##_##21();break; case 0x22:prefix##_##22();break; case 0x23:prefix##_##23();break; \
case 0x24:prefix##_##24();break; case 0x25:prefix##_##25();break; case 0x26:prefix##_##26();break; case 0x27:prefix##_##27();break; \
case 0x28:prefix##_##28();break; case 0x29:prefix##_##29();break; case 0x2a:prefix##_##2a();break; case 0x2b:prefix##_##2b();break; \
case 0x2c:prefix##_##2c();break; case 0x2d:prefix##_##2d();break; case 0x2e:prefix##_##2e();break; case 0x2f:prefix##_##2f();break; \
case 0x30:prefix##_##30();break; case 0x31:prefix##_##31();break; case 0x32:prefix##_##32();break; case 0x33:prefix##_##33();break; \
case 0x34:prefix##_##34();break; case 0x35:prefix##_##35();break; case 0x36:prefix##_##36();break; case 0x37:prefix##_##37();break; \
case 0x38:prefix##_##38();break; case 0x39:prefix##_##39();break; case 0x3a:prefix##_##3a();break; case 0x3b:prefix##_##3b();break; \
case 0x3c:prefix##_##3c();break; case 0x3d:prefix##_##3d();break; case 0x3e:prefix##_##3e();break; case 0x3f:prefix##_##3f();break; \
case 0x40:prefix##_##40();break; case 0x41:prefix##_##41();break; case 0x42:prefix##_##42();break; case 0x43:prefix##_##43();break; \
case 0x44:prefix##_##44();break; case 0x45:prefix##_##45();break; case 0x46:prefix##_##46();break; case 0x47:prefix##_##47();break; \
case 0x48:prefix##_##48();break; case 0x49:prefix##_##49();break; case 0x4a:prefix##_##4a();break; case 0x4b:prefix##_##4b();break; \
case 0x4c:prefix##_##4c();break; case 0x4d:prefix##_##4d();break; case 0x4e:prefix##_##4e();break; case 0x4f:prefix##_##4f();break; \
case 0x50:prefix##_##50();break; case 0x51:prefix##_##51();break; case 0x52:prefix##_##52();break; case 0x53:prefix##_##53();break; \
case 0x54:prefix##_##54();break; case 0x55:prefix##_##55();break; case 0x56:prefix##_##56();break; case 0x57:prefix##_##57();break; \
case 0x58:prefix##_##58();break; case 0x59:prefix##_##59();break; case 0x5a:prefix##_##5a();break; case 0x5b:prefix##_##5b();break; \
case 0x5c:prefix##_##5c();break; case 0x5d:prefix##_##5d();break; case 0x5e:prefix##_##5e();break; case 0x5f:prefix##_##5f();break; \
case 0x60:prefix##_##60();break; case 0x61:prefix##_##61();break; case 0x62:prefix##_##62();break; case 0x63:prefix##_##63();break; \
case 0x64:prefix##_##64();break; case 0x65:prefix##_##65();break; case 0x66:prefix##_##66();break; case 0x67:prefix##_##67();break; \
case 0x68:prefix##_##68();break; case 0x69:prefix##_##69();break; case 0x6a:prefix##_##6a();break; case 0x6b:prefix##_##6b();break; \
case 0x6c:prefix##_##6c();break; case 0x6d:prefix##_##6d();break; case 0x6e:prefix##_##6e();break; case 0x6f:prefix##_##6f();break; \
case 0x70:prefix##_##70();break; case 0x71:prefix##_##71();break; case 0x72:prefix##_##72();break; case 0x73:prefix##_##73();break; \
case 0x74:prefix##_##74();break; case 0x75:prefix##_##75();break; case 0x76:prefix##_##76();break; case 0x77:prefix##_##77();break; \
case 0x78:prefix##_##78();break; case 0x79:prefix##_##79();break; case 0x7a:prefix##_##7a();break; case 0x7b:prefix##_##7b();break; \
case 0x7c:prefix##_##7c();break; case 0x7d:prefix##_##7d();break; case 0x7e:prefix##_##7e();break; case 0x7f:prefix##_##7f();break; \
case 0x80:prefix##_##80();break; case 0x81:prefix##_##81();break; case 0x82:prefix##_##82();break; case 0x83:prefix##_##83();break; \
case 0x84:prefix##_##84();break; case 0x85:prefix##_##85();break; case 0x86:prefix##_##86();break; case 0x87:prefix##_##87();break; \
case 0x88:prefix##_##88();break; case 0x89:prefix##_##89();break; case 0x8a:prefix##_##8a();break; case 0x8b:prefix##_##8b();break; \
case 0x8c:prefix##_##8c();break; case 0x8d:prefix##_##8d();break; case 0x8e:prefix##_##8e();break; case 0x8f:prefix##_##8f();break; \
case 0x90:prefix##_##90();break; case 0x91:prefix##_##91();break; case 0x92:prefix##_##92();break; case 0x93:prefix##_##93();break; \
case 0x94:prefix##_##94();break; case 0x95:prefix##_##95();break; case 0x96:prefix##_##96();break; case 0x97:prefix##_##97();break; \
case 0x98:prefix##_##98();break; case 0x99:prefix##_##99();break; case 0x9a:prefix##_##9a();break; case 0x9b:prefix##_##9b();break; \
case 0x9c:prefix##_##9c();break; case 0x9d:prefix##_##9d();break; case 0x9e:prefix##_##9e();break; case 0x9f:prefix##_##9f();break; \
case 0xa0:prefix##_##a0();break; case 0xa1:prefix##_##a1();break; case 0xa2:prefix##_##a2();break; case 0xa3:prefix##_##a3();break; \
case 0xa4:prefix##_##a4();break; case 0xa5:prefix##_##a5();break; case 0xa6:prefix##_##a6();break; case 0xa7:prefix##_##a7();break; \
case 0xa8:prefix##_##a8();break; case 0xa9:prefix##_##a9();break; case 0xaa:prefix##_##aa();break; case 0xab:prefix##_##ab();break; \
case 0xac:prefix##_##ac();break; case 0xad:prefix##_##ad();break; case 0xae:prefix##_##ae();break; case 0xaf:prefix##_##af();break; \
case 0xb0:prefix##_##b0();break; case 0xb1:prefix##_##b1();break; case 0xb2:prefix##_##b2();break; case 0xb3:prefix##_##b3();break; \
case 0xb4:prefix##_##b4();break; case 0xb5:prefix##_##b5();break; case 0xb6:prefix##_##b6();break; case 0xb7:prefix##_##b7();break; \
case 0xb8:prefix##_##b8();break; case 0xb9:prefix##_##b9();break; case 0xba:prefix##_##ba();break; case 0xbb:prefix##_##bb();break; \
case 0xbc:prefix##_##bc();break; case 0xbd:prefix##_##bd();break; case 0xbe:prefix##_##be();break; case 0xbf:prefix##_##bf();break; \
case 0xc0:prefix##_##c0();break; case 0xc1:prefix##_##c1();break; case 0xc2:prefix##_##c2();break; case 0xc3:prefix##_##c3();break; \
case 0xc4:prefix##_##c4();break; case 0xc5:prefix##_##c5();break; case 0xc6:prefix##_##c6();break; case 0xc7:prefix##_##c7();break; \
case 0xc8:prefix##_##c8();break; case 0xc9:prefix##_##c9();break; case 0xca:prefix##_##ca();break; case 0xcb:prefix##_##cb();break; \
case 0xcc:prefix##_##cc();break; case 0xcd:prefix##_##cd();break; case 0xce:prefix##_##ce();break; case 0xcf:prefix##_##cf();break; \
case 0xd0:prefix##_##d0();break; case 0xd1:prefix##_##d1();break; case 0xd2:prefix##_##d2();break; case 0xd3:prefix##_##d3();break; \
case 0xd4:prefix##_##d4();break; case 0xd5:prefix##_##d5();break; case 0xd6:prefix##_##d6();break; case 0xd7:prefix##_##d7();break; \
case 0xd8:prefix##_##d8();break; case 0xd9:prefix##_##d9();break; case 0xda:prefix##_##da();break; case 0xdb:prefix##_##db();break; \
case 0xdc:prefix##_##dc();break; case 0xdd:prefix##_##dd();break; case 0xde:prefix##_##de();break; case 0xdf:prefix##_##df();break; \
case 0xe0:prefix##_##e0();break; case 0xe1:prefix##_##e1();break; case 0xe2:prefix##_##e2();break; case 0xe3:prefix##_##e3();break; \
case 0xe4:prefix##_##e4();break; case 0xe5:prefix##_##e5();break; case 0xe6:prefix##_##e6();break; case 0xe7:prefix##_##e7();break; \
case 0xe8:prefix##_##e8();break; case 0xe9:prefix##_##e9();break; case 0xea:prefix##_##ea();break; case 0xeb:prefix##_##eb();break; \
case 0xec:prefix##_##ec();break; case 0xed:prefix##_##ed();break; case 0xee:prefix##_##ee();break; case 0xef:prefix##_##ef();break; \
case 0xf0:prefix##_##f0();break; case 0xf1:prefix##_##f1();break; case 0xf2:prefix##_##f2();break; case 0xf3:prefix##_##f3();break; \
case 0xf4:prefix##_##f4();break; case 0xf5:prefix##_##f5();break; case 0xf6:prefix##_##f6();break; case 0xf7:prefix##_##f7();break; \
case 0xf8:prefix##_##f8();break; case 0xf9:prefix##_##f9();break; case 0xfa:prefix##_##fa();break; case 0xfb:prefix##_##fb();break; \
case 0xfc:prefix##_##fc();break; case 0xfd:prefix##_##fd();break; case 0xfe:prefix##_##fe();break; case 0xff:prefix##_##ff();break; \
} \
}
#else
#define EXEC_INLINE EXEC
#endif
/***************************************************************
* Enter HALT state; write 1 to fake port on first execution
***************************************************************/
#define ENTER_HALT { \
_PC--; \
_HALT = 1; \
if( !after_EI ) \
z80_burn( z80_ICount ); \
}
/***************************************************************
* Leave HALT state; write 0 to fake port
***************************************************************/
#define LEAVE_HALT { \
if( _HALT ) \
{ \
_HALT = 0; \
_PC++; \
} \
}
/***************************************************************
* Input a byte from given I/O port
***************************************************************/
#define IN(port) ((UINT8)cpu_readport(port))
/***************************************************************
* Output a byte to given I/O port
***************************************************************/
#define OUT(port,value) cpu_writeport(port,value)
/***************************************************************
* Read a byte from given memory location
***************************************************************/
#define RM(addr) (UINT8)cpu_readmem16(addr)
/***************************************************************
* Read a word from given memory location
***************************************************************/
static __inline__ void RM16( UINT32 addr, PAIR *r )
{
r->b.l = RM(addr);
r->b.h = RM((addr+1)&0xffff);
}
/***************************************************************
* Write a byte to given memory location
***************************************************************/
#define WM(addr,value) cpu_writemem16(addr,value)
/***************************************************************
* Write a word to given memory location
***************************************************************/
static __inline__ void WM16( UINT32 addr, PAIR *r )
{
WM(addr,r->b.l);
WM((addr+1)&0xffff,r->b.h);
}
/***************************************************************
* ROP() is identical to RM() except it is used for
* reading opcodes. In case of system with memory mapped I/O,
* this function can be used to greatly speed up emulation
***************************************************************/
static __inline__ UINT8 ROP(void)
{
unsigned pc = _PCD;
_PC++;
return cpu_readop(pc);
}
/****************************************************************
* ARG() is identical to ROP() except it is used
* for reading opcode arguments. This difference can be used to
* support systems that use different encoding mechanisms for
* opcodes and opcode arguments
***************************************************************/
static __inline__ UINT8 ARG(void)
{
unsigned pc = _PCD;
_PC++;
return cpu_readop_arg(pc);
}
static __inline__ UINT32 ARG16(void)
{
unsigned pc = _PCD;
_PC += 2;
return cpu_readop_arg(pc) | (cpu_readop_arg((pc+1)&0xffff) << 8);
}
/***************************************************************
* Calculate the effective address EA of an opcode using
* IX+offset resp. IY+offset addressing.
***************************************************************/
#define EAX EA = (UINT32)(UINT16)(_IX+(INT8)ARG())
#define EAY EA = (UINT32)(UINT16)(_IY+(INT8)ARG())
/***************************************************************
* POP
***************************************************************/
#define POP(DR) { RM16( _SPD, &Z80.DR ); _SP += 2; }
/***************************************************************
* PUSH
***************************************************************/
#define PUSH(SR) { _SP -= 2; WM16( _SPD, &Z80.SR ); }
/***************************************************************
* JP
***************************************************************/
#if BUSY_LOOP_HACKS
#define JP { \
unsigned oldpc = _PCD-1; \
_PCD = ARG16(); \
/* speed up busy loop */ \
if( _PCD == oldpc ) \
{ \
if( !after_EI ) \
BURNODD( z80_ICount, 1, 10 ); \
} \
else \
{ \
UINT8 op = cpu_readop(_PCD); \
if( _PCD == oldpc-1 ) \
{ \
/* NOP - JP $-1 or EI - JP $-1 */ \
if ( op == 0x00 || op == 0xfb ) \
{ \
if( !after_EI ) \
BURNODD( z80_ICount-4, 2, 4+10 ); \
} \
} \
else \
/* LD SP,#xxxx - JP $-3 (Galaga) */ \
if( _PCD == oldpc-3 && op == 0x31 ) \
{ \
if( !after_EI ) \
BURNODD( z80_ICount-10, 2, 10+10 ); \
} \
} \
}
#else
#define JP { \
_PCD = ARG16(); \
}
#endif
/***************************************************************
* JP_COND
***************************************************************/
#define JP_COND(cond) \
if( cond ) \
{ \
_PCD = ARG16(); \
} \
else \
{ \
_PC += 2; \
}
/***************************************************************
* JR
***************************************************************/
#define JR() \
{ \
unsigned oldpc = _PCD-1; \
INT8 arg = (INT8)ARG(); /* ARG() also increments _PC */ \
_PC += arg; /* so don't do _PC += ARG() */ \
/* speed up busy loop */ \
if( _PCD == oldpc ) \
{ \
if( !after_EI ) \
BURNODD( z80_ICount, 1, 12 ); \
} \
else \
{ \
UINT8 op = cpu_readop(_PCD); \
if( _PCD == oldpc-1 ) \
{ \
/* NOP - JR $-1 or EI - JR $-1 */ \
if ( op == 0x00 || op == 0xfb ) \
{ \
if( !after_EI ) \
BURNODD( z80_ICount-4, 2, 4+12 ); \
} \
} \
else \
/* LD SP,#xxxx - JR $-3 */ \
if( _PCD == oldpc-3 && op == 0x31 ) \
{ \
if( !after_EI ) \
BURNODD( z80_ICount-12, 2, 10+12 ); \
} \
} \
}
/***************************************************************
* JR_COND
***************************************************************/
#define JR_COND(cond) \
if( cond ) \
{ \
INT8 arg = (INT8)ARG(); /* ARG() also increments _PC */ \
_PC += arg; /* so don't do _PC += ARG() */ \
CY(5); \
} \
else _PC++; \
/***************************************************************
* CALL
***************************************************************/
#define CALL(cond) \
if( cond ) \
{ \
EA = ARG16(); \
PUSH( PC ); \
_PCD = EA; \
CY(7); \
} \
else \
{ \
_PC+=2; \
}
/***************************************************************
* RET
***************************************************************/
#define RET(cond) \
if( cond ) \
{ \
POP(PC); \
CY(6); \
}
/***************************************************************
* RETN
***************************************************************/
#define RETN { \
RET(1); \
if( _IFF1 == 0 && _IFF2 == 1 ) \
{ \
_IFF1 = 1; \
if( Z80.irq_state != CLEAR_LINE || \
Z80.request_irq >= 0 ) \
{ \
take_interrupt(); \
} \
} \
else _IFF1 = _IFF2; \
}
/***************************************************************
* RETI
***************************************************************/
#define RETI { \
int device = Z80.service_irq; \
RET(1); \
/* according to http://www.msxnet.org/tech/Z80/z80undoc.txt */ \
/* _IFF1 = _IFF2; */ \
if( device >= 0 ) \
{ \
Z80.irq[device].interrupt_reti(Z80.irq[device].irq_param); \
} \
}
/***************************************************************
* LD R,A
***************************************************************/
#define LD_R_A { \
_R = _A; \
_R2 = _A & 0x80; /* keep bit 7 of R */ \
}
/***************************************************************
* LD A,R
***************************************************************/
#define LD_A_R { \
_A = (_R & 0x7f) | _R2; \
_F = (_F & CF) | SZ[_A] | ( _IFF2 << 2 ); \
}
/***************************************************************
* LD I,A
***************************************************************/
#define LD_I_A { \
_I = _A; \
}
/***************************************************************
* LD A,I
***************************************************************/
#define LD_A_I { \
_A = _I; \
_F = (_F & CF) | SZ[_A] | ( _IFF2 << 2 ); \
}
/***************************************************************
* RST
***************************************************************/
#define RST(addr) \
PUSH( PC ); \
_PCD = addr;
/***************************************************************
* INC r8
***************************************************************/
static __inline__ UINT8 INC(UINT8 value)
{
UINT8 res = value + 1;
_F = (_F & CF) | SZHV_inc[res];
return (UINT8)res;
}
/***************************************************************
* DEC r8
***************************************************************/
static __inline__ UINT8 DEC(UINT8 value)
{
UINT8 res = value - 1;
_F = (_F & CF) | SZHV_dec[res];
return res;
}
/***************************************************************
* RLCA
***************************************************************/
#if Z80_EXACT
#define RLCA \
_A = (_A << 1) | (_A >> 7); \
_F = (_F & (SF | ZF | PF)) | (_A & (YF | XF | CF))
#else
#define RLCA \
_A = (_A << 1) | (_A >> 7); \
_F = (_F & (SF | ZF | YF | XF | PF)) | (_A & CF)
#endif
/***************************************************************
* RRCA
***************************************************************/
#if Z80_EXACT
#define RRCA \
_F = (_F & (SF | ZF | PF)) | (_A & (YF | XF | CF)); \
_A = (_A >> 1) | (_A << 7)
#else
#define RRCA \
_F = (_F & (SF | ZF | YF | XF | PF)) | (_A & CF); \
_A = (_A >> 1) | (_A << 7)
#endif
/***************************************************************
* RLA
***************************************************************/
#if Z80_EXACT
#define RLA { \
UINT8 res = (_A << 1) | (_F & CF); \
UINT8 c = (_A & 0x80) ? CF : 0; \
_F = (_F & (SF | ZF | PF)) | c | (res & (YF | XF)); \
_A = res; \
}
#else
#define RLA { \
UINT8 res = (_A << 1) | (_F & CF); \
UINT8 c = (_A & 0x80) ? CF : 0; \
_F = (_F & (SF | ZF | YF | XF | PF)) | c; \
_A = res; \
}
#endif
/***************************************************************
* RRA
***************************************************************/
#if Z80_EXACT
#define RRA { \
UINT8 res = (_A >> 1) | (_F << 7); \
UINT8 c = (_A & 0x01) ? CF : 0; \
_F = (_F & (SF | ZF | PF)) | c | (res & (YF | XF)); \
_A = res; \
}
#else
#define RRA { \
UINT8 res = (_A >> 1) | (_F << 7); \
UINT8 c = (_A & 0x01) ? CF : 0; \
_F = (_F & (SF | ZF | YF | XF | PF)) | c; \
_A = res; \
}
#endif
/***************************************************************
* RRD
***************************************************************/
#define RRD { \
UINT8 n = RM(_HL); \
WM( _HL, (n >> 4) | (_A << 4) ); \
_A = (_A & 0xf0) | (n & 0x0f); \
_F = (_F & CF) | SZP[_A]; \
}
/***************************************************************
* RLD
***************************************************************/
#define RLD { \
UINT8 n = RM(_HL); \
WM( _HL, (n << 4) | (_A & 0x0f) ); \
_A = (_A & 0xf0) | (n >> 4); \
_F = (_F & CF) | SZP[_A]; \
}
/***************************************************************
* ADD A,n
***************************************************************/
#ifdef X86_ASM
#if Z80_EXACT
#define ADD(value) \
asm ( \
" addb %2,%0 \n" \
" lahf \n" \
" setob %1 \n" /* al = 1 if overflow */ \
" addb %1,%1 \n" \
" addb %1,%1 \n" /* shift to P/V bit position */ \
" andb $0xd1,%%ah \n" /* sign, zero, half carry, carry */ \
" orb %%ah,%1 \n" \
" movb %0,%%ah \n" /* get result */ \
" andb $0x28,%%ah \n" /* maks flags 5+3 */ \
" orb %%ah,%1 \n" /* put them into flags */ \
:"=r" (_A), "=r" (_F) \
:"r" (value), "1" (_F), "0" (_A) \
)
#else
#define ADD(value) \
asm ( \
" addb %2,%0 \n" \
" lahf \n" \
" setob %1 \n" /* al = 1 if overflow */ \
" addb %1,%1 \n" \
" addb %1,%1 \n" /* shift to P/V bit position */ \
" andb $0xd1,%%ah \n" /* sign, zero, half carry, carry */ \
" orb %%ah,%1 \n" \
:"=r" (_A), "=r" (_F) \
:"r" (value), "1" (_F), "0" (_A) \
)
#endif
#else
#if BIG_FLAGS_ARRAY
#define ADD(value) \
{ \
UINT32 ah = _AFD & 0xff00; \
UINT32 res = (UINT8)((ah >> 8) + value); \
_F = SZHVC_add[ah | res]; \
_A = res; \
}
#else
#define ADD(value) \
{ \
unsigned val = value; \
unsigned res = _A + val; \
_F = SZ[(UINT8)res] | ((res >> 8) & CF) | \
((_A ^ res ^ val) & HF) | \
(((val ^ _A ^ 0x80) & (val ^ res) & 0x80) >> 5); \
_A = (UINT8)res; \
}
#endif
#endif
/***************************************************************
* ADC A,n
***************************************************************/
#ifdef X86_ASM
#if Z80_EXACT
#define ADC(value) \
asm ( \
" shrb $1,%1 \n" \
" adcb %2,%0 \n" \
" lahf \n" \
" setob %1 \n" /* al = 1 if overflow */ \
" addb %1,%1 \n" /* shift to P/V bit position */ \
" addb %1,%1 \n" \
" andb $0xd1,%%ah \n" /* sign, zero, half carry, carry */ \
" orb %%ah,%1 \n" /* combine with P/V */ \
" movb %0,%%ah \n" /* get result */ \
" andb $0x28,%%ah \n" /* maks flags 5+3 */ \
" orb %%ah,%1 \n" /* put them into flags */ \
:"=r" (_A), "=r" (_F) \