Language Support

Fixed-Point Runtime Library Support

The coder supports most of the fixed-point runtime library functions supported by the Embedded MATLAB Function block. For a complete list of these functions, see Supported Functions and Limitations of the Fixed-Point Embedded MATLAB Subset in the Fixed-Point Toolbox documentation.

Some functions are not supported, or are subject to some restrictions. These functions are summarized in the following table.

FunctionRestrictionNotes
dispNot supported 
getNot supportedThis function returns a struct. Struct data types are not supported in this release.
pow2Not supported 
realNot supported 
divideSupported, with restrictions

The divisor must be a constant and a power of two.

fiSupported, with restrictions

Only the following rounding modes are supported: ceil, fix, floor, nearest.

fimathSupported, with restrictions

Only the following rounding modes are supported: ceil, fix, floor, nearest.

subsasgnSupported, with restrictions

Subscripted assignment supported; see Data Type Usage

subsrefSupported, with restrictions

Subscripted reference supported; see Data Type Usage

Variables and Constants

This section summarizes supported data types and typing rules for variable and constants, and the use of persistent variables in modeling registers.

Data Type Usage

When generating code for the Embedded MATLAB Function block, the coder supports a subset of MATLAB data types. The following table summarizes supported and unsupported data types.

Type(s)SupportNotes
Integer

Supported:

  • uint8, uint16, uint32,

  • int8, int16, int32

 
Real

Supported:

  • double

  • single

HDL code generated with double or single data types is not synthesizable.
Character

Supported:

char
 
Logical

Supported:

Boolean

 
Fixed point

Supported:

  • Scaled (binary point only) fixed point numbers

  • Custom integers (zero binary point)

Fixed point numbers with slope (not equal to 1.0) and bias (not equal to 0.0) are not supported.

Maximum word size for fixed-point numbers is 32 bits.

The convergent and matlab rounding modes are not currently supported. Do not specify these modes in fimath in specifications.

Vectors

Supported:

  • unordered {N}

  • row {1, N}

  • column {N, 1}

The maximum number of vector elements allowed is 2^32.

A variable must be fully defined before it is subscripted.

MatrixN/AMatrix data types are not supported in the current release.
StructN/AStruct data types are not supported in the current release.
Cell arraysN/ACell arrays are not supported in the current release.

Typing Ports, Variables and Constants

Strong typing rules are applied to Embedded MATLAB Function blocks, as follows:

Persistent Variables

Persistent variables let you model registers. If you need to preserve state between invocations of an Embedded MATLAB Function block, use persistent variables.

Each persistent variable must be initialized with a statement specifying its size and type before it is referenced. You can initialize a persistent variable with either a constant value or a variable, as in the following code listings:

% Initialize with a constant
persistent p;
if isempty(p)
    p = fi(0,0,8,0); 
end

% Initialize with a variable
initval = fi(0,0,8,0);

persistent p;
if isempty(p)
    p = initval; 
end

When testing whether a persistent variable has been initialized, it is good practice to use simple logical expressions, as in the preceding examples. Using simple expressions ensures that the HDL code for the test is generated in the reset process, and therefore is executed only once.

You can initialize multiple variables based on a single simple logical expression, as in the following example:

% Initialize with  variables
initval1 = fi(0,0,8,0);
initval2 = fi(0,0,7,0);

persistent p;
if isempty(p)
    x = initval1; 
    y = initval2; 
end

See also The Incrementer Function Code for an example of the initialization and use of a persistent variable.

Limitation on Use of Persistent Variables.   As described in Using Persistent Variables to Model State, you can use persistent variables in Embedded MATLAB code to simulate various kinds of delay blocks.

However, note that the ports on the Embedded MATLAB Function block act as direct feedthrough ports during simulation. The delay constructs internal to the Embedded MATLAB Function block are not recognized during simulation. Therefore a feedback loop in the model causes an algebraic loop condition.

To work around this limitation:

Use of Nontunable Parameter Arguments

An Embedded MATLAB function argument can be declared to be a parameter argument by setting its Scope to Parameter in the Ports and Data Manager GUI. Such a parameter argument does not appear as a signal port on the block. Parameter arguments for Embedded MATLAB Function blocks do not take their values from signals in the Simulink model. Instead, their values come from parameters defined in a parent Simulink masked subsystem or variables defined in the MATLAB base workspace.

