-
Notifications
You must be signed in to change notification settings - Fork 3
/
Z80.c
1661 lines (1388 loc) · 69.1 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
/* ______ ______ ______
/\___ \ /\ __ \/\ \
\/__/ /_\ \ __ \ \ \ \
/\_____\ \_____\ \_____\
Zilog \/_____/\/_____/\/_____/ CPU Emulator
Copyright (C) 1999-2018 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 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
this emulator. If not, see <http://www.gnu.org/licenses/>. */
#ifndef CPU_Z80_DEPENDENCIES_H
# include <Z/macros/value.h>
# include <Z/macros/pointer.h>
#endif
#if defined(CPU_Z80_HIDE_API)
# define CPU_Z80_API static
#elif defined(CPU_Z80_STATIC)
# define CPU_Z80_API
#else
# define CPU_Z80_API Z_API_EXPORT
#endif
#if defined(CPU_Z80_WITH_MODULE_ABI) && !defined(CPU_Z80_WITH_ABI)
# define CPU_Z80_WITH_ABI
#endif
#ifdef CPU_Z80_WITH_ABI
# if defined(CPU_Z80_HIDE_ABI)
# define CPU_Z80_ABI static
# elif defined(CPU_Z80_STATIC)
# define CPU_Z80_ABI
# else
# define CPU_Z80_ABI Z_API_EXPORT
# endif
#endif
#if defined(CPU_Z80_USE_LOCAL_HEADER)
# include "Z80.h"
#else
# include <emulation/CPU/Z80.h>
#endif
/* MARK: - Types */
typedef zuint8 (* Instruction)(Z80 *object);
/* MARK: - Macros: External */
#define O(member) Z_OFFSET_OF(Z80, member)
#define ROL(value) value = (zuint8)Z_8BIT_ROTATE_LEFT( value, 1)
#define ROR(value) value = (zuint8)Z_8BIT_ROTATE_RIGHT(value, 1)
/* MARK: - Macros & Functions: Callback */
#define READ_8(address) object->read (object->context, (zuint16)(address))
#define WRITE_8(address, value) object->write (object->context, (zuint16)(address), (zuint8)(value))
#define IN(port) object->in (object->context, (zuint16)(port ))
#define OUT(port, value) object->out (object->context, (zuint16)(port ), (zuint8)(value))
#define INT_DATA object->int_data(object->context)
#define READ_OFFSET(address) ((zsint8)READ_8(address))
#define SET_HALT if (object->halt != NULL) object->halt(object->context, TRUE )
#define CLEAR_HALT if (object->halt != NULL) object->halt(object->context, FALSE)
#define HOOK(address) if (object->hook != NULL) object->hook(object->context, (address)) /* SNO */
static Z_INLINE zuint16 read_16bit(Z80 *object, zuint16 address)
{return (zuint16)(READ_8(address) | (zuint16)READ_8(address + 1) << 8);}
static Z_INLINE void write_16bit(Z80 *object, zuint16 address, zuint16 value)
{
WRITE_8(address, (zuint8)value);
WRITE_8(address + 1, value >> 8);
}
#define READ_16(address) read_16bit (object, (zuint16)(address))
#define WRITE_16(address, value) write_16bit(object, (zuint16)(address), (zuint16)(value))
/* MARK: - Macros: Registers */
#define AF object->state.Z_Z80_STATE_MEMBER_AF
#define BC object->state.Z_Z80_STATE_MEMBER_BC
#define DE object->state.Z_Z80_STATE_MEMBER_DE
#define HL object->state.Z_Z80_STATE_MEMBER_HL
#define IX object->state.Z_Z80_STATE_MEMBER_IX
#define IY object->state.Z_Z80_STATE_MEMBER_IY
#define PC object->state.Z_Z80_STATE_MEMBER_PC
#define SP object->state.Z_Z80_STATE_MEMBER_SP
#define AF_ object->state.Z_Z80_STATE_MEMBER_AF_
#define BC_ object->state.Z_Z80_STATE_MEMBER_BC_
#define DE_ object->state.Z_Z80_STATE_MEMBER_DE_
#define HL_ object->state.Z_Z80_STATE_MEMBER_HL_
#define A object->state.Z_Z80_STATE_MEMBER_A
#define F object->state.Z_Z80_STATE_MEMBER_F
#define B object->state.Z_Z80_STATE_MEMBER_B
#define C object->state.Z_Z80_STATE_MEMBER_C
#define L object->state.Z_Z80_STATE_MEMBER_L
#define I object->state.Z_Z80_STATE_MEMBER_I
#define R object->state.Z_Z80_STATE_MEMBER_R
#define R_ALL ((R & 127) | (R7 & 128))
/* MARK: - Macros: Internal Bits */
#define HALT object->state.Z_Z80_STATE_MEMBER_HALT
#define IFF1 object->state.Z_Z80_STATE_MEMBER_IFF1
#define IFF2 object->state.Z_Z80_STATE_MEMBER_IFF2
#define EI object->state.Z_Z80_STATE_MEMBER_EI
#define IM object->state.Z_Z80_STATE_MEMBER_IM
#define NMI object->state.Z_Z80_STATE_MEMBER_NMI
#define INT object->state.Z_Z80_STATE_MEMBER_IRQ
/* MARK: - Macros: Temporal Data */
#define CYCLES object->cycles
#define R7 object->r7
#define BYTE(index) object->data.array_uint8[index]
#define BYTE0 BYTE(0)
#define BYTE1 BYTE(1)
#define BYTE2 BYTE(2)
#define BYTE3 BYTE(3)
#define XY object->xy.value_uint16
#define XY_ADDRESS ((zuint16)(XY + object->data.array_sint8[2]))
/* MARK: - Macros: Flags */
#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 SZPF (SF | ZF | PF)
#define SYXF (SF | YF | XF)
#define ZPF (ZF | PF )
#define YXCF (YF | XF | CF)
#define YXF (YF | XF )
#define PNF (PF | NF )
#define HCF (HF | CF )
#define F_H (F & HF)
#define F_N (F & NF)
#define F_C (F & CF)
#define F_SZP (F & SZPF)
#define A_SYX (A & SYXF)
#define A_YX (A & YXF)
#define ZF_ZERO(value) (!(value) << 6)
/* MARK: - P/V Flag Computation */
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]
#define VF(function, operand) \
static Z_INLINE zuint8 pf_overflow_##function##8(zuint8 a, zuint8 b) \
{ \
zsint total = ((zsint)((zsint8)a)) operand ((zsint)((zsint8)b)); \
\
return total < -128 || total > 127 ? PF : 0; \
}
VF(add, +)
VF(sub, -)
#undef VF
#define VF(function, bits, type, operand, minimum, maximum) \
static Z_INLINE zuint8 pf_overflow_##function##bits(zuint##bits a, zuint##bits b, zuint8 carry) \
{ \
type total = ((type)((zsint##bits)a)) operand ((type)((zsint##bits)b)) operand carry; \
\
return total < minimum || total > maximum ? PF : 0; \
}
VF(adc, 8, zsint, +, -128, 127)
VF(sbc, 8, zsint, -, -128, 127)
VF(adc, 16, zsint32, +, -32768, 32767)
VF(sbc, 16, zsint32, -, -32768, 32767)
/* MARK: - 8-Bit Register Resolution
.----------. .---------. .-----------. .-----------.
| 76543210 | | X / Y | | J / K | | P / Q |
|----------| |---------| |-----------| |-----------|
| __xxx___ | | 000 = b | | 000 = b | | 000 = b |
| _____yyy | | 001 = c | | 001 = c | | 001 = c |
| __jjj___ | | 010 = d | | 010 = d | | 010 = d |
| _____kkk | | 011 = e | | 011 = e | | 011 = e |
| __ppp___ | | 100 = h | | 100 = ixh | | 100 = iyh |
| _____qqq | | 101 = l | | 101 = ixl | | 101 = iyl |
'----------' | 111 = a | | 111 = a | | 111 = a |
'---------' '-----------' '----------*/
static zuint8 const x_y_table[8] = {
O(state.Z_Z80_STATE_MEMBER_B),
O(state.Z_Z80_STATE_MEMBER_C),
O(state.Z_Z80_STATE_MEMBER_D),
O(state.Z_Z80_STATE_MEMBER_E),
O(state.Z_Z80_STATE_MEMBER_H),
O(state.Z_Z80_STATE_MEMBER_L),
0,
O(state.Z_Z80_STATE_MEMBER_A)
};
static zuint8 const j_k_p_q_table[8] = {
O(state.Z_Z80_STATE_MEMBER_B),
O(state.Z_Z80_STATE_MEMBER_C),
O(state.Z_Z80_STATE_MEMBER_D),
O(state.Z_Z80_STATE_MEMBER_E),
O(xy.values_uint8.index1 ),
O(xy.values_uint8.index0 ),
0,
O(state.Z_Z80_STATE_MEMBER_A)
};
#define R_8(name, table, offset, mask, shift) \
static Z_INLINE zuint8 *name(Z80 *object) \
{return ((zuint8 *)object) + table[(BYTE(offset) & mask) shift];}
R_8(__xxx___0, x_y_table, 0, 56, >> 3 )
R_8(__xxx___1, x_y_table, 1, 56, >> 3 )
R_8(_____yyy0, x_y_table, 0, 7, Z_EMPTY)
R_8(_____yyy1, x_y_table, 1, 7, Z_EMPTY)
R_8(_____yyy3, x_y_table, 3, 7, Z_EMPTY)
R_8(__jjj___ , j_k_p_q_table, 1, 56, >> 3 )
R_8(_____kkk , j_k_p_q_table, 1, 7, 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 |
'----------' | 10 = hl | | 10 = hl | | 10 = XY |
| 11 = sp | | 11 = af | | 11 = sp |
'---------' '---------' '--------*/
static zuint8 const s_table[4] = {
O(state.Z_Z80_STATE_MEMBER_BC),
O(state.Z_Z80_STATE_MEMBER_DE),
O(state.Z_Z80_STATE_MEMBER_HL),
O(state.Z_Z80_STATE_MEMBER_SP)
};
static zuint8 const t_table[4] = {
O(state.Z_Z80_STATE_MEMBER_BC),
O(state.Z_Z80_STATE_MEMBER_DE),
O(state.Z_Z80_STATE_MEMBER_HL),
O(state.Z_Z80_STATE_MEMBER_AF)
};
static zuint8 const w_table[4] = {
O(state.Z_Z80_STATE_MEMBER_BC),
O(state.Z_Z80_STATE_MEMBER_DE),
O(xy ),
O(state.Z_Z80_STATE_MEMBER_SP)
};
#define R_16(name, table, offset) \
static Z_INLINE zuint16 *name(Z80 *object) \
{return Z_BOP(zuint16 *, object, table[(BYTE(offset) & 48) >> 4]);}
R_16(__ss____0, s_table, 0)
R_16(__ss____1, s_table, 1)
R_16(__tt____ , t_table, 0)
/* MARK: - Condition Resolution
.----------. .----------.
| 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_INLINE zboolean __zzz___(Z80 *object)
{
zuint8 z = (BYTE0 & 56) >> 3;
return (F & (z_table[z]))
? (z & 1) /* Flag is 1 */
: !(z & 1); /* Flag is 0 */
}
/* MARK: - 8-Bit Arithmetic and Logical Operation Resolution and Execution
.----------. .-----------. .-------------------------------.
| 76543210 | | U | | S | Z | Y | H | X | P | N | C |
|----------| |-----------| .-----+---+---+---+---+---+---+---+---|
| __uuu___ | | 000 = add | | add | S | Z | 5 | H | 3 | V | 0 | C |
| _____vvv | | 001 = adc | |-----+---+---+---+---+---+---+---+---|
'----------' | 010 = sub | | adc | S | Z | 5 | H | 3 | V | 0 | C |
| 011 = sbc | |-----+---+---+---+---+---+---+---+---|
| 100 = and | | sub | S | Z | 5 | H | 3 | V | 1 | C |
| 101 = xor | |-----+---+---+---+---+---+---+---+---|
| 110 = or | | sbc | S | Z | 5 | H | 3 | V | 1 | C |
| 111 = cp | |-----+---+---+---+---+---+---+---+---|
|-----------| | and | S | Z | 5 | 1 | 3 | P | 0 | 0 |
| V | |-----+---+---+---+---+---+---+---+---|
|-----------| | xor | S | Z | 5 | 0 | 3 | P | 0 | 0 |
| 100 = inc | |-----+---+---+---+---+---+---+---+---|
| 101 = dec | | or | S | Z | 5 | 0 | 3 | P | 0 | 0 |
'-----------' |-----+---+---+---+---+---+---+---+---|
| cp | S | Z |v.5| H |v.3| V | 1 | C |
|-----+---+---+---+---+---+---+---+---|
| inc | S | Z |v.5| H |v.3| V | 0 | . |
|-----+---+---+---+---+---+---+---+---|
| dec | S | Z |v.5| H |v.3| V | 1 | . |
'------------------------------------*/
static void __uuu___(Z80 *object, zuint8 offset, zuint8 value)
{
zuint8 t;
switch ((object->data.array_uint8[offset] >> 3) & 7)
{
case 0: /* ADD */
t = A + value;
F = ((zuint)A + value > 255) /* CF = Carry */
| pf_overflow_add8(A, value) /* PF = Overflow */
| ((A ^ value ^ t) & HF); /* HF = Half-carry */
A = t; /* NF = 0 */
break;
case 1: /* ADC */
t = F_C;
F = ((zuint)A + value + t > 255) /* CF = Carry */
| pf_overflow_adc8(A, value, t) /* PF = Overflow */
| (((A & 0xF) + (value & 0xF) + t) & HF); /* HF = Half-carry */
/* NF = 0 */
A += value + t;
break;
case 2: /* SUB */
t = A - value;
F = (A < value) /* CF = Borrow */
| NF /* NF = 1 */
| pf_overflow_sub8(A, value) /* PF = Overflow */
| ((A ^ value ^ t) & HF); /* HF = Half-Borrow */
A = t;
break;
case 3: /* SBC */
t = F_C;
F = ((zsint)A - (zsint)value - (zsint)t < 0) /* CF = Borrow */
| NF /* NF = 1 */
| pf_overflow_sbc8(A, value, t) /* PF = Overflow */
| (((A & 0xF) - (value & 0xF) - t) & HF); /* HF = Half-Borrow */
A -= value + t;
break;
case 4: /* AND */
A &= value;
F = HF | PF_PARITY(A); /* HF = 1; PF = Parity */
break; /* NF, CF = 0 */
case 5: /* XOR */
A ^= value;
F = PF_PARITY(A); /* PF = Parity */
break; /* HF, NF, CF = 0 */
case 6: /* OR */
A |= value;
F = PF_PARITY(A); /* PF = Parity */
break; /* HF, NF, CF = 0 */
case 7: /* CP */
t = A - value;
F = (zuint8)
((A < value) /* CF = Borrow */
| NF /* NF = 1 */
| pf_overflow_sub8(A, value) /* PF = Overflow */
| ((A ^ value ^ t) & HF) /* HF = Half-Borrow */
| (value & YXF) /* YF = v.5, XF = v.3 */
| ZF_ZERO(t) /* ZF = !(A - v) */
| (t & SF)); /* SF = (A - v).7 */
return;
}
F = (zuint8)
((F & (HF | PF | NF | CF)) /* CF, NF, PF and HF already changed */
| A_SYX /* SF = A.7; YF = A.5; XF = A.3 */
| ZF_ZERO(A)); /* ZF = !A */
}
static zuint8 _____vvv(Z80 *object, zuint8 offset, zuint8 value)
{
zuint8 t, pn;
/* DEC */
if (object->data.array_uint8[offset] & 1)
{ /* PF = Overflow */
pn = value == 128 ? PNF : NF; /* NF = 1 */
t = value - 1;
}
/* INC */
else { /* PF = Overflow */
pn = value == 127 ? PF : 0; /* NF = 0 */
t = value + 1;
}
F = (zuint8)
((F & CF) /* CF unchanged */
| pn /* PF and NF already calculated */
| (t & SYXF) /* SF = v.7; YF = v.5; XF = v.3 */
| ((value ^ 1 ^ t) & HF) /* HF = Half-Borrow */
| ZF_ZERO(t)); /* ZF = !v */
return t;
}
/* MARK: - Rotation and Shift Operation Resolution and Execution
.----------. .-----------.
| 76543210 | | G |
|----------| |-----------|
| __ggg___ | | 000 = rlc |
'----------' | 001 = rrc |
| 010 = rl |
| 011 = rr |
| 100 = sla |
| 101 = sra |
| 110 = sll |
| 111 = srl |
'----------*/
static zuint8 __ggg___(Z80 *object, zuint8 offset, zuint8 value)
{
zuint8 c;
switch ((object->data.array_uint8[offset] >> 3) & 7)
{
/* RLC .----------------.
.----. | .---------. |
| CF |<-----| 7 <-- 0 |<--'
'----' '--------*/
case 0:
ROL(value);
c = value & CF;
break;
/* RRC .----------------.
| .---------. | .----.
'-->| 7 --> 0 |----->| CF |
'---------' '---*/
case 1:
c = value & CF;
ROR(value);
break;
/* RL .-------------------------.
| .----. .---------. |
'--| CF |<--| 7 <-- 0 |<--'
'----' '--------*/
case 2:
c = value >> 7;
value = (zuint8)((value << 1) | F_C);
break;
/* RR .-------------------------.
| .---------. .----. |
'-->| 7 --> 0 |-->| CF |--'
'---------' '---*/
case 3:
c = value & CF;
value = (zuint8)((value >> 1) | (F_C << 7));
break;
/* SLA .----. .---------.
| CF |<--| 7 <-- 0 |<-- 0
'----' '--------*/
case 4:
c = value >> 7;
value <<= 1;
break;
/* SRA .---------. .----.
.-->| 7 --> 0 |-->| CF |
| '---------' '----'
| |
'----*/
case 5:
c = value & CF;
value = (value & 128) | (value >> 1);
break;
/* SLL .----. .---------.
| CF |<--| 7 <-- 0 |<-- 1
'----' '--------*/
case 6:
c = value >> 7;
value = (value << 1) & 1;
break;
/* SRL .---------. .----.
0 -->| 7 --> 0 |-->| CF |
'---------' '---*/
case 7:
c = value & CF;
value >>= 1;
break;
/* Uncoment to Avoid a compiler warning */
/*default: c = 0; break;*/
}
F = (zuint8)((value & SYXF) | ZF_ZERO(value) | PF_PARITY(value) | c);
return value;
}
/* MARK: - Bit Set and Reset Operation Resolution and Execution
.----------. .---------.
| 76543210 | | M |
|----------| |---------|
| _m______ | | 0 = res |
'----------' | 1 = set |
'--------*/
static Z_INLINE zuint8 _m______(Z80 *object, zuint8 offset, zuint8 value)
{
zuint8 t = object->data.array_uint8[offset];
return (zuint8)((t & 64)
? value | (1 << ((t & 56) >> 3)) /* SET */
: value & ~(1 << ((t & 56) >> 3))); /* RES */
}
/* MARK: - Macros: Shortening */
#define N(x) ((BYTE##x & 56) >> 3)
#define X0 (*__xxx___0(object))
#define X1 (*__xxx___1(object))
#define Y0 (*_____yyy0(object))
#define Y1 (*_____yyy1(object))
#define Y3 (*_____yyy3(object))
#define JP (*__jjj___ (object))
#define KQ (*_____kkk (object))
#define SS0 (*__ss____0(object))
#define SS1 (*__ss____1(object))
#define TT (*__tt____ (object))
#define Z __zzz___ (object)
#define U0(value) __uuu___ (object, 0, value)
#define U1(value) __uuu___ (object, 1, value)
#define V0(value) _____vvv (object, 0, value)
#define V1(value) _____vvv (object, 1, value)
#define G1(value) __ggg___ (object, 1, value)
#define G3(value) __ggg___ (object, 3, value)
#define M1(value) _m______ (object, 1, value)
#define M3(value) _m______ (object, 3, value)
#define WW (*Z_BOP(zuint16 *, object, w_table[(BYTE1 >> 4) & 3]))
/* MARK: - Macros & Functions: Reusable Code */
#define INSTRUCTION(name) static zuint8 name(Z80 *object)
#define EXIT_HALT if (HALT) {PC++; HALT = FALSE; CLEAR_HALT;}
#define PUSH(value) WRITE_16(SP -= 2, value)
#define LD_A_I_LD_A_R \
F = (zuint8) /* HF = 0 / NF = 0 */ \
(A_SYX /* SF = A.7; YF = A.5; XF = A.3 */ \
| ZF_ZERO(A) /* ZF = !A */ \
| (IFF2 << 2) /* PF = IFF2 */ \
| F_C); /* CF unchanged */
#define EX(a, b) t = a; a = b; b = t;
#define EX_VSP_X(register) \
t = READ_16(SP); \
WRITE_16(SP, register); \
register = t;
#define LDX(operator) \
zuint8 n; \
\
PC += 2; \
WRITE_8(DE operator, n = READ_8(HL operator)); \
n += A; \
\
F = (zuint8) /* HF = 0, NF = 0 */ \
((F & (SF | ZF | CF)) /* SF, ZF, CF unchanged */ \
| ((n & 2) << 4) /* YF = ([HL] + A).1 */ \
| (n & XF) /* XF = ([HL] + A).3 */ \
| (!!(--BC) << 2)); /* PF = 1 if BC != 0, else PF = 0 */
#define LDXR(operator) \
LDX(operator) \
if (!BC) return 16; \
PC -= 2; return 21;
#define CPX(operator) \
zuint8 v, n0, n1; \
\
PC += 2; \
n1 = (n0 = A - (v = READ_8(HL operator))) - !!F_H; \
\
F = (zuint8) \
((n0 & SF) /* SF = (A - [HL]).7 */ \
| ZF_ZERO(n0) /* ZF = !(A - [HL]) */ \
| ((A ^ v ^ n0) & HF) /* HF = borrow from bit 5 */ \
| ((n1 & 2) << 4) /* YF = (A - [HL] - HF).1 */ \
| (n1 & XF) /* XF = (A - [HL] - HF).3 */ \
| (!!(--BC) << 2) /* PF = !!BC */ \
| NF /* NF = 1 */ \
| F_C); /* CF unchanged */
#define CPXR(operator) \
CPX(operator) \
if (!BC || !n0) return 16; \
PC -= 2; return 21;
static Z_INLINE void add_RR_NN(Z80 *object, zuint16 *r, zuint16 v)
{
zuint16 t = *r + v;
F = F_SZP /* SF, ZF, PF unchanged */
| ((t >> 8) & YXF) /* YF = RR.13; XF = RR.11 */
| (((*r ^ v ^ t) >> 8) & HF) /* HF = RRh half-carry */
| ((zuint32)*r + v > 65535); /* CF = Carry */
*r = t; /* NF = 0 */
}
#define ADD_RR_NN(register, value) \
add_RR_NN(object, (zuint16 *)®ister, value);
#define ADC_SBC_HL_SS(function, sign, cf_test, set_nf) \
zuint8 c = F_C; \
zuint16 v = SS1, t = HL sign v sign c; \
\
F = (zuint8) \
(((t >> 8) & SYXF) /* SF = HL.15; YF = HL.13; XF = HL.11 */ \
| ZF_ZERO(t) /* ZF = !HL */ \
| ((((HL & 0xFFF) sign (v & 0xFFF) sign c) >> 8) & HF) /* HF = Half-carry of H */ \
| pf_overflow_##function##16(HL, v, c) /* PF = Overflow */ \
| !!(cf_test) /* CF = Carry */ \
set_nf); /* ADC: NF = 0; SBC: NF = 1 */ \
\
HL = t; \
PC += 2; \
return 15;
#define RXA \
F = F_SZP /* SF, ZF, PF unchanged */ \
| A_YX /* YF = A.5; XF = A.3 */ \
| c; /* CF = Ai.7 (rla) / Ai.0 (rra) */
#define RXD(a, b, c) \
zuint8 t; \
\
PC += 2; \
WRITE_8(HL, ((t = READ_8(HL)) a 4) | (A b)); \
A = (A & 0xF0) | (t c); \
\
F = (zuint8) \
(A_SYX /* SF = A.7; YF = A.5; XF = A.3 */ \
| ZF_ZERO(A) /* ZF = !A */ \
| PF_PARITY(A) /* PF = Parity of A */ \
| F_C); /* CF unchanged */ \
/* HF = 0, NF = 0; */
#define BIT_N_VALUE(value) \
zuint8 n = value & (1 << N(1)); /* SF = value.N && N == 7 */ \
/* ZF, PF = !value.N */ \
F = (n ? n & SYXF : ZPF) /* YF = value.N && N == 5 */ \
| HF /* HF = 1; NF = 0; CF unchanged */ \
| F_C; /* XF = value.N && N == 3 */
#define BIT_N_VADDRESS(address) \
Z16Bit a; \
zuint8 n = READ_8(a.value_uint16 = address) & (1 << N(3)); \
\
F = (n ? (n & SF) : ZPF) \
| (a.values_uint8.index1 & YXF) \
| HF \
| F_C;
#define IN_VC \
zuint8 t; \
\
PC += 2; \
t = IN(BC); \
\
F = (zuint8) \
((t & SYXF) \
| ZF_ZERO(t) \
| PF_PARITY(t) \
| F_C);
#define INX(hl_operator, c_operator) \
zuint8 v; \
zuint t; \
\
PC += 2; \
t = (zuint)(v = IN(BC)) + ((C c_operator 1) & 255); \
WRITE_8(HL, v); \
HL hl_operator; \
B--; \
\
F = (zuint8) \
((B & SYXF) /* SF = (B - 1).7; YF = (B - 1).5; XF = (B - 1).3 */ \
| ZF_ZERO(B) /* ZF = !(B - 1) */ \
| PF_PARITY((t & 7) ^ B) /* PF = Parity of (([HL] + ((C +/- 1) & 255)) and 7) xor B */ \
| (v & 128)); /* NF = IN(BC).7 */ \
/* if (([HL] + ((C +/- 1) & 255)) > 255) HF = 1; else HF = 0 */ \
if (t > 255) F |= HCF; /* if (([HL] + ((C +/- 1) & 255)) > 255) HF = 1; else HF = 0 */
#define INXR(hl_operator, c_operator) \
INX(hl_operator, c_operator); \
if (!B) return 16; \
PC -= 2; return 21;
#define OUTX(operator) \
zuint8 t; \
\
PC += 2; \
OUT(BC, t = READ_8(HL)); \
HL operator; \
B--; \
\
F = (zuint8) \
((B & SYXF) /* SF = (B - 1).7; YF = (B - 1).5; XF = (B - 1).3 */ \
| ZF_ZERO(B) /* ZF = !(B - 1) */ \
| PF_PARITY(((L + t) & 7) ^ B) /* PF = Parity of ((L + [HL]) and 7) xor B */ \
| (t & 128)); /* NF = IN(BC).7 */ \
/* if (L + [HL] > 255) HF = 1; else HF = 0 */ \
if ((zuint)t + L > 255) F |= HCF; /* if (L + [HL] > 255) CF = 1; else CF = 0 */
#define OTXR(operator) \
OUTX(operator); \
if (!B) return 16; \
PC -= 2; return 21;
#define RET PC = READ_16(SP); SP += 2;
/* MARK: - Instructions: 8-Bit Load Group
.---------------------------------------------------------------------------.
| 0 1 2 3 Flags |
| Assembly 76543210765432107654321076543210 szyhxpnc Cycles |
| ----------------------------------------------------------------------- |
| ld X,Y 01xxxyyy ........ 1 / 4 |
| ld J,K < DD >01jjjkkk ........ 2 / 8 |
| ld P,Q < FD >01pppqqq ........ 2 / 8 |
| ld X,BYTE 00xxx110< BYTE > ........ 2 / 7 |
| ld J,BYTE < DD >00jjj110< BYTE > ........ 3 / 11 |
| ld P,BYTE < FD >00ppp110< BYTE > ........ 3 / 11 |
| ld X,(hl) 01xxx110 ........ 2 / 7 |
| ld X,(ix+OFFSET) < DD >01xxx110<OFFSET> ........ 5 / 19 |
| ld X,(iy+OFFSET) < FD >01xxx110<OFFSET> ........ 5 / 19 |
| ld (hl),Y 01110yyy ........ 2 / 7 |
| ld (ix+OFFSET),Y < DD >01110yyy<OFFSET> ........ 5 / 19 |
| ld (iy+OFFSET),Y < FD >01110yyy<OFFSET> ........ 5 / 19 |
| ld (hl),BYTE < 36 >< BYTE > ........ 3 / 10 |
| ld (ix+OFFSET),BYTE < DD >< 36 ><OFFSET>< BYTE > ........ 5 / 19 |
| ld (iy+OFFSET),BYTE < FD >< 36 ><OFFSET>< BYTE > ........ 5 / 19 |
| ld a,(bc) < 0A > ........ 2 / 7 |
| ld a,(de) < 1A > ........ 2 / 7 |
| ld a,(WORD) < 3A >< WORD > ........ 4 / 13 |
| ld (bc),a < 02 > ........ 2 / 7 |
| ld (de),a < 12 > ........ 2 / 7 |
| ld (WORD),a < 32 >< WORD > ........ 4 / 13 |
| ld a,i < ED >< 57 > szy0xi0. 2 / 9 |
| ld a,r < ED >< 5F > szy0xi0. 2 / 9 |
| ld i,a < ED >< 47 > ........ 2 / 9 |
| ld r,a < ED >< 4F > ........ 2 / 9 |
'--------------------------------------------------------------------------*/
INSTRUCTION(ld_X_Y) {PC++; X0 = Y0; return 4;}
INSTRUCTION(ld_JP_KQ) {PC += 2; JP = KQ; return 8;}
INSTRUCTION(ld_X_BYTE) {X0 = READ_8((PC += 2) - 1); return 7;}
INSTRUCTION(ld_JP_BYTE) {JP = READ_8((PC += 3) - 1); return 11;}
INSTRUCTION(ld_X_vhl) {PC++; X0 = READ_8(HL); return 7;}
INSTRUCTION(ld_X_vXYOFFSET) {X1 = READ_8(XY + READ_OFFSET((PC += 3) - 1)); return 19;}
INSTRUCTION(ld_vhl_Y) {PC++; WRITE_8(HL, Y0); return 7;}
INSTRUCTION(ld_vXYOFFSET_Y) {WRITE_8(XY + READ_OFFSET((PC += 3) - 1), Y1); return 19;}
INSTRUCTION(ld_vhl_BYTE) {WRITE_8(HL, READ_8((PC += 2) - 1)); return 10;}
INSTRUCTION(ld_vXYOFFSET_BYTE) {PC += 4; WRITE_8(XY + READ_OFFSET(PC - 2), READ_8(PC - 1)); return 19;}
INSTRUCTION(ld_a_vbc) {PC++; A = READ_8(BC); return 7;}
INSTRUCTION(ld_a_vde) {PC++; A = READ_8(DE); return 7;}
INSTRUCTION(ld_a_vWORD) {A = READ_8(READ_16((PC += 3) - 2)); return 13;}
INSTRUCTION(ld_vbc_a) {PC++; WRITE_8(BC, A); return 7;}
INSTRUCTION(ld_vde_a) {PC++; WRITE_8(DE, A); return 7;}
INSTRUCTION(ld_vWORD_a) {WRITE_8(READ_16((PC += 3) - 2), A); return 13;}
INSTRUCTION(ld_a_i) {PC += 2; A = I; LD_A_I_LD_A_R; return 9;}
INSTRUCTION(ld_a_r) {PC += 2; A = R_ALL; LD_A_I_LD_A_R; return 9;}
INSTRUCTION(ld_i_a) {PC += 2; I = A; return 9;}
INSTRUCTION(ld_r_a) {PC += 2; R = R7 = A; return 9;}
/* MARK: - Instructions: 16-Bit Load Group
.---------------------------------------------------------------------------.
| 0 1 2 3 Flags |
| Assembly 76543210765432107654321076543210 szyhxpnc Cycles |
| ----------------------------------------------------------------------- |
| ld SS,WORD 00ss0001< WORD > ........ 3 / 10 |
| ld ix,WORD < DD >< 21 >< WORD > ........ 4 / 14 |
| ld iy,WORD < FD >< 21 >< WORD > ........ 4 / 14 |
| ld hl,(WORD) < 2A >< WORD > ........ 5 / 16 |
| ld SS,(WORD) < ED >01ss1011< WORD > ........ 6 / 20 |
| ld ix,(WORD) < DD >< 2A >< WORD > ........ 6 / 20 |
| ld iy,(WORD) < FD >< 2A >< WORD > ........ 6 / 20 |
| ld (WORD),hl < 22 >< WORD > ........ 5 / 16 |
| ld (WORD),SS < ED >01ss0011< WORD > ........ 6 / 20 |
| ld (WORD),ix < DD >< 22 >< WORD > ........ 6 / 20 |
| ld (WORD),iy < FD >< 22 >< WORD > ........ 6 / 20 |
| ld sp,hl < F9 > ........ 1 / 6 |
| ld sp,ix < DD >< F9 > ........ 2 / 10 |
| ld sp,iy < FD >< F9 > ........ 2 / 10 |
| push TT 11tt0101 ........ 3 / 11 |
| push ix < DD >< E5 > ........ 4 / 15 |
| push iy < FD >< E5 > ........ 4 / 15 |
| pop TT 11tt0001 ........ 3 / 10 |
| pop ix < DD >< E1 > ........ 4 / 14 |
| pop iy < FD >< E1 > ........ 4 / 14 |
'--------------------------------------------------------------------------*/
INSTRUCTION(ld_SS_WORD) {SS0 = READ_16((PC += 3) - 2); return 10;}
INSTRUCTION(ld_XY_WORD) {XY = READ_16((PC += 4) - 2); return 14;}
INSTRUCTION(ld_hl_vWORD) {HL = READ_16(READ_16((PC += 3) - 2)); return 16;}
INSTRUCTION(ld_SS_vWORD) {SS1 = READ_16(READ_16((PC += 4) - 2)); return 20;}
INSTRUCTION(ld_XY_vWORD) {XY = READ_16(READ_16((PC += 4) - 2)); return 20;}
INSTRUCTION(ld_vWORD_hl) {WRITE_16(READ_16((PC += 3) - 2), HL); return 16;}
INSTRUCTION(ld_vWORD_SS) {WRITE_16(READ_16((PC += 4) - 2), SS1); return 20;}
INSTRUCTION(ld_vWORD_XY) {WRITE_16(READ_16((PC += 4) - 2), XY); return 20;}
INSTRUCTION(ld_sp_hl) {PC++; SP = HL; return 6;}
INSTRUCTION(ld_sp_XY) {PC += 2; SP = XY; return 10;}
INSTRUCTION(push_TT) {PC++; WRITE_16(SP -= 2, TT); return 11;}
INSTRUCTION(push_XY) {PC += 2; WRITE_16(SP -= 2, XY); return 15;}
INSTRUCTION(pop_TT) {PC++; TT = READ_16(SP); SP += 2; return 10;}
INSTRUCTION(pop_XY) {PC += 2; XY = READ_16(SP); SP += 2; return 14;}
/* MARK: - Instructions: Exchange, Block Transfer and Search Groups
.--------------------------------------------------------------------------------.
| 0 1 2 3 Flags |
| Assembly 76543210765432107654321076543210 szyhxpnc Cycles |
| ---------------------------------------------------------------------------- |
| ex de,hl < EB > ........ 1 / 4 |
| ex af,af' < 08 > ........ 1 / 4 |
| exx < D9 > ........ 1 / 4 |
| ex (sp),hl < E3 > ........ 5 / 19 |
| ex (sp),ix < DD >< E3 > ........ 6 / 23 |
| ex (sp),iy < FD >< E3 > ........ 6 / 23 |
| ldi < ED >< A0 > ..*0**0. 4 / 16 |
| ldir < ED >< B0 > ..*0*00. 5,4 / 21,16 |
| ldd < ED >< A8 > ..*0**0. 4 / 16 |
| lddr < ED >< B8 > ..*0*00. 5,4 / 21,16 |
| cpi < ED >< A1 > ******1. 4 / 16 |
| cpir < ED >< B1 > ******1. 5,4 / 21,16 |
| cpd < ED >< A9 > ******1. 4 / 16 |
| cpdr < ED >< B9 > ******1. 5,4 / 21,16 |
'-------------------------------------------------------------------------------*/
INSTRUCTION(ex_de_hl) {zuint16 t; PC++; EX(DE, HL) return 4;}
INSTRUCTION(ex_af_af_) {zuint16 t; PC++; EX(AF, AF_) return 4;}
INSTRUCTION(exx) {zuint16 t; PC++; EX(BC, BC_) EX(DE, DE_) EX(HL, HL_) return 4;}
INSTRUCTION(ex_vsp_hl) {zuint16 t; PC++; EX_VSP_X(HL) return 19;}
INSTRUCTION(ex_vsp_XY) {zuint16 t; PC += 2; EX_VSP_X(XY) return 23;}
INSTRUCTION(ldi) {LDX (++) return 16;}
INSTRUCTION(ldir) {LDXR(++) }
INSTRUCTION(ldd) {LDX (--) return 16;}
INSTRUCTION(lddr) {LDXR(--) }
INSTRUCTION(cpi) {CPX (++) return 16;}
INSTRUCTION(cpir) {CPXR(++) }
INSTRUCTION(cpd) {CPX (--) return 16;}
INSTRUCTION(cpdr) {CPXR(--) }
/* MARK: - Instructions: 8-Bit Arithmetic and Logical Group
.---------------------------------------------------------------------------.
| 0 1 2 3 Flags |
| Assembly 76543210765432107654321076543210 szyhxpnc Cycles |
| ----------------------------------------------------------------------- |
| U [a,]Y 10uuuyyy sz5h3*** 1 / 4 |
| U [a,]K < DD >10uuukkk sz5h3*** 2 / 8 |
| U [a,]Q < FD >10uuuqqq sz5h3*** 2 / 8 |
| U [a,]BYTE 11uuu110< BYTE > sz5h3*** 2 / 7 |
| U [a,](hl) 10uuu110 sz5h3*** 2 / 7 |
| U [a,](ix+OFFSET) < DD >10uuu110<OFFSET> sz5h3*** 5 / 19 |
| U [a,](iy+OFFSET) < FD >10uuu110<OFFSET> sz5h3*** 5 / 19 |
| V X 00xxxvvv sz5h3v0. 1 / 4 |
| V J < DD >00jjjvvv sz5h3v0. 2 / 8 |
| V P < FD >00pppvvv sz5h3v0. 2 / 8 |
| V (hl) 00110vvv sz5h3v*. 3 / 11 |
| V (ix+OFFSET) < DD >00110vvv<OFFSET> sz5h3v*. 6 / 23 |
| V (iy+OFFSET) < FD >00110vvv<OFFSET> sz5h3v*. 6 / 23 |
'--------------------------------------------------------------------------*/
INSTRUCTION(U_a_Y) {PC++; U0(Y0); return 4;}
INSTRUCTION(U_a_KQ) {PC += 2; U1(KQ); return 8;}
INSTRUCTION(U_a_BYTE) {U0(READ_8((PC += 2) - 1)); return 7;}
INSTRUCTION(U_a_vhl) {PC++; U0(READ_8(HL)); return 7;}
INSTRUCTION(U_a_vXYOFFSET) {U1(READ_8(XY + READ_OFFSET((PC += 3) - 1))); return 19;}
INSTRUCTION(V_X) {zuint8 *r; PC++; r = __xxx___0(object); *r = V0(*r); return 4;}
INSTRUCTION(V_JP) {zuint8 *r; PC += 2; r = __jjj___ (object); *r = V1(*r); return 8;}
INSTRUCTION(V_vhl) {PC++; WRITE_8(HL, V0(READ_8(HL))); return 11;}
INSTRUCTION(V_vXYOFFSET) {zuint16 a = (zuint16)(XY + READ_OFFSET((PC += 3) - 1));
WRITE_8(a, V1(READ_8(a))); return 23;}
/* MARK: - Instructions: General-Purpose Arithmetic and CPU Control Group
.---------------------------------------------------------------------------.
| 0 1 2 3 Flags |
| Assembly 76543210765432107654321076543210 szyhxpnc Cycles |
| ----------------------------------------------------------------------- |
| nop < 00 > ........ 1 / 4 |
| halt < 76 > ........ 1 / 4 |
| di < F3 > ........ 1 / 4 |
| ei < FB > ........ 1 / 4 |
| im 0 < ED >< 46 > ........ 2 / 8 |
| < ED >< 4E > |
| < ED >< 66 > |
| < ED >< 6E > |
| im 1 < ED >< 56 > ........ 2 / 8 |
| < ED >< 76 > |