diff --git a/src/simudator/cli/cli.py b/src/simudator/cli/cli.py index 0918d19af3ebab33c3dc2cf12d20b7aad133381d..7eb5e46ddcf8349ff805b61a6b647575bdbccca0 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 e45c8bdd63fe0703066bbdf76a0fe8add8a6b9c9..d7f8399ec2dbf27c29a9244a96c23a26739f19b3 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 3169e88b13e841b555bd6aa128217cb01639e44f..ac2ba002cd7d41e8efa182514e5abadf082159bc 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 d615f61ac344002be4ddbb31e9f0062a7f403827..a46a4ebd2c25c1f472caf226dc2c55af398455a0 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)