main content

basic hdl code generation and fpga synthesis from matlab -凯发k8网页登录

this example shows how to create a hdl coder™ project, generate code for your matlab® design, and synthesize the hdl code. in this example, you:

  1. create a matlab hdl coder project.

  2. add the design and test bench files to the project.

  3. start the hdl workflow advisor for the matlab design.

  4. run fixed-point conversion and hdl code generation.

  5. generate a hdl test bench from the matlab test bench.

  6. verify the generated hdl code by using a hdl simulator. this example uses modelsim® as the tool.

  7. synthesize the generated hdl code by using a synthesis tool. this example uses xilinx® vivado® as the tool.

fir filter matlab design

the matlab design mlhdlc_sfir is a simple symmetric fir filter.

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

review the matlab design.

open(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-2019 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

fir filter matlab test bench

a matlab testbench mlhdlc_sfir_tb exercises the filter design by using a representative input range. review the matlab test bench mlhdlc_sfir_tb.

open(testbench_name);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matlab test bench for the fir filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   凯发官网入口首页 copyright 2011-2019 the mathworks, inc.
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])

test the original matlab algorithm

to avoid run-time errors, simulate the design by using the test bench.

mlhdlc_sfir_tb

set up hdl simulator and synthesis tool path

if you want to synthesize the generated hdl code, before you use hdl coder to generate code, set up your synthesis tool path. to set up the path to your synthesis tool, use the hdlsetuptoolpath function. for example, if your synthesis tool is xilinx vivado:

hdlsetuptoolpath('toolname','xilinx vivado','toolpath',...
               'c:\xilinx\vivado\2020.2\bin\vivado.bat');

you must have already installed xilinx vivado. to check your xilinx vivado synthesis tool setup, launch the tool by running this command:

!vivado

if you want to simulate the generated hdl code by using a hdl test bench, you can use an hdl simulator such as modelsim®. you must have already installed the hdl simulator.

create an hdl coder project

to create an hdl coder project:

1. create a project by running this command:

coder -hdlcoder -new sfir_project

2. for matlab function, add the matlab design mlhdlc_sfir. add mlhdlc_sfir_tb.m as the matlab test bench.

3. click autodefine types and use the recommended types for the matlab design. the code generator infers data types by running the test bench.

create fixed-point versions of algorithm and test bench

  1. click the workflow advisor button to open the workflow advisor. you see that the define input types task has passed.

  2. run the fixed-point conversion task. the fixed-point conversion tool opens in the right pane.

when you run fixed-point conversion, to propose fraction lengths for floating-point data types, hdl coder uses the default word length. in this tutorial, the default word length is 14. the advisor provides a default safety margin for simulation min/max of 0%. the advisor adjusts the range of the data by this safety factor. for example, a value of 4 specifies that you want a range of at least 4 percent larger. see also .

select code generation options and generate hdl code

before you generate hdl code, if you want to deploy the code onto a target platform, specify the synthesis tool. in the code generation target task, leave workflow to generic asic/fpga and specify xilinx vivado as the synthesis tool. if you don't see the synthesis tool, click refresh list. run this task.

in the hdl code generation task, by using the tabs on the right side of this task, you can specify additional code generation options.

  1. by default, hdl coder generates vhdl® code. to generate verilog code, in the target tab, choose verilog as the language.

  2. to generate a code generation report with comments and traceability links, in the coding style tab, select include matlab source code as comments and generate report.

  3. to optimize your design, you can use the distributed pipelining optimization. in the optimizations tab, specify 1 for input pipelining and output pipelining and then select distribute pipeline registers. to learn more, see .

  4. click run to generate verilog code.

examine the log window and click the links to explore the generated code and the reports.

generate hdl test bench and simulate the generated code

hdl coder generates a hdl test bench, runs the hdl test bench by using a hdl simulator, and verifies whether the hdl simulation matches the numerics and latency of the fixed-point matlab simulation.

to generate a hdl test bench and simulate the generated code, in the hdl verification > verify with hdl test bench task:

  1. in the output settings tab, select generate hdl test bench.

  2. to simulate the generated test bench, set the simulation tool to modelsim. you must have already installed modelsim.

  3. to specify generation of hdl test bench code and test bench data in separate files, in the test bench options tab, select multi-file test bench.

  4. click the run button.

the task generates an hdl test bench, then simulates the fixed-point design by using the selected simulation tool, and generates a compilation report and a simulation report.

synthesize generated hdl code

hdl coder synthesizes the hdl code on the target platform and generates area and timing reports for your design based on the target device that you specify.

to synthesize the generated hdl code:

1. run the create project task.

this task creates a xilinx vivado synthesis project for the hdl code. hdl coder uses this project in the next task to synthesize the design.

2. select and run the run synthesis task.

this task launches the synthesis tool in the background, opens the synthesis project, compiles the hdl code, synthesizes the design, and generates netlists and area and timing reports.

3. select and run the run implementation task.

this task launches the synthesis tool in the background, runs place and route on the design, and generates pre- and post-route timing information for use in critical path analysis and back annotation of your source model.

网站地图