26 : motherboard(motherboard_)
28 motherboard.getCommandController(),
"cputrace",
29 "CPU tracing on/off", false,
Setting::Save::NO)
31 motherboard.getCommandController(),
"di_halt_callback",
32 "Tcl proc called when the CPU executed a DI/HALT sequence",
33 "default_di_halt_callback",
36 motherboard,
"z80", traceSetting,
37 diHaltCallback, EmuTime::zero()))
38 , r800(motherboard.isTurboR()
40 motherboard,
"r800", traceSetting,
41 diHaltCallback, EmuTime::zero())
43 , timeInfo(motherboard.getMachineInfoCommand())
44 , z80FreqInfo(motherboard.getMachineInfoCommand(),
"z80_freq", *z80)
46 ?
std::make_unique<CPUFreqInfoTopic>(
47 motherboard.getMachineInfoCommand(),
"r800_freq", *r800)
49 , debuggable(motherboard_)
53 traceSetting.
attach(*
this);
55 z80->freqLocked.attach(*
this);
56 z80->freqValue.attach(*
this);
58 r800->freqLocked.attach(*
this);
59 r800->freqValue.attach(*
this);
61 invalidateMemCacheSlot();
66 traceSetting.
detach(*
this);
67 z80->freqLocked.detach(*
this);
68 z80->freqValue.detach(*
this);
70 r800->freqLocked.detach(*
this);
71 r800->freqValue.detach(*
this);
79 interface = interface_;
80 z80 ->setInterface(interface);
81 if (r800) r800->setInterface(interface);
87 if (r800) r800->doReset(time);
99 if (tmp != z80Active) {
108 r800->setDRAMmode(dram);
111void MSXCPU::execute(
bool fastForward)
113 if (z80Active != newZ80Active) {
114 EmuTime time = getCurrentTime();
115 z80Active = newZ80Active;
116 z80Active ? z80 ->warp(time)
119 auto zCache = z80 ->getCacheLines();
120 auto rCache = r800->getCacheLines();
121 auto from = z80Active ? rCache : zCache;
122 auto to = z80Active ? zCache : rCache;
126 z80Active ? z80 ->execute(fastForward)
127 : r800->execute(fastForward);
132 z80Active ? z80 ->exitCPULoopSync()
133 : r800->exitCPULoopSync();
137 z80Active ? z80 ->exitCPULoopAsync()
138 : r800->exitCPULoopAsync();
141EmuTime::param MSXCPU::getCurrentTime()
const
143 return z80Active ? z80 ->getCurrentTime()
144 : r800->getCurrentTime();
149 z80Active ? z80 ->setNextSyncPoint(time)
150 : r800->setNextSyncPoint(time);
153void MSXCPU::invalidateMemCacheSlot()
158 for (
auto i :
xrange(16)) {
166 assert(primarySlot < 4);
167 assert(secondarySlot < 4);
169 byte from = slots[page];
170 byte to = narrow<byte>(4 * primarySlot + secondarySlot);
173 auto [cpuReadLines, cpuWriteLines] = z80Active ? z80->getCacheLines() : r800->getCacheLines();
177 std::copy_n(&cpuReadLines [first], num, &slotReadLines [from][first]);
178 std::copy_n(&slotReadLines [to][first], num, &cpuReadLines [first]);
179 std::copy_n(&cpuWriteLines [first], num, &slotWriteLines[from][first]);
180 std::copy_n(&slotWriteLines[to][first], num, &cpuWriteLines [first]);
182 if (r800) r800->updateVisiblePage(page, primarySlot, secondarySlot);
188 auto [cpuReadLines, cpuWriteLines] = z80Active ? z80->getCacheLines() : r800->getCacheLines();
195 for (
auto i :
xrange(16)) {
201template<
bool READ,
bool WRITE,
bool SUB_START>
202void MSXCPU::setRWCache(
unsigned start,
unsigned size,
const byte* rData,
byte* wData,
int ps,
int ss,
203 std::span<const byte, 256> disallowRead,
204 std::span<const byte, 256> disallowWrite)
206 if constexpr (!SUB_START) {
207 assert(rData ==
nullptr);
208 assert(wData ==
nullptr);
215 int slot = 4 * ps + ss;
216 unsigned page = start >> 14;
217 assert(((start + size - 1) >> 14) == page);
218 if constexpr (SUB_START && READ) rData -= start;
219 if constexpr (SUB_START && WRITE) wData -= start;
222 auto [readLines, writeLines] = [&] {
223 if (slot == slots[page]) {
224 return z80Active ? z80->getCacheLines() : r800->getCacheLines();
226 return CacheLines{slotReadLines [slot],
227 slotWriteLines[slot]};
234 static auto*
const NON_CACHEABLE = std::bit_cast<byte*>(uintptr_t(1));
235 for (
auto i :
xrange(num)) {
236 if constexpr (READ) readLines [first + i] = disallowRead [first + i] ? NON_CACHEABLE : rData;
237 if constexpr (WRITE) writeLines[first + i] = disallowWrite[first + i] ? NON_CACHEABLE : wData;
241static constexpr void extendForAlignment(
unsigned& start,
unsigned& size)
243 constexpr unsigned MASK = ~CacheLine::LOW;
252 std::span<const byte, 256> disallowRead,
253 std::span<const byte, 256> disallowWrite)
256 extendForAlignment(start, size);
257 setRWCache<true, true, false>(start, size,
nullptr,
nullptr, ps, ss, disallowRead, disallowWrite);
260 std::span<const byte, 256> disallowRead,
261 std::span<const byte, 256> disallowWrite)
263 extendForAlignment(start, size);
264 setRWCache<true, false, false>(start, size,
nullptr,
nullptr, ps, ss, disallowRead, disallowWrite);
267 std::span<const byte, 256> disallowRead,
268 std::span<const byte, 256> disallowWrite)
270 extendForAlignment(start, size);
271 setRWCache<false, true, false>(start, size,
nullptr,
nullptr, ps, ss, disallowRead, disallowWrite);
275 std::span<const byte, 256> disallowRead,
276 std::span<const byte, 256> disallowWrite)
278 setRWCache<true, true, true>(start, size, rData, wData, ps, ss, disallowRead, disallowWrite);
281 std::span<const byte, 256> disallowRead,
282 std::span<const byte, 256> disallowWrite)
284 setRWCache<true, false, true>(start, size, rData,
nullptr, ps, ss, disallowRead, disallowWrite);
287 std::span<const byte, 256> disallowRead,
288 std::span<const byte, 256> disallowWrite)
290 setRWCache<false, true, true>(start, size,
nullptr, wData, ps, ss, disallowRead, disallowWrite);
296 if (r800) r800->raiseIRQ();
301 if (r800) r800->lowerIRQ();
306 if (r800) r800->raiseNMI();
311 if (r800) r800->lowerNMI();
316 return z80Active ? z80 ->isM1Cycle(address)
317 : r800->isM1Cycle(address);
327 z80Active ? z80 ->wait(time)
333 return z80Active ? z80 ->waitCycles(time, cycles)
339 return z80Active ? time
340 : r800->waitCycles(time, cycles);
355 if (r800) r800->update(
setting);
362 z80 ->setExtHALT(paused);
363 z80 ->exitCPULoopSync();
365 r800->setExtHALT(paused);
366 r800->exitCPULoopSync();
373MSXCPU::TimeInfoTopic::TimeInfoTopic(
InfoCommand& machineInfoCommand)
378void MSXCPU::TimeInfoTopic::execute(
379 std::span<const TclObject> , TclObject& result)
const
381 const auto& cpu =
OUTER(MSXCPU, timeInfo);
382 EmuDuration dur = cpu.getCurrentTime() - cpu.reference;
383 result = dur.toDouble();
386std::string MSXCPU::TimeInfoTopic::help(std::span<const TclObject> )
const
388 return "Prints the time in seconds that the MSX is powered on\n";
394MSXCPU::CPUFreqInfoTopic::CPUFreqInfoTopic(
395 InfoCommand& machineInfoCommand,
396 const std::string& name_, CPUClock& clock_)
397 : InfoTopic(machineInfoCommand, name_)
402void MSXCPU::CPUFreqInfoTopic::execute(
403 std::span<const TclObject> , TclObject& result)
const
405 result = clock.getFreq();
408std::string MSXCPU::CPUFreqInfoTopic::help(std::span<const TclObject> )
const
410 return "Returns the actual frequency of this CPU.\n"
411 "This frequency can vary because:\n"
412 " - the user has overridden the freq via the '{z80,r800}_freq' setting\n"
413 " - (only on some MSX machines) the MSX software can switch the Z80 between 2 frequencies\n"
414 "See also the '{z80,r800}_freq_locked' setting.\n";
421 "Registers of the active CPU (Z80 or R800).\n"
422 "Each byte in this debuggable represents one 8 bit register:\n"
423 " 0 -> A 1 -> F 2 -> B 3 -> C\n"
424 " 4 -> D 5 -> E 6 -> H 7 -> L\n"
425 " 8 -> A' 9 -> F' 10 -> B' 11 -> C'\n"
426 " 12 -> D' 13 -> E' 14 -> H' 15 -> L'\n"
427 " 16 -> IXH 17 -> IXL 18 -> IYH 19 -> IYL\n"
428 " 20 -> PCH 21 -> PCL 22 -> SPH 23 -> SPL\n"
429 " 24 -> I 25 -> R 26 -> IM 27 -> IFF1/2\n"
430 "The last position (27) contains the IFF1 and IFF2 flags in respectively\n"
431 "bit 0 and 1. Bit 2 contains 'IFF1 AND last-instruction-was-not-EI', so\n"
432 "this effectively indicates that the CPU could accept an interrupt at\n"
433 "the start of the current instruction.\n";
436 : SimpleDebuggable(motherboard_,
"CPU regs", CPU_REGS_DESC, 28)
440byte MSXCPU::Debuggable::read(
unsigned address)
442 auto& cpu =
OUTER(MSXCPU, debuggable);
443 const CPURegs& regs = cpu.getRegisters();
445 case 0:
return regs.getA();
446 case 1:
return regs.getF();
447 case 2:
return regs.getB();
448 case 3:
return regs.getC();
449 case 4:
return regs.getD();
450 case 5:
return regs.getE();
451 case 6:
return regs.getH();
452 case 7:
return regs.getL();
453 case 8:
return regs.getA2();
454 case 9:
return regs.getF2();
455 case 10:
return regs.getB2();
456 case 11:
return regs.getC2();
457 case 12:
return regs.getD2();
458 case 13:
return regs.getE2();
459 case 14:
return regs.getH2();
460 case 15:
return regs.getL2();
461 case 16:
return regs.getIXh();
462 case 17:
return regs.getIXl();
463 case 18:
return regs.getIYh();
464 case 19:
return regs.getIYl();
465 case 20:
return regs.getPCh();
466 case 21:
return regs.getPCl();
467 case 22:
return regs.getSPh();
468 case 23:
return regs.getSPl();
469 case 24:
return regs.getI();
470 case 25:
return regs.getR();
471 case 26:
return regs.getIM();
472 case 27:
return byte(1 * regs.getIFF1() +
474 4 * (regs.getIFF1() && !regs.prevWasEI()));
479void MSXCPU::Debuggable::write(
unsigned address,
byte value)
481 auto& cpu =
OUTER(MSXCPU, debuggable);
482 CPURegs& regs = cpu.getRegisters();
484 case 0: regs.setA(value);
break;
485 case 1: regs.setF(value);
break;
486 case 2: regs.setB(value);
break;
487 case 3: regs.setC(value);
break;
488 case 4: regs.setD(value);
break;
489 case 5: regs.setE(value);
break;
490 case 6: regs.setH(value);
break;
491 case 7: regs.setL(value);
break;
492 case 8: regs.setA2(value);
break;
493 case 9: regs.setF2(value);
break;
494 case 10: regs.setB2(value);
break;
495 case 11: regs.setC2(value);
break;
496 case 12: regs.setD2(value);
break;
497 case 13: regs.setE2(value);
break;
498 case 14: regs.setH2(value);
break;
499 case 15: regs.setL2(value);
break;
500 case 16: regs.setIXh(value);
break;
501 case 17: regs.setIXl(value);
break;
502 case 18: regs.setIYh(value);
break;
503 case 19: regs.setIYl(value);
break;
504 case 20: regs.setPCh(value);
break;
505 case 21: regs.setPCl(value);
break;
506 case 22: regs.setSPh(value);
break;
507 case 23: regs.setSPl(value);
break;
508 case 24: regs.setI(value);
break;
509 case 25: regs.setR(value);
break;
511 if (value < 3) regs.setIM(value);
514 regs.setIFF1((value & 0x01) != 0);
515 regs.setIFF2((value & 0x02) != 0);
525template<
typename Archive>
528 if (ar.versionAtLeast(version, 2)) {
529 ar.serialize(
"z80", *z80);
530 if (r800) ar.serialize(
"r800", *r800);
531 ar.serialize(
"z80Active", z80Active,
532 "newZ80Active", newZ80Active);
535 assert(Archive::IS_LOADER);
537 ar.serializeWithID(
"z80", *z80);
538 if (r800) ar.serializeWithID(
"r800", *r800);
541 ar.serializePointerID(
"activeCPU", activeCPU);
542 ar.serializePointerID(
"newCPU", newCPU);
543 z80Active = activeCPU == z80.get();
545 newZ80Active = newCPU == z80.get();
547 newZ80Active = z80Active;
550 ar.serialize(
"resetTime", reference);
552 if constexpr (Archive::IS_LOADER) {
553 invalidateMemCacheSlot();
void setCPU(MSXCPU *cpu_)
void setZ80Freq(unsigned freq)
Switch the Z80 clock freq.
bool isM1Cycle(unsigned address) const
Should only be used from within a MSXDevice::readMem() method.
void lowerNMI()
This methods lowers the non-maskable interrupt again.
void serialize(Archive &ar, unsigned version)
void invalidateAllSlotsRWCache(word start, unsigned size)
Invalidate the CPU its cache for the interval [start, start + size) For example MSXMemoryMapper and M...
void fillWCache(unsigned start, unsigned size, byte *wData, int ps, int ss, std::span< const byte, 256 > disallowRead, std::span< const byte, 256 > disallowWrite)
void updateVisiblePage(byte page, byte primarySlot, byte secondarySlot)
Inform CPU of bank switch.
void fillRCache(unsigned start, unsigned size, const byte *rData, int ps, int ss, std::span< const byte, 256 > disallowRead, std::span< const byte, 256 > disallowWrite)
void fillRWCache(unsigned start, unsigned size, const byte *rData, byte *wData, int ps, int ss, std::span< const byte, 256 > disallowRead, std::span< const byte, 256 > disallowWrite)
Fill the read and write cache lines for a specific slot with the specified value.
void invalidateRCache(unsigned start, unsigned size, int ps, int ss, std::span< const byte, 256 > disallowRead, std::span< const byte, 256 > disallowWrite)
EmuTime waitCyclesR800(EmuTime::param time, unsigned cycles)
void exitCPULoopSync()
See CPUCore::exitCPULoopSync()
void setNextSyncPoint(EmuTime::param time)
EmuTime waitCyclesZ80(EmuTime::param time, unsigned cycles)
void raiseIRQ()
This method raises a maskable interrupt.
void raiseNMI()
This method raises a non-maskable interrupt.
void wait(EmuTime::param time)
void doReset(EmuTime::param time)
Reset CPU.
void setActiveCPU(Type cpu)
Switch between Z80/R800.
void invalidateRWCache(unsigned start, unsigned size, int ps, int ss, std::span< const byte, 256 > disallowRead, std::span< const byte, 256 > disallowWrite)
Similar to the method above, but only invalidates one specific slot.
void setPaused(bool paused)
(un)pause CPU.
void setInterface(MSXCPUInterface *interface)
void exitCPULoopAsync()
See CPUCore::exitCPULoopAsync()
void setDRAMmode(bool dram)
Sets DRAM or ROM mode (influences memory access speed for R800).
void lowerIRQ()
This methods lowers the maskable interrupt again.
MSXCPU(MSXMotherBoard &motherboard)
void invalidateWCache(unsigned start, unsigned size, int ps, int ss, std::span< const byte, 256 > disallowRead, std::span< const byte, 256 > disallowWrite)
Scheduler & getScheduler()
void setCPU(MSXCPU *cpu_)
void detach(Observer< T > &observer)
void attach(Observer< T > &observer)
This file implemented 3 utility functions:
uint8_t byte
8 bit unsigned integer
uint16_t word
16 bit unsigned integer
constexpr void fill(ForwardRange &&range, const T &value)
constexpr auto copy(InputRange &&range, OutputIter out)
size_t size(std::string_view utf8)
#define OUTER(type, member)
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
constexpr auto xrange(T e)
constexpr auto end(const zstring_view &x)