openMSX
CPUCore.hh
Go to the documentation of this file.
1#ifndef CPUCORE_HH
2#define CPUCORE_HH
3
4#include "CPURegs.hh"
5#include "CacheLine.hh"
6#include "Probe.hh"
7#include "EmuTime.hh"
8#include "BooleanSetting.hh"
9#include "IntegerSetting.hh"
10#include "serialize_meta.hh"
11#include "openmsx.hh"
12#include <array>
13#include <atomic>
14#include <span>
15#include <string>
16
17namespace openmsx {
18
19class MSXCPUInterface;
20class Scheduler;
21class MSXMotherBoard;
22class TclCallback;
23class TclObject;
24class Interpreter;
25enum Reg8 : int;
26enum Reg16 : int;
27
28class CPUBase {}; // only for bw-compat savestates
29
30struct II { // InstructionInfo
31 // Number of instruction byte fetches since the last M1 cycle.
32 // In other words, at the end of an instruction the PC register should
33 // be incremented by this amount.
35
36 // Total duration of the instruction. At the end of the instruction
37 // this value is added to the total cycle counter. For efficiency
38 // reasons we only want to adjust this total once per instruction
39 // instead of small incremental updates after each micro-code.
40 int cycles;
41};
42
43enum class ExecIRQ {
44 NMI, // about to execute NMI routine
45 IRQ, // about to execute normal IRQ routine
46 NONE, // about to execute regular instruction
47};
48
49struct CacheLines {
50 std::span<const byte*, CacheLine::NUM> read;
51 std::span< byte*, CacheLine::NUM> write;
52};
53
54template<typename CPU_POLICY>
55class CPUCore final : public CPUBase, public CPURegs, public CPU_POLICY
56{
57public:
58 CPUCore(MSXMotherBoard& motherboard, const std::string& name,
59 const BooleanSetting& traceSetting,
60 TclCallback& diHaltCallback, EmuTime::param time);
61
62 void setInterface(MSXCPUInterface* interface_) { interface = interface_; }
63
67 void doReset(EmuTime::param time);
68
69 void execute(bool fastForward);
70
76 void exitCPULoopSync();
77
81 void exitCPULoopAsync();
82
83 void warp(EmuTime::param time);
84 [[nodiscard]] EmuTime::param getCurrentTime() const;
85 void wait(EmuTime::param time);
86 EmuTime waitCycles(EmuTime::param time, unsigned cycles);
87 void setNextSyncPoint(EmuTime::param time);
88 [[nodiscard]] CacheLines getCacheLines() {
89 return {readCacheLine, writeCacheLine};
90 }
91 [[nodiscard]] bool isM1Cycle(unsigned address) const;
92
93 void disasmCommand(Interpreter& interp,
94 std::span<const TclObject> tokens,
95 TclObject& result) const;
96
101 void raiseIRQ();
102
107 void lowerIRQ();
108
113 void raiseNMI();
114
119 void lowerNMI();
120
124 void setFreq(unsigned freq);
125
126 [[nodiscard]] BooleanSetting& getFreqLockedSetting() { return freqLocked; }
127 [[nodiscard]] IntegerSetting& getFreqValueSetting() { return freqValue; }
128
129 template<typename Archive>
130 void serialize(Archive& ar, unsigned version);
131
132private:
133 void execute2(bool fastForward);
134 [[nodiscard]] bool needExitCPULoop();
135 void setSlowInstructions();
136 void doSetFreq();
137
138 // Observer<Setting> !! non-virtual !!
139 void update(const Setting& setting) noexcept;
140
141private:
142 // memory cache
143 std::array<const byte*, CacheLine::NUM> readCacheLine;
144 std::array< byte*, CacheLine::NUM> writeCacheLine;
145
146 MSXMotherBoard& motherboard;
147 Scheduler& scheduler;
148 MSXCPUInterface* interface = nullptr;
149
150 const BooleanSetting& traceSetting;
151 TclCallback& diHaltCallback;
152
153 Probe<int> IRQStatus;
154 Probe<void> IRQAccept;
155
156 // dynamic freq
157 BooleanSetting freqLocked;
158 IntegerSetting freqValue;
159 unsigned freq;
160
161 // state machine variables
162 int slowInstructions;
163 int NMIStatus = 0;
164
170 bool nmiEdge = false;
171
172 std::atomic<bool> exitLoop = false;
173
175 bool tracingEnabled;
176
178 const bool isCMOS;
179
180private:
181 inline void cpuTracePre();
182 inline void cpuTracePost();
183 void cpuTracePost_slow();
184
185 inline byte READ_PORT(word port, unsigned cc);
186 inline void WRITE_PORT(word port, byte value, unsigned cc);
187
188 template<bool PRE_PB, bool POST_PB>
189 byte RDMEMslow(unsigned address, unsigned cc);
190 template<bool PRE_PB, bool POST_PB>
191 inline byte RDMEM_impl2(unsigned address, unsigned cc);
192 template<bool PRE_PB, bool POST_PB>
193 inline byte RDMEM_impl (unsigned address, unsigned cc);
194 template<unsigned PC_OFFSET>
195 inline byte RDMEM_OPCODE(unsigned cc);
196 inline byte RDMEM(unsigned address, unsigned cc);
197
198 template<bool PRE_PB, bool POST_PB>
199 word RD_WORD_slow(unsigned address, unsigned cc);
200 template<bool PRE_PB, bool POST_PB>
201 inline word RD_WORD_impl2(unsigned address, unsigned cc);
202 template<bool PRE_PB, bool POST_PB>
203 inline word RD_WORD_impl (unsigned address, unsigned cc);
204 template<unsigned PC_OFFSET>
205 inline word RD_WORD_PC(unsigned cc);
206 inline word RD_WORD(unsigned address, unsigned cc);
207
208 template<bool PRE_PB, bool POST_PB>
209 void WRMEMslow(unsigned address, byte value, unsigned cc);
210 template<bool PRE_PB, bool POST_PB>
211 inline void WRMEM_impl2(unsigned address, byte value, unsigned cc);
212 template<bool PRE_PB, bool POST_PB>
213 inline void WRMEM_impl (unsigned address, byte value, unsigned cc);
214 inline void WRMEM(unsigned address, byte value, unsigned cc);
215
216 void WR_WORD_slow(unsigned address, word value, unsigned cc);
217 inline void WR_WORD(unsigned address, word value, unsigned cc);
218
219 template<bool PRE_PB, bool POST_PB>
220 void WR_WORD_rev_slow(unsigned address, word value, unsigned cc);
221 template<bool PRE_PB, bool POST_PB>
222 inline void WR_WORD_rev2(unsigned address, word value, unsigned cc);
223 template<bool PRE_PB, bool POST_PB>
224 inline void WR_WORD_rev (unsigned address, word value, unsigned cc);
225
226 void executeInstructions();
227 inline void nmi();
228 inline void irq0();
229 inline void irq1();
230 inline void irq2();
231 [[nodiscard]] ExecIRQ getExecIRQ() const;
232 void executeSlow(ExecIRQ execIRQ);
233
234 template<Reg8> [[nodiscard]] inline byte get8() const;
235 template<Reg16> [[nodiscard]] inline word get16() const;
236 template<Reg8> inline void set8 (byte x);
237 template<Reg16> inline void set16(word x);
238
239 template<Reg8 DST, Reg8 SRC, int EE> inline II ld_R_R();
240 template<Reg16 REG, int EE> inline II ld_sp_SS();
241 template<Reg16 REG> inline II ld_SS_a();
242 template<Reg8 SRC> inline II ld_xhl_R();
243 template<Reg16 IXY, Reg8 SRC> inline II ld_xix_R();
244
245 inline II ld_xhl_byte();
246 template<Reg16 IXY> inline II ld_xix_byte();
247
248 template<int EE> inline II WR_NN_Y(word reg);
249 template<Reg16 REG, int EE> inline II ld_xword_SS();
250 template<Reg16 REG> inline II ld_xword_SS_ED();
251 template<Reg16 REG> inline II ld_a_SS();
252
253 inline II ld_xbyte_a();
254 inline II ld_a_xbyte();
255
256 template<Reg8 DST, int EE> inline II ld_R_byte();
257 template<Reg8 DST> inline II ld_R_xhl();
258 template<Reg8 DST, Reg16 IXY> inline II ld_R_xix();
259
260 template<int EE> inline word RD_P_XX();
261 template<Reg16 REG, int EE> inline II ld_SS_xword();
262 template<Reg16 REG> inline II ld_SS_xword_ED();
263
264 template<Reg16 REG, int EE> inline II ld_SS_word();
265
266 inline void ADC(byte reg);
267 inline II adc_a_a();
268 template<Reg8 SRC, int EE> inline II adc_a_R();
269 inline II adc_a_byte();
270 inline II adc_a_xhl();
271 template<Reg16 IXY> inline II adc_a_xix();
272
273 inline void ADD(byte reg);
274 inline II add_a_a();
275 template<Reg8 SRC, int EE> inline II add_a_R();
276 inline II add_a_byte();
277 inline II add_a_xhl();
278 template<Reg16 IXY> inline II add_a_xix();
279
280 inline void AND(byte reg);
281 inline II and_a();
282 template<Reg8 SRC, int EE> inline II and_R();
283 inline II and_byte();
284 inline II and_xhl();
285 template<Reg16 IXY> inline II and_xix();
286
287 inline void CP(byte reg);
288 inline II cp_a();
289 template<Reg8 SRC, int EE> inline II cp_R();
290 inline II cp_byte();
291 inline II cp_xhl();
292 template<Reg16 IXY> inline II cp_xix();
293
294 inline void OR(byte reg);
295 inline II or_a();
296 template<Reg8 SRC, int EE> inline II or_R();
297 inline II or_byte();
298 inline II or_xhl();
299 template<Reg16 IXY> inline II or_xix();
300
301 inline void SBC(byte reg);
302 inline II sbc_a_a();
303 template<Reg8 SRC, int EE> inline II sbc_a_R();
304 inline II sbc_a_byte();
305 inline II sbc_a_xhl();
306 template<Reg16 IXY> inline II sbc_a_xix();
307
308 inline void SUB(byte reg);
309 inline II sub_a();
310 template<Reg8 SRC, int EE> inline II sub_R();
311 inline II sub_byte();
312 inline II sub_xhl();
313 template<Reg16 IXY> inline II sub_xix();
314
315 inline void XOR(byte reg);
316 inline II xor_a();
317 template<Reg8 SRC, int EE> inline II xor_R();
318 inline II xor_byte();
319 inline II xor_xhl();
320 template<Reg16 IXY> inline II xor_xix();
321
322 inline byte DEC(byte reg);
323 template<Reg8 REG, int EE> inline II dec_R();
324 template<int EE> inline void DEC_X(unsigned x);
325 inline II dec_xhl();
326 template<Reg16 IXY> inline II dec_xix();
327
328 inline byte INC(byte reg);
329 template<Reg8 REG, int EE> inline II inc_R();
330 template<int EE> inline void INC_X(unsigned x);
331 inline II inc_xhl();
332 template<Reg16 IXY> inline II inc_xix();
333
334 template<Reg16 REG> inline II adc_hl_SS();
335 inline II adc_hl_hl();
336 template<Reg16 REG1, Reg16 REG2, int EE> inline II add_SS_TT();
337 template<Reg16 REG, int EE> inline II add_SS_SS();
338 template<Reg16 REG> inline II sbc_hl_SS();
339 inline II sbc_hl_hl();
340
341 template<Reg16 REG, int EE> inline II dec_SS();
342 template<Reg16 REG, int EE> inline II inc_SS();
343
344 template<unsigned N, Reg8 REG> inline II bit_N_R();
345 template<unsigned N> inline II bit_N_xhl();
346 template<unsigned N> inline II bit_N_xix(unsigned a);
347
348 template<unsigned N, Reg8 REG> inline II res_N_R();
349 template<int EE> inline byte RES_X(unsigned bit, unsigned addr);
350 template<unsigned N> inline II res_N_xhl();
351 template<unsigned N, Reg8 REG> inline II res_N_xix_R(unsigned a);
352
353 template<unsigned N, Reg8 REG> inline II set_N_R();
354 template<int EE> inline byte SET_X(unsigned bit, unsigned addr);
355 template<unsigned N> inline II set_N_xhl();
356 template<unsigned N, Reg8 REG> inline II set_N_xix_R(unsigned a);
357
358 inline byte RL(byte reg);
359 template<int EE> inline byte RL_X(unsigned x);
360 template<Reg8 REG> inline II rl_R();
361 inline II rl_xhl();
362 template<Reg8 REG> inline II rl_xix_R(unsigned a);
363
364 inline byte RLC(byte reg);
365 template<int EE> inline byte RLC_X(unsigned x);
366 template<Reg8 REG> inline II rlc_R();
367 inline II rlc_xhl();
368 template<Reg8 REG> inline II rlc_xix_R(unsigned a);
369
370 inline byte RR(byte reg);
371 template<int EE> inline byte RR_X(unsigned x);
372 template<Reg8 REG> inline II rr_R();
373 inline II rr_xhl();
374 template<Reg8 REG> inline II rr_xix_R(unsigned a);
375
376 inline byte RRC(byte reg);
377 template<int EE> inline byte RRC_X(unsigned x);
378 template<Reg8 REG> inline II rrc_R();
379 inline II rrc_xhl();
380 template<Reg8 REG> inline II rrc_xix_R(unsigned a);
381
382 inline byte SLA(byte reg);
383 template<int EE> inline byte SLA_X(unsigned x);
384 template<Reg8 REG> inline II sla_R();
385 inline II sla_xhl();
386 template<Reg8 REG> inline II sla_xix_R(unsigned a);
387
388 inline byte SLL(byte reg);
389 template<int EE> inline byte SLL_X(unsigned x);
390 template<Reg8 REG> inline II sll_R();
391 inline II sll_xhl();
392 template<Reg8 REG> inline II sll_xix_R(unsigned a);
393 inline II sll2();
394
395 inline byte SRA(byte reg);
396 template<int EE> inline byte SRA_X(unsigned x);
397 template<Reg8 REG> inline II sra_R();
398 inline II sra_xhl();
399 template<Reg8 REG> inline II sra_xix_R(unsigned a);
400
401 inline byte SRL(byte reg);
402 template<int EE> inline byte SRL_X(unsigned x);
403 template<Reg8 REG> inline II srl_R();
404 inline II srl_xhl();
405 template<Reg8 REG> inline II srl_xix_R(unsigned a);
406
407 inline II rla();
408 inline II rlca();
409 inline II rra();
410 inline II rrca();
411
412 inline II rld();
413 inline II rrd();
414
415 template<int EE> inline void PUSH(word reg);
416 template<Reg16 REG, int EE> inline II push_SS();
417 template<int EE> inline word POP();
418 template<Reg16 REG, int EE> inline II pop_SS();
419
420 template<typename COND> inline II call(COND cond);
421 template<unsigned ADDR> inline II rst();
422
423 template<int EE, typename COND> inline II RET(COND cond);
424 template<typename COND> inline II ret(COND cond);
425 inline II ret();
426 inline II retn();
427
428 template<Reg16 REG, int EE> inline II jp_SS();
429 template<typename COND> inline II jp(COND cond);
430 template<typename COND> inline II jr(COND cond);
431 inline II djnz();
432
433 template<Reg16 REG, int EE> inline II ex_xsp_SS();
434
435 template<Reg8 REG> inline II in_R_c();
436 inline II in_a_byte();
437 template<Reg8 REG> inline II out_c_R();
438 inline II out_c_0();
439 inline II out_byte_a();
440
441 inline II BLOCK_CP(int increase, bool repeat);
442 inline II cpd();
443 inline II cpi();
444 inline II cpdr();
445 inline II cpir();
446
447 inline II BLOCK_LD(int increase, bool repeat);
448 inline II ldd();
449 inline II ldi();
450 inline II lddr();
451 inline II ldir();
452
453 inline II BLOCK_IN(int increase, bool repeat);
454 inline II ind();
455 inline II ini();
456 inline II indr();
457 inline II inir();
458
459 inline II BLOCK_OUT(int increase, bool repeat);
460 inline II outd();
461 inline II outi();
462 inline II otdr();
463 inline II otir();
464
465 template<int EE = 0> inline II nop();
466 inline II ccf();
467 inline II cpl();
468 inline II daa();
469 inline II neg();
470 inline II scf();
471 inline II ex_af_af();
472 inline II ex_de_hl();
473 inline II exx();
474 inline II di();
475 inline II ei();
476 inline II halt();
477 template<unsigned N> inline II im_N();
478
479 template<Reg8 REG> inline II ld_a_IR();
480 inline II ld_r_a();
481 inline II ld_i_a();
482
483 template<Reg8 REG> inline II mulub_a_R();
484 template<Reg16 REG> inline II muluw_hl_SS();
485
486 friend class MSXCPU;
487 friend class Z80TYPE;
488 friend class R800TYPE;
489};
490
491class Z80TYPE;
492class R800TYPE;
494SERIALIZE_CLASS_VERSION(CPUCore<R800TYPE>, 5); // keep these two the same
495
496} // namespace openmsx
497
498#endif
BaseSetting * setting
void setInterface(MSXCPUInterface *interface_)
Definition CPUCore.hh:62
void lowerIRQ()
Lowers the maskable interrupt count.
Definition CPUCore.cc:450
void setNextSyncPoint(EmuTime::param time)
Definition CPUCore.cc:505
void disasmCommand(Interpreter &interp, std::span< const TclObject > tokens, TclObject &result) const
Definition CPUCore.cc:521
void setFreq(unsigned freq)
Change the clock freq.
Definition CPUCore.cc:549
void execute(bool fastForward)
Definition CPUCore.cc:2566
void warp(EmuTime::param time)
Definition CPUCore.cc:332
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
BooleanSetting & getFreqLockedSetting()
Definition CPUCore.hh:126
void serialize(Archive &ar, unsigned version)
Definition CPUCore.cc:4425
void doReset(EmuTime::param time)
Reset the CPU.
Definition CPUCore.cc:343
IntegerSetting & getFreqValueSetting()
Definition CPUCore.hh:127
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
CacheLines getCacheLines()
Definition CPUCore.hh:88
void raiseIRQ()
Raises the maskable interrupt count.
Definition CPUCore.cc:441
A Setting with an integer value.
This file implemented 3 utility functions:
Definition Autofire.cc:11
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
#define SERIALIZE_CLASS_VERSION(CLASS, VERSION)
std::span< const byte *, CacheLine::NUM > read
Definition CPUCore.hh:50
std::span< byte *, CacheLine::NUM > write
Definition CPUCore.hh:51
word length
Definition CPUCore.hh:34
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
Definition xrange.hh:147