Generating ANSI C-Code

Navigation:  Toolboxes > Real-Time Toolbox > C-Code Generation >

Generating ANSI C-Code

Previous pageReturn to chapter overviewNext page


The modeling and simulation package 20-sim is capable of generating ANSI C-Code for several targets. Possible targets are Stand-Alone C, a C-Function, or a Simulink S-Function. Since 20-sim generates well-documented ANSI C-Code, it is easy for a user to make modifications or extensions to the generated code by hand. However it is also possible to fully define your own code target using C-Code templates, and to make modifications in these templates, so the resulting source code is tailor made for your application.

General Structure

The code generation process of 20-sim typically has the following structure:


Step 1

1.The process starts with a model and simulation in 20-sim.
2.In the 20-sim Simulator, the user selects the C-Code Generation command from the Tools menu.
3.20-sim collects information about the current model.
4.A target configuration file is read that specifies the different code generation targets that exist.
5.The C-code generation dialog is opened, allowing the user to specify the target to use.

Step 2

1.The selected target refers to specific template files (source files) in a target specific subdirectory.
2.20-sim adapts these template files with the model information and generated code for the model.

Step 3

1.The resulting files are placed in a destination directory.
2.These files are now specific for the target and the model.
3.If desired, 20-sim can call additional commands for further processing (f.i. make, run etc).

Code Generation Dialog

After 20-sim reads the target configuration file, the code generation dialog is shown with the information of this file:


The target list shows the names of the available targets. When a selection is made, the description of the selected target is given at the right. A submodel can be selected when the target requires this. The output directory can be overruled.


The user can create own targets. All targets are defined in the target definition file Targets.ini. This file has a typical INI-file structure and starts with a section that simply enumerates which targets exist. Each target then has its own section that holds the remaining information, like a short description of the target, the name of the template directory, the names of the template source files, additional commands to perform, etc. The following keywords may appear in a target section:


Each target can point to a set of template files that are used to create C-Code with. Own template files can be created by the user, with the help of the most powerful feature of 20-sim C-Code generation: the use of tokens! A token is a placeholder for model-dependent information. For instance in the C-Code Generation Dialog, the target destination directory contains the name of the selected submodel (e.g. c:\ temp\%SUBMODEL_NAME%) by default. Since this information is not yet known when the targets.ini file is created, a specific token that refers to this name is used instead. A complete description of predefined tokens is described in the section about Available 20-sim Tokens.

Generation Result

After the tokens in the target template files are replaced, the resulting target and model specific files will be placed in the destination directory. If commands are specified in the Targets.ini file, these commands are performed. This allows the user to call scripts automatically. E.g. for automatic compilation, linking and running of the code in a certain target environment.