1
<page xmlns="http://projectmallard.org/1.0/"
5
<title>VHDL Using gHDL</title>
8
<link type="guide" xref="getting-started-tutorials"/>
11
<section>Overview</section>
12
<p>This tutorial covers how to get started with the gHDL simulator, using a simple counter example.</p>
14
<p>This tutorial uses the following software and versions, but should work with out any problems on both older an newer versions:</p>
17
<item><p>Gedit 2.28, Text editor</p></item>
18
<item><p>gHDL 0.28, VHDL compiler and simulator</p></item>
19
<item><p>GTKWave 3.2.0, Waveform viewer</p></item>
20
<item><p>make, to run helper script (optional)</p></item>
23
<section>Setup</section>
25
<p>For this tutorial we will use Gedit, the default text editor in Gnome, to edit the VHDL code. Ubuntu has a number of different text editors available as shown on the <link href="http://mbmn.net/uer/tools/editors/">HDL Editors</link> page.</p>
27
<p>To make Gedit easier to use we will use a number of plugins. Plugins are small extensions to Gedit to allow Gedit to do some extra tricks. Alternatively use external programs instead of the plugins if desired.</p>
29
<p>Start Gedit (Accessories -> Text Editor) and select Edit -> Preferences and click the Plugins Tab. Click the box next to Code Comment, File Browser Pane and Embedded Terminal to active these plugins.</p>
31
<p>In the Left side panel of Gedit click the File Browser tab, this usually has a filing cabinet icon. Use the file browser (or if you wish use another file browser) to create a directory for this project and create three sub-directories: src, testbench and simulation.</p>
33
<p>To make using GHDL easier to use copy the following Makefile into Gedit and save it to the project directory and as a file named Makefile.</p>
41
TESTBENCHPATH = testbench/${TESTBENCH}$(VHDLEX)
45
GHDL_FLAGS = --ieee=synopsys --warn-no-vital-generic
48
# Simulation break condition
49
#GHDL_SIM_OPT = --assert-level=error
50
GHDL_SIM_OPT = --stop-time=500ns
52
WAVEFORM_VIEWER = gtkwave
57
echo "Setting up project ${PROJECT}"
58
mkdir src testbench simulation
61
ifeq ($(strip $(TESTBENCH)),)
62
@echo "TESTBENCH not set. Use TESTBENCH=value to set it."
67
$(GHDL_CMD) -i $(GHDL_FLAGS) --workdir=simulation --work=work $(TESTBENCHPATH) $(FILES)
68
$(GHDL_CMD) -m $(GHDL_FLAGS) --workdir=simulation --work=work $(TESTBENCH)
69
@mv $(TESTBENCH) simulation/$(TESTBENCH)
72
@$(SIMDIR)/$(TESTBENCH) $(GHDL_SIM_OPT) --vcdgz=$(SIMDIR)/$(TESTBENCH).vcdgz
75
gunzip --stdout $(SIMDIR)/$(TESTBENCH).vcdgz | $(WAVEFORM_VIEWER) --vcd
78
$(GHDL_CMD) --clean --workdir=simulation</code>
80
<section>Writing Code</section>
82
<p>Create a new file and save it in the the src directory as counter.vhd in and copy the following VHDL code into the file and save the file. This file describes a simple 8-bit counter with a up / down selection, input, output and reset.</p>
86
use ieee.std_logic_1164.all;
87
use ieee.std_logic_unsigned.all;
89
entity up_down_counter is
91
cout :out std_logic_vector (7 downto 0);
92
up_down :in std_logic; -- up_down control for counter
93
clk :in std_logic; -- Input clock
94
reset :in std_logic -- Input reset
98
architecture rtl of up_down_counter is
99
signal count :std_logic_vector (7 downto 0);
101
process (clk, reset) begin
102
if (reset = '1') then
103
count <= (others=>'0');
104
elsif (rising_edge(clk)) then
105
if (up_down = '1') then
106
count <= count + 1;
108
count <= count - 1;
113
end architecture;</code>
116
<p>Create another new file and save it in the testbench directory as counter_tb.vhd and copy the code below into the file and save the file. This file is a simple test bench to test the counter.</p>
119
use ieee.std_logic_1164.all;
120
use ieee.std_logic_unsigned.all;
125
architecture TB of counter_tb is
127
component up_down_counter
128
port( cout: out std_logic_vector(7 downto 0);
129
up_down: in std_logic;
136
signal cout: std_logic_vector(7 downto 0);
137
signal up_down: std_logic;
138
signal reset: std_logic;
139
signal clk: std_logic;
143
dut: up_down_counter port map (cout, up_down, reset, clk);
166
<section>Simulation and Viewing the result</section>
168
<p>Now we have created a simple counter and testbench it is time to simulate the design.
169
Select the terminal tab at the bottom of the Gedit window. To simulate the design first change to the top level project directory using the cd command, as shown below.</p>
171
<code>cd /path/to/project/dir</code>
173
<p>To simulate a design with gHDL three steps are needed, these are included in the Makefile given above, shown below, as we are using the make file don't run GHDL directly use the make commands shown below.</p>
176
ghdl -m TESTBENCH_NAME
177
ghdl -r TESTBENCH_NAME</code>
179
<p>First GHDL is ran with the -i option and a list of all files in the design. This analysis all units in the design in the correct order, and will check for and errors in the VHDL code. Next GHDL is ran with the -m option and the name of the top unit which will usually be a testbench. This analysis and elaborates the design and creates an executable to run the simulation. The simulation is ran executing ghdl with the -r option.</p>
181
<p>The above flow is included in the Makefile to make it easier to simulate designs and view the result, as shown below. The first line runs the Ghdl twice once with -i and then with -m, the second line runs Ghdl -r and the final line shows results in the waveform viewer GTKWave.</p>
183
<code>make compile TESTBENCH=counter_tb
184
make run TESTBENCH=counter_tb
187
<p>All three steps above can be ran with the command</p>
189
<code>make all TESTBENCH=counter_tb</code>
191
<p>The Makefile is designed to make your life easier. But to do this it makes some assumptions, which are:</p>
194
<item><p>The design files are in a sub-directory src.</p></item>
195
<item><p>The testbench is in a sub-directory called testbench.</p></item>
196
<item><p>The testbench entity has the same name as the testbench file.</p></item>
199
<p>These assumptions are met in the above example, so if you have done everything right you should have no trouble.
202
<p>After running the above commands GTKWave should open. To view signals from the design select the design from the top left side pane and select a signal from the bottom left side pane and click Insert at the bottom left of the GTKWave window.</p>
204
<section>Further Information</section>
206
<p>For more information on using GHDL and GTKWave see the project websites.</p>
208
<link href="http://ghdl.free.fr/">http://ghdl.free.fr/</link>
209
<link href="http://gtkwave.sourceforge.net/">http://gtkwave.sourceforge.net/</link>