Integer fields

Register fields can be of the type integer. Meaning, a numeric field, as opposed to a bit vector, that has a defined upper and lower range.

This page will show you how the set up integer fields in a register, and will showcase all the code that can be generated from it.

Usage in TOML

The TOML file below shows how to set up a register with three integer fields. See comments for rules about the different properties.

TOML that sets up a register with integer fields.
 1[conf]
 2
 3mode = "r_w"
 4description = "Configuration register."
 5
 6# This will allocate an integer field named "burst_length_bytes" in the "conf" register.
 7# The "type" property MUST be present and set to "integer".
 8burst_length_bytes.type = "integer"
 9
10# The "min_value" property is OPTIONAL for an integer field.
11# Will default to 0 if not specified.
12# The value specified MUST be an integer.
13burst_length_bytes.min_value = 1
14
15# The "max_value" property MUST be present for an integer field.
16# The value specified MUST be an integer, and greater than or equal to the
17# "min_value" parameter value.
18burst_length_bytes.max_value = 256
19
20# The "description" property is OPTIONAL for an integer field.
21# Will default to "" if not specified.
22# The value specified MUST be a string.
23burst_length_bytes.description = "The number of bytes to request."
24
25# The "default_value" property is OPTIONAL for a integer field.
26# Will default to the "min_value" parameter value if not specified.
27# The value specified MUST be an integer within the specified min/max range.
28burst_length_bytes.default_value = 64
29
30
31increment.type = "integer"
32increment.min_value = -4
33increment.max_value = 3
34increment.description = "Offset that will be added to data."
35increment.default_value = 0
36
37
38retry_count.type = "integer"
39retry_count.max_value = 5
40retry_count.description = "Number of retry attempts before giving up."

Note that the second field has a negative range, which is fully supported. Note also that the third field does not any lower bound specified, meaning it will default to zero. It furthermore does not have any default value, meaning it will automatically default to the lower bound, i.e. zero.

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 with integer fields.
 1import sys
 2from pathlib import Path
 3
 4from hdl_registers.generator.c.header import CHeaderGenerator
 5from hdl_registers.generator.cpp.implementation import CppImplementationGenerator
 6from hdl_registers.generator.cpp.interface import CppInterfaceGenerator
 7from hdl_registers.generator.html.page import HtmlPageGenerator
 8from hdl_registers.generator.vhdl.record_package import VhdlRecordPackageGenerator
 9from hdl_registers.generator.vhdl.register_package import VhdlRegisterPackageGenerator
10from hdl_registers.parser.toml import from_toml
11from hdl_registers.register_list import RegisterList
12from hdl_registers.register_modes import REGISTER_MODES
13
14THIS_DIR = Path(__file__).parent
15
16
17def parse_toml() -> RegisterList:
18    """
19    Create the register list by parsing a TOML data file.
20    """
21    return from_toml(name="caesar", toml_file=THIS_DIR.parent / "toml" / "field_integer.toml")
22
23
24def create_from_api() -> RegisterList:
25    """
26    Alternative method: Create the register list by using the Python API.
27    """
28    register_list = RegisterList(name="caesar")
29
30    register = register_list.append_register(
31        name="conf", mode=REGISTER_MODES["r_w"], description="Configuration register."
32    )
33
34    register.append_integer(
35        name="burst_length_bytes",
36        description="The number of bytes to request.",
37        min_value=1,
38        max_value=256,
39        default_value=64,
40    )
41
42    register.append_integer(
43        name="increment",
44        description="Offset that will be added to data.",
45        min_value=-4,
46        max_value=3,
47        default_value=0,
48    )
49
50    register.append_integer(
51        name="retry_count",
52        description="Number of retry attempts before giving up.",
53        min_value=0,
54        max_value=5,
55        default_value=0,
56    )
57
58    return register_list
59
60
61def generate(register_list: RegisterList, output_folder: Path) -> None:
62    """
63    Generate the artifacts that we are interested in.
64    """
65    CHeaderGenerator(register_list=register_list, output_folder=output_folder).create()
66
67    CppImplementationGenerator(register_list=register_list, output_folder=output_folder).create()
68    CppInterfaceGenerator(register_list=register_list, output_folder=output_folder).create()
69
70    HtmlPageGenerator(register_list=register_list, output_folder=output_folder).create()
71
72    VhdlRegisterPackageGenerator(register_list=register_list, output_folder=output_folder).create()
73    VhdlRecordPackageGenerator(register_list=register_list, output_folder=output_folder).create()
74
75
76def main(output_folder: Path) -> None:
77    generate(register_list=parse_toml(), output_folder=output_folder / "toml")
78    generate(register_list=create_from_api(), output_folder=output_folder / "api")
79
80
81if __name__ == "__main__":
82    main(output_folder=Path(sys.argv[1]))

See Register.append_integer() and the Integer class for more Python API details.

Generated code

See below for a description of the code that can be generated when using integer fields.

HTML page

See HTML file below for the human-readable documentation that is produced by the generate() call in the Python example above. Each integer field is documented with its valid range.

