Monday 29 August 2022

Installing GHDL and GTKwave on Windows

GHDL is a well established and powerful VHDL simulator that is free and open source. For more information, visit the GHDL web site or the GHDL Github repository.

GTKwave is a very useful companion to GHDL: it's a wave viewer which supports common simulation output formats, and is also free and open source. For more information, visit the GTKwave web site or the GTKwave Github repository.

This post is for hardware engineers who want to get GHDL and GTKwave up and running quickly on a Windows PC (Windows 10 or later is recommended, and your Windows version should be 64-bit). It includes instructions on how to add Vivado and Quartus libraries to the GHDL installation, so that you can use it for real FPGA projects.


GTKwave UI

There is more than one way to install these tools, but I believe that the recipe presented here is a good one.

1. Installing MSYS2

You can read about MSYS2 on the project web site. It provides the underpinnings to enable applications developed for Linux and other Unix flavours to run on Windows PCs, with little or no modification to their source code. It also includes some basic infrastructure for development, including a command line (a terminal for interacting with its Bash shell) and tools for easy installation and management of packages of software.

MSYS2 includes various "environments" (or "subsystems"). They are described here. We will be using the MINGW64 environment throughout.

To install MSYS2 on your Windows PC, download the latest installer from here: MSYS2 x86-64 binaries. You will see a long list of files to chose from; scroll to the bottom and pick the last one listed in the form msys2-x86_64-date.exe where data is consists of year then month then day. For example: msys2-x86_64-20220603.exe.

Run the downloaded executable. The "Setup - MSYS2 64 bit" dialog is displayed:


Click the Next button. The "Installation Folder" dialog is displayed:


Edit the installation folder if desired. For example, all my development tools live in the devtools directory on  D drive, so I changed this to read D:\devtools\msys64. Click the Next button. The "Start Menu Shortcuts" dialog is displayed:


Click the Next button. The "Installing MSYS2 64 bit" dialog is displayed:

The green progress bar will move to the right as the installer progresses. This can take a minute or so. When complete, the "Completing the MSYS2 64 bit Wizard" dialog will displayed:


Untick the "Run MSYS2 64 bit now" box and then click the Finish button. MSYS2 is now installed.

2. Update Windows Environment Variables

If you are not familiar with editing environment variables, please do a bit of searching to familiarise yourself with the procedure for your version of Windows.

Define an MSYS2 environment variable set to your MSYS2 install location (C:\msys64 by default). This will allow scripts to locate MSYS2 components easily.

In a development session you will often want to run both MSYS2 programs and Windows programs, so it makes sense to update the Windows path to ensure that you can run MSYS2 and MINGW64 binaries from a Windows Command Prompt or Windows Terminal. Add the following directories to your path: %MSYS2%\usr\bin and %MSYS2%\mingw64\bin.

3. The MSYS2 Terminal

To open an MSYS2 terminal (shell), run the MSYS2 MinGW x64 application from the Start Menu. The MSYS2 terminal window will appear:

Notice that the user, machine and environment names are displayed at the shell prompt (the $) along with the current directory, with ~ signifying the user's home directory. To find out where this actually is,  enter the bash command pwd:

user@MACHINE MINGW64 ~
$ pwd
/c/work

Notice that the path is /c/work not C:\work. MSYS2 is doing its job, translating Windows paths into Linux/Unix format for programs running under MSYS2. That means the slashes are changed from backwards to forwards, and drive letters become lower case prefixed by a slash, with the colon replaced by another slash.

Most of the time, when specifying a path to a MSYS2 program, you will need to use the MSYS2 path format.

4. Install GHDL and GTKwave

We will use the pacman package management tool to install GHDL and GTKwave in MSYS2.

Open an MSYS2 terminal and enter the following command exactly as shown:

pacman -S mingw-w64-x86_64-{ghdl-llvm,gtkwave}

