%% Generate Code for Model Reference Hierarchy
% This example shows how to reference an existing model and generate code
% for the model reference hierarchy.

% Copyright 2020 The MathWorks, Inc.

%% Create Model Reference Hierarchy
% 1. Open an existing model, for example the |ex_model_arg_ref| model.  
%    This model represents a reusable algorithm. 
%    This is a GRT model. If you have Embedded Coder, change the model
%    configuration parameter *System target file* to |ert.tlc|.
open_system('ex_model_arg_ref');

%%
% 2. A referenced model must specify that it is available to be referenced 
%    and whether it can be referenced just once or multiple times.
%    Specify the model configuration parameter 
%    <docid:simulink_gui#bq7cyf9-6 *Total number of instances allowed per
%    top model*> as |One| or |Multiple|. By default,
%    *Total number of instances allowed per top model* is set to
%    |Multiple|.
%
% 3. Create a new model that you want as a top model in the model
%    reference hierarchy. The top model refers to other models as referenced
%    models or multiple instances of a referenced model.
%
% 4. Add Inports and Outports to the top model as required. To add a
%    referenced model, use the <docid:simulink_ref#f7-2720095 Model> block.
%
% 5. The new block is initially unresolved because it does not specify a 
%    referenced model. To open the block parameters dialog box, 
%    double-click the Model block or use the context menu of the Model block.
%
% <<../model_block.png>>
%
% 6. Enter the name of the referenced model in the *Model name* field. 
%    This name must contain fewer than 60 characters, exclusive of the 
%    |.slx| suffix.
%
% 7. Click *OK*. If the referenced model contains root-level inputs or  
%    outputs, the Model block displays corresponding input and output ports.
% 
% 8. Create another instance of the Model block so that the top model
%    looks like model:
open_system('ex_model_arg');

%%
% 9. Name the model |ex_model_arg| and save the model.
%
% You can also convert a subsystem to a referenced model. For more details,
% see <docid:simulink_ug#f4-141943>.

%% Generate Code for Model Reference Hierarchy
%
% 1. Verify that the model configuration parameter Solver selection *Type*
%    is set to |Fixed-step| for the top model and the referenced model. 
%    To open the respective Configuration Parameters dialog boxes, on the 
%    *Modeling* tab, select *Model Settings* for the respective models.
%
% For a model reference hierarchy that has multiple referenced models, 
% you can access the *Model Settings* of the referenced model that is open
% and the top model through this menu. To access the *Model Settings* of 
% another referenced model in the model hierarchy, open the referenced 
% model to access this menu. 
%
% <<../model_settings.png>>
%
% 2. For the top model, open the *C Code* tab by selecting either  
%    *Simulink Coder* app or *Embedded Coder* app. To generate code for  
%    the model reference hierarchy, click the *Build* button.
%
%%
% The Simulink Coder build process generates and compiles code. The current
% folder now contains new files and a new folder.
%
% * |ex_model_arg_grt_rtw| &mdash; The build folder containing the generated
% code for the top model.
%
% * |slprj > grt > ex_model_arg_ref| &mdash; The folder containing the generated
% code for the referenced model.
%
% * |ex_model_arg.exe| &mdash; The executable created by the build process.
%
% * |ex_model_arg.slxc| &mdash; The Simulink cache file for the top model.
%
% * |ex_model_arg_ref.slxc| &mdash; The Simulink cache file for the referenced
% model.
%
% For more information about the configuration parameter requirements, 
% see <docid:rtw_ug#brbtbq1>.
%% View Generated Code in Code Generation Report
%
% To view the generated code, in the *C Code* tab, select *Open Report*.
% The code generation report contains hyperlinks to all the files generated
% for the model reference hierarchy.
%
% To open the generated code for the referenced model, click the referenced
% model hyperlink under the *Referenced Models* section on the left pane. 
%
% <<../code_gen_report.png>>
%
% To navigate back to the top model, click the *Go Backward* button on the
% top-left corner of the code generation report window.
