User guide
System Generator for DSP User Guide www.xilinx.com 51
UG640 (v 12.2) July 23, 2010
Compiling MATLAB into an FPGA
Compiling MATLAB into an FPGA
System Generator provides direct support for MATLAB through the MCode block. The
MCode block applies input values to an M-function for evaluation using Xilinx's fixed-
point data type. The evaluation is done once for each sample period. The block is capable
of keeping internal states with the use of persistent state variables. The input ports of the
block are determined by the input arguments of the specified M-function and the output
ports of the block are determined by the output arguments of the M-function. The block
provides a convenient way to build finite state machines, control logic, and computation
heavy systems.
In order to construct an MCode block, an M-function must be written. The M-file must be
in the directory of the model file that is to use the M-file or in a directory in the MATLAB
path.
This tutorial provides ten examples that use the MCode block:
• Example 1 Simple Selector shows how to implement a function that returns the
maximum value of its inputs;
• Example 2 Simple Arithmetic Operations shows how to implement simple arithmetic
operations;
• Example 3 Complex Multiplier with Latency shows how to build a complex
multiplier with latency;
• Example 4 Shift Operations shows how to implement shift operations;
• Example 5 Passing Parameters into the MCode Block shows how to pass parameters
into a MCode block;
• Example 6 Optional Input Ports shows how to implement optional input ports on an
MCode block;
• Example 7 Finite State Machines shows how to implement a finite state machine;
• Example 8 Parameterizable Accumulator shows how to build a parameterizable
accumulator;
• Example 9 FIR Example and System Verification shows how to model FIR blocks and
how to do system verification;
• Example 10 RPN Calculator shows how to model a RPN calculator – a stack machine;
• Example 11 Example of disp Function shows how to use disp function to print
variable values.
The first two examples are in the mcode_block_tutorial.mdl file of the
examples/mcode_block directory in your installation of the System Generator software.
Examples 3 and 4 are in the mcode_block_tutorial2.mdl file. Examples 5 and 6 are in the
mcode_block_tutorial3.mdl file. Examples 7 and 8 are in the mcode_block_tutorial4.mdl
file. Example 9 is mcode_block_verify_fir.mdl. Example 10 is in
mcode_block_rpn_calculator.mdl.
Simple Selector
This example is a simple controller for a data path, which assigns the maximum value of
two inputs to the output. The M-function is specified as the following and is saved in an M-
file xlmax.m:
function z = xlmax(x, y)
if x > y
z = x;