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.
1[config]
2
3mode = "r_w"
4description = "Configuration register."
5
6# This will allocate a bit vector field named "tuser" in the "config" register.
7[config.tuser]
8
9# The "type" property MUST be present and set to "bit_vector".
10type = "bit_vector"
11
12# The "width" property MUST be present for a bit vector field.
13# The value specified MUST be a positive integer.
14width = 4
15
16# The "description" property is OPTIONAL for a bit vector field.
17# Will default to "" if not specified.
18# The value specified MUST be a string.
19description = "Value to set for **TUSER** in the data stream."
20
21# The "default_value" property is OPTIONAL for a bit vector field.
22# Will default to all zeros if not specified.
23# The value specified MUST be a string whose length is the same as the
24# specified 'width' property value.
25# The value specified MUST contain only ones and zeros.
26default_value = "0101"
27
28
29[config.tid]
30
31type = "bit_vector"
32width = 8
33description = "Value to set for **TID** in the data stream."
Note that the second 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
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_vector.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_vector(
37 name="tuser",
38 description="Value to set for **TUSER** in the data stream.",
39 width=4,
40 default_value="0101",
41 )
42
43 register.append_bit_vector(
44 name="tid",
45 description="Value to set for **TID** in the data stream.",
46 width=8,
47 default_value="00000000",
48 )
49
50 return register_list
51
52
53def generate(register_list: RegisterList, output_folder: Path):
54 """
55 Generate the artifacts that we are interested in.
56 """
57 CHeaderGenerator(register_list=register_list, output_folder=output_folder).create()
58
59 CppImplementationGenerator(register_list=register_list, output_folder=output_folder).create()
60 CppInterfaceGenerator(register_list=register_list, output_folder=output_folder).create()
61
62 HtmlPageGenerator(register_list=register_list, output_folder=output_folder).create()
63
64 VhdlRegisterPackageGenerator(register_list=register_list, output_folder=output_folder).create()
65 VhdlRecordPackageGenerator(register_list=register_list, output_folder=output_folder).create()
66
67
68def main(output_folder: Path):
69 generate(register_list=parse_toml(), output_folder=output_folder / "toml")
70 generate(register_list=create_from_api(), output_folder=output_folder / "api")
71
72
73if __name__ == "__main__":
74 main(output_folder=Path(sys.argv[1]))
See Register.append_bit_vector()
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 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.
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.
For each bit vector field there is a named integer subtype that defines the fields’s bit range within the register.
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.
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;
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
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.
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.