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
|
`timescale 1ns / 1ps
module modexps6_montgomery_multiplier
(
clk,
ena, rdy,
operand_width,
x_bram_addr, x_bram_out,
y_bram_addr, y_bram_out,
n_bram_addr, n_bram_out,
z_bram_addr, z_bram_wr, z_bram_in, z_bram_out,
n0_modinv
);
//
// Parameters
//
parameter OPERAND_NUM_BITS = 11; // 1024 -> 11 bits
parameter OPERAND_ADDR_WIDTH = 5; // 1024 / 32 = 32 -> 5 bits
//
// Locals
//
localparam [OPERAND_ADDR_WIDTH:0] round_count_zero = {1'b0, {OPERAND_ADDR_WIDTH{1'b0}}};
localparam [OPERAND_ADDR_WIDTH:0] bram_addr_zero = {1'b0, {OPERAND_ADDR_WIDTH{1'b0}}};
//
// Ports
//
input wire clk;
input wire ena;
output wire rdy;
input wire [ OPERAND_NUM_BITS-1:0] operand_width;
output wire [OPERAND_ADDR_WIDTH :0] x_bram_addr;
input wire [ 31:0] x_bram_out;
output wire [OPERAND_ADDR_WIDTH :0] y_bram_addr;
input wire [ 31:0] y_bram_out;
output wire [OPERAND_ADDR_WIDTH :0] n_bram_addr;
input wire [ 31:0] n_bram_out;
output wire [OPERAND_ADDR_WIDTH :0] z_bram_addr;
output wire z_bram_wr;
output wire [ 31:0] z_bram_in;
input wire [ 31:0] z_bram_out;
input wire [ 31:0] n0_modinv;
//
// FSM
//
localparam FSM_STATE_IDLE = 6'd0;
localparam FSM_STATE_INIT = 6'd10;
localparam FSM_STATE_MUL_XY_CALC = 6'd21;
localparam FSM_STATE_MUL_XY_PIPELINE = 6'd22;
localparam FSM_STATE_MUL_XY_REGISTER = 6'd23;
localparam FSM_STATE_MUL_XY_WRITE = 6'd24;
localparam FSM_STATE_MAGIC_CALC = 6'd31;
localparam FSM_STATE_MAGIC_PIPELINE = 6'd32;
localparam FSM_STATE_MAGIC_REGISTER = 6'd33;
localparam FSM_STATE_MUL_MN_CALC = 6'd41;
localparam FSM_STATE_MUL_MN_PIPELINE = 6'd42;
localparam FSM_STATE_MUL_MN_REGISTER = 6'd43;
localparam FSM_STATE_MUL_MN_WRITE = 6'd44;
localparam FSM_STATE_SHIFT = 6'd50;
localparam FSM_STATE_ROUND = 6'd55;
localparam FSM_STATE_FINAL = 6'd60;
reg [ 5: 0] fsm_state = FSM_STATE_IDLE;
//
// Trigger
//
reg ena_dly = 1'b0;
always @(posedge clk) ena_dly <= ena;
wire ena_trig = (ena == 1'b1) && (ena_dly == 1'b0);
//
// Ready Register
//
reg rdy_reg = 1'b0;
assign rdy = rdy_reg;
//
// Enable / Ready Logic
//
always @(posedge clk)
//
if (fsm_state == FSM_STATE_FINAL) begin
//
rdy_reg <= 1'b1;
//
end else if (fsm_state == FSM_STATE_IDLE) begin
//
if (rdy_reg && !ena) rdy_reg <= 1'b0;
//
end
//
// X, Y, N BRAM Interface
//
reg [OPERAND_ADDR_WIDTH:0] x_bram_addr_reg = bram_addr_zero;
reg [OPERAND_ADDR_WIDTH:0] y_bram_addr_reg = bram_addr_zero;
reg [OPERAND_ADDR_WIDTH:0] n_bram_addr_reg = bram_addr_zero;
assign x_bram_addr = x_bram_addr_reg;
assign y_bram_addr = y_bram_addr_reg;
assign n_bram_addr = n_bram_addr_reg;
//
// Z BRAM Interface
//
reg [OPERAND_ADDR_WIDTH:0] z_bram_addr_reg = bram_addr_zero;
reg z_bram_wr_reg = 1'b0;
reg [ 31:0] z_bram_in_mux;
assign z_bram_addr = z_bram_addr_reg;
assign z_bram_wr = z_bram_wr_reg;
assign z_bram_in = z_bram_in_mux;
//
// Handy Wires
//
wire [OPERAND_ADDR_WIDTH-1:0] operand_width_msb = operand_width[OPERAND_NUM_BITS-1:OPERAND_NUM_BITS-OPERAND_ADDR_WIDTH];
wire [OPERAND_ADDR_WIDTH :0] bram_addr_last = {operand_width_msb, 1'b1}; // +1
//
// Hardware Multiplier (X * Y)
//
reg [31: 0] multiplier_xy_carry_in;
wire [31: 0] multiplier_xy_out;
wire [31: 0] multiplier_xy_carry_out;
modexps6_adder64_carry32 dsp_multiplier_xy
(
.clk (clk),
.t (/*(z_bram_addr_reg < bram_addr_last) ? */z_bram_out/* : {32{1'b0}}*/),
.x (/*(z_bram_addr_reg < bram_addr_last) ? */x_bram_out/* : {32{1'b0}}*/),
.y (/*(z_bram_addr_reg < bram_addr_last) ? */y_bram_out/* : {32{1'b0}}*/),
.s (multiplier_xy_out),
.c_in (multiplier_xy_carry_in),
.c_out (multiplier_xy_carry_out)
);
//
// Hardware Multiplier (Magic)
//
wire [63: 0] multiplier_magic_out;
reg [31: 0] magic_value_reg;
multiplier_s6 dsp_multiplier_magic
(
.clk (clk),
.a (z_bram_out),
.b (n0_modinv),
.p (multiplier_magic_out)
);
//
// Hardware Multiplier (M * N)
//
reg [31: 0] multiplier_mn_carry_in;
wire [31: 0] multiplier_mn_out;
wire [31: 0] multiplier_mn_carry_out;
modexps6_adder64_carry32 dsp_multiplier_mn
(
.clk (clk),
.t (z_bram_out),
.x (magic_value_reg),
.y (/*(z_bram_addr_reg < bram_addr_last) ? */n_bram_out/* : {32{1'b0}}*/),
.s (multiplier_mn_out),
.c_in (multiplier_mn_carry_in),
.c_out (multiplier_mn_carry_out)
);
//
// Z BRAM Input Selector
//
always @(*)
//
case (fsm_state)
FSM_STATE_INIT:
//
z_bram_in_mux = {32{1'b0}};
FSM_STATE_MUL_XY_WRITE:
//
if (z_bram_addr_reg < bram_addr_last) z_bram_in_mux = multiplier_xy_out;
else z_bram_in_mux = multiplier_xy_carry_in;
FSM_STATE_MUL_MN_WRITE:
//
if (z_bram_addr_reg < bram_addr_last) z_bram_in_mux = multiplier_mn_out;
else z_bram_in_mux = multiplier_mn_carry_in + z_bram_out;
FSM_STATE_SHIFT:
//
z_bram_in_mux = z_bram_out;
default:
//
z_bram_in_mux = {32{1'bX}};
endcase
//
// Handy Functions
//
function [OPERAND_ADDR_WIDTH:0] bram_addr_next_or_zero;
input [OPERAND_ADDR_WIDTH:0] bram_addr;
begin
bram_addr_next_or_zero = (bram_addr < bram_addr_last) ? bram_addr + 1'b1 : bram_addr_zero;
end
endfunction
function [OPERAND_ADDR_WIDTH:0] bram_addr_next_or_last;
input [OPERAND_ADDR_WIDTH:0] bram_addr;
begin
bram_addr_next_or_last = (bram_addr < bram_addr_last) ? bram_addr + 1'b1 : bram_addr_last;
end
endfunction
function [OPERAND_ADDR_WIDTH:0] bram_addr_prev_or_zero;
input [OPERAND_ADDR_WIDTH:0] bram_addr;
begin
bram_addr_prev_or_zero = (bram_addr > bram_addr_zero) ? bram_addr - 1'b1 : bram_addr_zero;
end
endfunction
//
// Round Counter
//
reg [OPERAND_ADDR_WIDTH:0] round_count = round_count_zero;
wire [OPERAND_ADDR_WIDTH:0] round_count_last = {operand_width_msb, 1'b0};
wire [OPERAND_ADDR_WIDTH:0] round_count_next = (round_count < round_count_last) ? round_count + 1'b1 : round_count_zero;
//
// Main Logic
//
always @(posedge clk)
//
case (fsm_state)
FSM_STATE_INIT: begin
//
z_bram_wr_reg <= (z_bram_addr_reg < bram_addr_last) ? 1'b1 : 1'b0;
z_bram_addr_reg <= z_bram_wr_reg ? bram_addr_next_or_zero(z_bram_addr_reg) : bram_addr_zero;
//
end
FSM_STATE_MUL_XY_CALC: begin
//
if (z_bram_addr_reg == bram_addr_zero) begin
//
multiplier_xy_carry_in <= {32{1'b0}};
//
end
//
end
FSM_STATE_MUL_XY_REGISTER: begin
//
z_bram_wr_reg <= 1'b1;
//
end
FSM_STATE_MUL_XY_WRITE: begin
//
z_bram_wr_reg <= 1'b0;
z_bram_addr_reg <= bram_addr_next_or_zero(z_bram_addr_reg);
//
x_bram_addr_reg <= bram_addr_next_or_zero(x_bram_addr_reg);
//
multiplier_xy_carry_in <= multiplier_xy_carry_out;
//
end
FSM_STATE_MUL_MN_CALC: begin
//
if (z_bram_addr_reg == bram_addr_zero) begin
//
multiplier_mn_carry_in <= {32{1'b0}};
//
magic_value_reg <= multiplier_magic_out[31:0];
//
end
//
end
FSM_STATE_MUL_MN_REGISTER: begin
//
z_bram_wr_reg <= 1'b1;
//
end
FSM_STATE_MUL_MN_WRITE: begin
//
z_bram_wr_reg <= 1'b0;
z_bram_addr_reg <= bram_addr_next_or_last(z_bram_addr_reg);
//
n_bram_addr_reg <= bram_addr_next_or_zero(n_bram_addr_reg);
//
multiplier_mn_carry_in <= multiplier_mn_carry_out;
//
end
FSM_STATE_SHIFT: begin
//
if (z_bram_wr_reg == 1'b0) z_bram_wr_reg <= 1'b1;
else if (z_bram_addr_reg == bram_addr_zero) z_bram_wr_reg <= 1'b0;
z_bram_addr_reg <= bram_addr_prev_or_zero(z_bram_addr_reg);
//
end
FSM_STATE_ROUND: begin
//
y_bram_addr_reg <= (round_count < round_count_last) ? bram_addr_next_or_zero(y_bram_addr_reg) : bram_addr_zero;
//
round_count <= round_count_next;
//
end
endcase
//
// FSM Transition Logic
//
always @(posedge clk)
//
case (fsm_state)
//
FSM_STATE_IDLE: fsm_state <= (!rdy_reg && ena_trig) ? FSM_STATE_INIT : FSM_STATE_IDLE;
FSM_STATE_INIT: fsm_state <= (z_bram_addr < bram_addr_last ) ? FSM_STATE_INIT : FSM_STATE_MUL_XY_CALC;
FSM_STATE_ROUND: fsm_state <= (round_count < round_count_last) ? FSM_STATE_MUL_XY_CALC : FSM_STATE_FINAL;
FSM_STATE_MUL_XY_CALC: fsm_state <= FSM_STATE_MUL_XY_PIPELINE;
FSM_STATE_MAGIC_CALC: fsm_state <= FSM_STATE_MAGIC_PIPELINE;
FSM_STATE_MUL_MN_CALC: fsm_state <= FSM_STATE_MUL_MN_PIPELINE;
FSM_STATE_MUL_XY_PIPELINE: fsm_state <= FSM_STATE_MUL_XY_REGISTER;
FSM_STATE_MAGIC_PIPELINE: fsm_state <= FSM_STATE_MAGIC_REGISTER;
FSM_STATE_MUL_MN_PIPELINE: fsm_state <= FSM_STATE_MUL_MN_REGISTER;
FSM_STATE_MUL_XY_REGISTER: fsm_state <= FSM_STATE_MUL_XY_WRITE;
FSM_STATE_MAGIC_REGISTER: fsm_state <= FSM_STATE_MUL_MN_CALC;
FSM_STATE_MUL_MN_REGISTER: fsm_state <= FSM_STATE_MUL_MN_WRITE;
FSM_STATE_MUL_XY_WRITE: fsm_state <= (z_bram_addr < bram_addr_last) ? FSM_STATE_MUL_XY_CALC : FSM_STATE_MAGIC_CALC;
FSM_STATE_MUL_MN_WRITE: fsm_state <= (z_bram_addr < bram_addr_last) ? FSM_STATE_MUL_MN_CALC : FSM_STATE_SHIFT;
FSM_STATE_SHIFT: fsm_state <= (z_bram_addr > bram_addr_zero) ? FSM_STATE_SHIFT : FSM_STATE_ROUND;
FSM_STATE_FINAL: fsm_state <= FSM_STATE_IDLE;
default: fsm_state <= FSM_STATE_IDLE;
endcase
endmodule
|