Create common function for running instructions
authorHugo Villeneuve <hugo@hugovil.com>
Sat, 11 Jan 2014 22:24:23 +0000 (17:24 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Sat, 11 Jan 2014 22:24:23 +0000 (17:24 -0500)
src/cli/emuconsole.c
src/common/cpu8051.c
src/common/cpu8051.h
src/gtk/emugtk.c

index ab85975..5dd93df 100644 (file)
@@ -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();
 }
index bf7bd99..2c2845e 100644 (file)
@@ -24,7 +24,9 @@
 
 #include <stdio.h>
 #include <stdint.h>
+#include <stdbool.h>
 
+#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[]
index adb96c8..49494f0 100644 (file)
@@ -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);
 
index 308bdeb..0c1ca49 100644 (file)
@@ -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;
 }