diff options
author | Andree Buschmann <AndreeBuschmann@t-online.de> | 2011-08-07 20:01:04 +0000 |
---|---|---|
committer | Andree Buschmann <AndreeBuschmann@t-online.de> | 2011-08-07 20:01:04 +0000 |
commit | acb0917556fc33681c1df5a530cf754193e67705 (patch) | |
tree | 052a47097009a210e4aed9c207bd6aa4828cc000 /apps/codecs/libgme/gb_cpu_run.h | |
parent | 93c6f1329a5691a8be158cefe15641bd1daf9ef8 (diff) | |
download | rockbox-acb0917556fc33681c1df5a530cf754193e67705.tar.gz rockbox-acb0917556fc33681c1df5a530cf754193e67705.zip |
Submit initial patch from FS#12176. Adds support for several new game music formats (AY, GBS, HES, KSS, SGC, VGM and VGZ) and replaces the current NSF and NSFE with a new implementation based on a port of the Game Music Emu library 'GME'. This first submit does not cover the full functionality provided by the author's original patch: Coleco-SGV is not supported, some GME-specific m3u-support has been removed and IRAM is not used yet. Further changes are very likely to follow this submit. Thanks to Mauricio Garrido.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30264 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libgme/gb_cpu_run.h')
-rw-r--r-- | apps/codecs/libgme/gb_cpu_run.h | 1187 |
1 files changed, 1187 insertions, 0 deletions
diff --git a/apps/codecs/libgme/gb_cpu_run.h b/apps/codecs/libgme/gb_cpu_run.h new file mode 100644 index 0000000000..86f06fa859 --- /dev/null +++ b/apps/codecs/libgme/gb_cpu_run.h | |||
@@ -0,0 +1,1187 @@ | |||
1 | // Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/ | ||
2 | |||
3 | #if 0 | ||
4 | /* Define these macros in the source file before #including this file. | ||
5 | - Parameters might be expressions, so they are best evaluated only once, | ||
6 | though they NEVER have side-effects, so multiple evaluation is OK. | ||
7 | - Output parameters might be a multiple-assignment expression like "a=x", | ||
8 | so they must NOT be parenthesized. | ||
9 | - Macros "returning" void may use a {} statement block. */ | ||
10 | |||
11 | // 0 <= addr <= 0xFFFF + page_size | ||
12 | // time functions can be used | ||
13 | int READ_MEM( addr_t ); | ||
14 | void WRITE_MEM( addr_t, int data ); | ||
15 | |||
16 | // Access of 0xFF00 + offset | ||
17 | // 0 <= offset <= 0xFF | ||
18 | int READ_IO( int offset ); | ||
19 | void WRITE_IO( int offset, int data ); | ||
20 | |||
21 | // Often-used instructions use this instead of READ_MEM | ||
22 | void READ_FAST( addr_t, int& out ); | ||
23 | |||
24 | // The following can be used within macros: | ||
25 | |||
26 | // Current time | ||
27 | cpu_time_t TIME(); | ||
28 | #endif | ||
29 | |||
30 | /* Copyright (C) 2003-2009 Shay Green. This module is free software; you | ||
31 | can redistribute it and/or modify it under the terms of the GNU Lesser | ||
32 | General Public License as published by the Free Software Foundation; either | ||
33 | version 2.1 of the License, or (at your option) any later version. This | ||
34 | module is distributed in the hope that it will be useful, but WITHOUT ANY | ||
35 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
36 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
37 | details. You should have received a copy of the GNU Lesser General Public | ||
38 | License along with this module; if not, write to the Free Software Foundation, | ||
39 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ | ||
40 | |||
41 | // Common instructions: | ||
42 | // | ||
43 | // 365880 FA LD A,(nn) | ||
44 | // 355863 20 JR NZ | ||
45 | // 313655 21 LD HL,nn | ||
46 | // 274580 28 JR Z | ||
47 | // 252878 FE CP n | ||
48 | // 230541 7E LD A,(HL) | ||
49 | // 226209 2A LD A,(HL+) | ||
50 | // 217467 CD CALL | ||
51 | // 212034 C9 RET | ||
52 | // 208376 CB CB prefix | ||
53 | // | ||
54 | // 27486 CB 7E BIT 7,(HL) | ||
55 | // 15925 CB 76 BIT 6,(HL) | ||
56 | // 13035 CB 19 RR C | ||
57 | // 11557 CB 7F BIT 7,A | ||
58 | // 10898 CB 37 SWAP A | ||
59 | // 10208 CB 66 BIT 4,(HL) | ||
60 | |||
61 | // Allows MWCW debugger to step through code properly | ||
62 | #ifdef CPU_BEGIN | ||
63 | CPU_BEGIN | ||
64 | #endif | ||
65 | |||
66 | #define TIME() s.time | ||
67 | |||
68 | #define CODE_PAGE( addr ) s.code_map [GB_CPU_PAGE( addr )] | ||
69 | #define READ_CODE( addr ) (CODE_PAGE( addr ) [GB_CPU_OFFSET( addr )]) | ||
70 | |||
71 | // Flags with hex value for clarity when used as mask. | ||
72 | // Stored in indicated variable during emulation. | ||
73 | int const z80 = 0x80; // cz | ||
74 | int const n40 = 0x40; // ph | ||
75 | int const h20 = 0x20; // ph | ||
76 | int const c10 = 0x10; // cz | ||
77 | |||
78 | #define SET_FLAGS( in )\ | ||
79 | {\ | ||
80 | cz = ((in) << 4 & 0x100) + (~(in) >> 7 & 1);\ | ||
81 | ph = (~(in) << 2 & 0x100) + ((in) >> 1 & 0x10);\ | ||
82 | } | ||
83 | |||
84 | // random bits in cz to catch misuse of them | ||
85 | #define SET_FLAGS_DEBUG( in )\ | ||
86 | {\ | ||
87 | cz = ((in) << 4 & 0x100) | (rand() & ~0x1FF) | ((in) & 0x80 ? 0 : (rand() & 0xFF) | 1);\ | ||
88 | ph = (~(in) << 2 & 0x100) | (((in) >> 1 & 0x10) ^ BYTE( cz ));\ | ||
89 | } | ||
90 | |||
91 | #define GET_FLAGS( out )\ | ||
92 | {\ | ||
93 | out = (cz >> 4 & c10);\ | ||
94 | out += ~ph >> 2 & n40;\ | ||
95 | out += (ph ^ cz) << 1 & h20;\ | ||
96 | if ( !BYTE( cz ) )\ | ||
97 | out += z80;\ | ||
98 | } | ||
99 | |||
100 | #define CC_NZ() ( BYTE( cz )) | ||
101 | #define CC_Z() (!BYTE( cz )) | ||
102 | #define CC_NC() (!(cz & 0x100)) | ||
103 | #define CC_C() ( cz & 0x100 ) | ||
104 | |||
105 | // Truncation | ||
106 | #define BYTE( n ) ((uint8_t ) (n)) /* (unsigned) n & 0xFF */ | ||
107 | #define SBYTE( n ) ((int8_t ) (n)) /* (BYTE( n ) ^ 0x80) - 0x80 */ | ||
108 | #define WORD( n ) ((uint16_t) (n)) /* (unsigned) n & 0xFFFF */ | ||
109 | |||
110 | { | ||
111 | struct cpu_state_t s; | ||
112 | cpu->cpu_state = &s; | ||
113 | memcpy( &s, &cpu->cpu_state_, sizeof s ); | ||
114 | |||
115 | union { | ||
116 | struct { | ||
117 | #ifdef BLARGG_BIG_ENDIAN | ||
118 | byte b, c, d, e, h, l, flags, a; | ||
119 | #else | ||
120 | byte c, b, e, d, l, h, a, flags; | ||
121 | #endif | ||
122 | } rg; // individual registers | ||
123 | struct core_regs_t rp; // pairs | ||
124 | |||
125 | byte r8_ [8]; // indexed registers (use R8 macro due to endian dependence) | ||
126 | uint16_t r16 [4]; // indexed pairs | ||
127 | } reg; | ||
128 | BOOST_STATIC_ASSERT( sizeof reg.rg == 8 && sizeof reg.rp == 8 ); | ||
129 | |||
130 | #ifdef BLARGG_BIG_ENDIAN | ||
131 | #define R8( n ) (reg.r8_ [n]) | ||
132 | #elif BLARGG_LITTLE_ENDIAN | ||
133 | #define R8( n ) (reg.r8_ [(n) ^ 1]) | ||
134 | #else | ||
135 | // Be sure "blargg_endian.h" has been #included in the file that #includes this | ||
136 | #error "Byte order of CPU must be known" | ||
137 | #endif | ||
138 | |||
139 | #define R16( n ) (reg.r16 [n]) | ||
140 | #define RG (reg.rg) | ||
141 | #define RP (reg.rp) | ||
142 | |||
143 | RP = cpu->r.rp; | ||
144 | int pc = cpu->r.pc; | ||
145 | int sp = cpu->r.sp; | ||
146 | int ph; | ||
147 | int cz; | ||
148 | SET_FLAGS( RG.flags ); | ||
149 | |||
150 | int time = s.time; | ||
151 | |||
152 | loop: | ||
153 | |||
154 | check( (unsigned) pc < 0x10000 + 1 ); // +1 so emulator can catch wrap-around | ||
155 | check( (unsigned) sp < 0x10000 ); | ||
156 | |||
157 | byte const* instr = CODE_PAGE( pc ); | ||
158 | int op; | ||
159 | |||
160 | if ( GB_CPU_OFFSET(~0) == ~0 ) | ||
161 | { | ||
162 | op = instr [pc]; | ||
163 | pc++; | ||
164 | instr += pc; | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | instr += GB_CPU_OFFSET( pc ); | ||
169 | op = *instr++; | ||
170 | pc++; | ||
171 | } | ||
172 | |||
173 | #define GET_ADDR() GET_LE16( instr ) | ||
174 | |||
175 | static byte const instr_times [256*2] ICONST_ATTR = { | ||
176 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F | ||
177 | 4,12, 8, 8, 4, 4, 8, 4,20, 8, 8, 8, 4, 4, 8, 4,// 0 | ||
178 | 4,12, 8, 8, 4, 4, 8, 4,12, 8, 8, 8, 4, 4, 8, 4,// 1 | ||
179 | 8,12, 8, 8, 4, 4, 8, 4, 8, 8, 8, 8, 4, 4, 8, 4,// 2 | ||
180 | 8,12, 8, 8,12,12,12, 4, 8, 8, 8, 8, 4, 4, 8, 4,// 3 | ||
181 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// 4 | ||
182 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// 5 | ||
183 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// 6 | ||
184 | 8, 8, 8, 8, 8, 8, 0, 8, 4, 4, 4, 4, 4, 4, 8, 4,// 7 | ||
185 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// 8 | ||
186 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// 9 | ||
187 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// A | ||
188 | 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 8, 4,// B | ||
189 | 8,12,16,16,12,16, 8,16, 8,16,16, 0,12,24, 8,16,// C | ||
190 | 8,12,16, 0,12,16, 8,16, 8,16,16, 0,12, 0, 8,16,// D | ||
191 | 12,12, 8, 0, 0,16, 8,16,16, 4,16, 0, 0, 0, 8,16,// E | ||
192 | 12,12, 8, 4, 0,16, 8,16,12, 8,16, 4, 0, 0, 8,16,// F | ||
193 | |||
194 | // CB prefixed | ||
195 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F | ||
196 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// 0 | ||
197 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// 1 | ||
198 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// 2 | ||
199 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// 3 | ||
200 | 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,// 4 | ||
201 | 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,// 5 | ||
202 | 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,// 6 | ||
203 | 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,// 7 | ||
204 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// 8 | ||
205 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// 9 | ||
206 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// A | ||
207 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// B | ||
208 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// C | ||
209 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// D | ||
210 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// E | ||
211 | 8, 8, 8, 8, 8, 8,16, 8, 8, 8, 8, 8, 8, 8,16, 8,// F | ||
212 | }; | ||
213 | |||
214 | if ( time >= 0 ) | ||
215 | goto stop; | ||
216 | |||
217 | time += instr_times [op]; | ||
218 | |||
219 | int data; | ||
220 | data = *instr; | ||
221 | s.time = time; | ||
222 | |||
223 | #ifdef CPU_INSTR_HOOK | ||
224 | { CPU_INSTR_HOOK( (pc-1), (instr-1), rg.a, rp.bc, rp.de, rp.hl, sp ); } | ||
225 | #endif | ||
226 | |||
227 | switch ( op ) | ||
228 | { | ||
229 | |||
230 | // TODO: more efficient way to handle negative branch that wraps PC around | ||
231 | #define BRANCH_( cond, clocks )\ | ||
232 | {\ | ||
233 | pc++;\ | ||
234 | if ( !(cond) )\ | ||
235 | goto loop;\ | ||
236 | pc = WORD( pc + SBYTE( data ) );\ | ||
237 | time += clocks;\ | ||
238 | goto loop;\ | ||
239 | } | ||
240 | |||
241 | #define BRANCH( cond ) BRANCH_( cond, 4 ) | ||
242 | |||
243 | // Most Common | ||
244 | |||
245 | case 0x20: // JR NZ | ||
246 | BRANCH( CC_NZ() ) | ||
247 | |||
248 | case 0x21: // LD HL,IMM (common) | ||
249 | RP.hl = GET_ADDR(); | ||
250 | pc += 2; | ||
251 | goto loop; | ||
252 | |||
253 | case 0x28: // JR Z | ||
254 | BRANCH( CC_Z() ) | ||
255 | |||
256 | case 0xF2: // LD A,(0xFF00+C) | ||
257 | READ_IO( this, RG.c, RG.a ); | ||
258 | goto loop; | ||
259 | |||
260 | case 0xF0: // LD A,(0xFF00+imm) | ||
261 | pc++; | ||
262 | READ_IO( this, data, RG.a ); | ||
263 | goto loop; | ||
264 | |||
265 | { | ||
266 | int temp; | ||
267 | case 0x0A: // LD A,(BC) | ||
268 | temp = RP.bc; | ||
269 | goto ld_a_ind_comm; | ||
270 | |||
271 | case 0x3A: // LD A,(HL-) | ||
272 | temp = RP.hl; | ||
273 | RP.hl = temp - 1; | ||
274 | goto ld_a_ind_comm; | ||
275 | |||
276 | case 0x1A: // LD A,(DE) | ||
277 | temp = RP.de; | ||
278 | goto ld_a_ind_comm; | ||
279 | |||
280 | case 0x2A: // LD A,(HL+) (common) | ||
281 | temp = RP.hl; | ||
282 | RP.hl = temp + 1; | ||
283 | goto ld_a_ind_comm; | ||
284 | |||
285 | case 0xFA: // LD A,IND16 (common) | ||
286 | temp = GET_ADDR(); | ||
287 | pc += 2; | ||
288 | ld_a_ind_comm: | ||
289 | READ_FAST( this, temp, RG.a ); | ||
290 | goto loop; | ||
291 | } | ||
292 | |||
293 | { | ||
294 | int temp; | ||
295 | case 0xBE: // CP (HL) | ||
296 | temp = READ_MEM( this, RP.hl ); | ||
297 | goto cmp_comm; | ||
298 | |||
299 | case 0xB8: // CP B | ||
300 | case 0xB9: // CP C | ||
301 | case 0xBA: // CP D | ||
302 | case 0xBB: // CP E | ||
303 | case 0xBC: // CP H | ||
304 | case 0xBD: // CP L | ||
305 | case 0xBF: // CP A | ||
306 | temp = R8( op & 7 ); | ||
307 | cmp_comm: | ||
308 | ph = RG.a ^ temp; // N=1 H=* | ||
309 | cz = RG.a - temp; // C=* Z=* | ||
310 | goto loop; | ||
311 | } | ||
312 | |||
313 | case 0xFE: // CP IMM | ||
314 | pc++; | ||
315 | ph = RG.a ^ data; // N=1 H=* | ||
316 | cz = RG.a - data; // C=* Z=* | ||
317 | goto loop; | ||
318 | |||
319 | case 0x46: // LD B,(HL) | ||
320 | case 0x4E: // LD C,(HL) | ||
321 | case 0x56: // LD D,(HL) | ||
322 | case 0x5E: // LD E,(HL) | ||
323 | case 0x66: // LD H,(HL) | ||
324 | case 0x6E: // LD L,(HL) | ||
325 | case 0x7E:{// LD A,(HL) | ||
326 | int addr = RP.hl; | ||
327 | READ_FAST( this, addr, R8( op >> 3 & 7 ) ); | ||
328 | goto loop; | ||
329 | } | ||
330 | |||
331 | case 0xC4: // CNZ (next-most-common) | ||
332 | pc += 2; | ||
333 | if ( CC_Z() ) | ||
334 | goto loop; | ||
335 | call: | ||
336 | time += 12; | ||
337 | pc -= 2; | ||
338 | case 0xCD: // CALL (most-common) | ||
339 | data = pc + 2; | ||
340 | pc = GET_ADDR(); | ||
341 | push: { | ||
342 | int addr = WORD( sp - 1 ); | ||
343 | WRITE_MEM( this, addr, (data >> 8) ); | ||
344 | sp = WORD( sp - 2 ); | ||
345 | WRITE_MEM( this, sp, data ); | ||
346 | goto loop; | ||
347 | } | ||
348 | |||
349 | case 0xC8: // RET Z (next-most-common) | ||
350 | if ( CC_NZ() ) | ||
351 | goto loop; | ||
352 | ret: | ||
353 | time += 12; | ||
354 | case 0xD9: // RETI | ||
355 | case 0xC9:{// RET (most common) | ||
356 | pc = READ_MEM( this, sp ); | ||
357 | int addr = sp + 1; | ||
358 | sp = WORD( sp + 2 ); | ||
359 | pc += 0x100 * READ_MEM( this, addr ); | ||
360 | goto loop; | ||
361 | } | ||
362 | |||
363 | case 0x00: // NOP | ||
364 | case 0x40: // LD B,B | ||
365 | case 0x49: // LD C,C | ||
366 | case 0x52: // LD D,D | ||
367 | case 0x5B: // LD E,E | ||
368 | case 0x64: // LD H,H | ||
369 | case 0x6D: // LD L,L | ||
370 | case 0x7F: // LD A,A | ||
371 | goto loop; | ||
372 | |||
373 | // CB Instructions | ||
374 | |||
375 | case 0xCB: | ||
376 | time += (instr_times + 256) [data]; | ||
377 | pc++; | ||
378 | // now data is the opcode | ||
379 | switch ( data ) { | ||
380 | |||
381 | case 0x46: // BIT b,(HL) | ||
382 | case 0x4E: | ||
383 | case 0x56: | ||
384 | case 0x5E: | ||
385 | case 0x66: | ||
386 | case 0x6E: | ||
387 | case 0x76: | ||
388 | case 0x7E: { | ||
389 | int addr = RP.hl; | ||
390 | READ_FAST( this, addr, op ); | ||
391 | goto bit_comm; | ||
392 | } | ||
393 | |||
394 | case 0x40: case 0x41: case 0x42: case 0x43: // BIT b,r | ||
395 | case 0x44: case 0x45: case 0x47: case 0x48: | ||
396 | case 0x49: case 0x4A: case 0x4B: case 0x4C: | ||
397 | case 0x4D: case 0x4F: case 0x50: case 0x51: | ||
398 | case 0x52: case 0x53: case 0x54: case 0x55: | ||
399 | case 0x57: case 0x58: case 0x59: case 0x5A: | ||
400 | case 0x5B: case 0x5C: case 0x5D: case 0x5F: | ||
401 | case 0x60: case 0x61: case 0x62: case 0x63: | ||
402 | case 0x64: case 0x65: case 0x67: case 0x68: | ||
403 | case 0x69: case 0x6A: case 0x6B: case 0x6C: | ||
404 | case 0x6D: case 0x6F: case 0x70: case 0x71: | ||
405 | case 0x72: case 0x73: case 0x74: case 0x75: | ||
406 | case 0x77: case 0x78: case 0x79: case 0x7A: | ||
407 | case 0x7B: case 0x7C: case 0x7D: case 0x7F: | ||
408 | op = R8( data & 7 ); | ||
409 | bit_comm: | ||
410 | ph = op >> (data >> 3 & 7) & 1; | ||
411 | cz = (cz & 0x100) + ph; | ||
412 | ph ^= 0x110; // N=0 H=1 | ||
413 | goto loop; | ||
414 | |||
415 | case 0x86: // RES b,(HL) | ||
416 | case 0x8E: | ||
417 | case 0x96: | ||
418 | case 0x9E: | ||
419 | case 0xA6: | ||
420 | case 0xAE: | ||
421 | case 0xB6: | ||
422 | case 0xBE: { | ||
423 | int temp = READ_MEM( this, RP.hl ); | ||
424 | temp &= ~(1 << (data >> 3 & 7)); | ||
425 | WRITE_MEM( this, RP.hl, temp ); | ||
426 | goto loop; | ||
427 | } | ||
428 | |||
429 | case 0xC6: // SET b,(HL) | ||
430 | case 0xCE: | ||
431 | case 0xD6: | ||
432 | case 0xDE: | ||
433 | case 0xE6: | ||
434 | case 0xEE: | ||
435 | case 0xF6: | ||
436 | case 0xFE: { | ||
437 | int temp = READ_MEM( this, RP.hl ); | ||
438 | temp |= 1 << (data >> 3 & 7); | ||
439 | WRITE_MEM( this, RP.hl, temp ); | ||
440 | goto loop; | ||
441 | } | ||
442 | |||
443 | case 0xC0: case 0xC1: case 0xC2: case 0xC3: // SET b,r | ||
444 | case 0xC4: case 0xC5: case 0xC7: case 0xC8: | ||
445 | case 0xC9: case 0xCA: case 0xCB: case 0xCC: | ||
446 | case 0xCD: case 0xCF: case 0xD0: case 0xD1: | ||
447 | case 0xD2: case 0xD3: case 0xD4: case 0xD5: | ||
448 | case 0xD7: case 0xD8: case 0xD9: case 0xDA: | ||
449 | case 0xDB: case 0xDC: case 0xDD: case 0xDF: | ||
450 | case 0xE0: case 0xE1: case 0xE2: case 0xE3: | ||
451 | case 0xE4: case 0xE5: case 0xE7: case 0xE8: | ||
452 | case 0xE9: case 0xEA: case 0xEB: case 0xEC: | ||
453 | case 0xED: case 0xEF: case 0xF0: case 0xF1: | ||
454 | case 0xF2: case 0xF3: case 0xF4: case 0xF5: | ||
455 | case 0xF7: case 0xF8: case 0xF9: case 0xFA: | ||
456 | case 0xFB: case 0xFC: case 0xFD: case 0xFF: | ||
457 | R8( data & 7 ) |= 1 << (data >> 3 & 7); | ||
458 | goto loop; | ||
459 | |||
460 | case 0x80: case 0x81: case 0x82: case 0x83: // RES b,r | ||
461 | case 0x84: case 0x85: case 0x87: case 0x88: | ||
462 | case 0x89: case 0x8A: case 0x8B: case 0x8C: | ||
463 | case 0x8D: case 0x8F: case 0x90: case 0x91: | ||
464 | case 0x92: case 0x93: case 0x94: case 0x95: | ||
465 | case 0x97: case 0x98: case 0x99: case 0x9A: | ||
466 | case 0x9B: case 0x9C: case 0x9D: case 0x9F: | ||
467 | case 0xA0: case 0xA1: case 0xA2: case 0xA3: | ||
468 | case 0xA4: case 0xA5: case 0xA7: case 0xA8: | ||
469 | case 0xA9: case 0xAA: case 0xAB: case 0xAC: | ||
470 | case 0xAD: case 0xAF: case 0xB0: case 0xB1: | ||
471 | case 0xB2: case 0xB3: case 0xB4: case 0xB5: | ||
472 | case 0xB7: case 0xB8: case 0xB9: case 0xBA: | ||
473 | case 0xBB: case 0xBC: case 0xBD: case 0xBF: | ||
474 | R8( data & 7 ) &= ~(1 << (data >> 3 & 7)); | ||
475 | goto loop; | ||
476 | |||
477 | case 0x36: // SWAP (HL) | ||
478 | op = READ_MEM( this, RP.hl ); | ||
479 | goto swap_comm; | ||
480 | |||
481 | case 0x30: // SWAP B | ||
482 | case 0x31: // SWAP C | ||
483 | case 0x32: // SWAP D | ||
484 | case 0x33: // SWAP E | ||
485 | case 0x34: // SWAP H | ||
486 | case 0x35: // SWAP L | ||
487 | case 0x37: // SWAP A | ||
488 | op = R8( data & 7 ); | ||
489 | swap_comm: | ||
490 | op = (op >> 4) + (op << 4); | ||
491 | cz = BYTE( op ); | ||
492 | ph = cz + 0x100; | ||
493 | if ( data == 0x36 ) | ||
494 | goto write_hl_op_ff; | ||
495 | R8( data & 7 ) = op; | ||
496 | goto loop; | ||
497 | |||
498 | // Shift/Rotate | ||
499 | |||
500 | case 0x26: // SLA (HL) | ||
501 | cz = 0; | ||
502 | case 0x16: // RL (HL) | ||
503 | cz = (cz >> 8 & 1) + (READ_MEM( this, RP.hl ) << 1); | ||
504 | goto rl_hl_common; | ||
505 | |||
506 | case 0x06: // RLC (HL) | ||
507 | cz = READ_MEM( this, RP.hl ); | ||
508 | cz = (cz << 1) + (cz >> 7 & 1); | ||
509 | rl_hl_common: | ||
510 | // Z=* C=* | ||
511 | ph = cz | 0x100; // N=0 H=0 | ||
512 | WRITE_MEM( this, RP.hl, cz ); | ||
513 | goto loop; | ||
514 | |||
515 | case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x27: // SLA r | ||
516 | cz = 0; | ||
517 | case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x17: // RL r | ||
518 | cz = (cz >> 8 & 1) + (R8( data & 7 ) << 1); | ||
519 | goto rl_common; | ||
520 | |||
521 | case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x07: // RLC r | ||
522 | cz = R8( data & 7 ); | ||
523 | cz = (cz << 1) + (cz >> 7 & 1); | ||
524 | rl_common: | ||
525 | // Z=* C=* | ||
526 | ph = cz | 0x100; // N=0 H=0 | ||
527 | R8( data & 7 ) = cz; | ||
528 | goto loop; | ||
529 | |||
530 | case 0x0E: // RRC (HL) | ||
531 | cz = READ_MEM( this, RP.hl ); | ||
532 | cz += cz << 8 & 0x100; | ||
533 | goto rr_hl_common; | ||
534 | |||
535 | case 0x2E: // SRA (HL) | ||
536 | cz = READ_MEM( this, RP.hl ); | ||
537 | cz += cz << 1 & 0x100; | ||
538 | goto rr_hl_common; | ||
539 | |||
540 | case 0x3E: // SRL (HL) | ||
541 | cz = 0; | ||
542 | case 0x1E: // RR (HL) | ||
543 | cz = (cz & 0x100) + READ_MEM( this, RP.hl ); | ||
544 | rr_hl_common: | ||
545 | cz = (cz << 8) + (cz >> 1); // Z=* C=* | ||
546 | ph = cz | 0x100; // N=0 H=0 | ||
547 | WRITE_MEM( this, RP.hl, cz ); | ||
548 | goto loop; | ||
549 | |||
550 | case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0F: // RRC r | ||
551 | cz = R8( data & 7 ); | ||
552 | cz += cz << 8 & 0x100; | ||
553 | goto rr_common; | ||
554 | |||
555 | case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2F: // SRA r | ||
556 | cz = R8( data & 7 ); | ||
557 | cz += cz << 1 & 0x100; | ||
558 | goto rr_common; | ||
559 | |||
560 | case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3F: // SRL r | ||
561 | cz = 0; | ||
562 | case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1F: // RR r | ||
563 | cz = (cz & 0x100) + R8( data & 7 ); | ||
564 | rr_common: | ||
565 | cz = (cz << 8) + (cz >> 1); // Z=* C=* | ||
566 | ph = cz | 0x100; // N=0 H=0 | ||
567 | R8( data & 7 ) = cz; | ||
568 | goto loop; | ||
569 | |||
570 | } // CB op | ||
571 | assert( false ); // unhandled CB op | ||
572 | |||
573 | case 0x07: // RLCA | ||
574 | cz = RG.a >> 7; | ||
575 | goto rlc_common; | ||
576 | case 0x17: // RLA | ||
577 | cz = cz >> 8 & 1; | ||
578 | rlc_common: | ||
579 | cz += RG.a << 1; | ||
580 | ph = cz | 0x100; | ||
581 | RG.a = BYTE( cz ); | ||
582 | cz |= 1; | ||
583 | goto loop; | ||
584 | |||
585 | case 0x0F: // RRCA | ||
586 | ph = RG.a << 8; | ||
587 | goto rrc_common; | ||
588 | case 0x1F: // RRA | ||
589 | ph = cz; | ||
590 | rrc_common: | ||
591 | cz = (RG.a << 8) + 1; // Z=0 C=* | ||
592 | RG.a = ((ph & 0x100) + RG.a) >> 1; | ||
593 | ph = 0x100; // N=0 H=0 | ||
594 | goto loop; | ||
595 | |||
596 | // Load | ||
597 | |||
598 | case 0x70: // LD (HL),B | ||
599 | case 0x71: // LD (HL),C | ||
600 | case 0x72: // LD (HL),D | ||
601 | case 0x73: // LD (HL),E | ||
602 | case 0x74: // LD (HL),H | ||
603 | case 0x75: // LD (HL),L | ||
604 | case 0x77: // LD (HL),A | ||
605 | op = R8( op & 7 ); | ||
606 | write_hl_op_ff: | ||
607 | WRITE_MEM( this, RP.hl, op ); | ||
608 | goto loop; | ||
609 | |||
610 | case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x47: // LD r,r | ||
611 | case 0x48: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4F: | ||
612 | case 0x50: case 0x51: case 0x53: case 0x54: case 0x55: case 0x57: | ||
613 | case 0x58: case 0x59: case 0x5A: case 0x5C: case 0x5D: case 0x5F: | ||
614 | case 0x60: case 0x61: case 0x62: case 0x63: case 0x65: case 0x67: | ||
615 | case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6F: | ||
616 | case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: | ||
617 | R8( op >> 3 & 7 ) = R8( op & 7 ); | ||
618 | goto loop; | ||
619 | |||
620 | case 0x08: // LD IND16,SP | ||
621 | data = GET_ADDR(); | ||
622 | pc += 2; | ||
623 | WRITE_MEM( this, data, sp ); | ||
624 | data++; | ||
625 | WRITE_MEM( this, data, (sp >> 8) ); | ||
626 | goto loop; | ||
627 | |||
628 | case 0xF9: // LD SP,HL | ||
629 | sp = RP.hl; | ||
630 | goto loop; | ||
631 | |||
632 | case 0x31: // LD SP,IMM | ||
633 | sp = GET_ADDR(); | ||
634 | pc += 2; | ||
635 | goto loop; | ||
636 | |||
637 | case 0x01: // LD BC,IMM | ||
638 | case 0x11: // LD DE,IMM | ||
639 | R16( (unsigned) op >> 4 ) = GET_ADDR(); | ||
640 | pc += 2; | ||
641 | goto loop; | ||
642 | |||
643 | case 0xE2: // LD (0xFF00+C),A | ||
644 | WRITE_IO( this, RG.c, RG.a ); | ||
645 | goto loop; | ||
646 | |||
647 | case 0xE0: // LD (0xFF00+imm),A | ||
648 | pc++; | ||
649 | WRITE_IO( this, data, RG.a ); | ||
650 | goto loop; | ||
651 | |||
652 | { | ||
653 | int temp; | ||
654 | case 0x32: // LD (HL-),A | ||
655 | temp = RP.hl; | ||
656 | RP.hl = temp - 1; | ||
657 | goto write_data_rg_a; | ||
658 | |||
659 | case 0x02: // LD (BC),A | ||
660 | temp = RP.bc; | ||
661 | goto write_data_rg_a; | ||
662 | |||
663 | case 0x12: // LD (DE),A | ||
664 | temp = RP.de; | ||
665 | goto write_data_rg_a; | ||
666 | |||
667 | case 0x22: // LD (HL+),A | ||
668 | temp = RP.hl; | ||
669 | RP.hl = temp + 1; | ||
670 | goto write_data_rg_a; | ||
671 | |||
672 | case 0xEA: // LD IND16,A (common) | ||
673 | temp = GET_ADDR(); | ||
674 | pc += 2; | ||
675 | write_data_rg_a: | ||
676 | WRITE_MEM( this, temp, RG.a ); | ||
677 | goto loop; | ||
678 | } | ||
679 | |||
680 | case 0x06: // LD B,IMM | ||
681 | RG.b = data; | ||
682 | pc++; | ||
683 | goto loop; | ||
684 | |||
685 | case 0x0E: // LD C,IMM | ||
686 | RG.c = data; | ||
687 | pc++; | ||
688 | goto loop; | ||
689 | |||
690 | case 0x16: // LD D,IMM | ||
691 | RG.d = data; | ||
692 | pc++; | ||
693 | goto loop; | ||
694 | |||
695 | case 0x1E: // LD E,IMM | ||
696 | RG.e = data; | ||
697 | pc++; | ||
698 | goto loop; | ||
699 | |||
700 | case 0x26: // LD H,IMM | ||
701 | RG.h = data; | ||
702 | pc++; | ||
703 | goto loop; | ||
704 | |||
705 | case 0x2E: // LD L,IMM | ||
706 | RG.l = data; | ||
707 | pc++; | ||
708 | goto loop; | ||
709 | |||
710 | case 0x36: // LD (HL),IMM | ||
711 | WRITE_MEM( this, RP.hl, data ); | ||
712 | pc++; | ||
713 | goto loop; | ||
714 | |||
715 | case 0x3E: // LD A,IMM | ||
716 | RG.a = data; | ||
717 | pc++; | ||
718 | goto loop; | ||
719 | |||
720 | // Increment/decrement | ||
721 | |||
722 | case 0x03: // INC BC | ||
723 | case 0x13: // INC DE | ||
724 | case 0x23: // INC HL | ||
725 | R16( (unsigned) op >> 4 )++; | ||
726 | goto loop; | ||
727 | |||
728 | case 0x33: // INC SP | ||
729 | sp = WORD( sp + 1 ); | ||
730 | goto loop; | ||
731 | |||
732 | case 0x0B: // DEC BC | ||
733 | case 0x1B: // DEC DE | ||
734 | case 0x2B: // DEC HL | ||
735 | R16( (unsigned) op >> 4 )--; | ||
736 | goto loop; | ||
737 | |||
738 | case 0x3B: // DEC SP | ||
739 | sp = WORD( sp - 1 ); | ||
740 | goto loop; | ||
741 | |||
742 | case 0x34: // INC (HL) | ||
743 | op = RP.hl; | ||
744 | data = READ_MEM( this, op ); | ||
745 | data++; | ||
746 | WRITE_MEM( this, op, data ); | ||
747 | goto inc_comm; | ||
748 | |||
749 | case 0x04: // INC B | ||
750 | case 0x0C: // INC C (common) | ||
751 | case 0x14: // INC D | ||
752 | case 0x1C: // INC E | ||
753 | case 0x24: // INC H | ||
754 | case 0x2C: // INC L | ||
755 | case 0x3C: // INC A | ||
756 | op = op >> 3 & 7; | ||
757 | data = R8( op ) + 1; | ||
758 | R8( op ) = data; | ||
759 | inc_comm: | ||
760 | ph = data - 0x101; // N=0 H=* | ||
761 | cz = (cz & 0x100) + BYTE( data ); // C=- Z=* | ||
762 | goto loop; | ||
763 | |||
764 | case 0x35: // DEC (HL) | ||
765 | op = RP.hl; | ||
766 | data = READ_MEM( this, op ); | ||
767 | data--; | ||
768 | WRITE_MEM( this, op, data ); | ||
769 | goto dec_comm; | ||
770 | |||
771 | case 0x05: // DEC B | ||
772 | case 0x0D: // DEC C | ||
773 | case 0x15: // DEC D | ||
774 | case 0x1D: // DEC E | ||
775 | case 0x25: // DEC H | ||
776 | case 0x2D: // DEC L | ||
777 | case 0x3D: // DEC A | ||
778 | op = op >> 3 & 7; | ||
779 | data = R8( op ) - 1; | ||
780 | R8( op ) = data; | ||
781 | dec_comm: | ||
782 | ph = data + 1; // N=1 H=* | ||
783 | cz = (cz & 0x100) + BYTE( data ); // C=- Z=* | ||
784 | goto loop; | ||
785 | |||
786 | // Add 16-bit | ||
787 | |||
788 | case 0xF8: // LD HL,SP+n | ||
789 | case 0xE8:{// ADD SP,n | ||
790 | pc++; | ||
791 | int t = WORD( sp + SBYTE( data ) ); | ||
792 | cz = ((BYTE( sp ) + data) & 0x100) + 1; // Z=0 C=* | ||
793 | ph = (sp ^ data ^ t) | 0x100; // N=0 H=* | ||
794 | if ( op == 0xF8 ) | ||
795 | { | ||
796 | RP.hl = t; | ||
797 | goto loop; | ||
798 | } | ||
799 | sp = t; | ||
800 | goto loop; | ||
801 | } | ||
802 | |||
803 | case 0x39: // ADD HL,SP | ||
804 | data = sp; | ||
805 | goto add_hl_comm; | ||
806 | |||
807 | case 0x09: // ADD HL,BC | ||
808 | case 0x19: // ADD HL,DE | ||
809 | case 0x29: // ADD HL,HL | ||
810 | data = R16( (unsigned) op >> 4 ); | ||
811 | add_hl_comm: | ||
812 | ph = RP.hl ^ data; | ||
813 | data += RP.hl; | ||
814 | RP.hl = WORD( data ); | ||
815 | ph ^= data; | ||
816 | cz = BYTE( cz ) + (data >> 8 & 0x100); // C=* Z=- | ||
817 | ph = ((ph >> 8) ^ cz) | 0x100; // N=0 H=* | ||
818 | goto loop; | ||
819 | |||
820 | case 0x86: // ADD (HL) | ||
821 | data = READ_MEM( this, RP.hl ); | ||
822 | goto add_comm; | ||
823 | |||
824 | case 0x80: // ADD B | ||
825 | case 0x81: // ADD C | ||
826 | case 0x82: // ADD D | ||
827 | case 0x83: // ADD E | ||
828 | case 0x84: // ADD H | ||
829 | case 0x85: // ADD L | ||
830 | case 0x87: // ADD A | ||
831 | data = R8( op & 7 ); | ||
832 | goto add_comm; | ||
833 | |||
834 | case 0xC6: // ADD IMM | ||
835 | pc++; | ||
836 | add_comm: | ||
837 | ph = (RG.a ^ data) | 0x100; // N=1 H=* | ||
838 | cz = RG.a + data; // C=* Z=* | ||
839 | RG.a = cz; | ||
840 | goto loop; | ||
841 | |||
842 | // Add/Subtract | ||
843 | |||
844 | case 0x8E: // ADC (HL) | ||
845 | data = READ_MEM( this, RP.hl ); | ||
846 | goto adc_comm; | ||
847 | |||
848 | case 0x88: // ADC B | ||
849 | case 0x89: // ADC C | ||
850 | case 0x8A: // ADC D | ||
851 | case 0x8B: // ADC E | ||
852 | case 0x8C: // ADC H | ||
853 | case 0x8D: // ADC L | ||
854 | case 0x8F: // ADC A | ||
855 | data = R8( op & 7 ); | ||
856 | goto adc_comm; | ||
857 | |||
858 | case 0xCE: // ADC IMM | ||
859 | pc++; | ||
860 | adc_comm: | ||
861 | ph = (RG.a ^ data) | 0x100; // N=1 H=* | ||
862 | cz = RG.a + data + (cz >> 8 & 1); // C=* Z=* | ||
863 | RG.a = cz; | ||
864 | goto loop; | ||
865 | |||
866 | case 0x96: // SUB (HL) | ||
867 | data = READ_MEM( this, RP.hl ); | ||
868 | goto sub_comm; | ||
869 | |||
870 | case 0x90: // SUB B | ||
871 | case 0x91: // SUB C | ||
872 | case 0x92: // SUB D | ||
873 | case 0x93: // SUB E | ||
874 | case 0x94: // SUB H | ||
875 | case 0x95: // SUB L | ||
876 | case 0x97: // SUB A | ||
877 | data = R8( op & 7 ); | ||
878 | goto sub_comm; | ||
879 | |||
880 | case 0xD6: // SUB IMM | ||
881 | pc++; | ||
882 | sub_comm: | ||
883 | ph = RG.a ^ data; // N=1 H=* | ||
884 | cz = RG.a - data; // C=* Z=* | ||
885 | RG.a = cz; | ||
886 | goto loop; | ||
887 | |||
888 | case 0x9E: // SBC (HL) | ||
889 | data = READ_MEM( this, RP.hl ); | ||
890 | goto sbc_comm; | ||
891 | |||
892 | case 0x98: // SBC B | ||
893 | case 0x99: // SBC C | ||
894 | case 0x9A: // SBC D | ||
895 | case 0x9B: // SBC E | ||
896 | case 0x9C: // SBC H | ||
897 | case 0x9D: // SBC L | ||
898 | case 0x9F: // SBC A | ||
899 | data = R8( op & 7 ); | ||
900 | goto sbc_comm; | ||
901 | |||
902 | case 0xDE: // SBC IMM | ||
903 | pc++; | ||
904 | sbc_comm: | ||
905 | ph = RG.a ^ data; // N=1 H=* | ||
906 | cz = RG.a - data - (cz >> 8 & 1); // C=* Z=* | ||
907 | RG.a = cz; | ||
908 | goto loop; | ||
909 | |||
910 | // Logical | ||
911 | |||
912 | case 0xA0: // AND B | ||
913 | case 0xA1: // AND C | ||
914 | case 0xA2: // AND D | ||
915 | case 0xA3: // AND E | ||
916 | case 0xA4: // AND H | ||
917 | case 0xA5: // AND L | ||
918 | data = R8( op & 7 ); | ||
919 | goto and_comm; | ||
920 | |||
921 | case 0xA6: // AND (HL) | ||
922 | data = READ_MEM( this, RP.hl ); | ||
923 | goto and_comm; | ||
924 | case 0xE6: // AND IMM | ||
925 | pc++; | ||
926 | and_comm: | ||
927 | cz = RG.a & data; // C=0 Z=* | ||
928 | ph = ~cz; // N=0 H=1 | ||
929 | RG.a = cz; | ||
930 | goto loop; | ||
931 | |||
932 | case 0xA7: // AND A | ||
933 | cz = RG.a; // C=0 Z=* | ||
934 | ph = ~RG.a; // N=0 H=1 | ||
935 | goto loop; | ||
936 | |||
937 | case 0xB0: // OR B | ||
938 | case 0xB1: // OR C | ||
939 | case 0xB2: // OR D | ||
940 | case 0xB3: // OR E | ||
941 | case 0xB4: // OR H | ||
942 | case 0xB5: // OR L | ||
943 | data = R8( op & 7 ); | ||
944 | goto or_comm; | ||
945 | |||
946 | case 0xB6: // OR (HL) | ||
947 | data = READ_MEM( this, RP.hl ); | ||
948 | goto or_comm; | ||
949 | case 0xF6: // OR IMM | ||
950 | pc++; | ||
951 | or_comm: | ||
952 | cz = RG.a | data; // C=0 Z=* | ||
953 | ph = cz | 0x100; // N=0 H=0 | ||
954 | RG.a = cz; | ||
955 | goto loop; | ||
956 | |||
957 | case 0xB7: // OR A | ||
958 | cz = RG.a; // C=0 Z=* | ||
959 | ph = RG.a + 0x100; // N=0 H=0 | ||
960 | goto loop; | ||
961 | |||
962 | case 0xA8: // XOR B | ||
963 | case 0xA9: // XOR C | ||
964 | case 0xAA: // XOR D | ||
965 | case 0xAB: // XOR E | ||
966 | case 0xAC: // XOR H | ||
967 | case 0xAD: // XOR L | ||
968 | data = R8( op & 7 ); | ||
969 | goto xor_comm; | ||
970 | |||
971 | case 0xAE: // XOR (HL) | ||
972 | data = READ_MEM( this, RP.hl ); | ||
973 | pc--; | ||
974 | case 0xEE: // XOR IMM | ||
975 | pc++; | ||
976 | xor_comm: | ||
977 | cz = RG.a ^ data; // C=0 Z=* | ||
978 | ph = cz + 0x100; // N=0 H=0 | ||
979 | RG.a = cz; | ||
980 | goto loop; | ||
981 | |||
982 | case 0xAF: // XOR A | ||
983 | RG.a = 0; | ||
984 | cz = 0; // C=0 Z=* | ||
985 | ph = 0x100; // N=0 H=0 | ||
986 | goto loop; | ||
987 | |||
988 | // Stack | ||
989 | |||
990 | case 0xF1: // POP AF | ||
991 | case 0xC1: // POP BC | ||
992 | case 0xD1: // POP DE | ||
993 | case 0xE1: // POP HL (common) | ||
994 | data = READ_MEM( this, sp ); | ||
995 | R16( op >> 4 & 3 ) = data + 0x100 * READ_MEM( this, (sp + 1) ); | ||
996 | sp = WORD( sp + 2 ); | ||
997 | if ( op != 0xF1 ) | ||
998 | goto loop; | ||
999 | |||
1000 | SET_FLAGS( RG.a ); | ||
1001 | RG.a = RG.flags; | ||
1002 | goto loop; | ||
1003 | |||
1004 | case 0xC5: // PUSH BC | ||
1005 | data = RP.bc; | ||
1006 | goto push; | ||
1007 | |||
1008 | case 0xD5: // PUSH DE | ||
1009 | data = RP.de; | ||
1010 | goto push; | ||
1011 | |||
1012 | case 0xE5: // PUSH HL | ||
1013 | data = RP.hl; | ||
1014 | goto push; | ||
1015 | |||
1016 | case 0xF5: // PUSH AF | ||
1017 | GET_FLAGS( data ); | ||
1018 | data += RG.a << 8; | ||
1019 | goto push; | ||
1020 | |||
1021 | // Flow control | ||
1022 | |||
1023 | case 0xFF: case 0xC7: case 0xCF: case 0xD7: // RST | ||
1024 | case 0xDF: case 0xE7: case 0xEF: case 0xF7: | ||
1025 | data = pc; | ||
1026 | pc = (op & 0x38) + cpu->rst_base; | ||
1027 | goto push; | ||
1028 | |||
1029 | case 0xCC: // CALL Z | ||
1030 | pc += 2; | ||
1031 | if ( CC_Z() ) | ||
1032 | goto call; | ||
1033 | goto loop; | ||
1034 | |||
1035 | case 0xD4: // CALL NC | ||
1036 | pc += 2; | ||
1037 | if ( CC_NC() ) | ||
1038 | goto call; | ||
1039 | goto loop; | ||
1040 | |||
1041 | case 0xDC: // CALL C | ||
1042 | pc += 2; | ||
1043 | if ( CC_C() ) | ||
1044 | goto call; | ||
1045 | goto loop; | ||
1046 | |||
1047 | case 0xC0: // RET NZ | ||
1048 | if ( CC_NZ() ) | ||
1049 | goto ret; | ||
1050 | goto loop; | ||
1051 | |||
1052 | case 0xD0: // RET NC | ||
1053 | if ( CC_NC() ) | ||
1054 | goto ret; | ||
1055 | goto loop; | ||
1056 | |||
1057 | case 0xD8: // RET C | ||
1058 | if ( CC_C() ) | ||
1059 | goto ret; | ||
1060 | goto loop; | ||
1061 | |||
1062 | case 0x18: // JR | ||
1063 | BRANCH_( true, 0 ) | ||
1064 | |||
1065 | case 0x30: // JR NC | ||
1066 | BRANCH( CC_NC() ) | ||
1067 | |||
1068 | case 0x38: // JR C | ||
1069 | BRANCH( CC_C() ) | ||
1070 | |||
1071 | case 0xE9: // LD PC,HL | ||
1072 | pc = RP.hl; | ||
1073 | goto loop; | ||
1074 | |||
1075 | case 0xC3: // JP (next-most-common) | ||
1076 | pc = GET_ADDR(); | ||
1077 | goto loop; | ||
1078 | |||
1079 | case 0xC2: // JP NZ | ||
1080 | pc += 2; | ||
1081 | if ( CC_NZ() ) | ||
1082 | goto jp_taken; | ||
1083 | time -= 4; | ||
1084 | goto loop; | ||
1085 | |||
1086 | case 0xCA: // JP Z (most common) | ||
1087 | pc += 2; | ||
1088 | if ( CC_Z() ) | ||
1089 | goto jp_taken; | ||
1090 | time -= 4; | ||
1091 | goto loop; | ||
1092 | |||
1093 | jp_taken: | ||
1094 | pc -= 2; | ||
1095 | pc = GET_ADDR(); | ||
1096 | goto loop; | ||
1097 | |||
1098 | case 0xD2: // JP NC | ||
1099 | pc += 2; | ||
1100 | if ( CC_NC() ) | ||
1101 | goto jp_taken; | ||
1102 | time -= 4; | ||
1103 | goto loop; | ||
1104 | |||
1105 | case 0xDA: // JP C | ||
1106 | pc += 2; | ||
1107 | if ( CC_C() ) | ||
1108 | goto jp_taken; | ||
1109 | time -= 4; | ||
1110 | goto loop; | ||
1111 | |||
1112 | // Flags | ||
1113 | |||
1114 | case 0x2F: // CPL | ||
1115 | RG.a = ~RG.a; | ||
1116 | ph = BYTE( ~cz ); // N=1 H=1 | ||
1117 | goto loop; | ||
1118 | |||
1119 | case 0x3F: // CCF | ||
1120 | ph = cz | 0x100; // N=0 H=0 | ||
1121 | cz ^= 0x100; // C=* Z=- | ||
1122 | goto loop; | ||
1123 | |||
1124 | case 0x37: // SCF | ||
1125 | ph = cz | 0x100; // N=0 H=0 | ||
1126 | cz |= 0x100; // C=1 Z=- | ||
1127 | goto loop; | ||
1128 | |||
1129 | case 0xF3: // DI | ||
1130 | goto loop; | ||
1131 | |||
1132 | case 0xFB: // EI | ||
1133 | goto loop; | ||
1134 | |||
1135 | case 0x27:{// DAA | ||
1136 | unsigned a = RG.a; | ||
1137 | int h = ph ^ cz; | ||
1138 | if ( ph & 0x100 ) | ||
1139 | { | ||
1140 | if ( (h & 0x10) || (a & 0x0F) > 9 ) | ||
1141 | a += 6; | ||
1142 | |||
1143 | if ( (cz & 0x100) || a > 0x9F ) | ||
1144 | a += 0x60; | ||
1145 | } | ||
1146 | else | ||
1147 | { | ||
1148 | if ( h & 0x10 ) | ||
1149 | a = (a - 6) & 0xFF; | ||
1150 | |||
1151 | if ( cz & 0x100 ) | ||
1152 | a -= 0x60; | ||
1153 | } | ||
1154 | cz = (cz & 0x100) | a; // C=- Z=* | ||
1155 | RG.a = a; | ||
1156 | ph = (ph & 0x100) + BYTE( a ); // N=- H=0 | ||
1157 | goto loop; | ||
1158 | } | ||
1159 | |||
1160 | // Special | ||
1161 | |||
1162 | case 0x76: // HALT | ||
1163 | case 0x10: // STOP | ||
1164 | case 0xD3: case 0xDB: case 0xDD: // Illegal | ||
1165 | case 0xE3: case 0xE4: case 0xEB: case 0xEC: case 0xED: // (all freeze cpu) | ||
1166 | case 0xF4: case 0xFC: case 0xFD: | ||
1167 | goto stop; | ||
1168 | } | ||
1169 | |||
1170 | // If this fails then an opcode isn't handled above | ||
1171 | assert( false ); | ||
1172 | |||
1173 | stop: | ||
1174 | pc--; | ||
1175 | |||
1176 | // copy state back | ||
1177 | cpu->cpu_state_.time = time; | ||
1178 | cpu->r.pc = pc; | ||
1179 | cpu->r.sp = sp; | ||
1180 | { | ||
1181 | int t; | ||
1182 | GET_FLAGS( t ); | ||
1183 | RG.flags = t; | ||
1184 | } | ||
1185 | cpu->cpu_state = &cpu->cpu_state_; | ||
1186 | cpu->r.rp = RP; | ||
1187 | } | ||