## Embedded Coder |

This demo shows you how to use Target Function Libraries (TFLs) to replace operators and functions in the generated code.
The models described below demonstrate the replacement capabilities. With each example model, a separate Target Function Library
is provided to illustrate the creation of operator and function replacements using a MATLAB^{®} based API and how to register
them with Simulink^{®}.

Using Target Function Libraries enables:

- Better integration of model code with external and legacy code, to reduce code size and verification efforts
- The use of target-specific function implementations, to optimize performance of the embedded application

The Target Function Library can:

- Replace math functions with target-specific function implementations
- Replace math operations with target-specific function implementations
- Specify build information for compiling and building the replacements with the generated code

Please see the following sections from the Real-Time Workshop Embedded Coder^{®} documentation (login
required) for more information on:

- Steps in Using a Target Function Library
- Addition and Subtraction Operator Replacement for Built-in Integers
- Multiplication and Division Operator Replacement for Built-in Integers
- Fixed-Point Operator Replacement for Basic Operators
- Addition and Subtraction Operator Replacement in Embedded MATLAB Coder
- Set Embedded MATLAB Coder to Use the Target Function Library
- Compile the M File into a C Source File
- Inspect the Embedded MATLAB Coder Generated Code
- Math Function Replacement
- Viewer for Target Function Libraries
- Build Support
- Reserved Identifier Support
- Removing the Example Target Function Libraries

You can follow these steps to use a Target Function Library:

- Create a table of replacement function entries
- Register a Target Function Library consisting of one or more tables using Simulink's
`sl_customization.m`API - From the model, select the desired Target Function Library using the Interface pane under Real-Time Workshop
^{®}in the Configuration Parameters dialog box - Generate code for the model with Real-Time Workshop Embedded Coder

Please see the Introduction to Target Function Libraries section in the Real-Time Workshop Embedded Coder documentation for more information on the above steps (login required).

This example Target Function Library replaces '+' and '-' for two input, scalar operations on data types that are built-in integers:

`int8, uint8``int16, uint16``int32, uint32`

You can execute the following command to open the `rtwdemo_tfladdsub` model that demonstrates these replacements:

```
open_system('rtwdemo_tfladdsub')
```

Please see the Example: Mapping Operators to Target-Specific Implementations section in the Real-Time Workshop Embedded Coder documentation for more information on operator replacements (login required).

This example Target Function Library replaces '*' and '/' for two input, scalar operations on data types that are built-in integers:

`int8, uint8``int16, uint16``int32, uint32`

You can execute the following command to open the `rtwdemo_tflmuldiv` model that demonstrates these replacements:

```
open_system('rtwdemo_tflmuldiv')
```

This example Target Function Library replaces '+', '-', '*', and '/' for two input, scalar operations on data types that are fixed point. Replacements can be defined as matching:

- A specific slope/bias scaling combination on the inputs and output
- A specific binary point scaling combination on the inputs and output
- A relative scaling between the inputs and output
- The same slope value and a zero net bias across the inputs and output

You can execute the following command to open the `rtwdemo_tflfixpt` model that demonstrates these replacements:

```
open_system('rtwdemo_tflfixpt')
```

**Note:** Using fixed-point data types in a model requires a Simulink Fixed Point license.

This example Target Function Library replaces '+' and '-' for two input, scalar operations on integer data types when using
the `emlc` command.

- Please see the Example: Mapping Operators to Target-Specific Implementations section in the Real-Time Workshop Embedded Coder documentation for more information on operator replacements (login required).
- Please see the Working with Embedded MATLAB™ Coder section in the Real-Time Workshop documentation for more information on embeddable C code generation using Embedded MATLAB Coder (login required).

To execute the code shown below, you need to first copy the MATLAB program file to a temporary directory. You will need write-permission to your system's temporary directory for this operation.

You can execute the following commands to copy the temporary file to your system's temporary directory:

