%% Collect Code Coverage Metrics with a Third-Party Tool
% If Simulink(R) Coverage(TM) is installed, you can collect
% code coverage metrics during a software-in-the-loop (SIL) or
% processor-in-the-loop (PIL) simulation - see 
% <docid:slcoverage_doccenter#mw_7358873a-57d9-4564-8aea-0db9e917296f>. In this
% example, you can collect code coverage metrics with a third-party tool, i.e.,
% BullseyeCoverage or LDRA Testbed. You must install the third-party tool 
% - see <docid:ecoder_ug.bsp4b6m>.
%
% This screen shot shows a code coverage report obtained
% by running a SIL simulation with a code coverage tool enabled.
% The annotations depend on the code coverage tool that you specify.
%
% <<../codecov_sample_html.jpg>>
%
% In this example, you measure model coverage during normal mode simulation
% and repeat the same simulation in SIL mode to measure code coverage.
% By using the hyperlinks in the model coverage and code
% coverage reports, you can compare model coverage and code coverage results.
%
% For SIL and PIL simulation examples, see <docid:ecoder_ug.mw_caa570c4-efd7-4c14-925d-c422da369a46>.

% Copyright 2010-2013 The MathWorks, Inc.

%% Initial Setup 
% Make sure the model is newly opened.
model='rtwdemo_sil_topmodel';
close_system(model,0)
open_system(model)

%%
% Remove existing build folders.
buildFolder=RTW.getBuildDir(model);
if exist(buildFolder.BuildDirectory,'dir')
    rmdir(buildFolder.BuildDirectory,'s');
end

%%
% Configure generation of model coverage report.
set_param(model, 'RecordCoverage','on')
clear covCumulativeData

%%
% Set up the stimulus data.
T=0.1; % sample time
[ticks_to_count, reset, counter_mode, count_enable, ...
 counter_mode_values_run1, counter_mode_values_run2, ...
 count_enable_values_run1, count_enable_values_run2] = ...
    rtwdemo_sil_topmodel_data(T);

%% Run a Simulation in Normal Mode
% The model is configured to collect model coverage metrics. When a
% simulation is complete, the model coverage report opens. Use the coverage 
% display window to navigate from blocks in the model to the corresponding 
% sections of the coverage report.
counter_mode.signals.values = counter_mode_values_run1;
count_enable.signals.values = count_enable_values_run1;
set_param(model,'SimulationMode','normal');

%%
% Set up Simulation Data Inspector for interactive viewing and comparison of
% simulation results.
Simulink.sdi.view;
Simulink.sdi.clear;

%%
% Run the simulation.
simout_normal_run1 = sim(model, 'ReturnWorkspaceOutputs', 'on');

%%
% Capture the results.
Simulink.sdi.createRun('Run 1 (normal mode)', 'namevalue',...
                       {'simout_normal_run1'}, {simout_normal_run1});

%% Run a Second Simulation in Normal Mode
% For the first simulation, the report shows that the achieved coverage is
% less than 100%. Run a second simulation with different input signals that
% increase the level of MC/DC coverage to 100%. Note that the model
% coverage report is configured to show cumulative coverage across both
% simulation runs.
counter_mode.signals.values = counter_mode_values_run2;
count_enable.signals.values = count_enable_values_run2;
set_param(model,'SimulationMode','normal');

simout_normal_run2 = sim(model, 'ReturnWorkspaceOutputs', 'on');

Simulink.sdi.createRun('Run 2 (normal mode)', 'namevalue',...
                       {'simout_normal_run2'}, {simout_normal_run2});

%% Configure the Model to Measure Code Coverage
% Before running a SIL simulation, check the availability of third-party
% tools, and configure the model to collect code coverage metrics.
% If a third-party tool is not available, the model uses Simulink(R) 
% Verification and Validation(TM).
covToolPath = '';
ldraPath = coder.coverage.LDRA.getPath;
bullseyePath = coder.coverage.BullseyeCoverage.getPath;

coverageSettings = get_param(model,'CodeCoverageSettings');
coverageSettings.TopModelCoverage='on';
if ~isempty(ldraPath)
    coverageSettings.CoverageTool='LDRA Testbed';
elseif ~isempty(bullseyePath)
    coverageSettings.CoverageTool='BullseyeCoverage';
else
    coverageSettings.CoverageTool='None';
end    
set_param(model,'CodeCoverageSettings',coverageSettings);

%% Run Simulations in SIL Mode
% The normal mode simulations produce coverage metrics for the model.
% With a SIL simulation, you can apply the same input stimulus signals 
% to the generated code and measure code coverage.
%
% Run the first simulation in SIL mode.
counter_mode.signals.values = counter_mode_values_run1;
count_enable.signals.values = count_enable_values_run1;
set_param(model,'SimulationMode','software-in-the-loop');
set_param(model,'CodeExecutionProfiling','off');
set_param(model,'CodeProfilingInstrumentation','off');
simout_sil_run1 = sim(model, 'ReturnWorkspaceOutputs', 'on');
Simulink.sdi.createRun('Run 1 (SIL mode)', 'namevalue',...
                       {'simout_sil_run1'}, {simout_sil_run1});

%%
% Run the second simulation in SIL mode.
counter_mode.signals.values = counter_mode_values_run2;
count_enable.signals.values = count_enable_values_run2;
set_param(model,'SimulationMode','software-in-the-loop');
set_param(model,'CodeExecutionProfiling','off');
set_param(model,'CodeProfilingInstrumentation','off');
simout_sil_run2 = sim(model, 'ReturnWorkspaceOutputs', 'on');
Simulink.sdi.createRun('Run 2 (SIL mode)', 'namevalue',...
                       {'simout_sil_run2'}, {simout_sil_run2});

%%
% When a simulation is complete, click the link in the Command Window
% to open the code coverage report and view the cumulative code coverage
% results. The link is available only if you have a third-party tool
% installed.
%
% Use hyperlinks in the code coverage report to go to corresponding
% locations in the block diagram. Then, by using the coverage
% display window, you can open corresponding sections of the model
% coverage report. Compare model coverage and code coverage results.
%
% The Simulation Data Inspector opens automatically, allowing interactive
% viewing and analysis of the results. Use the Compare and Inspect panes
% to confirm that the SIL and normal mode logged signals are identical for
% both runs.
%% Concluding Remarks
% In this example, you:
% 
% * Collected model coverage metrics during a normal mode simulation.
% * Collected code coverage metrics during a SIL simulation.
% * Navigated between the code coverage and model coverage reports.
% * Cross-checked metrics from both reports.