Only nontunable parameters are supported for HDL code generation. If you declare parameter arguments in Embedded MATLAB function code that is intended for HDL code generation, be sure to clear the Tunable option for each such parameter argument.

See also Parameter Arguments in Embedded MATLAB Functions in the Simulink documentation.

Arithmetic Operators

When generating code for the Embedded MATLAB Function block, the coder supports the arithmetic operators (and their M-function equivalents) listed in the following table.

OperationOperator SyntaxM-Function EquivalentFixed Point Support?
Binary addition A+B plus(A,B) Y
Matrix multiplication A*B mtimes(A,B) Y
Arraywise multiplication A.*B times(A,B)Y
Matrix right division A/B mrdivide(A,B) Y
Arraywise right division A./Brdivide(A,B) Y
Matrix left division A\B mldivide(A,B) Y
Arraywise left division A.\B ldivide(A,B) Y
Matrix power A^Bmpower(A,B) Y
Arraywise power A.^B power(A,B) Y
Complex transpose A'ctranspose(A) Y
Matrix transpose A.'transpose(A) Y
Matrix concat [A B] NoneY
Matrix index
Note: A variable must be fully defined before it is subscripted.
A(r c) NoneY

Relational Operators

When generating code for the Embedded MATLAB Function block, the coder supports the relational operators (and their M-function equivalents) listed in the following table.

RelationOperator SyntaxM Function EquivalentFixed-Point Support?
Less than A<B lt(A,B) Y
Less than or equal to A<=B le(A,B) Y
Greater than or equal to A>=B ge(A,B) Y
Greater than A>B gt(A,B) Y
Equal A==B eq(A,B) Y
Not equal A~=B ne(A,B) Y

Logical Operators

When generating code for the Embedded MATLAB Function block, the coder supports the logical operators (and their M function equivalents) listed in the following table.

RelationOperator SyntaxM Function EquivalentFixed-Point Support?Notes
Logical And A&Band(A,B)Y  
Logical Or A|Bor(A,B)Y  
Logical Xor A xor Bxor(A,B)Y  
Logical And (short circuiting) A&&B

N/A

Y Use short circuiting logical operators within conditionals. See also Control Flow Statements.
Logical Or (short circuiting) A||B

N/A

Y Use short circuiting logical operators within conditionals. See also Control Flow Statements.
Element complement ~Anot(A)Y  

Control Flow Statements

When generating code for the Embedded MATLAB Function block, the coder imposes some restrictions on the use of control flow statements and constructs. The following table summarizes supported and unsupported control flow statements.

Control Flow Statement Notes

break

continue

return

Do not use these statements within loops. Use of these statements in a loop causes the coder to report the following error:

Unstructured flow graph or loop containing 
[statement type] not supported for HDL

while

while loops are not supported. Use of while loops causes the coder to report the following error:

Unstructured flow graph or loop containing 
[statement type] not supported for HDL

for

for loops without static bounds are not supported. Use of for loops without static bounds causes the coder to report the following error:

Unstructured flow graph or loop containing 
[statement type] not supported for HDL

Do not use a for loop with an increment other than 1. Use of an increment other than 1 can cause errors in HDL simulation and synthesis. If you require a for loop increment other than 1, you should use the Embedded MATLAB function eml.unroll to unroll the loop, as in the following code example, which unrolls a loop with an increment of 2.

for i=eml.unroll(1:2:8) 
    ... 
   end 

See also eml.unroll.

Do not use the & and | operators within conditions of a for statement. Instead, use the && and || operators.

The Embedded MATLAB Function block does not support nonscalar expressions in the conditions of for statements. Use the all or any functions to collapse logical vectors into scalars.

if

Do not use the & and | operators within conditions of an if statement. Instead, use the && and || operators.

The Embedded MATLAB Function block does not support nonscalar expressions are not supported in the conditions of if statements. Use the all or any functions to collapse logical vectors into scalars.

switch

The HDL code matches the behavior of the switch statement; the first matching case statement is executed.

Use only scalars in conditional expressions in a switch statement.

Use of fi variables in switch or case conditionals is not supported. For HDL code generation, the usage is restricted to uint8, uint16, uint32, sint8, sint16, and sint32.

If multiple case statements make assignments to the same variable, then their numeric type and fimath specification should match that variable.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS