Coverage for hdl_registers/register_array.py: 100%

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

11 

12 

13class RegisterArray: 

14 

15 """ 

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

17 number of times in a register list. 

18 """ 

19 

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

21 """ 

22 Arguments: 

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

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

25 the register list. 

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

27 description (str): Textual register array description. 

28 """ 

29 self.name = name 

30 self.base_index = base_index 

31 self.length = length 

32 self.description = description 

33 

34 self.registers = [] 

35 

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

37 """ 

38 Append a register to this array. 

39 

40 Arguments: 

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

42 mode (str): A valid register mode. 

43 description (str): Textual register description. 

44 

45 Return: 

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

47 """ 

48 index = len(self.registers) 

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

50 

51 self.registers.append(register) 

52 return register 

53 

54 def get_register(self, name): 

55 """ 

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

57 

58 Arguments: 

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

60 Return: 

61 :class:`.Register`: The register. 

62 """ 

63 for register in self.registers: 

64 if register.name == name: 

65 return register 

66 

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

68 

69 @property 

70 def index(self): 

71 """ 

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

73 

74 Return: 

75 int: The highest index occupied by this array. 

76 """ 

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

78 

79 def get_start_index(self, array_index): 

80 """ 

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

82 

83 Arguments: 

84 array_index (int): The array iteration index. 

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

86 """ 

87 if array_index >= self.length: 

88 raise ValueError( 

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

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

91 ) 

92 

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

94 

95 def __repr__(self): 

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

97name={self.name},\ 

98base_index={self.base_index},\ 

99length={self.length},\ 

100description={self.description},\ 

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

102)"""