basic fir filter
note
the filter design hdl coder™ product will be discontinued in a future release. instead, you can model hardware behavior, and generate hdl code by using system objects or simulink® blocks from dsp hdl toolbox™. these objects and blocks include hardware-friendly control signals and architecture options. to generate hdl code from dsp hdl toolbox objects and blocks, you must also have the hdl coder™ product.
for examples of modeling an fir filter for hardware, see fully parallel systolic fir filter implementation (dsp hdl toolbox) and (dsp hdl toolbox). these examples use the (dsp hdl toolbox) block. equivalent functionality is also available in the (dsp hdl toolbox) system object™.
create a folder for your tutorial files
set up a writable working folder outside your matlab® installation
folder to store files that will be generated as you complete your
tutorial work. the tutorial instructions assume that you create the
folder hdlfilter_tutorials on drive c.
design a fir filter in filter designer
this section assumes that you are familiar with the matlab user interface and the filter designer. these instructions guide you through designing and creating a basic fir filter using filter designer.
start the matlab software.
set your current folder to the folder you created in create a folder for your tutorial files.
start filter designer by entering the
filterdesignercommand in the matlab command window. the filter design & analysis tool appears.
in the filter design & analysis tool, check that these filter options are set.
option value response type lowpassdesign method fir equiripplefilter order minimum order options density factor: 20frequency specifications units:
hzfs:
48000fpass:
9600fstop:
12000magnitude specifications units:
dbapass:
1astop:
80these settings are for the default filter design that the filter designer creates for you. if you do not have to change the filter, and design filter is grayed out, you are done and can skip to quantize the filter.
if you modified options listed in step 4, click design filter. the filter designer creates a filter for the specified design and displays this message in the filter designer status bar when the task is complete.
designing filter... done
for more information on designing filters with the filter designer, see the dsp system toolbox™ documentation.
quantize the filter
you must quantize filters for hdl code generation. to quantize your filter,
open the basic fir filter design you created in design a fir filter in filter designer.
click the set quantization parameters button
in the left-side toolbar. the filter
designer displays a filter arithmetic menu in the
bottom half of the window. 
select
fixed-pointfrom the filter arithmetic list. then selectspecify allfrom the filter precision list. the filter designer displays the first of three tabbed panels of quantization parameters across the bottom half of the window.
use the quantization options to test the effects of various settings on the performance and accuracy of the quantized filter.
set the quantization parameters as follows:
tab parameter setting coefficients numerator word length 16best-precision fraction lengths selecteduse unsigned representation clearedscale the numerator coefficients to fully utilize the entire dynamic range clearedinput/output input word length 16input fraction length 15output word length 16filter internals rounding mode flooroverflow mode saturateaccum. word length 40click apply.
for more information on quantizing filters with the filter designer, see the dsp system toolbox documentation.
configure and generate vhdl code
after you quantize your filter, you are ready to configure coder options and generate vhdl code for the filter. this section guides you through starting the filter design hdl coder ui, setting options, and generating the vhdl code and test bench for the basic fir filter you designed and quantized in design a fir filter in filter designer and quantize the filter.
start the filter design hdl coder ui by selecting targets > generate hdl in the filter designer tool. the filter designer displays the generate hdl tool.

find the filter design hdl coder online help.
in the matlab window, click the help button in the toolbar or click help > product help.
in the contents pane of the help browser, select the filter design hdl coder entry.
minimize the help browser.
in the generate hdl tool, click the help button. a small context-sensitive help window opens. the window displays information about the tool.
close the help window.
place your cursor over the folder label or text box in the target pane of the generate hdl tool, and right-click. a what's this? button appears.

click what's this? the context-sensitive help window displays information describing the folder option. configure the contents and style of the generated hdl code, using the context-sensitive help to get more information as you work. a help topic is available for each option.
in the name text box of the target pane, replace the default name with
basicfir. this option names the vhdl entity and the file that contains the vhdl code for the filter.
select the global settings tab of the ui. then select the general tab of the additional settings section of the ui. type
tutorial - basic fir filterin the comment in header text box. the coder adds the comment to the end of the header comment block in each generated file.
select the ports tab of the additional settings section of the ui.

change the names of the input and output ports. in the input port text box, replace
filter_inwithdata_in. in the output port text box, replacefilter_outwithdata_out.
clear the check box for the add input register option. the ports pane now looks like this figure.

