%% Data Initialization in the Generated Code
% This example shows how the generated code initializes signal, state, and
% parameter data.
%% Explore Example Model
% Open the example model, |rtwdemo_rtwintro|.
open_system('rtwdemo_rtwintro')
%%
% In the Apps gallery, click *Simulink Coder*.
%
% Open the Model Data Editor. On the *Modeling* tab, click *Model Data
% Editor*.
%
% Inspect the *States* tab. For the Unit Delay
% block state, the *Initial Value* is set to
% |0|, the default, which means the initial value
% of the state is zero. The state is named |X|.
%
% Set *Initial Value* to a nonzero number, for example, |5|.
set_param('rtwdemo_rtwintro/X','InitialCondition','5')
%%
% Open the Code Mappings editor. In the *C Code* tab, select *Code
% Interface > Individual Element Code Mappings*.
%
% On the *Signals/States* tab, under *States* set *Storage Class* for |X| to |ExportedGlobal|. With this
% setting, the block state appears in the generated code as a separate
% global variable.
coder.mapping.utils.create('rtwdemo_rtwintro');
cm = coder.mapping.api.get('rtwdemo_rtwintro');
setState(cm,'rtwdemo_rtwintro/X','StorageClass','ExportedGlobal');
%%
% In the model, open the |Amplifier| subsystem, which is a triggered
% subsystem.
%
% In the Model Data Editor, inspect the *Parameters* tab. To set and inspect initial
% values of signals and states, you can use the *Parameters* tab instead
% of the *States* tab.
%
% The Model Data Editor shows that for the Outport
% block, the *Initial output* (|InitialOutput|)
% parameter is set to |0|, the default. This
% subsystem output requires an initial value because the subsystem executes
% conditionally. Leave the initial value at the default.
%
% In the model, clear *Configuration Parameters > Signal
% storage reuse*. When you clear this setting, signal lines appear in the generated
% code as fields of a generated structure whose purpose is to store signal data. This
% representation of the signals makes it easier to see how the code
% generator initializes data.
set_param('rtwdemo_rtwintro','OptimizeBlockIOStorage','off')
%%
% In the model, inspect the configuration parameter *Configuration
% Parameters > Optimization > Default parameter
% behavior*. The configuration parameter is set to |Inlined|, which means block
% parameters, including initial values, appear in the generated code as
% inlined literals or as |const| data.
%
% Navigate to the root level of the model.
%
% In the Model Data Editor, click the *Show/refresh additional information*
% button.
%
% Near the *Filter contents* box, click the *Filter using selection*
% button.
%
% In the model, click the Constant block labeled |INC|. The Model Data Editor shows that the *Constant
% value* (|Value|) parameter of the block is set to |INC|. |INC| is a MATLAB variable
% in the base workspace.
%
% For |INC|, set the value in the *Value* column to |Simulink.Parameter(uint8(1))|. MATLAB converts |INC| to a |Simulink.Parameter| object.
%
% In the Code Mappings editor, on the *Parameters* tab, apply the storage class
% |ExportedGlobal| to |INC|. With this setting, the
% generated code defines |INC| as a global variable. Concerning
% initialization, |INC| is an item of parameter data.
INC = Simulink.Parameter(INC);
INC.StorageClass = 'ExportedGlobal';
%% Generate and Inspect Code
% To build the model and generate code, press *Ctrl+B* .
rtwbuild('rtwdemo_rtwintro');
%%
% In the generated file |rtwdemo_rtwintro.c|, outside the definition of a function, the code
% statically initializes the parameter data |INC|. The value of |INC| is
% |1|.
file = fullfile('rtwdemo_rtwintro_grt_rtw','rtwdemo_rtwintro.c');
rtwdemodbtype(file,'/* Exported block parameters */','uint8_T INC = 1U;',1,1)
%%
% In the same file, inspect the definition of the
% |rtwdemo_rtwintro_initialize| function. First, the function uses |memset|
% to initialize the internal signals in the model to a stored
% value of |0|.
rtwdemodbtype(file,'/* block I/O */','sizeof(B_rtwdemo_rtwintro_T));',1,1)
%%
% The function then initializes the Unit Delay state, |X|, to
% a ground value. In this case, the ground value is zero.
rtwdemodbtype(file,'/* exported global states */','X = 0U;',1,1)
%%
% The function also initializes other data, including the root-level inputs
% and outputs (Inport and Outport blocks), to ground values.
rtwdemodbtype(file,'/* external inputs */','Amplifier_Trig_ZCE = POS_ZCSIG;',1,1)
%%
% The function then initializes |X| to the value that you specified in the
% *Initial condition* block parameter.
rtwdemodbtype(file,'/* InitializeConditions for UnitDelay: ''<Root>/X'' */',...
'X = 5U;',1,1)
%%
% Finally, the function initializes the output of the |Amplifier|
% subsystem.
rtwdemodbtype(file,'SystemInitialize for Triggered SubSystem',...
'End of SystemInitialize for SubSystem',1,1)
%% Inspect Difference Between Stored Value and Real-World Value
% For some data, even if the real-world initial value is zero, a computer
% stores a nonzero value in memory. To observe this difference, apply a
% slope-bias fixed-point data type to the block state, |X|. To run this
% example, you must have Fixed-Point Designer(TM).
%
% In the Model Data Editor, inspect the *Signals* tab.
%
% In the model, click the output signal of the Switch block, |switch_out|.
%
% Use the *Data Type* column of the Model Data Editor to set the signal data type to |fixdt(1,16,1,3)|. This
% expression represents a fixed-point data type with a slope of |1|
% and a bias of |3|.
set_param('rtwdemo_rtwintro/Switch','OutDataTypeStr',...
'fixdt(1,16,1,3)')
%%
% In the model, select the Sum block. In the Property Inspector,
% under *Signal Attributes*, clear *Require all inputs to have the same data type*.
%
set_param('rtwdemo_rtwintro/Sum','InputSameDT','off')
%%
% To prevent compilation errors on different platforms, select the model configuration parameter *Generate
% code only*. This setting causes the model to generate only code.
set_param('rtwdemo_rtwintro','GenCodeOnly','on')
%%
% To build the model and generate code, press *Ctrl+B* .
rtwbuild('rtwdemo_rtwintro')
%%
% The model initialization function first initializes |X| to a real-world
% ground value, |0|. Due to the slope-bias data type, which has bias |3|, this real-world value
% corresponds to a stored value of |-3|.
rtwdemodbtype(file,'/* exported global states */','X = -3;',1,1)
%%
% The function then initializes |X| to the real-world initial value that
% you specified in the *Initial condition* block parameter, |5|. This
% real-world value corresponds to a stored value of |2|.
rtwdemodbtype(file,'/* InitializeConditions for UnitDelay: ''<Root>/X'' */',...
'X = 2;',1,1)

%%
% Copyright 2019 The MathWorks, Inc.