See HTML generator for more details about the HTML generator and its capabilities.

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. See VHDL generator for instructions on how it can be used in your VHDL project.

Base register package

Some interesting things to notice:

  1. There is only one register, at index 0.

  2. VHDL supports integer types natively. For each field there is a sub-type that is a properly ranged integer.

  3. For each integer field, there are conversion functions for

    1. Converting from the integer type to std_logic_vector.

    2. Slicing a register value at the correct range and converting from std_logic_vector to integer.

Click to expand/collapse code.
Generated VHDL register package.
  1-- -----------------------------------------------------------------------------
  2-- This file is automatically generated by hdl-registers version 7.3.1-dev.
  3-- Code generator VhdlRegisterPackageGenerator version 2.0.0.
  4-- Generated 2025-04-26 09:24 at Git commit e16bd7741c27.
  5-- Register hash 78096d26a817ed466b1fc4fb8e1d3585885bdcb7.
  6-- -----------------------------------------------------------------------------
  7
  8library ieee;
  9use ieee.std_logic_1164.all;
 10use ieee.numeric_std.all;
 11use ieee.fixed_pkg.all;
 12
 13library register_file;
 14use register_file.register_file_pkg.all;
 15
 16
 17package caesar_regs_pkg is
 18
 19  -- ---------------------------------------------------------------------------
 20  -- The valid range of register indexes.
 21  subtype caesar_register_range is natural range 0 to 0;
 22
 23  -- ---------------------------------------------------------------------------
 24  -- The number of bits needed to address all 1 registers on a register bus.
 25  -- Note that this figure includes the lowest two address bits that are assumed zero, since
 26  -- registers are 32-bit and unaligned accesses are not supported.
 27  constant caesar_address_width : positive := 3;
 28
 29  -- Register indexes, within the list of registers.
 30  constant caesar_conf : natural := 0;
 31
 32  -- Declare 'register_map' and 'regs_init' constants here but define them in
 33  -- the package body (deferred constants).
 34  -- So that functions have been elaborated when they are called.
 35  -- Needed for ModelSim compilation to pass.
 36
 37  -- To be used as the 'registers' generic of 'axi_lite_register_file.vhd'.
 38  constant caesar_register_map : register_definition_vec_t(caesar_register_range);
 39
 40  -- To be used for the 'regs_up' and 'regs_down' ports of 'axi_lite_register_file.vhd'.
 41  subtype caesar_regs_t is register_vec_t(caesar_register_range);
 42  -- To be used as the 'default_values' generic of 'axi_lite_register_file.vhd'.
 43  constant caesar_regs_init : caesar_regs_t;
 44
 45  -- To be used for the 'reg_was_read' and 'reg_was_written' ports of 'axi_lite_register_file.vhd'.
 46  subtype caesar_reg_was_accessed_t is std_ulogic_vector(caesar_register_range);
 47
 48  -- -----------------------------------------------------------------------------
 49  -- Fields in the 'conf' register.
 50  -- Range of the 'burst_length_bytes' field.
 51  subtype caesar_conf_burst_length_bytes is natural range 8 downto 0;
 52  -- Width of the 'burst_length_bytes' field.
 53  constant caesar_conf_burst_length_bytes_width : positive := 9;
 54  -- Type for the 'burst_length_bytes' field.
 55  subtype caesar_conf_burst_length_bytes_t is integer range 1 to 256;
 56  -- Default value of the 'burst_length_bytes' field.
 57  constant caesar_conf_burst_length_bytes_init : caesar_conf_burst_length_bytes_t := 64;
 58  -- Type for the 'burst_length_bytes' field as an SLV.
 59  subtype caesar_conf_burst_length_bytes_slv_t is std_ulogic_vector(8 downto 0);
 60  -- Cast a 'burst_length_bytes' field value to SLV.
 61  function to_caesar_conf_burst_length_bytes_slv(data : caesar_conf_burst_length_bytes_t) return caesar_conf_burst_length_bytes_slv_t;
 62  -- Get a 'burst_length_bytes' field value from a register value.
 63  function to_caesar_conf_burst_length_bytes(data : register_t) return caesar_conf_burst_length_bytes_t;
 64
 65  -- Range of the 'increment' field.
 66  subtype caesar_conf_increment is natural range 11 downto 9;
 67  -- Width of the 'increment' field.
 68  constant caesar_conf_increment_width : positive := 3;
 69  -- Type for the 'increment' field.
 70  subtype caesar_conf_increment_t is integer range -4 to 3;
 71  -- Default value of the 'increment' field.
 72  constant caesar_conf_increment_init : caesar_conf_increment_t := 0;
 73  -- Type for the 'increment' field as an SLV.
 74  subtype caesar_conf_increment_slv_t is std_ulogic_vector(2 downto 0);
 75  -- Cast a 'increment' field value to SLV.
 76  function to_caesar_conf_increment_slv(data : caesar_conf_increment_t) return caesar_conf_increment_slv_t;
 77  -- Get a 'increment' field value from a register value.
 78  function to_caesar_conf_increment(data : register_t) return caesar_conf_increment_t;
 79
 80  -- Range of the 'retry_count' field.
 81  subtype caesar_conf_retry_count is natural range 14 downto 12;
 82  -- Width of the 'retry_count' field.
 83  constant caesar_conf_retry_count_width : positive := 3;
 84  -- Type for the 'retry_count' field.
 85  subtype caesar_conf_retry_count_t is integer range 0 to 5;
 86  -- Default value of the 'retry_count' field.
 87  constant caesar_conf_retry_count_init : caesar_conf_retry_count_t := 0;
 88  -- Type for the 'retry_count' field as an SLV.
 89  subtype caesar_conf_retry_count_slv_t is std_ulogic_vector(2 downto 0);
 90  -- Cast a 'retry_count' field value to SLV.
 91  function to_caesar_conf_retry_count_slv(data : caesar_conf_retry_count_t) return caesar_conf_retry_count_slv_t;
 92  -- Get a 'retry_count' field value from a register value.
 93  function to_caesar_conf_retry_count(data : register_t) return caesar_conf_retry_count_t;
 94
 95end package;
 96
 97package body caesar_regs_pkg is
 98
 99  constant caesar_register_map : register_definition_vec_t(caesar_register_range) := (
100    0 => (index => caesar_conf, mode => r_w, utilized_width => 15)
101  );
102
103  constant caesar_regs_init : caesar_regs_t := (
104    0 => "00000000000000000000000001000000"
105  );
106
107  -- Cast a 'burst_length_bytes' field value to SLV.
108  function to_caesar_conf_burst_length_bytes_slv(data : caesar_conf_burst_length_bytes_t) return caesar_conf_burst_length_bytes_slv_t is
109    constant result : caesar_conf_burst_length_bytes_slv_t := std_ulogic_vector(to_unsigned(data, caesar_conf_burst_length_bytes_width));
110  begin
111    return result;
112  end function;
113
114  -- Get a 'burst_length_bytes' field value from a register value.
115  function to_caesar_conf_burst_length_bytes(data : register_t) return caesar_conf_burst_length_bytes_t is
116    constant result : integer := to_integer(unsigned(data(caesar_conf_burst_length_bytes)));
117  begin
118    return result;
119  end function;
120
121  -- Cast a 'increment' field value to SLV.
122  function to_caesar_conf_increment_slv(data : caesar_conf_increment_t) return caesar_conf_increment_slv_t is
123    constant result : caesar_conf_increment_slv_t := std_ulogic_vector(to_signed(data, caesar_conf_increment_width));
124  begin
125    return result;
126  end function;
127
128  -- Get a 'increment' field value from a register value.
129  function to_caesar_conf_increment(data : register_t) return caesar_conf_increment_t is
130    constant result : integer := to_integer(signed(data(caesar_conf_increment)));
131  begin
132    return result;
133  end function;
134
135  -- Cast a 'retry_count' field value to SLV.
136  function to_caesar_conf_retry_count_slv(data : caesar_conf_retry_count_t) return caesar_conf_retry_count_slv_t is
137    constant result : caesar_conf_retry_count_slv_t := std_ulogic_vector(to_unsigned(data, caesar_conf_retry_count_width));
138  begin
139    return result;
140  end function;
141
142  -- Get a 'retry_count' field value from a register value.
143  function to_caesar_conf_retry_count(data : register_t) return caesar_conf_retry_count_t is
144    constant result : integer := to_integer(unsigned(data(caesar_conf_retry_count)));
145  begin
146    return result;
147  end function;
148
149end package body;

