-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathz80.c
5450 lines (3973 loc) · 90.9 KB
/
z80.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
/*
* GZX - George's ZX Spectrum Emulator
* Z80 CPU emulation
*
* Copyright (c) 1999-2017 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Flag computations for most instructions should work
except block I/O instructions and undocumented 5,3 flags
for BIT (HL)
some games known not to work:
belegost (dies)
tapper (dies)
!spemu not implemented right in spectemu
to do:
sooner
- flags for I/O block instructions
later
- BIT (HL) - vlajky 3,5
- timing of undocumented instructions
*/
#include <stdint.h>
#include <stdlib.h>
#include "z80.h"
#include "z80dep.h"
static int z80_readinstr(void);
static void z80_check_nmi(void);
static void z80_check_int(void);
static uint8_t opcode;
z80s cpus;
/** CPU state used to read memory addresses from. Used by Spec256. */
z80s *rcpus = &cpus;
static uint8_t cbop;
unsigned long z80_clock;
/* debugging & statistics */
unsigned long uoc; /* unsupported opcode counter */
unsigned long smc; /* stray modifier counter */
static uint8_t prefix1,prefix2;
static void (*const *ei_tab)(void);
#ifndef NO_Z80STAT
static unsigned stat_tab[7][256];
static int stat_i;
#endif
/* fast flag computation lookup table */
static uint8_t ox_tab[256]; /* OR,XOR and more */
static uint16_t z80_memget16(uint16_t addr) {
return (uint16_t)z80_memget8(addr)+(((uint16_t)z80_memget8(addr+1))<<8);
}
static uint16_t z80_imemget16(uint16_t addr) {
return (uint16_t)z80_imemget8(addr)+(((uint16_t)z80_imemget8(addr+1))<<8);
}
static void z80_memset16(uint16_t addr, uint16_t val) {
z80_memset8(addr, val & 0xff);
z80_memset8(addr+1, val >> 8);
}
static inline void z80_clock_inc(uint8_t inc)
{
#ifndef NO_Z80CLOCK
z80_clock += inc;
#endif
}
/* returns the signed value of the byte: 0..127 ->0..127
128..255 ->-128..-1 */
static int u8sval(uint8_t u) {
if(u<0x80) return u;
else return (int)(u&0x7f)-128;
}
static int u16sval(uint16_t u) {
if(u<0x8000) return u;
else return (int)(u&0x7fff)-32768;
}
/********************** flags calculation ****************************/
/* these functions return 1 when sign overflow occurs */
/* (the result would be >127 or <-128) */
static int add_v8(uint8_t a, uint8_t b) {
int sign_r;
sign_r=u8sval(a)+u8sval(b);
return sign_r<-128 || sign_r>127;
}
static int sub_v8(uint8_t a, uint8_t b) {
int sign_r;
sign_r=u8sval(a)-u8sval(b);
return sign_r<-128 || sign_r>127;
}
static int adc_v8(uint8_t a, uint8_t b, uint8_t c) {
int sign_r;
sign_r=u8sval(a)+u8sval(b)+u8sval(c);
return sign_r<-128 || sign_r>127;
}
static int sbc_v8(uint8_t a, uint8_t b, uint8_t c) {
int sign_r;
sign_r=u8sval(a)-u8sval(b)-c;
return sign_r<-128 || sign_r>127;
}
static int adc_v16(uint16_t a, uint16_t b, uint16_t c) {
int sign_r;
sign_r=u16sval(a)+u16sval(b)+u16sval(c);
return sign_r<-32768 || sign_r>32767;
}
static int sbc_v16(uint16_t a, uint16_t b, uint16_t c) {
int sign_r;
sign_r=u16sval(a)-u16sval(b)-c;
return sign_r<-32768 || sign_r>32767;
}
/* calculates an even parity for the given 8-bit number */
static int oddp8(uint8_t x) {
x^=x>>4;
x^=x>>2;
x^=x>>1;
x^=1;
return x&1;
}
static void setflags(int s, int z, int hc, int pv, int n, int c) {
if(s>=0) cpus.F = (cpus.F & (fS^0xff)) | (s?fS:0);
if(z>=0) cpus.F = (cpus.F & (fZ^0xff)) | (z?fZ:0);
if(hc>=0) cpus.F = (cpus.F & (fHC^0xff)) | (hc?fHC:0);
if(pv>=0) cpus.F = (cpus.F & (fPV^0xff)) | (pv?fPV:0);
if(n>=0) cpus.F = (cpus.F & (fN^0xff)) | (n?fN:0);
if(c>=0) cpus.F = (cpus.F & (fC^0xff)) | (c?fC:0);
}
#ifndef NO_Z80UNDOC
static void setundocflags8(uint8_t res) {
cpus.F &= fD; /* leave only documented flags */
cpus.F |= (res & fU); /* set undocumented flags */
}
#else
#define setundocflags8(res) ((void)(res))
static void ei_undoc(void)
{
z80_clock_inc(4);
}
#endif
static void incr_R(uint8_t amount) {
cpus.R = (cpus.R & 0x80) | ((cpus.R+amount)&0x7f);
}
/**************************** address register access *******************/
/*
* In GPU case we need to take addresses from the CPU registers as a special
* case. If GPU is not enabled, rcpus just points to the CPU state.
*/
static uint16_t get_addrBC(void)
{
return ((uint16_t)rcpus->r[rB] << 8) | rcpus->r[rC];
}
static uint16_t get_addrDE(void)
{
return ((uint16_t)rcpus->r[rD] << 8) | rcpus->r[rE];
}
static uint16_t get_addrHL(void)
{
return ((uint16_t)rcpus->r[rH] << 8) | rcpus->r[rL];
}
static uint16_t get_addrIX(void)
{
return rcpus->IX;
}
static uint16_t get_addrIY(void)
{
return rcpus->IY;
}
/**************************** operand access ***************************/
/* returns (HL)(8) */
static uint8_t _iHL8(void) {
return z80_memget8(get_addrHL());
}
/* returns (BC) */
static uint8_t _iBC8(void) {
return z80_memget8(get_addrBC());
}
/* returns (DE) */
static uint8_t _iDE8(void) {
return z80_memget8(get_addrDE());
}
/* returns (IX+N) */
static uint8_t _iIXN8(uint16_t N) {
return z80_memget8(get_addrIX()+u8sval(N));
}
/* returns (IY+N) */
static uint8_t _iIYN8(uint16_t N) {
return z80_memget8(get_addrIY()+u8sval(N));
}
/* (IX+N) <- val*/
static void s_iIXN8(uint16_t N, uint8_t val) {
z80_memset8(get_addrIX()+u8sval(N),val);
}
/* (IY+N) <- val*/
static void s_iIYN8(uint16_t N, uint8_t val) {
z80_memset8(get_addrIY()+u8sval(N),val);
}
/* (HL) <- val */
static void s_iHL8(uint8_t val) {
z80_memset8(get_addrHL(),val);
}
/* (BC) <- val */
static void s_iBC8(uint8_t val) {
z80_memset8(get_addrBC(),val);
}
/* (DE) <- val */
static void s_iDE8(uint8_t val) {
z80_memset8(get_addrDE(),val);
}
/* returns (SP)(16-bits) */
static uint16_t _iSP16(void) {
return z80_memget16(cpus.SP);
}
/* (SP)(16-bits) <- val */
static void s_iSP16(uint16_t val) {
z80_memset16(cpus.SP,val);
}
static uint16_t getAF(void) {
return ((uint16_t)cpus.r[rA] << 8)|(uint16_t)cpus.F;
}
static uint16_t getBC(void) {
return ((uint16_t)cpus.r[rB] << 8)|(uint16_t)cpus.r[rC];
}
static uint16_t getDE(void) {
return ((uint16_t)cpus.r[rD] << 8)|(uint16_t)cpus.r[rE];
}
static uint16_t getHL(void) {
return ((uint16_t)cpus.r[rH] << 8)|(uint16_t)cpus.r[rL];
}
static uint16_t getAF_(void) {
return ((uint16_t)cpus.r_[rA] << 8)|(uint16_t)cpus.F_;
}
static uint16_t getBC_(void) {
return ((uint16_t)cpus.r_[rB] << 8)|(uint16_t)cpus.r_[rC];
}
static uint16_t getDE_(void) {
return ((uint16_t)cpus.r_[rD] << 8)|(uint16_t)cpus.r_[rE];
}
static uint16_t getHL_(void) {
return ((uint16_t)cpus.r_[rH] << 8)|(uint16_t)cpus.r_[rL];
}
static void setAF(uint16_t val) {
cpus.r[rA]=val>>8;
cpus.F=val & 0xff;
}
static void setBC(uint16_t val) {
cpus.r[rB]=val>>8;
cpus.r[rC]=val & 0xff;
}
static void setDE(uint16_t val) {
cpus.r[rD]=val>>8;
cpus.r[rE]=val & 0xff;
}
static void setHL(uint16_t val) {
cpus.r[rH]=val>>8;
cpus.r[rL]=val & 0xff;
}
uint16_t z80_getAF(void)
{
return getAF();
}
uint16_t z80_getBC(void)
{
return getBC();
}
uint16_t z80_getDE(void)
{
return getDE();
}
uint16_t z80_getHL(void)
{
return getHL();
}
uint16_t z80_getAF_(void)
{
return getAF_();
}
uint16_t z80_getBC_(void)
{
return getBC_();
}
uint16_t z80_getDE_(void)
{
return getDE_();
}
uint16_t z80_getHL_(void)
{
return getHL_();
}
static uint8_t z80_iget8(void) {
uint8_t tmp;
tmp=z80_imemget8(cpus.PC);
cpus.PC++;
return tmp;
}
static uint16_t z80_iget16(void) {
uint16_t tmp;
tmp=z80_imemget16(cpus.PC);
cpus.PC+=2;
return tmp;
}
/******************* undocumented operand access ************************/
#ifndef NO_Z80UNDOC
static void setIXh(uint8_t val) {
cpus.IX = (cpus.IX & 0x00ff) | ((uint16_t)val<<8);
}
static void setIYh(uint8_t val) {
cpus.IY = (cpus.IY & 0x00ff) | ((uint16_t)val<<8);
}
static void setIXl(uint8_t val) {
cpus.IX = (cpus.IX & 0xff00) | (uint16_t)val;
}
static void setIYl(uint8_t val) {
cpus.IY = (cpus.IY & 0xff00) | (uint16_t)val;
}
static uint8_t getIXh(void) {
return cpus.IX>>8;
}
static uint8_t getIYh(void) {
return cpus.IY>>8;
}
static uint8_t getIXl(void) {
return cpus.IX&0xff;
}
static uint8_t getIYl(void) {
return cpus.IY&0xff;
}
#endif
/************************************************************************/
static void _push16(uint16_t val);
/************************************************************************/
void z80_init_tables(void) {
unsigned u;
for(u=0;u<256;u++) {
ox_tab[u] = u & (fS | fU1 | fU2);
if(oddp8(u)) ox_tab[u] |= fPV;
}
ox_tab[0] |= fZ;
}
/************************ operations ************************************/
static uint8_t _adc8(uint16_t a, uint16_t b) {
uint16_t res;
uint16_t c;
c=(cpus.F&fC)?1:0;
res=a+b+c;
setflags((res>>7)&1,
(res&0xff)==0,
(a&0x0f)+(b&0x0f)+c > 0x0f,
adc_v8(a,b,c),
0,
res>0xff);
setundocflags8(res);
return res & 0xff;
}
static uint16_t _adc16(uint16_t a, uint16_t b) {
uint16_t res0,res1,a1,b1,c,c1;
c=((cpus.F&fC)?1:0);
res0=(a&0xff)+(b&0xff)+ c;
a1=a>>8; b1=b>>8; c1=((res0>0xff)?1:0);
res1=a1+b1+c1;
setflags(res1&0x80,
!((res0&0xff)|(res1&0xff)),
(a1&0x0f) + (b1&0x0f)>0x0f,
adc_v16(a,b,c),
0,
res1>0xff);
setundocflags8(res1);
return (res0&0xff)|((res1&0xff)<<8);
}
/************************************************************************/
static uint8_t _add8(uint16_t a, uint16_t b) {
uint16_t res;
res=a+b;
setflags((res>>7)&1,
(res&0xff)==0,
(a&0x0f)+(b&0x0f) > 0x0f,
add_v8(a,b),
0,
res>0xff);
setundocflags8(res);
return res & 0xff;
}
static uint16_t _add16(uint16_t a, uint16_t b) {
uint16_t res0,res1,a1,b1;
res0=(a&0xff)+(b&0xff);
a1=a>>8; b1=b>>8;
res1=a1+b1+((res0>0xff)?1:0);
setflags(-1,
-1,
(a1&0x0f) + (b1&0x0f)>0x0f,
-1,
0,
res1>0xff);
setundocflags8(res1);
return (res0&0xff)|((res1&0xff)<<8);
}
/************************************************************************/
static uint8_t _and8(uint8_t a, uint8_t b) {
uint8_t res;
res=a&b;
cpus.F=ox_tab[res]|fHC;
return res;
}
/************************************************************************/
static uint8_t _bit8(uint8_t a, uint8_t b) {
uint8_t res;
res=b & (1<<a);
cpus.F=(cpus.F&fC)|ox_tab[res]; /* CF does not change */
/* setflags(res&0x80,
res==0,
1,
res==0,
0,
-1);*/
/* undoc flags are different for different opcodes. see BIT opcodes. */
return res;
}
/************************************************************************/
static void _call16(uint16_t addr) {
_push16(cpus.PC);
cpus.PC=addr;
}
/************************************************************************/
static uint8_t _cp8(uint16_t a, uint16_t b) {
uint16_t res;
res=a-b;
setflags((res>>7)&1,
(res&0xff)==0,
(a&0x0f)-(b&0x0f) < 0, /* I hope */
sub_v8(a,b),
1,
res>0xff);
setundocflags8(b); /* not from the result! */
return res & 0xff;
}
/************************************************************************/
static uint8_t _dec8(uint16_t a) {
uint16_t res;
res=(a-1)&0xff;
setflags(res>>7,
res==0,
(a&0x0f)-1<0, /* I hope */
sub_v8(a,1),
1, /* !spemu */
-1);
setundocflags8(res);
return res;
}
/************************************************************************/
static uint8_t _in8pf(uint16_t a) {
return z80_in8(a); /* query ZX */
}
static uint8_t _in8(uint16_t a) {
uint16_t res;
res=_in8pf(a)&0xff;
setflags(res>>7,
res==0,
0,
oddp8(res),
0,
-1);
return res;
}
/************************************************************************/
static uint8_t _inc8(uint16_t a) {
uint16_t res;
res=(a+1)&0xff;
setflags(res>>7,
res==0,
(a&0x0f)+1>0x0f,
add_v8(a,1),
0, /* !spemu */
-1);
setundocflags8(res);
return res;
}
/************************************************************************/
static void _jp16(uint16_t addr) {
cpus.PC=addr;
}
/************************************************************************/
static void _jr8(uint8_t ofs) {
cpus.PC+=u8sval(ofs);
}
/************************************************************************/
static uint8_t _or8(uint8_t a, uint8_t b) {
uint8_t res;
res=a|b;
cpus.F=ox_tab[res];
return res;
}
/************************************************************************/
static void _out8(uint16_t addr, uint8_t val) {
z80_out8(addr,val); /* pass it to ZX */
}
/************************************************************************/
static void _push16(uint16_t val) {
cpus.SP-=2;
z80_memset16(cpus.SP,val);
}
static uint16_t _pop16(void) {
uint16_t res;
res=z80_memget16(cpus.SP);
cpus.SP+=2;
return res;
}
/************************************************************************/
static uint8_t _res8(uint16_t a, uint16_t b) {
uint16_t res;
res=b & ((1<<a)^0xff);
return res;
}
/************************************************************************/
static uint8_t _rla8(uint8_t a) {
uint8_t nC,oC;
nC=a>>7;
oC=(cpus.F & fC)?1:0;
a=(a<<1)|oC;
cpus.F = (cpus.F & ~(fU1|fHC|fU2|fN|fC)) | (a&(fU1|fU2)) | nC;
return a;
}
static uint8_t _rl8(uint8_t a) {
uint8_t nC,oC;
nC=a>>7;
oC=(cpus.F & fC)?1:0;
a=(a<<1)|oC;
cpus.F=ox_tab[a]|nC;
return a;
}
static uint8_t _rlca8(uint8_t a) {
uint8_t tmp;
tmp=a>>7;
a=(a<<1)|tmp;
cpus.F = (cpus.F & ~(fU1|fHC|fU2|fN|fC)) | (a&(fU1|fU2)) | tmp;
return a;
}
static uint8_t _rlc8(uint8_t a) {
uint8_t tmp;
tmp=a>>7;
a=(a<<1)|tmp;
cpus.F=ox_tab[a]|tmp;
return a;
}
static uint8_t _rra8(uint8_t a) {
uint8_t nC,oC;
nC=a&1;
oC=(cpus.F & fC)?1:0;
a=(a>>1)|(oC<<7);
cpus.F = (cpus.F & ~(fU1|fHC|fU2|fN|fC)) | (a&(fU1|fU2)) | nC;
return a;
}
static uint8_t _rr8(uint8_t a) {
uint8_t nC,oC;
nC=a&1;
oC=(cpus.F & fC)?1:0;
a=(a>>1)|(oC<<7);
cpus.F=ox_tab[a]|nC;
return a;
}
static uint8_t _rrca8(uint8_t a) {
uint8_t tmp;
tmp=a&1;
a=(a>>1)|(tmp<<7);
cpus.F = (cpus.F & ~(fU1|fHC|fU2|fN|fC)) | (a&(fU1|fU2)) | tmp;
return a;
}
static uint8_t _rrc8(uint8_t a) {
uint8_t tmp;
tmp=a&1;
a=(a>>1)|(tmp<<7);
cpus.F=ox_tab[a]|tmp;
return a;
}
/************************************************************************/
static uint8_t _sla8(uint8_t a) {
uint8_t nC;
nC=a>>7;
a<<=1;
cpus.F=ox_tab[a]|nC;
return a;
}
static uint8_t _sra8(uint8_t a) {
uint8_t nC;
nC=a&1;
a=(a&0x80) | (a>>1);
cpus.F=ox_tab[a]|nC;
return a;
}
#ifndef NO_Z80UNDOC
/* Shift left with 1 insertion (sl1), also called sll */
static uint8_t _sll8(uint8_t a) {
uint8_t nC;
nC=a>>7;
a=(a<<1)|0x1;
cpus.F=ox_tab[a]|nC;
return a;
}
#endif
static uint8_t _srl8(uint16_t a) {
uint16_t nC;
nC=a&1;
a>>=1;
cpus.F=ox_tab[a]|nC;
return a;
}
/************************************************************************/
static uint8_t _sbc8(uint16_t a, uint16_t b) {
uint16_t res;
uint16_t c;
c=((cpus.F&fC)!=0);
res=a-b-c;
setflags((res>>7)&1,
(res&0xff)==0,
(a&0x0f)-(b&0x0f)-c < 0, /* I hope */
sbc_v8(a,b,c),
1,
res>0xff);
setundocflags8(res);
return res & 0xff;
}
static uint16_t _sbc16(uint16_t a, uint16_t b) {
uint16_t res0,res1,a1,b1,c,c1;
c=((cpus.F&fC)?1:0);
res0=(a&0xff)-(b&0xff)- c;
a1=a>>8; b1=b>>8; c1=((res0>0xff)?1:0);
res1=a1-b1-c1;
setflags(res1&0x80,
!((res0&0xff)|(res1&0xff)),
(a1&0x0f) - (b1&0x0f)<0,
sbc_v16(a,b,c),
0,
res1>0xff);
setundocflags8(res1);
return (res0&0xff)|((res1&0xff)<<8);
}
/************************************************************************/
static uint8_t _set8(uint16_t a, uint16_t b) {
uint16_t res;
res=b | (1<<a);
return res;
}
/************************************************************************/
static uint8_t _sub8(uint16_t a, uint16_t b) {
uint16_t res;
res=a-b;
setflags((res>>7)&1,
(res&0xff)==0,
(a&0x0f)-(b&0x0f) < 0, /* I hope */
sub_v8(a,b),
1,
res>0xff);
setundocflags8(res);
return res & 0xff;
}
/************************************************************************/
static uint8_t _xor8(uint8_t a, uint8_t b) {
uint8_t res;
res=a^b;
cpus.F=ox_tab[res];
return res;
}
/************************************************************************/
/************************************************************************/
/********************* documented opcodes *******************************/
static void ei_adc_A_r(void) {
uint8_t res;
res=_adc8(cpus.r[rA],cpus.r[opcode & 0x07]);
cpus.r[rA]=res;
z80_clock_inc(4);
}
static void ei_adc_A_N(void) {
uint8_t res,op;
op=z80_iget8();
res=_adc8(cpus.r[rA],op);
cpus.r[rA]=res;
z80_clock_inc(7);
}
static void ei_adc_A_iHL(void) {
uint8_t res;
res=_adc8(cpus.r[rA],_iHL8());
cpus.r[rA]=res;
z80_clock_inc(7);
}
static void ei_adc_A_iIXN(void) {
uint8_t res,op;
op=z80_iget8();
res=_adc8(cpus.r[rA],_iIXN8(op));
cpus.r[rA]=res;
z80_clock_inc(15);
}
static void ei_adc_A_iIYN(void) {
uint8_t res,op;
op=z80_iget8();
res=_adc8(cpus.r[rA],_iIYN8(op));
cpus.r[rA]=res;
z80_clock_inc(15);
}
static void ei_adc_HL_BC(void) {
uint16_t res;
res=_adc16(getHL(),getBC());
setHL(res);
z80_clock_inc(15);
}
static void ei_adc_HL_DE(void) {
uint16_t res;
res=_adc16(getHL(),getDE());
setHL(res);
z80_clock_inc(15);
}
static void ei_adc_HL_HL(void) {
uint16_t res;
res=_adc16(getHL(),getHL());
setHL(res);
z80_clock_inc(15);
}
static void ei_adc_HL_SP(void) {
uint16_t res;
res=_adc16(getHL(),cpus.SP);
setHL(res);
z80_clock_inc(15);
}
/************************************************************************/
static void ei_add_A_r(void) {
uint8_t res;
res=_add8(cpus.r[rA],cpus.r[opcode & 0x07]);
cpus.r[rA]=res;
z80_clock_inc(4);
}
static void ei_add_A_N(void) {
uint8_t res,op;
op=z80_iget8();
res=_add8(cpus.r[rA],op);
cpus.r[rA]=res;
z80_clock_inc(7);
}
static void ei_add_A_iHL(void) {
uint8_t res;
res=_add8(cpus.r[rA],_iHL8());
cpus.r[rA]=res;
z80_clock_inc(7);
}
static void ei_add_A_iIXN(void) {
uint8_t res,op;
op=z80_iget8();