Coverage for tsfpga/vivado/generics.py: 100%
17 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-21 23:39 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-21 23:39 +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
11from tsfpga.generics import BitVectorGenericValue, GenericValue, StringGenericValue
14def get_vivado_tcl_generic_value(
15 value: GenericValue,
16) -> str:
17 """
18 Convert generic values of different types to the format recognized by Vivado TCL:
19 https://www.xilinx.com/support/answers/52217.html
21 Arguments:
22 value: A generic value of native Python type..
24 Return:
25 The ``value`` formatted as TCL.
26 """
27 # Note that bool is a sub-class of int in Python, so check for bool must be first
28 if isinstance(value, bool):
29 return f"1'b{int(value)}"
31 if isinstance(value, int):
32 return str(value)
34 if isinstance(value, float):
35 return str(value)
37 if isinstance(value, BitVectorGenericValue):
38 return f"{value.length}'b{value.value}"
40 if isinstance(value, StringGenericValue):
41 return f'"{value.value}"'
43 message = f'Unsupported type for generic. Got type="{type(value)}", value="{value}".'
45 # When the type is a string, we can be a little more helpful and indicate what types shall
46 # be used instead.
47 if isinstance(value, str):
48 message += (
49 " Please use either of the explicit types StringGenericValue or BitVectorGenericValue."
50 )
52 raise TypeError(message)