Main Content

Map Persistent Variables and Persistent Arrays to RAM

R2026b

You can model registers by using persistent variables, and model RAM by using persistent array variables.

Map Persistent Variables to RAM

Use persistent variables to model registers and preserve state between invocations of your MATLAB® algorithm.

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 initialization test executes only once as part of the reset process.

You can also initialize multiple variables in a single logical expression. For example:

% Initialize with multiple 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 result in inefficient hardware.

Map Persistent Arrays to RAM for HLS Code Generation

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 reduces the area of your design on 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

  1. Open the HDL Workflow Advisor and set Code Generation Workflow to MATLAB to HLS.

  2. In the Define Input Types task, select the MATLAB Function and MATLAB Test Bench.

  3. In the Select Code Generation Target task, set Workflow to Generic ASIC/FPGA.

  4. In the HLS Code Generation task, 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 SettingMapping 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.

See Also

|

Topics