|
WolPSX
|
Class to emulate the CPU. More...
#include <cpu.hpp>
Public Member Functions | |
| CPU () | |
| Construct a new CPU object. | |
| void | connectBus (Bus *bus) |
| Connects Bus to the CPU. | |
| void | clock () |
| Clocks the CPU once. | |
| void | show_regs () |
| Shows the values of the registers. | |
| void | reset () |
| Resets the CPU to its initial state. | |
Private Member Functions | |
| void | load_next_ins () |
| Load the next instruction into the instruction register. | |
| void | decode_and_execute () |
| Decodes and executes the instruction in the instruction register. | |
| uint32_t | read32 (uint32_t addr) |
| Read a 32 bit word from the bus. | |
| void | write32 (uint32_t addr, uint32_t data) |
| Write a 32 bit word to the bus. | |
| uint16_t | read16 (uint32_t addr) |
| Read a 16 bit word from the bus. | |
| void | write16 (uint32_t addr, uint16_t data) |
| Write a 16 bit word to the bus. | |
| uint8_t | read8 (uint32_t addr) |
| Read a 8 bit word from the bus. | |
| void | write8 (uint32_t addr, uint8_t data) |
| Write a 8 bit word to the bus. | |
| void | branch (uint32_t offset) |
| Branches to the given offset. | |
| void | set_reg (uint8_t reg, uint32_t data) |
| Sets the value of the given register from the general purpose registers. | |
| uint32_t | get_reg (uint8_t reg) |
| Gets the value of the given register from the general purpose registers. | |
| void | load_regs () |
| Loads the registers from the load queue. | |
| void | conf_ins_lookup () |
| Configures the instruction lookup table. | |
| void | conf_mnemonic_lookup () |
| Configures the mnemonic lookup table. (for debugging) | |
| void | LUI () |
| Load Upper Immediate. | |
| void | ORI () |
| Bitwise OR Immediate. | |
| void | SW () |
| Store Word. | |
| void | ADDIU () |
| Add Immediate Unsigned. | |
| void | J () |
| Unconditional Jump. | |
| void | BNE () |
| Branch on Not Equal. | |
| void | ADDI () |
| Add Immediate. | |
| void | LW () |
| Load Word. | |
| void | SH () |
| Store Halfword. | |
| void | JAL () |
| Jump and Link. | |
| void | ANDI () |
| Bitwise AND Immediate. | |
| void | SB () |
| Store Byte. | |
| void | LB () |
| Load Byte. | |
| void | BEQ () |
| Branch on Equal. | |
| void | BGTZ () |
| Branch on Greater Than Zero. | |
| void | BLEZ () |
| Branch on Less Than or Equal to Zero. | |
| void | LBU () |
| Load Byte Unsigned. | |
| void | SLTI () |
| Set on Less Than Immediate. | |
| void | SLTIU () |
| Set on Less Than Immediate Unsigned. | |
| void | SPECIAL () |
| Looks up and executes the appropriate SPECIAL instruction. | |
| void | SLL () |
| Shift Left Logical. | |
| void | OR () |
| Bitwise OR. | |
| void | SLTU () |
| Set on Less Than Unsigned. | |
| void | ADDU () |
| Add Unsigned. | |
| void | JR () |
| Jump Register. | |
| void | AND () |
| Bitwise AND. | |
| void | ADD () |
| Add. | |
| void | JALR () |
| Jump and Link Register. | |
| void | SRA () |
| Shift Right Arithmetic. | |
| void | SUBU () |
| Subtract Unsigned. | |
| void | DIV () |
| Divide. | |
| void | MFLO () |
| Move From LO. | |
| void | SRL () |
| Shift Right Logical. | |
| void | DIVU () |
| Divide Unsigned. | |
| void | MFHI () |
| Move From HI. | |
| void | SLT () |
| Set on Less Than. | |
| void | BLGE () |
| Choose between BLTZAL, BGEZAL, BLTZ, BGEZ. | |
| void | BLTZ () |
| Branch on Less Than Zero. | |
| void | BGEZ () |
| Branch on Greater Than or Equal to Zero. | |
| void | BGEZAL () |
| Branch on Greater Than or Equal to Zero And Link. | |
| void | BLTZAL () |
| Branch on Less Than Zero And Link. | |
| void | COP0 () |
| Looks up and executes the appropriate coprocessor 1 instruction. | |
| void | MTC0 () |
| Move to Coprocessor 0. | |
| void | MFC0 () |
| Move From Coprocessor 0. | |
| void | COP1 () |
| Looks up and executes the appropriate coprocessor 1 instruction. (UNUSED) | |
| void | COP2 () |
| Looks up and executes the appropriate coprocessor 2 (Graphics) instruction. | |
| void | COP3 () |
| Looks up and executes the appropriate coprocessor 3 instruction. (UNUSED) | |
Private Attributes | |
| Bus * | bus |
| Pointer to the Bus object. | |
| std::queue< RegisterLoad > | load_queue |
| Queue to store the loads to the general purpose registers. | |
| uint32_t | pc = 0 |
| Program counter. | |
| uint32_t | ir |
| Instruction register. | |
| uint32_t | ir_next |
| Instruction immediately after the current instruction in the memory. | |
| Instruction | ins |
| Instruction in the form of the Instruction structure. | |
| uint32_t | regs [32] = {0xdeaddeed} |
| General purpose registers. | |
| uint32_t | hi = 0 |
| HI register. | |
| uint32_t | lo = 0 |
| LO register. | |
| uint32_t | cop0_status |
| COP0 status register. | |
| uint32_t | cop0_bpc |
| COP0 breakpoint exception register. | |
| uint32_t | cop0_bda |
| COP0 breakpoint exception register (data) | |
| uint32_t | cop0_dcic |
| COP0 breakpoint exception register (hardware) | |
| uint32_t | cop0_bdam |
| Bitmask applied to COP0 breakpoint exception register (data) | |
| uint32_t | cop0_bpcm |
| Bitmask applied on COP0 breakpoint exception register. | |
| uint32_t | cop0_cause |
| COP0 cause register. | |
| std::map< uint8_t, void(CPU::*)()> | lookup_op |
| Lookup table for instructions. | |
| std::map< uint8_t, void(CPU::*)()> | lookup_special |
| Lookup table for special instructions (opcode = 0b000000) | |
| std::map< uint8_t, void(CPU::*)()> | lookup_cop0 |
| Lookup table for cop0 instructions (opcode = 0b010000) | |
| std::map< uint8_t, void(CPU::*)()> | lookup_cop2 |
| Lookup table for cop1 instructions (opcode = 0b010001) | |
| std::map< uint8_t, std::string > | lookup_mnemonic_op |
| Lookup table for the mnemonics of instructions. | |
| std::map< uint8_t, std::string > | lookup_mnemonic_special |
| Lookup table for the mnemonics of special instructions. | |
| CPU::CPU | ( | ) |
Construct a new CPU object.
Sets the initial values of the registers and the initializes the opcode lookup tables.
References: reset conf_ins_lookup conf_mnemonic_lookup
|
private |
Add.
Signed addition. TODO: Handle signed overflow exception
| std::runtime_error | if signed overflow occurs. |
References: Instruction::rs Instruction::rt Instruction::rd get_reg set_reg
Definition at line 130 of file ins_special.cpp.
|
private |
Add Immediate.
Signed addition.
| std::runtime_error | if signed overflow occurs. |
References: Instruction::imm Instruction::rs Instruction::rt get_reg set_reg
|
private |
Add Immediate Unsigned.
References: set_reg get_reg Instruction::rt Instruction::rs Instruction::imm
|
private |
Add Unsigned.
References: Instruction::rs Instruction::rt Instruction::rd get_reg set_reg
Definition at line 79 of file ins_special.cpp.
|
private |
Bitwise AND.
References: Instruction::rs Instruction::rt Instruction::rd get_reg set_reg
Definition at line 109 of file ins_special.cpp.
|
private |
Bitwise AND Immediate.
References: Instruction::rs Instruction::rt Instruction::imm get_reg set_reg
|
private |
Branch on Equal.
References: Instruction::imm Instruction::rs Instruction::rt get_reg branch
|
private |
Branch on Greater Than or Equal to Zero.
References: Instruction::imm Instruction::rs get_reg branch
|
private |
Branch on Greater Than or Equal to Zero And Link.
References: Instruction::imm Instruction::rs get_reg branch set_reg
|
private |
Branch on Greater Than Zero.
References: Instruction::imm Instruction::rs get_reg branch
|
private |
Branch on Less Than or Equal to Zero.
References: Instruction::imm Instruction::rs get_reg branch
|
private |
|
private |
Branch on Less Than Zero.
References: Instruction::imm Instruction::rs get_reg branch
|
private |
Branch on Less Than Zero And Link.
References: Instruction::imm Instruction::rs get_reg branch set_reg
|
private |
Branch on Not Equal.
References: Instruction::imm Instruction::rs Instruction::rt get_reg branch
|
private |
Branches to the given offset.
The offset is multiplied by 4 before branching.
| offset | Offset to branch to |
Definition at line 44 of file cpu_utils.cpp.
| void CPU::clock | ( | ) |
Clocks the CPU once.
Executes the load_next_ins and decode_and_execute functions. Implements the load delay by copying the output registers to the input registers after the instruction is executed.
References: load_next_ins decode_and_execute load_regs
|
private |
Configures the instruction lookup table.
Definition at line 36 of file cpu_conf.cpp.
|
private |
Configures the mnemonic lookup table. (for debugging)
Definition at line 90 of file cpu_conf.cpp.
|
inline |
|
private |
Looks up and executes the appropriate coprocessor 1 instruction.
| std::runtime_error | if the instruction is not mapped in the lookup_cop0 table. |
Definition at line 10 of file ins_cop0.cpp.
|
private |
|
private |
Looks up and executes the appropriate coprocessor 2 (Graphics) instruction.
Definition at line 7 of file ins_cop2.cpp.
|
private |
|
private |
Decodes and executes the instruction in the instruction register.
Uses the opcode to lookup the instruction in the opcode lookup table and executes the appropriate function.
| std::runtime_error | if the instruction is not mapped in the opcode lookup table. |
Definition at line 25 of file cpu_utils.cpp.
|
private |
Divide.
| std::runtime_error | if division by zero occurs. |
References: Instruction::rs Instruction::rt get_reg lo hi
Definition at line 220 of file ins_special.cpp.
|
private |
Divide Unsigned.
| std::runtime_error | if division by zero occurs. |
References: Instruction::rs Instruction::rt get_reg lo hi
Definition at line 296 of file ins_special.cpp.
|
private |
Gets the value of the given register from the general purpose registers.
| reg | Register to get the value of |
Definition at line 74 of file cpu_utils.cpp.
|
private |
|
private |
Jump and Link.
Stores the address of the next instruction in register 31 and jumps to the address given by the instruction.
References: Instruction::addr set_reg
|
private |
Jump and Link Register.
Use a register (specified by the instruction in rd) to store the address of the next instruction and jump to the address given by the register specified by the instruction in rs.
References: Instruction::rs Instruction::rd set_reg
Definition at line 160 of file ins_special.cpp.
|
private |
|
private |
Load Byte.
Sign extends the byte read from memory.
References: Instruction::imm Instruction::rs Instruction::rt get_reg read8 RegisterLoad
|
private |
Load Byte Unsigned.
Unsigned load. Zero extends the byte read from memory.
References: Instruction::imm Instruction::rs Instruction::rt get_reg read8 RegisterLoad
|
private |
Load the next instruction into the instruction register.
Executes a read32 at the address given by the program counter and increments the program counter by 4.
Definition at line 10 of file cpu_utils.cpp.
|
private |
Loads the registers from the load queue.
Decrements the delay of each load in the load queue. If the delay is 0, the data is written to the register.
Definition at line 85 of file cpu_utils.cpp.
|
private |
Load Upper Immediate.
References: set_reg Instruction::rt Instruction::imm
|
private |
Load Word.
Sign extends the immediate value read from memory.
References: Instruction::imm Instruction::rs Instruction::rt get_reg read32 RegisterLoad
|
private |
Move From Coprocessor 0.
| std::runtime_error | if an unhandled register is read from. (Not one of the following: status, cause). |
References: Instruction::rd Instruction::rt cop0_status cop0_cause set_reg RegisterLoad
Definition at line 90 of file ins_cop0.cpp.
|
private |
Move From HI.
TODO: Stall until the mul/div is complete.
References: Instruction::rd set_reg hi
Definition at line 326 of file ins_special.cpp.
|
private |
Move From LO.
TODO: Stall until the mul/div is complete.
References: Instruction::rd set_reg lo
Definition at line 262 of file ins_special.cpp.
|
private |
Move to Coprocessor 0.
| std::runtime_error | if nonzero value is written to any register other than status. (TODO: Handle these cases) |
| std::runtime_error | if an unhandled register is written to. (Not one of the following: status, cause, bda, bpcm, bpc, dcic, bdam). |
References: Instruction::rd Instruction::rt cop0_status cop0_cause get_reg set_reg
Definition at line 37 of file ins_cop0.cpp.
|
private |
Bitwise OR.
References: set_reg get_reg Instruction::rd Instruction::rs Instruction::rt
Definition at line 47 of file ins_special.cpp.
|
private |
Bitwise OR Immediate.
References: set_reg get_reg Instruction::rs Instruction::imm
|
private |
Read a 16 bit word from the bus.
| addr | Address to read from |
References: Bus::read16_cpu
Definition at line 41 of file cpu_rw.cpp.
|
private |
Read a 32 bit word from the bus.
| addr | Address to read from |
References: Bus::read32_cpu
Definition at line 13 of file cpu_rw.cpp.
|
private |
Read a 8 bit word from the bus.
| addr | Address to read from |
References Bus::read8_cpu
Definition at line 69 of file cpu_rw.cpp.
| void CPU::reset | ( | ) |
Resets the CPU to its initial state.
Sets all the general purpose registers (except the zero register) to 0xdeadbeef, the HI and LO registers to 0xdeadbeef, the PC to 0xbfc00000, and the coprocessor 0 registers to 0x00000000.
Definition at line 9 of file cpu_conf.cpp.
|
private |
Store Byte.
TODO: Implement Cache
References: Instruction::imm Instruction::rs Instruction::rt get_reg write8 cop0_status
|
private |
Sets the value of the given register from the general purpose registers.
| reg | Register to set |
| data | Value to set the register to |
Definition at line 61 of file cpu_utils.cpp.
|
private |
Store Halfword.
TODO: Implement Cache
References: Instruction::imm Instruction::rs Instruction::rt get_reg write16 cop0_status
| void CPU::show_regs | ( | ) |
Shows the values of the registers.
Used for debugging.
Definition at line 106 of file cpu_utils.cpp.
|
private |
Shift Left Logical.
References: set_reg Instruction::rd Instruction::rt Instruction::shamt
Definition at line 32 of file ins_special.cpp.
|
private |
Set on Less Than.
References: Instruction::rs Instruction::rt Instruction::rd get_reg set_reg
Definition at line 342 of file ins_special.cpp.
|
private |
Set on Less Than Immediate.
References: Instruction::rs Instruction::rt Instruction::imm get_reg set_reg
|
private |
Set on Less Than Immediate Unsigned.
References: Instruction::rs Instruction::rt Instruction::imm get_reg set_reg
|
private |
Set on Less Than Unsigned.
References: Instruction::rs Instruction::rt Instruction::rd get_reg set_reg
Definition at line 63 of file ins_special.cpp.
|
private |
Looks up and executes the appropriate SPECIAL instruction.
| std::runtime_error | if the instruction is not mapped in the lookup_special table. |
Definition at line 10 of file ins_special.cpp.
|
private |
Shift Right Arithmetic.
References: Instruction::rt Instruction::shamt get_reg set_reg
Definition at line 178 of file ins_special.cpp.
|
private |
Shift Right Logical.
References: Instruction::rt Instruction::shamt get_reg set_reg
Definition at line 277 of file ins_special.cpp.
|
private |
Subtract Unsigned.
References: Instruction::rs Instruction::rt Instruction::rd get_reg set_reg
Definition at line 202 of file ins_special.cpp.
|
private |
Store Word.
TODO: Implement Cache
References: set_reg get_reg write_32 Instruction::rt Instruction::rs cop0_status
|
private |
Write a 16 bit word to the bus.
| addr | Address to write to |
| data | Data to write to the bus |
References Bus::write16_cpu
Definition at line 55 of file cpu_rw.cpp.
|
private |
Write a 32 bit word to the bus.
| addr | Address to write to |
| data | Data to write to the bus |
References: Bus::write32_cpu
Definition at line 27 of file cpu_rw.cpp.
|
private |
Write a 8 bit word to the bus.
| addr | Address to write to |
| data | Data to write to the bus |
References Bus::write8_cpu
Definition at line 83 of file cpu_rw.cpp.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
Instruction in the form of the Instruction structure.
|
private |
Instruction register.
|
private |
Instruction immediately after the current instruction in the memory.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |