b1c330d89cc0782cd3cec05c1ad26e4bd192c37f
[yosys.git] / tests / tools / autotest.sh
1 #!/bin/bash
2
3 libs=""
4 genvcd=false
5 use_isim=false
6 use_modelsim=false
7 verbose=false
8 keeprunning=false
9 backend_opts="-noattr -noexpr"
10 kompare_xst=false
11 scriptfiles=""
12 scriptopt=""
13 toolsdir="$(cd $(dirname $0); pwd)"
14
15 if [ ! -f $toolsdir/cmp_tbdata -o $toolsdir/cmp_tbdata.c -nt $toolsdir/cmp_tbdata ]; then
16 ( set -ex; gcc -Wall -o $toolsdir/cmp_tbdata $toolsdir/cmp_tbdata.c; ) || exit 1
17 fi
18
19 while getopts iml:wkvrxs:p: opt; do
20 case "$opt" in
21 i)
22 use_isim=true ;;
23 m)
24 use_modelsim=true ;;
25 l)
26 libs="$libs $(cd $(dirname $OPTARG); pwd)/$(basename $OPTARG)";;
27 w)
28 genvcd=true ;;
29 k)
30 keeprunning=true ;;
31 v)
32 verbose=true ;;
33 r)
34 backend_opts="$backend_opts -norename" ;;
35 x)
36 kompare_xst=true ;;
37 s)
38 [[ "$OPTARG" == /* ]] || OPTARG="$PWD/$OPTARG"
39 scriptfiles="$scriptfiles $OPTARG" ;;
40 p)
41 scriptopt="$OPTARG" ;;
42 *)
43 echo "Usage: $0 [-i] [-w] [-k] [-v] [-r] [-x] [-l libs] [-s script] [-p cmdstring] verilog-files\n" >&2
44 exit 1
45 esac
46 done
47
48 create_ref() {
49 if $kompare_xst; then
50 echo "verilog work $1" > $2.prj
51 cat <<- EOT > $2.xst
52 run
53 -ifn $2.prj -ifmt mixed -ofn $2 -ofmt NGC -p xc6slx4-3-tqg144
54 -top $( grep ^module $1 | sed -r 's,[^0-9A-Za-z_]+, ,g' | awk '{ print $2; exit; }'; )
55 -opt_mode Speed -opt_level 1 -iobuf NO
56 EOT
57 (
58 set +x
59 prefix="$2"
60 xilver=$( ls -v /opt/Xilinx/ | grep '^[0-9]' | tail -n1; )
61 case "$( uname -m )" in
62 x86_64)
63 set --; . /opt/Xilinx/$xilver/ISE_DS/settings64.sh ;;
64 *)
65 set --; . /opt/Xilinx/$xilver/ISE_DS/settings32.sh ;;
66 esac
67 set -x
68 xst -ifn $prefix.xst
69 netgen -w -ofmt verilog $prefix.ngc $prefix
70 )
71 else
72 cp "$1" "$2.v"
73 fi
74 }
75
76 compile_and_run() {
77 exe="$1"; output="$2"; shift 2
78 if $use_modelsim; then
79 altver=$( ls -v /opt/altera/ | grep '^[0-9]' | tail -n1; )
80 /opt/altera/$altver/modelsim_ase/bin/vlib work
81 /opt/altera/$altver/modelsim_ase/bin/vlog "$@"
82 /opt/altera/$altver/modelsim_ase/bin/vsim -c -do 'run -all; exit;' testbench | grep '#OUT#' > "$output"
83 elif $use_isim; then
84 (
85 set +x
86 files=( "$@" )
87 xilver=$( ls -v /opt/Xilinx/ | grep '^[0-9]' | tail -n1; )
88 case "$( uname -m )" in
89 x86_64)
90 set --; . /opt/Xilinx/$xilver/ISE_DS/settings64.sh ;;
91 *)
92 set --; . /opt/Xilinx/$xilver/ISE_DS/settings32.sh ;;
93 esac
94 set -x
95 vlogcomp "${files[@]}"
96 if $kompare_xst; then
97 fuse -o "$exe" -lib unisims_ver -top testbench -top glbl
98 else
99 fuse -o "$exe" -top testbench
100 fi
101 { echo "run all"; echo "exit"; } > run-all.tcl
102 PATH="$PATH:" "$exe" -tclbatch run-all.tcl > "$output"
103 )
104 else
105 iverilog -s testbench -o "$exe" "$@"
106 vvp -n "$exe" > "$output"
107 fi
108 }
109
110 shift $((OPTIND - 1))
111
112 for fn
113 do
114 bn=${fn%.v}
115 if [ "$bn" == "$fn" ]; then
116 echo "Invalid argument: $fn" >&2
117 exit 1
118 fi
119 [[ "$bn" == *_tb ]] && continue
120 echo -n "Test: $bn "
121
122 rm -f ${bn}.{err,log}
123 mkdir -p ${bn}.out
124 rm -rf ${bn}.out/*
125
126 body() {
127 cd ${bn}.out
128 cp ../$fn $fn
129 if [ ! -f ../${bn}_tb.v ]; then
130 "$toolsdir"/../../yosys -b autotest -o ${bn}_tb.v $fn
131 else
132 cp ../${bn}_tb.v ${bn}_tb.v
133 fi
134 if $genvcd; then sed -i 's,// \$dump,$dump,g' ${bn}_tb.v; fi
135 create_ref $fn ${bn}_ref
136 compile_and_run ${bn}_tb_ref ${bn}_out_ref ${bn}_tb.v ${bn}_ref.v $libs
137 if $genvcd; then mv testbench.vcd ${bn}_ref.vcd; fi
138
139 test_count=0
140 test_passes() {
141 "$toolsdir"/../../yosys -b "verilog $backend_opts" "$@" -o ${bn}_syn${test_count}.v $fn $scriptfiles
142 compile_and_run ${bn}_tb_syn${test_count} ${bn}_out_syn${test_count} \
143 ${bn}_tb.v ${bn}_syn${test_count}.v $libs \
144 "$toolsdir"/../../techlibs/common/simlib.v \
145 "$toolsdir"/../../techlibs/common/simcells.v
146 if $genvcd; then mv testbench.vcd ${bn}_syn${test_count}.vcd; fi
147 $toolsdir/cmp_tbdata ${bn}_out_ref ${bn}_out_syn${test_count}
148 test_count=$(( test_count + 1 ))
149 }
150
151 if [ -n "$scriptfiles" ]; then
152 test_passes
153 elif [ -n "$scriptopt" ]; then
154 test_passes -p "$scriptopt"
155 else
156 test_passes -p "hierarchy; proc; opt; memory; opt; fsm; opt"
157 test_passes -p "hierarchy; proc; opt; memory; opt; fsm; opt; techmap; opt; abc -dff; opt"
158 fi
159 touch ../${bn}.log
160 }
161
162 if $verbose; then
163 echo ".."
164 echo "Output written to console." > ${bn}.err
165 ( set -ex; body; )
166 else
167 ( set -ex; body; ) > ${bn}.err 2>&1
168 fi
169
170 if [ -f ${bn}.log ]; then
171 mv ${bn}.err ${bn}.log
172 echo "-> ok"
173 else echo "-> ERROR!"; $keeprunning || exit 1; fi
174 done
175
176 exit 0