Go to the documentation of this file.
18 #ifdef DEBUGGER_ENABLED
42 std::reference_wrapper<Memory::IMemoryBus>
_bus;
98 void _push(uint8_t data);
100 void _push(uint16_t data);
591 CPU(
const CPU &) =
default;
595 ~CPU()
override =
default;
600 unsigned update(
unsigned maxCycle);
609 unsigned runDMA(
unsigned maxCycles);
631 [[nodiscard]] std::string
getName()
const override;
653 #ifdef DEBUGGER_ENABLED
int PHB(uint24_t, AddressingMode)
Push the data bank register to the stack.
Definition: InternalInstruction.cpp:92
Definition: RegisterViewer.hpp:49
int TXA(uint24_t, AddressingMode)
Transfer X to A.
Definition: TransferRegisters.cpp:97
int PLD(uint24_t, AddressingMode)
Pull the direct page register to the stack.
Definition: InternalInstruction.cpp:148
bool _hasIndexCrossedPageBoundary
True if an addressing mode with an iterator (x, y) has crossed the page. (Used because crossing the p...
Definition: CPU.hpp:50
int INX(uint24_t, AddressingMode)
Increment the X register.
Definition: MathematicalOperations.cpp:189
int TCS(uint24_t, AddressingMode)
Transfer 16 bit A to SP.
Definition: TransferRegisters.cpp:61
int CPY(uint24_t valueAddr, AddressingMode)
Compare the Y register with the memory.
Definition: MathematicalOperations.cpp:235
int PHA(uint24_t, AddressingMode)
Push the accumulator to the stack.
Definition: InternalInstruction.cpp:83
uint24_t _getStackRelativeIndirectIndexedYAddr()
The <8-bit exp> is added to S and combined with DBR to form the base address. Y is added to the base ...
Definition: AddressingModes.cpp:188
uint24_t _getDirectIndirectIndexedYLongAddr()
This mode is like the previous addressing mode, but the difference is that rather than pulling 2 byte...
Definition: AddressingModes.cpp:75
int SEC(uint24_t, AddressingMode)
Set the carry Flag.
Definition: InternalInstruction.cpp:9
Component
Definition: Components.hpp:9
uint16_t pc
Definition: Registers.hpp:49
@ Implied
Definition: Instruction.hpp:16
int DEY(uint24_t, AddressingMode)
Decrement the Y register.
Definition: MathematicalOperations.cpp:139
void setBus(Memory::IMemoryBus &bus)
Set the memory bus used by this CPU.
Definition: CPU.cpp:21
The main CPU.
Definition: CPU.hpp:26
int TYA(uint24_t, AddressingMode)
Transfer Y to A.
Definition: TransferRegisters.cpp:113
std::string getName() const override
Get the name of this accessor (used for debug purpose)
Definition: CPU.cpp:355
uint24_t _getImmediateAddr8Bits()
Immediate address mode is specified with a value in 8 bits. (This functions returns the 24bit space a...
Definition: AddressingModes.cpp:10
virtual uint8_t read(uint24_t addr)=0
Read data at a global address. This form allow read to be silenced.
bool _isStopped
If the processor is stopped (using an STP instruction), the clock is stopped and no instruction will ...
Definition: CPU.hpp:37
int BVS(uint24_t valueAddr, AddressingMode)
Branch if Overflow Set.
Definition: InternalInstruction.cpp:290
uint24_t _getDirectIndirectAddr()
2 bytes are pulled from the direct page address to form the 16-bit address. It is combined with DBR t...
Definition: AddressingModes.cpp:165
int MVN(uint24_t, AddressingMode)
Block Move Next. This instruction is special: it takes parameter in the registers.
Definition: TransferRegisters.cpp:155
int RTI(uint24_t, AddressingMode)
Return from Interrupt - Used to return from a interrupt handler.
Definition: Interrupts.cpp:62
int BMI(uint24_t valueAddr, AddressingMode)
Branch if minus.
Definition: InternalInstruction.cpp:254
uint24_t _getDirectIndirectIndexedXAddr()
The direct page address is calculated and added with x. 2 bytes from the dp address combined with DBR...
Definition: AddressingModes.cpp:84
const Instruction instructions[0x100]
All the instructions of the CPU. @info Instructions are indexed by their opcode.
Definition: CPU.hpp:327
int BVC(uint24_t valueAddr, AddressingMode)
Branch if Overflow Clear.
Definition: InternalInstruction.cpp:283
CPU(Memory::IMemoryBus &bus, Cartridge::Header &cartridgeHeader)
Construct a new generic CPU.
Definition: CPU.cpp:13
int ROL(uint24_t, AddressingMode)
Definition: BitsInstructions.cpp:158
@ AbsoluteIndirectIndexedByX
Definition: Instruction.hpp:45
@ DirectPageIndirectIndexedByX
Definition: Instruction.hpp:36
int PEI(uint24_t, AddressingMode)
Push Effective Indirect Address.
Definition: InternalInstruction.cpp:205
int RTL(uint24_t, AddressingMode)
Return from subroutine long.
Definition: InternalInstruction.cpp:320
@ AbsoluteLong
Definition: Instruction.hpp:24
@ StackRelativeIndirectIndexedByY
Definition: Instruction.hpp:41
int CLV(uint24_t, AddressingMode)
Clear the overflow flag.
Definition: InternalInstruction.cpp:45
int MVP(uint24_t, AddressingMode)
Block Move Previous. This instruction is special: it takes parameter in the registers.
Definition: TransferRegisters.cpp:172
@ DirectPageIndirect
Definition: Instruction.hpp:27
bool _isEmulationMode
Is the CPU running in emulation mode (in 8bits)
Definition: CPU.hpp:35
@ AbsoluteIndexedByX
Definition: Instruction.hpp:30
uint24_t _getImmediateAddr16Bits()
Immediate address mode is specified with a value in 16 bits. (This functions returns the 24bit space ...
Definition: AddressingModes.cpp:17
int TSC(uint24_t, AddressingMode)
Transfer DP to 16 bit A.
Definition: TransferRegisters.cpp:77
void write(uint24_t addr, uint8_t data) override
Write data to the internal CPU register.
Definition: CPU.cpp:103
int STX(uint24_t addr, AddressingMode)
Store the index register X to memory.
Definition: MemoryInstructions.cpp:34
int JML(uint24_t valueAddr, AddressingMode)
Long jump.
Definition: InternalInstruction.cpp:303
int NOP(uint24_t, AddressingMode)
No OP.
Definition: InternalInstruction.cpp:309
int ROR(uint24_t, AddressingMode)
Definition: BitsInstructions.cpp:201
int PHD(uint24_t, AddressingMode)
Push the direct page register to the stack.
Definition: InternalInstruction.cpp:98
int ASL(uint24_t, AddressingMode)
Arithmetic Shift Left.
Definition: BitsInstructions.cpp:80
uint24_t _getImmediateAddrForX()
Immediate address mode is specified with a value in 8 or 16 bits. The value is 16 bits if the x flag ...
Definition: AddressingModes.cpp:33
Definition: AddressingModes.cpp:8
Cartridge::Header & _cartridgeHeader
The cartridge header (stored for interrupt vectors..)
Definition: CPU.hpp:44
int WDM(uint24_t, AddressingMode)
WDM Reserved for Future Expansion (used as a code breakpoint)
Definition: InternalInstruction.cpp:333
int EOR(uint24_t, AddressingMode)
XOR, Exclusive OR accumulator with memory.
Definition: MathematicalOperations.cpp:369
Struct containing basic information about instructions.
Definition: Instruction.hpp:51
uint24_t _getAbsoluteIndirectAddr()
2 bytes are pulled from the <abs exp> to form the effective address.
Definition: AddressingModes.cpp:136
int TAY(uint24_t, AddressingMode)
Transfer A to Y.
Definition: TransferRegisters.cpp:24
int INC(uint24_t, AddressingMode)
Increment.
Definition: MathematicalOperations.cpp:289
int STA(uint24_t addr, AddressingMode)
Store the accumulator to memory.
Definition: MemoryInstructions.cpp:9
uint8_t _pop()
Pop 8 bits of data from the stack.
Definition: CPU.cpp:343
unsigned uint24_t
Definition: Ints.hpp:10
@ AbsoluteIndexedByY
Definition: Instruction.hpp:32
int PHK(uint24_t, AddressingMode)
Push the program bank register to the stack.
Definition: InternalInstruction.cpp:104
int SBC(uint24_t valueAddr, AddressingMode)
Subtract with Borrow from Accumulator.
Definition: MathematicalOperations.cpp:52
int DEC(uint24_t, AddressingMode)
Decrement.
Definition: MathematicalOperations.cpp:329
int PLA(uint24_t, AddressingMode)
Pull the accumulator to the stack.
Definition: InternalInstruction.cpp:128
int PLB(uint24_t, AddressingMode)
Pull the data bank register to the stack.
Definition: InternalInstruction.cpp:140
@ ImmediateForX
Definition: Instruction.hpp:21
unsigned update(unsigned maxCycle)
This function continue to execute the Cartridge code.
Definition: CPU.cpp:210
uint24_t _getAbsoluteLongAddr()
The effective address is the expression. (This functions returns the 24bit space address of the value...
Definition: AddressingModes.cpp:56
uint24_t _getAbsoluteIndexedByXAddr()
The absolute expression is added with X and combined with DBR to form the effective address.
Definition: AddressingModes.cpp:108
int ADC(uint24_t valueAddr, AddressingMode)
Add with carry - Adds operand to the Accumulator; adds an additional 1 if carry is set.
Definition: MathematicalOperations.cpp:10
int PHP(uint24_t, AddressingMode)
Push the processor status register to the stack.
Definition: InternalInstruction.cpp:110
int STZ(uint24_t addr, AddressingMode)
Store zero to the memory.
Definition: MemoryInstructions.cpp:56
std::array< DMA, 8 > _dmaChannels
DMA channels witch are mapped to the bus.
Definition: CPU.hpp:47
uint8_t _readPC()
Return the data at the program bank concatenated with the program counter. It also increment the prog...
Definition: CPU.hpp:107
uint24_t _getAbsoluteAddr()
The effective address is formed by DBR:<16-bit exp>. (This functions returns the 24bit space address ...
Definition: AddressingModes.cpp:48
int TSX(uint24_t, AddressingMode)
Transfer SP to X.
Definition: TransferRegisters.cpp:85
int TAX(uint24_t, AddressingMode)
Transfer A to X.
Definition: TransferRegisters.cpp:10
int CLC(uint24_t, AddressingMode)
Clear the carry flag.
Definition: InternalInstruction.cpp:27
int LDY(uint24_t addr, AddressingMode)
Load the Y index register from memory.
Definition: MemoryInstructions.cpp:128
@ ImmediateForA
Definition: Instruction.hpp:20
int BPL(uint24_t valueAddr, AddressingMode)
Branch if plus.
Definition: InternalInstruction.cpp:261
int JSL(uint24_t addr, AddressingMode)
Jump to subroutine (long)
Definition: InternalInstruction.cpp:74
Component getComponent() const override
Get the component of this accessor (used for debug purpose)
Definition: CPU.cpp:360
int BIT(uint24_t, AddressingMode)
Test Memory Bits against Accumulator.
Definition: BitsInstructions.cpp:52
uint24_t _getDirectIndirectIndexedYAddr()
The address is DBR:$(read($($Value + D)) + Y). (This functions returns the 24bit space address of the...
Definition: AddressingModes.cpp:64
int STP(uint24_t, AddressingMode)
Stop the processor.
Definition: InternalInstruction.cpp:327
@ DirectPage
Definition: Instruction.hpp:26
bool _isWaitingForInterrupt
Is the processor waiting for an interrupt (if true, instructions are not run until an interrupt is re...
Definition: CPU.hpp:39
int SED(uint24_t, AddressingMode)
Set the decimal flag.
Definition: InternalInstruction.cpp:15
The memory bus is the component responsible of mapping addresses to components address and transmitti...
Definition: IMemoryBus.hpp:19
@ DirectPageIndirectIndexedByYLong
Definition: Instruction.hpp:38
int TDC(uint24_t, AddressingMode)
Transfer DP to 16 bit A.
Definition: TransferRegisters.cpp:69
@ StackRelative
Definition: Instruction.hpp:40
@ DirectPageIndirectIndexedByY
Definition: Instruction.hpp:37
int LSR(uint24_t, AddressingMode)
Definition: BitsInstructions.cpp:120
int TXY(uint24_t, AddressingMode)
Transfer X to Y.
Definition: TransferRegisters.cpp:129
uint8_t read(uint24_t addr) override
Read from the internal CPU register.
Definition: CPU.cpp:29
std::string getValueName(uint24_t addr) const override
Get the name of the data at the address.
Definition: TransferRegisters.cpp:189
@ DirectPageIndirectLong
Definition: Instruction.hpp:28
int LDX(uint24_t addr, AddressingMode)
Load the X index register from memory.
Definition: MemoryInstructions.cpp:101
void _runInterrupt(uint24_t nativeHandler, uint24_t emulationHandler)
Run an interrupt (save state of the processor and jump to the interrupt handler)
Definition: Interrupts.cpp:26
@ Immediate16bits
Definition: Instruction.hpp:19
@ DirectPageIndexedByX
Definition: Instruction.hpp:34
Struct containing internal registers of the CPU.
Definition: Registers.hpp:115
int BRK(uint24_t, AddressingMode)
Break instruction - Causes a software break. The PC is loaded from a vector table.
Definition: Interrupts.cpp:46
bool IsIRQRequested
Is an interrupt (maskable) requested.
Definition: CPU.hpp:646
uint24_t _getAbsoluteIndirectLongAddr()
3 bytes are pulled from the <abs exp> to form the effective address.
Definition: AddressingModes.cpp:145
int BRA(uint24_t valueAddr, AddressingMode)
Branch always.
Definition: InternalInstruction.cpp:268
A window that show registers and the disassembly of a CPU.
Definition: CPUDebug.hpp:201
int PLX(uint24_t, AddressingMode)
Pull the x index register to the stack.
Definition: InternalInstruction.cpp:166
uint24_t _getDirectIndexedByXAddr()
The DP address is added to X to form the effective address. The effective address is always in bank 0...
Definition: AddressingModes.cpp:94
InternalRegisters _internalRegisters
Internal registers of the CPU (accessible from the bus via addr $4200 to $421F).
Definition: CPU.hpp:32
@ AbsoluteIndexedByXLong
Definition: Instruction.hpp:31
int INY(uint24_t, AddressingMode)
Increment the Y register.
Definition: MathematicalOperations.cpp:202
int CLD(uint24_t, AddressingMode)
Clear the decimal flag.
Definition: InternalInstruction.cpp:39
int PLY(uint24_t, AddressingMode)
Pull the y index register to the stack.
Definition: InternalInstruction.cpp:178
int RESB()
Reset interrupt - Called on boot and when the reset button is pressed.
Definition: Interrupts.cpp:9
uint24_t _getDirectIndexedByYAddr()
The DP address is added to Y to form the effective address. The effective address is always in bank 0...
Definition: AddressingModes.cpp:101
int TYX(uint24_t, AddressingMode)
Transfer Y to X.
Definition: TransferRegisters.cpp:142
uint16_t _pop16()
Pop 16 bits of data from the stack.
Definition: CPU.cpp:348
void _checkInterrupts()
Check if an interrupt is requested and handle it.
Definition: CPU.cpp:232
Abstract class representing a continuous block of memory.
Definition: AMemory.hpp:18
unsigned executeInstruction()
Execute a single instruction.
Definition: CPU.cpp:323
int BNE(uint24_t valueAddr, AddressingMode)
Branch if not equal.
Definition: InternalInstruction.cpp:247
@ Absolute
Definition: Instruction.hpp:23
int RTS(uint24_t, AddressingMode)
Return from subroutine.
Definition: InternalInstruction.cpp:314
int TRB(uint24_t, AddressingMode)
Test and Reset Memory Bits Against Accumulator.
Definition: BitsInstructions.cpp:31
int TSB(uint24_t, AddressingMode)
Test and Set Memory Bits Against Accumulator.
Definition: BitsInstructions.cpp:11
int JSR(uint24_t addr, AddressingMode)
Jump to subroutine.
Definition: InternalInstruction.cpp:67
int PER(uint24_t, AddressingMode)
Push Effective PC Relative Indirect Address.
Definition: InternalInstruction.cpp:190
@ AbsoluteIndirectLong
Definition: Instruction.hpp:47
int XCE(uint24_t, AddressingMode)
Exchange Carry and Emulation Flags.
Definition: InternalInstruction.cpp:211
uint24_t _getImmediateAddrForA()
Immediate address mode is specified with a value in 8 or 16 bits. The value is 16 bits if the m flag ...
Definition: AddressingModes.cpp:24
int XBA(uint24_t, AddressingMode)
Exchange the B and A Accumulators.
Definition: MathematicalOperations.cpp:402
uint24_t _getDirectAddr()
The destination is formed by adding the direct page register with the 8-bit address to form an effect...
Definition: AddressingModes.cpp:42
uint24_t pac
The current Program Address Counter (does not exist in a snes but is useful here).
Definition: Registers.hpp:55
int AND(uint24_t valueAddr, AddressingMode)
And accumulator with memory.
Definition: MathematicalOperations.cpp:255
A callback where you can subscribe to and emit it.
Definition: Callback.hpp:16
int BCS(uint24_t valueAddr, AddressingMode)
Branch if carry set.
Definition: InternalInstruction.cpp:233
bool IsNMIRequested
Is an NMI (non-maskable interrupt) requested.
Definition: CPU.hpp:644
AddressingMode
Different addressing modes that instructions can use for the main CPU.
Definition: Instruction.hpp:15
int BEQ(uint24_t valueAddr, AddressingMode)
Branch if equal.
Definition: InternalInstruction.cpp:240
uint24_t getSize() const override
Get the size of the data. This size can be lower than the mapped data.
Definition: CPU.cpp:205
int REP(uint24_t valueAddr, AddressingMode)
Reset status bits.
Definition: InternalInstruction.cpp:57
bool IsAbortRequested
Is an abort requested.
Definition: CPU.hpp:648
int WAI(uint24_t, AddressingMode)
Wait for Interrupt.
Definition: Interrupts.cpp:72
int CMP(uint24_t, AddressingMode)
Compare Accumulator with Memory.
Definition: MathematicalOperations.cpp:151
int BRL(uint24_t valueAddr, AddressingMode)
Branch always long.
Definition: InternalInstruction.cpp:274
uint24_t _getAbsoluteIndexedByXLongAddr()
The effective address is formed by adding the <long exp> with X.
Definition: AddressingModes.cpp:128
Registers _registers
All the registers of the CPU.
Definition: CPU.hpp:30
Struct containing registers for the main CPU.
Definition: Registers.hpp:13
std::reference_wrapper< Memory::IMemoryBus > _bus
The memory bus to use for read/write.
Definition: CPU.hpp:42
uint24_t _getDirectIndirectLongAddr()
3 bytes are pulled from the direct page address to form an effective address.
Definition: AddressingModes.cpp:174
int DEX(uint24_t, AddressingMode)
Decrement the X register.
Definition: MathematicalOperations.cpp:127
int PLP(uint24_t, AddressingMode)
Pull the processor status register to the stack.
Definition: InternalInstruction.cpp:156
int JMP(uint24_t valueAddr, AddressingMode)
Jump.
Definition: InternalInstruction.cpp:297
void _push(uint8_t data)
Push 8 bits of data to the stack.
Definition: CPU.cpp:332
CPU & operator=(const CPU &)=delete
A CPU is not assignable.
int TXS(uint24_t, AddressingMode)
Transfer X to SP.
Definition: TransferRegisters.cpp:38
int ORA(uint24_t valueAddr, AddressingMode mode)
Or accumulator with memory.
Definition: MathematicalOperations.cpp:94
int PHX(uint24_t, AddressingMode)
Push the x index register to the stack.
Definition: InternalInstruction.cpp:116
int LDA(uint24_t addr, AddressingMode)
Load the accumulator from memory.
Definition: MemoryInstructions.cpp:66
int BCC(uint24_t valueAddr, AddressingMode)
Branch if carry clear.
Definition: InternalInstruction.cpp:226
uint24_t _getAbsoluteIndirectIndexedByXAddr()
The <abs exp> is added with X, then 2 bytes are pulled from that address to form the new location.
Definition: AddressingModes.cpp:155
uint24_t _getAbsoluteIndexedByYAddr()
The absolute expression is added with Y and combined with DBR to form the effective address.
Definition: AddressingModes.cpp:118
int SEP(uint24_t valueAddr, AddressingMode)
Set status bits.
Definition: InternalInstruction.cpp:51
Memory::IMemoryBus & getBus()
Get the memory bus used by this CPU.
Definition: CPU.hpp:317
int SEI(uint24_t, AddressingMode)
Set the Interrupt Disable flag.
Definition: InternalInstruction.cpp:21
@ DirectPageIndexedByY
Definition: Instruction.hpp:35
int PEA(uint24_t, AddressingMode)
Push Effective Absolute Address.
Definition: InternalInstruction.cpp:199
int PHY(uint24_t, AddressingMode)
Push the y index register to the stack.
Definition: InternalInstruction.cpp:122
uint24_t _getStackRelativeAddr()
The stack register is added to the <8-bit exp> to form the effective address.
Definition: AddressingModes.cpp:183
bool isDisabled
True if you want to disable updates of this CPU.
Definition: CPU.hpp:651
@ AbsoluteIndirect
Definition: Instruction.hpp:44
~CPU() override=default
A default destructor.
int CLI(uint24_t, AddressingMode)
Clear the Interrupt Disable flag.
Definition: InternalInstruction.cpp:33
@ Immediate8bits
Definition: Instruction.hpp:18
int COP(uint24_t, AddressingMode)
Co-Processor Enable instruction - Causes a software break. The PC is loaded from a vector table.
Definition: Interrupts.cpp:54
Callback onReset
The callback triggered on reset.
Definition: CPU.hpp:641
unsigned runDMA(unsigned maxCycles)
Run DMA's pending transfers.
Definition: CPU.cpp:252
int CPX(uint24_t valueAddr, AddressingMode)
Compare the X register with the memory.
Definition: MathematicalOperations.cpp:215
uint24_t _getValueAddr(const Instruction &instruction)
Get the parameter address of an instruction from it's addressing mode. @info The current program coun...
Definition: CPU.cpp:264
int STY(uint24_t addr, AddressingMode)
Store the index register Y to memory.
Definition: MemoryInstructions.cpp:45
int TCD(uint24_t, AddressingMode)
Transfer 16 bit A to DP.
Definition: TransferRegisters.cpp:53