Repository navigation
Expand file tree
/
Copy pathverilog.clj
More file actions
1137 lines (1044 loc) · 40.4 KB
/
Copy pathverilog.clj
File metadata and controls
1137 lines (1044 loc) · 40.4 KB
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
(ns piplin.verilog
(:refer-clojure :exclude [replace cast])
(:require [piplin.walk :as walk]
[plumbing.core :as plumb])
(:use [slingshot.slingshot])
(:use [clojure.walk :only [postwalk]])
(:use [clojure.set :only [map-invert]])
(:use [clojure.string :only [join replace]])
(:use [piplin [modules :exclude [verilog]] types])
(:use [swiss-arrows.core :only [-<>>]])
(:require [piplin.types.core-impl :as impl])
(:use [piplin.types.sints :only [sign-extend sints]])
(:use [piplin.types.uintm :only [uintm]])
(:use [piplin.types.array :only [array?]])
(:use [piplin.types.complex :only [real-part imag-part]])
(:use [piplin.types [bits :only [bit-width-of bits deserialize serialize bit-cat bit-slice]]])
(:require [piplin.mux :as mux]
[piplin.types.binops :as binops])
(:use [piplin [types :only [piplin-clojure-dispatch]] protocols [util :only [let']]]))
(defn sanitize-str
"Takes a string and makes it safe for verilog."
[s]
(replace (munge (name s)) "." "_DOT_"))
(declare render-single-expr)
(defn array-width-decl
"Takes a width `x` and returns the string `\"[x:0]\"`,
or `\"\"` if x is one"
[x]
(if (= 1 x)
""
(str "[" (dec x) ":0]")))
(defn format-verilog
"Takes a wire name, its width, and the corresponding
verilog, and returns the formatted `wire` declaration."
[width name verilog & args]
(format " wire %s %s = %s;\n"
(array-width-decl width) name (apply format verilog args)))
(defn gen-verilog-name
"Takes a string, symbol, or keyword, and returns a unique
similar verilog name."
([] (name (gensym)))
([base-name]
(-> base-name
name
sanitize-str
gensym
name)))
(defn make-union-verilog
[tag padding value]
(str "{" tag
(when (pos? padding)
(str ", " padding "'b" 0))
", " value "}"))
(defmulti verilog-repr
kindof
:hierarchy types)
(defmethod verilog-repr :default
[x]
(throw+ (error "cannot convert" (kindof x) "to verilog:" x)))
;TODO: why must this method be included?
;I cannot figure out where it fits in the output
;probably used by bit-slice as a const, but not actually
;used so it doesn't appear in output
(defmethod verilog-repr :j-long
[x]
(str "j-long is unconvertible {" x \}))
(defmethod verilog-repr :uintm
[x]
(let [t (typeof x)
i (value x)
w (bit-width-of t)]
(str w "'d" i)))
(defmethod verilog-repr :sints
[x]
(let [t (typeof x)
i (value x)
w (bit-width-of t)]
(str (when (neg? i) "-")
w "'d"
(if (neg? i) (- i) i))))
(defmethod verilog-repr :sfxpts
[x]
(verilog-repr (serialize x)))
(defmethod verilog-repr :complex
[x]
(verilog-repr (serialize x)))
(defmethod verilog-repr :boolean
[x]
(if x "1'b1" "1'b0"))
(defmethod verilog-repr :bits
[x]
(let [t (typeof x)
b (value x)
w (bit-width-of t)]
(if (zero? w)
"0'b0"
(str w "'b" (join b)))))
(defmethod verilog-repr :enum
[x]
(let [t (typeof x)
keymap (:keymap t)
e (value x)
w (bit-width-of t)]
(str (verilog-repr (keymap e)))))
(defmethod verilog-repr :union
[x]
(let [t (typeof x)
{:keys [schema enum]} t
v (first (value x))
tag (key v)
v (val v)
w (bit-width-of t)
padding (- w
(bit-width-of enum)
(bit-width-of (tag schema)))]
(make-union-verilog
(verilog-repr (enum tag))
padding
(verilog-repr v)
)))
(defmethod verilog-repr :bundle
[x]
(verilog-repr (serialize x)))
(defmethod verilog-repr :array
[x]
(verilog-repr (serialize x)))
(defn lookup-expr
[table expr]
(if (pipinst? expr)
(verilog-repr expr)
(if-let [name (get table expr)]
name
(do
#_"UNKNOWN_QUANTITY"
(throw+ (error expr "not found in" table))))))
(defmulti verilog-of
(fn [ast name-lookup] (if (pipinst? ast)
::immediate
(:op (value ast)))))
(defmethod verilog-of :default
[ast name-lookup]
(throw+ (error "cannot convert" (:op (value ast)) "to verilog:" ast)))
(defmethod verilog-of ::immediate
[ast name-lookup]
[(verilog-repr ast)])
(defmethod verilog-of :port
[ast name-lookup]
[(lookup-expr name-lookup ast)])
(defn get-args
[ast]
(:args (value ast)))
(defmacro let-args
[ast name-lookup argvec & body]
(let [lookups (mapcat #(vector %
`(lookup-expr ~name-lookup ~%))
argvec)]
`(let [{:keys ~argvec} (get-args ~ast)
~@lookups]
~@body)))
(defmethod verilog-of :make-union
[ast name-lookup]
(let [t (typeof ast)
{:keys [schema enum]} t
{:keys [tag val]} (get-args ast)
w (bit-width-of t)
padding (- w
(bit-width-of enum)
(bit-width-of (tag schema)))]
[(make-union-verilog
(lookup-expr name-lookup (enum tag))
padding
(lookup-expr name-lookup val))]))
(defmethod verilog-of :get-value
[ast name-lookup]
(let [valtype (typeof ast)
union (:u (get-args ast))
top (dec (bit-width-of valtype))]
[(str (lookup-expr name-lookup union)
"[" top (when-not (zero? top) ":0") "]")]))
(defmethod verilog-of :get-tag
[ast name-lookup]
(let [enum (typeof ast)
union (:u (get-args ast))
top (dec (bit-width-of (typeof union)))
bottom (inc (- top (bit-width-of enum)))]
[(str (lookup-expr name-lookup union)
"[" top
(when-not (= top bottom)
(str ":" bottom))
"]")]))
(defmethod verilog-of :make-bundle
[ast name-lookup]
(let [schema-ks (keys (:schema (typeof ast)))
bundle-inst (get-args ast)
ordered-vals (map #(lookup-expr name-lookup
(get bundle-inst %))
schema-ks)]
[(str "{" (join ", " ordered-vals) "}")]))
(defn compute-key-offsets
"Returns a map from keys to pairs. The pairs
are the low (inclusive) to high (inclusive)
bit indices in the verilog representation."
[bundle-type]
(let [schema (:schema bundle-type)
key-widths (map (comp bit-width-of
(partial get schema))
(keys schema))
offsets (reductions + (conj key-widths 0))
total (reduce + key-widths)
pairs (partition 2 1 (map (partial - total) offsets))
pairs (map (fn [[x y]] [(dec x) y]) pairs)
]
(into {} (map vector (keys schema) pairs))))
(defmethod verilog-of :bundle-assoc
[ast name-lookup]
(let [{:keys [bund k v]} (get-args ast)
t (typeof ast)
offsets (compute-key-offsets t)
w (bit-width-of t)
[high low] (get offsets k)
v (lookup-expr name-lookup v)
bund (lookup-expr name-lookup bund)]
[(str "{"
(when-not (= (dec w) high)
(str bund "[" (dec w) ":" (inc high) "], "))
v
(when-not (= low 0)
(str ", " bund "[" (dec low) ":0]"))
"}")]))
(defmethod verilog-of :bundle-key
[ast name-lookup]
(let [{:keys [bundle key]} (get-args ast)
offsets (compute-key-offsets (typeof bundle))
[high low] (get offsets key)]
[(str (lookup-expr name-lookup bundle) "[" high ":" low "]")]))
(defmethod verilog-of :make-array
[ast name-lookup]
(let [keys (->> (typeof ast)
:array-len
range
(map (comp keyword str)))
array-inst (get-args ast)
ordered-vals (map #(lookup-expr name-lookup
(get array-inst %))
keys)]
[(str "{" (join ", " ordered-vals) "}")]))
(defmethod verilog-of :array-get
[ast name-lookup]
(let [{:keys [array i]} (get-args ast)
subtype-width (bit-width-of (:array-type (typeof array)))
index (lookup-expr name-lookup i)]
[(str
(lookup-expr name-lookup array)
"["
(lookup-expr name-lookup i)
"]")]))
(defmethod verilog-of :array-nth
[ast name-lookup]
(let [{:keys [array i]} (get-args ast)]
[(lookup-expr name-lookup (get array (keyword (str i))))]))
#_(defmethod verilog-of :array-assoc
[ast name-lookup]
(let [{:keys [array index v]} (get-args ast)
{:keys [array-len array-type]} (typeof array)
upper-bound (dec (* (bit-width-of array-type) (inc index)))
lower-bound (* (bit-width-of array-type) index)
array-sym (lookup-expr name-lookup array)
arg-sym (lookup-expr name-lookup v)]
(str "{"
(when-not (= (dec (bit-width-of (typeof ast))) high)
(str bund "[" (dec w) ":" (inc high) "], "))
v
(when-not (= low 0)
(str ", " bund "[" (dec low) ":0]"))
"}")))
(defmethod verilog-of :slice
[ast name-lookup]
(let [{:keys [expr high low]} (get-args ast)]
[(str (lookup-expr name-lookup expr) "[" (dec high) ":" low "]")]))
(defmethod verilog-of :bit-cat
[ast name-lookup]
(let-args ast name-lookup [b1 b2]
(let [{b1-ast :b1 b2-ast :b2} (get-args ast)
b1-zero? (zero? (-> b1-ast typeof bit-width-of))
b2-zero? (zero? (-> b2-ast typeof bit-width-of))]
(cond
(and b1-zero? b2-zero?)
(throw+ (error "Cannot concat 2 zero-width signals"))
b1-zero? [b2]
b2-zero? [b1]
:else
[(str "{" b1 ", " b2 "}")]))))
(defmethod verilog-of :mux2
[ast name-lookup]
(let [t (typeof ast)
{:keys [sel v1 v2]} (get-args ast)]
[(str (lookup-expr name-lookup sel) " ? "
(lookup-expr name-lookup v1) " : "
(lookup-expr name-lookup v2))]))
(defmethod verilog-of :sign-extend
[ast name-lookup]
(let [orig-width (-> ast
value
:args
:num
typeof
bit-width-of)
new-width (-> ast
typeof
bit-width-of)]
(let-args ast name-lookup [num]
[(format "{ {%d{%s[%d]}}, %s}"
(- new-width orig-width)
num (dec orig-width) num)])))
(defmethod verilog-of :real-part
[ast name-lookup]
(let [{:as complex-type
:keys [imag]} (-> (get-args ast)
:complex
typeof)]
(let-args ast name-lookup [complex]
[(format "%s[%d:%d]"
complex
(dec (bit-width-of complex-type))
(bit-width-of imag))])))
(defmethod verilog-of :imag-part
[ast name-lookup]
(let [{:keys [imag]} (-> (get-args ast)
:complex
typeof)]
(let-args ast name-lookup [complex]
[(format "%s[%d:0]"
complex
(dec (bit-width-of imag)))])))
(defmethod verilog-of :+
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
(condp = (kindof ast)
:uintm
[(str lhs " + " rhs)]
:sintm
[(str "$signed(" lhs ") + $signed(" rhs ")")]
:complex
(let' [{:keys [lhs rhs]} (get-args ast)
r1 (real-part lhs)
i1 (imag-part lhs)
r2 (real-part rhs)
i2 (imag-part rhs)
r' (impl/+ r1 r2)
i' (impl/+ i1 i2)
r'-bits (serialize r')
i'-bits (serialize i')
result (bit-cat r'-bits i'-bits)
[name-lookup' body] (walk/compile
result
render-single-expr
name-lookup [])]
[(name-lookup' result)
(vec body)])
:sfxpts
(let [{:as type
:keys [i f]} (typeof ast)
width (bit-width-of type)
{:keys [lhs rhs]} (get-args ast)
lhs-sints (->> (serialize lhs)
(deserialize (sints width)))
rhs-sints (->> (serialize rhs)
(deserialize (sints width)))]
(verilog-of (impl/+ lhs-sints
rhs-sints)
(assoc name-lookup
lhs-sints (name-lookup lhs)
rhs-sints (name-lookup rhs)))
)
:sints
(let [type (typeof ast)
width (bit-width-of type)
lhs-tmp-name (gen-verilog-name "lhs")
lhs-tmp (format-verilog width lhs-tmp-name lhs)
rhs-tmp-name (gen-verilog-name "rhs")
rhs-tmp (format-verilog width rhs-tmp-name rhs)
extended-sum-name (gen-verilog-name "extended_sum")
extended-sum (format-verilog
(inc width)
extended-sum-name
"{%s[%d],%s} + {%s[%d],%s}"
lhs-tmp-name (dec width) lhs-tmp-name
rhs-tmp-name (dec width) rhs-tmp-name)
extended-sum-msb (str extended-sum-name "[" width "]")
overflow? (str "(" extended-sum-msb " == "
extended-sum-name "[" (dec width) "])")
overflow-case (str "(" extended-sum-msb " == 1'b0) ? "
(verilog-repr
(piplin.types.sints/max-value type))
" : "
(verilog-repr
(piplin.types.sints/min-value type)))]
[(str overflow? " ? "
extended-sum-name (array-width-decl width)
" : " overflow-case)
[lhs-tmp rhs-tmp extended-sum]])
(throw+ (error "Cannot add" (kindof ast))))))
(defmethod verilog-of :-
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
(condp = (kindof ast)
:uintm
[(str lhs " - " rhs)]
:sints
(let' [{rhs-ast :rhs lhs-ast :lhs} (get-args ast)
rhs-type (typeof rhs-ast)
rhs-width (bit-width-of rhs-type)
rhs-extended-type (sints (inc rhs-width))
raw-diff (->> rhs-ast
(sign-extend (inc rhs-width))
serialize
impl/bit-not
(deserialize (-> rhs-width
inc
uintm))
impl/inc
serialize
(deserialize rhs-extended-type)
(impl/+ (sign-extend
(inc rhs-width)
lhs-ast)))
difference (-<>> raw-diff
serialize
(bit-slice <> 0 rhs-width)
(deserialize rhs-type))
msb0 (-> raw-diff
serialize
(bit-slice rhs-width (inc rhs-width)))
msb1 (-> raw-diff
serialize
(bit-slice (dec rhs-width) rhs-width))
overflow? (->> (impl/bit-xor msb0 msb1)
serialize
(deserialize
(anontype :boolean)))
min-val (piplin.types.sints/min-value
rhs-type)
max-val (piplin.types.sints/max-value
rhs-type)
result (mux/mux2 overflow?
(mux/mux2 (binops/= #b0 msb0)
max-val
min-val)
difference)
[name-lookup' body] (walk/compile
result
render-single-expr
name-lookup [])]
[(name-lookup' result)
(vec body)])
:sfxpts
(let [{:as type
:keys [i f]} (typeof ast)
width (bit-width-of type)
{:keys [lhs rhs]} (get-args ast)
lhs-sints (->> (serialize lhs)
(deserialize (sints width)))
rhs-sints (->> (serialize rhs)
(deserialize (sints width)))]
(verilog-of (impl/- lhs-sints
rhs-sints)
(assoc name-lookup
lhs-sints (name-lookup lhs)
rhs-sints (name-lookup rhs))))
)))
(defmethod verilog-of :*
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
(condp = (kindof ast)
:uintm
[(str lhs " * " rhs)]
:sfxpts
(let' [{:as type
:keys [i f]} (typeof ast)
width (bit-width-of type)
{:keys [lhs rhs]} (get-args ast)
lhs-sints (->> (serialize lhs)
(deserialize (sints width))
(sign-extend (+ f width)))
rhs-sints (->> (serialize rhs)
(deserialize (sints width))
(sign-extend (+ f width)))
sints-* (impl/* lhs-sints rhs-sints)
[name-lookup' body] (walk/compile
sints-*
render-single-expr
name-lookup [])
sints-tmp-name (gen-verilog-name "full_result")
sints-tmp (format-verilog
(+ f width)
sints-tmp-name
(name-lookup' sints-*))]
[(format "%s[%d:%d]"
sints-tmp-name
(+ f (dec width))
f)
(conj (vec body) sints-tmp)])
:complex
(let' [{:keys [lhs rhs]} (get-args ast)
r1 (real-part lhs)
i1 (imag-part lhs)
r2 (real-part rhs)
i2 (imag-part rhs)
r' (impl/- (impl/* r1 r2) (impl/* i1 i2))
i' (impl/+ (impl/* i1 r2) (impl/* r1 i2))
r'-bits (serialize r')
i'-bits (serialize i')
result (bit-cat r'-bits i'-bits)
[name-lookup' body] (walk/compile
result
render-single-expr
name-lookup [])]
[(name-lookup' result)
(vec body)])
:sints
(let [type (typeof ast)
width (bit-width-of type)
lhs-tmp-name (gen-verilog-name "lhs")
lhs-tmp (format-verilog width lhs-tmp-name lhs)
rhs-tmp-name (gen-verilog-name "rhs")
rhs-tmp (format-verilog width rhs-tmp-name rhs)
prod-sym (gen-verilog-name "prod")
prod (format-verilog
width
prod-sym
"%s * %s"
lhs-tmp-name rhs-tmp-name)
lhs-pos?-sym (gen-verilog-name "lhs_pos")
rhs-pos?-sym (gen-verilog-name "rhs_pos")
lhs-pos? (format-verilog 1 lhs-pos?-sym "~%s[%d]" lhs-tmp-name (dec width))
rhs-pos? (format-verilog 1 rhs-pos?-sym "~%s[%d]" rhs-tmp-name (dec width))
lhs-leading-0s-sym (gen-verilog-name "lhs_leading_zeros")
lhs-leading-0s-clauses (->> (range (dec width))
(map #(format "~|%s[%d:%d] ? %d :"
lhs-tmp-name
(dec width) %
(- width %)))
(join " ")
(format "%s 1"))
lhs-leading-0s (format-verilog
(log2 width) lhs-leading-0s-sym
lhs-leading-0s-clauses)
rhs-leading-0s-sym (gen-verilog-name "rhs_leading_zeros")
rhs-leading-0s-clauses (->> (range (dec width))
(map #(format "~|%s[%d:%d] ? %d :"
rhs-tmp-name
(dec width) %
(- width %)))
(join " ")
(format "%s 1"))
rhs-leading-0s (format-verilog
(log2 width) rhs-leading-0s-sym
rhs-leading-0s-clauses)
lhs-leading-1s-sym (gen-verilog-name "lhs_leading_ones")
lhs-leading-1s-clauses (->> (range (dec width))
(map #(format "&%s[%d:%d] ? %d :"
lhs-tmp-name
(dec width) %
(- width %)))
(join " ")
(format "%s 1"))
lhs-leading-1s (format-verilog
(log2 width) lhs-leading-1s-sym
lhs-leading-1s-clauses)
rhs-leading-1s-sym (gen-verilog-name "rhs_leading_ones")
rhs-leading-1s-clauses (->> (range (dec width))
(map #(format "&%s[%d:%d] ? %d :"
rhs-tmp-name
(dec width) %
(- width %)))
(join " ")
(format "%s 1"))
rhs-leading-1s (format-verilog
(log2 width) rhs-leading-1s-sym
rhs-leading-1s-clauses)
pos-pos-ovf?-sym (gen-verilog-name "pos_pos_ovf")
pos-pos-ovf? (format-verilog
1 pos-pos-ovf?-sym
"(%s + %s < %d) | ((%s + %s == %d) & %s[%d])"
lhs-leading-0s-sym rhs-leading-0s-sym width
lhs-leading-0s-sym rhs-leading-0s-sym width
prod-sym (dec width))
neg-neg-ovf?-sym (gen-verilog-name "neg_neg_ovf")
neg-neg-ovf? (format-verilog
1 neg-neg-ovf?-sym
"(%s + %s < %d) | ((%s + %s == %d || %s + %s == %d) & (%s[%d] || %s == %s))"
lhs-leading-1s-sym rhs-leading-1s-sym width
lhs-leading-1s-sym rhs-leading-1s-sym width
lhs-leading-1s-sym rhs-leading-1s-sym (inc width)
prod-sym (dec width)
prod-sym (verilog-repr
((piplin.types.bits/bits width)
(vec (repeat width 0)))))
negative-leading-sym (gen-verilog-name "neg_leading")
negative-leading (format-verilog
(log2 width) negative-leading-sym
"~%s ? %s : %s"
lhs-pos?-sym
lhs-leading-1s-sym rhs-leading-1s-sym)
positive-leading-sym (gen-verilog-name "pos_leading")
positive-leading (format-verilog
(log2 width) positive-leading-sym
"~%s ? %s : %s"
lhs-pos?-sym
rhs-leading-0s-sym lhs-leading-0s-sym)
neg-pos-ovf?-sym (gen-verilog-name "neg_pos_ovf")
neg-pos-ovf? (format-verilog
1 neg-pos-ovf?-sym
"(%s + %s < %d) | ~%s[%d]"
negative-leading-sym positive-leading-sym
width prod-sym (dec width))
zero-sym (format "%d'd0" width)
has-zero?-sym (gen-verilog-name "zero")
has-zero? (format-verilog
1 has-zero?-sym
"%s == %s || %s == %s"
lhs-tmp-name zero-sym
rhs-tmp-name zero-sym)
min-val (verilog-repr
(piplin.types.sints/min-value type))
max-val (verilog-repr
(piplin.types.sints/max-value type))
]
[(format "%s ? %s : ((%s & %s & %s) | (~%s & ~%s & %s)) ? %s : ((%s ^ %s) & %s) ? %s : %s"
has-zero?-sym zero-sym
lhs-pos?-sym rhs-pos?-sym pos-pos-ovf?-sym
lhs-pos?-sym rhs-pos?-sym neg-neg-ovf?-sym
max-val
lhs-pos?-sym rhs-pos?-sym neg-pos-ovf?-sym
min-val
prod-sym
)
[lhs-tmp rhs-tmp has-zero? prod lhs-pos? rhs-pos? lhs-leading-0s lhs-leading-1s rhs-leading-0s rhs-leading-1s
pos-pos-ovf? neg-neg-ovf? negative-leading positive-leading neg-pos-ovf?]]
))))
(defmethod verilog-of :>
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " > " rhs)]))
(defmethod verilog-of :>=
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " >= " rhs)]))
(defmethod verilog-of :<
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " < " rhs)]))
(defmethod verilog-of :<=
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " <= " rhs)]))
(defmethod verilog-of :bit-shift-left
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " << " rhs)]))
(defmethod verilog-of :bit-shift-right
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " >> " rhs)]))
(defmethod verilog-of :bit-and
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " & " rhs)]))
(defmethod verilog-of :bit-or
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " | " rhs)]))
(defmethod verilog-of :bit-xor
[ast name-lookup]
(let-args ast name-lookup [lhs rhs]
[(str lhs " ^ " rhs)]))
(defmethod verilog-of :bit-not
[ast name-lookup]
(let-args ast name-lookup [x]
[(str "~" x)]))
(defmethod verilog-of :not
[ast name-lookup]
(let-args ast name-lookup [x]
[(str "~" x)]))
(defmethod verilog-of :and
[ast name-lookup]
(let-args ast name-lookup [x y]
[(str x " & " y)]))
(defmethod verilog-of :or
[ast name-lookup]
(let-args ast name-lookup [x y]
[(str x " | " y)]))
(defmethod verilog-of :=
[ast name-lookup]
[(let [{:keys [x y]} (get-args ast)]
(str (lookup-expr name-lookup x) " == " (lookup-expr name-lookup y)))])
(defn verilog-noop-passthrough
([ast name-lookup]
(verilog-noop-passthrough ast name-lookup :expr)
)
([ast name-lookup passthrough]
(let [args (get-args ast)]
(when-not (contains? args passthrough)
(throw+ (error (str "must have an " passthrough)
args)))
(when (not= 1 (count args))
(throw+ (error "must have exactly one expr")))
(verilog-of (passthrough args) name-lookup))))
(defmethod verilog-of :cast
[ast name-lookup]
(verilog-noop-passthrough ast name-lookup))
(defmethod verilog-of :noop
[ast name-lookup]
(verilog-noop-passthrough ast name-lookup))
(defmethod verilog-of :serialize
[ast name-lookup]
(verilog-noop-passthrough ast name-lookup))
(defmethod verilog-of :deserialize
[ast name-lookup]
(verilog-noop-passthrough ast name-lookup :bits))
(defn render-single-expr
"Takes an expr and a name table and
returns an updated name table and a string
to add to the text of the verilog"
[expr name-table]
(cond
(pipinst? expr)
[(verilog-repr expr) ""]
(= :use-core-impl (piplin-clojure-dispatch expr))
(do
(println "WARNING: trying to render clojure type, skipping")
[nil ""])
:else
(let [name (if-let [let-name (-> expr meta :let-name)]
(gen-verilog-name let-name)
(gen-verilog-name))
bit-width (bit-width-of (typeof expr))
[body structural] (verilog-of expr name-table)]
[name
(conj (vec structural)
(format-verilog bit-width name body))])))
(defn verilog
([expr name-table]
(verilog expr name-table ""))
([expr name-table text]
(let [[name-table body]
(walk/compile expr render-single-expr
name-table [])]
[name-table (str text (join body))])))
(defn assert-hierarchical
[indent dut-name var val cycle]
(let [dut-var var
assertion-str (str dut-var " !== " val)]
(str indent "if (" assertion-str ") begin\n"
indent " $display(\"Cycle " cycle ": failed assertion: " assertion-str ", instead is %b\", " dut-var ");\n"
indent " $finish;\n"
indent "end\n")))
(defn assert-hierarchical-cycle
"indent is prepended as indentation (usually whitespace).
dut-name is the name of the dut module; registers will
be asserted hierarchically rooted at the dut.
cycle-map is a map from vectors of keywords representing
the hierarchical path to a register (excluding the dut's name)
to the values."
[indent dut-name cycle-map cycle]
(reduce (fn [text [path val]]
(when-not (vector? path)
(throw+ "Path must be a vector"))
(let [array? (= :array (kindof val))]
(str text
(if-not array?
(assert-hierarchical indent dut-name
(join \_ (map sanitize-str path))
(verilog-repr val)
cycle)
"" #_(->> (map (fn [val index]
(assert-hierarchical
indent dut-name
(str (join \. (map sanitize-str path))
\[ index \])
(verilog-repr val)
cycle))
val (range))
(join "\n"))))))
""
cycle-map))
(defn register-declarations
"This takes a map from register port objects to verilog names,
and a seq of register configurations (i.e. maps containing the keys
`:piplin.modules/port` and `:piplin.modules/init`) and returns a
string which is a block of `reg [3:0] foo = 4'd7;`-style verilog
register declarations."
[port-names module-regs]
(join (map
;The anonymous fn groups the initial value and port object
(fn [{init :piplin.modules/init
port :piplin.modules/port}]
;declare a register of proper bit width, name,
;and default value
(if (= (kindof init) :array)
;TODO some arrays are written with stores,
;but others are written all at once. We should
;compile the latter to normal registers instead
;of verilog memories.
(let [{:keys [array-type array-len]} (typeof init)
inits (map #(format " %s[%d] = %s;\n"
(port-names port)
%2
(verilog-repr %1))
init
(range))]
(format
" reg %s %s %s;\n initial begin\n%s end\n"
(-> array-type
bit-width-of
array-width-decl)
;looks up gensym name
(port-names port)
;array width
(array-width-decl array-len)
(join inits)))
(format
" reg %s %s = %s;\n"
(-> init
typeof
bit-width-of
array-width-decl)
;looks up gensym name
(port-names port)
;must be using an immediate by contract
(verilog-repr init))))
module-regs)))
(defn register-assignments
"This takes a name-table and a seq of register configurations
(i.e. maps containing the keys `:piplin.modules/port` and
`:piplin.modules/fn`) and returns a string which is a block of
` foo <= my_value48529`\n`-style verilog register assignments."
[name-table module-regs]
(join (map
;each module-regs entry gets the following:
; - convert the register conf to the pair of port+fn
; - take that pair and lookup their true names in verilog
; - make proper decl
(comp (fn [[n v]]
(format " %s <= %s;\n" n v))
(juxt (comp name-table :piplin.modules/port)
(comp name-table :piplin.modules/fn)))
module-regs)))
(defn memory-stores
"This takes a name-table and a seq of store operations
and returns a string which is a block of gated
` foo <= my_value48529`\n`-style verilog register assignments."
[name-table stores]
(join (map
(fn [{i :piplin.modules/index
we :piplin.modules/write-enable?
v :piplin.modules/value
mem :piplin.modules/dest}]
(format " if (%s) %s[%s] <= %s;\n"
(name-table we)
(name-table mem)
(name-table i)
(name-table v)))
stores)))
(defn ->verilog
[compiled-module outputs]
(let [;seq of all the input ports used in the module
module-inputs (find-inputs compiled-module)
;map from register port object to their verilog names
port-names (plumb/for-map
[[name v] compiled-module]
(:piplin.modules/port v) (gen-verilog-name (last name)))
;map from input port object to their verilog name
input-names (plumb/for-map [port module-inputs
:let [name (-> port value :port)]]
port name)
;seqs of the keys that are registers, wires, and stores
{:keys [reg-keys store-keys wire-keys]} (module-keys-by-type compiled-module)
;string containing the register decls
regs-inits (register-declarations
port-names
(map compiled-module reg-keys))
;Compile the main logic
;collect all scalar roots
scalar-roots (map (comp :piplin.modules/fn second) compiled-module)
memory-roots (mapcat (comp (juxt
:piplin.modules/index
:piplin.modules/write-enable?
:piplin.modules/value
:piplin.modules/dest)
second)
compiled-module)
[name-table code]
(->> (concat scalar-roots memory-roots)
;TODO remove this once nil is synthesizable
(remove nil?)
;could insert a filter here to allow DCE to work
(reduce (fn [[name-table text] expr]
(verilog expr name-table text))
[(merge port-names
input-names) ""]))
;string containing the register assigns
reg-assigns (register-assignments
name-table
(->> reg-keys
(map compiled-module)
(remove (comp array?
:piplin.modules/init))))
;string containing the memory stores
memory-stores (memory-stores