Main Content

Activate the Code Replacement Feature

MathWorks® code generation software generates ANSI®/ISO® C code. For the best execution performance, generated code must often be hardware-specific. The code replacement feature helps you enable your target to replace parts of generated code with hardware-specific code. In this section, you will implement the code replacement feature.

ARM® Cortex®-M processors have some specialized instruction sets, which are tailored for math and signal processing applications. Optimized libraries provided by ARM take advantage of these instructions. The reference target for ARM Cortex-M hardware, ships a code replacement library (CRL) that uses these instructions. Your target can reuse this library.

ARM Cortex-R processors have some specialized instruction sets, which are tailored for math and signal processing applications. Optimized libraries provided by ARM take advantage of these instructions. The reference target for ARM Cortex-R hardware, ships a code replacement library (CRL) that uses these instructions. Your target can reuse this library.

If you need to implement additional code replacement libraries for your reference target, see Develop a Code Replacement Library.

Example Code After Code Replacement

Consider a simple Simulink® model that adds two 9x9 matrices.

The model contains Constant blocks, an Add block, and a Scope block.

The following table shows the source code generated for the Add block in the model: ANSI/ISO C code, on the left, and the hardware-specific code using a code replacement library, on the right.

ANSI/ISO C Source Code

Hardware-Specific Code After Code Replacement

for (i = 0; i < 81; i++) {
           test_B.Add[i] =
                         test_P.Constant_Value[i] +
                         test_P.Constant1_Value[i];
}
mw_arm_mat_add_f32(
                           test_P.Constant_Value,
                           test_P.Constant1_Value,
                           test_B.Add, 9U, 9U);

Verify that the expected code replacements happen for your target with the following steps.

  1. In MATLAB®, on the Home tab, select New > Simulink Model. Next, select Blank Model. Then, select Save > Save as... and save your model as test.

  2. Select Simulation > Model Configuration Parameters.

  3. In the Configuration Parameters dialog box, select Solver.

  4. From the Type list, select Fixed-step. From the Solver list, select auto.

  5. In the Configuration Parameters dialog box, select the Hardware Implementation tab.

  6. Set Hardware board to the hardware you registered, for example, 'My ARM Cortex M Board'.

  7. In the Model Configuration Parameters dialog box, select Code Generation tab.

  8. In the Build process group, check Generate code only.

  9. Select Code Generation > Report and check Create code generation report

  10. Select Simulation > Hardware Implementation, set Device vendor to ARM Compatible and set Device type to ARM Cortex.

  11. Select All Parameters, and then select Optimization category. Clear Block reduction. Click OK.

  12. Select Library Browser.

  13. In Simulink library, open Sources and add the Constant block to your model.

  14. Double-click the Constant block and set the Constant value to single(rand(9,9))

  15. Copy and paste the Constant block to the same model.

  16. In Simulink library, open Math Operations and add the Add block to your model.

  17. Connect the Constant and Constant1 blocks to the Add block.

  18. In Simulink library, open Sinks, add the Scope block to the model and connect it to the Add block.

  19. Click Build Model in your model.

  20. In Code Generation Report that opens, click the test.c file and find the code that implements the Add block.

  21. Verify that matrix addition is implemented as a for loop adding matrix elements one-by-one.

  22. Select Simulation > Model Configuration Parameters.

  23. Select Code Generation > Interface and set Code replacement library to ARM Cortex-M (CMSIS). Click OK.

  24. Click Build Model in your model.

  25. In Code Generation Report, click the test.c file and find code that implements the Add block.

  26. Verify that matrix addition is implemented as a function call to mw_arm_mat_add_f32 function.