-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Z80.c
2942 lines (2523 loc) · 128 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 v0.2
______ ______ ______
/\___ \/\ __ \\ __ \
____ \/__/ /\_\ __ \\ \/\ \ ________________________________________________
| /\_____\\_____\\_____\ |
| Zilog \/_____//_____//_____/ CPU Emulator |
| Copyright (C) 1999-2024 Manuel Sainz de Baranda y Goñi. |
| |
| This emulator is free software: you can redistribute it and/or modify it |
| under the terms of the GNU Lesser General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or (at your |
| option) any later version. |
| |
| This emulator 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. See the GNU Lesser General Public |
| License for more details. |
| |
| You should have received a copy of the GNU Lesser General Public License |
| along with this emulator. If not, see <http://www.gnu.org/licenses/>. |
| |
| -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
| |
| A NOTE FROM THE ORIGINAL AUTHOR |
| |
| Those familiar with the official documentation of the Zilog Z80 CPU will |
| find this source code quite intuitive. The purpose has not been to write |
| the fastest possible emulator, although the speed aspect is not neglected, |
| but a portable, hackable and well- |
| .----._.----. structured piece of software; |
| A11 <-01--|1 o|--40-> A10 something small, solid and elegant |
| A12 <-02--| |--39-> A09 that can stand the test of time |
| A13 <-03--| |--38-> A08 with no need for major changes. |
| A14 <-04--| |--37-> A07 |
| A15 <-05--| |--36-> A06 Some of the main design decisions |
| CLK --06->| |--35-> A05 have been the following: |
| D4 <-07->| |--34-> A04 |
| D3 <-08->|.---------.|--33-> A03 1. Opcode partial decoding keeps |
| D5 <-09->|| ZILOG ||--32-> A02 the code small and maintainable. |
| D6 <-10->|| Z80 ||--31-> A01 |
| +5V --11--|| CPU ||--30-> A00 2. Function pointer tables for |
| D2 <-12->|| ||--29-- GND opcode selection allow easy reuse |
| D7 <-13->|'---------'|--28-> RFSH of almost all instruction code |
| D0 <-14->| |--27-> M1 in the interrupt mode 0. |
| D1 <-15->| |<-26-- RESET |
| INT --16->| |<-25-- BUSREQ 3. Avoiding conditional statements |
| NMI --17->| |<-24-- WAIT as much as possible reduces the |
| HALT <-18--| |--23-> BUSACK branch penalty in modern pipelined |
| MREQ <-19--| |--22-> WR processors. |
| IORQ <-20--| |--21-> RD |
| '-----------' |
| Zilog Z80 CPU, May 1976 version |
| 40-pin ceramic DIP pinout Manuel |
| |
'=============================================================================*/
#ifndef Z80_EXTERNAL_HEADER
# include <Z/constants/pointer.h>
# include <Z/macros/bitwise.h>
# include <Z/macros/structure.h>
#endif
#ifdef Z80_STATIC
# define Z80_API
#else
# define Z80_API Z_API_EXPORT
#endif
#ifdef Z80_WITH_LOCAL_HEADER
# include "Z80.h"
#else
# include <Z80.h>
#endif
/* MARK: - Precomputed Values of AF for `daa` */
#ifdef Z80_WITH_PRECOMPUTED_DAA
# define H(value) Z_UINT16(0x##value)
static zuint16 const daa_af_table[2048] = {
/* HNC */
/* 000 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(0044), H(0100), H(0200), H(0304), H(0400), H(0504), H(0604), H(0700), H(0808), H(090C), H(1010), H(1114), H(1214), H(1310), H(1414), H(1510),
/* 1 */ H(1000), H(1104), H(1204), H(1300), H(1404), H(1500), H(1600), H(1704), H(180C), H(1908), H(2030), H(2134), H(2234), H(2330), H(2434), H(2530),
/* 2 */ H(2020), H(2124), H(2224), H(2320), H(2424), H(2520), H(2620), H(2724), H(282C), H(2928), H(3034), H(3130), H(3230), H(3334), H(3430), H(3534),
/* 3 */ H(3024), H(3120), H(3220), H(3324), H(3420), H(3524), H(3624), H(3720), H(3828), H(392C), H(4010), H(4114), H(4214), H(4310), H(4414), H(4510),
/* 4 */ H(4000), H(4104), H(4204), H(4300), H(4404), H(4500), H(4600), H(4704), H(480C), H(4908), H(5014), H(5110), H(5210), H(5314), H(5410), H(5514),
/* 5 */ H(5004), H(5100), H(5200), H(5304), H(5400), H(5504), H(5604), H(5700), H(5808), H(590C), H(6034), H(6130), H(6230), H(6334), H(6430), H(6534),
/* 6 */ H(6024), H(6120), H(6220), H(6324), H(6420), H(6524), H(6624), H(6720), H(6828), H(692C), H(7030), H(7134), H(7234), H(7330), H(7434), H(7530),
/* 7 */ H(7020), H(7124), H(7224), H(7320), H(7424), H(7520), H(7620), H(7724), H(782C), H(7928), H(8090), H(8194), H(8294), H(8390), H(8494), H(8590),
/* 8 */ H(8080), H(8184), H(8284), H(8380), H(8484), H(8580), H(8680), H(8784), H(888C), H(8988), H(9094), H(9190), H(9290), H(9394), H(9490), H(9594),
/* 9 */ H(9084), H(9180), H(9280), H(9384), H(9480), H(9584), H(9684), H(9780), H(9888), H(998C), H(0055), H(0111), H(0211), H(0315), H(0411), H(0515),
/* A */ H(0045), H(0101), H(0201), H(0305), H(0401), H(0505), H(0605), H(0701), H(0809), H(090D), H(1011), H(1115), H(1215), H(1311), H(1415), H(1511),
/* B */ H(1001), H(1105), H(1205), H(1301), H(1405), H(1501), H(1601), H(1705), H(180D), H(1909), H(2031), H(2135), H(2235), H(2331), H(2435), H(2531),
/* C */ H(2021), H(2125), H(2225), H(2321), H(2425), H(2521), H(2621), H(2725), H(282D), H(2929), H(3035), H(3131), H(3231), H(3335), H(3431), H(3535),
/* D */ H(3025), H(3121), H(3221), H(3325), H(3421), H(3525), H(3625), H(3721), H(3829), H(392D), H(4011), H(4115), H(4215), H(4311), H(4415), H(4511),
/* E */ H(4001), H(4105), H(4205), H(4301), H(4405), H(4501), H(4601), H(4705), H(480D), H(4909), H(5015), H(5111), H(5211), H(5315), H(5411), H(5515),
/* F */ H(5005), H(5101), H(5201), H(5305), H(5401), H(5505), H(5605), H(5701), H(5809), H(590D), H(6035), H(6131), H(6231), H(6335), H(6431), H(6535),
/* HNC */
/* 001 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(6025), H(6121), H(6221), H(6325), H(6421), H(6525), H(6625), H(6721), H(6829), H(692D), H(7031), H(7135), H(7235), H(7331), H(7435), H(7531),
/* 1 */ H(7021), H(7125), H(7225), H(7321), H(7425), H(7521), H(7621), H(7725), H(782D), H(7929), H(8091), H(8195), H(8295), H(8391), H(8495), H(8591),
/* 2 */ H(8081), H(8185), H(8285), H(8381), H(8485), H(8581), H(8681), H(8785), H(888D), H(8989), H(9095), H(9191), H(9291), H(9395), H(9491), H(9595),
/* 3 */ H(9085), H(9181), H(9281), H(9385), H(9481), H(9585), H(9685), H(9781), H(9889), H(998D), H(A0B5), H(A1B1), H(A2B1), H(A3B5), H(A4B1), H(A5B5),
/* 4 */ H(A0A5), H(A1A1), H(A2A1), H(A3A5), H(A4A1), H(A5A5), H(A6A5), H(A7A1), H(A8A9), H(A9AD), H(B0B1), H(B1B5), H(B2B5), H(B3B1), H(B4B5), H(B5B1),
/* 5 */ H(B0A1), H(B1A5), H(B2A5), H(B3A1), H(B4A5), H(B5A1), H(B6A1), H(B7A5), H(B8AD), H(B9A9), H(C095), H(C191), H(C291), H(C395), H(C491), H(C595),
/* 6 */ H(C085), H(C181), H(C281), H(C385), H(C481), H(C585), H(C685), H(C781), H(C889), H(C98D), H(D091), H(D195), H(D295), H(D391), H(D495), H(D591),
/* 7 */ H(D081), H(D185), H(D285), H(D381), H(D485), H(D581), H(D681), H(D785), H(D88D), H(D989), H(E0B1), H(E1B5), H(E2B5), H(E3B1), H(E4B5), H(E5B1),
/* 8 */ H(E0A1), H(E1A5), H(E2A5), H(E3A1), H(E4A5), H(E5A1), H(E6A1), H(E7A5), H(E8AD), H(E9A9), H(F0B5), H(F1B1), H(F2B1), H(F3B5), H(F4B1), H(F5B5),
/* 9 */ H(F0A5), H(F1A1), H(F2A1), H(F3A5), H(F4A1), H(F5A5), H(F6A5), H(F7A1), H(F8A9), H(F9AD), H(0055), H(0111), H(0211), H(0315), H(0411), H(0515),
/* A */ H(0045), H(0101), H(0201), H(0305), H(0401), H(0505), H(0605), H(0701), H(0809), H(090D), H(1011), H(1115), H(1215), H(1311), H(1415), H(1511),
/* B */ H(1001), H(1105), H(1205), H(1301), H(1405), H(1501), H(1601), H(1705), H(180D), H(1909), H(2031), H(2135), H(2235), H(2331), H(2435), H(2531),
/* C */ H(2021), H(2125), H(2225), H(2321), H(2425), H(2521), H(2621), H(2725), H(282D), H(2929), H(3035), H(3131), H(3231), H(3335), H(3431), H(3535),
/* D */ H(3025), H(3121), H(3221), H(3325), H(3421), H(3525), H(3625), H(3721), H(3829), H(392D), H(4011), H(4115), H(4215), H(4311), H(4415), H(4511),
/* E */ H(4001), H(4105), H(4205), H(4301), H(4405), H(4501), H(4601), H(4705), H(480D), H(4909), H(5015), H(5111), H(5211), H(5315), H(5411), H(5515),
/* F */ H(5005), H(5101), H(5201), H(5305), H(5401), H(5505), H(5605), H(5701), H(5809), H(590D), H(6035), H(6131), H(6231), H(6335), H(6431), H(6535),
/* HNC */
/* 010 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(0046), H(0102), H(0202), H(0306), H(0402), H(0506), H(0606), H(0702), H(080A), H(090E), H(0402), H(0506), H(0606), H(0702), H(080A), H(090E),
/* 1 */ H(1002), H(1106), H(1206), H(1302), H(1406), H(1502), H(1602), H(1706), H(180E), H(190A), H(1406), H(1502), H(1602), H(1706), H(180E), H(190A),
/* 2 */ H(2022), H(2126), H(2226), H(2322), H(2426), H(2522), H(2622), H(2726), H(282E), H(292A), H(2426), H(2522), H(2622), H(2726), H(282E), H(292A),
/* 3 */ H(3026), H(3122), H(3222), H(3326), H(3422), H(3526), H(3626), H(3722), H(382A), H(392E), H(3422), H(3526), H(3626), H(3722), H(382A), H(392E),
/* 4 */ H(4002), H(4106), H(4206), H(4302), H(4406), H(4502), H(4602), H(4706), H(480E), H(490A), H(4406), H(4502), H(4602), H(4706), H(480E), H(490A),
/* 5 */ H(5006), H(5102), H(5202), H(5306), H(5402), H(5506), H(5606), H(5702), H(580A), H(590E), H(5402), H(5506), H(5606), H(5702), H(580A), H(590E),
/* 6 */ H(6026), H(6122), H(6222), H(6326), H(6422), H(6526), H(6626), H(6722), H(682A), H(692E), H(6422), H(6526), H(6626), H(6722), H(682A), H(692E),
/* 7 */ H(7022), H(7126), H(7226), H(7322), H(7426), H(7522), H(7622), H(7726), H(782E), H(792A), H(7426), H(7522), H(7622), H(7726), H(782E), H(792A),
/* 8 */ H(8082), H(8186), H(8286), H(8382), H(8486), H(8582), H(8682), H(8786), H(888E), H(898A), H(8486), H(8582), H(8682), H(8786), H(888E), H(898A),
/* 9 */ H(9086), H(9182), H(9282), H(9386), H(9482), H(9586), H(9686), H(9782), H(988A), H(998E), H(3423), H(3527), H(3627), H(3723), H(382B), H(392F),
/* A */ H(4003), H(4107), H(4207), H(4303), H(4407), H(4503), H(4603), H(4707), H(480F), H(490B), H(4407), H(4503), H(4603), H(4707), H(480F), H(490B),
/* B */ H(5007), H(5103), H(5203), H(5307), H(5403), H(5507), H(5607), H(5703), H(580B), H(590F), H(5403), H(5507), H(5607), H(5703), H(580B), H(590F),
/* C */ H(6027), H(6123), H(6223), H(6327), H(6423), H(6527), H(6627), H(6723), H(682B), H(692F), H(6423), H(6527), H(6627), H(6723), H(682B), H(692F),
/* D */ H(7023), H(7127), H(7227), H(7323), H(7427), H(7523), H(7623), H(7727), H(782F), H(792B), H(7427), H(7523), H(7623), H(7727), H(782F), H(792B),
/* E */ H(8083), H(8187), H(8287), H(8383), H(8487), H(8583), H(8683), H(8787), H(888F), H(898B), H(8487), H(8583), H(8683), H(8787), H(888F), H(898B),
/* F */ H(9087), H(9183), H(9283), H(9387), H(9483), H(9587), H(9687), H(9783), H(988B), H(998F), H(9483), H(9587), H(9687), H(9783), H(988B), H(998F),
/* HNC */
/* 011 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(A0A7), H(A1A3), H(A2A3), H(A3A7), H(A4A3), H(A5A7), H(A6A7), H(A7A3), H(A8AB), H(A9AF), H(A4A3), H(A5A7), H(A6A7), H(A7A3), H(A8AB), H(A9AF),
/* 1 */ H(B0A3), H(B1A7), H(B2A7), H(B3A3), H(B4A7), H(B5A3), H(B6A3), H(B7A7), H(B8AF), H(B9AB), H(B4A7), H(B5A3), H(B6A3), H(B7A7), H(B8AF), H(B9AB),
/* 2 */ H(C087), H(C183), H(C283), H(C387), H(C483), H(C587), H(C687), H(C783), H(C88B), H(C98F), H(C483), H(C587), H(C687), H(C783), H(C88B), H(C98F),
/* 3 */ H(D083), H(D187), H(D287), H(D383), H(D487), H(D583), H(D683), H(D787), H(D88F), H(D98B), H(D487), H(D583), H(D683), H(D787), H(D88F), H(D98B),
/* 4 */ H(E0A3), H(E1A7), H(E2A7), H(E3A3), H(E4A7), H(E5A3), H(E6A3), H(E7A7), H(E8AF), H(E9AB), H(E4A7), H(E5A3), H(E6A3), H(E7A7), H(E8AF), H(E9AB),
/* 5 */ H(F0A7), H(F1A3), H(F2A3), H(F3A7), H(F4A3), H(F5A7), H(F6A7), H(F7A3), H(F8AB), H(F9AF), H(F4A3), H(F5A7), H(F6A7), H(F7A3), H(F8AB), H(F9AF),
/* 6 */ H(0047), H(0103), H(0203), H(0307), H(0403), H(0507), H(0607), H(0703), H(080B), H(090F), H(0403), H(0507), H(0607), H(0703), H(080B), H(090F),
/* 7 */ H(1003), H(1107), H(1207), H(1303), H(1407), H(1503), H(1603), H(1707), H(180F), H(190B), H(1407), H(1503), H(1603), H(1707), H(180F), H(190B),
/* 8 */ H(2023), H(2127), H(2227), H(2323), H(2427), H(2523), H(2623), H(2727), H(282F), H(292B), H(2427), H(2523), H(2623), H(2727), H(282F), H(292B),
/* 9 */ H(3027), H(3123), H(3223), H(3327), H(3423), H(3527), H(3627), H(3723), H(382B), H(392F), H(3423), H(3527), H(3627), H(3723), H(382B), H(392F),
/* A */ H(4003), H(4107), H(4207), H(4303), H(4407), H(4503), H(4603), H(4707), H(480F), H(490B), H(4407), H(4503), H(4603), H(4707), H(480F), H(490B),
/* B */ H(5007), H(5103), H(5203), H(5307), H(5403), H(5507), H(5607), H(5703), H(580B), H(590F), H(5403), H(5507), H(5607), H(5703), H(580B), H(590F),
/* C */ H(6027), H(6123), H(6223), H(6327), H(6423), H(6527), H(6627), H(6723), H(682B), H(692F), H(6423), H(6527), H(6627), H(6723), H(682B), H(692F),
/* D */ H(7023), H(7127), H(7227), H(7323), H(7427), H(7523), H(7623), H(7727), H(782F), H(792B), H(7427), H(7523), H(7623), H(7727), H(782F), H(792B),
/* E */ H(8083), H(8187), H(8287), H(8383), H(8487), H(8583), H(8683), H(8787), H(888F), H(898B), H(8487), H(8583), H(8683), H(8787), H(888F), H(898B),
/* F */ H(9087), H(9183), H(9283), H(9387), H(9483), H(9587), H(9687), H(9783), H(988B), H(998F), H(9483), H(9587), H(9687), H(9783), H(988B), H(998F),
/* HNC */
/* 100 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(0604), H(0700), H(0808), H(090C), H(0A0C), H(0B08), H(0C0C), H(0D08), H(0E08), H(0F0C), H(1010), H(1114), H(1214), H(1310), H(1414), H(1510),
/* 1 */ H(1600), H(1704), H(180C), H(1908), H(1A08), H(1B0C), H(1C08), H(1D0C), H(1E0C), H(1F08), H(2030), H(2134), H(2234), H(2330), H(2434), H(2530),
/* 2 */ H(2620), H(2724), H(282C), H(2928), H(2A28), H(2B2C), H(2C28), H(2D2C), H(2E2C), H(2F28), H(3034), H(3130), H(3230), H(3334), H(3430), H(3534),
/* 3 */ H(3624), H(3720), H(3828), H(392C), H(3A2C), H(3B28), H(3C2C), H(3D28), H(3E28), H(3F2C), H(4010), H(4114), H(4214), H(4310), H(4414), H(4510),
/* 4 */ H(4600), H(4704), H(480C), H(4908), H(4A08), H(4B0C), H(4C08), H(4D0C), H(4E0C), H(4F08), H(5014), H(5110), H(5210), H(5314), H(5410), H(5514),
/* 5 */ H(5604), H(5700), H(5808), H(590C), H(5A0C), H(5B08), H(5C0C), H(5D08), H(5E08), H(5F0C), H(6034), H(6130), H(6230), H(6334), H(6430), H(6534),
/* 6 */ H(6624), H(6720), H(6828), H(692C), H(6A2C), H(6B28), H(6C2C), H(6D28), H(6E28), H(6F2C), H(7030), H(7134), H(7234), H(7330), H(7434), H(7530),
/* 7 */ H(7620), H(7724), H(782C), H(7928), H(7A28), H(7B2C), H(7C28), H(7D2C), H(7E2C), H(7F28), H(8090), H(8194), H(8294), H(8390), H(8494), H(8590),
/* 8 */ H(8680), H(8784), H(888C), H(8988), H(8A88), H(8B8C), H(8C88), H(8D8C), H(8E8C), H(8F88), H(9094), H(9190), H(9290), H(9394), H(9490), H(9594),
/* 9 */ H(9684), H(9780), H(9888), H(998C), H(9A8C), H(9B88), H(9C8C), H(9D88), H(9E88), H(9F8C), H(0055), H(0111), H(0211), H(0315), H(0411), H(0515),
/* A */ H(0605), H(0701), H(0809), H(090D), H(0A0D), H(0B09), H(0C0D), H(0D09), H(0E09), H(0F0D), H(1011), H(1115), H(1215), H(1311), H(1415), H(1511),
/* B */ H(1601), H(1705), H(180D), H(1909), H(1A09), H(1B0D), H(1C09), H(1D0D), H(1E0D), H(1F09), H(2031), H(2135), H(2235), H(2331), H(2435), H(2531),
/* C */ H(2621), H(2725), H(282D), H(2929), H(2A29), H(2B2D), H(2C29), H(2D2D), H(2E2D), H(2F29), H(3035), H(3131), H(3231), H(3335), H(3431), H(3535),
/* D */ H(3625), H(3721), H(3829), H(392D), H(3A2D), H(3B29), H(3C2D), H(3D29), H(3E29), H(3F2D), H(4011), H(4115), H(4215), H(4311), H(4415), H(4511),
/* E */ H(4601), H(4705), H(480D), H(4909), H(4A09), H(4B0D), H(4C09), H(4D0D), H(4E0D), H(4F09), H(5015), H(5111), H(5211), H(5315), H(5411), H(5515),
/* F */ H(5605), H(5701), H(5809), H(590D), H(5A0D), H(5B09), H(5C0D), H(5D09), H(5E09), H(5F0D), H(6035), H(6131), H(6231), H(6335), H(6431), H(6535),
/* HNC */
/* 101 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(6625), H(6721), H(6829), H(692D), H(6A2D), H(6B29), H(6C2D), H(6D29), H(6E29), H(6F2D), H(7031), H(7135), H(7235), H(7331), H(7435), H(7531),
/* 1 */ H(7621), H(7725), H(782D), H(7929), H(7A29), H(7B2D), H(7C29), H(7D2D), H(7E2D), H(7F29), H(8091), H(8195), H(8295), H(8391), H(8495), H(8591),
/* 2 */ H(8681), H(8785), H(888D), H(8989), H(8A89), H(8B8D), H(8C89), H(8D8D), H(8E8D), H(8F89), H(9095), H(9191), H(9291), H(9395), H(9491), H(9595),
/* 3 */ H(9685), H(9781), H(9889), H(998D), H(9A8D), H(9B89), H(9C8D), H(9D89), H(9E89), H(9F8D), H(A0B5), H(A1B1), H(A2B1), H(A3B5), H(A4B1), H(A5B5),
/* 4 */ H(A6A5), H(A7A1), H(A8A9), H(A9AD), H(AAAD), H(ABA9), H(ACAD), H(ADA9), H(AEA9), H(AFAD), H(B0B1), H(B1B5), H(B2B5), H(B3B1), H(B4B5), H(B5B1),
/* 5 */ H(B6A1), H(B7A5), H(B8AD), H(B9A9), H(BAA9), H(BBAD), H(BCA9), H(BDAD), H(BEAD), H(BFA9), H(C095), H(C191), H(C291), H(C395), H(C491), H(C595),
/* 6 */ H(C685), H(C781), H(C889), H(C98D), H(CA8D), H(CB89), H(CC8D), H(CD89), H(CE89), H(CF8D), H(D091), H(D195), H(D295), H(D391), H(D495), H(D591),
/* 7 */ H(D681), H(D785), H(D88D), H(D989), H(DA89), H(DB8D), H(DC89), H(DD8D), H(DE8D), H(DF89), H(E0B1), H(E1B5), H(E2B5), H(E3B1), H(E4B5), H(E5B1),
/* 8 */ H(E6A1), H(E7A5), H(E8AD), H(E9A9), H(EAA9), H(EBAD), H(ECA9), H(EDAD), H(EEAD), H(EFA9), H(F0B5), H(F1B1), H(F2B1), H(F3B5), H(F4B1), H(F5B5),
/* 9 */ H(F6A5), H(F7A1), H(F8A9), H(F9AD), H(FAAD), H(FBA9), H(FCAD), H(FDA9), H(FEA9), H(FFAD), H(0055), H(0111), H(0211), H(0315), H(0411), H(0515),
/* A */ H(0605), H(0701), H(0809), H(090D), H(0A0D), H(0B09), H(0C0D), H(0D09), H(0E09), H(0F0D), H(1011), H(1115), H(1215), H(1311), H(1415), H(1511),
/* B */ H(1601), H(1705), H(180D), H(1909), H(1A09), H(1B0D), H(1C09), H(1D0D), H(1E0D), H(1F09), H(2031), H(2135), H(2235), H(2331), H(2435), H(2531),
/* C */ H(2621), H(2725), H(282D), H(2929), H(2A29), H(2B2D), H(2C29), H(2D2D), H(2E2D), H(2F29), H(3035), H(3131), H(3231), H(3335), H(3431), H(3535),
/* D */ H(3625), H(3721), H(3829), H(392D), H(3A2D), H(3B29), H(3C2D), H(3D29), H(3E29), H(3F2D), H(4011), H(4115), H(4215), H(4311), H(4415), H(4511),
/* E */ H(4601), H(4705), H(480D), H(4909), H(4A09), H(4B0D), H(4C09), H(4D0D), H(4E0D), H(4F09), H(5015), H(5111), H(5211), H(5315), H(5411), H(5515),
/* F */ H(5605), H(5701), H(5809), H(590D), H(5A0D), H(5B09), H(5C0D), H(5D09), H(5E09), H(5F0D), H(6035), H(6131), H(6231), H(6335), H(6431), H(6535),
/* HNC */
/* 110 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(FABE), H(FBBA), H(FCBE), H(FDBA), H(FEBA), H(FFBE), H(0046), H(0102), H(0202), H(0306), H(0402), H(0506), H(0606), H(0702), H(080A), H(090E),
/* 1 */ H(0A1E), H(0B1A), H(0C1E), H(0D1A), H(0E1A), H(0F1E), H(1002), H(1106), H(1206), H(1302), H(1406), H(1502), H(1602), H(1706), H(180E), H(190A),
/* 2 */ H(1A1A), H(1B1E), H(1C1A), H(1D1E), H(1E1E), H(1F1A), H(2022), H(2126), H(2226), H(2322), H(2426), H(2522), H(2622), H(2726), H(282E), H(292A),
/* 3 */ H(2A3A), H(2B3E), H(2C3A), H(2D3E), H(2E3E), H(2F3A), H(3026), H(3122), H(3222), H(3326), H(3422), H(3526), H(3626), H(3722), H(382A), H(392E),
/* 4 */ H(3A3E), H(3B3A), H(3C3E), H(3D3A), H(3E3A), H(3F3E), H(4002), H(4106), H(4206), H(4302), H(4406), H(4502), H(4602), H(4706), H(480E), H(490A),
/* 5 */ H(4A1A), H(4B1E), H(4C1A), H(4D1E), H(4E1E), H(4F1A), H(5006), H(5102), H(5202), H(5306), H(5402), H(5506), H(5606), H(5702), H(580A), H(590E),
/* 6 */ H(5A1E), H(5B1A), H(5C1E), H(5D1A), H(5E1A), H(5F1E), H(6026), H(6122), H(6222), H(6326), H(6422), H(6526), H(6626), H(6722), H(682A), H(692E),
/* 7 */ H(6A3E), H(6B3A), H(6C3E), H(6D3A), H(6E3A), H(6F3E), H(7022), H(7126), H(7226), H(7322), H(7426), H(7522), H(7622), H(7726), H(782E), H(792A),
/* 8 */ H(7A3A), H(7B3E), H(7C3A), H(7D3E), H(7E3E), H(7F3A), H(8082), H(8186), H(8286), H(8382), H(8486), H(8582), H(8682), H(8786), H(888E), H(898A),
/* 9 */ H(8A9A), H(8B9E), H(8C9A), H(8D9E), H(8E9E), H(8F9A), H(9086), H(9182), H(9282), H(9386), H(3423), H(3527), H(3627), H(3723), H(382B), H(392F),
/* A */ H(3A3F), H(3B3B), H(3C3F), H(3D3B), H(3E3B), H(3F3F), H(4003), H(4107), H(4207), H(4303), H(4407), H(4503), H(4603), H(4707), H(480F), H(490B),
/* B */ H(4A1B), H(4B1F), H(4C1B), H(4D1F), H(4E1F), H(4F1B), H(5007), H(5103), H(5203), H(5307), H(5403), H(5507), H(5607), H(5703), H(580B), H(590F),
/* C */ H(5A1F), H(5B1B), H(5C1F), H(5D1B), H(5E1B), H(5F1F), H(6027), H(6123), H(6223), H(6327), H(6423), H(6527), H(6627), H(6723), H(682B), H(692F),
/* D */ H(6A3F), H(6B3B), H(6C3F), H(6D3B), H(6E3B), H(6F3F), H(7023), H(7127), H(7227), H(7323), H(7427), H(7523), H(7623), H(7727), H(782F), H(792B),
/* E */ H(7A3B), H(7B3F), H(7C3B), H(7D3F), H(7E3F), H(7F3B), H(8083), H(8187), H(8287), H(8383), H(8487), H(8583), H(8683), H(8787), H(888F), H(898B),
/* F */ H(8A9B), H(8B9F), H(8C9B), H(8D9F), H(8E9F), H(8F9B), H(9087), H(9183), H(9283), H(9387), H(9483), H(9587), H(9687), H(9783), H(988B), H(998F),
/* HNC */
/* 111 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ H(9A9F), H(9B9B), H(9C9F), H(9D9B), H(9E9B), H(9F9F), H(A0A7), H(A1A3), H(A2A3), H(A3A7), H(A4A3), H(A5A7), H(A6A7), H(A7A3), H(A8AB), H(A9AF),
/* 1 */ H(AABF), H(ABBB), H(ACBF), H(ADBB), H(AEBB), H(AFBF), H(B0A3), H(B1A7), H(B2A7), H(B3A3), H(B4A7), H(B5A3), H(B6A3), H(B7A7), H(B8AF), H(B9AB),
/* 2 */ H(BABB), H(BBBF), H(BCBB), H(BDBF), H(BEBF), H(BFBB), H(C087), H(C183), H(C283), H(C387), H(C483), H(C587), H(C687), H(C783), H(C88B), H(C98F),
/* 3 */ H(CA9F), H(CB9B), H(CC9F), H(CD9B), H(CE9B), H(CF9F), H(D083), H(D187), H(D287), H(D383), H(D487), H(D583), H(D683), H(D787), H(D88F), H(D98B),
/* 4 */ H(DA9B), H(DB9F), H(DC9B), H(DD9F), H(DE9F), H(DF9B), H(E0A3), H(E1A7), H(E2A7), H(E3A3), H(E4A7), H(E5A3), H(E6A3), H(E7A7), H(E8AF), H(E9AB),
/* 5 */ H(EABB), H(EBBF), H(ECBB), H(EDBF), H(EEBF), H(EFBB), H(F0A7), H(F1A3), H(F2A3), H(F3A7), H(F4A3), H(F5A7), H(F6A7), H(F7A3), H(F8AB), H(F9AF),
/* 6 */ H(FABF), H(FBBB), H(FCBF), H(FDBB), H(FEBB), H(FFBF), H(0047), H(0103), H(0203), H(0307), H(0403), H(0507), H(0607), H(0703), H(080B), H(090F),
/* 7 */ H(0A1F), H(0B1B), H(0C1F), H(0D1B), H(0E1B), H(0F1F), H(1003), H(1107), H(1207), H(1303), H(1407), H(1503), H(1603), H(1707), H(180F), H(190B),
/* 8 */ H(1A1B), H(1B1F), H(1C1B), H(1D1F), H(1E1F), H(1F1B), H(2023), H(2127), H(2227), H(2323), H(2427), H(2523), H(2623), H(2727), H(282F), H(292B),
/* 9 */ H(2A3B), H(2B3F), H(2C3B), H(2D3F), H(2E3F), H(2F3B), H(3027), H(3123), H(3223), H(3327), H(3423), H(3527), H(3627), H(3723), H(382B), H(392F),
/* A */ H(3A3F), H(3B3B), H(3C3F), H(3D3B), H(3E3B), H(3F3F), H(4003), H(4107), H(4207), H(4303), H(4407), H(4503), H(4603), H(4707), H(480F), H(490B),
/* B */ H(4A1B), H(4B1F), H(4C1B), H(4D1F), H(4E1F), H(4F1B), H(5007), H(5103), H(5203), H(5307), H(5403), H(5507), H(5607), H(5703), H(580B), H(590F),
/* C */ H(5A1F), H(5B1B), H(5C1F), H(5D1B), H(5E1B), H(5F1F), H(6027), H(6123), H(6223), H(6327), H(6423), H(6527), H(6627), H(6723), H(682B), H(692F),
/* D */ H(6A3F), H(6B3B), H(6C3F), H(6D3B), H(6E3B), H(6F3F), H(7023), H(7127), H(7227), H(7323), H(7427), H(7523), H(7623), H(7727), H(782F), H(792B),
/* E */ H(7A3B), H(7B3F), H(7C3B), H(7D3F), H(7E3F), H(7F3B), H(8083), H(8187), H(8287), H(8383), H(8487), H(8583), H(8683), H(8787), H(888F), H(898B),
/* F */ H(8A9B), H(8B9F), H(8C9B), H(8D9F), H(8E9F), H(8F9B), H(9087), H(9183), H(9283), H(9387), H(9483), H(9587), H(9687), H(9783), H(988B), H(998F)};
# undef H
#endif
/* MARK: - Types */
typedef zuint8 (* Insn)(Z80 *self);
#ifdef Z80_WITH_FULL_IM0
typedef struct {
Z80* z80;
void* context;
Z80Read fetch;
Z80Read read;
Z80Write write;
Z80Read in;
Z80Write out;
Z80Notify ld_i_a;
Z80Notify ld_r_a;
Z80Notify reti;
Z80Notify retn;
zuint16 pc;
} IM0;
#endif
/* MARK: - Instance Variable and Callback Shortcuts */
#define MEMPTR self->memptr.uint16_value
#define PC self->pc.uint16_value
#define SP self->sp.uint16_value
#define XY self->xy.uint16_value
#define IX self->ix_iy[0].uint16_value
#define IY self->ix_iy[1].uint16_value
#define AF self->af.uint16_value
#define BC self->bc.uint16_value
#define DE self->de.uint16_value
#define HL self->hl.uint16_value
#define AF_ self->af_.uint16_value
#define BC_ self->bc_.uint16_value
#define DE_ self->de_.uint16_value
#define HL_ self->hl_.uint16_value
#define MEMPTRH self->memptr.uint8_values.at_1
#define MEMPTRL self->memptr.uint8_values.at_0
#define PCH self->pc.uint8_values.at_1
#define A self->af.uint8_values.at_1
#define F self->af.uint8_values.at_0
#define B self->bc.uint8_values.at_1
#define C self->bc.uint8_values.at_0
#define E self->de.uint8_values.at_0
#define L self->hl.uint8_values.at_0
#define I self->i
#define R self->r
#define R7 self->r7
#define Q self->q
#define IFF1 self->iff1
#define IFF2 self->iff2
#define IM self->im
#define HALT_LINE self->halt_line
#define INT_LINE self->int_line
#define DATA self->data.uint8_array
#define REQUEST self->request
#define RESUME self->resume
#define OPTIONS self->options
#define CONTEXT self->context
#define FETCH_OPCODE(address) self->fetch_opcode(CONTEXT, address)
#define FETCH(address) self->fetch (CONTEXT, address)
#define READ(address) self->read (CONTEXT, address)
#define WRITE(address, value) self->write (CONTEXT, address, value)
#define IN(port) self->in (CONTEXT, port)
#define OUT(port, value) self->out (CONTEXT, port, value)
#define NOTIFY(callback) if (self->callback != Z_NULL) self->callback(CONTEXT)
/* MARK: - 16-bit Callback Operations */
static Z_ALWAYS_INLINE zuint16 fetch_16(Z80 *self, zuint16 address)
{
zuint8 l = FETCH(address);
return (zuint16)(l | ((zuint16)FETCH(address + 1) << 8));
}
static Z_ALWAYS_INLINE zuint16 read_16(Z80 *self, zuint16 address)
{
zuint8 l = READ(address);
return (zuint16)(l | ((zuint16)READ(address + 1) << 8));
}
static Z_ALWAYS_INLINE void write_16f(Z80 *self, zuint16 address, zuint16 value)
{
WRITE(address, (zuint8)value);
WRITE(address + 1, (zuint8)(value >> 8));
}
static Z_ALWAYS_INLINE void write_16b(Z80 *self, zuint16 address, zuint16 value)
{
WRITE(address + 1, (zuint8)(value >> 8));
WRITE(address, (zuint8)value);
}
#ifndef Z80_WITH_FULL_IM0
static Z_ALWAYS_INLINE zuint16 int_fetch_16(Z80 *self)
{
zuint8 l = self->int_fetch(CONTEXT, PC);
return (zuint16)(l | ((zuint16)self->int_fetch(CONTEXT, PC) << 8));
}
#endif
#define FETCH_16(address) fetch_16 (self, address)
#define READ_16(address) read_16 (self, address)
#define WRITE_16F(address, value) write_16f(self, address, value)
#define WRITE_16B(address, value) write_16b(self, address, value)
/* MARK: - Interrupt Mode 0: Callback Trampolines */
#ifdef Z80_WITH_FULL_IM0
static zuint8 im0_fetch(IM0 const *self, zuint16 address)
{
Z_UNUSED(address)
return self->z80->int_fetch(CONTEXT, self->pc);
}
static zuint8 im0_read(IM0 const *self, zuint16 address)
{return READ(address);}
static void im0_write(IM0 const *self, zuint16 address, zuint8 value)
{WRITE(address, value);}
static zuint8 im0_in(IM0 const *self, zuint16 port)
{return IN(port);}
static void im0_out(IM0 const *self, zuint16 port, zuint8 value)
{OUT(port, value);}
static void im0_ld_i_a(IM0 const *self) {NOTIFY(ld_i_a);}
static void im0_ld_r_a(IM0 const *self) {NOTIFY(ld_r_a);}
# ifdef Z80_WITH_IM0_RETX_NOTIFICATIONS
#define IM0_NOTIFY_RETX(callback) \
if ( self->callback != Z_NULL && \
(self->z80->options & \
Z80_OPTION_IM0_RETX_NOTIFICATIONS) \
) \
{ \
self->z80->data.uint8_array[2] |= 2; \
self->callback(CONTEXT); \
}
static void im0_reti(IM0 const *self) {IM0_NOTIFY_RETX(reti)}
static void im0_retn(IM0 const *self) {IM0_NOTIFY_RETX(retn)}
# endif
#endif
/* MARK: - Interrupt Mode 0: PC Decrements for Unprefixed Instructions */
#ifdef Z80_WITH_FULL_IM0
static zuint8 const im0_pc_decrement_table[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 1 */ 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
/* 2 */ 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
/* 3 */ 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
/* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* C */ 1, 0, 3, 3, 3, 0, 0, 1, 1, 1, 3, 0, 3, 3, 0, 1,
/* D */ 1, 0, 3, 0, 3, 0, 0, 1, 1, 0, 3, 0, 3, 0, 0, 1,
/* E */ 1, 0, 3, 0, 3, 0, 0, 1, 1, 1, 3, 0, 3, 0, 0, 1,
/* F */ 1, 0, 3, 0, 3, 0, 0, 1, 1, 0, 3, 0, 3, 0, 0, 1};
#endif
/* MARK: - Flags */
/*----------------.
| 7 6 5 4 3 2 1 0 |
| S Z Y H X P N C |
'-|-|-|-|-|-|-|-|-'
| | | | | | | '-- carry / borrow
| | | | | | '---- addition / subtraction
| | | | | '------ parity (P) / two's complement signed overflow (V)
| | | | '-------- result's bit 3 (undocumented)
| | | '---------- half carry / half borrow
| | '------------ result's bit 5 (undocumented)
| '-------------- zero
'---------------- sign */
#define SF 128
#define ZF 64
#define YF 32
#define HF 16
#define XF 8
#define PF 4
#define NF 2
#define CF 1
#define SZPCF (SF | ZF | PF | CF)
#define SZPF (SF | ZF | PF )
#define SZCF (SF | ZF | CF )
#define SYXF (SF | YF | XF )
#define ZPF (ZF | PF )
#define YXCF (YF | XF | CF )
#define YXF (YF | XF )
#define HCF (HF | CF )
#define F_SZPC (F & SZPCF)
#define F_SZP (F & SZPF)
#define F_SZC (F & SZCF)
#define F_C (F & CF)
#define A_SYX (A & SYXF)
#define A_YX (A & YXF)
#define ZF_ZERO(value) (!(value) << 6)
#ifdef Z80_WITH_PARITY_COMPUTATION
static Z_ALWAYS_INLINE zuint8 pf_parity(zuint8 value)
{return (zuint8)(((0x9669U >> ((value ^ (value >> 4)) & 0xF)) & 1) << 2);}
# define PF_PARITY pf_parity
#else
static zuint8 const pf_parity_table[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* 1 */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* 2 */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* 3 */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* 4 */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* 5 */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* 6 */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* 7 */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* 8 */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* 9 */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* A */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* B */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* C */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
/* D */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* E */ 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
/* F */ 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4};
# define PF_PARITY(value) pf_parity_table[value]
#endif
/*-----------------------------------------------------------------------------.
| `PF_OVERFLOW` computes PF according to whether signed overflow occurs in the |
| addition or subtraction of two integers. For additions, `rhs` must be passed |
| bit-wise ~inverted. For an explanation of the formula, check: |
| * https://stackoverflow.com/a/199668 |
| * http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Comb/overflow.html |
'=============================================================================*/
#define PF_OVERFLOW(width, result, lhs, rhs) \
(((zuint##width)((lhs ^ rhs) & (lhs ^ result)) >> (width - 3)) & PF)
/*-----------------------------------------------------------------------------.
| Q serves as an abstraction for a set of latches related to flag computation. |
| From an emulation perspective, instructions that affect the flags copy the |
| final value of F to Q, whereas instructions that do not affect the flags |
| (including `ex af,af'`, `pop af`, internal NOPs and interrupt responses) set |
| Q to 0. Q is used to compute YF and XF in the `ccf` and `scf` instructions. |
| |
| References: |
| * https://worldofspectrum.org/forums/discussion/20345 |
| * https://worldofspectrum.org/forums/discussion/41704 |
'=============================================================================*/
#ifdef Z80_WITH_Q
# define FLAGS Q = F
# define Q_0 Q = 0;
#else
# define FLAGS F
# define Q_0
#endif
/* MARK: - Bit Rotation */
#define ROL(value) Z_UINT8_ROTATE_LEFT (value, 1)
#define ROR(value) Z_UINT8_ROTATE_RIGHT(value, 1)
/* MARK: - 8-Bit Register Resolution */
/*---------. .---------------------.
| 76543210 | | J / K | O / P |
|----------| |---------+-----------|
| __jjj___ | | 000 = b | 000 = b |
| _____kkk | | 001 = c | 001 = c |
| __ooo___ | | 010 = d | 010 = d |
| _____ppp | | 011 = e | 011 = e |
'----------' | 100 = h | 100 = XYh |
| 101 = l | 101 = XYl |
| 111 = a | 111 = a |
'--------------------*/
static zusize const j_k_table[8] = {
Z_MEMBER_OFFSET(Z80, bc.uint8_values.at_1),
Z_MEMBER_OFFSET(Z80, bc.uint8_values.at_0),
Z_MEMBER_OFFSET(Z80, de.uint8_values.at_1),
Z_MEMBER_OFFSET(Z80, de.uint8_values.at_0),
Z_MEMBER_OFFSET(Z80, hl.uint8_values.at_1),
Z_MEMBER_OFFSET(Z80, hl.uint8_values.at_0),
0,
Z_MEMBER_OFFSET(Z80, af.uint8_values.at_1)};
static zusize const o_p_table[8] = {
Z_MEMBER_OFFSET(Z80, bc.uint8_values.at_1),
Z_MEMBER_OFFSET(Z80, bc.uint8_values.at_0),
Z_MEMBER_OFFSET(Z80, de.uint8_values.at_1),
Z_MEMBER_OFFSET(Z80, de.uint8_values.at_0),
Z_MEMBER_OFFSET(Z80, xy.uint8_values.at_1),
Z_MEMBER_OFFSET(Z80, xy.uint8_values.at_0),
0,
Z_MEMBER_OFFSET(Z80, af.uint8_values.at_1)};
#define REGISTER_8(table, offset, shift) \
*((zuint8 *)self + table[(DATA[offset] shift) & 7])
#define J0 REGISTER_8(j_k_table, 0, >> 3 )
#define J1 REGISTER_8(j_k_table, 1, >> 3 )
#define K0 REGISTER_8(j_k_table, 0, Z_EMPTY)
#define K1 REGISTER_8(j_k_table, 1, Z_EMPTY)
#define K3 REGISTER_8(j_k_table, 3, Z_EMPTY)
#define O REGISTER_8(o_p_table, 1, >> 3 )
#define P REGISTER_8(o_p_table, 1, Z_EMPTY)
/* MARK: - 16-Bit Register Resolution */
/*---------. .-----------------------------.
| 76543210 | | S | T | W |
|----------| |---------+---------+---------|
| __ss____ | | 00 = bc | 00 = bc | 00 = bc |
| __tt____ | | 01 = de | 01 = de | 01 = de |
| __ww____ | | 10 = hl | 10 = hl | 10 = XY |
'----------' | 11 = sp | 11 = af | 11 = sp |
'----------------------------*/
static zusize const s_table[4] = {
Z_MEMBER_OFFSET(Z80, bc.uint16_value),
Z_MEMBER_OFFSET(Z80, de.uint16_value),
Z_MEMBER_OFFSET(Z80, hl.uint16_value),
Z_MEMBER_OFFSET(Z80, sp.uint16_value)};
static zusize const t_table[4] = {
Z_MEMBER_OFFSET(Z80, bc.uint16_value),
Z_MEMBER_OFFSET(Z80, de.uint16_value),
Z_MEMBER_OFFSET(Z80, hl.uint16_value),
Z_MEMBER_OFFSET(Z80, af.uint16_value)};
static zusize const w_table[4] = {
Z_MEMBER_OFFSET(Z80, bc.uint16_value),
Z_MEMBER_OFFSET(Z80, de.uint16_value),
Z_MEMBER_OFFSET(Z80, xy.uint16_value),
Z_MEMBER_OFFSET(Z80, sp.uint16_value)};
#define REGISTER_16(table, offset) \
*(zuint16 *)(void *)((zchar *)self + table[(DATA[offset] >> 4) & 3])
#define SS0 REGISTER_16(s_table, 0)
#define SS1 REGISTER_16(s_table, 1)
#define TT REGISTER_16(t_table, 0)
#define WW REGISTER_16(w_table, 1)
/* MARK: - Condition Evaluation */
/*---------. .----------.
| 76543210 | | Z |
|----------| |----------|
| __zzz___ | | 000 = nz |
| ___zz___ | | 001 = z |
'----------' | 010 = nc |
| 011 = c |
| 100 = po |
| 101 = pe |
| 110 = p |
| 111 = m |
'---------*/
static zuint8 const z_table[8] = {ZF, ZF, CF, CF, PF, PF, SF, SF};
static Z_ALWAYS_INLINE zsint zzz(Z80 const *self, zuint8 mask)
{
zsint z = (DATA[0] >> 3) & mask;
return !(F & z_table[z]) ^ (z & 1);
}
/* MARK: - 8-Bit Arithmetic and Logical Operations */
/*---------. .---------------------.
| 76543210 | | U SZYHXPNC |
|----------| |---------------------|
| __uuu___ | | 000 = add szycxv0c |
| _____10v | | 001 = adc szycxv0c |
'----------' | 010 = sub szybxv1b |
| 011 = sbc szybxv1b |
| 100 = and szy1xp00 |
| 101 = xor szy0xp00 |
| 110 = or szy0xp00 |
| 111 = cp sz*b*v1b |
|---------------------|
| V SZYHXPNC |
|---------------------|
| 100 = inc szycxv0. |
| 101 = dec szybxv1. |
'--------------------*/
static void uuu(Z80 *self, zuint8 offset, zuint8 rhs)
{
zuint8 t, f;
switch ((DATA[offset] >> 3) & 7)
{
case 0: /* add */
t = A + rhs;
f = ((zuint)A + rhs > 255) | /* CF = carry */
PF_OVERFLOW(8, t, A, ~rhs) | /* PF = overflow */
((A ^ rhs ^ t) & HF); /* HF = half-carry */
/* NF = 0 */
A = t;
break;
case 1: /* adc */
t = A + rhs + (f = F_C);
f = ((zuint)A + rhs + f > 255) | /* CF = carry */
PF_OVERFLOW(8, t, A, ~rhs) | /* PF = overflow */
((A ^ rhs ^ t) & HF); /* HF = half-carry */
/* NF = 0 */
A = t;
break;
case 2: /* sub */
t = A - rhs;
f = (A < rhs) | /* CF = borrow */
NF | /* NF = 1 */
PF_OVERFLOW(8, t, A, rhs) | /* PF = overflow */
((A ^ rhs ^ t) & HF); /* HF = half-borrow */
A = t;
break;
case 3: /* sbc */
t = A - rhs - (f = F_C);
f = ((zsint)A - rhs - f < 0) | /* CF = borrow */
NF | /* NF = 1 */
PF_OVERFLOW(8, t, A, rhs) | /* PF = overflow */
((A ^ rhs ^ t) & HF); /* HF = half-borrow */
A = t;
break;
case 4: /* and */
A &= rhs;
f = HF | PF_PARITY(A); /* HF = 1; PF = parity */
break; /* NF, CF = 0 */
case 5: /* xor */
A ^= rhs;
f = PF_PARITY(A); /* PF = parity */
break; /* HF, NF, CF = 0 */
case 6: /* or */
A |= rhs;
f = PF_PARITY(A); /* PF = parity */
break; /* HF, NF, CF = 0 */
case 7: /* cp */
t = A - rhs;
FLAGS = (zuint8)(
(t & SF) | /* SF = sign */
ZF_ZERO(t) | /* ZF = zero */
((A ^ rhs ^ t) & HF) | /* HF = half-borrow */
PF_OVERFLOW(8, t, A, rhs) | /* PF = overflow */
(A < rhs) | /* CF = borrow */
(rhs & YXF) | /* YF = rhs.5; XF = rhs.3 */
NF); /* NF = 1 */
return;
}
FLAGS = (zuint8)(
f | /* HF, PF, NF and CF already computed */
A_SYX | /* SF = sign; YF = Y; XF = X */
ZF_ZERO(A)); /* ZF = zero */
}
static zuint8 vvv(Z80 *self, zuint8 offset, zuint8 value)
{
zuint8 dec = DATA[offset] & 1;
zuint8 nf = (zuint8)(dec << 1);
zuint8 t = value + 1 - nf;
FLAGS = (zuint8)(
(t & SYXF) | /* SF = sign; YF = Y; XF = X */
ZF_ZERO(t) | /* ZF = zero */
((value ^ t) & HF) | /* HF = half-carry/borrow */
((value == 127 + dec) << 2) | /* PF = overflow */
nf | /* NF = 0 (inc), 1 (dec) */
F_C); /* CF unchanged */
return t;
}
/* MARK: - Rotation and Shift Operations */
/*---------. .-----------.
| 76543210 | | G |
|----------| |-----------|
| __ggg___ | | 000 = rlc |
'----------' | 001 = rrc |
| 010 = rl |
| 011 = rr |
| 100 = sla |
| 101 = sra |
| 110 = sll |
| 111 = srl |
'----------*/
static zuint8 ggg(Z80 *self, zuint8 offset, zuint8 value)
{
zuint8 cf;
switch ((DATA[offset] >> 3) & 7)
{
/* rlc .----------------.
.----. | .---------. |
| CF |<-----| 7 <-- 0 |<--'
'----' '--------*/
case 0:
cf = (value = ROL(value)) & 1;
break;
/* rrc .----------------.
| .---------. | .----.
'-->| 7 --> 0 |----->| CF |
'---------' '---*/
case 1:
cf = value & 1;
value = ROR(value);
break;
/* rl .-------------------------.
| .----. .---------. |
'--| CF |<--| 7 <-- 0 |<--'
'----' '--------*/
case 2:
cf = value >> 7;
value = (zuint8)((value << 1) | F_C);
break;
/* rr .-------------------------.
| .---------. .----. |
'-->| 7 --> 0 |-->| CF |--'
'---------' '---*/
case 3:
cf = value & 1;
value = (zuint8)((value >> 1) | ((zuint8)F_C << 7));
break;
/* sla .----. .---------.
| CF |<--| 7 <-- 0 |<-- 0
'----' '--------*/
case 4:
cf = value >> 7;
value <<= 1;
break;
/* sra .---------. .----.
.-->| 7 --> 0 |-->| CF |
| '-|-------' '----'
'----*/
case 5:
cf = value & 1;
value = (zuint8)((value & 128) | (value >> 1));
break;
/* sll .----. .---------.
| CF |<--| 7 <-- 0 |<-- 1
'----' '--------*/
case 6:
cf = value >> 7;
value = (zuint8)((value << 1) | 1);
break;
/* srl .---------. .----.
0 -->| 7 --> 0 |-->| CF |
'---------' '---*/
case 7:
cf = value & 1;
value >>= 1;
break;
/* Uncoment to avoid compiler warnings */
/*default: cf = 0;*/
}
FLAGS = (zuint8)( /* HF, NF = 0 */
(value & SYXF) | /* SF = sign; YF = Y; XF = X */
ZF_ZERO(value) | /* ZF = zero */
PF_PARITY(value) | /* PF = parity */
cf); /* CF already computed */
return value;
}
/* MARK: - Bit Set and Reset Operations */
/*---------. .---------.
| 76543210 | | M |
|----------| |---------|
| _m______ | | 0 = res |
'----------' | 1 = set |
'--------*/
static Z_ALWAYS_INLINE zuint8 m(Z80 *self, zuint8 offset, zuint8 value)
{
zuint8 t;
Q_0
t = DATA[offset];
return (zuint8)((t & 64)
? value | (1U << ((t >> 3) & 7))
: value & ~(1U << ((t >> 3) & 7)));
}
/* MARK: - Function Shortcuts and Reusable Code */
#define INSN(name) static zuint8 name(Z80 *self)
#define N(offset) ((DATA[offset] >> 3) & 7)
#define Z(mask) zzz(self, mask)
#define U0(value) uuu(self, 0, value)
#define U1(value) uuu(self, 1, value)
#define V0(value) vvv(self, 0, value)
#define V1(value) vvv(self, 1, value)
#define G1(value) ggg(self, 1, value)
#define G3(value) ggg(self, 3, value)
#define M1(value) m (self, 1, value)
#define M3(value) m (self, 3, value)
#define PUSH(value) WRITE_16B(SP -= 2, value)
#define R_ALL ((R & 127) | (R7 & 128))
#define RET MEMPTR = PC = READ_16(SP); SP += 2
#define FETCH_XY_EA(address) MEMPTR = (zuint16)(XY + (zsint8)FETCH(address))
#define IS_XY_PREFIX(opcode) ((opcode) & 0xDF) == 0xDD
#define EX(a, b) t = a; a = b; b = t
#define LD_A_IR(rhs) \
A = rhs; \
\
FLAGS = (zuint8)( /* HF, NF = 0 */ \
A_SYX | /* SF = sign; YF = Y; XF = X */ \
ZF_ZERO(A) | /* ZF = zero */ \
(IFF2 << 2) | /* PF = IFF2 */ \
F_C); /* CF unchanged */ \
\
PC += 2; \
return 9
#define LDX(operator) \
zuint8 t = READ(HL operator); \
\
WRITE(DE operator, t); \
t += A; \
\
FLAGS = (zuint8)( /* HF, NF = 0 */ \
F_SZC | /* SF, ZF, CF unchanged */ \
((t & 2) << 4) | /* YF = (A + [HLi]).1 */ \
(t & XF) | /* XF = (A + [HLi]).3 */ \
(!!(--BC) << 2)); /* PF = !!BCo */ \
\
PC += 2; \
return 16
#define LDXR(operator) \
zuint8 t = READ(HL operator); \
\
WRITE(DE operator, t); \
t += A; \
\
if (--BC) \
{ /* HF, NF = 0 */ \
FLAGS = F_SZC | /* SF, ZF, CF unchanged */ \
((PC >> 8) & YXF) | /* YF = PCi.13; XF = PCi.11 */ \
PF; /* PF = 1 */ \
\
MEMPTR = PC + 1; \
return 21; \
} \
\
FLAGS = (zuint8)( /* HF, PF, NF = 0 */ \
F_SZC | /* SF, ZF, CF unchanged */ \
((t & 2) << 4) | /* YF = (A + [HLi]).1 */ \
(t & XF)); /* XF = (A + [HLi]).3 */ \
\
PC += 2; \
return 16
#define CPX(operator) \
zuint8 n = READ(HL operator); \
zuint8 t0 = A - n; \
zuint8 hf = (A ^ n ^ t0) & HF; \
zuint8 t1 = t0 - (hf >> 4); \
\
FLAGS = (zuint8)( \
(t0 & SF) | /* SF = sign */ \
ZF_ZERO(t0) | /* ZF = zero */ \
hf | /* HF = half-borrow */ \
((t1 & 2) << 4) | /* YF = (A - [HLi] - HFo).1 */ \
(t1 & XF) | /* XF = (A - [HLi] - HFo).3 */ \
(!!(--BC) << 2) | /* PF = !!BCo */ \
NF | /* NF = 1 */ \
F_C); /* CF unchanged */ \
\
MEMPTR operator; \
PC += 2; \
return 16
#define CPXR(operator) \
zuint8 n = READ(HL operator); \
zuint8 t0 = A - n; \
zuint8 hf = (A ^ n ^ t0) & HF; \
zuint8 t1 = t0 - (hf >> 4); \
\
zuint8 f = (zuint8)( \
(t0 & SF) | /* SF = sign */ \
ZF_ZERO(t0) | /* ZF = zero */ \
hf | /* HF = half-borrow */ \
(!!(--BC) << 2) | /* PF = !!BCo */ \
NF | /* NF = 1 */ \
F_C); /* CF unchanged */ \
\
if (t0 && BC) \
{ \
/* YF = PCi.13; XF = PCi.11 */ \
FLAGS = f | ((PC >> 8) & YXF); \
MEMPTR = PC + 1; \
return 21; \