Record package

The caesar_regs_down_t type is a record with a member config, the only register in this example. The type of the config member is another record with the three integers set up in our example: burst_length_bytes, increment and retry_count. These are of integer types defined in the base register package above.

In our VHDL code we can access a field value for example like this:

result_data <= input_data + regs_down.config.increment;
Click to expand/collapse code.
Generated VHDL record package.
  1-- -----------------------------------------------------------------------------
  2-- This file is automatically generated by hdl-registers version 7.3.1-dev.
  3-- Code generator VhdlRecordPackageGenerator version 1.0.0.
  4-- Generated 2025-04-26 09:24 at Git commit e16bd7741c27.
  5-- Register hash 78096d26a817ed466b1fc4fb8e1d3585885bdcb7.
  6-- -----------------------------------------------------------------------------
  7
  8library ieee;
  9use ieee.fixed_pkg.all;
 10use ieee.std_logic_1164.all;
 11use ieee.numeric_std.all;
 12
 13library register_file;
 14use register_file.register_file_pkg.register_t;
 15
 16use work.caesar_regs_pkg.all;
 17
 18
 19package caesar_register_record_pkg is
 20
 21  -- -----------------------------------------------------------------------------
 22  -- Record with correctly-typed members for each field in each register.
 23  -- Fields in the 'conf' register as a record.
 24  type caesar_conf_t is record
 25    burst_length_bytes : caesar_conf_burst_length_bytes_t;
 26    increment : caesar_conf_increment_t;
 27    retry_count : caesar_conf_retry_count_t;
 28  end record;
 29  -- Default value for the 'conf' register as a record.
 30  constant caesar_conf_init : caesar_conf_t := (
 31    burst_length_bytes => caesar_conf_burst_length_bytes_init,
 32    increment => caesar_conf_increment_init,
 33    retry_count => caesar_conf_retry_count_init
 34  );
 35  -- Convert a record of the 'conf' register to SLV.
 36  function to_slv(data : caesar_conf_t) return register_t;
 37  -- Convert an SLV register value to the record for the 'conf' register.
 38  function to_caesar_conf(data : register_t) return caesar_conf_t;
 39
 40  -- -----------------------------------------------------------------------------
 41  -- Below is a record with correctly typed and ranged members for all registers, register arrays
 42  -- and fields that are in the 'down' direction.
 43  -- Record with everything in the 'down' direction.
 44  type caesar_regs_down_t is record
 45    conf : caesar_conf_t;
 46  end record;
 47  -- Default value of the above record.
 48  constant caesar_regs_down_init : caesar_regs_down_t := (
 49    conf => caesar_conf_init
 50  );
 51  -- Convert SLV register list to record with everything in the 'down' direction.
 52  function to_caesar_regs_down(data : caesar_regs_t) return caesar_regs_down_t;
 53
 54  -- ---------------------------------------------------------------------------
 55  -- Below is a record with a status bit for each readable register in the register list.
 56  -- It can be used for the 'reg_was_read' port of a register file wrapper.
 57  -- Combined status mask record for all readable register.
 58  type caesar_reg_was_read_t is record
 59    conf : std_ulogic;
 60  end record;
 61  -- Default value for the above record.
 62  constant caesar_reg_was_read_init : caesar_reg_was_read_t := (
 63    others => '0'
 64  );
 65  -- Convert an SLV 'reg_was_read' from generic register file to the record above.
 66  function to_caesar_reg_was_read(
 67    data : caesar_reg_was_accessed_t
 68  ) return caesar_reg_was_read_t;
 69
 70  -- ---------------------------------------------------------------------------
 71  -- Below is a record with a status bit for each writeable register in the register list.
 72  -- It can be used for the 'reg_was_written' port of a register file wrapper.
 73  -- Combined status mask record for all writeable register.
 74  type caesar_reg_was_written_t is record
 75    conf : std_ulogic;
 76  end record;
 77  -- Default value for the above record.
 78  constant caesar_reg_was_written_init : caesar_reg_was_written_t := (
 79    others => '0'
 80  );
 81  -- Convert an SLV 'reg_was_written' from generic register file to the record above.
 82  function to_caesar_reg_was_written(
 83    data : caesar_reg_was_accessed_t
 84  ) return caesar_reg_was_written_t;
 85
 86end package;
 87
 88package body caesar_register_record_pkg is
 89
 90  function to_slv(data : caesar_conf_t) return register_t is
 91    variable result : register_t := (others => '-');
 92  begin
 93    result(caesar_conf_burst_length_bytes) := to_caesar_conf_burst_length_bytes_slv(data.burst_length_bytes);
 94    result(caesar_conf_increment) := to_caesar_conf_increment_slv(data.increment);
 95    result(caesar_conf_retry_count) := to_caesar_conf_retry_count_slv(data.retry_count);
 96
 97    return result;
 98  end function;
 99
