add width check function to utils

This commit is contained in:
Fischer Moseley 2024-02-17 21:34:17 -08:00
parent ea6b3f73b9
commit 3f724b3336
5 changed files with 29 additions and 14 deletions

View File

@ -187,8 +187,9 @@ class LogicAnalyzerCore(Elaboratable):
def get_max_addr(self): def get_max_addr(self):
""" """
Return the maximum addresses in memory used by the core. The address space used Return the maximum addresses in memory used by the core. The address
by the core extends from `base_addr` to the number returned by this function. space used by the core extends from `base_addr` to the number returned
by this function.
""" """
return self.sample_mem.get_max_addr() return self.sample_mem.get_max_addr()

View File

@ -52,8 +52,9 @@ class LogicAnalyzerFSM(Elaboratable):
def get_max_addr(self): def get_max_addr(self):
""" """
Return the maximum addresses in memory used by the core. The address space used Return the maximum addresses in memory used by the core. The address
by the core extends from `base_addr` to the number returned by this function. space used by the core extends from `base_addr` to the number returned
by this function.
""" """
return self.r.get_max_addr() return self.r.get_max_addr()

View File

@ -32,8 +32,9 @@ class LogicAnalyzerTriggerBlock(Elaboratable):
def get_max_addr(self): def get_max_addr(self):
""" """
Return the maximum addresses in memory used by the core. The address space used Return the maximum addresses in memory used by the core. The address
by the core extends from `base_addr` to the number returned by this function. space used by the core extends from `base_addr` to the number returned
by this function.
""" """
return self.r.get_max_addr() return self.r.get_max_addr()

View File

@ -152,8 +152,9 @@ class ReadOnlyMemoryCore(Elaboratable):
def get_max_addr(self): def get_max_addr(self):
""" """
Return the maximum addresses in memory used by the core. The address space used Return the maximum addresses in memory used by the core. The address
by the core extends from `base_addr` to the number returned by this function. space used by the core extends from `base_addr` to the number returned
by this function.
""" """
return self._max_addr return self._max_addr

View File

@ -38,16 +38,27 @@ def words_to_value(data):
concatenates them together in little-endian order. concatenates them together in little-endian order.
""" """
for d in data: [check_value_fits_in_bits(d, 16) 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.")
return int("".join([f"{i:016b}" for i in data[::-1]]), 2) 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): def value_to_words(data, n_words):
""" """
Takes a integer, interprets it as a set of 16-bit integers Takes a integer, interprets it as a set of 16-bit integers