## HDL Coder |

This example shows how to work with MATLAB® HDL Coder™ projects.

On this page… |
---|

Create a New Folder and Copy Relevant Files Create a New HDL Coder Project Specify the Design and the Test Bench Launch the Workflow Advisor from the Build Tab |

This example helps you familiarize yourself with the following aspects of MATLAB HDL Coder™ projects.

Creating a New MATLAB HDL Coder project

Adding design and testbench files to the project

Launching the HDL workflow advisor for MATLAB

Running code generation steps

At the end of each section take a note of the UI menus and windows and perform any tasks required in the example before moving to the next steps.

This is an introductory example that shows various UI elements of MATLAB HDL Coder project workflow. For more advanced coding examples and other workflow tasks refer to other MATLAB HDL Coder examples.

The MATLAB code used in the example is a simple symmetric FIR filter. The example also shows a MATLAB testbench that exercises the filter.

design_name = 'mlhdlc_sfir'; testbench_name = 'mlhdlc_sfir_tb';

Let us take a look at the MATLAB design

type(design_name);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATLAB design: Symmetric FIR Filter % % Introduction: % % We can reduce the complexity of the FIR filter by leveraging its symmetry. % Symmetry for an n-tap filter implies, coefficient h0 = coefficient hn-1, % coefficient, h1 = coefficient hn-2, etc. In this case, the number of % multipliers can be approximately halved. The key is to add the % two data values that need to be multiplied with the same coefficient % prior to performing the multiplication. % % Key Design pattern covered in this example: % (1) Filter states represented using the persistent variables % (2) Filter coefficients passed in as parameters % % % Copyright 2011 The MathWorks, Inc. %#codegen function [y_out, delayed_xout] = mlhdlc_sfir(x_in, h_in1, h_in2, h_in3, h_in4) % Symmetric FIR Filter % declare and initialize the delay registers persistent ud1 ud2 ud3 ud4 ud5 ud6 ud7 ud8; if isempty(ud1) ud1 = 0; ud2 = 0; ud3 = 0; ud4 = 0; ud5 = 0; ud6 = 0; ud7 = 0; ud8 = 0; end % access the previous value of states/registers a1 = ud1 + ud8; a2 = ud2 + ud7; a3 = ud3 + ud6; a4 = ud4 + ud5; % multiplier chain m1 = h_in1 * a1; m2 = h_in2 * a2; m3 = h_in3 * a3; m4 = h_in4 * a4; % adder chain a5 = m1 + m2; a6 = m3 + m4; % filtered output y_out = a5 + a6; % delayout input signal delayed_xout = ud8; % update the delay line ud8 = ud7; ud7 = ud6; ud6 = ud5; ud5 = ud4; ud4 = ud3; ud3 = ud2; ud2 = ud1; ud1 = x_in; end

type(testbench_name);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATLAB test bench for the FIR filter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear mlhdlc_sfir; T = 2; dt = 0.001; N = T/dt+1; sample_time = 0:dt:T; df = 1/dt; sample_freq = linspace(-1/2,1/2,N).*df; % input signal with noise x_in = cos(2.*pi.*(sample_time).*(1+(sample_time).*75)).'; % filter coefficients h1 = -0.1339; h2 = -0.0838; h3 = 0.2026; h4 = 0.4064; len = length(x_in); y_out = zeros(1,len); x_out = zeros(1,len); for ii=1:len data = x_in(ii); % call to the design 'mlhdlc_sfir' that is targeted for hardware [y_out(ii), x_out(ii)] = mlhdlc_sfir(data, h1, h2, h3, h4); end figure('Name', [mfilename, '_plot']); subplot(3,1,1); plot(1:len,x_in,'-b'); xlabel('Time (ms)') ylabel('Amplitude') title('Input Signal (with noise)') subplot(3,1,2); plot(1:len,y_out,'-b'); xlabel('Time (ms)') ylabel('Amplitude') title('Output Signal (filtered)') freq_fft = @(x) abs(fftshift(fft(x))); subplot(3,1,3); semilogy(sample_freq,freq_fft(x_in),'-b'); hold on semilogy(sample_freq,freq_fft(y_out),'-r') hold off xlabel('Frequency (Hz)') ylabel('Amplitude (dB)') title('Input and Output Signals (Frequency domain)') legend({'FilterIn', 'FilterOut'}, 'Location','South') axis([-500 500 1 100])

