-
Notifications
You must be signed in to change notification settings - Fork 0
/
utest.scm
executable file
·1144 lines (1050 loc) · 42 KB
/
utest.scm
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env guile
!#
;; Copyright (c) 2022 Nikolay Puzanov <punzik@gmail.com>
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;; -*- geiser-scheme-implementation: guile -*-
(import
;; (scheme base) ; R7RS base (not needed for Guile)
(srfi srfi-1) ; Lists
(srfi srfi-9) ; Records
(srfi srfi-11) ; let-values
(srfi srfi-13) ; String library
(srfi srfi-28) ; Simple format
(srfi srfi-39)) ; Parameters
(use-modules
(ice-9 regex)
(ice-9 popen)
(ice-9 textual-ports)
(ice-9 threads)
(ice-9 getopt-long))
(define APP_VERSION "0.0.1")
(define MAKEFILE_NAME_REGEXP ".*\\.utest$")
(define TIMEOUT_MODULE_NAME "utest_timeout")
(define DUMP_MODULE_NAME "utest_dump")
(define WORK_DIR_PREFIX "")
;;; Globals
(define utest/force-dump (make-parameter #f))
(define utest/restart-dump (make-parameter #f))
(define utest/keep-output (make-parameter #f))
(define utest/verbose (make-parameter #f))
(define utest/static (make-parameter #f))
(define utest/nocolor (make-parameter #f))
(define utest/base-path (make-parameter ""))
(define utest/work-path (make-parameter ""))
(define utest/extra (make-parameter '()))
(define-record-type <log-type>
(log-type log-prefix color out-prefix verbose)
log-type?
(log-prefix lt-log-prefix)
(color lt-color)
(out-prefix lt-out-prefix)
(verbose lt-verbose))
(define LOG_PREFIX_INFO "INFO#")
(define LOG_PREFIX_WARN "WARN#")
(define LOG_PREFIX_FAIL "FAIL#")
(define log-types
`((info ,(log-type LOG_PREFIX_INFO 15 " | " #t))
(warning ,(log-type LOG_PREFIX_WARN 226 " + " #t))
(error ,(log-type LOG_PREFIX_FAIL 196 " + " #t))
(test-head ,(log-type "TSTH#" 14 "" #t))
(test-info ,(log-type "TSTI#" 6 "" #t))
(test-succ ,(log-type "TSTS#" 47 "" #t))
(test-fail ,(log-type "TSTF#" 196 "" #t))))
(define log-type-default
(log-type "" 244 " | " #f))
;;; Like assoc but with functional predicate
(define (assf f l)
(find (lambda (x) (f (car x))) l))
;;; Get log type by id (level)
(define (log-type-by-id id)
(let ((t (assq id log-types)))
(if t (cadr t) log-type-default)))
;;; Get log type with log-prefix matched to prefix of str
(define (log-type-by-prefix str)
(let ((t (assf (lambda (x) (string-prefix? x str))
(map (lambda (x) (let ((t (cadr x)))
(list (lt-log-prefix t) t)))
log-types))))
(if t (cadr t) log-type-default)))
;;; Useful print functions
(define (printf . rest) (display (apply format rest)))
(define (println . rest) (for-each (lambda (x) (display x) (newline)) rest))
;;; Return log line as string
(define (utest/slog a . args)
(if (string? a)
(apply format a args)
(string-append
(lt-log-prefix (log-type-by-id a))
(apply format args))))
;;; Display log line
;;; Split line by newline character and display
;;; each piece as seperate line.
(define (utest/log a . args)
(let* ((pre (if (string? a) "" (lt-log-prefix (log-type-by-id a))))
(fmt (if (string? a) (cons a args) args))
(strs
(map (lambda (x) (string-append pre x))
(string-split
(apply format fmt)
#\newline))))
(for-each println strs) #t))
;;; Transpose matrix
;;; Example: (transpose '((1 2) (3 4) (5 6))) -> '((1 3 5) (2 4 6))
(define (transpose l)
(apply map list l))
;;; Make lists combinations
;;; Example: (combinations '(1 2 3) '(a b)) -> '((1 a) (1 b) (2 a) (2 b) (3 a) (3 b))
(define (combinations . lists)
(cond
((null? lists) '())
((null? (cdr lists)) (car lists))
(else
(fold (lambda (comb out)
(append out
(map (lambda (x)
(if (list? comb)
(cons x comb)
(list x comb)))
(car lists))))
'() (apply combinations (cdr lists))))))
;;;
;;; Colorize text
;;;
(define* (color text fg #:optional (bg 'default))
(format "~a~a~a~a[0m"
;; Foreground
(if (number? fg)
(format "~a[38;5;~am" #\esc fg)
(format "~a[~am" #\esc (case fg
((black) "30")
((red) "31")
((green) "32")
((yellow) "33")
((blue) "34")
((magenta) "35")
((cyan) "36")
((white) "37")
((default) "39"))))
;; Background
(if (number? bg)
(format "~a[48;5;~am" #\esc bg)
(format "~a[~am" #\esc (case bg
((black) "40")
((red) "41")
((green) "42")
((yellow) "43")
((blue) "44")
((magenta) "45")
((cyan) "46")
((white) "47")
((default) "49"))))
text #\esc))
;;;
;;; Print log
;;;
(define (convert-log-item item colorize)
(let* ((t (log-type-by-prefix item))
(pre (lt-out-prefix t))
(str (substring item (string-length (lt-log-prefix t)))))
(string-append
pre
(if colorize
(color str (lt-color t))
str))))
(define* (print-log log-strings
#:key
(verbose #f)
(colorize #f))
(for-each
(lambda (str)
(let ((t (log-type-by-prefix str)))
(when (or verbose (lt-verbose t))
(println (convert-log-item str colorize)))))
log-strings))
;;;
;;; Catch exception and continue with default value
;;;
(define-syntax guard
(syntax-rules ()
((_ default code...)
(with-exception-handler (lambda (e) default)
(lambda () code...)
#:unwind? #t))))
;;;
;;; Check path existence
;;; If path not exists throw exception
;;;
(define (check-path path)
(if (not (file-exists? path))
(raise-exception (format "Path ~a is not exists" path))
(let ((type (stat:type (stat path))))
(if (or (not (access? path R_OK))
(and (eq? type 'directory)
(not (access? path X_OK))))
(raise-exception (format "Path ~a is not readable" path))
#t))))
;;;
;;; Convert path to absolute path
;;;
(define* (path->absolute path #:optional (base ""))
(let ((path
(if (or (string-null? base)
(absolute-file-name? path))
path
(string-append base "/" path))))
(check-path path)
(canonicalize-path path)))
;;;
;;; Create file with simulation timeout watchdog
;;;
(define (create-timeout-module path modname timeout)
(define (* . fmt) (println (apply format fmt)))
(let ((filename (format "~a/~a.v" path modname)))
(with-output-to-file filename
;; #:exists 'replace
(lambda ()
(* "// Automatically generated file")
(* "`timescale 1ps/1ps")
(* "module ~a();" modname)
(* " initial begin")
(if (list? timeout)
(* " #(~a~a);"
(car timeout)
(if (null? (cdr timeout))
""
(symbol->string (cadr timeout))))
(* " #~a;" timeout))
(* " $display(\"~aTimeout at %0t\", $time);"
(lt-log-prefix (log-type-by-id 'error)))
(* " $finish;")
(* " end")
(* "endmodule")))
(path->absolute filename)))
;;;
;;; Create dump module
;;;
(define (create-dump-module path modname top dump-type)
(define (* . fmt) (println (apply format fmt)))
(let ((filename (format "~a/~a.v" path modname)))
(with-output-to-file filename
;; #:exists 'replace
(lambda ()
(* "// Automatically generated file")
(* "`timescale 1ps/1ps")
(* "module ~a();" modname)
(* " initial begin")
(* " $dumpfile(\"~a/~a.~a\");" path top dump-type)
(* " $dumpvars(0, ~a);" top)
(* " end")
(* "endmodule")))
(path->absolute filename)))
;;;
;;; Return directory list
;;;
(define (list-dir path)
(if (file-exists? path)
(let ((dir (opendir path)))
(let loop ((ls '()))
(let ((item (readdir dir)))
(if (eof-object? item)
(begin
(closedir dir)
ls)
(if (or (string=? item ".")
(string=? item ".."))
(loop ls)
(loop (cons (string-append path "/" item) ls)))))))
'()))
;;;
;;; Recursive delete directory
;;;
(define (delete-recursive path)
(let ((path (path->absolute path)))
(if (eq? 'directory (stat:type (stat path)))
(begin
(for-each delete-recursive (list-dir path))
(rmdir path))
(delete-file path))))
;;;
;;; Recursive find path items for which the function f returns true
;;; (fn fullpath type) : (-> (string symbol) boolean)
;;; Returns empty list if files not found
;;;
(define* (find-paths-rec fn base #:optional (follow-symlink #f))
(let ((ls (list-dir base)))
(let ((files.dirs
(fold (lambda (name f.d)
;; There is a risk that some paths may have disappeared during recursive search.
;; To avoid an error, we can catch the exception from the stat function
;; (let ((t (guard #f (stat:type (stat name)))))
;; (if t
;; (let* ((files (car f.d))
;; (dirs (cdr f.d))
;; (f (if (fn name t) (cons name files) files)))
;; (if (or (eq? t 'directory)
;; (and follow-symlink
;; (eq? t symlink)))
;; (cons f (cons name dirs))
;; (cons f dirs)))
;; f.d))
(let* ((files (car f.d))
(dirs (cdr f.d))
(t (stat:type (stat name)))
(f (if (fn name t) (cons name files) files)))
(if (or (eq? t 'directory)
(and follow-symlink
(eq? t symlink)))
(cons f (cons name dirs))
(cons f dirs))))
'(()) ls)))
(let ((files (car files.dirs))
(dirs (cdr files.dirs)))
(fold (lambda (dir files)
(append files (find-paths-rec fn dir follow-symlink)))
files dirs)))))
;;;
;;; Recursive find files with name matched a regular expression
;;; (find-files-rec-regexp rx base [follow-symlink #f]) -> (listof path?)
;;; rx : string?
;;; base : string?
;;; follow-symlink : boolean?
;;;
;;; rx - regulat expression
;;; base - base directory for files search
;;;
(define* (find-files-rec-regexp rx base #:optional (follow-symlink #f))
(if (eq? 'regular (stat:type (stat base)))
(if (string-match rx (basename base)) (list base) '())
(find-paths-rec
(lambda (f t)
(and (eq? t 'regular)
(string-match rx (basename f))))
base follow-symlink)))
;;;
;;; Recursive find files in testbench base directory
;;;
(define* (utest/find-files-rec rx #:key (base "") (follow-symlink #f))
(find-files-rec-regexp
rx
(if (string-null? base)
(utest/base-path)
(format "~a/~a" (utest/base-path) base))
follow-symlink))
;;;
;;; Get path relative to base
;;;
(define (utest/base-rel path)
(if (absolute-file-name? path)
path
(path->absolute
(if (string-null? path)
(utest/base-path)
(string-append (utest/base-path) "/" path)))))
;;;
;;; Get path relative to work
;;;
(define (utest/work-rel path)
(if (absolute-file-name? path)
path
(path->absolute
(if (string-null? path)
(utest/work-path)
(string-append (utest/work-path) "/" path)))))
;;;
;;; Find files in testbench base directory
;;;
(define* (utest/find-files rx #:key (base "") (follow-symlink #f))
(let* ((base (utest/base-rel base))
(ls (list-dir base)))
(filter (lambda (f)
(and (not (string=? f "."))
(not (string=? f ".."))
(string-match rx f)))
ls)))
;;;
;;; Prepare argument list
;;; #f -> '()
;;; Value -> '(Value)
;;; '(...) -> '(...)
;;;
(define (arg-to-list arg)
(cond ((not arg)'())
((list? arg) arg)
(else (list arg))))
;;;
;;; Flatten list
;;;
(define (flatten x)
(cond ((null? x) '())
((pair? x) (append (flatten (car x)) (flatten (cdr x))))
(else (list x))))
;;;
;;; Trim list
;;;
(define (list-trim-left l pred)
(if (or (null? l) (not (pred (car l))))
l
(list-trim-left (cdr l) pred)))
(define (list-trim-right l pred)
(reverse (list-trim-left (reverse l) pred)))
(define (list-trim l pred)
(list-trim-right
(list-trim-left l pred)
pred))
;;;
;;; Execute system command and capture stdout and stderr to string list
;;;
(define* (system-to-string-list cmd #:key (pwd #f))
(let* ((cmd (string-append cmd " 2>&1"))
(cmd (if pwd (format "cd ~a; ~a" pwd cmd) cmd))
(p (open-input-pipe cmd))
(out (get-string-all p)))
(values
(close-pipe p)
(list-trim (string-split out #\newline) string-null?))))
;;;
;;; Compile code with Icarus Verilog
;;;
(define* (iverilog-compile sources
#:key
(iverilog-executable "iverilog")
(modpaths '()) ; -y
(modtypes '(".v" ".sv")) ; -Y
(includes '()) ; -I
(top #f) ; -s
(output #f) ; -o
(lang "2012") ; -g2012
(features '()) ; -g
(vpipaths '()) ; -L
(vpimods '()) ; -m
(libs '()) ; -l
(netlist #f) ; -N
(separate #f) ; -u
(warnings "all") ; -W
(defines '()) ; -D=X
(parameters '()) ; -P=X
(verbose #f)
(other '()))
(define (string-or-num-param x)
(if (number? x)
(format "~a" x)
(format "'\"~a\"'" x)))
(let ((opts
(cons
iverilog-executable
(append
(if verbose '("-v") '())
(if lang (list (format "-g~a" lang)) '())
(if output (list "-o" output) '())
(if separate '("-u") '())
(if netlist (list (format "-N~a" netlist)) '())
(flatten (map (lambda (x) (list "-s" x)) (arg-to-list top)))
(map (lambda (x) (format "-W~a" x)) (arg-to-list warnings))
(map (lambda (x) (format "-y~a" x)) (arg-to-list modpaths))
(map (lambda (x) (format "-Y~a" x)) (arg-to-list modtypes))
(map (lambda (x) (format "-I~a" x)) (arg-to-list includes))
(map (lambda (x) (format "-g~a" x)) (arg-to-list features))
(map (lambda (x) (format "-L~a" x)) (arg-to-list vpipaths))
(map (lambda (x) (format "-m~a" x)) (arg-to-list vpimods))
(map (lambda (x) (format "-l~a" x)) (arg-to-list libs))
(map (lambda (x)
(format "-P~a=~a"
(if (or (not top) (list? top))
(car x)
(format "~a.~a" top (car x)))
(string-or-num-param (cadr x))))
parameters)
(map (lambda (x)
(if (list? x)
(format "-D~a=~a" (car x) (string-or-num-param (cadr x)))
(format "-D~a" x)))
defines)
other
(arg-to-list sources)))))
(let ((cmdline (fold (lambda (x s) (string-append s x " ")) "" opts)))
(let-values (((status output)
(system-to-string-list cmdline)))
(values (= status 0) cmdline output)))))
;;;
;;; Run simulation of executable compiled with Icarus Verilog
;;;
(define* (iverilog-run vvp-binary
#:key
(vvp-executable "vvp")
(vpipaths '()) ; -M
(vpimods '()) ; -m
(dumpformat 'fst)
(plusargs '())
(verbose #f))
(let ((opts
(cons
vvp-executable
(append
(if verbose '("-v") '())
(map (lambda (x) (format "-M~a" x)) (arg-to-list vpipaths))
(map (lambda (x) (format "-m~a" x)) (arg-to-list vpimods))
(list "-N" vvp-binary) ; $finish on CTRL-C
(case dumpformat
((vcd) '("-vcd"))
((fst) '("-fst"))
((lxt) '("-lxt"))
((lxt2) '("-lxt2"))
((none) '("-none"))
(else '()))
(map (lambda (x) (format "+~a" x)) plusargs)))))
(let ((cmdline (fold (lambda (x s) (string-append s x " ")) "" opts)))
(let-values (((status output)
(system-to-string-list cmdline)))
(let ((status
;; Fix vvp issue (https://github.com/steveicarus/iverilog/issues/737)
(if (find (lambda (x) (string-prefix? "VCD Error: " x)) output) -1 status)))
(values (= status 0) cmdline output))))))
;;;
;;; Call iverilog-vpi tool
;;;
(define* (iverilog-compile-vpi sources
#:key
(iverilog-vpi-executable "iverilog-vpi")
(output-dir #f)
(name #f) ; --name
(libs '()) ; -l
(libdirs '()) ; -L
(includes '()) ; -I
(defines '())) ; -D
(define (string-or-num-param x)
(if (number? x)
(format "~a" x)
(format "'\"~a\"'" x)))
(let ((opts
(cons
iverilog-vpi-executable
(append
(if (and name (not (string-null? name))) (list (format "--name=~a" name)))
(map (lambda (x) (format "-l~a" x)) (arg-to-list libs))
(map (lambda (x) (format "-L~a" x)) (arg-to-list libdirs))
(map (lambda (x) (format "-I~a" x)) (arg-to-list includes))
(map (lambda (x)
(if (list? x)
(format "-D~a=~a" (car x) (string-or-num-param (cadr x)))
(format "-D~a" x)))
defines)
(arg-to-list sources)))))
(let* ((cmdline (fold (lambda (x s) (string-append s x " ")) "" opts)))
(let-values (((status output)
(system-to-string-list cmdline #:pwd output-dir)))
(values (= status 0) cmdline output)))))
;;;
;;; VPI compiler wrapper for run inside tests
;;;
(define* (utest/iverilog-compile-vpi sources
#:key
(iverilog-vpi-executable "iverilog-vpi")
(output-dir #f)
(name #f)
(libs '())
(libdirs '())
(includes '())
(defines '()))
(let ((base-path (utest/base-path))
(work-path (utest/work-path)))
(let ((sources (map (lambda (x) (path->absolute x base-path)) (arg-to-list sources)))
(libdirs (map (lambda (x) (path->absolute x base-path)) (arg-to-list libdirs)))
(includes (map (lambda (x) (path->absolute x base-path)) (arg-to-list includes))))
(let-values (((succ cmdl output)
(iverilog-compile-vpi sources
#:iverilog-vpi-executable iverilog-vpi-executable
#:output-dir (if output-dir output-dir work-path)
#:name name
#:libs libs
#:libdirs libdirs
#:includes includes
#:defines defines)))
;; Print command line and output
(printf "$ ~a\n" cmdl)
(for-each println output)
succ))))
;;;
;;; Check log for errors or warnings
;;;
(define (check-log log)
(let ((sim-fail-prefix (lt-log-prefix (log-type-by-id 'error)))
(sim-warn-prefix (lt-log-prefix (log-type-by-id 'warning))))
(cond
((find (lambda (x) (string-prefix? sim-fail-prefix x)) log) #f)
((find (lambda (x) (string-prefix? sim-warn-prefix x)) log) 'warning)
(else #t))))
;;;
;;; Return list of UTEST_* defines
;;;
(define (utest-verilog-defines)
(append
`(UTEST_TESTBENCH
(UTEST_BASE_DIR ,(format "'\"~a\"'" (utest/base-path)))
(UTEST_WORK_DIR ,(format "'\"~a\"'" (utest/work-path))))
(fold (lambda (x l)
(if (car x)
(append l (cdr x))
l))
'()
`((,(utest/verbose) UTEST_VERBOSE)
(,(utest/force-dump) UTEST_FORCE_DUMP)
(,(utest/keep-output) UTEST_KEEP_OUTPUT)
(,(utest/restart-dump) UTEST_RESTART_DUMP)))))
;;;
;;; Run compile and simulation with Icarus Verilog
;;;
(define* (utest/run-simulation-iverilog sources
top
#:key
(iverilog-executable "iverilog")
(vvp-executable "vvp")
(modpaths '())
(modtypes '(".v" ".sv"))
(includes '())
(lang "2012")
(parameters '())
(defines '())
(features '())
(separate #f)
(plusargs '())
(vpimods '())
(vpipaths '())
(warnings "all")
(dumpformat 'fst)
(timeout '(1 s)))
;; Get parameters
(let ((force-dump (utest/force-dump))
(base-path (utest/base-path))
(work-path (utest/work-path)))
;; Create helper modules - timeout watchdog and waveform dumper
(let ((timeout-module (create-timeout-module work-path TIMEOUT_MODULE_NAME timeout))
(dump-module (create-dump-module work-path DUMP_MODULE_NAME top dumpformat)))
;; Convert relative paths to absolute
(let ((sources (append (map (lambda (x) (path->absolute x base-path))
(arg-to-list sources))
(list timeout-module dump-module)))
(defines (append defines (utest-verilog-defines)))
(includes (append (map (lambda (x) (path->absolute x base-path)) (arg-to-list includes))
(list base-path)))
(modpaths (map (lambda (x) (path->absolute x base-path)) (arg-to-list modpaths)))
(vpipaths (map (lambda (x) (path->absolute x base-path))
(let ((vpipaths (arg-to-list vpipaths)))
(if (null? vpipaths) (list work-path) vpipaths))))
(execfile (format "~a/~a.vvp" work-path top)))
(let ((succ
;; Start compilation
(let-values
(((succ cmdl outp)
(iverilog-compile sources
#:iverilog-executable iverilog-executable #:modpaths modpaths
#:modtypes '(".v" ".sv") #:includes includes
#:top top #:other `("-s" ,TIMEOUT_MODULE_NAME "-s" ,DUMP_MODULE_NAME)
#:output execfile #:lang lang #:features features #:vpipaths vpipaths
#:vpimods vpimods #:separate separate #:warnings warnings #:defines defines
#:parameters parameters #:verbose (utest/verbose))))
;; Print iverilog command line and output
(printf "$ ~a\n" cmdl)
(for-each println outp)
;; Run simulation. On error the simulation will retry
;; with dump enabled, if needed
(if succ
(let retry ((dump force-dump))
(let-values
(((succ cmdl outp)
(iverilog-run execfile
#:vvp-executable vvp-executable #:vpipaths vpipaths
#:vpimods vpimods #:dumpformat (if dump dumpformat 'none)
#:plusargs plusargs #:verbose (utest/verbose))))
(let ((succ (if succ (check-log outp) succ)))
(if (or succ dump (not (utest/restart-dump)))
(begin
;; Print vvp command line and output
(printf "$ ~a\n" cmdl)
(for-each println outp)
succ)
(retry #t)))))
succ))))
succ)))))
;;;
;;; Return all test procs from make files
;;; Return list of pairs (base-dir . test-proc)
;;;
(define (collect-test-procs files)
(fold
(lambda (f procs)
(let* ((f (path->absolute f))
(base (dirname f)))
(append
procs
(filter
car
(map (lambda (proc) (list proc base (basename f)))
(let ((procs
(parameterize ((utest/base-path base)
(utest/work-path #f))
(load f))))
(if procs
(if (list? procs) procs
(if (procedure? procs)
(list procs)
'(#f)))
'(#f))))))))
'() files))
;;;
;;; Call test proc. Collect output to list of string
;;;
(define (call-test test work)
(let* ((pass #f)
(proc (car test))
(base (cadr test))
(name (proc 'name))
(name (format "~a~a/~a"
(let* ((pwd (string-append (path->absolute (getcwd)) "/"))
(base-loc
(if (string-prefix? pwd base)
(substring base (string-length pwd))
base)))
(if (string-null? base-loc)
""
(string-append base-loc "/")))
(caddr test)
(if name name "-")))
(descr (proc 'description))
(log
(string-split
(with-output-to-string
(lambda ()
(utest/log 'test-head "TEST ~a" name)
(when descr (utest/log 'test-info "~a" descr))
(utest/log "Base: ~a" base)
(utest/log "Work: ~a" work)
(set! pass
(with-exception-handler (lambda (e)
(display "EXCEPTION: ")
(display e) #f)
(lambda ()
(parameterize ((utest/base-path base)
(utest/work-path work))
(proc)))
#:unwind? #t))))
#\newline)))
;; Check log
(let ((log (append log
(list (if pass
(utest/slog 'test-succ "PASS")
(utest/slog 'test-fail "FAIL (~a)" (basename work)))))))
(values pass log))))
;;;
;;; Create temporary working directory, call test,
;;; print log to stdout and save verbose log to a file.
;;;
;;; test : (list test-proc base-path makefile-base-name)
;;; returns: (values pass output)
;;;
(define (execute-test test)
(let ((proc (car test))
(base (cadr test))
(makefile-name (caddr test)))
(let* ((name (proc 'name))
(name (if name name "noname"))
(work
(if (utest/static)
(let ((dir-name
(format "~a/~a~a-static"
base WORK_DIR_PREFIX
makefile-name)))
(when (not (access? dir-name W_OK))
(mkdir dir-name))
dir-name)
(mkdtemp (format "~a/~a~a-~a-~a-XXXXXX"
base WORK_DIR_PREFIX
makefile-name
(string-map (lambda (c) (if (char-whitespace? c) #\_ c))
(string-downcase name))
(current-time))))))
;; Execute test
(let* ((p #f)
(o (with-output-to-string
(lambda ()
(let-values (((pass log)
(call-test test work)))
(set! p pass)
;; Print log
(print-log log
#:colorize (not (utest/nocolor))
#:verbose (or (utest/verbose)
(not (eq? pass #t))))
;; Save log
(with-output-to-file (format "~a/log.txt" work)
(lambda () (print-log log #:colorize #f #:verbose #t)))
;; Delete work dir if test pass and no need to keep directory
(if (and (eq? pass #t)
(not (utest/force-dump))
(not (utest/keep-output)))
(begin
(when (utest/verbose)
(printf "Delete work dir ~a\n" work))
(delete-recursive work))
(printf "See output at ~a\n" work)))))))
(values p o)))))
;;;
;;; Execute tests in series (in one thread)
;;; tests : list-of (test-proc base-path makefile-base-name)
;;;
(define (execute-tests tests)
(let ((test-count (length tests))
(pass-count
(apply + (map (lambda (test)
(let-values (((pass out) (execute-test test)))
(println out)
(if pass 1 0)))
tests))))
(printf "PASSED ~a/~a\n\n" pass-count test-count)))
;;;
;;; Execute tests in parallel
;;; tests : list-of (test-proc base-path makefile-base-name)
;;;
(define (execute-tests-parallel tests max-threads-count)
(let ((test-count (length tests)))
(let loop ((tests tests)
(threads '())
(pass-count 0))
(if (and (null? tests)
(null? threads))
;; Done
(printf "PASSED ~a/~a\n\n" pass-count test-count)
;; Not all tests complete
(let ((threads-prev threads))
;; Run new thread if thread pool is not full
(let-values
(((tests threads)
(if (and (< (length threads) max-threads-count)
(not (null? tests)))
(let* ((test (car tests))
(thd
(call-with-new-thread
(lambda ()
(execute-test test)))))
(values (cdr tests) (cons thd threads)))
(values tests threads))))
;; Get exited threads
(let* ((trest.passc
(fold
(lambda (thd t.p)
(let ((trest (car t.p))
(passc (cdr t.p)))
(if (thread-exited? thd)
(let-values (((pass out) (join-thread thd)))
(println out)
(cons trest (+ passc (if pass 1 0))))
(cons (cons thd trest) passc))))
(cons '() pass-count) threads))
(threads (car trest.passc))
(pass-count (cdr trest.passc)))
;; Sleep when no new threads and no exited threads
(when (= (length threads-prev)
(length threads))
(usleep 10000)
(yield))
;; Loop
(loop tests threads pass-count))))))))
;;;
;;; Test item macro
;;;
(define-syntax utest/tb
(syntax-rules ()
((_ (n d ...) body ...)
(lambda id
(cond
((null? id) (begin body ...))
((eq? (car id) 'name) n)
((eq? (car id) 'description)
((lambda rest
(if (null? rest)
#f
(string-append
(car rest)
(apply string-append
(map (lambda (x) (string-append "\n" x))
(cdr rest))))))
d ...))
(else #f))))
((_ () body ...)
(utest/tb (#f) body ...))))
;;;
;;; Map combinations macro
;;;
(define-syntax utest/map-comb
(syntax-rules ()
((_ (args ...) (body ...) lists ...)
(apply
map
(lambda (args ...) (body ...))
(transpose
(combinations lists ...))))))
;;;
;;; Delete working folders
;;;
(define (delete-work-dirs base force)
(let ((work-dirs
(if force
(find-paths-rec
(lambda (p t)
(and (eq? t 'directory)
(or
(string-match
(format "^~a.*-[0-9]{10}-.{6}$" WORK_DIR_PREFIX)
(basename p))
(string-match
(format "^~a.*-static$" WORK_DIR_PREFIX)
(basename p)))))
base)
(fold