100  function to_caesar_conf(data : register_t) return caesar_conf_t is
101    variable result : caesar_conf_t := caesar_conf_init;
102  begin
103    result.burst_length_bytes := to_caesar_conf_burst_length_bytes(data);
104    result.increment := to_caesar_conf_increment(data);
105    result.retry_count := to_caesar_conf_retry_count(data);
106
107    return result;
108  end function;
109
110  function to_caesar_regs_down(data : caesar_regs_t) return caesar_regs_down_t is
111    variable result : caesar_regs_down_t := caesar_regs_down_init;
112  begin
113    result.conf := to_caesar_conf(data(caesar_conf));
114
115    return result;
116  end function;
117
118  function to_caesar_reg_was_read(
119    data : caesar_reg_was_accessed_t
120  ) return caesar_reg_was_read_t is
121    variable result : caesar_reg_was_read_t := caesar_reg_was_read_init;
122  begin
123    result.conf := data(caesar_conf);
124
125    return result;
126  end function;
127
128  function to_caesar_reg_was_written(
129    data : caesar_reg_was_accessed_t
130  ) return caesar_reg_was_written_t is
131    variable result : caesar_reg_was_written_t := caesar_reg_was_written_init;
132  begin
133    result.conf := data(caesar_conf);
134
135    return result;
136  end function;
137
138end package body;

C++

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

The class header is skipped here, since its inclusion would make this page very long. See C++ generator for more details and an example of how the excluded file might look.

C++ interface header

Note that the setters and getters for each field value use integer types as argument or return value. The signed field uses int32_t while the unsigned fields use uint32_t.

