diff --git a/src/simudator/core/modules/memory.py b/src/simudator/core/modules/memory.py index 492601d6a4f3916924536123d90463391f4ac381..163d1f7f51a351dd247275dc18900502700eacbe 100644 --- a/src/simudator/core/modules/memory.py +++ b/src/simudator/core/modules/memory.py @@ -33,7 +33,7 @@ class Memory(Module): super().__init__(signals, name) # Internal state - self.memory = [0 for i in range(size)] + self.memory = [0 for _ in range(size)] self.current_address = 0 self.is_write = False diff --git a/src/simudator/gui/module_graphics_item/memory_graphic.py b/src/simudator/gui/module_graphics_item/memory_graphic.py index a39999626666c8470049c8676190c6b588cd9652..2817042cc772ef63fbbce36395542b585fda8eec 100644 --- a/src/simudator/gui/module_graphics_item/memory_graphic.py +++ b/src/simudator/gui/module_graphics_item/memory_graphic.py @@ -1,3 +1,5 @@ +from math import ceil + from qtpy.QtCore import Qt from qtpy.QtCore import Signal as pyqtSignal from qtpy.QtCore import Slot @@ -5,9 +7,8 @@ from qtpy.QtWidgets import ( QAction, QGraphicsRectItem, QGraphicsSimpleTextItem, - QTextEdit, - QVBoxLayout, - QWidget, + QTableWidget, + QTableWidgetItem, ) from simudator.core.modules import Memory @@ -17,36 +18,84 @@ from simudator.gui.orientation import Orientation from simudator.gui.port_graphics_item import PortGraphicsItem -class MemoryWindow(QWidget): - """ - Widget for showing content of memory +class MemoryWindow(QTableWidget): """ + A class showing the contents of a memory module in a QTableWidget. + + This class assumes that the size of the memory module will remain constant. - _ROW_LENGTH = 5 + Parameters + ---------- + memory_module: An instance of the Memory base class. + column_size: An integer specifying the number of columns, optional. + + """ - def __init__(self, memory_module: Memory): + def __init__(self, memory_module: Memory, column_size=-1): super().__init__() - self.module = memory_module + self._memory = memory_module + self._column_size = column_size + self._memory_size = len(self._memory.get_state()["memory"]) + self._set_column_size() + self.setColumnCount(self._column_size) + self.setRowCount(ceil(self._memory_size / self._column_size)) + self.setHorizontalHeaderLabels(["+" + str(i) for i in range(4)]) - self.text = QTextEdit("") - layout = QVBoxLayout() - layout.addWidget(self.text) - self.setLayout(layout) + vertical_headers = [] + for i in range(0, self._memory_size, self._column_size): + vertical_headers.append(str(hex(i))) + + self.setVerticalHeaderLabels(vertical_headers) self.update() def update(self): - memory_str = "" - for address, value in enumerate(self.module.memory): - # Add address and content to string - # Make sure its unifrom lenght so rows are consistent - memory_str += f"{address}" + ": " + f"{value}" + " " + "\t" + """ + Update the content of this widget to reflect the content of the memory module. + """ + memory_content = self._memory.get_state()["memory"] + for i in range(self._memory_size): + value = memory_content[i] + row = i // self._column_size + col = i % self._column_size + self.set_item(row, col, str(value)) - # Make new line when we reach end of row - if address % self._ROW_LENGTH == self._ROW_LENGTH - 1: - memory_str += "\n" + def set_item(self, row: int, col: int, text: str) -> None: + """Set the text at specified table cell to the given text. + + Parameters + ---------- + row: int + The items row position in the pipeline diagram. + col: int + The items column position in the pipeline diagram. + text: str + The text to be displayed. + """ + item = QTableWidgetItem(text) - self.text.setText(memory_str) + self.setItem(row, col, item) + + def _set_column_size(self) -> None: + """ + Set the column size to a reasonable value if the size was not given to the constructor. + + This function assumes that the attributes `column_size` and `memory_size` are set before it is called. + """ + + if not self._column_size == -1: + return + + if self._memory_size > 200: + self._column_size = 4 + return + + if self._memory_size > 100: + self._column_size = 2 + return + + self._column_size = 1 + return class MemoryGraphicsItem(ModuleGraphicsItem): diff --git a/src/simudator/gui/pipeline.py b/src/simudator/gui/pipeline.py index 68534413f08e4a580a6fadb4608b71ed14b64d04..b2779420763b0f92ec243d5c5c4d2e85345db22a 100644 --- a/src/simudator/gui/pipeline.py +++ b/src/simudator/gui/pipeline.py @@ -1,6 +1,7 @@ -from qtpy.QtWidgets import QTableWidget, QTableWidgetItem from typing import Any +from qtpy.QtWidgets import QTableWidget, QTableWidgetItem + class PipeLine(QTableWidget): """ @@ -28,6 +29,8 @@ class PipeLine(QTableWidget): Instructions are set from get_current_instructions() in the Processor class. """ + def set_instruction(self, instructions: list[str]) -> None: + """Give the pipeline the current CPU instructions.""" self.instructions = instructions for i in range(len(self.instructions)): instruction = self.instructions[i][0] diff --git a/src/simudator/processor/mia/gui/mia_memory_graphic.py b/src/simudator/processor/mia/gui/mia_memory_graphic.py index 07c7b46ed5d4db658e161506978dc4ec50de2c02..303ddb8d9cccd9b2e53139c4b9de9fe0fdb26766 100644 --- a/src/simudator/processor/mia/gui/mia_memory_graphic.py +++ b/src/simudator/processor/mia/gui/mia_memory_graphic.py @@ -11,44 +11,15 @@ from qtpy.QtWidgets import ( from simudator.core.modules import Memory from simudator.gui.color_scheme import ColorScheme as CS -from simudator.gui.module_graphics_item.memory_graphic import MemoryGraphicsItem +from simudator.gui.module_graphics_item.memory_graphic import ( + MemoryGraphicsItem, + MemoryWindow, +) from simudator.gui.orientation import Orientation from simudator.gui.port_graphics_item import PortGraphicsItem from simudator.processor.mia.gui.mia_memory_content_dialog import MiaMemoryContentDialog -class MiaMemoryWindow(QWidget): - """ - Widget for showing content of memory - """ - - ROW_LENGTH = 4 - - def __init__(self, memory_module: Memory): - super().__init__() - self.module = memory_module - - self.text = QTextEdit("") - layout = QVBoxLayout() - layout.addWidget(self.text) - self.setLayout(layout) - - self.update() - - def update(self): - memory_str = "" - for address, value in enumerate(self.module.memory): - # Add address and content to string - # Make sure its unifrom lenght so rows are consistent - memory_str += f"0x{address:02x}" + ": " + f"0x{value:04x}" + " " + "\t" - - # Make new line when we reach end of row - if address % self.ROW_LENGTH == self.ROW_LENGTH - 1: - memory_str += "\n" - - self.text.setText(memory_str) - - class MiaMemoryGraphicsItem(MemoryGraphicsItem): """ Graphics module for a Memory module. @@ -99,7 +70,7 @@ class MiaMemoryGraphicsItem(MemoryGraphicsItem): Create and show a MemoryWindow that displays the contents of the memory module associated with this graphics item. """ - self.memory_window = MiaMemoryWindow(self.module) + self.memory_window = MemoryWindow(self.module) self.memory_window.show() def memoryBreakpointDialog(self) -> None: