Loren on the Art of MATLAB

Turn ideas into MATLAB

MATLAB to FPGA using HDL Coder(TM) 16

Posted by Loren Shure,

It's my pleasure to introduce guest blogger Kiran Kintali. Kiran is the product development lead for HDL Coder at MathWorks. In this post, Kiran introduces a new capability in HDL Coder™ that generates synthesizable VHDL/Verilog code directly from MATLAB and highlights some of the key features of this new MATLAB based workflow.


Introduction to HDL Code Generation from MATLAB

If you are using MATLAB to model digital signal processing (DSP) or video and image processing algorithms that eventually end up in FPGAs or ASICs, read on...

FPGAs provide a good compromise between general purpose processors (GPPs) and application specific integrated circuits (ASICs). GPPs are fully programmable but are less efficient in terms of power and performance; ASICs implement dedicated functionality and show the best power and performance characteristics, but require extremely expensive design validation and implementation cycles. FPGAs are also used for prototyping in ASIC workflows for hardware verification and early software development.

Due to the order of magnitude performance improvement when running high-throughput, high-performance applications, algorithm designers are increasingly using FPGAs to prototype and validate their innovations instead of using traditional processors. However, many of the algorithms are implemented in MATLAB due to the simple-to-use programming model and rich analysis and visualization capabilities. When targeting FPGAs or ASICs these MATLAB algorithms have to be manually translated to HDL.

For many algorithm developers who are well-versed with software programming paradigms, mastering the FPGA design workflow is a challenge. Unlike software algorithm development, hardware development requires them to think parallel. Other obstacles include: learning the VHDL or Verilog language, mastering IDEs from FPGA vendors, and understanding esoteric terms like "multi-cycle path" and "delay balancing".

In this post, I describe an easier path from MATLAB to FPGAs. I will show how you can automatically generate HDL code from your MATLAB algorithm, implement the HDL code on an FPGA, and use MATLAB to verify your HDL code.

MATLAB to Hardware Workflow

The process of translating MATLAB designs to hardware consists of the following steps:

  1. Model your algorithm in MATLAB - use MATLAB to simulate, debug, and iteratively test and optimize the design.
  2. Generate HDL code - automatically create HDL code for FPGA prototyping.
  3. Verify HDL code - reuse your MATLAB test bench to verify the generated HDL code.
  4. Create and verify FPGA prototype - implement and verify your design on FPGAs.

There are some unique challenges in translating MATLAB to hardware. MATLAB code is procedural and can be highly abstract; it can use floating-point data and has no notion of time. Complex loops can be inferred from matrix operations and toolbox functions.

Implementing MATLAB code in hardware involves:

  • Converting floating-point MATLAB code to fixed-point MATLAB code with optimized bit widths suitable for efficient hardware generation.
  • Identifying and mapping procedural constructs to concurrent area- and speed-optimized hardware operations.
  • Introducing the concept of time by adding clocks and clock rates to schedule the operations in hardware.
  • Creating resource-shared architectures to implement expensive operators like multipliers and for-loop bodies.
  • Mapping large persistent arrays to block RAM in hardware

HDL Coder™ simplifies the above tasks though workflow automation.

Example MATLAB Algorithm

Let’s take a MATLAB function implementing histogram equalization and go through this workflow. This algorithm, implemented in MATLAB, enhances image contrast by transforming the values in an intensity image so that the histogram of the output image is approximately flat.

type mlhdlc_heq.m

% Histogram Equalization Algorithm
function [pixel_out] = mlhdlc_heq(x_in, y_in, pixel_in, width, height)
persistent histogram
persistent transferFunc
persistent histInd
persistent cumSum
if isempty(histogram)
    histogram = zeros(1, 2^8);
    transferFunc = zeros(1, 2^8);
    histInd = 0;
    cumSum = 0;
% Figure out indices based on where we are in the frame
if y_in < height && x_in < width % valid pixel data
    histInd = pixel_in + 1;
elseif y_in == height && x_in == 0 % first column of height+1
    histInd = 1;
elseif y_in >= height % vertical blanking period
    histInd = min(histInd + 1, 2^8);
elseif y_in < height % horizontal blanking - do nothing
    histInd = 1;
