Skip to content

Commit

Permalink
cpu: added hardware breakpoints on execution
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Sep 3, 2019
1 parent bb02ebf commit 2b82089
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions src/cpu/cop0.h
Expand Up @@ -32,6 +32,8 @@ struct COP0 {
uint32_t superMasterEnable2 : 1; // bits 24..29
};

inline bool codeBreakpointEnabled() { return masterEnableBreakpoints && superMasterEnable1 && superMasterEnable2 && breakOnCode; }

uint32_t _reg;
DCIC() : _reg(0) {}
};
Expand Down
16 changes: 13 additions & 3 deletions src/cpu/cpu.cpp
Expand Up @@ -54,7 +54,17 @@ void CPU::saveStateForException() {
branchTaken = false;
}

bool CPU::handleBreakpoints() {
void CPU::handleHardwareBreakpoints() {
if (cop0.dcic.codeBreakpointEnabled()) {
if (((PC ^ cop0.bpcm) & cop0.bpc) == 0) {
cop0.dcic.codeBreakpointHit = 1;
cop0.dcic.breakpointHit = 1;
instructions::exception(this, COP0::CAUSE::Exception::breakpoint);
}
}
}

bool CPU::handleSoftwareBreakpoints() {
if (!breakpoints.empty()) {
auto bp = breakpoints.find(PC);
if (bp != breakpoints.end() && bp->second.enabled) {
Expand Down Expand Up @@ -83,10 +93,10 @@ bool CPU::executeInstructions(int count) {
if (maskedPc == 0xa0 || maskedPc == 0xb0 || maskedPc == 0xc0) sys->handleBiosFunction();

saveStateForException();

checkForInterrupts();
handleHardwareBreakpoints();

if (handleBreakpoints()) return false;
if (handleSoftwareBreakpoints()) return false;

_opcode = Opcode(sys->readMemory32(PC));
const auto& op = instructions::OpcodeTable[_opcode.op];
Expand Down
3 changes: 2 additions & 1 deletion src/cpu/cpu.h
Expand Up @@ -96,7 +96,8 @@ struct CPU {
}

void saveStateForException();
bool handleBreakpoints();
void handleHardwareBreakpoints();
bool handleSoftwareBreakpoints();
bool executeInstructions(int count);

struct Breakpoint {
Expand Down
7 changes: 6 additions & 1 deletion src/cpu/instructions.cpp
Expand Up @@ -201,7 +201,12 @@ void exception(CPU *cpu, COP0::CAUSE::Exception cause) {
cpu->cop0.tar = cpu->PC;
}

cpu->setPC(cpu->cop0.status.getHandlerAddress());
uint32_t handlerAddress = cpu->cop0.status.getHandlerAddress();
if (cause == Exception::breakpoint) {
handlerAddress -= 0x40;
}

cpu->setPC(handlerAddress);
}

void dummy(CPU *cpu, Opcode i) {
Expand Down

0 comments on commit 2b82089

Please sign in to comment.