Coverage for hdl_registers/bit.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-29 22:03 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the hdl_registers project, a HDL register generator fast enough to be run 

5# in real time. 

6# https://hdl-registers.com 

7# https://gitlab.com/hdl_registers/hdl_registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10# Local folder libraries 

11from .register_field import RegisterField 

12 

13 

14class Bit(RegisterField): 

15 

16 """ 

17 Used to represent a bit field in a register. 

18 """ 

19 

20 width = 1 

21 

22 def __init__(self, name, index, description, default_value): 

23 """ 

24 Arguments: 

25 name (str): The name of the bit. 

26 index (int): The zero-based index of this bit within the register. 

27 description (str): Textual bit description. 

28 default_value (str): Default value. Either "1" or "0". 

29 """ 

30 self.name = name 

31 self._base_index = index 

32 self.description = description 

33 

34 self._default_value = None 

35 # Assign self._default_value via setter 

36 self.default_value = default_value 

37 

38 @property 

39 def base_index(self): 

40 """ 

41 The index within the register for the lowest bit of this Field. 

42 """ 

43 return self._base_index 

44 

45 @property 

46 def default_value(self): 

47 """ 

48 Getter for default_value. 

49 """ 

50 return self._default_value 

51 

52 @default_value.setter 

53 def default_value(self, value): 

54 """ 

55 Setter for default_value that performs sanity checks. 

56 """ 

57 if not isinstance(value, str): 

58 message = ( 

59 f'Bit "{self.name}" should have string value for "default_value". Got "{value}".' 

60 ) 

61 raise ValueError(message) 

62 

63 if value not in ["0", "1"]: 

64 message = f'Bit "{self.name}" invalid binary value for "default_value". Got: "{value}".' 

65 raise ValueError(message) 

66 

67 self._default_value = value 

68 

69 @property 

70 def range(self): 

71 return str(self.base_index) 

72 

73 @property 

74 def default_value_str(self): 

75 return f"0b{self.default_value}" 

76 

77 @property 

78 def default_value_uint(self): 

79 return int(self.default_value, base=2) 

80 

81 def __repr__(self): 

82 return f"""{self.__class__.__name__}(\ 

83name={self.name},\ 

84base_index={self.base_index}, 

85description={self.description}, 

86default_value={self.default_value}, 

87)"""