openMSX
MSXCPU.hh
Go to the documentation of this file.
1 #ifndef MSXCPU_HH
2 #define MSXCPU_HH
3 
4 #include "InfoTopic.hh"
5 #include "SimpleDebuggable.hh"
6 #include "Observer.hh"
7 #include "BooleanSetting.hh"
8 #include "CacheLine.hh"
9 #include "EmuTime.hh"
10 #include "TclCallback.hh"
11 #include "serialize_meta.hh"
12 #include "openmsx.hh"
13 #include "span.hh"
14 #include <memory>
15 
16 namespace openmsx {
17 
18 class MSXMotherBoard;
19 class MSXCPUInterface;
20 class CPUClock;
21 class CPURegs;
22 class Z80TYPE;
23 class R800TYPE;
24 template <typename T> class CPUCore;
25 class TclObject;
26 class Interpreter;
27 
28 class MSXCPU final : private Observer<Setting>
29 {
30 public:
32 
33  explicit MSXCPU(MSXMotherBoard& motherboard);
34  ~MSXCPU();
35 
40  void doReset(EmuTime::param time);
41 
43  void setActiveCPU(CPUType cpu);
44 
46  void setDRAMmode(bool dram);
47 
50  void updateVisiblePage(byte page, byte primarySlot, byte secondarySlot);
51 
55  void invalidateAllSlotsRWCache(word start, unsigned size);
56 
62  void invalidateRWCache(unsigned start, unsigned size, int ps, int ss,
63  const byte* disallowRead, const byte* disallowWrite);
64  void invalidateRCache (unsigned start, unsigned size, int ps, int ss,
65  const byte* disallowRead);
66  void invalidateWCache (unsigned start, unsigned size, int ps, int ss,
67  const byte* disallowWrite);
68 
78  void fillRWCache(unsigned start, unsigned size, const byte* rData, byte* wData, int ps, int ss,
79  const byte* disallowRead, const byte* disallowWrite);
80  void fillRCache (unsigned start, unsigned size, const byte* rData, int ps, int ss,
81  const byte* disallowRead);
82  void fillWCache (unsigned start, unsigned size, byte* wData, int ps, int ss,
83  const byte* disallowWrite);
84 
90  void raiseIRQ();
91 
96  void lowerIRQ();
97 
103  void raiseNMI();
104 
109  void lowerNMI();
110 
116  bool isM1Cycle(unsigned address) const;
117 
119  void exitCPULoopSync();
121  void exitCPULoopAsync();
122 
124  bool isR800Active() const { return !z80Active; }
125 
127  void setZ80Freq(unsigned freq);
128 
129  void setInterface(MSXCPUInterface* interf);
130 
131  void disasmCommand(Interpreter& interp,
132  span<const TclObject> tokens,
133  TclObject& result) const;
134 
137  void setPaused(bool paused);
138 
139  void setNextSyncPoint(EmuTime::param time);
140 
141  void wait(EmuTime::param time);
142  EmuTime waitCyclesZ80(EmuTime::param time, unsigned cycles);
143  EmuTime waitCyclesR800(EmuTime::param time, unsigned cycles);
144 
146 
147  template<typename Archive>
148  void serialize(Archive& ar, unsigned version);
149 
150 private:
151  void invalidateMemCacheSlot();
152 
153  // only for MSXMotherBoard
154  void execute(bool fastForward);
155  friend class MSXMotherBoard;
156 
162  EmuTime::param getCurrentTime() const;
163 
164  // Observer<Setting>
165  void update(const Setting& setting) override;
166 
167  template<bool READ, bool WRITE, bool SUB_START>
168  void setRWCache(unsigned start, unsigned size, const byte* rData, byte* wData, int ps, int ss,
169  const byte* disallowRead, const byte* disallowWrite);
170 
171 private:
172  MSXMotherBoard& motherboard;
173  BooleanSetting traceSetting;
174  TclCallback diHaltCallback;
175  const std::unique_ptr<CPUCore<Z80TYPE>> z80;
176  const std::unique_ptr<CPUCore<R800TYPE>> r800; // can be nullptr
177 
178  const byte* slotReadLines [16][CacheLine::NUM];
179  byte* slotWriteLines[16][CacheLine::NUM];
180  byte slots[4]; // active slot for page (= 4 * primSlot + secSlot)
181 
182  struct TimeInfoTopic final : InfoTopic {
183  explicit TimeInfoTopic(InfoCommand& machineInfoCommand);
184  void execute(span<const TclObject> tokens,
185  TclObject& result) const override;
186  std::string help (const std::vector<std::string>& tokens) const override;
187  } timeInfo;
188 
189  class CPUFreqInfoTopic final : public InfoTopic {
190  public:
191  CPUFreqInfoTopic(InfoCommand& machineInfoCommand,
192  const std::string& name, CPUClock& clock);
193  void execute(span<const TclObject> tokens,
194  TclObject& result) const override;
195  std::string help (const std::vector<std::string>& tokens) const override;
196  private:
197  CPUClock& clock;
198  };
199  CPUFreqInfoTopic z80FreqInfo; // always present
200  const std::unique_ptr<CPUFreqInfoTopic> r800FreqInfo; // can be nullptr
201 
202  struct Debuggable final : SimpleDebuggable {
203  explicit Debuggable(MSXMotherBoard& motherboard);
204  byte read(unsigned address) override;
205  void write(unsigned address, byte value) override;
206  } debuggable;
207 
208  EmuTime reference;
209  bool z80Active;
210  bool newZ80Active;
211 
212  MSXCPUInterface* interface = nullptr; // only used for debug
213 };
215 
216 } // namespace openmsx
217 
218 #endif
openmsx.hh
openmsx::MSXCPU::waitCyclesR800
EmuTime waitCyclesR800(EmuTime::param time, unsigned cycles)
Definition: MSXCPU.cc:319
openmsx::CPURegs
CPURegs
Definition: CPURegs.cc:50
openmsx::CPUClock
CPUClock
Definition: CPUClock.cc:26
openmsx::MSXCPU::exitCPULoopAsync
void exitCPULoopAsync()
See CPUCore::exitCPULoopAsync()
Definition: MSXCPU.cc:135
openmsx::MSXCPU::MSXCPU
MSXCPU(MSXMotherBoard &motherboard)
Definition: MSXCPU.cc:23
openmsx::InfoTopic
Definition: InfoTopic.hh:15
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:227
serialize_meta.hh
openmsx::MSXCPU::invalidateRWCache
void invalidateRWCache(unsigned start, unsigned size, int ps, int ss, const byte *disallowRead, const byte *disallowWrite)
Similar to the method above, but only invalidates one specific slot.
Definition: MSXCPU.cc:243
openmsx::MSXCPU::setPaused
void setPaused(bool paused)
(un)pause CPU.
Definition: MSXCPU.cc:351
openmsx::MSXCPU::setInterface
void setInterface(MSXCPUInterface *interf)
Definition: MSXCPU.cc:77
openmsx::MSXCPU::waitCyclesZ80
EmuTime waitCyclesZ80(EmuTime::param time, unsigned cycles)
Definition: MSXCPU.cc:313
openmsx::MSXCPU::fillRWCache
void fillRWCache(unsigned start, unsigned size, const byte *rData, byte *wData, int ps, int ss, const byte *disallowRead, const byte *disallowWrite)
Fill the read and write cache lines for a specific slot with the specified value.
Definition: MSXCPU.cc:259
openmsx::MSXCPU::lowerNMI
void lowerNMI()
This methods lowers the non-maskable interrupt again.
Definition: MSXCPU.cc:290
openmsx::MSXCPU::CPUType
CPUType
Definition: MSXCPU.hh:31
openmsx::MSXCPU::wait
void wait(EmuTime::param time)
Definition: MSXCPU.cc:307
openmsx::MSXCPU::doReset
void doReset(EmuTime::param time)
Reset CPU.
Definition: MSXCPU.cc:84
openmsx::MSXCPU
MSXCPU
Definition: MSXCPU.cc:549
openmsx::Setting
Definition: Setting.hh:119
BooleanSetting.hh
openmsx::MSXCPUInterface
Definition: MSXCPUInterface.hh:63
openmsx::MSXCPU::invalidateWCache
void invalidateWCache(unsigned start, unsigned size, int ps, int ss, const byte *disallowWrite)
Definition: MSXCPU.cc:253
span
Definition: span.hh:34
openmsx::SERIALIZE_CLASS_VERSION
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
openmsx::MSXCPU::isM1Cycle
bool isM1Cycle(unsigned address) const
Should only be used from within a MSXDevice::readMem() method.
Definition: MSXCPU.cc:296
openmsx::CacheLine::NUM
constexpr unsigned NUM
Definition: CacheLine.hh:8
Observer.hh
openmsx::MSXCPU::invalidateAllSlotsRWCache
void invalidateAllSlotsRWCache(word start, unsigned size)
Invalidate the CPU its cache for the interval [start, start + size) For example MSXMemoryMapper and M...
Definition: MSXCPU.cc:182
openmsx::MSXCPU::CPU_Z80
Definition: MSXCPU.hh:31
openmsx::InfoCommand
Definition: InfoCommand.hh:11
openmsx::MSXMotherBoard
Definition: MSXMotherBoard.hh:59
openmsx::MSXCPU
Definition: MSXCPU.hh:28
openmsx::MSXCPU::CPU_R800
Definition: MSXCPU.hh:31
openmsx::MSXCPU::raiseNMI
void raiseNMI()
This method raises a non-maskable interrupt.
Definition: MSXCPU.cc:285
openmsx::MSXCPU::~MSXCPU
~MSXCPU()
Definition: MSXCPU.cc:64
openmsx::MSXCPU::setActiveCPU
void setActiveCPU(CPUType cpu)
Switch between Z80/R800.
Definition: MSXCPU.cc:94
openmsx::MSXCPU::invalidateRCache
void invalidateRCache(unsigned start, unsigned size, int ps, int ss, const byte *disallowRead)
Definition: MSXCPU.cc:248
CacheLine.hh
openmsx::MSXCPU::getRegisters
CPURegs & getRegisters()
Definition: MSXCPU.cc:325
SimpleDebuggable.hh
openmsx::MSXCPU::lowerIRQ
void lowerIRQ()
This methods lowers the maskable interrupt again.
Definition: MSXCPU.cc:280
span.hh
openmsx::MSXCPUInterface
MSXCPUInterface
Definition: MSXCPUInterface.cc:1274
TclCallback.hh
openmsx::MSXCPU::updateVisiblePage
void updateVisiblePage(byte page, byte primarySlot, byte secondarySlot)
Inform CPU of bank switch.
Definition: MSXCPU.cc:164
openmsx::MSXCPU::raiseIRQ
void raiseIRQ()
This method raises a maskable interrupt.
Definition: MSXCPU.cc:275
EmuTime.hh
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::MSXCPU::setZ80Freq
void setZ80Freq(unsigned freq)
Switch the Z80 clock freq.
Definition: MSXCPU.cc:302
openmsx::MSXCPU::isR800Active
bool isR800Active() const
Is the R800 currently active?
Definition: MSXCPU.hh:124
openmsx::BooleanSetting
Definition: BooleanSetting.hh:8
openmsx::MSXCPU::setDRAMmode
void setDRAMmode(bool dram)
Sets DRAM or ROM mode (influences memory access speed for R800).
Definition: MSXCPU.cc:105
InfoTopic.hh
openmsx::TclObject
Definition: TclObject.hh:21
openmsx::MSXCPU::fillWCache
void fillWCache(unsigned start, unsigned size, byte *wData, int ps, int ss, const byte *disallowWrite)
Definition: MSXCPU.cc:269
openmsx::MSXCPU::exitCPULoopSync
void exitCPULoopSync()
See CPUCore::exitCPULoopsync()
Definition: MSXCPU.cc:130
openmsx::CPURegs
Definition: CPURegs.hh:19
openmsx::MSXCPU::MSXMotherBoard
friend class MSXMotherBoard
Definition: MSXCPU.hh:155
openmsx::Interpreter
Definition: Interpreter.hh:16
openmsx::MSXCPU::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXCPU.cc:518
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::MSXCPU::disasmCommand
void disasmCommand(Interpreter &interp, span< const TclObject > tokens, TclObject &result) const
Definition: MSXCPU.cc:343
openmsx::MSXCPU::setNextSyncPoint
void setNextSyncPoint(EmuTime::param time)
Definition: MSXCPU.cc:147
openmsx::Observer
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
openmsx::CPUClock
Definition: CPUClock.hh:10
openmsx::MSXCPU::fillRCache
void fillRCache(unsigned start, unsigned size, const byte *rData, int ps, int ss, const byte *disallowRead)
Definition: MSXCPU.cc:264
openmsx::TclCallback
Definition: TclCallback.hh:13