Bit vector fields

Register fields can be of the type bit vector. Meaning, an array of logic bits.

This page will show you how the set up bit vector 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 two bit vector fields. See comments for rules about the different properties.

TOML that sets up a register with bit vector fields.
 1[conf]
 2
 3mode = "r_w"
 4description = "Configuration register."
 5
 6# This will allocate a bit vector field named "tuser" in the "conf" register.
 7# The "type" property MUST be present and set to "bit_vector".
 8tuser.type = "bit_vector"
 9
10# The "width" property MUST be present for a bit vector field.
11# The value specified MUST be a positive integer.
12tuser.width = 4
13
14# The "description" property is OPTIONAL for a bit vector field.
15# Will default to "" if not specified.
16# The value specified MUST be a string.
17tuser.description = "Value to set for **TUSER** in the data stream."
18
19# The "default_value" property is OPTIONAL for a bit vector field.
20# Will default to all zeros if not specified.
21# The value specified MUST be either
22# 1. A string of 1's and 0's. The string length MUST be the same as the field width.
23# 2. An unsigned integer. The value's binary representation MUST fit within the field width.
24tuser.default_value = "0101"
25
26
27tid.type = "bit_vector"
28tid.width = 8
29tid.description = "Value to set for **TID** in the data stream."
30tid.default_value = 0xf3
31
32
33tdest.type = "bit_vector"
34tdest.width = 3
35tdest.description = "Value to set for **TDEST** in the data stream."

Note that the third field does not have any default value specified, meaning it will default to all zeros.

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 bit vector 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_bit_vector.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_bit_vector(
35        name="tuser",
36        description="Value to set for **TUSER** in the data stream.",
37        width=4,
38        default_value="0101",
39    )
40
41    register.append_bit_vector(
42        name="tid",
43        description="Value to set for **TID** in the data stream.",
44        width=8,
45        default_value=0xF3,
46    )
47
48    register.append_bit_vector(
49        name="tdest",
50        description="Value to set for **TDEST** in the data stream.",
51        width=3,
52        default_value=0,
53    )
54
55    return register_list
56
57
58def generate(register_list: RegisterList, output_folder: Path) -> None:
59    """
60    Generate the artifacts that we are interested in.
61    """
62    CHeaderGenerator(register_list=register_list, output_folder=output_folder).create()
63
64    CppImplementationGenerator(register_list=register_list, output_folder=output_folder).create()
65    CppInterfaceGenerator(register_list=register_list, output_folder=output_folder).create()
66
67    HtmlPageGenerator(register_list=register_list, output_folder=output_folder).create()
68
69    VhdlRegisterPackageGenerator(register_list=register_list, output_folder=output_folder).create()
70    VhdlRecordPackageGenerator(register_list=register_list, output_folder=output_folder).create()
71
72
73def main(output_folder: Path) -> None:
74    generate(register_list=parse_toml(), output_folder=output_folder / "toml")
75    generate(register_list=create_from_api(), output_folder=output_folder / "api")
76
77
78if __name__ == "__main__":
79    main(output_folder=Path(sys.argv[1]))

See Register.append_bit_vector() and the BitVector class for more Python API details.

Generated code

See below for a description of the code that can be generated when using bit vector 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 bit vector field is documented with its range, default value and description.

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. The first field is four bits wide, occupying bits 3 down to 0, while the second one is eight bits wide, occupying but 11 down to 4.

  3. For each bit vector field there is a named integer subtype that defines the fields’s bit range within the register.

  4. In VHDL, slicing out a range from the register value will yield a value of type std_ulogic_vector, meaning that typically no casting is needed. Hence there are no conversion functions for bit vector fields, the way there are for e.g. enumeration fields.

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-01 11:34 at Git commit 228a22928e9a.
 5-- Register hash d4bbf353ec4cd79009cabb4c3f23dbd4cce80525.
 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 'tuser' field.
