Coverage for hdl_registers/register_code_generator.py: 95%

20 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 RegisterCodeGenerator: 

14 

15 """ 

16 Common functions for generating register code. 

17 """ 

18 

19 @staticmethod 

20 def _iterate_registers(register_objects): 

21 for register_object in register_objects: 

22 if isinstance(register_object, Register): 

23 yield (register_object, None) 

24 else: 

25 for register in register_object.registers: 

26 yield (register, register_object) 

27 

28 @staticmethod 

29 def _comment(comment, indentation=0): 

30 raise NotImplementedError("Should be overloaded in child class") 

31 

32 def _comment_block(self, text, indentation=0): 

33 """ 

34 Create a comment block from a string with newlines. 

35 """ 

36 text_lines = text.split("\n") 

37 

38 # Very common that the last line is empty. An effect of TOML formatting with 

39 # multi-line strings. Remove to make the output look more clean. 

40 if text_lines[-1] == "": 

41 text_lines.pop() 

42 

43 return "".join(self._comment(line, indentation=indentation) for line in text_lines) 

44 

45 @staticmethod 

46 def _to_pascal_case(snake_string): 

47 """ 

48 Returns e.g., my_funny_string -> MyFunnyString 

49 """ 

50 return snake_string.title().replace("_", "")