Working with Python API

The recommended way of defining your registers and fields is to use a .toml data file. However depending on your needs it might make sense to define them directly in Python instead. This page will showcase a simple example of how to get started with this.

The Python code below sets up a RegisterList with two Registers and some fields. After this it performs a VHDL code generation.

Register TOML format example.
 1# Standard libraries
 2import sys
 3from pathlib import Path
 4
 5# First party libraries
 6from hdl_registers.generator.vhdl.register_package import VhdlRegisterPackageGenerator
 7from hdl_registers.register_list import RegisterList
 8from hdl_registers.register_modes import REGISTER_MODES
 9
10
11def main(output_folder: Path):
12    """
13    Set up some dummy registers and fields using the Python API.
14    """
15    register_list = RegisterList(name="caesar")
16
17    register = register_list.append_register(
18        name="config", mode=REGISTER_MODES["r_w"], description="Configuration register."
19    )
20
21    register.append_bit_vector(
22        name="tuser",
23        description="Value to set for **TUSER** in the data stream.",
24        width=4,
25        default_value="0101",
26    )
27
28    register.append_integer(
29        name="increment",
30        description="Offset that will be added to data.",
31        min_value=-4,
32        max_value=3,
33        default_value=0,
34    )
35
36    register = register_list.append_register(
37        name="status", mode=REGISTER_MODES["r"], description="General status register."
38    )
39
40    VhdlRegisterPackageGenerator(register_list=register_list, output_folder=output_folder).create()
41
42
43if __name__ == "__main__":
44    main(output_folder=Path(sys.argv[1]))

The articles in the the menubar on the left all showcase how to utilize the different features using the Python API as well as a TOML file. Please read these articles for further information.