%% 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)
%

% Copyright 2020 The MathWorks, Inc.


%% Protected Model
% Use the pregenerated protected model |rtwdemo_crossrelease_protected_counter.slxp|, 
% which was created in R2018b.
%
% 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 re-create the protected model with different settings, 
% you can export the model |rtwdemo_crossrelease_unprotected_counter| from 
% the current release into a format that can be used in MATLAB R2018b.  In
% the current release, run these commands:
%
% <include>ExportCrossReleaseExampleModelToPrevious.m</include>
%
% The protected model was created in MATLAB R2018b by running this command:
%
% <include>CreateProtectedModelInOldRelease.m</include>
%

%% Open Top Model
% Open |rtwdemo_crossrelease_protected_model|, the top model that
% references the protected model.
model = 'rtwdemo_crossrelease_protected_model';
open_system(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 = 'rtwdemo_crossrelease_protected_counter';
Simulink.ModelReference.ProtectedModel.displayReport(protectedModel);

%%
% <<../CrossReleaseProtectedModelReport.png>>
%
% The report shows that the protected model was generated by using Simulink
% Coder 9.0 (R2018b).

%% 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('rtwdemo_crossrelease_protected_counter.slxp', 'sharedCode', 'Interactive', false);


%% 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);
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.
%%
rtwbuild(model);