Click to expand/collapse code.
Generated C++ class interface code.
  1// -----------------------------------------------------------------------------
  2// This file is automatically generated by hdl-registers version 7.3.1-dev.
  3// Code generator CppInterfaceGenerator version 1.0.0.
  4// Generated 2025-04-26 09:24 at Git commit e16bd7741c27.
  5// Register hash 78096d26a817ed466b1fc4fb8e1d3585885bdcb7.
  6// -----------------------------------------------------------------------------
  7
  8#pragma once
  9
 10#include <sstream>
 11#include <cstdint>
 12#include <cstdlib>
 13
 14namespace fpga_regs
 15{
 16
 17  namespace caesar
 18  {
 19
 20    // Attributes for the 'conf' register.
 21    namespace conf {
 22      // Attributes for the 'burst_length_bytes' field.
 23      namespace burst_length_bytes
 24      {
 25        // The number of bits that the field occupies.
 26        static const size_t width = 9;
 27        // The bit index of the lowest bit in the field.
 28        static const size_t shift = 0;
 29        // The bit mask of the field, at index zero.
 30        static const uint32_t mask_at_base = (1uLL << width) - 1;
 31        // The bit mask of the field, at the field's bit index.
 32        static const uint32_t mask_shifted = mask_at_base << shift;
 33
 34        // Initial value of the field at device startup/reset.
 35        static const uint32_t default_value = 64;
 36        // Raw representation of the initial value, at the the field's bit index.
 37        static const uint32_t default_value_raw = 64uL << shift;
 38      }
 39
 40      // Attributes for the 'increment' field.
 41      namespace increment
 42      {
 43        // The number of bits that the field occupies.
 44        static const size_t width = 3;
 45        // The bit index of the lowest bit in the field.
 46        static const size_t shift = 9;
 47        // The bit mask of the field, at index zero.
 48        static const uint32_t mask_at_base = (1uLL << width) - 1;
 49        // The bit mask of the field, at the field's bit index.
 50        static const uint32_t mask_shifted = mask_at_base << shift;
 51
 52        // Initial value of the field at device startup/reset.
 53        static const int32_t default_value = 0;
 54        // Raw representation of the initial value, at the the field's bit index.
 55        static const uint32_t default_value_raw = 0uL << shift;
 56      }
 57
 58      // Attributes for the 'retry_count' field.
 59      namespace retry_count
 60      {
 61        // The number of bits that the field occupies.
 62        static const size_t width = 3;
 63        // The bit index of the lowest bit in the field.
 64        static const size_t shift = 12;
 65        // The bit mask of the field, at index zero.
 66        static const uint32_t mask_at_base = (1uLL << width) - 1;
 67        // The bit mask of the field, at the field's bit index.
 68        static const uint32_t mask_shifted = mask_at_base << shift;
 69
 70        // Initial value of the field at device startup/reset.
 71        static const uint32_t default_value = 0;
 72        // Raw representation of the initial value, at the the field's bit index.
 73        static const uint32_t default_value_raw = 0uL << shift;
 74      }
 75
 76      // Struct that holds the value of each field in the register,
 77      // in a native C++ representation.
 78      struct Value {
 79        uint32_t burst_length_bytes;
 80        int32_t increment;
 81        uint32_t retry_count;
 82      };
 83      // Initial value of the register at device startup/reset.
 84      const Value default_value = {
 85        burst_length_bytes::default_value,
 86        increment::default_value,
 87        retry_count::default_value,
 88      };
 89    }
 90
 91  } // namespace caesar
 92
 93  class ICaesar
 94  {
 95  public:
 96    // Number of registers within this register list.
 97    static const size_t num_registers = 1uL;
 98
 99    virtual ~ICaesar() {}
100
101    // -------------------------------------------------------------------------
102    // Methods for the 'conf' register.
103    // Mode 'Read, Write'.
104    // -------------------------------------------------------------------------
105    // Read the whole register value over the register bus.
106    virtual caesar::conf::Value get_conf() const = 0;
107
108    // Read the whole register value over the register bus.
109    // This method returns the raw register value without any type conversion.
110    virtual uint32_t get_conf_raw() const = 0;
111
112    // Read the register and slice out the 'burst_length_bytes' field value.
113    virtual uint32_t get_conf_burst_length_bytes() const = 0;
114
115    // Read the register and slice out the 'increment' field value.
116    virtual int32_t get_conf_increment() const = 0;
117
118    // Read the register and slice out the 'retry_count' field value.
119    virtual uint32_t get_conf_retry_count() const = 0;
120
121    // Write the whole register value over the register bus.
122    virtual void set_conf(
123      caesar::conf::Value register_value
124    ) const = 0;
125
126    // Write the whole register value over the register bus.
127    // This method takes a raw register value and does not perform any type conversion.
128    virtual void set_conf_raw(
129      uint32_t register_value
130    ) const = 0;
131
132    // Write the 'burst_length_bytes' field value.
133    // Will read-modify-write the register.
134    virtual void set_conf_burst_length_bytes(
135      uint32_t field_value
136    ) const = 0;
137
138    // Write the 'increment' field value.
139    // Will read-modify-write the register.
140    virtual void set_conf_increment(
141      int32_t field_value
142    ) const = 0;
143
144    // Write the 'retry_count' field value.
145    // Will read-modify-write the register.
146    virtual void set_conf_retry_count(
147      uint32_t field_value
148    ) const = 0;
149    // -------------------------------------------------------------------------
150  };
151
152} // namespace fpga_regs