pacman will list 70+ packages, and give a total size for the download and installation, and ask you if you wish to proceed. Something like this:

user@MACHINE MINGW64 ~
$ pacman -S mingw-w64-x86_64-{ghdl-llvm,gtkwave}
resolving dependencies...
looking for conflicting packages...
warning: dependency cycle detected:
warning: mingw-w64-x86_64-libwebp will be installed before its mingw-w64-x86_64-libtiff dependency
warning: dependency cycle detected:
warning: mingw-w64-x86_64-harfbuzz will be installed before its mingw-w64-x86_64-freetype dependency

Packages (74) mingw-w64-x86_64-adwaita-icon-theme-42.0-1
mingw-w64-x86_64-atk-2.38.0-1 mingw-w64-x86_64-binutils-2.38-2
mingw-w64-x86_64-brotli-1.0.9-4 mingw-w64-x86_64-bzip2-1.0.8-2
...
mingw-w64-x86_64-zstd-1.5.2-2
mingw-w64-x86_64-ghdl-llvm-2.0.0.r77.g4775ebbd6-1
mingw-w64-x86_64-gtkwave-3.3.111-1

Total Download Size: 287.71 MiB
Total Installed Size: 1896.30 MiB

:: Proceed with installation? [Y/n]

Enter Y to begin. The packages are downloaded in parallel, with progress bars. This will be followed by stage that processes package changes, then "post-transaction hooks". The whole process will take a few minutes.

GHDL and GTKwave are now installed and may be used - unless you need vendor libraries, in which case see below.

You can check the installed version of each application as follows:

user@MACHINE MINGW64 ~
$ ghdl --version
GHDL 3.0.0-dev (2.0.0.r77.g4775ebbd6) [Dunoon edition]
Compiled with GNAT Version: 11.2.0
llvm code generator
Written by Tristan Gingold.

Copyright (C) 2003 - 2022 Tristan Gingold.
GHDL is free software, covered by the GNU General Public License. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

user@MACHINE MINGW64 ~
$ gtkwave --version
GTKWave Analyzer v3.3.111 (w)1999-2020 BSI

This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

5. Compiling Vendor Libraries

If you want to use Vivado or Quartus libraries in your simulation, you will need to build them from the source code that is provided as part of the installation of these tools.

Start by opening an MSYS2 terminal and navigating to the GHDL vendor libraries directory:

cd /mingw64/lib/ghdl/vendors

Note that in the MSYS2 environment, / corresponds to the MSYS2 install directory. So if MSYS2 is installed in c:\msys64 then the MSYS2 path /mingw64/lib/ghdl/vendors corresponds to  the Windows path C:\msys64\mingw64\lib\ghdl\vendors. You need to know this because the next step is to open and edit the config.sh file which is located there in your preferred text editor (I recommend Notepad++).

You need to find the sections corresponding to Vivado and/or Quartus and update them to match the installation directories on your PC.

For Vivado, find the following section e.g. by searching for "vivado":

declare -A Xilinx_Vivado_Settings=(
	["InstallationDirectory"]=""              # "/opt/Xilinx/Vivado/2020.2"
	["SourceDirectory"]="data/vhdl/src"
	["DestinationDirectory"]="xilinx-vivado"
)

