From 648447045c94f47cd2772a5a8826aa76aa85995f Mon Sep 17 00:00:00 2001
From: Martin <martin.hogstedt@hotmail.com>
Date: Tue, 2 Jul 2024 09:50:04 +0200
Subject: [PATCH] fixed the bug

---
 .../integer_memory_graphic.py                 | 24 ++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/simudator/gui/module_graphics_item/integer_memory_graphic.py b/src/simudator/gui/module_graphics_item/integer_memory_graphic.py
index 65c2ba3..ecf598b 100644
--- a/src/simudator/gui/module_graphics_item/integer_memory_graphic.py
+++ b/src/simudator/gui/module_graphics_item/integer_memory_graphic.py
@@ -17,6 +17,17 @@ class Base(Enum):
     HEXADECIMAL = 4
 
 
+class ValueToBig(Exception):
+    """
+    A class representing the error of a user inputting a value that does not fit in the memory.
+
+    Used to differentiate between wrong input using the exception python raises by default (ValueError)
+    and the error of a value that does not fit.
+    """
+
+    pass
+
+
 class IntegerMemoryWindow(MemoryWindow):
     """
     A class showing the contents of a memory module in a new window.
@@ -187,6 +198,7 @@ class IntegerMemoryTable(MemoryTable):
 
         item = self.item(row, col)
         value = item.text()
+        max_value = 2**self._bit_length
 
         # Turn every value into a positive int
         # in base 10
@@ -203,6 +215,10 @@ class IntegerMemoryTable(MemoryTable):
                 self._base == Base.DECIMAL_SIGNED or self._base == Base.DECIMAL_UNSIGNED
             ):
                 value = int(value)
+
+            if value > max_value:
+                raise ValueToBig
+
         except ValueError:
             msg = None
             if self._base == Base.BINARY:
@@ -217,12 +233,18 @@ class IntegerMemoryTable(MemoryTable):
             self.update()
             return
 
+        except ValueToBig:
+            self._errorMessageWidget.showMessage(
+                "Input value does not fit within the bit length."
+            )
+            self.update()
+            return
+
         # Turn negative signed numbers into their positive unsigned
         # counter part
         # Ex 4 bits:
         # -7 = 1001
         # -7 + 16 % 16 = 9 = 1001 = -7
-        max_value = 2**self._bit_length
         value = (value + max_value) % max_value
 
         index = row * 4 + col
-- 
GitLab