%% Improve Resource Sharing with Clone Detection and Replacement
% This example shows how you can automatically identify and replace
% recurring patterns by using the Clone Detector App. Consequently, it also
% improves opportunities for optimizing the model by using resource
% sharing. The Clone Detector App refactors models by identifying clones in
% a design and replacing clones with links to subsystem blocks in a
% library. This example uses a model that has already been modified for
% optimal resource sharing by using the Clone Detector App. To learn more
% about these modifications, see "Improve Resource Sharing with Design
% Modifications."

% Copyright 2019 The MathWorks, Inc.

%% Clone Detection
% Clone Detection is a tool that can be used to identify modeling patterns
% in a design that are similar to a few sample patterns provided as inputs
% through a custom library file. Once the Clone Detector App identifies
% these patterns, it replaces the patterns with atomic subsystems. This
% enables the generation of optimal code through code reuse and results in
% better resource utilization through sharing of resources among the atomic
% subsystems. For more information regarding Clone Detection, please refer
% to <docid:slcheck_ug#bvdvcxp-1>.

%% Set Up Model for Resource Sharing
% This edited model is saved as hdlcoderParkTransformShare.slx. Copy this version
% of the model into the temporary directory using the following commands:
design_name = 'hdlcoderParkTransformShare';
design_new_name = 'hdlcoderParkTransformShareCopy';

hdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos');  
hdlc_temp_dir = [tempdir 'hdlcoderParkTransformDir'];

% Create a temporary folder and copy the Simulink model.
cd(tempdir);
[~, ~, ~] = rmdir(hdlc_temp_dir, 's');
mkdir(hdlc_temp_dir);
cd(hdlc_temp_dir);

copyfile(fullfile(hdlc_demo_dir, [design_name,'.slx']), fullfile(hdlc_temp_dir, [design_new_name,'.slx']), 'f');

% Open the model.
open_system(design_new_name);

%%
% Set a *SharingFactor* of 6 on the subsystem of interest and generate
% HDL along with the corresponding reports from the model.
subsystem = [design_new_name '/DUT'];
hdlset_param(subsystem, 'SharingFactor', 6);
makehdl(subsystem);

%% 
% The High-level Resource Report shows that the resource utilization for 
% the design was estimated to be:
%
%               Resource              Usage
%       _________________________   ________ 
%       Multipliers                 13
%       Adders/Subtractors          178
%       Registers                   1012
%       Total 1-Bit Registers       16888
%       RAMs                        0
%       Multiplexers                610
%       I/O Bits                    196
%       Static Shift operators      8
%       Dynamic Shift operators     8

%%
% You can also run synthesis workflow on the model. The synthesis results
% for this model are:
%
%               Resource             Usage
%       _________________________   ________ 
%       Slice LUTs                  8792
%       Slice Registers             8419
%       DSPs                        13
%       Block Ram Tile              0
%       URAM                        0
%
%
% For more details regarding synthesis workflow, see
% <docid:hdlcoder_gs#btzatd4-1>.

%% Use Clone Detection to Optimize the Model for Resource Sharing
% Before you use the Clone Detector App, create a customer library that
% contains each repeating pattern in the model as an Atomic Subsystem. For
% this example, you are given a custom library called
% hdlcoderParkTransformShareLib.slx.

%%

% Copy the library into the temporary directory
lib_name = 'hdlcoderParkTransformShareLib';
lib_new_name = 'hdlcoderParkTransformShareLibCopy';

copyfile(fullfile(hdlc_demo_dir, [lib_name,'.slx']), fullfile(hdlc_temp_dir, [lib_new_name,'.slx']), 'f');

% Open the custom library
open_system('hdlcoderParkTransformShareLibCopy');

%%
% Replace patterns of the model with the library containing the Atomic
% Subsystem. To configure the Clone Detector App and replace the clones,
% refer to <docid:slcheck_ug#bvdvcxp-1>.
% Specifically, follow the instructions in the _Set the Parameters for
% Clone Detection_ section to link the library for clone detection, and set
% the *Maximum number of different parameters* value to |0|.
% 
% Then, follow the _Replace Clones_ section to apply this library to the 
% model for exact clone replacement.
%
% Notice that all the patterns in the original model has been replaced with
% atomic subsystems from the library as shown below:
%
% <<hdlcoderParkTransform_result_model.PNG>>
%
% Save the changes made to this system. In the Simulink Editor, on the
% *Simulation* tab, click *Save*.


%% Compare Results from Optimized Model
% Since this model contains the Sin, Cos, and Product blocks inside atomic
% subsystems, they can now be shared and results in an improvement in
% resource utilization.
subsystem = [design_new_name '/DUT'];
hdlset_param(subsystem, 'SharingFactor', 3);
% Generate HDL code and the corresponding reports for the optimized model
makehdl(subsystem);

%% 
% After sharing, the High-level Resource Report shows that the resource
% utilization for the design was estimated to be:
%
%               Resource             Usage
%       _________________________   ________ 
%       Multipliers                 13
%       Adders/Subtractors          177
%       Registers                   1004
%       Total 1-Bit Registers       16657
%       RAMs                        0
%       Multiplexers                610
%       I/O Bits                    196
%       Static Shift operators      8
%       Dynamic Shift operators     8
%
% Notice that the *Adders/Subtractors*, *Registers*, and *Total 1-Bit Registers*
% counts have reduced after the atomic subsystems are shared.

%%
% You can also run synthesis workflow on the model. The synthesis results
% for this model are:
%
%               Resource             Usage
%       _________________________   ________ 
%       Slice LUTs                  8848
%       Slice Registers             8308
%       DSPs                        13
%       Block Ram Tile              0
%       URAM                        0
%
% Notice that the *Slice Registers* count has reduced after sharing.