1######################################################################
2#
3# DESCRIPTION: Verilator Example: Small Makefile
4#
5# This calls the object directory makefile.  That allows the objects to
6# be placed in the "current directory" which simplifies the Makefile.
7#
8# This file ONLY is placed under the Creative Commons Public Domain, for
9# any use, without warranty, 2020 by Wilson Snyder.
10# SPDX-License-Identifier: CC0-1.0
11#
12######################################################################
13# Check for sanity to avoid later confusion
14
15ifneq ($(words $(CURDIR)),1)
16 $(error Unsupported: GNU Make cannot build in directories containing spaces, build elsewhere: '$(CURDIR)')
17endif
18
19######################################################################
20# Set up variables
21
22# If $VERILATOR_ROOT isn't in the environment, we assume it is part of a
23# package install, and verilator is in your path. Otherwise find the
24# binary relative to $VERILATOR_ROOT (such as when inside the git sources).
25ifeq ($(VERILATOR_ROOT),)
26VERILATOR = verilator
27VERILATOR_COVERAGE = verilator_coverage
28else
29export VERILATOR_ROOT
30VERILATOR = $(VERILATOR_ROOT)/bin/verilator
31VERILATOR_COVERAGE = $(VERILATOR_ROOT)/bin/verilator_coverage
32endif
33
34VERILATOR_FLAGS =
35# Generate SystemC in executable form
36VERILATOR_FLAGS += -sc --exe
37# Generate makefile dependencies (not shown as complicates the Makefile)
38#VERILATOR_FLAGS += -MMD
39# Optimize
40VERILATOR_FLAGS += -Os -x-assign 0
41# Warn abount lint issues; may not want this on less solid designs
42VERILATOR_FLAGS += -Wall
43# Make waveforms
44VERILATOR_FLAGS += --trace
45# Check SystemVerilog assertions
46VERILATOR_FLAGS += --assert
47# Generate coverage analysis
48VERILATOR_FLAGS += --coverage
49# Run Verilator in debug mode
50#VERILATOR_FLAGS += --debug
51# Add this trace to get a backtrace in gdb
52#VERILATOR_FLAGS += --gdbbt
53
54# Input files for Verilator
55VERILATOR_INPUT = -f input.vc top.v sc_main.cpp
56
57# Check if SC exists via a verilator call (empty if not)
58SYSTEMC_EXISTS := $(shell $(VERILATOR) --getenv SYSTEMC_INCLUDE)
59
60######################################################################
61
62ifneq ($(SYSTEMC_EXISTS),)
63default: run
64else
65default: nosc
66endif
67
68run:
69	@echo
70	@echo "-- Verilator tracing example"
71
72	@echo
73	@echo "-- VERILATE ----------------"
74	$(VERILATOR) $(VERILATOR_FLAGS) $(VERILATOR_INPUT)
75
76	@echo
77	@echo "-- COMPILE -----------------"
78# To compile, we can either
79# 1. Pass --build to Verilator by editing VERILATOR_FLAGS above.
80# 2. Or, run the make rules Verilator does:
81#	$(MAKE) -j -C obj_dir -f Vtop.mk
82# 3. Or, call a submakefile where we can override the rules ourselves:
83	$(MAKE) -j -C obj_dir -f ../Makefile_obj
84
85	@echo
86	@echo "-- RUN ---------------------"
87	@rm -rf logs
88	@mkdir -p logs
89	obj_dir/Vtop +trace
90
91	@echo
92	@echo "-- COVERAGE ----------------"
93	@rm -rf logs/annotated
94	$(VERILATOR_COVERAGE) --annotate logs/annotated logs/coverage.dat
95
96	@echo
97	@echo "-- DONE --------------------"
98	@echo "To see waveforms, open vlt_dump.vcd in a waveform viewer"
99	@echo
100
101
102######################################################################
103# Other targets
104
105nosc:
106	@echo
107	@echo "%Skip: SYSTEMC_INCLUDE not in environment"
108	@echo "(If you have SystemC see the README, and rebuild Verilator)"
109	@echo
110
111show-config:
112	$(VERILATOR) -V
113
114maintainer-copy::
115clean mostlyclean distclean maintainer-clean::
116	-rm -rf obj_dir logs *.log *.dmp *.vpd coverage.dat core
117