-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsingle_core.ml
More file actions
311 lines (294 loc) · 7.74 KB
/
Copy pathsingle_core.ml
File metadata and controls
311 lines (294 loc) · 7.74 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
open! Base
open! Hardcaml
module Make (Config : Core_config.S) = struct
open! Signal
module Var = Always.Variable
let logn = Config.logn
let n = 1 lsl logn
let datapath_latency = Core_config.datapath_latency
module Controller = Controller.Make (Config)
module Datapath = Datapath.Make (Config)
module I = struct
type 'a t =
{ clock : 'a
; clear : 'a
; start : 'a
; first_iter : 'a
; first_4step_pass : 'a
; d1 : 'a [@bits Gf.Signal.num_bits]
; d2 : 'a [@bits Gf.Signal.num_bits]
}
[@@deriving sexp_of, hardcaml]
end
module O = struct
type 'a t =
{ q1 : 'a [@bits Gf.Signal.num_bits]
; q2 : 'a [@bits Gf.Signal.num_bits]
; addr1_in : 'a [@bits logn]
; addr2_in : 'a [@bits logn]
; read_enable_in : 'a
; addr1_out : 'a [@bits logn]
; addr2_out : 'a [@bits logn]
; write_enable_out : 'a
; first_stage : 'a
; last_stage : 'a
; twiddle_stage : 'a
; flip : 'a
; done_ : 'a
}
[@@deriving sexp_of, hardcaml]
end
let create ?row scope (i : _ I.t) =
let spec = Reg_spec.create ~clock:i.clock ~clear:i.clear () in
let controller =
Controller.hierarchy
scope
{ Controller.I.clock = i.clock
; clear = i.clear
; start = i.start
; first_iter = i.first_iter
; first_4step_pass = i.first_4step_pass
}
in
let datapath =
Datapath.hierarchy
?row
scope
{ Datapath.I.clock = i.clock
; clear = i.clear
; start = i.start
; first_iter = i.first_iter
; d1 = i.d1
; d2 = i.d2
; omegas = controller.omegas
; start_twiddles = controller.start_twiddles
; twiddle_stage = controller.twiddle_stage
; twiddle_update = controller.twiddle_update
}
in
let pipe = pipeline spec ~n:(datapath_latency + 1) in
{ O.q1 = datapath.q1
; q2 = datapath.q2
; addr1_in = controller.addr1
; addr2_in = controller.addr2
; read_enable_in = controller.read_write_enable
; addr1_out = controller.addr1 |> pipe
; addr2_out = controller.addr2 |> pipe
; write_enable_out = controller.read_write_enable |> pipe
; first_stage = controller.first_stage
; last_stage = controller.last_stage
; twiddle_stage = controller.twiddle_stage
; flip = controller.flip
; done_ = controller.done_
}
;;
let hierarchy ?row scope =
let module Hier = Hierarchy.In_scope (I) (O) in
Hier.hierarchical ~name:"core" ~scope (create ?row)
;;
end
module With_rams (Config : Core_config.S) = struct
open! Signal
let logn = Config.logn
let n = 1 lsl logn
let datapath_latency = Core_config.datapath_latency
let ram_latency = Core_config.ram_latency
module Core = Make (Config)
module I = struct
type 'a t =
{ clock : 'a
; clear : 'a
; start : 'a
; first_4step_pass : 'a
; first_iter : 'a
; flip : 'a
; wr_d : 'a [@bits Gf.Signal.num_bits]
; wr_en : 'a
; wr_addr : 'a [@bits logn]
; rd_en : 'a
; rd_addr : 'a [@bits logn]
}
[@@deriving sexp_of, hardcaml]
end
module O = struct
type 'a t =
{ done_ : 'a
; rd_q : 'a [@bits Gf.Signal.num_bits]
}
[@@deriving sexp_of, hardcaml]
end
let input_ram
scope
build_mode
~clock
~clear
~flip
~wr_addr
~wr_d
~wr_en
~addr1_in
~addr2_in
~read_enable_in
=
Bram.create_dual
(Scope.sub_scope scope "ram_in")
~build_mode
~size:n
~read_latency:ram_latency
~clock
~clear
~flip
~write_port_a:{ address = wr_addr; data = wr_d; enable = wr_en }
~write_port_b:{ address = zero logn; data = zero Gf.Signal.num_bits; enable = gnd }
~read_port_a:{ address = reverse addr1_in; enable = read_enable_in }
~read_port_b:{ address = reverse addr2_in; enable = read_enable_in }
;;
let transpose_ram
scope
build_mode
~clock
~clear
~addr1_in
~addr1_out
~q1
~addr2_in
~addr2_out
~q2
~read_enable_in
~write_enable_out
~flip
~last_stage
=
let scope = Scope.sub_scope scope "ram_transp" in
let ( -- ) = Scope.naming scope in
let q0, q1 =
Bram.create_dual
scope
~build_mode
~size:n
~read_latency:ram_latency
~clock
~clear
~flip
~write_port_a:
{ address = addr1_out; data = q1; enable = write_enable_out &: ~:last_stage }
~write_port_b:
{ address = addr2_out; data = q2; enable = write_enable_out &: ~:last_stage }
~read_port_a:{ address = addr1_in; enable = read_enable_in }
~read_port_b:{ address = addr2_in; enable = read_enable_in }
in
q0 -- "q0", q1 -- "q1"
;;
let output_ram
scope
build_mode
~clock
~clear
~flip
~last_stage
~twiddle_stage
~rd_addr
~rd_en
~addr1_out
~q1
~addr2_out
~q2
~write_enable_out
=
let q, _ =
Bram.create_dual
(Scope.sub_scope scope "ram_out")
~build_mode
~size:n
~read_latency:ram_latency
~clock
~clear
~flip
~write_port_a:
{ address = addr1_out
; data = q1
; enable = write_enable_out &: (last_stage &: ~:twiddle_stage)
}
~write_port_b:
{ address = addr2_out; data = q2; enable = write_enable_out &: last_stage }
~read_port_a:{ address = rd_addr; enable = rd_en }
~read_port_b:{ address = zero logn; enable = gnd }
in
q
;;
let create ?row ~build_mode scope (i : _ I.t) =
let spec = Reg_spec.create ~clock:i.clock ~clear:i.clear () in
let core = Core.O.Of_signal.wires () in
let pipe ~n d = pipeline spec ~n d in
(* input and output rams *)
let d_in_0, d_in_1 =
input_ram
scope
build_mode
~clock:i.clock
~clear:i.clear
~flip:i.flip
~wr_addr:i.wr_addr
~wr_d:i.wr_d
~wr_en:i.wr_en
~addr1_in:core.addr1_in
~addr2_in:core.addr2_in
~read_enable_in:core.read_enable_in
in
let d_out_0, d_out_1 =
transpose_ram
scope
build_mode
~clock:i.clock
~clear:i.clear
~addr1_in:core.addr1_in
~addr1_out:core.addr1_out
~q1:core.q1
~addr2_in:core.addr2_in
~addr2_out:core.addr2_out
~q2:core.q2
~read_enable_in:core.read_enable_in
~write_enable_out:core.write_enable_out
~flip:core.flip
~last_stage:core.last_stage
in
(* core *)
Core.O.iter2
core
(Core.hierarchy
?row
scope
(let first_stage = pipe ~n:Core_config.ram_latency core.first_stage in
{ clock = i.clock
; clear = i.clear
; start = i.start
; first_iter = i.first_iter
; first_4step_pass = i.first_4step_pass
; d1 = mux2 first_stage d_in_0 d_out_0
; d2 = mux2 first_stage d_in_1 d_out_1
}))
~f:( <== );
let d_out =
output_ram
scope
build_mode
~clock:i.clock
~clear:i.clear
~flip:i.flip
~last_stage:(pipe ~n:(datapath_latency + 1) core.last_stage)
~twiddle_stage:(pipe ~n:(datapath_latency + 1) core.twiddle_stage)
~rd_addr:i.rd_addr
~rd_en:i.rd_en
~addr1_out:core.addr1_out
~q1:core.q1
~addr2_out:core.addr2_out
~q2:core.q2
~write_enable_out:core.write_enable_out
in
{ O.done_ = core.done_; rd_q = d_out }
;;
let hierarchy ?row ?instance ~build_mode scope =
let module Hier = Hierarchy.In_scope (I) (O) in
Hier.hierarchical ~name:"ntt_with_rams" ?instance ~scope (create ?row ~build_mode)
;;
end