-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.c
1107 lines (999 loc) · 26.6 KB
/
cpu.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
#include "cpu.h"
#include <string.h>
#define SET_ZSP(c, val) \
do { \
c->zf = (val) == 0; \
c->sf = (val) >> 7; \
c->pf = parity(val); \
} while (0)
enum OPCODES
{
OP_NOP,
OP_LXI_B,
OP_STAX_B,
OP_INX_B,
OP_INR_B,
OP_DCR_B,
OP_MVI_B,
OP_RLC,
OP_INVAL_1,
OP_DAD_B,
OP_LDAX_B,
OP_DCX_B,
OP_INR_C,
OP_DCR_C,
OP_MVI_C,
OP_RRC,
OP_INVAL_2,
OP_LXI_D,
OP_STAX_D,
OP_INX_D,
OP_INR_D,
OP_DCR_D,
OP_MVI_D,
OP_RAL,
OP_INVAL_3,
OP_DAD_D,
OP_LDAX_D,
OP_DCX_D,
OP_INR_E,
OP_DCR_E,
OP_MVI_E,
OP_RAR,
OP_INVAL_4,
OP_LXI_H,
OP_SHLD,
OP_INX_H,
OP_INR_H,
OP_DCR_H,
OP_MVI_H,
OP_DAA,
OP_INVAL_5,
OP_DAD_H,
OP_LHLD,
OP_DCX_H,
OP_INR_L,
OP_DCR_L,
OP_MVI_L,
OP_CMA,
OP_INVAL_6,
OP_LXI_SP,
OP_STA,
OP_INX_SP,
OP_INR_M,
OP_DCR_M,
OP_MVI_M,
OP_STC,
OP_INVAL_7,
OP_DAD_SP,
OP_LDA,
OP_DCX_SP,
OP_INR_A,
OP_DCR_A,
OP_MVI_A,
OP_CMC,
OP_MOV_B_B,
OP_MOV_B_C,
OP_MOV_B_D,
OP_MOV_B_E,
OP_MOV_B_H,
OP_MOV_B_L,
OP_MOV_B_M,
OP_MOV_B_A,
OP_MOV_C_B,
OP_MOV_C_C,
OP_MOV_C_D,
OP_MOV_C_E,
OP_MOV_C_H,
OP_MOV_C_L,
OP_MOV_C_M,
OP_MOV_C_A,
OP_MOV_D_B,
OP_MOV_D_C,
OP_MOV_D_D,
OP_MOV_D_E,
OP_MOV_D_H,
OP_MOV_D_L,
OP_MOV_D_M,
OP_MOV_D_A,
OP_MOV_E_B,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, 5, 4, 4, 4, 4, 4,
4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4,
4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4,
7, 4, 5, 10, 10, 10, 11, 11, 7, 11, 5, 10, 10, 10, 11, 17, 7, 11, 5,
10, 10, 10, 11, 11, 7, 11, 5, 10, 10, 10, 11, 17, 7, 11, 5, 10, 10, 18,
11, 11, 7, 11, 5, 5, 10, 4, 11, 17, 7, 11, 5, 10, 10, 4, 11, 11, 7,
11, 5, 5, 10, 4, 11, 17, 7, 11
};
const char* op_name[] = {
"NOP", "LXI B ,D16", "STAX B", "INX B", "INR B",
"DCR B", "MVI B ,D8", "RLC", "ILL", "DAD B",
"LDAX B", "DCX B", "INR C", "DCR C", "MVI C ,D8",
"RRC", "ILL", "LXI D ,D16", "STAX D", "INX D",
"INR D", "DCR D", "MVI D ,D8", "RAL", "ILL",
"DAD D", "LDAX D", "DCX D", "INR E", "DCR E",
"MVI E ,D8", "RAR", "ILL", "LXI H ,D16", "SHLD",
"INX H", "INR H", "DCR H", "MVI H ,D8", "DAA",
"ILL", "DAD H", "LHLD", "DCX H", "INR L",
"DCR L", "MVI L ,D8", "CMA", "ILL", "LXI SP ,D16",
"STA A16", "INX SP", "INR M", "DCR M", "MVI M ,D8",
"STC", "ILL", "DAD SP", "LDA A16", "DCX SP",
"INR A", "DCR A", "MVI A ,D8", "CMC", "MOV B ,B",
"MOV B ,C", "MOV B ,D", "MOV B ,E", "MOV B ,H", "MOV B ,L",
"MOV B ,M", "MOV B ,A", "MOV C ,B", "MOV C ,C", "MOV C ,D",
"MOV C ,E", "MOV C ,H", "MOV C ,L", "MOV C ,M", "MOV C ,A",
"MOV D ,B", "MOV D ,C", "MOV D ,D", "MOV D ,E", "MOV D ,H",
"MOV D ,L", "MOV D ,M", "MOV D ,A", "MOV E ,B", "MOV E ,C",
"MOV E ,D", "MOV E ,E", "MOV E ,H", "MOV E ,L", "MOV E ,M",
"MOV E ,A", "MOV H ,B", "MOV H ,C", "MOV H ,D", "MOV H ,E",
"MOV H ,H", "MOV H ,L", "MOV H ,M", "MOV H ,A", "MOV L ,B",
"MOV L ,C", "MOV L ,D", "MOV L ,E", "MOV L ,H", "MOV L ,L",
"MOV L ,M", "MOV L ,A", "MOV M ,B", "MOV M ,C", "MOV M ,D",
"MOV M ,E", "MOV M ,H", "MOV M ,L", "HLT", "MOV M ,A",
"MOV A ,B", "MOV A ,C", "MOV A ,D", "MOV A , E", "MOV A ,H",
"MOV A ,L", "MOV A ,M", "MOV A ,A", "ADD B", "ADD C",
"ADD D", "ADD E", "ADD H", "ADD L", "ADD M",
"ADD A", "ADC B", "ADC C", "ADC D", "ADC E",
"ADC H", "ADC L", "ADC M", "ADC A", "SUB B",
"SUB C", "SUB D", "SUB E", "SUB H", "SUB L",
"SUB M", "SUB A", "SBB B", "SBB C", "SBB D",
"SBB E", "SBB H", "SBB L", "SBB M", "SBB A",
"ANA B", "ANA C", "ANA D", "ANA E", "ANA H",
"ANA L", "ANA M", "ANA A", "XRA B", "XRA C",
"XRA D", "XRA E", "XRA H", "XRA L", "XRA M",
"XRA A", "ORA B", "ORA C", "ORA D", "ORA E",
"ORA H", "ORA L", "ORA M", "ORA A", "CMP B",
"CMP C", "CMP D", "CMP E", "CMP H", "CMP L",
"CMP M", "CMP A", "RNZ", "POP B", "JNZ A16",
"JMP A16", "CNZ A16", "PUSH B", "ADI D8", "RST 0",
"RZ", "RET", "JZ A16", "ILL", "CZ A16",
"CALL A16", "ACI D8", "RST 1", "RNC", "POP D",
"JNC A16", "OUT P", "CNC A16", "PUSH D", "SUI D8",
"RST 2", "RC", "ILL", "JC A16", "IN P",
"CC A16", "ILL", "SBI D8", "RST 3", "RPO",
"POP H", "JPO A16", "XTHL", "CPO A16", "PUSH H",
"ANI D8", "RST 4", "RPE", "PCHL", "JPE A16",
"XCHG", "CPE A16", "ILL", "XRI D8", "RST 5",
"RP", "POP PSW", "JP A16", "DI", "CP A16",
"PUSH PSW", "ORI D8", "RST 6", "RM", "SPHL",
"JM A16", "EI", "CM A16", "ILL", "CPI D8",
"RST 7"
};
static const int op_size[] = {
1, 3, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 3, 1, 1, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 1, 2, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 3, 3, 1,
1, 1, 2, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 2, 1, 1, 1, 3, 1, 3, 3, 2, 1,
1, 1, 3, 2, 3, 1, 2, 1, 1, 1, 3, 2, 3, 1, 2, 1, 1, 1, 3, 1, 3, 1, 2, 1, 1, 1,
3, 1, 3, 1, 2, 1, 1, 1, 3, 1, 3, 1, 2, 1, 1, 1, 3, 1, 3, 1, 2, 1,
};
static uint8_t
rb(CPU* c, uint16_t addr)
{
return c->rb(c->udata, addr);
}
static void
wb(CPU* c, uint16_t addr, uint8_t val)
{
c->wb(c->udata, addr, val);
}
static uint16_t
rw(CPU* c, uint16_t addr)
{
return c->rb(c->udata, addr + 1) << 8 | c->rb(c->udata, addr);
}
static void
ww(CPU* c, uint16_t addr, uint16_t val)
{
c->wb(c->udata, addr, val & 0xff);
c->wb(c->udata, addr + 1, val >> 8);
}
static uint8_t
nb(CPU* c)
{
return rb(c, c->pc++);
}
static uint16_t
nw(CPU* c)
{
uint16_t res;
res = rw(c, c->pc);
c->pc += 2;
return res;
}
static void
set_bc(CPU* c, uint16_t val)
{
c->b = val >> 8;
c->c = val & 0xff;
}
static void
set_de(CPU* c, uint16_t val)
{
c->d = val >> 8;
c->e = val & 0xff;
}
static void
set_hl(CPU* c, uint16_t val)
{
c->h = val >> 8;
c->l = val & 0xff;
}
static uint16_t
get_bc(CPU* c)
{
return (c->b << 8) | c->c;
}
static uint16_t
get_de(CPU* c)
{
return (c->d << 8) | c->e;
}
static uint16_t
get_hl(CPU* c)
{
return (c->h << 8) | c->l;
}
static void
push_stack(CPU* c, uint16_t val)
{
c->sp -= 2;
ww(c, c->sp, val);
}
static uint16_t
pop_stack(CPU* c)
{
uint16_t val;
val = rw(c, c->sp);
c->sp += 2;
return val;
}
static bool
parity(uint8_t val)
{
uint8_t n;
n = 0;
for (int i = 0; i < 8; ++i) {
if ((val >> i) & 1)
++n;
}
if (n & 1)
return false;
return true;
}
static bool
carry(int n, uint8_t a, uint8_t b, bool c)
{
int16_t res, carry;
int dst;
dst = 1 << n;
res = a + b + c;
carry = res ^ a ^ b;
if (dst & carry)
return true;
return false;
}
static void
add(CPU* c, uint8_t* reg, uint8_t val, bool cy)
{
uint8_t res;
res = *reg + val + cy;
c->cf = carry(8, *reg, val, cy);
c->hf = carry(4, *reg, val, cy);
SET_ZSP(c, res);
*reg = res;
}
static void
sub(CPU* c, uint8_t* reg, uint8_t val, bool cy)
{
add(c, reg, ~val, !cy);
c->cf = !c->cf;
}
/* add a word to HL */
static void
dad(CPU* c, uint16_t val)
{
uint16_t hl;
hl = get_hl(c);
c->cf = ((hl + val) >> 16) & 1;
set_hl(c, hl + val);
}
static uint8_t
inr(CPU* c, uint8_t val)
{
uint8_t res;
res = val + 1;
c->hf = (res & 0xf) == 0;
SET_ZSP(c, res);
return res;
}
static uint8_t
dcr(CPU* c, uint8_t val)
{
uint8_t res;
res = val - 1;
c->hf = !((res & 0xf) == 0xf);
SET_ZSP(c, res);
return res;
}
static void
ana(CPU* c, uint8_t val)
{
uint8_t res;
res = c->a & val;
c->cf = 0;
c->hf = ((c->a | val) & 0x08) != 0;
SET_ZSP(c, res);
c->a = res;
}
/* xor and store */
static void
xra(CPU* c, uint8_t val)
{
c->a ^= val;
c->cf = 0;
c->hf = 0;
SET_ZSP(c, c->a);
}
static void
ora(CPU* c, uint8_t val)
{
c->a |= val;
c->cf = 0;
c->hf = 0;
SET_ZSP(c, c->a);
}
static void
cmp(CPU* c, uint8_t val)
{
int16_t res;
res = c->a - val;
c->cf = res >> 8; /* check carry */
c->hf = ~(c->a ^ res ^ val) & 0x10;
SET_ZSP(c, res & 0xff);
}
static void
jmp(CPU* c, uint16_t addr)
{
c->pc = addr;
}
static void
cond_jmp(CPU* c, bool cond)
{
uint16_t addr;
addr = nw(c);
if (cond)
c->pc = addr;
}
static void
call(CPU* c, uint16_t addr)
{
push_stack(c, c->pc);
jmp(c, addr);
}
static void
cond_call(CPU* c, bool cond)
{
uint16_t addr;
addr = nw(c);
if (cond) {
call(c, addr);
c->cyc += 6;
}
}
static void
ret(CPU* c)
{
c->pc = pop_stack(c);
}
static void
cond_ret(CPU* c, bool cond)
{
if (cond) {
ret(c);
c->cyc += 6;
}
}
static void
push_psw(CPU* c)
{
uint8_t psw;
psw = 0;
psw |= c->sf << 7;
psw |= c->zf << 6;
psw |= c->hf << 4;
psw |= c->pf << 2;
psw |= 1 << 1;
psw |= c->cf << 0;
push_stack(c, c->a << 8 | psw);
}
static void
pop_psw(CPU* c)
{
uint16_t af;
uint8_t psw;
af = pop_stack(c);
c->a = af >> 8;
psw = af & 0xff;
c->sf = (psw >> 7) & 1;
c->zf = (psw >> 6) & 1;
c->hf = (psw >> 4) & 1;
c->pf = (psw >> 2) & 1;
c->cf = (psw >> 0) & 1;
}
/* rotate a left */
static void
rlc(CPU* c)
{
c->cf = c->a >> 7;
c->a = (c->a << 1) | c->cf;
}
/* rotate right */
static void
rrc(CPU* c)
{
c->cf = c->a & 1;
c->a = (c->a >> 1) | (c->cf << 7);
}
/* rotate left with carry */
static void
ral(CPU* c)
{
bool cy;
cy = c->cf;
c->cf = c->a >> 7;
c->a = (c->a << 1) | cy;
}
static void
rar(CPU* c)
{
bool cy;
cy = c->cf;
c->cf = c->a & 1;
c->a = (c->a >> 1) | (cy << 7);
}
static void
daa(CPU* c)
{
bool cy;
uint8_t correction;
uint8_t lsb, msb;
cy = c->cf;
correction = 0;
lsb = c->a & 0x0f;
msb = c->a >> 4;
if (c->hf || lsb > 9)
correction = 0x6;
if (c->cf || msb > 9 || (msb >= 9 && lsb > 9)) {
correction += 0x60;
cy = 1;
}
add(c, &c->a, correction, 0);
c->cf = cy;
}
static void
xchg(CPU* c)
{
uint16_t de;
de = get_de(c);
set_de(c, get_hl(c));
set_hl(c, de);
}
static void
xthl(CPU* c)
{
uint16_t val;
val = rw(c, c->sp);
ww(c, c->sp, get_hl(c));
set_hl(c, val);
}
static void
debug_output(CPU* c, bool diss)
{
uint8_t f;
f = 0;
f |= c->sf << 7;
f |= c->zf << 6;
f |= c->hf << 4;
f |= c->pf << 2;
f |= 1 << 1;
f |= c->cf << 0;
printf("PC: %04X, AF: %04X, BC: %04X, DE: %04X, HL: %04X, SP: %04X, CYC: %lu",
c->pc,
c->a << 8 | f,
get_bc(c),
get_de(c),
get_hl(c),
c->sp,
c->cyc);
printf("\t(%02X %02X %02X %02X)",
rb(c, c->pc),
rb(c, c->pc + 1),
rb(c, c->pc + 2),
rb(c, c->pc + 3));
if (diss) {
printf(" - %s", op_name[rb(c, c->pc)]);
}
printf("\n");
}
/* clang-format off */
int
cpu_execute(CPU* c, uint8_t opcode)
{
c->cyc += op_cycles[opcode];
if (c->inter_delay > 0)
c->inter_delay -= 1;
switch (opcode) {
case OP_MOV_A_A: c->a = c->a; break;
case OP_MOV_A_B: c->a = c->b; break;
case OP_MOV_A_C: c->a = c->c; break;
case OP_MOV_A_D: c->a = c->d; break;
case OP_MOV_A_E: c->a = c->e; break;
case OP_MOV_A_H: c->a = c->h; break;
case OP_MOV_A_L: c->a = c->l; break;
case OP_MOV_A_M: c->a = rb(c, get_hl(c)); break;
case OP_LDAX_B: c->a = rb(c, get_bc(c)); break;
case OP_LDAX_D: c->a = rb(c, get_de(c)); break;
case OP_LDA: c->a = rb(c, nw(c)); break;
case OP_MOV_B_A: c->b = c->a; break;
case OP_MOV_B_B: c->b = c->b; break;
case OP_MOV_B_C: c->b = c->c; break;
case OP_MOV_B_D: c->b = c->d; break;
case OP_MOV_B_E: c->b = c->e; break;
case OP_MOV_B_H: c->b = c->h; break;
case OP_MOV_B_L: c->b = c->l; break;
case OP_MOV_B_M: c->b = rb(c, get_hl(c)); break;
case OP_MOV_C_A: c->c = c->a; break;
case OP_MOV_C_B: c->c = c->b; break;
case OP_MOV_C_C: c->c = c->c; break;
case OP_MOV_C_D: c->c = c->d; break;
case OP_MOV_C_E: c->c = c->e; break;
case OP_MOV_C_H: c->c = c->h; break;
case OP_MOV_C_L: c->c = c->l; break;
case OP_MOV_C_M: c->c = rb(c, get_hl(c)); break;
case OP_MOV_D_A: c->d = c->a; break;
case OP_MOV_D_B: c->d = c->b; break;
case OP_MOV_D_C: c->d = c->c; break;
case OP_MOV_D_D: c->d = c->d; break;
case OP_MOV_D_E: c->d = c->e; break;
case OP_MOV_D_H: c->d = c->h; break;
case OP_MOV_D_L: c->d = c->l; break;
case OP_MOV_D_M: c->d = rb(c, get_hl(c)); break;
case OP_MOV_E_A: c->e = c->a; break;
case OP_MOV_E_B: c->e = c->b; break;
case OP_MOV_E_C: c->e = c->c; break;
case OP_MOV_E_D: c->e = c->d; break;
case OP_MOV_E_E: c->e = c->e; break;
case OP_MOV_E_H: c->e = c->h; break;
case OP_MOV_E_L: c->e = c->l; break;
case OP_MOV_E_M: c->e = rb(c, get_hl(c)); break;
case OP_MOV_H_A: c->h = c->a; break;
case OP_MOV_H_B: c->h = c->b; break;
case OP_MOV_H_C: c->h = c->c; break;
case OP_MOV_H_D: c->h = c->d; break;
case OP_MOV_H_E: c->h = c->e; break;
case OP_MOV_H_H: c->h = c->h; break;
case OP_MOV_H_L: c->h = c->l; break;
case OP_MOV_H_M: c->h = rb(c, get_hl(c)); break;
case OP_MOV_L_A: c->l = c->a; break;
case OP_MOV_L_B: c->l = c->b; break;
case OP_MOV_L_C: c->l = c->c; break;
case OP_MOV_L_D: c->l = c->d; break;
case OP_MOV_L_E: c->l = c->e; break;
case OP_MOV_L_H: c->l = c->h; break;
case OP_MOV_L_L: c->l = c->l; break;
case OP_MOV_L_M: c->l = rb(c, get_hl(c)); break;
case OP_MOV_M_A: wb(c, get_hl(c), c->a); break;
case OP_MOV_M_B: wb(c, get_hl(c), c->b); break;
case OP_MOV_M_C: wb(c, get_hl(c), c->c); break;
case OP_MOV_M_D: wb(c, get_hl(c), c->d); break;
case OP_MOV_M_E: wb(c, get_hl(c), c->e); break;
case OP_MOV_M_H: wb(c, get_hl(c), c->h); break;
case OP_MOV_M_L: wb(c, get_hl(c), c->l); break;
case OP_MVI_A: c->a = nb(c); break;
case OP_MVI_B: c->b = nb(c); break;
case OP_MVI_C: c->c = nb(c); break;
case OP_MVI_D: c->d = nb(c); break;
case OP_MVI_E: c->e = nb(c); break;
case OP_MVI_H: c->h = nb(c); break;
case OP_MVI_L: c->l = nb(c); break;
case OP_MVI_M: wb(c, get_hl(c), nb(c)); break;
case OP_STAX_B: wb(c, get_bc(c), c->a); break;
case OP_STAX_D: wb(c, get_de(c), c->a); break;
case OP_STA: wb(c, nw(c), c->a); break;
case OP_LXI_B: set_bc(c, nw(c)); break;
case OP_LXI_D: set_de(c, nw(c)); break;
case OP_LXI_H: set_hl(c, nw(c)); break;
case OP_LXI_SP: c->sp = nw(c); break;
case OP_LHLD: set_hl(c, rw(c, nw(c))); break;
case OP_SHLD: ww(c, nw(c), get_hl(c)); break;
case OP_SPHL: c->sp = get_hl(c); break;
case OP_XCHG: xchg(c); break;
case OP_XTHL: xthl(c); break;
case OP_ADD_A: add(c, &c->a, c->a, 0); break;
case OP_ADD_B: add(c, &c->a, c->b, 0); break;
case OP_ADD_C: add(c, &c->a, c->c, 0); break;
case OP_ADD_D: add(c, &c->a, c->d, 0); break;
case OP_ADD_E: add(c, &c->a, c->e, 0); break;
case OP_ADD_H: add(c, &c->a, c->h, 0); break;
case OP_ADD_L: add(c, &c->a, c->l, 0); break;
case OP_ADD_M: add(c, &c->a, rb(c, get_hl(c)), 0); break;
case OP_ADI: add(c, &c->a, nb(c), 0); break;
case OP_ADC_A: add(c, &c->a, c->a, c->cf); break;
case OP_ADC_B: add(c, &c->a, c->b, c->cf); break;
case OP_ADC_C: add(c, &c->a, c->c, c->cf); break;
case OP_ADC_D: add(c, &c->a, c->d, c->cf); break;
case OP_ADC_E: add(c, &c->a, c->e, c->cf); break;
case OP_ADC_H: add(c, &c->a, c->h, c->cf); break;
case OP_ADC_L: add(c, &c->a, c->l, c->cf); break;
case OP_ADC_M: add(c, &c->a, rb(c, get_hl(c)), c->cf); break;
case OP_ACI: add(c, &c->a, nb(c), c->cf); break;
case OP_SUB_A: sub(c, &c->a, c->a, 0); break;
case OP_SUB_B: sub(c, &c->a, c->b, 0); break;
case OP_SUB_C: sub(c, &c->a, c->c, 0); break;
case OP_SUB_D: sub(c, &c->a, c->d, 0); break;
case OP_SUB_E: sub(c, &c->a, c->e, 0); break;
case OP_SUB_H: sub(c, &c->a, c->h, 0); break;
case OP_SUB_L: sub(c, &c->a, c->l, 0); break;
case OP_SUB_M: sub(c, &c->a, rb(c, get_hl(c)), 0); break;
case OP_SUI: sub(c, &c->a, nb(c), 0); break;
case OP_SBB_A: sub(c, &c->a, c->a, c->cf); break;
case OP_SBB_B: sub(c, &c->a, c->b, c->cf); break;
case OP_SBB_C: sub(c, &c->a, c->c, c->cf); break;
case OP_SBB_D: sub(c, &c->a, c->d, c->cf); break;
case OP_SBB_E: sub(c, &c->a, c->e, c->cf); break;
case OP_SBB_H: sub(c, &c->a, c->h, c->cf); break;
case OP_SBB_L: sub(c, &c->a, c->l, c->cf); break;
case OP_SBB_M: sub(c, &c->a, rb(c, get_hl(c)), c->cf); break;
case OP_SBI: sub(c, &c->a, nb(c), c->cf); break;
case OP_DAD_B: dad(c, get_bc(c)); break;
case OP_DAD_D: dad(c, get_de(c)); break;
case OP_DAD_H: dad(c, get_hl(c)); break;
case OP_DAD_SP: dad(c, c->sp); break;
case OP_DI: c->iff = 0; break;
case OP_EI: c->iff = c->inter_delay = 1; break;
case OP_NOP: break;
case OP_HLT: c->halted = 1; break;
case OP_INR_A: c->a = inr(c, c->a); break;
case OP_INR_B: c->b = inr(c, c->b); break;
case OP_INR_C: c->c = inr(c, c->c); break;
case OP_INR_D: c->d = inr(c, c->d); break;
case OP_INR_E: c->e = inr(c, c->e); break;
case OP_INR_H: c->h = inr(c, c->h); break;
case OP_INR_L: c->l = inr(c, c->l); break;
case OP_INR_M: wb(c, get_hl(c), inr(c, rb(c, get_hl(c)))); break;
case OP_DCR_A: c->a = dcr(c, c->a); break;
case OP_DCR_B: c->b = dcr(c, c->b); break;
case OP_DCR_C: c->c = dcr(c, c->c); break;
case OP_DCR_D: c->d = dcr(c, c->d); break;
case OP_DCR_E: c->e = dcr(c, c->e); break;
case OP_DCR_H: c->h = dcr(c, c->h); break;
case OP_DCR_L: c->l = dcr(c, c->l); break;
case OP_DCR_M: wb(c, get_hl(c), dcr(c, rb(c, get_hl(c)))); break;
case OP_INX_B: set_bc(c, get_bc(c) + 1); break;
case OP_INX_D: set_de(c, get_de(c) + 1); break;
case OP_INX_H: set_hl(c, get_hl(c) + 1); break;
case OP_INX_SP: c->sp += 1; break;
case OP_DCX_B: set_bc(c, get_bc(c) - 1); break;
case OP_DCX_D: set_de(c, get_de(c) - 1); break;
case OP_DCX_H: set_hl(c, get_hl(c) - 1); break;
case OP_DCX_SP: c->sp -= 1; break;
case OP_DAA: daa(c); break;
case OP_CMA: c->a = ~c->a; break;
case OP_STC: c->cf = 1; break;
case OP_CMC: c->cf = !c->cf; break;
case OP_RLC: rlc(c); break;
case OP_RRC: rrc(c); break;
case OP_RAL: ral(c); break;
case OP_RAR: rar(c); break;
case OP_ANA_A: ana(c, c->a); break;
case OP_ANA_B: ana(c, c->b); break;
case OP_ANA_C: ana(c, c->c); break;
case OP_ANA_D: ana(c, c->d); break;
case OP_ANA_E: ana(c, c->e); break;
case OP_ANA_H: ana(c, c->h); break;
case OP_ANA_L: ana(c, c->l); break;
case OP_ANA_M: ana(c, rb(c, get_hl(c))); break;
case OP_ANI: ana(c, nb(c)); break;
case OP_XRA_A: xra(c, c->a); break;
case OP_XRA_B: xra(c, c->b); break;
case OP_XRA_C: xra(c, c->c); break;
case OP_XRA_D: xra(c, c->d); break;
case OP_XRA_E: xra(c, c->e); break;
case OP_XRA_H: xra(c, c->h); break;
case OP_XRA_L: xra(c, c->l); break;
case OP_XRA_M: xra(c, rb(c, get_hl(c))); break;
case OP_XRI: xra(c, nb(c)); break;
case OP_ORA_A: ora(c, c->a); break;
case OP_ORA_B: ora(c, c->b); break;
case OP_ORA_C: ora(c, c->c); break;
case OP_ORA_D: ora(c, c->d); break;
case OP_ORA_E: ora(c, c->e); break;
case OP_ORA_H: ora(c, c->h); break;
case OP_ORA_L: ora(c, c->l); break;
case OP_ORA_M: ora(c, rb(c, get_hl(c))); break;
case OP_ORI: ora(c, nb(c)); break;
case OP_CMP_A: cmp(c, c->a); break;
case OP_CMP_B: cmp(c, c->b); break;
case OP_CMP_C: cmp(c, c->c); break;
case OP_CMP_D: cmp(c, c->d); break;
case OP_CMP_E: cmp(c, c->e); break;
case OP_CMP_H: cmp(c, c->h); break;
case OP_CMP_L: cmp(c, c->l); break;
case OP_CMP_M: cmp(c, rb(c, get_hl(c))); break;
case OP_CPI: cmp(c, nb(c)); break;