Coverage for tsfpga/generics.py: 96%
25 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-09 00:40 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-09 00:40 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the tsfpga project, a project platform for modern FPGA development.
5# https://tsfpga.com
6# https://github.com/tsfpga/tsfpga
7# --------------------------------------------------------------------------------------------------
9from __future__ import annotations
12class StringGenericValue:
13 """
14 Use this type for generic values of type ``string``.
15 """
17 def __init__(self, value: str) -> None:
18 """
19 Arguments:
20 value: A string of variable length with any content.
21 """
22 if not isinstance(value, str):
23 raise TypeError(
24 f"Expected {self.__class__.__name__} value to be of type str."
25 f' Got type="{type(value)}", value="{value}".'
26 )
28 if " " in value:
29 raise ValueError(
30 f'Expected {self.__class__.__name__} value to not contain spaces. Got "{value}".'
31 )
33 self.value = value
35 def __str__(self) -> str:
36 return self.value
39class BitVectorGenericValue:
40 """
41 Use this type for generic values of type ``std_logic_vector``.
42 """
44 def __init__(self, value: str) -> None:
45 """
46 Arguments:
47 value: A string of variable length containing only "1" or "0".
48 """
49 if not isinstance(value, str):
50 raise TypeError(
51 f"Expected {self.__class__.__name__} value to be of type str."
52 f' Got type="{type(value)}", value="{value}".'
53 )
55 for bit_value in value:
56 if bit_value not in ["1", "0"]:
57 raise ValueError(
58 f'Expected {self.__class__.__name__} value to contain only "1" or "0".'
59 f' Got "{value}".'
60 )
62 self.value = value
64 @property
65 def length(self) -> int:
66 """
67 The number of bits in the vector.
68 """
69 return len(self.value)
71 def __str__(self) -> str:
72 return self.value
75#: A generic value of a native Python type, or one of the tsfpga wrapper classes above.
76GenericValue = bool | int | float | StringGenericValue | BitVectorGenericValue
78#: A mapping of generic name to :data:`.GenericValue`, as accepted by the build project classes.
79GenericValues = dict[str, GenericValue]