Skip to content

Commit

Permalink
Merge pull request #3 from binary1230/sync-w-upstream-5-26-21
Browse files Browse the repository at this point in the history
Sync w upstream devinacker
  • Loading branch information
binary1230 committed Jul 22, 2021
2 parents f09a660 + 7bf5c84 commit c4b01b4
Show file tree
Hide file tree
Showing 46 changed files with 930 additions and 169 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
*.rar
*/out
*/bin
.idea
45 changes: 45 additions & 0 deletions bsnes/data/default.sgb.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; default registers used for Super Game Boy

[labels]
00:FF00 GB.JOYP
00:FF01 GB.SB
00:FF02 GB.SC
00:FF04 GB.DIV
00:FF05 GB.TIMA
00:FF06 GB.TMA
00:FF07 GB.TAC
00:FF0F GB.IF
00:FF10 GB.NR10
00:FF11 GB.NR11
00:FF12 GB.NR12
00:FF13 GB.NR13
00:FF14 GB.NR14
00:FF16 GB.NR21
00:FF17 GB.NR22
00:FF18 GB.NR23
00:FF19 GB.NR24
00:FF1A GB.NR30
00:FF1B GB.NR31
00:FF1C GB.NR32
00:FF1D GB.NR33
00:FF1E GB.NR34
00:FF20 GB.NR41
00:FF21 GB.NR42
00:FF22 GB.NR43
00:FF23 GB.NR44
00:FF24 GB.NR50
00:FF25 GB.NR51
00:FF26 GB.NR52
00:FF40 GB.LCDC
00:FF41 GB.STAT
00:FF42 GB.SCY
00:FF43 GB.SCX
00:FF44 GB.LY
00:FF45 GB.LYC
00:FF46 GB.DMA
00:FF47 GB.BGP
00:FF48 GB.OBP0
00:FF49 GB.OBP1
00:FF4A GB.WY
00:FF4B GB.WX
00:FFFF GB.IE
75 changes: 28 additions & 47 deletions bsnes/snes/chip/supergameboy/debugger/debugger.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#ifdef SUPERGAMEBOY_CPP

#include <nall/snes/sgb.hpp>
using namespace nall;

#include "disassembler.cpp"

SGBDebugger::SGBDebugger() {
usage = new uint8_t[1 << 16](); // TODO
cart_usage = 0;
usage_ = new uint8_t[1 << 24]();
cart_usage = 0; // TODO

opcode_pc = 0;
}

SGBDebugger::~SGBDebugger() {
delete[] usage;
delete[] usage_;
// delete[] cart_usage;
}