51  subtype caesar_conf_tuser is natural range 3 downto 0;
52  -- Width of the 'tuser' field.
53  constant caesar_conf_tuser_width : positive := 4;
54  -- Type for the 'tuser' field.
55  subtype caesar_conf_tuser_t is u_unsigned(3 downto 0);
56  -- Default value of the 'tuser' field.
57  constant caesar_conf_tuser_init : caesar_conf_tuser_t := "0101";
58
59  -- Range of the 'tid' field.
60  subtype caesar_conf_tid is natural range 11 downto 4;
61  -- Width of the 'tid' field.
62  constant caesar_conf_tid_width : positive := 8;
63  -- Type for the 'tid' field.
64  subtype caesar_conf_tid_t is u_unsigned(7 downto 0);
65  -- Default value of the 'tid' field.
66  constant caesar_conf_tid_init : caesar_conf_tid_t := "11110011";
67
68  -- Range of the 'tdest' field.
69  subtype caesar_conf_tdest is natural range 14 downto 12;
70  -- Width of the 'tdest' field.
71  constant caesar_conf_tdest_width : positive := 3;
72  -- Type for the 'tdest' field.
73  subtype caesar_conf_tdest_t is u_unsigned(2 downto 0);
74  -- Default value of the 'tdest' field.
75  constant caesar_conf_tdest_init : caesar_conf_tdest_t := "000";
76
77end package;
78
79package body caesar_regs_pkg is
80
81  constant caesar_register_map : register_definition_vec_t(caesar_register_range) := (
82    0 => (index => caesar_conf, mode => r_w, utilized_width => 15)
83  );
84
85  constant caesar_regs_init : caesar_regs_t := (
86    0 => "00000000000000000000111100110101"
87  );
88
89end 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 two bit vectors set up in our example: tuser and tid. These are of unsigned vector types defined in the base register package above.

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

result_tuser <= regs_down.config.tuser;
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-01 11:34 at Git commit 228a22928e9a.
  5-- Register hash d4bbf353ec4cd79009cabb4c3f23dbd4cce80525.
  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    tuser : caesar_conf_tuser_t;
 26    tid : caesar_conf_tid_t;
 27    tdest : caesar_conf_tdest_t;
 28  end record;
 29  -- Default value for the 'conf' register as a record.
 30  constant caesar_conf_init : caesar_conf_t := (
 31    tuser => caesar_conf_tuser_init,
 32    tid => caesar_conf_tid_init,
 33    tdest => caesar_conf_tdest_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_tuser) := std_ulogic_vector(data.tuser);
 94    result(caesar_conf_tid) := std_ulogic_vector(data.tid);
 95    result(caesar_conf_tdest) := std_ulogic_vector(data.tdest);
 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.tuser := caesar_conf_tuser_t(data(caesar_conf_tuser));
104    result.tid := caesar_conf_tid_t(data(caesar_conf_tid));
105    result.tdest := caesar_conf_tdest_t(data(caesar_conf_tdest));
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 the setters and getters for each individual field value.

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-01 11:34 at Git commit 228a22928e9a.
  5// Register hash d4bbf353ec4cd79009cabb4c3f23dbd4cce80525.
  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 'tuser' field in the 'conf' register.
 18  namespace caesar::conf::tuser
 19  {
 20    static const auto width = 4;
 21    static const auto default_value = 0b0101;
 22  }
 23  // Attributes for the 'tid' field in the 'conf' register.
 24  namespace caesar::conf::tid
 25  {
 26    static const auto width = 8;
 27    static const auto default_value = 0b11110011;
 28  }
 29  // Attributes for the 'tdest' field in the 'conf' register.
 30  namespace caesar::conf::tdest
 31  {
 32    static const auto width = 3;
 33    static const auto default_value = 0b000;
 34  }
 35
 36  class ICaesar
 37  {
 38  public:
 39    // Number of registers within this register list.
 40    static const size_t num_registers = 1uL;
 41
 42    virtual ~ICaesar() {}
 43
 44    // -------------------------------------------------------------------------
 45    // Methods for the 'conf' register. Mode 'Read, Write'.
 46
 47    // Getter that will read the whole register's value over the register bus.
 48    virtual uint32_t get_conf() const = 0;
 49
 50    // Setter that will write the whole register's value over the register bus.
 51    virtual void set_conf(
 52      uint32_t register_value
 53    ) const = 0;
 54
 55    // Getter for the 'tuser' field in the 'conf' register,
 56    // which will read register value over the register bus.
 57    virtual uint32_t get_conf_tuser() const = 0;
 58    // Getter for the 'tuser' field in the 'conf' register,
 59    // given a register value.
 60    virtual uint32_t get_conf_tuser_from_value(
 61      uint32_t register_value
 62    ) const = 0;
 63    // Setter for the 'tuser' field in the 'conf' register,
 64    // which will read-modify-write over the register bus.
 65    virtual void set_conf_tuser(
 66      uint32_t field_value
 67    ) const = 0;
 68    // Setter for the 'tuser' field in the 'conf' register,
 69    // given a register value, which will return an updated value.
 70    virtual uint32_t set_conf_tuser_from_value(
 71      uint32_t register_value,
 72      uint32_t field_value
 73    ) const = 0;
 74
 75    // Getter for the 'tid' field in the 'conf' register,
 76    // which will read register value over the register bus.
 77    virtual uint32_t get_conf_tid() const = 0;
 78    // Getter for the 'tid' field in the 'conf' register,
 79    // given a register value.
 80    virtual uint32_t get_conf_tid_from_value(
 81      uint32_t register_value
 82    ) const = 0;
 83    // Setter for the 'tid' field in the 'conf' register,
 84    // which will read-modify-write over the register bus.
 85    virtual void set_conf_tid(
 86      uint32_t field_value
 87    ) const = 0;
 88    // Setter for the 'tid' field in the 'conf' register,
 89    // given a register value, which will return an updated value.
 90    virtual uint32_t set_conf_tid_from_value(
 91      uint32_t register_value,
 92      uint32_t field_value
 93    ) const = 0;
 94
 95    // Getter for the 'tdest' field in the 'conf' register,
 96    // which will read register value over the register bus.
 97    virtual uint32_t get_conf_tdest() const = 0;
 98    // Getter for the 'tdest' field in the 'conf' register,
 99    // given a register value.
100    virtual uint32_t get_conf_tdest_from_value(
101      uint32_t register_value
102    ) const = 0;
103    // Setter for the 'tdest' field in the 'conf' register,
104    // which will read-modify-write over the register bus.
105    virtual void set_conf_tdest(
106      uint32_t field_value
107    ) const = 0;
108    // Setter for the 'tdest' field in the 'conf' register,
109    // given a register value, which will return an updated value.
110    virtual uint32_t set_conf_tdest_from_value(
111      uint32_t register_value,
112      uint32_t field_value
113    ) const = 0;
114
115  };
116
117} /* namespace fpga_regs */