% You can view the MATLAB program file by uncommenting the following MATLAB command % edit(fullfile(matlabroot,'toolbox','rtw','rtwdemos',

% 'tfl_demo','addsub_two_int16.m')) look here>. emlcdir = [tempname filesep 'emlcdir']; if ~exist(emlcdir,'dir') mkdir(emlcdir); end emlcsrc = ... fullfile(matlabroot,'toolbox','rtw','rtwdemos',

'tfl_demo','addsub_two_int16.m'); copyfile(emlcsrc,emlcdir,'f'); emlcurdir = pwd; cd(emlcdir);

You can execute the following commands to configure Embedded MATLAB Coder to use the Target Function Library by setting up the configuration parameters for a Real-Time Workshop build and define the operation input type:

addpath(fullfile(matlabroot,'toolbox','rtw','rtwdemos','tfl_demo')); sl_refresh_customizations; rtwConfig = emlcoder.RTWConfig('ert'); rtwConfig.TargetFunctionLibrary = 'Addition & Subtraction Examples'; rtwConfig.GenerateReport = false; rtwConfig.LaunchReport = false; t = int16(2);

If you have a copy of Real-Time Workshop, you can see the TFL table definition file in the `tfl_table_addsub.m` file.

You can execute the following command to compile the MATLAB code using the configuration parameters that point to the desired Target
Function Library and the example input class defined in the previous step as input parameters to the `emlc` command:

emlc -T rtw -s rtwConfig -c addsub_two_int16 -eg {t, t};

After compiling, you may want to explore the generated source code.

Target Function Libraries support replacement of the following math functions (float versions only):

`sin, asin, sinh``cos, acos, cosh``tan, atan, tanh``exp, log, log10``ceil, floor, sqrt, abs``pow/power`

Additional functions that are supported are:

- memcpy, getNaN, getInf, getMinusInf

You can execute the following commands to open the `rtwdemo_tflmath` model that demonstrates these replacements:

```
cd(emlcurdir);
open_system('rtwdemo_tflmath')
```

Please see the Example: Mapping Math Functions to Target-Specific Implementations section in the Real-Time Workshop Embedded Coder documentation for more information on math function replacements (login required).

A viewer is provided for examining and validating tables and their entries. For example, you can execute the following commands
to view the `tfl_tablemuldiv` table:

tfl = tfl_table_muldiv; me = RTW.viewTfl(tfl);

Please see the Examining and Validating Function Replacement Tables section in the Real-Time Workshop Embedded Coder documentation for more information on the Target Function Library Viewer (login required).

Each entry in a Target Function Library table can specify build information such as:

- Header file dependencies
- Source file dependencies
- Additional include paths
- Additional source paths

Additionally, the method `RTW.copyFileToBuildDir` can be used to locally copy the source and header files specified by an entry. Please see the Specifying Build Information for Function Replacements section in the Real-Time Workshop Embedded Coder documentation for more information on specifying compilation information
(login required).

**Note:** The models in this demo are configured for code generation only, as the implementations for the replacement functions are
not provided.

Each function implementation name defined by an entry will be reserved as a unique identifier. Other identifiers can be specified with a table on a per-header-file basis. Providing additional reserved identifiers ensures that duplicate symbols and other identifier-related compile and link issues do not occur.

Please see the Adding Target Function Library Reserved Identifiers section in the Real-Time Workshop Embedded Coder documentation for more information on specifying reserved identifiers (login required).

Once you are finished using the example models, you can remove the example Target Function Libraries and close the example models with the following commands:

rmpath(fullfile(matlabroot,'toolbox','rtw','rtwdemos','tfl_demo')); sl_refresh_customizations; close_system('rtwdemo_tfladdsub', 0) close_system('rtwdemo_tflmuldiv', 0) close_system('rtwdemo_tflfixpt', 0) close_system('rtwdemo_tflmath', 0) if isa(me, 'DAStudio.Explorer') me.delete; end clear tfl; clear me; clear emlcdir; clear emlcsrc; clear emlcurdir; clear n1; clear rtwConfig; clear t;