Main Content

Data Structures in the Generated Code

The code generator organizes the data used by the model in data structures. For each model, the code generator stores data, such as signals and states, in global structures that are also referred to as standard data structures. Use these data structures to access the data in the generated code.

Real-time model (rtModel) data structure is a top level data structure that contains instance-specific data, which is used by the model for reusable functions. The code generator uses this data structure for only multi-instance code generation.

Standard Data Structures

In a model, root-level Inport and Outport blocks represent the primary inputs and outputs of the block algorithm. By default, the code generator aggregates these blocks into standard structures that store input and output data.

Signal lines, block parameters, states, and other model data appear in the generated code as fields of standard structures. For general information, see How Generated Code Stores Internal Signal, State, and Parameter Data.

The table shows some common data structures in the generated code. The name of the structure type and, if applicable, structure variable in the code that you generate from a model depends on the model settings. Regardless of the settings, the name of the structure type in the code contains the short name from the Short Name of Structure Type column in the table.

Data Structures Generated for a Model

Short Name of Structure TypeData Represented in the Structure
ExtU

Data from root Inport blocks

ExtY

Data from root Outport blocks

B

Block output signals

ConstB

Constant-value block output that could not be inlined

P

Block parameters

ConstP

Constant-value parameters that could not be inlined

DW

Discrete block states

XDis

Status of enabled subsystems

InstP

Parameter arguments

SharedDSMShared local data stores

You can exclude data from appearing in these structures by using:

Real-Time Model Data Structure

To access the fields of an S-function's simulation data structure (SimStruct), the Simulink® software provides a set of functions. The code generator replaces SimStruct with an optimized real-time model (rtModel) data structure.

Code generation by using the GRT and ERT system target files uses the real-time model data structure (rtModel). This structure encapsulates model-specific information in a much more compact form than the simulation structure SimStruct. Depending on the model, the model-specific information includes data such as internal data for reusable functions and error state.

When you set the Code interface packaging model configuration parameter to Reusable function, the code generator packages model data (such as block I/O, Dwork, and parameters) into the real-time model data structure. If the model contains blocks that depend on absolute or elapsed time, the code generator packages timer service code into the real-time model data structure regardless of the setting of the Code interface packaging parameter. The code generator passes the real-time model structure by reference to generated model entry-point functions. If you set Pass root-level I/O as (Embedded Coder) to Part of model data structure, the code generator packages root-level model input and output into the real-time model data structure.

Efficient capabilities of the generated code depend on generation of rtModel rather than SimStruct, including:

  • Integer absolute and elapsed timing services

  • Independent timers for asynchronous tasks

  • Generation of improved C API code for signal, state, and parameter monitoring

  • Pruning the data structure to minimize its size (ERT-derived system target files only)

The real-time model data structure is defined in the main.c or main.cpp file that is generated when you build your model. For ERT models, if you clear the model configuration parameter Generate an example main program (Embedded Coder), rtmodel.h file is generated. The rtmodel.h file contains #include directives required by the rt_main.c or rt_cppclass_main.cpp static main program module. The rt_main.c or rt_cppclass_main.cpp includes rtmodel.h to access model-specific data structures and entry points because the static main program module is not created at code generation time.

The code generator provides a set of macros for accessing the rtModel data structure. These macros are analogous to the ssSetxxx and ssGetxxx macros that S-functions use to access SimStruct data, including noninlined S-functions compiled by the code generator.

Use the set of macros rtmGetxxx and rtmSetxxx to access the real-time model data structure. These macros are used in the generated code as well as in the main.c or main.cpp module of your external code. If you are customizing main.c or main.cpp (either a static or a generated file), use rtmGetxxx and rtmSetxxx instead of the ssSetxxx and ssGetxxx macros.

Usage of rtmGetxxx and rtmSetxxx macros is the same as for the ssSetxxx and ssGetxxx versions, except that you replace SimStruct S by real-time model data structure rtM. This table lists rtmGetxxx and rtmSetxxx macros that are used in rt_main.c and rt_main.cpp.

Macros for Accessing the Real-Time Model Data Structure

rtm Macro Syntax

Description

rtmGetdX(rtm)

Get the derivatives of block continuous states

rtmGetOffsetTimePtr(RT_MDL rtM)

Return the pointer to vector that stores sample time offsets of the model associated with rtM

rtmGetNumSampleTimes(RT_MDL rtM)

Get the number of sample times of a block

rtmGetPerTaskSampleHitsPtr(RT_MDL)

Return a pointer to NumSampleTime × NumSampleTime matrix

rtmGetRTWExtModeInfo(RT_MDL rtM)

Return an external mode information data structure of the model (used by code generator only for external mode)

rtmGetRTWLogInfo(RT_MDL)

Return a data structure used by code generator logging (used by code generator only)

rtmGetRTWRTModelMethodsInfo(RT_MDL)

Return a data structure of real-time model methods information (used by code generator only)

rtmGetRTWSolverInfo(RT_MDL)

Return data structure containing model solver information (used by code generator only)

rtmGetSampleHitPtr(RT_MDL)

Return a pointer to Sample Hit flag vector

rtmGetSampleTime(RT_MDL rtM, int TID)

Get task sample time

rtmGetSampleTimePtr(RT_MDL rtM)

Get pointer to a task sample time

rtmGetSampleTimeTaskIDPtr(RT_MDL rtM)

Get pointer to a task ID

rtmGetSimTimeStep(RT_MDL)

Return simulation step type ID (MINOR_TIME_STEP, MAJOR_TIME_STEP)

rtmGetStepSize(RT_MDL)

Return the fundamental step size of the model

rtmGetT(RT_MDL,t)

Get the current simulation time

rtmSetT(RT_MDL,t)

Set the time of the next sample hit

rtmGetTaskTime(RT_MDL,tid)

Get the current time for the current task

rtmGetTFinal(RT_MDL)

Get the simulation stop time

rtmSetTFinal(RT_MDL,finalT)

Set the simulation stop time

rtmGetTimingData(RT_MDL)

Return a data structure used by timing engine of the model (used by code generator only)

rtmGetTPtr(RT_MDL)

Return a pointer to the current time

rtmGetTStart(RT_MDL)

Get the simulation start time

rtmIsContinuousTask(rtm)

Determine whether a task is continuous

rtmIsMajorTimeStep(rtm)

Determine whether the simulation is in a major step

rtmIsSampleHit(RT_MDL,tid)

Determine whether the sample time is hit

rtmGetErrorStatus(rtm)

Get the current error status

rtmSetErrorStatus(rtm,val)

Set the current error status

rtmGetErrorStatusPointer(rtm)

Return a pointer to the current error status

rtmGetStopRequested(rtm)

Return whether a stop is requested

rtmGetBlockIO(rtm)

Get the block I/O data structure

rtmSetBlockIO(rtm,val)

Set the block I/O data structure

rtmGetContStates(rtm)

Get the continuous states data structure

rtmSetContStates(rtm,val)

Set the continuous states data structure

rtmGetDefaultParam(rtm)

Get the default parameters data structure

rtmSetDefaultParam(rtm,val)

Set the default parameters data structure

rtmGetPrevZCSigState(rtm)

Get the previous zero-crossing signal state data structure

rtmSetPrevZCSigState(rtm,val)

Set the previous zero-crossing signal state data structure

rtmGetRootDWork(rtm)

Get the DWork data structure

rtmSetRootDWork(rtm,val)

Set the DWork data structure

rtmGetU(rtm)

Get the root inputs data structure (when root inputs are passed as part of the model data structure)

rtmSetU(rtm,val)

Set the root inputs data structure (when root inputs are passed as part of the model data structure)

rtmGetY(rtm)

Get the root outputs data structure (when root outputs are passed as part of the model data structure)

rtmSetY(rtm,val)

Set the root outputs data structure (when root outputs are passed as part of the model data structure)

For more information, see SimStruct Macros and Functions Listed by Usage.

Related Topics