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

37 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-09-27 20:00 +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( 

22 f"Expected {self.__class__.__name__} value to be of type str." 

23 f' Got type="{type(value)}", value="{value}".' 

24 ) 

25 

26 if " " in value: 

27 raise ValueError( 

28 f'Expected {self.__class__.__name__} value to not contain spaces. Got "{value}".' 

29 ) 

30 

31 self.value = value 

32 

33 def __str__(self): 

34 return self.value 

35 

36 

37class BitVectorGenericValue: 

38 """ 

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

40 """ 

41 

42 def __init__(self, value): 

43 """ 

44 Arguments: 

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

46 """ 

47 if not isinstance(value, str): 

48 raise ValueError( 

49 f"Expected {self.__class__.__name__} value to be of type str." 

50 f' Got type="{type(value)}", value="{value}".' 

51 ) 

52 

53 for bit_value in value: 

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

55 raise ValueError( 

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

57 f' Got "{value}".' 

58 ) 

59 

60 self.value = value 

61 

62 @property 

63 def length(self): 

64 """ 

65 int: The number of bits in the vector. 

66 """ 

67 return len(self.value) 

68 

69 def __str__(self): 

70 return self.value 

71 

72 

73def get_vivado_tcl_generic_value(value): 

74 """ 

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

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

77 

78 Arguments: 

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

80 """ 

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

82 if isinstance(value, bool): 

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

84 

85 if isinstance(value, int): 

86 return str(value) 

87 

88 if isinstance(value, float): 

89 return str(value) 

90 

91 if isinstance(value, BitVectorGenericValue): 

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

93 

94 if isinstance(value, StringGenericValue): 

95 return f'"{value.value}"' 

96 

97 message = f'Unsupported type for generic. Got type="{type(value)}", value="{value}".' 

98 

99 # When the type is a string, we can be a little more helpful and indicate what types shall 

100 # be used instead. 

101 if isinstance(value, str): 

102 message += ( 

103 " Please use either of the explicit types StringGenericValue or BitVectorGenericValue." 

104 ) 

105 

106 raise ValueError(message)