RAM Mapping with the MATLAB Function Block

This example shows how to map persistent arrays to RAM using the MapPersistentVarsToRAM block-level parameter. The RAM size must be greater than or equal to the RAMMappingThreshold. The resource report shows the difference in area improvements resulting from RAM Mapping.

  1. In the Simulink® editor, create a model, and open the Simulink Library Browser.

  2. Add an Inport block, a MATLAB Function block, and an Outport block to your model, and name them as shown in figure.

  3. Double-click the Line Buffer MATLAB Function block. In the MATLAB® editor, copy this MATLAB code for a line_buffer function.

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Line buffer: Uses a presistent array to store the image
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function y = line_buffer(u)
    persistent u_d ctr;
    if isempty(u_d)
        u_d = uint8(zeros(1,80)); % You can map this to RAM
        ctr = uint8(1);
    end
    y = u_d(ctr);
    u_d(ctr) = u;
    if ctr == uint8(80)
        ctr = uint8(1);
    else
        ctr = ctr + 1;
    end
    end

  4. To use this model as your design under test (DUT) for generating HDL code, select all blocks and lines, and create a subsystem. Save your model as RAM_Mapping_Using_MATLAB_Function. By default, RAM mapping is disabled, as MapPersistentVarsToRAM is set to off.

  5. In the Simulation > Model Configuration Parameters > HDL Code Generation pane, enable Generate resource utilization report and click Apply.

  6. Click Generate to generate HDL code.

  7. In the Code Generation Report, select High-level Resource Report.

    The design uses 81 registers, 648 1–bit Registers, and no RAM.

  8. To enable RAM mapping, right-click the Line Buffer block, select HDL Code > HDL Block Properties, and set MapPersistentVarsToRAM to on. Click OK.

  9. In the Simulation > Model Configuration Parameters > HDL Code Generation pane, click Generate to generate HDL code.

  10. In the Code Generation Report, select High-level Resource Report.

    The design now uses one register, eight 1–bit registers, and one RAM.

To learn about design patterns that enable efficient RAM mapping of persistent arrays in MATLAB Function blocks, see the eml_hdl_design_patterns/RAMs library.

See Also

More About

Was this topic helpful?