(Note that the # marks a comment which can be ignored.)

Amend the "InstallationDirectory" entry to match the Vivado installation on your PC, using an MSYS2 path format. For example:

declare -A Xilinx_Vivado_Settings=(
	["InstallationDirectory"]="/c/Xilinx/Vivado/2022.1"
	["SourceDirectory"]="data/vhdl/src"
	["DestinationDirectory"]="xilinx-vivado"
)

For Quartus, find the following section by searching e.g. for "intel":

declare -A Intel_Quartus_Settings=(
	["InstallationDirectory"]=""              # "/opt/intelFPGA/20.1/quartus"
	["SourceDirectory"]="eda/sim_lib"
	["DestinationDirectory"]="intel"
)

Amend the "InstallationDirectory" entry to match the Quartus installation on your PC, using an MSYS2 path format. For example:

declare -A Intel_Quartus_Settings=(
	["InstallationDirectory"]="/c/intelFPGA_lite/21.1/quartus"
	["SourceDirectory"]="eda/sim_lib"
	["DestinationDirectory"]="intel"
)

Once you have made these edits, save config.sh and quit your editor app.

You may now build the libraries you need by entering one or more of the following commands - one at a time - in an MSYS2 terminal:

  • for Vivado and VHDL-1993: ./compile-xilinx-vivado.sh -a --vhdl93
  • for Vivado and VHDL-2008: ./compile-xilinx-vivado.sh -a --vhdl2008
  • for Quartus and VHDL-1993: ./compile-intel.sh -a --vhdl93
  • for Quartus and VHDL-2008: ./compile-intel.sh -a --vhdl2008

Each command invokes a script which will build the (many) source files required. Note that this process takes many minutes - maybe many tens of minutes. Here's an example of a successful run - despite the warning and error messages:

user@MACHINE MINGW64 /mingw64/lib/ghdl/vendors
$ ./compile-xilinx-vivado.sh -a --vhdl93
Loading environment...
Creating vendor directory: '/mingw64/lib/ghdl/vendors/xilinx-vivado'.
Analyzing library 'unisim'...
Creating VHDL Library 'unisim'...
Analyzing files into library 'unisim'...
  WARNING: D:/devtools/Xilinx/Vivado/2022.1/data/vhdl/src/unisims/primitive/SYSMONE4.vhd:1536:44:warning: prefix of array attribute must be an object name [-Wattribute]
SCRIPT ERROR: Unfiltered line
v_str_time_length := time'image(now)'length;
SCRIPT ERROR: Unfiltered line
^
  Warnings detected by filtering script.
Analyzing library 'secureip'...
Creating VHDL Library 'secureip'...
Analyzing files into library 'secureip'...
Analyzing library 'unimacro'...
Creating VHDL Library 'unimacro'...
Analyzing files into library 'unimacro'...
Analyzing library 'unifast'...
Creating VHDL Library 'unifast'...
Analyzing files into library 'unifast'...
Analyzing library 'secureip'...
Creating VHDL Library 'secureip'...
Analyzing files into library 'secureip'...
  WARNING: D:/devtools/Xilinx/Vivado/2022.1/data/vhdl/src/unifast/secureip/GTHE2_CHANNEL.vhd:29:1:warning: entity "gthe2_channel" was also defined in file "D:/devtools/Xilinx/Vivado/2022.1/data/vhdl/src/unisims/secureip/GTHE2_CHANNEL.vhd" [-Wlibrary]
SCRIPT ERROR: Unfiltered line
library IEEE;
SCRIPT ERROR: Unfiltered line
^
  Warnings detected by filtering script.
  WARNING: D:/devtools/Xilinx/Vivado/2022.1/data/vhdl/src/unifast/secureip/GTXE2_CHANNEL.vhd:34:1:warning: entity "gtxe2_channel" was also defined in file "D:/devtools/Xilinx/Vivado/2022.1/data/vhdl/src/unisims/secureip/GTXE2_CHANNEL.vhd" [-Wlibrary]
SCRIPT ERROR: Unfiltered line
library IEEE;
SCRIPT ERROR: Unfiltered line
^
  Warnings detected by filtering script.
--------------------------------------------------------------------------------
Compiling Xilinx Vivado libraries [SUCCESSFUL]

You are now ready to simulate designs including Vivado and Quartus libraries. Note that you need to tell GHDL the path to the compiled libraries by adding a -P option to the command line for the analysis and elaboration steps. For example:

ghdl -a -frelaxed -fsynopsys -P/mingw64/lib/ghdl/vendors/xilinx-vivado top.vhd