Global Sources
EE Times-India
 
EE Times-India > EDA/IP
 
 
EDA/IP  

How to generate C code automatically

Posted: 13 Sep 2011     Print Version  Bookmark and Share

Keywords:C code  C++  MATLAB 

As an automatic MATLAB to C code generation tool, MATLAB Coder enables engineers to specify these requirements and make implementation decisions directly within MATLAB. This allows engineers to maintain one reference design and iterate on the evolving design in MATLAB.

The following sections of this article describe the recommended three-step workflow that enables engineers to specify the implementation details that are needed to convert their MATLAB algorithms into the desired C code and verify the resulting code against their original MATLAB specification. This workflow is illustrated in figure 1.

 Three-step workflow

Figure 1: Three-step workflow.

Code preparation
The first step in the code generation process is to prepare your MATLAB algorithms by introducing implementation details and ensuring your code is using the subset of the MATLAB language that is supported for code generation. This subset includes basic operators, numerical computations, standard programming constructs, and hundreds of built-in functions.

Implementation details are required to prepare MATLAB code for code generation because MATLAB is polymorphic–the behaviour of its operators and functions adapts to the nature of the input arguments they are operating upon.

A result that can be achieved with a few lines of MATLAB may require tens or hundreds of lines of C code. For example, a simple function that multiplies two inputs of any size and data type is a single line of MATLAB code. When coding this in C, the engineer must take into account the size and data types of the inputs as well as whether they are real or complex. This is illustrated in the figure 2.

 coding in C

Figure 2: When coding in C, the engineer must take into account the size and data types of the inputs as well as whether they are real or complex.

Polymorphism is one reason that MATLAB is a versatile and convenient language for exploring design ideas. When writing C code by hand or generating it with MATLAB Coder, however, engineers must explicitly specify the input sizes and data types for their algorithms. In MATLAB Coder, this can be done through the command line interface or the project UI as show in figure 3.

 writing C code

 writing C code

Figure 3: When writing C code by hand or generating it with MATLAB Coder, however, engineers must explicitly specify the input sizes and data types for their algorithms.

For the same reason, certain local variables within the algorithm code in MATLAB have to be pre-allocated so the code generation engine knows the sizes and the data types of the variables to be used in the generated code.

Another key step in the preparation process is to check (cheque for banks) if the MATLAB algorithm code uses only those language features and constructs that support code generation. The product documentation lists all supported functions and language features.

Test and verification
In the traditional workflow, verifying that the hand-written C/C++ code functionally matches the original MATLAB algorithms is a very important and challenging task. Two common approaches are used to perform this functional verification:
� Maintain separate file-based test benches, and compare the test results for the MATLAB and C implementations
� Integrate the hand-written C code using the MEX API specification so that it may be directly called from within MATLAB, and use test benches in MATLAB to exercise the MEX function and verify its result

 First Page Previous Page 1 • 2 • 3 Next Page Last Page



Comment on "How to generate C code automatically"
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top