openMSX
CPUCore.cc
Go to the documentation of this file.
1 // MEMORY EMULATION
2 // ----------------
3 //
4 // Memory access emulation is a very important part of the CPU emulation.
5 // Because they happen so frequently they really need to be executed as fast as
6 // possible otherwise they will completely bring down the speed of the CPU
7 // emulation.
8 //
9 // A very fast way to emulate memory accesses is by simply reading/writing to a
10 // 64kb array (for a 16-bit address space). Unfortunately this doesn't allow
11 // for memory mapped IO (MMIO). These are memory regions where read/writes
12 // trigger side effects, so where we need to execute device-specific code on
13 // read or writes. An alternative that does work with MMIO is for every access
14 // execute a virtual method call, (this is the approach taken by most current
15 // MSX emulators). Unfortunately this is also a lot slower.
16 //
17 // It is possible to combine the speed of array accesses with the flexibility
18 // of virtual methods. In openMSX it's implemened as follows: the 64kb address
19 // space is divided in 256 regions of 256 bytes (called cacheLines in the code
20 // below). For each such region we store a pointer, if this pointer is nullptr
21 // then we have to use the slow way (=virtual method call). If it is not nullptr,
22 // the pointer points to a block of memory that can be directly accessed. In
23 // some contexts accesses via the pointer are known as backdoor accesses while
24 // the accesses directly to the device are known as frontdoor accesses.
25 //
26 // We keep different pointers for read and write accesses. This allows to also
27 // implement ROMs efficiently: read is handled as regular RAM, but writes end
28 // up in some dummy memory region. This region is called 'unmappedWrite' in the
29 // code. There is also a special region 'unmappedRead', this region is filled
30 // with 0xFF and can be used to model (parts of) a device that don't react to
31 // reads (so reads return 0xFF).
32 //
33 // Because of bankswitching (the MSX slot select mechanism, but also e.g.
34 // MegaROM backswitching) the memory map as seen by the Z80 is not static. This
35 // means that the cacheLine pointers also need to change during runtime. To
36 // solve this we made the bankswitch code also responsible for invalidating the
37 // cacheLines of the switched region. These pointers are filled-in again in a
38 // lazy way: the first read or write to a cache line will first get this
39 // pointer (see getReadCacheLine() and getWriteCacheLine() in the code below),
40 // from then on this pointer is used for all further accesses to this region,
41 // until the cache is invalidated again.
42 //
43 //
44 // INSTRUCTION EMULATION
45 // ---------------------
46 //
47 // UPDATE: the 'threaded interpreter model' is not enabled by default
48 // main reason is the huge memory requirement while compiling
49 // and that it doesn't work on non-gcc compilers
50 //
51 // The current implementation is based on a 'threaded interpreter model'. In
52 // the text below I'll call the older implementation the 'traditional
53 // interpreter model'. From a very high level these two models look like this:
54 //
55 // Traditional model:
56 // while (!needExit()) {
57 // byte opcode = fetch(PC++);
58 // switch (opcode) {
59 // case 0x00: nop(); break;
60 // case 0x01: ld_bc_nn(); break;
61 // ...
62 // }
63 // }
64 //
65 // Threaded model:
66 // byte opcode = fetch(PC++); //
67 // goto *(table[opcode]); // fetch-and-dispatch
68 // // note: the goto * syntax is a gcc extension called computed-gotos
69 //
70 // op00: nop(); if (!needExit()) [fetch-and-dispatch];
71 // op01: ld_bc_nn(); if (!needExit()) [fetch-and-dispatch];
72 // ...
73 //
74 // In the first model there is a central place in the code that fetches (the
75 // first byte of) the instruction and based on this byte jumps to the
76 // appropriate routine. In the second model, this fetch-and-dispatch logic is
77 // duplicated at the end of each instruction.
78 //
79 // Typically the 'dispatch' part in above paragraph is implemented (either by
80 // the compiler or manually using computed goto's) via a jump table. Thus on
81 // assembler level via an indirect jump. For the host CPU it's hard to predict
82 // the destination address of such an indirect jump, certainly if there's only
83 // one such jump for all dispatching (the traditional model). If each
84 // instruction has its own indirect jump instruction (the threaded model), it
85 // becomes a bit easier, because often one particular z80 instructions is
86 // followed by a specific other z80 instruction (or one from a small subset).
87 // For example a z80 'cp' instruction is most likely followed by a 'conditional
88 // jump' z80 instruction. Modern CPUs are quite sensitive to
89 // branch-(mis)predictions, so the above optimization helps quite a lot. I
90 // measured a speedup of more than 10%!
91 //
92 // There is another advantage to the threaded model. Because also the
93 // needExit() test is duplicated for each instruction, it becomes possible to
94 // tweak it for individual instructions. But first let me explain this
95 // exit-test in more detail.
96 //
97 // These are the main reasons why the emulator should stop emulating CPU
98 // instructions:
99 // 1) When other devices than the CPU must be emulated (e.g. video frame
100 // rendering). In openMSX this is handled by the Scheduler class and
101 // actually we don't exit the CPU loop (anymore) for this. Instead we
102 // simply execute the device code as a subroutine. Each time right before
103 // we access an IO port or do a frontdoor memory access, there is a check
104 // whether we should emulate device code (search for schedule() in the code
105 // below).
106 // 2) To keep the inner CPU loop as fast as possible we don't check for IRQ,
107 // NMI or HALT status in this loop. Instead this condition is checked only
108 // once at the beginning outside of the loop (if there wasn't a pending IRQ
109 // on the first instruction there also won't be one on the second
110 // instruction, if all we did was emulating cpu instructions). Now when one
111 // of these conditions changes, we must exit the inner loop and re-evaluate
112 // them. For example after an EI instruction we must check the IRQ status
113 // again.
114 // 3) Various reasons like:
115 // * Z80/R800 switch
116 // * executing a Tcl command (could be a cpu-register debug read)
117 // * exit the emulator
118 // 4) 'once-in-a-while': To avoid threading problems and race conditions,
119 // several threads in openMSX only 'schedule' work that will later be
120 // executed by the main emulation thread. The main thread checks for such
121 // task outside of the cpu emulation loop. So once-in-a-while we need to
122 // exit the loop. The exact timing doesn't matter here because anyway the
123 // relative timing between threads is undefined.
124 // So for 1) we don't need to do anything (we don't actually exit). For 2) and
125 // 3) we need the exit the loop as soon as possible (right after the current
126 // instruction is finished). For 4) it's OK to exit 'eventually' (a few hundred
127 // z80 instructions late is still OK).
128 //
129 // Condition 2) is implemented with the 'slowInstructions' mechanism. Condition
130 // 3) via exitCPULoopSync() (may only get called by the main emulation thread)
131 // and condition 4) is implemented via exitCPULoopAsync() (can be called from
132 // any thread).
133 //
134 // Now back to the exit-test optimization: in the threaded model each
135 // instruction ends with:
136 //
137 // if (needExit()) return
138 // byte opcode = fetch(PC++);
139 // goto *(table[opcode]);
140 //
141 // And if we look in more detail at fetch():
142 //
143 // if (canDoBackdoor(addr)) {
144 // doBackdoorAccess(addr);
145 // } else {
146 // doFrontdoorAccess(addr);
147 // }
148 //
149 // So there are in fact two checks per instruction. This can be reduced to only
150 // one check with the following trick:
151 //
152 // !!!WRONG!!!
153 // In the past we optimized this to only check canDoBackdoor() (and make sure
154 // canDoBackdoor() returned false when needExit() would return true). This
155 // worked rather well, except for one case: when we exit the CPU loop we also
156 // check for pending Syncronization points. It is possible such a SyncPoint
157 // raises the IRQ line. So it is important to check for exit after every
158 // instruction, otherwise we would enter the IRQ routine a couple of
159 // instructions too late.
160 
161 #include "CPUCore.hh"
162 #include "MSXCPUInterface.hh"
163 #include "Scheduler.hh"
164 #include "MSXMotherBoard.hh"
165 #include "CliComm.hh"
166 #include "TclCallback.hh"
167 #include "Dasm.hh"
168 #include "Z80.hh"
169 #include "R800.hh"
170 #include "Thread.hh"
171 #include "endian.hh"
172 #include "likely.hh"
173 #include "inline.hh"
174 #include "unreachable.hh"
175 #include <iomanip>
176 #include <iostream>
177 #include <type_traits>
178 #include <cassert>
179 #include <cstring>
180 
181 
182 //
183 // #define USE_COMPUTED_GOTO
184 //
185 // Computed goto's are not enabled by default:
186 // - Computed goto's are a gcc extension, it's not part of the official c++
187 // standard. So this will only work if you use gcc as your compiler (it
188 // won't work with visual c++ for example)
189 // - This is only beneficial on CPUs with branch prediction for indirect jumps
190 // and a reasonable amout of cache. For example it is very benefical for a
191 // intel core2 cpu (10% faster), but not for a ARM920 (a few percent slower)
192 // - Compiling src/cpu/CPUCore.cc with computed goto's enabled is very demanding
193 // on the compiler. On older gcc versions it requires up to 1.5GB of memory.
194 // But even on more recent gcc versions it still requires around 700MB.
195 //
196 // Probably the easiest way to enable this, is to pass the -DUSE_COMPUTED_GOTO
197 // flag to the compiler. This is for example done in the super-opt flavour.
198 // See build/flavour-super-opt.mk
199 
200 
201 using std::string;
202 
203 namespace openmsx {
204 
205 // This actually belongs in Z80.cc and R800.cc (these files don't exist yet).
206 // As a quick hack I put these two lines here because I found it overkill to
207 // create two files each containing only a single line.
208 // Technically these two lines _are_ required according to the c++ standard.
209 // Though usually it works just find without them, but during experiments I did
210 // get a link error when these lines were missing (it only happened during a
211 // debug build with some specific compiler version and only with some
212 // combination of other code changes, but again when strictly following the
213 // language rules, these lines should be here).
214 // ... But visual studio is not fully standard compliant, see also comment
215 // in SectorAccesibleDisk.cc
216 #ifndef _MSC_VER
217 const int Z80TYPE ::CLOCK_FREQ;
218 const int R800TYPE::CLOCK_FREQ;
219 #endif
220 
221 enum Reg8 : int { A, F, B, C, D, E, H, L, IXH, IXL, IYH, IYL, REG_I, REG_R, DUMMY };
222 enum Reg16 : int { AF, BC, DE, HL, IX, IY, SP };
223 
224 // flag positions
225 static const byte S_FLAG = 0x80;
226 static const byte Z_FLAG = 0x40;
227 static const byte Y_FLAG = 0x20;
228 static const byte H_FLAG = 0x10;
229 static const byte X_FLAG = 0x08;
230 static const byte V_FLAG = 0x04;
231 static const byte P_FLAG = V_FLAG;
232 static const byte N_FLAG = 0x02;
233 static const byte C_FLAG = 0x01;
234 
235 // flag-register tables, initialized at run-time
236 static byte ZSTable[256];
237 static byte ZSXYTable[256];
238 static byte ZSPTable[256];
239 static byte ZSPXYTable[256];
240 static byte ZSPHTable[256];
241 
242 static const byte ZS0 = Z_FLAG;
243 static const byte ZSXY0 = Z_FLAG;
244 static const byte ZSP0 = Z_FLAG | V_FLAG;
245 static const byte ZSPXY0 = Z_FLAG | V_FLAG;
246 static const byte ZS255 = S_FLAG;
247 static const byte ZSXY255 = S_FLAG | X_FLAG | Y_FLAG;
248 
249 // Global variable, because it should be shared between Z80 and R800.
250 // It must not be shared between the CPUs of different MSX machines, but
251 // the (logical) lifetime of this variable cannot overlap between execution
252 // of two MSX machines.
253 static word start_pc;
254 
255 // conditions
256 struct CondC { bool operator()(byte f) const { return (f & C_FLAG) != 0; } };
257 struct CondNC { bool operator()(byte f) const { return !(f & C_FLAG); } };
258 struct CondZ { bool operator()(byte f) const { return (f & Z_FLAG) != 0; } };
259 struct CondNZ { bool operator()(byte f) const { return !(f & Z_FLAG); } };
260 struct CondM { bool operator()(byte f) const { return (f & S_FLAG) != 0; } };
261 struct CondP { bool operator()(byte f) const { return !(f & S_FLAG); } };
262 struct CondPE { bool operator()(byte f) const { return (f & V_FLAG) != 0; } };
263 struct CondPO { bool operator()(byte f) const { return !(f & V_FLAG); } };
264 struct CondTrue { bool operator()(byte) const { return true; } };
265 
266 static void initTables()
267 {
268  static bool alreadyInit = false;
269  if (alreadyInit) return;
270  alreadyInit = true;
271 
272  for (int i = 0; i < 256; ++i) {
273  byte zFlag = (i == 0) ? Z_FLAG : 0;
274  byte sFlag = i & S_FLAG;
275  byte xFlag = i & X_FLAG;
276  byte yFlag = i & Y_FLAG;
277  byte vFlag = V_FLAG;
278  for (int v = 128; v != 0; v >>= 1) {
279  if (i & v) vFlag ^= V_FLAG;
280  }
281  ZSTable [i] = zFlag | sFlag;
282  ZSXYTable [i] = zFlag | sFlag | xFlag | yFlag;
283  ZSPTable [i] = zFlag | sFlag | vFlag;
284  ZSPXYTable[i] = zFlag | sFlag | xFlag | yFlag | vFlag;
285  ZSPHTable [i] = zFlag | sFlag | vFlag | H_FLAG;
286  }
287  assert(ZSTable [ 0] == ZS0);
288  assert(ZSXYTable [ 0] == ZSXY0);
289  assert(ZSPTable [ 0] == ZSP0);
290  assert(ZSPXYTable[ 0] == ZSPXY0);
291  assert(ZSTable [255] == ZS255);
292  assert(ZSXYTable [255] == ZSXY255);
293 }
294 
295 template<class T> CPUCore<T>::CPUCore(
296  MSXMotherBoard& motherboard_, const string& name,
297  const BooleanSetting& traceSetting_,
298  TclCallback& diHaltCallback_, EmuTime::param time)
299  : CPURegs(T::isR800())
300  , T(time, motherboard_.getScheduler())
301  , motherboard(motherboard_)
302  , scheduler(motherboard.getScheduler())
303  , interface(nullptr)
304  , traceSetting(traceSetting_)
305  , diHaltCallback(diHaltCallback_)
306  , IRQStatus(motherboard.getDebugger(), name + ".pendingIRQ",
307  "Non-zero if there are pending IRQs (thus CPU would enter "
308  "interrupt routine in EI mode).",
309  0)
310  , IRQAccept(motherboard.getDebugger(), name + ".acceptIRQ",
311  "This probe is only useful to set a breakpoint on (the value "
312  "return by read is meaningless). The breakpoint gets triggered "
313  "right after the CPU accepted an IRQ.")
314  , freqLocked(
315  motherboard.getCommandController(), name + "_freq_locked",
316  "real (locked) or custom (unlocked) " + name + " frequency",
317  true)
318  , freqValue(
319  motherboard.getCommandController(), name + "_freq",
320  "custom " + name + " frequency (only valid when unlocked)",
321  T::CLOCK_FREQ, 1000000, 1000000000)
322  , freq(T::CLOCK_FREQ)
323  , NMIStatus(0)
324  , nmiEdge(false)
325  , exitLoop(false)
326  , tracingEnabled(traceSetting.getBoolean())
327  , isTurboR(motherboard.isTurboR())
328 {
329  static_assert(!std::is_polymorphic<CPUCore<T>>::value,
330  "keep CPUCore non-virtual to keep PC at offset 0");
331  doSetFreq();
332  doReset(time);
333 
334  initTables();
335 }
336 
337 template<class T> void CPUCore<T>::warp(EmuTime::param time)
338 {
339  assert(T::getTimeFast() <= time);
340  T::setTime(time);
341 }
342 
344 {
345  return T::getTime();
346 }
347 
348 template<class T> void CPUCore<T>::invalidateMemCache(unsigned start, unsigned size)
349 {
350  unsigned first = start / CacheLine::SIZE;
351  unsigned num = (size + CacheLine::SIZE - 1) / CacheLine::SIZE;
352  memset(&readCacheLine [first], 0, num * sizeof(byte*)); // nullptr
353  memset(&writeCacheLine [first], 0, num * sizeof(byte*)); //
354  memset(&readCacheTried [first], 0, num * sizeof(bool)); // FALSE
355  memset(&writeCacheTried[first], 0, num * sizeof(bool)); //
356 }
357 
358 template<class T> void CPUCore<T>::doReset(EmuTime::param time)
359 {
360  // AF and SP are 0xFFFF
361  // PC, R, IFF1, IFF2, HALT and IM are 0x0
362  // all others are random
363  setAF(0xFFFF);
364  setBC(0xFFFF);
365  setDE(0xFFFF);
366  setHL(0xFFFF);
367  setIX(0xFFFF);
368  setIY(0xFFFF);
369  setPC(0x0000);
370  setSP(0xFFFF);
371  setAF2(0xFFFF);
372  setBC2(0xFFFF);
373  setDE2(0xFFFF);
374  setHL2(0xFFFF);
375  setIFF1(false);
376  setIFF2(false);
377  setHALT(false);
378  setExtHALT(false);
379  setIM(0);
380  setI(0x00);
381  setR(0x00);
382  T::setMemPtr(0xFFFF);
383  clearPrevious();
384  invalidateMemCache(0x0000, 0x10000);
385 
386  // We expect this assert to be valid
387  // assert(T::getTimeFast() <= time); // time shouldn't go backwards
388  // But it's disabled for the following reason:
389  // 'motion' (IRC nickname) managed to create a replay file that
390  // contains a reset command that falls in the middle of a Z80
391  // instruction. Replayed commands go via the Scheduler, and are
392  // (typically) executed right after a complete CPU instruction. So
393  // the CPU is (slightly) ahead in time of the about to be executed
394  // reset command.
395  // Normally this situation should never occur: console commands,
396  // hotkeys, commands over clicomm, ... are all handled via the global
397  // event mechanism. Such global events are scheduled between CPU
398  // instructions, so also in a replay they should fall between CPU
399  // instructions.
400  // However if for some reason the timing of the emulation changed
401  // (improved emulation accuracy or a bug so that emulation isn't
402  // deterministic or the replay file was edited, ...), then the above
403  // reasoning no longer holds and the assert can trigger.
404  // We need to be robust against loading older replays (when emulation
405  // timing has changed). So in that respect disabling the assert is
406  // good. Though in the example above (motion's replay) it's not clear
407  // whether the assert is really triggered by mixing an old replay
408  // with a newer openMSX version. In any case so far we haven't been
409  // able to reproduce this assert by recording and replaying using a
410  // single openMSX version.
411  T::setTime(time);
412 
413  assert(NMIStatus == 0); // other devices must reset their NMI source
414  assert(IRQStatus == 0); // other devices must reset their IRQ source
415 }
416 
417 // I believe the following two methods are thread safe even without any
418 // locking. The worst that can happen is that we occasionally needlessly
419 // exit the CPU loop, but that's harmless
420 // TODO thread issues are always tricky, can someone confirm this really
421 // is thread safe
422 template<class T> void CPUCore<T>::exitCPULoopAsync()
423 {
424  // can get called from non-main threads
425  exitLoop = true;
426 }
427 template<class T> void CPUCore<T>::exitCPULoopSync()
428 {
429  assert(Thread::isMainThread());
430  exitLoop = true;
431  T::disableLimit();
432 }
433 template<class T> inline bool CPUCore<T>::needExitCPULoop()
434 {
435  // always executed in main thread
436  if (unlikely(exitLoop)) {
437  // Note: The test-and-set is _not_ atomic! But that's fine.
438  // An atomic implementation is trivial (see below), but
439  // this version (at least on x86) avoids the more expensive
440  // instructions on the likely path.
441  exitLoop = false;
442  return true;
443  }
444  return false;
445 
446  // Alternative implementation:
447  // atomically set to false and return the old value
448  //return exitLoop.exchange(false);
449 }
450 
451 template<class T> void CPUCore<T>::setSlowInstructions()
452 {
453  slowInstructions = 2;
454  T::disableLimit();
455 }
456 
457 template<class T> void CPUCore<T>::raiseIRQ()
458 {
459  assert(IRQStatus >= 0);
460  if (IRQStatus == 0) {
461  setSlowInstructions();
462  }
463  IRQStatus = IRQStatus + 1;
464 }
465 
466 template<class T> void CPUCore<T>::lowerIRQ()
467 {
468  IRQStatus = IRQStatus - 1;
469  assert(IRQStatus >= 0);
470 }
471 
472 template<class T> void CPUCore<T>::raiseNMI()
473 {
474  // NMIs are currently disabled, they are anyway not used in MSX and
475  // not having to check for them allows to emulate slightly faster
476  UNREACHABLE;
477  assert(NMIStatus >= 0);
478  if (NMIStatus == 0) {
479  nmiEdge = true;
480  setSlowInstructions();
481  }
482  NMIStatus++;
483 }
484 
485 template<class T> void CPUCore<T>::lowerNMI()
486 {
487  NMIStatus--;
488  assert(NMIStatus >= 0);
489 }
490 
491 template<class T> bool CPUCore<T>::isM1Cycle(unsigned address) const
492 {
493  // This method should only be called from within a MSXDevice::readMem()
494  // method. It can be used to check whether the current read action has
495  // the M1 pin active. The 'address' parameter that is give to readMem()
496  // should be passed (unchanged) to this method.
497  //
498  // This simple implementation works because the rest of the CPUCore
499  // code is careful to only update the PC register on M1 cycles. In
500  // practice that means that the PC is (only) updated at the very end of
501  // every instruction, even if is a multi-byte instruction. Or for
502  // prefix-instructions the PC is also updated after the prefix is
503  // fetched (because such instructions activate M1 twice).
504  return address == getPC();
505 }
506 
507 template<class T> void CPUCore<T>::wait(EmuTime::param time)
508 {
509  assert(time >= getCurrentTime());
510  scheduler.schedule(time);
511  T::advanceTime(time);
512 }
513 
514 template<class T> EmuTime CPUCore<T>::waitCycles(EmuTime::param time, unsigned cycles)
515 {
516  T::add(cycles);
517  EmuTime time2 = T::calcTime(time, cycles);
518  // note: time2 is not necessarily equal to T::getTime() because of the
519  // way how WRITE_PORT() is implemented.
520  scheduler.schedule(time2);
521  return time2;
522 }
523 
524 template<class T> void CPUCore<T>::setNextSyncPoint(EmuTime::param time)
525 {
526  T::setLimit(time);
527 }
528 
529 
530 static inline char toHex(byte x)
531 {
532  return (x < 10) ? (x + '0') : (x - 10 + 'A');
533 }
534 static void toHex(byte x, char* buf)
535 {
536  buf[0] = toHex(x / 16);
537  buf[1] = toHex(x & 15);
538 }
539 
540 template<class T> void CPUCore<T>::disasmCommand(
541  Interpreter& interp, array_ref<TclObject> tokens, TclObject& result) const
542 {
543  word address = (tokens.size() < 3) ? getPC() : tokens[2].getInt(interp);
544  byte outBuf[4];
545  std::string dasmOutput;
546  unsigned len = dasm(*interface, address, outBuf, dasmOutput,
547  T::getTimeFast());
548  result.addListElement(dasmOutput);
549  char tmp[3]; tmp[2] = 0;
550  for (unsigned i = 0; i < len; ++i) {
551  toHex(outBuf[i], tmp);
552  result.addListElement(tmp);
553  }
554 }
555 
556 template<class T> void CPUCore<T>::update(const Setting& setting)
557 {
558  if (&setting == &freqLocked) {
559  doSetFreq();
560  } else if (&setting == &freqValue) {
561  doSetFreq();
562  } else if (&setting == &traceSetting) {
563  tracingEnabled = traceSetting.getBoolean();
564  }
565 }
566 
567 template<class T> void CPUCore<T>::setFreq(unsigned freq_)
568 {
569  freq = freq_;
570  doSetFreq();
571 }
572 
573 template<class T> void CPUCore<T>::doSetFreq()
574 {
575  if (freqLocked.getBoolean()) {
576  // locked, use value set via setFreq()
577  T::setFreq(freq);
578  } else {
579  // unlocked, use value set by user
580  T::setFreq(freqValue.getInt());
581  }
582 }
583 
584 
585 template<class T> inline byte CPUCore<T>::READ_PORT(unsigned port, unsigned cc)
586 {
587  EmuTime time = T::getTimeFast(cc);
588  scheduler.schedule(time);
589  byte result = interface->readIO(port, time);
590  // note: no forced page-break after IO
591  return result;
592 }
593 
594 template<class T> inline void CPUCore<T>::WRITE_PORT(unsigned port, byte value, unsigned cc)
595 {
596  EmuTime time = T::getTimeFast(cc);
597  scheduler.schedule(time);
598  interface->writeIO(port, value, time);
599  // note: no forced page-break after IO
600 }
601 
602 template<class T> template<bool PRE_PB, bool POST_PB>
603 NEVER_INLINE byte CPUCore<T>::RDMEMslow(unsigned address, unsigned cc)
604 {
605  // not cached
606  unsigned high = address >> CacheLine::BITS;
607  if (!readCacheTried[high]) {
608  // try to cache now
609  unsigned addrBase = address & CacheLine::HIGH;
610  if (const byte* line = interface->getReadCacheLine(addrBase)) {
611  // cached ok
612  T::template PRE_MEM<PRE_PB, POST_PB>(address);
613  T::template POST_MEM< POST_PB>(address);
614  readCacheLine[high] = line - addrBase;
615  return readCacheLine[high][address];
616  }
617  }
618  // uncacheable
619  readCacheTried[high] = true;
620  T::template PRE_MEM<PRE_PB, POST_PB>(address);
621  EmuTime time = T::getTimeFast(cc);
622  scheduler.schedule(time);
623  byte result = interface->readMem(address, time);
624  T::template POST_MEM<POST_PB>(address);
625  return result;
626 }
627 template<class T> template<bool PRE_PB, bool POST_PB>
628 ALWAYS_INLINE byte CPUCore<T>::RDMEM_impl2(unsigned address, unsigned cc)
629 {
630  const byte* line = readCacheLine[address >> CacheLine::BITS];
631  if (likely(line != nullptr)) {
632  // cached, fast path
633  T::template PRE_MEM<PRE_PB, POST_PB>(address);
634  T::template POST_MEM< POST_PB>(address);
635  return line[address];
636  } else {
637  return RDMEMslow<PRE_PB, POST_PB>(address, cc); // not inlined
638  }
639 }
640 template<class T> template<bool PRE_PB, bool POST_PB>
641 ALWAYS_INLINE byte CPUCore<T>::RDMEM_impl(unsigned address, unsigned cc)
642 {
643  static const bool PRE = T::template Normalize<PRE_PB >::value;
644  static const bool POST = T::template Normalize<POST_PB>::value;
645  return RDMEM_impl2<PRE, POST>(address, cc);
646 }
647 template<class T> template<unsigned PC_OFFSET> ALWAYS_INLINE byte CPUCore<T>::RDMEM_OPCODE(unsigned cc)
648 {
649  // Real Z80 would update the PC register now. In this implementation
650  // we've chosen to instead update PC only once at the end of the
651  // instruction. (Of course we made sure this difference is not
652  // noticeable by the program).
653  //
654  // See the comments in isM1Cycle() for the motivation for this
655  // deviation. Apart from that functional aspect it also turns out to be
656  // faster to only update PC once per instruction instead of after each
657  // fetch.
658  unsigned address = (getPC() + PC_OFFSET) & 0xFFFF;
659  return RDMEM_impl<false, false>(address, cc);
660 }
661 template<class T> ALWAYS_INLINE byte CPUCore<T>::RDMEM(unsigned address, unsigned cc)
662 {
663  return RDMEM_impl<true, true>(address, cc);
664 }
665 
666 template<class T> template<bool PRE_PB, bool POST_PB>
667 NEVER_INLINE unsigned CPUCore<T>::RD_WORD_slow(unsigned address, unsigned cc)
668 {
669  unsigned res = RDMEM_impl<PRE_PB, false>(address, cc);
670  res += RDMEM_impl<false, POST_PB>((address + 1) & 0xFFFF, cc + T::CC_RDMEM) << 8;
671  return res;
672 }
673 template<class T> template<bool PRE_PB, bool POST_PB>
674 ALWAYS_INLINE unsigned CPUCore<T>::RD_WORD_impl2(unsigned address, unsigned cc)
675 {
676  const byte* line = readCacheLine[address >> CacheLine::BITS];
677  if (likely(((address & CacheLine::LOW) != CacheLine::LOW) && line)) {
678  // fast path: cached and two bytes in same cache line
679  T::template PRE_WORD<PRE_PB, POST_PB>(address);
680  T::template POST_WORD< POST_PB>(address);
681  return Endian::read_UA_L16(&line[address]);
682  } else {
683  // slow path, not inline
684  return RD_WORD_slow<PRE_PB, POST_PB>(address, cc);
685  }
686 }
687 template<class T> template<bool PRE_PB, bool POST_PB>
688 ALWAYS_INLINE unsigned CPUCore<T>::RD_WORD_impl(unsigned address, unsigned cc)
689 {
690  static const bool PRE = T::template Normalize<PRE_PB >::value;
691  static const bool POST = T::template Normalize<POST_PB>::value;
692  return RD_WORD_impl2<PRE, POST>(address, cc);
693 }
694 template<class T> template<unsigned PC_OFFSET> ALWAYS_INLINE unsigned CPUCore<T>::RD_WORD_PC(unsigned cc)
695 {
696  unsigned addr = (getPC() + PC_OFFSET) & 0xFFFF;
697  return RD_WORD_impl<false, false>(addr, cc);
698 }
699 template<class T> ALWAYS_INLINE unsigned CPUCore<T>::RD_WORD(
700  unsigned address, unsigned cc)
701 {
702  return RD_WORD_impl<true, true>(address, cc);
703 }
704 
705 template<class T> template<bool PRE_PB, bool POST_PB>
706 NEVER_INLINE void CPUCore<T>::WRMEMslow(unsigned address, byte value, unsigned cc)
707 {
708  // not cached
709  unsigned high = address >> CacheLine::BITS;
710  if (!writeCacheTried[high]) {
711  // try to cache now
712  unsigned addrBase = address & CacheLine::HIGH;
713  if (byte* line = interface->getWriteCacheLine(addrBase)) {
714  // cached ok
715  T::template PRE_MEM<PRE_PB, POST_PB>(address);
716  T::template POST_MEM< POST_PB>(address);
717  writeCacheLine[high] = line - addrBase;
718  writeCacheLine[high][address] = value;
719  return;
720  }
721  }
722  // uncacheable
723  writeCacheTried[high] = true;
724  T::template PRE_MEM<PRE_PB, POST_PB>(address);
725  EmuTime time = T::getTimeFast(cc);
726  scheduler.schedule(time);
727  interface->writeMem(address, value, time);
728  T::template POST_MEM<POST_PB>(address);
729 }
730 template<class T> template<bool PRE_PB, bool POST_PB>
732  unsigned address, byte value, unsigned cc)
733 {
734  byte* line = writeCacheLine[address >> CacheLine::BITS];
735  if (likely(line != nullptr)) {
736  // cached, fast path
737  T::template PRE_MEM<PRE_PB, POST_PB>(address);
738  T::template POST_MEM< POST_PB>(address);
739  line[address] = value;
740  } else {
741  WRMEMslow<PRE_PB, POST_PB>(address, value, cc); // not inlined
742  }
743 }
744 template<class T> template<bool PRE_PB, bool POST_PB>
746  unsigned address, byte value, unsigned cc)
747 {
748  static const bool PRE = T::template Normalize<PRE_PB >::value;
749  static const bool POST = T::template Normalize<POST_PB>::value;
750  WRMEM_impl2<PRE, POST>(address, value, cc);
751 }
752 template<class T> ALWAYS_INLINE void CPUCore<T>::WRMEM(
753  unsigned address, byte value, unsigned cc)
754 {
755  WRMEM_impl<true, true>(address, value, cc);
756 }
757 
758 template<class T> NEVER_INLINE void CPUCore<T>::WR_WORD_slow(
759  unsigned address, unsigned value, unsigned cc)
760 {
761  WRMEM_impl<true, false>( address, value & 255, cc);
762  WRMEM_impl<false, true>((address + 1) & 0xFFFF, value >> 8, cc + T::CC_WRMEM);
763 }
764 template<class T> ALWAYS_INLINE void CPUCore<T>::WR_WORD(
765  unsigned address, unsigned value, unsigned cc)
766 {
767  byte* line = writeCacheLine[address >> CacheLine::BITS];
768  if (likely(((address & CacheLine::LOW) != CacheLine::LOW) && line)) {
769  // fast path: cached and two bytes in same cache line
770  T::template PRE_WORD<true, true>(address);
771  T::template POST_WORD< true>(address);
772  Endian::write_UA_L16(&line[address], value);
773  } else {
774  // slow path, not inline
775  WR_WORD_slow(address, value, cc);
776  }
777 }
778 
779 // same as WR_WORD, but writes high byte first
780 template<class T> template<bool PRE_PB, bool POST_PB>
782  unsigned address, unsigned value, unsigned cc)
783 {
784  WRMEM_impl<PRE_PB, false>((address + 1) & 0xFFFF, value >> 8, cc);
785  WRMEM_impl<false, POST_PB>( address, value & 255, cc + T::CC_WRMEM);
786 }
787 template<class T> template<bool PRE_PB, bool POST_PB>
789  unsigned address, unsigned value, unsigned cc)
790 {
791  byte* line = writeCacheLine[address >> CacheLine::BITS];
792  if (likely(((address & CacheLine::LOW) != CacheLine::LOW) && line)) {
793  // fast path: cached and two bytes in same cache line
794  T::template PRE_WORD<PRE_PB, POST_PB>(address);
795  T::template POST_WORD< POST_PB>(address);
796  Endian::write_UA_L16(&line[address], value);
797  } else {
798  // slow path, not inline
799  WR_WORD_rev_slow<PRE_PB, POST_PB>(address, value, cc);
800  }
801 }
802 template<class T> template<bool PRE_PB, bool POST_PB>
804  unsigned address, unsigned value, unsigned cc)
805 {
806  static const bool PRE = T::template Normalize<PRE_PB >::value;
807  static const bool POST = T::template Normalize<POST_PB>::value;
808  WR_WORD_rev2<PRE, POST>(address, value, cc);
809 }
810 
811 
812 // NMI interrupt
813 template<class T> inline void CPUCore<T>::nmi()
814 {
815  incR(1);
816  setHALT(false);
817  setIFF1(false);
818  PUSH<T::EE_NMI_1>(getPC());
819  setPC(0x0066);
820  T::add(T::CC_NMI);
821 }
822 
823 // IM0 interrupt
824 template<class T> inline void CPUCore<T>::irq0()
825 {
826  // TODO current implementation only works for 1-byte instructions
827  // ok for MSX
828  assert(interface->readIRQVector() == 0xFF);
829  incR(1);
830  setHALT(false);
831  setIFF1(false);
832  setIFF2(false);
833  PUSH<T::EE_IRQ0_1>(getPC());
834  setPC(0x0038);
835  T::setMemPtr(getPC());
836  T::add(T::CC_IRQ0);
837 }
838 
839 // IM1 interrupt
840 template<class T> inline void CPUCore<T>::irq1()
841 {
842  incR(1);
843  setHALT(false);
844  setIFF1(false);
845  setIFF2(false);
846  PUSH<T::EE_IRQ1_1>(getPC());
847  setPC(0x0038);
848  T::setMemPtr(getPC());
849  T::add(T::CC_IRQ1);
850 }
851 
852 // IM2 interrupt
853 template<class T> inline void CPUCore<T>::irq2()
854 {
855  incR(1);
856  setHALT(false);
857  setIFF1(false);
858  setIFF2(false);
859  PUSH<T::EE_IRQ2_1>(getPC());
860  unsigned x = interface->readIRQVector() | (getI() << 8);
861  setPC(RD_WORD(x, T::CC_IRQ2_2));
862  T::setMemPtr(getPC());
863  T::add(T::CC_IRQ2);
864 }
865 
866 template<class T>
868 {
869  checkNoCurrentFlags();
870 #ifdef USE_COMPUTED_GOTO
871  // Addresses of all main-opcode routines,
872  // Note that 40/49/53/5B/64/6D/7F is replaced by 00 (ld r,r == nop)
873  static void* opcodeTable[256] = {
874  &&op00, &&op01, &&op02, &&op03, &&op04, &&op05, &&op06, &&op07,
875  &&op08, &&op09, &&op0A, &&op0B, &&op0C, &&op0D, &&op0E, &&op0F,
876  &&op10, &&op11, &&op12, &&op13, &&op14, &&op15, &&op16, &&op17,
877  &&op18, &&op19, &&op1A, &&op1B, &&op1C, &&op1D, &&op1E, &&op1F,
878  &&op20, &&op21, &&op22, &&op23, &&op24, &&op25, &&op26, &&op27,
879  &&op28, &&op29, &&op2A, &&op2B, &&op2C, &&op2D, &&op2E, &&op2F,
880  &&op30, &&op31, &&op32, &&op33, &&op34, &&op35, &&op36, &&op37,
881  &&op38, &&op39, &&op3A, &&op3B, &&op3C, &&op3D, &&op3E, &&op3F,
882  &&op00, &&op41, &&op42, &&op43, &&op44, &&op45, &&op46, &&op47,
883  &&op48, &&op00, &&op4A, &&op4B, &&op4C, &&op4D, &&op4E, &&op4F,
884  &&op50, &&op51, &&op00, &&op53, &&op54, &&op55, &&op56, &&op57,
885  &&op58, &&op59, &&op5A, &&op00, &&op5C, &&op5D, &&op5E, &&op5F,
886  &&op60, &&op61, &&op62, &&op63, &&op00, &&op65, &&op66, &&op67,
887  &&op68, &&op69, &&op6A, &&op6B, &&op6C, &&op00, &&op6E, &&op6F,
888  &&op70, &&op71, &&op72, &&op73, &&op74, &&op75, &&op76, &&op77,
889  &&op78, &&op79, &&op7A, &&op7B, &&op7C, &&op7D, &&op7E, &&op00,
890  &&op80, &&op81, &&op82, &&op83, &&op84, &&op85, &&op86, &&op87,
891  &&op88, &&op89, &&op8A, &&op8B, &&op8C, &&op8D, &&op8E, &&op8F,
892  &&op90, &&op91, &&op92, &&op93, &&op94, &&op95, &&op96, &&op97,
893  &&op98, &&op99, &&op9A, &&op9B, &&op9C, &&op9D, &&op9E, &&op9F,
894  &&opA0, &&opA1, &&opA2, &&opA3, &&opA4, &&opA5, &&opA6, &&opA7,
895  &&opA8, &&opA9, &&opAA, &&opAB, &&opAC, &&opAD, &&opAE, &&opAF,
896  &&opB0, &&opB1, &&opB2, &&opB3, &&opB4, &&opB5, &&opB6, &&opB7,
897  &&opB8, &&opB9, &&opBA, &&opBB, &&opBC, &&opBD, &&opBE, &&opBF,
898  &&opC0, &&opC1, &&opC2, &&opC3, &&opC4, &&opC5, &&opC6, &&opC7,
899  &&opC8, &&opC9, &&opCA, &&opCB, &&opCC, &&opCD, &&opCE, &&opCF,
900  &&opD0, &&opD1, &&opD2, &&opD3, &&opD4, &&opD5, &&opD6, &&opD7,
901  &&opD8, &&opD9, &&opDA, &&opDB, &&opDC, &&opDD, &&opDE, &&opDF,
902  &&opE0, &&opE1, &&opE2, &&opE3, &&opE4, &&opE5, &&opE6, &&opE7,
903  &&opE8, &&opE9, &&opEA, &&opEB, &&opEC, &&opED, &&opEE, &&opEF,
904  &&opF0, &&opF1, &&opF2, &&opF3, &&opF4, &&opF5, &&opF6, &&opF7,
905  &&opF8, &&opF9, &&opFA, &&opFB, &&opFC, &&opFD, &&opFE, &&opFF,
906  };
907 
908 // Check T::limitReached(). If it's OK to continue,
909 // fetch and execute next instruction.
910 #define NEXT \
911  setPC(getPC() + ii.length); \
912  T::add(ii.cycles); \
913  T::R800Refresh(*this); \
914  if (likely(!T::limitReached())) { \
915  incR(1); \
916  unsigned address = getPC(); \
917  const byte* line = readCacheLine[address >> CacheLine::BITS]; \
918  if (likely(line != nullptr)) { \
919  T::template PRE_MEM<false, false>(address); \
920  T::template POST_MEM< false>(address); \
921  byte op = line[address]; \
922  goto *(opcodeTable[op]); \
923  } else { \
924  goto fetchSlow; \
925  } \
926  } \
927  return;
928 
929 // After some instructions we must always exit the CPU loop (ei, halt, retn)
930 #define NEXT_STOP \
931  setPC(getPC() + ii.length); \
932  T::add(ii.cycles); \
933  T::R800Refresh(*this); \
934  assert(T::limitReached()); \
935  return;
936 
937 #define NEXT_EI \
938  setPC(getPC() + ii.length); \
939  T::add(ii.cycles); \
940  /* !! NO T::R800Refresh(*this); !! */ \
941  assert(T::limitReached()); \
942  return;
943 
944 // Define a label (instead of case in a switch statement)
945 #define CASE(X) op##X:
946 
947 #else // USE_COMPUTED_GOTO
948 
949 #define NEXT \
950  setPC(getPC() + ii.length); \
951  T::add(ii.cycles); \
952  T::R800Refresh(*this); \
953  if (likely(!T::limitReached())) { \
954  goto start; \
955  } \
956  return;
957 
958 #define NEXT_STOP \
959  setPC(getPC() + ii.length); \
960  T::add(ii.cycles); \
961  T::R800Refresh(*this); \
962  assert(T::limitReached()); \
963  return;
964 
965 #define NEXT_EI \
966  setPC(getPC() + ii.length); \
967  T::add(ii.cycles); \
968  /* !! NO T::R800Refresh(*this); !! */ \
969  assert(T::limitReached()); \
970  return;
971 
972 #define CASE(X) case 0x##X:
973 
974 #endif // USE_COMPUTED_GOTO
975 
976 #ifndef USE_COMPUTED_GOTO
977 start:
978 #endif
979  unsigned ixy; // for dd_cb/fd_cb
980  byte opcodeMain = RDMEM_OPCODE<0>(T::CC_MAIN);
981  incR(1);
982 #ifdef USE_COMPUTED_GOTO
983  goto *(opcodeTable[opcodeMain]);
984 
985 fetchSlow: {
986  unsigned address = getPC();
987  byte opcodeSlow = RDMEMslow<false, false>(address, T::CC_MAIN);
988  goto *(opcodeTable[opcodeSlow]);
989 }
990 #endif
991 
992 #ifndef USE_COMPUTED_GOTO
993 switchopcode:
994  switch (opcodeMain) {
995 CASE(40) // ld b,b
996 CASE(49) // ld c,c
997 CASE(52) // ld d,d
998 CASE(5B) // ld e,e
999 CASE(64) // ld h,h
1000 CASE(6D) // ld l,l
1001 CASE(7F) // ld a,a
1002 #endif
1003 CASE(00) { II ii = nop(); NEXT; }
1004 CASE(07) { II ii = rlca(); NEXT; }
1005 CASE(0F) { II ii = rrca(); NEXT; }
1006 CASE(17) { II ii = rla(); NEXT; }
1007 CASE(1F) { II ii = rra(); NEXT; }
1008 CASE(08) { II ii = ex_af_af(); NEXT; }
1009 CASE(27) { II ii = daa(); NEXT; }
1010 CASE(2F) { II ii = cpl(); NEXT; }
1011 CASE(37) { II ii = scf(); NEXT; }
1012 CASE(3F) { II ii = ccf(); NEXT; }
1013 CASE(20) { II ii = jr(CondNZ()); NEXT; }
1014 CASE(28) { II ii = jr(CondZ ()); NEXT; }
1015 CASE(30) { II ii = jr(CondNC()); NEXT; }
1016 CASE(38) { II ii = jr(CondC ()); NEXT; }
1017 CASE(18) { II ii = jr(CondTrue()); NEXT; }
1018 CASE(10) { II ii = djnz(); NEXT; }
1019 CASE(32) { II ii = ld_xbyte_a(); NEXT; }
1020 CASE(3A) { II ii = ld_a_xbyte(); NEXT; }
1021 CASE(22) { II ii = ld_xword_SS<HL,0>(); NEXT; }
1022 CASE(2A) { II ii = ld_SS_xword<HL,0>(); NEXT; }
1023 CASE(02) { II ii = ld_SS_a<BC>(); NEXT; }
1024 CASE(12) { II ii = ld_SS_a<DE>(); NEXT; }
1025 CASE(1A) { II ii = ld_a_SS<DE>(); NEXT; }
1026 CASE(0A) { II ii = ld_a_SS<BC>(); NEXT; }
1027 CASE(03) { II ii = inc_SS<BC,0>(); NEXT; }
1028 CASE(13) { II ii = inc_SS<DE,0>(); NEXT; }
1029 CASE(23) { II ii = inc_SS<HL,0>(); NEXT; }
1030 CASE(33) { II ii = inc_SS<SP,0>(); NEXT; }
1031 CASE(0B) { II ii = dec_SS<BC,0>(); NEXT; }
1032 CASE(1B) { II ii = dec_SS<DE,0>(); NEXT; }
1033 CASE(2B) { II ii = dec_SS<HL,0>(); NEXT; }
1034 CASE(3B) { II ii = dec_SS<SP,0>(); NEXT; }
1035 CASE(09) { II ii = add_SS_TT<HL,BC,0>(); NEXT; }
1036 CASE(19) { II ii = add_SS_TT<HL,DE,0>(); NEXT; }
1037 CASE(29) { II ii = add_SS_SS<HL ,0>(); NEXT; }
1038 CASE(39) { II ii = add_SS_TT<HL,SP,0>(); NEXT; }
1039 CASE(01) { II ii = ld_SS_word<BC,0>(); NEXT; }
1040 CASE(11) { II ii = ld_SS_word<DE,0>(); NEXT; }
1041 CASE(21) { II ii = ld_SS_word<HL,0>(); NEXT; }
1042 CASE(31) { II ii = ld_SS_word<SP,0>(); NEXT; }
1043 CASE(04) { II ii = inc_R<B,0>(); NEXT; }
1044 CASE(0C) { II ii = inc_R<C,0>(); NEXT; }
1045 CASE(14) { II ii = inc_R<D,0>(); NEXT; }
1046 CASE(1C) { II ii = inc_R<E,0>(); NEXT; }
1047 CASE(24) { II ii = inc_R<H,0>(); NEXT; }
1048 CASE(2C) { II ii = inc_R<L,0>(); NEXT; }
1049 CASE(3C) { II ii = inc_R<A,0>(); NEXT; }
1050 CASE(34) { II ii = inc_xhl(); NEXT; }
1051 CASE(05) { II ii = dec_R<B,0>(); NEXT; }
1052 CASE(0D) { II ii = dec_R<C,0>(); NEXT; }
1053 CASE(15) { II ii = dec_R<D,0>(); NEXT; }
1054 CASE(1D) { II ii = dec_R<E,0>(); NEXT; }
1055 CASE(25) { II ii = dec_R<H,0>(); NEXT; }
1056 CASE(2D) { II ii = dec_R<L,0>(); NEXT; }
1057 CASE(3D) { II ii = dec_R<A,0>(); NEXT; }
1058 CASE(35) { II ii = dec_xhl(); NEXT; }
1059 CASE(06) { II ii = ld_R_byte<B,0>(); NEXT; }
1060 CASE(0E) { II ii = ld_R_byte<C,0>(); NEXT; }
1061 CASE(16) { II ii = ld_R_byte<D,0>(); NEXT; }
1062 CASE(1E) { II ii = ld_R_byte<E,0>(); NEXT; }
1063 CASE(26) { II ii = ld_R_byte<H,0>(); NEXT; }
1064 CASE(2E) { II ii = ld_R_byte<L,0>(); NEXT; }
1065 CASE(3E) { II ii = ld_R_byte<A,0>(); NEXT; }
1066 CASE(36) { II ii = ld_xhl_byte(); NEXT; }
1067 
1068 CASE(41) { II ii = ld_R_R<B,C,0>(); NEXT; }
1069 CASE(42) { II ii = ld_R_R<B,D,0>(); NEXT; }
1070 CASE(43) { II ii = ld_R_R<B,E,0>(); NEXT; }
1071 CASE(44) { II ii = ld_R_R<B,H,0>(); NEXT; }
1072 CASE(45) { II ii = ld_R_R<B,L,0>(); NEXT; }
1073 CASE(47) { II ii = ld_R_R<B,A,0>(); NEXT; }
1074 CASE(48) { II ii = ld_R_R<C,B,0>(); NEXT; }
1075 CASE(4A) { II ii = ld_R_R<C,D,0>(); NEXT; }
1076 CASE(4B) { II ii = ld_R_R<C,E,0>(); NEXT; }
1077 CASE(4C) { II ii = ld_R_R<C,H,0>(); NEXT; }
1078 CASE(4D) { II ii = ld_R_R<C,L,0>(); NEXT; }
1079 CASE(4F) { II ii = ld_R_R<C,A,0>(); NEXT; }
1080 CASE(50) { II ii = ld_R_R<D,B,0>(); NEXT; }
1081 CASE(51) { II ii = ld_R_R<D,C,0>(); NEXT; }
1082 CASE(53) { II ii = ld_R_R<D,E,0>(); NEXT; }
1083 CASE(54) { II ii = ld_R_R<D,H,0>(); NEXT; }
1084 CASE(55) { II ii = ld_R_R<D,L,0>(); NEXT; }
1085 CASE(57) { II ii = ld_R_R<D,A,0>(); NEXT; }
1086 CASE(58) { II ii = ld_R_R<E,B,0>(); NEXT; }
1087 CASE(59) { II ii = ld_R_R<E,C,0>(); NEXT; }
1088 CASE(5A) { II ii = ld_R_R<E,D,0>(); NEXT; }
1089 CASE(5C) { II ii = ld_R_R<E,H,0>(); NEXT; }
1090 CASE(5D) { II ii = ld_R_R<E,L,0>(); NEXT; }
1091 CASE(5F) { II ii = ld_R_R<E,A,0>(); NEXT; }
1092 CASE(60) { II ii = ld_R_R<H,B,0>(); NEXT; }
1093 CASE(61) { II ii = ld_R_R<H,C,0>(); NEXT; }
1094 CASE(62) { II ii = ld_R_R<H,D,0>(); NEXT; }
1095 CASE(63) { II ii = ld_R_R<H,E,0>(); NEXT; }
1096 CASE(65) { II ii = ld_R_R<H,L,0>(); NEXT; }
1097 CASE(67) { II ii = ld_R_R<H,A,0>(); NEXT; }
1098 CASE(68) { II ii = ld_R_R<L,B,0>(); NEXT; }
1099 CASE(69) { II ii = ld_R_R<L,C,0>(); NEXT; }
1100 CASE(6A) { II ii = ld_R_R<L,D,0>(); NEXT; }
1101 CASE(6B) { II ii = ld_R_R<L,E,0>(); NEXT; }
1102 CASE(6C) { II ii = ld_R_R<L,H,0>(); NEXT; }
1103 CASE(6F) { II ii = ld_R_R<L,A,0>(); NEXT; }
1104 CASE(78) { II ii = ld_R_R<A,B,0>(); NEXT; }
1105 CASE(79) { II ii = ld_R_R<A,C,0>(); NEXT; }
1106 CASE(7A) { II ii = ld_R_R<A,D,0>(); NEXT; }
1107 CASE(7B) { II ii = ld_R_R<A,E,0>(); NEXT; }
1108 CASE(7C) { II ii = ld_R_R<A,H,0>(); NEXT; }
1109 CASE(7D) { II ii = ld_R_R<A,L,0>(); NEXT; }
1110 CASE(70) { II ii = ld_xhl_R<B>(); NEXT; }
1111 CASE(71) { II ii = ld_xhl_R<C>(); NEXT; }
1112 CASE(72) { II ii = ld_xhl_R<D>(); NEXT; }
1113 CASE(73) { II ii = ld_xhl_R<E>(); NEXT; }
1114 CASE(74) { II ii = ld_xhl_R<H>(); NEXT; }
1115 CASE(75) { II ii = ld_xhl_R<L>(); NEXT; }
1116 CASE(77) { II ii = ld_xhl_R<A>(); NEXT; }
1117 CASE(46) { II ii = ld_R_xhl<B>(); NEXT; }
1118 CASE(4E) { II ii = ld_R_xhl<C>(); NEXT; }
1119 CASE(56) { II ii = ld_R_xhl<D>(); NEXT; }
1120 CASE(5E) { II ii = ld_R_xhl<E>(); NEXT; }
1121 CASE(66) { II ii = ld_R_xhl<H>(); NEXT; }
1122 CASE(6E) { II ii = ld_R_xhl<L>(); NEXT; }
1123 CASE(7E) { II ii = ld_R_xhl<A>(); NEXT; }
1124 CASE(76) { II ii = halt(); NEXT_STOP; }
1125 
1126 CASE(80) { II ii = add_a_R<B,0>(); NEXT; }
1127 CASE(81) { II ii = add_a_R<C,0>(); NEXT; }
1128 CASE(82) { II ii = add_a_R<D,0>(); NEXT; }
1129 CASE(83) { II ii = add_a_R<E,0>(); NEXT; }
1130 CASE(84) { II ii = add_a_R<H,0>(); NEXT; }
1131 CASE(85) { II ii = add_a_R<L,0>(); NEXT; }
1132 CASE(86) { II ii = add_a_xhl(); NEXT; }
1133 CASE(87) { II ii = add_a_a(); NEXT; }
1134 CASE(88) { II ii = adc_a_R<B,0>(); NEXT; }
1135 CASE(89) { II ii = adc_a_R<C,0>(); NEXT; }
1136 CASE(8A) { II ii = adc_a_R<D,0>(); NEXT; }
1137 CASE(8B) { II ii = adc_a_R<E,0>(); NEXT; }
1138 CASE(8C) { II ii = adc_a_R<H,0>(); NEXT; }
1139 CASE(8D) { II ii = adc_a_R<L,0>(); NEXT; }
1140 CASE(8E) { II ii = adc_a_xhl(); NEXT; }
1141 CASE(8F) { II ii = adc_a_a(); NEXT; }
1142 CASE(90) { II ii = sub_R<B,0>(); NEXT; }
1143 CASE(91) { II ii = sub_R<C,0>(); NEXT; }
1144 CASE(92) { II ii = sub_R<D,0>(); NEXT; }
1145 CASE(93) { II ii = sub_R<E,0>(); NEXT; }
1146 CASE(94) { II ii = sub_R<H,0>(); NEXT; }
1147 CASE(95) { II ii = sub_R<L,0>(); NEXT; }
1148 CASE(96) { II ii = sub_xhl(); NEXT; }
1149 CASE(97) { II ii = sub_a(); NEXT; }
1150 CASE(98) { II ii = sbc_a_R<B,0>(); NEXT; }
1151 CASE(99) { II ii = sbc_a_R<C,0>(); NEXT; }
1152 CASE(9A) { II ii = sbc_a_R<D,0>(); NEXT; }
1153 CASE(9B) { II ii = sbc_a_R<E,0>(); NEXT; }
1154 CASE(9C) { II ii = sbc_a_R<H,0>(); NEXT; }
1155 CASE(9D) { II ii = sbc_a_R<L,0>(); NEXT; }
1156 CASE(9E) { II ii = sbc_a_xhl(); NEXT; }
1157 CASE(9F) { II ii = sbc_a_a(); NEXT; }
1158 CASE(A0) { II ii = and_R<B,0>(); NEXT; }
1159 CASE(A1) { II ii = and_R<C,0>(); NEXT; }
1160 CASE(A2) { II ii = and_R<D,0>(); NEXT; }
1161 CASE(A3) { II ii = and_R<E,0>(); NEXT; }
1162 CASE(A4) { II ii = and_R<H,0>(); NEXT; }
1163 CASE(A5) { II ii = and_R<L,0>(); NEXT; }
1164 CASE(A6) { II ii = and_xhl(); NEXT; }
1165 CASE(A7) { II ii = and_a(); NEXT; }
1166 CASE(A8) { II ii = xor_R<B,0>(); NEXT; }
1167 CASE(A9) { II ii = xor_R<C,0>(); NEXT; }
1168 CASE(AA) { II ii = xor_R<D,0>(); NEXT; }
1169 CASE(AB) { II ii = xor_R<E,0>(); NEXT; }
1170 CASE(AC) { II ii = xor_R<H,0>(); NEXT; }
1171 CASE(AD) { II ii = xor_R<L,0>(); NEXT; }
1172 CASE(AE) { II ii = xor_xhl(); NEXT; }
1173 CASE(AF) { II ii = xor_a(); NEXT; }
1174 CASE(B0) { II ii = or_R<B,0>(); NEXT; }
1175 CASE(B1) { II ii = or_R<C,0>(); NEXT; }
1176 CASE(B2) { II ii = or_R<D,0>(); NEXT; }
1177 CASE(B3) { II ii = or_R<E,0>(); NEXT; }
1178 CASE(B4) { II ii = or_R<H,0>(); NEXT; }
1179 CASE(B5) { II ii = or_R<L,0>(); NEXT; }
1180 CASE(B6) { II ii = or_xhl(); NEXT; }
1181 CASE(B7) { II ii = or_a(); NEXT; }
1182 CASE(B8) { II ii = cp_R<B,0>(); NEXT; }
1183 CASE(B9) { II ii = cp_R<C,0>(); NEXT; }
1184 CASE(BA) { II ii = cp_R<D,0>(); NEXT; }
1185 CASE(BB) { II ii = cp_R<E,0>(); NEXT; }
1186 CASE(BC) { II ii = cp_R<H,0>(); NEXT; }
1187 CASE(BD) { II ii = cp_R<L,0>(); NEXT; }
1188 CASE(BE) { II ii = cp_xhl(); NEXT; }
1189 CASE(BF) { II ii = cp_a(); NEXT; }
1190 
1191 CASE(D3) { II ii = out_byte_a(); NEXT; }
1192 CASE(DB) { II ii = in_a_byte(); NEXT; }
1193 CASE(D9) { II ii = exx(); NEXT; }
1194 CASE(E3) { II ii = ex_xsp_SS<HL,0>(); NEXT; }
1195 CASE(EB) { II ii = ex_de_hl(); NEXT; }
1196 CASE(E9) { II ii = jp_SS<HL,0>(); NEXT; }
1197 CASE(F9) { II ii = ld_sp_SS<HL,0>(); NEXT; }
1198 CASE(F3) { II ii = di(); NEXT; }
1199 CASE(FB) { II ii = ei(); NEXT_EI; }
1200 CASE(C6) { II ii = add_a_byte(); NEXT; }
1201 CASE(CE) { II ii = adc_a_byte(); NEXT; }
1202 CASE(D6) { II ii = sub_byte(); NEXT; }
1203 CASE(DE) { II ii = sbc_a_byte(); NEXT; }
1204 CASE(E6) { II ii = and_byte(); NEXT; }
1205 CASE(EE) { II ii = xor_byte(); NEXT; }
1206 CASE(F6) { II ii = or_byte(); NEXT; }
1207 CASE(FE) { II ii = cp_byte(); NEXT; }
1208 CASE(C0) { II ii = ret(CondNZ()); NEXT; }
1209 CASE(C8) { II ii = ret(CondZ ()); NEXT; }
1210 CASE(D0) { II ii = ret(CondNC()); NEXT; }
1211 CASE(D8) { II ii = ret(CondC ()); NEXT; }
1212 CASE(E0) { II ii = ret(CondPO()); NEXT; }
1213 CASE(E8) { II ii = ret(CondPE()); NEXT; }
1214 CASE(F0) { II ii = ret(CondP ()); NEXT; }
1215 CASE(F8) { II ii = ret(CondM ()); NEXT; }
1216 CASE(C9) { II ii = ret(); NEXT; }
1217 CASE(C2) { II ii = jp(CondNZ()); NEXT; }
1218 CASE(CA) { II ii = jp(CondZ ()); NEXT; }
1219 CASE(D2) { II ii = jp(CondNC()); NEXT; }
1220 CASE(DA) { II ii = jp(CondC ()); NEXT; }
1221 CASE(E2) { II ii = jp(CondPO()); NEXT; }
1222 CASE(EA) { II ii = jp(CondPE()); NEXT; }
1223 CASE(F2) { II ii = jp(CondP ()); NEXT; }
1224 CASE(FA) { II ii = jp(CondM ()); NEXT; }
1225 CASE(C3) { II ii = jp(CondTrue()); NEXT; }
1226 CASE(C4) { II ii = call(CondNZ()); NEXT; }
1227 CASE(CC) { II ii = call(CondZ ()); NEXT; }
1228 CASE(D4) { II ii = call(CondNC()); NEXT; }
1229 CASE(DC) { II ii = call(CondC ()); NEXT; }
1230 CASE(E4) { II ii = call(CondPO()); NEXT; }
1231 CASE(EC) { II ii = call(CondPE()); NEXT; }
1232 CASE(F4) { II ii = call(CondP ()); NEXT; }
1233 CASE(FC) { II ii = call(CondM ()); NEXT; }
1234 CASE(CD) { II ii = call(CondTrue()); NEXT; }
1235 CASE(C1) { II ii = pop_SS <BC,0>(); NEXT; }
1236 CASE(D1) { II ii = pop_SS <DE,0>(); NEXT; }
1237 CASE(E1) { II ii = pop_SS <HL,0>(); NEXT; }
1238 CASE(F1) { II ii = pop_SS <AF,0>(); NEXT; }
1239 CASE(C5) { II ii = push_SS<BC,0>(); NEXT; }
1240 CASE(D5) { II ii = push_SS<DE,0>(); NEXT; }
1241 CASE(E5) { II ii = push_SS<HL,0>(); NEXT; }
1242 CASE(F5) { II ii = push_SS<AF,0>(); NEXT; }
1243 CASE(C7) { II ii = rst<0x00>(); NEXT; }
1244 CASE(CF) { II ii = rst<0x08>(); NEXT; }
1245 CASE(D7) { II ii = rst<0x10>(); NEXT; }
1246 CASE(DF) { II ii = rst<0x18>(); NEXT; }
1247 CASE(E7) { II ii = rst<0x20>(); NEXT; }
1248 CASE(EF) { II ii = rst<0x28>(); NEXT; }
1249 CASE(F7) { II ii = rst<0x30>(); NEXT; }
1250 CASE(FF) { II ii = rst<0x38>(); NEXT; }
1251 CASE(CB) {
1252  setPC(getPC() + 1); // M1 cycle at this point
1253  byte cb_opcode = RDMEM_OPCODE<0>(T::CC_PREFIX);
1254  incR(1);
1255  switch (cb_opcode) {
1256  case 0x00: { II ii = rlc_R<B>(); NEXT; }
1257  case 0x01: { II ii = rlc_R<C>(); NEXT; }
1258  case 0x02: { II ii = rlc_R<D>(); NEXT; }
1259  case 0x03: { II ii = rlc_R<E>(); NEXT; }
1260  case 0x04: { II ii = rlc_R<H>(); NEXT; }
1261  case 0x05: { II ii = rlc_R<L>(); NEXT; }
1262  case 0x07: { II ii = rlc_R<A>(); NEXT; }
1263  case 0x06: { II ii = rlc_xhl(); NEXT; }
1264  case 0x08: { II ii = rrc_R<B>(); NEXT; }
1265  case 0x09: { II ii = rrc_R<C>(); NEXT; }
1266  case 0x0a: { II ii = rrc_R<D>(); NEXT; }
1267  case 0x0b: { II ii = rrc_R<E>(); NEXT; }
1268  case 0x0c: { II ii = rrc_R<H>(); NEXT; }
1269  case 0x0d: { II ii = rrc_R<L>(); NEXT; }
1270  case 0x0f: { II ii = rrc_R<A>(); NEXT; }
1271  case 0x0e: { II ii = rrc_xhl(); NEXT; }
1272  case 0x10: { II ii = rl_R<B>(); NEXT; }
1273  case 0x11: { II ii = rl_R<C>(); NEXT; }
1274  case 0x12: { II ii = rl_R<D>(); NEXT; }
1275  case 0x13: { II ii = rl_R<E>(); NEXT; }
1276  case 0x14: { II ii = rl_R<H>(); NEXT; }
1277  case 0x15: { II ii = rl_R<L>(); NEXT; }
1278  case 0x17: { II ii = rl_R<A>(); NEXT; }
1279  case 0x16: { II ii = rl_xhl(); NEXT; }
1280  case 0x18: { II ii = rr_R<B>(); NEXT; }
1281  case 0x19: { II ii = rr_R<C>(); NEXT; }
1282  case 0x1a: { II ii = rr_R<D>(); NEXT; }
1283  case 0x1b: { II ii = rr_R<E>(); NEXT; }
1284  case 0x1c: { II ii = rr_R<H>(); NEXT; }
1285  case 0x1d: { II ii = rr_R<L>(); NEXT; }
1286  case 0x1f: { II ii = rr_R<A>(); NEXT; }
1287  case 0x1e: { II ii = rr_xhl(); NEXT; }
1288  case 0x20: { II ii = sla_R<B>(); NEXT; }
1289  case 0x21: { II ii = sla_R<C>(); NEXT; }
1290  case 0x22: { II ii = sla_R<D>(); NEXT; }
1291  case 0x23: { II ii = sla_R<E>(); NEXT; }
1292  case 0x24: { II ii = sla_R<H>(); NEXT; }
1293  case 0x25: { II ii = sla_R<L>(); NEXT; }
1294  case 0x27: { II ii = sla_R<A>(); NEXT; }
1295  case 0x26: { II ii = sla_xhl(); NEXT; }
1296  case 0x28: { II ii = sra_R<B>(); NEXT; }
1297  case 0x29: { II ii = sra_R<C>(); NEXT; }
1298  case 0x2a: { II ii = sra_R<D>(); NEXT; }
1299  case 0x2b: { II ii = sra_R<E>(); NEXT; }
1300  case 0x2c: { II ii = sra_R<H>(); NEXT; }
1301  case 0x2d: { II ii = sra_R<L>(); NEXT; }
1302  case 0x2f: { II ii = sra_R<A>(); NEXT; }
1303  case 0x2e: { II ii = sra_xhl(); NEXT; }
1304  case 0x30: { II ii = T::isR800() ? sla_R<B>() : sll_R<B>(); NEXT; }
1305  case 0x31: { II ii = T::isR800() ? sla_R<C>() : sll_R<C>(); NEXT; }
1306  case 0x32: { II ii = T::isR800() ? sla_R<D>() : sll_R<D>(); NEXT; }
1307  case 0x33: { II ii = T::isR800() ? sla_R<E>() : sll_R<E>(); NEXT; }
1308  case 0x34: { II ii = T::isR800() ? sla_R<H>() : sll_R<H>(); NEXT; }
1309  case 0x35: { II ii = T::isR800() ? sla_R<L>() : sll_R<L>(); NEXT; }
1310  case 0x37: { II ii = T::isR800() ? sla_R<A>() : sll_R<A>(); NEXT; }
1311  case 0x36: { II ii = T::isR800() ? sla_xhl() : sll_xhl(); NEXT; }
1312  case 0x38: { II ii = srl_R<B>(); NEXT; }
1313  case 0x39: { II ii = srl_R<C>(); NEXT; }
1314  case 0x3a: { II ii = srl_R<D>(); NEXT; }
1315  case 0x3b: { II ii = srl_R<E>(); NEXT; }
1316  case 0x3c: { II ii = srl_R<H>(); NEXT; }
1317  case 0x3d: { II ii = srl_R<L>(); NEXT; }
1318  case 0x3f: { II ii = srl_R<A>(); NEXT; }
1319  case 0x3e: { II ii = srl_xhl(); NEXT; }
1320 
1321  case 0x40: { II ii = bit_N_R<0,B>(); NEXT; }
1322  case 0x41: { II ii = bit_N_R<0,C>(); NEXT; }
1323  case 0x42: { II ii = bit_N_R<0,D>(); NEXT; }
1324  case 0x43: { II ii = bit_N_R<0,E>(); NEXT; }
1325  case 0x44: { II ii = bit_N_R<0,H>(); NEXT; }
1326  case 0x45: { II ii = bit_N_R<0,L>(); NEXT; }
1327  case 0x47: { II ii = bit_N_R<0,A>(); NEXT; }
1328  case 0x48: { II ii = bit_N_R<1,B>(); NEXT; }
1329  case 0x49: { II ii = bit_N_R<1,C>(); NEXT; }
1330  case 0x4a: { II ii = bit_N_R<1,D>(); NEXT; }
1331  case 0x4b: { II ii = bit_N_R<1,E>(); NEXT; }
1332  case 0x4c: { II ii = bit_N_R<1,H>(); NEXT; }
1333  case 0x4d: { II ii = bit_N_R<1,L>(); NEXT; }
1334  case 0x4f: { II ii = bit_N_R<1,A>(); NEXT; }
1335  case 0x50: { II ii = bit_N_R<2,B>(); NEXT; }
1336  case 0x51: { II ii = bit_N_R<2,C>(); NEXT; }
1337  case 0x52: { II ii = bit_N_R<2,D>(); NEXT; }
1338  case 0x53: { II ii = bit_N_R<2,E>(); NEXT; }
1339  case 0x54: { II ii = bit_N_R<2,H>(); NEXT; }
1340  case 0x55: { II ii = bit_N_R<2,L>(); NEXT; }
1341  case 0x57: { II ii = bit_N_R<2,A>(); NEXT; }
1342  case 0x58: { II ii = bit_N_R<3,B>(); NEXT; }
1343  case 0x59: { II ii = bit_N_R<3,C>(); NEXT; }
1344  case 0x5a: { II ii = bit_N_R<3,D>(); NEXT; }
1345  case 0x5b: { II ii = bit_N_R<3,E>(); NEXT; }
1346  case 0x5c: { II ii = bit_N_R<3,H>(); NEXT; }
1347  case 0x5d: { II ii = bit_N_R<3,L>(); NEXT; }
1348  case 0x5f: { II ii = bit_N_R<3,A>(); NEXT; }
1349  case 0x60: { II ii = bit_N_R<4,B>(); NEXT; }
1350  case 0x61: { II ii = bit_N_R<4,C>(); NEXT; }
1351  case 0x62: { II ii = bit_N_R<4,D>(); NEXT; }
1352  case 0x63: { II ii = bit_N_R<4,E>(); NEXT; }
1353  case 0x64: { II ii = bit_N_R<4,H>(); NEXT; }
1354  case 0x65: { II ii = bit_N_R<4,L>(); NEXT; }
1355  case 0x67: { II ii = bit_N_R<4,A>(); NEXT; }
1356  case 0x68: { II ii = bit_N_R<5,B>(); NEXT; }
1357  case 0x69: { II ii = bit_N_R<5,C>(); NEXT; }
1358  case 0x6a: { II ii = bit_N_R<5,D>(); NEXT; }
1359  case 0x6b: { II ii = bit_N_R<5,E>(); NEXT; }
1360  case 0x6c: { II ii = bit_N_R<5,H>(); NEXT; }
1361  case 0x6d: { II ii = bit_N_R<5,L>(); NEXT; }
1362  case 0x6f: { II ii = bit_N_R<5,A>(); NEXT; }
1363  case 0x70: { II ii = bit_N_R<6,B>(); NEXT; }
1364  case 0x71: { II ii = bit_N_R<6,C>(); NEXT; }
1365  case 0x72: { II ii = bit_N_R<6,D>(); NEXT; }
1366  case 0x73: { II ii = bit_N_R<6,E>(); NEXT; }
1367  case 0x74: { II ii = bit_N_R<6,H>(); NEXT; }
1368  case 0x75: { II ii = bit_N_R<6,L>(); NEXT; }
1369  case 0x77: { II ii = bit_N_R<6,A>(); NEXT; }
1370  case 0x78: { II ii = bit_N_R<7,B>(); NEXT; }
1371  case 0x79: { II ii = bit_N_R<7,C>(); NEXT; }
1372  case 0x7a: { II ii = bit_N_R<7,D>(); NEXT; }
1373  case 0x7b: { II ii = bit_N_R<7,E>(); NEXT; }
1374  case 0x7c: { II ii = bit_N_R<7,H>(); NEXT; }
1375  case 0x7d: { II ii = bit_N_R<7,L>(); NEXT; }
1376  case 0x7f: { II ii = bit_N_R<7,A>(); NEXT; }
1377  case 0x46: { II ii = bit_N_xhl<0>(); NEXT; }
1378  case 0x4e: { II ii = bit_N_xhl<1>(); NEXT; }
1379  case 0x56: { II ii = bit_N_xhl<2>(); NEXT; }
1380  case 0x5e: { II ii = bit_N_xhl<3>(); NEXT; }
1381  case 0x66: { II ii = bit_N_xhl<4>(); NEXT; }
1382  case 0x6e: { II ii = bit_N_xhl<5>(); NEXT; }
1383  case 0x76: { II ii = bit_N_xhl<6>(); NEXT; }
1384  case 0x7e: { II ii = bit_N_xhl<7>(); NEXT; }
1385 
1386  case 0x80: { II ii = res_N_R<0,B>(); NEXT; }
1387  case 0x81: { II ii = res_N_R<0,C>(); NEXT; }
1388  case 0x82: { II ii = res_N_R<0,D>(); NEXT; }
1389  case 0x83: { II ii = res_N_R<0,E>(); NEXT; }
1390  case 0x84: { II ii = res_N_R<0,H>(); NEXT; }
1391  case 0x85: { II ii = res_N_R<0,L>(); NEXT; }
1392  case 0x87: { II ii = res_N_R<0,A>(); NEXT; }
1393  case 0x88: { II ii = res_N_R<1,B>(); NEXT; }
1394  case 0x89: { II ii = res_N_R<1,C>(); NEXT; }
1395  case 0x8a: { II ii = res_N_R<1,D>(); NEXT; }
1396  case 0x8b: { II ii = res_N_R<1,E>(); NEXT; }
1397  case 0x8c: { II ii = res_N_R<1,H>(); NEXT; }
1398  case 0x8d: { II ii = res_N_R<1,L>(); NEXT; }
1399  case 0x8f: { II ii = res_N_R<1,A>(); NEXT; }
1400  case 0x90: { II ii = res_N_R<2,B>(); NEXT; }
1401  case 0x91: { II ii = res_N_R<2,C>(); NEXT; }
1402  case 0x92: { II ii = res_N_R<2,D>(); NEXT; }
1403  case 0x93: { II ii = res_N_R<2,E>(); NEXT; }
1404  case 0x94: { II ii = res_N_R<2,H>(); NEXT; }
1405  case 0x95: { II ii = res_N_R<2,L>(); NEXT; }
1406  case 0x97: { II ii = res_N_R<2,A>(); NEXT; }
1407  case 0x98: { II ii = res_N_R<3,B>(); NEXT; }
1408  case 0x99: { II ii = res_N_R<3,C>(); NEXT; }
1409  case 0x9a: { II ii = res_N_R<3,D>(); NEXT; }
1410  case 0x9b: { II ii = res_N_R<3,E>(); NEXT; }
1411  case 0x9c: { II ii = res_N_R<3,H>(); NEXT; }
1412  case 0x9d: { II ii = res_N_R<3,L>(); NEXT; }
1413  case 0x9f: { II ii = res_N_R<3,A>(); NEXT; }
1414  case 0xa0: { II ii = res_N_R<4,B>(); NEXT; }
1415  case 0xa1: { II ii = res_N_R<4,C>(); NEXT; }
1416  case 0xa2: { II ii = res_N_R<4,D>(); NEXT; }
1417  case 0xa3: { II ii = res_N_R<4,E>(); NEXT; }
1418  case 0xa4: { II ii = res_N_R<4,H>(); NEXT; }
1419  case 0xa5: { II ii = res_N_R<4,L>(); NEXT; }
1420  case 0xa7: { II ii = res_N_R<4,A>(); NEXT; }
1421  case 0xa8: { II ii = res_N_R<5,B>(); NEXT; }
1422  case 0xa9: { II ii = res_N_R<5,C>(); NEXT; }
1423  case 0xaa: { II ii = res_N_R<5,D>(); NEXT; }
1424  case 0xab: { II ii = res_N_R<5,E>(); NEXT; }
1425  case 0xac: { II ii = res_N_R<5,H>(); NEXT; }
1426  case 0xad: { II ii = res_N_R<5,L>(); NEXT; }
1427  case 0xaf: { II ii = res_N_R<5,A>(); NEXT; }
1428  case 0xb0: { II ii = res_N_R<6,B>(); NEXT; }
1429  case 0xb1: { II ii = res_N_R<6,C>(); NEXT; }
1430  case 0xb2: { II ii = res_N_R<6,D>(); NEXT; }
1431  case 0xb3: { II ii = res_N_R<6,E>(); NEXT; }
1432  case 0xb4: { II ii = res_N_R<6,H>(); NEXT; }
1433  case 0xb5: { II ii = res_N_R<6,L>(); NEXT; }
1434  case 0xb7: { II ii = res_N_R<6,A>(); NEXT; }
1435  case 0xb8: { II ii = res_N_R<7,B>(); NEXT; }
1436  case 0xb9: { II ii = res_N_R<7,C>(); NEXT; }
1437  case 0xba: { II ii = res_N_R<7,D>(); NEXT; }
1438  case 0xbb: { II ii = res_N_R<7,E>(); NEXT; }
1439  case 0xbc: { II ii = res_N_R<7,H>(); NEXT; }
1440  case 0xbd: { II ii = res_N_R<7,L>(); NEXT; }
1441  case 0xbf: { II ii = res_N_R<7,A>(); NEXT; }
1442  case 0x86: { II ii = res_N_xhl<0>(); NEXT; }
1443  case 0x8e: { II ii = res_N_xhl<1>(); NEXT; }
1444  case 0x96: { II ii = res_N_xhl<2>(); NEXT; }
1445  case 0x9e: { II ii = res_N_xhl<3>(); NEXT; }
1446  case 0xa6: { II ii = res_N_xhl<4>(); NEXT; }
1447  case 0xae: { II ii = res_N_xhl<5>(); NEXT; }
1448  case 0xb6: { II ii = res_N_xhl<6>(); NEXT; }
1449  case 0xbe: { II ii = res_N_xhl<7>(); NEXT; }
1450 
1451  case 0xc0: { II ii = set_N_R<0,B>(); NEXT; }
1452  case 0xc1: { II ii = set_N_R<0,C>(); NEXT; }
1453  case 0xc2: { II ii = set_N_R<0,D>(); NEXT; }
1454  case 0xc3: { II ii = set_N_R<0,E>(); NEXT; }
1455  case 0xc4: { II ii = set_N_R<0,H>(); NEXT; }
1456  case 0xc5: { II ii = set_N_R<0,L>(); NEXT; }
1457  case 0xc7: { II ii = set_N_R<0,A>(); NEXT; }
1458  case 0xc8: { II ii = set_N_R<1,B>(); NEXT; }
1459  case 0xc9: { II ii = set_N_R<1,C>(); NEXT; }
1460  case 0xca: { II ii = set_N_R<1,D>(); NEXT; }
1461  case 0xcb: { II ii = set_N_R<1,E>(); NEXT; }
1462  case 0xcc: { II ii = set_N_R<1,H>(); NEXT; }
1463  case 0xcd: { II ii = set_N_R<1,L>(); NEXT; }
1464  case 0xcf: { II ii = set_N_R<1,A>(); NEXT; }
1465  case 0xd0: { II ii = set_N_R<2,B>(); NEXT; }
1466  case 0xd1: { II ii = set_N_R<2,C>(); NEXT; }
1467  case 0xd2: { II ii = set_N_R<2,D>(); NEXT; }
1468  case 0xd3: { II ii = set_N_R<2,E>(); NEXT; }
1469  case 0xd4: { II ii = set_N_R<2,H>(); NEXT; }
1470  case 0xd5: { II ii = set_N_R<2,L>(); NEXT; }
1471  case 0xd7: { II ii = set_N_R<2,A>(); NEXT; }
1472  case 0xd8: { II ii = set_N_R<3,B>(); NEXT; }
1473  case 0xd9: { II ii = set_N_R<3,C>(); NEXT; }
1474  case 0xda: { II ii = set_N_R<3,D>(); NEXT; }
1475  case 0xdb: { II ii = set_N_R<3,E>(); NEXT; }
1476  case 0xdc: { II ii = set_N_R<3,H>(); NEXT; }
1477  case 0xdd: { II ii = set_N_R<3,L>(); NEXT; }
1478  case 0xdf: { II ii = set_N_R<3,A>(); NEXT; }
1479  case 0xe0: { II ii = set_N_R<4,B>(); NEXT; }
1480  case 0xe1: { II ii = set_N_R<4,C>(); NEXT; }
1481  case 0xe2: { II ii = set_N_R<4,D>(); NEXT; }
1482  case 0xe3: { II ii = set_N_R<4,E>(); NEXT; }
1483  case 0xe4: { II ii = set_N_R<4,H>(); NEXT; }
1484  case 0xe5: { II ii = set_N_R<4,L>(); NEXT; }
1485  case 0xe7: { II ii = set_N_R<4,A>(); NEXT; }
1486  case 0xe8: { II ii = set_N_R<5,B>(); NEXT; }
1487  case 0xe9: { II ii = set_N_R<5,C>(); NEXT; }
1488  case 0xea: { II ii = set_N_R<5,D>(); NEXT; }
1489  case 0xeb: { II ii = set_N_R<5,E>(); NEXT; }
1490  case 0xec: { II ii = set_N_R<5,H>(); NEXT; }
1491  case 0xed: { II ii = set_N_R<5,L>(); NEXT; }
1492  case 0xef: { II ii = set_N_R<5,A>(); NEXT; }
1493  case 0xf0: { II ii = set_N_R<6,B>(); NEXT; }
1494  case 0xf1: { II ii = set_N_R<6,C>(); NEXT; }
1495  case 0xf2: { II ii = set_N_R<6,D>(); NEXT; }
1496  case 0xf3: { II ii = set_N_R<6,E>(); NEXT; }
1497  case 0xf4: { II ii = set_N_R<6,H>(); NEXT; }
1498  case 0xf5: { II ii = set_N_R<6,L>(); NEXT; }
1499  case 0xf7: { II ii = set_N_R<6,A>(); NEXT; }
1500  case 0xf8: { II ii = set_N_R<7,B>(); NEXT; }
1501  case 0xf9: { II ii = set_N_R<7,C>(); NEXT; }
1502  case 0xfa: { II ii = set_N_R<7,D>(); NEXT; }
1503  case 0xfb: { II ii = set_N_R<7,E>(); NEXT; }
1504  case 0xfc: { II ii = set_N_R<7,H>(); NEXT; }
1505  case 0xfd: { II ii = set_N_R<7,L>(); NEXT; }
1506  case 0xff: { II ii = set_N_R<7,A>(); NEXT; }
1507  case 0xc6: { II ii = set_N_xhl<0>(); NEXT; }
1508  case 0xce: { II ii = set_N_xhl<1>(); NEXT; }
1509  case 0xd6: { II ii = set_N_xhl<2>(); NEXT; }
1510  case 0xde: { II ii = set_N_xhl<3>(); NEXT; }
1511  case 0xe6: { II ii = set_N_xhl<4>(); NEXT; }
1512  case 0xee: { II ii = set_N_xhl<5>(); NEXT; }
1513  case 0xf6: { II ii = set_N_xhl<6>(); NEXT; }
1514  case 0xfe: { II ii = set_N_xhl<7>(); NEXT; }
1515  default: UNREACHABLE; return;
1516  }
1517 }
1518 CASE(ED) {
1519  setPC(getPC() + 1); // M1 cycle at this point
1520  byte ed_opcode = RDMEM_OPCODE<0>(T::CC_PREFIX);
1521  incR(1);
1522  switch (ed_opcode) {
1523  case 0x00: case 0x01: case 0x02: case 0x03:
1524  case 0x04: case 0x05: case 0x06: case 0x07:
1525  case 0x08: case 0x09: case 0x0a: case 0x0b:
1526  case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1527  case 0x10: case 0x11: case 0x12: case 0x13:
1528  case 0x14: case 0x15: case 0x16: case 0x17:
1529  case 0x18: case 0x19: case 0x1a: case 0x1b:
1530  case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1531  case 0x20: case 0x21: case 0x22: case 0x23:
1532  case 0x24: case 0x25: case 0x26: case 0x27:
1533  case 0x28: case 0x29: case 0x2a: case 0x2b:
1534  case 0x2c: case 0x2d: case 0x2e: case 0x2f:
1535  case 0x30: case 0x31: case 0x32: case 0x33:
1536  case 0x34: case 0x35: case 0x36: case 0x37:
1537  case 0x38: case 0x39: case 0x3a: case 0x3b:
1538  case 0x3c: case 0x3d: case 0x3e: case 0x3f:
1539 
1540  case 0x77: case 0x7f:
1541 
1542  case 0x80: case 0x81: case 0x82: case 0x83:
1543  case 0x84: case 0x85: case 0x86: case 0x87:
1544  case 0x88: case 0x89: case 0x8a: case 0x8b:
1545  case 0x8c: case 0x8d: case 0x8e: case 0x8f:
1546  case 0x90: case 0x91: case 0x92: case 0x93:
1547  case 0x94: case 0x95: case 0x96: case 0x97:
1548  case 0x98: case 0x99: case 0x9a: case 0x9b:
1549  case 0x9c: case 0x9d: case 0x9e: case 0x9f:
1550  case 0xa4: case 0xa5: case 0xa6: case 0xa7:
1551  case 0xac: case 0xad: case 0xae: case 0xaf:
1552  case 0xb4: case 0xb5: case 0xb6: case 0xb7:
1553  case 0xbc: case 0xbd: case 0xbe: case 0xbf:
1554 
1555  case 0xc0: case 0xc2:
1556  case 0xc4: case 0xc5: case 0xc6: case 0xc7:
1557  case 0xc8: case 0xca: case 0xcb:
1558  case 0xcc: case 0xcd: case 0xce: case 0xcf:
1559  case 0xd0: case 0xd2: case 0xd3:
1560  case 0xd4: case 0xd5: case 0xd6: case 0xd7:
1561  case 0xd8: case 0xda: case 0xdb:
1562  case 0xdc: case 0xdd: case 0xde: case 0xdf:
1563  case 0xe0: case 0xe1: case 0xe2: case 0xe3:
1564  case 0xe4: case 0xe5: case 0xe6: case 0xe7:
1565  case 0xe8: case 0xe9: case 0xea: case 0xeb:
1566  case 0xec: case 0xed: case 0xee: case 0xef:
1567  case 0xf0: case 0xf1: case 0xf2:
1568  case 0xf4: case 0xf5: case 0xf6: case 0xf7:
1569  case 0xf8: case 0xf9: case 0xfa: case 0xfb:
1570  case 0xfc: case 0xfd: case 0xfe: case 0xff:
1571  { II ii = nop(); NEXT; }
1572 
1573  case 0x40: { II ii = in_R_c<B>(); NEXT; }
1574  case 0x48: { II ii = in_R_c<C>(); NEXT; }
1575  case 0x50: { II ii = in_R_c<D>(); NEXT; }
1576  case 0x58: { II ii = in_R_c<E>(); NEXT; }
1577  case 0x60: { II ii = in_R_c<H>(); NEXT; }
1578  case 0x68: { II ii = in_R_c<L>(); NEXT; }
1579  case 0x70: { II ii = in_R_c<DUMMY>(); NEXT; }
1580  case 0x78: { II ii = in_R_c<A>(); NEXT; }
1581 
1582  case 0x41: { II ii = out_c_R<B>(); NEXT; }
1583  case 0x49: { II ii = out_c_R<C>(); NEXT; }
1584  case 0x51: { II ii = out_c_R<D>(); NEXT; }
1585  case 0x59: { II ii = out_c_R<E>(); NEXT; }
1586  case 0x61: { II ii = out_c_R<H>(); NEXT; }
1587  case 0x69: { II ii = out_c_R<L>(); NEXT; }
1588  case 0x71: { II ii = out_c_0(); NEXT; }
1589  case 0x79: { II ii = out_c_R<A>(); NEXT; }
1590 
1591  case 0x42: { II ii = sbc_hl_SS<BC>(); NEXT; }
1592  case 0x52: { II ii = sbc_hl_SS<DE>(); NEXT; }
1593  case 0x62: { II ii = sbc_hl_hl (); NEXT; }
1594  case 0x72: { II ii = sbc_hl_SS<SP>(); NEXT; }
1595 
1596  case 0x4a: { II ii = adc_hl_SS<BC>(); NEXT; }
1597  case 0x5a: { II ii = adc_hl_SS<DE>(); NEXT; }
1598  case 0x6a: { II ii = adc_hl_hl (); NEXT; }
1599  case 0x7a: { II ii = adc_hl_SS<SP>(); NEXT; }
1600 
1601  case 0x43: { II ii = ld_xword_SS_ED<BC>(); NEXT; }
1602  case 0x53: { II ii = ld_xword_SS_ED<DE>(); NEXT; }
1603  case 0x63: { II ii = ld_xword_SS_ED<HL>(); NEXT; }
1604  case 0x73: { II ii = ld_xword_SS_ED<SP>(); NEXT; }
1605 
1606  case 0x4b: { II ii = ld_SS_xword_ED<BC>(); NEXT; }
1607  case 0x5b: { II ii = ld_SS_xword_ED<DE>(); NEXT; }
1608  case 0x6b: { II ii = ld_SS_xword_ED<HL>(); NEXT; }
1609  case 0x7b: { II ii = ld_SS_xword_ED<SP>(); NEXT; }
1610 
1611  case 0x47: { II ii = ld_i_a(); NEXT; }
1612  case 0x4f: { II ii = ld_r_a(); NEXT; }
1613  case 0x57: { II ii = ld_a_IR<REG_I>(); if (T::isR800()) { NEXT; } else { NEXT_STOP; }}
1614  case 0x5f: { II ii = ld_a_IR<REG_R>(); if (T::isR800()) { NEXT; } else { NEXT_STOP; }}
1615 
1616  case 0x67: { II ii = rrd(); NEXT; }
1617  case 0x6f: { II ii = rld(); NEXT; }
1618 
1619  case 0x45: case 0x4d: case 0x55: case 0x5d:
1620  case 0x65: case 0x6d: case 0x75: case 0x7d:
1621  { II ii = retn(); NEXT_STOP; }
1622  case 0x46: case 0x4e: case 0x66: case 0x6e:
1623  { II ii = im_N<0>(); NEXT; }
1624  case 0x56: case 0x76:
1625  { II ii = im_N<1>(); NEXT; }
1626  case 0x5e: case 0x7e:
1627  { II ii = im_N<2>(); NEXT; }
1628  case 0x44: case 0x4c: case 0x54: case 0x5c:
1629  case 0x64: case 0x6c: case 0x74: case 0x7c:
1630  { II ii = neg(); NEXT; }
1631 
1632  case 0xa0: { II ii = ldi(); NEXT; }
1633  case 0xa1: { II ii = cpi(); NEXT; }
1634  case 0xa2: { II ii = ini(); NEXT; }
1635  case 0xa3: { II ii = outi(); NEXT; }
1636  case 0xa8: { II ii = ldd(); NEXT; }
1637  case 0xa9: { II ii = cpd(); NEXT; }
1638  case 0xaa: { II ii = ind(); NEXT; }
1639  case 0xab: { II ii = outd(); NEXT; }
1640  case 0xb0: { II ii = ldir(); NEXT; }
1641  case 0xb1: { II ii = cpir(); NEXT; }
1642  case 0xb2: { II ii = inir(); NEXT; }
1643  case 0xb3: { II ii = otir(); NEXT; }
1644  case 0xb8: { II ii = lddr(); NEXT; }
1645  case 0xb9: { II ii = cpdr(); NEXT; }
1646  case 0xba: { II ii = indr(); NEXT; }
1647  case 0xbb: { II ii = otdr(); NEXT; }
1648 
1649  case 0xc1: { II ii = T::isR800() ? mulub_a_R<B>() : nop(); NEXT; }
1650  case 0xc9: { II ii = T::isR800() ? mulub_a_R<C>() : nop(); NEXT; }
1651  case 0xd1: { II ii = T::isR800() ? mulub_a_R<D>() : nop(); NEXT; }
1652  case 0xd9: { II ii = T::isR800() ? mulub_a_R<E>() : nop(); NEXT; }
1653  case 0xc3: { II ii = T::isR800() ? muluw_hl_SS<BC>() : nop(); NEXT; }
1654  case 0xf3: { II ii = T::isR800() ? muluw_hl_SS<SP>() : nop(); NEXT; }
1655  default: UNREACHABLE; return;
1656  }
1657 }
1658 opDD_2:
1659 CASE(DD) {
1660  setPC(getPC() + 1); // M1 cycle at this point
1661  byte opcodeDD = RDMEM_OPCODE<0>(T::CC_DD + T::CC_MAIN);
1662  incR(1);
1663  switch (opcodeDD) {
1664  case 0x00: // nop();
1665  case 0x01: // ld_bc_word();
1666  case 0x02: // ld_xbc_a();
1667  case 0x03: // inc_bc();
1668  case 0x04: // inc_b();
1669  case 0x05: // dec_b();
1670  case 0x06: // ld_b_byte();
1671  case 0x07: // rlca();
1672  case 0x08: // ex_af_af();
1673  case 0x0a: // ld_a_xbc();
1674  case 0x0b: // dec_bc();
1675  case 0x0c: // inc_c();
1676  case 0x0d: // dec_c();
1677  case 0x0e: // ld_c_byte();
1678  case 0x0f: // rrca();
1679  case 0x10: // djnz();
1680  case 0x11: // ld_de_word();
1681  case 0x12: // ld_xde_a();
1682  case 0x13: // inc_de();
1683  case 0x14: // inc_d();
1684  case 0x15: // dec_d();
1685  case 0x16: // ld_d_byte();
1686  case 0x17: // rla();
1687  case 0x18: // jr();
1688  case 0x1a: // ld_a_xde();
1689  case 0x1b: // dec_de();
1690  case 0x1c: // inc_e();
1691  case 0x1d: // dec_e();
1692  case 0x1e: // ld_e_byte();
1693  case 0x1f: // rra();
1694  case 0x20: // jr_nz();
1695  case 0x27: // daa();
1696  case 0x28: // jr_z();
1697  case 0x2f: // cpl();
1698  case 0x30: // jr_nc();
1699  case 0x31: // ld_sp_word();
1700  case 0x32: // ld_xbyte_a();
1701  case 0x33: // inc_sp();
1702  case 0x37: // scf();
1703  case 0x38: // jr_c();
1704  case 0x3a: // ld_a_xbyte();
1705  case 0x3b: // dec_sp();
1706  case 0x3c: // inc_a();
1707  case 0x3d: // dec_a();
1708  case 0x3e: // ld_a_byte();
1709  case 0x3f: // ccf();
1710 
1711  case 0x40: // ld_b_b();
1712  case 0x41: // ld_b_c();
1713  case 0x42: // ld_b_d();
1714  case 0x43: // ld_b_e();
1715  case 0x47: // ld_b_a();
1716  case 0x48: // ld_c_b();
1717  case 0x49: // ld_c_c();
1718  case 0x4a: // ld_c_d();
1719  case 0x4b: // ld_c_e();
1720  case 0x4f: // ld_c_a();
1721  case 0x50: // ld_d_b();
1722  case 0x51: // ld_d_c();
1723  case 0x52: // ld_d_d();
1724  case 0x53: // ld_d_e();
1725  case 0x57: // ld_d_a();
1726  case 0x58: // ld_e_b();
1727  case 0x59: // ld_e_c();
1728  case 0x5a: // ld_e_d();
1729  case 0x5b: // ld_e_e();
1730  case 0x5f: // ld_e_a();
1731  case 0x64: // ld_ixh_ixh(); == nop
1732  case 0x6d: // ld_ixl_ixl(); == nop
1733  case 0x76: // halt();
1734  case 0x78: // ld_a_b();
1735  case 0x79: // ld_a_c();
1736  case 0x7a: // ld_a_d();
1737  case 0x7b: // ld_a_e();
1738  case 0x7f: // ld_a_a();
1739 
1740  case 0x80: // add_a_b();
1741  case 0x81: // add_a_c();
1742  case 0x82: // add_a_d();
1743  case 0x83: // add_a_e();
1744  case 0x87: // add_a_a();
1745  case 0x88: // adc_a_b();
1746  case 0x89: // adc_a_c();
1747  case 0x8a: // adc_a_d();
1748  case 0x8b: // adc_a_e();
1749  case 0x8f: // adc_a_a();
1750  case 0x90: // sub_b();
1751  case 0x91: // sub_c();
1752  case 0x92: // sub_d();
1753  case 0x93: // sub_e();
1754  case 0x97: // sub_a();
1755  case 0x98: // sbc_a_b();
1756  case 0x99: // sbc_a_c();
1757  case 0x9a: // sbc_a_d();
1758  case 0x9b: // sbc_a_e();
1759  case 0x9f: // sbc_a_a();
1760  case 0xa0: // and_b();
1761  case 0xa1: // and_c();
1762  case 0xa2: // and_d();
1763  case 0xa3: // and_e();
1764  case 0xa7: // and_a();
1765  case 0xa8: // xor_b();
1766  case 0xa9: // xor_c();
1767  case 0xaa: // xor_d();
1768  case 0xab: // xor_e();
1769  case 0xaf: // xor_a();
1770  case 0xb0: // or_b();
1771  case 0xb1: // or_c();
1772  case 0xb2: // or_d();
1773  case 0xb3: // or_e();
1774  case 0xb7: // or_a();
1775  case 0xb8: // cp_b();
1776  case 0xb9: // cp_c();
1777  case 0xba: // cp_d();
1778  case 0xbb: // cp_e();
1779  case 0xbf: // cp_a();
1780 
1781  case 0xc0: // ret_nz();
1782  case 0xc1: // pop_bc();
1783  case 0xc2: // jp_nz();
1784  case 0xc3: // jp();
1785  case 0xc4: // call_nz();
1786  case 0xc5: // push_bc();
1787  case 0xc6: // add_a_byte();
1788  case 0xc7: // rst_00();
1789  case 0xc8: // ret_z();
1790  case 0xc9: // ret();
1791  case 0xca: // jp_z();
1792  case 0xcc: // call_z();
1793  case 0xcd: // call();
1794  case 0xce: // adc_a_byte();
1795  case 0xcf: // rst_08();
1796  case 0xd0: // ret_nc();
1797  case 0xd1: // pop_de();
1798  case 0xd2: // jp_nc();
1799  case 0xd3: // out_byte_a();
1800  case 0xd4: // call_nc();
1801  case 0xd5: // push_de();
1802  case 0xd6: // sub_byte();
1803  case 0xd7: // rst_10();
1804  case 0xd8: // ret_c();
1805  case 0xd9: // exx();
1806  case 0xda: // jp_c();
1807  case 0xdb: // in_a_byte();
1808  case 0xdc: // call_c();
1809  case 0xde: // sbc_a_byte();
1810  case 0xdf: // rst_18();
1811  case 0xe0: // ret_po();
1812  case 0xe2: // jp_po();
1813  case 0xe4: // call_po();
1814  case 0xe6: // and_byte();
1815  case 0xe7: // rst_20();
1816  case 0xe8: // ret_pe();
1817  case 0xea: // jp_pe();
1818  case 0xeb: // ex_de_hl();
1819  case 0xec: // call_pe();
1820  case 0xed: // ed();
1821  case 0xee: // xor_byte();
1822  case 0xef: // rst_28();
1823  case 0xf0: // ret_p();
1824  case 0xf1: // pop_af();
1825  case 0xf2: // jp_p();
1826  case 0xf3: // di();
1827  case 0xf4: // call_p();
1828  case 0xf5: // push_af();
1829  case 0xf6: // or_byte();
1830  case 0xf7: // rst_30();
1831  case 0xf8: // ret_m();
1832  case 0xfa: // jp_m();
1833  case 0xfb: // ei();
1834  case 0xfc: // call_m();
1835  case 0xfe: // cp_byte();
1836  case 0xff: // rst_38();
1837  if (T::isR800()) {
1838  II ii = nop();
1839  ii.cycles += T::CC_DD;
1840  NEXT;
1841  } else {
1842  T::add(T::CC_DD);
1843  #ifdef USE_COMPUTED_GOTO
1844  goto *(opcodeTable[opcodeDD]);
1845  #else
1846  opcodeMain = opcodeDD;
1847  goto switchopcode;
1848  #endif
1849  }
1850 
1851  case 0x09: { II ii = add_SS_TT<IX,BC,T::CC_DD>(); NEXT; }
1852  case 0x19: { II ii = add_SS_TT<IX,DE,T::CC_DD>(); NEXT; }
1853  case 0x29: { II ii = add_SS_SS<IX ,T::CC_DD>(); NEXT; }
1854  case 0x39: { II ii = add_SS_TT<IX,SP,T::CC_DD>(); NEXT; }
1855  case 0x21: { II ii = ld_SS_word<IX,T::CC_DD>(); NEXT; }
1856  case 0x22: { II ii = ld_xword_SS<IX,T::CC_DD>(); NEXT; }
1857  case 0x2a: { II ii = ld_SS_xword<IX,T::CC_DD>(); NEXT; }
1858  case 0x23: { II ii = inc_SS<IX,T::CC_DD>(); NEXT; }
1859  case 0x2b: { II ii = dec_SS<IX,T::CC_DD>(); NEXT; }
1860  case 0x24: { II ii = inc_R<IXH,T::CC_DD>(); NEXT; }
1861  case 0x2c: { II ii = inc_R<IXL,T::CC_DD>(); NEXT; }
1862  case 0x25: { II ii = dec_R<IXH,T::CC_DD>(); NEXT; }
1863  case 0x2d: { II ii = dec_R<IXL,T::CC_DD>(); NEXT; }
1864  case 0x26: { II ii = ld_R_byte<IXH,T::CC_DD>(); NEXT; }
1865  case 0x2e: { II ii = ld_R_byte<IXL,T::CC_DD>(); NEXT; }
1866  case 0x34: { II ii = inc_xix<IX>(); NEXT; }
1867  case 0x35: { II ii = dec_xix<IX>(); NEXT; }
1868  case 0x36: { II ii = ld_xix_byte<IX>(); NEXT; }
1869 
1870  case 0x44: { II ii = ld_R_R<B,IXH,T::CC_DD>(); NEXT; }
1871  case 0x45: { II ii = ld_R_R<B,IXL,T::CC_DD>(); NEXT; }
1872  case 0x4c: { II ii = ld_R_R<C,IXH,T::CC_DD>(); NEXT; }
1873  case 0x4d: { II ii = ld_R_R<C,IXL,T::CC_DD>(); NEXT; }
1874  case 0x54: { II ii = ld_R_R<D,IXH,T::CC_DD>(); NEXT; }
1875  case 0x55: { II ii = ld_R_R<D,IXL,T::CC_DD>(); NEXT; }
1876  case 0x5c: { II ii = ld_R_R<E,IXH,T::CC_DD>(); NEXT; }
1877  case 0x5d: { II ii = ld_R_R<E,IXL,T::CC_DD>(); NEXT; }
1878  case 0x7c: { II ii = ld_R_R<A,IXH,T::CC_DD>(); NEXT; }
1879  case 0x7d: { II ii = ld_R_R<A,IXL,T::CC_DD>(); NEXT; }
1880  case 0x60: { II ii = ld_R_R<IXH,B,T::CC_DD>(); NEXT; }
1881  case 0x61: { II ii = ld_R_R<IXH,C,T::CC_DD>(); NEXT; }
1882  case 0x62: { II ii = ld_R_R<IXH,D,T::CC_DD>(); NEXT; }
1883  case 0x63: { II ii = ld_R_R<IXH,E,T::CC_DD>(); NEXT; }
1884  case 0x65: { II ii = ld_R_R<IXH,IXL,T::CC_DD>(); NEXT; }
1885  case 0x67: { II ii = ld_R_R<IXH,A,T::CC_DD>(); NEXT; }
1886  case 0x68: { II ii = ld_R_R<IXL,B,T::CC_DD>(); NEXT; }
1887  case 0x69: { II ii = ld_R_R<IXL,C,T::CC_DD>(); NEXT; }
1888  case 0x6a: { II ii = ld_R_R<IXL,D,T::CC_DD>(); NEXT; }
1889  case 0x6b: { II ii = ld_R_R<IXL,E,T::CC_DD>(); NEXT; }
1890  case 0x6c: { II ii = ld_R_R<IXL,IXH,T::CC_DD>(); NEXT; }
1891  case 0x6f: { II ii = ld_R_R<IXL,A,T::CC_DD>(); NEXT; }
1892  case 0x70: { II ii = ld_xix_R<IX,B>(); NEXT; }
1893  case 0x71: { II ii = ld_xix_R<IX,C>(); NEXT; }
1894  case 0x72: { II ii = ld_xix_R<IX,D>(); NEXT; }
1895  case 0x73: { II ii = ld_xix_R<IX,E>(); NEXT; }
1896  case 0x74: { II ii = ld_xix_R<IX,H>(); NEXT; }
1897  case 0x75: { II ii = ld_xix_R<IX,L>(); NEXT; }
1898  case 0x77: { II ii = ld_xix_R<IX,A>(); NEXT; }
1899  case 0x46: { II ii = ld_R_xix<B,IX>(); NEXT; }
1900  case 0x4e: { II ii = ld_R_xix<C,IX>(); NEXT; }
1901  case 0x56: { II ii = ld_R_xix<D,IX>(); NEXT; }
1902  case 0x5e: { II ii = ld_R_xix<E,IX>(); NEXT; }
1903  case 0x66: { II ii = ld_R_xix<H,IX>(); NEXT; }
1904  case 0x6e: { II ii = ld_R_xix<L,IX>(); NEXT; }
1905  case 0x7e: { II ii = ld_R_xix<A,IX>(); NEXT; }
1906 
1907  case 0x84: { II ii = add_a_R<IXH,T::CC_DD>(); NEXT; }
1908  case 0x85: { II ii = add_a_R<IXL,T::CC_DD>(); NEXT; }
1909  case 0x86: { II ii = add_a_xix<IX>(); NEXT; }
1910  case 0x8c: { II ii = adc_a_R<IXH,T::CC_DD>(); NEXT; }
1911  case 0x8d: { II ii = adc_a_R<IXL,T::CC_DD>(); NEXT; }
1912  case 0x8e: { II ii = adc_a_xix<IX>(); NEXT; }
1913  case 0x94: { II ii = sub_R<IXH,T::CC_DD>(); NEXT; }
1914  case 0x95: { II ii = sub_R<IXL,T::CC_DD>(); NEXT; }
1915  case 0x96: { II ii = sub_xix<IX>(); NEXT; }
1916  case 0x9c: { II ii = sbc_a_R<IXH,T::CC_DD>(); NEXT; }
1917  case 0x9d: { II ii = sbc_a_R<IXL,T::CC_DD>(); NEXT; }
1918  case 0x9e: { II ii = sbc_a_xix<IX>(); NEXT; }
1919  case 0xa4: { II ii = and_R<IXH,T::CC_DD>(); NEXT; }
1920  case 0xa5: { II ii = and_R<IXL,T::CC_DD>(); NEXT; }
1921  case 0xa6: { II ii = and_xix<IX>(); NEXT; }
1922  case 0xac: { II ii = xor_R<IXH,T::CC_DD>(); NEXT; }
1923  case 0xad: { II ii = xor_R<IXL,T::CC_DD>(); NEXT; }
1924  case 0xae: { II ii = xor_xix<IX>(); NEXT; }
1925  case 0xb4: { II ii = or_R<IXH,T::CC_DD>(); NEXT; }
1926  case 0xb5: { II ii = or_R<IXL,T::CC_DD>(); NEXT; }
1927  case 0xb6: { II ii = or_xix<IX>(); NEXT; }
1928  case 0xbc: { II ii = cp_R<IXH,T::CC_DD>(); NEXT; }
1929  case 0xbd: { II ii = cp_R<IXL,T::CC_DD>(); NEXT; }
1930  case 0xbe: { II ii = cp_xix<IX>(); NEXT; }
1931 
1932  case 0xe1: { II ii = pop_SS <IX,T::CC_DD>(); NEXT; }
1933  case 0xe5: { II ii = push_SS<IX,T::CC_DD>(); NEXT; }
1934  case 0xe3: { II ii = ex_xsp_SS<IX,T::CC_DD>(); NEXT; }
1935  case 0xe9: { II ii = jp_SS<IX,T::CC_DD>(); NEXT; }
1936  case 0xf9: { II ii = ld_sp_SS<IX,T::CC_DD>(); NEXT; }
1937  case 0xcb: ixy = getIX(); goto xx_cb;
1938  case 0xdd: T::add(T::CC_DD); goto opDD_2;
1939  case 0xfd: T::add(T::CC_DD); goto opFD_2;
1940  default: UNREACHABLE; return;
1941  }
1942 }
1943 opFD_2:
1944 CASE(FD) {
1945  setPC(getPC() + 1); // M1 cycle at this point
1946  byte opcodeFD = RDMEM_OPCODE<0>(T::CC_DD + T::CC_MAIN);
1947  incR(1);
1948  switch (opcodeFD) {
1949  case 0x00: // nop();
1950  case 0x01: // ld_bc_word();
1951  case 0x02: // ld_xbc_a();
1952  case 0x03: // inc_bc();
1953  case 0x04: // inc_b();
1954  case 0x05: // dec_b();
1955  case 0x06: // ld_b_byte();
1956  case 0x07: // rlca();
1957  case 0x08: // ex_af_af();
1958  case 0x0a: // ld_a_xbc();
1959  case 0x0b: // dec_bc();
1960  case 0x0c: // inc_c();
1961  case 0x0d: // dec_c();
1962  case 0x0e: // ld_c_byte();
1963  case 0x0f: // rrca();
1964  case 0x10: // djnz();
1965  case 0x11: // ld_de_word();
1966  case 0x12: // ld_xde_a();
1967  case 0x13: // inc_de();
1968  case 0x14: // inc_d();
1969  case 0x15: // dec_d();
1970  case 0x16: // ld_d_byte();
1971  case 0x17: // rla();
1972  case 0x18: // jr();
1973  case 0x1a: // ld_a_xde();
1974  case 0x1b: // dec_de();
1975  case 0x1c: // inc_e();
1976  case 0x1d: // dec_e();
1977  case 0x1e: // ld_e_byte();
1978  case 0x1f: // rra();
1979  case 0x20: // jr_nz();
1980  case 0x27: // daa();
1981  case 0x28: // jr_z();
1982  case 0x2f: // cpl();
1983  case 0x30: // jr_nc();
1984  case 0x31: // ld_sp_word();
1985  case 0x32: // ld_xbyte_a();
1986  case 0x33: // inc_sp();
1987  case 0x37: // scf();
1988  case 0x38: // jr_c();
1989  case 0x3a: // ld_a_xbyte();
1990  case 0x3b: // dec_sp();
1991  case 0x3c: // inc_a();
1992  case 0x3d: // dec_a();
1993  case 0x3e: // ld_a_byte();
1994  case 0x3f: // ccf();
1995 
1996  case 0x40: // ld_b_b();
1997  case 0x41: // ld_b_c();
1998  case 0x42: // ld_b_d();
1999  case 0x43: // ld_b_e();
2000  case 0x47: // ld_b_a();
2001  case 0x48: // ld_c_b();
2002  case 0x49: // ld_c_c();
2003  case 0x4a: // ld_c_d();
2004  case 0x4b: // ld_c_e();
2005  case 0x4f: // ld_c_a();
2006  case 0x50: // ld_d_b();
2007  case 0x51: // ld_d_c();
2008  case 0x52: // ld_d_d();
2009  case 0x53: // ld_d_e();
2010  case 0x57: // ld_d_a();
2011  case 0x58: // ld_e_b();
2012  case 0x59: // ld_e_c();
2013  case 0x5a: // ld_e_d();
2014  case 0x5b: // ld_e_e();
2015  case 0x5f: // ld_e_a();
2016  case 0x64: // ld_ixh_ixh(); == nop
2017  case 0x6d: // ld_ixl_ixl(); == nop
2018  case 0x76: // halt();
2019  case 0x78: // ld_a_b();
2020  case 0x79: // ld_a_c();
2021  case 0x7a: // ld_a_d();
2022  case 0x7b: // ld_a_e();
2023  case 0x7f: // ld_a_a();
2024 
2025  case 0x80: // add_a_b();
2026  case 0x81: // add_a_c();
2027  case 0x82: // add_a_d();
2028  case 0x83: // add_a_e();
2029  case 0x87: // add_a_a();
2030  case 0x88: // adc_a_b();
2031  case 0x89: // adc_a_c();
2032  case 0x8a: // adc_a_d();
2033  case 0x8b: // adc_a_e();
2034  case 0x8f: // adc_a_a();
2035  case 0x90: // sub_b();
2036  case 0x91: // sub_c();
2037  case 0x92: // sub_d();
2038  case 0x93: // sub_e();
2039  case 0x97: // sub_a();
2040  case 0x98: // sbc_a_b();
2041  case 0x99: // sbc_a_c();
2042  case 0x9a: // sbc_a_d();
2043  case 0x9b: // sbc_a_e();
2044  case 0x9f: // sbc_a_a();
2045  case 0xa0: // and_b();
2046  case 0xa1: // and_c();
2047  case 0xa2: // and_d();
2048  case 0xa3: // and_e();
2049  case 0xa7: // and_a();
2050  case 0xa8: // xor_b();
2051  case 0xa9: // xor_c();
2052  case 0xaa: // xor_d();
2053  case 0xab: // xor_e();
2054  case 0xaf: // xor_a();
2055  case 0xb0: // or_b();
2056  case 0xb1: // or_c();
2057  case 0xb2: // or_d();
2058  case 0xb3: // or_e();
2059  case 0xb7: // or_a();
2060  case 0xb8: // cp_b();
2061  case 0xb9: // cp_c();
2062  case 0xba: // cp_d();
2063  case 0xbb: // cp_e();
2064  case 0xbf: // cp_a();
2065 
2066  case 0xc0: // ret_nz();
2067  case 0xc1: // pop_bc();
2068  case 0xc2: // jp_nz();
2069  case 0xc3: // jp();
2070  case 0xc4: // call_nz();
2071  case 0xc5: // push_bc();
2072  case 0xc6: // add_a_byte();
2073  case 0xc7: // rst_00();
2074  case 0xc8: // ret_z();
2075  case 0xc9: // ret();
2076  case 0xca: // jp_z();
2077  case 0xcc: // call_z();
2078  case 0xcd: // call();
2079  case 0xce: // adc_a_byte();
2080  case 0xcf: // rst_08();
2081  case 0xd0: // ret_nc();
2082  case 0xd1: // pop_de();
2083  case 0xd2: // jp_nc();
2084  case 0xd3: // out_byte_a();
2085  case 0xd4: // call_nc();
2086  case 0xd5: // push_de();
2087  case 0xd6: // sub_byte();
2088  case 0xd7: // rst_10();
2089  case 0xd8: // ret_c();
2090  case 0xd9: // exx();
2091  case 0xda: // jp_c();
2092  case 0xdb: // in_a_byte();
2093  case 0xdc: // call_c();
2094  case 0xde: // sbc_a_byte();
2095  case 0xdf: // rst_18();
2096  case 0xe0: // ret_po();
2097  case 0xe2: // jp_po();
2098  case 0xe4: // call_po();
2099  case 0xe6: // and_byte();
2100  case 0xe7: // rst_20();
2101  case 0xe8: // ret_pe();
2102  case 0xea: // jp_pe();
2103  case 0xeb: // ex_de_hl();
2104  case 0xec: // call_pe();
2105  case 0xed: // ed();
2106  case 0xee: // xor_byte();
2107  case 0xef: // rst_28();
2108  case 0xf0: // ret_p();
2109  case 0xf1: // pop_af();
2110  case 0xf2: // jp_p();
2111  case 0xf3: // di();
2112  case 0xf4: // call_p();
2113  case 0xf5: // push_af();
2114  case 0xf6: // or_byte();
2115  case 0xf7: // rst_30();
2116  case 0xf8: // ret_m();
2117  case 0xfa: // jp_m();
2118  case 0xfb: // ei();
2119  case 0xfc: // call_m();
2120  case 0xfe: // cp_byte();
2121  case 0xff: // rst_38();
2122  if (T::isR800()) {
2123  II ii = nop();
2124  ii.cycles += T::CC_DD;
2125  NEXT;
2126  } else {
2127  T::add(T::CC_DD);
2128  #ifdef USE_COMPUTED_GOTO
2129  goto *(opcodeTable[opcodeFD]);
2130  #else
2131  opcodeMain = opcodeFD;
2132  goto switchopcode;
2133  #endif
2134  }
2135 
2136  case 0x09: { II ii = add_SS_TT<IY,BC,T::CC_DD>(); NEXT; }
2137  case 0x19: { II ii = add_SS_TT<IY,DE,T::CC_DD>(); NEXT; }
2138  case 0x29: { II ii = add_SS_SS<IY ,T::CC_DD>(); NEXT; }
2139  case 0x39: { II ii = add_SS_TT<IY,SP,T::CC_DD>(); NEXT; }
2140  case 0x21: { II ii = ld_SS_word<IY,T::CC_DD>(); NEXT; }
2141  case 0x22: { II ii = ld_xword_SS<IY,T::CC_DD>(); NEXT; }
2142  case 0x2a: { II ii = ld_SS_xword<IY,T::CC_DD>(); NEXT; }
2143  case 0x23: { II ii = inc_SS<IY,T::CC_DD>(); NEXT; }
2144  case 0x2b: { II ii = dec_SS<IY,T::CC_DD>(); NEXT; }
2145  case 0x24: { II ii = inc_R<IYH,T::CC_DD>(); NEXT; }
2146  case 0x2c: { II ii = inc_R<IYL,T::CC_DD>(); NEXT; }
2147  case 0x25: { II ii = dec_R<IYH,T::CC_DD>(); NEXT; }
2148  case 0x2d: { II ii = dec_R<IYL,T::CC_DD>(); NEXT; }
2149  case 0x26: { II ii = ld_R_byte<IYH,T::CC_DD>(); NEXT; }
2150  case 0x2e: { II ii = ld_R_byte<IYL,T::CC_DD>(); NEXT; }
2151  case 0x34: { II ii = inc_xix<IY>(); NEXT; }
2152  case 0x35: { II ii = dec_xix<IY>(); NEXT; }
2153  case 0x36: { II ii = ld_xix_byte<IY>(); NEXT; }
2154 
2155  case 0x44: { II ii = ld_R_R<B,IYH,T::CC_DD>(); NEXT; }
2156  case 0x45: { II ii = ld_R_R<B,IYL,T::CC_DD>(); NEXT; }
2157  case 0x4c: { II ii = ld_R_R<C,IYH,T::CC_DD>(); NEXT; }
2158  case 0x4d: { II ii = ld_R_R<C,IYL,T::CC_DD>(); NEXT; }
2159  case 0x54: { II ii = ld_R_R<D,IYH,T::CC_DD>(); NEXT; }
2160  case 0x55: { II ii = ld_R_R<D,IYL,T::CC_DD>(); NEXT; }
2161  case 0x5c: { II ii = ld_R_R<E,IYH,T::CC_DD>(); NEXT; }
2162  case 0x5d: { II ii = ld_R_R<E,IYL,T::CC_DD>(); NEXT; }
2163  case 0x7c: { II ii = ld_R_R<A,IYH,T::CC_DD>(); NEXT; }
2164  case 0x7d: { II ii = ld_R_R<A,IYL,T::CC_DD>(); NEXT; }
2165  case 0x60: { II ii = ld_R_R<IYH,B,T::CC_DD>(); NEXT; }
2166  case 0x61: { II ii = ld_R_R<IYH,C,T::CC_DD>(); NEXT; }
2167  case 0x62: { II ii = ld_R_R<IYH,D,T::CC_DD>(); NEXT; }
2168  case 0x63: { II ii = ld_R_R<IYH,E,T::CC_DD>(); NEXT; }
2169  case 0x65: { II ii = ld_R_R<IYH,IYL,T::CC_DD>(); NEXT; }
2170  case 0x67: { II ii = ld_R_R<IYH,A,T::CC_DD>(); NEXT; }
2171  case 0x68: { II ii = ld_R_R<IYL,B,T::CC_DD>(); NEXT; }
2172  case 0x69: { II ii = ld_R_R<IYL,C,T::CC_DD>(); NEXT; }
2173  case 0x6a: { II ii = ld_R_R<IYL,D,T::CC_DD>(); NEXT; }
2174  case 0x6b: { II ii = ld_R_R<IYL,E,T::CC_DD>(); NEXT; }
2175  case 0x6c: { II ii = ld_R_R<IYL,IYH,T::CC_DD>(); NEXT; }
2176  case 0x6f: { II ii = ld_R_R<IYL,A,T::CC_DD>(); NEXT; }
2177  case 0x70: { II ii = ld_xix_R<IY,B>(); NEXT; }
2178  case 0x71: { II ii = ld_xix_R<IY,C>(); NEXT; }
2179  case 0x72: { II ii = ld_xix_R<IY,D>(); NEXT; }
2180  case 0x73: { II ii = ld_xix_R<IY,E>(); NEXT; }
2181  case 0x74: { II ii = ld_xix_R<IY,H>(); NEXT; }
2182  case 0x75: { II ii = ld_xix_R<IY,L>(); NEXT; }
2183  case 0x77: { II ii = ld_xix_R<IY,A>(); NEXT; }
2184  case 0x46: { II ii = ld_R_xix<B,IY>(); NEXT; }
2185  case 0x4e: { II ii = ld_R_xix<C,IY>(); NEXT; }
2186  case 0x56: { II ii = ld_R_xix<D,IY>(); NEXT; }
2187  case 0x5e: { II ii = ld_R_xix<E,IY>(); NEXT; }
2188  case 0x66: { II ii = ld_R_xix<H,IY>(); NEXT; }
2189  case 0x6e: { II ii = ld_R_xix<L,IY>(); NEXT; }
2190  case 0x7e: { II ii = ld_R_xix<A,IY>(); NEXT; }
2191 
2192  case 0x84: { II ii = add_a_R<IYH,T::CC_DD>(); NEXT; }
2193  case 0x85: { II ii = add_a_R<IYL,T::CC_DD>(); NEXT; }
2194  case 0x86: { II ii = add_a_xix<IY>(); NEXT; }
2195  case 0x8c: { II ii = adc_a_R<IYH,T::CC_DD>(); NEXT; }
2196  case 0x8d: { II ii = adc_a_R<IYL,T::CC_DD>(); NEXT; }
2197  case 0x8e: { II ii = adc_a_xix<IY>(); NEXT; }
2198  case 0x94: { II ii = sub_R<IYH,T::CC_DD>(); NEXT; }
2199  case 0x95: { II ii = sub_R<IYL,T::CC_DD>(); NEXT; }
2200  case 0x96: { II ii = sub_xix<IY>(); NEXT; }
2201  case 0x9c: { II ii = sbc_a_R<IYH,T::CC_DD>(); NEXT; }
2202  case 0x9d: { II ii = sbc_a_R<IYL,T::CC_DD>(); NEXT; }
2203  case 0x9e: { II ii = sbc_a_xix<IY>(); NEXT; }
2204  case 0xa4: { II ii = and_R<IYH,T::CC_DD>(); NEXT; }
2205  case 0xa5: { II ii = and_R<IYL,T::CC_DD>(); NEXT; }
2206  case 0xa6: { II ii = and_xix<IY>(); NEXT; }
2207  case 0xac: { II ii = xor_R<IYH,T::CC_DD>(); NEXT; }
2208  case 0xad: { II ii = xor_R<IYL,T::CC_DD>(); NEXT; }
2209  case 0xae: { II ii = xor_xix<IY>(); NEXT; }
2210  case 0xb4: { II ii = or_R<IYH,T::CC_DD>(); NEXT; }
2211  case 0xb5: { II ii = or_R<IYL,T::CC_DD>(); NEXT; }
2212  case 0xb6: { II ii = or_xix<IY>(); NEXT; }
2213  case 0xbc: { II ii = cp_R<IYH,T::CC_DD>(); NEXT; }
2214  case 0xbd: { II ii = cp_R<IYL,T::CC_DD>(); NEXT; }
2215  case 0xbe: { II ii = cp_xix<IY>(); NEXT; }
2216 
2217  case 0xe1: { II ii = pop_SS <IY,T::CC_DD>(); NEXT; }
2218  case 0xe5: { II ii = push_SS<IY,T::CC_DD>(); NEXT; }
2219  case 0xe3: { II ii = ex_xsp_SS<IY,T::CC_DD>(); NEXT; }
2220  case 0xe9: { II ii = jp_SS<IY,T::CC_DD>(); NEXT; }
2221  case 0xf9: { II ii = ld_sp_SS<IY,T::CC_DD>(); NEXT; }
2222  case 0xcb: ixy = getIY(); goto xx_cb;
2223  case 0xdd: T::add(T::CC_DD); goto opDD_2;
2224  case 0xfd: T::add(T::CC_DD); goto opFD_2;
2225  default: UNREACHABLE; return;
2226  }
2227 }
2228 #ifndef USE_COMPUTED_GOTO
2229  default: UNREACHABLE; return;
2230 }
2231 #endif
2232 
2233 xx_cb: {
2234  unsigned tmp = RD_WORD_PC<1>(T::CC_DD + T::CC_DD_CB);
2235  int8_t ofst = tmp & 0xFF;
2236  unsigned addr = (ixy + ofst) & 0xFFFF;
2237  byte xxcb_opcode = tmp >> 8;
2238  switch (xxcb_opcode) {
2239  case 0x00: { II ii = rlc_xix_R<B>(addr); NEXT; }
2240  case 0x01: { II ii = rlc_xix_R<C>(addr); NEXT; }
2241  case 0x02: { II ii = rlc_xix_R<D>(addr); NEXT; }
2242  case 0x03: { II ii = rlc_xix_R<E>(addr); NEXT; }
2243  case 0x04: { II ii = rlc_xix_R<H>(addr); NEXT; }
2244  case 0x05: { II ii = rlc_xix_R<L>(addr); NEXT; }
2245  case 0x06: { II ii = rlc_xix_R<DUMMY>(addr); NEXT; }
2246  case 0x07: { II ii = rlc_xix_R<A>(addr); NEXT; }
2247  case 0x08: { II ii = rrc_xix_R<B>(addr); NEXT; }
2248  case 0x09: { II ii = rrc_xix_R<C>(addr); NEXT; }
2249  case 0x0a: { II ii = rrc_xix_R<D>(addr); NEXT; }
2250  case 0x0b: { II ii = rrc_xix_R<E>(addr); NEXT; }
2251  case 0x0c: { II ii = rrc_xix_R<H>(addr); NEXT; }
2252  case 0x0d: { II ii = rrc_xix_R<L>(addr); NEXT; }
2253  case 0x0e: { II ii = rrc_xix_R<DUMMY>(addr); NEXT; }
2254  case 0x0f: { II ii = rrc_xix_R<A>(addr); NEXT; }
2255  case 0x10: { II ii = rl_xix_R<B>(addr); NEXT; }
2256  case 0x11: { II ii = rl_xix_R<C>(addr); NEXT; }
2257  case 0x12: { II ii = rl_xix_R<D>(addr); NEXT; }
2258  case 0x13: { II ii = rl_xix_R<E>(addr); NEXT; }
2259  case 0x14: { II ii = rl_xix_R<H>(addr); NEXT; }
2260  case 0x15: { II ii = rl_xix_R<L>(addr); NEXT; }
2261  case 0x16: { II ii = rl_xix_R<DUMMY>(addr); NEXT; }
2262  case 0x17: { II ii = rl_xix_R<A>(addr); NEXT; }
2263  case 0x18: { II ii = rr_xix_R<B>(addr); NEXT; }
2264  case 0x19: { II ii = rr_xix_R<C>(addr); NEXT; }
2265  case 0x1a: { II ii = rr_xix_R<D>(addr); NEXT; }
2266  case 0x1b: { II ii = rr_xix_R<E>(addr); NEXT; }
2267  case 0x1c: { II ii = rr_xix_R<H>(addr); NEXT; }
2268  case 0x1d: { II ii = rr_xix_R<L>(addr); NEXT; }
2269  case 0x1e: { II ii = rr_xix_R<DUMMY>(addr); NEXT; }
2270  case 0x1f: { II ii = rr_xix_R<A>(addr); NEXT; }
2271  case 0x20: { II ii = sla_xix_R<B>(addr); NEXT; }
2272  case 0x21: { II ii = sla_xix_R<C>(addr); NEXT; }
2273  case 0x22: { II ii = sla_xix_R<D>(addr); NEXT; }
2274  case 0x23: { II ii = sla_xix_R<E>(addr); NEXT; }
2275  case 0x24: { II ii = sla_xix_R<H>(addr); NEXT; }
2276  case 0x25: { II ii = sla_xix_R<L>(addr); NEXT; }
2277  case 0x26: { II ii = sla_xix_R<DUMMY>(addr); NEXT; }
2278  case 0x27: { II ii = sla_xix_R<A>(addr); NEXT; }
2279  case 0x28: { II ii = sra_xix_R<B>(addr); NEXT; }
2280  case 0x29: { II ii = sra_xix_R<C>(addr); NEXT; }
2281  case 0x2a: { II ii = sra_xix_R<D>(addr); NEXT; }
2282  case 0x2b: { II ii = sra_xix_R<E>(addr); NEXT; }
2283  case 0x2c: { II ii = sra_xix_R<H>(addr); NEXT; }
2284  case 0x2d: { II ii = sra_xix_R<L>(addr); NEXT; }
2285  case 0x2e: { II ii = sra_xix_R<DUMMY>(addr); NEXT; }
2286  case 0x2f: { II ii = sra_xix_R<A>(addr); NEXT; }
2287  case 0x30: { II ii = T::isR800() ? sll2() : sll_xix_R<B>(addr); NEXT; }
2288  case 0x31: { II ii = T::isR800() ? sll2() : sll_xix_R<C>(addr); NEXT; }
2289  case 0x32: { II ii = T::isR800() ? sll2() : sll_xix_R<D>(addr); NEXT; }
2290  case 0x33: { II ii = T::isR800() ? sll2() : sll_xix_R<E>(addr); NEXT; }
2291  case 0x34: { II ii = T::isR800() ? sll2() : sll_xix_R<H>(addr); NEXT; }
2292  case 0x35: { II ii = T::isR800() ? sll2() : sll_xix_R<L>(addr); NEXT; }
2293  case 0x36: { II ii = T::isR800() ? sll2() : sll_xix_R<DUMMY>(addr); NEXT; }
2294  case 0x37: { II ii = T::isR800() ? sll2() : sll_xix_R<A>(addr); NEXT; }
2295  case 0x38: { II ii = srl_xix_R<B>(addr); NEXT; }
2296  case 0x39: { II ii = srl_xix_R<C>(addr); NEXT; }
2297  case 0x3a: { II ii = srl_xix_R<D>(addr); NEXT; }
2298  case 0x3b: { II ii = srl_xix_R<E>(addr); NEXT; }
2299  case 0x3c: { II ii = srl_xix_R<H>(addr); NEXT; }
2300  case 0x3d: { II ii = srl_xix_R<L>(addr); NEXT; }
2301  case 0x3e: { II ii = srl_xix_R<DUMMY>(addr); NEXT; }
2302  case 0x3f: { II ii = srl_xix_R<A>(addr); NEXT; }
2303 
2304  case 0x40: case 0x41: case 0x42: case 0x43:
2305  case 0x44: case 0x45: case 0x46: case 0x47:
2306  { II ii = bit_N_xix<0>(addr); NEXT; }
2307  case 0x48: case 0x49: case 0x4a: case 0x4b:
2308  case 0x4c: case 0x4d: case 0x4e: case 0x4f:
2309  { II ii = bit_N_xix<1>(addr); NEXT; }
2310  case 0x50: case 0x51: case 0x52: case 0x53:
2311  case 0x54: case 0x55: case 0x56: case 0x57:
2312  { II ii = bit_N_xix<2>(addr); NEXT; }
2313  case 0x58: case 0x59: case 0x5a: case 0x5b:
2314  case 0x5c: case 0x5d: case 0x5e: case 0x5f:
2315  { II ii = bit_N_xix<3>(addr); NEXT; }
2316  case 0x60: case 0x61: case 0x62: case 0x63:
2317  case 0x64: case 0x65: case 0x66: case 0x67:
2318  { II ii = bit_N_xix<4>(addr); NEXT; }
2319  case 0x68: case 0x69: case 0x6a: case 0x6b:
2320  case 0x6c: case 0x6d: case 0x6e: case 0x6f:
2321  { II ii = bit_N_xix<5>(addr); NEXT; }
2322  case 0x70: case 0x71: case 0x72: case 0x73:
2323  case 0x74: case 0x75: case 0x76: case 0x77:
2324  { II ii = bit_N_xix<6>(addr); NEXT; }
2325  case 0x78: case 0x79: case 0x7a: case 0x7b:
2326  case 0x7c: case 0x7d: case 0x7e: case 0x7f:
2327  { II ii = bit_N_xix<7>(addr); NEXT; }
2328 
2329  case 0x80: { II ii = res_N_xix_R<0,B>(addr); NEXT; }
2330  case 0x81: { II ii = res_N_xix_R<0,C>(addr); NEXT; }
2331  case 0x82: { II ii = res_N_xix_R<0,D>(addr); NEXT; }
2332  case 0x83: { II ii = res_N_xix_R<0,E>(addr); NEXT; }
2333  case 0x84: { II ii = res_N_xix_R<0,H>(addr); NEXT; }
2334  case 0x85: { II ii = res_N_xix_R<0,L>(addr); NEXT; }
2335  case 0x87: { II ii = res_N_xix_R<0,A>(addr); NEXT; }
2336  case 0x88: { II ii = res_N_xix_R<1,B>(addr); NEXT; }
2337  case 0x89: { II ii = res_N_xix_R<1,C>(addr); NEXT; }
2338  case 0x8a: { II ii = res_N_xix_R<1,D>(addr); NEXT; }
2339  case 0x8b: { II ii = res_N_xix_R<1,E>(addr); NEXT; }
2340  case 0x8c: { II ii = res_N_xix_R<1,H>(addr); NEXT; }
2341  case 0x8d: { II ii = res_N_xix_R<1,L>(addr); NEXT; }
2342  case 0x8f: { II ii = res_N_xix_R<1,A>(addr); NEXT; }
2343  case 0x90: { II ii = res_N_xix_R<2,B>(addr); NEXT; }
2344  case 0x91: { II ii = res_N_xix_R<2,C>(addr); NEXT; }
2345  case 0x92: { II ii = res_N_xix_R<2,D>(addr); NEXT; }
2346  case 0x93: { II ii = res_N_xix_R<2,E>(addr); NEXT; }
2347  case 0x94: { II ii = res_N_xix_R<2,H>(addr); NEXT; }
2348  case 0x95: { II ii = res_N_xix_R<2,L>(addr); NEXT; }
2349  case 0x97: { II ii = res_N_xix_R<2,A>(addr); NEXT; }
2350  case 0x98: { II ii = res_N_xix_R<3,B>(addr); NEXT; }
2351  case 0x99: { II ii = res_N_xix_R<3,C>(addr); NEXT; }
2352  case 0x9a: { II ii = res_N_xix_R<3,D>(addr); NEXT; }
2353  case 0x9b: { II ii = res_N_xix_R<3,E>(addr); NEXT; }
2354  case 0x9c: { II ii = res_N_xix_R<3,H>(addr); NEXT; }
2355  case 0x9d: { II ii = res_N_xix_R<3,L>(addr); NEXT; }
2356  case 0x9f: { II ii = res_N_xix_R<3,A>(addr); NEXT; }
2357  case 0xa0: { II ii = res_N_xix_R<4,B>(addr); NEXT; }
2358  case 0xa1: { II ii = res_N_xix_R<4,C>(addr); NEXT; }
2359  case 0xa2: { II ii = res_N_xix_R<4,D>(addr); NEXT; }
2360  case 0xa3: { II ii = res_N_xix_R<4,E>(addr); NEXT; }
2361  case 0xa4: { II ii = res_N_xix_R<4,H>(addr); NEXT; }
2362  case 0xa5: { II ii = res_N_xix_R<4,L>(addr); NEXT; }
2363  case 0xa7: { II ii = res_N_xix_R<4,A>(addr); NEXT; }
2364  case 0xa8: { II ii = res_N_xix_R<5,B>(addr); NEXT; }
2365  case 0xa9: { II ii = res_N_xix_R<5,C>(addr); NEXT; }
2366  case 0xaa: { II ii = res_N_xix_R<5,D>(addr); NEXT; }
2367  case 0xab: { II ii = res_N_xix_R<5,E>(addr); NEXT; }
2368  case 0xac: { II ii = res_N_xix_R<5,H>(addr); NEXT; }
2369  case 0xad: { II ii = res_N_xix_R<5,L>(addr); NEXT; }
2370  case 0xaf: { II ii = res_N_xix_R<5,A>(addr); NEXT; }
2371  case 0xb0: { II ii = res_N_xix_R<6,B>(addr); NEXT; }
2372  case 0xb1: { II ii = res_N_xix_R<6,C>(addr); NEXT; }
2373  case 0xb2: { II ii = res_N_xix_R<6,D>(addr); NEXT; }
2374  case 0xb3: { II ii = res_N_xix_R<6,E>(addr); NEXT; }
2375  case 0xb4: { II ii = res_N_xix_R<6,H>(addr); NEXT; }
2376  case 0xb5: { II ii = res_N_xix_R<6,L>(addr); NEXT; }
2377  case 0xb7: { II ii = res_N_xix_R<6,A>(addr); NEXT; }
2378  case 0xb8: { II ii = res_N_xix_R<7,B>(addr); NEXT; }
2379  case 0xb9: { II ii = res_N_xix_R<7,C>(addr); NEXT; }
2380  case 0xba: { II ii = res_N_xix_R<7,D>(addr); NEXT; }
2381  case 0xbb: { II ii = res_N_xix_R<7,E>(addr); NEXT; }
2382  case 0xbc: { II ii = res_N_xix_R<7,H>(addr); NEXT; }
2383  case 0xbd: { II ii = res_N_xix_R<7,L>(addr); NEXT; }
2384  case 0xbf: { II ii = res_N_xix_R<7,A>(addr); NEXT; }
2385  case 0x86: { II ii = res_N_xix_R<0,DUMMY>(addr); NEXT; }
2386  case 0x8e: { II ii = res_N_xix_R<1,DUMMY>(addr); NEXT; }
2387  case 0x96: { II ii = res_N_xix_R<2,DUMMY>(addr); NEXT; }
2388  case 0x9e: { II ii = res_N_xix_R<3,DUMMY>(addr); NEXT; }
2389  case 0xa6: { II ii = res_N_xix_R<4,DUMMY>(addr); NEXT; }
2390  case 0xae: { II ii = res_N_xix_R<5,DUMMY>(addr); NEXT; }
2391  case 0xb6: { II ii = res_N_xix_R<6,DUMMY>(addr); NEXT; }
2392  case 0xbe: { II ii = res_N_xix_R<7,DUMMY>(addr); NEXT; }
2393 
2394  case 0xc0: { II ii = set_N_xix_R<0,B>(addr); NEXT; }
2395  case 0xc1: { II ii = set_N_xix_R<0,C>(addr); NEXT; }
2396  case 0xc2: { II ii = set_N_xix_R<0,D>(addr); NEXT; }
2397  case 0xc3: { II ii = set_N_xix_R<0,E>(addr); NEXT; }
2398  case 0xc4: { II ii = set_N_xix_R<0,H>(addr); NEXT; }
2399  case 0xc5: { II ii = set_N_xix_R<0,L>(addr); NEXT; }
2400  case 0xc7: { II ii = set_N_xix_R<0,A>(addr); NEXT; }
2401  case 0xc8: { II ii = set_N_xix_R<1,B>(addr); NEXT; }
2402  case 0xc9: { II ii = set_N_xix_R<1,C>(addr); NEXT; }
2403  case 0xca: { II ii = set_N_xix_R<1,D>(addr); NEXT; }
2404  case 0xcb: { II ii = set_N_xix_R<1,E>(addr); NEXT; }
2405  case 0xcc: { II ii = set_N_xix_R<1,H>(addr); NEXT; }
2406  case 0xcd: { II ii = set_N_xix_R<1,L>(addr); NEXT; }
2407  case 0xcf: { II ii = set_N_xix_R<1,A>(addr); NEXT; }
2408  case 0xd0: { II ii = set_N_xix_R<2,B>(addr); NEXT; }
2409  case 0xd1: { II ii = set_N_xix_R<2,C>(addr); NEXT; }
2410  case 0xd2: { II ii = set_N_xix_R<2,D>(addr); NEXT; }
2411  case 0xd3: { II ii = set_N_xix_R<2,E>(addr); NEXT; }
2412  case 0xd4: { II ii = set_N_xix_R<2,H>(addr); NEXT; }
2413  case 0xd5: { II ii = set_N_xix_R<2,L>(addr); NEXT; }
2414  case 0xd7: { II ii = set_N_xix_R<2,A>(addr); NEXT; }
2415  case 0xd8: { II ii = set_N_xix_R<3,B>(addr); NEXT; }
2416  case 0xd9: { II ii = set_N_xix_R<3,C>(addr); NEXT; }
2417  case 0xda: { II ii = set_N_xix_R<3,D>(addr); NEXT; }
2418  case 0xdb: { II ii = set_N_xix_R<3,E>(addr); NEXT; }
2419  case 0xdc: { II ii = set_N_xix_R<3,H>(addr); NEXT; }
2420  case 0xdd: { II ii = set_N_xix_R<3,L>(addr); NEXT; }
2421  case 0xdf: { II ii = set_N_xix_R<3,A>(addr); NEXT; }
2422  case 0xe0: { II ii = set_N_xix_R<4,B>(addr); NEXT; }
2423  case 0xe1: { II ii = set_N_xix_R<4,C>(addr); NEXT; }
2424  case 0xe2: { II ii = set_N_xix_R<4,D>(addr); NEXT; }
2425  case 0xe3: { II ii = set_N_xix_R<4,E>(addr); NEXT; }
2426  case 0xe4: { II ii = set_N_xix_R<4,H>(addr); NEXT; }
2427  case 0xe5: { II ii = set_N_xix_R<4,L>(addr); NEXT; }
2428  case 0xe7: { II ii = set_N_xix_R<4,A>(addr); NEXT; }
2429  case 0xe8: { II ii = set_N_xix_R<5,B>(addr); NEXT; }
2430  case 0xe9: { II ii = set_N_xix_R<5,C>(addr); NEXT; }
2431  case 0xea: { II ii = set_N_xix_R<5,D>(addr); NEXT; }
2432  case 0xeb: { II ii = set_N_xix_R<5,E>(addr); NEXT; }
2433  case 0xec: { II ii = set_N_xix_R<5,H>(addr); NEXT; }
2434  case 0xed: { II ii = set_N_xix_R<5,L>(addr); NEXT; }
2435  case 0xef: { II ii = set_N_xix_R<5,A>(addr); NEXT; }
2436  case 0xf0: { II ii = set_N_xix_R<6,B>(addr); NEXT; }
2437  case 0xf1: { II ii = set_N_xix_R<6,C>(addr); NEXT; }
2438  case 0xf2: { II ii = set_N_xix_R<6,D>(addr); NEXT; }
2439  case 0xf3: { II ii = set_N_xix_R<6,E>(addr); NEXT; }
2440  case 0xf4: { II ii = set_N_xix_R<6,H>(addr); NEXT; }
2441  case 0xf5: { II ii = set_N_xix_R<6,L>(addr); NEXT; }
2442  case 0xf7: { II ii = set_N_xix_R<6,A>(addr); NEXT; }
2443  case 0xf8: { II ii = set_N_xix_R<7,B>(addr); NEXT; }
2444  case 0xf9: { II ii = set_N_xix_R<7,C>(addr); NEXT; }
2445  case 0xfa: { II ii = set_N_xix_R<7,D>(addr); NEXT; }
2446  case 0xfb: { II ii = set_N_xix_R<7,E>(addr); NEXT; }
2447  case 0xfc: { II ii = set_N_xix_R<7,H>(addr); NEXT; }
2448  case 0xfd: { II ii = set_N_xix_R<7,L>(addr); NEXT; }
2449  case 0xff: { II ii = set_N_xix_R<7,A>(addr); NEXT; }
2450  case 0xc6: { II ii = set_N_xix_R<0,DUMMY>(addr); NEXT; }
2451  case 0xce: { II ii = set_N_xix_R<1,DUMMY>(addr); NEXT; }
2452  case 0xd6: { II ii = set_N_xix_R<2,DUMMY>(addr); NEXT; }
2453  case 0xde: { II ii = set_N_xix_R<3,DUMMY>(addr); NEXT; }
2454  case 0xe6: { II ii = set_N_xix_R<4,DUMMY>(addr); NEXT; }
2455  case 0xee: { II ii = set_N_xix_R<5,DUMMY>(addr); NEXT; }
2456  case 0xf6: { II ii = set_N_xix_R<6,DUMMY>(addr); NEXT; }
2457  case 0xfe: { II ii = set_N_xix_R<7,DUMMY>(addr); NEXT; }
2458  default: UNREACHABLE;
2459  }
2460  }
2461 }
2462 
2463 template<class T> inline void CPUCore<T>::cpuTracePre()
2464 {
2465  start_pc = getPC();
2466 }
2467 template<class T> inline void CPUCore<T>::cpuTracePost()
2468 {
2469  if (unlikely(tracingEnabled)) {
2470  cpuTracePost_slow();
2471  }
2472 }
2473 template<class T> void CPUCore<T>::cpuTracePost_slow()
2474 {
2475  byte opbuf[4];
2476  string dasmOutput;
2477  dasm(*interface, start_pc, opbuf, dasmOutput, T::getTimeFast());
2478  std::cout << std::setfill('0') << std::hex << std::setw(4) << start_pc
2479  << " : " << dasmOutput
2480  << " AF=" << std::setw(4) << getAF()
2481  << " BC=" << std::setw(4) << getBC()
2482  << " DE=" << std::setw(4) << getDE()
2483  << " HL=" << std::setw(4) << getHL()
2484  << " IX=" << std::setw(4) << getIX()
2485  << " IY=" << std::setw(4) << getIY()
2486  << " SP=" << std::setw(4) << getSP()
2487  << std::endl << std::dec;
2488 }
2489 
2490 template<class T> void CPUCore<T>::executeSlow()
2491 {
2492  if (unlikely(false && nmiEdge)) {
2493  // Note: NMIs are disabled, see also raiseNMI()
2494  nmiEdge = false;
2495  nmi(); // NMI occured
2496  } else if (unlikely(IRQStatus && getIFF1() && !prevWasEI())) {
2497  // normal interrupt
2498  if (unlikely(prevWasLDAI())) {
2499  // HACK!!!
2500  // The 'ld a,i' or 'ld a,r' instruction copies the IFF2
2501  // bit to the V flag. Though when the Z80 accepts an
2502  // IRQ directly after this instruction, the V flag is 0
2503  // (instead of the expected value 1). This can probably
2504  // be explained if you look at the pipeline of the Z80.
2505  // But for speed reasons we implement it here as a
2506  // fix-up (a hack) in the IRQ routine. This behaviour
2507  // is actually a bug in the Z80.
2508  // Thanks to n_n for reporting this behaviour. I think
2509  // this was discovered by GuyveR800. Also thanks to
2510  // n_n for writing a test program that demonstrates
2511  // this quirk.
2512  // I also wrote a test program that demonstrates this
2513  // behaviour is the same whether 'ld a,i' is preceded
2514  // by a 'ei' instruction or not (so it's not caused by
2515  // the 'delayed IRQ acceptance of ei').
2516  assert(getF() & V_FLAG);
2517  setF(getF() & ~V_FLAG);
2518  }
2519  IRQAccept.signal();
2520  switch (getIM()) {
2521  case 0: irq0();
2522  break;
2523  case 1: irq1();
2524  break;
2525  case 2: irq2();
2526  break;
2527  default:
2528  UNREACHABLE;
2529  }
2530  } else if (unlikely(getHALT())) {
2531  // in halt mode
2532  incR(T::advanceHalt(T::haltStates(), scheduler.getNext()));
2533  setSlowInstructions();
2534  } else {
2535  cpuTracePre();
2536  assert(T::limitReached()); // we want only one instruction
2537  executeInstructions();
2538  endInstruction();
2539 
2540  if (T::isR800()) {
2541  if (unlikely(prev2WasCall()) && likely(!prevWasPopRet())) {
2542  // On R800 a CALL or RST instruction not _immediately_
2543  // followed by a (single-byte) POP or RET instruction
2544  // causes an extra cycle in that following instruction.
2545  // No idea why yet. See doc/internal/r800-call.txt
2546  // for more information.
2547  //
2548  // TODO this implementation adds the extra cycle at
2549  // the end of the instruction POP/RET. It is not known
2550  // where in the instruction the real R800 adds this cycle.
2551  T::add(1);
2552  }
2553  }
2554  cpuTracePost();
2555  }
2556 }
2557 
2558 template<class T> void CPUCore<T>::execute(bool fastForward)
2559 {
2560  // In fast-forward mode, breakpoints, watchpoints or debug condtions
2561  // won't trigger. It is possible we already are in break mode, but
2562  // break is ignored in fast-forward mode.
2563  assert(fastForward || !interface->isBreaked());
2564  if (fastForward) {
2565  interface->setFastForward(true);
2566  }
2567  execute2(fastForward);
2568  interface->setFastForward(false);
2569 }
2570 
2571 template<class T> void CPUCore<T>::execute2(bool fastForward)
2572 {
2573  // note: Don't use getTimeFast() here, because 'once in a while' we
2574  // need to CPUClock::sync() to avoid overflow.
2575  // Should be done at least once per second (approx). So only
2576  // once in this method is enough.
2577  scheduler.schedule(T::getTime());
2578  setSlowInstructions();
2579 
2580  if (!fastForward && (interface->isContinue() || interface->isStep())) {
2581  // at least one instruction
2582  interface->setContinue(false);
2583  executeSlow();
2584  scheduler.schedule(T::getTimeFast());
2585  --slowInstructions;
2586  if (interface->isStep()) {
2587  interface->setStep(false);
2588  interface->doBreak();
2589  return;
2590  }
2591  }
2592 
2593  // Note: we call scheduler _after_ executing the instruction and before
2594  // deciding between executeFast() and executeSlow() (because a
2595  // SyncPoint could set an IRQ and then we must choose executeSlow())
2596  if (fastForward ||
2597  (!interface->anyBreakPoints() && !tracingEnabled)) {
2598  // fast path, no breakpoints, no tracing
2599  while (!needExitCPULoop()) {
2600  if (slowInstructions) {
2601  --slowInstructions;
2602  executeSlow();
2603  scheduler.schedule(T::getTimeFast());
2604  } else {
2605  while (slowInstructions == 0) {
2606  T::enableLimit(); // does CPUClock::sync()
2607  if (likely(!T::limitReached())) {
2608  // multiple instructions
2609  executeInstructions();
2610  // note: pipeline only shifted one
2611  // step for multiple instructions
2612  endInstruction();
2613  }
2614  scheduler.schedule(T::getTimeFast());
2615  if (needExitCPULoop()) return;
2616  }
2617  }
2618  }
2619  } else {
2620  while (!needExitCPULoop()) {
2621  if (interface->checkBreakPoints(getPC(), motherboard)) {
2622  assert(interface->isBreaked());
2623  break;
2624  }
2625  if (slowInstructions == 0) {
2626  cpuTracePre();
2627  assert(T::limitReached()); // only one instruction
2628  executeInstructions();
2629  endInstruction();
2630  cpuTracePost();
2631  } else {
2632  --slowInstructions;
2633  executeSlow();
2634  }
2635  // Don't use getTimeFast() here, we need a call to
2636  // CPUClock::sync() 'once in a while'. (During a
2637  // reverse fast-forward this wasn't always the case).
2638  scheduler.schedule(T::getTime());
2639  }
2640  }
2641 }
2642 
2643 template<class T> template<Reg8 R8> ALWAYS_INLINE byte CPUCore<T>::get8() const {
2644  if (R8 == A) { return getA(); }
2645  else if (R8 == F) { return getF(); }
2646  else if (R8 == B) { return getB(); }
2647  else if (R8 == C) { return getC(); }
2648  else if (R8 == D) { return getD(); }
2649  else if (R8 == E) { return getE(); }
2650  else if (R8 == H) { return getH(); }
2651  else if (R8 == L) { return getL(); }
2652  else if (R8 == IXH) { return getIXh(); }
2653  else if (R8 == IXL) { return getIXl(); }
2654  else if (R8 == IYH) { return getIYh(); }
2655  else if (R8 == IYL) { return getIYl(); }
2656  else if (R8 == REG_I) { return getI(); }
2657  else if (R8 == REG_R) { return getR(); }
2658  else if (R8 == DUMMY) { return 0; }
2659  else { UNREACHABLE; return 0; }
2660 }
2661 template<class T> template<Reg16 R16> ALWAYS_INLINE unsigned CPUCore<T>::get16() const {
2662  if (R16 == AF) { return getAF(); }
2663  else if (R16 == BC) { return getBC(); }
2664  else if (R16 == DE) { return getDE(); }
2665  else if (R16 == HL) { return getHL(); }
2666  else if (R16 == IX) { return getIX(); }
2667  else if (R16 == IY) { return getIY(); }
2668  else if (R16 == SP) { return getSP(); }
2669  else { UNREACHABLE; return 0; }
2670 }
2671 template<class T> template<Reg8 R8> ALWAYS_INLINE void CPUCore<T>::set8(byte x) {
2672  if (R8 == A) { setA(x); }
2673  else if (R8 == F) { setF(x); }
2674  else if (R8 == B) { setB(x); }
2675  else if (R8 == C) { setC(x); }
2676  else if (R8 == D) { setD(x); }
2677  else if (R8 == E) { setE(x); }
2678  else if (R8 == H) { setH(x); }
2679  else if (R8 == L) { setL(x); }
2680  else if (R8 == IXH) { setIXh(x); }
2681  else if (R8 == IXL) { setIXl(x); }
2682  else if (R8 == IYH) { setIYh(x); }
2683  else if (R8 == IYL) { setIYl(x); }
2684  else if (R8 == REG_I) { setI(x); }
2685  else if (R8 == REG_R) { setR(x); }
2686  else if (R8 == DUMMY) { /* nothing */ }
2687  else { UNREACHABLE; }
2688 }
2689 template<class T> template<Reg16 R16> ALWAYS_INLINE void CPUCore<T>::set16(unsigned x) {
2690  if (R16 == AF) { setAF(x); }
2691  else if (R16 == BC) { setBC(x); }
2692  else if (R16 == DE) { setDE(x); }
2693  else if (R16 == HL) { setHL(x); }
2694  else if (R16 == IX) { setIX(x); }
2695  else if (R16 == IY) { setIY(x); }
2696  else if (R16 == SP) { setSP(x); }
2697  else { UNREACHABLE; }
2698 }
2699 
2700 // LD r,r
2701 template<class T> template<Reg8 DST, Reg8 SRC, int EE> II CPUCore<T>::ld_R_R() {
2702  set8<DST>(get8<SRC>()); return {1, T::CC_LD_R_R + EE};
2703 }
2704 
2705 // LD SP,ss
2706 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::ld_sp_SS() {
2707  setSP(get16<REG>()); return {1, T::CC_LD_SP_HL + EE};
2708 }
2709 
2710 // LD (ss),a
2711 template<class T> template<Reg16 REG> II CPUCore<T>::ld_SS_a() {
2712  T::setMemPtr((getA() << 8) | ((get16<REG>() + 1) & 0xFF));
2713  WRMEM(get16<REG>(), getA(), T::CC_LD_SS_A_1);
2714  return {1, T::CC_LD_SS_A};
2715 }
2716 
2717 // LD (HL),r
2718 template<class T> template<Reg8 SRC> II CPUCore<T>::ld_xhl_R() {
2719  WRMEM(getHL(), get8<SRC>(), T::CC_LD_HL_R_1);
2720  return {1, T::CC_LD_HL_R};
2721 }
2722 
2723 // LD (IXY+e),r
2724 template<class T> template<Reg16 IXY, Reg8 SRC> II CPUCore<T>::ld_xix_R() {
2725  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_LD_XIX_R_1);
2726  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
2727  T::setMemPtr(addr);
2728  WRMEM(addr, get8<SRC>(), T::CC_DD + T::CC_LD_XIX_R_2);
2729  return {2, T::CC_DD + T::CC_LD_XIX_R};
2730 }
2731 
2732 // LD (HL),n
2733 template<class T> II CPUCore<T>::ld_xhl_byte() {
2734  byte val = RDMEM_OPCODE<1>(T::CC_LD_HL_N_1);
2735  WRMEM(getHL(), val, T::CC_LD_HL_N_2);
2736  return {2, T::CC_LD_HL_N};
2737 }
2738 
2739 // LD (IXY+e),n
2740 template<class T> template<Reg16 IXY> II CPUCore<T>::ld_xix_byte() {
2741  unsigned tmp = RD_WORD_PC<1>(T::CC_DD + T::CC_LD_XIX_N_1);
2742  int8_t ofst = tmp & 0xFF;
2743  byte val = tmp >> 8;
2744  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
2745  T::setMemPtr(addr);
2746  WRMEM(addr, val, T::CC_DD + T::CC_LD_XIX_N_2);
2747  return {3, T::CC_DD + T::CC_LD_XIX_N};
2748 }
2749 
2750 // LD (nn),A
2751 template<class T> II CPUCore<T>::ld_xbyte_a() {
2752  unsigned x = RD_WORD_PC<1>(T::CC_LD_NN_A_1);
2753  T::setMemPtr((getA() << 8) | ((x + 1) & 0xFF));
2754  WRMEM(x, getA(), T::CC_LD_NN_A_2);
2755  return {3, T::CC_LD_NN_A};
2756 }
2757 
2758 // LD (nn),ss
2759 template<class T> template<int EE> inline II CPUCore<T>::WR_NN_Y(unsigned reg) {
2760  unsigned addr = RD_WORD_PC<1>(T::CC_LD_XX_HL_1 + EE);
2761  T::setMemPtr(addr + 1);
2762  WR_WORD(addr, reg, T::CC_LD_XX_HL_2 + EE);
2763  return {3, T::CC_LD_XX_HL + EE};
2764 }
2765 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::ld_xword_SS() {
2766  return WR_NN_Y<EE >(get16<REG>());
2767 }
2768 template<class T> template<Reg16 REG> II CPUCore<T>::ld_xword_SS_ED() {
2769  return WR_NN_Y<T::EE_ED>(get16<REG>());
2770 }
2771 
2772 // LD A,(ss)
2773 template<class T> template<Reg16 REG> II CPUCore<T>::ld_a_SS() {
2774  T::setMemPtr(get16<REG>() + 1);
2775  setA(RDMEM(get16<REG>(), T::CC_LD_A_SS_1));
2776  return {1, T::CC_LD_A_SS};
2777 }
2778 
2779 // LD A,(nn)
2780 template<class T> II CPUCore<T>::ld_a_xbyte() {
2781  unsigned addr = RD_WORD_PC<1>(T::CC_LD_A_NN_1);
2782  T::setMemPtr(addr + 1);
2783  setA(RDMEM(addr, T::CC_LD_A_NN_2));
2784  return {3, T::CC_LD_A_NN};
2785 }
2786 
2787 // LD r,n
2788 template<class T> template<Reg8 DST, int EE> II CPUCore<T>::ld_R_byte() {
2789  set8<DST>(RDMEM_OPCODE<1>(T::CC_LD_R_N_1 + EE)); return {2, T::CC_LD_R_N + EE};
2790 }
2791 
2792 // LD r,(hl)
2793 template<class T> template<Reg8 DST> II CPUCore<T>::ld_R_xhl() {
2794  set8<DST>(RDMEM(getHL(), T::CC_LD_R_HL_1)); return {1, T::CC_LD_R_HL};
2795 }
2796 
2797 // LD r,(IXY+e)
2798 template<class T> template<Reg8 DST, Reg16 IXY> II CPUCore<T>::ld_R_xix() {
2799  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_LD_R_XIX_1);
2800  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
2801  T::setMemPtr(addr);
2802  set8<DST>(RDMEM(addr, T::CC_DD + T::CC_LD_R_XIX_2));
2803  return {2, T::CC_DD + T::CC_LD_R_XIX};
2804 }
2805 
2806 // LD ss,(nn)
2807 template<class T> template<int EE> inline unsigned CPUCore<T>::RD_P_XX() {
2808  unsigned addr = RD_WORD_PC<1>(T::CC_LD_HL_XX_1 + EE);
2809  T::setMemPtr(addr + 1);
2810  unsigned result = RD_WORD(addr, T::CC_LD_HL_XX_2 + EE);
2811  return result;
2812 }
2813 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::ld_SS_xword() {
2814  set16<REG>(RD_P_XX<EE>()); return {3, T::CC_LD_HL_XX + EE};
2815 }
2816 template<class T> template<Reg16 REG> II CPUCore<T>::ld_SS_xword_ED() {
2817  set16<REG>(RD_P_XX<T::EE_ED>()); return {3, T::CC_LD_HL_XX + T::EE_ED};
2818 }
2819 
2820 // LD ss,nn
2821 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::ld_SS_word() {
2822  set16<REG>(RD_WORD_PC<1>(T::CC_LD_SS_NN_1 + EE)); return {3, T::CC_LD_SS_NN + EE};
2823 }
2824 
2825 
2826 // ADC A,r
2827 template<class T> inline void CPUCore<T>::ADC(byte reg) {
2828  unsigned res = getA() + reg + ((getF() & C_FLAG) ? 1 : 0);
2829  byte f = ((res & 0x100) ? C_FLAG : 0) |
2830  ((getA() ^ res ^ reg) & H_FLAG) |
2831  (((getA() ^ res) & (reg ^ res) & 0x80) >> 5) | // V_FLAG
2832  0; // N_FLAG
2833  if (T::isR800()) {
2834  f |= ZSTable[res & 0xFF];
2835  f |= getF() & (X_FLAG | Y_FLAG);
2836  } else {
2837  f |= ZSXYTable[res & 0xFF];
2838  }
2839  setF(f);
2840  setA(res);
2841 }
2842 template<class T> inline II CPUCore<T>::adc_a_a() {
2843  unsigned res = 2 * getA() + ((getF() & C_FLAG) ? 1 : 0);
2844  byte f = ((res & 0x100) ? C_FLAG : 0) |
2845  (res & H_FLAG) |
2846  (((getA() ^ res) & 0x80) >> 5) | // V_FLAG
2847  0; // N_FLAG
2848  if (T::isR800()) {
2849  f |= ZSTable[res & 0xFF];
2850  f |= getF() & (X_FLAG | Y_FLAG);
2851  } else {
2852  f |= ZSXYTable[res & 0xFF];
2853  }
2854  setF(f);
2855  setA(res);
2856  return {1, T::CC_CP_R};
2857 }
2858 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::adc_a_R() {
2859  ADC(get8<SRC>()); return {1, T::CC_CP_R + EE};
2860 }
2861 template<class T> II CPUCore<T>::adc_a_byte() {
2862  ADC(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
2863 }
2864 template<class T> II CPUCore<T>::adc_a_xhl() {
2865  ADC(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
2866 }
2867 template<class T> template<Reg16 IXY> II CPUCore<T>::adc_a_xix() {
2868  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
2869  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
2870  T::setMemPtr(addr);
2871  ADC(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
2872  return {2, T::CC_DD + T::CC_CP_XIX};
2873 }
2874 
2875 // ADD A,r
2876 template<class T> inline void CPUCore<T>::ADD(byte reg) {
2877  unsigned res = getA() + reg;
2878  byte f = ((res & 0x100) ? C_FLAG : 0) |
2879  ((getA() ^ res ^ reg) & H_FLAG) |
2880  (((getA() ^ res) & (reg ^ res) & 0x80) >> 5) | // V_FLAG
2881  0; // N_FLAG
2882  if (T::isR800()) {
2883  f |= ZSTable[res & 0xFF];
2884  f |= getF() & (X_FLAG | Y_FLAG);
2885  } else {
2886  f |= ZSXYTable[res & 0xFF];
2887  }
2888  setF(f);
2889  setA(res);
2890 }
2891 template<class T> inline II CPUCore<T>::add_a_a() {
2892  unsigned res = 2 * getA();
2893  byte f = ((res & 0x100) ? C_FLAG : 0) |
2894  (res & H_FLAG) |
2895  (((getA() ^ res) & 0x80) >> 5) | // V_FLAG
2896  0; // N_FLAG
2897  if (T::isR800()) {
2898  f |= ZSTable[res & 0xFF];
2899  f |= getF() & (X_FLAG | Y_FLAG);
2900  } else {
2901  f |= ZSXYTable[res & 0xFF];
2902  }
2903  setF(f);
2904  setA(res);
2905  return {1, T::CC_CP_R};
2906 }
2907 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::add_a_R() {
2908  ADD(get8<SRC>()); return {1, T::CC_CP_R + EE};
2909 }
2910 template<class T> II CPUCore<T>::add_a_byte() {
2911  ADD(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
2912 }
2913 template<class T> II CPUCore<T>::add_a_xhl() {
2914  ADD(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
2915 }
2916 template<class T> template<Reg16 IXY> II CPUCore<T>::add_a_xix() {
2917  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
2918  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
2919  T::setMemPtr(addr);
2920  ADD(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
2921  return {2, T::CC_DD + T::CC_CP_XIX};
2922 }
2923 
2924 // AND r
2925 template<class T> inline void CPUCore<T>::AND(byte reg) {
2926  setA(getA() & reg);
2927  byte f = 0;
2928  if (T::isR800()) {
2929  f |= ZSPHTable[getA()];
2930  f |= getF() & (X_FLAG | Y_FLAG);
2931  } else {
2932  f |= ZSPXYTable[getA()] | H_FLAG;
2933  }
2934  setF(f);
2935 }
2936 template<class T> II CPUCore<T>::and_a() {
2937  byte f = 0;
2938  if (T::isR800()) {
2939  f |= ZSPHTable[getA()];
2940  f |= getF() & (X_FLAG | Y_FLAG);
2941  } else {
2942  f |= ZSPXYTable[getA()] | H_FLAG;
2943  }
2944  setF(f);
2945  return {1, T::CC_CP_R};
2946 }
2947 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::and_R() {
2948  AND(get8<SRC>()); return {1, T::CC_CP_R + EE};
2949 }
2950 template<class T> II CPUCore<T>::and_byte() {
2951  AND(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
2952 }
2953 template<class T> II CPUCore<T>::and_xhl() {
2954  AND(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
2955 }
2956 template<class T> template<Reg16 IXY> II CPUCore<T>::and_xix() {
2957  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
2958  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
2959  T::setMemPtr(addr);
2960  AND(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
2961  return {2, T::CC_DD + T::CC_CP_XIX};
2962 }
2963 
2964 // CP r
2965 template<class T> inline void CPUCore<T>::CP(byte reg) {
2966  unsigned q = getA() - reg;
2967  byte f = ZSTable[q & 0xFF] |
2968  ((q & 0x100) ? C_FLAG : 0) |
2969  N_FLAG |
2970  ((getA() ^ q ^ reg) & H_FLAG) |
2971  (((reg ^ getA()) & (getA() ^ q) & 0x80) >> 5); // V_FLAG
2972  if (T::isR800()) {
2973  f |= getF() & (X_FLAG | Y_FLAG);
2974  } else {
2975  f |= reg & (X_FLAG | Y_FLAG); // XY from operand, not from result
2976  }
2977  setF(f);
2978 }
2979 template<class T> II CPUCore<T>::cp_a() {
2980  byte f = ZS0 | N_FLAG;
2981  if (T::isR800()) {
2982  f |= getF() & (X_FLAG | Y_FLAG);
2983  } else {
2984  f |= getA() & (X_FLAG | Y_FLAG); // XY from operand, not from result
2985  }
2986  setF(f);
2987  return {1, T::CC_CP_R};
2988 }
2989 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::cp_R() {
2990  CP(get8<SRC>()); return {1, T::CC_CP_R + EE};
2991 }
2992 template<class T> II CPUCore<T>::cp_byte() {
2993  CP(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
2994 }
2995 template<class T> II CPUCore<T>::cp_xhl() {
2996  CP(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
2997 }
2998 template<class T> template<Reg16 IXY> II CPUCore<T>::cp_xix() {
2999  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
3000  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3001  T::setMemPtr(addr);
3002  CP(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
3003  return {2, T::CC_DD + T::CC_CP_XIX};
3004 }
3005 
3006 // OR r
3007 template<class T> inline void CPUCore<T>::OR(byte reg) {
3008  setA(getA() | reg);
3009  byte f = 0;
3010  if (T::isR800()) {
3011  f |= ZSPTable[getA()];
3012  f |= getF() & (X_FLAG | Y_FLAG);
3013  } else {
3014  f |= ZSPXYTable[getA()];
3015  }
3016  setF(f);
3017 }
3018 template<class T> II CPUCore<T>::or_a() {
3019  byte f = 0;
3020  if (T::isR800()) {
3021  f |= ZSPTable[getA()];
3022  f |= getF() & (X_FLAG | Y_FLAG);
3023  } else {
3024  f |= ZSPXYTable[getA()];
3025  }
3026  setF(f);
3027  return {1, T::CC_CP_R};
3028 }
3029 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::or_R() {
3030  OR(get8<SRC>()); return {1, T::CC_CP_R + EE};
3031 }
3032 template<class T> II CPUCore<T>::or_byte() {
3033  OR(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
3034 }
3035 template<class T> II CPUCore<T>::or_xhl() {
3036  OR(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
3037 }
3038 template<class T> template<Reg16 IXY> II CPUCore<T>::or_xix() {
3039  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
3040  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3041  T::setMemPtr(addr);
3042  OR(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
3043  return {2, T::CC_DD + T::CC_CP_XIX};
3044 }
3045 
3046 // SBC A,r
3047 template<class T> inline void CPUCore<T>::SBC(byte reg) {
3048  unsigned res = getA() - reg - ((getF() & C_FLAG) ? 1 : 0);
3049  byte f = ((res & 0x100) ? C_FLAG : 0) |
3050  N_FLAG |
3051  ((getA() ^ res ^ reg) & H_FLAG) |
3052  (((reg ^ getA()) & (getA() ^ res) & 0x80) >> 5); // V_FLAG
3053  if (T::isR800()) {
3054  f |= ZSTable[res & 0xFF];
3055  f |= getF() & (X_FLAG | Y_FLAG);
3056  } else {
3057  f |= ZSXYTable[res & 0xFF];
3058  }
3059  setF(f);
3060  setA(res);
3061 }
3062 template<class T> II CPUCore<T>::sbc_a_a() {
3063  if (T::isR800()) {
3064  word t = (getF() & C_FLAG)
3065  ? (255 * 256 | ZS255 | C_FLAG | H_FLAG | N_FLAG)
3066  : ( 0 * 256 | ZS0 | N_FLAG);
3067  setAF(t | (getF() & (X_FLAG | Y_FLAG)));
3068  } else {
3069  setAF((getF() & C_FLAG) ?
3070  (255 * 256 | ZSXY255 | C_FLAG | H_FLAG | N_FLAG) :
3071  ( 0 * 256 | ZSXY0 | N_FLAG));
3072  }
3073  return {1, T::CC_CP_R};
3074 }
3075 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::sbc_a_R() {
3076  SBC(get8<SRC>()); return {1, T::CC_CP_R + EE};
3077 }
3078 template<class T> II CPUCore<T>::sbc_a_byte() {
3079  SBC(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
3080 }
3081 template<class T> II CPUCore<T>::sbc_a_xhl() {
3082  SBC(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
3083 }
3084 template<class T> template<Reg16 IXY> II CPUCore<T>::sbc_a_xix() {
3085  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
3086  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3087  T::setMemPtr(addr);
3088  SBC(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
3089  return {2, T::CC_DD + T::CC_CP_XIX};
3090 }
3091 
3092 // SUB r
3093 template<class T> inline void CPUCore<T>::SUB(byte reg) {
3094  unsigned res = getA() - reg;
3095  byte f = ((res & 0x100) ? C_FLAG : 0) |
3096  N_FLAG |
3097  ((getA() ^ res ^ reg) & H_FLAG) |
3098  (((reg ^ getA()) & (getA() ^ res) & 0x80) >> 5); // V_FLAG
3099  if (T::isR800()) {
3100  f |= ZSTable[res & 0xFF];
3101  f |= getF() & (X_FLAG | Y_FLAG);
3102  } else {
3103  f |= ZSXYTable[res & 0xFF];
3104  }
3105  setF(f);
3106  setA(res);
3107 }
3108 template<class T> II CPUCore<T>::sub_a() {
3109  if (T::isR800()) {
3110  word t = 0 * 256 | ZS0 | N_FLAG;
3111  setAF(t | (getF() & (X_FLAG | Y_FLAG)));
3112  } else {
3113  setAF(0 * 256 | ZSXY0 | N_FLAG);
3114  }
3115  return {1, T::CC_CP_R};
3116 }
3117 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::sub_R() {
3118  SUB(get8<SRC>()); return {1, T::CC_CP_R + EE};
3119 }
3120 template<class T> II CPUCore<T>::sub_byte() {
3121  SUB(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
3122 }
3123 template<class T> II CPUCore<T>::sub_xhl() {
3124  SUB(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
3125 }
3126 template<class T> template<Reg16 IXY> II CPUCore<T>::sub_xix() {
3127  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
3128  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3129  T::setMemPtr(addr);
3130  SUB(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
3131  return {2, T::CC_DD + T::CC_CP_XIX};
3132 }
3133 
3134 // XOR r
3135 template<class T> inline void CPUCore<T>::XOR(byte reg) {
3136  setA(getA() ^ reg);
3137  byte f = 0;
3138  if (T::isR800()) {
3139  f |= ZSPTable[getA()];
3140  f |= getF() & (X_FLAG | Y_FLAG);
3141  } else {
3142  f |= ZSPXYTable[getA()];
3143  }
3144  setF(f);
3145 }
3146 template<class T> II CPUCore<T>::xor_a() {
3147  if (T::isR800()) {
3148  word t = 0 * 256 + ZSP0;
3149  setAF(t | (getF() & (X_FLAG | Y_FLAG)));
3150  } else {
3151  setAF(0 * 256 + ZSPXY0);
3152  }
3153  return {1, T::CC_CP_R};
3154 }
3155 template<class T> template<Reg8 SRC, int EE> II CPUCore<T>::xor_R() {
3156  XOR(get8<SRC>()); return {1, T::CC_CP_R + EE};
3157 }
3158 template<class T> II CPUCore<T>::xor_byte() {
3159  XOR(RDMEM_OPCODE<1>(T::CC_CP_N_1)); return {2, T::CC_CP_N};
3160 }
3161 template<class T> II CPUCore<T>::xor_xhl() {
3162  XOR(RDMEM(getHL(), T::CC_CP_XHL_1)); return {1, T::CC_CP_XHL};
3163 }
3164 template<class T> template<Reg16 IXY> II CPUCore<T>::xor_xix() {
3165  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_CP_XIX_1);
3166  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3167  T::setMemPtr(addr);
3168  XOR(RDMEM(addr, T::CC_DD + T::CC_CP_XIX_2));
3169  return {2, T::CC_DD + T::CC_CP_XIX};
3170 }
3171 
3172 
3173 // DEC r
3174 template<class T> inline byte CPUCore<T>::DEC(byte reg) {
3175  byte res = reg - 1;
3176  byte f = ((reg & ~res & 0x80) >> 5) | // V_FLAG
3177  (((res & 0x0F) + 1) & H_FLAG) |
3178  N_FLAG;
3179  if (T::isR800()) {
3180  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
3181  f |= ZSTable[res];
3182  } else {
3183  f |= getF() & C_FLAG;
3184  f |= ZSXYTable[res];
3185  }
3186  setF(f);
3187  return res;
3188 }
3189 template<class T> template<Reg8 REG, int EE> II CPUCore<T>::dec_R() {
3190  set8<REG>(DEC(get8<REG>())); return {1, T::CC_INC_R + EE};
3191 }
3192 template<class T> template<int EE> inline void CPUCore<T>::DEC_X(unsigned x) {
3193  byte val = DEC(RDMEM(x, T::CC_INC_XHL_1 + EE));
3194  WRMEM(x, val, T::CC_INC_XHL_2 + EE);
3195 }
3196 template<class T> II CPUCore<T>::dec_xhl() {
3197  DEC_X<0>(getHL());
3198  return {1, T::CC_INC_XHL};
3199 }
3200 template<class T> template<Reg16 IXY> II CPUCore<T>::dec_xix() {
3201  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_INC_XIX_1);
3202  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3203  T::setMemPtr(addr);
3204  DEC_X<T::CC_DD + T::EE_INC_XIX>(addr);
3205  return {2, T::CC_INC_XHL + T::CC_DD + T::EE_INC_XIX};
3206 }
3207 
3208 // INC r
3209 template<class T> inline byte CPUCore<T>::INC(byte reg) {
3210  reg++;
3211  byte f = ((reg & -reg & 0x80) >> 5) | // V_FLAG
3212  (((reg & 0x0F) - 1) & H_FLAG) |
3213  0; // N_FLAG
3214  if (T::isR800()) {
3215  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
3216  f |= ZSTable[reg];
3217  } else {
3218  f |= getF() & C_FLAG;
3219  f |= ZSXYTable[reg];
3220  }
3221  setF(f);
3222  return reg;
3223 }
3224 template<class T> template<Reg8 REG, int EE> II CPUCore<T>::inc_R() {
3225  set8<REG>(INC(get8<REG>())); return {1, T::CC_INC_R + EE};
3226 }
3227 template<class T> template<int EE> inline void CPUCore<T>::INC_X(unsigned x) {
3228  byte val = INC(RDMEM(x, T::CC_INC_XHL_1 + EE));
3229  WRMEM(x, val, T::CC_INC_XHL_2 + EE);
3230 }
3231 template<class T> II CPUCore<T>::inc_xhl() {
3232  INC_X<0>(getHL());
3233  return {1, T::CC_INC_XHL};
3234 }
3235 template<class T> template<Reg16 IXY> II CPUCore<T>::inc_xix() {
3236  int8_t ofst = RDMEM_OPCODE<1>(T::CC_DD + T::CC_INC_XIX_1);
3237  unsigned addr = (get16<IXY>() + ofst) & 0xFFFF;
3238  T::setMemPtr(addr);
3239  INC_X<T::CC_DD + T::EE_INC_XIX>(addr);
3240  return {2, T::CC_INC_XHL + T::CC_DD + T::EE_INC_XIX};
3241 }
3242 
3243 
3244 // ADC HL,ss
3245 template<class T> template<Reg16 REG> inline II CPUCore<T>::adc_hl_SS() {
3246  unsigned reg = get16<REG>();
3247  T::setMemPtr(getHL() + 1);
3248  unsigned res = getHL() + reg + ((getF() & C_FLAG) ? 1 : 0);
3249  byte f = (res >> 16) | // C_FLAG
3250  0; // N_FLAG
3251  if (T::isR800()) {
3252  f |= getF() & (X_FLAG | Y_FLAG);
3253  }
3254  if (res & 0xFFFF) {
3255  f |= ((getHL() ^ res ^ reg) >> 8) & H_FLAG;
3256  f |= 0; // Z_FLAG
3257  f |= ((getHL() ^ res) & (reg ^ res) & 0x8000) >> 13; // V_FLAG
3258  if (T::isR800()) {
3259  f |= (res >> 8) & S_FLAG;
3260  } else {
3261  f |= (res >> 8) & (S_FLAG | X_FLAG | Y_FLAG);
3262  }
3263  } else {
3264  f |= ((getHL() ^ reg) >> 8) & H_FLAG;
3265  f |= Z_FLAG;
3266  f |= (getHL() & reg & 0x8000) >> 13; // V_FLAG
3267  f |= 0; // S_FLAG (X_FLAG Y_FLAG)
3268  }
3269  setF(f);
3270  setHL(res);
3271  return {1, T::CC_ADC_HL_SS};
3272 }
3273 template<class T> II CPUCore<T>::adc_hl_hl() {
3274  T::setMemPtr(getHL() + 1);
3275  unsigned res = 2 * getHL() + ((getF() & C_FLAG) ? 1 : 0);
3276  byte f = (res >> 16) | // C_FLAG
3277  0; // N_FLAG
3278  if (T::isR800()) {
3279  f |= getF() & (X_FLAG | Y_FLAG);
3280  }
3281  if (res & 0xFFFF) {
3282  f |= 0; // Z_FLAG
3283  f |= ((getHL() ^ res) & 0x8000) >> 13; // V_FLAG
3284  if (T::isR800()) {
3285  f |= (res >> 8) & (H_FLAG | S_FLAG);
3286  } else {
3287  f |= (res >> 8) & (H_FLAG | S_FLAG | X_FLAG | Y_FLAG);
3288  }
3289  } else {
3290  f |= Z_FLAG;
3291  f |= (getHL() & 0x8000) >> 13; // V_FLAG
3292  f |= 0; // H_FLAG S_FLAG (X_FLAG Y_FLAG)
3293  }
3294  setF(f);
3295  setHL(res);
3296  return {1, T::CC_ADC_HL_SS};
3297 }
3298 
3299 // ADD HL/IX/IY,ss
3300 template<class T> template<Reg16 REG1, Reg16 REG2, int EE> II CPUCore<T>::add_SS_TT() {
3301  unsigned reg1 = get16<REG1>();
3302  unsigned reg2 = get16<REG2>();
3303  T::setMemPtr(reg1 + 1);
3304  unsigned res = reg1 + reg2;
3305  byte f = (((reg1 ^ res ^ reg2) >> 8) & H_FLAG) |
3306  (res >> 16) | // C_FLAG
3307  0; // N_FLAG
3308  if (T::isR800()) {
3309  f |= getF() & (S_FLAG | Z_FLAG | V_FLAG | X_FLAG | Y_FLAG);
3310  } else {
3311  f |= getF() & (S_FLAG | Z_FLAG | V_FLAG);
3312  f |= (res >> 8) & (X_FLAG | Y_FLAG);
3313  }
3314  setF(f);
3315  set16<REG1>(res & 0xFFFF);
3316  return {1, T::CC_ADD_HL_SS + EE};
3317 }
3318 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::add_SS_SS() {
3319  unsigned reg = get16<REG>();
3320  T::setMemPtr(reg + 1);
3321  unsigned res = 2 * reg;
3322  byte f = (res >> 16) | // C_FLAG
3323  0; // N_FLAG
3324  if (T::isR800()) {
3325  f |= getF() & (S_FLAG | Z_FLAG | V_FLAG | X_FLAG | Y_FLAG);
3326  f |= (res >> 8) & H_FLAG;
3327  } else {
3328  f |= getF() & (S_FLAG | Z_FLAG | V_FLAG);
3329  f |= (res >> 8) & (H_FLAG | X_FLAG | Y_FLAG);
3330  }
3331  setF(f);
3332  set16<REG>(res & 0xFFFF);
3333  return {1, T::CC_ADD_HL_SS + EE};
3334 }
3335 
3336 // SBC HL,ss
3337 template<class T> template<Reg16 REG> inline II CPUCore<T>::sbc_hl_SS() {
3338  unsigned reg = get16<REG>();
3339  T::setMemPtr(getHL() + 1);
3340  unsigned res = getHL() - reg - ((getF() & C_FLAG) ? 1 : 0);
3341  byte f = ((res & 0x10000) ? C_FLAG : 0) |
3342  N_FLAG;
3343  if (T::isR800()) {
3344  f |= getF() & (X_FLAG | Y_FLAG);
3345  }
3346  if (res & 0xFFFF) {
3347  f |= ((getHL() ^ res ^ reg) >> 8) & H_FLAG;
3348  f |= 0; // Z_FLAG
3349  f |= ((reg ^ getHL()) & (getHL() ^ res) & 0x8000) >> 13; // V_FLAG
3350  if (T::isR800()) {
3351  f |= (res >> 8) & S_FLAG;
3352  } else {
3353  f |= (res >> 8) & (S_FLAG | X_FLAG | Y_FLAG);
3354  }
3355  } else {
3356  f |= ((getHL() ^ reg) >> 8) & H_FLAG;
3357  f |= Z_FLAG;
3358  f |= ((reg ^ getHL()) & getHL() & 0x8000) >> 13; // V_FLAG
3359  f |= 0; // S_FLAG (X_FLAG Y_FLAG)
3360  }
3361  setF(f);
3362  setHL(res);
3363  return {1, T::CC_ADC_HL_SS};
3364 }
3365 template<class T> II CPUCore<T>::sbc_hl_hl() {
3366  T::setMemPtr(getHL() + 1);
3367  byte f = T::isR800() ? (getF() & (X_FLAG | Y_FLAG)) : 0;
3368  if (getF() & C_FLAG) {
3369  f |= C_FLAG | H_FLAG | S_FLAG | N_FLAG;
3370  if (!T::isR800()) {
3371  f |= X_FLAG | Y_FLAG;
3372  }
3373  setHL(0xFFFF);
3374  } else {
3375  f |= Z_FLAG | N_FLAG;
3376  setHL(0);
3377  }
3378  setF(f);
3379  return {1, T::CC_ADC_HL_SS};
3380 }
3381 
3382 // DEC ss
3383 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::dec_SS() {
3384  set16<REG>(get16<REG>() - 1); return {1, T::CC_INC_SS + EE};
3385 }
3386 
3387 // INC ss
3388 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::inc_SS() {
3389  set16<REG>(get16<REG>() + 1); return {1, T::CC_INC_SS + EE};
3390 }
3391 
3392 
3393 // BIT n,r
3394 template<class T> template<unsigned N, Reg8 REG> II CPUCore<T>::bit_N_R() {
3395  byte reg = get8<REG>();
3396  byte f = 0; // N_FLAG
3397  if (T::isR800()) {
3398  // this is very different from Z80 (not only XY flags)
3399  f |= getF() & (S_FLAG | V_FLAG | C_FLAG | X_FLAG | Y_FLAG);
3400  f |= H_FLAG;
3401  f |= (reg & (1 << N)) ? 0 : Z_FLAG;
3402  } else {
3403  f |= ZSPHTable[reg & (1 << N)];
3404  f |= getF() & C_FLAG;
3405  f |= reg & (X_FLAG | Y_FLAG);
3406  }
3407  setF(f);
3408  return {1, T::CC_BIT_R};
3409 }
3410 template<class T> template<unsigned N> inline II CPUCore<T>::bit_N_xhl() {
3411  byte m = RDMEM(getHL(), T::CC_BIT_XHL_1) & (1 << N);
3412  byte f = 0; // N_FLAG
3413  if (T::isR800()) {
3414  f |= getF() & (S_FLAG | V_FLAG | C_FLAG | X_FLAG | Y_FLAG);
3415  f |= H_FLAG;
3416  f |= m ? 0 : Z_FLAG;
3417  } else {
3418  f |= ZSPHTable[m];
3419  f |= getF() & C_FLAG;
3420  f |= (T::getMemPtr() >> 8) & (X_FLAG | Y_FLAG);
3421  }
3422  setF(f);
3423  return {1, T::CC_BIT_XHL};
3424 }
3425 template<class T> template<unsigned N> inline II CPUCore<T>::bit_N_xix(unsigned addr) {
3426  T::setMemPtr(addr);
3427  byte m = RDMEM(addr, T::CC_DD + T::CC_BIT_XIX_1) & (1 << N);
3428  byte f = 0; // N_FLAG
3429  if (T::isR800()) {
3430  f |= getF() & (S_FLAG | V_FLAG | C_FLAG | X_FLAG | Y_FLAG);
3431  f |= H_FLAG;
3432  f |= m ? 0 : Z_FLAG;
3433  } else {
3434  f |= ZSPHTable[m];
3435  f |= getF() & C_FLAG;
3436  f |= (addr >> 8) & (X_FLAG | Y_FLAG);
3437  }
3438  setF(f);
3439  return {3, T::CC_DD + T::CC_BIT_XIX};
3440 }
3441 
3442 // RES n,r
3443 static inline byte RES(unsigned b, byte reg) {
3444  return reg & ~(1 << b);
3445 }
3446 template<class T> template<unsigned N, Reg8 REG> II CPUCore<T>::res_N_R() {
3447  set8<REG>(RES(N, get8<REG>())); return {1, T::CC_SET_R};
3448 }
3449 template<class T> template<int EE> inline byte CPUCore<T>::RES_X(unsigned bit, unsigned addr) {
3450  byte res = RES(bit, RDMEM(addr, T::CC_SET_XHL_1 + EE));
3451  WRMEM(addr, res, T::CC_SET_XHL_2 + EE);
3452  return res;
3453 }
3454 template<class T> template<unsigned N> II CPUCore<T>::res_N_xhl() {
3455  RES_X<0>(N, getHL()); return {1, T::CC_SET_XHL};
3456 }
3457 template<class T> template<unsigned N, Reg8 REG> II CPUCore<T>::res_N_xix_R(unsigned a) {
3458  T::setMemPtr(a);
3459  set8<REG>(RES_X<T::CC_DD + T::EE_SET_XIX>(N, a));
3460  return {3, T::CC_DD + T::CC_SET_XIX};
3461 }
3462 
3463 // SET n,r
3464 static inline byte SET(unsigned b, byte reg) {
3465  return reg | (1 << b);
3466 }
3467 template<class T> template<unsigned N, Reg8 REG> II CPUCore<T>::set_N_R() {
3468  set8<REG>(SET(N, get8<REG>())); return {1, T::CC_SET_R};
3469 }
3470 template<class T> template<int EE> inline byte CPUCore<T>::SET_X(unsigned bit, unsigned addr) {
3471  byte res = SET(bit, RDMEM(addr, T::CC_SET_XHL_1 + EE));
3472  WRMEM(addr, res, T::CC_SET_XHL_2 + EE);
3473  return res;
3474 }
3475 template<class T> template<unsigned N> II CPUCore<T>::set_N_xhl() {
3476  SET_X<0>(N, getHL()); return {1, T::CC_SET_XHL};
3477 }
3478 template<class T> template<unsigned N, Reg8 REG> II CPUCore<T>::set_N_xix_R(unsigned a) {
3479  T::setMemPtr(a);
3480  set8<REG>(SET_X<T::CC_DD + T::EE_SET_XIX>(N, a));
3481  return {3, T::CC_DD + T::CC_SET_XIX};
3482 }
3483 
3484 // RL r
3485 template<class T> inline byte CPUCore<T>::RL(byte reg) {
3486  byte c = reg >> 7;
3487  reg = (reg << 1) | ((getF() & C_FLAG) ? 0x01 : 0);
3488  byte f = c ? C_FLAG : 0;
3489  if (T::isR800()) {
3490  f |= ZSPTable[reg];
3491  f |= getF() & (X_FLAG | Y_FLAG);
3492  } else {
3493  f |= ZSPXYTable[reg];
3494  }
3495  setF(f);
3496  return reg;
3497 }
3498 template<class T> template<int EE> inline byte CPUCore<T>::RL_X(unsigned x) {
3499  byte res = RL(RDMEM(x, T::CC_SET_XHL_1 + EE));
3500  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3501  return res;
3502 }
3503 template<class T> template<Reg8 REG> II CPUCore<T>::rl_R() {
3504  set8<REG>(RL(get8<REG>())); return {1, T::CC_SET_R};
3505 }
3506 template<class T> II CPUCore<T>::rl_xhl() {
3507  RL_X<0>(getHL()); return {1, T::CC_SET_XHL};
3508 }
3509 template<class T> template<Reg8 REG> II CPUCore<T>::rl_xix_R(unsigned a) {
3510  T::setMemPtr(a);
3511  set8<REG>(RL_X<T::CC_DD + T::EE_SET_XIX>(a));
3512  return {3, T::CC_DD + T::CC_SET_XIX};
3513 }
3514 
3515 // RLC r
3516 template<class T> inline byte CPUCore<T>::RLC(byte reg) {
3517  byte c = reg >> 7;
3518  reg = (reg << 1) | c;
3519  byte f = c ? C_FLAG : 0;
3520  if (T::isR800()) {
3521  f |= ZSPTable[reg];
3522  f |= getF() & (X_FLAG | Y_FLAG);
3523  } else {
3524  f |= ZSPXYTable[reg];
3525  }
3526  setF(f);
3527  return reg;
3528 }
3529 template<class T> template<int EE> inline byte CPUCore<T>::RLC_X(unsigned x) {
3530  byte res = RLC(RDMEM(x, T::CC_SET_XHL_1 + EE));
3531  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3532  return res;
3533 }
3534 template<class T> template<Reg8 REG> II CPUCore<T>::rlc_R() {
3535  set8<REG>(RLC(get8<REG>())); return {1, T::CC_SET_R};
3536 }
3537 template<class T> II CPUCore<T>::rlc_xhl() {
3538  RLC_X<0>(getHL()); return {1, T::CC_SET_XHL};
3539 }
3540 template<class T> template<Reg8 REG> II CPUCore<T>::rlc_xix_R(unsigned a) {
3541  T::setMemPtr(a);
3542  set8<REG>(RLC_X<T::CC_DD + T::EE_SET_XIX>(a));
3543  return {3, T::CC_DD + T::CC_SET_XIX};
3544 }
3545 
3546 // RR r
3547 template<class T> inline byte CPUCore<T>::RR(byte reg) {
3548  byte c = reg & 1;
3549  reg = (reg >> 1) | ((getF() & C_FLAG) << 7);
3550  byte f = c ? C_FLAG : 0;
3551  if (T::isR800()) {
3552  f |= ZSPTable[reg];
3553  f |= getF() & (X_FLAG | Y_FLAG);
3554  } else {
3555  f |= ZSPXYTable[reg];
3556  }
3557  setF(f);
3558  return reg;
3559 }
3560 template<class T> template<int EE> inline byte CPUCore<T>::RR_X(unsigned x) {
3561  byte res = RR(RDMEM(x, T::CC_SET_XHL_1 + EE));
3562  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3563  return res;
3564 }
3565 template<class T> template<Reg8 REG> II CPUCore<T>::rr_R() {
3566  set8<REG>(RR(get8<REG>())); return {1, T::CC_SET_R};
3567 }
3568 template<class T> II CPUCore<T>::rr_xhl() {
3569  RR_X<0>(getHL()); return {1, T::CC_SET_XHL};
3570 }
3571 template<class T> template<Reg8 REG> II CPUCore<T>::rr_xix_R(unsigned a) {
3572  T::setMemPtr(a);
3573  set8<REG>(RR_X<T::CC_DD + T::EE_SET_XIX>(a));
3574  return {3, T::CC_DD + T::CC_SET_XIX};
3575 }
3576 
3577 // RRC r
3578 template<class T> inline byte CPUCore<T>::RRC(byte reg) {
3579  byte c = reg & 1;
3580  reg = (reg >> 1) | (c << 7);
3581  byte f = c ? C_FLAG : 0;
3582  if (T::isR800()) {
3583  f |= ZSPTable[reg];
3584  f |= getF() & (X_FLAG | Y_FLAG);
3585  } else {
3586  f |= ZSPXYTable[reg];
3587  }
3588  setF(f);
3589  return reg;
3590 }
3591 template<class T> template<int EE> inline byte CPUCore<T>::RRC_X(unsigned x) {
3592  byte res = RRC(RDMEM(x, T::CC_SET_XHL_1 + EE));
3593  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3594  return res;
3595 }
3596 template<class T> template<Reg8 REG> II CPUCore<T>::rrc_R() {
3597  set8<REG>(RRC(get8<REG>())); return {1, T::CC_SET_R};
3598 }
3599 template<class T> II CPUCore<T>::rrc_xhl() {
3600  RRC_X<0>(getHL()); return {1, T::CC_SET_XHL};
3601 }
3602 template<class T> template<Reg8 REG> II CPUCore<T>::rrc_xix_R(unsigned a) {
3603  T::setMemPtr(a);
3604  set8<REG>(RRC_X<T::CC_DD + T::EE_SET_XIX>(a));
3605  return {3, T::CC_DD + T::CC_SET_XIX};
3606 }
3607 
3608 // SLA r
3609 template<class T> inline byte CPUCore<T>::SLA(byte reg) {
3610  byte c = reg >> 7;
3611  reg <<= 1;
3612  byte f = c ? C_FLAG : 0;
3613  if (T::isR800()) {
3614  f |= ZSPTable[reg];
3615  f |= getF() & (X_FLAG | Y_FLAG);
3616  } else {
3617  f |= ZSPXYTable[reg];
3618  }
3619  setF(f);
3620  return reg;
3621 }
3622 template<class T> template<int EE> inline byte CPUCore<T>::SLA_X(unsigned x) {
3623  byte res = SLA(RDMEM(x, T::CC_SET_XHL_1 + EE));
3624  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3625  return res;
3626 }
3627 template<class T> template<Reg8 REG> II CPUCore<T>::sla_R() {
3628  set8<REG>(SLA(get8<REG>())); return {1, T::CC_SET_R};
3629 }
3630 template<class T> II CPUCore<T>::sla_xhl() {
3631  SLA_X<0>(getHL()); return {1, T::CC_SET_XHL};
3632 }
3633 template<class T> template<Reg8 REG> II CPUCore<T>::sla_xix_R(unsigned a) {
3634  T::setMemPtr(a);
3635  set8<REG>(SLA_X<T::CC_DD + T::EE_SET_XIX>(a));
3636  return {3, T::CC_DD + T::CC_SET_XIX};
3637 }
3638 
3639 // SLL r
3640 template<class T> inline byte CPUCore<T>::SLL(byte reg) {
3641  assert(!T::isR800()); // this instruction is Z80-only
3642  byte c = reg >> 7;
3643  reg = (reg << 1) | 1;
3644  byte f = c ? C_FLAG : 0;
3645  f |= ZSPXYTable[reg];
3646  setF(f);
3647  return reg;
3648 }
3649 template<class T> template<int EE> inline byte CPUCore<T>::SLL_X(unsigned x) {
3650  byte res = SLL(RDMEM(x, T::CC_SET_XHL_1 + EE));
3651  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3652  return res;
3653 }
3654 template<class T> template<Reg8 REG> II CPUCore<T>::sll_R() {
3655  set8<REG>(SLL(get8<REG>())); return {1, T::CC_SET_R};
3656 }
3657 template<class T> II CPUCore<T>::sll_xhl() {
3658  SLL_X<0>(getHL()); return {1, T::CC_SET_XHL};
3659 }
3660 template<class T> template<Reg8 REG> II CPUCore<T>::sll_xix_R(unsigned a) {
3661  T::setMemPtr(a);
3662  set8<REG>(SLL_X<T::CC_DD + T::EE_SET_XIX>(a));
3663  return {3, T::CC_DD + T::CC_SET_XIX};
3664 }
3665 template<class T> II CPUCore<T>::sll2() {
3666  assert(T::isR800()); // this instruction is R800-only
3667  byte f = (getF() & (X_FLAG | Y_FLAG)) |
3668  (getA() >> 7) | // C_FLAG
3669  0; // all other flags zero
3670  setF(f);
3671  return {3, T::CC_DD + T::CC_SET_XIX}; // TODO
3672 }
3673 
3674 // SRA r
3675 template<class T> inline byte CPUCore<T>::SRA(byte reg) {
3676  byte c = reg & 1;
3677  reg = (reg >> 1) | (reg & 0x80);
3678  byte f = c ? C_FLAG : 0;
3679  if (T::isR800()) {
3680  f |= ZSPTable[reg];
3681  f |= getF() & (X_FLAG | Y_FLAG);
3682  } else {
3683  f |= ZSPXYTable[reg];
3684  }
3685  setF(f);
3686  return reg;
3687 }
3688 template<class T> template<int EE> inline byte CPUCore<T>::SRA_X(unsigned x) {
3689  byte res = SRA(RDMEM(x, T::CC_SET_XHL_1 + EE));
3690  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3691  return res;
3692 }
3693 template<class T> template<Reg8 REG> II CPUCore<T>::sra_R() {
3694  set8<REG>(SRA(get8<REG>())); return {1, T::CC_SET_R};
3695 }
3696 template<class T> II CPUCore<T>::sra_xhl() {
3697  SRA_X<0>(getHL()); return {1, T::CC_SET_XHL};
3698 }
3699 template<class T> template<Reg8 REG> II CPUCore<T>::sra_xix_R(unsigned a) {
3700  T::setMemPtr(a);
3701  set8<REG>(SRA_X<T::CC_DD + T::EE_SET_XIX>(a));
3702  return {3, T::CC_DD + T::CC_SET_XIX};
3703 }
3704 
3705 // SRL R
3706 template<class T> inline byte CPUCore<T>::SRL(byte reg) {
3707  byte c = reg & 1;
3708  reg >>= 1;
3709  byte f = c ? C_FLAG : 0;
3710  if (T::isR800()) {
3711  f |= ZSPTable[reg];
3712  f |= getF() & (X_FLAG | Y_FLAG);
3713  } else {
3714  f |= ZSPXYTable[reg];
3715  }
3716  setF(f);
3717  return reg;
3718 }
3719 template<class T> template<int EE> inline byte CPUCore<T>::SRL_X(unsigned x) {
3720  byte res = SRL(RDMEM(x, T::CC_SET_XHL_1 + EE));
3721  WRMEM(x, res, T::CC_SET_XHL_2 + EE);
3722  return res;
3723 }
3724 template<class T> template<Reg8 REG> II CPUCore<T>::srl_R() {
3725  set8<REG>(SRL(get8<REG>())); return {1, T::CC_SET_R};
3726 }
3727 template<class T> II CPUCore<T>::srl_xhl() {
3728  SRL_X<0>(getHL()); return {1, T::CC_SET_XHL};
3729 }
3730 template<class T> template<Reg8 REG> II CPUCore<T>::srl_xix_R(unsigned a) {
3731  T::setMemPtr(a);
3732  set8<REG>(SRL_X<T::CC_DD + T::EE_SET_XIX>(a));
3733  return {3, T::CC_DD + T::CC_SET_XIX};
3734 }
3735 
3736 // RLA RLCA RRA RRCA
3737 template<class T> II CPUCore<T>::rla() {
3738  byte c = getF() & C_FLAG;
3739  byte f = (getA() & 0x80) ? C_FLAG : 0;
3740  if (T::isR800()) {
3741  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | X_FLAG | Y_FLAG);
3742  } else {
3743  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG);
3744  }
3745  setA((getA() << 1) | (c ? 1 : 0));
3746  if (!T::isR800()) {
3747  f |= getA() & (X_FLAG | Y_FLAG);
3748  }
3749  setF(f);
3750  return {1, T::CC_RLA};
3751 }
3752 template<class T> II CPUCore<T>::rlca() {
3753  setA((getA() << 1) | (getA() >> 7));
3754  byte f = 0;
3755  if (T::isR800()) {
3756  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | X_FLAG | Y_FLAG);
3757  f |= getA() & C_FLAG;
3758  } else {
3759  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG);
3760  f |= getA() & (Y_FLAG | X_FLAG | C_FLAG);
3761  }
3762  setF(f);
3763  return {1, T::CC_RLA};
3764 }
3765 template<class T> II CPUCore<T>::rra() {
3766  byte c = (getF() & C_FLAG) << 7;
3767  byte f = (getA() & 0x01) ? C_FLAG : 0;
3768  if (T::isR800()) {
3769  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | X_FLAG | Y_FLAG);
3770  } else {
3771  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG);
3772  }
3773  setA((getA() >> 1) | c);
3774  if (!T::isR800()) {
3775  f |= getA() & (X_FLAG | Y_FLAG);
3776  }
3777  setF(f);
3778  return {1, T::CC_RLA};
3779 }
3780 template<class T> II CPUCore<T>::rrca() {
3781  byte f = getA() & C_FLAG;
3782  if (T::isR800()) {
3783  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | X_FLAG | Y_FLAG);
3784  } else {
3785  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG);
3786  }
3787  setA((getA() >> 1) | (getA() << 7));
3788  if (!T::isR800()) {
3789  f |= getA() & (X_FLAG | Y_FLAG);
3790  }
3791  setF(f);
3792  return {1, T::CC_RLA};
3793 }
3794 
3795 
3796 // RLD
3797 template<class T> II CPUCore<T>::rld() {
3798  byte val = RDMEM(getHL(), T::CC_RLD_1);
3799  T::setMemPtr(getHL() + 1);
3800  WRMEM(getHL(), (val << 4) | (getA() & 0x0F), T::CC_RLD_2);
3801  setA((getA() & 0xF0) | (val >> 4));
3802  byte f = 0;
3803  if (T::isR800()) {
3804  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
3805  f |= ZSPTable[getA()];
3806  } else {
3807  f |= getF() & C_FLAG;
3808  f |= ZSPXYTable[getA()];
3809  }
3810  setF(f);
3811  return {1, T::CC_RLD};
3812 }
3813 
3814 // RRD
3815 template<class T> II CPUCore<T>::rrd() {
3816  byte val = RDMEM(getHL(), T::CC_RLD_1);
3817  T::setMemPtr(getHL() + 1);
3818  WRMEM(getHL(), (val >> 4) | (getA() << 4), T::CC_RLD_2);
3819  setA((getA() & 0xF0) | (val & 0x0F));
3820  byte f = 0;
3821  if (T::isR800()) {
3822  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
3823  f |= ZSPTable[getA()];
3824  } else {
3825  f |= getF() & C_FLAG;
3826  f |= ZSPXYTable[getA()];
3827  }
3828  setF(f);
3829  return {1, T::CC_RLD};
3830 }
3831 
3832 
3833 // PUSH ss
3834 template<class T> template<int EE> inline void CPUCore<T>::PUSH(unsigned reg) {
3835  setSP(getSP() - 2);
3836  WR_WORD_rev<true, true>(getSP(), reg, T::CC_PUSH_1 + EE);
3837 }
3838 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::push_SS() {
3839  PUSH<EE>(get16<REG>()); return {1, T::CC_PUSH + EE};
3840 }
3841 
3842 // POP ss
3843 template<class T> template<int EE> inline unsigned CPUCore<T>::POP() {
3844  unsigned addr = getSP();
3845  setSP(addr + 2);
3846  if (T::isR800()) {
3847  // handles both POP and RET instructions (RET with condition = true)
3848  if (EE == 0) { // not reti/retn, not pop ix/iy
3849  setCurrentPopRet();
3850  // No need for setSlowInstructions()
3851  // -> this only matters directly after a CALL
3852  // instruction and in that case we're still
3853  // executing slow instructions.
3854  }
3855  }
3856  return RD_WORD(addr, T::CC_POP_1 + EE);
3857 }
3858 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::pop_SS() {
3859  set16<REG>(POP<EE>()); return {1, T::CC_POP + EE};
3860 }
3861 
3862 
3863 // CALL nn / CALL cc,nn
3864 template<class T> template<typename COND> II CPUCore<T>::call(COND cond) {
3865  unsigned addr = RD_WORD_PC<1>(T::CC_CALL_1);
3866  T::setMemPtr(addr);
3867  if (cond(getF())) {
3868  PUSH<T::EE_CALL>(getPC() + 3);
3869  setPC(addr);
3870  if (T::isR800()) {
3871  setCurrentCall();
3872  setSlowInstructions();
3873  }
3874  return {0/*3*/, T::CC_CALL_A};
3875  } else {
3876  return {3, T::CC_CALL_B};
3877  }
3878 }
3879 
3880 
3881 // RST n
3882 template<class T> template<unsigned ADDR> II CPUCore<T>::rst() {
3883  PUSH<0>(getPC() + 1);
3884  T::setMemPtr(ADDR);
3885  setPC(ADDR);
3886  if (T::isR800()) {
3887  setCurrentCall();
3888  setSlowInstructions();
3889  }
3890  return {0/*1*/, T::CC_RST};
3891 }
3892 
3893 
3894 // RET
3895 template<class T> template<int EE, typename COND> inline II CPUCore<T>::RET(COND cond) {
3896  if (cond(getF())) {
3897  unsigned addr = POP<EE>();
3898  T::setMemPtr(addr);
3899  setPC(addr);
3900  return {0/*1*/, T::CC_RET_A + EE};
3901  } else {
3902  return {1, T::CC_RET_B + EE};
3903  }
3904 }
3905 template<class T> template<typename COND> II CPUCore<T>::ret(COND cond) {
3906  return RET<T::EE_RET_C>(cond);
3907 }
3908 template<class T> II CPUCore<T>::ret() {
3909  return RET<0>(CondTrue());
3910 }
3911 template<class T> II CPUCore<T>::retn() { // also reti
3912  setIFF1(getIFF2());
3913  setSlowInstructions();
3914  return RET<T::EE_RETN>(CondTrue());
3915 }
3916 
3917 
3918 // JP ss
3919 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::jp_SS() {
3920  setPC(get16<REG>()); T::R800ForcePageBreak(); return {0/*1*/, T::CC_JP_HL + EE};
3921 }
3922 
3923 // JP nn / JP cc,nn
3924 template<class T> template<typename COND> II CPUCore<T>::jp(COND cond) {
3925  unsigned addr = RD_WORD_PC<1>(T::CC_JP_1);
3926  T::setMemPtr(addr);
3927  if (cond(getF())) {
3928  setPC(addr);
3929  T::R800ForcePageBreak();
3930  return {0/*3*/, T::CC_JP_A};
3931  } else {
3932  return {3, T::CC_JP_B};
3933  }
3934 }
3935 
3936 // JR e
3937 template<class T> template<typename COND> II CPUCore<T>::jr(COND cond) {
3938  int8_t ofst = RDMEM_OPCODE<1>(T::CC_JR_1);
3939  if (cond(getF())) {
3940  if (((getPC() + 2) & 0xFF) == 0) {
3941  // On R800, when this instruction is located in the
3942  // last two byte of a page (a page is a 256-byte
3943  // (aligned) memory block) and even if we jump back,
3944  // thus fetching the next opcode byte does not cause a
3945  // page-break, there still is one cycle overhead. It's
3946  // as-if there is a page-break.
3947  //
3948  // This could be explained by some (very limited)
3949  // pipeline behaviour in R800: it seems that the
3950  // decision to cause a page-break on the next
3951  // instruction is already made before the jump
3952  // destination address for the current instruction is
3953  // calculated (though a destination address in another
3954  // page is also a reason for a page-break).
3955  //
3956  // It's likely all instructions behave like this, but I
3957  // think we can get away with only explicitly emulating
3958  // this behaviour in the djnz and the jr (conditional
3959  // or not) instructions: all other instructions that
3960  // cause the PC to change in a non-incremental way do
3961  // already force a pagebreak for another reason, so
3962  // this effect is masked. Examples of such instructions
3963  // are: JP, RET, CALL, RST, all repeated block
3964  // instructions, accepting an IRQ, (are there more
3965  // instructions or events that change PC?)
3966  //
3967  // See doc/r800-djnz.txt for more details.
3968  T::R800ForcePageBreak();
3969  }
3970  setPC((getPC() + 2 + ofst) & 0xFFFF);
3971  T::setMemPtr(getPC());
3972  return {0/*2*/, T::CC_JR_A};
3973  } else {
3974  return {2, T::CC_JR_B};
3975  }
3976 }
3977 
3978 // DJNZ e
3979 template<class T> II CPUCore<T>::djnz() {
3980  byte b = getB() - 1;
3981  setB(b);
3982  int8_t ofst = RDMEM_OPCODE<1>(T::CC_JR_1 + T::EE_DJNZ);
3983  if (b) {
3984  if (((getPC() + 2) & 0xFF) == 0) {
3985  // See comment in jr()
3986  T::R800ForcePageBreak();
3987  }
3988  setPC((getPC() + 2 + ofst) & 0xFFFF);
3989  T::setMemPtr(getPC());
3990  return {0/*2*/, T::CC_JR_A + T::EE_DJNZ};
3991  } else {
3992  return {2, T::CC_JR_B + T::EE_DJNZ};
3993  }
3994 }
3995 
3996 // EX (SP),ss
3997 template<class T> template<Reg16 REG, int EE> II CPUCore<T>::ex_xsp_SS() {
3998  unsigned res = RD_WORD_impl<true, false>(getSP(), T::CC_EX_SP_HL_1 + EE);
3999  T::setMemPtr(res);
4000  WR_WORD_rev<false, true>(getSP(), get16<REG>(), T::CC_EX_SP_HL_2 + EE);
4001  set16<REG>(res);
4002  return {1, T::CC_EX_SP_HL + EE};
4003 }
4004 
4005 // IN r,(c)
4006 template<class T> template<Reg8 REG> II CPUCore<T>::in_R_c() {
4007  if (T::isR800()) T::waitForEvenCycle(T::CC_IN_R_C_1);
4008  T::setMemPtr(getBC() + 1);
4009  byte res = READ_PORT(getBC(), T::CC_IN_R_C_1);
4010  byte f = 0;
4011  if (T::isR800()) {
4012  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
4013  f |= ZSPTable[res];
4014  } else {
4015  f |= getF() & C_FLAG;
4016  f |= ZSPXYTable[res];
4017  }
4018  setF(f);
4019  set8<REG>(res);
4020  return {1, T::CC_IN_R_C};
4021 }
4022 
4023 // IN a,(n)
4024 template<class T> II CPUCore<T>::in_a_byte() {
4025  unsigned y = RDMEM_OPCODE<1>(T::CC_IN_A_N_1) + 256 * getA();
4026  T::setMemPtr(y + 1);
4027  if (T::isR800()) T::waitForEvenCycle(T::CC_IN_A_N_2);
4028  setA(READ_PORT(y, T::CC_IN_A_N_2));
4029  return {2, T::CC_IN_A_N};
4030 }
4031 
4032 // OUT (c),r
4033 template<class T> template<Reg8 REG> II CPUCore<T>::out_c_R() {
4034  if (T::isR800()) T::waitForEvenCycle(T::CC_OUT_C_R_1);
4035  T::setMemPtr(getBC() + 1);
4036  WRITE_PORT(getBC(), get8<REG>(), T::CC_OUT_C_R_1);
4037  return {1, T::CC_OUT_C_R};
4038 }
4039 template<class T> II CPUCore<T>::out_c_0() {
4040  // TODO not on R800
4041  if (T::isR800()) T::waitForEvenCycle(T::CC_OUT_C_R_1);
4042  T::setMemPtr(getBC() + 1);
4043  byte out_c_x = isTurboR ? 255 : 0;
4044  WRITE_PORT(getBC(), out_c_x, T::CC_OUT_C_R_1);
4045  return {1, T::CC_OUT_C_R};
4046 }
4047 
4048 // OUT (n),a
4049 template<class T> II CPUCore<T>::out_byte_a() {
4050  byte port = RDMEM_OPCODE<1>(T::CC_OUT_N_A_1);
4051  unsigned y = (getA() << 8) | port;
4052  T::setMemPtr((getA() << 8) | ((port + 1) & 255));
4053  if (T::isR800()) T::waitForEvenCycle(T::CC_OUT_N_A_2);
4054  WRITE_PORT(y, getA(), T::CC_OUT_N_A_2);
4055  return {2, T::CC_OUT_N_A};
4056 }
4057 
4058 
4059 // block CP
4060 template<class T> inline II CPUCore<T>::BLOCK_CP(int increase, bool repeat) {
4061  T::setMemPtr(T::getMemPtr() + increase);
4062  byte val = RDMEM(getHL(), T::CC_CPI_1);
4063  byte res = getA() - val;
4064  setHL(getHL() + increase);
4065  setBC(getBC() - 1);
4066  byte f = ((getA() ^ val ^ res) & H_FLAG) |
4067  ZSTable[res] |
4068  N_FLAG |
4069  (getBC() ? V_FLAG : 0);
4070  if (T::isR800()) {
4071  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
4072  } else {
4073  f |= getF() & C_FLAG;
4074  unsigned k = res - ((f & H_FLAG) >> 4);
4075  f |= (k << 4) & Y_FLAG; // bit 1 -> flag 5
4076  f |= k & X_FLAG; // bit 3 -> flag 3
4077  }
4078  setF(f);
4079  if (repeat && getBC() && res) {
4080  //setPC(getPC() - 2);
4081  T::setMemPtr(getPC() + 1);
4082  return {-1/*1*/, T::CC_CPIR};
4083  } else {
4084  return {1, T::CC_CPI};
4085  }
4086 }
4087 template<class T> II CPUCore<T>::cpd() { return BLOCK_CP(-1, false); }
4088 template<class T> II CPUCore<T>::cpi() { return BLOCK_CP( 1, false); }
4089 template<class T> II CPUCore<T>::cpdr() { return BLOCK_CP(-1, true ); }
4090 template<class T> II CPUCore<T>::cpir() { return BLOCK_CP( 1, true ); }
4091 
4092 
4093 // block LD
4094 template<class T> inline II CPUCore<T>::BLOCK_LD(int increase, bool repeat) {
4095  byte val = RDMEM(getHL(), T::CC_LDI_1);
4096  WRMEM(getDE(), val, T::CC_LDI_2);
4097  setHL(getHL() + increase);
4098  setDE(getDE() + increase);
4099  setBC(getBC() - 1);
4100  byte f = getBC() ? V_FLAG : 0;
4101  if (T::isR800()) {
4102  f |= getF() & (S_FLAG | Z_FLAG | C_FLAG | X_FLAG | Y_FLAG);
4103  } else {
4104  f |= getF() & (S_FLAG | Z_FLAG | C_FLAG);
4105  f |= ((getA() + val) << 4) & Y_FLAG; // bit 1 -> flag 5
4106  f |= (getA() + val) & X_FLAG; // bit 3 -> flag 3
4107  }
4108  setF(f);
4109  if (repeat && getBC()) {
4110  //setPC(getPC() - 2);
4111  T::setMemPtr(getPC() + 1);
4112  return {-1/*1*/, T::CC_LDIR};
4113  } else {
4114  return {1, T::CC_LDI};
4115  }
4116 }
4117 template<class T> II CPUCore<T>::ldd() { return BLOCK_LD(-1, false); }
4118 template<class T> II CPUCore<T>::ldi() { return BLOCK_LD( 1, false); }
4119 template<class T> II CPUCore<T>::lddr() { return BLOCK_LD(-1, true ); }
4120 template<class T> II CPUCore<T>::ldir() { return BLOCK_LD( 1, true ); }
4121 
4122 
4123 // block IN
4124 template<class T> inline II CPUCore<T>::BLOCK_IN(int increase, bool repeat) {
4125  // TODO R800 flags
4126  if (T::isR800()) T::waitForEvenCycle(T::CC_INI_1);
4127  T::setMemPtr(getBC() + increase);
4128  setBC(getBC() - 0x100); // decr before use
4129  byte val = READ_PORT(getBC(), T::CC_INI_1);
4130  WRMEM(getHL(), val, T::CC_INI_2);
4131  setHL(getHL() + increase);
4132  unsigned k = val + ((getC() + increase) & 0xFF);
4133  byte b = getB();
4134  setF(((val & S_FLAG) >> 6) | // N_FLAG
4135  ((k & 0x100) ? (H_FLAG | C_FLAG) : 0) |
4136  ZSXYTable[b] |
4137  (ZSPXYTable[(k & 0x07) ^ b] & P_FLAG));
4138  if (repeat && b) {
4139  //setPC(getPC() - 2);
4140  return {-1/*1*/, T::CC_INIR};
4141  } else {
4142  return {1, T::CC_INI};
4143  }
4144 }
4145 template<class T> II CPUCore<T>::ind() { return BLOCK_IN(-1, false); }
4146 template<class T> II CPUCore<T>::ini() { return BLOCK_IN( 1, false); }
4147 template<class T> II CPUCore<T>::indr() { return BLOCK_IN(-1, true ); }
4148 template<class T> II CPUCore<T>::inir() { return BLOCK_IN( 1, true ); }
4149 
4150 
4151 // block OUT
4152 template<class T> inline II CPUCore<T>::BLOCK_OUT(int increase, bool repeat) {
4153  // TODO R800 flags
4154  byte val = RDMEM(getHL(), T::CC_OUTI_1);
4155  setHL(getHL() + increase);
4156  if (T::isR800()) T::waitForEvenCycle(T::CC_OUTI_2);
4157  WRITE_PORT(getBC(), val, T::CC_OUTI_2);
4158  setBC(getBC() - 0x100); // decr after use
4159  T::setMemPtr(getBC() + increase);
4160  unsigned k = val + getL();
4161  byte b = getB();
4162  setF(((val & S_FLAG) >> 6) | // N_FLAG
4163  ((k & 0x100) ? (H_FLAG | C_FLAG) : 0) |
4164  ZSXYTable[b] |
4165  (ZSPXYTable[(k & 0x07) ^ b] & P_FLAG));
4166  if (repeat && b) {
4167  //setPC(getPC() - 2);
4168  return {-1/*1*/, T::CC_OTIR};
4169  } else {
4170  return {1, T::CC_OUTI};
4171  }
4172 }
4173 template<class T> II CPUCore<T>::outd() { return BLOCK_OUT(-1, false); }
4174 template<class T> II CPUCore<T>::outi() { return BLOCK_OUT( 1, false); }
4175 template<class T> II CPUCore<T>::otdr() { return BLOCK_OUT(-1, true ); }
4176 template<class T> II CPUCore<T>::otir() { return BLOCK_OUT( 1, true ); }
4177 
4178 
4179 // various
4180 template<class T> II CPUCore<T>::nop() { return {1, T::CC_NOP}; }
4181 template<class T> II CPUCore<T>::ccf() {
4182  byte f = 0;
4183  if (T::isR800()) {
4184  // H flag is different from Z80 (and as always XY flags as well)
4185  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | C_FLAG | X_FLAG | Y_FLAG | H_FLAG);
4186  } else {
4187  f |= (getF() & C_FLAG) << 4; // H_FLAG
4188  // only set X(Y) flag (don't reset if already set)
4189  if (isTurboR) {
4190  // Y flag is not changed on a turboR-Z80
4191  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | C_FLAG | Y_FLAG);
4192  f |= (getF() | getA()) & X_FLAG;
4193  } else {
4194  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | C_FLAG);
4195  f |= (getF() | getA()) & (X_FLAG | Y_FLAG);
4196  }
4197  }
4198  f ^= C_FLAG;
4199  setF(f);
4200  return {1, T::CC_CCF};
4201 }
4202 template<class T> II CPUCore<T>::cpl() {
4203  setA(getA() ^ 0xFF);
4204  byte f = H_FLAG | N_FLAG;
4205  if (T::isR800()) {
4206  f |= getF();
4207  } else {
4208  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | C_FLAG);
4209  f |= getA() & (X_FLAG | Y_FLAG);
4210  }
4211  setF(f);
4212  return {1, T::CC_CPL};
4213 }
4214 template<class T> II CPUCore<T>::daa() {
4215  byte a = getA();
4216  byte f = getF();
4217  byte adjust = 0;
4218  if ((f & H_FLAG) || ((getA() & 0xf) > 9)) adjust += 6;
4219  if ((f & C_FLAG) || (getA() > 0x99)) adjust += 0x60;
4220  if (f & N_FLAG) a -= adjust; else a += adjust;
4221  if (T::isR800()) {
4222  f &= C_FLAG | N_FLAG | X_FLAG | Y_FLAG;
4223  f |= ZSPTable[a];
4224  } else {
4225  f &= C_FLAG | N_FLAG;
4226  f |= ZSPXYTable[a];
4227  }
4228  f |= (getA() > 0x99) | ((getA() ^ a) & H_FLAG);
4229  setA(a);
4230  setF(f);
4231  return {1, T::CC_DAA};
4232 }
4233 template<class T> II CPUCore<T>::neg() {
4234  // alternative: LUT word negTable[256]
4235  unsigned a = getA();
4236  unsigned res = -signed(a);
4237  byte f = ((res & 0x100) ? C_FLAG : 0) |
4238  N_FLAG |
4239  ((res ^ a) & H_FLAG) |
4240  ((a & res & 0x80) >> 5); // V_FLAG
4241  if (T::isR800()) {
4242  f |= ZSTable[res & 0xFF];
4243  f |= getF() & (X_FLAG | Y_FLAG);
4244  } else {
4245  f |= ZSXYTable[res & 0xFF];
4246  }
4247  setF(f);
4248  setA(res);
4249  return {1, T::CC_NEG};
4250 }
4251 template<class T> II CPUCore<T>::scf() {
4252  byte f = C_FLAG;
4253  if (T::isR800()) {
4254  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | X_FLAG | Y_FLAG);
4255  } else {
4256  // only set X(Y) flag (don't reset if already set)
4257  if (isTurboR) {
4258  // Y flag is not changed on a turboR-Z80
4259  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG | Y_FLAG);
4260  f |= (getF() | getA()) & X_FLAG;
4261  } else {
4262  f |= getF() & (S_FLAG | Z_FLAG | P_FLAG);
4263  f |= (getF() | getA()) & (X_FLAG | Y_FLAG);
4264  }
4265  }
4266  setF(f);
4267  return {1, T::CC_SCF};
4268 }
4269 
4270 template<class T> II CPUCore<T>::ex_af_af() {
4271  unsigned t = getAF2(); setAF2(getAF()); setAF(t);
4272  return {1, T::CC_EX};
4273 }
4274 template<class T> II CPUCore<T>::ex_de_hl() {
4275  unsigned t = getDE(); setDE(getHL()); setHL(t);
4276  return {1, T::CC_EX};
4277 }
4278 template<class T> II CPUCore<T>::exx() {
4279  unsigned t1 = getBC2(); setBC2(getBC()); setBC(t1);
4280  unsigned t2 = getDE2(); setDE2(getDE()); setDE(t2);
4281  unsigned t3 = getHL2(); setHL2(getHL()); setHL(t3);
4282  return {1, T::CC_EX};
4283 }
4284 
4285 template<class T> II CPUCore<T>::di() {
4286  setIFF1(false);
4287  setIFF2(false);
4288  return {1, T::CC_DI};
4289 }
4290 template<class T> II CPUCore<T>::ei() {
4291  setIFF1(true);
4292  setIFF2(true);
4293  setCurrentEI(); // no ints directly after this instr
4294  setSlowInstructions();
4295  return {1, T::CC_EI};
4296 }
4297 template<class T> II CPUCore<T>::halt() {
4298  setHALT(true);
4299  setSlowInstructions();
4300 
4301  if (!(getIFF1() || getIFF2())) {
4302  diHaltCallback.execute();
4303  }
4304  return {1, T::CC_HALT};
4305 }
4306 template<class T> template<unsigned N> II CPUCore<T>::im_N() {
4307  setIM(N); return {1, T::CC_IM};
4308 }
4309 
4310 // LD A,I/R
4311 template<class T> template<Reg8 REG> II CPUCore<T>::ld_a_IR() {
4312  setA(get8<REG>());
4313  byte f = getIFF2() ? V_FLAG : 0;
4314  if (T::isR800()) {
4315  f |= getF() & (C_FLAG | X_FLAG | Y_FLAG);
4316  f |= ZSTable[getA()];
4317  } else {
4318  f |= getF() & C_FLAG;
4319  f |= ZSXYTable[getA()];
4320  // see comment in the IRQ acceptance part of executeSlow().
4321  setCurrentLDAI(); // only Z80 (not R800) has this quirk
4322  setSlowInstructions();
4323  }
4324  setF(f);
4325  return {1, T::CC_LD_A_I};
4326 }
4327 
4328 // LD I/R,A
4329 template<class T> II CPUCore<T>::ld_r_a() {
4330  // This code sequence:
4331  // XOR A / LD R,A / LD A,R
4332  // gives A=2 for Z80, but A=1 for R800. The difference can possibly be
4333  // explained by a difference in the relative time between writing the
4334  // new value to the R register and increasing the R register per M1
4335  // cycle. Here we implemented the R800 behaviour by storing 'A-1' into
4336  // R, that's good enough for now.
4337  byte val = getA();
4338  if (T::isR800()) val -= 1;
4339  setR(val);
4340  return {1, T::CC_LD_A_I};
4341 }
4342 template<class T> II CPUCore<T>::ld_i_a() {
4343  setI(getA());
4344  return {1, T::CC_LD_A_I};
4345 }
4346 
4347 // MULUB A,r
4348 template<class T> template<Reg8 REG> II CPUCore<T>::mulub_a_R() {
4349  assert(T::isR800()); // this instruction is R800-only
4350  // Verified on real R800:
4351  // YHXN flags are unchanged
4352  // SV flags are reset
4353  // Z flag is set when result is zero
4354  // C flag is set when result doesn't fit in 8-bit
4355  setHL(unsigned(getA()) * get8<REG>());
4356  setF((getF() & (N_FLAG | H_FLAG | X_FLAG | Y_FLAG)) |
4357  0 | // S_FLAG V_FLAG
4358  (getHL() ? 0 : Z_FLAG) |
4359  ((getHL() & 0xFF00) ? C_FLAG : 0));
4360  return {1, T::CC_MULUB};
4361 }
4362 
4363 // MULUW HL,ss
4364 template<class T> template<Reg16 REG> II CPUCore<T>::muluw_hl_SS() {
4365  assert(T::isR800()); // this instruction is R800-only
4366  // Verified on real R800:
4367  // YHXN flags are unchanged
4368  // SV flags are reset
4369  // Z flag is set when result is zero
4370  // C flag is set when result doesn't fit in 16-bit
4371  unsigned res = unsigned(getHL()) * get16<REG>();
4372  setDE(res >> 16);
4373  setHL(res & 0xffff);
4374  setF((getF() & (N_FLAG | H_FLAG | X_FLAG | Y_FLAG)) |
4375  0 | // S_FLAG V_FLAG
4376  (res ? 0 : Z_FLAG) |
4377  ((res & 0xFFFF0000) ? C_FLAG : 0));
4378  return {1, T::CC_MULUW};
4379 }
4380 
4381 
4382 // versions:
4383 // 1 -> initial version
4384 // 2 -> moved memptr from here to Z80TYPE (and not to R800TYPE)
4385 // 3 -> timing of the emulation changed (no changes in serialization)
4386 // 4 -> timing of the emulation changed again (see doc/internal/r800-call.txt)
4387 template<class T> template<typename Archive>
4388 void CPUCore<T>::serialize(Archive& ar, unsigned version)
4389 {
4390  T::serialize(ar, version);
4391  ar.serialize("regs", static_cast<CPURegs&>(*this));
4392  if (ar.versionBelow(version, 2)) {
4393  unsigned mptr = 0; // dummy value (avoid warning)
4394  ar.serialize("memptr", mptr);
4395  T::setMemPtr(mptr);
4396  }
4397 
4398  if (ar.isLoader()) {
4399  invalidateMemCache(0x0000, 0x10000);
4400  }
4401 
4402  // don't serialize
4403  // IRQStatus
4404  // NMIStatus, nmiEdge
4405  // slowInstructions
4406  // exitLoop
4407 
4408  if (T::isR800() && ar.versionBelow(version, 4)) {
4409  motherboard.getMSXCliComm().printWarning(
4410  "Loading an old savestate: the timing of the R800 "
4411  "emulation has changed. This may cause synchronization "
4412  "problems in replay.");
4413  }
4414 }
4415 
4416 // Force template instantiation
4417 template class CPUCore<Z80TYPE>;
4418 template class CPUCore<R800TYPE>;
4419 
4422 
4423 } // namespace openmsx
#define CASE(X)
static const int CLOCK_FREQ
Definition: R800.hh:33
#define NEVER_INLINE
Definition: inline.hh:17
#define ALWAYS_INLINE
Definition: inline.hh:16
#define unlikely(x)
Definition: likely.hh:15
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
unsigned dasm(const MSXCPUInterface &interf, word pc, byte buf[4], std::string &dest, EmuTime::param time)
Disassemble.
Definition: Dasm.cc:18
CPUCore(MSXMotherBoard &motherboard, const std::string &name, const BooleanSetting &traceSetting, TclCallback &diHaltCallback, EmuTime::param time)
Definition: CPUCore.cc:295
bool operator()(byte f) const
Definition: CPUCore.cc:256
bool operator()(byte f) const
Definition: CPUCore.cc:257
#define NEXT
#define NEXT_STOP
int cycles
Definition: CPUCore.hh:40
This class implements a subset of the proposal for std::array_ref (proposed for the next c++ standard...
Definition: array_ref.hh:19
bool operator()(byte) const
Definition: CPUCore.cc:264
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
size_t size() const
bool operator()(byte f) const
Definition: CPUCore.cc:263
static const int CLOCK_FREQ
Definition: Z80.hh:17
size_type size() const
Definition: array_ref.hh:61
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
void addListElement(string_ref element)
Definition: TclObject.cc:69
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
bool operator()(byte f) const
Definition: CPUCore.cc:260
bool operator()(byte f) const
Definition: CPUCore.cc:262
bool operator()(byte f) const
Definition: CPUCore.cc:258
#define NEXT_EI
#define likely(x)
Definition: likely.hh:14
bool operator()(byte f) const
Definition: CPUCore.cc:261
bool operator()(byte f) const
Definition: CPUCore.cc:259
void serialize(Archive &ar, T &t, unsigned version)
bool isMainThread()
Returns true when called from the main thread.
Definition: Thread.cc:16
#define UNREACHABLE
Definition: unreachable.hh:35