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

See Register.append_integer() 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 code 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 code 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-- This file is automatically generated by hdl-registers version 6.2.1-dev.
  2-- Code generator VhdlRegisterPackageGenerator version 1.0.0.
  3-- Generated 2024-12-19 20:52 at commit cd01ff93f646632c.
  4-- Register hash 805a50de03b71b95d3523176ad9155b5fe7be903.
  5
  6library ieee;
  7use ieee.std_logic_1164.all;
  8use ieee.numeric_std.all;
  9use ieee.fixed_pkg.all;
 10
 11library reg_file;
 12use reg_file.reg_file_pkg.all;
 13
 14
 15package caesar_regs_pkg is
 16
 17  -- ---------------------------------------------------------------------------
 18  -- The valid range of register indexes.
 19  subtype caesar_reg_range is natural range 0 to 0;
 20
 21  -- Register indexes, within the list of registers.
 22  constant caesar_config : natural := 0;
 23
 24  -- Declare 'reg_map' and 'regs_init' constants here but define them in body (deferred constants).
 25  -- So that functions have been elaborated when they are called.
 26  -- Needed for ModelSim compilation to pass.
 27
 28  -- To be used as the 'regs' generic of 'axi_lite_reg_file.vhd'.
 29  constant caesar_reg_map : reg_definition_vec_t(caesar_reg_range);
 30
 31  -- To be used for the 'regs_up' and 'regs_down' ports of 'axi_lite_reg_file.vhd'.
 32  subtype caesar_regs_t is reg_vec_t(caesar_reg_range);
 33  -- To be used as the 'default_values' generic of 'axi_lite_reg_file.vhd'.
 34  constant caesar_regs_init : caesar_regs_t;
 35
 36  -- To be used for the 'reg_was_read' and 'reg_was_written' ports of 'axi_lite_reg_file.vhd'.
 37  subtype caesar_reg_was_accessed_t is std_ulogic_vector(caesar_reg_range);
 38
 39  -- -----------------------------------------------------------------------------
 40  -- Fields in the 'config' register.
 41  -- Range of the 'burst_length_bytes' field.
 42  subtype caesar_config_burst_length_bytes is natural range 8 downto 0;
 43  -- Width of the 'burst_length_bytes' field.
 44  constant caesar_config_burst_length_bytes_width : positive := 9;
 45  -- Type for the 'burst_length_bytes' field.
 46  subtype caesar_config_burst_length_bytes_t is integer range 1 to 256;
 47  -- Default value of the 'burst_length_bytes' field.
 48  constant caesar_config_burst_length_bytes_init : caesar_config_burst_length_bytes_t := 64;
 49  -- Type for the 'burst_length_bytes' field as an SLV.
 50  subtype caesar_config_burst_length_bytes_slv_t is std_ulogic_vector(8 downto 0);
 51  -- Cast a 'burst_length_bytes' field value to SLV.
 52  function to_caesar_config_burst_length_bytes_slv(data : caesar_config_burst_length_bytes_t) return caesar_config_burst_length_bytes_slv_t;
 53  -- Get a 'burst_length_bytes' field value from a register value.
 54  function to_caesar_config_burst_length_bytes(data : reg_t) return caesar_config_burst_length_bytes_t;
 55
 56  -- Range of the 'increment' field.
 57  subtype caesar_config_increment is natural range 11 downto 9;
 58  -- Width of the 'increment' field.
 59  constant caesar_config_increment_width : positive := 3;
 60  -- Type for the 'increment' field.
 61  subtype caesar_config_increment_t is integer range -4 to 3;
 62  -- Default value of the 'increment' field.
 63  constant caesar_config_increment_init : caesar_config_increment_t := 0;
 64  -- Type for the 'increment' field as an SLV.
 65  subtype caesar_config_increment_slv_t is std_ulogic_vector(2 downto 0);
 66  -- Cast a 'increment' field value to SLV.
 67  function to_caesar_config_increment_slv(data : caesar_config_increment_t) return caesar_config_increment_slv_t;
 68  -- Get a 'increment' field value from a register value.
 69  function to_caesar_config_increment(data : reg_t) return caesar_config_increment_t;
 70
 71  -- Range of the 'retry_count' field.
 72  subtype caesar_config_retry_count is natural range 14 downto 12;
 73  -- Width of the 'retry_count' field.
 74  constant caesar_config_retry_count_width : positive := 3;
 75  -- Type for the 'retry_count' field.
 76  subtype caesar_config_retry_count_t is integer range 0 to 5;
 77  -- Default value of the 'retry_count' field.
 78  constant caesar_config_retry_count_init : caesar_config_retry_count_t := 0;
 79  -- Type for the 'retry_count' field as an SLV.
 80  subtype caesar_config_retry_count_slv_t is std_ulogic_vector(2 downto 0);
 81  -- Cast a 'retry_count' field value to SLV.
 82  function to_caesar_config_retry_count_slv(data : caesar_config_retry_count_t) return caesar_config_retry_count_slv_t;
 83  -- Get a 'retry_count' field value from a register value.
 84  function to_caesar_config_retry_count(data : reg_t) return caesar_config_retry_count_t;
 85
 86end package;
 87
 88package body caesar_regs_pkg is
 89
 90  constant caesar_reg_map : reg_definition_vec_t(caesar_reg_range) := (
 91    0 => (idx => caesar_config, reg_type => r_w)
 92  );
 93
 94  constant caesar_regs_init : caesar_regs_t := (
 95    0 => "00000000000000000000000001000000"
 96  );
 97
 98  -- Cast a 'burst_length_bytes' field value to SLV.
 99  function to_caesar_config_burst_length_bytes_slv(data : caesar_config_burst_length_bytes_t) return caesar_config_burst_length_bytes_slv_t is
100    constant result : caesar_config_burst_length_bytes_slv_t := std_ulogic_vector(to_unsigned(data, caesar_config_burst_length_bytes_width));
101  begin
102    return result;
103  end function;
104
105  -- Get a 'burst_length_bytes' field value from a register value.
106  function to_caesar_config_burst_length_bytes(data : reg_t) return caesar_config_burst_length_bytes_t is
107    constant result : integer := to_integer(unsigned(data(caesar_config_burst_length_bytes)));
108  begin
109    return result;
110  end function;
111
112  -- Cast a 'increment' field value to SLV.
113  function to_caesar_config_increment_slv(data : caesar_config_increment_t) return caesar_config_increment_slv_t is
114    constant result : caesar_config_increment_slv_t := std_ulogic_vector(to_signed(data, caesar_config_increment_width));
115  begin
116    return result;
117  end function;
118
119  -- Get a 'increment' field value from a register value.
120  function to_caesar_config_increment(data : reg_t) return caesar_config_increment_t is
121    constant result : integer := to_integer(signed(data(caesar_config_increment)));
122  begin
123    return result;
124  end function;
125
126  -- Cast a 'retry_count' field value to SLV.
127  function to_caesar_config_retry_count_slv(data : caesar_config_retry_count_t) return caesar_config_retry_count_slv_t is
128    constant result : caesar_config_retry_count_slv_t := std_ulogic_vector(to_unsigned(data, caesar_config_retry_count_width));
129  begin
130    return result;
131  end function;
132
133  -- Get a 'retry_count' field value from a register value.
134  function to_caesar_config_retry_count(data : reg_t) return caesar_config_retry_count_t is
135    constant result : integer := to_integer(unsigned(data(caesar_config_retry_count)));
136  begin
137    return result;
138  end function;
139
140end 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-- This file is automatically generated by hdl-registers version 6.2.1-dev.
  2-- Code generator VhdlRecordPackageGenerator version 1.0.0.
  3-- Generated 2024-12-19 20:52 at commit cd01ff93f646632c.
  4-- Register hash 805a50de03b71b95d3523176ad9155b5fe7be903.
  5
  6library ieee;
  7use ieee.fixed_pkg.all;
  8use ieee.std_logic_1164.all;
  9use ieee.numeric_std.all;
 10
 11library reg_file;
 12use reg_file.reg_file_pkg.reg_t;
 13
 14use work.caesar_regs_pkg.all;
 15
 16
 17package caesar_register_record_pkg is
 18
 19  -- -----------------------------------------------------------------------------
 20  -- Record with correctly-typed members for each field in each register.
 21  -- Fields in the 'config' register as a record.
 22  type caesar_config_t is record
 23    burst_length_bytes : caesar_config_burst_length_bytes_t;
 24    increment : caesar_config_increment_t;
 25    retry_count : caesar_config_retry_count_t;
 26  end record;
 27  -- Default value for the 'config' register as a record.
 28  constant caesar_config_init : caesar_config_t := (
 29    burst_length_bytes => caesar_config_burst_length_bytes_init,
 30    increment => caesar_config_increment_init,
 31    retry_count => caesar_config_retry_count_init
 32  );
 33  -- Convert a record of the 'config' register to SLV.
 34  function to_slv(data : caesar_config_t) return reg_t;
 35  -- Convert an SLV register value to the record for the 'config' register.
 36  function to_caesar_config(data : reg_t) return caesar_config_t;
 37
 38  -- -----------------------------------------------------------------------------
 39  -- Below is a record with correctly typed and ranged members for all registers, register arrays
 40  -- and fields that are in the 'down' direction.
 41  -- Record with everything in the 'down' direction.
 42  type caesar_regs_down_t is record
 43    config : caesar_config_t;
 44  end record;
 45  -- Default value of the above record.
 46  constant caesar_regs_down_init : caesar_regs_down_t := (
 47    config => caesar_config_init
 48  );
 49  -- Convert SLV register list to record with everything in the 'down' direction.
 50  function to_caesar_regs_down(data : caesar_regs_t) return caesar_regs_down_t;
 51
 52  -- ---------------------------------------------------------------------------
 53  -- Below is a record with a status bit for each readable register in the register map.
 54  -- It can be used for the 'reg_was_read' port of a register file wrapper.
 55  -- Combined status mask record for all readable register.
 56  type caesar_reg_was_read_t is record
 57    config : std_ulogic;
 58  end record;
 59  -- Default value for the above record.
 60  constant caesar_reg_was_read_init : caesar_reg_was_read_t := (
 61    others => '0'
 62  );
 63  -- Convert an SLV 'reg_was_read' from generic register file to the record above.
 64  function to_caesar_reg_was_read(
 65    data : caesar_reg_was_accessed_t
 66  ) return caesar_reg_was_read_t;
 67
 68  -- ---------------------------------------------------------------------------
 69  -- Below is a record with a status bit for each writeable register in the register map.
 70  -- It can be used for the 'reg_was_written' port of a register file wrapper.
 71  -- Combined status mask record for all writeable register.
 72  type caesar_reg_was_written_t is record
 73    config : std_ulogic;
 74  end record;
 75  -- Default value for the above record.
 76  constant caesar_reg_was_written_init : caesar_reg_was_written_t := (
 77    others => '0'
 78  );
 79  -- Convert an SLV 'reg_was_written' from generic register file to the record above.
 80  function to_caesar_reg_was_written(
 81    data : caesar_reg_was_accessed_t
 82  ) return caesar_reg_was_written_t;
 83
 84end package;
 85
 86package body caesar_register_record_pkg is
 87
 88  function to_slv(data : caesar_config_t) return reg_t is
 89    variable result : reg_t := (others => '-');
 90  begin
 91    result(caesar_config_burst_length_bytes) := to_caesar_config_burst_length_bytes_slv(data.burst_length_bytes);
 92    result(caesar_config_increment) := to_caesar_config_increment_slv(data.increment);
 93    result(caesar_config_retry_count) := to_caesar_config_retry_count_slv(data.retry_count);
 94
 95    return result;
 96  end function;
 97
 98  function to_caesar_config(data : reg_t) return caesar_config_t is
 99    variable result : caesar_config_t := caesar_config_init;
