Coverage for tsfpga/vivado/generics.py: 97%

34 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +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://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9 

10class StringGenericValue: 

11 """ 

12 Use this type for generic values of type ``string``. 

13 """ 

14 

15 def __init__(self, value): 

16 """ 

17 Arguments: 

18 value (str): A string of variable length with any content. 

19 """ 

20 if not isinstance(value, str): 

21 raise ValueError(f"Expected {self.__class__.__name__} value to be of type str: {value}") 

22 

23 if " " in value: 

24 raise ValueError( 

25 f"Expected {self.__class__.__name__} value to not contain spaces: {value}" 

26 ) 

27 

28 self.value = value 

29 

30 def __str__(self): 

31 return self.value 

32 

33 

34class BitVectorGenericValue: 

35 """ 

36 Use this type for generic values of type ``std_logic_vector``. 

37 """ 

38 

39 def __init__(self, value): 

40 """ 

41 Arguments: 

42 value (str): A string of variable length containing only "1" or "0". 

43 """ 

44 if not isinstance(value, str): 

45 raise ValueError(f"Expected {self.__class__.__name__} value to be of type str: {value}") 

46 

47 for bit_value in value: 

48 if bit_value not in ["1", "0"]: 

49 raise ValueError( 

50 f'Expected {self.__class__.__name__} value to contain only "1" or "0": {value}' 

51 ) 

52 

53 self.value = value 

54 

55 @property 

56 def length(self): 

57 """ 

58 int: The number of bits in the vector. 

59 """ 

60 return len(self.value) 

61 

62 def __str__(self): 

63 return self.value 

64 

65 

66def get_vivado_tcl_generic_value(value): 

67 """ 

68 Convert generic values of different types to the format recognized by Vivado TCL: 

69 https://www.xilinx.com/support/answers/52217.html 

70 

71 Arguments: 

72 value (bool, int, float, StringGenericValue, BitVectorGenericValue): A generic value. 

73 """ 

74 # Note that bool is a sub-class of int in Python, so check for bool must be first 

75 if isinstance(value, bool): 

76 return f"1'b{int(value)}" 

77 

78 if isinstance(value, int): 

79 return str(value) 

80 

81 if isinstance(value, float): 

82 return str(value) 

83 

84 if isinstance(value, BitVectorGenericValue): 

85 return f"{value.length}'b{value.value}" 

86 

87 if isinstance(value, StringGenericValue): 

88 return value.value 

89 

90 raise ValueError(f"Got unsupported type for generic. Type={type(value)}, value={value}.")