1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2004 Forgotten and the VBA development team
4 
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #include <mednafen/mednafen.h>
20 #include <mednafen/state.h>
21 
22 #include "gb.h"
23 #include "gbGlobals.h"
24 #include "z80.h"
25 #include "memory.h"
26 
27 namespace MDFN_IEN_GB
28 {
29 
30 // GameBoy Z80-clone emulation
31 
32 static const uint16 DAATable[] = {
33   0x0080,0x0100,0x0200,0x0300,0x0400,0x0500,0x0600,0x0700,
34   0x0800,0x0900,0x1000,0x1100,0x1200,0x1300,0x1400,0x1500,
35   0x1000,0x1100,0x1200,0x1300,0x1400,0x1500,0x1600,0x1700,
36   0x1800,0x1900,0x2000,0x2100,0x2200,0x2300,0x2400,0x2500,
37   0x2000,0x2100,0x2200,0x2300,0x2400,0x2500,0x2600,0x2700,
38   0x2800,0x2900,0x3000,0x3100,0x3200,0x3300,0x3400,0x3500,
39   0x3000,0x3100,0x3200,0x3300,0x3400,0x3500,0x3600,0x3700,
40   0x3800,0x3900,0x4000,0x4100,0x4200,0x4300,0x4400,0x4500,
41   0x4000,0x4100,0x4200,0x4300,0x4400,0x4500,0x4600,0x4700,
42   0x4800,0x4900,0x5000,0x5100,0x5200,0x5300,0x5400,0x5500,
43   0x5000,0x5100,0x5200,0x5300,0x5400,0x5500,0x5600,0x5700,
44   0x5800,0x5900,0x6000,0x6100,0x6200,0x6300,0x6400,0x6500,
45   0x6000,0x6100,0x6200,0x6300,0x6400,0x6500,0x6600,0x6700,
46   0x6800,0x6900,0x7000,0x7100,0x7200,0x7300,0x7400,0x7500,
47   0x7000,0x7100,0x7200,0x7300,0x7400,0x7500,0x7600,0x7700,
48   0x7800,0x7900,0x8000,0x8100,0x8200,0x8300,0x8400,0x8500,
49   0x8000,0x8100,0x8200,0x8300,0x8400,0x8500,0x8600,0x8700,
50   0x8800,0x8900,0x9000,0x9100,0x9200,0x9300,0x9400,0x9500,
51   0x9000,0x9100,0x9200,0x9300,0x9400,0x9500,0x9600,0x9700,
52   0x9800,0x9900,0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,
53   0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,0x0610,0x0710,
54   0x0810,0x0910,0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,
55   0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,0x1610,0x1710,
56   0x1810,0x1910,0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,
57   0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,0x2610,0x2710,
58   0x2810,0x2910,0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,
59   0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,0x3610,0x3710,
60   0x3810,0x3910,0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,
61   0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,0x4610,0x4710,
62   0x4810,0x4910,0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,
63   0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,0x5610,0x5710,
64   0x5810,0x5910,0x6010,0x6110,0x6210,0x6310,0x6410,0x6510,
65   0x6010,0x6110,0x6210,0x6310,0x6410,0x6510,0x6610,0x6710,
66   0x6810,0x6910,0x7010,0x7110,0x7210,0x7310,0x7410,0x7510,
67   0x7010,0x7110,0x7210,0x7310,0x7410,0x7510,0x7610,0x7710,
68   0x7810,0x7910,0x8010,0x8110,0x8210,0x8310,0x8410,0x8510,
69   0x8010,0x8110,0x8210,0x8310,0x8410,0x8510,0x8610,0x8710,
70   0x8810,0x8910,0x9010,0x9110,0x9210,0x9310,0x9410,0x9510,
71   0x9010,0x9110,0x9210,0x9310,0x9410,0x9510,0x9610,0x9710,
72   0x9810,0x9910,0xA010,0xA110,0xA210,0xA310,0xA410,0xA510,
73   0xA010,0xA110,0xA210,0xA310,0xA410,0xA510,0xA610,0xA710,
74   0xA810,0xA910,0xB010,0xB110,0xB210,0xB310,0xB410,0xB510,
75   0xB010,0xB110,0xB210,0xB310,0xB410,0xB510,0xB610,0xB710,
76   0xB810,0xB910,0xC010,0xC110,0xC210,0xC310,0xC410,0xC510,
77   0xC010,0xC110,0xC210,0xC310,0xC410,0xC510,0xC610,0xC710,
78   0xC810,0xC910,0xD010,0xD110,0xD210,0xD310,0xD410,0xD510,
79   0xD010,0xD110,0xD210,0xD310,0xD410,0xD510,0xD610,0xD710,
80   0xD810,0xD910,0xE010,0xE110,0xE210,0xE310,0xE410,0xE510,
81   0xE010,0xE110,0xE210,0xE310,0xE410,0xE510,0xE610,0xE710,
82   0xE810,0xE910,0xF010,0xF110,0xF210,0xF310,0xF410,0xF510,
83   0xF010,0xF110,0xF210,0xF310,0xF410,0xF510,0xF610,0xF710,
84   0xF810,0xF910,0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,
85   0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,0x0610,0x0710,
86   0x0810,0x0910,0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,
87   0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,0x1610,0x1710,
88   0x1810,0x1910,0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,
89   0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,0x2610,0x2710,
90   0x2810,0x2910,0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,
91   0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,0x3610,0x3710,
92   0x3810,0x3910,0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,
93   0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,0x4610,0x4710,
94   0x4810,0x4910,0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,
95   0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,0x5610,0x5710,
96   0x5810,0x5910,0x6010,0x6110,0x6210,0x6310,0x6410,0x6510,
97   0x0600,0x0700,0x0800,0x0900,0x0A00,0x0B00,0x0C00,0x0D00,
98   0x0E00,0x0F00,0x1000,0x1100,0x1200,0x1300,0x1400,0x1500,
99   0x1600,0x1700,0x1800,0x1900,0x1A00,0x1B00,0x1C00,0x1D00,
100   0x1E00,0x1F00,0x2000,0x2100,0x2200,0x2300,0x2400,0x2500,
101   0x2600,0x2700,0x2800,0x2900,0x2A00,0x2B00,0x2C00,0x2D00,
102   0x2E00,0x2F00,0x3000,0x3100,0x3200,0x3300,0x3400,0x3500,
103   0x3600,0x3700,0x3800,0x3900,0x3A00,0x3B00,0x3C00,0x3D00,
104   0x3E00,0x3F00,0x4000,0x4100,0x4200,0x4300,0x4400,0x4500,
105   0x4600,0x4700,0x4800,0x4900,0x4A00,0x4B00,0x4C00,0x4D00,
106   0x4E00,0x4F00,0x5000,0x5100,0x5200,0x5300,0x5400,0x5500,
107   0x5600,0x5700,0x5800,0x5900,0x5A00,0x5B00,0x5C00,0x5D00,
108   0x5E00,0x5F00,0x6000,0x6100,0x6200,0x6300,0x6400,0x6500,
109   0x6600,0x6700,0x6800,0x6900,0x6A00,0x6B00,0x6C00,0x6D00,
110   0x6E00,0x6F00,0x7000,0x7100,0x7200,0x7300,0x7400,0x7500,
111   0x7600,0x7700,0x7800,0x7900,0x7A00,0x7B00,0x7C00,0x7D00,
112   0x7E00,0x7F00,0x8000,0x8100,0x8200,0x8300,0x8400,0x8500,
113   0x8600,0x8700,0x8800,0x8900,0x8A00,0x8B00,0x8C00,0x8D00,
114   0x8E00,0x8F00,0x9000,0x9100,0x9200,0x9300,0x9400,0x9500,
115   0x9600,0x9700,0x9800,0x9900,0x9A00,0x9B00,0x9C00,0x9D00,
116   0x9E00,0x9F00,0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,
117   0x0610,0x0710,0x0810,0x0910,0x0A10,0x0B10,0x0C10,0x0D10,
118   0x0E10,0x0F10,0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,
119   0x1610,0x1710,0x1810,0x1910,0x1A10,0x1B10,0x1C10,0x1D10,
120   0x1E10,0x1F10,0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,
121   0x2610,0x2710,0x2810,0x2910,0x2A10,0x2B10,0x2C10,0x2D10,
122   0x2E10,0x2F10,0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,
123   0x3610,0x3710,0x3810,0x3910,0x3A10,0x3B10,0x3C10,0x3D10,
124   0x3E10,0x3F10,0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,
125   0x4610,0x4710,0x4810,0x4910,0x4A10,0x4B10,0x4C10,0x4D10,
126   0x4E10,0x4F10,0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,
127   0x5610,0x5710,0x5810,0x5910,0x5A10,0x5B10,0x5C10,0x5D10,
128   0x5E10,0x5F10,0x6010,0x6110,0x6210,0x6310,0x6410,0x6510,
129   0x6610,0x6710,0x6810,0x6910,0x6A10,0x6B10,0x6C10,0x6D10,
130   0x6E10,0x6F10,0x7010,0x7110,0x7210,0x7310,0x7410,0x7510,
131   0x7610,0x7710,0x7810,0x7910,0x7A10,0x7B10,0x7C10,0x7D10,
132   0x7E10,0x7F10,0x8010,0x8110,0x8210,0x8310,0x8410,0x8510,
133   0x8610,0x8710,0x8810,0x8910,0x8A10,0x8B10,0x8C10,0x8D10,
134   0x8E10,0x8F10,0x9010,0x9110,0x9210,0x9310,0x9410,0x9510,
135   0x9610,0x9710,0x9810,0x9910,0x9A10,0x9B10,0x9C10,0x9D10,
136   0x9E10,0x9F10,0xA010,0xA110,0xA210,0xA310,0xA410,0xA510,
137   0xA610,0xA710,0xA810,0xA910,0xAA10,0xAB10,0xAC10,0xAD10,
138   0xAE10,0xAF10,0xB010,0xB110,0xB210,0xB310,0xB410,0xB510,
139   0xB610,0xB710,0xB810,0xB910,0xBA10,0xBB10,0xBC10,0xBD10,
140   0xBE10,0xBF10,0xC010,0xC110,0xC210,0xC310,0xC410,0xC510,
141   0xC610,0xC710,0xC810,0xC910,0xCA10,0xCB10,0xCC10,0xCD10,
142   0xCE10,0xCF10,0xD010,0xD110,0xD210,0xD310,0xD410,0xD510,
143   0xD610,0xD710,0xD810,0xD910,0xDA10,0xDB10,0xDC10,0xDD10,
144   0xDE10,0xDF10,0xE010,0xE110,0xE210,0xE310,0xE410,0xE510,
145   0xE610,0xE710,0xE810,0xE910,0xEA10,0xEB10,0xEC10,0xED10,
146   0xEE10,0xEF10,0xF010,0xF110,0xF210,0xF310,0xF410,0xF510,
147   0xF610,0xF710,0xF810,0xF910,0xFA10,0xFB10,0xFC10,0xFD10,
148   0xFE10,0xFF10,0x0090,0x0110,0x0210,0x0310,0x0410,0x0510,
149   0x0610,0x0710,0x0810,0x0910,0x0A10,0x0B10,0x0C10,0x0D10,
150   0x0E10,0x0F10,0x1010,0x1110,0x1210,0x1310,0x1410,0x1510,
151   0x1610,0x1710,0x1810,0x1910,0x1A10,0x1B10,0x1C10,0x1D10,
152   0x1E10,0x1F10,0x2010,0x2110,0x2210,0x2310,0x2410,0x2510,
153   0x2610,0x2710,0x2810,0x2910,0x2A10,0x2B10,0x2C10,0x2D10,
154   0x2E10,0x2F10,0x3010,0x3110,0x3210,0x3310,0x3410,0x3510,
155   0x3610,0x3710,0x3810,0x3910,0x3A10,0x3B10,0x3C10,0x3D10,
156   0x3E10,0x3F10,0x4010,0x4110,0x4210,0x4310,0x4410,0x4510,
157   0x4610,0x4710,0x4810,0x4910,0x4A10,0x4B10,0x4C10,0x4D10,
158   0x4E10,0x4F10,0x5010,0x5110,0x5210,0x5310,0x5410,0x5510,
159   0x5610,0x5710,0x5810,0x5910,0x5A10,0x5B10,0x5C10,0x5D10,
160   0x5E10,0x5F10,0x6010,0x6110,0x6210,0x6310,0x6410,0x6510,
161   0x00C0,0x0140,0x0240,0x0340,0x0440,0x0540,0x0640,0x0740,
162   0x0840,0x0940,0x0A40,0x0B40,0x0C40,0x0D40,0x0E40,0x0F40,
163   0x1040,0x1140,0x1240,0x1340,0x1440,0x1540,0x1640,0x1740,
164   0x1840,0x1940,0x1A40,0x1B40,0x1C40,0x1D40,0x1E40,0x1F40,
165   0x2040,0x2140,0x2240,0x2340,0x2440,0x2540,0x2640,0x2740,
166   0x2840,0x2940,0x2A40,0x2B40,0x2C40,0x2D40,0x2E40,0x2F40,
167   0x3040,0x3140,0x3240,0x3340,0x3440,0x3540,0x3640,0x3740,
168   0x3840,0x3940,0x3A40,0x3B40,0x3C40,0x3D40,0x3E40,0x3F40,
169   0x4040,0x4140,0x4240,0x4340,0x4440,0x4540,0x4640,0x4740,
170   0x4840,0x4940,0x4A40,0x4B40,0x4C40,0x4D40,0x4E40,0x4F40,
171   0x5040,0x5140,0x5240,0x5340,0x5440,0x5540,0x5640,0x5740,
172   0x5840,0x5940,0x5A40,0x5B40,0x5C40,0x5D40,0x5E40,0x5F40,
173   0x6040,0x6140,0x6240,0x6340,0x6440,0x6540,0x6640,0x6740,
174   0x6840,0x6940,0x6A40,0x6B40,0x6C40,0x6D40,0x6E40,0x6F40,
175   0x7040,0x7140,0x7240,0x7340,0x7440,0x7540,0x7640,0x7740,
176   0x7840,0x7940,0x7A40,0x7B40,0x7C40,0x7D40,0x7E40,0x7F40,
177   0x8040,0x8140,0x8240,0x8340,0x8440,0x8540,0x8640,0x8740,
178   0x8840,0x8940,0x8A40,0x8B40,0x8C40,0x8D40,0x8E40,0x8F40,
179   0x9040,0x9140,0x9240,0x9340,0x9440,0x9540,0x9640,0x9740,
180   0x9840,0x9940,0x9A40,0x9B40,0x9C40,0x9D40,0x9E40,0x9F40,
181   0xA040,0xA140,0xA240,0xA340,0xA440,0xA540,0xA640,0xA740,
182   0xA840,0xA940,0xAA40,0xAB40,0xAC40,0xAD40,0xAE40,0xAF40,
183   0xB040,0xB140,0xB240,0xB340,0xB440,0xB540,0xB640,0xB740,
184   0xB840,0xB940,0xBA40,0xBB40,0xBC40,0xBD40,0xBE40,0xBF40,
185   0xC040,0xC140,0xC240,0xC340,0xC440,0xC540,0xC640,0xC740,
186   0xC840,0xC940,0xCA40,0xCB40,0xCC40,0xCD40,0xCE40,0xCF40,
187   0xD040,0xD140,0xD240,0xD340,0xD440,0xD540,0xD640,0xD740,
188   0xD840,0xD940,0xDA40,0xDB40,0xDC40,0xDD40,0xDE40,0xDF40,
189   0xE040,0xE140,0xE240,0xE340,0xE440,0xE540,0xE640,0xE740,
190   0xE840,0xE940,0xEA40,0xEB40,0xEC40,0xED40,0xEE40,0xEF40,
191   0xF040,0xF140,0xF240,0xF340,0xF440,0xF540,0xF640,0xF740,
192   0xF840,0xF940,0xFA40,0xFB40,0xFC40,0xFD40,0xFE40,0xFF40,
193   0xA050,0xA150,0xA250,0xA350,0xA450,0xA550,0xA650,0xA750,
194   0xA850,0xA950,0xAA50,0xAB50,0xAC50,0xAD50,0xAE50,0xAF50,
195   0xB050,0xB150,0xB250,0xB350,0xB450,0xB550,0xB650,0xB750,
196   0xB850,0xB950,0xBA50,0xBB50,0xBC50,0xBD50,0xBE50,0xBF50,
197   0xC050,0xC150,0xC250,0xC350,0xC450,0xC550,0xC650,0xC750,
198   0xC850,0xC950,0xCA50,0xCB50,0xCC50,0xCD50,0xCE50,0xCF50,
199   0xD050,0xD150,0xD250,0xD350,0xD450,0xD550,0xD650,0xD750,
200   0xD850,0xD950,0xDA50,0xDB50,0xDC50,0xDD50,0xDE50,0xDF50,
201   0xE050,0xE150,0xE250,0xE350,0xE450,0xE550,0xE650,0xE750,
202   0xE850,0xE950,0xEA50,0xEB50,0xEC50,0xED50,0xEE50,0xEF50,
203   0xF050,0xF150,0xF250,0xF350,0xF450,0xF550,0xF650,0xF750,
204   0xF850,0xF950,0xFA50,0xFB50,0xFC50,0xFD50,0xFE50,0xFF50,
205   0x00D0,0x0150,0x0250,0x0350,0x0450,0x0550,0x0650,0x0750,
206   0x0850,0x0950,0x0A50,0x0B50,0x0C50,0x0D50,0x0E50,0x0F50,
207   0x1050,0x1150,0x1250,0x1350,0x1450,0x1550,0x1650,0x1750,
208   0x1850,0x1950,0x1A50,0x1B50,0x1C50,0x1D50,0x1E50,0x1F50,
209   0x2050,0x2150,0x2250,0x2350,0x2450,0x2550,0x2650,0x2750,
210   0x2850,0x2950,0x2A50,0x2B50,0x2C50,0x2D50,0x2E50,0x2F50,
211   0x3050,0x3150,0x3250,0x3350,0x3450,0x3550,0x3650,0x3750,
212   0x3850,0x3950,0x3A50,0x3B50,0x3C50,0x3D50,0x3E50,0x3F50,
213   0x4050,0x4150,0x4250,0x4350,0x4450,0x4550,0x4650,0x4750,
214   0x4850,0x4950,0x4A50,0x4B50,0x4C50,0x4D50,0x4E50,0x4F50,
215   0x5050,0x5150,0x5250,0x5350,0x5450,0x5550,0x5650,0x5750,
216   0x5850,0x5950,0x5A50,0x5B50,0x5C50,0x5D50,0x5E50,0x5F50,
217   0x6050,0x6150,0x6250,0x6350,0x6450,0x6550,0x6650,0x6750,
218   0x6850,0x6950,0x6A50,0x6B50,0x6C50,0x6D50,0x6E50,0x6F50,
219   0x7050,0x7150,0x7250,0x7350,0x7450,0x7550,0x7650,0x7750,
220   0x7850,0x7950,0x7A50,0x7B50,0x7C50,0x7D50,0x7E50,0x7F50,
221   0x8050,0x8150,0x8250,0x8350,0x8450,0x8550,0x8650,0x8750,
222   0x8850,0x8950,0x8A50,0x8B50,0x8C50,0x8D50,0x8E50,0x8F50,
223   0x9050,0x9150,0x9250,0x9350,0x9450,0x9550,0x9650,0x9750,
224   0x9850,0x9950,0x9A50,0x9B50,0x9C50,0x9D50,0x9E50,0x9F50,
225   0xFA40,0xFB40,0xFC40,0xFD40,0xFE40,0xFF40,0x00C0,0x0140,
226   0x0240,0x0340,0x0440,0x0540,0x0640,0x0740,0x0840,0x0940,
227   0x0A40,0x0B40,0x0C40,0x0D40,0x0E40,0x0F40,0x1040,0x1140,
228   0x1240,0x1340,0x1440,0x1540,0x1640,0x1740,0x1840,0x1940,
229   0x1A40,0x1B40,0x1C40,0x1D40,0x1E40,0x1F40,0x2040,0x2140,
230   0x2240,0x2340,0x2440,0x2540,0x2640,0x2740,0x2840,0x2940,
231   0x2A40,0x2B40,0x2C40,0x2D40,0x2E40,0x2F40,0x3040,0x3140,
232   0x3240,0x3340,0x3440,0x3540,0x3640,0x3740,0x3840,0x3940,
233   0x3A40,0x3B40,0x3C40,0x3D40,0x3E40,0x3F40,0x4040,0x4140,
234   0x4240,0x4340,0x4440,0x4540,0x4640,0x4740,0x4840,0x4940,
235   0x4A40,0x4B40,0x4C40,0x4D40,0x4E40,0x4F40,0x5040,0x5140,
236   0x5240,0x5340,0x5440,0x5540,0x5640,0x5740,0x5840,0x5940,
237   0x5A40,0x5B40,0x5C40,0x5D40,0x5E40,0x5F40,0x6040,0x6140,
238   0x6240,0x6340,0x6440,0x6540,0x6640,0x6740,0x6840,0x6940,
239   0x6A40,0x6B40,0x6C40,0x6D40,0x6E40,0x6F40,0x7040,0x7140,
240   0x7240,0x7340,0x7440,0x7540,0x7640,0x7740,0x7840,0x7940,
241   0x7A40,0x7B40,0x7C40,0x7D40,0x7E40,0x7F40,0x8040,0x8140,
242   0x8240,0x8340,0x8440,0x8540,0x8640,0x8740,0x8840,0x8940,
243   0x8A40,0x8B40,0x8C40,0x8D40,0x8E40,0x8F40,0x9040,0x9140,
244   0x9240,0x9340,0x9440,0x9540,0x9640,0x9740,0x9840,0x9940,
245   0x9A40,0x9B40,0x9C40,0x9D40,0x9E40,0x9F40,0xA040,0xA140,
246   0xA240,0xA340,0xA440,0xA540,0xA640,0xA740,0xA840,0xA940,
247   0xAA40,0xAB40,0xAC40,0xAD40,0xAE40,0xAF40,0xB040,0xB140,
248   0xB240,0xB340,0xB440,0xB540,0xB640,0xB740,0xB840,0xB940,
249   0xBA40,0xBB40,0xBC40,0xBD40,0xBE40,0xBF40,0xC040,0xC140,
250   0xC240,0xC340,0xC440,0xC540,0xC640,0xC740,0xC840,0xC940,
251   0xCA40,0xCB40,0xCC40,0xCD40,0xCE40,0xCF40,0xD040,0xD140,
252   0xD240,0xD340,0xD440,0xD540,0xD640,0xD740,0xD840,0xD940,
253   0xDA40,0xDB40,0xDC40,0xDD40,0xDE40,0xDF40,0xE040,0xE140,
254   0xE240,0xE340,0xE440,0xE540,0xE640,0xE740,0xE840,0xE940,
255   0xEA40,0xEB40,0xEC40,0xED40,0xEE40,0xEF40,0xF040,0xF140,
256   0xF240,0xF340,0xF440,0xF540,0xF640,0xF740,0xF840,0xF940,
257   0x9A50,0x9B50,0x9C50,0x9D50,0x9E50,0x9F50,0xA050,0xA150,
258   0xA250,0xA350,0xA450,0xA550,0xA650,0xA750,0xA850,0xA950,
259   0xAA50,0xAB50,0xAC50,0xAD50,0xAE50,0xAF50,0xB050,0xB150,
260   0xB250,0xB350,0xB450,0xB550,0xB650,0xB750,0xB850,0xB950,
261   0xBA50,0xBB50,0xBC50,0xBD50,0xBE50,0xBF50,0xC050,0xC150,
262   0xC250,0xC350,0xC450,0xC550,0xC650,0xC750,0xC850,0xC950,
263   0xCA50,0xCB50,0xCC50,0xCD50,0xCE50,0xCF50,0xD050,0xD150,
264   0xD250,0xD350,0xD450,0xD550,0xD650,0xD750,0xD850,0xD950,
265   0xDA50,0xDB50,0xDC50,0xDD50,0xDE50,0xDF50,0xE050,0xE150,
266   0xE250,0xE350,0xE450,0xE550,0xE650,0xE750,0xE850,0xE950,
267   0xEA50,0xEB50,0xEC50,0xED50,0xEE50,0xEF50,0xF050,0xF150,
268   0xF250,0xF350,0xF450,0xF550,0xF650,0xF750,0xF850,0xF950,
269   0xFA50,0xFB50,0xFC50,0xFD50,0xFE50,0xFF50,0x00D0,0x0150,
270   0x0250,0x0350,0x0450,0x0550,0x0650,0x0750,0x0850,0x0950,
271   0x0A50,0x0B50,0x0C50,0x0D50,0x0E50,0x0F50,0x1050,0x1150,
272   0x1250,0x1350,0x1450,0x1550,0x1650,0x1750,0x1850,0x1950,
273   0x1A50,0x1B50,0x1C50,0x1D50,0x1E50,0x1F50,0x2050,0x2150,
274   0x2250,0x2350,0x2450,0x2550,0x2650,0x2750,0x2850,0x2950,
275   0x2A50,0x2B50,0x2C50,0x2D50,0x2E50,0x2F50,0x3050,0x3150,
276   0x3250,0x3350,0x3450,0x3550,0x3650,0x3750,0x3850,0x3950,
277   0x3A50,0x3B50,0x3C50,0x3D50,0x3E50,0x3F50,0x4050,0x4150,
278   0x4250,0x4350,0x4450,0x4550,0x4650,0x4750,0x4850,0x4950,
279   0x4A50,0x4B50,0x4C50,0x4D50,0x4E50,0x4F50,0x5050,0x5150,
280   0x5250,0x5350,0x5450,0x5550,0x5650,0x5750,0x5850,0x5950,
281   0x5A50,0x5B50,0x5C50,0x5D50,0x5E50,0x5F50,0x6050,0x6150,
282   0x6250,0x6350,0x6450,0x6550,0x6650,0x6750,0x6850,0x6950,
283   0x6A50,0x6B50,0x6C50,0x6D50,0x6E50,0x6F50,0x7050,0x7150,
284   0x7250,0x7350,0x7450,0x7550,0x7650,0x7750,0x7850,0x7950,
285   0x7A50,0x7B50,0x7C50,0x7D50,0x7E50,0x7F50,0x8050,0x8150,
286   0x8250,0x8350,0x8450,0x8550,0x8650,0x8750,0x8850,0x8950,
287   0x8A50,0x8B50,0x8C50,0x8D50,0x8E50,0x8F50,0x9050,0x9150,
288   0x9250,0x9350,0x9450,0x9550,0x9650,0x9750,0x9850,0x9950,
289 };
290 
291 static const int gbCycles[] = {
292 //  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
293     1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1,  // 0
294     1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1,  // 1
295     2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1,  // 2
296     2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1,  // 3
297     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 4
298     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 5
299     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 6
300     2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1,  // 7
301     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 8
302     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 9
303     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // a
304     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // b
305     2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 2, 3, 6, 2, 4,  // c
306     2, 3, 3, 0, 3, 4, 2, 4, 2, 4, 3, 0, 3, 0, 2, 4,  // d
307     3, 3, 2, 0, 0, 4, 2, 4, 4, 1, 4, 0, 0, 0, 2, 4,  // e
308     3, 3, 2, 1, 0, 4, 2, 4, 3, 2, 4, 1, 0, 0, 2, 4   // f
309 };
310 
311 static const int gbCyclesCB[] = {
312 //  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
313     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 0
314     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 1
315     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 2
316     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 3
317     2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 4
318     2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 5
319     2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 6
320     2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 7
321     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 8
322     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 9
323     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // a
324     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // b
325     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // c
326     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // d
327     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // e
328     2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2   // f
329 };
330 
331 static const uint8 ZeroTable[256] = {
332   0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
333   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
334   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
335   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
336   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
337   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
338   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
339   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
340   0,0,0,0,0,0,0,0,
341   0,0,0,0,0,0,0,0,
342   0,0,0,0,0,0,0,0,
343   0,0,0,0,0,0,0,0,
344   0,0,0,0,0,0,0,0,
345   0,0,0,0,0,0,0,0,
346   0,0,0,0,0,0,0,0,
347   0,0,0,0,0,0,0,0,
348   0,0,0,0,0,0,0,0,
349   0,0,0,0,0,0,0,0,
350   0,0,0,0,0,0,0,0,
351   0,0,0,0,0,0,0,0,
352   0,0,0,0,0,0,0,0,
353   0,0,0,0,0,0,0,0,
354   0,0,0,0,0,0,0,0,
355   0,0,0,0,0,0,0,0
356 };
357 
358 
359 
360 // registers
361 static gbRegister PC;
362 static gbRegister SP;
363 static gbRegister AF;
364 static gbRegister BC;
365 static gbRegister DE;
366 static gbRegister HL;
367 static uint8 IFF;
368 static bool InHALT;
369 static bool RepeatNextByte;
370 static bool EI_Delayed;
371 
GBZ80_Interrupt(int which)372 void GBZ80_Interrupt(int which)
373 {
374  IFF = false;
375  EI_Delayed = false;
376 
377  register_IF &= ~(1 << which);
378 
379  gbWriteMemory(--SP.W, PC.B.B1);
380  gbWriteMemory(--SP.W, PC.B.B0);
381  PC.W = 0x40 + (which << 3);
382 }
383 
GBZ80_Reset(void)384 void GBZ80_Reset(void)
385 {
386   SP.W = 0xfffe;
387   AF.W = 0x01b0;
388   BC.W = 0x0013;
389   DE.W = 0x00d8;
390   HL.W = 0x014d;
391   PC.W = 0x0100;
392   IFF = false;
393 
394   EI_Delayed = false;
395   RepeatNextByte = false;
396   InHALT = false;
397 
398   if(gbCgbMode)
399   {
400    AF.W = 0x11b0;
401    BC.W = 0x0000;
402    DE.W = 0xff56;
403    HL.W = 0x000d;
404    if(gbEmulatorType == 4)
405     BC.B.B1 |= 0x01;
406   }
407 }
408 
409 // Returns the number of cycles this instruction took
GBZ80_RunOp(void)410 int GBZ80_RunOp(void)
411 {
412  int clockTicks;
413  uint8 opcode;
414  gbRegister tempRegister;
415  uint8 tempValue;
416  int8 offset;
417 
418 
419  // HALT will be exited even when interrupts are disabled by IFF
420  if(register_IF & register_IE)
421   InHALT = false;
422 
423  if(IFF)
424  {
425   if(register_IF & register_IE & 0x01)
426    GBZ80_Interrupt(0);
427   else if(register_IF & register_IE & 0x02)
428    GBZ80_Interrupt(1);
429   else if(register_IF & register_IE & 0x04)
430    GBZ80_Interrupt(2);
431   else if(register_IF & register_IE & 0x08)
432    GBZ80_Interrupt(3);
433   else if(register_IF & register_IE & 0x10)
434    GBZ80_Interrupt(4);
435  }
436 
437  if(EI_Delayed)
438  {
439   IFF = true;
440   EI_Delayed = false;
441  }
442 
443  if(InHALT)
444   return(4);
445 
446  opcode = gbReadMemory(PC.W++);
447 
448  if(RepeatNextByte)
449  {
450   RepeatNextByte = false;
451   PC.W--;
452  }
453 
454  clockTicks = gbCycles[opcode];
455 
456  switch(opcode)
457  {
458   case 0xCB:
459          // extended opcode
460          opcode = gbReadMemory(PC.W++);
461          clockTicks = gbCyclesCB[opcode];
462          switch(opcode)
463          {
464           #include "gbCodesCB.h"
465          }
466          break;
467 
468          #include "gbCodes.h"
469  }
470 
471  return(clockTicks);
472 }
473 
GBZ80_StateAction(StateMem * sm,int load,int data_only)474 void GBZ80_StateAction(StateMem *sm, int load, int data_only)
475 {
476  SFORMAT StateRegs[] =
477  {
478   SFVAR(PC.W),
479   SFVAR(SP.W),
480   SFVAR(AF.W),
481   SFVAR(BC.W),
482   SFVAR(DE.W),
483   SFVAR(HL.W),
484   SFVAR(IFF),
485   SFVAR(InHALT),
486   SFVAR(RepeatNextByte),
487   SFVAR(EI_Delayed),
488   SFEND
489  };
490  MDFNSS_StateAction(sm, load, data_only, StateRegs, "CPU");
491 
492  if(load)
493  {
494 
495  }
496 }
497 
498 }
499