C++ code generator

A complete C++ class can be generated with methods to read/write the registers or fields.

  • CppInterfaceGenerator creates an abstract interface header that can be used for mocking in a unit test environment. Contains method declarations, register attributes, and register constant values.

  • CppHeaderGenerator creates a class header which inherits the abstract class.

  • CppImplementationGenerator creates a class implementation with setters and getters.

C++ code is generated by running the Python code below. Note that it will parse and generate artifacts from the TOML file used in the TOML Format example.

Python code that parses the example TOML file and generates the C++ code we need.
 1import sys
 2from pathlib import Path
 3
 4from hdl_registers.generator.cpp.header import CppHeaderGenerator
 5from hdl_registers.generator.cpp.implementation import CppImplementationGenerator
 6from hdl_registers.generator.cpp.interface import CppInterfaceGenerator
 7from hdl_registers.parser.toml import from_toml
 8
 9THIS_DIR = Path(__file__).parent
10
11
12def main(output_folder: Path) -> None:
13    """
14    Create register C++ artifacts from the TOML example file.
15    """
16    register_list = from_toml(
17        name="example",
18        toml_file=THIS_DIR.parent.parent / "user_guide" / "toml" / "toml_format.toml",
19    )
20
21    CppInterfaceGenerator(
22        register_list=register_list, output_folder=output_folder / "include"
23    ).create()
24
25    CppHeaderGenerator(
26        register_list=register_list, output_folder=output_folder / "include"
27    ).create()
28
29    CppImplementationGenerator(register_list=register_list, output_folder=output_folder).create()
30
31
32if __name__ == "__main__":
33    main(output_folder=Path(sys.argv[1]))

Getters

It can be noted, most clearly in the Interface header below, that there are three ways to read a register field:

  1. The method that reads the whole register, e.g. get_configuration().

  2. The method that reads the register and then slices out the field value, e.g. get_configuration_enable().

  3. The method that slices out the field value given a previously read register value, e.g. get_configuration_enable_from_value(register_value).

Method (2) is the most convenient in most cases. However if we want to read out more than one field from a register it would be very inefficient to read the register value more than once over the register bus, which would be the result of calling (2) multiple times. Instead we can call (1) once and then (3) multiple times to get our field values.

Setters

Conversely there are three ways to write a register field:

  1. The method that writes the whole register, e.g. set_configuration().

  2. The method that reads the register, updates the value of the field, and then writes the register back, e.g. set_configuration_enable().

  3. The method that updates the value of the field given a previously read register value, and returns an updated register value, e.g. set_configuration_enable_from_value(register_value).

Method (2) is the most convenient in most cases. However if we want to update more than one field of a register it would be very inefficient to read and write the register more than once over the register bus, which would be the result of calling (2) multiple times. Instead we can call a register getter once, e.g. get_configuration(), and then (3) multiple times to get our updated register value. This value is then written over the register bus using (1).

Exceptions

The discussion about setters above is valid for “read write” mode registers, which is arguably the most common type. However there are three register modes where the previously written register value can not be read back over the bus and then modified: “write only”, “write pulse”, and “read, write pulse”. The field setters for registers of this mode will write all bits outside of the current field as zero. This can for example be seen in the setter set_channels_configuration_enable() in the generated code below.

Assertion macros

There are a few register-related things that can go wrong in an embedded system:

  1. An error in hardware might result in reading a field value that is out of bounds. This is mostly possible for enumeration and integer fields.

  2. Conversely, an error in software might result in writing a field value that is out of bounds.

  3. An error in software might result in a register array read/write with an index that is out of bounds.

The generated C++ implementation checks for these errors using custom assertion macros. Meaning, they can be detected at runtime as well as in unit tests.

If an assertion fails a user-specified handler function is called. In this function, the user could e.g. call a custom logger, perform a controlled shutdown, throw exceptions, etc. One argument is provided that contains a descriptive error message. The function must return a boolean true.

