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