Main Content

Use Protected Models from Previous Releases to Perform SIL Testing and Generate Code

This example shows how you can use a protected model from a previous release (R2018b onwards) to:

  • Test numerical equivalence between the protected model and its previously generated code.

  • Generate new code in the current release.

The workflow in this example supports only protected models that specify these system target file types:

  • ERT

  • ERT-based

  • AUTOSAR Classic

  • GRT (Embedded Coder required)

  • GRT-based (Embedded Coder required)

Protected Model

Use the pregenerated protected model CrossReleaseProtectedCounter.slxp, which was created in R2023b.

Add the folder containing the protected model for your current platform to the MATLAB path.

addpath(fullfile(pwd, 'CrossReleaseProtectedCounter', computer('arch')));

The protected model allows the user to:

  • Open a read-only view of the model.

  • Run simulations.

  • Use generated code.

The protected model supports the ERT system target file, and the code that it contains is "Readable source code" and supports the "Model reference" code interface.

If you want to recreate the protected model with different settings, you can export the model CrossReleaseUnprotectedCounter from the current release into a format that can be used in an earlier release of MATLAB. For example, to export to R2023b, in the current release, run these commands:

load_system('CrossReleaseUnprotectedCounter');
Simulink.exportToVersion('CrossReleaseUnprotectedCounter', 'CrossReleaseProtectedCounter.slx', 'R2023b');
close_system('CrossReleaseUnprotectedCounter', 0);

The protected model was created in MATLAB R2023b by running this command:

Simulink.ModelReference.protect('CrossReleaseProtectedCounter', ...
    'Mode', 'CodeGeneration', ...
    'OutputFormat', 'MinimalCode', ...
    'ObfuscateCode', false, ...
    'WebView', true, ...
    'Report', true);

To make sure you are using the protected model, you can delete CrossReleaseProtectedCounter.slx.

delete('CrossReleaseProtectedCounter.slx');

Open Top Model

Open CrossReleaseProtectedModel, the top model that references the protected model.

model = 'CrossReleaseProtectedModel';
open_system(model);

CrossReleaseProtectedModelTop.png

The Windows version of the example protected model was created with the 'MinGW64 | gmake (64-bit Windows)' toolchain. The top model needs to be consistent with this. On other platforms, the platform default toolchain is selected.

if ispc   
    set_param(model, 'Toolchain', 'MinGW64 | gmake (64-bit Windows)');
end

Alternatively, if you created your own protected model in an earlier release using the commands in the previous section, set the Toolchain parameter to match the toolchain you used when creating the protected model.

Open Protected Model Report

To open the protected model report, on one of the Model blocks, right- click the protected-model badge icon and then click "Display report". Or, open the report from the command line.

protectedModel = 'CrossReleaseProtectedCounter';
Simulink.ModelReference.ProtectedModel.displayReport(protectedModel);

CrossReleaseProtectedModelReport.png

The report shows that the protected model was generated by using Simulink Coder 23.2 (R2023b).

Open Read-Only View

To open a read-only view of the protected model, right-click the protected-model badge icon and then click "Show web view". Or, open the read-only view from the command line.

Simulink.ModelReference.ProtectedModel.displayWebview(protectedModel);

CrossReleaseProtectedModelWebView.png

Merge Shared Utility Code

When you integrate code from multiple releases, you can specify the shared utility code that the final build uses.

Specify the shared code folder for the top model.

mkdir('sharedCode');
set_param(model, 'ExistingSharedCode', fullfile(pwd, 'sharedCode'));

Use sharedCodeUpdate to merge protected model shared utility files with the shared code folder content.

Simulink.ModelReference.ProtectedModel.CurrentTarget.clear(protectedModel);
sharedCodeUpdate('CrossReleaseProtectedCounter.slxp', 'sharedCode', 'Interactive', false);
The following files will be copied from CrossReleaseProtectedCounter.slxp:slprj/ert/_sharedutils to sharedCode/R2023b:

    rtwtypes.h

Files copied from CrossReleaseProtectedCounter.slxp:slprj/ert/_sharedutils to sharedCode/R2023b.

Test the Code in the Protected model

The top model references two instances of the protected model. The Simulation mode Model block parameter for "CounterA" and "CounterB" are Software-in-the-loop (SIL) and Accelerator respectively. When you run a top-model simulation, "CounterA" runs generated code from the protected model while "CounterB" runs the protected model.

simOut = sim(model);
### Searching for referenced models in model 'CrossReleaseProtectedModel'.
### Found 0 model reference targets to update.
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Starting SIL simulation for component: CrossReleaseProtectedCounter
### Application stopped
### Stopping SIL simulation for component: CrossReleaseProtectedCounter
Simulink.sdi.view;

Compare Outputs from Both Model Blocks

To view the simulation output and compare the values of the signals from the Model blocks, use the Simulation Data Inspector.

simulationRunObj =  Simulink.sdi.getCurrentSimulationRun(model);
counterASignal = getSignalsByName(simulationRunObj, 'counterA');
counterBSignal = getSignalsByName(simulationRunObj, 'counterB');
Simulink.sdi.compareSignals(counterASignal.ID, counterBSignal.ID);
Simulink.sdi.view(Simulink.sdi.GUITabType.CompareRuns);

CrossReleaseProtectedModelSignalComparison.png

Generate Code from Top Model

You can generate code from the top model, referencing the code that is contained in the protected model.

slbuild(model);
### Searching for referenced models in model 'CrossReleaseProtectedModel'.
### Found 0 model reference targets to update.
### Starting build procedure for: CrossReleaseProtectedModel
### Successful completion of build procedure for: CrossReleaseProtectedModel

Build Summary

Top model targets:

Model                       Build Reason                                         Status                        Build Duration
=============================================================================================================================
CrossReleaseProtectedModel  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 17.99s  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 25.378s

See Also

Related Topics