This error handler function must be provided to the constructor of the generated class.

Minimal example

A minimal and somewhat unrealistic handler function is shown below. More advanced handler functions are left to the user.

uintptr_t base_address = 0x43C00000;

bool register_assert_fail_handler(const std::string *diagnostic_message)
{
  std::cerr << *diagnostic_message << std::endl;
  std::exit(EXIT_FAILURE);
  return true;
}

fpga_regs::Example example(base_address, register_assert_fail_handler);

Disabling assertions

The assertions add to the binary size and to the runtime of the program. The user can disable the assertions by defining the following macros (usually with the -D compiler flag):

  1. NO_REGISTER_GETTER_ASSERT

  2. NO_REGISTER_SETTER_ASSERT

  3. NO_REGISTER_ARRAY_INDEX_ASSERT

This should result in no overhead.

Interface header

Below is the resulting abstract interface header code, generated from the TOML Format example. Note that all register constants as well as field attributes are included here.

Example interface header
  1// -----------------------------------------------------------------------------
  2// This file is automatically generated by hdl-registers version 7.0.5-dev.
  3// Code generator CppInterfaceGenerator version 1.0.0.
  4// Generated 2025-02-22 20:51 from file toml_format.toml at commit 57cebe3573f7.
  5// Register hash 4df9765ebb584803b583628671e4659579eb85f4.
  6// -----------------------------------------------------------------------------
  7
  8#pragma once
  9
 10#include <sstream>
 11#include <cstdint>
 12#include <cstdlib>
 13
 14namespace fpga_regs
 15{
 16
 17  // Attributes for the 'enable' field in the 'config' register.
 18  namespace example::config::enable
 19  {
 20    static const auto width = 1;
 21    static const auto default_value = 0b1;
 22  }
 23  // Attributes for the 'direction' field in the 'config' register.
 24  namespace example::config::direction
 25  {
 26    static const auto width = 2;
 27    enum Enumeration
 28    {
 29      data_in = 0,
 30      high_z = 1,
 31      data_out = 2,
 32    };
 33    static const auto default_value = Enumeration::high_z;
 34  }
 35
 36  // Attributes for the 'enable' field in the 'config' register within the 'channels' register array.
 37  namespace example::channels::config::enable
 38  {
 39    static const auto width = 1;
 40    static const auto default_value = 0b0;
 41  }
 42  // Attributes for the 'tuser' field in the 'config' register within the 'channels' register array.
 43  namespace example::channels::config::tuser
 44  {
 45    static const auto width = 8;
 46    static const auto default_value = 0b00000000;
 47  }
 48
 49  // Attributes for the "channels" register array.
 50  namespace example::channels
 51  {
 52    // Number of times the registers of the array are repeated.
 53    static const auto array_length = 4;
 54  };
 55
 56  class IExample
 57  {
 58  public:
 59    // Register constant.
 60    static const int axi_data_width = 64;
 61    // Register constant.
 62    static constexpr double clock_rate_hz = 156250000.0;
 63
 64    // Number of registers within this register list.
 65    static const size_t num_registers = 10uL;
 66
 67    virtual ~IExample() {}
 68
 69    // -------------------------------------------------------------------------
 70    // Methods for the 'config' register. Mode 'Read, Write'.
 71
 72    // Getter that will read the whole register's value over the register bus.
 73    virtual uint32_t get_config() const = 0;
 74
 75    // Setter that will write the whole register's value over the register bus.
 76    virtual void set_config(
 77      uint32_t register_value
 78    ) const = 0;
 79
 80    // Getter for the 'enable' field in the 'config' register,
 81    // which will read register value over the register bus.
 82    virtual uint32_t get_config_enable() const = 0;
 83    // Getter for the 'enable' field in the 'config' register,
 84    // given a register value.
 85    virtual uint32_t get_config_enable_from_value(
 86      uint32_t register_value
 87    ) const = 0;
 88    // Setter for the 'enable' field in the 'config' register,
 89    // which will read-modify-write over the register bus.
 90    virtual void set_config_enable(
 91      uint32_t field_value
 92    ) const = 0;
 93    // Setter for the 'enable' field in the 'config' register,
 94    // given a register value, which will return an updated value.
 95    virtual uint32_t set_config_enable_from_value(
 96      uint32_t register_value,
 97      uint32_t field_value
 98    ) const = 0;
 99
100    // Getter for the 'direction' field in the 'config' register,
101    // which will read register value over the register bus.
102    virtual example::config::direction::Enumeration get_config_direction() const = 0;
103    // Getter for the 'direction' field in the 'config' register,
104    // given a register value.
105    virtual example::config::direction::Enumeration get_config_direction_from_value(
106      uint32_t register_value
107    ) const = 0;
108    // Setter for the 'direction' field in the 'config' register,
109    // which will read-modify-write over the register bus.
110    virtual void set_config_direction(
111      example::config::direction::Enumeration field_value
112    ) const = 0;
113    // Setter for the 'direction' field in the 'config' register,
114    // given a register value, which will return an updated value.
115    virtual uint32_t set_config_direction_from_value(
116      uint32_t register_value,
117      example::config::direction::Enumeration field_value
118    ) const = 0;
119
120    // -------------------------------------------------------------------------
121    // Methods for the 'status' register. Mode 'Read'.
122
123    // Getter that will read the whole register's value over the register bus.
124    virtual uint32_t get_status() const = 0;
125
126    // -------------------------------------------------------------------------
127    // Methods for the 'read_address' register within the 'channels' register array. Mode 'Read, Write'.
128
129    // Getter that will read the whole register's value over the register bus.
130    virtual uint32_t get_channels_read_address(
131      size_t array_index
132    ) const = 0;
133
134    // Setter that will write the whole register's value over the register bus.
135    virtual void set_channels_read_address(
136      size_t array_index,
137      uint32_t register_value
138    ) const = 0;
139
140    // -------------------------------------------------------------------------
141    // Methods for the 'config' register within the 'channels' register array. Mode 'Write'.
142
143    // Setter that will write the whole register's value over the register bus.
144    virtual void set_channels_config(
145      size_t array_index,
146      uint32_t register_value
147    ) const = 0;
148
149    // Setter for the 'enable' field in the 'config' register within the 'channels' register array,
150    // which will set the field to the given value, and everything else to default.
151    virtual void set_channels_config_enable(
152      size_t array_index,
153      uint32_t field_value
154    ) const = 0;
155    // Setter for the 'enable' field in the 'config' register within the 'channels' register array,
156    // given a register value, which will return an updated value.
157    virtual uint32_t set_channels_config_enable_from_value(
158      uint32_t register_value,
159      uint32_t field_value
160    ) const = 0;
161
162    // Setter for the 'tuser' field in the 'config' register within the 'channels' register array,
163    // which will set the field to the given value, and everything else to default.
164    virtual void set_channels_config_tuser(
165      size_t array_index,
166      uint32_t field_value
167    ) const = 0;
168    // Setter for the 'tuser' field in the 'config' register within the 'channels' register array,
169    // given a register value, which will return an updated value.
170    virtual uint32_t set_channels_config_tuser_from_value(
171      uint32_t register_value,
172      uint32_t field_value
173    ) const = 0;
174
175  };
176
177} /* namespace fpga_regs */