**Create a New Folder and Copy Relevant Files**

Execute the following lines of code to copy the necessary example files into a temporary folder.

design_name = 'mlhdlc_sfir'; testbench_name = 'mlhdlc_sfir_tb'; mlhdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos', 'matlabhdlcoderdemos'); mlhdlc_temp_dir = [tempdir 'mlhdlc_sfir']; % Create a temporary folder and copy the MATLAB files. cd(tempdir); [~, ~, ~] = rmdir(mlhdlc_temp_dir, 's'); mkdir(mlhdlc_temp_dir); cd(mlhdlc_temp_dir); copyfile(fullfile(mlhdlc_demo_dir, [design_name,'.m*']), mlhdlc_temp_dir); copyfile(fullfile(mlhdlc_demo_dir, [testbench_name,'.m*']), mlhdlc_temp_dir);

It is always a good practice to simulate the design with the testbench prior to code generation to make sure there are no runtime errors.

mlhdlc_sfir_tb

**Create a New HDL Coder Project**

To create a new HDL Coder project, click the Apps tab. Under Code Generation, select 'HDL Coder'.

The selection opens the following dialog.

Change the 'Name:' of the project to 'sfir_project'

Click OK to close the dialog

Alternatively, you can also create a project from the MATLAB command prompt using the following command.

coder -hdlcoder -new sfir_project

The following HDL Code Generation UI panel will appear in the MATLAB desktop after the previous step.

In addition a new file called 'sfir_project.prj' is created in the current folder. This file holds all the project selections made in the UI.

**Specify the Design and the Test Bench**

Drag the file 'mlhdlc_sfir.m' from the Current Folder Browser into the Entry Points tab of the HDL Coder UI, under the "MATLAB Function" section.

Drag the file 'mlhdlc_sfir_tb.m' into the HDL Coder UI, under "MATLAB Test Bench" section.

Alternatively you can use the hyperlinks in the 'HDL Project UI Panel' to browse to the files and add them to the project.

You can leave the input argument types 'Undefined' for the MATLAB Function. The input types are automatically inferred from the MATLAB Test Bench.

**Launch the Workflow Advisor from the Build Tab**

Click on 'Workflow Advisor' button to bring up the MATLAB HDL Coder workflow advisor dialog.

The workflow advisor helps in:

Translating your floating-point MATLAB design to fixed-point design

Generating HDL code from the fixed-point MATLAB design

Automating the steps in driving simulation and synthesis tools

**Run Fixed-Point Conversion and HDL Code Generation**

Right click on the 'HDL Code Generation' step and choose the option 'Run to selected task' to run all the steps from the beginning through the HDL code generation.

The Workflow Advisor will transform your MATLAB design to fixed-point and then generate HDL from the fixed-point design.

Examine the generated fixed-point code from the floating-point design by clicking on the hyperlinks in the 'Type Validation Output' window to open the generated fixed-point MATLAB code in the MATLAB editor.

For more details on floating-point to fixed-point conversion refer to the tutorial Floating-Point to Fixed-Point Conversion

Examine the generated HDL code by clicking on the hyperlinks in the Code Generation Log window.

For more details on HDL code generation, simulation and synthesis steps refer to the tutorial HDL Code Generation Workflow

You can run the following commands to clean up the temporary project folder.

mlhdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos', 'matlabhdlcoderdemos'); mlhdlc_temp_dir = [tempdir 'mlhdlc_sfir']; clear mex; cd (mlhdlc_demo_dir); rmdir(mlhdlc_temp_dir, 's');