100  begin
101    result.burst_length_bytes := to_caesar_config_burst_length_bytes(data);
102    result.increment := to_caesar_config_increment(data);
103    result.retry_count := to_caesar_config_retry_count(data);
104
105    return result;
106  end function;
107
108  function to_caesar_regs_down(data : caesar_regs_t) return caesar_regs_down_t is
109    variable result : caesar_regs_down_t := caesar_regs_down_init;
110  begin
111    result.config := to_caesar_config(data(caesar_config));
112
113    return result;
114  end function;
115
116  function to_caesar_reg_was_read(
117    data : caesar_reg_was_accessed_t
118  ) return caesar_reg_was_read_t is
119    variable result : caesar_reg_was_read_t := caesar_reg_was_read_init;
120  begin
121    result.config := data(caesar_config);
122
123    return result;
124  end function;
125
126  function to_caesar_reg_was_written(
127    data : caesar_reg_was_accessed_t
128  ) return caesar_reg_was_written_t is
129    variable result : caesar_reg_was_written_t := caesar_reg_was_written_init;
130  begin
131    result.config := data(caesar_config);
132
133    return result;
134  end function;
135
136end 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++ code 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// This file is automatically generated by hdl-registers version 6.2.1-dev.
  2// Code generator CppInterfaceGenerator version 1.0.0.
  3// Generated 2024-12-19 20:52 at commit cd01ff93f646632c.
  4// Register hash 805a50de03b71b95d3523176ad9155b5fe7be903.
  5
  6#pragma once
  7
  8#include <sstream>
  9#include <cstdint>
 10#include <cstdlib>
 11
 12namespace fpga_regs
 13{
 14
 15  // Attributes for the 'burst_length_bytes' field in the 'config' register.
 16  namespace caesar::config::burst_length_bytes
 17  {
 18    static const auto width = 9;
 19    static const auto default_value = 64;
 20  }
 21  // Attributes for the 'increment' field in the 'config' register.
 22  namespace caesar::config::increment
 23  {
 24    static const auto width = 3;
 25    static const auto default_value = 0;
 26  }
 27  // Attributes for the 'retry_count' field in the 'config' register.
 28  namespace caesar::config::retry_count
 29  {
 30    static const auto width = 3;
 31    static const auto default_value = 0;
 32  }
 33
 34  class ICaesar
 35  {
 36  public:
 37    // Number of registers within this register map.
 38    static const size_t num_registers = 1uL;
 39
 40    virtual ~ICaesar() {}
 41
 42    // -------------------------------------------------------------------------
 43    // Methods for the 'config' register. Mode 'Read, Write'.
 44
 45    // Getter that will read the whole register's value over the register bus.
 46    virtual uint32_t get_config() const = 0;
 47
 48    // Setter that will write the whole register's value over the register bus.
 49    virtual void set_config(
 50      uint32_t register_value
 51    ) const = 0;
 52
 53    // Getter for the 'burst_length_bytes' field in the 'config' register,
 54    // which will read register value over the register bus.
 55    virtual uint32_t get_config_burst_length_bytes() const = 0;
 56    // Getter for the 'burst_length_bytes' field in the 'config' register,
 57    // given a register value.
 58    virtual uint32_t get_config_burst_length_bytes_from_value(
 59      uint32_t register_value
 60    ) const = 0;
 61    // Setter for the 'burst_length_bytes' field in the 'config' register,
 62    // which will read-modify-write over the register bus.
 63    virtual void set_config_burst_length_bytes(
 64      uint32_t field_value
 65    ) const = 0;
 66    // Setter for the 'burst_length_bytes' field in the 'config' register,
 67    // given a register value, which will return an updated value.
 68    virtual uint32_t set_config_burst_length_bytes_from_value(
 69      uint32_t register_value,
 70      uint32_t field_value
 71    ) const = 0;
 72
 73    // Getter for the 'increment' field in the 'config' register,
 74    // which will read register value over the register bus.
 75    virtual int32_t get_config_increment() const = 0;
 76    // Getter for the 'increment' field in the 'config' register,
 77    // given a register value.
 78    virtual int32_t get_config_increment_from_value(
 79      uint32_t register_value
 80    ) const = 0;
 81    // Setter for the 'increment' field in the 'config' register,
 82    // which will read-modify-write over the register bus.
 83    virtual void set_config_increment(
 84      int32_t field_value
 85    ) const = 0;
 86    // Setter for the 'increment' field in the 'config' register,
 87    // given a register value, which will return an updated value.
 88    virtual uint32_t set_config_increment_from_value(
 89      uint32_t register_value,
 90      int32_t field_value
 91    ) const = 0;
 92
 93    // Getter for the 'retry_count' field in the 'config' register,
 94    // which will read register value over the register bus.
 95    virtual uint32_t get_config_retry_count() const = 0;
 96    // Getter for the 'retry_count' field in the 'config' register,
 97    // given a register value.
 98    virtual uint32_t get_config_retry_count_from_value(
 99      uint32_t register_value
100    ) const = 0;
101    // Setter for the 'retry_count' field in the 'config' register,
102    // which will read-modify-write over the register bus.
103    virtual void set_config_retry_count(
104      uint32_t field_value
105    ) const = 0;
106    // Setter for the 'retry_count' field in the 'config' register,
107    // given a register value, which will return an updated value.
108    virtual uint32_t set_config_retry_count_from_value(
109      uint32_t register_value,
110      uint32_t field_value
111    ) const = 0;
112
113  };
114
115} /* 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// This file is automatically generated by hdl-registers version 6.2.1-dev.
  2// Code generator CppImplementationGenerator version 2.0.0.
  3// Generated 2024-12-19 20:52 at commit cd01ff93f646632c.
  4// Register hash 805a50de03b71b95d3523176ad9155b5fe7be903.
  5
  6#include "include/caesar.h"
  7
  8namespace fpga_regs
  9{
 10
 11#ifdef NO_REGISTER_SETTER_ASSERT
 12
 13#define _SETTER_ASSERT_TRUE(expression, message) ((void)0)
 14
 15#else // Not NO_REGISTER_SETTER_ASSERT.
 16
 17// This macro is called by the register code to check for runtime errors.
 18#define _SETTER_ASSERT_TRUE(expression, message)                                 \
 19  {                                                                              \
 20    if (!static_cast<bool>(expression)) {                                        \
 21      std::ostringstream diagnostics;                                            \
 22      diagnostics << "Tried to set value out of range in " << __FILE__ << ":"    \
 23                  << __LINE__ << ", message: " << message << ".";                \
 24      std::string diagnostic_message = diagnostics.str();                        \
 25      _assert_failed(&diagnostic_message);                                       \
 26    }                                                                            \
 27  }
 28
 29#endif // NO_REGISTER_SETTER_ASSERT.
 30
 31#ifdef NO_REGISTER_GETTER_ASSERT
 32
 33#define _GETTER_ASSERT_TRUE(expression, message) ((void)0)
 34
 35#else // Not NO_REGISTER_GETTER_ASSERT.
 36
 37// This macro is called by the register code to check for runtime errors.
 38#define _GETTER_ASSERT_TRUE(expression, message)                                 \
 39  {                                                                              \
 40    if (!static_cast<bool>(expression)) {                                        \
 41      std::ostringstream diagnostics;                                            \
 42      diagnostics << "Got read value out of range in " << __FILE__ << ":"        \
 43                  << __LINE__ << ", message: " << message << ".";                \
 44      std::string diagnostic_message = diagnostics.str();                        \
 45      _assert_failed(&diagnostic_message);                                       \
 46    }                                                                            \
 47  }
 48
 49#endif // NO_REGISTER_GETTER_ASSERT.
 50
 51#ifdef NO_REGISTER_ARRAY_INDEX_ASSERT
 52
 53#define _ARRAY_INDEX_ASSERT_TRUE(expression, message) ((void)0)
 54
 55#else // Not NO_REGISTER_ARRAY_INDEX_ASSERT.
 56
 57// This macro is called by the register code to check for runtime errors.
 58#define _ARRAY_INDEX_ASSERT_TRUE(expression, message)                            \
 59  {                                                                              \
 60    if (!static_cast<bool>(expression)) {                                        \
 61      std::ostringstream diagnostics;                                            \
 62      diagnostics << "Provided array index out of range in " << __FILE__ << ":"  \
 63                  << __LINE__ << ", message: " << message << ".";                \
 64      std::string diagnostic_message = diagnostics.str();                        \
 65      _assert_failed(&diagnostic_message);                                       \
 66    }                                                                            \
 67  }
 68
 69#endif // NO_REGISTER_ARRAY_INDEX_ASSERT.
 70
 71  Caesar::Caesar(volatile uint8_t *base_address, bool (*assertion_handler) (const std::string*))
 72      : m_registers(reinterpret_cast<volatile uint32_t *>(base_address)),
 73        m_assertion_handler(assertion_handler)
 74  {
 75    // Empty
 76  }
 77
 78  void Caesar::_assert_failed(const std::string *message) const
 79  {
 80    m_assertion_handler(message);
 81  }
 82
 83  // ---------------------------------------------------------------------------
 84  // Methods for the 'config' register.
 85  // See interface header for documentation.
 86
 87  uint32_t Caesar::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 Caesar::get_config_burst_length_bytes() const
 96  {
 97    const uint32_t register_value = get_config();
 98    const uint32_t field_value = get_config_burst_length_bytes_from_value(register_value);
 99
100    return field_value;
101  }
102
103  uint32_t Caesar::get_config_burst_length_bytes_from_value(
104    uint32_t register_value
105  ) const
106  {
107    const uint32_t shift = 0uL;
108    const uint32_t mask_at_base = 0b111111111uL;
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    // Check that field value is within the legal range.
120    _GETTER_ASSERT_TRUE(
121      field_value >= 1,
122      "'burst_length_bytes' value too small, got '" << field_value << "'"
123    );
124    _GETTER_ASSERT_TRUE(
125      field_value <= 256,
126      "'burst_length_bytes' value too large, got '" << field_value << "'"
127    );
128
129    return field_value;
130  }
131
132  int32_t Caesar::get_config_increment() const
133  {
134    const uint32_t register_value = get_config();
135    const int32_t field_value = get_config_increment_from_value(register_value);
136
137    return field_value;
138  }
139
140  int32_t Caesar::get_config_increment_from_value(
141    uint32_t register_value
142  ) const
143  {
144    const uint32_t shift = 9uL;
145    const uint32_t mask_at_base = 0b111uL;
146    const uint32_t mask_shifted = mask_at_base << shift;
147
148    const uint32_t result_masked = register_value & mask_shifted;
149    const uint32_t result_shifted = result_masked >> shift;
150
151    int32_t field_value;
152
153    const int32_t sign_bit_mask = 1 << 2;
154
155    if (result_shifted & sign_bit_mask)
156    {
157      // Value is to be interpreted as negative.
158      // Sign extend it from the width of the field to the width of the return type.
159      field_value = result_shifted - 2 * sign_bit_mask;
160    }
161    else
162    {
163      // Value is positive.
164      field_value = result_shifted;
165    }
166
167    // Check that field value is within the legal range.
168    _GETTER_ASSERT_TRUE(
169      field_value >= -4,
170      "'increment' value too small, got '" << field_value << "'"
171    );
172    _GETTER_ASSERT_TRUE(
173      field_value <= 3,
174      "'increment' value too large, got '" << field_value << "'"
175    );
176
177    return field_value;
178  }
179
180  uint32_t Caesar::get_config_retry_count() const
181  {
182    const uint32_t register_value = get_config();
183    const uint32_t field_value = get_config_retry_count_from_value(register_value);
184
185    return field_value;
186  }
187
188  uint32_t Caesar::get_config_retry_count_from_value(
189    uint32_t register_value
190  ) const
191  {
192    const uint32_t shift = 12uL;
193    const uint32_t mask_at_base = 0b111uL;
194    const uint32_t mask_shifted = mask_at_base << shift;
195
196    const uint32_t result_masked = register_value & mask_shifted;
197    const uint32_t result_shifted = result_masked >> shift;
198
199    uint32_t field_value;
200
201    // No casting needed.
202    field_value = result_shifted;
203
204    // Check that field value is within the legal range.
205    _GETTER_ASSERT_TRUE(
206      field_value >= 0,
207      "'retry_count' value too small, got '" << field_value << "'"
208    );
209    _GETTER_ASSERT_TRUE(
210      field_value <= 5,
211      "'retry_count' value too large, got '" << field_value << "'"
212    );
213
214    return field_value;
215  }
216
217  void Caesar::set_config(
218    uint32_t register_value
219  ) const
220  {
221    const size_t index = 0;
222    m_registers[index] = register_value;
223  }
224
225  void Caesar::set_config_burst_length_bytes(
226    uint32_t field_value
227  ) const
228  {
229    // Get the current value of other fields by reading register on the bus.
230    const uint32_t current_register_value = get_config();
231    const uint32_t result_register_value = set_config_burst_length_bytes_from_value(current_register_value, field_value);
232    set_config(result_register_value);
233  }
234
235  uint32_t Caesar::set_config_burst_length_bytes_from_value(
236    uint32_t register_value,
237    uint32_t field_value
238  ) const  {
239    const uint32_t shift = 0uL;
240    const uint32_t mask_at_base = 0b111111111uL;
241    const uint32_t mask_shifted = mask_at_base << shift;
242
243    // Check that field value is within the legal range.
244    _SETTER_ASSERT_TRUE(
245      field_value >= 1,
246      "'burst_length_bytes' value too small, got '" << field_value << "'"
247    );
248    _SETTER_ASSERT_TRUE(
249      field_value <= 256,
250      "'burst_length_bytes' value too large, got '" << field_value << "'"
251    );
252
253    const uint32_t field_value_masked = field_value & mask_at_base;
254    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
255
256    const uint32_t mask_shifted_inverse = ~mask_shifted;
257    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
258
259    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
260
261    return result_register_value;
262  }
263
264  void Caesar::set_config_increment(
265    int32_t field_value
266  ) const
267  {
268    // Get the current value of other fields by reading register on the bus.
269    const uint32_t current_register_value = get_config();
270    const uint32_t result_register_value = set_config_increment_from_value(current_register_value, field_value);
271    set_config(result_register_value);
272  }
273
274  uint32_t Caesar::set_config_increment_from_value(
275    uint32_t register_value,
276    int32_t field_value
277  ) const  {
278    const uint32_t shift = 9uL;
279    const uint32_t mask_at_base = 0b111uL;
280    const uint32_t mask_shifted = mask_at_base << shift;
281
282    // Check that field value is within the legal range.
283    _SETTER_ASSERT_TRUE(
284      field_value >= -4,
285      "'increment' value too small, got '" << field_value << "'"
286    );
287    _SETTER_ASSERT_TRUE(
288      field_value <= 3,
289      "'increment' value too large, got '" << field_value << "'"
290    );
291
292    const uint32_t field_value_masked = field_value & mask_at_base;
293    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
294
295    const uint32_t mask_shifted_inverse = ~mask_shifted;
296    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
297
298    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
299
300    return result_register_value;
301  }
302
303  void Caesar::set_config_retry_count(
304    uint32_t field_value
305  ) const
306  {
307    // Get the current value of other fields by reading register on the bus.
308    const uint32_t current_register_value = get_config();
309    const uint32_t result_register_value = set_config_retry_count_from_value(current_register_value, field_value);
310    set_config(result_register_value);
311  }
312
313  uint32_t Caesar::set_config_retry_count_from_value(
314    uint32_t register_value,
315    uint32_t field_value
316  ) const  {
317    const uint32_t shift = 12uL;
318    const uint32_t mask_at_base = 0b111uL;
319    const uint32_t mask_shifted = mask_at_base << shift;
320
321    // Check that field value is within the legal range.
322    _SETTER_ASSERT_TRUE(
323      field_value >= 0,
324      "'retry_count' value too small, got '" << field_value << "'"
325    );
326    _SETTER_ASSERT_TRUE(
327      field_value <= 5,
328      "'retry_count' value too large, got '" << field_value << "'"
329    );
330
331    const uint32_t field_value_masked = field_value & mask_at_base;
332    const uint32_t field_value_masked_and_shifted = field_value_masked << shift;
333
334    const uint32_t mask_shifted_inverse = ~mask_shifted;
335    const uint32_t register_value_masked = register_value & mask_shifted_inverse;
336
337    const uint32_t result_register_value = register_value_masked | field_value_masked_and_shifted;
338
339    return result_register_value;
340  }
341
342} /* 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// This file is automatically generated by hdl-registers version 6.2.1-dev.
 2// Code generator CHeaderGenerator version 1.0.0.
 3// Generated 2024-12-19 20:52 at commit cd01ff93f646632c.
 4// Register hash 805a50de03b71b95d3523176ad9155b5fe7be903.
 5
 6#ifndef CAESAR_REGS_H
 7#define CAESAR_REGS_H
 8
 9
10// Number of registers within this register map.
11#define CAESAR_NUM_REGS (1u)
12
13// Type for this register map.
14typedef struct caesar_regs_t
15{
16  // Mode "Read, Write".
17  uint32_t config;
18} caesar_regs_t;
19
20// Address of the 'config' register.
21// Mode 'Read, Write'.
22#define CAESAR_CONFIG_INDEX (0u)
23#define CAESAR_CONFIG_ADDR (4u * CAESAR_CONFIG_INDEX)
24// Attributes for the 'burst_length_bytes' field in the 'config' register.
25#define CAESAR_CONFIG_BURST_LENGTH_BYTES_SHIFT (0u)
26#define CAESAR_CONFIG_BURST_LENGTH_BYTES_MASK (0b111111111u << 0u)
27#define CAESAR_CONFIG_BURST_LENGTH_BYTES_MASK_INVERSE (~CAESAR_CONFIG_BURST_LENGTH_BYTES_MASK)
28// Attributes for the 'increment' field in the 'config' register.
29#define CAESAR_CONFIG_INCREMENT_SHIFT (9u)
30#define CAESAR_CONFIG_INCREMENT_MASK (0b111u << 9u)
31#define CAESAR_CONFIG_INCREMENT_MASK_INVERSE (~CAESAR_CONFIG_INCREMENT_MASK)
32// Attributes for the 'retry_count' field in the 'config' register.
33#define CAESAR_CONFIG_RETRY_COUNT_SHIFT (12u)
34#define CAESAR_CONFIG_RETRY_COUNT_MASK (0b111u << 12u)
35#define CAESAR_CONFIG_RETRY_COUNT_MASK_INVERSE (~CAESAR_CONFIG_RETRY_COUNT_MASK)
36
37#endif // CAESAR_REGS_H