-
Notifications
You must be signed in to change notification settings - Fork 6
/
z80emu.c
2653 lines (1587 loc) · 73.7 KB
/
z80emu.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
/* z80emu.c
* Z80 processor emulator. Modify z80emu.h to customize it for your needs but
* not this module directly.
*
* Copyright (c) 2012 Lin Ke-Fong
*
* This program is free, do whatever you want with it.
*/
#include "z80emu.h"
#include "instructions.h"
#include "macros.h"
#include "tables.h"
/* Indirect (HL) or prefixed indexed (IX + d) and (IY + d) memory operands are
* encoded using the 3 bits "110" (0x06).
*/
#define INDIRECT_HL 0x06
/* Condition codes are encoded using 2 or 3 bits. The xor table is needed for
* negated conditions, it is used along with the and table.
*/
static const int XOR_CONDITION_TABLE[8] = {
Z80_Z_FLAG,
0,
Z80_C_FLAG,
0,
Z80_P_FLAG,
0,
Z80_S_FLAG,
0,
};
static const int AND_CONDITION_TABLE[8] = {
Z80_Z_FLAG,
Z80_Z_FLAG,
Z80_C_FLAG,
Z80_C_FLAG,
Z80_P_FLAG,
Z80_P_FLAG,
Z80_S_FLAG,
Z80_S_FLAG,
};
/* RST instruction restart addresses, encoded by Y() bits of the opcode. */
static const int RST_TABLE[8] = {
0x00,
0x08,
0x10,
0x18,
0x20,
0x28,
0x30,
0x38,
};
/* There is an overflow if the xor of the carry out and the carry of the most
* significant bit is not zero.
*/
static const int OVERFLOW_TABLE[4] = {
0,
Z80_V_FLAG,
Z80_V_FLAG,
0,
};
static int emulate (Z80_STATE * state, int number_cycles, int opcode);
/* Reset processor's state to power-on default and reset status. */
void Z80Reset (Z80_STATE *state)
{
state->status = 0;
AF = 0xffff;
SP = 0xffff;
state->i = state->pc = state->iff1 = state->iff2 = 0;
state->im = Z80_INTERRUPT_MODE_0;
}
/* Trigger an interrupt according to the current interrupt mode and return the
* number of cycles required to accept it. If maskable interrupts are disabled,
* this will return zero. Z80_STATE's status is updated. In interrupt mode 0,
* data_on_bus must be a single byte opcode.
*/
int Z80Interrupt (Z80_STATE *state, int data_on_bus)
{
state->status = 0;
if (state->iff1) {
state->iff1 = state->iff2 = 0;
state->r = (state->r & 0x80) | ((state->r + 1) & 0x7f);
switch (state->im) {
case Z80_INTERRUPT_MODE_0: {
/* Assuming the opcode in data_on_bus is an
* RST instruction, accepting the interrupt
* should take 2 + 11 = 13 cycles.
*/
return 2 + emulate(state, 4, data_on_bus);
}
case Z80_INTERRUPT_MODE_1: {
SP -= 2;
Z80_WRITE_WORD(SP, state->pc);
state->pc = 0x0038;
return 13;
}
case Z80_INTERRUPT_MODE_2:
default: {
SP -= 2;
Z80_WRITE_WORD(SP, state->pc);
state->pc =
(state->i << 8 | data_on_bus)
& 0xfffe;
return 19;
}
}
} else
return 0;
}
/* Trigger a non maskable interrupt, return the number of cycles needed to
* accept it. Z80_STATE's status is reset.
*/
int Z80NonMaskableInterrupt (Z80_STATE *state)
{
state->status = 0;
state->iff2 = state->iff1;
state->iff1 = 0;
state->r = (state->r & 0x80) | ((state->r + 1) & 0x7f);
SP -= 2;
Z80_WRITE_WORD(SP, state->pc);
state->pc = 0x0066;
return 11;
}
/* Emulate a Z80 processor for number_cycles cycles, which must be greater or
* equal to 4. Return the number of emulated cycles, this number will be equal
* to number_cycles or be slightly greater (most probable case). It will be
* less if the emulation has been interrupted. Z80_STATE's status member is
* reset at the start of the emulation. If the emulation is interrupted, it may
* indicate the reason why.
*/
int Z80Emulate (Z80_STATE *state, int number_cycles)
{
int opcode;
Z80_FETCH_BYTE(state->pc, opcode);
state->pc++;
state->status = 0;
return emulate(state, number_cycles, opcode);
}
/* Actual emulation function. opcode is the first opcode to emulate, this is
* needed by Z80Interrupt() for interrupt mode 0.
*/
static int emulate (Z80_STATE * state, int number_cycles, int opcode)
{
int elapsed_cycles,
pc, r,
i;
void *register_table[16],
*dd_register_table[16],
*fd_register_table[16];
elapsed_cycles = 0;
pc = state->pc;
r = state->r & 0x7f;
/* Build register decoding tables for both 3-bit encoded 8-bit
* registers and 2-bit encoded 16-bit registers. When an opcode is
* prefixed by 0xdd, HL is replaced by IX. When 0xfd prefixed, HL is
* replaced by IY.
*/
/* 8-bit "R" registers. */
register_table[0] = &state->registers.byte[Z80_B];
register_table[1] = &state->registers.byte[Z80_C];
register_table[2] = &state->registers.byte[Z80_D];
register_table[3] = &state->registers.byte[Z80_E];
register_table[4] = &state->registers.byte[Z80_H];
register_table[5] = &state->registers.byte[Z80_L];
/* Encoding 0x06 is used for indexed memory operands and direct HL or
* IX/IY register access.
*/
register_table[6] = &state->registers.word[Z80_HL];
register_table[7] = &state->registers.byte[Z80_A];
/* "Regular" 16-bit "RR" registers. */
register_table[8] = &state->registers.word[Z80_BC];
register_table[9] = &state->registers.word[Z80_DE];
register_table[10] = &state->registers.word[Z80_HL];
register_table[11] = &state->registers.word[Z80_SP];
/* 16-bit "SS" registers for PUSH and POP instructions (note that SP is
* replaced by AF).
*/
register_table[12] = &state->registers.word[Z80_BC];
register_table[13] = &state->registers.word[Z80_DE];
register_table[14] = &state->registers.word[Z80_HL];
register_table[15] = &state->registers.word[Z80_AF];
/* 0xdd and 0xfd prefixed register decoding tables. */
for (i = 0; i < 16; i++)
dd_register_table[i]
= fd_register_table[i]
= register_table[i];
dd_register_table[4] = &state->registers.byte[Z80_IXH];
dd_register_table[5] = &state->registers.byte[Z80_IXL];
dd_register_table[6] = &state->registers.word[Z80_IX];
dd_register_table[10] = &state->registers.word[Z80_IX];
dd_register_table[14] = &state->registers.word[Z80_IX];
fd_register_table[4] = &state->registers.byte[Z80_IYH];
fd_register_table[5] = &state->registers.byte[Z80_IYL];
fd_register_table[6] = &state->registers.word[Z80_IY];
fd_register_table[10] = &state->registers.word[Z80_IY];
fd_register_table[14] = &state->registers.word[Z80_IY];
goto start_emulation;
for ( ; ; ) {
void **registers;
int instruction;
Z80_FETCH_BYTE(pc, opcode);
pc++;
start_emulation:
registers = register_table;
emulate_next_opcode:
instruction = INSTRUCTION_TABLE[opcode];
emulate_next_instruction:
elapsed_cycles += 4;
r++;
switch (instruction) {
/* 8-bit load group. */
case LD_R_R: {
R(Y(opcode)) = R(Z(opcode));
break;
}
case LD_R_N: {
READ_N(R(Y(opcode)));
break;
}
case LD_R_INDIRECT_HL: {
if (registers == register_table) {
READ_BYTE(HL, R(Y(opcode)));
} else {
int d;
READ_D(d);
d += HL_IX_IY;
READ_BYTE(d, S(Y(opcode)));
elapsed_cycles += 5;
}
break;
}
case LD_INDIRECT_HL_R: {
if (registers == register_table) {
WRITE_BYTE(HL, R(Z(opcode)));
} else {
int d;
READ_D(d);
d += HL_IX_IY;
WRITE_BYTE(d, S(Z(opcode)));
elapsed_cycles += 5;
}
break;
}
case LD_INDIRECT_HL_N: {
int n;
if (registers == register_table) {
READ_N(n);
WRITE_BYTE(HL, n);
} else {
int d;
READ_D(d);
d += HL_IX_IY;
READ_N(n);
WRITE_BYTE(d, n);
elapsed_cycles += 2;
}
break;
}
case LD_A_INDIRECT_BC: {
READ_BYTE(BC, A);
break;
}
case LD_A_INDIRECT_DE: {
READ_BYTE(DE, A);
break;
}
case LD_A_INDIRECT_NN: {
int nn;
READ_NN(nn);
READ_BYTE(nn, A);
break;
}
case LD_INDIRECT_BC_A: {
WRITE_BYTE(BC, A);
break;
}
case LD_INDIRECT_DE_A: {
WRITE_BYTE(DE, A);
break;
}
case LD_INDIRECT_NN_A: {
int nn;
READ_NN(nn);
WRITE_BYTE(nn, A);
break;
}
case LD_A_I_LD_A_R: {
int a, f;
a = opcode == OPCODE_LD_A_I
? state->i
: (state->r & 0x80) | (r & 0x7f);
f = SZYX_FLAGS_TABLE[a];
/* Note: On a real processor, if an interrupt
* occurs during the execution of either
* "LD A, I" or "LD A, R", the parity flag is
* reset. That can never happen here.
*/
f |= state->iff2 << Z80_P_FLAG_SHIFT;
f |= F & Z80_C_FLAG;
AF = (a << 8) | f;
elapsed_cycles++;
break;
}
case LD_I_A_LD_R_A: {
if (opcode == OPCODE_LD_I_A)
state->i = A;
else {
state->r = A;
r = A & 0x7f;
}
elapsed_cycles++;
break;
}
/* 16-bit load group. */
case LD_RR_NN: {
READ_NN(RR(P(opcode)));
break;
}
case LD_HL_INDIRECT_NN: {
int nn;
READ_NN(nn);
READ_WORD(nn, HL_IX_IY);
break;
}
case LD_RR_INDIRECT_NN: {
int nn;
READ_NN(nn);
READ_WORD(nn, RR(P(opcode)));
break;
}
case LD_INDIRECT_NN_HL: {
int nn;
READ_NN(nn);
WRITE_WORD(nn, HL_IX_IY);
break;
}
case LD_INDIRECT_NN_RR: {
int nn;
READ_NN(nn);
WRITE_WORD(nn, RR(P(opcode)));
break;
}
case LD_SP_HL: {
SP = HL_IX_IY;
elapsed_cycles += 2;
break;
}
case PUSH_SS: {
PUSH(SS(P(opcode)));
elapsed_cycles++;
break;
}
case POP_SS: {
POP(SS(P(opcode)));
break;
}
/* Exchange, block transfer and search group. */
case EX_DE_HL: {
EXCHANGE(DE, HL);
break;
}
case EX_AF_AF_PRIME: {
EXCHANGE(AF, state->alternates[Z80_AF]);
break;
}
case EXX: {
EXCHANGE(BC, state->alternates[Z80_BC]);
EXCHANGE(DE, state->alternates[Z80_DE]);
EXCHANGE(HL, state->alternates[Z80_HL]);
break;
}
case EX_INDIRECT_SP_HL: {
int t;
READ_WORD(SP, t);
WRITE_WORD(SP, HL_IX_IY);
HL_IX_IY = t;
elapsed_cycles += 3;
break;
}
case LDI_LDD: {
int n, f, d;
READ_BYTE(HL, n);
WRITE_BYTE(DE, n);
f = F & SZC_FLAGS;
f |= --BC ? Z80_P_FLAG : 0;
#ifndef Z80_DOCUMENTED_FLAGS_ONLY
n += A;
f |= n & Z80_X_FLAG;
f |= (n << (Z80_Y_FLAG_SHIFT - 1))
& Z80_Y_FLAG;
#endif
F = f;
d = opcode == OPCODE_LDI ? +1 : -1;
DE += d;
HL += d;
elapsed_cycles += 2;
break;
}
case LDIR_LDDR: {
int d, f, bc, de, hl, n;
#ifdef Z80_HANDLE_SELF_MODIFYING_CODE
int p, q;
p = (pc - 2) & 0xffff;
q = (pc - 1) & 0xffff;
#endif
d = opcode == OPCODE_LDIR ? +1 : -1;
f = F & SZC_FLAGS;
bc = BC;
de = DE;
hl = HL;
r -= 2;
elapsed_cycles -= 8;
for ( ; ; ) {
r += 2;
Z80_READ_BYTE(hl, n);
Z80_WRITE_BYTE(de, n);
hl += d;
de += d;
if (--bc)
elapsed_cycles += 21;
else {
elapsed_cycles += 16;
break;
}
#ifdef Z80_HANDLE_SELF_MODIFYING_CODE
if (((de - d) & 0xffff) == p
|| ((de - d) & 0xffff) == q) {
f |= Z80_P_FLAG;
pc -= 2;
break;
}
#endif
if (elapsed_cycles < number_cycles)
continue;
else {
f |= Z80_P_FLAG;
pc -= 2;
break;
}
}
HL = hl;
DE = de;
BC = bc;
#ifndef Z80_DOCUMENTED_FLAGS_ONLY
n += A;
f |= n & Z80_X_FLAG;
f |= (n << (Z80_Y_FLAG_SHIFT - 1))
& Z80_Y_FLAG;
#endif
F = f;
break;
}
case CPI_CPD: {
int a, n, z, f;
a = A;
READ_BYTE(HL, n);
z = a - n;
HL += opcode == OPCODE_CPI ? +1 : -1;
f = (a ^ n ^ z) & Z80_H_FLAG;
#ifndef Z80_DOCUMENTED_FLAGS_ONLY
n = z - (f >> Z80_H_FLAG_SHIFT);
f |= (n << (Z80_Y_FLAG_SHIFT - 1))
& Z80_Y_FLAG;
f |= n & Z80_X_FLAG;
#endif
f |= SZYX_FLAGS_TABLE[z & 0xff] & SZ_FLAGS;
f |= --BC ? Z80_P_FLAG : 0;
F = f | Z80_N_FLAG | (F & Z80_C_FLAG);
elapsed_cycles += 5;
break;
}
case CPIR_CPDR: {
int d, a, bc, hl, n, z, f;
d = opcode == OPCODE_CPIR ? +1 : -1;
a = A;
bc = BC;
hl = HL;
r -= 2;
elapsed_cycles -= 8;
for ( ; ; ) {
r += 2;
Z80_READ_BYTE(hl, n);
z = a - n;
hl += d;
if (--bc && z)
elapsed_cycles += 21;
else {
elapsed_cycles += 16;
break;
}
if (elapsed_cycles < number_cycles)
continue;
else {
pc -= 2;
break;
}
}
HL = hl;
BC = bc;
f = (a ^ n ^ z) & Z80_H_FLAG;
#ifndef Z80_DOCUMENTED_FLAGS_ONLY
n = z - (f >> Z80_H_FLAG_SHIFT);
f |= (n << (Z80_Y_FLAG_SHIFT - 1))
& Z80_Y_FLAG;
f |= n & Z80_X_FLAG;
#endif
f |= SZYX_FLAGS_TABLE[z & 0xff] & SZ_FLAGS;
f |= bc ? Z80_P_FLAG : 0;
F = f | Z80_N_FLAG | (F & Z80_C_FLAG);
break;
}
/* 8-bit arithmetic and logical group. */
case ADD_R: {
ADD(R(Z(opcode)));
break;
}
case ADD_N: {
int n;
READ_N(n);
ADD(n);
break;
}
case ADD_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
ADD(x);
break;
}
case ADC_R: {
ADC(R(Z(opcode)));
break;
}
case ADC_N: {
int n;
READ_N(n);
ADC(n);
break;
}
case ADC_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
ADC(x);
break;
}
case SUB_R: {
SUB(R(Z(opcode)));
break;
}
case SUB_N: {
int n;
READ_N(n);
SUB(n);
break;
}
case SUB_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
SUB(x);
break;
}
case SBC_R: {
SBC(R(Z(opcode)));
break;
}
case SBC_N: {
int n;
READ_N(n);
SBC(n);
break;
}
case SBC_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
SBC(x);
break;
}
case AND_R: {
AND(R(Z(opcode)));
break;
}
case AND_N: {
int n;
READ_N(n);
AND(n);
break;
}
case AND_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
AND(x);
break;
}
case OR_R: {
OR(R(Z(opcode)));
break;
}
case OR_N: {
int n;
READ_N(n);
OR(n);
break;
}
case OR_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
OR(x);
break;
}
case XOR_R: {
XOR(R(Z(opcode)));
break;
}
case XOR_N: {
int n;
READ_N(n);
XOR(n);
break;
}
case XOR_INDIRECT_HL: {
int x;
READ_INDIRECT_HL(x);
XOR(x);
break;