From 454ce0a753710e88bf393f298de77d61293bf8c5 Mon Sep 17 00:00:00 2001
From: Johannes Kung <johku144@student.liu.se>
Date: Mon, 10 Jun 2024 09:11:14 +0200
Subject: [PATCH] Minor refactor for clarity

---
 src/simudator/gui/gui.py                     | 23 +++++++++++---------
 src/simudator/gui/run_continuously_thread.py | 16 +++++++++-----
 2 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/simudator/gui/gui.py b/src/simudator/gui/gui.py
index 7fb4d02..d8372d2 100644
--- a/src/simudator/gui/gui.py
+++ b/src/simudator/gui/gui.py
@@ -90,7 +90,7 @@ class GUI(QMainWindow):
     and added individually.
     """
 
-    halted_signal = pyqtSignal(int)
+    cpu_tick_signal = pyqtSignal(int)
     HALT_MESSAGE_THRESHOLD = 100
 
     def __init__(self, cpu: Processor):
@@ -114,7 +114,7 @@ class GUI(QMainWindow):
         self.threadpool = QtCore.QThreadPool()
 
         # Signal to tell gui when cpu has halted
-        self.halted_signal.connect(self.cpuHaltedFunction)
+        self.cpu_tick_signal.connect(self.handleCpuTick)
 
         # Used to lock some actions in ui when cpu is running in another thread
         # Using the cpu's internal status directly could case problems
@@ -291,6 +291,7 @@ class GUI(QMainWindow):
     def updateCpuListeners(self) -> None:
         """
         Updates the graphics items in the scene and the clock.
+
         Used after the cpu has run or when the user has edited somehting.
         """
         self.cpu_graphics_scene.updateGraphicsItems()
@@ -298,8 +299,9 @@ class GUI(QMainWindow):
 
     def updateCpuClockCycle(self) -> None:
         """
-        Updates the clock cycle counter. Used while the program is running
-        to show the user nothing has crached.
+        Update the clock cycle counter. 
+
+        Used while the program is running to show the user nothing has crashed.
         """
         self.clock_label.setText("Clockcycle: " + str(self.cpu.get_clock()))
 
@@ -432,8 +434,9 @@ class GUI(QMainWindow):
         self.cpu_running = True
         self.setDisabledWhenRunning(True)
         self.cpu.unstop()
-        simultaion_thread = RunThread(self.cpu, self.halted_signal, False, steps)
-        self.threadpool.start(simultaion_thread)
+        simulation_thread = RunThread(self.cpu, self.cpu_tick_signal, False, steps)
+        self.threadpool.start(simulation_thread)
+        # self.updateCpuListeners()
 
     def runToolBarButtonClick(self) -> None:
         """
@@ -448,12 +451,12 @@ class GUI(QMainWindow):
         self.cpu_running = True
         self.setDisabledWhenRunning(True)
         self.cpu.unstop()
-        simultaion_thread = RunThread(self.cpu, self.halted_signal)
-        self.threadpool.start(simultaion_thread)
+        simulation_thread = RunThread(self.cpu, self.cpu_tick_signal)
+        self.threadpool.start(simulation_thread)
         self.updateCpuListeners()
 
     @Slot(int)
-    def cpuHaltedFunction(self, steps: int) -> None:
+    def handleCpuTick(self, steps: int) -> None:
         """
         Called from other thread after every cpu tick.
         Will inform the user and update visuals.
@@ -462,7 +465,7 @@ class GUI(QMainWindow):
         # Update cpu clock counter every tick
         self.updateCpuClockCycle()
 
-        # If a breakpoint halted the program inform thr user
+        # If a breakpoint halted the program, inform the user
         # Update other visuals and set the cpu to the correct state (not running)
         if self.cpu.breakpoint_reached:
             self.messageBox("Reached breakpoint: " + self.cpu.last_breakpoint.__str__())
diff --git a/src/simudator/gui/run_continuously_thread.py b/src/simudator/gui/run_continuously_thread.py
index daa095a..3694165 100644
--- a/src/simudator/gui/run_continuously_thread.py
+++ b/src/simudator/gui/run_continuously_thread.py
@@ -2,13 +2,17 @@ import time
 
 from qtpy.QtCore import QRunnable
 
+CPU_TICK_DELAY = 0.0000001
 
 class RunThread(QRunnable):
     """
-    This class is used to run the cpu on a seperate thread.
-    This allows the user to interact with the GUI will the cpu is running.
-    When the cpu halts this thread will emit to it's given signal
-    the GUI can then handel what should happend after execution on its own.
+    This class is used to run the simulated cpu several ticks or continuously on
+    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
+    the GUI can update itself and possibly inform the user of when the execution 
+    has halted.
     """
 
     def __init__(self, cpu, signal, run_continuously=True, steps=0):
@@ -23,10 +27,10 @@ class RunThread(QRunnable):
             while not self.cpu.should_halt():
                 self.cpu.do_tick()
                 self.signal.emit(1)
-                time.sleep(0.0000001)
+                time.sleep(CPU_TICK_DELAY)
 
         else:
             for _ in range(self.steps):
                 self.cpu.do_tick()
                 self.signal.emit(1)
-                time.sleep(0.0000001)
+                time.sleep(CPU_TICK_DELAY)
-- 
GitLab