Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

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

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

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

8 

9from .register_field import RegisterField 

10 

11 

12class Bit(RegisterField): 

13 

14 """ 

15 Used to represent a bit field in a register. 

16 """ 

17 

18 width = 1 

19 

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

21 """ 

22 Arguments: 

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

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

25 description (str): Textual bit array description. 

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

27 """ 

28 self.name = name 

29 self.base_index = index 

30 self.description = description 

31 

32 self._default_value = None 

33 # Assign self._default_value via setter 

34 self.default_value = default_value 

35 

36 @property 

37 def default_value(self): 

38 """ 

39 Getter for default_value. 

40 """ 

41 return self._default_value 

42 

43 @default_value.setter 

44 def default_value(self, value): 

45 """ 

46 Setter for default_value that performs sanity checks. 

47 """ 

48 if not isinstance(value, str): 

49 message = ( 

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

51 ) 

52 raise ValueError(message) 

53 

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

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

56 raise ValueError(message) 

57 

58 self._default_value = value 

59 

60 def get_value(self, register_value): 

61 shift = self.base_index 

62 mask = 1 << self.base_index 

63 value = (register_value & mask) >> shift 

64 return value 

65 

66 @property 

67 def range(self): 

68 return str(self.base_index) 

69 

70 @property 

71 def default_value_str(self): 

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

73 

74 @property 

75 def default_value_uint(self): 

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

77 

78 def __repr__(self): 

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

80name={self.name},\ 

81base_index={self.base_index}, 

82description={self.description}, 

83default_value={self.default_value}, 

84)"""