Writing a custom code generator

The register code generator API in hdl-registers is carefully designed to be very extensible. Writing and using your own custom code generator is very simple.

The key is to inherit the RegisterCodeGenerator class, as seen in the example below.


The example below is a simple “code” generator that will dump the names of each register to a text file.

Example of a custom code generator.
 1# Standard libraries
 2import sys
 3from pathlib import Path
 5# First party libraries
 6from hdl_registers.generator.register_code_generator import RegisterCodeGenerator
 7from hdl_registers.generator.register_code_generator_helpers import RegisterCodeGeneratorHelpers
 8from hdl_registers.register_list import RegisterList
11class TxtRegisterListGenerator(RegisterCodeGenerator, RegisterCodeGeneratorHelpers):
13    """
14    Custom code generator that generates a .txt file with all register names.
15    """
17    SHORT_DESCRIPTION = "text list"
18    COMMENT_START = "#"
20    @property
21    def output_file(self) -> Path:
22        """
23        Result will be placed in this file.
24        """
25        return self.output_folder / f"{self.name}_registers.txt"
27    def get_code(self, **kwargs) -> str:
28        """
29        Generate a textual list of all registers and register arrays.
30        """
31        txt = f'{self.header}\nAvailable registers in the "{self.name}" register list:\n\n'
33        for register, register_array in self.iterate_registers():
34            if register_array:
35                # This register is part of a register array.
36                txt += f"{register_array.name}[{register_array.length}].{register.name}\n"
37            else:
38                # This register is a plain register.
39                txt += f"{register.name}\n"
41        return txt
44def main(output_folder: Path):
45    """
46    Set up some registers and generate text file with our custom generator.
47    """
48    register_list = RegisterList(name="caesar")
50    register_list.append_register(name="config", mode="r_w", description="")
51    register_list.append_register(name="status", mode="r", description="")
52    register_list.append_register(name="command", mode="wpulse", description="")
54    register_array = register_list.append_register_array(name="channels", length=4, description="")
55    register_array.append_register(name="read_address", mode="r_w", description="")
56    register_array.append_register(name="write_address", mode="r_w", description="")
58    TxtRegisterListGenerator(register_list=register_list, output_folder=output_folder).create()
61if __name__ == "__main__":
62    main(output_folder=Path(sys.argv[1]))

The COMMENT_START, SHORT_DESCRIPTION and output_file class properties are abstract in the super class, and MUST hence be implemented in the subclass. The same is true, naturally, for the get_code method which is where all the code generation happens.

The generator class inheriting from RegisterCodeGenerator means that it has the public methods RegisterCodeGenerator.create(), RegisterCodeGenerator.create_if_needed() and RegisterCodeGenerator.header() just like the standard hdl-registers generators.

If the custom generator also inherits from RegisterCodeGeneratorHelpers, it has access to some useful functions for constructing the generated code. Used in the example above is RegisterCodeGeneratorHelpers.iterate_registers().

Running the example script above will yield the following result file:

Result text file from custom code generator.
# This file is automatically generated by hdl-registers version 4.1.1-dev2.
# Code generator TxtRegisterListGenerator version 0.0.1.
# Generated 2023-12-04 10:43 at commit d6ece437d0e3b2bc.
# Register hash 61b9927ebb802ac7b052809196c72bcffa905fda.

Available registers in the "caesar" register list:


Note that the header uses the COMMENT_START character to start each line. This header is used by RegisterCodeGenerator.create_if_needed() to determine if a re-generate of the file is necessary.

Running the script above yields the following shell printout:

Shell output when running custom code generator.
Creating text list file: /home/runner/work/hdl-registers/hdl-registers/generated/sphinx_rst/register_code/extensions/extensions_custom_generator/caesar_registers.txt

Note that the “text list” part of the printout is the SHORT_DESCRIPTION property of our generator class above. The file name is of course also given by output_file of our generator class.


If you write a high-quality code generator you are more than welcome to contribute it to the hdl-registers project. Please open an issue or a pull request.


Since code generation is usually quite slow, there is a mechanism in place for speeding up the process and giving a better user experience. For code generation that is time-critical, such as generating a register package before a user simulation run, it is recommended to call the RegisterCodeGenerator.create_if_needed() method. This method will only re-create the artifact if some register definition has changed or a tool version is bumped. Hence using this method as opposed to RegisterCodeGenerator.create() saves a lot of time, especially in a large project.

Code templating engine

The code generation in a custom code generator can be done either using plain Python, as in the example above, or using an engine such as Jinja. This is completely up to the user. The custom code generator is pure Python and you are free to be as fancy/creative as you want.

Custom arguments

The RegisterCodeGenerator.create(), RegisterCodeGenerator.create_if_needed() and RegisterCodeGenerator.get_code() all have a **kwargs argument available. If you want to send further information/arguments to the code generator for some exotic feature, this makes it possible.