How do I compile and run a VHDL program on Mac with ghdl?

user2426316 picture user2426316 · Jun 12, 2013 · Viewed 18.2k times · Source

i just wrote a simple VHDL program. I saved the file as a .vhd file. Then I compiled it with

ghdl -a test.vhd

and then built and executable file with

ghdl -e test

And finally tried to run it with

ghdl -r test

What happened is that I now have a work-obj93.cf file that is visible and two other files, namely .. and . that are somehow invisible.

Can someone tell how exactly I can test a vhdl program on my Mac? Maybe in in combination with GTKWave.

Answer

user1155120 picture user1155120 · Jun 13, 2013

That as they say is a big ask!

There's a version of ghdl for OS X running on Intel processors for versions 10.5, 10.6,10.7 and 10.8 available from ghdl.free.fr (can be downloaded at GHDL for Mac OS X). It's the mcode version (like on Windows), which means it doesn't produce object codes or a standalone executable of a VHDL model with the consequence you can't bind foreign objects (subprograms) to the model.

The elaborated model only exists in memory at run time and the -e elaborate command is superfluous other than an entry in the working library .cf file. In an mcode version of ghdl the -r run command also elaborates. All you are going to see is the work-obj93.cf file for the working directory and any .cf files for pre-analyzed libraries, by default these will show up in /usr/local/ghdl/libraries, the executable ghdl found in /usr/local/bin links to /usr/local/ghdl/translate/ghdldrv/ghdl_mcode, and /usr/local/ghdl is a stripped down tree resulting from the compilation of the ghdl_mcode version.

This version is derived from svn129 (following the ghdl-0.29 release), and contains an i386 binary.

The documentation for ghdl is found in /usr/local/ghdl/doc/, there's the ghdl man page which is linked elsewhere, ghdl.html and ghdl.texi which are the ghdl manual and not linked elsewhere. In ghdl.html you could search for every occurrence of 'Windows' to find reference to the mcode version. As an oversight I didn't think to amend the ghdl manual to include the word mcode wherever Windows appeared and release modified manual.

Where ever 'Windows' appears it should be read as 'Windows or other mcode version'.

In the ghdl manual see 1.3 What is GHDL?, (The Windows(TM) version of GHDL is not based on GCC but on an internal code generator). Also 2.1 The hello world program:

2.1 The hello world program

To illustrate the large purpose of VHDL, here is a commented VHDL "Hello world" program.

 --  Hello world program.
 use std.textio.all; --  Imports the standard textio package.

 --  Defines a design entity, without any ports.
 entity hello_world is
 end hello_world;

 architecture behaviour of hello_world is
 begin
    process
       variable l : line;
    begin
       write (l, String'("Hello world!"));
       writeline (output, l);
       wait;
    end process;
 end behaviour;

Suppose this program is contained in the file hello.vhdl. First, you have to compile the file; this is called analysis of a design file in VHDL terms.

 $ ghdl -a hello.vhdl

This command creates or updates a file work-obj93.cf, which describes the library ‘work’. On GNU/Linux, this command generates a file hello.o, which is the object file corresponding to your VHDL program. The object file is not created on Windows.

Then, you have to build an executable file.

 $ ghdl -e hello_world

The ‘-e’ option means elaborate. With this option, GHDL creates code in order to elaborate a design, with the ‘hello’ entity at the top of the hierarchy.

On GNU/Linux, the result is an executable program called hello which can be run:

 $ ghdl -r hello_world

or directly:

 $ ./hello_world

On Windows, no file is created. The simulation is launched using this command:

 > ghdl -r hello_world

The result of the simulation appears on the screen:

 Hello world!

Saving the source code for the hello_world command to the file hello.vhdl and executing the commands:

david_koontz@Macbook: ghdl -a hello.vhdl

david_koontz@Macbook: ghdl -e hello_world

david_koontz@Macbook: ghdl -r hello_world

Yields:

Hello world!

on standard output (the TTY session in your terminal window).

You'll end up with a work-obj93.cf library configuration file in the current working directory and no other output files. You'll also want to understand the implications of the -r run command (section 3.1.3), with no optional secondary unit passed on the command line VHDL defaults to the last compiled architecture.

Obtaining Tony Bybell's gtkwave gtkwave.app, the documentation is found in /Applications/gtkwave.app/Contents/Resources/doc, both a source .odt file and a PDF file. (You can select an application in the Applications folder and 'Show Package Contents' to navigate to the doc directory).

There's a waveform format unique to ghdl called ghw, which can be invoked with the run command option '--wave=.ghw'. See ghdl manual 4.1 Simulation options. Also see the vcd options in that directory should you choose to use VCD instead.

You should also limit the run time duration with a guard timer should your test bench provide a free running clock. VHDL will only stop executing when either ther are no more scheduled signal updates or execution time maxes out. This is a run command option along the lines of '--stop-time=587200ns'.

The OS X gtkwave.app is sensitive to two file types with extensions .ghw and .gtkw, the first the ghdl waveform dump file, the second gtkwave's configuration file with an OS X suffix allowing invocation of gtkwave by clicking on the gtkw file. The configuration file has to be saved after setting up your wave form display in gtkwave before it exists. See the gtkwave manual. A ghw file can be clicked on and gtkwave has a set of rules used to search for the applicable gtkw file.

There are a couple of advantages to using the ghw file format. The automatic invocation of the gtkwave app by clicking on files is one. Another is that ghdl saves all signal transactions to it's ghw dump file. You can always select more in gtkwave to display more information. While this can be a disadvantage in large models the ghw format allows any type or subtype enumeration literal display.

Joining the GHDL discuss list and posting a question is a good way to attract ghdl expertise attention. (And yes the certificate for gna.org is expired or otherwise invalid). Tony Bybell provides an email contact on the gtkwave web page but exigencies of paid employment limit his availability.


Elaborating the use of ghdl and gtkwave with an example.

The original poster asked for some real examples with command line values. There's a however poor GHDL/GTKWave Tutorial (PDF, 234 KB) that contains an example and used VCD.

I'll list the command lines for a GHDL Waveform format version:

david_koontz@Macbook: ghdl -a CarryRipple.vhd

david_koontz@Macbook: ghdl -a CarryRipple_tb.vhd

david_koontz@Macbook: ghdl -e carryRipple_tb

david_koontz@Macbook: ghdl -r carryRipple_tb --wave=carryRipple_tb.ghw

invoking gtkwave.app can either be done by clicking on the resulting carryRipple_tb.ghw or by using OS X's open command:

david_koontz@Macbook: open -a gtkwave carryRipple_tb.ghw

Gtkwave's SST window (upper left) will display top, expand by clicking the + box. Do the same for the revealed carryripple_tb. Click a. In the signals window select a[3:0] and hit the insert button. Repeat for b.

In the SST window select U0 and then in the signals window select cin hit the insert button then the same for cout.

In the SST window select s, and in the signals window select s[3:0] and hit insert.

And yes getting the source for the two VHDL files from the 'tutorial' was painful. I didn't do it.

You might also note the assignment of 'X' to Cin at 60 ns in the gtkwave display, I did it to show the last values assigned in the testbench.

I wouldn't claim it's a good tutorial, but you're getting what you paid for here. Doing better would have been a bigger ask.

The test bench (carryRipple_tb.vhd) provides stimulous to drive the model (CarryRipple.vhd) during simulation. The model quits executing after the last signal event so no stop time need be passed to the run command ( e.g. --stop-time=587200ns).

There's also a five part GHDL/GTKWave Tutorial on Youtube by Chad Kersey. The resolution isn't so good and it's Linux centric. Part 0 is about installing the two tools. The only real difference in my example is using the ghw waveform format. Most browsers will allow you to see the gtkwave window shot below at full size.

enter image description here

added

There's a recent answer showing a demonstration with a Toggle Flip Flop, showing an error in the original VHDL code, and three possible fixes. It uses configuration declarations to run the various fixed versions. See how to avoid delay in the output of simple process statement in VHDL.