aboutsummaryrefslogtreecommitdiff
path: root/src/main/stanza/verilog.stanza
blob: 19472573c8e09d25276439aac70d8e67eb51fe6c (plain)
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
defpackage firrtl/verilog :
   import core
   import verse
   import firrtl/ir-utils
   import firrtl/ir2

public defstruct Verilog <: Pass :
   file : String
public defmethod pass (b:Verilog) -> (Circuit -> Circuit) : emit-verilog{file(b),_}
public defmethod name (b:Verilog) -> String : "To Verilog"
public defmethod short-name (b:Verilog) -> String : "To Verilog"

;============ Utilz =============
defn width! (w:Width) -> Int :
   match(w) : 
      (w:IntWidth) : width(w)
      (w) : error("Non-supported width type.")

defn width! (t:Type) -> Int :
   match(t) :
      (t:UIntType) : width!(width(t))
      (t:SIntType) : width!(width(t))
      (t) : error("Non-supported type.")

defn emit (w:Width) -> String :
   match(w) : 
      (w:IntWidth) : string-join $ ["[" width(w) ":0]"] ;TODO check if need to special case 0 or 1 width wires
      (w) : error("Non-supported width type.")

defn get-width (t:Type) -> String :
   match(t) :
      (t:UIntType) : emit(width(t))
      (t:SIntType) : emit(width(t))
      (t) : error("Non-supported type.")

defn remove-subfield (e:Expression) -> Expression :
   match(map(remove-subfield,e)) :
      (e:Subfield) : Ref(to-symbol $ string-join $ [emit(exp(e)) "_" name(e)],type(e))
      (e) : e


;============ Verilog Backend =============

defn emit (e:Expression) -> String :
   match(e) :
      (e:Ref) : to-string $ name(e)
      (e:UIntValue) : string-join $ [width!(type(e)) "'d" value(e)]
      (e:SIntValue) : string-join $ [width!(type(e)) "'sd" value(e)]
      (e:Subfield) : error("Non-supported expression")
      (e:Index) : error("Non-supported expression")
      (e:Register) : error("Non-supported expression")
      (e:ReadPort) : error("Non-supported expression")
      (e:WritePort) : error("Non-supported expression")
      (e:DoPrim) : string-join $ switch {_ == op(e)} :
         ADD-OP : [emit(args(e)[0]) " + " emit(args(e)[1])]
         SUB-OP : [emit(args(e)[0]) " - " emit(args(e)[1])]
         MUL-OP : [emit(args(e)[0]) " * " emit(args(e)[1])]
         DIV-OP : [emit(args(e)[0]) " / " emit(args(e)[1])]
         MOD-OP : [emit(args(e)[0]) " % " emit(args(e)[1])]
         QUO-OP : [emit(args(e)[0]) " / " emit(args(e)[1])]
         REM-OP : [emit(args(e)[0]) " % " emit(args(e)[1])]
         ADD-WRAP-OP : [emit(args(e)[0]) " + " emit(args(e)[1])]
         SUB-WRAP-OP : [emit(args(e)[0]) " - " emit(args(e)[1])]
         LESS-OP : [emit(args(e)[0]) " < " emit(args(e)[1])]
         LESS-EQ-OP : [emit(args(e)[0]) " <= " emit(args(e)[1])]
         GREATER-OP : [emit(args(e)[0]) " > " emit(args(e)[1])]
         GREATER-EQ-OP : [emit(args(e)[0]) " >= " emit(args(e)[1])]
         NEQUAL-OP : [emit(args(e)[0]) " != " emit(args(e)[1])]
         EQUAL-OP : [emit(args(e)[0]) " == " emit(args(e)[1])]
         MUX-OP : [emit(args(e)[0]) " ? " emit(args(e)[1]) " : " emit(args(e)[2])]
         PAD-OP : 
            val x = args(e)[0]
            val w = width!(type(x))
            val diff = consts(e)[0] - w
            ["{" diff "{" x "[" w - 1 "]}," emit(x)]
         AS-UINT-OP : 
            ["$unsigned(" emit(args(e)[0]) " "]
         AS-SINT-OP : 
            ["$signed(" emit(args(e)[0]) " "]
         DYN-SHIFT-LEFT-OP : [emit(args(e)[0]) " << " emit(args(e)[1])]
         DYN-SHIFT-RIGHT-OP : [emit(args(e)[0]) " >> " emit(args(e)[1])]
         SHIFT-LEFT-OP : [emit(args(e)[0]) " << " emit(args(e)[1])]
         SHIFT-RIGHT-OP : [emit(args(e)[0]) " >> " emit(args(e)[1])]
         NEG-OP : ["-{" emit(args(e)[0]) "}"]
         CONVERT-OP : 
            match(type(args(e)[0])) :
               (t:UIntType) : ["{1'b0," emit(args(e)[0]) "}"]
               (t:SIntType) : [emit(args(e)[0])]
         BIT-NOT-OP : ["!" emit(args(e)[0])]
         BIT-AND-OP : [emit(args(e)[0]) " & " emit(args(e)[1])]
         BIT-OR-OP : [emit(args(e)[0]) " | " emit(args(e)[1])]
         BIT-XOR-OP : [emit(args(e)[0]) " ^ " emit(args(e)[1])]
         CONCAT-OP : ["{" emit(args(e)[0]) "," emit(args(e)[1]) "}"]
         BIT-SELECT-OP : [emit(args(e)[0]) "[" consts(e)[0] "]"]
         BITS-SELECT-OP : [emit(args(e)[0]) "[" consts(e)[1] ":" consts(e)[0] "]"]
         BIT-AND-REDUCE-OP : 
            var v = emit(args(e)[0])
            for x in tail(args(e)) do :
               v = concat(v, [" & " emit(x)])
            v
         BIT-OR-REDUCE-OP : 
            var v = emit(args(e)[0])
            for x in tail(args(e)) do :
               v = concat(v, [" | " emit(x)])
            v
         BIT-XOR-REDUCE-OP : 
            var v = emit(args(e)[0])
            for x in tail(args(e)) do :
               v = concat(v, [" ^ " emit(x)])
            v

defn emit-module (m:Module) :
   val wires = Vector<Streamable>()
   val regs = Vector<Streamable>()
   val inits = Vector<Streamable>()
   val assigns = Vector<Streamable>()
   val updates = Vector<Streamable>()
   val insts = HashTable<Symbol,Symbol>(symbol-hash) ; inst -> module
   val inst-ports = HashTable<Symbol,Vector<Streamable>>(symbol-hash)
   
   defn emit-s (s:Stmt) :
      match(map(remove-subfield,s)) :
         (s:DefWire) : add(wires,["wire " get-width(type(s)) " " name(s) ";"])
         (s:DefInstance) : 
            inst-ports[name(s)] = Vector<Streamable>()
            insts[name(s)] = name(module(s) as Ref)
            for f in fields(type(module(s)) as BundleType) do :
               ;val sf = value(s) as Subfield
               ;val e = exp(sf) as Ref
               val n* = to-symbol $ string-join $ [name(s) "_" name(f)]
               add(wires,["wire " get-width(type(f)) " " n* ";"])
               add(inst-ports[name(s)], ["." name(f) "( " n* " )"])
         (s:DefMemory) :
            val vtype = type(s) as VectorType
            val innerwidth = 
            add(regs,["reg  " get-width(type(vtype)) " " name(s) " [0:" size(vtype) "];"])
            add(inits,["for (initvar = 0; initvar < " size(vtype) "; initvar = initvar+1)"])
            add(inits,[name(s) " = {" width!(type(vtype)) "{$random}};"])
         (s:DefNode) :
            if value(s) typeof Register :
               val reg = value(s) as Register
               add(regs,["reg  " get-width(type(reg)) " " name(s) ";"])
               add(inits,[name(s) " = {" width!(type(reg)) "{$random}};"])
               add(updates,["if(" emit(enable(reg)) ") begin"])
               add(updates,["  " name(s) " <= " emit(value(reg)) ";"])
               add(updates,["end"])
            else if value(s) typeof ReadPort :
               val rp = value(s) as ReadPort
               add(assigns,["assign " name(s) " = " emit(mem(rp)) "[" emit(index(rp)) "];"])
            else :
               add(wires,["wire " get-width(type(value(s))) " " name(s) ";"])
               add(assigns,["assign " name(s) " = " emit(value(s)) ";"])
         (s:Begin) : do(emit-s, body(s))
         (s:Connect) : 
            if loc(s) typeof WritePort :
               val wp = loc(s) as WritePort
               add(updates,["if(" emit(enable(wp)) ") begin"])
               add(updates,["  " emit(mem(wp)) "[" emit(index(wp)) "] <= " emit(exp(s)) ";"])
               add(updates,["end"])
            else :
               add(assigns,["assign " emit(loc(s)) " = " emit(exp(s)) ";"])
         (s) : s 
   
   emit-s(body(m))

   ;==== Actually printing module =====
   val port-indent = "    "
   print-all(["module " name(m) "(input clk, input reset,\n"])
   for (p in ports(m),i in 1 to false) do :
      if name(p) !=`reset :
         var end = ",\n"
         if length(ports(m)) - 1 == i :
            end = "\n);\n"
         switch {_ == direction(p)} :
            INPUT  : print-all([port-indent "input  " get-width(type(p)) " " name(p) end])
            OUTPUT : print-all([port-indent "output " get-width(type(p)) " " name(p) end])

   for w in wires do :
      print("  ")
      println-all(w)
   for r in regs do :
      print("  ")
      println-all(r)

   if length(inits) != 0 :
      println("`ifndef SYNTHESIS")
      println("  integer initvar;")
      println("  initial begin")
      println("    #0.002;")
      for i in inits do :
         print-all("    ")
         println-all(i)
      println("  end")
      println("`endif")

   for a in assigns do :
      print("  ")
      println-all(a)

   for x in insts do :
      println-all(["  " value(x) " " key(x) ".clk(clk),"])
      for (y in inst-ports[key(x)],i in 1 to false) do :
        print("       ")
        print-all(y)
        if length(inst-ports[key(x)]) != i :
          print(",\n")
      println("\n  );")

   if length(updates) != 0 :
      println("  always @(posedge clk) begin")
      for u in updates do :
         print("    ")
         println-all(u)
      println("  end")
   
   println("endmodule")


public defn emit-verilog (file:String, c:Circuit) :
   with-output-file{file, _} $ fn () :
      for m in modules(c) do :
         emit-module(m)
   c