String constants

Register constants can be of type string, i.e. a string of textual characters. This page will show you how the set up string constants, as well as showcase all the code that can be generated from it.

Usage in TOML

The TOML file below shows how to set up a register list with two string constants. Note that in the TOML, the type of the constant is determined by the type of the literal value.

TOML that sets up a register list with string constants.
 1# This will allocate a string constant with the name "supports_pre_filtering" of data type string.
 2[module_name]
 3
 4# The "type" property MUST be present and set to "constant".
 5type = "constant"
 6
 7# The "value" property MUST be present for a string constant.
 8# The value specified MUST be a string.
 9value = "preprocessing"
10
11# The "description" property is OPTIONAL for a constant.
12# Will default to "" if not specified.
13# The value specified MUST be a string.
14description = "The name of this module, to be used in FPGA as well as software."
15
16
17[module_description]
18
19type = "constant"
20value = "This module removes glitches and filters incoming data."

Note that the second constant does not have a description specified, meaning it will default to an empty string.

Below you will see how you can parse this TOML file and generate artifacts from it.

Usage with Python API

The Python code below shows

  1. How to parse the TOML file listed above.

  2. How to create an identical register list when instead using the Python API.

  3. How to generate register artifacts.

Note that the result of the create_from_api call is identical to that of the parse_toml call. Meaning that using a TOML file or using the Python API is completely equivalent. You choose yourself which method you want to use in your code base.

Python code that sets up a register list with string constants.
 1# Standard libraries
 2import sys
 3from pathlib import Path
 4
 5# First party libraries
 6from hdl_registers.generator.c.header import CHeaderGenerator
 7from hdl_registers.generator.cpp.interface import CppInterfaceGenerator
 8from hdl_registers.generator.html.page import HtmlPageGenerator
 9from hdl_registers.generator.vhdl.register_package import VhdlRegisterPackageGenerator
10from hdl_registers.parser.toml import from_toml
11from hdl_registers.register_list import RegisterList
12
13THIS_DIR = Path(__file__).parent
14
15
16def parse_toml() -> RegisterList:
17    """
18    Create the register list by parsing a TOML data file.
19    """
20    return from_toml(name="caesar", toml_file=THIS_DIR.parent / "toml" / "constant_string.toml")
21
22
23def create_from_api() -> RegisterList:
24    """
25    Alternative method: Create the register list by using the Python API.
26    """
27    register_list = RegisterList(name="caesar")
28
29    register_list.add_constant(
30        name="module_name",
31        value="preprocessing",
32        description="The name of this module, to be used in FPGA as well as software.",
33    )
34
35    register_list.add_constant(
36        name="module_description",
37        value="This module removes glitches and filters incoming data.",
38        description="",
39    )
40
41    return register_list
42
43
44def generate(register_list: RegisterList, output_folder: Path):
45    """
46    Generate the artifacts that we are interested in.
47    """
48    CHeaderGenerator(register_list=register_list, output_folder=output_folder).create()
49    CppInterfaceGenerator(register_list=register_list, output_folder=output_folder).create()
50    HtmlPageGenerator(register_list=register_list, output_folder=output_folder).create()
51    VhdlRegisterPackageGenerator(register_list=register_list, output_folder=output_folder).create()
52
53
54def main(output_folder: Path):
55    generate(register_list=parse_toml(), output_folder=output_folder / "toml")
56    generate(register_list=create_from_api(), output_folder=output_folder / "api")
57
58
59if __name__ == "__main__":
60    main(output_folder=Path(sys.argv[1]))

See RegisterList.add_constant() for more Python API details.

Generated code

See below for a description of the code that can be generated with these constants.

Note that the examples on this page set up a register list with only constants, no registers. This allowed of course, but albeit a little bit rare.

HTML page

See HTML file below for the human-readable documentation that is produced by the generate() call in the Python example above.

HTML page

VHDL package

The VHDL code below is produced by the generate() call in the Python example above. Click the button to expand and view the code.

Click to expand/collapse code.
Generated VHDL code.
 1-- -----------------------------------------------------------------------------
 2-- This file is automatically generated by hdl-registers version 6.2.1-dev2.
 3-- Code generator VhdlRegisterPackageGenerator version 1.0.0.
 4-- Generated 2024-12-29 20:51 at commit c518991abac6.
 5-- Register hash d07820360d5d22d88d27ca0ad4b90cade97cfc3f.
 6-- -----------------------------------------------------------------------------
 7
 8library ieee;
 9use ieee.std_logic_1164.all;
10use ieee.numeric_std.all;
11use ieee.fixed_pkg.all;
12
13library reg_file;
14use reg_file.reg_file_pkg.all;
15
16
17package caesar_regs_pkg is
18
19  -- ---------------------------------------------------------------------------
20  -- Values of register constants.
21  constant caesar_constant_module_name : string := "preprocessing";
22  constant caesar_constant_module_description : string := "This module removes glitches and filters incoming data.";
23
24end package;

C++ interface

The C++ interface header code below is produced by the generate() call in the Python example above. Click the button to expand and view the code.

Click to expand/collapse code.
Generated C++ interface class code.
 1// -----------------------------------------------------------------------------
 2// This file is automatically generated by hdl-registers version 6.2.1-dev2.
 3// Code generator CppInterfaceGenerator version 1.0.0.
 4// Generated 2024-12-29 20:51 at commit c518991abac6.
 5// Register hash d07820360d5d22d88d27ca0ad4b90cade97cfc3f.
 6// -----------------------------------------------------------------------------
 7
 8#pragma once
 9
10#include <sstream>
11#include <cstdint>
12#include <cstdlib>
13
14namespace fpga_regs
15{
16
17  class ICaesar
18  {
19  public:
20    // Register constant.
21    static constexpr auto module_name = "preprocessing";
22    // Register constant.
23    static constexpr auto module_description = "This module removes glitches and filters incoming data.";
24
25    // Number of registers within this register list.
26    static const size_t num_registers = 0uL;
27
28    virtual ~ICaesar() {}
29
30  };
31
32} /* namespace fpga_regs */

C header

The C code below is produced by the generate() call in the Python example above.

Click to expand/collapse code.
Generated C code.
 1// -----------------------------------------------------------------------------
 2// This file is automatically generated by hdl-registers version 6.2.1-dev2.
 3// Code generator CHeaderGenerator version 1.0.0.
 4// Generated 2024-12-29 20:51 at commit c518991abac6.
 5// Register hash d07820360d5d22d88d27ca0ad4b90cade97cfc3f.
 6// -----------------------------------------------------------------------------
 7
 8#ifndef CAESAR_REGS_H
 9#define CAESAR_REGS_H
10
11// Value of register constant 'module_name'.
12static char *CAESAR_MODULE_NAME = "preprocessing";
13// Value of register constant 'module_description'.
14static char *CAESAR_MODULE_DESCRIPTION = "This module removes glitches and filters incoming data.";
15
16// Number of registers within this register list.
17#define CAESAR_NUM_REGS (0u)
18
19// Type for this register list.
20typedef struct caesar_regs_t
21{
22} caesar_regs_t;
23
24#endif // CAESAR_REGS_H