-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathace
executable file
·342 lines (318 loc) · 8.63 KB
/
ace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
[[ $_ != $0 ]] && {
export PATH="$PATH:$PWD"
export CFLAGS=""
export ROOTDIR="$PWD"
export MEMORY_DEPTH=2048
export MEMORY_WIDTH=128
[[ $# -gt 0 ]] || return 0
[[ -z $PS1_BAK ]] && export PS1_BAK=$PS1
echo ' __ ___________ _____ ___ ____________'
echo ' / |/ / _/ __ \/ ___/ / | / ____/ ____/ /\_/\'
echo ' / /|_/ // // /_/ /\__ \ / /| |/ / / __/ ____/ o o \'
echo ' / / / // // ____/___/ / / ___ / /___/ /___ /~____ =ø= /'
echo '/_/ /_/___/_/ /____/ /_/ |_\____/_____/ (______)__m_m)'
echo ''
export PS1="\$([[ \${PWD} == \"$ROOTDIR\"* ]] && echo \"[\$(git diff --quiet HEAD && echo '\033[33m' || echo '\033[31m')\$(git rev-parse --abbrev-ref HEAD)\033[0m] \033[32m\$(git config --global user.name)\033[0m@\033[34mace\033[0m\${PWD#$ROOTDIR}: \" || echo \"$PS1_BAK\")"
return 0
}
NC='\033[0m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
CALLDIR=$PWD
[[ -z $ROOTDIR ]] || cd $ROOTDIR
die() {
echo "$@" >&2
exit 1
}
usage() {
echo 'usage: ace <command> [<args>]'
echo
echo 'The following commands are available:'
echo ' todo List todos and fixmes in source files'
echo ' check Check if module compiles correctly'
echo ' test Compile and run testbench'
echo ' create Create boilerplate testbench or module'
echo ' clean Remove all intermediate files'
echo ' run Run assembly or memory'
echo
echo 'To learn more about the options for the'
echo 'different commands try ace help <command>'
exit 1
}
details() {
case $1 in
todo)
echo 'usage: ace todo [-a|--all] [-q|--quiet] [<file>]'
echo
echo ' -a|--all Gets all todos'
echo ' -q|--quiet Quiet'
echo
echo 'List all TODO and FIXME tokens in source files (.v).'
;;
check)
echo 'usage: ace check [-a|--all] [-q|--quiet] [<file>]'
echo
echo ' -a|--all Check all files'
echo ' -q|--quiet Quiet, no error messages'
echo
echo 'Tries to compile source file (.v). This is useful for'
echo 'syntax checking'
;;
test)
echo 'usage: ace test [-d|--define <flag>] [-g|--gtkwave] [-t|--trace <trace>] <testbench>'
echo
echo ' -t|--trace Writes trace to <trace>'
echo ' -g|--gtkwave Graphical mode (GTKWave)'
echo ' -d|--define Add debug flag'
echo
echo 'All testbenches are located in the test folder.'
echo 'GTKWave needs a valid trace file'
;;
create)
echo 'usage: ace create <module>'
echo
echo 'All modules are located in the src folder, all'
echo 'testbenches in the test folder. Testbenches should'
echo 'end on _tb.'
;;
clean)
echo 'usage: ace clean'
;;
run)
echo 'usage: ace run [-t|--trace <trace>] [-g|--gtkwave]'
echo ' [-m|--main <main>] [-k|--kernel <kernel>]'
echo ' [-b|--bare] [-d|--define <flag>] <program>'
echo
echo ' -t|--trace Name for tracefile'
echo ' -g|--gtkwave Graphical mode (GTKWave)'
echo ' -m|--main Main module (default: cpu_tb)'
echo ' -d|--define Add debug flag'
echo ' -k|--kernel Name of the kernel'
echo ' -b|--bare Bare-metal mode, no kernel loaded'
echo
echo 'This command builds the cpu and runs a program against it.'
echo 'You can either use assembly source code or a hexadecimal'
echo 'file containing the memory input data. The memory format is'
echo 'defined by MEMORY_WIDTH and MEMORY_DEPTH env variables.'
;;
*)
echo "Unrecognized command $1"
;;
esac
exit 1
}
clean() {
rm -rf build/
rm -rf dump/
}
check_config() {
[[ -z $MEMORY_WIDTH ]] && die 'Memory width is undefined'
[[ -z $MEMORY_DEPTH ]] && die 'Memory depth is undefined'
}
find_for() {
TMP="/${1%.v}.v"
find . -type f -path ".*${TMP}"
}
# $1 -> file
compile() {
FILE=$1
BUILD=`basename ${1%.v}`
CFLAGS="$CFLAGS -DMEMORY_DEPTH=$MEMORY_DEPTH -DMEMORY_WIDTH=$MEMORY_WIDTH"
iverilog -o build/run.vvp $CFLAGS -Isrc -Itest `utils/list-deps $FILE`
}
check_files() {
[[ $# -gt 0 ]] || return 1
for FILE in $@ ; do
if compile $FILE 2> build/compile.log ; then
echo -e "[${GREEN}Ok${NC}] $FILE"
else
echo -e "[${RED}Error${NC}] $FILE"
fi
[[ $ARG_QUIET -eq 1 ]] || cat build/compile.log
done
}
check() {
ARG_ALL=0
ARG_QUIET=0
TEMP=`getopt -o aq --long all,quiet -- "$@" 2>/dev/null`
eval set -- "$TEMP"
mkdir -p build
while true ; do
case "$1" in
-a|--all) ARG_ALL=1 ; shift ;;
-q|--quiet) ARG_QUIET=1 ; shift ;;
--) shift ; break ;;
*) die "Unrecognized option $1" ;;
esac
done
if [[ $ARG_ALL -eq 1 ]] ; then
FILES=(`find . -type f -name "*.v" -not -name "defines.v"`)
check_files ${FILES[@]}
else
[[ $# -gt 0 ]] || die 'No arguments'
for ARG in "$@" ; do
FILES=(`find_for $ARG`)
check_files ${FILES[@]} || echo -e "[${RED}Error${NC}] $ARG not found"
done
fi
}
list_todos() {
awk '/\/{2}\s*TODO\s*(.*)/ {
gsub(/[^/]*\/*\s*TODO\s*/, "");
print $0; f=1
} END { exit f }' $1 > build/todo.log
}
todo_files() {
[[ $# -gt 0 ]] || return 1
for FILE in $@ ; do
if list_todos $FILE ; then
echo -e "[${GREEN}Ok${NC}] $FILE"
else
echo -e "[${BLUE}Todo${NC}] $FILE"
[[ $ARG_QUIET -eq 1 ]] || cat build/todo.log
fi
done
}
todo() {
ARG_ALL=0
ARG_QUIET=0
TEMP=`getopt -o aq --long all,quiet -- "$@" 2>/dev/null`
eval set -- "$TEMP"
mkdir -p build
while true ; do
case "$1" in
-a|--all) ARG_ALL=1 ; shift ;;
-q|--quiet) ARG_QUIET=1 ; shift ;;
--) shift ; break ;;
*) die "Unrecognized option $1" ;;
esac
done
if [[ $ARG_ALL -eq 1 ]] ; then
FILES=(`find . -type f -name "*.v"`)
todo_files ${FILES[@]}
else
[[ $# -gt 0 ]] || die "No arguments"
for ARG in "$@" ; do
FILES=(`find_for $ARG`)
todo_files ${FILES[@]} || echo -e "[${RED}Error${NC}] $ARG not found"
done
fi
}
runtest() {
[[ -z $ARG_TRACE ]] || CFLAGS="$CFLAGS -DTRACEFILE=\"$ARG_TRACE\""
mkdir -p build dump
FILE=${1%.v}.v
[[ $FILE != "" ]] || die 'No program'
compile $FILE 2> build/compile.log || {
cat build/compile.log >&2
die "Unable to build"
}
cat build/compile.log >&2
build/run.vvp
[[ $ARG_GTK -eq 1 ]] && [[ -z $ARG_TRACE ]] && die 'Option -g ignored'
[[ $ARG_GTK -eq 1 ]] || exit 0
ps | grep -sq gtkwave && die "GTKWave already running"
gtkwave -A -f $ARG_TRACE >/dev/null 2>/dev/null &
}
testbench() {
TEMP=`getopt -o d:gt: --long define:,gtkwave,trace: -- "$@" 2>/dev/null`
ARG_GTK=0
ARG_TRACE=
eval set -- "$TEMP"
while true ; do
case "$1" in
-d|--define) CFLAGS="$CFLAGS -D$2" ; shift 2 ;;
-g|--gtkwave) ARG_GTK=1 ; shift ;;
-t|--trace) ARG_TRACE=${2%.vcd}.vcd ; shift 2 ;;
--) shift ; break ;;
*) die "Unrecognized option $1" ;;
esac
done
[[ $# -gt 0 ]] || die "No testbench"
check_config
runtest "$1"
}
run() {
TEMP=`getopt -o bd:gt:m:k: --long bare,define:,gtkwave,trace:,main:,kernel: -- "$@" 2>/dev/null`
ARG_GTK=0
ARG_MAIN="test/cpu_tb.v"
ARG_TRACE=
ARG_KERNEL=default
ARG_BARE=0
eval set -- "$TEMP"
mkdir -p build
while true ; do
case "$1" in
-d|--define) CFLAGS="$CFLAGS -D$2" ; shift 2 ;;
-g|--gtkwave) ARG_GTK=1 ; shift ;;
-t|--trace) ARG_TRACE=${2%.vcd}.vcd ; shift 2 ;;
-m|--main) ARG_MAIN=${2%.v}.v ; shift 2 ;;
-k|--kernel) ARG_KERNEL=$2 ; shift 2 ;;
-b|--bare) ARG_BARE=1 ; shift ;;
--) shift ; break ;;
*) die "Unrecognized option $1" ;;
esac
done
[[ $# -gt 0 ]] || die "No program"
SRCFILE=$CALLDIR/$1
[[ -f $SRCFILE ]] || die "File not found $1"
check_config
FILE_ENDING=${SRCFILE##*.}
case $FILE_ENDING in
s|asm)
if [[ $ARG_BARE -eq 0 ]] ; then
[[ -f kernel/${ARG_KERNEL}.s ]] || die "Kernel $ARG_KERNEL not available"
cat kernel/${ARG_KERNEL}.s $SRCFILE > build/memory.asm
export ASM_MODE="user_kernel"
else
cat $SRCFILE > build/memory.asm
export ASM_MODE="user_only"
fi
utils/asm build/memory.asm > build/memory.dat 2> build/memory.log
if [[ $? -ne 0 ]] ; then
cat build/memory.log >&2
die 'Unable to assemble program'
fi
utils/memformat build/memory.dat > build/memory.raw 2> build/memory.log
if [[ $? -ne 0 ]] ; then
cat build/memory.log >&2
die 'Unable to format memory'
fi
;;
dat)
utils/memformat $SRCFILE > build/memory.raw 2> build/memory.log
if [[ $? -ne 0 ]] ; then
cat build/memory.log >&2
die 'Unable to format memory'
fi
;;
raw)
cat $SRCFILE > build/memory.raw
;;
*)
die "Unrecognized format $FILE_ENDING"
;;
esac
runtest "$ARG_MAIN"
}
[[ $# -gt 0 ]] || usage
CMD=$1
shift
case $CMD in
help)
[[ $# -gt 0 ]] || usage
details $1
;;
check) check "$@" ;;
todo) todo "$@" ;;
create)
[[ $# -gt 0 ]] || die 'No arguments'
utils/boilerplate "${1%.v}" ;;
clean) clean ;;
test) testbench "$@" ;;
run) run "$@" ;;
*) usage ;;
esac