From 3f724b333684b1e89991a871dbdb25860e2f9b72 Mon Sep 17 00:00:00 2001 From: Fischer Moseley <42497969+fischermoseley@users.noreply.github.com> Date: Sat, 17 Feb 2024 21:34:17 -0800 Subject: [PATCH] add width check function to utils --- src/manta/logic_analyzer/__init__.py | 5 +++-- src/manta/logic_analyzer/fsm.py | 5 +++-- src/manta/logic_analyzer/trigger_block.py | 5 +++-- src/manta/memory_core.py | 5 +++-- src/manta/utils.py | 23 +++++++++++++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/manta/logic_analyzer/__init__.py b/src/manta/logic_analyzer/__init__.py index 66e62a0..b46a449 100644 --- a/src/manta/logic_analyzer/__init__.py +++ b/src/manta/logic_analyzer/__init__.py @@ -187,8 +187,9 @@ class LogicAnalyzerCore(Elaboratable): def get_max_addr(self): """ - Return the maximum addresses in memory used by the core. The address space used - by the core extends from `base_addr` to the number returned by this function. + Return the maximum addresses in memory used by the core. The address + space used by the core extends from `base_addr` to the number returned + by this function. """ return self.sample_mem.get_max_addr() diff --git a/src/manta/logic_analyzer/fsm.py b/src/manta/logic_analyzer/fsm.py index d90322a..461fa8b 100644 --- a/src/manta/logic_analyzer/fsm.py +++ b/src/manta/logic_analyzer/fsm.py @@ -52,8 +52,9 @@ class LogicAnalyzerFSM(Elaboratable): def get_max_addr(self): """ - Return the maximum addresses in memory used by the core. The address space used - by the core extends from `base_addr` to the number returned by this function. + Return the maximum addresses in memory used by the core. The address + space used by the core extends from `base_addr` to the number returned + by this function. """ return self.r.get_max_addr() diff --git a/src/manta/logic_analyzer/trigger_block.py b/src/manta/logic_analyzer/trigger_block.py index f0bf5f6..487a9c4 100644 --- a/src/manta/logic_analyzer/trigger_block.py +++ b/src/manta/logic_analyzer/trigger_block.py @@ -32,8 +32,9 @@ class LogicAnalyzerTriggerBlock(Elaboratable): def get_max_addr(self): """ - Return the maximum addresses in memory used by the core. The address space used - by the core extends from `base_addr` to the number returned by this function. + Return the maximum addresses in memory used by the core. The address + space used by the core extends from `base_addr` to the number returned + by this function. """ return self.r.get_max_addr() diff --git a/src/manta/memory_core.py b/src/manta/memory_core.py index a99549a..6d92ee4 100644 --- a/src/manta/memory_core.py +++ b/src/manta/memory_core.py @@ -152,8 +152,9 @@ class ReadOnlyMemoryCore(Elaboratable): def get_max_addr(self): """ - Return the maximum addresses in memory used by the core. The address space used - by the core extends from `base_addr` to the number returned by this function. + Return the maximum addresses in memory used by the core. The address + space used by the core extends from `base_addr` to the number returned + by this function. """ return self._max_addr diff --git a/src/manta/utils.py b/src/manta/utils.py index 1e63bcb..7fe12f2 100644 --- a/src/manta/utils.py +++ b/src/manta/utils.py @@ -38,16 +38,27 @@ def words_to_value(data): concatenates them together in little-endian order. """ - for d in data: - if d > 0 and d > 2**16 - 1: - raise ValueError("Unsigned integer too large.") - - if d < 0 and d < -(2**15): - raise ValueError("Signed integer too large.") + [check_value_fits_in_bits(d, 16) for d in data] return int("".join([f"{i:016b}" for i in data[::-1]]), 2) +def check_value_fits_in_bits(value, n_bits): + """ + Rasies an exception if the provided value isn't an integer that cannot + be expressed with the provided number of bits. + """ + + if not isinstance(value, int): + raise TypeError("Value must be an integer.") + + if value > 0 and value > 2**n_bits - 1: + raise ValueError("Unsigned integer too large.") + + if value < 0 and value < -(2 ** (n_bits - 1)): + raise ValueError("Signed integer too large.") + + def value_to_words(data, n_words): """ Takes a integer, interprets it as a set of 16-bit integers