Class header

Below is the generated class header:

Example class header
  1// -----------------------------------------------------------------------------
  2// This file is automatically generated by hdl-registers version 7.0.5-dev.
  3// Code generator CppHeaderGenerator version 1.0.0.
  4// Generated 2025-02-22 20:51 from file toml_format.toml at commit 57cebe3573f7.
  5// Register hash 4df9765ebb584803b583628671e4659579eb85f4.
  6// -----------------------------------------------------------------------------
  7
  8#pragma once
  9
 10#include "i_example.h"
 11
 12namespace fpga_regs
 13{
 14
 15  class Example : public IExample
 16  {
 17  private:
 18    volatile uint32_t *m_registers;
 19    bool (*m_assertion_handler) (const std::string*);
 20
 21  public:
 22    /**
 23     * Class constructor.
 24     * @param base_address Byte address where these registers are memory mapped.
 25     *                     Can be e.g. '0x43C00000' in bare metal, or e.g.
 26     *                     'reinterpret_cast<uintptr_t>(mmap(...))' in Linux.
 27     *                     When using an operating system, care must be taken to pass the
 28     *                     virtual address, not the physical address.
 29     *                     When using bare metal, these are the same.
 30     * @param assertion_handler Function to call when an assertion fails.
 31     *                          Function takes a string pointer as an argument and must return a
 32     *                          boolean 'true'.
 33     */
 34    Example(uintptr_t base_address, bool (*assertion_handler) (const std::string*));
 35
 36    virtual ~Example() {}
 37
 38    // -------------------------------------------------------------------------
 39    // Methods for the 'config' register.
 40    // See interface header for documentation.
 41    virtual uint32_t get_config() const override;
 42    virtual uint32_t get_config_enable() const override;
 43    virtual uint32_t get_config_enable_from_value(
 44      uint32_t register_value
 45    ) const override;
 46    virtual example::config::direction::Enumeration get_config_direction() const override;
 47    virtual example::config::direction::Enumeration get_config_direction_from_value(
 48      uint32_t register_value
 49    ) const override;
 50    virtual void set_config(
 51      uint32_t register_value
 52    ) const override;
 53    virtual void set_config_enable(
 54      uint32_t field_value
 55    ) const override;
 56    virtual uint32_t set_config_enable_from_value(
 57      uint32_t register_value,
 58      uint32_t field_value
 59    ) const override;
 60    virtual void set_config_direction(
 61      example::config::direction::Enumeration field_value
 62    ) const override;
 63    virtual uint32_t set_config_direction_from_value(
 64      uint32_t register_value,
 65      example::config::direction::Enumeration field_value
 66    ) const override;
 67
 68    // -------------------------------------------------------------------------
 69    // Methods for the 'status' register.
 70    // See interface header for documentation.
 71    virtual uint32_t get_status() const override;
 72
 73    // -------------------------------------------------------------------------
 74    // Methods for the 'read_address' register within the 'channels' register array.
 75    // See interface header for documentation.
 76    virtual uint32_t get_channels_read_address(
 77      size_t array_index
 78    ) const override;
 79    virtual void set_channels_read_address(
 80      size_t array_index,
 81      uint32_t register_value
 82    ) const override;
 83
 84    // -------------------------------------------------------------------------
 85    // Methods for the 'config' register within the 'channels' register array.
 86    // See interface header for documentation.
 87    virtual void set_channels_config(
 88      size_t array_index,
 89      uint32_t register_value
 90    ) const override;
 91    virtual void set_channels_config_enable(
 92      size_t array_index,
 93      uint32_t field_value
 94    ) const override;
 95    virtual uint32_t set_channels_config_enable_from_value(
 96      uint32_t register_value,
 97      uint32_t field_value
 98    ) const override;
 99    virtual void set_channels_config_tuser(
100      size_t array_index,
101      uint32_t field_value
102    ) const override;
103    virtual uint32_t set_channels_config_tuser_from_value(
104      uint32_t register_value,
105      uint32_t field_value
106    ) const override;
107  };
108} /* namespace fpga_regs */

