Map Persistent Variables and Persistent Arrays to RAM
You can model registers by using persistent variables and model RAM by using persistent array variables.
Map Persistent Variables to RAM
You can use persistent variables to model registers. For example, to preserve the state between invocations of your MATLAB® algorithm, use persistent variables.
Before you use a persistent variable, you must initialize it with a statement that specifies its size and type. You can initialize a persistent variable with either a constant value or a variable. For example:
% Initialize with a constant persistent p; if isempty(p) p = fi(0,0,8,0); end
% Initialize with a variable initval = fi(0,0,8,0); persistent p; if isempty(p) p = initval; end
Use a logical expression that evaluates to a constant to test whether a persistent variable has been initialized. Using logical expressions ensures that the generated HDL and high-level synthesis (HLS) code for the test execute only once as part of the reset process.
You can also initialize multiple variables in a single logical expression. For example:
% Initialize with variables initval1 = fi(0,0,8,0); initval2 = fi(0,0,7,0); persistent p; if isempty(p) x = initval1; y = initval2; end
Note
If you do not initialize persistent variables, extra sentinel variables can appear in the generated code. These sentinel variables can translate to inefficient hardware.
Map Persistent Arrays to RAM
To map persistent MATLAB array variables to RAM in the generated HLS code, use the RAM mapping optimization. Without this optimization, the code generator maps the array variables to registers. RAM mapping is an area optimization that reduces the area of your design in the target hardware.
To learn how persistent array variables map to RAM for HDL code generation, see Map Persistent Arrays and dsp.Delay Objects to RAM.
Enable RAM Mapping
In the HDL Workflow Advisor, select the HLS Code Generation task and then select the Optimizations tab.
Select Map persistent array variables to RAMs.
Set RAM mapping threshold to the size, in bits, of the smallest persistent array that you want to map to RAM.
This table shows the RAM mapping behavior for persistent arrays when you use the Map persistent array variables to RAMs parameter.
Parameter Setting | Mapping Behavior |
---|---|
on | Map to RAM in the generated HLS code. |
off | Map to registers in the generated HLS code. |
Limitations and Considerations
Large persistent arrays that have a size in bits greater than or equal to RAM mapping threshold map to RAM. The equation
NumElements
*WordLength
determines the size in bits, where:NumElements
is the number of elements in the array.WordLength
is the number of bits that represent the data type of the array.
The
ml.tcl
metadata file generated during HLS code generation contains the list of RAM variables. The Stratus HLS tool reads the metadata file during project creation and maps these variables to RAM.If you clear Initialize Block RAM, RAM variables that have an initial value of zero are not initialized inside the
initialize_ram_vars()
method.If the initial value of any RAM variables is nonzero, then those RAM variables are initialized inside the
initialize_ram_vars()
method, irrespective of the Initialize Block RAM value.When you set the synthesis tool to
Xilinx Vitis HLS
, the code generator ignores the values of Map persistent array variables to RAMs and any related settings. During synthesis, the tool automatically selects appropriate memory types for arrays in the generated HLS code.