-
Notifications
You must be signed in to change notification settings - Fork 0
/
instr_single.c
4091 lines (3707 loc) · 79.7 KB
/
instr_single.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
/*
* Z80SIM - a Z80-CPU simulator
*
* Copyright (C) 1987-2008 by Udo Munk
*
* History:
* 28-SEP-87 Development on TARGON/35 with AT&T Unix System V.3
* 11-JAN-89 Release 1.1
* 08-FEB-89 Release 1.2
* 13-MAR-89 Release 1.3
* 09-FEB-90 Release 1.4 Ported to TARGON/31 M10/30
* 20-DEC-90 Release 1.5 Ported to COHERENT 3.0
* 10-JUN-92 Release 1.6 long casting problem solved with COHERENT 3.2
* and some optimization
* 25-JUN-92 Release 1.7 comments in english and ported to COHERENT 4.0
* 02-OCT-06 Release 1.8 modified to compile on modern POSIX OS's
* 18-NOV-06 Release 1.9 modified to work with CP/M sources
* 08-DEC-06 Release 1.10 modified MMU for working with CP/NET
* 17-DEC-06 Release 1.11 TCP/IP sockets for CP/NET
* 25-DEC-06 Release 1.12 CPU speed option
* 19-FEB-07 Release 1.13 various improvements
* 06-OCT-07 Release 1.14 bug fixes and improvements
* 06-AUG-08 Release 1.15 many improvements and Windows support via Cygwin
* 25-AUG-08 Release 1.16 console status I/O loop detection and line discipline
* 20-OCT-08 Release 1.17 frontpanel integrated and Altair/IMSAI emulations
*/
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include "config.h"
#include "global.h"
#ifdef FRONTPANEL
#include "../../frontpanel/frontpanel.h"
#endif
#ifdef WANT_GUI
void check_gui_break(void);
#endif
#ifdef WANT_COUNTERS
extern void run_counters(void);
#endif
//static int op_notimpl(void);
static int op_nop(void), op_halt(void), op_scf(void);
static int op_ccf(void), op_cpl(void), op_daa(void);
static int op_ei(void), op_di(void);
static int op_in(void), op_out(void);
static int op_ldan(void), op_ldbn(void), op_ldcn(void);
static int op_lddn(void), op_lden(void);
static int op_ldhn(void), op_ldln(void);
static int op_ldabc(void), op_ldade(void), op_ldann(void);
static int op_ldbca(void), op_lddea(void), op_ldnna(void);
static int op_ldhla(void), op_ldhlb(void), op_ldhlc(void), op_ldhld(void);
static int op_ldhle(void), op_ldhlh(void), op_ldhll(void), op_ldhl1(void);
static int op_ldaa(void), op_ldab(void), op_ldac(void);
static int op_ldad(void), op_ldae(void);
static int op_ldah(void), op_ldal(void), op_ldahl(void);
static int op_ldba(void), op_ldbb(void), op_ldbc(void);
static int op_ldbd(void), op_ldbe(void);
static int op_ldbh(void), op_ldbl(void), op_ldbhl(void);
static int op_ldca(void), op_ldcb(void), op_ldcc(void);
static int op_ldcd(void), op_ldce(void);
static int op_ldch(void), op_ldcl(void), op_ldchl(void);
static int op_ldda(void), op_lddb(void), op_lddc(void);
static int op_lddd(void), op_ldde(void);
static int op_lddh(void), op_lddl(void), op_lddhl(void);
static int op_ldea(void), op_ldeb(void), op_ldec(void);
static int op_lded(void), op_ldee(void);
static int op_ldeh(void), op_ldel(void), op_ldehl(void);
static int op_ldha(void), op_ldhb(void), op_ldhc(void);
static int op_ldhd(void), op_ldhe(void);
static int op_ldhh(void), op_ldhl(void), op_ldhhl(void);
static int op_ldla(void), op_ldlb(void), op_ldlc(void);
static int op_ldld(void), op_ldle(void);
static int op_ldlh(void), op_ldll(void), op_ldlhl(void);
static int op_ldbcnn(void), op_lddenn(void), op_ldhlnn(void);
static int op_ldspnn(void), op_ldsphl(void);
static int op_ldhlin(void), op_ldinhl(void);
static int op_incbc(void), op_incde(void), op_inchl(void), op_incsp(void);
static int op_decbc(void), op_decde(void), op_dechl(void), op_decsp(void);
static int op_adhlbc(void), op_adhlde(void), op_adhlhl(void), op_adhlsp(void);
static int op_anda(void), op_andb(void), op_andc(void), op_andd(void), op_ande(void);
static int op_andh(void), op_andl(void), op_andhl(void), op_andn(void);
static int op_ora(void), op_orb(void), op_orc(void), op_ord(void), op_ore(void);
static int op_orh(void), op_orl(void), op_orhl(void), op_orn(void);
static int op_xora(void), op_xorb(void), op_xorc(void), op_xord(void), op_xore(void);
static int op_xorh(void), op_xorl(void), op_xorhl(void), op_xorn(void);
static int op_adda(void), op_addb(void), op_addc(void), op_addd(void), op_adde(void);
static int op_addh(void), op_addl(void), op_addhl(void), op_addn(void);
static int op_adca(void), op_adcb(void), op_adcc(void), op_adcd(void), op_adce(void);
static int op_adch(void), op_adcl(void), op_adchl(void), op_adcn(void);
static int op_suba(void), op_subb(void), op_subc(void), op_subd(void), op_sube(void);
static int op_subh(void), op_subl(void), op_subhl(void), op_subn(void);
static int op_sbca(void), op_sbcb(void), op_sbcc(void), op_sbcd(void), op_sbce(void);
static int op_sbch(void), op_sbcl(void), op_sbchl(void), op_sbcn(void);
static int op_cpa(void), op_cpb(void), op_cpc(void), op_cpd(void), op_cpe(void);
static int op_cph(void), op_cplr(void), op_cphl(void), op_cpn(void);
static int op_inca(void), op_incb(void), op_incc(void), op_incd(void), op_ince(void);
static int op_inch(void), op_incl(void), op_incihl(void);
static int op_deca(void), op_decb(void), op_decc(void), op_decd(void), op_dece(void);
static int op_dech(void), op_decl(void), op_decihl(void);
static int op_rlca(void), op_rrca(void),op_rla(void),op_rra(void);
static int op_exdehl(void), op_exafaf(void), op_exx(void), op_exsphl(void);
static int op_pushaf(void), op_pushbc(void), op_pushde(void), op_pushhl(void);
static int op_popaf(void), op_popbc(void), op_popde(void), op_pophl(void);
static int op_jp(void), op_jphl(void), op_jr(void), op_djnz(void), op_call(void), op_ret(void);
static int op_jpz(void), op_jpnz(void), op_jpc(void), op_jpnc(void);
static int op_jppe(void), op_jppo(void), op_jpm(void), op_jpp(void);
static int op_calz(void), op_calnz(void), op_calc(void), op_calnc(void);
static int op_calpe(void), op_calpo(void), op_calm(void), op_calp(void);
static int op_retz(void), op_retnz(void), op_retc(void), op_retnc(void);
static int op_retpe(void), op_retpo(void), op_retm(void), op_retp(void);
static int op_jrz(void), op_jrnz(void), op_jrc(void), op_jrnc(void);
static int op_rst00(void), op_rst08(void), op_rst10(void), op_rst18(void);
static int op_rst20(void), op_rst28(void), op_rst30(void), op_rst38(void);
extern int op_cb_handel(void), op_dd_handel(void);
extern int op_ed_handel(void), op_fd_handel(void);
/*
* This function builds the Z80 central processing unit.
* The opcode where PC points to is fetched from the memory
* and PC incremented by one. The opcode is used as an
* index to an array with function pointers, to execute a
* function which emulates this Z80 opcode.
*/
void cpu(void)
{
static int (*op_sim[256]) (void) = {
op_nop, /* 0x00 */
op_ldbcnn, /* 0x01 */
op_ldbca, /* 0x02 */
op_incbc, /* 0x03 */
op_incb, /* 0x04 */
op_decb, /* 0x05 */
op_ldbn, /* 0x06 */
op_rlca, /* 0x07 */
op_exafaf, /* 0x08 */
op_adhlbc, /* 0x09 */
op_ldabc, /* 0x0a */
op_decbc, /* 0x0b */
op_incc, /* 0x0c */
op_decc, /* 0x0d */
op_ldcn, /* 0x0e */
op_rrca, /* 0x0f */
op_djnz, /* 0x10 */
op_lddenn, /* 0x11 */
op_lddea, /* 0x12 */
op_incde, /* 0x13 */
op_incd, /* 0x14 */
op_decd, /* 0x15 */
op_lddn, /* 0x16 */
op_rla, /* 0x17 */
op_jr, /* 0x18 */
op_adhlde, /* 0x19 */
op_ldade, /* 0x1a */
op_decde, /* 0x1b */
op_ince, /* 0x1c */
op_dece, /* 0x1d */
op_lden, /* 0x1e */
op_rra, /* 0x1f */
op_jrnz, /* 0x20 */
op_ldhlnn, /* 0x21 */
op_ldinhl, /* 0x22 */
op_inchl, /* 0x23 */
op_inch, /* 0x24 */
op_dech, /* 0x25 */
op_ldhn, /* 0x26 */
op_daa, /* 0x27 */
op_jrz, /* 0x28 */
op_adhlhl, /* 0x29 */
op_ldhlin, /* 0x2a */
op_dechl, /* 0x2b */
op_incl, /* 0x2c */
op_decl, /* 0x2d */
op_ldln, /* 0x2e */
op_cpl, /* 0x2f */
op_jrnc, /* 0x30 */
op_ldspnn, /* 0x31 */
op_ldnna, /* 0x32 */
op_incsp, /* 0x33 */
op_incihl, /* 0x34 */
op_decihl, /* 0x35 */
op_ldhl1, /* 0x36 */
op_scf, /* 0x37 */
op_jrc, /* 0x38 */
op_adhlsp, /* 0x39 */
op_ldann, /* 0x3a */
op_decsp, /* 0x3b */
op_inca, /* 0x3c */
op_deca, /* 0x3d */
op_ldan, /* 0x3e */
op_ccf, /* 0x3f */
op_ldbb, /* 0x40 */
op_ldbc, /* 0x41 */
op_ldbd, /* 0x42 */
op_ldbe, /* 0x43 */
op_ldbh, /* 0x44 */
op_ldbl, /* 0x45 */
op_ldbhl, /* 0x46 */
op_ldba, /* 0x47 */
op_ldcb, /* 0x48 */
op_ldcc, /* 0x49 */
op_ldcd, /* 0x4a */
op_ldce, /* 0x4b */
op_ldch, /* 0x4c */
op_ldcl, /* 0x4d */
op_ldchl, /* 0x4e */
op_ldca, /* 0x4f */
op_lddb, /* 0x50 */
op_lddc, /* 0x51 */
op_lddd, /* 0x52 */
op_ldde, /* 0x53 */
op_lddh, /* 0x54 */
op_lddl, /* 0x55 */
op_lddhl, /* 0x56 */
op_ldda, /* 0x57 */
op_ldeb, /* 0x58 */
op_ldec, /* 0x59 */
op_lded, /* 0x5a */
op_ldee, /* 0x5b */
op_ldeh, /* 0x5c */
op_ldel, /* 0x5d */
op_ldehl, /* 0x5e */
op_ldea, /* 0x5f */
op_ldhb, /* 0x60 */
op_ldhc, /* 0x61 */
op_ldhd, /* 0x62 */
op_ldhe, /* 0x63 */
op_ldhh, /* 0x64 */
op_ldhl, /* 0x65 */
op_ldhhl, /* 0x66 */
op_ldha, /* 0x67 */
op_ldlb, /* 0x68 */
op_ldlc, /* 0x69 */
op_ldld, /* 0x6a */
op_ldle, /* 0x6b */
op_ldlh, /* 0x6c */
op_ldll, /* 0x6d */
op_ldlhl, /* 0x6e */
op_ldla, /* 0x6f */
op_ldhlb, /* 0x70 */
op_ldhlc, /* 0x71 */
op_ldhld, /* 0x72 */
op_ldhle, /* 0x73 */
op_ldhlh, /* 0x74 */
op_ldhll, /* 0x75 */
op_halt, /* 0x76 */
op_ldhla, /* 0x77 */
op_ldab, /* 0x78 */
op_ldac, /* 0x79 */
op_ldad, /* 0x7a */
op_ldae, /* 0x7b */
op_ldah, /* 0x7c */
op_ldal, /* 0x7d */
op_ldahl, /* 0x7e */
op_ldaa, /* 0x7f */
op_addb, /* 0x80 */
op_addc, /* 0x81 */
op_addd, /* 0x82 */
op_adde, /* 0x83 */
op_addh, /* 0x84 */
op_addl, /* 0x85 */
op_addhl, /* 0x86 */
op_adda, /* 0x87 */
op_adcb, /* 0x88 */
op_adcc, /* 0x89 */
op_adcd, /* 0x8a */
op_adce, /* 0x8b */
op_adch, /* 0x8c */
op_adcl, /* 0x8d */
op_adchl, /* 0x8e */
op_adca, /* 0x8f */
op_subb, /* 0x90 */
op_subc, /* 0x91 */
op_subd, /* 0x92 */
op_sube, /* 0x93 */
op_subh, /* 0x94 */
op_subl, /* 0x95 */
op_subhl, /* 0x96 */
op_suba, /* 0x97 */
op_sbcb, /* 0x98 */
op_sbcc, /* 0x99 */
op_sbcd, /* 0x9a */
op_sbce, /* 0x9b */
op_sbch, /* 0x9c */
op_sbcl, /* 0x9d */
op_sbchl, /* 0x9e */
op_sbca, /* 0x9f */
op_andb, /* 0xa0 */
op_andc, /* 0xa1 */
op_andd, /* 0xa2 */
op_ande, /* 0xa3 */
op_andh, /* 0xa4 */
op_andl, /* 0xa5 */
op_andhl, /* 0xa6 */
op_anda, /* 0xa7 */
op_xorb, /* 0xa8 */
op_xorc, /* 0xa9 */
op_xord, /* 0xaa */
op_xore, /* 0xab */
op_xorh, /* 0xac */
op_xorl, /* 0xad */
op_xorhl, /* 0xae */
op_xora, /* 0xaf */
op_orb, /* 0xb0 */
op_orc, /* 0xb1 */
op_ord, /* 0xb2 */
op_ore, /* 0xb3 */
op_orh, /* 0xb4 */
op_orl, /* 0xb5 */
op_orhl, /* 0xb6 */
op_ora, /* 0xb7 */
op_cpb, /* 0xb8 */
op_cpc, /* 0xb9 */
op_cpd, /* 0xba */
op_cpe, /* 0xbb */
op_cph, /* 0xbc */
op_cplr, /* 0xbd */
op_cphl, /* 0xbe */
op_cpa, /* 0xbf */
op_retnz, /* 0xc0 */
op_popbc, /* 0xc1 */
op_jpnz, /* 0xc2 */
op_jp, /* 0xc3 */
op_calnz, /* 0xc4 */
op_pushbc, /* 0xc5 */
op_addn, /* 0xc6 */
op_rst00, /* 0xc7 */
op_retz, /* 0xc8 */
op_ret, /* 0xc9 */
op_jpz, /* 0xca */
op_cb_handel, /* 0xcb */
op_calz, /* 0xcc */
op_call, /* 0xcd */
op_adcn, /* 0xce */
op_rst08, /* 0xcf */
op_retnc, /* 0xd0 */
op_popde, /* 0xd1 */
op_jpnc, /* 0xd2 */
op_out, /* 0xd3 */
op_calnc, /* 0xd4 */
op_pushde, /* 0xd5 */
op_subn, /* 0xd6 */
op_rst10, /* 0xd7 */
op_retc, /* 0xd8 */
op_exx, /* 0xd9 */
op_jpc, /* 0xda */
op_in, /* 0xdb */
op_calc, /* 0xdc */
op_dd_handel, /* 0xdd */
op_sbcn, /* 0xde */
op_rst18, /* 0xdf */
op_retpo, /* 0xe0 */
op_pophl, /* 0xe1 */
op_jppo, /* 0xe2 */
op_exsphl, /* 0xe3 */
op_calpo, /* 0xe4 */
op_pushhl, /* 0xe5 */
op_andn, /* 0xe6 */
op_rst20, /* 0xe7 */
op_retpe, /* 0xe8 */
op_jphl, /* 0xe9 */
op_jppe, /* 0xea */
op_exdehl, /* 0xeb */
op_calpe, /* 0xec */
op_ed_handel, /* 0xed */
op_xorn, /* 0xee */
op_rst28, /* 0xef */
op_retp, /* 0xf0 */
op_popaf, /* 0xf1 */
op_jpp, /* 0xf2 */
op_di, /* 0xf3 */
op_calp, /* 0xf4 */
op_pushaf, /* 0xf5 */
op_orn, /* 0xf6 */
op_rst30, /* 0xf7 */
op_retm, /* 0xf8 */
op_ldsphl, /* 0xf9 */
op_jpm, /* 0xfa */
op_ei, /* 0xfb */
op_calm, /* 0xfc */
op_fd_handel, /* 0xfd */
op_cpn, /* 0xfe */
op_rst38 /* 0xff */
};
#ifdef WANT_TIM
register int t = 0;
struct timespec timer;
#ifdef FRONTPANEL
register int states;
#endif
#endif
do {
#ifdef FRONTPANEL /* update frontpanel */
fp_led_address = PC - ram;
fp_led_data = *PC;
fp_sampleData();
#endif
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_M1 | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_clock += 7;
fp_sampleLightGroup(0, 0);
#endif
#ifdef HISIZE /* write history */
his[h_next].h_adr = PC - ram;
his[h_next].h_af = (A << 8) + F;
his[h_next].h_bc = (B << 8) + C;
his[h_next].h_de = (D << 8) + E;
his[h_next].h_hl = (H << 8) + L;
his[h_next].h_ix = IX;
his[h_next].h_iy = IY;
his[h_next].h_sp = STACK - ram;
h_next++;
if (h_next == HISIZE) {
h_flag = 1;
h_next = 0;
}
#endif
#ifdef WANT_TIM /* check for start address of runtime measurement */
if (PC == t_start && !t_flag) {
t_flag = 1; /* switch measurement on */
t_states = 0L; /* initialize counted T-states */
}
#endif
#ifdef WANT_COUNTERS
run_counters();
#endif
#ifdef WANT_INT /* CPU interrupt handling */
if (int_type) // if there is an interrupt available to handle
// TODO: this could be reworked to be cleaner
// source info: http://www.z80.info/interrup.htm
switch (int_type) {
case INT_NMI: // NMIs
int_type = INT_NONE; // ack the interrupt
IFF &= 0x02; // clear IFF1
// this block stores the current execution address on the stack
#ifdef WANT_SPC
if (STACK <= ram)
STACK = ram + 65536L;
#endif
*--STACK = (PC - ram) >> 8; // put MSB of PC on stack and decrement SP
#ifdef WANT_SPC
if (STACK <= ram)
STACK = ram + 65536L;
#endif
*--STACK = (PC - ram); // LSB of PC
// set the new PC
PC = ram + 0x66; // static entry point of NMIs
if (INT_DEBUG) printf("--- NMI: Jumping to 0x0066\n");
break;
case INT_INT: // maskable ints
if (!(IFF&0x01)) break; // ints only accepted if IFF1 is set
IFF=0x00; // if the interrupt is accepted, IFF1 and 2 are cleared
switch (int_mode) {
case 0: // TODO
printf("Interrupt mode 0 unimplemented! Doing nothing.\n");
int_type = INT_NONE; // ack the interrupt anyway
break;
case 1: // mode 1 works: just jp to 0x0038;
int_type = INT_NONE;
#ifdef WANT_SPC
if (STACK <= ram)
STACK = ram + 65536L;
#endif
*--STACK = (PC - ram) >> 8;
#ifdef WANT_SPC
if (STACK <= ram)
STACK = ram + 65536L;
#endif
*--STACK = (PC - ram);
PC = ram + 0x38;
if (INT_DEBUG) printf("--- Mode 1 Int: Jumping to 0x0038\n");
break;
case 2:
int_vect=(I<<8)+int_lsb; // address of the vector table entry
// push current PC onto stack
*--STACK = (PC - ram) >> 8;
*--STACK = (PC - ram);
// set the new PC from the vector lookup table
PC = ram + *(ram + int_vect); // LSB of the entry address
PC += (*(ram + int_vect + 1)) << 8; // MSB
if (INT_DEBUG) // TODO: move this mode into a cli-configurable var
printf("--- Mode 2 Int: Lookup from 0x%04x, points to 0x%04lx.\n",int_vect,PC-ram);
int_type = INT_NONE; // ack the interrupt
break;
}
break;
}
#endif
#ifdef WANT_TIM
#ifdef FRONTPANEL
states = (*op_sim[*PC++]) (); /* execute next opcode */
t += states;
fp_clock += states;
#else
t += (*op_sim[*PC++]) ();
#endif
if (f_flag) { /* adjust CPU speed */
if (t > tmax) {
timer.tv_sec = 0;
timer.tv_nsec = 10000000;
nanosleep(&timer, NULL);
t = 0;
}
}
#else
(*op_sim[*PC++]) ();
#endif
#ifdef WANT_PCC
if (PC > ram + 65535) /* check for PC overrun */
PC = ram;
#endif
R++; /* increment refresh register */
#ifdef WANT_TIM /* do runtime measurement */
if (t_flag) {
t_states += t; /* add T-states for this opcode */
if (PC == t_end) /* check for end address */
t_flag = 0; /* if reached, switch off */
}
#endif
#ifdef WANT_GUI
check_gui_break();
#endif
} while (cpu_state == CONTIN_RUN);
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_M1 | CPU_MEMR;
#endif
}
/*
* Trap not implemented opcodes. This function may be usefull
* later to trap some wanted opcodes.
*/
/*static int op_notimpl(void) // currently unreferenced and generates compiler warnings
{
cpu_error = OPTRAP1;
cpu_state = STOPPED;
return(0);
}*/
static int op_nop(void) /* NOP */
{
return(4);
}
static int op_halt(void) /* HALT */
{
extern int busy_loop_cnt[];
struct timespec timer;
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_HLTA | CPU_MEMR;
#endif
#ifndef FRONTPANEL
if (IFF == 0) {
cpu_error = OPHALT;
cpu_state = STOPPED;
} else
#endif
while ((int_type == 0) && (cpu_state == CONTIN_RUN)) {
#ifdef FRONTPANEL
fp_clock += 4;
fp_sampleData();
#endif
timer.tv_sec = 0;
timer.tv_nsec = 1000000L;
nanosleep(&timer, NULL);
R += 9999;
}
busy_loop_cnt[0] = 0;
return(0);
}
static int op_scf(void) /* SCF */
{
F |= C_FLAG;
F &= ~(N_FLAG | H_FLAG);
return(4);
}
static int op_ccf(void) /* CCF */
{
if (F & C_FLAG) {
F |= H_FLAG;
F &= ~C_FLAG;
} else {
F &= ~H_FLAG;
F |= C_FLAG;
}
F &= ~N_FLAG;
return(4);
}
static int op_cpl(void) /* CPL */
{
A = ~A;
F |= H_FLAG | N_FLAG;
return(4);
}
static int op_daa(void) /* DAA */
{
register int old_a;
old_a = A;
if (F & N_FLAG) { /* subtractions */
if (((A & 0x0f) > 9) || (F & H_FLAG)) {
(((old_a & 0x0f) - 6) < 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
A = old_a -= 6;
}
if (((A & 0xf0) > 0x90) || (F & C_FLAG)) {
A -= 0x60;
if (old_a - 0x60 < 0)
F |= C_FLAG;
}
} else { /* additions */
if (((A & 0x0f) > 9) || (F & H_FLAG)) {
(((old_a & 0x0f) + 6) > 0x0f) ? (F |= H_FLAG) : (F &= ~H_FLAG);
A = old_a += 6;
}
if (((A & 0xf0) > 0x90) || (F & C_FLAG)) {
A += 0x60;
if (old_a + 0x60 > 255)
F |= C_FLAG;
}
}
(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
(parrity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
return(4);
}
static int op_ei(void) /* EI */
{
IFF = 3;
return(4);
}
static int op_di(void) /* DI */
{
IFF = 0;
return(4);
}
static int op_in(void) /* IN A,(n) */
{
BYTE io_in();
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_INP;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
A = io_in(*PC++);
return(11);
}
static int op_out(void) /* OUT (n),A */
{
BYTE io_out();
#ifdef BUS_8080
cpu_bus = CPU_OUT;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
io_out(*PC++, A);
return(11);
}
static int op_ldan(void) /* LD A,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
A = *PC++;
return(7);
}
static int op_ldbn(void) /* LD B,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
B = *PC++;
return(7);
}
static int op_ldcn(void) /* LD C,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
C = *PC++;
return(7);
}
static int op_lddn(void) /* LD D,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
D = *PC++;
return(7);
}
static int op_lden(void) /* LD E,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
E = *PC++;
return(7);
}
static int op_ldhn(void) /* LD H,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
H = *PC++;
return(7);
}
static int op_ldln(void) /* LD L,n */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
L = *PC++;
return(7);
}
static int op_ldabc(void) /* LD A,(BC) */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
A = *(ram + (B << 8) + C);
return(7);
}
static int op_ldade(void) /* LD A,(DE) */
{
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
A = *(ram + (D << 8) + E);
return(7);
}
static int op_ldann(void) /* LD A,(nn) */
{
register unsigned i;
#ifdef BUS_8080
cpu_bus = CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
i = *PC++;
i += *PC++ << 8;
A = *(ram + i);
return(13);
}
static int op_ldbca(void) /* LD (BC),A */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (B << 8) + C) = A;
return(7);
}
static int op_lddea(void) /* LD (DE),A */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (D << 8) + E) = A;
return(7);
}
static int op_ldnna(void) /* LD (nn),A */
{
register unsigned i;
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
i = *PC++;
i += *PC++ << 8;
*(ram + i) = A;
return(13);
}
static int op_ldhla(void) /* LD (HL),A */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = A;
return(7);
}
static int op_ldhlb(void) /* LD (HL),B */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = B;
return(7);
}
static int op_ldhlc(void) /* LD (HL),C */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = C;
return(7);
}
static int op_ldhld(void) /* LD (HL),D */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = D;
return(7);
}
static int op_ldhle(void) /* LD (HL),E */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = E;
return(7);
}
static int op_ldhlh(void) /* LD (HL),H */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = H;
return(7);
}
static int op_ldhll(void) /* LD (HL),L */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = L;
return(7);
}
static int op_ldhl1(void) /* LD (HL),n */
{
#ifdef BUS_8080
cpu_bus = 0;
#endif
#ifdef FRONTPANEL
fp_sampleLightGroup(0, 0);
#endif
*(ram + (H << 8) + L) = *PC++;
return(10);
}
static int op_ldaa(void) /* LD A,A */
{
return(4);
}
static int op_ldab(void) /* LD A,B */
{
A = B;
return(4);
}
static int op_ldac(void) /* LD A,C */
{
A = C;
return(4);
}
static int op_ldad(void) /* LD A,D */
{
A = D;
return(4);
}
static int op_ldae(void) /* LD A,E */
{
A = E;
return(4);
}
static int op_ldah(void) /* LD A,H */
{