Bit fields
Register fields can be of the type bit. Meaning a field of width one that can only take on the logic values of zero or one.
This page will show you how the set up bit 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 fields. See comments for rules about the different properties.
1[config]
2
3mode = "r_w"
4description = "Configuration register."
5
6# This will allocate a bit field named "enable" in the "config" register.
7[config.enable]
8
9# The "type" property MUST be present and set to "bit".
10type = "bit"
11
12# The "description" property is OPTIONAL for a bit field.
13# Will default to "" if not specified.
14# The value specified MUST be a string.
15description = "Enable data passthrough."
16
17# The "default_value" property is OPTIONAL for a bit field.
18# Will default to "0" if not specified.
19# The value specified MUST be a string, consisting of either a "1" or a "0".
20default_value = "1"
21
22
23[config.invert]
24
25type = "bit"
26description = "Optionally enable inversion of data."
Note that the second field does not have any default value specified, meaning it will default to 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
How to parse the TOML file listed above.
How to create an identical register list when instead using the Python API.
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.
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_bit.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_bit(
37 name="enable",
38 description="Enable data passthrough.",
39 default_value="1",
40 )
41
42 register.append_bit(
43 name="invert",
44 description="Optionally enable inversion of data.",
45 default_value="0",
46 )
47
48 return register_list
49
50
51def generate(register_list: RegisterList, output_folder: Path):
52 """
53 Generate the artifacts that we are interested in.
54 """
55 CHeaderGenerator(register_list=register_list, output_folder=output_folder).create()
56
57 CppImplementationGenerator(register_list=register_list, output_folder=output_folder).create()
58 CppInterfaceGenerator(register_list=register_list, output_folder=output_folder).create()
59
60 HtmlPageGenerator(register_list=register_list, output_folder=output_folder).create()
61
62 VhdlRegisterPackageGenerator(register_list=register_list, output_folder=output_folder).create()
63 VhdlRecordPackageGenerator(register_list=register_list, output_folder=output_folder).create()
64
65
66def main(output_folder: Path):
67 generate(register_list=parse_toml(), output_folder=output_folder / "toml")
68 generate(register_list=create_from_api(), output_folder=output_folder / "api")
69
70
71if __name__ == "__main__":
72 main(output_folder=Path(sys.argv[1]))
See Register.append_bit()
for more Python API details.
Generated code
See below for a description of the code that can be generated when using bit 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 field is documented with its bit index, default value and description.
See HTML code generator for more details about the HTML generator and its capabilities.
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:
There is only one register, at index 0.
For each bit field there is a named constant that defines the bit’s index within the register.
In VHDL, slicing out a bit from the register value will yield a value of type
std_ulogic
, meaning that typically no casting is needed. Hence there are no conversion functions for bit fields, the way there are for e.g. enumeration fields.
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 bits set up in
our example: enable
and invert
.
In our VHDL code we can access a field value for example like this:
result_valid <= input_valid and regs_down.config.enable;
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 the setters and getters for each individual field value.
C++ implementation
C header
The C code below is produced by the generate()
call in the Python example above.
The index and mask of each field are available as constants.