-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz80.hpp
6499 lines (6100 loc) · 268 KB
/
z80.hpp
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
/**
* SUZUKI PLAN - Z80 Emulator
* -----------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (c) 2019 Yoji Suzuki.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* -----------------------------------------------------------------------------
*/
#ifndef INCLUDE_Z80_HPP
#define INCLUDE_Z80_HPP
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(Z80_DISABLE_BREAKPOINT) || !defined(Z80_DISABLE_NESTCHECK)
#include <map>
#include <vector>
#endif
#ifndef Z80_NO_FUNCTIONAL
#include <functional>
#endif
#ifndef Z80_NO_EXCEPTION
#include <stdexcept>
#endif
class Z80
{
public: // Interface data types
struct WaitClocks {
int fetch; // Wait T-cycle (Hz) before fetching instruction (default is 0 = no wait)
int fetchM; // Wait T-cycle (Hz) before fetching multi-bytes instruction (default is 0 = no wait)
int read; // Wait T-cycle (Hz) before to read memory (default is 0 = no wait)
int write; // Wait T-cycle (Hz) before to write memory (default is 0 = no wait)
} wtc;
struct RegisterPair {
unsigned char A;
unsigned char F;
unsigned char B;
unsigned char C;
unsigned char D;
unsigned char E;
unsigned char H;
unsigned char L;
};
struct Register {
struct RegisterPair pair;
struct RegisterPair back;
unsigned short PC;
unsigned short SP;
unsigned short IX;
unsigned short IY;
unsigned short interruptVector; // interrupt vector for IRQ
unsigned short interruptAddrN; // interrupt address for NMI
unsigned short WZ;
unsigned short reserved16;
unsigned char R;
unsigned char I;
unsigned char IFF;
unsigned char interrupt; // NI-- --mm (N: NMI, I: IRQ, mm: mode)
unsigned char consumeClockCounter;
unsigned char execEI;
unsigned char reserved8[2];
} reg;
inline unsigned char flagS() { return 0b10000000; }
inline unsigned char flagZ() { return 0b01000000; }
inline unsigned char flagY() { return 0b00100000; }
inline unsigned char flagH() { return 0b00010000; }
inline unsigned char flagX() { return 0b00001000; }
inline unsigned char flagPV() { return 0b00000100; }
inline unsigned char flagN() { return 0b00000010; }
inline unsigned char flagC() { return 0b00000001; }
inline unsigned char readByte(unsigned short addr, int clock = 4)
{
#ifndef Z80_DISABLE_BREAKPOINT
if (clock && wtc.read) consumeClock(wtc.read);
unsigned char byte = CB.read(CB.arg, addr);
if (clock) consumeClock(clock);
#else
consumeClock(wtc.read);
unsigned char byte = CB.read(CB.arg, addr);
consumeClock(clock);
#endif
return byte;
}
inline void writeByte(unsigned short addr, unsigned char value, int clock = 4)
{
consumeClock(wtc.write);
CB.write(CB.arg, addr, value);
consumeClock(clock);
}
private: // Internal functions & variables
// bit table
const unsigned char bits[8] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000};
// flag setter
inline void setFlagS() { reg.pair.F |= flagS(); }
inline void setFlagZ() { reg.pair.F |= flagZ(); }
inline void setFlagY() { reg.pair.F |= flagY(); }
inline void setFlagH() { reg.pair.F |= flagH(); }
inline void setFlagX() { reg.pair.F |= flagX(); }
inline void setFlagPV() { reg.pair.F |= flagPV(); }
inline void setFlagN() { reg.pair.F |= flagN(); }
inline void setFlagC() { reg.pair.F |= flagC(); }
inline void resetFlagS() { reg.pair.F &= ~flagS(); }
inline void resetFlagZ() { reg.pair.F &= ~flagZ(); }
inline void resetFlagY() { reg.pair.F &= ~flagY(); }
inline void resetFlagH() { reg.pair.F &= ~flagH(); }
inline void resetFlagX() { reg.pair.F &= ~flagX(); }
inline void resetFlagPV() { reg.pair.F &= ~flagPV(); }
inline void resetFlagN() { reg.pair.F &= ~flagN(); }
inline void resetFlagC() { reg.pair.F &= ~flagC(); }
inline void setFlagS(bool on) { on ? setFlagS() : resetFlagS(); }
inline void setFlagZ(bool on) { on ? setFlagZ() : resetFlagZ(); }
inline void setFlagY(bool on) { on ? setFlagY() : resetFlagY(); }
inline void setFlagH(bool on) { on ? setFlagH() : resetFlagH(); }
inline void setFlagX(bool on) { on ? setFlagX() : resetFlagX(); }
inline void setFlagPV(bool on) { on ? setFlagPV() : resetFlagPV(); }
inline void setFlagN(bool on) { on ? setFlagN() : resetFlagN(); }
inline void setFlagC(bool on) { on ? setFlagC() : resetFlagC(); }
inline void setFlagXY(unsigned char value)
{
setFlagX(value & flagX());
setFlagY(value & flagY());
}
// flag checker
inline bool isFlagS() { return reg.pair.F & flagS(); }
inline bool isFlagZ() { return reg.pair.F & flagZ(); }
inline bool isFlagH() { return reg.pair.F & flagH(); }
inline bool isFlagPV() { return reg.pair.F & flagPV(); }
inline bool isFlagN() { return reg.pair.F & flagN(); }
inline bool isFlagC() { return reg.pair.F & flagC(); }
enum class Condition {
Z = 0x40,
C = 0x01,
PV = 0x04,
S = 0x80,
NZ = 0x4040,
NC = 0x0101,
NPV = 0x0404,
NS = 0x8080,
};
inline bool checkConditionFlag(Condition c)
{
int ic = (int)c;
return (ic & reg.pair.F) ^ ((ic & 0xFF00) >> 8);
}
inline unsigned char IFF1() { return 0b00000001; }
inline unsigned char IFF2() { return 0b00000100; }
inline unsigned char IFF_IRQ() { return 0b00100000; }
inline unsigned char IFF_NMI() { return 0b01000000; }
inline unsigned char IFF_HALT() { return 0b10000000; }
#ifndef Z80_DISABLE_BREAKPOINT
class BreakPoint
{
public:
unsigned short addr;
#ifdef Z80_NO_FUNCTIONAL
void (*callback)(void*);
BreakPoint(unsigned short addr_, void (*callback_)(void*))
#else
std::function<void(void*)> callback;
BreakPoint(unsigned short addr_, const std::function<void(void*)>& callback_)
#endif
{
this->addr = addr_;
this->callback = callback_;
}
};
class BreakOperand
{
public:
int prefixNumber;
unsigned char operandNumber;
#ifdef Z80_NO_FUNCTIONAL
void (*callback)(void*, unsigned char*, int);
BreakOperand(int prefixNumber_, unsigned char operandNumber_, void (*callback_)(void*, unsigned char*, int))
{
this->prefixNumber = prefixNumber_;
this->operandNumber = operandNumber_;
this->callback = callback_;
}
#else
std::function<void(void*, unsigned char*, int)> callback;
BreakOperand(int prefixNumber_, unsigned char operandNumber_, const std::function<void(void*, unsigned char*, int)>& callback_)
{
this->prefixNumber = prefixNumber_;
this->operandNumber = operandNumber_;
this->callback = callback_;
}
#endif
};
#endif
#ifndef Z80_DISABLE_NESTCHECK
class SimpleHandler
{
public:
#ifdef Z80_NO_FUNCTIONAL
void (*callback)(void*);
SimpleHandler(void (*callback_)(void*))
#else
std::function<void(void*)> callback;
SimpleHandler(const std::function<void(void*)>& callback_)
#endif
{
this->callback = callback_;
}
};
inline void invokeReturnHandlers()
{
for (auto handler : this->CB.returnHandlers) {
handler->callback(this->CB.arg);
}
}
inline void invokeCallHandlers()
{
for (auto handler : this->CB.callHandlers) {
handler->callback(this->CB.arg);
}
}
#endif
struct Callback {
#ifdef Z80_NO_FUNCTIONAL
unsigned char (*read)(void*, unsigned short);
void (*write)(void*, unsigned short, unsigned char);
unsigned char (*in)(void*, unsigned short);
void (*out)(void*, unsigned short, unsigned char);
void (*consumeClock)(void*, int);
#else
std::function<unsigned char(void*, unsigned short)> read;
std::function<void(void*, unsigned short, unsigned char)> write;
std::function<unsigned char(void*, unsigned short)> in;
std::function<void(void*, unsigned short, unsigned char)> out;
std::function<void(void*, int)> consumeClock;
#endif
#ifndef Z80_UNSUPPORT_16BIT_PORT
bool returnPortAs16Bits;
#endif
#ifndef Z80_DISABLE_DEBUG
#ifdef Z80_NO_FUNCTIONAL
void (*debugMessage)(void*, const char*);
#else
std::function<void(void*, const char*)> debugMessage;
#endif
bool debugMessageEnabled;
#endif
#ifndef Z80_DISABLE_BREAKPOINT
std::map<int, std::vector<BreakPoint*>*> breakPoints;
std::map<int, std::vector<BreakOperand*>*> breakOperands;
#endif
#ifndef Z80_DISABLE_NESTCHECK
std::vector<SimpleHandler*> returnHandlers;
std::vector<SimpleHandler*> callHandlers;
#endif
bool consumeClockEnabled;
void* arg;
} CB;
bool requestBreakFlag;
#ifndef Z80_DISABLE_BREAKPOINT
inline void checkBreakPoint()
{
auto it = CB.breakPoints.find(reg.PC);
if (it == CB.breakPoints.end()) return;
for (auto bp : *CB.breakPoints[reg.PC]) {
bp->callback(CB.arg);
}
}
inline void readFullOpcode(BreakOperand* operand, unsigned char* opcode, int* opcodeLength)
{
*opcodeLength = 0;
switch (operand->prefixNumber) {
case 0x00:
opcode[0] = operand->operandNumber;
*opcodeLength = opLength1[opcode[0]];
for (int i = 1; i < *opcodeLength; i++) {
opcode[i] = readByte(reg.PC + i - 1, 0); // read without consume clocks
}
break;
case 0xCB:
opcode[0] = 0xCB;
opcode[1] = operand->operandNumber;
*opcodeLength = 2;
break;
case 0xED:
opcode[0] = 0xED;
opcode[1] = operand->operandNumber;
*opcodeLength = opLengthED[opcode[1]];
for (int i = 2; i < *opcodeLength; i++) {
opcode[i] = readByte(reg.PC + i - 2, 0); // read without consume clocks
}
break;
case 0xDD:
opcode[0] = 0xDD;
opcode[1] = operand->operandNumber;
*opcodeLength = opLengthIXY[opcode[1]];
for (int i = 2; i < *opcodeLength; i++) {
opcode[i] = readByte(reg.PC + i - 2, 0); // read without consume clocks
}
break;
case 0xFD:
opcode[0] = 0xFD;
opcode[1] = operand->operandNumber;
*opcodeLength = opLengthIXY[opcode[1]];
for (int i = 2; i < *opcodeLength; i++) {
opcode[i] = readByte(reg.PC + i - 2, 0); // read without consume clocks
}
break;
case 0xDDCB:
opcode[0] = 0xDD;
opcode[1] = 0xCB;
opcode[2] = operand->operandNumber;
opcode[3] = readByte(reg.PC, 0);
*opcodeLength = 4;
break;
case 0xFDCB:
opcode[0] = 0xFD;
opcode[1] = 0xCB;
opcode[2] = operand->operandNumber;
opcode[3] = readByte(reg.PC, 0);
*opcodeLength = 4;
break;
}
}
inline void checkBreakOperand(int operandNumber)
{
if (CB.breakOperands.empty()) return;
auto it = CB.breakOperands.find(operandNumber);
if (it == CB.breakOperands.end()) return;
unsigned char opcode[16];
int opcodeLength = 16;
bool first = true;
for (auto bo : *CB.breakOperands[operandNumber]) {
if (first) {
readFullOpcode(bo, opcode, &opcodeLength);
first = false;
}
bo->callback(CB.arg, opcode, opcodeLength);
}
}
inline void checkBreakOperandCB(unsigned char operandNumber) { checkBreakOperand(0xCB00 | operandNumber); }
inline void checkBreakOperandED(unsigned char operandNumber) { checkBreakOperand(0xED00 | operandNumber); }
inline void checkBreakOperandIX(unsigned char operandNumber) { checkBreakOperand(0xDD00 | operandNumber); }
inline void checkBreakOperandIY(unsigned char operandNumber) { checkBreakOperand(0xFD00 | operandNumber); }
inline void checkBreakOperandIX4(unsigned char operandNumber) { checkBreakOperand(0xDDCB00 | operandNumber); }
inline void checkBreakOperandIY4(unsigned char operandNumber) { checkBreakOperand(0xFDCB00 | operandNumber); }
#endif
#ifndef Z80_DISABLE_DEBUG
inline void log(const char* format, ...)
{
char buf[1024];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
CB.debugMessage(CB.arg, buf);
}
#endif
inline unsigned short getAF()
{
return make16BitsFromLE(reg.pair.F, reg.pair.A);
}
inline unsigned short getAF2() { return make16BitsFromLE(reg.back.F, reg.back.A); }
inline unsigned short getBC() { return make16BitsFromLE(reg.pair.C, reg.pair.B); }
inline unsigned short getBC2() { return make16BitsFromLE(reg.back.C, reg.back.B); }
inline unsigned short getDE() { return make16BitsFromLE(reg.pair.E, reg.pair.D); }
inline unsigned short getDE2() { return make16BitsFromLE(reg.back.E, reg.back.D); }
inline unsigned short getHL() { return make16BitsFromLE(reg.pair.L, reg.pair.H); }
inline unsigned short getHL2() { return make16BitsFromLE(reg.back.L, reg.back.H); }
inline void setAF(unsigned short value) { splitTo8BitsPair(value, ®.pair.A, ®.pair.F); }
inline void setAF2(unsigned short value) { splitTo8BitsPair(value, ®.back.A, ®.back.F); }
inline void setBC(unsigned short value) { splitTo8BitsPair(value, ®.pair.B, ®.pair.C); }
inline void setBC2(unsigned short value) { splitTo8BitsPair(value, ®.back.B, ®.back.C); }
inline void setDE(unsigned short value) { splitTo8BitsPair(value, ®.pair.D, ®.pair.E); }
inline void setDE2(unsigned short value) { splitTo8BitsPair(value, ®.back.D, ®.back.E); }
inline void setHL(unsigned short value) { splitTo8BitsPair(value, ®.pair.H, ®.pair.L); }
inline void setHL2(unsigned short value) { splitTo8BitsPair(value, ®.back.H, ®.back.L); }
inline unsigned short getRP(unsigned char rp)
{
switch (rp & 0b11) {
case 0b00: return getBC();
case 0b01: return getDE();
case 0b10: return getHL();
default: return reg.SP;
}
}
inline unsigned short getRPIX(unsigned char rp)
{
switch (rp & 0b11) {
case 0b00: return getBC();
case 0b01: return getDE();
case 0b10: return reg.IX;
default: return reg.SP;
}
}
inline unsigned short getRPIY(unsigned char rp)
{
switch (rp & 0b11) {
case 0b00: return getBC();
case 0b01: return getDE();
case 0b10: return reg.IY;
default: return reg.SP;
}
}
inline void setRP(unsigned char rp, unsigned short value)
{
switch (rp & 0b11) {
case 0b00: setBC(value); break;
case 0b01: setDE(value); break;
case 0b10: setHL(value); break;
default: reg.SP = value;
}
}
inline unsigned char getIXH() { return (reg.IX & 0xFF00) >> 8; }
inline unsigned char getIXL() { return reg.IX & 0x00FF; }
inline unsigned char getIYH() { return (reg.IY & 0xFF00) >> 8; }
inline unsigned char getIYL() { return reg.IY & 0x00FF; }
inline unsigned char getPCH() { return (reg.PC & 0xFF00) >> 8; }
inline unsigned char getPCL() { return reg.PC & 0x00FF; }
inline void setPCH(unsigned char v) { reg.PC = (reg.PC & 0x00FF) + v * 256; }
inline void setPCL(unsigned char v) { reg.PC = (reg.PC & 0xFF00) + v; }
inline void setSPH(unsigned char v) { reg.SP = (reg.SP & 0x00FF) + v * 256; }
inline void setSPL(unsigned char v) { reg.SP = (reg.SP & 0xFF00) + v; }
inline void setIXH(unsigned char v) { reg.IX = (reg.IX & 0x00FF) + v * 256; }
inline void setIXL(unsigned char v) { reg.IX = (reg.IX & 0xFF00) + v; }
inline void setIYH(unsigned char v) { reg.IY = (reg.IY & 0x00FF) + v * 256; }
inline void setIYL(unsigned char v) { reg.IY = (reg.IY & 0xFF00) + v; }
inline bool isEvenNumberBits(unsigned char value)
{
int on = 0;
int off = 0;
value & 0b10000000 ? on++ : off++;
value & 0b01000000 ? on++ : off++;
value & 0b00100000 ? on++ : off++;
value & 0b00010000 ? on++ : off++;
value & 0b00001000 ? on++ : off++;
value & 0b00000100 ? on++ : off++;
value & 0b00000010 ? on++ : off++;
value & 0b00000001 ? on++ : off++;
return (on & 1) == 0;
}
inline void consumeClock(int hz)
{
reg.consumeClockCounter += hz;
#ifndef Z80_CALLBACK_PER_INSTRUCTION
#ifdef Z80_CALLBACK_WITHOUT_CHECK
CB.consumeClock(CB.arg, hz);
#else
if (CB.consumeClockEnabled && hz) CB.consumeClock(CB.arg, hz);
#endif
#endif
}
inline unsigned short getPort16WithB(unsigned char c) { return make16BitsFromLE(c, reg.pair.B); }
inline unsigned short getPort16WithA(unsigned char c) { return make16BitsFromLE(c, reg.pair.A); }
inline unsigned char inPortWithB(unsigned char port, int clock = 4)
{
#ifdef Z80_UNSUPPORT_16BIT_PORT
unsigned char byte = CB.in(CB.arg, port);
#else
unsigned char byte = CB.in(CB.arg, CB.returnPortAs16Bits ? getPort16WithB(port) : port);
#endif
consumeClock(clock);
return byte;
}
inline unsigned char inPortWithA(unsigned char port, int clock = 4)
{
#ifdef Z80_UNSUPPORT_16BIT_PORT
unsigned char byte = CB.in(CB.arg, port);
#else
unsigned char byte = CB.in(CB.arg, CB.returnPortAs16Bits ? getPort16WithA(port) : port);
#endif
consumeClock(clock);
return byte;
}
inline void outPortWithB(unsigned char port, unsigned char value, int clock = 4)
{
#ifdef Z80_UNSUPPORT_16BIT_PORT
CB.out(CB.arg, port, value);
#else
CB.out(CB.arg, CB.returnPortAs16Bits ? getPort16WithB(port) : port, value);
#endif
consumeClock(clock);
}
inline void outPortWithA(unsigned char port, unsigned char value, int clock = 4)
{
#ifdef Z80_UNSUPPORT_16BIT_PORT
CB.out(CB.arg, port, value);
#else
CB.out(CB.arg, CB.returnPortAs16Bits ? getPort16WithA(port) : port, value);
#endif
consumeClock(clock);
}
static inline void NOP(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] NOP", ctx->reg.PC - 1);
#endif
}
static inline void HALT(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] HALT", ctx->reg.PC - 1);
#endif
ctx->reg.IFF |= ctx->IFF_HALT();
}
static inline void DI(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] DI", ctx->reg.PC - 1);
#endif
ctx->reg.IFF &= ~(ctx->IFF1() | ctx->IFF2());
}
static inline void EI(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] EI", ctx->reg.PC - 1);
#endif
ctx->reg.IFF |= ctx->IFF1() | ctx->IFF2();
ctx->reg.execEI = 1;
}
static inline void IM0(Z80* ctx) { ctx->IM(0); }
static inline void IM1(Z80* ctx) { ctx->IM(1); }
static inline void IM2(Z80* ctx) { ctx->IM(2); }
inline void IM(unsigned char interrptMode)
{
#ifndef Z80_DISABLE_DEBUG
if (isDebug()) log("[%04X] IM %d", reg.PC - 2, interrptMode);
#endif
reg.interrupt &= 0b11111100;
reg.interrupt |= interrptMode & 0b11;
}
static inline void LD_A_I_(Z80* ctx) { ctx->LD_A_I(); }
inline void LD_A_I()
{
#ifndef Z80_DISABLE_DEBUG
if (isDebug()) log("[%04X] LD A<$%02X>, I<$%02X>", reg.PC - 2, reg.pair.A, reg.I);
#endif
reg.pair.A = reg.I;
setFlagPV(reg.IFF & IFF2());
consumeClock(1);
}
static inline void LD_I_A_(Z80* ctx) { ctx->LD_I_A(); }
inline void LD_I_A()
{
#ifndef Z80_DISABLE_DEBUG
if (isDebug()) log("[%04X] LD I<$%02X>, A<$%02X>", reg.PC - 2, reg.I, reg.pair.A);
#endif
reg.I = reg.pair.A;
consumeClock(1);
}
static inline void LD_A_R_(Z80* ctx) { ctx->LD_A_R(); }
inline void LD_A_R()
{
#ifndef Z80_DISABLE_DEBUG
if (isDebug()) log("[%04X] LD A<$%02X>, R<$%02X>", reg.PC - 2, reg.pair.A, reg.R);
#endif
reg.pair.A = reg.R;
setFlagPV(reg.IFF & IFF1());
consumeClock(1);
}
static inline void LD_R_A_(Z80* ctx) { ctx->LD_R_A(); }
inline void LD_R_A()
{
#ifndef Z80_DISABLE_DEBUG
if (isDebug()) log("[%04X] LD R<$%02X>, A<$%02X>", reg.PC - 2, reg.R, reg.pair.A);
#endif
reg.R = reg.pair.A;
consumeClock(1);
}
static inline void OP_CB(Z80* ctx)
{
unsigned char operandNumber = ctx->fetch(4 + ctx->wtc.fetchM);
#ifndef Z80_DISABLE_BREAKPOINT
ctx->checkBreakOperandCB(operandNumber);
#endif
ctx->opSetCB[operandNumber](ctx);
}
static inline void OP_ED(Z80* ctx)
{
unsigned char operandNumber = ctx->fetch(4 + ctx->wtc.fetchM);
#ifndef Z80_NO_EXCEPTION
if (!ctx->opSetED[operandNumber]) {
char buf[80];
snprintf(buf, sizeof(buf), "detect an unknown operand (ED,%02X)", operandNumber);
throw std::runtime_error(buf);
}
#endif
#ifndef Z80_DISABLE_BREAKPOINT
ctx->checkBreakOperandED(operandNumber);
#endif
ctx->opSetED[operandNumber](ctx);
}
static inline void OP_IX(Z80* ctx)
{
unsigned char operandNumber = ctx->fetch(4 + ctx->wtc.fetchM);
#ifndef Z80_NO_EXCEPTION
if (!ctx->opSetIX[operandNumber]) {
char buf[80];
snprintf(buf, sizeof(buf), "detect an unknown operand (DD,%02X)", operandNumber);
throw std::runtime_error(buf);
}
#endif
#ifndef Z80_DISABLE_BREAKPOINT
ctx->checkBreakOperandIX(operandNumber);
#endif
ctx->opSetIX[operandNumber](ctx);
}
static inline void OP_IY(Z80* ctx)
{
unsigned char operandNumber = ctx->fetch(4 + ctx->wtc.fetchM);
#ifndef Z80_NO_EXCEPTION
if (!ctx->opSetIY[operandNumber]) {
char buf[80];
snprintf(buf, sizeof(buf), "detect an unknown operand (FD,%02X)", operandNumber);
throw std::runtime_error(buf);
}
#endif
#ifndef Z80_DISABLE_BREAKPOINT
ctx->checkBreakOperandIY(operandNumber);
#endif
ctx->opSetIY[operandNumber](ctx);
}
static inline void OP_IX4(Z80* ctx)
{
signed char op3 = (signed char)ctx->fetch(4);
unsigned char op4 = ctx->fetch(4);
#ifndef Z80_DISABLE_BREAKPOINT
ctx->checkBreakOperandIX4(op4);
#endif
ctx->opSetIX4[op4](ctx, op3);
}
static inline void OP_IY4(Z80* ctx)
{
signed char op3 = (signed char)ctx->fetch(4);
unsigned char op4 = ctx->fetch(4);
#ifndef Z80_DISABLE_BREAKPOINT
ctx->checkBreakOperandIY4(op4);
#endif
ctx->opSetIY4[op4](ctx, op3);
}
// Load location (HL) with value n
static inline void LD_HL_N(Z80* ctx)
{
unsigned char n = ctx->fetch(3);
unsigned short hl = ctx->getHL();
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD (HL<$%04X>), $%02X", ctx->reg.PC - 2, hl, n);
#endif
ctx->writeByte(hl, n, 3);
}
// Load Acc. wth location (BC)
static inline void LD_A_BC(Z80* ctx)
{
unsigned short addr = ctx->getBC();
unsigned char n = ctx->readByte(addr, 3);
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD A, (BC<$%02X%02X>) = $%02X", ctx->reg.PC - 1, ctx->reg.pair.B, ctx->reg.pair.C, n);
#endif
ctx->reg.pair.A = n;
}
// Load Acc. wth location (DE)
static inline void LD_A_DE(Z80* ctx)
{
unsigned short addr = ctx->getDE();
unsigned char n = ctx->readByte(addr, 3);
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD A, (DE<$%02X%02X>) = $%02X", ctx->reg.PC - 1, ctx->reg.pair.D, ctx->reg.pair.E, n);
#endif
ctx->reg.pair.A = n;
}
// Load Acc. wth location (nn)
static inline void LD_A_NN(Z80* ctx)
{
unsigned char l = ctx->fetch(3);
unsigned char h = ctx->fetch(3);
unsigned short addr = ctx->make16BitsFromLE(l, h);
unsigned char n = ctx->readByte(addr, 3);
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD A, ($%04X) = $%02X", ctx->reg.PC - 3, addr, n);
#endif
ctx->reg.pair.A = n;
}
// Load location (BC) wtih Acc.
static inline void LD_BC_A(Z80* ctx)
{
unsigned short addr = ctx->getBC();
unsigned char n = ctx->reg.pair.A;
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD (BC<$%02X%02X>), A<$%02X>", ctx->reg.PC - 1, ctx->reg.pair.B, ctx->reg.pair.C, n);
#endif
ctx->writeByte(addr, n, 3);
}
// Load location (DE) wtih Acc.
static inline void LD_DE_A(Z80* ctx)
{
unsigned short addr = ctx->getDE();
unsigned char n = ctx->reg.pair.A;
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD (DE<$%02X%02X>), A<$%02X>", ctx->reg.PC - 1, ctx->reg.pair.D, ctx->reg.pair.E, n);
#endif
ctx->writeByte(addr, n, 3);
}
// Load location (nn) with Acc.
static inline void LD_NN_A(Z80* ctx)
{
unsigned char l = ctx->fetch(3);
unsigned char h = ctx->fetch(3);
unsigned short addr = ctx->make16BitsFromLE(l, h);
unsigned char n = ctx->reg.pair.A;
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD ($%04X), A<$%02X>", ctx->reg.PC - 3, addr, n);
#endif
ctx->writeByte(addr, n, 3);
}
// Load HL with location (nn).
static inline void LD_HL_ADDR(Z80* ctx)
{
unsigned char l = ctx->fetch(3);
unsigned char h = ctx->fetch(3);
unsigned short addr = ctx->make16BitsFromLE(l, h);
#ifndef Z80_DISABLE_DEBUG
unsigned short hl = ctx->getHL();
#endif
ctx->reg.pair.L = ctx->readByte(addr, 3);
ctx->reg.pair.H = ctx->readByte(addr + 1, 3);
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD HL<$%04X>, ($%04X) = $%04X", ctx->reg.PC - 3, hl, addr, ctx->getHL());
#endif
}
// Load location (nn) with HL.
static inline void LD_ADDR_HL(Z80* ctx)
{
unsigned char l = ctx->fetch(3);
unsigned char h = ctx->fetch(3);
unsigned short addr = ctx->make16BitsFromLE(l, h);
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD ($%04X), %s", ctx->reg.PC - 3, addr, ctx->registerPairDump(0b10));
#endif
ctx->writeByte(addr, ctx->reg.pair.L, 3);
ctx->writeByte(addr + 1, ctx->reg.pair.H, 3);
}
// Load SP with HL.
static inline void LD_SP_HL(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] LD %s, HL<$%04X>", ctx->reg.PC - 1, ctx->registerPairDump(0b11), ctx->getHL());
#endif
ctx->reg.SP = ctx->getHL();
ctx->consumeClock(2);
}
// Exchange H and L with D and E
static inline void EX_DE_HL(Z80* ctx)
{
unsigned short de = ctx->getDE();
unsigned short hl = ctx->getHL();
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] EX %s, %s", ctx->reg.PC - 1, ctx->registerPairDump(0b01), ctx->registerPairDump(0b10));
#endif
ctx->setDE(hl);
ctx->setHL(de);
}
// Exchange A and F with A' and F'
static inline void EX_AF_AF2(Z80* ctx)
{
unsigned short af = ctx->getAF();
unsigned short af2 = ctx->getAF2();
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] EX AF<$%02X%02X>, AF'<$%02X%02X>", ctx->reg.PC - 1, ctx->reg.pair.A, ctx->reg.pair.F, ctx->reg.back.A, ctx->reg.back.F);
#endif
ctx->setAF(af2);
ctx->setAF2(af);
}
static inline void EX_SP_HL(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
unsigned short sp = ctx->reg.SP;
#endif
unsigned char l = ctx->pop(4);
unsigned char h = ctx->pop(4);
#ifndef Z80_DISABLE_DEBUG
unsigned short hl = ctx->getHL();
if (ctx->isDebug()) ctx->log("[%04X] EX (SP<$%04X>) = $%02X%02X, HL<$%04X>", ctx->reg.PC - 1, sp, h, l, hl);
#endif
ctx->push(ctx->reg.pair.H, 4);
ctx->reg.pair.H = h;
ctx->push(ctx->reg.pair.L, 3);
ctx->reg.pair.L = l;
}
static inline void EXX(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] EXX", ctx->reg.PC - 1);
#endif
unsigned short bc = ctx->getBC();
unsigned short bc2 = ctx->getBC2();
unsigned short de = ctx->getDE();
unsigned short de2 = ctx->getDE2();
unsigned short hl = ctx->getHL();
unsigned short hl2 = ctx->getHL2();
ctx->setBC(bc2);
ctx->setBC2(bc);
ctx->setDE(de2);
ctx->setDE2(de);
ctx->setHL(hl2);
ctx->setHL2(hl);
}
inline void push(unsigned char value, int clocks)
{
reg.SP--;
writeByte(reg.SP, value, clocks);
}
inline unsigned char pop(int clocks)
{
unsigned char value = readByte(reg.SP, clocks);
reg.SP++;
return value;
}
static inline void PUSH_AF(Z80* ctx)
{
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] PUSH AF<$%02X%02X> <SP:$%04X>", ctx->reg.PC - 1, ctx->reg.pair.A, ctx->reg.pair.F, ctx->reg.SP);
#endif
ctx->push(ctx->reg.pair.A, 4);
ctx->push(ctx->reg.pair.F, 3);
}
static inline void POP_AF(Z80* ctx)
{
ctx->reg.pair.F = ctx->pop(3);
ctx->reg.pair.A = ctx->pop(3);
#ifndef Z80_DISABLE_DEBUG
if (ctx->isDebug()) ctx->log("[%04X] POP AF <SP:$%04X> = $%04X", ctx->reg.PC - 1, ctx->reg.SP - 2, ctx->getAF());
#endif
}
unsigned char* registerPointerTable[8] = {®.pair.B, ®.pair.C, ®.pair.D, ®.pair.E, ®.pair.H, ®.pair.L, ®.pair.F, ®.pair.A};
inline unsigned char* getRegisterPointer(unsigned char r) { return registerPointerTable[r]; }
inline unsigned char getRegister(unsigned char r) { return *registerPointerTable[r]; }
#ifndef Z80_DISABLE_DEBUG
inline char* registerDump(unsigned char r)
{
static char A[16];
static char B[16];
static char C[16];
static char D[16];
static char E[16];
static char H[16];
static char L[16];
static char F[16];
static char unknown[2];
switch (r & 0b111) {
case 0b111: snprintf(A, sizeof(A), "A<$%02X>", reg.pair.A); return A;
case 0b000: snprintf(B, sizeof(B), "B<$%02X>", reg.pair.B); return B;
case 0b001: snprintf(C, sizeof(C), "C<$%02X>", reg.pair.C); return C;
case 0b010: snprintf(D, sizeof(D), "D<$%02X>", reg.pair.D); return D;
case 0b011: snprintf(E, sizeof(E), "E<$%02X>", reg.pair.E); return E;
case 0b100: snprintf(H, sizeof(H), "H<$%02X>", reg.pair.H); return H;
case 0b101: snprintf(L, sizeof(L), "L<$%02X>", reg.pair.L); return L;
case 0b110: snprintf(F, sizeof(F), "F<$%02X>", reg.pair.F); return F;
}
unknown[0] = '?';
unknown[1] = '\0';
return unknown;
}
inline char* conditionDump(Condition c)
{
static char CN[4];
switch (c) {
case Condition::NZ: strcpy(CN, "NZ"); break;
case Condition::Z: strcpy(CN, "Z"); break;
case Condition::NC: strcpy(CN, "NC"); break;
case Condition::C: strcpy(CN, "C"); break;
case Condition::NPV: strcpy(CN, "PO"); break;
case Condition::PV: strcpy(CN, "PE"); break;
case Condition::NS: strcpy(CN, "P"); break;
case Condition::S: strcpy(CN, "M"); break;
}
return CN;
}
inline char* relativeDump(unsigned short pc, signed char e)
{
static char buf[80];
if (e < 0) {
int ee = -e;
ee -= 2;
snprintf(buf, sizeof(buf), "$%04X - %d = $%04X", pc, ee, pc + e + 2);
} else {
snprintf(buf, sizeof(buf), "$%04X + %d = $%04X", pc, e + 2, pc + e + 2);
}
return buf;
}
inline char* registerDump2(unsigned char r)
{
static char A[16];
static char B[16];
static char C[16];
static char D[16];
static char E[16];
static char H[16];
static char L[16];
static char unknown[2] = "?";
switch (r) {
case 0b111: snprintf(A, sizeof(A), "A'<$%02X>", reg.back.A); return A;
case 0b000: snprintf(B, sizeof(B), "B'<$%02X>", reg.back.B); return B;
case 0b001: snprintf(C, sizeof(C), "C'<$%02X>", reg.back.C); return C;