1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "ticks.h"
7 
8 #if defined(_WIN32) || defined(WIN32)
9 #ifndef strcasecmp
10 #define strcasecmp(a,b) stricmp(a,b)
11 #endif
12 #endif
13 
14 
15 
16 
17 // fr = zero, ff&256 = carry, ff&128 = s/p
18 
19 // TODO: Setting P flag
20 #define BOOL(hi, lo, hia, loa, isalt) do {                \
21           if ( isalt ) {                  \
22             loa = fr_ = hi|lo ? 1 : 0; hia = 0; ff_ &= ~256; \
23           } else {                       \
24             lo = fr = hi|lo ? 1 : 0; hi = 0; ff &= ~256; \
25           }                              \
26         } while (0)
27 
28 #define LDRIM(r)                \
29           st += isez80() ? 2 : israbbit() ? 4 : isz180() ? 6 : isgbz80() ? 8 :  7, \
30           r= get_memory(pc++)
31 
32 #define LDRRIM(a, b)            \
33           st += isez80() ? 3 : israbbit() ? 6 : isz180() ? 9 : isgbz80() ? 12 : 10, \
34           b= get_memory(pc++),         \
35           a= get_memory(pc++)
36 
37 #define LDRP(a, b, r)           \
38           st += isez80() ? 2 : israbbit() ? (&a == &h) ? 5 : 6 : isgbz80() ? 8 : isz180() ? 6 : 7, \
39           r= get_memory(mp= b|a<<8),   \
40           ++mp
41 
42 #define LDRPI(a, b, r)          \
43           st += isez80() ? 3 : israbbit() ? 7 : isz180() ? 11 : 15, \
44           r= get_memory(((get_memory(pc++)^128)-128+(b|a<<8))&65535)
45 
46 #define LEA(r1, r2, s1, s2, t) do { \
47     uint16_t offs = ((get_memory(pc++)^128)-128+(s1|s2<<8))&65535; \
48     r2 = get_memory(offs); \
49     r1 = get_memory(offs+1); \
50     st += t; \
51 } while (0)
52 
53 #define LDPR(a, b, r)           \
54           st += isez80() ? 2 : israbbit() ? (&a == &h) ? 6 : 7 : isgbz80() ? 8 : 7, \
55           put_memory(b|a<<8,r),       \
56           mp= b+1&255 | a<<8
57 
58 #define LDPRI(a, b, r)          \
59           st += isez80() ? 3 : israbbit() ? 8 : isz180() ? 12 : 15, \
60           put_memory(((get_memory(pc++)^128)-128+(b|a<<8))&65535,r)
61 
62 // ld r,r'
63 #define LDRR(dr, sr, dr_, n) do {          \
64             st+= n;               \
65             if ( altd ) dr_ = sr; \
66             else dr = sr; \
67           } while(0)
68 
69 #define LDPNNRR(a, b, n)        \
70           st+= n,               \
71           t= get_memory(pc++),         \
72           put_memory(t|= get_memory(pc++)<<8, b), \
73           put_memory(mp= t+1,a)
74 
75 #define LDPIN(a, b)             \
76           st+= isez80() ? 4 : israbbit() ? 9 : isz180() ? 12 : 15,              \
77           t= get_memory(pc++),         \
78           put_memory(((t^128)-128+(b|a<<8))&65535, get_memory(pc++))
79 
80 #define INCW(a, b)              \
81           st += isez80() ? 1 : israbbit() ? 2 : isgbz80() ? 8 : is8080() ? 5 : 6, \
82           ++b || a++
83 
84 #define DECW(a, b)              \
85           st += isez80() ? 1 : israbbit() ? 2 : isgbz80() ? 8 : is8080() ? 5 : 6, \
86           b-- || a--
87 
88 // TODO: Should affect alternate flags if altd
89 #define INC(r)                  \
90           st +=isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4, \
91           ff= ff&256            \
92             | (fr= r= (fa= r)+(fb= 1))
93 
94 // TODO: Should affect alternate flags if altd
95 #define DEC(r)                  \
96           st +=isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4, \
97           ff= ff&256            \
98             | (fr= r= (fa= r)+(fb= -1))
99 
100 // TODO: Should affect alternate flags if altd
101 #define INCPI(a, b)             \
102           st +=isez80() ? 5 : israbbit() ? 12 : 19, \
103           fa= get_memory(t= (get_memory(pc++)^128)-128+(b|a<<8)), \
104           ff= ff&256            \
105             | (fr= put_memory(t,fa+(fb=1)))
106 
107 // TODO: Should affect alternate flags if altd
108 #define DECPI(a, b)             \
109           st +=isez80() ? 5 : israbbit() ? 12 : 19, \
110           fa= get_memory(t= (get_memory(pc++)^128)-128+(b|a<<8)), \
111           ff= ff&256            \
112             | (fr= put_memory(t,fa+(fb=-1)))
113 
114 #define ADDRRRR(a, b, c, d)     \
115           st+= isez80() ? 1 :israbbit() ? 2 : is808x() ? 10 : isgbz80() ? 8 : 11,              \
116           v= b+d+               \
117            ( (a+c) << 8 ),        \
118           ff= (ff    & 128)       \
119             | (v>>8  & 296),      \
120           fb= (fb&128)            \
121             | ((v>>8^a^c^fr^fa)&16),  \
122           mp= b+1+( a<<8 ),     \
123           a= v>>8,              \
124           b= v
125 
126 #define ADDRRRR_ALTD(a, b, c, d, dh, dl)     \
127           st+= israbbit() ? 2 :11,              \
128           v= b+d+               \
129            ( (a+c) << 8 ),        \
130           ff_= (ff_    & 128)       \
131             | ((v>>8)  & 296),      \
132           fb_= (fb_&128)            \
133             | ((v>>8^a^c^fr_^fa_)&16),  \
134           mp= b+1+( a<<8 ),     \
135           dh= v>>8,              \
136           dl= v
137 
138 #define JRCI(c)                 \
139           if(c)                 \
140             st+= isez80() ? 3 : isgbz80() ? 8 : isz180() ? 8 : 12,            \
141             pc+= (get_memory(pc)^128)-127; \
142           else                  \
143             st+= isez80() ? 2 : isgbz80() ? 8 : isz180() ? 6 : 7,             \
144             pc++
145 
146 #define JRC(c)                  \
147           if(c)                 \
148             st+= isez80() ? 2 : isgbz80() ? 8 : isz180() ? 6 : 7,             \
149             pc++;               \
150           else                  \
151             st+= isez80() ? 3 : isgbz80() ? 8 : isz180() ? 8 : 12,            \
152             pc+= (get_memory(pc)^128)-127
153 
154 #define LDRRPNN(a, b, n)        \
155           st+= n,               \
156           t= get_memory(pc++),         \
157           b= get_memory(t|= get_memory(pc++)<<8), \
158           a= get_memory(mp= t+1)
159 
160 #define ADDISP(a, b)            \
161           st+= isez80() ? 1 : is808x() ? 10 : isgbz80() ? 8 : 11,              \
162           v= sp+(b|a<<8),       \
163           ff= ff  &128          \
164             | v>>8&296,         \
165           fb= fb&128            \
166             | (v>>8^sp>>8^a^fr^fa)&16, \
167           mp= b+1+(a<<8),       \
168           a= v>>8,              \
169           b= v
170 
171 #define ADDISP_ALTD(a, b, dh, dl)            \
172           st+= 11,              \
173           v= sp+(b|a<<8),       \
174           ff_= ff_  &128          \
175             | v>>8&296,         \
176           fb_= fb_&128            \
177             | (v>>8^sp>>8^a^fr_^fa_)&16, \
178           mp= b+1+(a<<8),       \
179           dh= v>>8,              \
180           dl= v
181 
182 #define ADD(b, n)  do {        \
183           st+= n;               \
184           if ( altd ) fr_= a_= (ff_= (fa_= a)+(fb_= b)); \
185           else fr= a= (ff= (fa= a)+(fb= b)); \
186       } while (0)
187 
188 #define ADC(b, n)  do {         \
189           st+= n;               \
190           if ( altd ) fr_= a_= (ff_= (fa_= a)+(fb_= b)+(ff_>>8&1)); \
191           fr= a= (ff= (fa= a)+(fb= b)+(ff>>8&1)); \
192         } while (0)
193 
194 #define SUB(b, n)  do {         \
195           st+= n;               \
196           if ( altd ) fr_= a_= (ff_= (fa_= a)+(fb_= ~b)+1); \
197           else fr= a= (ff= (fa= a)+(fb= ~b)+1); \
198         } while (0)
199 
200 #define SBC(b, n) do {             \
201           st+= n;               \
202           if ( altd ) fr_= a_= (ff_= (fa_= a)+(fb_= ~b)+(ff_>>8&1^1)); \
203           else fr= a= (ff= (fa= a)+(fb= ~b)+(ff>>8&1^1)); \
204         } while (0)
205 
206 #define AND(b, n) do {          \
207           st+= n;               \
208           if ( altd ) { fa_= ~(a_= ff_= fr_= a&b); fb_= 0;} \
209           else { fa= ~(a= ff= fr= a&b);  fb= 0; } \
210       } while (0)
211 
212 // TODO: Flags not right
213 #define AND2(r1, r2, ra) do {            \
214           if ( altd ) {                  \
215             fa_= ~(ra= ff_= fr_= r1&r2); \
216             fb_= 0;                      \
217           } else {                       \
218             fa= ~(r1= ff= fr= r1&r2);    \
219             fb= 0;                       \
220           }                              \
221         } while (0)
222 
223 
224 #define XOR(b, n) do {               \
225           st+= n;                    \
226           if ( altd ) {              \
227             fa_= 256                 \
228               | (ff_= fr_= a_= a^b); \
229             fb_= 0;                  \
230           } else {                   \
231             fa= 256                  \
232               | (ff= fr= a^= b);     \
233             fb= 0;                   \
234           }                          \
235         } while (0)
236 
237 #define OR(b, n) do {                  \
238           st += n;                     \
239           if ( altd ) {                \
240             fa_= 256                   \
241               | (ff_= fr_= a_ = a|b);  \
242             fb_= 0;                    \
243           } else {                     \
244             fa= 256                    \
245               | (ff= fr= a|= b);       \
246             fb= 0;                     \
247           } \
248         } while (0)
249 
250 // TODO: Flags not right
251 #define OR2(r1, r2)             \
252           fa= 256               \
253             | (ff= fr= r1|= r2),  \
254           fb= 0
255 
256 #define CP(b, n)                \
257           st+= n,               \
258           fr= (fa= a)-b,        \
259           fb= ~b,               \
260           ff= fr  & -41         \
261             | b   &  40,        \
262           fr&= 255
263 
264 #define RET(n)                  \
265           st+= (n),             \
266           mp= get_memory(sp++), \
267           pc= mp|= get_memory(sp++)<<8
268 
269 #define RETC(c)                 \
270           if(c)                 \
271             st+= isez80() ? 2 : israbbit() ? 2 : is8080() ?  5 : is8085() ?  6 : isgbz80() ? 8 :  5;             \
272           else                  \
273             st+= isez80() ? 6 : israbbit() ? 8 : is8080() ? 11 : is8085() ? 12 : isgbz80() ? 8 : isz180() ? 10 : 11,            \
274             mp= get_memory(sp++),      \
275             pc= mp|= get_memory(sp++)<<8
276 
277 #define RETCI(c)                \
278           if(c)                 \
279             st+= isez80() ? 6 : israbbit() ? 8 : is8080() ? 11 : is8085() ? 12 : isgbz80() ? 8 : isz180() ? 10 : 11,            \
280             mp= get_memory(sp++),      \
281             pc= mp|= get_memory(sp++)<<8; \
282           else                  \
283             st+= isez80() ? 2 : israbbit() ? 2 : is8080() ?  5 : is8085() ?  6 : isgbz80() ? 8 :  5
284 
285 #define PUSH(a, b)              \
286           st+= isez80() ? 3 :israbbit() ? 10 : is8085() ? 12 : isgbz80() ? 16 : 11,              \
287           put_memory(--sp,a),   \
288           put_memory(--sp,b)
289 
290 #define POP(a, b)               \
291           st+= isez80() ? 3 : israbbit() ? 7 : isz180() ? 9 : isgbz80() ? 12 : 10,              \
292           b= get_memory(sp++),  \
293           a= get_memory(sp++)
294 
295 #define JPC(c)                  \
296           st+= isez80() ? 3 : israbbit() ? 7 : isz180() ? 6 : is8085() ? 7 : isgbz80() ? 12 : 10;              \
297           if(c)                 \
298             pc+= 2;             \
299           else                  \
300             st += isez80() ? 1 : isz180() ? 3 : is8085() ? 3 : 0,  \
301             pc= get_memory(pc) | get_memory(pc+1)<<8
302 
303 #define JPCI(c)                 \
304           st+= isez80() ? 3 : israbbit() ? 7 : isz180() ? 6 : is8085() ? 7 : isgbz80() ? 12 : 10;              \
305           if(c)                 \
306             st += isez80() ? 1 : isz180() ? 3 : is8085() ? 3 : 0,  \
307             pc= get_memory(pc) | get_memory(pc+1)<<8; \
308           else                  \
309             pc+= 2
310 
311 #define CALLC(c)                \
312           if(c)                 \
313             st+= isez80() ? 3 : isz180() ? 6 : is8085() ? 9 : is8080() ? 11 : isgbz80() ? 12 :10,            \
314             pc+= 2;             \
315           else                  \
316             st+= isez80() ? 6 : isz180() ? 16 : is8085() ? 18 : isgbz80() ? 12 : 17,            \
317             t= pc+2,            \
318             mp= pc= get_memory(pc) | get_memory(pc+1)<<8, \
319             put_memory(--sp,t>>8),    \
320             put_memory(--sp,t)
321 
322 #define CALLCI(c)               \
323           if(c)                 \
324             st+= isez80() ? 6 : isz180() ? 16 : is8085() ? 18 : isgbz80() ? 12 : 17,            \
325             t= pc+2,            \
326             mp= pc= get_memory(pc) | get_memory(pc+1)<<8, \
327             put_memory(--sp,t>>8),    \
328             put_memory(--sp,t);    \
329           else                  \
330             st+= isez80() ? 3 : isz180() ? 6 : is8085() ? 9 : is8080() ? 11 : isgbz80() ? 12 : 10,            \
331             pc+= 2
332 
333 #define RST(n)                  \
334           st+= isez80() ? 5 :israbbit() ? 8 : is8085() ? 12 : isgbz80() ? 32 : 11,              \
335           put_memory(--sp,pc>>8),     \
336           put_memory(--sp,pc),        \
337           mp= pc= n
338 
339 #define EXSPI(a, b)             \
340           st+= isez80() ? 5 : israbbit() ? 13 : isz180() ? 16 : is8080() ? 18 : is8085() ? 16 : 19, \
341           t= get_memory(sp),    \
342           put_memory(sp++,b),   \
343           b= t,                 \
344           t= get_memory(sp),    \
345           put_memory(sp--,a),   \
346           a= t,                 \
347           mp= b | a<<8
348 
349 #define RLC(r)                  \
350           st+= isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
351           ff= r*257>>7,         \
352           fa= 256               \
353             | (fr= r= ff),      \
354           fb= 0
355 
356 #define RRC(r)                  \
357           st+= isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
358           ff=  r >> 1           \
359               | ((r&1)+1 ^ 1)<<7, \
360           fa= 256               \
361             | (fr= r= ff),      \
362           fb= 0
363 
364 #define RL(r)                   \
365           st+= isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
366           ff= r << 1            \
367             | ff  >> 8 & 1,     \
368           fa= 256               \
369             | (fr= r= ff),      \
370           fb= 0
371 
372 #define RR(r)                   \
373           st+=isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
374           ff= (r*513 | ff&256)>>1, \
375           fa= 256               \
376             | (fr= r= ff),      \
377           fb= 0
378 
379 #define SLA(r)                  \
380           st+=isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
381           ff= r<<1,             \
382           fa= 256               \
383             | (fr= r= ff),      \
384           fb= 0
385 
386 #define SRA(r)                  \
387           st+=isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
388           ff= (r*513+128^128)>>1, \
389           fa= 256               \
390             | (fr= r= ff),      \
391           fb= 0
392 
393 #define SLL(r)                  \
394           if ( cansll() )       \
395             st+= 8,             \
396             ff= r<<1 | 1,       \
397             fa= 256             \
398               | (fr= r= ff),    \
399             fb= 0
400 
401 #define SWAP(r)                 \
402           r = (( r & 0xf0) >> 4) | (( r & 0x0f) << 4);  \
403           st += 8
404 
405 #define SRL(r)                  \
406           st+=isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8,               \
407           ff= r*513 >> 1,       \
408           fa= 256               \
409             | (fr= r= ff),      \
410           fb= 0
411 
412 #define BIT(n, r)               \
413           st += isez80() ? 2 : israbbit() ? 4 : isz180() ? 6 : 8, \
414           ff= ff  & -256        \
415             | r   &   40        \
416             | (fr= r & n),      \
417           fa= ~fr,              \
418           fb= 0
419 
420 #define BITHL(n)                \
421           st += isez80() ? 3 : israbbit() ? 7 : isz180() ? 9 : isgbz80() ? 16 : 12, \
422           t = get_memory(l | h<<8),     \
423           ff= ff    & -256      \
424             | mp>>8 &   40      \
425             | -41   & (t&= n),  \
426           fa= ~(fr= t),         \
427           fb= 0
428 
429 // 11T has already been added + index flag
430 #define BITI(n) do {               \
431           st += isez80() ? -7 :israbbit() ? -1 : isz180() ? 4 : 5; \
432           if ( altd ) {           \
433             ff_= ff_    & -256    \
434               | mp>>8 &   40      \
435               | -41   & (t&= n);  \
436             fa_= ~(fr_= t);       \
437             fb_= 0;               \
438           } else {                \
439             ff= ff    & -256      \
440               | mp>>8 &   40      \
441               | -41   & (t&= n);  \
442             fa= ~(fr= t);         \
443             fb= 0;                \
444           }                       \
445         } while ( 0 )
446 
447 #define RES(n, r)               \
448           st += isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8, \
449           r&= n
450 
451 #define RESHL(n)                \
452           st += isez80() ? 3 : israbbit() ? 10 : isz180() ? 13 : isgbz80() ? 16 : 15, \
453           t = l|h<<8, \
454           put_memory(t, get_memory(t) & n)
455 
456 #define SET(n, r)               \
457           st += isez80() ? 2 : israbbit() ? 4 : isz180() ? 7 : 8, \
458           r|= n
459 
460 #define SETHL(n)                \
461           st += isez80() ? 3 : israbbit() ? 10 : isz180() ? 13 : isgbz80() ? 16 : 15, \
462           t = l|h<<8, \
463           put_memory(t, get_memory(t) | n)
464 
465 #define INR(r)                  \
466           st+= 12,              \
467           r= in(mp= b<<8 | c),  \
468           ++mp,                 \
469           ff= ff & -256         \
470             | (fr= r),          \
471           fa= r | 256,          \
472           fb= 0
473 
474 #define OUTR(r)                 \
475           st+= 12,              \
476           out(mp= c | b<<8, r), \
477           ++mp
478 
479 #define SBCHLRR(a, b)           \
480           st += isez80() ? 2 : israbbit() ? 4 : isz180() ? 10 : 15, \
481           v= l-b+((h-a)<<8)-(ff>>8&1),\
482           mp= l+1+(h<<8),       \
483           ff= v>>8,             \
484           fa= h,                \
485           fb= ~a,               \
486           h= ff,                \
487           l= v,                 \
488           fr= h|l<<8
489 
490 #define ADCHLRR(a, b)           \
491           st += isez80() ? 2 :israbbit() ? 4 : isz180() ? 10 : 15, \
492           v= l+b+(h+a<<8)+(ff>>8&1),\
493           mp= l+1+(h<<8),       \
494           ff= v>>8,             \
495           fa= h,                \
496           fb= a,                \
497           h= ff,                \
498           l= v,                 \
499           fr= h|l<<8
500 
501 #define TEST(v, ticks) do {  \
502       uint8_t olda = a;        \
503       AND(v, 0);\
504       a = olda;                \
505       st += ticks;             \
506     } while (0)
507 
508 FILE * ft;
509 unsigned char * tapbuf;
510 
511 int     v
512       , wavpos= 0
513       , wavlen= 0
514       , mues
515       ;
516 
517 unsigned short
518         pc= 0
519       , sp= 0
520       , mp= 0
521       , t= 0
522       , u= 0
523       , ff= 0
524       , ff_= 0
525       , fa= 0
526       , fa_= 0
527       , fb= 0
528       , fb_= 0
529       , fr= 0
530       , fr_= 0
531       ;
532 long long
533         st= 0
534       , sttap
535       , stint
536       , counter= 1e8
537       ;
538 unsigned char
539         a= 0
540       , b= 0
541       , c= 0
542       , d= 0
543       , e= 0
544       , h= 0
545       , l= 0
546       , a_= 0
547       , b_= 0
548       , c_= 0
549       , d_= 0
550       , e_= 0
551       , h_= 0
552       , l_= 0
553       , xl= 0
554       , xh= 0
555       , yl= 0
556       , yh= 0
557       , i= 0
558       , r= 0
559       , r7= 0
560       , ih= 1
561       , iy= 0
562       , iff= 0
563       , im= 0
564       , w= 0
565       , ear= 255
566       , halted= 0
567       , altd = 0
568       , ioi = 0
569       , ioe = 0
570       ;
571 
572 
573 char   cmd_arguments[255];
574 int    cmd_arguments_len = 0;
575 
576 int    ioport = -1;
577 int    c_cpu = CPU_Z80;
578 int    rom_size = 0;
579 int    rc2014_mode = 0;
580 
581 static const uint8_t mirror_table[] = {
582     0x0, 0x8, 0x4, 0xC,  /*  0-3  */
583     0x2, 0xA, 0x6, 0xE,  /*  4-7  */
584     0x1, 0x9, 0x5, 0xD,  /*  8-11 */
585     0x3, 0xB, 0x7, 0xF   /* 12-15 */
586 };
587 
exit_log(int code,char * fmt,...)588 void exit_log(int code, char *fmt, ...)
589 {
590     va_list  ap;
591 
592     va_start(ap, fmt);
593     if ( fmt != NULL ) {
594        fprintf(stderr, "ticks: ");
595        vfprintf(stderr, fmt, ap);
596     }
597 
598     va_end(ap);
599     exit(code);
600 }
601 
tapcycles(void)602 long tapcycles(void){
603   mues= 1;
604   wavpos!=0x20000 && (ear^= 64);
605   if( wavpos>0x1f000 ) {
606     fseek( ft, wavpos-0x20000, SEEK_CUR );
607     wavlen-= wavpos;
608     wavpos= 0;
609     if (0x20000 != fread(tapbuf, 1, 0x20000, ft)) { fclose(ft); exit_log(1, "Routine tapcycles could not read required data\n"); }
610   }
611   while( (tapbuf[++wavpos]^ear<<1)&0x80 && wavpos<0x20000 )
612     mues+= 81;  // correct value must be 79.365, adjusted to simulate contention in Alkatraz
613   if( wavlen<=wavpos )
614     return 0;
615   else
616     return mues;
617 }
618 
in(int port)619 int in(int port){
620   int val;
621 
622   if ( (val = hook_console_in(port)) != -1 ) return val;
623   if ( (val = apu_in(port)) != -1 ) return val;
624   if ( (val = acia_in(port)) != -1 ) return val;
625 
626   return port&1 ? 255 : ear;
627 }
628 
out(int port,int value)629 void out(int port, int value){
630   if ( hook_console_out(port,value) == 0 ) return;
631   if ( apu_out(port,value) == 0 ) return;
632   if ( acia_out(port, value) == 0 ) return;
633 
634   memory_handle_paging(port, value);
635 }
636 
f(void)637 int f(void){
638   return  ff & 168  // S, 5, 3: bits 7, 5, 3
639         | ff >> 8 & 1 // C bit 0, so value 256
640         | !fr << 6    // Z, bit 6
641         | fb >> 8 & 2 // N (subtract flag) bit 1, value 512
642         | (fr ^ fa ^ fb ^ fb >> 8) & 16 // H (half carry) bit 4
643         | (fa & -256
644             ? 154020 >> ((fr ^ fr >> 4) & 15)
645             : ((fr ^ fa) & (fr ^ fb)) >> 5) & 4; // P/V bit 2
646 }
647 
f_(void)648 int f_(void){
649   return  ff_ & 168
650         | ff_ >> 8 & 1
651         | !fr_ << 6
652         | fb_ >> 8 & 2
653         | (fr_ ^ fa_ ^ fb_ ^ fb_ >> 8) & 16
654         | (fa_ & -256
655             ? 154020 >> ((fr_ ^ fr_ >> 4) & 15)
656             : ((fr_ ^ fa_) & (fr_ ^ fb_)) >> 5) & 4;
657 }
658 
setf(int a)659 void setf(int a){
660   fr= ~a & 64;
661   ff= a|= a<<8;
662   fa= 255 & (fb= a & -129 | (a&4)<<5);
663 }
664 
665 
666 
main(int argc,char ** argv)667 int main (int argc, char **argv){
668   int size= 0, start= 0, end= 0, intr= 0, tap= 0, alarmtime = 0, load_address = 0;
669   char * output= NULL;
670   char  *memory_model = "standard";
671   FILE * fh;
672 
673   hook_init();
674   debugger_init();
675   apu_reset();
676 
677   tapbuf= (unsigned char *) malloc (0x20000);
678   if( argc==1 )
679     printf("Ticks v0.14c beta, a silent Z80 emulator by Antonio Villena, 10 Jan 2013\n\n"),
680     printf("  ticks [-pc X] [-start X] [-end X] [-counter X] [-output <file>] <input_file>\n\n"),
681     printf("  <input_file>   File between 1 and 65536 bytes with Z80 machine code\n"),
682     printf("  -tape <file>   emulates ZX tape in port $FE from a .WAV file\n"),
683     printf("  -trace         outputs register values and disassembly while executing\n"),
684     printf("  -pc X          X in hexadecimal is the initial PC value\n"),
685     printf("  -start X       X in hexadecimal is the PC condition to start the counter\n"),
686     printf("  -end X         X in hexadecimal is the PC condition to exit\n"),
687     printf("  -counter X     X in decimal is another condition to exit\n"),
688     printf("  -int X         X in decimal are number of cycles for periodic interrupts\n"),
689     printf("  -w X           Maximum amount of running time (400000000 cycles per unit)\n"),
690     printf("  -d             Enable debugger\n"),
691     printf("  -l X           Load file to address\n"),
692     printf("  -b <model>     Memory model (zxn/zx/z180)\n"),
693     printf("  -m8080         Emulate an 8080\n"),
694     printf("  -m8085         Emulate an 8085 (mostly)\n"),
695     printf("  -mgbz80        Emulate a gbz80 (mostly)\n"),
696     printf("  -mz80          Emulate a z80\n"),
697     printf("  -mz180         Emulate a z180\n"),
698     printf("  -mr2k          Emulate a Rabbit 2000\n"),
699     printf("  -mr3k          Emulate a Rabbit 3000\n"),
700     printf("  -mz80n         Emulate a Spectrum Next z80n\n"),
701     printf("  -mez80         Emulate an ez80 (z80 mode)\n"),
702     printf("  -x <file>      Symbol file to read\n"),
703     printf("  -ide0 <file>   Set file to be ide device 0\n"),
704     printf("  -ide1 <file>   Set file to be ide device 1\n"),
705     printf("  -iochar X      Set port X to be character input/output\n"),
706     printf("  -output <file> dumps the RAM content to a 64K file\n"),
707     printf("  -rom X         write-protect memory, X in hexadecimal is first RAM address\n\n"),
708     printf("  Default values for -pc, -start and -end are 0000 if ommited. When the program "),
709     printf("exits, it'll show the number of cycles between start and end trigger in decimal\n\n"),
710     exit(0);
711   while (argc > 1){
712     if( argv[1][0] == '-' && argv[2] )
713       switch (argc--, argv++[1][1]){
714         case 'w':
715           alarmtime = strtol(argv[1], NULL, 10);
716           counter = 400000000LL * alarmtime;
717           break;
718         case 'b':
719           memory_model = argv[1];
720           break;
721         case 'p':
722           pc= strtol(argv[1], NULL, 16);
723           break;
724         case 's':
725           start= strtol(argv[1], NULL, 16);
726           break;
727         case 'e':
728           end= strtol(argv[1], NULL, 16);
729           break;
730         case 'r':
731           rom_size= strtol(argv[1], NULL, 16);
732           break;
733         case 'i':
734           if ( strcmp(&argv[0][1], "ide0") == 0 ) {
735             hook_io_set_ide_device(0, argv[1]);
736           } else if ( strcmp(&argv[0][1], "ide1") == 0 ) {
737             hook_io_set_ide_device(1, argv[1]);
738           } else if ( strcmp(&argv[0][1], "iochar") == 0 ) {
739             ioport = strtol(argv[1], NULL, 10);
740           } else {
741             intr= strtol(argv[1], NULL, 10);
742           }
743           break;
744         case 'l':
745           load_address = pc = strtol(argv[1], NULL, 0);
746           break;
747         case 'c':
748           sscanf(argv[1], "%llu", &counter);
749           counter<0 && (counter= 9e18);
750           break;
751         case 'd':
752           debugger_active = 1;
753           argv--;
754           argc++;
755           break;
756         case 'x':
757           read_symbol_file(argv[1]);
758           break;
759         case 'm':
760           if ( strcmp(&argv[0][1],"mz80") == 0 ) {
761             c_cpu = CPU_Z80;
762           } else if ( strcmp(&argv[0][1],"m8080") == 0 ) {
763             c_cpu = CPU_8080;
764           } else if ( strcmp(&argv[0][1],"m8085") == 0 ) {
765             c_cpu = CPU_8085;
766           } else if ( strcmp(&argv[0][1],"mz180") == 0 ) {
767             c_cpu = CPU_Z180;
768           } else if ( strcmp(&argv[0][1],"mz80n") == 0 ) {
769             c_cpu = CPU_Z80N;
770             memory_model = "zxn";
771           } else if ( strcmp(&argv[0][1],"mr2k") == 0 ) {
772             c_cpu = CPU_R2K;
773           } else if ( strcmp(&argv[0][1],"mr3k") == 0 ) {
774             c_cpu = CPU_R2K;
775           } else if ( strcmp(&argv[0][1],"mez80") == 0 ) {
776             c_cpu = CPU_EZ80;
777           } else if ( strcmp(&argv[0][1],"mgbz80") == 0 ) {
778             c_cpu = CPU_GBZ80;
779           } else {
780             printf("Unknown CPU: %s\n",&argv[0][1]);
781           }
782           argv--;
783           argc++;
784           break;
785         case 'o':
786           output= argv[1];
787           break;
788         case 't':
789           if (strcmp(&argv[0][1], "tape") == 0) {
790             ft= fopen(argv[1], "rb");
791             if( !ft )
792               printf("\nTape file not found: %s\n", argv[1]),
793               exit(-1);
794             if (0x20000 != fread(tapbuf, 1, 0x20000, ft)) { fclose(ft); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
795             memcpy(&wavlen, tapbuf+4, 4);
796             wavlen+= 8;
797             if( *(int*) tapbuf != 0x46464952 )
798               printf("\nInvalid WAV header\n"),
799               exit(-1);
800             if( *(int*)(tapbuf+16) != 16 )
801               printf("\nInvalid subchunk size\n"),
802               exit(-1);
803             if( *(int*)(tapbuf+20) != 0x10001 )
804               printf("\nInvalid number of channels or compression (only Mono and PCM allowed)\n"),
805               exit(-1);
806             if( *(int*)(tapbuf+24) != 44100 )
807               printf("\nInvalid sample rate (only 44100Hz allowed)\n"),
808               exit(-1);
809             if( *(int*)(tapbuf+32) != 0x80001 )
810               printf("\nInvalid align or bits per sample (only 8-bits samples allowed)\n"),
811               exit(-1);
812             if( *(int*)(tapbuf+40)+44 != wavlen )
813               printf("\nInvalid header size\n"),
814               exit(-1);
815             wavpos= 44;
816           }
817           else if (strcmp(&argv[0][1], "trace") == 0) {
818             debugger_active = 0;
819             trace = 1;
820             argv--;
821             argc++;
822           }
823           else {
824             printf("\nWrong Argument: %s\n", argv[0]);
825             exit(-1);
826           }
827           break;
828 		case '-':
829           while ( argc > 1 ) {
830             // I think windows is now comformant with snprintf? Either way, we can't grow the arugment buffer...
831             cmd_arguments_len += snprintf(cmd_arguments + cmd_arguments_len, sizeof(cmd_arguments) - cmd_arguments_len, "%s%s",cmd_arguments_len > 0 ? " " : "", argv[1]);
832             argc--;
833             argv++;
834           }
835           if ( pc == 256 ) {
836             put_memory(0x80,cmd_arguments_len % 256);
837             memcpy(get_memory_addr(0x81), cmd_arguments, cmd_arguments_len % 256);
838           } else {
839             put_memory(65280,cmd_arguments_len % 256);
840             memcpy(get_memory_addr(65281), cmd_arguments, cmd_arguments_len % 256);
841           }
842           break;
843         default:
844           printf("\nWrong Argument: %s\n", argv[0]);
845           exit(-1);
846       }
847     else{
848       memory_init(memory_model);
849 
850       fh= fopen(argv[1], "rb");
851       if( !fh )
852         printf("\nFile not found: %s\n", argv[1]),
853         exit(-1);
854       fseek(fh, 0, SEEK_END);
855       size= ftell(fh);
856       rewind(fh);
857       if( size>65536 && size!=65574 )
858         printf("\nIncorrect length: %d\n", size),
859         exit(-1);
860       else if( strstr(argv[1], "rc2014") != NULL ) {
861         *get_memory_addr(0x08) = 0xED;
862         *get_memory_addr(0x09) = 0xFE;
863         *get_memory_addr(0x0a) = 0xC9;
864         *get_memory_addr(0x10) = 0xED;
865         *get_memory_addr(0x11) = 0xFE;
866         *get_memory_addr(0x12) = 0xC9;
867         *get_memory_addr(0x18) = 0xED;
868         *get_memory_addr(0x19) = 0xFE;
869         *get_memory_addr(0x1a) = 0xC9;
870         rc2014_mode = 1;
871         if (1 != fread(get_memory_addr(pc), size, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
872       } else if( !strcasecmp(strchr(argv[1], '.'), ".com" ) ){
873         *get_memory_addr(5) = 0xED;
874         *get_memory_addr(6) = 0xFE;
875         *get_memory_addr(7) = 0xC9;
876         pc = 256;
877         // CP/M emulator
878         if (1 != fread(get_memory_addr(256), size, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
879       } else if( !strcasecmp(strchr(argv[1], '.'), ".sna" ) && size==49179 ){
880         FILE *fk= fopen("48.rom", "rb");
881         if( !fk )
882           printf("\nZX Spectrum ROM file not found: 48.rom\n"),
883           exit(-1);
884         if (16384 != fread(get_memory_addr(0), 1, 16384, fk)) { fclose(fk); exit_log(1, "Could not read required data from <48.rom>\n"); }
885         fclose(fk);
886         if (1 != fread(&i,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
887         if (1 != fread(&l_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
888         if (1 != fread(&h_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
889         if (1 != fread(&e_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
890         if (1 != fread(&d_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
891         if (1 != fread(&c_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
892         if (1 != fread(&b_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
893         if (1 != fread(&w,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
894         setf(w);
895         ff_= ff;
896         fr_= fr;
897         fa_= fa;
898         fb_= fb;
899         if (1 != fread(&a_,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
900         if (1 != fread(&l,   1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
901         if (1 != fread(&h,   1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
902         if (1 != fread(&e,   1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
903         if (1 != fread(&d,   1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
904         if (1 != fread(&c,   1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
905         if (1 != fread(&b,   1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
906         if (1 != fread(&yl,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
907         if (1 != fread(&yh,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
908         if (1 != fread(&xl,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
909         if (1 != fread(&xh,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
910         if (1 != fread(&iff, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
911         iff>>= 2;
912         if (1 != fread(&r, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
913         r7= r;
914         if (1 != fread(&w,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
915         setf(w);
916         if (1 != fread(&a,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
917         if (1 != fread(&sp, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
918         if (1 != fread(&im, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
919         if (1 != fread(&w,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
920         if (0xc000 != fread(get_memory_addr(0x4000), 1, 0xc000, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
921         RET(0);
922       }
923       else if( size==65574 ) {
924         if (65536 != fread(get_memory_addr(0), 1, 65536, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
925 
926         if (1 != fread(&w, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
927         u= w;
928         if (1 != fread(&a,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
929         if (1 != fread(&c,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
930         if (1 != fread(&b,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
931         if (1 != fread(&l,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
932         if (1 != fread(&h,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
933         if (1 != fread(&pc, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
934         if (1 != fread(&sp, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
935         if (1 != fread(&i,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
936         if (1 != fread(&r,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
937         r7= r;
938         if (1 != fread(&e,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
939         if (1 != fread(&d,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
940         if (1 != fread(&c_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
941         if (1 != fread(&b_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
942         if (1 != fread(&e_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
943         if (1 != fread(&d_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
944         if (1 != fread(&l_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
945         if (1 != fread(&h_, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
946         if (1 != fread(&w,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
947         setf(w);
948         ff_= ff;
949         fr_= fr;
950         fa_= fa;
951         fb_= fb;
952         setf(u);
953         if (1 != fread(&a_,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
954         if (1 != fread(&yl,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
955         if (1 != fread(&yh,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
956         if (1 != fread(&xl,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
957         if (1 != fread(&xh,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
958         if (1 != fread(&iff, 1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
959         if (1 != fread(&im,  1, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
960         if (1 != fread(&mp,  2, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
961       }
962       else {
963         int l;
964         for ( l = 0; l < size; l++ ) {
965           *get_memory_addr(load_address+l) = fgetc(fh);
966         }
967       }
968     }
969     ++argv;
970     --argc;
971   }
972   if( size==65574 ){
973     if (1 != fread(&wavpos, 4, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
974     ear= wavpos<<6 | 191;
975     wavpos>>= 1;
976     if( wavpos && ft ) {
977       fseek(ft, wavlen-wavpos, SEEK_SET);
978       wavlen= wavpos;
979       wavpos= 0;
980       if (0x20000 != fread(tapbuf, 1, 0x20000, ft)) { fclose(ft); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
981     }
982     if (1 != fread(&sttap, 4, 1, fh)) { fclose(fh); exit_log(1, "Could not read required data from <%s>\n", argv[1]); }
983     tap= sttap;
984   }
985   else
986     sttap= tap= tapcycles();
987   fclose(fh);
988   if( !size )
989     printf("File not specified or zero length\n");
990   stint= intr;
991 
992 
993   do{
994     char buf[256];
995     if ( ih ) debugger();
996     if( pc==start )
997       st= 0,
998       stint= intr,
999       sttap= tap;
1000     if( intr && st>stint && ih ){
1001       stint= st+intr;
1002       if( iff ){
1003         halted && (pc++, halted= 0);
1004         iff= 0;
1005         put_memory(--sp,pc>>8);
1006         put_memory(--sp,pc);
1007         r++;
1008         switch( im ){
1009           case 1:
1010             st++;
1011           case 0:
1012             pc= 56;
1013             st+= 12;
1014             break;
1015           default:
1016             pc= get_memory(t= 255 | i << 8);
1017             pc|= get_memory(++t) << 8;
1018             st+= 19;
1019         }
1020       }
1021     }
1022     if( tap && st>sttap )
1023       sttap= st+( tap= tapcycles() );
1024     r++;
1025     switch( get_memory(pc++) ){
1026       case 0x00: // NOP
1027         st+= israbbit() ? 2 : isz180() ? 3 : 4;
1028         ih=1;altd=0;ioi=0;ioe=0;break;
1029         break;
1030       case 0x40: // LD B,B
1031         if ( altd ) { b_ = b; st += 2; break; }
1032       case 0x49: // LD C,C
1033         if ( altd ) { c_ = c; st += 2; break; }
1034       case 0x52: // LD D,D
1035         if ( altd ) { d_ = d; st += 2; break; }
1036       case 0x5b: // LD E,E
1037         if ( altd ) { e_ = e; st += 2; break; }
1038       case 0x64: // LD H,H
1039         if ( altd ) { h_ = h; st += 2; break; }
1040       case 0x6d: // LD L,L
1041         if ( altd ) { l_ = l; st += 2; break; }
1042       case 0x7f: // LD A,A
1043         if ( altd ) { a_ = a; st += 2; break; }
1044         st+= israbbit() ? 2 : is8080() ? 5 : 4;
1045         ih=1;altd=0;ioi=0;ioe=0;break;
1046       case 0x76: // HALT
1047         if ( israbbit()) {
1048           altd = 1;
1049           st += 2;
1050         } else {
1051           st+= is8080() ? 7 : is8085() ? 5 : isz180() ? 3 : 4;
1052           halted= 1;
1053           pc--;
1054           altd=0;ioi=0;ioe=0;
1055         }
1056         ih=1;
1057         break;
1058       case 0x01: // LD BC,nn
1059         if ( altd ) LDRRIM(b_,c_);
1060         else LDRRIM(b, c);
1061         ih=1;altd=0;ioi=0;ioe=0;break;
1062       case 0x11: // LD DE,nn
1063         if ( altd ) LDRRIM(d_,e_);
1064         else LDRRIM(d, e);
1065         ih=1;altd=0;ioi=0;ioe=0;break;
1066       case 0x21: // LD HL,nn // LD IX,nn // LD IY,nn
1067         if( ih ) {
1068           if ( altd ) LDRRIM(h_,l_);
1069           else LDRRIM(h, l);
1070         } else if( iy )
1071           LDRRIM(yh, yl);
1072         else
1073           LDRRIM(xh, xl);
1074         ih=1;altd=0;ioi=0;ioe=0;break;
1075       case 0x31: // LD SP,nn / (EZ80) ld iy,(ix+d)
1076         if ( isez80() && ih == 0 ) {
1077             t = (get_memory(pc++)^128)-128;
1078             st += 5;
1079             if ( iy == 0 ) {  // ld iy,(ix+d)
1080                 yl = get_memory(t+(xl|xh<<8));
1081                 yh = get_memory(t+(xl|xh<<8) + 1);
1082             } else {  // ld ix,(iy+d)
1083                 xl = get_memory(t+(yl|yh<<8));
1084                 xh = get_memory(t+(yl|yh<<8) + 1);
1085             }
1086             break;
1087         }
1088         st+= israbbit() ? 6 : isgbz80() ? 12 : isz180() ? 9 : 10;
1089         sp= get_memory(pc++);
1090         sp|= get_memory(pc++)<<8;
1091         ih=1;altd=0;ioi=0;ioe=0;break;
1092       case 0x02: // LD (BC),A
1093         LDPR(b, c, a);
1094         ih=1;altd=0;ioi=0;ioe=0;break;
1095       case 0x12: // LD (DE),A
1096         LDPR(d, e, a);
1097         ih=1;altd=0;ioi=0;ioe=0;break;
1098       case 0x0a: // LD A,(BC)
1099         if ( altd ) LDRP(b, c, a_);
1100         else LDRP(b, c, a);
1101         ih=1;altd=0;ioi=0;ioe=0;break;
1102       case 0x1a: // LD A,(DE)
1103         if ( altd ) LDRP(d, e, a_);
1104         else LDRP(d, e, a);
1105         ih=1;altd=0;ioi=0;ioe=0;break;
1106       case 0x22: // LD (nn),HL // LD (nn),IX // LD (nn),IY
1107         if ( isgbz80() ) { // ld (hl+),a
1108           long long save = st;
1109           LDPR(h, l, a);
1110           INCW(h,l);
1111           st = save + 8;
1112           break;
1113         } else if( ih )
1114           LDPNNRR(h, l,isez80() ? 5 : israbbit() ? 13 : 16);
1115         else if( iy )
1116           LDPNNRR(yh, yl,isez80() ? 5 : israbbit() ? 13 : 16);
1117         else
1118           LDPNNRR(xh, xl, isez80() ? 5 :israbbit() ? 13 : 16);
1119         ih=1;altd=0;ioi=0;ioe=0;break;
1120       case 0x32: // LD (nn),A
1121         if ( isgbz80() ) { // ld (hl-),a
1122           long long save = st;
1123           LDPR(h, l, a);
1124           DECW(h,l);
1125           st = save + 8;
1126           break;
1127         }
1128         st+= isez80() ? 4 : israbbit() ? 10 : 13;
1129         t= get_memory(pc++);
1130         put_memory(t|= get_memory(pc++)<<8,a);
1131         mp= t+1 & 255
1132           | a<<8;
1133         ih=1;altd=0;ioi=0;ioe=0;break;
1134       case 0x2a: // LD HL,(nn) // LD IX,(nn) // LD IY,(nn)
1135       if ( isgbz80() ) { // ld a,(hl+)
1136           long long save = st;
1137           LDRP(h, l, a);
1138           INCW(h,l);
1139           st = save + 8;
1140           break;
1141         } else if( ih ) {
1142           if ( altd ) LDRRPNN(h_, l_, 11);
1143           else LDRRPNN(h, l, isez80() ? 5 : israbbit() ? 11 : isz180() ? 15 : 16);
1144         } else if( iy )
1145           LDRRPNN(yh, yl, isez80() ? 65: israbbit() ? 11 : isz180() ? 15 : 16);
1146         else
1147           LDRRPNN(xh, xl, isez80() ? 5 : israbbit() ? 11 : isz180() ? 15 : 16);
1148         ih=1;altd=0;ioi=0;ioe=0;break;
1149       case 0x3a: // LD A,(nn)
1150         if ( isgbz80() ) { // ld a,(hl-)
1151           long long save = st;
1152           LDRP(h, l, a);
1153           DECW(h,l);
1154           st = save + 8;
1155           break;
1156         }
1157         st+= isez80() ? 4 : israbbit() ? 9 : isz180() ? 12 : 13;
1158         mp= get_memory(pc++);
1159         if ( altd ) a_ = get_memory(mp|= get_memory(pc++)<<8);
1160         else a= get_memory(mp|= get_memory(pc++)<<8);
1161         ++mp;
1162         ih=1;altd=0;ioi=0;ioe=0;break;
1163       case 0x03: // INC BC
1164         if ( altd ) INCW(b_,c_);
1165         else INCW(b, c);
1166         ih=1;altd=0;ioi=0;ioe=0;break;
1167         break;
1168       case 0x13: // INC DE
1169         if ( altd ) INCW(d_,e_);
1170         else INCW(d, e);
1171         ih=1;altd=0;ioi=0;ioe=0;break;
1172       case 0x23: // INC HL // INC IX // INC IY
1173         if( ih ) {
1174           if ( altd ) INCW(h_,l_);
1175           else INCW(h, l);
1176         } else if( iy )
1177           INCW(yh, yl);
1178         else
1179           INCW(xh, xl);
1180         ih=1;altd=0;ioi=0;ioe=0;break;
1181       case 0x33: // INC SP
1182         st+= isez80() ? 1 : isgbz80() ? 8 : is8080() ? 5 : 6;
1183         sp++;
1184         ih=1;altd=0;ioi=0;ioe=0;break;
1185       case 0x0b: // DEC BC
1186         if ( altd ) DECW(b_,c_);
1187         else DECW(b, c);
1188         ih=1;altd=0;ioi=0;ioe=0;break;
1189       case 0x1b: // DEC DE
1190         if ( altd ) DECW(d_,e_);
1191         else DECW(d, e);
1192         ih=1;altd=0;ioi=0;ioe=0;break;
1193       case 0x2b: // DEC HL // DEC IX // DEC IY
1194         if( ih ) {
1195           if ( altd ) DECW(h_,l_);
1196           else DECW(h, l);
1197         } else if( iy )
1198           DECW(yh, yl);
1199         else
1200           DECW(xh, xl);
1201         ih=1;altd=0;ioi=0;ioe=0;break;
1202       case 0x3b: // DEC SP
1203         st+= isez80() ? 1 : isgbz80() ? 8 : is8080() ? 5 : 6;
1204         sp--;
1205         ih=1;altd=0;ioi=0;ioe=0;break;
1206       case 0x04: // INC B
1207         if ( altd ) INC(b_);
1208         else INC(b);
1209         ih=1;altd=0;ioi=0;ioe=0;break;
1210         break;
1211       case 0x0c: // INC C
1212         if ( altd ) INC(c_);
1213         else INC(c);
1214         ih=1;altd=0;ioi=0;ioe=0;break;
1215       case 0x14: // INC D
1216         if ( altd ) INC(d_);
1217         else INC(d);
1218         ih=1;altd=0;ioi=0;ioe=0;break;
1219       case 0x1c: // INC E
1220         if ( altd ) INC(e_);
1221         else INC(e);
1222         ih=1;altd=0;ioi=0;ioe=0;break;
1223       case 0x24: // INC H // INC IXh // INC IYh
1224         if( ih ) {
1225           if ( altd ) INC(h_);
1226           else INC(h);
1227         } else if( iy && canixh() )
1228           INC(yh);
1229         else if ( canixh() )
1230           INC(xh);
1231         ih=1;altd=0;ioi=0;ioe=0;break;
1232       case 0x2c: // INC L // INC IXl // INC IYl
1233         if( ih ) {
1234           if ( altd ) INC(l_);
1235           else INC(l);
1236         } else if( iy && canixh() )
1237           INC(yl);
1238         else if ( canixh() )
1239           INC(xl);
1240         ih=1;altd=0;ioi=0;ioe=0;break;
1241       case 0x34: // INC (HL) // INC (IX+d) // INC (IY+d)
1242         if( ih )
1243           st+=isez80() ? 4 : israbbit() ? 8 : is808x() ? 10 : isgbz80() ? 12 : 11,
1244           fa= get_memory(t= l | h<<8),
1245           ff= ff&256
1246             | (fr= put_memory(t,fa+(fb=+1)));
1247         else if( iy )
1248           INCPI(yh, yl);
1249         else
1250           INCPI(xh, xl);
1251         ih=1;altd=0;ioi=0;ioe=0;break;
1252       case 0x3c: // INC A
1253         INC(a);
1254         ih=1;altd=0;ioi=0;ioe=0;break;
1255       case 0x05: // DEC B
1256         DEC(b);
1257         ih=1;altd=0;ioi=0;ioe=0;break;
1258       case 0x0d: // DEC C
1259         DEC(c);
1260         ih=1;altd=0;ioi=0;ioe=0;break;
1261       case 0x15: // DEC D
1262         DEC(d);
1263         ih=1;altd=0;ioi=0;ioe=0;break;
1264       case 0x1d: // DEC E
1265         DEC(e);
1266         ih=1;altd=0;ioi=0;ioe=0;break;
1267       case 0x25: // DEC H // DEC IXh // DEC IYh
1268         if( ih )
1269           DEC(h);
1270         else if( iy )
1271           DEC(yh);
1272         else
1273           DEC(xh);
1274         ih=1;altd=0;ioi=0;ioe=0;break;
1275       case 0x2d: // DEC L // DEC IXl // DEC IYl
1276         if( ih )
1277           DEC(l);
1278         else if( iy )
1279           DEC(yl);
1280         else
1281           DEC(xl);
1282         ih=1;altd=0;ioi=0;ioe=0;break;
1283       case 0x35: // DEC (HL) // DEC (IX+d) // DEC (IY+d)
1284         if( ih )
1285           st+=isez80() ? 4 : israbbit() ? 8 : is808x() ? 10 : isgbz80() ? 12 : 11,
1286           fa= get_memory(t= l | h<<8),
1287           ff= ff&256
1288             | (fr= put_memory(t,fa+(fb=-1)));
1289         else if( iy )
1290           DECPI(yh, yl);
1291         else
1292           DECPI(xh, xl);
1293         ih=1;altd=0;ioi=0;ioe=0;break;
1294       case 0x3d: // DEC A
1295         if ( altd ) DEC(a_);
1296         else DEC(a);
1297         ih=1;altd=0;ioi=0;ioe=0;break;
1298       case 0x06: // LD B,n
1299         if ( altd ) LDRIM(b_);
1300         else LDRIM(b);
1301         ih=1;altd=0;ioi=0;ioe=0;break;
1302       case 0x0e: // LD C,n
1303         if ( altd ) LDRIM(c_);
1304         else LDRIM(c);
1305         ih=1;altd=0;ioi=0;ioe=0;break;
1306       case 0x16: // LD D,n
1307         if ( altd ) LDRIM(d_);
1308         else LDRIM(d);
1309         ih=1;altd=0;ioi=0;ioe=0;break;
1310       case 0x1e: // LD E,n
1311         if ( altd ) LDRIM(e_);
1312         else LDRIM(e);
1313         ih=1;altd=0;ioi=0;ioe=0;break;
1314       case 0x26: // LD H,n // LD IXh,n // LD IYh,n
1315         if( ih ) {
1316           if ( altd ) LDRIM(h_);
1317           else LDRIM(h);
1318         } else if( iy && canixh() )
1319           LDRIM(yh);
1320         else if ( canixh() )
1321           LDRIM(xh);
1322         ih=1;altd=0;ioi=0;ioe=0;break;
1323       case 0x2e: // LD L,n // LD IXl,n // LD IYl,n
1324         if( ih ) {
1325           if ( altd ) LDRIM(l_);
1326           else LDRIM(l);
1327         } else if( iy && canixh() )
1328           LDRIM(yl);
1329         else if ( canixh() )
1330           LDRIM(xl);
1331         ih=1;altd=0;ioi=0;ioe=0;break;
1332       case 0x36: // LD (HL),n // LD (IX+d),n // LD (IY+d),n
1333         if( ih )
1334           st+= israbbit() ? 7 : isgbz80() ? 12 : isz180() ? 9 : 10,
1335           put_memory(l|h<<8,get_memory(pc++));
1336         else if( iy )
1337           LDPIN(yh, yl);
1338         else
1339           LDPIN(xh, xl);
1340         ih=1;altd=0;ioi=0;ioe=0;break;
1341       case 0x3e: // LD A,n / (EZ80) ld (ix+d),iy (prefixed)
1342         if ( isez80() && ih == 0 ) {
1343             t = (get_memory(pc++)^128)-128;
1344             st += 4;
1345             if ( iy == 0 ) {  // ld (ix+d),iy
1346                 put_memory(t+(xl|xh<<8),yl);
1347                 put_memory(t+(xl|xh<<8) + 1,yh);
1348             } else {  // ld (iy+d),iy
1349                 put_memory(t+(yl|yh<<8),xl);
1350                 put_memory(t+(yl|yh<<8) + 1,xh);
1351             }
1352             break;
1353         }
1354         if ( altd ) LDRIM(a_);
1355         else LDRIM(a);
1356         ih=1;altd=0;ioi=0;ioe=0;break;
1357       case 0x07: // RLCA / (EZ80) ld bc,(ix+d) (prefixed)
1358         if ( isez80() && ih == 0 ) {
1359             t = (get_memory(pc++)^128)-128;
1360             st += 4;
1361             if ( iy == 0 ) {  // ld bc,(ix+d)
1362                 c = get_memory(t+(xl|xh<<8));
1363                 b = get_memory(t+(xl|xh<<8) + 1);
1364             } else {  // ld bc,(iy+d)
1365                 c = get_memory(t+(yl|yh<<8));
1366                 b = get_memory(t+(yl|yh<<8) + 1);
1367             }
1368             break;
1369         }
1370 	      st+= isez80() ? 1 :  israbbit() ? 2 : isz180() ? 3 : 4;
1371         if ( altd ) {
1372           a_= t= a_*257>>7;
1373           ff_= ff_&215
1374             | t &296;
1375           fb_= fb_      &128
1376             | (fa_^fr_) & 16;
1377         } else {
1378           a= t= a*257>>7;
1379           ff= ff&215
1380             | t &296;
1381           fb= fb      &128
1382             | (fa^fr) & 16;
1383         }
1384         ih=1;altd=0;ioi=0;ioe=0;break;
1385       case 0x0f: // RRCA / (EZ80) ld (ix+d),bc (prefixed)
1386         if ( isez80() && ih == 0 ) {
1387             t = (get_memory(pc++)^128)-128;
1388             st += 4;
1389             if ( iy == 0 ) {  // ld (ix+d),bc
1390                 put_memory(t+(xl|xh<<8),c);
1391                 put_memory(t+(xl|xh<<8) + 1,b);
1392             } else {  // ld (iy+d),bc
1393                 put_memory(t+(yl|yh<<8),c);
1394                 put_memory(t+(yl|yh<<8) + 1,b);
1395             }
1396             break;
1397         }
1398         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
1399         if ( altd ) {
1400           a_= t= a_>>1
1401               | ((a_&1)+1^1)<<7;
1402           ff_= ff_&215
1403             | t &296;
1404           fb_= fb_      &128
1405             | (fa_^fr_) & 16;
1406         } else {
1407           a= t= a>>1
1408               | ((a&1)+1^1)<<7;
1409           ff= ff&215
1410             | t &296;
1411           fb= fb      &128
1412             | (fa^fr) & 16;
1413         }
1414         ih=1;altd=0;ioi=0;ioe=0;break;
1415       case 0x17: // RLA,  (EZ80) ld de,(ix+d) (prefixed)
1416         if ( isez80() && ih == 0 ) {
1417             t = (get_memory(pc++)^128)-128;
1418             st += 4;
1419             if ( iy == 0 ) {  // ld de,(ix+d)
1420                 e = get_memory(t+(xl|xh<<8));
1421                 d = get_memory(t+(xl|xh<<8) + 1);
1422             } else {  // ld de,(iy+d)
1423                 e = get_memory(t+(yl|yh<<8));
1424                 d = get_memory(t+(yl|yh<<8) + 1);
1425             }
1426             break;
1427         }
1428         st+= isez80() ? 1 :  israbbit() ? 2 : isz180() ? 3 : 4;
1429         if ( altd ) {
1430           a_= t= a_<<1
1431               | ff_>>8 & 1;
1432           ff_= ff_&215
1433             | t &296;
1434           fb_= fb_      & 128
1435             | (fa_^fr_) &  16;
1436         } else {
1437           a= t= a<<1
1438               | ff>>8 & 1;
1439           ff= ff&215
1440             | t &296;
1441           fb= fb      & 128
1442             | (fa^fr) &  16;
1443         }
1444         ih=1;altd=0;ioi=0;ioe=0;break;
1445       case 0x1f: // RRA / (EZ80) ld (ix+d),de (prefixed)
1446         if ( isez80() && ih == 0 ) {
1447             t = (get_memory(pc++)^128)-128;
1448             st += 4;
1449             if ( iy == 0 ) {  // ld (ix+d),de
1450                 put_memory(t+(xl|xh<<8),e);
1451                 put_memory(t+(xl|xh<<8) + 1,d);
1452             } else {  // ld (iy+d),de
1453                 put_memory(t+(yl|yh<<8),e);
1454                 put_memory(t+(yl|yh<<8) + 1,d);
1455             }
1456             break;
1457         }
1458         st+= isez80() ? 1 : israbbit() ? 2 :isz180() ? 3 : 4;
1459         if ( altd ) {
1460           a_= t= (a_*513 | ff_&256)>>1;
1461           ff_= ff_&215
1462             | t &296;
1463           fb_= fb_      &128
1464             | (fa_^fr_) & 16;
1465         } else {
1466           a= t= (a*513 | ff&256)>>1;
1467           ff= ff&215
1468             | t &296;
1469           fb= fb      &128
1470             | (fa^fr) & 16;
1471         }
1472         ih=1;altd=0;ioi=0;ioe=0;break;
1473       case 0x09: // ADD HL,BC // ADD IX,BC // ADD IY,BC
1474         if( ih ) {
1475           if ( altd ) ADDRRRR_ALTD(h, l, b, c, h_, l_);
1476           else ADDRRRR(h, l, b, c);
1477         } else if( iy ) {
1478           if ( altd ) ADDRRRR_ALTD(yh, yl, b, c, yh, yl);
1479           else ADDRRRR(yh, yl, b, c);
1480         } else {
1481           if ( altd ) ADDRRRR_ALTD(xh, xl, b, c, xh, xl);
1482           else ADDRRRR(xh, xl, b, c);
1483         }
1484         ih=1;altd=0;ioi=0;ioe=0;break;
1485       case 0x19: // ADD HL,DE // ADD IX,DE // ADD IY,DE
1486         if( ih ) {
1487           if ( altd ) ADDRRRR_ALTD(h, l, d, e, h_, l_);
1488           ADDRRRR(h, l, d, e);
1489         } else if( iy ) {
1490           if ( altd ) ADDRRRR_ALTD(yh, yl, d, e, yh, yl);
1491           else ADDRRRR(yh, yl, d, e);
1492         } else {
1493           if ( altd ) ADDRRRR_ALTD(xh, xl, d, e, xh, xl);
1494           else ADDRRRR(xh, xl, d, e);
1495         }
1496         ih=1;altd=0;ioi=0;ioe=0;break;
1497       case 0x29: // ADD HL,HL // ADD IX,IX // ADD IY,IY
1498         if( ih ) {
1499           if ( altd ) ADDRRRR_ALTD(h, l, h, l, h_, l_);
1500           else ADDRRRR(h, l, h, l);
1501         } else if( iy ) {
1502           if ( altd ) ADDRRRR_ALTD(yh, yl, yh, yl, yh, yl);
1503           else ADDRRRR(yh, yl, yh, yl);
1504         } else {
1505           if ( altd ) ADDRRRR_ALTD(xh, xl, xh, xl, xh, xl);
1506           else ADDRRRR(xh, xl, xh, xl);
1507         }
1508         ih=1;altd=0;ioi=0;ioe=0;break;
1509       case 0x39: // ADD HL,SP // ADD IX,SP // ADD IY,SP
1510         if( ih ) {
1511           if ( altd ) ADDISP_ALTD(h, l, h_, l_);
1512           else ADDISP(h, l);
1513         } else if( iy ) {
1514           if ( altd ) ADDISP_ALTD(yh, yl, yh, yl);
1515           else ADDISP(yh, yl);
1516         } else {
1517           if ( altd ) ADDISP_ALTD(xh, xl, xh, xl);
1518           ADDISP(xh, xl);
1519         }
1520         ih=1;altd=0;ioi=0;ioe=0;break;
1521       case 0x18: // JR
1522         if ( is8085() ) { // (8085) RL DE (RDEL)
1523           long long savest = st;
1524           RL(e);
1525           RL(d);
1526           st = savest;
1527           st+=10;
1528           break;
1529         } else if ( is8080() ) {
1530           printf("%04x: ILLEGAL 8080 opcode JR\n",pc-1);
1531           break;
1532         }
1533         st+= isez80() ? 3 : isgbz80() ? 8 : isz180() ? 8 : 12;
1534         mp= pc+= (get_memory(pc)^128)-127;
1535         ih=1;altd=0;ioi=0;ioe=0;break;
1536       case 0x20: // JR NZ,s8
1537         if ( is8085() ) { // (8085) RIM
1538           st+=4;
1539           break;
1540         } else if ( is808x() ) {
1541           printf("%04x: ILLEGAL 8080 opcode JR NZ\n",pc-1);
1542           st+=4;
1543           break;
1544 		} else {
1545           JRCI(fr);
1546 		}
1547         ih=1;altd=0;ioi=0;ioe=0;break;
1548       case 0x28: // JR Z,s8
1549         if ( is8085() ) {  // (8085) ld de,hl+nn (LDHI)
1550           uint16_t val =(l | h<<8) + get_memory(pc++);
1551           d = val / 256;
1552           e = val % 256;
1553           st += 10;
1554           break;
1555         } else if ( is8080() ) {
1556           printf("%04x: ILLEGAL 8080 opcode JR Z\n",pc-1);
1557           st+=4;
1558           break;
1559         }
1560         JRC(fr);
1561         ih=1;altd=0;ioi=0;ioe=0;break;
1562       case 0x30: // JR NC,s8
1563         if ( is8085() ) { // (8085) SIM
1564           st+=4;
1565           break;
1566         } else if ( is8080() ) {
1567           printf("%04x: ILLEGAL 8080 opcode JR NC\n",pc-1);
1568           st+=4;
1569           break;
1570         }
1571         JRC(ff&256);
1572         ih=1;altd=0;ioi=0;ioe=0;break;
1573       case 0x38: // JR C,s8
1574         if ( is8085() ) { // (8085) LD DE,SP+nn (LDSI)
1575           uint16_t val = sp + get_memory(pc++);
1576           d = val / 256;
1577           e = val % 256;
1578           st += 10;
1579           break;
1580         } else if ( is8080() ) {
1581           printf("%04x: ILLEGAL 8080 opcode JR C\n",pc-1);
1582           st+=4;
1583           break;
1584         }
1585         JRCI(ff&256);
1586         ih=1;altd=0;ioi=0;ioe=0;break;
1587       case 0x08: // EX AF,AF'
1588         if ( is8085() ) {  // (8085) SUB HL,BC (DSUB)
1589           long long savest = st;
1590           ff&=~256;  // Clear carry
1591           SBCHLRR(b,c);
1592 		  st = savest;
1593 		  st += 10;
1594           break;
1595         } else if ( is8080()) {
1596           printf("%04x: ILLEGAL 8080 opcode EX AF,AF\n",pc-1);
1597           st+= 4;
1598           break;
1599         } else if ( isgbz80() ) {  // ld (nn),sp
1600           mp= get_memory(pc++);
1601           put_memory(mp|= get_memory(pc++)<<8, sp);
1602           put_memory(++mp,sp>>8);
1603           st += 20;
1604           break;
1605         }
1606         st+= israbbit() ? 2 : 4;
1607         t  =  a_;
1608         a_ =  a;
1609         a  =  t;
1610         t  =  ff_;
1611         ff_=  ff;
1612         ff =  t;
1613         t  =  fr_;
1614         fr_=  fr;
1615         fr =  t;
1616         t  =  fa_;
1617         fa_=  fa;
1618         fa =  t;
1619         t  =  fb_;
1620         fb_=  fb;
1621         fb =  t;
1622         ih=1;altd=0;ioi=0;ioe=0;break;
1623       case 0x10: // DJNZ
1624         if ( is8080() ) {
1625           printf("%04x: ILLEGAL 8080 opcode DJNZ\n",pc-1);
1626           st+=4;
1627           break;
1628         } else if ( is8085() ) {   // (8085) SRA HL (ARHL)
1629           SRA(h);
1630           RR(l);
1631           st += (-16 + 7);
1632           break;
1633         } else if ( isgbz80() ) {  // STOP
1634 		  t = get_memory(pc++);    // collect and ignore 00 byte
1635           st += 4;
1636 		  end = pc; // stop simulation
1637 		  break;
1638         }
1639         if( ( altd && --b_) || ( altd == 0 && --b) )
1640           st+= isez80() ? 4 :israbbit() ? 5 : 13,
1641           mp= pc+= (get_memory(pc)^128)-127;
1642         else
1643           st+= isez80() ? 2 : israbbit() ? 5 : 8,
1644           pc++;
1645         ih=1;altd=0;ioi=0;ioe=0;break;
1646       case 0x27: // DAA / (RCM) add sp,d / (EZ80) ld hl,(ix+d) (prefixed)
1647         if ( isez80() && ih == 0 ) {
1648             t = (get_memory(pc++)^128)-128;
1649             st += 5;
1650             if ( iy == 0 ) {  // ld hl,(ix+d)
1651                 l = get_memory(t+(xl|xh<<8));
1652                 h = get_memory(t+(xl|xh<<8) + 1);
1653             } else {  // ld hl,(iy+d)
1654                 l = get_memory(t+(yl|yh<<8));
1655                 h = get_memory(t+(yl|yh<<8) + 1);
1656             }
1657             break;
1658         }
1659         if ( israbbit()) {
1660           st += 4;
1661           sp += (get_memory(pc++)^128)-128; // TODO: Carry
1662         } else {
1663           st+= isez80() ? 1 : 4;
1664           t= (fr^fa^fb^fb>>8) & 16;  // H flag
1665           u= 0;		// incr
1666           if ( isz180() ) {
1667             if ( t || (!(fb&512) && (a&0x0f) > 0x9) )
1668                u |= 6;
1669             if ( (ff & 256) || (!(fb&512) && a > 0x99) )
1670                u |= 0x160;
1671           } else {
1672             (a |ff&256)>0x99 && (u= 0x160);
1673             (a&15 | t)>9 && (u+= 6);
1674           }
1675           fa= a|256;
1676           if( fb&512) // N (subtract) flag set
1677             a-= u,
1678             fb= ~u;
1679           else
1680             a+= fb= u;
1681           ff= (fr= a)
1682             | u&256;
1683         }
1684         ih=1;altd=0;ioi=0;ioe=0;break;
1685       case 0x2f: // CPL / (EZ80) ld (ix+d),hl
1686         if ( isez80() && ih == 0 ) {
1687             t = (get_memory(pc++)^128)-128;
1688             st += 5;
1689             if ( iy == 0 ) {  // ld (ix+d),hl
1690                 put_memory(t+(xl|xh<<8),l);
1691                 put_memory(t+(xl|xh<<8) + 1,h);
1692             } else {  // ld (iy+d),hl
1693                 put_memory(t+(yl|yh<<8),l);
1694                 put_memory(t+(yl|yh<<8) + 1,h);
1695             }
1696             break;
1697         }
1698         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
1699         if ( altd ) {
1700           ff= ff      &-41
1701             | (a_ = a^255)& 40;
1702           fb|= -129;
1703           fa=  fa & -17
1704             | ~fr &  16;
1705         } else {
1706           ff= ff      &-41
1707             | (a^=255)& 40;
1708           fb|= -129;
1709           fa=  fa & -17
1710             | ~fr &  16;
1711         }
1712         ih=1;altd=0;ioi=0;ioe=0;break;
1713       case 0x37: // SCF/ (EZ80) ld ix,(ix+d) (prefixed)
1714         if ( isez80() && ih == 0) {
1715             t = (get_memory(pc++)^128)-128;
1716             st += 5;
1717             if ( iy == 0 ) {  // ld ix,(ix+d)
1718                 unsigned char tl;
1719                 tl = get_memory(t+(xl|xh<<8));
1720                 xh = get_memory(t+(xl|xh<<8) + 1);
1721                 xl = tl;
1722             } else {  // ld iy,(iy+d)
1723                 unsigned char tl;
1724                 tl = get_memory(t+(yl|yh<<8));
1725                 yh = get_memory(t+(yl|yh<<8) + 1);
1726                 yl = tl;
1727             }
1728             break;
1729         }
1730         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
1731         if ( altd ) {
1732           fb_= fb_      &128
1733             | (fr_^fa_) & 16;
1734           ff_= 256
1735             | ff_  &128
1736             | a_   & 40;
1737         } else {
1738           fb= fb      &128
1739             | (fr^fa) & 16;
1740           ff= 256
1741             | ff  &128
1742             | a   & 40;
1743         }
1744         ih=1;altd=0;ioi=0;ioe=0;break;
1745       case 0x3f: // CCF / (EZ80) ld (ix+d),ix
1746         if ( isez80() && ih == 0 ) {
1747             t = (get_memory(pc++)^128)-128;
1748             st += 5;
1749             if ( iy == 0 ) {  // ld (ix+d),ix
1750                 put_memory(t+(xl|xh<<8),xl);
1751                 put_memory(t+(xl|xh<<8) + 1,xh);
1752             } else {  // ld (iy+d),iy
1753                 put_memory(t+(yl|yh<<8),yl);
1754                 put_memory(t+(yl|yh<<8) + 1,yh);
1755             }
1756             break;
1757         }
1758         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
1759         if ( altd ) {
1760           fb_= fb_            &128
1761             | (ff_>>4^fr_^fa_) & 16;
1762           ff_= ~ff_ & 256
1763             | ff_  & 128
1764             | a_   &  40;
1765         } else {
1766           fb= fb            &128
1767             | (ff>>4^fr^fa) & 16;
1768           ff= ~ff & 256
1769             | ff  & 128
1770             | a   &  40;
1771         }
1772         ih=1;altd=0;ioi=0;ioe=0;break;
1773       case 0x41: // LD B,C
1774         LDRR(b, c, b_, isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1775         ih=1;altd=0;ioi=0;ioe=0;break;
1776       case 0x42: // LD B,D
1777         LDRR(b, d, b_, isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1778         ih=1;altd=0;ioi=0;ioe=0;break;
1779       case 0x43: // LD B,E
1780         LDRR(b, e, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1781         ih=1;altd=0;ioi=0;ioe=0;break;
1782       case 0x44: // LD B,H // LD B,IXh // LD B,IYh
1783         if( ih ) {
1784           LDRR(b, h, h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1785         } else if( iy && canixh() )
1786           LDRR(b, yh, b,isez80() ? 1 : 4);
1787         else if ( canixh() )
1788           LDRR(b, xh, b,isez80() ? 1 : 4);
1789         ih=1;altd=0;ioi=0;ioe=0;break;
1790       case 0x45: // LD B,L // LD B,IXl // LD B,IYl
1791         if( ih ) {
1792           LDRR(b, l, b_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1793         } else if( iy && canixh() )
1794           LDRR(b, yl, b,isez80() ? 1 : 4);
1795         else if ( canixh() )
1796           LDRR(b, xl, b,isez80() ? 1 : 4);
1797         ih=1;altd=0;ioi=0;ioe=0;break;
1798       case 0x46: // LD B,(HL) // LD B,(IX+d) // LD B,(IY+d)
1799         if( ih ) {
1800           if ( altd ) LDRP(h, l, b_);
1801           else LDRP(h, l, b);
1802         } else if( iy )
1803           LDRPI(yh, yl, b);
1804         else
1805           LDRPI(xh, xl, b);
1806         ih=1;altd=0;ioi=0;ioe=0;break;
1807       case 0x47: // LD B,A
1808         LDRR(b, a, b_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1809         ih=1;altd=0;ioi=0;ioe=0;break;
1810       case 0x48: // LD C,B
1811         LDRR(c, b, c_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1812         ih=1;altd=0;ioi=0;ioe=0;break;
1813       case 0x4a: // LD C,D
1814         LDRR(c, d, c_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1815         ih=1;altd=0;ioi=0;ioe=0;break;
1816       case 0x4b: // LD C,E
1817         LDRR(c, e,c_, israbbit() ? 2 : is8080() ? 5 : 4);
1818         ih=1;altd=0;ioi=0;ioe=0;break;
1819       case 0x4c: // LD C,H // LD C,IXh // LD C,IYh
1820         if( ih )
1821           LDRR(c, h, c_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1822         else if( iy && canixh() )
1823           LDRR(c, yh, c,isez80() ? 1 : 4);
1824         else if ( canixh() )
1825           LDRR(c, xh, c,isez80() ? 1 : 4);
1826         ih=1;altd=0;ioi=0;ioe=0;break;
1827       case 0x4d: // LD C,L // LD C,IXl // LD C,IYl
1828         if( ih )
1829           LDRR(c, l, c_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1830         else if( iy && canixh() )
1831           LDRR(c, yl, c,isez80() ? 1 : 4);
1832         else if ( canixh() )
1833           LDRR(c, xl, c,isez80() ? 1 : 4);
1834         ih=1;altd=0;ioi=0;ioe=0;break;
1835       case 0x4e: // LD C,(HL) // LD C,(IX+d) // LD C,(IY+d)
1836         if( ih )
1837           LDRP(h, l, c);
1838         else if( iy )
1839           LDRPI(yh, yl, c);
1840         else
1841           LDRPI(xh, xl, c);
1842         ih=1;altd=0;ioi=0;ioe=0;break;
1843       case 0x4f: // LD C,A
1844         LDRR(c, a, c_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1845         ih=1;altd=0;ioi=0;ioe=0;break;
1846       case 0x50: // LD D,B
1847         LDRR(d, b, d_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1848         ih=1;altd=0;ioi=0;ioe=0;break;
1849       case 0x51: // LD D,C
1850         LDRR(d, c,  d_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1851         ih=1;altd=0;ioi=0;ioe=0;break;
1852       case 0x53: // LD D,E
1853         LDRR(d, e,  d_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1854         ih=1;altd=0;ioi=0;ioe=0;break;
1855       case 0x54: // LD D,H // LD D,IXh // LD D,IYh
1856         if( ih )
1857           LDRR(d, h,  d_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1858         else if( iy && canixh() )
1859           LDRR(d, yh, d,isez80() ? 1 : 4);
1860         else if ( canixh() )
1861           LDRR(d, xh, d,isez80() ? 1 : 4);
1862         ih=1;altd=0;ioi=0;ioe=0;break;
1863       case 0x55: // LD D,L // LD D,IXl // LD D,IYl
1864         if( ih )
1865           LDRR(d, l,  d_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1866         else if( iy && canixh() )
1867           LDRR(d, yl, d,isez80() ? 1 : 4);
1868         else if (canixh() )
1869           LDRR(d, xl, d,isez80() ? 1 : 4);
1870         ih=1;altd=0;ioi=0;ioe=0;break;
1871       case 0x56: // LD D,(HL) // LD D,(IX+d) // LD D,(IY+d)
1872         if( ih )
1873           LDRP(h, l, d);
1874         else if( iy )
1875           LDRPI(yh, yl, d);
1876         else
1877           LDRPI(xh, xl, d);
1878         ih=1;altd=0;ioi=0;ioe=0;break;
1879       case 0x57: // LD D,A
1880         LDRR(d, a,  d_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1881         ih=1;altd=0;ioi=0;ioe=0;break;
1882       case 0x58: // LD E,B
1883         LDRR(e, b, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1884         ih=1;altd=0;ioi=0;ioe=0;break;
1885       case 0x59: // LD E,C
1886         LDRR(e, c, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1887         ih=1;altd=0;ioi=0;ioe=0;break;
1888       case 0x5a: // LD E,D
1889         LDRR(e, d, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1890         ih=1;altd=0;ioi=0;ioe=0;break;
1891       case 0x5c: // LD E,H // LD E,IXh // LD E,IYh
1892         if( ih )
1893           LDRR(e, h, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1894         else if( iy && canixh() )
1895           LDRR(e, yh, e,isez80() ? 1 : 4);
1896         else if ( canixh() )
1897           LDRR(e, xh, e,isez80() ? 1 : 4);
1898         ih=1;altd=0;ioi=0;ioe=0;break;
1899       case 0x5d: // LD E,L // LD E,IXl // LD E,IYl
1900         if( ih )
1901           LDRR(e, l, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1902         else if( iy && canixh() )
1903           LDRR(e, yl, e,isez80() ? 1 : 4);
1904         else if ( canixh() )
1905           LDRR(e, xl, e,isez80() ? 1 : 4);
1906         ih=1;altd=0;ioi=0;ioe=0;break;
1907       case 0x5e: // LD E,(HL) // LD E,(IX+d) // LD E,(IY+d)
1908         if( ih )
1909           LDRP(h, l, e);
1910         else if( iy )
1911           LDRPI(yh, yl, e);
1912         else
1913           LDRPI(xh, xl, e);
1914         ih=1;altd=0;ioi=0;ioe=0;break;
1915       case 0x5f: // LD E,A
1916         LDRR(e, a, e_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1917         ih=1;altd=0;ioi=0;ioe=0;break;
1918       case 0x60: // LD H,B // LD IXh,B // LD IYh,B
1919         if( ih )
1920           LDRR(h, b, h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1921         else if( iy && canixh() )
1922           LDRR(yh, b, yh,isez80() ? 1 : 4);
1923         else if ( canixh() )
1924           LDRR(xh, b, xh,isez80() ? 1 : 4);
1925         ih=1;altd=0;ioi=0;ioe=0;break;
1926       case 0x61: // LD H,C // LD IXh,C // LD IYh,C
1927         if( ih )
1928           LDRR(h, c, h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1929         else if( iy && canixh() )
1930           LDRR(yh, c, yh,isez80() ? 1 : 4);
1931         else if ( canixh() )
1932           LDRR(xh, c, xh,isez80() ? 1 : 4);
1933         ih=1;altd=0;ioi=0;ioe=0;break;
1934       case 0x62: // LD H,D // LD IXh,D // LD IYh,D
1935         if( ih )
1936           LDRR(h, d,  h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1937         else if( iy && canixh() )
1938           LDRR(yh, d, yh,isez80() ? 1 : 4);
1939         else if ( canixh() )
1940           LDRR(xh, d, xh,isez80() ? 1 : 4);
1941         ih=1;altd=0;ioi=0;ioe=0;break;
1942       case 0x63: // LD H,E // LD IXh,E // LD IYh,E
1943         if( ih )
1944           LDRR(h, e,  h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1945         else if( iy && canixh() )
1946           LDRR(yh, e, yh,isez80() ? 1 : 4);
1947         else if ( canixh() )
1948           LDRR(xh, e, xh,isez80() ? 1 : 4);
1949         ih=1;altd=0;ioi=0;ioe=0;break;
1950       case 0x65: // LD H,L // LD IXh,IXl // LD IYh,IYl
1951         if( ih )
1952           LDRR(h, l, h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1953         else if( iy && canixh() )
1954           LDRR(yh, yl, yh,isez80() ? 1 : 4);
1955         else if ( canixh() )
1956           LDRR(xh, yl, xh,isez80() ? 1 : 4);
1957         ih=1;altd=0;ioi=0;ioe=0;break;
1958       case 0x66: // LD H,(HL) // LD H,(IX+d) // LD H,(IY+d)
1959         if( ih )
1960           LDRP(h, l, h);
1961         else if( iy )
1962           LDRPI(yh, yl, h);
1963         else
1964           LDRPI(xh, xl, h);
1965         ih=1;altd=0;ioi=0;ioe=0;break;
1966       case 0x67: // LD H,A // LD IXh,A // LD IYh,A
1967         if( ih )
1968           LDRR(h, a, h_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1969         else if( iy && canixh() )
1970           LDRR(yh, a, yh,isez80() ? 1 : 4);
1971         else if ( canixh() )
1972           LDRR(xh, a, xh,isez80() ? 1 : 4);
1973         ih=1;altd=0;ioi=0;ioe=0;break;
1974       case 0x68: // LD L,B // LD IXl,B // LD IYl,B
1975         if( ih )
1976           LDRR(l, b, l_,isez80() ? 1 : israbbit() ? 2 :is8080() ? 5 :  4);
1977         else if( iy && canixh() )
1978           LDRR(yl, b, yl,isez80() ? 1 : 4);
1979         else if ( canixh() )
1980           LDRR(xl, b, xl,isez80() ? 1 : 4);
1981         ih=1;altd=0;ioi=0;ioe=0;break;
1982       case 0x69: // LD L,C // LD IXl,C // LD IYl,C
1983         if( ih )
1984           LDRR(l, c, l_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1985         else if( iy && canixh() )
1986           LDRR(yl, c, yl,isez80() ? 1 : 4);
1987         else if ( canixh() )
1988           LDRR(xl, c, xl,isez80() ? 1 : 4);
1989         ih=1;altd=0;ioi=0;ioe=0;break;
1990       case 0x6a: // LD L,D // LD IXl,D // LD IYl,D
1991         if( ih )
1992           LDRR(l, d, l_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
1993         else if( iy && canixh() )
1994           LDRR(yl, d, yl,isez80() ? 1 : 4);
1995         else if ( canixh() )
1996           LDRR(xl, d, xl,isez80() ? 1 : 4);
1997         ih=1;altd=0;ioi=0;ioe=0;break;
1998       case 0x6b: // LD L,E // LD IXl,E // LD IYl,E
1999         if( ih )
2000           LDRR(l, e, l_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2001         else if( iy && canixh() )
2002           LDRR(yl, e, yl,isez80() ? 1 : 4);
2003         else if ( canixh() )
2004           LDRR(xl, e, xl,isez80() ? 1 : 4);
2005         ih=1;altd=0;ioi=0;ioe=0;break;
2006       case 0x6c: // LD L,H // LD IXl,IXh // LD IYl,IYh
2007         if( ih )
2008           LDRR(l, h, l_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2009         else if( iy && canixh() )
2010           LDRR(yl, yh, yl,isez80() ? 1 : 4);
2011         else if ( canixh() )
2012           LDRR(xl, xh, xl,isez80() ? 1 : 4);
2013         ih=1;altd=0;ioi=0;ioe=0;break;
2014       case 0x6e: // LD L,(HL) // LD L,(IX+d) // LD L,(IY+d)
2015         if( ih )
2016           LDRP(h, l, l);
2017         else if( iy )
2018           LDRPI(yh, yl, l);
2019         else
2020           LDRPI(xh, xl, l);
2021         ih=1;altd=0;ioi=0;ioe=0;break;
2022       case 0x6f: // LD L,A // LD IXl,A // LD IYl,A
2023         if( ih )
2024           LDRR(l, a, l_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2025         else if( iy && canixh() )
2026           LDRR(yl, a, yl,isez80() ? 1 : 4);
2027         else if ( canixh() )
2028           LDRR(xl, a, xl,isez80() ? 1 : 4);
2029         ih=1;altd=0;ioi=0;ioe=0;break;
2030       case 0x70: // LD (HL),B // LD (IX+d),B // LD (IY+d),B
2031         if( ih )
2032           LDPR(h, l, b);
2033         else if( iy )
2034           LDPRI(yh, yl, b);
2035         else
2036           LDPRI(xh, xl, b);
2037         ih=1;altd=0;ioi=0;ioe=0;break;
2038       case 0x71: // LD (HL),C // LD (IX+d),C // LD (IY+d),C
2039         if( ih )
2040           LDPR(h, l, c);
2041         else if( iy )
2042           LDPRI(yh, yl, c);
2043         else
2044           LDPRI(xh, xl, c);
2045         ih=1;altd=0;ioi=0;ioe=0;break;
2046       case 0x72: // LD (HL),D // LD (IX+d),D // LD (IY+d),D
2047         if( ih )
2048           LDPR(h, l, d);
2049         else if( iy )
2050           LDPRI(yh, yl, d);
2051         else
2052           LDPRI(xh, xl, d);
2053         ih=1;altd=0;ioi=0;ioe=0;break;
2054       case 0x73: // LD (HL),E // LD (IX+d),E // LD (IY+d),E
2055         if( ih )
2056           LDPR(h, l, e);
2057         else if( iy )
2058           LDPRI(yh, yl, e);
2059         else
2060           LDPRI(xh, xl, e);
2061         ih=1;altd=0;ioi=0;ioe=0;break;
2062       case 0x74: // LD (HL),H // LD (IX+d),H // LD (IY+d),H
2063         if( ih )
2064           LDPR(h, l, h);
2065         else if( iy )
2066           LDPRI(yh, yl, h);
2067         else
2068           LDPRI(xh, xl, h);
2069         ih=1;altd=0;ioi=0;ioe=0;break;
2070       case 0x75: // LD (HL),L // LD (IX+d),L // LD (IY+d),L
2071         if( ih )
2072           LDPR(h, l, l);
2073         else if( iy )
2074           LDPRI(yh, yl, l);
2075         else
2076           LDPRI(xh, xl, l);
2077         ih=1;altd=0;ioi=0;ioe=0;break;
2078       case 0x77: // LD (HL),A // LD (IX+d),A // LD (IY+d),A
2079         if( ih )
2080           LDPR(h, l, a);
2081         else if( iy )
2082           LDPRI(yh, yl, a);
2083         else
2084           LDPRI(xh, xl, a);
2085         ih=1;altd=0;ioi=0;ioe=0;break;
2086       case 0x78: // LD A,B
2087         LDRR(a, b, a_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2088         ih=1;altd=0;ioi=0;ioe=0;break;
2089       case 0x79: // LD A,C
2090         LDRR(a, c, a_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2091         ih=1;altd=0;ioi=0;ioe=0;break;
2092       case 0x7a: // LD A,D
2093         LDRR(a, d, a_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2094         ih=1;altd=0;ioi=0;ioe=0;break;
2095       case 0x7b: // LD A,E
2096         LDRR(a, e, a_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2097         ih=1;altd=0;ioi=0;ioe=0;break;
2098       case 0x7c: // LD A,H // LD A,IXh // LD A,IYh (RCM) LD HL,IX LD HL,IY
2099         if ( israbbit()) {
2100           if ( ih ) {
2101              LDRR(a,h,a_, 2);
2102           } else if ( iy ) {
2103             if ( altd ) { h_ = yh; l_ = yl; }
2104             else { h = yh; l = yl; }
2105             st += 4;
2106           } else {
2107             if ( altd ) { h_ = xh; l_ = xl; }
2108             else { h = xh; l = xl; }
2109             st += 4;
2110           }
2111         } else {
2112           if( ih )
2113             LDRR(a, h, a,isez80() ? 1 : is8080() ? 5 : 4);
2114           else if( iy )
2115             LDRR(a, yh, a,isez80() ? 1 : 4);
2116           else
2117             LDRR(a, xh, a,isez80() ? 1 : 4);
2118         }
2119         ih=1;altd=0;ioi=0;ioe=0;break;
2120       case 0x7d: // LD A,L // LD A,IXl // LD A,IYl
2121         if( ih ) {
2122           LDRR(a, l, a_,isez80() ? 1 : israbbit() ? 2 : is8080() ? 5 : 4);
2123         } else if( iy ) {
2124           if ( israbbit() ) {
2125               yl = l; yh = h;      // LD IY,HL
2126               st += 4;
2127           } else {
2128               LDRR(a, yl, a,isez80() ? 1 : 4);
2129           }
2130         } else {
2131           if ( israbbit() ) {
2132               xl = l; xh = h;     // LD IX,HL
2133               st += 4;
2134           } else {
2135               LDRR(a, xl, a,isez80() ? 1 : 4);
2136           }
2137         }
2138         ih=1;altd=0;ioi=0;ioe=0;break;
2139       case 0x7e: // LD A,(HL) // LD A,(IX+d) // LD A,(IY+d)
2140         if( ih )
2141           LDRP(h, l, a);
2142         else if( iy )
2143           LDRPI(yh, yl, a);
2144         else
2145           LDRPI(xh, xl, a);
2146         ih=1;altd=0;ioi=0;ioe=0;break;
2147       case 0x80: // ADD A,B
2148         ADD(b,isez80() ? 1 : israbbit() ? 2 : 4);
2149         ih=1;altd=0;ioi=0;ioe=0;break;
2150       case 0x81: // ADD A,C
2151         ADD(c,isez80() ? 1 : israbbit() ? 2 : 4);
2152         ih=1;altd=0;ioi=0;ioe=0;break;
2153       case 0x82: // ADD A,D
2154         ADD(d,isez80() ? 1 : israbbit() ? 2 : 4);
2155         ih=1;altd=0;ioi=0;ioe=0;break;
2156       case 0x83: // ADD A,E
2157         ADD(e,isez80() ? 1 : israbbit() ? 2 : 4);
2158         ih=1;altd=0;ioi=0;ioe=0;break;
2159       case 0x84: // ADD A,H // ADD A,IXh // ADD A,IYh
2160         if( ih )
2161           ADD(h,isez80() ? 1 : israbbit() ? 2 : 4);
2162         else if( iy && canixh() )
2163           ADD(yh,isez80() ? 1 : 4);
2164         else if ( canixh() )
2165           ADD(xh,isez80() ? 1 : 4);
2166         ih=1;altd=0;ioi=0;ioe=0;break;
2167       case 0x85: // ADD A,L // ADD A,IXl // ADD A,IYl
2168         if( ih )
2169           ADD(l,isez80() ? 1 : israbbit() ? 2 : 4);
2170         else if( iy && canixh() )
2171           ADD(yl,isez80() ? 1 : 4);
2172         else if ( canixh() )
2173           ADD(xl,isez80() ? 1 : 4);
2174         ih=1;altd=0;ioi=0;ioe=0;break;
2175       case 0x86: // ADD A,(HL) // ADD A,(IX+d) // ADD A,(IY+d)
2176         if( ih )
2177           ADD(get_memory(l|h<<8),isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2178         else if( iy )
2179           ADD(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535),isez80() ? 3 : 15);
2180         else
2181           ADD(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535),isez80() ? 3 : 15);
2182         ih=1;altd=0;ioi=0;ioe=0;break;
2183       case 0x87: // ADD A,A
2184         st+= isez80() ? 1 : israbbit() ? 2 : 4;
2185         if ( altd ) fr_= a_= (ff_= 2*(fa_= fb_= a));
2186         else fr= a= (ff= 2*(fa= fb= a));
2187         ih=1;altd=0;ioi=0;ioe=0;break;
2188       case 0x88: // ADC A,B
2189         ADC(b,isez80() ? 1 : israbbit() ? 2 : 4);
2190         ih=1;altd=0;ioi=0;ioe=0;break;
2191       case 0x89: // ADC A,C
2192         ADC(c,isez80() ? 1 : israbbit() ? 2 : 4);
2193         ih=1;altd=0;ioi=0;ioe=0;break;
2194       case 0x8a: // ADC A,D
2195         ADC(d,isez80() ? 1 : israbbit() ? 2 : 4);
2196         ih=1;altd=0;ioi=0;ioe=0;break;
2197       case 0x8b: // ADC A,E
2198         ADC(e,isez80() ? 1 : israbbit() ? 2 : 4);
2199         ih=1;altd=0;ioi=0;ioe=0;break;
2200       case 0x8c: // ADC A,H // ADC A,IXh // ADC A,IYh
2201         if( ih )
2202           ADC(h,isez80() ? 1 :israbbit() ? 2 : 4);
2203         else if( iy && canixh() )
2204           ADC(yh,isez80() ? 1 : 4);
2205         else if ( canixh() )
2206           ADC(xh,isez80() ? 1 : 4);
2207         ih=1;altd=0;ioi=0;ioe=0;break;
2208       case 0x8d: // ADC A,L // ADC A,IXl // ADC A,IYl
2209         if( ih )
2210           ADC(l,isez80() ? 1 : israbbit() ? 2 : 4);
2211         else if( iy && canixh() )
2212           ADC(yl,isez80() ? 1 : 4);
2213         else if ( canixh() )
2214           ADC(xl,isez80() ? 1 : 4);
2215         ih=1;altd=0;ioi=0;ioe=0;break;
2216       case 0x8e: // ADC A,(HL) // ADC A,(IX+d) // ADC A,(IY+d)
2217         if( ih )
2218           ADC(get_memory(l|h<<8),isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2219         else if( iy )
2220           ADC(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535),isez80() ? 3 : 15);
2221         else
2222           ADC(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535),isez80() ? 3 : 15);
2223         ih=1;altd=0;ioi=0;ioe=0;break;
2224       case 0x8f: // ADC A,A
2225         st+=isez80() ? 1 : israbbit() ? 2 : 4;
2226         if ( altd ) fr_= a_= (ff_= 2*(fa_= fb_= a)+(ff_>>8&1));
2227         else fr= a= (ff= 2*(fa= fb= a)+(ff>>8&1));
2228         ih=1;altd=0;ioi=0;ioe=0;break;
2229       case 0x90: // SUB B
2230         SUB(b,isez80() ? 1 :israbbit() ? 2 : 4);
2231         ih=1;altd=0;ioi=0;ioe=0;break;
2232       case 0x91: // SUB C
2233         SUB(c,isez80() ? 1 :israbbit() ? 2 : 4);
2234         ih=1;altd=0;ioi=0;ioe=0;break;
2235       case 0x92: // SUB D
2236         SUB(d,isez80() ? 1 : israbbit() ? 2 : 4);
2237         ih=1;altd=0;ioi=0;ioe=0;break;
2238       case 0x93: // SUB E
2239         SUB(e,isez80() ? 1 :israbbit() ? 2 : 4);
2240         ih=1;altd=0;ioi=0;ioe=0;break;
2241       case 0x94: // SUB H // SUB IXh // SUB IYh
2242         if( ih )
2243           SUB(h,isez80() ? 1 : israbbit() ? 2 : 4);
2244         else if( iy && canixh() )
2245           SUB(yh,isez80() ? 1 : 4);
2246         else if ( canixh() )
2247           SUB(xh,isez80() ? 1 : 4);
2248         ih=1;altd=0;ioi=0;ioe=0;break;
2249       case 0x95: // SUB L // SUB IXl // SUB IYl
2250         if( ih )
2251           SUB(l,isez80() ? 1 : israbbit() ? 2 : 4);
2252         else if( iy && canixh() )
2253           SUB(yl,isez80() ? 1 : 4);
2254         else if ( canixh() )
2255           SUB(xl,isez80() ? 1 : 4);
2256         ih=1;altd=0;ioi=0;ioe=0;break;
2257       case 0x96: // SUB (HL) // SUB (IX+d) // SUB (IY+d)
2258         if( ih )
2259           SUB(get_memory(l|h<<8),isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2260         else if( iy )
2261           SUB(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535),isez80() ? 3 : 15);
2262         else
2263           SUB(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535),isez80() ? 3 : 15);
2264         ih=1;altd=0;ioi=0;ioe=0;break;
2265       case 0x97: // SUB A
2266         st+=isez80() ? 1 : israbbit() ? 2 : 4;
2267         if ( altd ) {
2268           fb_= ~(fa_= a);
2269           fr_= a_= ff_= 0;
2270         } else {
2271           fb= ~(fa= a);
2272           fr= a= ff= 0;
2273         }
2274         ih=1;altd=0;ioi=0;ioe=0;break;
2275       case 0x98: // SBC A,B
2276         SBC(b, isez80() ? 1 : israbbit() ? 2 : 4);
2277         ih=1;altd=0;ioi=0;ioe=0;break;
2278       case 0x99: // SBC A,C
2279         SBC(c, isez80() ? 1 : israbbit() ? 2 : 4);
2280         ih=1;altd=0;ioi=0;ioe=0;break;
2281       case 0x9a: // SBC A,D
2282         SBC(d, isez80() ? 1 : israbbit() ? 2 : 4);
2283         ih=1;altd=0;ioi=0;ioe=0;break;
2284       case 0x9b: // SBC A,E
2285         SBC(e, isez80() ? 1 : israbbit() ? 2 : 4);
2286         ih=1;altd=0;ioi=0;ioe=0;break;
2287       case 0x9c: // SBC A,H // SBC A,IXh // SBC A,IYh
2288         if( ih )
2289           SBC(h,isez80() ? 1 : israbbit() ? 2 : 4);
2290         else if( iy && canixh() )
2291           SBC(yh,isez80() ? 1 : 4);
2292         else if ( canixh() )
2293           SBC(xh,isez80() ? 1 : 4);
2294         ih=1;altd=0;ioi=0;ioe=0;break;
2295       case 0x9d: // SBC A,L // SBC A,IXl // SBC A,IYl
2296         if( ih )
2297           SBC(l,isez80() ? 1 : israbbit() ? 2 : 4);
2298         else if( iy && canixh() )
2299           SBC(yl,isez80() ? 1 : 4);
2300         else if ( canixh() )
2301           SBC(xl,isez80() ? 1 : 4);
2302         ih=1;altd=0;ioi=0;ioe=0;break;
2303       case 0x9e: // SBC A,(HL) // SBC A,(IX+d) // SBC A,(IY+d)
2304         if( ih )
2305           SBC(get_memory(l|h<<8),isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2306         else if( iy )
2307           SBC(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535), isez80() ? 3 : 15);
2308         else
2309           SBC(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535), isez80() ? 3 : 15);
2310         ih=1;altd=0;ioi=0;ioe=0;break;
2311       case 0x9f: // SBC A,A
2312         st+= isez80() ? 1 : israbbit() ? 2 : 4;
2313         if ( altd ) {
2314           fb_= ~(fa_= a);
2315           fr_= a_= (ff_= (ff_&256)/-256);
2316         } else {
2317           fb= ~(fa= a);
2318           fr= a= (ff= (ff&256)/-256);
2319         }
2320         ih=1;altd=0;ioi=0;ioe=0;break;
2321       case 0xa0: // AND B
2322         AND(b, isez80() ? 1 : israbbit() ? 2 : 4);
2323         ih=1;altd=0;ioi=0;ioe=0;break;
2324       case 0xa1: // AND C
2325         AND(c, isez80() ? 1 : israbbit() ? 2 : 4);
2326         ih=1;altd=0;ioi=0;ioe=0;break;
2327       case 0xa2: // AND D
2328         AND(d, isez80() ? 1 : israbbit() ? 2 : 4);
2329         ih=1;altd=0;ioi=0;ioe=0;break;
2330       case 0xa3: // AND E
2331         AND(e, isez80() ? 1 : israbbit() ? 2 : 4);
2332         ih=1;altd=0;ioi=0;ioe=0;break;
2333       case 0xa4: // AND H // AND IXh // AND IYh
2334         if( ih )
2335           AND(h,  isez80() ? 1 :israbbit() ? 2 : 4);
2336         else if( iy && canixh() )
2337           AND(yh, isez80() ? 1 : 4);
2338         else if ( canixh() )
2339           AND(xh, isez80() ? 1 : 4);
2340         ih=1;altd=0;ioi=0;ioe=0;break;
2341       case 0xa5: // AND L // AND IXl // AND IYl
2342         if( ih )
2343           AND(l, isez80() ? 1 : israbbit() ? 2 : 4);
2344         else if( iy && canixh() )
2345           AND(yl, isez80() ? 1 : 4);
2346         else if ( canixh() )
2347           AND(xl, isez80() ? 1 : 4);
2348         ih=1;altd=0;ioi=0;ioe=0;break;
2349       case 0xa6: // AND (HL) // AND (IX+d) // AND (IY+d)
2350         if( ih )
2351           AND(get_memory(l|h<<8), isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2352         else if( iy )
2353           AND(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535), isez80() ? 3 : 15);
2354         else
2355           AND(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535), isez80() ? 3 : 15);
2356         ih=1;altd=0;ioi=0;ioe=0;break;
2357       case 0xa7: // AND A
2358         st+=isez80() ? 1 : israbbit() ? 2 : 4;
2359         if ( altd ) {
2360           fa_= ~(ff_= fr_= a_);
2361           fb_= 0;
2362         } else {
2363           fa= ~(ff= fr= a);
2364           fb= 0;
2365         }
2366         ih=1;altd=0;ioi=0;ioe=0;break;
2367       case 0xa8: // XOR B
2368         XOR(b,isez80() ? 1 : israbbit() ? 2 : 4);
2369         ih=1;altd=0;ioi=0;ioe=0;break;
2370       case 0xa9: // XOR C
2371         XOR(c,isez80() ? 1 : israbbit() ? 2 : 4);
2372         ih=1;altd=0;ioi=0;ioe=0;break;
2373       case 0xaa: // XOR D
2374         XOR(d,isez80() ? 1 : israbbit() ? 2 : 4);
2375         ih=1;altd=0;ioi=0;ioe=0;break;
2376       case 0xab: // XOR E
2377         XOR(e,isez80() ? 1 : israbbit() ? 2 : 4);
2378         ih=1;altd=0;ioi=0;ioe=0;break;
2379       case 0xac: // XOR H // XOR IXh // XOR IYh
2380         if( ih )
2381           XOR(h,isez80() ? 1 : 4);
2382         else if( iy )
2383           XOR(yh,isez80() ? 1 : 4);
2384         else
2385           XOR(xh,isez80() ? 1 : 4);
2386         ih=1;altd=0;ioi=0;ioe=0;break;
2387       case 0xad: // XOR L // XOR IXl // XOR IYl
2388         if( ih )
2389           XOR(l,isez80() ? 1 : israbbit() ? 2 : 4);
2390         else if( iy && canixh() )
2391           XOR(yl,isez80() ? 1 : 4);
2392         else if ( canixh() )
2393           XOR(xl,isez80() ? 1 : 4);
2394         ih=1;altd=0;ioi=0;ioe=0;break;
2395       case 0xae: // XOR (HL) // XOR (IX+d) // XOR (IY+d)
2396         if( ih )
2397           XOR(get_memory(l|h<<8),isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2398         else if( iy )
2399           XOR(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535),isez80() ? 3 : 15);
2400         else
2401           XOR(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535),isez80() ? 3 : 15);
2402         ih=1;altd=0;ioi=0;ioe=0;break;
2403       case 0xaf: // XOR A
2404         st+=isez80() ? 1 : israbbit() ? 2 : 4;
2405         a= ff= fr= fb= 0;
2406         fa= 256;
2407         ih=1;altd=0;ioi=0;ioe=0;break;
2408       case 0xb0: // OR B
2409         OR(b,isez80() ? 1 : israbbit() ? 2 : 4);
2410         ih=1;altd=0;ioi=0;ioe=0;break;
2411       case 0xb1: // OR C
2412         OR(c,isez80() ? 1 : israbbit() ? 2 : 4);
2413         ih=1;altd=0;ioi=0;ioe=0;break;
2414       case 0xb2: // OR D
2415         OR(d,isez80() ? 1 : israbbit() ? 2 : 4);
2416         ih=1;altd=0;ioi=0;ioe=0;break;
2417       case 0xb3: // OR E
2418         OR(e,isez80() ? 1 : israbbit() ? 2 : 4);
2419         ih=1;altd=0;ioi=0;ioe=0;break;
2420       case 0xb4: // OR H // OR IXh // OR IYh
2421         if( ih )
2422           OR(h,isez80() ? 1 : israbbit() ? 2 : 4);
2423         else if( iy && canixh() )
2424           OR(yh,isez80() ? 1 : 4);
2425         else if ( canixh() )
2426           OR(xh,isez80() ? 1 : 4);
2427         ih=1;altd=0;ioi=0;ioe=0;break;
2428       case 0xb5: // OR L // OR IXl // OR IYl
2429         if( ih )
2430           OR(l,isez80() ? 1 : israbbit() ? 2 : 4);
2431         else if( iy && canixh() )
2432           OR(yl,isez80() ? 1 : 4);
2433         else if ( canixh() )
2434           OR(xl,isez80() ? 1 : 4);
2435         ih=1;altd=0;ioi=0;ioe=0;break;
2436       case 0xb6: // OR (HL) // OR (IX+d) // OR (IY+d)
2437         if( ih )
2438           OR(get_memory(l|h<<8),isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2439         else if( iy )
2440           OR(get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535),isez80() ? 3 : 15);
2441         else
2442           OR(get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535),isez80() ? 3 : 15);
2443         ih=1;altd=0;ioi=0;ioe=0;break;
2444       case 0xb7: // OR A
2445         st+=isez80() ? 1 : israbbit() ? 2 : 4;
2446         if ( altd ) {
2447           fa_= 256
2448             | (ff_= fr_= a);
2449           fb_= 0;
2450         } else {
2451           fa= 256
2452             | (ff= fr= a);
2453           fb= 0;
2454         }
2455         ih=1;altd=0;ioi=0;ioe=0;break;
2456       case 0xb8: // CP B
2457         CP(b,isez80() ? 1 : israbbit() ? 2 : 4);
2458         ih=1;altd=0;ioi=0;ioe=0;break;
2459       case 0xb9: // CP C
2460         CP(c,isez80() ? 1 : israbbit() ? 2 : 4);
2461         ih=1;altd=0;ioi=0;ioe=0;break;
2462       case 0xba: // CP D
2463         CP(d,isez80() ? 1 : israbbit() ? 2 : 4);
2464         ih=1;altd=0;ioi=0;ioe=0;break;
2465       case 0xbb: // CP E
2466         CP(e,isez80() ? 1 : israbbit() ? 2 : 4);
2467         ih=1;altd=0;ioi=0;ioe=0;break;
2468       case 0xbc: // CP H // CP IXh // CP IYh
2469         if( ih )
2470           CP(h,isez80() ? 1 : israbbit() ? 2 : 4);
2471         else if( iy && canixh() )
2472           CP(yh,isez80() ? 1 : 4);
2473         else if ( canixh() )
2474           CP(xh,isez80() ? 1 : 4);
2475         ih=1;altd=0;ioi=0;ioe=0;break;
2476       case 0xbd: // CP L // CP IXl // CP IYl
2477         if( ih )
2478           CP(l,isez80() ? 1 : israbbit() ? 2 : 4);
2479         else if( iy && canixh() )
2480           CP(yl,isez80() ? 1 : 4);
2481         else if (canixh())
2482           CP(xl,isez80() ? 1 : 4);
2483         ih=1;altd=0;ioi=0;ioe=0;break;
2484       case 0xbe: // CP (HL) // CP (IX+d) // CP (IY+d)
2485         if( ih )
2486           w= get_memory(l|h<<8),
2487           CP(w,isez80() ? 2 : israbbit() ? 5 : isgbz80() ? 8 : 7);
2488         else if( iy )
2489           w= get_memory(((get_memory(pc++)^128)-128+(yl|yh<<8))&65535),
2490           CP(w,isez80() ? 3 : 15);
2491         else
2492           w= get_memory(((get_memory(pc++)^128)-128+(xl|xh<<8))&65535),
2493           CP(w,isez80() ? 3 : 15);
2494         ih=1;altd=0;ioi=0;ioe=0;break;
2495       case 0xbf: // CP A
2496         st+=isez80() ? 1 : israbbit() ? 2 : 4;
2497         if ( altd ) {
2498           fr_= 0;
2499           fb_= ~(fa_= a);
2500           ff_= a&40;
2501         } else {
2502           fr= 0;
2503           fb= ~(fa= a);
2504           ff= a&40;
2505         }
2506         ih=1;altd=0;ioi=0;ioe=0;break;
2507       case 0xc9: // RET
2508         RET(isez80() ? 5 : israbbit() ?  8 : isz180() ? 9 : isgbz80() ? 8 : 10);
2509         ih=1;altd=0;ioi=0;ioe=0;break;
2510       case 0xc0: // RET NZ
2511         RETCI(fr);
2512         ih=1;altd=0;ioi=0;ioe=0;break;
2513       case 0xc8: // RET Z
2514         RETC(fr);
2515         ih=1;altd=0;ioi=0;ioe=0;break;
2516       case 0xd0: // RET NC
2517         RETC(ff&256);
2518         ih=1;altd=0;ioi=0;ioe=0;break;
2519       case 0xd8: // RET C
2520         RETCI(ff&256);
2521         ih=1;altd=0;ioi=0;ioe=0;break;
2522       case 0xe0: // RET PO
2523 		if (isgbz80()) { // LDH (n),A - I/O
2524 		  t = get_memory(pc++);
2525 		  put_memory(0xFF00 + t, a);
2526 		  st+= 12;
2527 		} else {
2528           RETC(fa&256?38505>>((fr^fr>>4)&15)&1:(fr^fa)&(fr^fb)&128);
2529 		}
2530         ih=1;altd=0;ioi=0;ioe=0;break;
2531       case 0xe8: // RET PE
2532         if ( isgbz80()) {  // add sp,d
2533           st += 16;
2534           sp += (get_memory(pc++)^128)-128;
2535           break;
2536         }
2537         RETCI(fa&256?38505>>((fr^fr>>4)&15)&1:(fr^fa)&(fr^fb)&128);
2538         ih=1;altd=0;ioi=0;ioe=0;break;
2539       case 0xf0: // RET P
2540   	    if (isgbz80()) { // LDH A, (n) - I/O
2541 		  t = get_memory(pc++);
2542 		  a = get_memory(0xFF00 + t);
2543 		  st+= 12;
2544 		} else {
2545           RETC(ff&128);
2546 		}
2547         ih=1;altd=0;ioi=0;ioe=0;break;
2548       case 0xf8: // RET M
2549         if ( isgbz80() ) {  // ld hl,sp+d
2550           st += 12;
2551           t = (sp + (get_memory(pc++)^128)-128) & 0xffff;
2552           h = t / 256;
2553           l = t % 256;
2554           break;
2555         }
2556         RETCI(ff&128);
2557         ih=1;altd=0;ioi=0;ioe=0;break;
2558       case 0xc1: // POP BC
2559         POP(b, c);
2560         ih=1;altd=0;ioi=0;ioe=0;break;
2561       case 0xd1: // POP DE
2562         POP(d, e);
2563         ih=1;altd=0;ioi=0;ioe=0;break;
2564       case 0xe1: // POP HL // POP IX // POP IY
2565         if( ih )
2566           POP(h, l);
2567         else if( iy )
2568           POP(yh, yl);
2569         else
2570           POP(xh, xl);
2571         ih=1;altd=0;ioi=0;ioe=0;break;
2572       case 0xf1: // POP AF
2573         st+= isez80() ? 3 : isgbz80() ? 12 : israbbit() ? 7 : isz180() ? 9 : 10;
2574         setf(get_memory(sp++));
2575         a= get_memory(sp++);
2576         ih=1;altd=0;ioi=0;ioe=0;break;
2577       case 0xc5: // PUSH BC
2578         PUSH(b, c);
2579         ih=1;altd=0;ioi=0;ioe=0;break;
2580       case 0xd5: // PUSH DE
2581         PUSH(d, e);
2582         ih=1;altd=0;ioi=0;ioe=0;break;
2583       case 0xe5: // PUSH HL // PUSH IX // PUSH IY
2584         if( ih )
2585           PUSH(h, l);
2586         else if( iy )
2587           PUSH(yh, yl);
2588         else
2589           PUSH(xh, xl);
2590         ih=1;altd=0;ioi=0;ioe=0;break;
2591       case 0xf5: // PUSH AF
2592         PUSH(a, f());
2593         ih=1;altd=0;ioi=0;ioe=0;break;
2594       case 0xc3: // JP nn
2595         st+= isez80() ? 4 : israbbit() ? 3 : israbbit() ? 7 : isz180() ? 9 : isgbz80() ? 12 : 10;
2596         mp= pc= get_memory(pc) | get_memory(pc+1)<<8;
2597         ih=1;altd=0;ioi=0;ioe=0;break;
2598       case 0xc2: // JP NZ
2599         JPCI(fr);
2600         ih=1;altd=0;ioi=0;ioe=0;break;
2601       case 0xca: // JP Z
2602         JPC(fr);
2603         ih=1;altd=0;ioi=0;ioe=0;break;
2604       case 0xd2: // JP NC
2605         JPC(ff&256);
2606         ih=1;altd=0;ioi=0;ioe=0;break;
2607       case 0xda: // JP C
2608         JPCI(ff&256);
2609         ih=1;altd=0;ioi=0;ioe=0;break;
2610       case 0xe2: // JP PO
2611 		if (isgbz80()) { // LD (C), A - I/O
2612 		  put_memory(0xFF00+c,a);
2613 		  st+= 8;
2614 		} else {
2615           JPC(fa&256?38505>>((fr^fr>>4)&15)&1:(fr^fa)&(fr^fb)&128);
2616 		}
2617         ih=1;altd=0;ioi=0;ioe=0;break;
2618       case 0xea: // JP PE
2619         if ( isgbz80() ) {  // ld (nn),a
2620           st+= 16;
2621           t= get_memory(pc++);
2622           put_memory(t|= get_memory(pc++)<<8,a);
2623           mp= t+1 & 255
2624              | a<<8;
2625           ih=1;altd=0;ioi=0;ioe=0;break;
2626           break;
2627         }
2628         JPCI(fa&256?38505>>((fr^fr>>4)&15)&1:(fr^fa)&(fr^fb)&128);
2629         ih=1;altd=0;ioi=0;ioe=0;break;
2630       case 0xf2: // JP P
2631 		if (isgbz80()) { // LD A, (C), A
2632 		  a= get_memory(0xFF00+c);
2633 		  st+= 8;
2634 		} else {
2635           JPC(ff&128);
2636         }
2637 		ih=1;altd=0;ioi=0;ioe=0;break;
2638       case 0xfa: // JP M
2639         if (isgbz80()) {  // ld a,(nn)
2640           st+= 16;
2641           mp= get_memory(pc++);
2642           if ( altd ) a_ = get_memory(mp|= get_memory(pc++)<<8);
2643           else a= get_memory(mp|= get_memory(pc++)<<8);
2644           ++mp;
2645           ih=1;altd=0;ioi=0;ioe=0;break;
2646         }
2647         JPCI(ff&128);
2648         ih=1;altd=0;ioi=0;ioe=0;break;
2649       case 0xcd: // CALL nn
2650         st+= isez80() ? 5 : israbbit() ? 12 : isz180() ? 16 : is8085() ? 18 : isgbz80() ? 12 : 17;
2651         t= pc+2;
2652         mp= pc= get_memory(pc) | get_memory(pc+1)<<8;
2653         put_memory(--sp,t>>8);
2654         put_memory(--sp,t);
2655         ih=1;altd=0;ioi=0;ioe=0;break;
2656       case 0xc4: // CALL NZ / (RCM) LD HL,(SP+N)
2657         if ( israbbit()) {
2658           int     offset = sp + get_memory(pc++);
2659           st += 9;
2660           if ( ih ) {
2661             l = get_memory(offset++);
2662             h = get_memory(offset);
2663           } else if ( iy ) {
2664             yl = get_memory(offset++);
2665             yh = get_memory(offset);
2666           } else {
2667             xl = get_memory(offset++);
2668             xh = get_memory(offset);
2669           }
2670         } else {
2671           CALLCI(fr);
2672         }
2673         ih=1;altd=0;ioi=0;ioe=0;break;
2674       case 0xcc: // CALL Z / (RCM) BOOL HL
2675         if ( israbbit()) {
2676           if ( ih ) {
2677             BOOL(h,l, h_, l_, altd);
2678           } else if ( iy ) {
2679             BOOL(yh, yl, yh, yl, 0);
2680           } else {
2681             BOOL(xh,xl,xh,xl, 0);
2682           }
2683           st += 2;
2684         } else {
2685           CALLC(fr);
2686         }
2687         ih=1;altd=0;ioi=0;ioe=0;break;
2688       case 0xd4: // CALL NC / (RCM) LD (SP+N),HL
2689         if ( israbbit()) {
2690           int     offset = sp + get_memory(pc++);
2691           st += 9;
2692 
2693           if ( ih ) {
2694             put_memory(offset++,l);
2695             put_memory(offset, h);
2696           } else if ( iy ) {
2697             put_memory(offset++,yl);
2698             put_memory(offset, yh);
2699           } else {
2700             put_memory(offset++,xl);
2701             put_memory(offset, xh);
2702           }
2703         } else {
2704           CALLC(ff&256);
2705         }
2706         ih=1;altd=0;ioi=0;ioe=0;break;
2707       case 0xdc: // CALL C / (RCM) AND HL,DE
2708         if ( israbbit()) {
2709           if ( ih ) {
2710             AND2(h,d,h_);
2711             AND2(l,e,e_);
2712           } else if ( iy ) {
2713             AND2(yh, d, yh);
2714             AND2(yl, e, yl);
2715           } else {
2716             AND2(xh, d, xh);
2717             AND2(xl, e, xl);
2718           }
2719           st += 2;
2720         } else {
2721           CALLCI(ff&256);
2722         }
2723         ih=1;altd=0;ioi=0;ioe=0;break;
2724       case 0xe4: // CALL PO / (RCM) LD HL,(IX+D)
2725         if ( israbbit()) {
2726           t = (get_memory(pc++)^128)-128;
2727           st += 11;
2728           if ( ih ) {    // ld hl,(ix+d)
2729             l = get_memory(t+(xl|xh<<8));
2730             h = get_memory(t+(xl|xh<<8) + 1);
2731           } else if ( iy ) { // ld hl,(iy+d)
2732             l = get_memory(t+(yl|yh<<8));
2733             h = get_memory(t+(yl|yh<<8) + 1);
2734           } else { // ld hl,(hl+d)
2735             unsigned char lt;
2736             lt = get_memory(t+(l|h<<8));
2737             h = get_memory(t+(l|h<<8) + 1);
2738             l = lt;
2739           }
2740 		} else if (isgbz80()) {
2741 		  printf("%04x: ILLEGAL gbz80 instruction E4\n", pc - 1);
2742         } else {
2743           CALLC(fa&256?38505>>((fr^fr>>4)&15)&1:(fr^fa)&(fr^fb)&128);
2744         }
2745         ih=1;altd=0;ioi=0;ioe=0;break;
2746       case 0xec: // CALL PE / (RCM) OR HL,DE
2747         if ( israbbit()) {
2748           if ( ih ) {
2749             OR2(h,d);
2750             OR2(l,e);
2751           } else if ( iy ) {
2752             OR2(yh, d);
2753             OR2(yl, e);
2754           } else {
2755             OR2(xh, d);
2756             OR2(xl, e);
2757           }
2758           st += 2;
2759 		} else if (isgbz80()) {
2760 		  printf("%04x: ILLEGAL gbz80 instruction EC\n", pc - 1);
2761         } else {
2762           CALLCI(fa&256?38505>>((fr^fr>>4)&15)&1:(fr^fa)&(fr^fb)&128);
2763         }
2764         ih=1;altd=0;ioi=0;ioe=0;break;
2765       case 0xf4: // CALL P or (RCM) LD (IX+D),HL
2766         if ( israbbit()) {
2767           t = (get_memory(pc++)^128)-128;
2768           st += 11;
2769           if ( ih ) {    // ld (ix+d),hl
2770             put_memory(t+(xl|xh<<8),l);
2771             put_memory(t+(xl|xh<<8) + 1,h);
2772           } else if ( iy ) { // ld (iy+d),hl
2773             put_memory(t+(yl|yh<<8),l);
2774             put_memory(t+(yl|yh<<8) + 1,h);
2775           } else { // ld (hl+d),hl
2776             int addr = t+(l|h<<8);
2777             put_memory(addr,l);
2778             put_memory(addr + 1, h);
2779           }
2780 		} else if (isgbz80()) {
2781 		  printf("%04x: ILLEGAL gbz80 instruction F4\n", pc - 1);
2782         } else {
2783           CALLC(ff&128);
2784         }
2785         ih=1;altd=0;ioi=0;ioe=0;break;
2786       case 0xfc: // CALL M  / (RCM) RR HL
2787         if ( israbbit()) {
2788           long long savest = st;
2789           if ( ih ) {
2790             RR(h);
2791             RR(l);
2792           } else if ( iy ) {
2793             RR(yh);
2794             RR(yl);
2795           } else {
2796             RR(xh);
2797             RR(xl);
2798           }
2799           st = savest;
2800           st += 2;
2801 		} else if (isgbz80()) {
2802 		  printf("%04x: ILLEGAL gbz80 instruction FC\n", pc - 1);
2803         } else {
2804           CALLCI(ff&128);
2805         }
2806         ih=1;altd=0;ioi=0;ioe=0;
2807         break;
2808       case 0xc6: // ADD A,n
2809         ADD(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2810         ih=1;altd=0;ioi=0;ioe=0;break;
2811       case 0xce: // ADC A,n
2812         ADC(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2813         ih=1;altd=0;ioi=0;ioe=0;break;
2814       case 0xd6: // SUB n
2815         SUB(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2816         ih=1;altd=0;ioi=0;ioe=0;break;
2817       case 0xde: // SBC A,n
2818         SBC(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2819         ih=1;altd=0;ioi=0;ioe=0;break;
2820       case 0xe6: // AND n
2821         AND(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2822         ih=1;altd=0;ioi=0;ioe=0;break;
2823       case 0xee: // XOR A,n
2824         XOR(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2825         ih=1;altd=0;ioi=0;ioe=0;break;
2826       case 0xf6: // OR n
2827         OR(get_memory(pc++), isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2828         ih=1;altd=0;ioi=0;ioe=0;break;
2829       case 0xfe: // CP A,n
2830         w= get_memory(pc++);
2831         CP(w, isez80() ? 2 : israbbit() ? 4 : isgbz80() ? 8 : 7);
2832         ih=1;altd=0;ioi=0;ioe=0;break;
2833       case 0xc7: // RST 0x00  (RCM) LJP
2834         RST(0);
2835         ih=1;altd=0;ioi=0;ioe=0;break;
2836       case 0xcf: // RST 0x08 (RCM) LCALL
2837         RST(8);
2838         ih=1;altd=0;ioi=0;ioe=0;break;
2839       case 0xd7: // RST 0x10
2840         RST(0x10);
2841         ih=1;altd=0;ioi=0;ioe=0;break;
2842       case 0xdf: // RST 0x18
2843         RST(0x18);
2844         ih=1;altd=0;ioi=0;ioe=0;break;
2845       case 0xe7: // RST 0x20
2846         RST(0x20);
2847         ih=1;altd=0;ioi=0;ioe=0;break;
2848       case 0xef: // RST 0x28
2849         RST(0x28);
2850         ih=1;altd=0;ioi=0;ioe=0;break;
2851       case 0xf7: // RST 0x30, (RCM) mul
2852         if ( israbbit()) {
2853           // HL:BC = BC • DE
2854           int32_t result = (( d * 256 ) + e) * (( b * 256 ) + c);
2855           h = (result >> 24);
2856           l = (result >> 16) & 0xff;
2857           b  = (result >> 8 ) & 0xff;
2858           c = result & 0xff;
2859           st += 12;
2860         } else {
2861           RST(0x30);
2862         }
2863         ih=1;altd=0;ioi=0;ioe=0;break;
2864       case 0xff: // RST 0x38
2865         RST(0x38);
2866         ih=1;altd=0;ioi=0;ioe=0;break;
2867       case 0xd3: // OUT (n),A
2868         if ( isgbz80()) {
2869         } else if ( israbbit()) {
2870           ioi=1;
2871           st+=2;
2872         } else {
2873           st+= is808x() ? 10 : 11;
2874           out(mp= get_memory(pc++) | a<<8, a);
2875           mp= mp&65280
2876             | ++mp;
2877           ih=1;altd=0;ioi=0;ioe=0;
2878         }
2879         break;
2880       case 0xdb: // IN A,(n) // (RCM) ioe
2881         if ( isgbz80() ) {
2882 
2883         } else if ( israbbit()) {
2884           ioe=1;
2885           st+=2;
2886         } else {
2887           st+= is808x() ? 10 : 11;
2888           a= in(mp= get_memory(pc++) | a<<8);
2889           ++mp;
2890           ih=1;altd=0;ioi=0;ioe=0;
2891         }
2892         break;
2893       case 0xf3: // DI  / (RCM) RL DE
2894         if ( israbbit()) {
2895           long long savest = st;
2896           RL(e);
2897           RL(d);
2898           st = savest;
2899           st += 2;
2900         } else {
2901           st+= isez80() ? 1 : isz180() ? 3 : 4;
2902           iff= 0;
2903           ih=1;altd=0;ioi=0;ioe=0;
2904         }
2905         break;
2906       case 0xfb: // EI / (RCM) RR DE
2907         if ( israbbit()) {
2908           long long savest = st;
2909           RR(d);
2910           RR(e);
2911           st = savest;
2912           st += 2;
2913         } else {
2914           st+= isez80() ? 1 : isz180() ? 3 : 4;
2915           iff= 1;
2916         }
2917         ih=1;altd=0;ioi=0;ioe=0;
2918         break;
2919       case 0xeb: // EX DE,HL
2920         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
2921         if (altd) {
2922             t = d;
2923             d = h_;
2924             h_ = t;
2925             t = e;
2926             e = l_;
2927             l_ = t;
2928         }
2929         else {
2930             t = d;
2931             d = h;
2932             h = t;
2933             t = e;
2934             e = l;
2935             l = t;
2936         }
2937         ih=1;altd=0;ioi=0;ioe=0;break;
2938       case 0xd9: // EXX
2939         if ( is8085() ) {  // (8085) ld (de),hl (SHLX)
2940           put_memory((e | d<<8),l);
2941           put_memory((e | d<<8) + 1,h);
2942           st+=10;
2943           break;
2944         } else if ( is8080() ) {
2945           printf("%04x: ILLEGAL 8080 instruction EXX\n",pc-1);
2946           RET(isez80() ? 5 : israbbit() ?  8 : isz180() ? 9 : 10);
2947           ih=1;altd=0;ioi=0;ioe=0;
2948           break;
2949         } else if ( isgbz80() ) {  // RETI
2950           RET(isgbz80() ? 8 : 16); break;
2951         }
2952         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
2953         t = b;
2954         b = b_;
2955         b_= t;
2956         t = c;
2957         c = c_;
2958         c_= t;
2959         t = d;
2960         d = d_;
2961         d_= t;
2962         t = e;
2963         e = e_;
2964         e_= t;
2965         t = h;
2966         h = h_;
2967         h_= t;
2968         t = l;
2969         l = l_;
2970         l_= t;
2971         ih=1;altd=0;ioi=0;ioe=0;break;
2972       case 0xe3: // EX (SP),HL // EX (SP),IX // EX (SP),IY or (RCM) EX DE',HL
2973         if ( isgbz80() ) {
2974           printf("%04x: ILLEGAL GBZ80 instruction EX (SP),HL\n",pc-1);
2975         } else if ( israbbit() && ih ) {
2976             if (altd) {
2977                 t = h_;
2978                 h_ = d_;
2979                 d_ = t;
2980                 t = l_;
2981                 l_ = e_;
2982                 e_ = t;
2983             }
2984             else {
2985                 t = h;
2986                 h = d_;
2987                 d_ = t;
2988                 t = l;
2989                 l = e_;
2990                 e_ = t;
2991             }
2992             st += 2;
2993         } else {
2994           if( ih )
2995             EXSPI(h, l);
2996           else if( iy )
2997             EXSPI(yh, yl);
2998           else
2999             EXSPI(xh, xl);
3000         }
3001         ih=1;altd=0;ioi=0;ioe=0;break;
3002       case 0xe9: // JP (HL)
3003         st+= isez80() ? 3 : isz180() ? 3 : is8085() ? 6 : is8080() ? 5 : 4;
3004         if( ih )
3005           pc= l | h<<8;
3006         else if( iy )
3007           pc= yl | yh<<8;
3008         else
3009           pc= xl | xh<<8;
3010         ih=1;altd=0;ioi=0;ioe=0;break;
3011       case 0xf9: // LD SP,HL
3012         st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 4 : is8085() ? 6  : is8080() ? 5 : isgbz80() ? 8 : 6;
3013         if( ih )
3014           sp= l | h<<8;
3015         else if( iy )
3016           sp= yl | yh<<8;
3017         else
3018           sp= xl | xh<<8;
3019         ih=1;altd=0;ioi=0;ioe=0;break;
3020       case 0xdd: // OP DD
3021         if ( is8085() ) { // (8085) JP NK,nnnn (JNK nnnn)
3022           // K flag is bit 5 of flags (not emulated since we don't use it)
3023           pc+=2;
3024           st+=7;
3025         } else if ( is8080() ) {
3026           printf("%04x: ILLEGAL 8080 prefix 0xDD\n",pc-1);
3027           st+= isez80() ? 5 : israbbit() ? 12 : isz180() ? 16 : 17;
3028           t= pc+2;
3029           mp= pc= get_memory(pc) | get_memory(pc+1)<<8;
3030           put_memory(--sp,t>>8);
3031           put_memory(--sp,t);
3032           ih=1;altd=0;ioi=0;ioe=0;
3033         } else if ( isgbz80() ) {
3034           printf("%04x: ILLEGAL GBZ80 prefix 0xDD\n",pc-1);
3035         } else {
3036           st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
3037           ih= iy= 0;
3038         }
3039         break;
3040       case 0xfd: // OP FD
3041         if ( is8085() ) { // (8085) JP K,nnnn (JK nnnn)
3042           // K flag is bit 5 of flags (not emulated since we don't use it)
3043           pc+=2;
3044           st+=7;
3045         } else if ( is808x() ) {
3046           printf("%04x: ILLEGAL 8080 prefix 0xFD\n",pc-1);
3047           st+= isez80() ? 5 : israbbit() ? 12 : isz180() ? 16 : 17;
3048           t= pc+2;
3049           mp= pc= get_memory(pc) | get_memory(pc+1)<<8;
3050           put_memory(--sp,t>>8);
3051           put_memory(--sp,t);
3052           ih=1;altd=0;ioi=0;ioe=0;
3053         } else if ( isgbz80() ) {
3054           printf("%04x: ILLEGAL GBZ80 prefix 0xFD\n",pc-1);
3055         } else {
3056           st+= isez80() ? 1 : israbbit() ? 2 : isz180() ? 3 : 4;
3057           ih= 0;
3058           iy= 1;
3059         }
3060         break;
3061       case 0xcb: // OP CB
3062 		if (is8085()) {		// (8085) RSTV, OVRST8
3063 		  // V flag is bit 1 of flags (not emulated since we don't use it)
3064 		  st += 6;
3065 		} else if ( is808x() ) {
3066           printf("%04x: ILLEGAL 8080 prefix 0xCB\n",pc-1);
3067           st+= isez80() ? 4 : israbbit() ? 3 : israbbit() ? 7 : isz180() ? 9 : 10;
3068           mp= pc= get_memory(pc) | get_memory(pc+1)<<8;
3069           ih=1;altd=0;ioi=0;ioe=0;break;
3070           break;
3071         }
3072         r++;
3073         if( ih )
3074           switch( get_memory(pc++) ){
3075             case 0x00:  RLC(b); break;                       // RLC B
3076             case 0x01:  RLC(c); break;                       // RLC C
3077             case 0x02:  RLC(d); break;                       // RLC D
3078             case 0x03:  RLC(e); break;                       // RLC E
3079             case 0x04:  RLC(h); break;                       // RLC H
3080             case 0x05:  RLC(l); break;                       // RLC L
3081             case 0x06:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // RLC (HL)
3082                         t= l|h<<8;
3083                         u=get_memory(t);
3084                         RLC(u);
3085                         put_memory(t, u); break;
3086             case 0x07:  RLC(a); break;                       // RLC A
3087             case 0x08:  RRC(b); break;                       // RRC B
3088             case 0x09:  RRC(c); break;                       // RRC C
3089             case 0x0a:  RRC(d); break;                       // RRC D
3090             case 0x0b:  RRC(e); break;                       // RRC E
3091             case 0x0c:  RRC(h); break;                       // RRC H
3092             case 0x0d:  RRC(l); break;                       // RRC L
3093             case 0x0e:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // RRC (HL)
3094                         t= l|h<<8;
3095                         u=get_memory(t);
3096                         RRC(u);
3097                         put_memory(t, u); break;
3098             case 0x0f:  RRC(a); break;                       // RRC A
3099             case 0x10:  RL(b); break;                        // RL B
3100             case 0x11:  RL(c); break;                        // RL C
3101             case 0x12:  RL(d); break;                        // RL D
3102             case 0x13:  RL(e); break;                        // RL E
3103             case 0x14:  RL(h); break;                        // RL H
3104             case 0x15:  RL(l); break;                        // RL L
3105             case 0x16:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // RL (HL)
3106                         t= l|h<<8;
3107                         u=get_memory(t);
3108                         RL(u);
3109                         put_memory(t, u); break;
3110             case 0x17:  RL(a); break;                        // RL A
3111             case 0x18:  RR(b); break;                        // RR B
3112             case 0x19:  RR(c); break;                        // RR C
3113             case 0x1a:  RR(d); break;                        // RR D
3114             case 0x1b:  RR(e); break;                        // RR E
3115             case 0x1c:  RR(h); break;                        // RR H
3116             case 0x1d:  RR(l); break;                        // RR L
3117             case 0x1e:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // RR (HL)
3118                         t= l|h<<8;
3119                         u=get_memory(t);
3120                         u=get_memory(t);
3121                         RR(u);
3122                         put_memory(t, u); break;
3123             case 0x1f:  RR(a); break;                        // RR A
3124             case 0x20:  SLA(b); break;                       // SLA B
3125             case 0x21:  SLA(c); break;                       // SLA C
3126             case 0x22:  SLA(d); break;                       // SLA D
3127             case 0x23:  SLA(e); break;                       // SLA E
3128             case 0x24:  SLA(h); break;                       // SLA H
3129             case 0x25:  SLA(l); break;                       // SLA L
3130             case 0x26:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // SLA (HL)
3131                         t= l|h<<8;
3132                         u=get_memory(t);
3133                         SLA(u);
3134                         put_memory(t, u); break;
3135             case 0x27:  SLA(a); break;                       // SLA A
3136             case 0x28:  SRA(b); break;                       // SRA B
3137             case 0x29:  SRA(c); break;                       // SRA C
3138             case 0x2a:  SRA(d); break;                       // SRA D
3139             case 0x2b:  SRA(e); break;                       // SRA E
3140             case 0x2c:  SRA(h); break;                       // SRA H
3141             case 0x2d:  SRA(l); break;                       // SRA L
3142             case 0x2e:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // SRA (HL)
3143                         t= l|h<<8;
3144                         u=get_memory(t);
3145                         SRA(u);
3146                         put_memory(t, u); break;
3147             case 0x2f:  SRA(a); break;                       // SRA A
3148             case 0x30:  if (isgbz80()) { SWAP(b); } else { SLL(b); } break;                       // SLL B,  SWAP B (gbz80)
3149             case 0x31:  if (isgbz80()) { SWAP(c); } else { SLL(c); } break;                       // SLL C,  SWAP C (gbz80)
3150             case 0x32:  if (isgbz80()) { SWAP(d); } else { SLL(d); } break;                       // SLL D,  SWAP D (gbz80)
3151             case 0x33:  if (isgbz80()) { SWAP(e); } else { SLL(e); } break;                       // SLL E,  SWAP E (gbz80)
3152             case 0x34:  if (isgbz80()) { SWAP(h); } else { SLL(h); } break;                       // SLL H,  SWAP H (gbz80)
3153             case 0x35:  if (isgbz80()) { SWAP(l); } else { SLL(l); } break;                       // SLL L,  SWAP L (gbz80)
3154             case 0x36:                                       // SLL (HL),  SWAP (hl) (gbz80)
3155                         if ( isgbz80() ) {
3156                           st += 8;
3157                           t= l|h<<8;
3158                           u=get_memory(t);
3159                           SWAP(u);
3160                           put_memory(t, u);
3161                         } else if (cansll() ) {
3162                           st+= 7;
3163                           t= l|h<<8;
3164                           u=get_memory(t);
3165                           SLL(u);
3166                           put_memory(t, u);
3167                         }
3168                         break;
3169             case 0x37:  if (isgbz80()) { SWAP(a); } else { SLL(a); } break;                       // SLL A,  SWAP A (gbz80)
3170             case 0x38:  SRL(b); break;                       // SRL B
3171             case 0x39:  SRL(c); break;                       // SRL C
3172             case 0x3a:  SRL(d); break;                       // SRL D
3173             case 0x3b:  SRL(e); break;                       // SRL E
3174             case 0x3c:  SRL(h); break;                       // SRL H
3175             case 0x3d:  SRL(l); break;                       // SRL L
3176             case 0x3e:  st+= israbbit() ? 6 : isgbz80() ? 8 : isz180() ? 6 : 7;             // SRL (HL)
3177                         t= l|h<<8;
3178                         u=get_memory(t);
3179                         SRL(u);
3180                         put_memory(t, u); break;
3181             case 0x3f:  SRL(a); break;                       // SRL A
3182             case 0x40:  BIT(1, b); break;                    // BIT 0,B
3183             case 0x41:  BIT(1, c); break;                    // BIT 0,C
3184             case 0x42:  BIT(1, d); break;                    // BIT 0,D
3185             case 0x43:  BIT(1, e); break;                    // BIT 0,E
3186             case 0x44:  BIT(1, h); break;                    // BIT 0,H
3187             case 0x45:  BIT(1, l); break;                    // BIT 0,L
3188             case 0x46:  BITHL(1); break;                     // BIT 0,(HL)
3189             case 0x47:  BIT(1, a); break;                    // BIT 0,A
3190             case 0x48:  BIT(2, b); break;                    // BIT 1,B
3191             case 0x49:  BIT(2, c); break;                    // BIT 1,C
3192             case 0x4a:  BIT(2, d); break;                    // BIT 1,D
3193             case 0x4b:  BIT(2, e); break;                    // BIT 1,E
3194             case 0x4c:  BIT(2, h); break;                    // BIT 1,H
3195             case 0x4d:  BIT(2, l); break;                    // BIT 1,L
3196             case 0x4e:  BITHL(2); break;                     // BIT 1,(HL)
3197             case 0x4f:  BIT(2, a); break;                    // BIT 1,A
3198             case 0x50:  BIT(4, b); break;                    // BIT 2,B
3199             case 0x51:  BIT(4, c); break;                    // BIT 2,C
3200             case 0x52:  BIT(4, d); break;                    // BIT 2,D
3201             case 0x53:  BIT(4, e); break;                    // BIT 2,E
3202             case 0x54:  BIT(4, h); break;                    // BIT 2,H
3203             case 0x55:  BIT(4, l); break;                    // BIT 2,L
3204             case 0x56:  BITHL(4); break;                     // BIT 2,(HL)
3205             case 0x57:  BIT(4, a); break;                    // BIT 2,A
3206             case 0x58:  BIT(8, b); break;                    // BIT 3,B
3207             case 0x59:  BIT(8, c); break;                    // BIT 3,C
3208             case 0x5a:  BIT(8, d); break;                    // BIT 3,D
3209             case 0x5b:  BIT(8, e); break;                    // BIT 3,E
3210             case 0x5c:  BIT(8, h); break;                    // BIT 3,H
3211             case 0x5d:  BIT(8, l); break;                    // BIT 3,L
3212             case 0x5e:  BITHL(8); break;                     // BIT 3,(HL)
3213             case 0x5f:  BIT(8, a); break;                    // BIT 3,A
3214             case 0x60:  BIT(16, b); break;                   // BIT 4,B
3215             case 0x61:  BIT(16, c); break;                   // BIT 4,C
3216             case 0x62:  BIT(16, d); break;                   // BIT 4,D
3217             case 0x63:  BIT(16, e); break;                   // BIT 4,E
3218             case 0x64:  BIT(16, h); break;                   // BIT 4,H
3219             case 0x65:  BIT(16, l); break;                   // BIT 4,L
3220             case 0x66:  BITHL(16); break;                    // BIT 4,(HL)
3221             case 0x67:  BIT(16, a); break;                   // BIT 4,A
3222             case 0x68:  BIT(32, b); break;                   // BIT 5,B
3223             case 0x69:  BIT(32, c); break;                   // BIT 5,C
3224             case 0x6a:  BIT(32, d); break;                   // BIT 5,D
3225             case 0x6b:  BIT(32, e); break;                   // BIT 5,E
3226             case 0x6c:  BIT(32, h); break;                   // BIT 5,H
3227             case 0x6d:  BIT(32, l); break;                   // BIT 5,L
3228             case 0x6e:  BITHL(32); break;                    // BIT 5,(HL)
3229             case 0x6f:  BIT(32, a); break;                   // BIT 5,A
3230             case 0x70:  BIT(64, b); break;                   // BIT 6,B
3231             case 0x71:  BIT(64, c); break;                   // BIT 6,C
3232             case 0x72:  BIT(64, d); break;                   // BIT 6,D
3233             case 0x73:  BIT(64, e); break;                   // BIT 6,E
3234             case 0x74:  BIT(64, h); break;                   // BIT 6,H
3235             case 0x75:  BIT(64, l); break;                   // BIT 6,L
3236             case 0x76:  BITHL(64); break;                    // BIT 6,(HL)
3237             case 0x77:  BIT(64, a); break;                   // BIT 6,A
3238             case 0x78:  BIT(128, b); break;                  // BIT 7,B
3239             case 0x79:  BIT(128, c); break;                  // BIT 7,C
3240             case 0x7a:  BIT(128, d); break;                  // BIT 7,D
3241             case 0x7b:  BIT(128, e); break;                  // BIT 7,E
3242             case 0x7c:  BIT(128, h); break;                  // BIT 7,H
3243             case 0x7d:  BIT(128, l); break;                  // BIT 7,L
3244             case 0x7e:  BITHL(128); break;                   // BIT 7,(HL)
3245             case 0x7f:  BIT(128, a); break;                  // BIT 7,A
3246             case 0x80:  RES(254, b); break;                  // RES 0,B
3247             case 0x81:  RES(254, c); break;                  // RES 0,C
3248             case 0x82:  RES(254, d); break;                  // RES 0,D
3249             case 0x83:  RES(254, e); break;                  // RES 0,E
3250             case 0x84:  RES(254, h); break;                  // RES 0,H
3251             case 0x85:  RES(254, l); break;                  // RES 0,L
3252             case 0x86:  RESHL(254); break;                   // RES 0,(HL)
3253             case 0x87:  RES(254, a); break;                  // RES 0,A
3254             case 0x88:  RES(253, b); break;                  // RES 1,B
3255             case 0x89:  RES(253, c); break;                  // RES 1,C
3256             case 0x8a:  RES(253, d); break;                  // RES 1,D
3257             case 0x8b:  RES(253, e); break;                  // RES 1,E
3258             case 0x8c:  RES(253, h); break;                  // RES 1,H
3259             case 0x8d:  RES(253, l); break;                  // RES 1,L
3260             case 0x8e:  RESHL(253); break;                   // RES 1,(HL)
3261             case 0x8f:  RES(253, a); break;                  // RES 1,A
3262             case 0x90:  RES(251, b); break;                  // RES 2,B
3263             case 0x91:  RES(251, c); break;                  // RES 2,C
3264             case 0x92:  RES(251, d); break;                  // RES 2,D
3265             case 0x93:  RES(251, e); break;                  // RES 2,E
3266             case 0x94:  RES(251, h); break;                  // RES 2,H
3267             case 0x95:  RES(251, l); break;                  // RES 2,L
3268             case 0x96:  RESHL(251); break;                   // RES 2,(HL)
3269             case 0x97:  RES(251, a); break;                  // RES 2,A
3270             case 0x98:  RES(247, b); break;                  // RES 3,B
3271             case 0x99:  RES(247, c); break;                  // RES 3,C
3272             case 0x9a:  RES(247, d); break;                  // RES 3,D
3273             case 0x9b:  RES(247, e); break;                  // RES 3,E
3274             case 0x9c:  RES(247, h); break;                  // RES 3,H
3275             case 0x9d:  RES(247, l); break;                  // RES 3,L
3276             case 0x9e:  RESHL(247); break;                   // RES 3,(HL)
3277             case 0x9f:  RES(247, a); break;                  // RES 3,A
3278             case 0xa0:  RES(239, b); break;                  // RES 4,B
3279             case 0xa1:  RES(239, c); break;                  // RES 4,C
3280             case 0xa2:  RES(239, d); break;                  // RES 4,D
3281             case 0xa3:  RES(239, e); break;                  // RES 4,E
3282             case 0xa4:  RES(239, h); break;                  // RES 4,H
3283             case 0xa5:  RES(239, l); break;                  // RES 4,L
3284             case 0xa6:  RESHL(239); break;                   // RES 4,(HL)
3285             case 0xa7:  RES(239, a); break;                  // RES 4,A
3286             case 0xa8:  RES(223, b); break;                  // RES 5,B
3287             case 0xa9:  RES(223, c); break;                  // RES 5,C
3288             case 0xaa:  RES(223, d); break;                  // RES 5,D
3289             case 0xab:  RES(223, e); break;                  // RES 5,E
3290             case 0xac:  RES(223, h); break;                  // RES 5,H
3291             case 0xad:  RES(223, l); break;                  // RES 5,L
3292             case 0xae:  RESHL(223); break;                   // RES 5,(HL)
3293             case 0xaf:  RES(223, a); break;                  // RES 5,A
3294             case 0xb0:  RES(191, b); break;                  // RES 6,B
3295             case 0xb1:  RES(191, c); break;                  // RES 6,C
3296             case 0xb2:  RES(191, d); break;                  // RES 6,D
3297             case 0xb3:  RES(191, e); break;                  // RES 6,E
3298             case 0xb4:  RES(191, h); break;                  // RES 6,H
3299             case 0xb5:  RES(191, l); break;                  // RES 6,L
3300             case 0xb6:  RESHL(191); break;                   // RES 6,(HL)
3301             case 0xb7:  RES(191, a); break;                  // RES 6,A
3302             case 0xb8:  RES(127, b); break;                  // RES 7,B
3303             case 0xb9:  RES(127, c); break;                  // RES 7,C
3304             case 0xba:  RES(127, d); break;                  // RES 7,D
3305             case 0xbb:  RES(127, e); break;                  // RES 7,E
3306             case 0xbc:  RES(127, h); break;                  // RES 7,H
3307             case 0xbd:  RES(127, l); break;                  // RES 7,L
3308             case 0xbe:  RESHL(127); break;                   // RES 7,(HL)
3309             case 0xbf:  RES(127, a); break;                  // RES 7,A
3310             case 0xc0:  SET(1, b); break;                    // SET 0,B
3311             case 0xc1:  SET(1, c); break;                    // SET 0,C
3312             case 0xc2:  SET(1, d); break;                    // SET 0,D
3313             case 0xc3:  SET(1, e); break;                    // SET 0,E
3314             case 0xc4:  SET(1, h); break;                    // SET 0,H
3315             case 0xc5:  SET(1, l); break;                    // SET 0,L
3316             case 0xc6:  SETHL(1); break;                     // SET 0,(HL)
3317             case 0xc7:  SET(1, a); break;                    // SET 0,A
3318             case 0xc8:  SET(2, b); break;                    // SET 1,B
3319             case 0xc9:  SET(2, c); break;                    // SET 1,C
3320             case 0xca:  SET(2, d); break;                    // SET 1,D
3321             case 0xcb:  SET(2, e); break;                    // SET 1,E
3322             case 0xcc:  SET(2, h); break;                    // SET 1,H
3323             case 0xcd:  SET(2, l); break;                    // SET 1,L
3324             case 0xce:  SETHL(2); break;                     // SET 1,(HL)
3325             case 0xcf:  SET(2, a); break;                    // SET 1,A
3326             case 0xd0:  SET(4, b); break;                    // SET 2,B
3327             case 0xd1:  SET(4, c); break;                    // SET 2,C
3328             case 0xd2:  SET(4, d); break;                    // SET 2,D
3329             case 0xd3:  SET(4, e); break;                    // SET 2,E
3330             case 0xd4:  SET(4, h); break;                    // SET 2,H
3331             case 0xd5:  SET(4, l); break;                    // SET 2,L
3332             case 0xd6:  SETHL(4); break;                     // SET 2,(HL)
3333             case 0xd7:  SET(4, a); break;                    // SET 2,A
3334             case 0xd8:  SET(8, b); break;                    // SET 3,B
3335             case 0xd9:  SET(8, c); break;                    // SET 3,C
3336             case 0xda:  SET(8, d); break;                    // SET 3,D
3337             case 0xdb:  SET(8, e); break;                    // SET 3,E
3338             case 0xdc:  SET(8, h); break;                    // SET 3,H
3339             case 0xdd:  SET(8, l); break;                    // SET 3,L
3340             case 0xde:  SETHL(8); break;                     // SET 3,(HL)
3341             case 0xdf:  SET(8, a); break;                    // SET 3,A
3342             case 0xe0:  SET(16, b); break;                   // SET 4,B
3343             case 0xe1:  SET(16, c); break;                   // SET 4,C
3344             case 0xe2:  SET(16, d); break;                   // SET 4,D
3345             case 0xe3:  SET(16, e); break;                   // SET 4,E
3346             case 0xe4:  SET(16, h); break;                   // SET 4,H
3347             case 0xe5:  SET(16, l); break;                   // SET 4,L
3348             case 0xe6:  SETHL(16); break;                    // SET 4,(HL)
3349             case 0xe7:  SET(16, a); break;                   // SET 4,A
3350             case 0xe8:  SET(32, b); break;                   // SET 5,B
3351             case 0xe9:  SET(32, c); break;                   // SET 5,C
3352             case 0xea:  SET(32, d); break;                   // SET 5,D
3353             case 0xeb:  SET(32, e); break;                   // SET 5,E
3354             case 0xec:  SET(32, h); break;                   // SET 5,H
3355             case 0xed:  SET(32, l); break;                   // SET 5,L
3356             case 0xee:  SETHL(32); break;                    // SET 5,(HL)
3357             case 0xef:  SET(32, a); break;                   // SET 5,A
3358             case 0xf0:  SET(64, b); break;                   // SET 6,B
3359             case 0xf1:  SET(64, c); break;                   // SET 6,C
3360             case 0xf2:  SET(64, d); break;                   // SET 6,D
3361             case 0xf3:  SET(64, e); break;                   // SET 6,E
3362             case 0xf4:  SET(64, h); break;                   // SET 6,H
3363             case 0xf5:  SET(64, l); break;                   // SET 6,L
3364             case 0xf6:  SETHL(64); break;                    // SET 6,(HL)
3365             case 0xf7:  SET(64, a); break;                   // SET 6,A
3366             case 0xf8:  SET(128, b); break;                  // SET 7,B
3367             case 0xf9:  SET(128, c); break;                  // SET 7,C
3368             case 0xfa:  SET(128, d); break;                  // SET 7,D
3369             case 0xfb:  SET(128, e); break;                  // SET 7,E
3370             case 0xfc:  SET(128, h); break;                  // SET 7,H
3371             case 0xfd:  SET(128, l); break;                  // SET 7,L
3372             case 0xfe:  SETHL(128); break;                   // SET 7,(HL)
3373             case 0xff:  SET(128, a); break;                  // SET 7,A
3374           }
3375         else{
3376           st+= isz180() ? 9 : 11;
3377           if( iy )
3378             t= get_memory(mp= ((get_memory(pc++)^128)-128+(yl|yh<<8)));
3379           else
3380             t= get_memory(mp= ((get_memory(pc++)^128)-128+(xl|xh<<8)));
3381           switch( get_memory(pc++) ){
3382             case 0x00: RLC(t); put_memory(mp, b=t); break;         // LD B,RLC (IX+d) // LD B,RLC (IY+d)
3383             case 0x01: RLC(t); put_memory(mp, c=t); break;         // LD C,RLC (IX+d) // LD C,RLC (IY+d)
3384             case 0x02: RLC(t); put_memory(mp, d=t); break;         // LD D,RLC (IX+d) // LD D,RLC (IY+d)
3385             case 0x03: RLC(t); put_memory(mp, e=t); break;         // LD E,RLC (IX+d) // LD E,RLC (IY+d)
3386             case 0x04: RLC(t); put_memory(mp, h=t); break;         // LD H,RLC (IX+d) // LD H,RLC (IY+d)
3387             case 0x05: RLC(t); put_memory(mp, l=t); break;         // LD L,RLC (IX+d) // LD L,RLC (IY+d)
3388             case 0x06: RLC(t); put_memory(mp, t); break;            // RLC (IX+d) // RLC (IY+d)
3389             case 0x07: RLC(t); put_memory(mp, a=t); break;         // LD A,RLC (IX+d) // LD A,RLC (IY+d)
3390             case 0x08: RRC(t); put_memory(mp, b=t); break;         // LD B,RRC (IX+d) // LD B,RRC (IY+d)
3391             case 0x09: RRC(t); put_memory(mp, c=t); break;         // LD C,RRC (IX+d) // LD C,RRC (IY+d)
3392             case 0x0a: RRC(t); put_memory(mp, d=t); break;         // LD D,RRC (IX+d) // LD D,RRC (IY+d)
3393             case 0x0b: RRC(t); put_memory(mp, e=t); break;         // LD E,RRC (IX+d) // LD E,RRC (IY+d)
3394             case 0x0c: RRC(t); put_memory(mp, h=t); break;         // LD H,RRC (IX+d) // LD H,RRC (IY+d)
3395             case 0x0d: RRC(t); put_memory(mp, l=t); break;         // LD L,RRC (IX+d) // LD L,RRC (IY+d)
3396             case 0x0e: RRC(t); put_memory(mp, t); break;            // RRC (IX+d) // RRC (IY+d)
3397             case 0x0f: RRC(t); put_memory(mp, a=t); break;         // LD A,RRC (IX+d) // LD A,RRC (IY+d)
3398             case 0x10: RL(t); put_memory(mp, b=t); break;          // LD B,RL (IX+d) // LD B,RL (IY+d)
3399             case 0x11: RL(t); put_memory(mp, c=t); break;          // LD C,RL (IX+d) // LD C,RL (IY+d)
3400             case 0x12: RL(t); put_memory(mp, d=t); break;          // LD D,RL (IX+d) // LD D,RL (IY+d)
3401             case 0x13: RL(t); put_memory(mp, e=t); break;          // LD E,RL (IX+d) // LD E,RL (IY+d)
3402             case 0x14: RL(t); put_memory(mp, h=t); break;          // LD H,RL (IX+d) // LD H,RL (IY+d)
3403             case 0x15: RL(t); put_memory(mp, l=t); break;          // LD L,RL (IX+d) // LD L,RL (IY+d)
3404             case 0x16: RL(t); put_memory(mp, t); break;             // RL (IX+d) // RL (IY+d)
3405             case 0x17: RL(t); put_memory(mp, a=t); break;          // LD A,RL (IX+d) // LD A,RL (IY+d)
3406             case 0x18: RR(t); put_memory(mp, b=t); break;          // LD B,RR (IX+d) // LD B,RR (IY+d)
3407             case 0x19: RR(t); put_memory(mp, c=t); break;          // LD C,RR (IX+d) // LD C,RR (IY+d)
3408             case 0x1a: RR(t); put_memory(mp, d=t); break;          // LD D,RR (IX+d) // LD D,RR (IY+d)
3409             case 0x1b: RR(t); put_memory(mp, e=t); break;          // LD E,RR (IX+d) // LD E,RR (IY+d)
3410             case 0x1c: RR(t); put_memory(mp, h=t); break;          // LD H,RR (IX+d) // LD H,RR (IY+d)
3411             case 0x1d: RR(t); put_memory(mp, l=t); break;          // LD L,RR (IX+d) // LD L,RR (IY+d)
3412             case 0x1e: RR(t); put_memory(mp, t); break;             // RR (IX+d) // RR (IY+d)
3413             case 0x1f: RR(t); put_memory(mp, a=t); break;          // LD A,RR (IX+d) // LD A,RR (IY+d)
3414             case 0x20: SLA(t); put_memory(mp, b=t); break;         // LD B,SLA (IX+d) // LD B,SLA (IY+d)
3415             case 0x21: SLA(t); put_memory(mp, c=t); break;         // LD C,SLA (IX+d) // LD C,SLA (IY+d)
3416             case 0x22: SLA(t); put_memory(mp, d=t); break;         // LD D,SLA (IX+d) // LD D,SLA (IY+d)
3417             case 0x23: SLA(t); put_memory(mp, e=t); break;         // LD E,SLA (IX+d) // LD E,SLA (IY+d)
3418             case 0x24: SLA(t); put_memory(mp, h=t); break;         // LD H,SLA (IX+d) // LD H,SLA (IY+d)
3419             case 0x25: SLA(t); put_memory(mp, l=t); break;         // LD L,SLA (IX+d) // LD L,SLA (IY+d)
3420             case 0x26: SLA(t); put_memory(mp, t); break;            // SLA (IX+d) // SLA (IY+d)
3421             case 0x27: SLA(t); put_memory(mp, a=t); break;         // LD A,SLA (IX+d) // LD A,SLA (IY+d)
3422             case 0x28: SRA(t); put_memory(mp, b=t); break;         // LD B,SRA (IX+d) // LD B,SRA (IY+d)
3423             case 0x29: SRA(t); put_memory(mp, c=t); break;         // LD C,SRA (IX+d) // LD C,SRA (IY+d)
3424             case 0x2a: SRA(t); put_memory(mp, d=t); break;         // LD D,SRA (IX+d) // LD D,SRA (IY+d)
3425             case 0x2b: SRA(t); put_memory(mp, e=t); break;         // LD E,SRA (IX+d) // LD E,SRA (IY+d)
3426             case 0x2c: SRA(t); put_memory(mp, h=t); break;         // LD H,SRA (IX+d) // LD H,SRA (IY+d)
3427             case 0x2d: SRA(t); put_memory(mp, l=t); break;         // LD L,SRA (IX+d) // LD L,SRA (IY+d)
3428             case 0x2e: SRA(t); put_memory(mp, t); break;            // SRA (IX+d) // SRA (IY+d)
3429             case 0x2f: SRA(t); put_memory(mp, a=t); break;         // LD A,SRA (IX+d) // LD A,SRA (IY+d)
3430             case 0x30: SLL(t); put_memory(mp, b=t); break;         // LD B,SLL (IX+d) // LD B,SLL (IY+d)
3431             case 0x31: SLL(t); put_memory(mp, c=t); break;         // LD C,SLL (IX+d) // LD C,SLL (IY+d)
3432             case 0x32: SLL(t); put_memory(mp, d=t); break;         // LD D,SLL (IX+d) // LD D,SLL (IY+d)
3433             case 0x33: SLL(t); put_memory(mp, e=t); break;         // LD E,SLL (IX+d) // LD E,SLL (IY+d)
3434             case 0x34: SLL(t); put_memory(mp, h=t); break;         // LD H,SLL (IX+d) // LD H,SLL (IY+d)
3435             case 0x35: SLL(t); put_memory(mp, l=t); break;         // LD L,SLL (IX+d) // LD L,SLL (IY+d)
3436             case 0x36: SLL(t); put_memory(mp, t); break;            // SLL (IX+d) // SLL (IY+d)
3437             case 0x37: SLL(t); put_memory(mp, a=t); break;         // LD A,SLL (IX+d) // LD A,SLL (IY+d)
3438             case 0x38: SRL(t); put_memory(mp, b=t); break;         // LD B,SRL (IX+d) // LD B,SRL (IY+d)
3439             case 0x39: SRL(t); put_memory(mp, c=t); break;         // LD C,SRL (IX+d) // LD C,SRL (IY+d)
3440             case 0x3a: SRL(t); put_memory(mp, d=t); break;         // LD D,SRL (IX+d) // LD D,SRL (IY+d)
3441             case 0x3b: SRL(t); put_memory(mp, e=t); break;         // LD E,SRL (IX+d) // LD E,SRL (IY+d)
3442             case 0x3c: SRL(t); put_memory(mp, h=t); break;         // LD H,SRL (IX+d) // LD H,SRL (IY+d)
3443             case 0x3d: SRL(t); put_memory(mp, l=t); break;         // LD L,SRL (IX+d) // LD L,SRL (IY+d)
3444             case 0x3e: SRL(t); put_memory(mp, t); break;            // SRL (IX+d) // SRL (IY+d)
3445             case 0x3f: SRL(t); put_memory(mp, a=t); break;         // LD A,SRL (IX+d) // LD A,SRL (IY+d)
3446             case 0x40: case 0x41: case 0x42: case 0x43:      // BIT 0,(IX+d) // BIT 0,(IY+d)
3447             case 0x44: case 0x45: case 0x46: case 0x47:
3448                        BITI(1); break;
3449             case 0x48: case 0x49: case 0x4a: case 0x4b:      // BIT 1,(IX+d) // BIT 1,(IY+d)
3450             case 0x4c: case 0x4d: case 0x4e: case 0x4f:
3451                        BITI(2); break;
3452             case 0x50: case 0x51: case 0x52: case 0x53:      // BIT 2,(IX+d) // BIT 2,(IY+d)
3453             case 0x54: case 0x55: case 0x56: case 0x57:
3454                        BITI(4); break;
3455             case 0x58: case 0x59: case 0x5a: case 0x5b:      // BIT 3,(IX+d) // BIT 3,(IY+d)
3456             case 0x5c: case 0x5d: case 0x5e: case 0x5f:
3457                        BITI(8); break;
3458             case 0x60: case 0x61: case 0x62: case 0x63:      // BIT 4,(IX+d) // BIT 4,(IY+d)
3459             case 0x64: case 0x65: case 0x66: case 0x67:
3460                        BITI(16); break;
3461             case 0x68: case 0x69: case 0x6a: case 0x6b:      // BIT 5,(IX+d) // BIT 5,(IY+d)
3462             case 0x6c: case 0x6d: case 0x6e: case 0x6f:
3463                        BITI(32); break;
3464             case 0x70: case 0x71: case 0x72: case 0x73:      // BIT 6,(IX+d) // BIT 6,(IY+d)
3465             case 0x74: case 0x75: case 0x76: case 0x77:
3466                        BITI(64); break;
3467             case 0x78: case 0x79: case 0x7a: case 0x7b:      // BIT 7,(IX+d) // BIT 7,(IY+d)
3468             case 0x7c: case 0x7d: case 0x7e: case 0x7f:
3469                        BITI(128); break;
3470             case 0x80: RES(254, t); put_memory(mp, b=t); break;    // LD B,RES 0,(IX+d) // LD B,RES 0,(IY+d)
3471             case 0x81: RES(254, t); put_memory(mp, c=t); break;    // LD C,RES 0,(IX+d) // LD C,RES 0,(IY+d)
3472             case 0x82: RES(254, t); put_memory(mp, d=t); break;    // LD D,RES 0,(IX+d) // LD D,RES 0,(IY+d)
3473             case 0x83: RES(254, t); put_memory(mp, e=t); break;    // LD E,RES 0,(IX+d) // LD E,RES 0,(IY+d)
3474             case 0x84: RES(254, t); put_memory(mp, h=t); break;    // LD H,RES 0,(IX+d) // LD H,RES 0,(IY+d)
3475             case 0x85: RES(254, t); put_memory(mp, l=t); break;    // LD L,RES 0,(IX+d) // LD L,RES 0,(IY+d)
3476             case 0x86: RES(254, t); put_memory(mp, t); if ( israbbit() ) st -= 9; break;       // RES 0,(IX+d) // RES 0,(IY+d)
3477             case 0x87: RES(254, t); put_memory(mp, a=t); break;    // LD A,RES 0,(IX+d) // LD A,RES 0,(IY+d)
3478             case 0x88: RES(253, t); put_memory(mp, b=t); break;    // LD B,RES 1,(IX+d) // LD B,RES 1,(IY+d)
3479             case 0x89: RES(253, t); put_memory(mp, c=t); break;    // LD C,RES 1,(IX+d) // LD C,RES 1,(IY+d)
3480             case 0x8a: RES(253, t); put_memory(mp, d=t); break;    // LD D,RES 1,(IX+d) // LD D,RES 1,(IY+d)
3481             case 0x8b: RES(253, t); put_memory(mp, e=t); break;    // LD E,RES 1,(IX+d) // LD E,RES 1,(IY+d)
3482             case 0x8c: RES(253, t); put_memory(mp, h=t); break;    // LD H,RES 1,(IX+d) // LD H,RES 1,(IY+d)
3483             case 0x8d: RES(253, t); put_memory(mp, l=t); break;    // LD L,RES 1,(IX+d) // LD L,RES 1,(IY+d)
3484             case 0x8e: RES(253, t); put_memory(mp, t); if ( israbbit() ) st -= 9; break;       // RES 1,(IX+d) // RES 1,(IY+d)
3485             case 0x8f: RES(253, t); put_memory(mp, a=t); break;    // LD A,RES 1,(IX+d) // LD A,RES 1,(IY+d)
3486             case 0x90: RES(251, t); put_memory(mp, b=t); break;    // LD B,RES 2,(IX+d) // LD B,RES 2,(IY+d)
3487             case 0x91: RES(251, t); put_memory(mp, c=t); break;    // LD C,RES 2,(IX+d) // LD C,RES 2,(IY+d)
3488             case 0x92: RES(251, t); put_memory(mp, d=t); break;    // LD D,RES 2,(IX+d) // LD D,RES 2,(IY+d)
3489             case 0x93: RES(251, t); put_memory(mp, e=t); break;    // LD E,RES 2,(IX+d) // LD E,RES 2,(IY+d)
3490             case 0x94: RES(251, t); put_memory(mp, h=t); break;    // LD H,RES 2,(IX+d) // LD H,RES 2,(IY+d)
3491             case 0x95: RES(251, t); put_memory(mp, l=t); break;    // LD L,RES 2,(IX+d) // LD L,RES 2,(IY+d)
3492             case 0x96: RES(251, t); put_memory(mp, t); if ( israbbit() ) st -= 9; break;       // RES 2,(IX+d) // RES 2,(IY+d)
3493             case 0x97: RES(251, t); put_memory(mp, a=t); break;    // LD A,RES 2,(IX+d) // LD A,RES 2,(IY+d)
3494             case 0x98: RES(247, t); put_memory(mp, b=t); break;    // LD B,RES 3,(IX+d) // LD B,RES 3,(IY+d)
3495             case 0x99: RES(247, t); put_memory(mp, c=t); break;    // LD C,RES 3,(IX+d) // LD C,RES 3,(IY+d)
3496             case 0x9a: RES(247, t); put_memory(mp, d=t); break;    // LD D,RES 3,(IX+d) // LD D,RES 3,(IY+d)
3497             case 0x9b: RES(247, t); put_memory(mp, e=t); break;    // LD E,RES 3,(IX+d) // LD E,RES 3,(IY+d)
3498             case 0x9c: RES(247, t); put_memory(mp, h=t); break;    // LD H,RES 3,(IX+d) // LD H,RES 3,(IY+d)
3499             case 0x9d: RES(247, t); put_memory(mp, l=t); break;    // LD L,RES 3,(IX+d) // LD L,RES 3,(IY+d)
3500             case 0x9e: RES(247, t); put_memory(mp, t); if ( israbbit() ) st -= 9; break;       // RES 3,(IX+d) // RES 3,(IY+d)
3501             case 0x9f: RES(247, t); put_memory(mp, a=t); break;    // LD A,RES 3,(IX+d) // LD A,RES 3,(IY+d)
3502             case 0xa0: RES(239, t); put_memory(mp, b=t); break;    // LD B,RES 4,(IX+d) // LD B,RES 4,(IY+d)
3503             case 0xa1: RES(239, t); put_memory(mp, c=t); break;    // LD C,RES 4,(IX+d) // LD C,RES 4,(IY+d)
3504             case 0xa2: RES(239, t); put_memory(mp, d=t); break;    // LD D,RES 4,(IX+d) // LD D,RES 4,(IY+d)
3505             case 0xa3: RES(239, t); put_memory(mp, e=t); break;    // LD E,RES 4,(IX+d) // LD E,RES 4,(IY+d)
3506             case 0xa4: RES(239, t); put_memory(mp, h=t); break;    // LD H,RES 4,(IX+d) // LD H,RES 4,(IY+d)
3507             case 0xa5: RES(239, t); put_memory(mp, l=t); break;    // LD L,RES 4,(IX+d) // LD L,RES 4,(IY+d)
3508             case 0xa6: RES(239, t); put_memory(mp, t); if ( israbbit() ) st -= 9; break;       // RES 4,(IX+d) // RES 4,(IY+d)
3509             case 0xa7: RES(239, t); put_memory(mp, a=t); break;    // LD A,RES 4,(IX+d) // LD A,RES 4,(IY+d)
3510             case 0xa8: RES(223, t); put_memory(mp, b=t); break;    // LD B,RES 5,(IX+d) // LD B,RES 5,(IY+d)
3511             case 0xa9: RES(223, t); put_memory(mp, c=t); break;    // LD C,RES 5,(IX+d) // LD C,RES 5,(IY+d)
3512             case 0xaa: RES(223, t); put_memory(mp, d=t); break;    // LD D,RES 5,(IX+d) // LD D,RES 5,(IY+d)
3513             case 0xab: RES(223, t); put_memory(mp, e=t); break;    // LD E,RES 5,(IX+d) // LD E,RES 5,(IY+d)
3514             case 0xac: RES(223, t); put_memory(mp, h=t); break;    // LD H,RES 5,(IX+d) // LD H,RES 5,(IY+d)
3515             case 0xad: RES(223, t); put_memory(mp, l=t); break;    // LD L,RES 5,(IX+d) // LD L,RES 5,(IY+d)
3516             case 0xae: RES(223, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;       // RES 5,(IX+d) // RES 5,(IY+d)
3517             case 0xaf: RES(223, t); put_memory(mp, a=t); break;    // LD A,RES 5,(IX+d) // LD A,RES 5,(IY+d)
3518             case 0xb0: RES(191, t); put_memory(mp, b=t); break;    // LD B,RES 6,(IX+d) // LD B,RES 6,(IY+d)
3519             case 0xb1: RES(191, t); put_memory(mp, c=t); break;    // LD C,RES 6,(IX+d) // LD C,RES 6,(IY+d)
3520             case 0xb2: RES(191, t); put_memory(mp, d=t); break;    // LD D,RES 6,(IX+d) // LD D,RES 6,(IY+d)
3521             case 0xb3: RES(191, t); put_memory(mp, e=t); break;    // LD E,RES 6,(IX+d) // LD E,RES 6,(IY+d)
3522             case 0xb4: RES(191, t); put_memory(mp, h=t); break;    // LD H,RES 6,(IX+d) // LD H,RES 6,(IY+d)
3523             case 0xb5: RES(191, t); put_memory(mp, l=t); break;    // LD L,RES 6,(IX+d) // LD L,RES 6,(IY+d)
3524             case 0xb6: RES(191, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;       // RES 6,(IX+d) // RES 6,(IY+d)
3525             case 0xb7: RES(191, t); put_memory(mp, a=t); break;    // LD A,RES 6,(IX+d) // LD A,RES 6,(IY+d)
3526             case 0xb8: RES(127, t); put_memory(mp, b=t); break;    // LD B,RES 7,(IX+d) // LD B,RES 7,(IY+d)
3527             case 0xb9: RES(127, t); put_memory(mp, c=t); break;    // LD C,RES 7,(IX+d) // LD C,RES 7,(IY+d)
3528             case 0xba: RES(127, t); put_memory(mp, d=t); break;    // LD D,RES 7,(IX+d) // LD D,RES 7,(IY+d)
3529             case 0xbb: RES(127, t); put_memory(mp, e=t); break;    // LD E,RES 7,(IX+d) // LD E,RES 7,(IY+d)
3530             case 0xbc: RES(127, t); put_memory(mp, h=t); break;    // LD H,RES 7,(IX+d) // LD H,RES 7,(IY+d)
3531             case 0xbd: RES(127, t); put_memory(mp, l=t); break;    // LD L,RES 7,(IX+d) // LD L,RES 7,(IY+d)
3532             case 0xbe: RES(127, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;       // RES 7,(IX+d) // RES 7,(IY+d)
3533             case 0xbf: RES(127, t); put_memory(mp, a=t); break;    // LD A,RES 7,(IX+d) // LD A,RES 7,(IY+d)
3534             case 0xc0: SET(1, t); put_memory(mp, b=t); break;      // LD B,SET 0,(IX+d) // LD B,SET 0,(IY+d)
3535             case 0xc1: SET(1, t); put_memory(mp, c=t); break;      // LD C,SET 0,(IX+d) // LD C,SET 0,(IY+d)
3536             case 0xc2: SET(1, t); put_memory(mp, d=t); break;      // LD D,SET 0,(IX+d) // LD D,SET 0,(IY+d)
3537             case 0xc3: SET(1, t); put_memory(mp, e=t); break;      // LD E,SET 0,(IX+d) // LD E,SET 0,(IY+d)
3538             case 0xc4: SET(1, t); put_memory(mp, h=t); break;      // LD H,SET 0,(IX+d) // LD H,SET 0,(IY+d)
3539             case 0xc5: SET(1, t); put_memory(mp, l=t); break;      // LD L,SET 0,(IX+d) // LD L,SET 0,(IY+d)
3540             case 0xc6: SET(1, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;         // SET 0,(IX+d) // SET 0,(IY+d)
3541             case 0xc7: SET(1, t); put_memory(mp, a=t); break;      // LD A,SET 0,(IX+d) // LD A,SET 0,(IY+d)
3542             case 0xc8: SET(2, t); put_memory(mp, b=t); break;      // LD B,SET 1,(IX+d) // LD B,SET 1,(IY+d)
3543             case 0xc9: SET(2, t); put_memory(mp, c=t); break;      // LD C,SET 1,(IX+d) // LD C,SET 1,(IY+d)
3544             case 0xca: SET(2, t); put_memory(mp, d=t); break;      // LD D,SET 1,(IX+d) // LD D,SET 1,(IY+d)
3545             case 0xcb: SET(2, t); put_memory(mp, e=t); break;      // LD E,SET 1,(IX+d) // LD E,SET 1,(IY+d)
3546             case 0xcc: SET(2, t); put_memory(mp, h=t); break;      // LD H,SET 1,(IX+d) // LD H,SET 1,(IY+d)
3547             case 0xcd: SET(2, t); put_memory(mp, l=t); break;      // LD L,SET 1,(IX+d) // LD L,SET 1,(IY+d)
3548             case 0xce: SET(2, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;         // SET 1,(IX+d) // SET 1,(IY+d)
3549             case 0xcf: SET(2, t); put_memory(mp, a=t); break;      // LD A,SET 1,(IX+d) // LD A,SET 1,(IY+d)
3550             case 0xd0: SET(4, t); put_memory(mp, b=t); break;      // LD B,SET 2,(IX+d) // LD B,SET 2,(IY+d)
3551             case 0xd1: SET(4, t); put_memory(mp, c=t); break;      // LD C,SET 2,(IX+d) // LD C,SET 2,(IY+d)
3552             case 0xd2: SET(4, t); put_memory(mp, d=t); break;      // LD D,SET 2,(IX+d) // LD D,SET 2,(IY+d)
3553             case 0xd3: SET(4, t); put_memory(mp, e=t); break;      // LD E,SET 2,(IX+d) // LD E,SET 2,(IY+d)
3554             case 0xd4: SET(4, t); put_memory(mp, h=t); break;      // LD H,SET 2,(IX+d) // LD H,SET 2,(IY+d)
3555             case 0xd5: SET(4, t); put_memory(mp, l=t); break;      // LD L,SET 2,(IX+d) // LD L,SET 2,(IY+d)
3556             case 0xd6: SET(4, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;         // SET 2,(IX+d) // SET 2,(IY+d)
3557             case 0xd7: SET(4, t); put_memory(mp, a=t); break;      // LD A,SET 2,(IX+d) // LD A,SET 2,(IY+d)
3558             case 0xd8: SET(8, t); put_memory(mp, b=t); break;      // LD B,SET 3,(IX+d) // LD B,SET 3,(IY+d)
3559             case 0xd9: SET(8, t); put_memory(mp, c=t); break;      // LD C,SET 3,(IX+d) // LD C,SET 3,(IY+d)
3560             case 0xda: SET(8, t); put_memory(mp, d=t); break;      // LD D,SET 3,(IX+d) // LD D,SET 3,(IY+d)
3561             case 0xdb: SET(8, t); put_memory(mp, e=t); break;      // LD E,SET 3,(IX+d) // LD E,SET 3,(IY+d)
3562             case 0xdc: SET(8, t); put_memory(mp, h=t); break;      // LD H,SET 3,(IX+d) // LD H,SET 3,(IY+d)
3563             case 0xdd: SET(8, t); put_memory(mp, l=t); break;      // LD L,SET 3,(IX+d) // LD L,SET 3,(IY+d)
3564             case 0xde: SET(8, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;         // SET 3,(IX+d) // SET 3,(IY+d)
3565             case 0xdf: SET(8, t); put_memory(mp, a=t); break;      // LD A,SET 3,(IX+d) // LD A,SET 3,(IY+d)
3566             case 0xe0: SET(16, t); put_memory(mp, b=t); break;     // LD B,SET 4,(IX+d) // LD B,SET 4,(IY+d)
3567             case 0xe1: SET(16, t); put_memory(mp, c=t); break;     // LD C,SET 4,(IX+d) // LD C,SET 4,(IY+d)
3568             case 0xe2: SET(16, t); put_memory(mp, d=t); break;     // LD D,SET 4,(IX+d) // LD D,SET 4,(IY+d)
3569             case 0xe3: SET(16, t); put_memory(mp, e=t); break;     // LD E,SET 4,(IX+d) // LD E,SET 4,(IY+d)
3570             case 0xe4: SET(16, t); put_memory(mp, h=t); break;     // LD H,SET 4,(IX+d) // LD H,SET 4,(IY+d)
3571             case 0xe5: SET(16, t); put_memory(mp, l=t); break;     // LD L,SET 4,(IX+d) // LD L,SET 4,(IY+d)
3572             case 0xe6: SET(16, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;        // SET 4,(IX+d) // SET 4,(IY+d)
3573             case 0xe7: SET(16, t); put_memory(mp, a=t); break;     // LD A,SET 4,(IX+d) // LD A,SET 4,(IY+d)
3574             case 0xe8: SET(32, t); put_memory(mp, b=t); break;     // LD B,SET 5,(IX+d) // LD B,SET 5,(IY+d)
3575             case 0xe9: SET(32, t); put_memory(mp, c=t); break;     // LD C,SET 5,(IX+d) // LD C,SET 5,(IY+d)
3576             case 0xea: SET(32, t); put_memory(mp, d=t); break;     // LD D,SET 5,(IX+d) // LD D,SET 5,(IY+d)
3577             case 0xeb: SET(32, t); put_memory(mp, e=t); break;     // LD E,SET 5,(IX+d) // LD E,SET 5,(IY+d)
3578             case 0xec: SET(32, t); put_memory(mp, h=t); break;     // LD H,SET 5,(IX+d) // LD H,SET 5,(IY+d)
3579             case 0xed: SET(32, t); put_memory(mp, l=t); break;     // LD L,SET 5,(IX+d) // LD L,SET 5,(IY+d)
3580             case 0xee: SET(32, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;        // SET 5,(IX+d) // SET 5,(IY+d)
3581             case 0xef: SET(32, t); put_memory(mp, a=t); break;     // LD A,SET 5,(IX+d) // LD A,SET 5,(IY+d)
3582             case 0xf0: SET(64, t); put_memory(mp, b=t); break;     // LD B,SET 6,(IX+d) // LD B,SET 6,(IY+d)
3583             case 0xf1: SET(64, t); put_memory(mp, c=t); break;     // LD C,SET 6,(IX+d) // LD C,SET 6,(IY+d)
3584             case 0xf2: SET(64, t); put_memory(mp, d=t); break;     // LD D,SET 6,(IX+d) // LD D,SET 6,(IY+d)
3585             case 0xf3: SET(64, t); put_memory(mp, e=t); break;     // LD E,SET 6,(IX+d) // LD E,SET 6,(IY+d)
3586             case 0xf4: SET(64, t); put_memory(mp, h=t); break;     // LD H,SET 6,(IX+d) // LD H,SET 6,(IY+d)
3587             case 0xf5: SET(64, t); put_memory(mp, l=t); break;     // LD L,SET 6,(IX+d) // LD L,SET 6,(IY+d)
3588             case 0xf6: SET(64, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;        // SET 6,(IX+d) // SET 6,(IY+d)
3589             case 0xf7: SET(64, t); put_memory(mp, a=t); break;     // LD A,SET 6,(IX+d) // LD A,SET 6,(IY+d)
3590             case 0xf8: SET(128, t); put_memory(mp, b=t); break;    // LD B,SET 7,(IX+d) // LD B,SET 7,(IY+d)
3591             case 0xf9: SET(128, t); put_memory(mp, c=t); break;    // LD C,SET 7,(IX+d) // LD C,SET 7,(IY+d)
3592             case 0xfa: SET(128, t); put_memory(mp, d=t); break;    // LD D,SET 7,(IX+d) // LD D,SET 7,(IY+d)
3593             case 0xfb: SET(128, t); put_memory(mp, e=t); break;    // LD E,SET 7,(IX+d) // LD E,SET 7,(IY+d)
3594             case 0xfc: SET(128, t); put_memory(mp, h=t); break;    // LD H,SET 7,(IX+d) // LD H,SET 7,(IY+d)
3595             case 0xfd: SET(128, t); put_memory(mp, l=t); break;    // LD L,SET 7,(IX+d) // LD L,SET 7,(IY+d)
3596             case 0xfe: SET(128, t); put_memory(mp, t); if ( israbbit()) st -= 9; break;       // SET 7,(IX+d) // SET 7,(IY+d)
3597             case 0xff: SET(128, t); put_memory(mp, a=t); break;    // LD A,SET 7,(IX+d) // LD A,SET 7,(IY+d)
3598           }
3599         }
3600         ih=1;altd=0;ioi=0;ioe=0;break;
3601       case 0xed: // OP ED
3602         if ( is8085() ) { // (8085) LD HL,(DE) (LHLDE)
3603            if ( get_memory(pc) != 0xfe) {
3604                l = get_memory( (e|d<<8));
3605                h = get_memory( (e|d<<8) + 1);
3606                st+=10;
3607 	       break;
3608            }
3609         } else if ( is8080() ) {
3610           if ( get_memory(pc) != 0xfe) {
3611             printf("%04x: ILLEGAL 8080 prefix 0xED\n",pc-1);
3612             break;
3613           }
3614         } else if ( isgbz80() ) {
3615           if ( get_memory(pc) != 0xfe) {
3616               printf("%04x: ILLEGAL GBZ80 prefix 0xED\n",pc-1);
3617               break;
3618           }
3619         }
3620         r++;
3621         switch( get_memory(pc++) ){
3622           case 0x02:    // (EZ80) LEA BC,IX+d
3623             if ( isez80() ) {
3624                 LEA(b, c, xh, xl, 3);
3625             } else {
3626               st += 8;
3627             }
3628             break;
3629           case 0x03:    // (EZ80) LEA BC,IY+d
3630             if ( isez80() ) {
3631                 LEA(b, c, yh, yl, 3);
3632             } else {
3633               st += 8;
3634             }
3635             break;
3636           case 0x04:    // (Z180) TST A,B
3637             if ( canz180() ) {
3638               TEST(b, isez80() ? 2 : 7);
3639             } else {
3640               st += 8;
3641             }
3642             break;
3643           case 0x07:    // (EZ80) ld bc,(hl)
3644             if ( isez80() ) {
3645               st += 4;
3646               c = get_memory((l|h<<8));
3647               b = get_memory((l|h<<8) + 1);
3648             } else {
3649               st += 8;
3650             }
3651             break;
3652           case 0x0c:    // (Z180) TST A,C
3653             if ( canz180() ) {
3654               TEST(c, isez80() ? 2 : 7);
3655             } else {
3656               st += 8;
3657             }
3658             break;
3659           case 0x0f:    // (EZ80) ld (hl),bc
3660             if ( isez80() ) {
3661               st += 4;
3662               put_memory((l|h<<8),c);
3663               put_memory((l|h<<8) + 1,b);
3664             } else {
3665               st += 8;
3666             }
3667             break;
3668           case 0x12:    // (EZ80) LEA DE,IX+d
3669             if ( isez80() ) {
3670                 LEA(d, e, xh, xl, 3);
3671             } else {
3672               st += 8;
3673             }
3674             break;
3675           case 0x13:    // (EZ80) LEA DE,IY+d
3676             if ( isez80() ) {
3677                 LEA(d, e, yh, yl, 3);
3678             } else {
3679               st += 8;
3680             }
3681             break;
3682           case 0x14:    // (Z180) TST A,D
3683             if ( canz180() ) {
3684               TEST(d, isez80() ? 2 : 7);
3685             } else {
3686               st += 8;
3687             }
3688             break;
3689           case 0x17:    // (EZ80) ld de,(hl)
3690             if ( isez80() ) {
3691               st += 4;
3692               e = get_memory((l|h<<8));
3693               d = get_memory((l|h<<8) + 1);
3694             } else {
3695               st += 8;
3696             }
3697             break;
3698           case 0x1c:    // (Z180) TST A,E
3699             if ( canz180() ) {
3700               TEST(e, isez80() ? 2 : 7);
3701             } else {
3702               st += 8;
3703             }
3704             break;
3705           case 0x1f:    // (EZ80) ld (hl),de
3706             if ( isez80() ) {
3707               st += 4;
3708               put_memory((l|h<<8),e);
3709               put_memory((l|h<<8) + 1,d);
3710             } else {
3711               st += 8;
3712             }
3713             break;
3714           case 0x22:    // (EZ80) LEA HL,IX+d
3715             if ( isez80() ) {
3716                 LEA(h, l, xh, xl, 3);
3717             } else {
3718               st += 8;
3719             }
3720             break;
3721           case 0x23:    // (EZ80) LEA HL,IY+d, (ZXN) swapnib
3722             if ( isez80() ) {
3723                 LEA(h, l, yh, yl, 3);
3724             } else if ( c_cpu == CPU_Z80N ) {
3725               SWAP(a);
3726             } else {
3727               st += 8;
3728             }
3729             break;
3730           case 0x24:    // (Z180) TST A,D
3731             if ( canz180() ) {
3732               TEST(h, isez80() ? 2 : 7);
3733             } else if ( c_cpu == CPU_Z80N ) {   // (ZXN) mirror a
3734               a = mirror_table[a & 0x0f] << 4 | mirror_table[(a & 0xf0) >> 4];
3735               st += 8;
3736             } else {
3737               st += 8;
3738             }
3739             break;
3740           case 0x28:    // (ZXN) bsla de,b
3741             if ( c_cpu == CPU_Z80N ) {
3742                 long long old_st = st;
3743                 unsigned short old_ff = ff, old_fa = fa, old_fb = fb, old_fr = fr;
3744                 int count;
3745                 for ( count = 0 ; count < (b & 0x1f); count++ ) {
3746                     SLA(e);
3747                     RL(d);
3748                 }
3749                 st = old_st + 8;
3750                 ff = old_ff; fa = old_fa; fb = old_fb; fr = old_fr;
3751             } else {
3752               st += 8;
3753             }
3754             break;
3755           case 0x29:    // (ZXN) bsra de,b
3756             if ( c_cpu == CPU_Z80N ) {
3757                 long long old_st = st;
3758                 unsigned short old_ff = ff, old_fa = fa, old_fb = fb, old_fr = fr;
3759                 int count;
3760                 for ( count = 0 ; count < (b & 0x1f); count++ ) {
3761                     SRA(d);
3762                     RR(e);
3763                 }
3764                 st = old_st + 8;
3765                 ff = old_ff; fa = old_fa; fb = old_fb; fr = old_fr;
3766             } else {
3767               st += 8;
3768             }
3769             break;
3770           case 0x2a:    // (ZXN) bsrl de,b
3771             if ( c_cpu == CPU_Z80N ) {
3772                 long long old_st = st;
3773                 unsigned short old_ff = ff, old_fa = fa, old_fb = fb, old_fr = fr;
3774                 int count;
3775                 for ( count = 0 ; count < (b & 0x1f); count++ ) {
3776                     SRL(d);
3777                     RR(e);
3778                 }
3779                 st = old_st + 8;
3780                 ff = old_ff; fa = old_fa; fb = old_fb; fr = old_fr;
3781             } else {
3782               st += 8;
3783             }
3784             break;
3785           case 0x2b:    // (ZXN) bsrf de,b
3786             if ( c_cpu == CPU_Z80N ) {
3787                 long long old_st = st;
3788                 unsigned short old_ff = ff, old_fa = fa, old_fb = fb, old_fr = fr;
3789                 int count;
3790                 for ( count = 0; count < (b & 0x1f); count++) {
3791                     RR(d);
3792                     d |= 128;
3793                     RR(e);
3794                 }
3795                 st = old_st + 8;
3796                 ff = old_ff; fa = old_fa; fb = old_fb; fr = old_fr;
3797             } else {
3798               st += 8;
3799             }
3800             break;
3801           case 0x2c:    // (Z180) TST A,E (ZXN) brlc de,b
3802             if ( canz180() ) {
3803               TEST(l, isez80() ? 2 : 7);
3804             } else if ( c_cpu == CPU_Z80N ) {
3805                 long long old_st = st;
3806                 unsigned short old_ff = ff, old_fa = fa, old_fb = fb, old_fr = fr;
3807                 int count;
3808                 for ( count = 0 ; count < (b & 0x0f); count++ ) {
3809                     ff &= ~256;
3810                     ff |= ( d & 128) ? 256 : 0;
3811                     RL(e);
3812                     RL(d);
3813                 }
3814                 st = old_st + 8;
3815                 ff = old_ff; fa = old_fa; fb = old_fb; fr = old_fr;
3816             } else {
3817               st += 8;
3818             }
3819             break;
3820           case 0x2f:    // (EZ80) ld (hl),hl
3821             if ( isez80() ) {
3822               st += 4;
3823               put_memory((l|h<<8),l);
3824               put_memory((l|h<<8) + 1,h);
3825             } else {
3826               st += 8;
3827             }
3828             break;
3829           case 0x32:    // (EZ80) LEA IX,IX+d, (ZXN) add de,a
3830             if ( isez80() ) {
3831                 LEA(xh, xl, xh, xl, 3);
3832             } else if ( c_cpu == CPU_Z80N ) {
3833               int16_t result = (( d * 256 ) + e) + a;
3834               d  = (result >> 8 ) & 0xff;
3835               e = result & 0xff;
3836               st += 8;
3837             } else {
3838               st += 8;
3839             }
3840             break;
3841           case 0x33:    // (EZ80) LEA IY,IY+d, (ZXN) add bc,a
3842             if ( isez80() ) {
3843                 LEA(yh, yl, yh, yl, 3);
3844             } else if ( c_cpu == CPU_Z80N ) {
3845               int16_t result = (( b * 256 ) + c) + a;
3846               b  = (result >> 8 ) & 0xff;
3847               c = result & 0xff;
3848               st += 8;
3849             } else {
3850               st += 8;
3851             }
3852             break;
3853           case 0x3e:    // (EZ80) ld (hl),ix
3854             if ( isez80() ) {
3855               st += 4;
3856               put_memory((l|h<<8),xl);
3857               put_memory((l|h<<8) + 1,xh);
3858             } else {
3859               st += 8;
3860             }
3861             break;
3862           case 0x3f:    // (EZ80) ld (hl),iy
3863             if ( isez80() ) {
3864               st += 4;
3865               put_memory((l|h<<8),yl);
3866               put_memory((l|h<<8) + 1,yh);
3867             } else {
3868               st += 8;
3869             }
3870             break;
3871           case 0x64:    // (Z180) TST A,n
3872             if ( canz180() ) {
3873               uint8_t v = get_memory(pc++);
3874               TEST(v, isez80() ? 3 : 9);
3875             } else {    // Z80 (Undocumented NEG)
3876               st+= 8;
3877               fr= a= (ff= (fb= ~a)+1);
3878               fa= 0; break;
3879             }
3880             break;
3881           case 0x90:
3882               if (c_cpu == CPU_Z80N) {  // OUTINB : out(BC,HL*); HL++
3883                   out(c | b << 8, t = get_memory(l | h << 8));
3884                   put_memory(e | d << 8, t = get_memory(l | h << 8));
3885                   ++l || h++;
3886                   st += 16;
3887               }
3888               else {
3889                   st += 8; break;
3890               }
3891               break;
3892           case 0x91:
3893             if ( c_cpu == CPU_Z80N ) {
3894               uint8_t v = get_memory(pc++);
3895               uint8_t r = get_memory(pc++);
3896               out(0x243b, v);
3897               out(0x253b, r);
3898               st += 20;
3899             } else {
3900               st+= 8; break;
3901             }
3902             break;
3903           case 0x92:
3904             if ( c_cpu == CPU_Z80N ) {
3905               uint8_t v = get_memory(pc++);
3906               out(0x243b, v);
3907               out(0x253b, a);
3908               st += 17;
3909             } else {
3910               st+= 8; break;
3911             }
3912             break;
3913           case 0xa5:
3914               if (c_cpu == CPU_Z80N) {  // LDWS : DE*:=HL*; INC L; INC D;
3915                   put_memory(e | d << 8, t = get_memory(l | h << 8));
3916                   l++; d++;
3917                   st += 14;
3918               }
3919               else {
3920                   st += 8; break;
3921               }
3922               break;
3923           case 0xb7:
3924               if (c_cpu == CPU_Z80N) {  // LDPIRX : do{t:=(HL&$FFF8+E&7)*; {if t!=A DE*:=t;} DE++; BC--}while(BC>0)
3925                   t = get_memory(((l | h << 8) & 0xfff8) + (e & 0x07));
3926                   if (t != a) put_memory(e | d << 8, t);
3927                   ++e || d++;
3928                   c-- || b--;
3929                   st += 16;
3930                   if ((b | c) != 0) { pc -= 2; st += 5; }
3931               }
3932               else {
3933                   st += 8; break;
3934               }
3935               break;
3936           case 0x00: case 0x01:       // NOP
3937           case 0x05: case 0x06:
3938           case 0x08: case 0x09: case 0x0a: case 0x0b:
3939           case 0x0d: case 0x0e:
3940           case 0x10: case 0x11:
3941           case 0x15: case 0x16:
3942           case 0x18: case 0x19: case 0x1a: case 0x1b:
3943           case 0x1d: case 0x1e:
3944           case 0x20: case 0x21:
3945           case 0x2d: case 0x2e:
3946           case 0x77: case 0x7f:
3947           case 0x80: case 0x81: case 0x82: case 0x83:
3948           case 0x84: case 0x85: case 0x86: case 0x87:
3949           case 0x88: case 0x89:
3950           case 0x8c: case 0x8d: case 0x8e: case 0x8f:
3951           case 0x93:
3952           case 0x94: case 0x95: case 0x96: case 0x97:
3953           case 0x98: case 0x99: case 0x9a: case 0x9b:
3954           case 0x9c: case 0x9d: case 0x9e: case 0x9f:
3955           case 0xa6: case 0xa7:
3956           case 0xad: case 0xae: case 0xaf:
3957           case 0xb6:
3958           case 0xbd: case 0xbe: case 0xbf:
3959           case 0xc0: case 0xc1: case 0xc2: case 0xc3:
3960           case 0xc4: case 0xc5: case 0xc6: case 0xc7:
3961           case 0xc8: case 0xc9: case 0xca: case 0xcb:
3962           case 0xcc: case 0xcd: case 0xce: case 0xcf:
3963           case 0xd0: case 0xd1: case 0xd2: case 0xd3:
3964           case 0xd4: case 0xd5: case 0xd6: case 0xd7:
3965           case 0xd8: case 0xd9: case 0xda: case 0xdb:
3966           case 0xdc: case 0xdd: case 0xde: case 0xdf:
3967           case 0xe0: case 0xe1: case 0xe2: case 0xe3:
3968           case 0xe4: case 0xe5: case 0xe6: case 0xe7:
3969           case 0xe8: case 0xe9: case 0xea: case 0xeb:
3970           case 0xec: case 0xed: case 0xee: case 0xef:
3971           case 0xf0: case 0xf1: case 0xf2: case 0xf3:
3972           case 0xf4: case 0xf5: case 0xf6: case 0xf7:
3973           case 0xf8: case 0xf9: case 0xfa: case 0xfb:
3974           case 0xfc: case 0xfd: case 0xff:
3975             st+= 8; break;
3976           case 0x26:                                         // (ZXN) mirror de
3977             if ( c_cpu != CPU_Z80N) { st += 8; break; }
3978 #if 0
3979             t = (mirror_table[e & 0x0f] << 4) | mirror_table[(e & 0xf0) >> 4];
3980             e = d;
3981             d = t;
3982             e = (mirror_table[e & 0x0f] << 4) | mirror_table[(e & 0xf0) >> 4];
3983 #endif
3984             st += 8;
3985             break;
3986           case 0x30:                                         // (ZXN) mul d,e
3987             if ( c_cpu == CPU_Z80N ) {
3988               int16_t result = d * e;
3989               d  = (result >> 8 ) & 0xff;
3990               e = result & 0xff;
3991               st += 8;
3992             } else {
3993               st += 8;
3994             }
3995             break;
3996           case 0x31:                                         // (ZXN) add hl,a
3997             if ( c_cpu == CPU_Z80N ) {
3998               int16_t result = (( h * 256 ) + l) + a;
3999               h  = (result >> 8 ) & 0xff;
4000               l = result & 0xff;
4001               st += 8;
4002             } else {
4003               st += 8;
4004             }
4005             break;
4006           case 0x34:                                         // (ZXN) add hl,$xxxx
4007             if ( c_cpu == CPU_Z80N ) {
4008               uint8_t lsb = get_memory(pc++);
4009               uint8_t msb = get_memory(pc++);
4010               int16_t result = (( h * 256 ) + l) + ( lsb + msb * 256);
4011               h  = (result >> 8 ) & 0xff;
4012               l = result & 0xff;
4013               st += 16;
4014             } else if ( canz180() ) {			// (Z180/EZ80) TST A,(HL)
4015               uint8_t v = get_memory(l | h << 8);
4016               TEST(v, isez80() ? 3 : 10);
4017             } else {
4018               st += 8;
4019             }
4020             break;
4021           case 0x35:                                         // (ZXN) add de,$xxxx
4022             if ( c_cpu == CPU_Z80N ) {
4023               uint8_t lsb = get_memory(pc++);
4024               uint8_t msb = get_memory(pc++);
4025               int16_t result = (( d * 256 ) + e) + ( lsb + msb * 256);
4026               d  = (result >> 8 ) & 0xff;
4027               e = result & 0xff;
4028               st += 16;
4029             } else {
4030               st += 8;
4031             }
4032             break;
4033           case 0x36:                                         // (ZXN) add bc,$xxxx/ (EZ80) ld iy,(hl)
4034             if ( isez80() ) {
4035               st += 4;
4036               yl = get_memory((l|h<<8));
4037               yh = get_memory((l|h<<8) + 1);
4038             } else if ( c_cpu == CPU_Z80N ) {
4039               uint8_t lsb = get_memory(pc++);
4040               uint8_t msb = get_memory(pc++);
4041               int16_t result = (( b * 256 ) + c) + ( lsb + msb * 256);
4042               b = (result >> 8 ) & 0xff;
4043               c = result & 0xff;
4044               st += 16;
4045             } else {
4046               st += 8;
4047             }
4048             break;
4049           case 0x25:
4050             st += 8;
4051             break;
4052           case 0x37:                                         // (ZXN) inc dehl / (EZ80) ld ix,(hl)
4053             if ( isez80() ) {
4054               st += 4;
4055               xl = get_memory((l|h<<8));
4056               xh = get_memory((l|h<<8) + 1);
4057             } else {
4058               st += 8;
4059             }
4060             break;
4061           case 0x38:                                         // (ZXN) dec dehl
4062             st += 8;
4063             break;
4064           case 0x39:                                         // (ZXN) add dehl,a
4065             st += 8;
4066             break;
4067           case 0x3A:                                         // (ZXN) add dehl,bc
4068             st += 8;
4069             break;
4070           case 0x3B:                                         // (ZXN) add dehl,$XXXX
4071             st += 8;
4072             break;
4073           case 0x3C:                                         // (ZXN) sub dehl,a
4074             if ( canz180()) {                                // (Z180) TST A,A
4075               TEST(a, isez80() ? 2 : 7);
4076             } else {
4077               st += 8;
4078             }
4079             break;
4080           case 0x3D:                                         // (ZXN) sub dehl,bc
4081             st += 8;
4082             break;
4083           case 0x8a:                                         // (ZXN) push $xxxx
4084             if ( c_cpu == CPU_Z80N ) {
4085               uint8_t lsb = get_memory(pc++);
4086               uint8_t msb = get_memory(pc++);
4087               long long old_st = st;
4088               PUSH(msb,lsb);
4089               st = old_st + 23;
4090             } else {
4091               st += 8;
4092             }
4093             break;
4094           case 0x8b:                                         // (ZXN) popx
4095             st += 8;
4096             break;
4097           case 0x27:                                         // (ZXN) tst $xx (EZ80) ld hl,(hl)
4098             if ( isez80() ) {
4099               unsigned char tl;
4100               st += 4;
4101               tl = get_memory((l|h<<8));
4102               h = get_memory((l|h<<8) + 1);
4103               l = tl;
4104             } else if ( c_cpu == CPU_Z80N ) {
4105               uint8_t v = get_memory(pc++);
4106               TEST(v, 7);
4107               st += 11;
4108             } else {
4109               st += 8;
4110             }
4111             break;
4112           case 0xa4:                                        // (ZXN) ldix
4113             if ( c_cpu != CPU_Z80N ) { st+= 8; break; }
4114             st += 16;
4115             t = get_memory(l | h<<8);
4116             if ( t != a ) {
4117               put_memory(e | d<<8, t= get_memory(l | h<<8));
4118             }
4119             ++l || h++;
4120             ++e || d++;
4121             c-- || b--;
4122             fr && (fr= 1);
4123             t+= a;
4124             ff=  ff    & -41
4125                   | t     &   8
4126                   | t<<4  &  32;
4127             fa= 0;
4128             b|c && (fa= 128);
4129             fb= fa; break;
4130           case 0xac:                                       // (ZXN) lddx
4131             if ( c_cpu != CPU_Z80N ) { st+= 8; break; }
4132             t = get_memory(l | h<<8);
4133             st += 16;
4134             if ( t != a ) {
4135                 put_memory(e | d<<8, t= get_memory(l | h<<8));
4136             }
4137             l-- || h--;
4138             e-- || d--;
4139             c-- || b--;
4140             fr && (fr= 1);
4141             t+= a;
4142             ff=  ff    & -41
4143                | t     &   8
4144                | t<<4  &  32;
4145             fa= 0;
4146             b|c && (fa= 128);
4147             fb= fa; break;
4148           case 0xb4:                                        // (ZXN) ldirx
4149             if ( c_cpu != CPU_Z80N ) { st+= 8; break; }
4150             t = get_memory(l | h<<8);
4151             st += 16;
4152             if ( t != a ) {
4153                 put_memory(e | d<<8, t= get_memory(l | h<<8));
4154             }
4155             ++l || h++;
4156             ++e || d++;
4157             c-- || b--;
4158             fr && (fr= 1);
4159             t+= a;
4160             ff=  ff    & -41
4161                | t     &   8
4162                | t<<4  &  32;
4163             fa= 0;
4164             b|c && ( fa= 128,
4165                      st+= 5,
4166                      mp= --pc,
4167                             --pc);
4168                    fb= fa; break;
4169           case 0xb5:                                         // (ZXN) fillde
4170             st += 8; break;
4171           case 0xbc:                                         // (ZXN) LDDRX
4172             if ( c_cpu != CPU_Z80N ) { st+= 8; break; }
4173             t = get_memory(l | h<<8);
4174             st += 16;
4175             if ( t != a ) {
4176                 put_memory(e | d<<8, t= get_memory(l | h<<8));
4177             }
4178             l-- || h--;
4179             e-- || d--;
4180             c-- || b--;
4181             fr && (fr= 1);
4182             t+= a;
4183             ff=  ff    & -41
4184                | t     &   8
4185                | t<<4  &  32;
4186             fa= 0;
4187             b|c && ( fa= 128,
4188                      st+= 5,
4189                      mp= --pc,
4190                      --pc);
4191             fb= fa; break;
4192           case 0xfe: PatchZ80(); break;
4193           case 0x40: INR(b); break;                          // IN B,(C)
4194           case 0x48: INR(c); break;                          // IN C,(C)
4195           case 0x50: INR(d); break;                          // IN D,(C)
4196           case 0x58: INR(e); break;                          // IN E,(C)
4197           case 0x60: INR(h); break;                          // IN H,(C)
4198           case 0x68: INR(l); break;                          // IN L,(C)
4199           case 0x70: INR(t); break;                          // IN X,(C)
4200           case 0x78: INR(a); break;                          // IN A,(C)
4201           case 0x41:                                         // OUT (C),B (RCM) LD BC',DE
4202             if ( israbbit()) {
4203                 b_ = d;
4204                 c_ = e;
4205                 st += 4;
4206             } else {
4207               OUTR(b);
4208             }
4209             break;
4210           case 0x49:                                         // OUT (C),C (RCM) LD BC',BC
4211             if ( israbbit()) {
4212                 b_ = b;
4213                 c_ = c;
4214                 st += 4;
4215             } else {
4216               OUTR(c);
4217             }
4218             break;
4219           case 0x51:                                         // OUT (C),D (RCM) LD DE',DE
4220             if ( israbbit()) {
4221                 d_ = d;
4222                 e_ = e;
4223                 st += 4;
4224             } else {
4225               OUTR(d);
4226             }
4227             break;
4228           case 0x59:                                         // OUT (C),E (RCM) LD DE',BC
4229             if ( israbbit()) {
4230                 d_ = b;
4231                 e_ = c;
4232                 st += 4;
4233             } else {
4234               OUTR(e);
4235             }
4236             break;
4237           case 0x61:                                         // OUT (C),H (RCM) LD HL',DE
4238             if ( israbbit()) {
4239                 h_ = d;
4240                 l_ = e;
4241                 st += 4;
4242             } else {
4243               OUTR(h);
4244             }
4245             break;
4246           case 0x69:                                         // OUT (C),EL(RCM) LD HL',BC
4247             if ( israbbit()) {
4248                 h_ = b;
4249                 l_ = c;
4250                 st += 4;
4251             } else {
4252               OUTR(l);
4253             }
4254             break;
4255           case 0x71: OUTR(0); break;                         // OUT (C),X
4256           case 0x79: OUTR(a); break;                         // OUT (C),A
4257           case 0x42: SBCHLRR(b, c); break;                   // SBC HL,BC
4258           case 0x52: SBCHLRR(d, e); break;                   // SBC HL,DE
4259           case 0x62: SBCHLRR(h, l); break;                   // SBC HL,HL
4260           case 0x72: st+= isez80() ? 2 : israbbit() ? 4 : isz180() ? 10 : 15;                                // SBC HL,SP
4261                      v= (mp= l|h<<8)-sp-(ff>>8&1);
4262                      ++mp;
4263                      ff= v>>8;
4264                      fa= h;
4265                      fb= ~sp>>8;
4266                      h= ff;
4267                      l= v;
4268                      fr= h | l<<8; break;
4269           case 0x4a: ADCHLRR(b, c); break;                   // ADC HL,BC
4270           case 0x5a: ADCHLRR(d, e); break;                   // ADC HL,DE
4271           case 0x6a: ADCHLRR(h, l); break;                   // ADC HL,HL
4272           case 0x7a: st+=isez80() ? 2 : israbbit() ? 4 : isz180() ? 10 : 15;                                // ADC HL,SP
4273                      v= (mp= l|h<<8)+sp+(ff>>8&1);
4274                      ++mp;
4275                      ff= v>>8;
4276                      fa= h;
4277                      fb= sp>>8;
4278                      h= ff;
4279                      l= v;
4280                      fr= h | l<<8; break;
4281           case 0x43: LDPNNRR(b, c, isez80() ? 6 : israbbit() ? 15 : isz180() ? 19 : 20); break;               // LD (NN),BC
4282           case 0x53: LDPNNRR(d, e, isez80() ? 6 : israbbit() ? 15 : isz180() ? 19 : 20); break;               // LD (NN),DE
4283           case 0x63: LDPNNRR(h, l, isez80() ? 6 : israbbit() ? 15 : isz180() ? 19 : 20); break;               // LD (NN),HL
4284           case 0x73: st+= isez80() ? 6 : israbbit() ? 15 : isz180() ? 19 : 20;                                // LD (NN),SP
4285                      mp= get_memory(pc++);
4286                      put_memory(mp|= get_memory(pc++)<<8, sp);
4287                      put_memory(++mp,sp>>8); break;
4288           case 0x4b: LDRRPNN(b, c, isez80() ? 6 : israbbit() ? 13 : isz180() ? 18 : 20); break;               // LD BC,(NN)
4289           case 0x5b: LDRRPNN(d, e, isez80() ? 6 : israbbit() ? 13 : isz180() ? 18 : 20); break;               // LD DE,(NN)
4290           case 0x6b: LDRRPNN(h, l, isez80() ? 6 : israbbit() ? 13 : isz180() ? 18 : 20); break;               // LD HL,(NN)
4291           case 0x7b: st+= isez80() ? 6 : israbbit() ? 13 : isz180() ? 18 : 20;                                // LD SP,(NN)
4292                      t= get_memory(pc++);
4293                      sp= get_memory(t|= get_memory(pc++)<<8);
4294                      sp|= get_memory(mp= t+1) << 8; break;
4295           case 0x4c:                                         // (Z180) MLT BC
4296             if ( canz180() ) {
4297               uint16_t v = b * c;
4298               b = v >> 8;
4299               c = v;
4300               st += isez80() ? 6 : 17;
4301             } else {  // (Z80) Undocumented NEG
4302                 st+= 8;
4303                 fr= a= (ff= (fb= ~a)+1);
4304                 fa= 0;
4305             }
4306             break;
4307           case 0x5c:                                         // (Z180) MLT DE
4308             if ( canz180() ) {
4309               uint16_t v = d * e;
4310               d = v >> 8;
4311               e = v;
4312               st += isez80() ? 6 : 17;
4313             } else {  // (Z80) Undocumented NEG
4314                 st+= 8;
4315                 fr= a= (ff= (fb= ~a)+1);
4316                 fa= 0;
4317             }
4318             break;
4319           case 0x6c:                                         // (Z180) MLT HL
4320             if ( canz180() ) {
4321               uint16_t v = h * l;
4322               h = v >> 8;
4323               l = v;
4324               st += isez80() ? 6 : 17;
4325             } else {  // (Z80) Undocumented NEG
4326                 st+= 8;
4327                 fr= a= (ff= (fb= ~a)+1);
4328                 fa= 0;
4329             }
4330             break;
4331           case 0x54:	// (RCM) ex (sp),hl,  (EZ80) LEA IX,IY+d
4332             if ( israbbit()) {
4333               EXSPI(h, l);
4334               st += 2;
4335               break;
4336             } else if ( isez80() ) {
4337                 LEA(xh, xl, yh, yl, 3);
4338                 break;
4339             }
4340             // Fall through for z80 case
4341           case 0x44:       // NEG
4342           case 0x74: case 0x7c:
4343                      st+= 8;
4344                      fr= a= (ff= (fb= ~a)+1);
4345                      fa= 0; break;
4346           case 0x55:    // (EZ80) LEA IX,IX+d
4347             if ( isez80() ) {
4348                 LEA(xh, xl, xh, xl, 3);
4349                 break;
4350             }
4351             // Fall through for z80 case
4352           case 0x65:    // (EZ80) PEA ix+d
4353             if ( isez80() ) {
4354                uint16_t tv = ((get_memory(pc++)^128)-128+(xl|xh<<8))&65535;
4355                st += 5;
4356                put_memory(--sp, tv / 256);
4357                put_memory(--sp, tv % 256);
4358                break;
4359             }
4360             // Fall through
4361           case 0x45: case 0x4d: case 0x5d:        // RETI // RETN
4362           case 0x6d: case 0x75: case 0x7d:
4363                      RET(israbbit() ? 12 : isz180() ? 12 : 14); break;
4364           case 0x66:    // (EZ80) PEA iy+d
4365             if (isez80() ) {
4366                uint16_t tv = ((get_memory(pc++)^128)-128+(yl|yh<<8))&65535;
4367                st += 5;
4368                put_memory(--sp, tv / 256);
4369                put_memory(--sp, tv % 256);
4370                break;
4371             }
4372             // Fall through
4373           case 0x46: case 0x4e: case 0x6e:        // IM 0
4374                      st+= 8; im= 0; break;
4375           case 0x56: case 0x76:                              // IM 1
4376                      st+= 8; im= 1; break;
4377           case 0x5e: case 0x7e:                              // IM 2
4378                      st+= 8; im= 2; break;
4379           case 0x47: LDRR(i, a, i, israbbit() ? 4 : isz180() ? 6 : 9); break;                   // LD I,A
4380           case 0x4f: LDRR(r, a, r, israbbit() ? 4 : isz180() ? 6 : 9); r7= r; break;            // LD R,A
4381           case 0x57: st += israbbit() ? 4 : isz180() ? 6 : 9;                                 // LD A,I
4382                      ff=  ff&-256
4383                         | (a= i);
4384                      fr= !!a;
4385                      fa= fb= iff<<7 & 128; break;
4386           case 0x5f: st += israbbit() ? 4 : isz180() ? 6 : 9;                                     // LD A,R
4387                      ff=  ff&-256
4388                         | (a= (r&127|r7&128));
4389                      fr= !!a;
4390                      fa= fb= iff<<7 & 128; break;
4391           case 0x67: st+= 18;                                // RRD
4392                      t= get_memory(mp= l|h<<8)
4393                       | a<<8;
4394                      a= (a &240)
4395                       | (t & 15);
4396                      ff=  ff&-256
4397                         | (fr= a);
4398                      fa= a|256;
4399                      fb= 0;
4400                      put_memory(mp++,t>>4); break;
4401           case 0x6f: st+= 18;                                // RLD
4402                      t= get_memory(mp= l|h<<8)<<4
4403                       | (a&15);
4404                      a= (a &240)
4405                       | t>>8;
4406                      ff=  ff&-256
4407                         | (fr= a);
4408                      fa= a|256;
4409                      fb= 0;
4410                      put_memory(mp++,t); break;
4411           case 0xa0: st+= israbbit() ? 10 : isz180() ? 12 : 16;                               // LDI
4412                      put_memory(e | d<<8, t= get_memory(l | h<<8));
4413                      ++l || h++;
4414                      ++e || d++;
4415                      c-- || b--;
4416                      fr && (fr= 1);
4417                      t+= a;
4418                      ff=  ff    & -41
4419                         | t     &   8
4420                         | t<<4  &  32;
4421                      fa= 0;
4422                      b|c && (fa= 128);
4423                      fb= fa; break;
4424           case 0xa8: st+= israbbit() ? 10 : isz180() ? 12 : 16;                                // LDD
4425                      put_memory(e | d<<8, t= get_memory(l | h<<8));
4426                      l-- || h--;
4427                      e-- || d--;
4428                      c-- || b--;
4429                      fr && (fr= 1);
4430                      t+= a;
4431                      ff=  ff    & -41
4432                         | t     &   8
4433                         | t<<4  &  32;
4434                      fa= 0;
4435                      b|c && (fa= 128);
4436                      fb= fa; break;
4437           case 0xb0: st+= israbbit() ? 7 : isz180() ? 12 : 16;                                // LDIR
4438                      put_memory(e | d<<8, t= get_memory(l | h<<8));
4439                      ++l || h++;
4440                      ++e || d++;
4441                      c-- || b--;
4442                      fr && (fr= 1);
4443                      t+= a;
4444                      ff=  ff    & -41
4445                         | t     &   8
4446                         | t<<4  &  32;
4447                      fa= 0;
4448                      b|c && ( fa= 128,
4449                               st+= israbbit() ? 6 : isz180() ? 2 : 5,
4450                               mp= --pc,
4451                               --pc);
4452                      fb= fa; break;
4453           case 0xb8: st+= israbbit() ? 7 : isz180() ? 12 : 16;                                // LDDR
4454                      put_memory(e | d<<8, t= get_memory(l | h<<8));
4455                      l-- || h--;
4456                      e-- || d--;
4457                      c-- || b--;
4458                      fr && (fr= 1);
4459                      t+= a;
4460                      ff=  ff    & -41
4461                         | t     &   8
4462                         | t<<4  &  32;
4463                      fa= 0;
4464                      b|c && ( fa= 128,
4465                               st+= israbbit() ? 6 : isz180() ? 2 : 5,
4466                               mp= --pc,
4467                               --pc);
4468                      fb= fa; break;
4469           case 0xa1: st+= 16;                                // CPI
4470                      w= a-(t= get_memory(l|h<<8));
4471                      ++l || h++;
4472                      c-- || b--;
4473                      ++mp;
4474                      fr=  w & 127
4475                         | w>>7;
4476                      fb= ~(t|128);
4477                      fa= a&127;
4478                      b|c && ( fa|= 128,
4479                               fb|= 128);
4480                      ff=  ff  & -256
4481                         | w   &  -41;
4482                     (w^t^a) & 16 && w--;
4483                     ff|= w<<4 & 32
4484                        | w    &  8; break;
4485           case 0xa9: st+= 16;                                // CPD
4486                      w= a-(t= get_memory(l|h<<8));
4487                      l-- || h--;
4488                      c-- || b--;
4489                      --mp;
4490                      fr=  w & 127
4491                         | w>>7;
4492                      fb= ~(t|128);
4493                      fa= a&127;
4494                      b|c && ( fa|= 128,
4495                               fb|= 128);
4496                      ff=  ff  & -256
4497                         | w   &  -41;
4498                     (w^t^a) & 16 && w--;
4499                     ff|= w<<4 & 32
4500                        | w    &  8; break;
4501           case 0xb1: st+= 16;                                // CPIR
4502                      w= a-(t= get_memory(l|h<<8));
4503                      ++l || h++;
4504                      c-- || b--;
4505                      ++mp;
4506                      fr=  w & 127
4507                         | w>>7;
4508                      fb= ~(t|128);
4509                      fa= a&127;
4510                      b|c && ( fa|= 128,
4511                               fb|= 128,
4512                               w && (st+= 5, mp=--pc, --pc));
4513                      ff=  ff  & -256
4514                         | w   &  -41;
4515                     (w^t^a) & 16 && w--;
4516                     ff|= w<<4 & 32
4517                        | w    &  8; break;
4518           case 0xb9: st+= 16;                                // CPDR
4519                      w= a-(t= get_memory(l|h<<8));
4520                      l-- || h--;
4521                      c-- || b--;
4522                      --mp;
4523                      fr=  w & 127
4524                         | w>>7;
4525                      fb= ~(t|128);
4526                      fa= a&127;
4527                      b|c && ( fa|= 128,
4528                               fb|= 128,
4529                               w && (st+= 5, mp=--pc, --pc));
4530                      ff=  ff  & -256
4531                         | w   &  -41;
4532                     (w^t^a) & 16 && w--;
4533                     ff|= w<<4 & 32
4534                        | w    &  8; break;
4535           case 0xa2: st+= 16;                                // INI
4536                      put_memory(l | h<<8,t= in(mp= c | b<<8));
4537                      ++l || h++;
4538                      ++mp;
4539                      u= t+(c+1&255);
4540                      --b;
4541                      fb= u&7^b;
4542                      ff= b | (u&= 256);
4543                      fa= (fr= b)^128;
4544                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4545                         | u>>4
4546                         | (t&128)<<2; break;
4547           case 0xaa: st+= 16;                                // IND
4548                      put_memory(l | h<<8, t= in(mp= c | b<<8));
4549                      l-- || h--;
4550                      --mp;
4551                      u= t+(c-1&255);
4552                      --b;
4553                      fb= u&7^b;
4554                      ff= b | (u&= 256);
4555                      fa= (fr= b)^128;
4556                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4557                         | u>>4
4558                         | (t&128)<<2; break;
4559           case 0xb2: st+= 16;                                // INIR
4560                      put_memory(l | h<<8, t= in(mp= c | b<<8));
4561                      ++l || h++;
4562                      ++mp;
4563                      u= t+(c+1&255);
4564                      --b && (st+= 5, mp= --pc, --pc);
4565                      fb= u&7^b;
4566                      ff= b | (u&= 256);
4567                      fa= (fr= b)^128;
4568                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4569                         | u>>4
4570                         | (t&128)<<2; break;
4571           case 0xba: st+= 16;                                // INDR
4572                      put_memory(l | h<<8, t= in(mp= c | b<<8));
4573                      l-- || h--;
4574                      --mp;
4575                      u= t+(c-1&255);
4576                      --b && (st+= 5, mp= --pc, --pc);
4577                      fb= u&7^b;
4578                      ff= b | (u&= 256);
4579                      fa= (fr= b)^128;
4580                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4581                         | u>>4
4582                         | (t&128)<<2; break;
4583           case 0xa3: st+= 16;                                // OUTI
4584                      --b;
4585                      out( mp= c | b<<8,
4586                           t = get_memory(l | h<<8));
4587                      ++mp;
4588                      ++l || h++;
4589                      u= t+l;
4590                      fb= u&7^b;
4591                      ff= b | (u&= 256);
4592                      fa= (fr= b)^128;
4593                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4594                         | u>>4
4595                         | (t&128)<<2; break;
4596           case 0xab: st+= 16;                                // OUTD
4597                      --b;
4598                      out( mp= c | b<<8,
4599                           t = get_memory(l | h<<8));
4600                      --mp;
4601                      l-- || h--;
4602                      u= t+l;
4603                      fb= u&7^b;
4604                      ff= b | (u&= 256);
4605                      fa= (fr= b)^128;
4606                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4607                         | u>>4
4608                         | (t&128)<<2; break;
4609           case 0xb3: st+= 16;                                // OTIR
4610                      --b;
4611                      out( mp= c | b<<8,
4612                           t = get_memory(l | h<<8));
4613                      ++mp;
4614                      ++l || h++;
4615                      u= t+l;
4616                      b && (st+= 5, mp= --pc, --pc);
4617                      fb= u&7^b;
4618                      ff= b | (u&= 256);
4619                      fa= (fr= b)^128;
4620                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4621                         | u>>4
4622                         | (t&128)<<2; break;
4623           case 0xbb: st+= 16;                                // OTDR
4624                      --b;
4625                      out( mp= c | b<<8,
4626                           t = get_memory(l | h<<8));
4627                      --mp;
4628                      l-- || h--;
4629                      u= t+l;
4630                      b && (st+= 5, mp= --pc, --pc);
4631                      fb= u&7^b;
4632                      ff= b | (u&= 256);
4633                      fa= (fr= b)^128;
4634                      fb=  (4928640>>((fb^fb>>4)&15)^b)&128
4635                         | u>>4
4636                         | (t&128)<<2; break;
4637         }
4638         ih=1;altd=0;ioi=0;ioe=0;//break;
4639     }
4640   } while ( pc != end && st < counter  );
4641   if ( alarmtime != 0 ) {
4642       /* We running as a test, we should never reach the end, so exit with error */
4643       exit(1);
4644   }
4645   if( tap && st>sttap )
4646     sttap= st+( tap= tapcycles() );
4647   if ( counter != -1 )
4648     printf("%llu\n", st);
4649   if( output ){
4650     fh= fopen(output, "wb+");
4651     if( !fh )
4652       printf("\nCannot create or write in file: %s\n", output),
4653       exit(-1);
4654     if( !strcasecmp(strchr(output, '.'), ".sna" ) )
4655       put_memory(--sp,pc>>8),
4656       put_memory(--sp,pc),
4657       fwrite(&i, 1, 1, fh),
4658       fwrite(&l_, 1, 1, fh),
4659       fwrite(&h_, 1, 1, fh),
4660       fwrite(&e_, 1, 1, fh),
4661       fwrite(&d_, 1, 1, fh),
4662       fwrite(&c_, 1, 1, fh),
4663       fwrite(&b_, 1, 1, fh),
4664       t= f(),
4665       ff= ff_,
4666       fr= fr_,
4667       fa= fa_,
4668       fb= fb_,
4669       w= f(),
4670       fwrite(&w, 1, 1, fh),
4671       fwrite(&a_, 1, 1, fh),
4672       fwrite(&l, 1, 1, fh),
4673       fwrite(&h, 1, 1, fh),
4674       fwrite(&e, 1, 1, fh),
4675       fwrite(&d, 1, 1, fh),
4676       fwrite(&c, 1, 1, fh),
4677       fwrite(&b, 1, 1, fh),
4678       fwrite(&yl, 1, 1, fh),
4679       fwrite(&yh, 1, 1, fh),
4680       fwrite(&xl, 1, 1, fh),
4681       fwrite(&xh, 1, 1, fh),
4682       iff<<= 2,
4683       fwrite(&iff, 1, 1, fh),
4684       r= ((r&127)|(r7&128)),
4685       fwrite(&r, 1, 1, fh),
4686       fwrite(&t, 1, 1, fh),
4687       fwrite(&a, 1, 1, fh),
4688       fwrite(&sp, 2, 1, fh),
4689       fwrite(&im, 1, 1, fh),
4690       fwrite(&w, 1, 1, fh),
4691       fwrite(get_memory_addr(0x4000), 1, 0xc000, fh);
4692     else if ( !strcasecmp(strchr(output, '.'), ".scr" ) )
4693       fwrite(get_memory_addr(0x4000), 1, 0x1b00, fh);
4694     else{
4695       fwrite(get_memory_addr(0), 1, 65536, fh);
4696       w= f();
4697       fwrite(&w, 1, 1, fh);    // 10000 F
4698       fwrite(&a, 1, 1, fh);    // 10001 A
4699       fwrite(&c, 1, 1, fh);    // 10002 C
4700       fwrite(&b, 1, 1, fh);    // 10003 B
4701       fwrite(&l, 1, 1, fh);    // 10004 L
4702       fwrite(&h, 1, 1, fh);    // 10005 H
4703       fwrite(&pc, 2, 1, fh);   // 10006 PCl
4704                                // 10007 PCh
4705       fwrite(&sp, 2, 1, fh);   // 10008 SPl
4706                                // 10009 SPh
4707       fwrite(&i, 1, 1, fh);    // 1000a I
4708       r= ((r&127)|(r7&128));
4709       fwrite(&r, 1, 1, fh);    // 1000b R
4710       fwrite(&e, 1, 1, fh);    // 1000c E
4711       fwrite(&d, 1, 1, fh);    // 1000d D
4712       fwrite(&c_, 1, 1, fh);   // 1000e C'
4713       fwrite(&b_, 1, 1, fh);   // 1000f B'
4714       fwrite(&e_, 1, 1, fh);   // 10010 E'
4715       fwrite(&d_, 1, 1, fh);   // 10011 D'
4716       fwrite(&l_, 1, 1, fh);   // 10012 L'
4717       fwrite(&h_, 1, 1, fh);   // 10013 H'
4718       ff= ff_;
4719       fr= fr_;
4720       fa= fa_;
4721       fb= fb_;
4722       w= f();
4723       fwrite(&w, 1, 1, fh);    // 10014 F'
4724       fwrite(&a_, 1, 1, fh);   // 10015 A'
4725       fwrite(&yl, 1, 1, fh);   // 10016 IYl
4726       fwrite(&yh, 1, 1, fh);   // 10017 IYh
4727       fwrite(&xl, 1, 1, fh);   // 10018 IXl
4728       fwrite(&xh, 1, 1, fh);   // 10019 IXh
4729       fwrite(&iff, 1, 1, fh);  // 1001a IFF
4730       fwrite(&im, 1, 1, fh);   // 1001b IM
4731       fwrite(&mp, 2, 1, fh);   // 1001c MEMPTRl
4732                                // 1001d MEMPTRh
4733       wavlen-= wavpos,
4734       sttap-= st;
4735       wavlen= (wavlen<<1) | (ear>>6&1);
4736       fwrite(&wavlen, 4, 1, fh);  // 1001e wavlen
4737       fwrite(&sttap, 4, 1, fh);   // 10022 sttap
4738     }
4739     fclose(fh);
4740   }
4741 }
4742