From 7f72e34564823629e4f28ac0452217ca514a18c1 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Sat, 11 Jan 2014 17:24:23 -0500 Subject: [PATCH] Create common function for running instructions --- src/cli/emuconsole.c | 14 ++----------- src/common/cpu8051.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/common/cpu8051.h | 3 +++ src/gtk/emugtk.c | 9 ++++---- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/cli/emuconsole.c b/src/cli/emuconsole.c index ab85975..5dd93df 100644 --- a/src/cli/emuconsole.c +++ b/src/cli/emuconsole.c @@ -80,22 +80,12 @@ console_exec(char *Address, char *NumberInst) log_info("Program executing..."); - do { - cpu8051_Exec(); - if (NbInst > 0) - NbInst--; - } while (!IsBreakpoint(cpu8051.pc) && !IsStoppoint(cpu8051.pc) && - (NbInst != 0) && !kbhit()); + cpu8051_run(NbInst, kbhit); + if (kbhit()) { (void) getch(); /* Flush key */ log_info("Caught break signal!"); } - if (NbInst == 0) - log_info("Number of instructions reached! Stopping!"); - if (IsBreakpoint(cpu8051.pc)) - log_info("Breakpoint hit at %.4X! Stopping!", cpu8051.pc); - if (IsStoppoint(cpu8051.pc)) - log_info("Stoppoint hit at %.4X! Stopping!", cpu8051.pc); ResetUnixKB(); } diff --git a/src/common/cpu8051.c b/src/common/cpu8051.c index bf7bd99..2c2845e 100644 --- a/src/common/cpu8051.c +++ b/src/common/cpu8051.c @@ -24,7 +24,9 @@ #include #include +#include +#include "common.h" #include "reg8051.h" #include "cpu8051.h" #include "memory.h" @@ -319,6 +321,53 @@ cpu8051_Exec(void) } } +/* + * Run specified number of instructions, or when encountering a + * breakpoint or a stop point. + * Set instr_count to -1 to disable running for a specific number + * of instructions. + * + * Returns TRUE when a breakpoint is encountered. + */ +int +cpu8051_run(int instr_count, int (*interface_stop)(void)) +{ + int stop = false; + int breakpoint_hit = false; + + while (stop == false) { + cpu8051_Exec(); + + if (instr_count > 0) + instr_count--; + + if (instr_count == 0) { + stop = true; + log_info("Number of instructions reached! Stopping!"); + } + + if (IsBreakpoint(cpu8051.pc)) { + stop = true; + breakpoint_hit = true; + log_info("Breakpoint hit at %.4X! Stopping!", cpu8051.pc); + } + + if (IsStoppoint(cpu8051.pc)) { + stop = true; + log_info("Stoppoint hit at %.4X! Stopping!", cpu8051.pc); + } + + if (interface_stop != NULL) { + if (interface_stop()) { + stop = true; + log_info("Caught break signal!"); + } + } + } + + return breakpoint_hit; +} + /* * Addressing modes defined in the order as they appear in disasm.h * from table argstext[] diff --git a/src/common/cpu8051.h b/src/common/cpu8051.h index adb96c8..49494f0 100644 --- a/src/common/cpu8051.h +++ b/src/common/cpu8051.h @@ -80,6 +80,9 @@ cpu8051_init(void); void cpu8051_Exec(void); +int +cpu8051_run(int instr_count, int (*interface_stop)(void)); + void cpu8051_Reset(void); diff --git a/src/gtk/emugtk.c b/src/gtk/emugtk.c index 308bdeb..0c1ca49 100644 --- a/src/gtk/emugtk.c +++ b/src/gtk/emugtk.c @@ -89,13 +89,14 @@ emugtk_stop_running() static gboolean emugtk_running(gpointer data) { + int breakpoint_hit; + (void) data; /* Remove compiler warning about unused variable. */ - cpu8051_Exec(); - if (IsBreakpoint(cpu8051.pc)) { - log_info("Breakpoint Hit"); + breakpoint_hit = cpu8051_run(1, NULL); + + if (breakpoint_hit) emugtk_stop_running(); - } return TRUE; } -- 2.20.1