C++ implementation

Note that each setter and getter perform assertions that the supplied argument is withing the legal range of the field. This will catch calculation errors during testing and at run-time.

Click to expand/collapse code.
Generated C++ class implementation code.
  1// -----------------------------------------------------------------------------
  2// This file is automatically generated by hdl-registers version 7.3.1-dev.
  3// Code generator CppImplementationGenerator version 2.0.2.
  4// Generated 2025-04-26 09:24 at Git commit e16bd7741c27.
  5// Register hash 78096d26a817ed466b1fc4fb8e1d3585885bdcb7.
  6// -----------------------------------------------------------------------------
  7
  8#include "include/caesar.h"
  9
 10namespace fpga_regs
 11{
 12
 13#ifdef NO_REGISTER_SETTER_ASSERT
 14
 15#define _SETTER_ASSERT_TRUE(expression, message) ((void)0)
 16
 17#else // Not NO_REGISTER_SETTER_ASSERT.
 18
 19// This macro is called by the register code to check for runtime errors.
 20#define _SETTER_ASSERT_TRUE(expression, message)                                 \
 21  {                                                                              \
 22    if (!static_cast<bool>(expression)) {                                        \
 23      std::ostringstream diagnostics;                                            \
 24      diagnostics << "caesar.cpp:" << __LINE__                                   \
 25                  << ": " << message << ".";                                     \
 26      std::string diagnostic_message = diagnostics.str();                        \
 27      m_assertion_handler(&diagnostic_message);                                  \
 28    }                                                                            \
 29  }
 30
 31#endif // NO_REGISTER_SETTER_ASSERT.
 32
 33#ifdef NO_REGISTER_GETTER_ASSERT
 34
 35#define _GETTER_ASSERT_TRUE(expression, message) ((void)0)
 36
 37#else // Not NO_REGISTER_GETTER_ASSERT.
 38
 39// This macro is called by the register code to check for runtime errors.
 40#define _GETTER_ASSERT_TRUE(expression, message)                                 \
 41  {                                                                              \
 42    if (!static_cast<bool>(expression)) {                                        \
 43      std::ostringstream diagnostics;                                            \
 44      diagnostics << "caesar.cpp:" << __LINE__                                   \
 45                  << ": " << message << ".";                                     \
 46      std::string diagnostic_message = diagnostics.str();                        \
 47      m_assertion_handler(&diagnostic_message);                                  \
 48    }                                                                            \
 49  }
 50
 51#endif // NO_REGISTER_GETTER_ASSERT.
 52
 53#ifdef NO_REGISTER_ARRAY_INDEX_ASSERT
 54
 55#define _ARRAY_INDEX_ASSERT_TRUE(expression, message) ((void)0)
 56
 57#else // Not NO_REGISTER_ARRAY_INDEX_ASSERT.
 58
 59// This macro is called by the register code to check for runtime errors.
 60#define _ARRAY_INDEX_ASSERT_TRUE(expression, message)                            \
 61  {                                                                              \
 62    if (!static_cast<bool>(expression)) {                                        \
 63      std::ostringstream diagnostics;                                            \
 64      diagnostics << "caesar.cpp:" << __LINE__                                   \
 65                  << ": " << message << ".";                                     \
 66      std::string diagnostic_message = diagnostics.str();                        \
 67      m_assertion_handler(&diagnostic_message);                                  \
 68    }                                                                            \
 69  }
 70
 71#endif // NO_REGISTER_ARRAY_INDEX_ASSERT.
 72
 73  Caesar::Caesar(uintptr_t base_address, bool (*assertion_handler) (const std::string*))
 74      : m_registers(reinterpret_cast<volatile uint32_t *>(base_address)),
 75        m_assertion_handler(assertion_handler)
 76  {
 77    // Empty
 78  }
 79
 80  // ---------------------------------------------------------------------------
 81  // Methods for the 'conf' register.
 82  // Mode 'Read, Write'.
 83  // ---------------------------------------------------------------------------
 84  // Read the whole register value over the register bus.
 85  caesar::conf::Value Caesar::get_conf() const
 86  {
 87    const uint32_t raw_value = get_conf_raw();
 88
 89    const uint32_t burst_length_bytes_value = get_conf_burst_length_bytes_from_raw(raw_value);
 90    const int32_t increment_value = get_conf_increment_from_raw(raw_value);
 91    const uint32_t retry_count_value = get_conf_retry_count_from_raw(raw_value);
 92
 93    return {burst_length_bytes_value, increment_value, retry_count_value};
 94  }
 95
 96  // Read the whole register value over the register bus.
 97  // This method returns the raw register value without any type conversion.
 98  uint32_t Caesar::get_conf_raw() const
 99  {
100    const size_t index = 0;
101    const uint32_t raw_value = m_registers[index];
102
103    return raw_value;
104  }
105
106  // Read the register and slice out the 'burst_length_bytes' field value.
107  uint32_t Caesar::get_conf_burst_length_bytes() const
108  {
109    const uint32_t raw_value = get_conf_raw();
110
111    return get_conf_burst_length_bytes_from_raw(raw_value);
112  }
113
114  // Slice out the 'burst_length_bytes' field value from a given raw register value.
115  // Performs no operation on the register bus.
116  uint32_t Caesar::get_conf_burst_length_bytes_from_raw(
117    uint32_t register_value
118  ) const
119  {
120    const uint32_t result_masked = register_value & caesar::conf::burst_length_bytes::mask_shifted;
121    const uint32_t result_shifted = result_masked >> caesar::conf::burst_length_bytes::shift;
122
123    // No casting needed.
124    const uint32_t field_value = result_shifted;
125
126    _GETTER_ASSERT_TRUE(
127      field_value >= 1 && field_value <= 256,
128      "Got 'burst_length_bytes' value out of range: " << field_value
129    );
130
131    return field_value;
132  }
133
134  // Read the register and slice out the 'increment' field value.
135  int32_t Caesar::get_conf_increment() const
136  {
137    const uint32_t raw_value = get_conf_raw();
138
139    return get_conf_increment_from_raw(raw_value);
140  }
141
142  // Slice out the 'increment' field value from a given raw register value.
143  // Performs no operation on the register bus.
144  int32_t Caesar::get_conf_increment_from_raw(
145    uint32_t register_value
146  ) const
147  {
148    const uint32_t result_masked = register_value & caesar::conf::increment::mask_shifted;
149    const uint32_t result_shifted = result_masked >> caesar::conf::increment::shift;
150
151    const uint32_t sign_bit_mask = 1uL << 2;
152    int32_t field_value;
153    if (result_shifted & sign_bit_mask)
154    {
155      // Value is to be interpreted as negative.
156      // This can be seen as a sign extension from the width of the field to the width of
157      // the result variable.
158      field_value = result_shifted - 2 * sign_bit_mask;
159    }
160    else
161    {
162      // Value is positive.
163      field_value = result_shifted;
164    }
165
166    _GETTER_ASSERT_TRUE(
167      field_value >= -4 && field_value <= 3,
168      "Got 'increment' value out of range: " << field_value
169    );
170
171    return field_value;
172  }
173
174  // Read the register and slice out the 'retry_count' field value.
175  uint32_t Caesar::get_conf_retry_count() const
176  {
177    const uint32_t raw_value = get_conf_raw();
178
179    return get_conf_retry_count_from_raw(raw_value);
180  }
181
182  // Slice out the 'retry_count' field value from a given raw register value.
183  // Performs no operation on the register bus.
184  uint32_t Caesar::get_conf_retry_count_from_raw(
185    uint32_t register_value
186  ) const
187  {
188    const uint32_t result_masked = register_value & caesar::conf::retry_count::mask_shifted;
189    const uint32_t result_shifted = result_masked >> caesar::conf::retry_count::shift;
190
191    // No casting needed.
192    const uint32_t field_value = result_shifted;
193
194    _GETTER_ASSERT_TRUE(
195      field_value <= 5,
196      "Got 'retry_count' value out of range: " << field_value
197    );
198
199    return field_value;
200  }
201
202  // Write the whole register value over the register bus.
203  void Caesar::set_conf(
204    caesar::conf::Value register_value
205  ) const
206  {
207    const uint32_t burst_length_bytes_value = get_conf_burst_length_bytes_to_raw(register_value.burst_length_bytes);
208    const uint32_t increment_value = get_conf_increment_to_raw(register_value.increment);
209    const uint32_t retry_count_value = get_conf_retry_count_to_raw(register_value.retry_count);
210    const uint32_t raw_value = burst_length_bytes_value | increment_value | retry_count_value;
211
212    set_conf_raw(raw_value);
213  }
214
215  // Write the whole register value over the register bus.
216  // This method takes a raw register value and does not perform any type conversion.
217  void Caesar::set_conf_raw(
218    uint32_t register_value
219  ) const
220  {
221    const size_t index = 0;
222    m_registers[index] = register_value;
223  }
224
225  // Write the 'burst_length_bytes' field value.
226  // Will read-modify-write the register.
227  void Caesar::set_conf_burst_length_bytes(
228    uint32_t field_value
229  ) const
230  {
231    const size_t index = 0;
232
233    const uint32_t raw_value = m_registers[index];
234    const uint32_t mask_shifted_inverse = ~caesar::conf::burst_length_bytes::mask_shifted;
235    const uint32_t base_value = raw_value & mask_shifted_inverse;
236
237    const uint32_t field_value_raw = get_conf_burst_length_bytes_to_raw(field_value);
238    const uint32_t register_value = base_value | field_value_raw;
239
240    m_registers[index] = register_value;
241  }
242
243  // Get the raw representation of a given 'burst_length_bytes' field value.
244  // Performs no operation on the register bus.
245  uint32_t Caesar::get_conf_burst_length_bytes_to_raw(uint32_t field_value) const
246  {
247    _SETTER_ASSERT_TRUE(
248      field_value >= 1 && field_value <= 256,
249      "Got 'burst_length_bytes' value out of range: " << field_value
250    );
251
252    const uint32_t field_value_shifted = field_value << caesar::conf::burst_length_bytes::shift;
253
254    return field_value_shifted;
255  }
256
257  // Write the 'increment' field value.
258  // Will read-modify-write the register.
259  void Caesar::set_conf_increment(
260    int32_t field_value
261  ) const
262  {
263    const size_t index = 0;
264
265    const uint32_t raw_value = m_registers[index];
266    const uint32_t mask_shifted_inverse = ~caesar::conf::increment::mask_shifted;
267    const uint32_t base_value = raw_value & mask_shifted_inverse;
268
269    const uint32_t field_value_raw = get_conf_increment_to_raw(field_value);
270    const uint32_t register_value = base_value | field_value_raw;
271
272    m_registers[index] = register_value;
273  }
274
275  // Get the raw representation of a given 'increment' field value.
276  // Performs no operation on the register bus.
277  uint32_t Caesar::get_conf_increment_to_raw(int32_t field_value) const
278  {
279    _SETTER_ASSERT_TRUE(
280      field_value >= -4 && field_value <= 3,
281      "Got 'increment' value out of range: " << field_value
282    );
283
284    const uint32_t field_value_unsigned = (uint32_t)field_value;
285    const uint32_t field_value_masked = field_value_unsigned & caesar::conf::increment::mask_at_base;
286    const uint32_t field_value_shifted = field_value_masked << caesar::conf::increment::shift;
287
288    return field_value_shifted;
289  }
290
291  // Write the 'retry_count' field value.
292  // Will read-modify-write the register.
293  void Caesar::set_conf_retry_count(
294    uint32_t field_value
295  ) const
296  {
297    const size_t index = 0;
298
299    const uint32_t raw_value = m_registers[index];
300    const uint32_t mask_shifted_inverse = ~caesar::conf::retry_count::mask_shifted;
301    const uint32_t base_value = raw_value & mask_shifted_inverse;
302
303    const uint32_t field_value_raw = get_conf_retry_count_to_raw(field_value);
304    const uint32_t register_value = base_value | field_value_raw;
305
306    m_registers[index] = register_value;
307  }
308
309  // Get the raw representation of a given 'retry_count' field value.
310  // Performs no operation on the register bus.
311  uint32_t Caesar::get_conf_retry_count_to_raw(uint32_t field_value) const
312  {
313    _SETTER_ASSERT_TRUE(
314      field_value <= 5,
315      "Got 'retry_count' value out of range: " << field_value
316    );
317
318    const uint32_t field_value_shifted = field_value << caesar::conf::retry_count::shift;
319
320    return field_value_shifted;
321  }
322  // ---------------------------------------------------------------------------
323
324} // namespace fpga_regs

