Skip to content

Instantly share code, notes, and snippets.

@etiennecollin
Last active April 13, 2024 23:05
Show Gist options
  • Save etiennecollin/198f7520c4c58d545368a196e08f83ed to your computer and use it in GitHub Desktop.
Save etiennecollin/198f7520c4c58d545368a196e08f83ed to your computer and use it in GitHub Desktop.
Easily compile and simulate VHDL entities using GHDL and GTKWave. See instructions in the top comment.
# By Etienne Collin
# https://gist.github.com/etiennecollin/198f7520c4c58d545368a196e08f83ed
# Dependencies (on macOS, install via Homebrew (https://brew.sh/)):
# ghdl:
# Source: https://github.com/ghdl/ghdl/
# gtkwave:
# Source: https://gtkwave.sourceforge.net/
#### INPUT REQUIRED ####
ENTITIES = entityName1 entityName2
VHDL_EXTENSION = vhd
VHDL_MAIN_PATH = .
VHDL_DEPENDENCIES_PATH = .
VHDL_ARGS = -fexplicit -fsynopsys --std=08
########################
OUT_DIR = out
VHDL_MAIN := $(VHDL_MAIN_PATH)/*.$(VHDL_EXTENSION)
VHDL_DEPENDENCIES := $(VHDL_DEPENDENCIES_PATH)/*.$(VHDL_EXTENSION)
CF := $(OUT_DIR)/*.cf
GHW := $(OUT_DIR)/*.ghw
ENTITY = $(OUT_DIR)/$${entity}.ghw
VHDL_MOD_TIME = $(OUT_DIR)/vhdl_last_modification_time.txt
CF_MOD_TIME = $(OUT_DIR)/cf_last_modification_time.txt
.PHONY: all simulate clear purge
all: $(GHW)
# Make this the dependency of $(CF) if the files are always compiled even when not modified
# $(VHDL_MOD_TIME): $(VHDL_DEPENDENCIES) $(VHDL_MAIN)
# @$(MAKE) checkstructure
# @[ -f "$(VHDL_MOD_TIME)" ] || touch $(VHDL_MOD_TIME)
# @find $(VHDL_DEPENDENCIES) $(VHDL_MAIN) -type f -exec stat -f "%m %N" {} \; | sort -nr | head -1 > $(VHDL_MOD_TIME)
$(CF): $(VHDL_DEPENDENCIES) $(VHDL_MAIN)
@$(MAKE) checkstructure
@echo "Analyzing $(VHDL_EXTENSION) files..."
@ghdl -a $(VHDL_ARGS) --workdir=$(OUT_DIR) $(VHDL_DEPENDENCIES)
@ghdl -a $(VHDL_ARGS) --workdir=$(OUT_DIR) $(VHDL_MAIN)
@for entity in $(ENTITIES); do \
echo "Compiling entity $${entity}..."; \
ghdl -e $(VHDL_ARGS) --workdir=$(OUT_DIR) $${entity}; \
done
# Make this the dependency of $(GHW) if the files are always compiled even when not modified
# $(CF_MOD_TIME): $(CF)
# @[ -f "$(CF_MOD_TIME)" ] || touch $(CF_MOD_TIME)
# @find $(CF) -type f -exec stat -f "%m %N" {} \; | sort -nr | head -1 > $(CF_MOD_TIME)
$(GHW): $(CF)
@for entity in $(ENTITIES); do \
echo "Generating $${entity}.ghw file..."; \
ghdl -r $(VHDL_ARGS) --workdir=$(OUT_DIR) $${entity} --wave=$(ENTITY); \
done
simulate: $(GHW)
@for entity in $(ENTITIES); do \
echo "Opening $${entity}.ghw in gtkwave..."; \
open -a gtkwave $(ENTITY); \
done
checkstructure:
@[ -d $(OUT_DIR) ] || ( echo "Creating output directory..."; mkdir -p $(OUT_DIR) )
clear:
@echo "Cleaning auto-generated files from output directory..."
@rm -rf $(OUT_DIR)/*
purge:
@echo "Purging auto-generated file structure..."
@rm -rf $(OUT_DIR)
@etiennecollin
Copy link
Author

etiennecollin commented Mar 30, 2023

Makefile VHDL

This makefile supports:

  • Compiling/simulating multiple VHDL entities at once.
  • Dependency VHDL files that are placed in a directory different than your main directory.
  • Incremental builds.

Installation

  1. Download makefile_vhdl.
  2. Rename makefile_vhdl to makefile.
  3. Place the makefile in your project directory.

Dependencies

On macOS, install the dependencies via Homebrew with the following command:

brew update && brew install ghdl gtkwave && brew cleanup

Setup

Set the required variables in the makefile (these variables are at the top of the makefile and marked by #### INPUT REQUIRED ####):

  • ENTITIES: The name of the entities to simulate, separated by spaces.
  • VHDL_EXTENSION: The extension you are using for your VHDL files (usually vhd or vhdl).
    • Do not put the . before the extension (such as .vhd or .vhdl); it is added automatically.
  • VHDL_MAIN_PATH: The relative path to the directory containing your main VHDL files.
  • VHDL_DEPENDENCIES_PATH: The relative path to the directory containing your VHDL dependencies (for example, components that are used in the main entity).
    • If you do not need a second directory, simply use the same path as the VHDL_MAIN_PATH.
  • VHDL_ARGS: The arguments to pass to the GHDL compiler.

Run the makefile:

  1. Open a new terminal in the project directory.
  2. Type one of the available commands:
    • make: Compile the files and run the tests.
    • make simulate: Compile the files, run the tests and open in GTKWave.
    • make clear: Delete auto-generated compilation files.
    • make purge: Delete auto-generated compilation files and directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment