Coverage for hdl_registers/generator/cpp/header.py: 100%
44 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the hdl-registers project, an HDL register generator fast enough to run
5# in real time.
6# https://hdl-registers.com
7# https://github.com/hdl-registers/hdl-registers
8# --------------------------------------------------------------------------------------------------
10# Standard libraries
11from pathlib import Path
12from typing import Any
14# Local folder libraries
15from .cpp_generator_common import CppGeneratorCommon
18class CppHeaderGenerator(CppGeneratorCommon):
19 """
20 Generate a C++ class header.
21 See the :ref:`generator_cpp` article for usage details.
23 The class header will contain:
25 * for each register, signature of getter and setter methods for reading/writing the register as
26 an ``uint``.
28 * for each field in each register, signature of getter and setter methods for reading/writing
29 the field as its native type (enumeration, positive/negative int, etc.).
31 * The setter will read-modify-write the register to update only the specified field,
32 depending on the mode of the register.
33 """
35 __version__ = "1.0.0"
37 SHORT_DESCRIPTION = "C++ header"
39 DEFAULT_INDENTATION_LEVEL = 4
41 @property
42 def output_file(self) -> Path:
43 """
44 Result will be placed in this file.
45 """
46 return self.output_folder / f"{self.name}.h"
48 def get_code(self, **kwargs: Any) -> str:
49 """
50 Get a complete C++ class header with methods for accessing registers and fields.
51 """
52 cpp_code = f" class {self._class_name} : public I{self._class_name}\n"
53 cpp_code += " {\n"
55 cpp_code += " private:\n"
56 cpp_code += " volatile uint32_t *m_registers;\n\n"
58 cpp_code += " public:\n"
59 cpp_code += f" {self._constructor_signature()};\n\n"
60 cpp_code += f" virtual ~{self._class_name}() {{}}\n"
62 def function(return_type_name: str, signature: str) -> str:
63 return f" virtual {return_type_name} {signature} const override;\n"
65 for register, register_array in self.iterate_registers():
66 cpp_code += f"\n{self.get_separator_line()}"
68 description = self._get_methods_description(
69 register=register, register_array=register_array
70 )
71 cpp_code += self.comment_block(
72 text=[description, "See interface header for documentation."]
73 )
75 if register.mode.software_can_read:
76 signature = self._register_getter_function_signature(
77 register=register, register_array=register_array
78 )
79 cpp_code += function(return_type_name="uint32_t", signature=signature)
81 for field in register.fields:
82 field_type_name = self._field_value_type_name(
83 register=register, register_array=register_array, field=field
84 )
86 signature = self._field_getter_function_signature(
87 register=register,
88 register_array=register_array,
89 field=field,
90 from_value=False,
91 )
92 cpp_code += function(return_type_name=field_type_name, signature=signature)
94 signature = self._field_getter_function_signature(
95 register=register,
96 register_array=register_array,
97 field=field,
98 from_value=True,
99 )
100 cpp_code += function(return_type_name=field_type_name, signature=signature)
102 if register.mode.software_can_write:
103 signature = self._register_setter_function_signature(
104 register=register, register_array=register_array
105 )
107 cpp_code += function(return_type_name="void", signature=signature)
109 for field in register.fields:
110 signature = self._field_setter_function_signature(
111 register=register,
112 register_array=register_array,
113 field=field,
114 from_value=False,
115 )
116 cpp_code += function(return_type_name="void", signature=signature)
118 signature = self._field_setter_function_signature(
119 register=register,
120 register_array=register_array,
121 field=field,
122 from_value=True,
123 )
124 cpp_code += function(return_type_name="uint32_t", signature=signature)
126 cpp_code += " };\n"
128 cpp_code_top = f"""\
129{self.header}
130#pragma once
132#include "i_{self.name}.h"
134"""
135 return cpp_code_top + self._with_namespace(cpp_code)