%Read histogram
histValRead = histogram(histInd);
%Read transfer function
transValRead = transferFunc(histInd);
%If valid part of frame add one to pixel bin and keep transfer func val
if y_in < height && x_in < width
    histValWrite = histValRead + 1; %Add pixel to bin
    transValWrite = transValRead; %Write back same value
    cumSum = 0;
elseif y_in >= height %In blanking time index through all bins and reset to zero
    histValWrite = 0;
    transValWrite = cumSum + histValRead;
    cumSum = transValWrite;
    histValWrite = histValRead;
    transValWrite = transValRead;
%Write histogram
histogram(histInd) = histValWrite;
%Write transfer function
transferFunc(histInd) = transValWrite;
pixel_out = transValRead;

Example MATLAB Test Bench

Here is the test bench that verifies that the algorithm works with an example image. (Note that this testbench uses Image Processing Toolbox functions for reading the original image and plotting the transformed image after equalization.)

type mlhdlc_heq_tb.m

%% Test bench for Histogram Equalization Algorithm
clear mlhdlc_heq;
testFile = 'office.png';
RGB = imread(testFile);
% Get intensity part of color image
YCBCR = rgb2ycbcr(RGB);
imgOrig = YCBCR(:,:,1);
[height, width] = size(imgOrig);
imgOut = zeros(height,width);
hBlank = 20;
% make sure we have enough vertical blanking to filter the histogram
vBlank = ceil(2^14/(width+hBlank));
for frame = 1:2
    disp(['working on frame: ', num2str(frame)]);
    for y_in = 0:height+vBlank-1
        %disp(['frame: ', num2str(frame), ' of 2, row: ', num2str(y_in)]);
        for x_in = 0:width+hBlank-1
            if x_in < width && y_in < height
                pixel_in = double(imgOrig(y_in+1, x_in+1));
                pixel_in = 0;
             [pixel_out] = mlhdlc_heq(x_in, y_in, pixel_in, width, height);
             if x_in < width && y_in < height
                 imgOut(y_in+1,x_in+1) = pixel_out;
% Make color image from equalized intensity image
% Rescale image
imgOut = double(imgOut);
imgOut(:) = imgOut/max(imgOut(:));
imgOut = uint8(imgOut*255);
YCBCR(:,:,1) = imgOut;
RGBOut = ycbcr2rgb(YCBCR);
subplot(2,2,1); imshow(RGB, []);
title('Original Image');
subplot(2,2,2); imshow(RGBOut, []);
title('Equalized Image');
subplot(2,2,3); hist(double(imgOrig(:)),2^14-1);
title('Histogram of original Image');
subplot(2,2,4); hist(double(imgOut(:)),2^14-1);
title('Histogram of equalized Image');

Let's simulate this algorithm to see the results.

working on frame: 1
working on frame: 2

HDL Workflow Advisor

The HDL Workflow Advisor (see the snapshot below) helps automate the steps and provides a guided path from MATLAB to hardware. You can see the following key steps of the workflow in the left pane of the workflow advisor:

  1. Fixed-Point Conversion
  2. HDL Code Generation
  3. HDL Verification
  4. HDL Synthesis and Analysis

Let's look at each workflow step in detail.

Fixed-Point Conversion

Signal processing applications are typically implemented using floating-point operations in MATLAB. However, for power, cost, and performance reasons, these algorithms need to be converted to use fixed-point operations when targeting hardware. Fixed-point conversion can be very challenging and time-consuming, typically demanding 25 to 50 percent of the total design and implementation time. The automatic floating-point to fixed-point conversion workflow in HDL Coder™ can greatly simplify and accelerate this conversion process.

The floating-point to fixed-point conversion workflow consists of the following steps:

  1. Verify that the floating-point design is compatible with code generation.
  2. Propose fixed-point types based on computed ranges, either through the simulation of the testbench or through static analysis that propagates design ranges to compute derived ranges for all the variables.
  3. Generate fixed-point MATLAB code by applying proposed fixed-point types.
  4. Verify the generated fixed-point code and compare the numerical accuracy of the generated fixed-point code with the original floating point code.

Note that this step is optional. You can skip this step if your MATLAB design is already implemented in fixed-point.

HDL Code Generation

The HDL Code Generation step generates HDL code from the fixed-point MATLAB code. You can generate either VHDL or Verilog code that implements your MATLAB design. In addition to generating synthesizable HDL code, HDL Coder™ also generates various reports, including a traceability report that helps you navigate between your MATLAB code and the generated HDL code, and a resource utilization report that shows you, at the algorithm level, approximately what hardware resources are needed to implement the design, in terms of adders, multipliers, and RAMs.

During code generation, you can specify various optimization options to explore the design space without having to modify your algorithm. In the Design Space Exploration and Optimization Options section below, you can see how you can modify code generation options and optimize your design for speed or area.

HDL Verification

Standalone HDL test bench generation:

HDL Coder™ generates VHDL and Verilog test benches from your MATLAB scripts for rapid verification of generated HDL code. You can customize an HDL test bench using a variety of options that apply stimuli to the HDL code. You can also generate script files to automate the process of compiling and simulating your code in HDL simulators. These steps help to ensure the results of MATLAB simulation match the results of HDL simulation.

HDL Coder™ also works with HDL Verifier to automatically generate two types of cosimulation testbenches:

  • HDL cosimulation-based verification works with Mentor Graphics® ModelSim® and QuestaSim®, where MATLAB and HDL simulation happen in lockstep.
  • FPGA-in-the-Loop simulation allows you to run a MATLAB simulation with an FPGA board in strict synchronization. You can use MATLAB to feed real world data into your design on the FPGA, and ensure that the algorithm will behave as expected when implemented in hardware.

HDL Synthesis

Apart from the language-related challenges, programming for FPGAs requires the use of complex EDA tools. Generating a bitstream from the HDL design and programming the FPGA can be daunting tasks. HDL Coder™ provides automation here, by creating project files for Xilinx® and Altera® that are configured with the generated HDL code. You can use the workflow steps to synthesize the HDL code within the MATLAB environment, see the results of synthesis, and iterate on the MATLAB design to improve synthesis results.

Design Space Exploration and Optimization Options

HDL Coder™ provides the following optimizations to help you explore the design space trade-offs between area and speed. You can use these options to explore various architectures and trade-offs without having to manually rewrite your algorithm.

Speed Optimizations

  • Pipelining : To improve the design’s clock frequency, HDL Coder enables you to insert pipeline registers in various locations within your design. For example, you can insert registers at the design inputs and outputs, and also at the output of a given MATLAB variable in your algorithm.
  • Distributed Pipelining : HDL Coder also provides an optimization based on retiming to automatically move pipeline registers you have inserted to maximize clock frequency, by minimizing the delay through combinational paths in your design.

Area Optimizations

  • RAM mapping: HDL Coder™ maps matrices to wires or registers in hardware. If persistent matrix variables are mapped to registers, they can take up a large amount of FPGA area. HDL Coder™ automatically maps persistent matrices to block RAM to improve area efficiency. The challenge in mapping MATLAB matrices to block RAM is that block RAM in hardware typically has a limited set of read and write ports. HDL Coder™ solves this problem by automatically partitioning and scheduling the matrix reads and writes to honor the block RAM’s port constraints, while still honoring the other control- and data-dependencies in the design.
  • Resource sharing: This optimization identifies functionally equivalent multiplier operations in MATLAB code and shares them. You can control the amount of multiplier sharing in the design.
  • Loop streaming: A MATLAB for-loop creates a FOR_GENERATE loop in VHDL. The body of the loop is replicated as many times in hardware as the number of loop iterations. This results in an inefficient use of area. The loop streaming optimization creates a single hardware instance of the loop body that is time-multiplexed across loop iterations.
  • Constant multiplier optimization: This design level optimization converts constant multipliers into shift and add operations using canonical signed digit (CSD) techniques.

Best Practices

Now, let's look at few best practices related to writing MATLAB code when targeting FPGAs.

When writing a MATLAB design:

  • Use the code generation subset of MATLAB supported for HDL code generation.
  • Keep the top-level interface as simple as possible. The top-level function size, types, and complexity determine the interface of the chip implemented in hardware.
  • Do not pass in a big chunk of parallel data into the design. Parallel data requires a large number of IO pins on the chip, and would probably not be synthesizable. In a typical image processing design, you should serialize the pixels as inputs and buffer them internally in the algorithm.

When writing a MATLAB test bench:

  • Call the design from the testbench function.
  • Exercise the design thoroughly. This is particularly important for floating-point to fixed-point conversion, where HDL Coder™ determines the ranges of the variables in the algorithm based on the values the testbench assigns to the variables. You can reuse this testbench to generate an HDL testbench for testing the generated hardware.
  • Simulate the design with the testbench prior to code generation to make sure there are no simulation errors, and to make sure all the required files are on the path.


HDL Coder™ provides a seamless workflow when you want to implement your algorithm in an FPGA. In this post, I have shown you how to take an image processing algorithm written in MATLAB, convert it to fixed-point, generate HDL code, verify the generated HDL code using the test bench, and finally, synthesize the design and implement it in hardware.

See this article about how one of the HDL Coder customers, FLIR has used MATLAB to HDL workflow to achieve good results. You can also learn more about this workflow using the product examples located here.

We hope this brief introduction to the HDL Coder™ and MATLAB-to-HDL code generation, verification framework has shown how you can quickly get started on implementing your MATLAB designs and target FPGAs. Please let us know in the comments for this post how you might use this new functionality. Or, if you've already tried using HDL Coder™, let us know about your experiences here.

Get the MATLAB code

Published with MATLAB® R2013a


Comments are closed.

16 CommentsOldest to Newest

