Coverage for hdl_registers/bit.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-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/tsfpga/hdl_registers 

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

9 

10from .register_field import RegisterField 

11 

12 

13class Bit(RegisterField): 

14 

15 """ 

16 Used to represent a bit field in a register. 

17 """ 

18 

19 width = 1 

20 

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

22 """ 

23 Arguments: 

24 name (str): The name of the bit array. 

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

26 description (str): Textual bit array description. 

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

28 """ 

29 self.name = name 

30 self._base_index = index 

31 self.description = description 

32 

33 self._default_value = None 

34 # Assign self._default_value via setter 

35 self.default_value = default_value 

36 

37 @property 

38 def base_index(self): 

39 """ 

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

41 """ 

42 return self._base_index 

43 

44 @property 

45 def default_value(self): 

46 """ 

47 Getter for default_value. 

48 """ 

49 return self._default_value 

50 

51 @default_value.setter 

52 def default_value(self, value): 

53 """ 

54 Setter for default_value that performs sanity checks. 

55 """ 

56 if not isinstance(value, str): 

57 message = ( 

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

59 ) 

60 raise ValueError(message) 

61 

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

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

64 raise ValueError(message) 

65 

66 self._default_value = value 

67 

68 @property 

69 def range(self): 

70 return str(self.base_index) 

71 

72 @property 

73 def default_value_str(self): 

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

75 

76 @property 

77 def default_value_uint(self): 

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

79 

80 def __repr__(self): 

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

82name={self.name},\ 

83base_index={self.base_index}, 

84description={self.description}, 

85default_value={self.default_value}, 

86)"""