-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAddressGenerator.v
443 lines (381 loc) · 14.3 KB
/
AddressGenerator.v
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
/*
Copyright 2020, Ahmet Can Mert <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
`include "defines.v"
module AddressGenerator (input clk,reset,
input start,
output reg [`RING_DEPTH-`PE_DEPTH+1:0] raddr0,
output reg [`RING_DEPTH-`PE_DEPTH+1:0] waddr0,waddr1,
output reg wen0 ,wen1 ,
output reg brsel0,brsel1,
output reg brselen0,brselen1,
output reg [2*`PE_NUMBER*(`PE_DEPTH+1)-1:0] brscramble0,
output reg [`RING_DEPTH-`PE_DEPTH+2:0] raddr_tw,
output reg [4:0] stage_count,
output reg ntt_finished);
// ---------------------------------------------------------------------------
// Control signals
reg [4:0] c_stage_limit;
reg [`RING_DEPTH-`PE_DEPTH:0] c_loop_limit;
reg [`RING_DEPTH-`PE_DEPTH+2:0] c_tw_limit;
reg [4:0] c_stage;
reg [`RING_DEPTH-`PE_DEPTH:0] c_loop;
reg [`RING_DEPTH-`PE_DEPTH+2:0] c_tw;
reg [8:0] c_wait_limit;
reg [8:0] c_wait;
reg [`RING_DEPTH-`PE_DEPTH-1:0] raddr;
reg [1:0] raddr_m;
reg [`RING_DEPTH-`PE_DEPTH-1:0] waddre,waddro;
reg [1:0] waddr_m;
reg wen;
reg brsel;
reg brselen;
reg finished;
reg [2*`PE_NUMBER*(`PE_DEPTH+1)-1:0] brscramble;
// ---------------------------------------------------------------------------
// FSM
reg [1:0] state;
// 0 --> IDLE
// 1 --> NTT
// 2 --> NTT (WAIT between stages)
always @(posedge clk or posedge reset) begin
if(reset)
state <= 0;
else begin
case(state)
2'd0: begin
state <= (start) ? 1 : 0;
end
2'd1: begin
state <= (c_loop == c_loop_limit) ? 2 : 1;
end
2'd2: begin
if((c_stage == c_stage_limit) && (c_wait == c_wait_limit)) // operation is finished
state <= 0;
else if(c_wait == c_wait_limit) // to next NTT stage
state <= 1;
else // wait
state <= 2;
end
default: state <= 0;
endcase
end
end
// --------------------------------------------------------------------------- WAIT OPERATION
always @(posedge clk or posedge reset) begin
if(reset) begin
c_wait_limit <= 0;
c_wait <= 0;
end
else begin
c_wait_limit <= (start) ? 8'd15 : c_wait_limit;
if(state == 2'd2)
c_wait <= (c_wait < c_wait_limit) ? (c_wait + 1) : 0;
else
c_wait <= 0;
end
end
// --------------------------------------------------------------------------- c_stage & c_loop
always @(posedge clk or posedge reset) begin
if(reset) begin
c_stage_limit <= 0;
c_loop_limit <= 0;
end
else begin
if(start) begin
c_stage_limit <= (`RING_DEPTH-1);
c_loop_limit <= ((`RING_SIZE >> (`PE_DEPTH+1))-1);
end
else begin
c_stage_limit <= c_stage_limit;
c_loop_limit <= c_loop_limit;
end
end
end
always @(posedge clk or posedge reset) begin
if(reset) begin
c_stage <= 0;
c_loop <= 0;
end
else begin
if(start) begin
c_stage <= 0;
c_loop <= 0;
end
else begin
// ---------------------------- c_stage
if((state == 2'd2) && (c_wait == c_wait_limit) && (c_stage == c_stage_limit))
c_stage <= 0;
else if((state == 2'd2) && (c_wait == c_wait_limit))
c_stage <= c_stage + 1;
else
c_stage <= c_stage;
// ---------------------------- c_loop
if((state == 2'd2) && (c_wait == c_wait_limit))
c_loop <= 0;
else if((state == 2'd1) && (c_loop < c_loop_limit))
c_loop <= c_loop + 1;
else
c_loop <= c_loop;
end
end
end
// --------------------------------------------------------------------------- twiddle factors
wire [`RING_DEPTH-`PE_DEPTH+2:0] c_tw_temp;
assign c_tw_temp = (c_loop_limit>>c_stage);
always @(posedge clk or posedge reset) begin
if(reset) begin
c_tw <= 0;
end
else begin
if(start) begin
c_tw <= 0;
end
else begin
if((state == 2'd1) && (c_loop != c_loop_limit)) begin
if(c_stage == 0) begin
if(c_loop[0] == 0)
c_tw <= (((c_tw + ((1 << (`RING_DEPTH-`PE_DEPTH-2))>>c_stage))) & c_loop_limit);
else
c_tw <= (((c_tw + 1 - ((1 << (`RING_DEPTH-`PE_DEPTH-2))>>c_stage))) & c_loop_limit);
end
else if(c_stage >= (`RING_DEPTH-`PE_DEPTH-1)) begin
c_tw <= c_tw;
end
else begin
if(c_loop[0] == 0) begin
c_tw <= c_tw + ((1 << (`RING_DEPTH-`PE_DEPTH-2))>>c_stage)
- (((c_loop & c_tw_temp) == c_tw_temp) ? (((c_loop & c_tw_temp)>>1)+1) : 0);
end
else begin
c_tw <= (c_tw + 1) - ((1 << (`RING_DEPTH-`PE_DEPTH-2))>>c_stage)
- (((c_loop & c_tw_temp) == c_tw_temp) ? (((c_loop & c_tw_temp)>>1)+1) : 0);
end
end
end
else if((state == 2'd2) && (c_wait == c_wait_limit) && (c_stage == c_stage_limit))
c_tw <= 0;
else if((state == 2'd2) && (c_wait == c_wait_limit)) begin
c_tw <= c_tw+1;
end
else begin
c_tw <= c_tw;
end
end
end
end
// --------------------------------------------------------------------------- raddr (1 cc delayed)
wire [`RING_DEPTH-`PE_DEPTH-1:0] raddr_temp;
assign raddr_temp = ((`RING_DEPTH-`PE_DEPTH-1) - (c_stage+1));
always @ (posedge clk or posedge reset) begin
if(reset) begin
raddr <= 0;
raddr_m <= 0;
end
else begin
if(start) begin
raddr <= 0;
raddr_m <= 0;
end
else begin
// ---------------------------- raddr
if((state == 2'd2) && (c_wait == c_wait_limit))
raddr <= 0;
else if((state == 2'd1) && (c_loop <= c_loop_limit)) begin
if(c_stage < (`RING_DEPTH-`PE_DEPTH-1)) begin
if(~c_loop[0])
raddr <= (c_loop >> 1) + ((c_loop >> (raddr_temp+1)) << raddr_temp);
else
raddr <= (1 << raddr_temp) + (c_loop >> 1) + ((c_loop >> (raddr_temp+1)) << raddr_temp);
end
else
raddr <= c_loop;
end
else
raddr <= raddr;
// ---------------------------- raddr_m
if((state == 2'd2) && (c_wait == c_wait_limit))
raddr_m <= {raddr_m[1],~raddr_m[0]};
else
raddr_m <= raddr_m;
end
end
end
// --------------------------------------------------------------------------- waddr (1 cc delayed)
wire [`RING_DEPTH-`PE_DEPTH-1:0] waddr_temp;
assign waddr_temp = ((`RING_DEPTH-`PE_DEPTH-1) - (c_stage+1));
always @ (posedge clk or posedge reset) begin
if(reset) begin
waddre <= 0;
waddro <= 0;
waddr_m <= 0;
end
else begin
if(start) begin
waddre <= 0;
waddro <= (1 << (`RING_DEPTH-`PE_DEPTH-1));
waddr_m <= 1;
end
else begin
// ---------------------------- raddr
if((state == 2'd2) && (c_wait == c_wait_limit)) begin
waddre <= 0;
waddro <= 0;
end
else if((state == 2'd1) && (c_loop <= c_loop_limit)) begin
if(c_stage < (`RING_DEPTH-`PE_DEPTH-1)) begin
waddre <= (c_loop >> 1) + ((c_loop >> (waddr_temp+1)) << waddr_temp);
waddro <= (c_loop >> 1) + ((c_loop >> (waddr_temp+1)) << waddr_temp) + (1 << waddr_temp);
end
else begin
waddre <= c_loop;
waddro <= c_loop;
end
end
else begin
waddre <= waddre;
waddro <= waddro;
end
// ---------------------------- raddr_m
if((state == 2'd2) && (c_wait == c_wait_limit) && (c_stage == (c_stage_limit-1)))
waddr_m <= 2'b10;
else if((state == 2'd2) && (c_wait == c_wait_limit))
waddr_m <= {waddr_m[1],~waddr_m[0]};
else
waddr_m <= waddr_m;
end
end
end
// --------------------------------------------------------------------------- wen,brsel,brselen (1 cc delayed)
always @(posedge clk or posedge reset) begin
if(reset) begin
wen <= 0;
brsel <= 0;
brselen <= 0;
end
else begin
if(state == 2'd1) begin
wen <= 1;
brsel <= c_loop[0];
brselen <= 1;
end
else begin
wen <= 0;
brsel <= 0;
brselen <= 0;
end
end
end
// --------------------------------------------------------------------------- brscrambled
wire [`PE_DEPTH:0] brscrambled_temp;
wire [`PE_DEPTH:0] brscrambled_temp2;
wire [`PE_DEPTH:0] brscrambled_temp3;
assign brscrambled_temp = (`PE_NUMBER >> (c_stage-(`RING_DEPTH-`PE_DEPTH-1)));
assign brscrambled_temp2 = (`PE_DEPTH - (c_stage-(`RING_DEPTH-`PE_DEPTH-1)));
assign brscrambled_temp3 = ((`PE_DEPTH+1) - (c_stage-(`RING_DEPTH-`PE_DEPTH-1)));
always @(posedge clk or posedge reset) begin: B_BLOCK
integer n;
for(n=0; n < (2*`PE_NUMBER); n=n+1) begin: LOOP_1
if(reset) begin
brscramble[(`PE_DEPTH+1)*n+:(`PE_DEPTH+1)] <= 0;
end
else begin
if(c_stage >= (`RING_DEPTH-`PE_DEPTH-1)) begin
brscramble[(`PE_DEPTH+1)*n+:(`PE_DEPTH+1)] <= (brscrambled_temp*n[0]) +
(((n>>1)<<1) & (brscrambled_temp-1)) +
((n>>(brscrambled_temp2+1))<<(brscrambled_temp3)) +
((n>>brscrambled_temp2) & 1);
end
else begin
brscramble[(`PE_DEPTH+1)*n+:(`PE_DEPTH+1)] <= 0;
end
end
end
end
// --------------------------------------------------------------------------- ntt_finished
always @(posedge clk or posedge reset) begin
if(reset) begin
finished <= 0;
end
else begin
if((state == 2'd2) && (c_wait == c_wait_limit) && (c_stage == c_stage_limit))
finished <= 1;
else
finished <= 0;
end
end
// --------------------------------------------------------------------------- delays
// -------------------- read signals
wire [`RING_DEPTH-`PE_DEPTH+2:0] c_tw_w;
ShiftReg #(.SHIFT(1),.DATA(`RING_DEPTH-`PE_DEPTH+3)) sr00(clk,reset,c_tw,c_tw_w);
always @(posedge clk or posedge reset) begin
if(reset) begin
raddr0 <= 0;
raddr_tw <= 0;
end
else begin
raddr0 <= {raddr_m,raddr};
raddr_tw <= c_tw_w;
end
end
// -------------------- write signals (waddr0/1, wen0/1, brsel0/1, brselen0/1)
// waddr0/1
wire [`RING_DEPTH-`PE_DEPTH+1:0] waddre_w,waddro_w;
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY ),.DATA(`RING_DEPTH-`PE_DEPTH+2)) sr01(clk,reset,{waddr_m,waddre},waddre_w);
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY+1),.DATA(`RING_DEPTH-`PE_DEPTH+2)) sr02(clk,reset,{waddr_m,waddro},waddro_w);
always @(*) begin
waddr0 = waddre_w;
waddr1 = waddro_w;
end
// wen0/1
wire [0:0] wen0_w,wen1_w;
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY ),.DATA(1)) sr03(clk,reset,wen,wen0_w);
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY+1),.DATA(1)) sr04(clk,reset,wen,wen1_w);
always @(*) begin
wen0 = wen0_w;
wen1 = wen1_w;
end
// brsel
wire [0:0] brsel0_w,brsel1_w;
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY ),.DATA(1)) sr05(clk,reset,brsel,brsel0_w);
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY+1),.DATA(1)) sr06(clk,reset,brsel,brsel1_w);
always @(*) begin
brsel0 = brsel0_w;
brsel1 = brsel1_w;
end
// brselen
wire [0:0] brselen0_w,brselen1_w;
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY ),.DATA(1)) sr07(clk,reset,brselen,brselen0_w);
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY+1),.DATA(1)) sr08(clk,reset,brselen,brselen1_w);
always @(*) begin
brselen0 = brselen0_w;
brselen1 = brselen1_w;
end
// stage count
wire [4:0] c_stage_w;
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY+1),.DATA(5)) sr09(clk,reset,c_stage,c_stage_w);
always @(*) begin
stage_count = c_stage_w;
end
// brascambled
wire [2*`PE_NUMBER*(`PE_DEPTH+1)-1:0] brscramble_w;
ShiftReg #(.SHIFT(`INTMUL_DELAY+`MODRED_DELAY+`STAGE_DELAY),.DATA(2*`PE_NUMBER*(`PE_DEPTH+1))) sr10(clk,reset,brscramble,brscramble_w);
always @(*) begin
brscramble0 = brscramble_w;
end
// ntt finished
wire finished_w;
ShiftReg #(.SHIFT(4),.DATA(1)) sr11(clk,reset,finished,finished_w);
always @(*) begin
ntt_finished = finished_w;
end
endmodule