-
Notifications
You must be signed in to change notification settings - Fork 2
/
8080emu.cpp
2427 lines (2319 loc) · 62.2 KB
/
8080emu.cpp
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
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org/>
*/
#include "memoryBase.h"
#include "memory.h"
#include "8080emuCPP.h"
#define PRINTOPS 1
namespace {
static int parity(int x, int size) {
int i;
int p = 0;
x = (x & ((1 << size) - 1));
for (i = 0; i < size; i++) {
if (x & 0x1) p++;
x = x >> 1;
}
return (0 == (p & 0x1));
}
unsigned char cycles8080[] = {
4, 10, 7, 5, 5, 5, 7, 4, 4, 10, 7, 5, 5, 5, 7, 4, //0x00..0x0f
4, 10, 7, 5, 5, 5, 7, 4, 4, 10, 7, 5, 5, 5, 7, 4, //0x10..0x1f
4, 10, 16, 5, 5, 5, 7, 4, 4, 10, 16, 5, 5, 5, 7, 4, //etc
4, 10, 13, 5, 10, 10, 10, 4, 4, 10, 13, 5, 5, 5, 7, 4,
5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5, //0x40..0x4f
5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5,
5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5,
7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 7, 5,
4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, //0x80..8x4f
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,
11, 10, 10, 10, 17, 11, 7, 11, 11, 10, 10, 10, 10, 17, 7, 11, //0xc0..0xcf
11, 10, 10, 10, 17, 11, 7, 11, 11, 10, 10, 10, 10, 17, 7, 11,
11, 10, 10, 18, 17, 11, 7, 11, 11, 5, 10, 5, 17, 17, 7, 11,
11, 10, 10, 4, 17, 11, 7, 11, 11, 5, 10, 4, 17, 17, 7, 11,
};
int Disassemble8080Op(MemoryBase *codebuffer, int pc) {
unsigned char *code = &codebuffer->at(pc);
char buf[50];
int opbytes = 1;
printf("%04x ", ((Memory *)codebuffer)->getBaseRegister());
printf("%04x ", pc);
switch (*code) {
case 0x00:
sprintf(buf, "NOP");
break;
case 0x01:
sprintf(buf, "LXI B,#$%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x02:
sprintf(buf, "STAX B");
break;
case 0x03:
sprintf(buf, "INX B");
break;
case 0x04:
sprintf(buf, "INR B");
break;
case 0x05:
sprintf(buf, "DCR B");
break;
case 0x06:
sprintf(buf, "MVI B,#$%02x", code[1]);
opbytes = 2;
break;
case 0x07:
sprintf(buf, "RLC");
break;
case 0x08:
sprintf(buf, "NOP");
break;
case 0x09:
sprintf(buf, "DAD B");
break;
case 0x0a:
sprintf(buf, "LDAX B");
break;
case 0x0b:
sprintf(buf, "DCX B");
break;
case 0x0c:
sprintf(buf, "INR C");
break;
case 0x0d:
sprintf(buf, "DCR C");
break;
case 0x0e:
sprintf(buf, "MVI C,#$%02x", code[1]);
opbytes = 2;
break;
case 0x0f:
sprintf(buf, "RRC");
break;
case 0x10:
sprintf(buf, "NOP");
break;
case 0x11:
sprintf(buf, "LXI D,#$%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x12:
sprintf(buf, "STAX D");
break;
case 0x13:
sprintf(buf, "INX D");
break;
case 0x14:
sprintf(buf, "INR D");
break;
case 0x15:
sprintf(buf, "DCR D");
break;
case 0x16:
sprintf(buf, "MVI D,#$%02x", code[1]);
opbytes = 2;
break;
case 0x17:
sprintf(buf, "RAL");
break;
case 0x18:
sprintf(buf, "NOP");
break;
case 0x19:
sprintf(buf, "DAD D");
break;
case 0x1a:
sprintf(buf, "LDAX D");
break;
case 0x1b:
sprintf(buf, "DCX D");
break;
case 0x1c:
sprintf(buf, "INR E");
break;
case 0x1d:
sprintf(buf, "DCR E");
break;
case 0x1e:
sprintf(buf, "MVI E,#$%02x", code[1]);
opbytes = 2;
break;
case 0x1f:
sprintf(buf, "RAR");
break;
case 0x20:
sprintf(buf, "NOP");
break;
case 0x21:
sprintf(buf, "LXI H,#$%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x22:
sprintf(buf, "SHLD $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x23:
sprintf(buf, "INX H");
break;
case 0x24:
sprintf(buf, "INR H");
break;
case 0x25:
sprintf(buf, "DCR H");
break;
case 0x26:
sprintf(buf, "MVI H,#$%02x", code[1]);
opbytes = 2;
break;
case 0x27:
sprintf(buf, "DAA");
break;
case 0x28:
sprintf(buf, "NOP");
break;
case 0x29:
sprintf(buf, "DAD H");
break;
case 0x2a:
sprintf(buf, "LHLD $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x2b:
sprintf(buf, "DCX H");
break;
case 0x2c:
sprintf(buf, "INR L");
break;
case 0x2d:
sprintf(buf, "DCR L");
break;
case 0x2e:
sprintf(buf, "MVI L,#$%02x", code[1]);
opbytes = 2;
break;
case 0x2f:
sprintf(buf, "CMA");
break;
case 0x30:
sprintf(buf, "NOP");
break;
case 0x31:
sprintf(buf, "LXI SP,#$%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x32:
sprintf(buf, "STA $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x33:
sprintf(buf, "INX SP");
break;
case 0x34:
sprintf(buf, "INR M");
break;
case 0x35:
sprintf(buf, "DCR M");
break;
case 0x36:
sprintf(buf, "MVI M,#$%02x", code[1]);
opbytes = 2;
break;
case 0x37:
sprintf(buf, "STC");
break;
case 0x38:
sprintf(buf, "NOP");
break;
case 0x39:
sprintf(buf, "DAD SP");
break;
case 0x3a:
sprintf(buf, "LDA $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0x3b:
sprintf(buf, "DCX SP");
break;
case 0x3c:
sprintf(buf, "INR A");
break;
case 0x3d:
sprintf(buf, "DCR A");
break;
case 0x3e:
sprintf(buf, "MVI A,#$%02x", code[1]);
opbytes = 2;
break;
case 0x3f:
sprintf(buf, "CMC");
break;
case 0x40:
sprintf(buf, "MOV B,B");
break;
case 0x41:
sprintf(buf, "MOV B,C");
break;
case 0x42:
sprintf(buf, "MOV B,D");
break;
case 0x43:
sprintf(buf, "MOV B,E");
break;
case 0x44:
sprintf(buf, "MOV B,H");
break;
case 0x45:
sprintf(buf, "MOV B,L");
break;
case 0x46:
sprintf(buf, "MOV B,M");
break;
case 0x47:
sprintf(buf, "MOV B,A");
break;
case 0x48:
sprintf(buf, "MOV C,B");
break;
case 0x49:
sprintf(buf, "MOV C,C");
break;
case 0x4a:
sprintf(buf, "MOV C,D");
break;
case 0x4b:
sprintf(buf, "MOV C,E");
break;
case 0x4c:
sprintf(buf, "MOV C,H");
break;
case 0x4d:
sprintf(buf, "MOV C,L");
break;
case 0x4e:
sprintf(buf, "MOV C,M");
break;
case 0x4f:
sprintf(buf, "MOV C,A");
break;
case 0x50:
sprintf(buf, "MOV D,B");
break;
case 0x51:
sprintf(buf, "MOV D,C");
break;
case 0x52:
sprintf(buf, "MOV D,D");
break;
case 0x53:
sprintf(buf, "MOV D.E");
break;
case 0x54:
sprintf(buf, "MOV D,H");
break;
case 0x55:
sprintf(buf, "MOV D,L");
break;
case 0x56:
sprintf(buf, "MOV D,M");
break;
case 0x57:
sprintf(buf, "MOV D,A");
break;
case 0x58:
sprintf(buf, "MOV E,B");
break;
case 0x59:
sprintf(buf, "MOV E,C");
break;
case 0x5a:
sprintf(buf, "MOV E,D");
break;
case 0x5b:
sprintf(buf, "MOV E,E");
break;
case 0x5c:
sprintf(buf, "MOV E,H");
break;
case 0x5d:
sprintf(buf, "MOV E,L");
break;
case 0x5e:
sprintf(buf, "MOV E,M");
break;
case 0x5f:
sprintf(buf, "MOV E,A");
break;
case 0x60:
sprintf(buf, "MOV H,B");
break;
case 0x61:
sprintf(buf, "MOV H,C");
break;
case 0x62:
sprintf(buf, "MOV H,D");
break;
case 0x63:
sprintf(buf, "MOV H.E");
break;
case 0x64:
sprintf(buf, "MOV H,H");
break;
case 0x65:
sprintf(buf, "MOV H,L");
break;
case 0x66:
sprintf(buf, "MOV H,M");
break;
case 0x67:
sprintf(buf, "MOV H,A");
break;
case 0x68:
sprintf(buf, "MOV L,B");
break;
case 0x69:
sprintf(buf, "MOV L,C");
break;
case 0x6a:
sprintf(buf, "MOV L,D");
break;
case 0x6b:
sprintf(buf, "MOV L,E");
break;
case 0x6c:
sprintf(buf, "MOV L,H");
break;
case 0x6d:
sprintf(buf, "MOV L,L");
break;
case 0x6e:
sprintf(buf, "MOV L,M");
break;
case 0x6f:
sprintf(buf, "MOV L,A");
break;
case 0x70:
sprintf(buf, "MOV M,B");
break;
case 0x71:
sprintf(buf, "MOV M,C");
break;
case 0x72:
sprintf(buf, "MOV M,D");
break;
case 0x73:
sprintf(buf, "MOV M.E");
break;
case 0x74:
sprintf(buf, "MOV M,H");
break;
case 0x75:
sprintf(buf, "MOV M,L");
break;
case 0x76:
sprintf(buf, "HLT");
break;
case 0x77:
sprintf(buf, "MOV M,A");
break;
case 0x78:
sprintf(buf, "MOV A,B");
break;
case 0x79:
sprintf(buf, "MOV A,C");
break;
case 0x7a:
sprintf(buf, "MOV A,D");
break;
case 0x7b:
sprintf(buf, "MOV A,E");
break;
case 0x7c:
sprintf(buf, "MOV A,H");
break;
case 0x7d:
sprintf(buf, "MOV A,L");
break;
case 0x7e:
sprintf(buf, "MOV A,M");
break;
case 0x7f:
sprintf(buf, "MOV A,A");
break;
case 0x80:
sprintf(buf, "ADD B");
break;
case 0x81:
sprintf(buf, "ADD C");
break;
case 0x82:
sprintf(buf, "ADD D");
break;
case 0x83:
sprintf(buf, "ADD E");
break;
case 0x84:
sprintf(buf, "ADD H");
break;
case 0x85:
sprintf(buf, "ADD L");
break;
case 0x86:
sprintf(buf, "ADD M");
break;
case 0x87:
sprintf(buf, "ADD A");
break;
case 0x88:
sprintf(buf, "ADC B");
break;
case 0x89:
sprintf(buf, "ADC C");
break;
case 0x8a:
sprintf(buf, "ADC D");
break;
case 0x8b:
sprintf(buf, "ADC E");
break;
case 0x8c:
sprintf(buf, "ADC H");
break;
case 0x8d:
sprintf(buf, "ADC L");
break;
case 0x8e:
sprintf(buf, "ADC M");
break;
case 0x8f:
sprintf(buf, "ADC A");
break;
case 0x90:
sprintf(buf, "SUB B");
break;
case 0x91:
sprintf(buf, "SUB C");
break;
case 0x92:
sprintf(buf, "SUB D");
break;
case 0x93:
sprintf(buf, "SUB E");
break;
case 0x94:
sprintf(buf, "SUB H");
break;
case 0x95:
sprintf(buf, "SUB L");
break;
case 0x96:
sprintf(buf, "SUB M");
break;
case 0x97:
sprintf(buf, "SUB A");
break;
case 0x98:
sprintf(buf, "SBB B");
break;
case 0x99:
sprintf(buf, "SBB C");
break;
case 0x9a:
sprintf(buf, "SBB D");
break;
case 0x9b:
sprintf(buf, "SBB E");
break;
case 0x9c:
sprintf(buf, "SBB H");
break;
case 0x9d:
sprintf(buf, "SBB L");
break;
case 0x9e:
sprintf(buf, "SBB M");
break;
case 0x9f:
sprintf(buf, "SBB A");
break;
case 0xa0:
sprintf(buf, "ANA B");
break;
case 0xa1:
sprintf(buf, "ANA C");
break;
case 0xa2:
sprintf(buf, "ANA D");
break;
case 0xa3:
sprintf(buf, "ANA E");
break;
case 0xa4:
sprintf(buf, "ANA H");
break;
case 0xa5:
sprintf(buf, "ANA L");
break;
case 0xa6:
sprintf(buf, "ANA M");
break;
case 0xa7:
sprintf(buf, "ANA A");
break;
case 0xa8:
sprintf(buf, "XRA B");
break;
case 0xa9:
sprintf(buf, "XRA C");
break;
case 0xaa:
sprintf(buf, "XRA D");
break;
case 0xab:
sprintf(buf, "XRA E");
break;
case 0xac:
sprintf(buf, "XRA H");
break;
case 0xad:
sprintf(buf, "XRA L");
break;
case 0xae:
sprintf(buf, "XRA M");
break;
case 0xaf:
sprintf(buf, "XRA A");
break;
case 0xb0:
sprintf(buf, "ORA B");
break;
case 0xb1:
sprintf(buf, "ORA C");
break;
case 0xb2:
sprintf(buf, "ORA D");
break;
case 0xb3:
sprintf(buf, "ORA E");
break;
case 0xb4:
sprintf(buf, "ORA H");
break;
case 0xb5:
sprintf(buf, "ORA L");
break;
case 0xb6:
sprintf(buf, "ORA M");
break;
case 0xb7:
sprintf(buf, "ORA A");
break;
case 0xb8:
sprintf(buf, "CMP B");
break;
case 0xb9:
sprintf(buf, "CMP C");
break;
case 0xba:
sprintf(buf, "CMP D");
break;
case 0xbb:
sprintf(buf, "CMP E");
break;
case 0xbc:
sprintf(buf, "CMP H");
break;
case 0xbd:
sprintf(buf, "CMP L");
break;
case 0xbe:
sprintf(buf, "CMP M");
break;
case 0xbf:
sprintf(buf, "CMP A");
break;
case 0xc0:
sprintf(buf, "RNZ");
break;
case 0xc1:
sprintf(buf, "POP B");
break;
case 0xc2:
sprintf(buf, "JNZ $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xc3:
sprintf(buf, "JMP $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xc4:
sprintf(buf, "CNZ $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xc5:
sprintf(buf, "PUSH B");
break;
case 0xc6:
sprintf(buf, "ADI #$%02x", code[1]);
opbytes = 2;
break;
case 0xc7:
sprintf(buf, "RST 0");
break;
case 0xc8:
sprintf(buf, "RZ");
break;
case 0xc9:
sprintf(buf, "RET");
break;
case 0xca:
sprintf(buf, "JZ $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xcb:
sprintf(buf, "JMP $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xcc:
sprintf(buf, "CZ $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xcd:
sprintf(buf, "CALL $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xce:
sprintf(buf, "ACI #$%02x", code[1]);
opbytes = 2;
break;
case 0xcf:
sprintf(buf, "RST 1");
break;
case 0xd0:
sprintf(buf, "RNC");
break;
case 0xd1:
sprintf(buf, "POP D");
break;
case 0xd2:
sprintf(buf, "JNC $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xd3:
sprintf(buf, "OUT #$%02x", code[1]);
opbytes = 2;
break;
case 0xd4:
sprintf(buf, "CNC $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xd5:
sprintf(buf, "PUSH D");
break;
case 0xd6:
sprintf(buf, "SUI #$%02x", code[1]);
opbytes = 2;
break;
case 0xd7:
sprintf(buf, "RST 2");
break;
case 0xd8:
sprintf(buf, "RC");
break;
case 0xd9:
sprintf(buf, "RET");
break;
case 0xda:
sprintf(buf, "JC $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xdb:
sprintf(buf, "IN #$%02x", code[1]);
opbytes = 2;
break;
case 0xdc:
sprintf(buf, "CC $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xdd:
sprintf(buf, "CALL $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xde:
sprintf(buf, "SBI #$%02x", code[1]);
opbytes = 2;
break;
case 0xdf:
sprintf(buf, "RST 3");
break;
case 0xe0:
sprintf(buf, "RPO");
break;
case 0xe1:
sprintf(buf, "POP H");
break;
case 0xe2:
sprintf(buf, "JPO $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xe3:
sprintf(buf, "XTHL");
break;
case 0xe4:
sprintf(buf, "CPO $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xe5:
sprintf(buf, "PUSH H");
break;
case 0xe6:
sprintf(buf, "ANI #$%02x", code[1]);
opbytes = 2;
break;
case 0xe7:
sprintf(buf, "RST 4");
break;
case 0xe8:
sprintf(buf, "RPE");
break;
case 0xe9:
sprintf(buf, "PCHL");
break;
case 0xea:
sprintf(buf, "JPE $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xeb:
sprintf(buf, "XCHG");
break;
case 0xec:
sprintf(buf, "CPE $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xed:
sprintf(buf, "CALL $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xee:
sprintf(buf, "XRI #$%02x", code[1]);
opbytes = 2;
break;
case 0xef:
sprintf(buf, "RST 5");
break;
case 0xf0:
sprintf(buf, "RP");
break;
case 0xf1:
sprintf(buf, "POP PSW");
break;
case 0xf2:
sprintf(buf, "JP $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xf3:
sprintf(buf, "DI");
break;
case 0xf4:
sprintf(buf, "CP $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xf5:
sprintf(buf, "PUSH PSW");
break;
case 0xf6:
sprintf(buf, "ORI #$%02x", code[1]);
opbytes = 2;
break;
case 0xf7:
sprintf(buf, "RST 6");
break;
case 0xf8:
sprintf(buf, "RM");
break;
case 0xf9:
sprintf(buf, "SPHL");
break;
case 0xfa:
sprintf(buf, "JM $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xfb:
sprintf(buf, "EI");
break;
case 0xfc:
sprintf(buf, "CM $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xfd:
sprintf(buf, "CALL $%02x%02x", code[2], code[1]);
opbytes = 3;
break;
case 0xfe:
sprintf(buf, "CPI #$%02x", code[1]);
opbytes = 2;
break;
case 0xff:
sprintf(buf, "RST 7");
break;
}
printf("%-15s", buf);
return opbytes;
}
void LogicFlagsA(State8080 *state) {
state->cc.cy = state->cc.ac = 0;
state->cc.z = (state->a == 0);
state->cc.s = (0x80 == (state->a & 0x80));
state->cc.p = parity(state->a, 8);
}
void ArithFlagsA(State8080 *state, uint16_t res) {
state->cc.cy = (res > 0xff);
state->cc.z = ((res & 0xff) == 0);
state->cc.s = (0x80 == (res & 0x80));
state->cc.p = parity(res & 0xff, 8);
}
void UnimplementedInstruction(MemoryBase *memory, State8080 *state) {
//pc will have advanced one, so undo that
printf("Error: Unimplemented instruction\n");
state->pc--;
Disassemble8080Op(memory, state->pc);
printf("\n");
exit(1);
}
void WriteMem(MemoryBase *memory, uint16_t address, uint8_t value) {
fflush(stdout);
//printf("Memory: %d\n",address);
memory->at(address) = value;
}
uint8_t ReadFromHL(MemoryBase *memory, State8080 *state) {
uint16_t offset = (state->h << 8) | state->l;
return memory->at(offset);
}
void WriteToHL(MemoryBase *memory, State8080 *state, uint8_t value) {
uint16_t offset = (state->h << 8) | state->l;
WriteMem(memory, offset, value);
}
void Push(MemoryBase *memory, State8080 *state, uint8_t high, uint8_t low) {
WriteMem(memory, state->sp - 1, high);
WriteMem(memory, state->sp - 2, low);
state->sp = state->sp - 2;
// printf ("%04x %04x\n", state->pc, state->sp);
}
void Pop(MemoryBase *memory, State8080 *state, uint8_t *high, uint8_t *low) {
*low = memory->at(state->sp);
*high = memory->at(state->sp + 1);
state->sp += 2;
//printf ("%04x %04x pop\n", state->pc, state->sp);
}
void FlagsZSP(State8080 *state, uint8_t value) {
state->cc.z = (value == 0);
state->cc.s = (0x80 == (value & 0x80));
state->cc.p = parity(value, 8);
}
} // End of unnamed namespace
unsigned CPU8080::Emulate8080p(int debug) {
lastOpcode = &memory->at(state->pc);
if(interrupt ==0){
lastOpcode = &memory->at(state->pc);
if(debug != 0)
Disassemble8080Op(memory, state->pc);
state->pc+=1;
}
else{
lastOpcode = &interrupt_code;
//Disassemble8080Op(memory,*lastOpcode);
onInterrupt();
state->pc-=2;
((Memory *)(memory))->setBaseRegister(0);
}
switch (*lastOpcode) {
case 0x00:
break; //NOP
case 0x01: //LXI B,word
state->c = lastOpcode[1];
state->b = lastOpcode[2];
state->pc += 2;
break;
case 0x02: //STAX B
{