Expand All @@ -23,9 +26,11 @@ void SGBDebugger::init() {
sgb_set_reg = sym("sgb_set_reg");
sgb_get_flag = sym("sgb_get_flag");
sgb_set_flag = sym("sgb_set_flag");


sgb_addr_with_bank = sym("sgb_addr_with_bank");

// set up debugger callbacks
function<void (void(*)(uint16_t))> stepcb;
function<void (void(*)(uint32_t))> stepcb;
stepcb = sym("sgb_callback_step");
if (stepcb) stepcb(op_step);
stepcb = sym("sgb_callback_call");
Expand All @@ -35,7 +40,7 @@ void SGBDebugger::init() {
stepcb = sym("sgb_callback_irq");
if (stepcb) stepcb(op_irq);

function<void (void(*)(uint16_t, uint8_t))> memcb;
function<void (void(*)(uint32_t, uint8_t))> memcb;
memcb = sym("sgb_callback_read");
if (memcb) memcb(op_read);
memcb = sym("sgb_callback_readpc");
Expand Down Expand Up @@ -120,13 +125,18 @@ void SGBDebugger::write_gb(uint16_t addr, uint8_t data) {
if (sgb_write_gb) sgb_write_gb(addr, data);
}

void SGBDebugger::op_call(uint16_t addr) {
uint8_t& SGBDebugger::usage(uint16_t addr) {
if (sgb_addr_with_bank) return usage_[sgb_addr_with_bank(addr)];
return usage_[addr];
}

void SGBDebugger::op_call(uint32_t addr) {
if (debugger.step_sgb) {
debugger.call_count++;
}
}

void SGBDebugger::op_irq(uint16_t addr) {
void SGBDebugger::op_irq(uint32_t addr) {
if (debugger.step_sgb) {
debugger.call_count++;

Expand All @@ -137,14 +147,14 @@ void SGBDebugger::op_irq(uint16_t addr) {
}
}

void SGBDebugger::op_ret(uint16_t addr) {
void SGBDebugger::op_ret(uint32_t addr) {
if (debugger.step_sgb) {
debugger.call_count--;
}
}

void SGBDebugger::op_step(uint16_t pc) {
supergameboy.usage[pc] |= UsageOpcode;
void SGBDebugger::op_step(uint32_t pc) {
supergameboy.usage_[pc] |= UsageOpcode;
supergameboy.opcode_pc = pc;

if(debugger.step_sgb &&
Expand All @@ -169,48 +179,19 @@ void SGBDebugger::op_step(uint16_t pc) {
}
}

void SGBDebugger::op_read(uint16_t addr, uint8_t data) {
supergameboy.usage[addr] |= UsageRead;
void SGBDebugger::op_read(uint32_t addr, uint8_t data) {
supergameboy.usage_[addr] |= UsageRead;
debugger.breakpoint_test(Debugger::Breakpoint::Source::SGBBus, Debugger::Breakpoint::Mode::Read, addr, data);
}

void SGBDebugger::op_readpc(uint16_t pc, uint8_t data) {
supergameboy.usage[pc] |= UsageExec;
void SGBDebugger::op_readpc(uint32_t pc, uint8_t data) {
supergameboy.usage_[pc] |= UsageExec;
}

void SGBDebugger::op_write(uint16_t addr, uint8_t data) {
void SGBDebugger::op_write(uint32_t addr, uint8_t data) {
debugger.breakpoint_test(Debugger::Breakpoint::Source::SGBBus, Debugger::Breakpoint::Mode::Write, addr, data);
supergameboy.usage[addr] |= UsageWrite;
supergameboy.usage[addr] &= ~UsageExec;
}

void SGBDebugger::disassemble_opcode(char *output, uint16_t addr) {
char t[256];
char *s = output;

if (!sgb_read_gb) return;

sprintf(s, "..%.4x ", addr);

uint8 op = sgb_read_gb(addr);
uint8 op0 = sgb_read_gb(addr + 1);
uint8 op1 = sgb_read_gb(addr + 2);

sprintf(t, "%-23s ", nall::GBCPU::disassemble(addr, op, op0, op1)());
strcat(s, t);

uint16_t af = getRegister(RegisterAF);
uint16_t bc = getRegister(RegisterBC);
uint16_t de = getRegister(RegisterDE);
uint16_t hl = getRegister(RegisterHL);
uint16_t sp = getRegister(RegisterSP);
sprintf(t, "AF:%.4x BC:%.4x DE:%.4x HL:%.4x SP:%.4x ", af, bc, de, hl, sp);
strcat(s, t);

sprintf(t, "%c%c%c%c ",
(af & 0x80) ? 'Z' : '.', (af & 0x40) ? 'N' : '.',
(af & 0x20) ? 'H' : '.', (af & 0x10) ? 'C' : '.');
strcat(s, t);
supergameboy.usage_[addr] |= UsageWrite;
supergameboy.usage_[addr] &= ~UsageExec;
}

#endif
30 changes: 18 additions & 12 deletions bsnes/snes/chip/supergameboy/debugger/debugger.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class SGBDebugger : public SuperGameBoy, public ChipDebugger {
public:

#include "disassembler.hpp"

bool property(unsigned id, string &name, string &value) {
return false; // no properties for now
}
Expand Down Expand Up @@ -29,19 +32,20 @@ class SGBDebugger : public SuperGameBoy, public ChipDebugger {
uint8_t read_gb(uint16_t addr);
void write_gb(uint16_t addr, uint8_t data);

void disassemble_opcode(char *output, uint16_t addr);

enum Usage {
UsageRead = 0x80,
UsageWrite = 0x40,
UsageExec = 0x20,
UsageOpcode = 0x10,
};
uint8 *usage; // currently unused
uint8 *cart_usage;

uint8_t& usage(uint16_t addr);

uint8 *usage_;
uint8 *cart_usage; // currently unused

function<void ()> step_event;
uint16_t opcode_pc;
uint32_t opcode_pc;

SGBDebugger();
~SGBDebugger();
Expand All @@ -50,17 +54,19 @@ class SGBDebugger : public SuperGameBoy, public ChipDebugger {
function<uint8_t (uint16_t)> sgb_read_gb;
function<void (uint16_t, uint8_t)> sgb_write_gb;

function<uint32_t (uint16_t)> sgb_addr_with_bank;

function<uint16_t (char)> sgb_get_reg;
function<void (char, uint16_t)> sgb_set_reg;
function<bool (char)> sgb_get_flag;
function<void (char, bool)> sgb_set_flag;

static void op_call(uint16_t addr);
static void op_irq(uint16_t addr);
static void op_ret(uint16_t addr);
static void op_step(uint16_t pc);
static void op_call(uint32_t addr);
static void op_irq(uint32_t addr);
static void op_ret(uint32_t addr);
static void op_step(uint32_t pc);

static void op_read(uint16_t addr, uint8_t data);
static void op_readpc(uint16_t pc, uint8_t data);
static void op_write(uint16_t addr, uint8_t data);
static void op_read(uint32_t addr, uint8_t data);
static void op_readpc(uint32_t pc, uint8_t data);
static void op_write(uint32_t addr, uint8_t data);
};
125 changes: 125 additions & 0 deletions bsnes/snes/chip/supergameboy/debugger/disassembler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifdef SUPERGAMEBOY_CPP

uint16 SGBDebugger::relb(int8 offset, int op_len, uint16 pc) {
return pc + op_len + offset;
}

uint8 SGBDebugger::dreadb(uint16 addr) {
SNES::debugger.bus_access = true;
return supergameboy.read_gb(addr);
SNES::debugger.bus_access = false;
}

uint16 SGBDebugger::dreadw(uint16 addr) {
uint16 r;
r = dreadb(addr + 0) << 0;
r |= dreadb(addr + 1) << 8;
return r;
}

uint24 SGBDebugger::decode(uint8 offset_type, uint16 addr, uint16 pc) {
uint24 r = 0;
uint16 dp = 0xff00;

switch(offset_type) {
case GBCPU::Addr8Ptr:
r = dp | (uint8)addr;
break;
case GBCPU::Addr16:
case GBCPU::Addr16Ptr:
r = addr;
break;
case GBCPU::RegBCPtr:
r = getRegister(RegisterBC);
break;
case GBCPU::RegCPtr:
r = dp | (uint8)getRegister(RegisterBC);
break;
case GBCPU::RegDEPtr:
r = getRegister(RegisterDE);
break;
case GBCPU::RegHLPtr:
case GBCPU::RegHLPtrInc:
case GBCPU::RegHLPtrDec:
case GBCPU::PrefixCB:
r = getRegister(RegisterHL);
break;
case GBCPU::PCRelative:
r = relb((int8)addr, 2, pc);
break;
case GBCPU::SPRelative:
r = relb((int8)addr, 2, getRegister(RegisterSP));
break;
case GBCPU::RST:
r = dreadb(pc) & 0x38;
break;
}

if (r > 0x4000 && sgb_addr_with_bank) {
r = sgb_addr_with_bank(r);
}
return r;
}

void SGBDebugger::disassemble_opcode_ex(SGBDebugger::Opcode &opcode, uint24 addr) {
uint8 param[3];

SNES::debugger.bus_access = true;
param[0] = read_gb(addr + 0);
param[1] = read_gb(addr + 1);
param[2] = read_gb(addr + 2);
SNES::debugger.bus_access = false;

const GBCPU::OpcodeInfo& op = gbOpcodeInfo[param[0]];
opcode.set(0, op.mode0, op.mode1, op.name, param, GBCPU::getOpcodeLength(param[0]) - 1);

switch (param[0]) {
case 0x18: case 0xc3: case 0xe9:
opcode.flags |= Opcode::FLAG_BRA; break;
case 0x20: case 0x28: case 0x30: case 0x38:
case 0xc2: case 0xc4: case 0xc7: case 0xcc:
case 0xcd: case 0xcf: case 0xd4: case 0xd7:
case 0xdc: case 0xdf: case 0xe7: case 0xef:
case 0xf7: case 0xff:
opcode.flags |= Opcode::FLAG_BRA_CONTINUE; break;
case 0xc9: case 0xd9:
opcode.flags |= Opcode::FLAG_RETURN; break;
case 0xf1:
opcode.flags |= Opcode::FLAG_POP_F; break;
case 0xf5:
opcode.flags |= Opcode::FLAG_PUSH_F; break;
}

if (GBCPU::getOpcodeIndirect(param[0], param[1])) {
opcode.flags |= Opcode::FLAG_INDIRECT;
}
}

void SGBDebugger::disassemble_opcode(char *output, uint24 addr) {
char t[256];
char *s = output;

sprintf(s, "%.6x ", addr);

uint8 op = read_gb(addr);
uint8 op0 = read_gb(addr + 1);
uint8 op1 = read_gb(addr + 2);

sprintf(t, "%-23s ", nall::GBCPU::disassemble((uint16)addr, op, op0, op1)());
strcat(s, t);

uint16_t af = getRegister(RegisterAF);
uint16_t bc = getRegister(RegisterBC);
uint16_t de = getRegister(RegisterDE);
uint16_t hl = getRegister(RegisterHL);
uint16_t sp = getRegister(RegisterSP);
sprintf(t, "AF:%.4x BC:%.4x DE:%.4x HL:%.4x SP:%.4x ", af, bc, de, hl, sp);
strcat(s, t);

sprintf(t, "%c%c%c%c ",
(af & 0x80) ? 'Z' : '.', (af & 0x40) ? 'N' : '.',
(af & 0x20) ? 'H' : '.', (af & 0x10) ? 'C' : '.');
strcat(s, t);
}

#endif
Loading

0 comments on commit c4b01b4

Please sign in to comment.