Persistent variables enable you to model registers. If you need to preserve state between invocations of your MATLAB® algorithm, use persistent variables.
Before you use a persistent variable, you must initialize it with a statement specifying its size and type. You can initialize a persistent variable with either a constant value or a variable, as in the following examples:
% 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, as in the preceding examples. Using a logical expression that evaluates to a constant ensures that the generated HDL code for the test is executed only once, as part of the reset process.
You can initialize multiple variables within a single logical expression, as in the following 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
If persistent variables are not initialized as described above, extra sentinel variables can appear in the generated code. These sentinel variables can translate to inefficient hardware.
Persistent array variables enable you to model RAM.
By default, the HDL Coder™ software optimizes the area of your design by mapping persistent array variables to RAM. If persistent array variables are not mapped to RAM, they map to registers. RAM mapping can therefore reduce the area of your design in the target hardware.
To learn how persistent array variables map to RAM, see Map Persistent Arrays and dsp.Delay to RAM.