verilog: add sv_maps iterators
[ghdl-vlg.git] / testsuite / testenv.sh
blobb8891aefa335474022b2ef02c48771ce366220ce
1 # Testsuite environment
3 # This file defines the shell functions to analyse a file, elaborate or run
4 # a design. There are version for expected success and expected failure.
6 # Every test should source and use this file.
8 # Note: the executable must be set in the GHDL environment variable
10 # User defined variables (can be set to run the testsuite in some
11 # configuration, such as optimization or debugging):
12 # GHDL_STD_FLAGS
14 # Testbench flags. Can be used to run the whole testsuite under a specific
15 # flag (like -O, -g). Should not be set in testsuite.sh files.
16 # GHDL_FLAGS
18 #GHDL=ghdl
19 RM=rm
20 LN=ln
22 # Exit in case of failure in shell scripts.
23 set -e
25 # Define colors
26 ANSI_NOCOLOR="\033[0m"
27 ANSI_RED="\033[31m"
28 ANSI_BLUE="\033[34m"
29 ANSI_GREEN="\033[32m"
30 # Optionally disable colors
31 if [ -z "$ENABLECOLOR" ]; then unset ANSI_NOCOLOR ANSI_RED ANSI_BLUE ANSI_GREEN; fi
33 if [ x"$GHDL" = x ]; then
34 echo "error: GHDL environment variable is not defined"
35 exit 4
38 PYTHON=${PYTHON:-python3}
40 # Analyze files (no error expected)
41 analyze ()
43 echo "analyze $@"
44 "$GHDL" -a $GHDL_STD_FLAGS $GHDL_FLAGS "$@"
47 verilog_compile ()
49 "$VLG" --no-elab "$@"
52 verilog_run ()
54 "$VLG" "$@"
57 verilog_synth_tb()
59 t=$1
60 shift
62 echo "run tb for $1"
63 verilog_run $t.v tb_$t.v $*
65 synth --out=verilog $t.v $* -e > syn_$t.v
66 echo "run tb for synthesized $1"
67 verilog_run syn_$t.v tb_$t.v $*
70 # Analyze files (failure expected)
71 analyze_failure ()
73 echo "try to analyze $@"
74 # for arg in $@; do echo "arg: $arg"; done
75 if ! "$GHDL" -a --expect-failure $GHDL_STD_FLAGS $GHDL_FLAGS "$@" ; then
76 echo "Failure expected"
77 return 1
81 # Elaborate a design (no error expected)
82 # Note: somewhat deprecated, use elab_simulate instead;
83 # unless ghdl_has_feature needs to be used between elab and simulate
84 elab ()
86 echo "elaborate $@"
87 "$GHDL" -e $GHDL_STD_FLAGS $GHDL_FLAGS $@
90 # Elaborate a design (failure expected)
91 # Note: somewhat deprecated, use elab_simulate_failure instead;
92 # unless ghdl_has_feature needs to be used between elab and simulate
93 elab_failure ()
95 echo "elaborate (failure expected) $@"
96 "$GHDL" -e --expect-failure $GHDL_STD_FLAGS $GHDL_FLAGS $@
99 # Simulate a design (no error expected)
100 # Note: somewhat deprecated, use elab_simulate instead;
101 # unless ghdl_has_feature needs to be used between elab and simulate
102 simulate ()
104 echo "simulate $@ ($GHDL_FLAGS $@)" >&2
105 "$GHDL" -r $GHDL_STD_FLAGS $GHDL_FLAGS "$@"
106 #./$@
109 # Simulate a design (failure expected)
110 # Note: somewhat deprecated, use elab_simulate_failure instead;
111 # unless ghdl_has_feature needs to be used between elab and simulate
112 simulate_failure ()
114 echo "simulate (failure expected) $@" >&2
115 "$GHDL" -r $GHDL_STD_FLAGS $GHDL_FLAGS $@ --expect-failure
116 #./$@
119 # Elaborate and simulate a design (no error expected)
120 elab_simulate ()
122 echo "elaborate and simulate $@"
123 "$GHDL" --elab-run $GHDL_STD_FLAGS $GHDL_FLAGS $@
126 # Elaborate and simulate a design (failure expected)
127 elab_simulate_failure ()
129 echo "elaborate and simulate (failure expected) $@"
130 "$GHDL" --elab-run $GHDL_STD_FLAGS $GHDL_FLAGS $@ --expect-failure
133 synth()
135 echo "Synthesis of $@" >&2
136 "$GHDL" --synth $GHDL_STD_FLAGS $GHDL_SYNTH_FLAGS $GHDL_FLAGS $@
139 synth_failure ()
141 echo "try to synthesize $@"
142 # for arg in $@; do echo "arg: $arg"; done
143 if ! "$GHDL" --synth --expect-failure $GHDL_STD_FLAGS $GHDL_FLAGS "$@" ; then
144 echo "Failure expected"
145 return 1
149 # Synthesis of a single file
150 synth_only()
152 synth $1.vhdl -e > syn_$1.vhdl
155 # Synthesis of a single file and analyze the result
156 synth_analyze()
158 synth $1.vhdl -e > syn_$1.vhdl
159 analyze syn_$1.vhdl
160 synth --out=verilog $1.vhdl -e > syn_$1.v
161 verilog_compile syn_$1.v
164 # Analyze and test $1
165 # Then synthesize and test the result
166 synth_tb()
168 # Extract the first unit
169 t=$1
170 shift
172 analyze $* $t.vhdl tb_$t.vhdl
173 elab_simulate tb_$t
174 clean
176 synth $* $t.vhdl -e $t > syn_$t.vhdl
177 synth --out=verilog $* $t.vhdl -e $t > syn_$t.v
178 analyze $* syn_$t.vhdl tb_$t.vhdl
179 verilog_compile syn_$t.v
180 elab_simulate tb_$t --ieee-asserts=disable-at-0 --assert-level=error
181 clean
184 # Check if a C compiler is installed on this system
185 c_compiler_is_available ()
187 if [ -z $CC ]; then
188 CC="gcc"
190 which $CC
193 # Check if a feature is present
194 ghdl_has_feature ()
196 "$GHDL" -r $GHDL_STD_FLAGS $GHDL_FLAGS $1 --has-feature=$2
199 ghdl_is_interpretation ()
201 "$GHDL" --version | grep -q interpretation
204 # Run a program
205 run ()
207 echo "run $@"
208 eval $@
211 # Run a program (failure expected)
212 run_failure ()
214 echo "run (failure expected) $@"
215 if eval $@; then
216 echo "failure expected";
217 false;
221 # Be sure vpi can be used
222 add_vpi_path()
224 if [ "$OS" = "Windows_NT" ]; then
225 # Need to put the directory containing libghdlvpi.dll in the path.
226 vpi_lib=`$GHDL --vpi-library-dir-unix`
227 echo vpi_lib: $vpi_lib
228 PATH="$PATH:$vpi_lib"
232 # Clean the environment
233 clean ()
235 if [ $# -eq 0 ]; then
236 echo "Remove work library"
237 "$GHDL" --remove $GHDL_STD_FLAGS
238 else
239 case "$1" in
240 --std=*)
241 echo "Remove work library"
242 "$GHDL" --remove $1 ;;
244 echo "Remove $1 library"
245 "$GHDL" --remove $GHDL_STD_FLAGS --work=$1 ;;
246 esac
251 # Like diff but ignore CR ('\r') end-of-line on windows (as the reference file
252 # was generated on a UNIX platform).
253 # The --strip-trailing-cr option is available with GNU diff but not on BSD
254 # platforms.
255 diff_nocr ()
257 if [ "$OS" = "Windows_NT" ]; then
258 diff --strip-trailing-cr $@
259 else
260 diff $@
264 # Call ghwdump
265 ghw_dump ()
267 if [ x"$GHWDUMP" = x ]; then
268 case "$GHDL" in
269 */*) export GHWDUMP=$(dirname $GHDL)/ghwdump;;
270 *) export GHWDUMP=ghwdump;;
271 esac
274 "$GHWDUMP" -ths "$1".ghw > "$1".txt
277 # Compare the dump of a GHW wave and a previous golden dump
278 ghw_diff ()
280 ghw_dump "$1"
281 if diff_nocr "$1".txt golden_"$1".txt; then
282 echo "The ghw dump matches."
283 else
284 echo "The ghw dump does not match what is expected."
285 exit 1