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