1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18ifeq (, $(shell which verilator))
19 $(error "No Verilator in $(PATH), consider doing apt-get install verilator")
20endif
21
22# Change VERILATOR_INC_DIR if Verilator is installed on a different location
23ifeq (, $(VERILATOR_INC_DIR))
24  ifeq (, $(wildcard /usr/local/share/verilator/include/*))
25    ifeq (, $(wildcard /usr/share/verilator/include/*))
26      $(error "Verilator include directory is not set properly")
27    else
28      VERILATOR_INC_DIR := /usr/share/verilator/include
29    endif
30  else
31      VERILATOR_INC_DIR := /usr/local/share/verilator/include
32  endif
33endif
34
35TOP = TestAccel
36BUILD_NAME = build
37USE_TRACE = 1
38LIBNAME = libhw
39
40vta_dir = $(abspath ../../../../)
41tvm_dir = $(abspath ../../../../../)
42build_dir = $(abspath .)/$(BUILD_NAME)
43verilator_build_dir = $(build_dir)/verilator
44chisel_build_dir = $(build_dir)/chisel
45
46verilator_opt = --cc
47verilator_opt += +define+RANDOMIZE_GARBAGE_ASSIGN
48verilator_opt += +define+RANDOMIZE_REG_INIT
49verilator_opt += +define+RANDOMIZE_MEM_INIT
50verilator_opt += --x-assign unique
51verilator_opt += --output-split 20000
52verilator_opt += --output-split-cfuncs 20000
53verilator_opt += --top-module ${TOP}
54verilator_opt += -Mdir ${verilator_build_dir}
55verilator_opt += -I$(chisel_build_dir)
56
57cxx_flags = -O2 -Wall -fPIC -shared
58cxx_flags += -fvisibility=hidden -std=c++11
59cxx_flags += -DVL_TSIM_NAME=V$(TOP)
60cxx_flags += -DVL_PRINTF=printf
61cxx_flags += -DVL_USER_FINISH
62cxx_flags += -DVM_COVERAGE=0
63cxx_flags += -DVM_SC=0
64cxx_flags += -Wno-sign-compare
65cxx_flags += -include V$(TOP).h
66cxx_flags += -I$(verilator_build_dir)
67cxx_flags += -I$(VERILATOR_INC_DIR)
68cxx_flags += -I$(VERILATOR_INC_DIR)/vltstd
69cxx_flags += -I$(vta_dir)/include
70cxx_flags += -I$(tvm_dir)/include
71cxx_flags += -I$(tvm_dir)/3rdparty/dlpack/include
72
73cxx_files = $(VERILATOR_INC_DIR)/verilated.cpp
74cxx_files += $(VERILATOR_INC_DIR)/verilated_dpi.cpp
75cxx_files += $(wildcard $(verilator_build_dir)/*.cpp)
76cxx_files += $(vta_dir)/hardware/dpi/tsim_device.cc
77
78ifneq ($(USE_TRACE), 0)
79  verilator_opt += --trace
80  cxx_flags += -DVM_TRACE=1
81  cxx_flags += -DTSIM_TRACE_FILE=$(verilator_build_dir)/$(TOP).vcd
82  cxx_files += $(VERILATOR_INC_DIR)/verilated_vcd_c.cpp
83else
84  cxx_flags += -DVM_TRACE=0
85endif
86
87# The following is to be consistent with cmake
88ifeq ($(shell uname), Darwin)
89  lib_path = $(build_dir)/$(LIBNAME).dylib
90else
91  lib_path = $(build_dir)/$(LIBNAME).so
92endif
93
94default: lib
95
96lib: $(lib_path)
97$(lib_path): $(verilator_build_dir)/V$(TOP).cpp
98	g++ $(cxx_flags) $(cxx_files) -o $@
99
100verilator: $(verilator_build_dir)/V$(TOP).cpp
101$(verilator_build_dir)/V$(TOP).cpp: $(chisel_build_dir)/$(TOP).v
102	verilator $(verilator_opt) $<
103
104verilog: $(chisel_build_dir)/$(TOP).v
105$(chisel_build_dir)/$(TOP).v: install_vta_package
106	sbt 'test:runMain test.Elaborate --target-dir $(chisel_build_dir) --top-name $(TOP)'
107
108install_vta_package:
109	cd $(vta_dir)/hardware/chisel && sbt publishLocal
110
111clean:
112	-rm -rf $(build_dir) target project/target project/project
113