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®-A processors use ARM NEON SIMD instructions, which are tailored for multimedia applications. Application-specific libraries provided by ARM and compiler-specific properties take advantage of these instructions. The reference target forARM Cortex-A 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, seeDevelop a Code Replacement Library.

Example Code After Code Replacement

Consider a simple Simulink®model that adds two4x4matrices. The model diagram is shown in the following figure.

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 < 16; i++) { rtb_Add[i] = test_P.MatrixA_Value[i] + test_P.MatrixB_Value[i]; }
mw_neon_mm_add_f32x4( test_P.MatrixA_Value, 4, 4, test_P.MatrixB_Value, rtb_Add);

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

  1. In MATLAB®, on theHometab, selectNew>Simulink Model. Next, selectBlank Model. Then, selectSave>Save as...and save your model astest.

  2. SelectSimulation>Model Configuration Parameters.

  3. In the Configuration Parameters dialog box, selectSolver.

  4. From theTypelist, selectFixed-step. From theSolverlist, selectauto.

  5. In the Configuration Parameters dialog box, select theHardware Implementationtab.

  6. SetHardware board例如,硬件你注册'My ARM Cortex A Board'.

  7. In theModel Configuration Parametersdialog box, selectCode Generationtab.

  8. In theBuild processgroup, checkGenerate codeonly.

  9. SelectCode Generation > Reportand checkCreate code generation report

  10. SelectSimulation > Hardware Implementation, setDevice vendortoARM Compatibleand setDevice typetoARM Cortex.

  11. SelectAll Parameters, and then selectOptimization类别。清晰的Block reduction. ClickOK.

  12. SelectLibrary Browser.

  13. In Simulink library, openSourcesand add theConstantblock to your model.

  14. Double-click theConstantblock and set theConstant valuetosingle(ones(4))

  15. Copy and paste theConstantblock to the same model.

  16. In Simulink library, openMath Operationsand add theAddblock to your model.

  17. 连接theConstantandConstant1blocks to theAddblock.

  18. In Simulink library, openSinks, add theScopeblock to the model and connect it to theAddblock.

  19. ClickBuild Modelin your model.

  20. In Code Generation Report that opens, click thetest.cfile 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. SelectSimulation>Model Configuration Parameters.

  23. SelectCode Generation>Interfaceand set代码替换库toARM Cortex-A. ClickOK.

  24. ClickBuild Modelin your model.

  25. In Code Generation Report, click thetest.cfile and find code that implements the Add block.

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