Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BASIC debugger for v2.0 #492

Merged
merged 11 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 66 additions & 24 deletions core/debug/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,73 @@ int debug_get_flags(void) {
}

void debug_open(int reason, uint32_t data) {
if ((cpu_check_signals() & CPU_SIGNAL_EXIT) || debug_is_open() || ((debug_get_flags() & DBG_IGNORE) && (reason >= DBG_BREAKPOINT && reason <= DBG_PORT_WRITE))) {
if ((cpu_check_signals() & CPU_SIGNAL_EXIT) || debug_is_open()) {
return;
}

debug_clear_step();

/* fixup reason for basic debugger */
if (debug.basicMode == true) {
if (debug.basicMode) {
if (reason == DBG_WATCHPOINT_WRITE) {
if (data == DBG_BASIC_CURPC) {
if (data == DBG_BASIC_CURPC+2) {
if (debug.basicDeferPC) {
return;
}
reason = DBG_BASIC_CURPC_WRITE;
}
else if (data == DBG_BASIC_BEGPC) {
} else if (data == DBG_BASIC_BEGPC+2) {
/* in the case where the program is returning from a subprogram, the updates
to the pc haven't finished yet on endpc or program name, so defer to the
program name write */
debug.basicDeferPC = true;
reason = DBG_BASIC_BEGPC_WRITE;
}
else if (data == DBG_BASIC_ENDPC) {
} else if (data == DBG_BASIC_ENDPC+2) {
debug.basicDeferPC = false;
reason = DBG_BASIC_ENDPC_WRITE;
} else if (data == DBG_BASIC_BASIC_PROG+8) {
debug.basicDeferPC = false;
reason = DBG_BASIC_BASIC_PROG_WRITE;
}
}
if (reason == DBG_WATCHPOINT_READ) {
if (data == DBG_BASIC_SYSHOOKFLAG2) {

// verify basic program execution
debug.basicDeferPC = false;
/* verify basic program execution */
if ((mem_peek_byte(DBG_BASIC_NEWDISPF) & DBG_BASIC_PROGEXECUTING_BIT) &&
(mem_peek_byte(DBG_BASIC_CMDFLAGS) & DBG_BASIC_CMDEXEC_BIT)) {

// check current pc for instruction "bit 1,(iy+$36)"
/* check current pc for instruction "bit 1,(iy+$36)" */
static const uint8_t instr[] = { 0xFD, 0xCB, 0x36, 0x4E };
const void *ptr = phys_mem_ptr(cpu.registers.PC - sizeof(instr), sizeof(instr));
if(ptr && !memcmp(ptr, instr, sizeof(instr))) {
debug.basicLastHookPC = cpu.registers.PC;
reason = DBG_BASIC_CURPC_WRITE;
}
} else {
return;
}
}
}
if (debug.stepBasic && (reason == DBG_BASIC_CURPC_WRITE || reason == DBG_BASIC_BASIC_PROG_WRITE)) {
uint32_t offset = mem_peek_long(DBG_BASIC_CURPC) - mem_peek_long(DBG_BASIC_BEGPC);
/* Allow self-looping with Step In, but only if the hook PC is the same as what was stepped from */
bool inRange = offset >= (uint32_t)debug.stepBasicBegin + (!debug.stepBasicNext && debug.basicLastHookPC == debug.stepBasicFromPC) &&
offset <= (uint32_t)debug.stepBasicEnd &&
!strncmp(debug.stepBasicPrgm, phys_mem_ptr(DBG_BASIC_BASIC_PROG, 9), 9);
if (!inRange ^ debug.stepBasicNext) {
reason = DBG_BASIC_STEP;
}
}

if (!debug.basicModeLive && reason > DBG_BASIC_LIVE_START && reason < DBG_BASIC_LIVE_END) {
return;
}
}

if ((debug_get_flags() & DBG_IGNORE) && (reason >= DBG_BREAKPOINT && reason <= DBG_PORT_WRITE)) {
return;
}

debug_clear_step();

debug.cpuCycles = cpu.cycles;
debug.cpuNext = cpu.next;
debug.cpuBaseCycles = cpu.baseCycles;
Expand Down Expand Up @@ -158,13 +186,15 @@ void debug_step(int mode, uint32_t addr) {
gui_debug_close();
debug.tempExec = addr;
break;
case DBG_BASIC_STEP:
case DBG_BASIC_STEP_IN:
case DBG_BASIC_STEP_NEXT:
gui_debug_close();
debug.stepBasic = true;
break;
case DBG_BASIC_STEP_NEXT:
debug.stepBasicNext = true;
debug.stepBasicNextAddr = addr;
debug.stepBasicNext = (mode == DBG_BASIC_STEP_NEXT);
debug.stepBasicFromPC = debug.basicLastHookPC;
debug.stepBasicBegin = addr;
debug.stepBasicEnd = addr >> 16;
debug_get_executing_basic_prgm(debug.stepBasicPrgm);
break;
}
}
Expand All @@ -174,6 +204,11 @@ void debug_clear_step(void) {
debug.tempExec = debug.stepOut = ~0u;
}

void debug_clear_basic_step(void) {
debug.stepBasic = false;
debug.stepBasicNext = false;
}

void debug_inst_start(void) {
uint32_t pc = cpu.registers.PC;
debug.addr[pc] |= DBG_INST_START_MARKER;
Expand Down Expand Up @@ -256,17 +291,24 @@ void debug_set_pc(uint32_t addr) {
/* internal breakpoints not visible in gui */
/* the gui should automatically update breakpoints, so it should be */
/* fine if asm or C also uses these addresses */
void debug_enable_basic_mode(bool fetches) {
debug_watch(DBG_BASIC_BEGPC, DBG_MASK_WRITE, fetches);
debug_watch(DBG_BASIC_CURPC, DBG_MASK_WRITE, fetches);
debug_watch(DBG_BASIC_ENDPC, DBG_MASK_WRITE, fetches);
void debug_enable_basic_mode(bool fetches, bool live) {
debug.basicMode = true;
debug.basicModeLive = live;
debug_watch(DBG_BASIC_BEGPC+2, DBG_MASK_WRITE, fetches);
debug_watch(DBG_BASIC_CURPC+2, DBG_MASK_WRITE, fetches);
//debug_watch(DBG_BASIC_ENDPC+2, DBG_MASK_WRITE, fetches);
debug_watch(DBG_BASIC_BASIC_PROG+8, DBG_MASK_WRITE, fetches);
debug_watch(DBG_BASIC_SYSHOOKFLAG2, DBG_MASK_READ, !fetches);
}

void debug_disable_basic_mode(void) {
debug_watch(DBG_BASIC_BEGPC, DBG_MASK_WRITE, false);
debug_watch(DBG_BASIC_CURPC, DBG_MASK_WRITE, false);
debug_watch(DBG_BASIC_ENDPC, DBG_MASK_WRITE, false);
debug.basicMode = false;
debug.basicModeLive = false;
debug_clear_basic_step();
debug_watch(DBG_BASIC_BEGPC+2, DBG_MASK_WRITE, false);
debug_watch(DBG_BASIC_CURPC+2, DBG_MASK_WRITE, false);
//debug_watch(DBG_BASIC_ENDPC+2, DBG_MASK_WRITE, false);
debug_watch(DBG_BASIC_BASIC_PROG+8, DBG_MASK_WRITE, false);
debug_watch(DBG_BASIC_SYSHOOKFLAG2, DBG_MASK_READ, false);
}

Expand Down
17 changes: 13 additions & 4 deletions core/debug/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ enum {
DBG_WATCHDOG_TIMEOUT, /* watchdog timer reset */
DBG_MISC_RESET, /* miscellaneous reset */
DBG_STEP, /* step command executed */
DBG_BASIC_USER, /* user requested a basic debug session */
DBG_BASIC_RECONFIG, /* basic mode reconfiguration needed */
DBG_BASIC_STEP, /* basic step command executed */
DBG_BASIC_LIVE_START,
DBG_BASIC_BEGPC_READ, /* begpc read */
DBG_BASIC_CURPC_READ, /* curpc read */
DBG_BASIC_ENDPC_READ, /* endpc read */
DBG_BASIC_BEGPC_WRITE, /* begpc write */
DBG_BASIC_CURPC_WRITE, /* curpc write */
DBG_BASIC_ENDPC_WRITE, /* endpc write */
DBG_BASIC_BASIC_PROG_WRITE, /* basic_prog write */
DBG_BASIC_LIVE_END,
DBG_NUMBER
};
Expand Down Expand Up @@ -76,7 +78,7 @@ void debug_step(int mode, uint32_t addr); /* set a step mode, addr po
void debug_open(int reason, uint32_t data); /* open the debugger (Should only be called from gui_do_stuff) */
bool debug_is_open(void); /* returns the status of the core debugger */
int debug_get_flags(void);
void debug_enable_basic_mode(bool fetches);
void debug_enable_basic_mode(bool fetches, bool live);
void debug_disable_basic_mode(void);
bool debug_get_executing_basic_prgm(char *name);

Expand Down Expand Up @@ -154,9 +156,15 @@ typedef struct {
uint8_t *addr;
uint8_t *port;
bool basicMode;
bool basicModeLive;
bool basicDeferPC;
bool stepBasic;
bool stepBasicNext;
uint32_t stepBasicNextAddr;
uint32_t basicLastHookPC;
uint32_t stepBasicFromPC;
uint16_t stepBasicBegin;
uint16_t stepBasicEnd;
char stepBasicPrgm[10];
} debug_state_t;

extern debug_state_t debug;
Expand All @@ -167,13 +175,14 @@ enum {
DBG_STEP_OVER,
DBG_STEP_NEXT,
DBG_RUN_UNTIL,
DBG_BASIC_STEP,
DBG_BASIC_STEP_IN,
DBG_BASIC_STEP_NEXT,
};

/* internal core functions */
void debug_step_switch(void);
void debug_clear_step(void);
void debug_clear_basic_step(void);

#ifdef __cplusplus
}
Expand Down
22 changes: 9 additions & 13 deletions gui/qt/basiccodeviewerwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,29 @@ void BasicEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
BasicHighlighter::BasicHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
HighlightingRule rule;
bool darkMode = isRunningInDarkMode();

numberFormat.setForeground(Qt::darkMagenta);
numberFormat.setForeground(QColor(darkMode ? "lime" : "darkmagenta"));
rule.pattern = QRegularExpression(R"((((\b[0-9]+)?\.)?\b[0-9]+(ᴇ⁻?[0-9]+)?))");
rule.format = numberFormat;
highlightingRules.append(rule);

variableFormat.setForeground(Qt::darkYellow);
variableFormat.setForeground(QColor(darkMode ? "yellow" : "darkyellow"));
rule.pattern = QRegularExpression(R"((\[[A-J]\])|([A-Zθ])|([\|?uvw])|((GDB|Str|Pic|Img)[0-9])|([XYr][₁₂₃₄₅₆₇₈₉]ᴛ?)|([XY](min|max|scl|res)))");
rule.format = variableFormat;
highlightingRules.append(rule);

listFormat.setForeground(Qt::blue);
listFormat.setForeground(QColor(darkMode ? "lightblue" : "blue"));
rule.pattern = QRegularExpression("(⌊[A-Zθ][A-Z0-9θ]{0,4})|(L[₁₂₃₄₅₆₇₈₉])");
rule.format = listFormat;
highlightingRules.append(rule);

keywordFormat.setForeground(QColor(isRunningInDarkMode() ? "darkorange" : "darkblue"));
keywordFormat.setForeground(QColor(darkMode ? "darkorange" : "darkblue"));
rule.pattern = QRegularExpression("\\b(Else|End|For|Goto|EndIf|ElseIf|End!If|If|!If|Lbl|Repeat|Return|Stop|Then|While)\\b");
rule.format = keywordFormat;
highlightingRules.append(rule);

builtinFormat.setForeground(Qt::darkCyan);
builtinFormat.setForeground(darkMode ? Qt::cyan : Qt::darkCyan);
QStringList builtinPatterns;
builtinPatterns << "\\bAns\\b" << "\\bAsmComp\\b" << "\\bAsm(8[34]CE?)?Prgm\\b" <<"\\bAUTO\\b"
<< "\\bAxesOff\\b" << "\\bAxesOn\\b" << "\\bBackgroundOn\\b" << "\\bBackgroundOff\\b"
Expand Down Expand Up @@ -205,12 +206,7 @@ BasicHighlighter::BasicHighlighter(QTextDocument *parent) : QSyntaxHighlighter(p
rule.format = constFormat;
highlightingRules.append(rule);

numberFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegularExpression(R"((((\b[0-9]+)?\.)?\b[0-9]+([eE][-+]?[0-9]+)?\b))");
rule.format = numberFormat;
highlightingRules.append(rule);

labelFormat.setForeground(QColor::fromRgb(0xBD, 0x3B, 0xB1));
labelFormat.setForeground(darkMode ? QColor(Qt::magenta) : QColor::fromRgb(0xBD, 0x3B, 0xB1));
rule.pattern = QRegularExpression("\\b(Lbl|Goto) [A-Z0-9θ]{1,2}\\b");
rule.format = labelFormat;
highlightingRules.append(rule);
Expand All @@ -220,7 +216,7 @@ BasicHighlighter::BasicHighlighter(QTextDocument *parent) : QSyntaxHighlighter(p
rule.format = prgmFormat;
highlightingRules.append(rule);

delvarFormat.setForeground(Qt::darkCyan);
delvarFormat.setForeground(darkMode ? Qt::cyan : Qt::darkCyan);
rule.pattern = QRegularExpression("DelVar ");
rule.format = delvarFormat;
highlightingRules.append(rule);
Expand All @@ -230,7 +226,7 @@ BasicHighlighter::BasicHighlighter(QTextDocument *parent) : QSyntaxHighlighter(p
rule.format = quotationFormat;
highlightingRules.append(rule);

otherFormat.setForeground(isRunningInDarkMode() ? Qt::darkGray : Qt::black);
otherFormat.setForeground(darkMode ? Qt::lightGray : Qt::black);
rule.pattern = QRegularExpression("→");
rule.format = otherFormat;
highlightingRules.append(rule);
Expand Down
Loading
Loading