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

10 

11 

12class RegisterArray: 

13 

14 """ 

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

16 number of times in a register list. 

17 """ 

18 

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

20 """ 

21 Arguments: 

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

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

24 the register list. 

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

26 description (str): Textual register array description. 

27 """ 

28 self.name = name 

29 self.base_index = base_index 

30 self.length = length 

31 self.description = description 

32 

33 self.registers = [] 

34 

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

36 """ 

37 Append a register to this array. 

38 

39 Arguments: 

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

41 mode (str): A valid register mode. 

42 description (str): Textual register description. 

43 

44 Return: 

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

46 """ 

47 index = len(self.registers) 

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

49 

50 self.registers.append(register) 

51 return register 

52 

53 def get_register(self, name): 

54 """ 

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

56 

57 Arguments: 

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

59 Return: 

60 :class:`.Register`: The register. 

61 """ 

62 for register in self.registers: 

63 if register.name == name: 

64 return register 

65 

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

67 

68 @property 

69 def index(self): 

70 """ 

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

72 

73 Return: 

74 int: The highest index occupied by this array. 

75 """ 

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

77 

78 def get_start_index(self, array_index): 

79 """ 

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

81 

82 Arguments: 

83 array_index (int): The array iteration index. 

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

85 """ 

86 if array_index >= self.length: 

87 raise ValueError( 

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

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

90 ) 

91 

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

93 

94 def __repr__(self): 

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

96name={self.name},\ 

97base_index={self.base_index},\ 

98length={self.length},\ 

99description={self.description},\ 

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

101)"""