C header

The C code below is produced by the generate() call in the Python example above. The range and mask of the each field are available as constants.

Click to expand/collapse code.
Generated C code.
 1// -----------------------------------------------------------------------------
 2// This file is automatically generated by hdl-registers version 7.3.1-dev.
 3// Code generator CHeaderGenerator version 1.0.0.
 4// Generated 2025-04-26 09:24 at Git commit e16bd7741c27.
 5// Register hash 78096d26a817ed466b1fc4fb8e1d3585885bdcb7.
 6// -----------------------------------------------------------------------------
 7
 8#ifndef CAESAR_REGS_H
 9#define CAESAR_REGS_H
10
11
12// Number of registers within this register list.
13#define CAESAR_NUM_REGS (1u)
14
15// Type for this register list.
16typedef struct caesar_regs_t
17{
18  // Mode "Read, Write".
19  uint32_t conf;
20} caesar_regs_t;
21
22// Address of the 'conf' register.
23// Mode 'Read, Write'.
24#define CAESAR_CONF_INDEX (0u)
25#define CAESAR_CONF_ADDR (4u * CAESAR_CONF_INDEX)
26// Attributes for the 'burst_length_bytes' field in the 'conf' register.
27#define CAESAR_CONF_BURST_LENGTH_BYTES_SHIFT (0u)
28#define CAESAR_CONF_BURST_LENGTH_BYTES_MASK (0b111111111u << 0u)
29#define CAESAR_CONF_BURST_LENGTH_BYTES_MASK_INVERSE (~CAESAR_CONF_BURST_LENGTH_BYTES_MASK)
30// Attributes for the 'increment' field in the 'conf' register.
31#define CAESAR_CONF_INCREMENT_SHIFT (9u)
32#define CAESAR_CONF_INCREMENT_MASK (0b111u << 9u)
33#define CAESAR_CONF_INCREMENT_MASK_INVERSE (~CAESAR_CONF_INCREMENT_MASK)
34// Attributes for the 'retry_count' field in the 'conf' register.
35#define CAESAR_CONF_RETRY_COUNT_SHIFT (12u)
36#define CAESAR_CONF_RETRY_COUNT_MASK (0b111u << 12u)
37#define CAESAR_CONF_RETRY_COUNT_MASK_INVERSE (~CAESAR_CONF_RETRY_COUNT_MASK)
38
39#endif // CAESAR_REGS_H