click the test bench tab in the generate hdl tool. in the file name text box, replace the default name with
basicfir_tb. this option names the generated test bench file.
click generate to start the code generation process.
the coder displays messages in the matlab command window as it generates the filter and test bench vhdl files:
### starting vhdl code generation process for filter: basicfir ### generating: c:\hdlfilter_tutorials\hdlsrc\basicfir.vhd ### starting generation of basicfir vhdl entity ### starting generation of basicfir vhdl architecture ### hdl latency is 2 samples ### successful completion of vhdl code generation process for filter: basicfir ### starting generation of vhdl test bench ### generating input stimulus ### done generating input stimulus; length 3429 samples. ### generating test bench: c:\hdlfilter_tutorials\hdlsrc\basicfir_tb.vhd ### please wait ... ### done generating vhdl test bench
as the messages indicate, the coder creates the folder
hdlsrcunder your current working folder and places the filesbasicfir.vhdandbasicfir_tb.vhdin that folder.observe that the messages include hyperlinks to the generated code and test bench files. by clicking these hyperlinks, you can open the code files directly into the matlab editor.
the generated vhdl code has these characteristics:
vhdl entity named
basicfir.registers that use asynchronous resets when the reset signal is active high (1).
the table shows the names of the ports.
vhdl port name input data_inoutput data_outclock input clkclock enable input clk_enablereset input resetan extra register for handling filter output.
clock input, clock enable input, and reset ports are of type
std_logicand data input and output ports are of typestd_logic_vector.coefficients are named
coeff, wherennis the coefficient number, starting with 1.type-safe representation is used when zeros are concatenated:
'0' & '0'...registers are generated with the statement
elsif clk'event and clk='1' thenrather than with therising_edgefunction.the postfix
'_process'is appended to process names.
the generated test bench:
is a portable vhdl file.
forces clock, clock enable, and reset input signals.
forces the clock enable input signal to active high.
drives the clock input signal high (1) for 5 nanoseconds and low (0) for 5 nanoseconds.
forces the reset signal for two cycles plus a hold time of 2 nanoseconds.
applies a hold time of 2 nanoseconds to data input signals.
for a fir filter, applies impulse, step, ramp, chirp, and white noise stimulus types.
when you have finished generating code, click close to close the generate hdl tool.
explore the generated vhdl code
get familiar with the generated vhdl code by opening and browsing through the file
basicfir.vhd in an ascii or hdl simulator editor.
open the generated vhdl filter file
basicfir.vhd.search for
basicfir. this line identifies the vhdl module, using the value you specified for the name option in the target pane. see step 5 in configure and generate vhdl code.search for
tutorial. this section is where the coder places the text you entered for the comment in header option. see step 10 in configure and generate vhdl code.search for
hdl code. this section lists coder options you modified in configure and generate vhdl code.search for
filter settings. this section describes the filter design and quantization settings as you specified in design a fir filter in filter designer and quantize the filter.search for
entity. this line names the vhdl entity, using the value you specified for the name option in the target pane. see step 5 in configure and generate vhdl code.search for
port. thisportdeclaration defines the clock, clock enable, reset, and data input and output ports. the ports for clock, clock enable, and reset signals are named with default character vectors. the ports for data input and output are named as you specified on the input port and output port options on the ports tab of the generate hdl tool. see step 12 in configure and generate vhdl code.search for
constants. this section defines the coefficients. they are named using the default naming scheme,coeff, wherennis the coefficient number, starting with 1.search for
signals. this section of code defines the signals for the filter.search for
process. theprocessblock namedelay_pipeline_processincludes the defaultprocessblock postfix'_process'.search for
if reset. this code asserts the reset signal. the default, active high (1), was specified. also note that theprocessblock applies the default asynchronous reset style when generating vhdl code for registers.search for
elsif. this code checks for rising edges when the filter operates on registers. the defaultelsif clk'eventstatement is used instead of the optionalrising_edgefunction.search for
output_register. this section of code writes the filter data to an output register. code for this register is generated by default. in step 13 in configure and generate vhdl code, you cleared the add input register option, but left the add output register selected. also note that theprocessblock nameoutput_register_processincludes the defaultprocessblock postfix'_process'.search for
data_out. this section of code drives the output data of the filter.
verify the generated vhdl code
this section explains how to verify the generated vhdl code for the basic fir filter with the generated vhdl test bench. this tutorial uses the mentor graphics® modelsim® software as the tool for compiling and simulating the vhdl code. you can also use other vhdl simulation tool packages.
to verify the filter code, complete these steps:
start your mentor graphics modelsim simulator.
set the current folder to the folder that contains your generated vhdl files. for example:

if desired, create a design library to store the compiled vhdl entities, packages, architectures, and configurations. in the mentor graphics modelsim simulator, you can create a design library with the
vlibcommand.
compile the generated filter and test bench vhdl files. in the mentor graphics modelsim simulator, you compile vhdl code with the
vcomcommand. these commands compile the filter and filter test bench vhdl code.vcom basicfir.vhd vcom basicfir_tb.vhd
this screen display shows the command sequence and informational messages displayed during compilation.

load the test bench for simulation. the procedure for loading the test bench varies depending on the simulator you are using. in the mentor graphics modelsim simulator, you load the test bench for simulation with the
vsimcommand. for example:vsim work.basicfir_tb
this figure shows the results of loading
work.basicfir_tbwith thevsimcommand.
open a display window for monitoring the simulation as the test bench runs. in the mentor graphics modelsim simulator, use this command to open a wave window and view the results of the simulation as hdl waveforms.

this wave window opens.

to start running the simulation, issue the start simulation command for your simulator. for example, in the mentor graphics modelsim simulator, you can start a simulation with the
runcommand.this figure shows starting a simulation with the
run -allcommand.
as your test bench simulation runs, watch for error messages. if error messages appear, interpret them as they pertain to your filter design and the hdl code generation options you selected. determine whether the results are expected based on the customizations you specified when generating the filter vhdl code.
this wave window shows the simulation results as hdl waveforms.
