Coverage for hdl_registers/field/bit.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-19 20:51 +0000

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

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

3# 

4# This file is part of the hdl-registers project, an HDL register generator fast enough to run 

5# in real time. 

6# https://hdl-registers.com 

7# https://github.com/hdl-registers/hdl-registers 

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

9 

10# Local folder libraries 

11from .register_field import RegisterField 

12 

13 

14class Bit(RegisterField): 

15 """ 

16 Used to represent a bit field in a register. 

17 """ 

18 

19 _width = 1 

20 

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

22 """ 

23 Arguments: 

24 name: The name of the bit. 

25 index: The zero-based index of this bit within the register. 

26 description: Textual field description. 

27 default_value: 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 = "" 

34 # Assign self._default_value via setter 

35 self.default_value = default_value 

36 

37 @property # type: ignore[override] 

38 def default_value(self) -> str: 

39 """ 

40 Getter for private member. 

41 """ 

42 return self._default_value 

43 

44 @default_value.setter 

45 def default_value(self, value: str) -> None: 

46 """ 

47 Setter for ``default_value`` that performs sanity checks. 

48 """ 

49 if not isinstance(value, str): 

50 message = ( 

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

52 ) 

53 raise ValueError(message) 

54 

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

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

57 raise ValueError(message) 

58 

59 self._default_value = value 

60 

61 @property 

62 def default_value_uint(self) -> int: 

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

64 

65 def __repr__(self) -> str: 

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

67name={self.name},\ 

68_base_index={self._base_index},\ 

69description={self.description},\ 

70_default_value={self._default_value},\ 

71)"""