Implementation

Below is the generated class implementation:

Example class implementation
  1// -----------------------------------------------------------------------------
  2// This file is automatically generated by hdl-registers version 7.0.5-dev.
  3// Code generator CppImplementationGenerator version 2.0.1.
  4// Generated 2025-02-22 20:51 from file toml_format.toml at commit 57cebe3573f7.
  5// Register hash 4df9765ebb584803b583628671e4659579eb85f4.
  6// -----------------------------------------------------------------------------
  7
  8#include "include/example.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 << "Tried to set value out of range in "                       \
 25                  << "example.cpp:" << __LINE__                                  \
 26                  << ", message: " << message << ".";                            \
 27      std::string diagnostic_message = diagnostics.str();                        \
 28      m_assertion_handler(&diagnostic_message);                                  \
 29    }                                                                            \
 30  }
 31
 32#endif // NO_REGISTER_SETTER_ASSERT.
 33
 34#ifdef NO_REGISTER_GETTER_ASSERT
 35
 36#define _GETTER_ASSERT_TRUE(expression, message) ((void)0)
 37
 38#else // Not NO_REGISTER_GETTER_ASSERT.
 39
 40// This macro is called by the register code to check for runtime errors.
 41#define _GETTER_ASSERT_TRUE(expression, message)                                 \
 42  {                                                                              \
 43    if (!static_cast<bool>(expression)) {                                        \
 44      std::ostringstream diagnostics;                                            \
 45      diagnostics << "Got read value out of range in "                           \
 46                  << "example.cpp:" << __LINE__                                  \
 47                  << ", message: " << message << ".";                            \
 48      std::string diagnostic_message = diagnostics.str();                        \
 49      m_assertion_handler(&diagnostic_message);                                  \
 50    }                                                                            \
 51  }
 52
 53#endif // NO_REGISTER_GETTER_ASSERT.
 54
 55#ifdef NO_REGISTER_ARRAY_INDEX_ASSERT
 56
 57#define _ARRAY_INDEX_ASSERT_TRUE(expression, message) ((void)0)
 58
 59#else // Not NO_REGISTER_ARRAY_INDEX_ASSERT.
 60
 61// This macro is called by the register code to check for runtime errors.
 62#define _ARRAY_INDEX_ASSERT_TRUE(expression, message)                            \
 63  {                                                                              \
 64    if (!static_cast<bool>(expression)) {                                        \
 65      std::ostringstream diagnostics;                                            \
 66      diagnostics << "Provided array index out of range in "                     \
 67                  << "example.cpp:" << __LINE__                                  \
 68                  << ", message: " << message << ".";                            \
 69      std::string diagnostic_message = diagnostics.str();                        \
 70      m_assertion_handler(&diagnostic_message);                                  \
 71    }                                                                            \
 72  }
 73
 74#endif // NO_REGISTER_ARRAY_INDEX_ASSERT.
 75
 76  Example::Example(uintptr_t base_address, bool (*assertion_handler) (const std::string*))
 77      : m_registers(reinterpret_cast<volatile uint32_t *>(base_address)),
 78        m_assertion_handler(assertion_handler)
 79  {
 80    // Empty
 81  }
 82
 83  // ---------------------------------------------------------------------------
 84  // Methods for the 'config' register.
 85  // See interface header for documentation.
 86
 87  uint32_t Example::get_config() const
 88  {
 89    const size_t index = 0;
 90    const uint32_t result = m_registers[index];
 91
 92    return result;
 93  }
 94
 95  uint32_t Example::get_config_enable() const
 96  {
 97    const uint32_t register_value = get_config();
 98    const uint32_t field_value = get_config_enable_from_value(register_value);
 99
100    return field_value;
101  }
102
103  uint32_t Example::get_config_enable_from_value(
104    uint32_t register_value
105  ) const
106  {
107    const uint32_t shift = 0uL;
108    const uint32_t mask_at_base = 0b1uL;
109    const uint32_t mask_shifted = mask_at_base << shift;
110
111    const uint32_t result_masked = register_value & mask_shifted;
112    const uint32_t result_shifted = result_masked >> shift;
113
114    uint32_t field_value;
115
116    // No casting needed.
117    field_value = result_shifted;
118
119    return field_value;
120  }
121
122  example::config::direction::Enumeration Example::get_config_direction() const
123  {
124    const uint32_t register_value = get_config();
125    const example::config::direction::Enumeration field_value = get_config_direction_from_value(register_value);
126
127    return field_value;
128  }
129
130  example::config::direction::Enumeration Example::get_config_direction_from_value(
131    uint32_t register_value
132  ) const
133  {
134    const uint32_t shift = 1uL;
135    const uint32_t mask_at_base = 0b11uL;
136    const uint32_t mask_shifted = mask_at_base << shift;
137
138    const uint32_t result_masked = register_value & mask_shifted;
139    const uint32_t result_shifted = result_masked >> shift;
140
141    example::config::direction::Enumeration field_value;
142
143    // "Cast" to the enum type.
144    field_value = example::config::direction::Enumeration(result_shifted);
145
146    return field_value;
147  }
148
149  void Example::set_config(
150    uint32_t register_value
151  ) const
152  {
153    const size_t index = 0;
154    m_registers[index] = register_value;
155  }
156
157  void Example::set_config_enable(
158    uint32_t field_value
159  ) const
160  {
161    // Get the current value of other fields by reading register on the bus.
162    const uint32_t current_register_value = get_config();
163    const uint32_t result_register_value = set_config_enable_from_value(current_register_value, field_value);
164    set_config(result_register_value);
165  }
166
167  uint32_t Example::set_config_enable_from_value(
168    uint32_t register_value,
169    uint32_t field_value
170  ) const  {
171    const uint32_t shift = 0uL;
172    const uint32_t mask_at_base = 0b1uL;
173    const uint32_t mask_shifted = mask_at_base << shift;
174
175    // Check that field value is within the legal range.
176    const uint32_t mask_at_base_inverse = ~mask_at_base;
177    _SETTER_ASSERT_TRUE(
178      (field_value & mask_at_base_inverse) == 0,
179      "'enable' value too many bits used, got '" << field_value << "'"
180    );
181
182    const uint32_t field_value_masked = field_value & mask_at_base;
183    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
184
185    const uint32_t mask_shifted_inverse = ~mask_shifted;
186    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
187
188    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
189
190    return result_register_value;
191  }
192
193  void Example::set_config_direction(
194    example::config::direction::Enumeration field_value
195  ) const
196  {
197    // Get the current value of other fields by reading register on the bus.
198    const uint32_t current_register_value = get_config();
199    const uint32_t result_register_value = set_config_direction_from_value(current_register_value, field_value);
200    set_config(result_register_value);
201  }
202
203  uint32_t Example::set_config_direction_from_value(
204    uint32_t register_value,
205    example::config::direction::Enumeration field_value
206  ) const  {
207    const uint32_t shift = 1uL;
208    const uint32_t mask_at_base = 0b11uL;
209    const uint32_t mask_shifted = mask_at_base << shift;
210
211    const uint32_t field_value_masked = field_value & mask_at_base;
212    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
213
214    const uint32_t mask_shifted_inverse = ~mask_shifted;
215    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
216
217    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
218
219    return result_register_value;
220  }
221
222  // ---------------------------------------------------------------------------
223  // Methods for the 'status' register.
224  // See interface header for documentation.
225
226  uint32_t Example::get_status() const
227  {
228    const size_t index = 1;
229    const uint32_t result = m_registers[index];
230
231    return result;
232  }
233
234  // ---------------------------------------------------------------------------
235  // Methods for the 'read_address' register within the 'channels' register array.
236  // See interface header for documentation.
237
238  uint32_t Example::get_channels_read_address(
239    size_t array_index
240  ) const
241  {
242    _ARRAY_INDEX_ASSERT_TRUE(
243      array_index < example::channels::array_length,
244      "'channels' array index out of range, got '" << array_index << "'"
245    );
246
247    const size_t index = 2 + array_index * 2 + 0;
248    const uint32_t result = m_registers[index];
249
250    return result;
251  }
252
253  void Example::set_channels_read_address(
254    size_t array_index,
255    uint32_t register_value
256  ) const
257  {
258    _ARRAY_INDEX_ASSERT_TRUE(
259      array_index < example::channels::array_length,
260      "'channels' array index out of range, got '" << array_index << "'"
261    );
262
263    const size_t index = 2 + array_index * 2 + 0;
264    m_registers[index] = register_value;
265  }
266
267  // ---------------------------------------------------------------------------
268  // Methods for the 'config' register within the 'channels' register array.
269  // See interface header for documentation.
270
271  void Example::set_channels_config(
272    size_t array_index,
273    uint32_t register_value
274  ) const
275  {
276    _ARRAY_INDEX_ASSERT_TRUE(
277      array_index < example::channels::array_length,
278      "'channels' array index out of range, got '" << array_index << "'"
279    );
280
281    const size_t index = 2 + array_index * 2 + 1;
282    m_registers[index] = register_value;
283  }
284
285  void Example::set_channels_config_enable(
286    size_t array_index,
287    uint32_t field_value
288  ) const
289  {
290    // Set everything except for the field to default when writing the value.
291    const uint32_t current_register_value = 0;
292    const uint32_t result_register_value = set_channels_config_enable_from_value(current_register_value, field_value);
293    set_channels_config(array_index, result_register_value);
294  }
295
296  uint32_t Example::set_channels_config_enable_from_value(
297    uint32_t register_value,
298    uint32_t field_value
299  ) const  {
300    const uint32_t shift = 0uL;
301    const uint32_t mask_at_base = 0b1uL;
302    const uint32_t mask_shifted = mask_at_base << shift;
303
304    // Check that field value is within the legal range.
305    const uint32_t mask_at_base_inverse = ~mask_at_base;
306    _SETTER_ASSERT_TRUE(
307      (field_value & mask_at_base_inverse) == 0,
308      "'enable' value too many bits used, got '" << field_value << "'"
309    );
310
311    const uint32_t field_value_masked = field_value & mask_at_base;
312    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
313
314    const uint32_t mask_shifted_inverse = ~mask_shifted;
315    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
316
317    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
318
319    return result_register_value;
320  }
321
322  void Example::set_channels_config_tuser(
323    size_t array_index,
324    uint32_t field_value
325  ) const
326  {
327    // Set everything except for the field to default when writing the value.
328    const uint32_t current_register_value = 0;
329    const uint32_t result_register_value = set_channels_config_tuser_from_value(current_register_value, field_value);
330    set_channels_config(array_index, result_register_value);
331  }
332
333  uint32_t Example::set_channels_config_tuser_from_value(
334    uint32_t register_value,
335    uint32_t field_value
336  ) const  {
337    const uint32_t shift = 1uL;
338    const uint32_t mask_at_base = 0b11111111uL;
339    const uint32_t mask_shifted = mask_at_base << shift;
340
341    // Check that field value is within the legal range.
342    const uint32_t mask_at_base_inverse = ~mask_at_base;
343    _SETTER_ASSERT_TRUE(
344      (field_value & mask_at_base_inverse) == 0,
345      "'tuser' value too many bits used, got '" << field_value << "'"
346    );
347
348    const uint32_t field_value_masked = field_value & mask_at_base;
349    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
350
351    const uint32_t mask_shifted_inverse = ~mask_shifted;
352    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
353
354    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
355
356    return result_register_value;
357  }
358
359} /* namespace fpga_regs */

Note that when the register is part of an array, the register setter/getter takes a second argument array_index. There is an assert that the user-provided array index is within the bounds of the array.