From a850f7782997b16832212c1da5f50d3b2991a3ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20H=C3=B6gstedt?= <marin.hogstedt@hotmail.com>
Date: Tue, 11 Jun 2024 09:14:35 +0200
Subject: [PATCH] stepping over asm instructions now works

---
 src/simudator/gui/gui.py                     |  9 ++--
 src/simudator/gui/run_continuously_thread.py | 44 +++++++++++++-------
 2 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/src/simudator/gui/gui.py b/src/simudator/gui/gui.py
index baec07a..9910e33 100644
--- a/src/simudator/gui/gui.py
+++ b/src/simudator/gui/gui.py
@@ -310,14 +310,14 @@ class GUI(QMainWindow):
         backward_arrow_icon = self.style().standardIcon(QStyle.SP_MediaSeekBackward)
         self.undo_asm_action = QAction(backward_arrow_icon, "Undo Asm", self)
         self.undo_asm_action.setStatusTip("Undo the last assembler instruction")
-        self.undo_asm_action.triggered.connect(self.undoToolBarButtonClick)
+        self.undo_asm_action.triggered.connect(self.undoAsmToolBarButtonClick)
         toolbar.addAction(self.undo_asm_action)
 
         # Add step button on toolbar
         forward_arrow_icon = self.style().standardIcon(QStyle.SP_MediaSeekForward)
         self.step_asm_action = QAction(forward_arrow_icon, "Step Asm", self)
         self.step_asm_action.setStatusTip("Run one assembler instruction")
-        self.step_asm_action.triggered.connect(self.stepToolBarButtonClick)
+        self.step_asm_action.triggered.connect(self.stepAsmToolBarButtonClick)
         toolbar.addAction(self.step_asm_action)
 
         # Add box for jump value
@@ -502,7 +502,7 @@ class GUI(QMainWindow):
         steps = self.jump_value_box.value()
         self.cpu_running = True
         self.setDisabledWhenRunning(True)
-        simulation_thread = RunThread(self.cpu, self.cpu_tick_signal, self.update_delay, False, steps)
+        simulation_thread = RunThread(self.cpu, self.cpu_tick_signal, self.update_delay, False, False, steps)
         self.threadpool.start(simulation_thread)
 
     def stepAsmToolBarButtonClick(self):
@@ -517,8 +517,7 @@ class GUI(QMainWindow):
         steps = self.asm_jump_value_box.value()
         self.cpu_running = True
         self.setDisabledWhenRunning(True)
-        self.cpu.unstop()
-        simultaion_thread = RunThread(self.cpu, self.halted_signal, False, steps)
+        simultaion_thread = RunThread(self.cpu, self.cpu_tick_signal, self.update_delay, False, True, steps)
         self.threadpool.start(simultaion_thread)
         self.updateCpuListeners()
 
diff --git a/src/simudator/gui/run_continuously_thread.py b/src/simudator/gui/run_continuously_thread.py
index 1f1ef5b..900d632 100644
--- a/src/simudator/gui/run_continuously_thread.py
+++ b/src/simudator/gui/run_continuously_thread.py
@@ -8,40 +8,54 @@ class RunThread(QRunnable):
     a seperate thread. This allows the user to interact with the GUI while the 
     simulation is running.
 
-    After each cpu tick, this thread will emit to its given QT signal so that
+    After each CPU tick, this thread will emit to its given QT signal so that
     the GUI can update itself and possibly inform the user of when the execution 
     has halted.
     """
 
-    def __init__(self, cpu, signal, delay: float, run_continuously=True, steps=0):
+    def __init__(self, cpu, signal, delay: float, run_continuously=True, 
+                 is_asm_instruciton=False, steps=0):
         super().__init__()
         self.cpu = cpu
         self.signal = signal
         self.run_continuously = run_continuously
         self.steps = steps
         self.delay = delay
+        self.is_asm_instruciton = is_asm_instruciton
 
     def run(self):
-        if self.run_continuously:
-            self.cpu.unstop()
-            while not self.cpu.is_stopped:
-                self.cpu.do_tick()
-                self.signal.emit(1)
-                time.sleep(self.delay)
+        if self.is_asm_instruciton:
 
-        else:
-            for _ in range(self.steps):
+            while self.steps > 0:
                 self.cpu.do_tick()
+
+                # We only care about asm instructions
+                if self.cpu.is_new_instruction():
+                    self.steps -= 1
+
                 self.signal.emit(1)
                 time.sleep(self.delay)
 
                 if self.cpu.is_stopped:
                     break
 
-        # Signal end of execution as having run 0 ticks
-        self.signal.emit(0)
-    
-    def run_asm(self):
-        self.cpu.run_asm(self.steps)
+        else:
+            if self.run_continuously:
+                self.cpu.unstop()
+                while not self.cpu.is_stopped:
+                    self.cpu.do_tick()
+                    self.signal.emit(1)
+                    time.sleep(self.delay)
+
+            else:
+                for _ in range(self.steps):
+                    self.cpu.do_tick()
+                    self.signal.emit(1)
+                    time.sleep(self.delay)
 
+                    if self.cpu.is_stopped:
+                        break
+
+
+        # Signal end of execution as having run 0 ticks
         self.signal.emit(0)
-- 
GitLab