Main Content

Remove Reset and Disable Functions from the Generated Code

This example shows how the code generator removes unreachable (dead code) instances of the reset and disable functions from the generated code for ERT-based systems that include model referencing hierarchies. Optimizing the generated code to remove unreachable code is a requirement for safety-critical systems. This optimization also improves execution speed and reduces ROM consumption.

If a model contains blocks with states, the generated code contains reset and disable functions. If the model is not part of a conditionally executed system, such as an enabled subsystem, the code generator can remove the disable function because the generated code does not call it. If the model is not part of a conditionally executed system that can reset states when a control input enables it, the code generator can remove the reset function because the generated code does not call it.

Example Model

A referenced model, DisableResetFunctionBottom, is in DisableResetFunctionTop. The referenced model contains two blocks with states, a Delay block and a Discrete-Time Integrator block.

top_model.png

bottom_model.png

Generate Code Without This Optimization

Open the DisableResetFunctionTop model and generate code.

modelTop = 'DisableResetFunctionTop';
open_system(modelTop);
evalc('slbuild(modelTop)');

The DisableResetFunctionBottom.c file contains these reset and disable functions.

cfileReset = fullfile('slprj','ert','DisableResetFunctionBottom','DisableResetFunctionBottom.c');
coder.example.extractLines(cfileReset,'/* System reset for referenced model:', '/* Enable for referenced model:', 1, 0);
/* System reset for referenced model: 'DisableResetFunctionBottom' */
void DisableResetFunctionBottom_Reset(DisableResetFunctionBottom_DW_f *localDW)
{
  /* InitializeConditions for Delay: '<Root>/Delay' */
  localDW->Delay_DSTATE = 0.0;

  /* InitializeConditions for DiscreteIntegrator: '<Root>/Discrete-Time Integrator' */
  localDW->DiscreteTimeIntegrator_DSTATE = 3.0;
}
cfileDisable = fullfile('slprj','ert','DisableResetFunctionBottom','DisableResetFunctionBottom.c');
coder.example.extractLines(cfileDisable,'/* Disable for referenced model', '/* Output and update for referenced model:', 1, 0);
/* Disable for referenced model: 'DisableResetFunctionBottom' */
void DisableResetFunctionBottom_Disable(real_T *rty_Out1,
  DisableResetFunctionBottom_DW_f *localDW)
{
  /* Disable for DiscreteIntegrator: '<Root>/Discrete-Time Integrator' */
  localDW->DiscreteTimeIntegrator_DSTATE = *rty_Out1;
}

The DisableResetFunctionTop_step function does not call the DisableResetFunctionBottom_Disable function because the model is not part of a conditionally executed system. The DisableResetFunctionTop_step function does not call the DisableResetFunctionBottom_Reset function because the model is not part of a conditionally executed system that can reset states when a control input enables it.

Generate Code With This Optimization

Open the DisableResetFunctionBottom model.

modelBottom = 'DisableResetFunctionBottom';
open_system(modelBottom);

Open the Configuration Parameters dialog box for DisableResetFunctionBottom. On the Interface pane, in the Advanced parameters section, select the Remove Disable Function and Remove Reset Function parameters.

Alternatively, use the command-line API to enable the optimization:

set_param(modelBottom, 'RemoveResetFunc','on');
set_param(modelBottom, 'RemoveDisableFunc','on');

Generate code for the DisableResetFunctionTop model.

evalc('slbuild(modelTop)');

Navigate to the ert folder inside the slprj folder and open the DisableResetFunctionBottom.c file. The code does not contain the DisableResetFunctionBottom_Reset function or the DisableResetFunctionBottom_Disable function.

Close the models and the code generation report.

bdclose(modelTop)
bdclose(modelBottom)

See Also

|

Related Topics