diff --git a/src/simudator/core/module.py b/src/simudator/core/module.py index f1cf900406eda3bb8959d4d25b949ce0bc59df0d..edd367fca29fef303741a9c5024218ca738ce15f 100644 --- a/src/simudator/core/module.py +++ b/src/simudator/core/module.py @@ -41,9 +41,17 @@ class Module: state_dict["name"] = self.name return state_dict + def get_gui_state(self) -> dict: + """ + Returns a dict of the module states that should be displayed in a GUI. + """ + state_dict = dict() + state_dict["name"] = self.name + return state_dict + def set_state(self, state: dict) -> None: """ - Sets the modules state to one given in dict. Dict format vill be + Sets the modules state to one given in dict. Dict format will be diffrent for each type of module so use get_state to get correct format. """ self.name = state["name"] diff --git a/src/simudator/core/modules/flag.py b/src/simudator/core/modules/flag.py index 53d9597e70972defc60ebf2f3e18e58e5843d8c5..70b88d52176467283be58aa46ce441b6008dfda2 100644 --- a/src/simudator/core/modules/flag.py +++ b/src/simudator/core/modules/flag.py @@ -71,14 +71,21 @@ class Flag(Module): state["mask"] = self.mask return state + def get_gui_state(self) -> dict: + state = super().get_gui_state() + state["value"] = self.value + return state + def set_state(self, state: dict[str: Any]) -> None: """ Sets the register state to one given in dict. """ self.name = state["name"] self.value = state["value"] - self.bit_length = state["bit_length"] - self.mask = state["mask"] + if "bit_length" in state: + self.bit_length = state["bit_length"] + if "mask" in state: + self.mask = state["mask"] def reset(self) -> None: """ diff --git a/src/simudator/core/modules/memory.py b/src/simudator/core/modules/memory.py index d3504318ee103983f7251c83b88cb1cc2451eaa6..017c2de2f4084dd3f52b73e757b2b915aa31f0ec 100644 --- a/src/simudator/core/modules/memory.py +++ b/src/simudator/core/modules/memory.py @@ -81,11 +81,20 @@ class Memory(Module): state["memory"] = self.memory[:] return state + def get_gui_state(self) -> dict: + state = super().get_gui_state() + state["current_adress"] = self.current_adress + state["memory"] = self.memory[:] + return state + def set_state(self, state: dict) -> None: super().set_state(state) - self.is_write = state["is_write"] - self.current_adress = state["current_adress"] - self.memory = state["memory"] + if "is_write" in state: + self.is_write = state["is_write"] + if "current_adress" in state: + self.current_adress = state["current_adress"] + if "memory" in state: + self.memory = state["memory"] def reset(self) -> None: """ diff --git a/src/simudator/gui/dialogs/module_state_dialog.py b/src/simudator/gui/dialogs/module_state_dialog.py index a51738db2b64a6ae689956be04e1adfbd5ed467c..a9f7271ddbed79e80966da0e51cb6fb5ec52ce22 100644 --- a/src/simudator/gui/dialogs/module_state_dialog.py +++ b/src/simudator/gui/dialogs/module_state_dialog.py @@ -24,7 +24,7 @@ class ModuleStateDialog(QDialog): super().__init__(parent, flags) self.module = module - states = module.get_state() + states = module.get_gui_state() # Remove the 'name' state of the module from the drop down menu so # that the user cannot e.g. edit or add a breakpoint of a module @@ -60,7 +60,7 @@ class ModuleStateDialog(QDialog): def updateState(self) -> None: selectedState = self.stateSelectWidget.currentText() - value = self.module.get_state()[selectedState] + value = self.module.get_gui_state()[selectedState] self.valueWidget.setText(str(value)) def signalOK(self) -> None: diff --git a/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py index 853528cea64a89fb1f9253a73e31b9210c2e9f41..9d182af4e12888690bfc5cbff66e8f1c588bade8 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py @@ -1,12 +1,15 @@ from simudator.gui.module_graphics_item.mia.mia_register_graphic import ( RegisterGraphicsItem, ) +from simudator.processor.mia.mia_register import MiaRegister class FlagGraphicsItem(RegisterGraphicsItem): """ Graphics module for mia's flag module. """ + def __init__(self, module: MiaRegister): + super().__init__(module) def draw_graphics_item(self): self.draw_rect() diff --git a/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py index 23edfdad8ed331df4b60959ac3ffac1883b5f6a3..13562f63be7fbb654ba21de8e407caa22d60383e 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py @@ -1,5 +1,6 @@ from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsSimpleTextItem +from simudator.core.module import Module from simudator.gui.module_graphics_item.module_graphics_item import ModuleGraphicsItem @@ -20,6 +21,10 @@ class IrGraphicsItem(ModuleGraphicsItem): TEXT_HEIGHT_MARGIN = 8 BIG_TEXT_HEIGHT_MARGIN = 12 + def __init__(self, module: Module, name: str = None): + super().__init__(module, name) + self.draw_ports() + def draw_graphics_item(self): # Add small rect diff --git a/src/simudator/gui/module_graphics_item/mia/mia_register_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_register_graphic.py index 9e47836a490c7d581797b04758752dbf72aa1d4b..92f0254e8902a250fec9828f3063595322277e9b 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_register_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_register_graphic.py @@ -19,6 +19,11 @@ class RegisterGraphicsItem(ModuleGraphicsItem): def __init__(self, module: MiaRegister): super().__init__(module) + self.draw_rect() + + def draw_graphics_item(self) -> None: + self.draw_rect() + self.draw_ports() def draw_rect(self): """ diff --git a/src/simudator/gui/module_graphics_item/module_graphics_item.py b/src/simudator/gui/module_graphics_item/module_graphics_item.py index eed66c67338029ff4e8a88b5f36bd2987a16d062..1ed7a58d804d2908f9510d805571eff7127df449 100644 --- a/src/simudator/gui/module_graphics_item/module_graphics_item.py +++ b/src/simudator/gui/module_graphics_item/module_graphics_item.py @@ -60,6 +60,7 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): self.actions = [] self.isLocked = False + print(self.module.get_gui_state()) # Do general draw self.draw_graphics_item() @@ -86,7 +87,7 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): # Calculate height according to number of signals and states in_signals = len(self.input_signals) out_signals = len(self.output_signals) - states = len(self.state) + states = len(self.module.get_gui_state()) inputs_height = (in_signals + 1) * self.SIGNAL_MARGIN outputs_height = (out_signals + 1) * self.SIGNAL_MARGIN members_height = states * self.CHAR_HEIGHT + self.STATE_MEMBER_MARGIN @@ -110,9 +111,10 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): # Create state lables self.state_lables = {} - for state_tuple in enumerate(self.state): + gui_states = self.module.get_gui_state() + for state_tuple in enumerate(gui_states): if state_tuple[1] != "name": - text = state_tuple[1] + ": " + str(self.state[state_tuple[1]]) + text = state_tuple[1] + ": " + str(gui_states[state_tuple[1]]) state_text = QGraphicsSimpleTextItem(text, self) state_text.setPos(width/20, state_tuple[0]*self.CHAR_HEIGHT) @@ -152,8 +154,9 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): widets will have to override it. """ self.state = self.module.get_state() + gui_state = self.module.get_gui_state() - for name, value in self.state.items(): + for name, value in gui_state.items(): if name != "name": lable = self.state_lables[name] lable.setText(name + ": " + str(value)) diff --git a/src/simudator/processor/mia/ir.py b/src/simudator/processor/mia/ir.py index a129f77f97bbc0756b5fda3bb402e28bc4c98432..1b7f37a26c1efcbded17c294eb6ecf2b734b1386 100644 --- a/src/simudator/processor/mia/ir.py +++ b/src/simudator/processor/mia/ir.py @@ -85,13 +85,22 @@ class IR(Module, MiaBusConnector): } return state + def get_gui_state(self) -> dict: + state = super().get_gui_state() + state["value"] = self.instruction + return state + def set_state(self, state: dict) -> None: super().set_state(state) self.instruction = state["value"] - self.op = state["op"] - self.grx = state["grx"] - self.m = state["m"] - self.a = state["a"] + if "op" in state: + self.op = state["op"] + if "grx" in state: + self.grx = state["grx"] + if "m" in state: + self.m = state["m"] + if "a" in state: + self.a = state["a"] def reset(self) -> None: """ diff --git a/src/simudator/processor/mia/lc.py b/src/simudator/processor/mia/lc.py index a9f528caa65d07c8dde66e6bf670bf2fbc823d11..cff9e9e3477bc8fcbd9456d0d8f9e972806c4110 100644 --- a/src/simudator/processor/mia/lc.py +++ b/src/simudator/processor/mia/lc.py @@ -168,11 +168,16 @@ class LC(Module): """ self.name = state["name"] self.value = state["value"] - self.bit_length = state["bit_length"] - self.mask = state["mask"] - self.read_from_bus = state["read_from_bus"] - self.read_from_uADR = state["read_from_uADR"] - self.decrement_by_one = state["decrement_by_one"] + if "bit_length" in state: + self.bit_length = state["bit_length"] + if "mask" in state: + self.mask = state["mask"] + if "read_from_bus" in state: + self.read_from_bus = state["read_from_bus"] + if "read_from_uADR" in state: + self.read_from_uADR = state["read_from_uADR"] + if "decrement_by_one" in state: + self.decrement_by_one = state["decrement_by_one"] def reset(self) -> None: """ diff --git a/src/simudator/processor/mia/mia_grx.py b/src/simudator/processor/mia/mia_grx.py index 4f39e44e65dafabca1a0e6e95eba2b999bb2dbae..b897a6a946350c6989ce16d031e592b4aed602cd 100644 --- a/src/simudator/processor/mia/mia_grx.py +++ b/src/simudator/processor/mia/mia_grx.py @@ -95,26 +95,32 @@ class GRX(Module, MiaBusConnector): def update_logic(self) -> None: self.output_register() - def get_state(self) -> dict[Any]: + def get_state(self) -> dict[str, Any]: """ Returns a dict of the grx state. These states are changable via set_states. """ state = { "name": self.name, - "bus_id": self.bus_id, "bit_length": self.bit_length, "registers": self.registers[:], } return state - def set_state(self, state: dict[str: Any]): + def get_gui_state(self) -> dict: + state = { + "name": self.name, + "registers": self.registers[:], + } + return state + + def set_state(self, state: dict[str, Any]): """ Sets the grx state to one given in dict. """ self.name = state["name"] - self.bus_id = state["bus_id"] - self.bit_length = state["bit_length"] + if "bit_length" in state: + self.bit_length = state["bit_length"] self.registers = state["registers"] def reset(self) -> None: diff --git a/src/simudator/processor/mia/mia_register.py b/src/simudator/processor/mia/mia_register.py index 1b34d991d3f81fc083fae80f2fc21503d72ed665..08d577d9904a0fa4f4e6cc9087bdac5a2cb44e7e 100644 --- a/src/simudator/processor/mia/mia_register.py +++ b/src/simudator/processor/mia/mia_register.py @@ -42,13 +42,18 @@ class MiaRegister(Register): state["mask"] = self.mask return state + def get_gui_state(self) -> dict: + state = super().get_state() + return state + def set_state(self, state: dict[str: Any]) -> None: """ Sets the register state to one given in dict. """ super().set_state(state) - self.bit_length = state["bit_length"] - self.mask = 2**self.bit_length -1 + if "bit_length" in state: + self.bit_length = state["bit_length"] + self.mask = 2**self.bit_length -1 def reset(self) -> None: self.value = 0 diff --git a/src/simudator/processor/mia/micro_memory.py b/src/simudator/processor/mia/micro_memory.py index 55a283b46bb4af931fda22aa5c3f7551773bab11..578d8fc53492f19ec5b518cffbb7dc16d421847c 100644 --- a/src/simudator/processor/mia/micro_memory.py +++ b/src/simudator/processor/mia/micro_memory.py @@ -1,3 +1,5 @@ +from typing import Any + from simudator.core.module import Module from simudator.core.signal import Signal @@ -262,10 +264,16 @@ class MicroMemory(Module): state["halt"] = self.halt return state + def get_gui_state(self) -> dict[str, Any]: + state = super().get_state() + state["memory"] = self.memory[:] + return state + def set_state(self, state: dict) -> None: super().set_state(state) self.memory = state["memory"] - self.halt = state["halt"] + if "halt" in state: + self.halt = state["halt"] def get_input_signals(self) -> [Signal]: return [self.upc_s, diff --git a/src/simudator/processor/mia/micro_pc.py b/src/simudator/processor/mia/micro_pc.py index f9572bd23814e843f1c5a479c22d9ff8a1f3158d..f95e1d426f7f25852c6ade71d5a88bdf9af2d1d0 100644 --- a/src/simudator/processor/mia/micro_pc.py +++ b/src/simudator/processor/mia/micro_pc.py @@ -19,16 +19,19 @@ class MicroPC(Module): from_supc: Signal, to_um: Signal, from_um: Signal, + bit_length: int = 8, name: str = "uPC") -> None: super().__init__(name) + self.value = 0 + self.bit_length = bit_length + # Input signals self.control_signal = control_signal self.from_k1 = from_k1 self.from_k2 = from_k2 self.from_supc = from_supc self.from_um = from_um - self.value = 0 # Output signals self.to_supc = to_supc @@ -70,12 +73,21 @@ class MicroPC(Module): def get_state(self) -> dict: state = super().get_state() state["value"] = self.value + state["bit_length"] = self.bit_length return state + def get_gui_state(self) -> dict: + state = { + "name": self.name, + "value": self.value, + } + return state def set_state(self, state: dict) -> None: super().set_state(state) self.value = state["value"] + if "bit_length" in state: + self.bit_length = state["bit_length"] def reset(self) -> None: """ diff --git a/src/simudator/processor/mia/pc.py b/src/simudator/processor/mia/pc.py index f063667be747e2aca7965d3528bf1391bfb95fa2..ceb8b1429390a54320a8265cda6a26f8b7089136 100644 --- a/src/simudator/processor/mia/pc.py +++ b/src/simudator/processor/mia/pc.py @@ -91,6 +91,14 @@ class PC(Module, MiaBusConnector): state["name"] = self.name state["value"] = self.value state["increment"] = self.increase_by_one + state["bit_length"] = 8 + return state + + def get_gui_state(self) -> dict: + state = { + "name": self.name, + "value": self.value, + } return state def set_state(self, state: dict[str: Any]) -> None: @@ -99,7 +107,8 @@ class PC(Module, MiaBusConnector): """ self.name = state["name"] self.value = state["value"] - self.increase_by_one = state["increment"] + if "increment" in state: + self.increase_by_one = state["increment"] def reset(self) -> None: """