TLC (Target Language Compiler) serves as the core rule description language for Simulink code generation. Its common instructions cover essential scenarios such as variable operations, logical control, and code output. Below, we categorize the instructions by function and provide a detailed introduction to the format, functionality, and examples of commonly used TLC instructions in the context of Simulink applications.
🔄 1. Variable Operation Instructions
These are used for declaring, assigning, and accessing variables in TLC scripts:
1. %assign variable_name = value;
Purpose: Declare a TLC variable and assign a value, supporting scalars, vectors, structures, and module property references.
Example:
tlc
%assign sampleTime = 0.1; // Discrete sampling time
%assign gainVal = Block.Gain; // Get module gain
2. %invoke function_name(parameters);
Purpose: Call built-in or custom TLC functions to perform complex operations.
Common built-in functions:
– tlcGetParam(Block, “Gain”): Get module parameters
– tlcConvertDataType(“double”, “float”): Convert data types
Example:
tlc
%assign gain = %invoke tlcGetParam(Block, “Gain”); // Built-in function to get parameters
⚙️ 2. Logical Control Instructions
These implement dynamic logical judgments and loop traversals in TLC scripts:
1. Conditional Judgments
tlc
%if Block.SampleTime == 0 %then
/* Continuous module initialization */
%elseif Block.SampleTime > 0 %then
/* Discrete module initialization */
%endif
2. Loop Traversal
tlc
%foreach inPort in Block.InputPorts
real_T u%<inPort.Index>[%<inPort.Width>];
%endforeach
📝 3. Code Output and Function Definition Instructions
The core bridge from TLC “rules” to code:
1. %<expression>
Purpose: Output TLC expressions to C code.
Example:
tlc
*y1 = %<u1> * %<Gain>; // Generate computation code
2. Custom Functions
tlc
%%Function CheckGainRange(Block)
%if Block.Gain > 100 %then
%tlcLog(“Gain exceeds limit!”);
%endif
%%EndFunction
🐞 4. Debugging and Auxiliary Instructions
These are used for syntax checking, breakpoint debugging, and log output:
1. Log Output
tlc
%tlcLog(“Module sampling time: %<sampleTime>”);
2. Breakpoint Debugging
tlc
%debug; // Set breakpoint
3. Command Line Instructions
Instruction Format Purpose Example
tlc -s script.tlc Check syntax tlc -s GainBlock.tlc
tlc -r model.slx -o path Generate code tlc -r Model.slx -o ./Code
💡 Usage Notes
1. Syntax Details
– Statements must end with a semicolon (except for code blocks)
– Module property references: Block.InputPorts(1).Width
2. Compatibility
– Built-in functions must match the TLC version
– Custom functions must be defined before calling
3. Performance Optimization
– Precompute static parameters
– Avoid repeated calls to %invoke within loops
By appropriately combining these instructions, a complete process from module parameter parsing to code generation can be achieved, which is a key technology for implementing Simulink models in embedded development.