Coverage for hdl_registers/register_array.py: 100%

27 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 import Register 

12 

13 

14class RegisterArray: 

15 

16 """ 

17 Represent an array of registers. That is, a sequence of registers that shall be repeated a 

18 number of times in a register list. 

19 """ 

20 

21 def __init__(self, name, base_index, length, description): 

22 """ 

23 Arguments: 

24 name (str): The name of this register array. 

25 base_index (int): The zero-based index of the first register of this array in 

26 the register list. 

27 length (int): The number of times the register sequence shall be repeated. 

28 description (str): Textual register array description. 

29 """ 

30 self.name = name 

31 self.base_index = base_index 

32 self.length = length 

33 self.description = description 

34 

35 self.registers = [] 

36 

37 def append_register(self, name, mode, description): 

38 """ 

39 Append a register to this array. 

40 

41 Arguments: 

42 name (str): The name of the register. 

43 mode (str): A valid register mode. 

44 description (str): Textual register description. 

45 

46 Return: 

47 :class:`.Register`: The register object that was created. 

48 """ 

49 index = len(self.registers) 

50 register = Register(name, index, mode, description) 

51 

52 self.registers.append(register) 

53 return register 

54 

55 def get_register(self, name): 

56 """ 

57 Get a register from this array. Will raise exception if no register matches. 

58 

59 Arguments: 

60 name (str): The name of the register. 

61 Return: 

62 :class:`.Register`: The register. 

63 """ 

64 for register in self.registers: 

65 if register.name == name: 

66 return register 

67 

68 raise ValueError(f'Could not find register "{name}" within register array "{self.name}"') 

69 

70 @property 

71 def index(self): 

72 """ 

73 Property exists to be used analogously with ``Register.index``. 

74 

75 Return: 

76 int: The highest index occupied by this array. 

77 """ 

78 return self.base_index + self.length * len(self.registers) - 1 

79 

80 def get_start_index(self, array_index): 

81 """ 

82 The index within the register list where array iteration number ``array_index`` starts. 

83 

84 Arguments: 

85 array_index (int): The array iteration index. 

86 Shall be less than or equal to the array ``length``. 

87 """ 

88 if array_index >= self.length: 

89 raise ValueError( 

90 f'Index {array_index} out of range for register array "{self.name}" ' 

91 f"of length {self.length}." 

92 ) 

93 

94 return self.base_index + array_index * len(self.registers) 

95 

96 def __repr__(self): 

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

98name={self.name},\ 

99base_index={self.base_index},\ 

100length={self.length},\ 

101description={self.description},\ 

102registers={','.join([repr(register) for register in self.registers])},\ 

103)"""