From 5512bb0aef2f8519c9634e64703aa451641fb18c Mon Sep 17 00:00:00 2001
From: Johannes Kung <johku144@student.liu.se>
Date: Mon, 10 Jun 2024 16:18:11 +0200
Subject: [PATCH] Simplified how execution of the processor is stopped

---
 src/simudator/cli/cli.py                     | 12 ++++++--
 src/simudator/core/processor.py              | 30 ++++++++++++--------
 src/simudator/gui/gui.py                     |  2 --
 src/simudator/gui/run_continuously_thread.py |  6 +++-
 4 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/src/simudator/cli/cli.py b/src/simudator/cli/cli.py
index 0918d19..7eb5e46 100644
--- a/src/simudator/cli/cli.py
+++ b/src/simudator/cli/cli.py
@@ -77,6 +77,9 @@ class CLI:
                         bp = self.processor.last_breakpoint
                         print(f"Reached breakpoint: {bp}")
 
+                    if self.processor.should_halt():
+                        print("The processor halted")
+
 
                 case ["r"] | ["resets"]:
                     # self.processor.load_cycle(0)
@@ -90,10 +93,13 @@ class CLI:
                     try:
                         for _ in range(int(user_input.split()[1])):
                             self.processor.do_tick()
-                            if self.processor.breakpoint_reached:
-                                bp = self.processor.last_breakpoint
-                                print(f"Reached breakpoint: {bp}")
+                            if self.processor.is_stopped:
                                 break
+                        if self.processor.breakpoint_reached:
+                            bp = self.processor.last_breakpoint
+                            print(f"Reached breakpoint: {bp}")
+                        if self.processor.should_halt():
+                            print("The processor halted")
                     except ValueError:
                         print("Invalid value")
 
diff --git a/src/simudator/core/processor.py b/src/simudator/core/processor.py
index e45c8bd..d7f8399 100644
--- a/src/simudator/core/processor.py
+++ b/src/simudator/core/processor.py
@@ -60,6 +60,7 @@ class Processor:
             self.module_history = self.module_history[0:self.clock]
             self.signal_history = self.signal_history[0:self.clock]
 
+        self.unstop()
         self.save_cycle()
         self.clock += 1
 
@@ -73,35 +74,39 @@ class Processor:
             module = self.update_queue.pop(0)
             module.update_logic()
 
-        self.check_breakpoints()
+        self.stop_at_breakpoints()
+        if self.should_halt():
+            self.stop()
 
     def run_continuously(self) -> None:
         """
         Run the processor until it halts, is stopped or reaches a breakpoint.
         """
-        while not self.should_halt() and not self.is_stopped:
+        self.unstop()
+        while not self.is_stopped:
             self.do_tick()
-            if self.check_breakpoints():
-                break
 
     def should_halt(self) -> bool:
         return False
 
     def stop(self) -> None:
+        """
+        Signal to stop the execution of the processor until the processor is 
+        instructed to run continuously or do some ticks again.
+        """
         self.is_stopped = True
 
     def unstop(self) -> None:
+        """
+        Reset the stop execution signal.
+        """
         self.is_stopped = False
 
-    def check_breakpoints(self) -> bool:
+    def stop_at_breakpoints(self) -> None:
         """
-        Check and record if a breakpoint has been reached at the current state.
-
-        Returns
-        -------
-        bool 
-            True if a breakpoint has been reached, False otherwise.
+        Stop the execution if any breakpoint has been reached during this cycle.
 
+        Also record the breakpoint that was reached.
         """
         self.breakpoint_reached = False
         for _, bp in self.breakpoints.items():
@@ -110,7 +115,8 @@ class Processor:
                 self.breakpoint_reached = True
                 self.last_breakpoint = bp
 
-        return self.breakpoint_reached
+        if self.breakpoint_reached:
+            self.stop()
 
 
     def reset(self):
diff --git a/src/simudator/gui/gui.py b/src/simudator/gui/gui.py
index 3169e88..ac2ba00 100644
--- a/src/simudator/gui/gui.py
+++ b/src/simudator/gui/gui.py
@@ -454,7 +454,6 @@ class GUI(QMainWindow):
         steps = self.jump_value_box.value()
         self.cpu_running = True
         self.setDisabledWhenRunning(True)
-        self.cpu.unstop()
         simulation_thread = RunThread(self.cpu, self.cpu_tick_signal, self.update_delay, False, steps)
         self.threadpool.start(simulation_thread)
 
@@ -470,7 +469,6 @@ class GUI(QMainWindow):
         # Create own thread for cpu simulation so gui dosent freeze
         self.cpu_running = True
         self.setDisabledWhenRunning(True)
-        self.cpu.unstop()
         simulation_thread = RunThread(self.cpu, self.cpu_tick_signal, self.update_delay)
         self.threadpool.start(simulation_thread)
 
diff --git a/src/simudator/gui/run_continuously_thread.py b/src/simudator/gui/run_continuously_thread.py
index d615f61..a46a4eb 100644
--- a/src/simudator/gui/run_continuously_thread.py
+++ b/src/simudator/gui/run_continuously_thread.py
@@ -23,7 +23,8 @@ class RunThread(QRunnable):
 
     def run(self):
         if self.run_continuously:
-            while not self.cpu.should_halt() and not self.cpu.is_stopped:
+            self.cpu.unstop()
+            while not self.cpu.is_stopped:
                 self.cpu.do_tick()
                 self.signal.emit(1)
                 time.sleep(self.delay)
@@ -34,5 +35,8 @@ class RunThread(QRunnable):
                 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