C-Script

Purpose

Execute custom C code

Library

Control / Functions & Tables

Description

../../_images/cscript.svg

The C-Script block allows for custom functionality to be implemented in the C programming language. For a detailed description of C-Scripts see chapter C-Scripts.

The C-Script dialog consists of two tabbed panes that are described below.

Setup

Number of inputs, outputs

A positive integer or a vector of positive integers. If you enter a scalar, the block will have one input or output terminal, and the scalar value determines the signal width. If you enter a vector, the number of elements determines the number of input or output terminals, and the elements in the vector determine the signal width of the corresponding terminal. The first input or output terminal is marked with a dot.

For dynamic sizing set the width of one or more input terminals to -1; the width will then be determined at simulation start depending on the number of elements in the signal that is connected to the input port. All occurrences of -1 in the input and output widths and any other data vector will be expanded to the same width.

Number of cont. states, disc. states, zero-crossings

A positive or zero integer specifying the sizes of the different data vectors (i.e. continuous and discrete state variables, and zero-crossing signals) that the C-Script registers with the solver.

Direct feedthrough

A vector of zeros and ones specifying the direct feedthrough flags for the input signals. An input signal has direct feedthrough if you need to access the current input signal value during the output function call. This has an influence on the block sorting order and the occurrence of algebraic loops (see Block Sorting).

If the C-Script block has one input terminal, a flag in the vector is applied to the corresponding element of the input signal vector; if the block has multiple input terminals, a flag is applied to the whole signal of the corresponding terminal. You can also specify a single scalar, which applies to all signals of all input terminals.

Sample time

A scalar specifying the sampling period or an \(n \times 2\) matrix specifying the sampling period(s) and offset(s), in seconds \((\mathrm{s})\). The table below lists the valid parameter values for the different sample time types. For a detailed description of the sample time types see Sample Time.

Type

Value

Continuous

[0, 0] or 0

Semi-Continuous

[0, -1]

Discrete-Periodic

[Tp, To] or Tp

Tp: Sample period, Tp\({} > 0\)

To: Sample offset, \(0 \le{}\)To\({}<{}\)Tp

Discrete-Variable

[-2, 0] or -2

Use terminal-based sample times

If this box is checked, the C-Script block uses different sample times for the individual input and output terminals. The Sample time parameter must be a matrix with one row per input terminal (in order) followed by one row per output terminal (in order) optionally followed by further block sample times.

Language standard

The language standard used by the compiler. Possible values are C90, C99 and C11. The default is C99.

Enable GNU extensions

If this box is checked, the compiler enables GNU C language features not found in ISO standard C. These extensions are disabled by default.

Highlight level

The number of levels the highlight is propagated upwards in the component hierarchy when the C-Script flags a diagnostic message. A highlight level of 0 means that the C-Script block itself will be highlighted. A highlight level of 1 means that the component containing the C-Script block will be highlighted when the assertion fails, etc.

Enable runtime checks

If this box is checked, protective code is added to guard against access violations when working with block data (i.e. signal values, states, zero-crossing signals etc.). The C-Script function calls are also wrapped with protective code to prevent you from violating solver policies such as accessing input signals in the output function without enabling direct feedthrough.

It is strongly recommended to leave the runtime checks enabled.

Parameters

A comma-separated list of expressions that are passed as external parameters into the C functions. The expressions can reference workspace variables and must evaluate to scalars, vectors, matrices, 3d-arrays or strings.

Code

The Code pane consists of a combobox for selecting a particular code section and a text editor that lets you edit the currently selected code section. For details on the individual sections see C-Script Functions. The different macros that you need to use in order to access block data such as input/output signals and states are listed in C-Script Macros.

If you have made changes to the C code, it will be compiled when you click on Apply or OK. Any errors or warnings that occur during compilation are listed in a diagnostic window. Small badges next to the line numbers indicate the problematic code lines. If you move the mouse cursor near such a badge, a tooltip with the diagnostics for that line will appear.

A Find dialog for finding and optionally replacing certain text is available from the context menu or by pressing Ctrl+F (on macOS: Cmd+F). The dialog has an option to search the current code section only (This section) or all code sections of the C-Script (All sections).

Probe Signals

Input \(i\)

The \(i\)-th input signal.

Output \(i\)

The \(i\)-th output signal.