C++ implementation

Note that each setter performs 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-01 11:34 at Git commit 228a22928e9a.
  5// Register hash d4bbf353ec4cd79009cabb4c3f23dbd4cce80525.
  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  // See interface header for documentation.
 83
 84  uint32_t Caesar::get_conf() const
 85  {
 86    const size_t index = 0;
 87    const uint32_t result = m_registers[index];
 88
 89    return result;
 90  }
 91
 92  uint32_t Caesar::get_conf_tuser() const
 93  {
 94    const uint32_t register_value = get_conf();
 95    const uint32_t field_value = get_conf_tuser_from_value(register_value);
 96
 97    return field_value;
 98  }
 99
100  uint32_t Caesar::get_conf_tuser_from_value(
101    uint32_t register_value
102  ) const
103  {
104    const uint32_t shift = 0uL;
105    const uint32_t mask_at_base = 0b1111uL;
106    const uint32_t mask_shifted = mask_at_base << shift;
107
108    const uint32_t result_masked = register_value & mask_shifted;
109    const uint32_t result_shifted = result_masked >> shift;
110
111    uint32_t field_value;
112
113    // No casting needed.
114    field_value = result_shifted;
115
116    return field_value;
117  }
118
119  uint32_t Caesar::get_conf_tid() const
120  {
121    const uint32_t register_value = get_conf();
122    const uint32_t field_value = get_conf_tid_from_value(register_value);
123
124    return field_value;
125  }
126
127  uint32_t Caesar::get_conf_tid_from_value(
128    uint32_t register_value
129  ) const
130  {
131    const uint32_t shift = 4uL;
132    const uint32_t mask_at_base = 0b11111111uL;
133    const uint32_t mask_shifted = mask_at_base << shift;
134
135    const uint32_t result_masked = register_value & mask_shifted;
136    const uint32_t result_shifted = result_masked >> shift;
137
138    uint32_t field_value;
139
140    // No casting needed.
141    field_value = result_shifted;
142
143    return field_value;
144  }
145
146  uint32_t Caesar::get_conf_tdest() const
147  {
148    const uint32_t register_value = get_conf();
149    const uint32_t field_value = get_conf_tdest_from_value(register_value);
150
151    return field_value;
152  }
153
154  uint32_t Caesar::get_conf_tdest_from_value(
155    uint32_t register_value
156  ) const
157  {
158    const uint32_t shift = 12uL;
159    const uint32_t mask_at_base = 0b111uL;
160    const uint32_t mask_shifted = mask_at_base << shift;
161
162    const uint32_t result_masked = register_value & mask_shifted;
163    const uint32_t result_shifted = result_masked >> shift;
164
165    uint32_t field_value;
166
167    // No casting needed.
168    field_value = result_shifted;
169
170    return field_value;
171  }
172
173  void Caesar::set_conf(
174    uint32_t register_value
175  ) const
176  {
177    const size_t index = 0;
178    m_registers[index] = register_value;
179  }
180
181  void Caesar::set_conf_tuser(
182    uint32_t field_value
183  ) const
184  {
185    // Get the current value of other fields by reading register on the bus.
186    const uint32_t current_register_value = get_conf();
187    const uint32_t result_register_value = set_conf_tuser_from_value(current_register_value, field_value);
188    set_conf(result_register_value);
189  }
190
191  uint32_t Caesar::set_conf_tuser_from_value(
192    uint32_t register_value,
193    uint32_t field_value
194  ) const  {
195    const uint32_t shift = 0uL;
196    const uint32_t mask_at_base = 0b1111uL;
197    const uint32_t mask_shifted = mask_at_base << shift;
198
199    // Check that field value is within the legal range.
200    const uint32_t mask_at_base_inverse = ~mask_at_base;
201    _SETTER_ASSERT_TRUE(
202      (field_value & mask_at_base_inverse) == 0,
203      "Got 'tuser' value too many bits used: " << field_value
204    );
205
206    const uint32_t field_value_masked = field_value & mask_at_base;
207    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
208
209    const uint32_t mask_shifted_inverse = ~mask_shifted;
210    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
211
212    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
213
214    return result_register_value;
215  }
216
217  void Caesar::set_conf_tid(
218    uint32_t field_value
219  ) const
220  {
221    // Get the current value of other fields by reading register on the bus.
222    const uint32_t current_register_value = get_conf();
223    const uint32_t result_register_value = set_conf_tid_from_value(current_register_value, field_value);
224    set_conf(result_register_value);
225  }
226
227  uint32_t Caesar::set_conf_tid_from_value(
228    uint32_t register_value,
229    uint32_t field_value
230  ) const  {
231    const uint32_t shift = 4uL;
232    const uint32_t mask_at_base = 0b11111111uL;
233    const uint32_t mask_shifted = mask_at_base << shift;
234
235    // Check that field value is within the legal range.
236    const uint32_t mask_at_base_inverse = ~mask_at_base;
237    _SETTER_ASSERT_TRUE(
238      (field_value & mask_at_base_inverse) == 0,
239      "Got 'tid' value too many bits used: " << field_value
240    );
241
242    const uint32_t field_value_masked = field_value & mask_at_base;
243    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
244
245    const uint32_t mask_shifted_inverse = ~mask_shifted;
246    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
247
248    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
249
250    return result_register_value;
251  }
252
253  void Caesar::set_conf_tdest(
254    uint32_t field_value
255  ) const
256  {
257    // Get the current value of other fields by reading register on the bus.
258    const uint32_t current_register_value = get_conf();
259    const uint32_t result_register_value = set_conf_tdest_from_value(current_register_value, field_value);
260    set_conf(result_register_value);
261  }
262
263  uint32_t Caesar::set_conf_tdest_from_value(
264    uint32_t register_value,
265    uint32_t field_value
266  ) const  {
267    const uint32_t shift = 12uL;
268    const uint32_t mask_at_base = 0b111uL;
269    const uint32_t mask_shifted = mask_at_base << shift;
270
271    // Check that field value is within the legal range.
272    const uint32_t mask_at_base_inverse = ~mask_at_base;
273    _SETTER_ASSERT_TRUE(
274      (field_value & mask_at_base_inverse) == 0,
275      "Got 'tdest' value too many bits used: " << field_value
276    );
277
278    const uint32_t field_value_masked = field_value & mask_at_base;
279    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
280
281    const uint32_t mask_shifted_inverse = ~mask_shifted;
282    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
283
284    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
285
286    return result_register_value;
287  }
288
289} /* 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-01 11:34 at Git commit 228a22928e9a.
 5// Register hash d4bbf353ec4cd79009cabb4c3f23dbd4cce80525.
 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 'tuser' field in the 'conf' register.
27#define CAESAR_CONF_TUSER_SHIFT (0u)
28#define CAESAR_CONF_TUSER_MASK (0b1111u << 0u)
29#define CAESAR_CONF_TUSER_MASK_INVERSE (~CAESAR_CONF_TUSER_MASK)
30// Attributes for the 'tid' field in the 'conf' register.
31#define CAESAR_CONF_TID_SHIFT (4u)
32#define CAESAR_CONF_TID_MASK (0b11111111u << 4u)
33#define CAESAR_CONF_TID_MASK_INVERSE (~CAESAR_CONF_TID_MASK)
34// Attributes for the 'tdest' field in the 'conf' register.
35#define CAESAR_CONF_TDEST_SHIFT (12u)
36#define CAESAR_CONF_TDEST_MASK (0b111u << 12u)
37#define CAESAR_CONF_TDEST_MASK_INVERSE (~CAESAR_CONF_TDEST_MASK)
38
39#endif // CAESAR_REGS_H