Eric replied on : 1 of 16
Kiran and/or Loren, do you think there might be a way to make the declaration/definition of persistent variables cleaner? I've seen the 'isempty()' form mentioned as the first step in converting many existing user functions for MATLAB Coder, HDL Coder etc, and was wondering if there was the possibility of introducing some new function/syntax to help encapsulate these steps... Similar to onCleanup, what about an onFirstRun() function or something?
(Brett Shoelson) Nice blog post Kiran—well organized, well written. I learned a lot reading it. One thought, though: it would be very useful to hyperlink this sentence to a list of supported commands, if one exists: "Use the code generation subset of MATLAB supported for HDL code generation." (Kiran Kintali) Hi Brett, Thanks for the comments. Here is the link for MATLAB language supported in HDLCoder. https://www.mathworks.com/help/hdlcoder/matlab-language-support.html
Andrew replied on : 4 of 16
Is this R2013a only? I tried to get more information on HDL coder but MathWorks' website locks me out from even seeing the documentation ("To see documentation for this product, you need to have the product on your license."): https://www.mathworks.com/help/coder/index.html What's the deal with that? That's awful unfriendly and certainly not going to get anyone to actually add an HDL Coder license. Also, I see from another page that HDL Coder isn't supported on Mac OS X. This isn't mentioned in this article. Please always indicate when you are talking about features that are only supported for a subset of your users. When is Matlab going to provide parity for it's FPGA and embedded functionality across platforms? I am interested in this and I imagine that others would be too. Currently, I use other tools and methods, but would really like to be able to take advantage of the power and flexibility of Matlab.
Kiran Kintali replied on : 5 of 16
Andrew, Thanks for the comments. HDLCoder is supported on windows(32,64bit), linux(64bit) platforms only. https://www.mathworks.com/products/availability/index.html#HD The primary reason was most of the RTL simulators and synthesis tools are supported on these platforms. However, support for mac is on our radar and hope to provide support soon. I will talk to my documentation team regarding unavailability of the product doc on the website without license.
Andrew replied on : 6 of 16
Thanks Kiran. >The primary reason was most of the RTL simulators and synthesis tools are supported on these platforms. That's exactly why I'd like Matlab to support as much of the process as possible in the OS that I use for all of the rest of my work. The OEMs aren't likely to do it any time soon it seems and their software is for the most part terrible even from a Windows standpoint anyways -they should really stick to hardware. If you can provide a Matlab-based development-level / research-level solution that would be really great.
Kiran Kintali replied on : 7 of 16
Thanks for the comment. I have entered an enhancement request for supporting MAC platform.
Eric replied on : 8 of 16
Any chance of supporting the Mojo $99 FPGA development board, http://embeddedmicro.com/, similar to how the Simulink Coder has targets for some low-end devices to help students etc learn to use the toolchain?
Kiran Kintali replied on : 9 of 16
Hi Eric, This is a great suggestion. Can you please create an enhancement request for the product? HDL Coder currently includes a set of predefined FPGA boards you can use with the Turnkey or FPGA-in-the-Loop workflows. We plan to provide the same capability in MATLAB to HDL workflow in the coming release. With an in-built FPGA Board Manager, you can add additional boards to use with the MATLAB to HDL workflow discussed in this post. All you need to add a board is the relevant information from the board specification documentation. The FPGA Board Manager helps you through the steps necessary to create a custom board configuration. We can provide pre-defined configuration file for this http://embeddedmicro.com/ board.
Marco replied on : 10 of 16
Is it possible to translate in VHDL functions where the inputs are vectors or matrices and then make some operations? For examples: function x = mul (a,b) x = a * b; end where x, a, b are matrices? If yes, where I can find some examples? Thanks.
Kiran Kintali replied on : 11 of 16
Hi Marco, MATLAB to HDL workflow supports the following basic data types [u]int8/16/32, fixed-point, logical data types. It also supports complex, vector (unordered, row, column vectors), matrix (not n-D and variable dimensions) data types and structure data types. For the top level entry-point function restrictions apply. Matrix and structure types are not supported at the top level. You need to stream the data into the function as scalars or vectors and buffer and reshape the data inside the function. I have written this article sometime back to show how to do this. You might find this interesting. https://www.mathworks.com/company/newsletters/articles/converting-matlab-algorithms-into-serialized-designs-for-hdl-code-generation.html You can also see several shipping product examples. >> mlhdlc_tutorial_corner_detection.m
Pavan replied on : 13 of 16
Hey, Is there a way through which i can add ports into the HDL file generated using HDL workflow advisor in MATLAB and assign a value to it. I need it to verify the output of the design in FPGA. Thank You
Kiran Kintali replied on : 14 of 16
Hi Pavan, Can you give us more details about your request? What do you eman by adding ports to HDL file? Have you seen the example? "Verify Sobel Edge Detection Algorithm in MATLAB-to-HDL Workflow" It illustrates how to debug algorithm written in MATLAB in the loop with FPGA in MATLAB to HDL workflow advisor. >> which cosim_fil_sobel.m
eshaan verma` replied on : 15 of 16
" hi kiran I am using the above mentioned code using hdlcoder in MATLAB 2012a but unfortunately i am getting error in MATLAB to hdl workflow in which at the first stage of code generation its give this error. ### MESSAGE: The design requires 2 times faster clock with respect to the base rate = 1. ### Begin VHDL Code Generation ### Working on mlhdlc_heq_FixPt/histogram_ram/DualPortRAM_256x10b as DualPortRAM_256x10b.vhd ### Working on mlhdlc_heq_FixPt/histogram_ram as histogram_ram.vhd ### Working on mlhdlc_heq_FixPt/transferFunc_ram/DualPortRAM_256x15b as DualPortRAM_256x15b.vhd ### Working on mlhdlc_heq_FixPt/transferFunc_ram as transferFunc_ram.vhd ### Working on mlhdlc_heq_FixPt_tc as mlhdlc_heq_FixPt_tc.vhd ### Working on as mlhdlc_heq_FixPt_enb_bypass.vhd ### Working on mlhdlc_heq_FixPt as mlhdlc_heq_FixPt.vhd ### Generating package file mlhdlc_heq_FixPt_pkg.vhd ### The DUT requires an initial pipeline setup latency. Each output port experiences these additional delays ### Output port 0: 4 cycles ### Output port 1: 4 cycles ### Generating Resource Utilization Report resource_report.html Error occurred when running post codegeneration tasks Error: failed to run post code generation tasks: MATLAB:save:invalidVarName 'drive\9718540749\asif\fpga\codegen\mlhdlc_heq\hdlsrc\codegen_info.mat' is not a valid variable name. Use help codegen for more information on using this command. please help me come out of this problem Regards Eshaan
Kiran Kintali replied on : 16 of 16
Hi eshaan, Can you please contact the tech support with this issue? I wonder if you are running into folder name related issue (with spaces and/or long path names). I do not see this error with the current version of the software (13a).