aboutsummaryrefslogblamecommitdiff
path: root/rtl/curve/point_mul_384.v
blob: 0b1827054cd2ac4ee80f5439aa73cb442dff10c9 (plain) (tree)
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
















































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                  
//------------------------------------------------------------------------------
//
// point_mul_384.v
// -----------------------------------------------------------------------------
// Elliptic curve point scalar multiplier.
//
// Authors: Pavel Shatov
//
// Copyright (c) 2018, NORDUnet A/S
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may be
//   used to endorse or promote products derived from this software without
//   specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//------------------------------------------------------------------------------

module point_mul_384
  (
   clk, rst_n,
   ena, rdy,
   k_addr, qx_addr, qy_addr, rx_addr, ry_addr,
   rx_wren, ry_wren,
   k_din, qx_din, qy_din,
   rx_dout, ry_dout
   );


   //
   // Constants
   //
   localparam	WORD_COUNTER_WIDTH	=  4;	// 0 .. 11
   localparam	OPERAND_NUM_WORDS	= 12;	// 12 * 32 = 384


   //
   // Ports
   //
   input	wire		clk;		// system clock
   input	wire		rst_n;		// active-low async reset

   input	wire		ena;		// enable input
   output	wire 		rdy;		// ready output

   output	wire [ 3: 0] 	k_addr;
   output	wire [ 3: 0] 	qx_addr;
   output	wire [ 3: 0] 	qy_addr;
   output	wire [ 3: 0] 	rx_addr;
   output	wire [ 3: 0] 	ry_addr;

   output	wire 		rx_wren;
   output	wire 		ry_wren;

   input	wire [31: 0]	k_din;
   input	wire [31: 0]	qx_din;
   input	wire [31: 0]	qy_din;   

   output	wire [31: 0]	rx_dout;
   output	wire [31: 0]	ry_dout;


   //
   // Temporary Variables
   //
   reg [ 3: 0]		bram_tx_wr_addr;
   reg [ 3: 0]		bram_ty_wr_addr;
   reg [ 3: 0]		bram_tz_wr_addr;

   reg [ 3: 0]		bram_rx_wr_addr;
   reg [ 3: 0]		bram_ry_wr_addr;
   reg [ 3: 0]		bram_rz_wr_addr;
   wire [ 3: 0] 	bram_rz1_wr_addr;

   wire [ 3: 0] 	bram_hx_wr_addr;
	wire [ 3: 0] 	bram_hy_wr_addr;
   
   reg [ 3: 0] 		bram_tx_rd_addr;
   reg [ 3: 0] 		bram_ty_rd_addr;
   reg [ 3: 0] 		bram_tz_rd_addr;

   reg [ 3: 0] 		bram_rx_rd_addr;
   reg [ 3: 0] 		bram_ry_rd_addr;
   reg [ 3: 0] 		bram_rz_rd_addr;
   wire [ 3: 0] 	bram_rz1_rd_addr;

	wire [ 3: 0] 	bram_hx_rd_addr;
	wire [ 3: 0] 	bram_hy_rd_addr;

   reg 			bram_tx_wr_en;
   reg 			bram_ty_wr_en;
   reg 			bram_tz_wr_en;

   reg 			bram_rx_wr_en;
   reg 			bram_ry_wr_en;
   reg 			bram_rz_wr_en;
   wire 		bram_rz1_wr_en;

	wire 		bram_hx_wr_en;
	wire 		bram_hy_wr_en;

   wire [31: 0] 	bram_tx_rd_data;
   wire [31: 0] 	bram_ty_rd_data;
   wire [31: 0] 	bram_tz_rd_data;

   wire [31: 0] 	bram_rx_rd_data;
   wire [31: 0] 	bram_ry_rd_data;
   wire [31: 0] 	bram_rz_rd_data;
   wire [31: 0] 	bram_rz1_rd_data;

	wire [31: 0] 	bram_hx_rd_data;
	wire [31: 0] 	bram_hy_rd_data;

   reg [31: 0] 		bram_tx_wr_data_in;
   reg [31: 0] 		bram_ty_wr_data_in;
   reg [31: 0] 		bram_tz_wr_data_in;

   reg [31: 0] 		bram_rx_wr_data_in;
   reg [31: 0] 		bram_ry_wr_data_in;
   reg [31: 0] 		bram_rz_wr_data_in;
   wire [31: 0] 	bram_rz1_wr_data_in;

	wire [31: 0] 	bram_hx_wr_data_in;
	wire [31: 0] 	bram_hy_wr_data_in;

   wire [31: 0] 	bram_tx_wr_data_out;
   wire [31: 0] 	bram_ty_wr_data_out;
   wire [31: 0] 	bram_tz_wr_data_out;

   wire [31: 0] 	bram_rx_wr_data_out;
   wire [31: 0] 	bram_ry_wr_data_out;
   wire [31: 0] 	bram_rz_wr_data_out;

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_tx (.clk(clk),
	    .a_addr(bram_tx_wr_addr), .a_wr(bram_tx_wr_en), .a_in(bram_tx_wr_data_in), .a_out(bram_tx_wr_data_out),
	    .b_addr(bram_tx_rd_addr),                                                  .b_out(bram_tx_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_ty (.clk(clk),
	    .a_addr(bram_ty_wr_addr), .a_wr(bram_ty_wr_en), .a_in(bram_ty_wr_data_in), .a_out(bram_ty_wr_data_out),
	    .b_addr(bram_ty_rd_addr),                                                  .b_out(bram_ty_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_tz (.clk(clk),
	    .a_addr(bram_tz_wr_addr), .a_wr(bram_tz_wr_en), .a_in(bram_tz_wr_data_in), .a_out(bram_tz_wr_data_out),
	    .b_addr(bram_tz_rd_addr),                                                  .b_out(bram_tz_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_rx (.clk(clk),
	    .a_addr(bram_rx_wr_addr), .a_wr(bram_rx_wr_en), .a_in(bram_rx_wr_data_in), .a_out(bram_rx_wr_data_out),
	    .b_addr(bram_rx_rd_addr),                                                  .b_out(bram_rx_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_ry (.clk(clk),
	    .a_addr(bram_ry_wr_addr), .a_wr(bram_ry_wr_en), .a_in(bram_ry_wr_data_in), .a_out(bram_ry_wr_data_out),
	    .b_addr(bram_ry_rd_addr),                                                  .b_out(bram_ry_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_rz (.clk(clk),
	    .a_addr(bram_rz_wr_addr), .a_wr(bram_rz_wr_en), .a_in(bram_rz_wr_data_in), .a_out(bram_rz_wr_data_out),
	    .b_addr(bram_rz_rd_addr),                                                  .b_out(bram_rz_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_rz1 (.clk(clk),
	     .a_addr(bram_rz1_wr_addr), .a_wr(bram_rz1_wr_en), .a_in(bram_rz1_wr_data_in), .a_out(),
	     .b_addr(bram_rz1_rd_addr),                                                    .b_out(bram_rz1_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_hx (.clk(clk),
	     .a_addr(bram_hx_wr_addr), .a_wr(bram_hx_wr_en), .a_in(bram_hx_wr_data_in), .a_out(),
	     .b_addr(bram_hx_rd_addr),                                                  .b_out(bram_hx_rd_data));

   bram_1rw_1ro_readfirst # (.MEM_WIDTH(32), .MEM_ADDR_BITS(4))
   bram_hy (.clk(clk),
	     .a_addr(bram_hy_wr_addr), .a_wr(bram_hy_wr_en), .a_in(bram_hy_wr_data_in), .a_out(),
	     .b_addr(bram_hy_rd_addr),                                                  .b_out(bram_hy_rd_data));


   //
   // FSM
   //
   localparam	[ 4: 0]	FSM_STATE_IDLE								= 5'd00;

   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_PREPARE_TRIG	= {1'b0, 4'd01};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_PREPARE_WAIT	= {1'b0, 4'd02};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_DOUBLE_TRIG		= {1'b0, 4'd03};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_DOUBLE_WAIT		= {1'b0, 4'd04};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_COPY_TRIG		= {1'b0, 4'd05};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_COPY_WAIT		= {1'b0, 4'd06};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_INVERT_TRIG		= {1'b0, 4'd07};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_INVERT_WAIT		= {1'b0, 4'd08};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_CONVERT_TRIG	= {1'b0, 4'd09};
   localparam	[ 4: 0]	FSM_STATE_PRECOMPUTE_CONVERT_WAIT	= {1'b0, 4'd10};
	
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_PREPARE_TRIG		= {1'b1, 4'd01};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_PREPARE_WAIT		= {1'b1, 4'd02};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_DOUBLE_TRIG		= {1'b1, 4'd03};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_DOUBLE_WAIT		= {1'b1, 4'd04};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_ADD_TRIG			= {1'b1, 4'd05};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_ADD_WAIT			= {1'b1, 4'd06};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_COPY_TRIG			= {1'b1, 4'd07};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_COPY_WAIT			= {1'b1, 4'd08};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_INVERT_TRIG		= {1'b1, 4'd09};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_INVERT_WAIT		= {1'b1, 4'd10};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_CONVERT_TRIG		= {1'b1, 4'd11};
   localparam	[ 4: 0]	FSM_STATE_MULTIPLY_CONVERT_WAIT		= {1'b1, 4'd12};

   localparam	[ 4: 0]	FSM_STATE_DONE								= 5'd31;

   reg [4:0] 			     fsm_state = FSM_STATE_IDLE;


   //
   // Round Counter
   //
   reg [ 8: 0] 			     bit_counter;
   wire [ 8: 0] 		     bit_counter_max = 9'd383;
   wire [ 8: 0] 		     bit_counter_zero = 9'd0;
   wire [ 8: 0] 		     bit_counter_next =
				     (bit_counter < bit_counter_max) ? bit_counter + 1'b1 : bit_counter_zero;


   //
   // Round Completion
   //
   wire [ 4: 0] 		     fsm_state_round_next = (bit_counter < bit_counter_max) ?
				     FSM_STATE_MULTIPLY_DOUBLE_TRIG : FSM_STATE_MULTIPLY_INVERT_TRIG;


   //
   // OP Trigger Logic
   //
   reg 				     op_trig;
   wire 			     op_done;

   always @(posedge clk or negedge rst_n)
     //
     if (rst_n == 1'b0)	op_trig <= 1'b0;
     else case (fsm_state)
			FSM_STATE_PRECOMPUTE_PREPARE_TRIG,
			FSM_STATE_PRECOMPUTE_DOUBLE_TRIG,
			FSM_STATE_PRECOMPUTE_CONVERT_TRIG,
			FSM_STATE_MULTIPLY_PREPARE_TRIG,
			FSM_STATE_MULTIPLY_DOUBLE_TRIG,
			FSM_STATE_MULTIPLY_ADD_TRIG,
			FSM_STATE_MULTIPLY_CONVERT_TRIG:	op_trig <= 1'b1;
			default:										op_trig <= 1'b0;
		endcase

		
   //
   // Microprograms
   //
   wire [ 5: 0] 		     op_rom_addr;
   wire [19: 0] 		     op_rom_init_data;
   wire [19: 0] 		     op_rom_dbl_data;
   wire [19: 0] 		     op_rom_add_data;
   wire [19: 0] 		     op_rom_conv_data;
   wire [19: 0] 		     op_rom_init_ecdh_data;
   reg [19: 0] 			     op_rom_mux_data;

   (* RAM_STYLE="BLOCK" *)
   uop_init_rom op_rom_init
     (
      .clk	(clk),
      .addr	(op_rom_addr),
      .data	(op_rom_init_data)
      );

   (* RAM_STYLE="BLOCK" *)
   uop_dbl_rom op_rom_dbl
     (
      .clk	(clk),
      .addr	(op_rom_addr),
      .data	(op_rom_dbl_data)
      );

   (* RAM_STYLE="BLOCK" *)
   uop_add_rom op_rom_add
     (
      .clk	(clk),
      .addr	(op_rom_addr),
      .data	(op_rom_add_data)
      );

   (* RAM_STYLE="BLOCK" *)
   uop_conv_rom op_rom_conv
     (
      .clk	(clk),
      .addr	(op_rom_addr),
      .data	(op_rom_conv_data)
      );

   (* RAM_STYLE="BLOCK" *)
   uop_init_rom_ecdh op_rom_conv_ecdh
     (
      .clk	(clk),
      .addr	(op_rom_addr),
      .data	(op_rom_init_ecdh_data)
      );

	  
   always @(*)
   //
   case (fsm_state)
	 FSM_STATE_PRECOMPUTE_PREPARE_WAIT:	op_rom_mux_data = op_rom_init_ecdh_data;
     FSM_STATE_MULTIPLY_PREPARE_WAIT:		op_rom_mux_data = op_rom_init_data;
     FSM_STATE_PRECOMPUTE_DOUBLE_WAIT,
	 FSM_STATE_MULTIPLY_DOUBLE_WAIT:		op_rom_mux_data = op_rom_dbl_data;
     FSM_STATE_MULTIPLY_ADD_WAIT:			op_rom_mux_data = op_rom_add_data;
	 FSM_STATE_PRECOMPUTE_CONVERT_WAIT,
     FSM_STATE_MULTIPLY_CONVERT_WAIT:		op_rom_mux_data = op_rom_conv_data;
     default:			op_rom_mux_data = {20{1'bX}};
   endcase



   //
   // Modulus
   //
   reg [ 3: 0]			rom_q_addr;
   wire [31: 0]			rom_q_data;

   brom_p384_q rom_q
     (
      .clk	(clk),
      .b_addr	(rom_q_addr),
      .b_out	(rom_q_data)
      );


   //
   // Worker
   //
   wire [ 3: 0] 		     worker_addr_px;
   wire [ 3: 0] 		     worker_addr_py;
   wire [ 3: 0] 		     worker_addr_pz;

   wire [ 3: 0] 		     worker_addr_rx;
   wire [ 3: 0] 		     worker_addr_ry;
   wire [ 3: 0] 		     worker_addr_rz;

   wire [ 3: 0] 		     worker_addr_q;

   wire 			     worker_wren_rx;
   wire 			     worker_wren_ry;
   wire 			     worker_wren_rz;

   reg [31: 0] 			     worker_din_px;
   reg [31: 0] 			     worker_din_py;
   reg [31: 0] 			     worker_din_pz;

   reg [31: 0] 			     worker_din_rx;
   reg [31: 0] 			     worker_din_ry;
   reg [31: 0] 			     worker_din_rz;

   wire [31: 0] 		     worker_dout_rx;
   wire [31: 0] 		     worker_dout_ry;
   wire [31: 0] 		     worker_dout_rz;

   point_dbl_add_384 worker
     (
      .clk		(clk),
      .rst_n		(rst_n),

      .ena		(op_trig),
      .rdy		(op_done),

      .uop_addr		(op_rom_addr),
      .uop		(op_rom_mux_data),

	  .gx_addr		(qx_addr),
	  .gy_addr		(qy_addr),
		
	  .hx_addr		(bram_hx_rd_addr),
	  .hy_addr		(bram_hy_rd_addr),
	  
      .px_addr		(worker_addr_px),
      .py_addr		(worker_addr_py),
      .pz_addr		(worker_addr_pz),

      .rx_addr		(worker_addr_rx),
      .ry_addr		(worker_addr_ry),
      .rz_addr		(worker_addr_rz),

      .q_addr		(worker_addr_q),

      .v_addr		(bram_rz1_rd_addr),

      .rx_wren		(worker_wren_rx),
      .ry_wren		(worker_wren_ry),
      .rz_wren		(worker_wren_rz),

	  .gx_din		(qx_din),
	  .gy_din		(qy_din),
		
	  .hx_din		(bram_hx_rd_data),
	  .hy_din		(bram_hy_rd_data),

      .px_din		(worker_din_px),
      .py_din		(worker_din_py),
      .pz_din		(worker_din_pz),

      .rx_din		(worker_din_rx),
      .ry_din		(worker_din_ry),
      .rz_din		(worker_din_rz),

      .rx_dout		(worker_dout_rx),
      .ry_dout		(worker_dout_ry),
      .rz_dout		(worker_dout_rz),

      .q_din		(rom_q_data),

      .v_din		(bram_rz1_rd_data)
      );


   //
   // Mover
   //
   reg 				     move_trig;
   wire 			     move_done;

   wire [ 3: 0] 		     mover_addr_x;
   wire [ 3: 0] 		     mover_addr_y;

   wire 			     mover_wren_y;

   always @(posedge clk or negedge rst_n)
     //
     if (rst_n == 1'b0)	move_trig <= 1'b0;
     else case (fsm_state)
			FSM_STATE_PRECOMPUTE_COPY_TRIG,
			FSM_STATE_MULTIPLY_COPY_TRIG:			move_trig <= 1'b1;
			default: 									move_trig <= 1'b0;
	  endcase

   mw_mover #
     (
      .WORD_COUNTER_WIDTH	(4),
      .OPERAND_NUM_WORDS	(12)
      )
   mover
     (
      .clk	(clk),
      .rst_n	(rst_n),

      .ena	(move_trig),
      .rdy	(move_done),

      .x_addr	(mover_addr_x),
      .y_addr	(mover_addr_y),
      .y_wren	(mover_wren_y),

      .x_din	({32{1'bX}}),
      .y_dout	()
      );


   //
   // Invertor
   //
   reg 				     invert_trig;
   wire 			     invert_done;

   wire [ 3: 0] 		     invertor_addr_a;
   wire [ 3: 0] 		     invertor_addr_q;

   always @(posedge clk or negedge rst_n)
     //
     if (rst_n == 1'b0)	invert_trig <= 1'b0;
     else case (fsm_state)
			FSM_STATE_PRECOMPUTE_INVERT_TRIG,
			FSM_STATE_MULTIPLY_INVERT_TRIG:	invert_trig <= 1'b1;
			default:										invert_trig <= 1'b0;
		endcase

   modular_invertor #
     (
      .MAX_OPERAND_WIDTH(384)
      )
   invertor
     (
      .clk		(clk),
      .rst_n		(rst_n),

      .ena		(invert_trig),
      .rdy		(invert_done),

      .a_addr		(invertor_addr_a),
      .q_addr		(invertor_addr_q),
      .a1_addr		(bram_rz1_wr_addr),
      .a1_wren		(bram_rz1_wr_en),

      .a_din		(bram_rz_rd_data),
      .q_din		(rom_q_data),
      .a1_dout		(bram_rz1_wr_data_in)
      );


   //
   // FSM Transition Logic
   //
   always @(posedge clk or negedge rst_n)
     //
     if (rst_n == 1'b0)			fsm_state <= FSM_STATE_IDLE;
     else case (fsm_state)

	    FSM_STATE_IDLE:		fsm_state <= ena ? FSM_STATE_PRECOMPUTE_PREPARE_TRIG : FSM_STATE_IDLE;

		FSM_STATE_PRECOMPUTE_PREPARE_TRIG: 	fsm_state <= FSM_STATE_PRECOMPUTE_PREPARE_WAIT;
		FSM_STATE_PRECOMPUTE_PREPARE_WAIT:		fsm_state <= (!op_trig && op_done) ? FSM_STATE_PRECOMPUTE_DOUBLE_TRIG : FSM_STATE_PRECOMPUTE_PREPARE_WAIT;
		 
		FSM_STATE_PRECOMPUTE_DOUBLE_TRIG: 		fsm_state <= FSM_STATE_PRECOMPUTE_DOUBLE_WAIT;
		FSM_STATE_PRECOMPUTE_DOUBLE_WAIT:		fsm_state <= (!op_trig && op_done) ? FSM_STATE_PRECOMPUTE_COPY_TRIG : FSM_STATE_PRECOMPUTE_DOUBLE_WAIT;
		 
		FSM_STATE_PRECOMPUTE_COPY_TRIG:		fsm_state <= FSM_STATE_PRECOMPUTE_COPY_WAIT; 
		FSM_STATE_PRECOMPUTE_COPY_WAIT:		fsm_state <= (!move_trig && move_done) ? FSM_STATE_PRECOMPUTE_INVERT_TRIG : FSM_STATE_PRECOMPUTE_COPY_WAIT;
		 
		FSM_STATE_PRECOMPUTE_INVERT_TRIG:		fsm_state <= FSM_STATE_PRECOMPUTE_INVERT_WAIT;
		FSM_STATE_PRECOMPUTE_INVERT_WAIT:		fsm_state <= (!invert_trig && invert_done) ? FSM_STATE_PRECOMPUTE_CONVERT_TRIG : FSM_STATE_PRECOMPUTE_INVERT_WAIT;
		 
		FSM_STATE_PRECOMPUTE_CONVERT_TRIG:		fsm_state <= FSM_STATE_PRECOMPUTE_CONVERT_WAIT; 
		FSM_STATE_PRECOMPUTE_CONVERT_WAIT:		fsm_state <= (!op_trig && op_done) ? FSM_STATE_MULTIPLY_PREPARE_TRIG : FSM_STATE_PRECOMPUTE_CONVERT_WAIT;

	    FSM_STATE_MULTIPLY_PREPARE_TRIG:	fsm_state <= FSM_STATE_MULTIPLY_PREPARE_WAIT;
	    FSM_STATE_MULTIPLY_PREPARE_WAIT:	fsm_state <= (!op_trig && op_done) ? FSM_STATE_MULTIPLY_DOUBLE_TRIG : FSM_STATE_MULTIPLY_PREPARE_WAIT;

	    FSM_STATE_MULTIPLY_DOUBLE_TRIG:	fsm_state <= FSM_STATE_MULTIPLY_DOUBLE_WAIT;
	    FSM_STATE_MULTIPLY_DOUBLE_WAIT:	fsm_state <= (!op_trig && op_done) ? FSM_STATE_MULTIPLY_ADD_TRIG : FSM_STATE_MULTIPLY_DOUBLE_WAIT;

	    FSM_STATE_MULTIPLY_ADD_TRIG:		fsm_state <= FSM_STATE_MULTIPLY_ADD_WAIT;
	    FSM_STATE_MULTIPLY_ADD_WAIT:		fsm_state <= (!op_trig && op_done) ? FSM_STATE_MULTIPLY_COPY_TRIG : FSM_STATE_MULTIPLY_ADD_WAIT;

	    FSM_STATE_MULTIPLY_COPY_TRIG:	fsm_state <= FSM_STATE_MULTIPLY_COPY_WAIT;
	    FSM_STATE_MULTIPLY_COPY_WAIT:	fsm_state <= (!move_trig && move_done) ? fsm_state_round_next : FSM_STATE_MULTIPLY_COPY_WAIT;

	    FSM_STATE_MULTIPLY_INVERT_TRIG:	fsm_state <= FSM_STATE_MULTIPLY_INVERT_WAIT;
	    FSM_STATE_MULTIPLY_INVERT_WAIT:	fsm_state <= (!invert_trig && invert_done) ? FSM_STATE_MULTIPLY_CONVERT_TRIG : FSM_STATE_MULTIPLY_INVERT_WAIT;

	    FSM_STATE_MULTIPLY_CONVERT_TRIG:	fsm_state <= FSM_STATE_MULTIPLY_CONVERT_WAIT;
	    FSM_STATE_MULTIPLY_CONVERT_WAIT:	fsm_state <= (!op_trig && op_done) ? FSM_STATE_DONE : FSM_STATE_MULTIPLY_CONVERT_WAIT;

	    FSM_STATE_DONE:		fsm_state <= FSM_STATE_IDLE;

	    default:			fsm_state <= FSM_STATE_IDLE;

	  endcase


   //
   // Bit Counter Increment
   //
   always @(posedge clk) begin
      //
      if ((fsm_state == FSM_STATE_MULTIPLY_PREPARE_WAIT) && !op_trig && op_done)
	bit_counter <= bit_counter_zero;
      //
      if ((fsm_state == FSM_STATE_MULTIPLY_COPY_WAIT) && !move_trig && move_done)
	bit_counter <= bit_counter_next;
      //
   end


   //
   // K Latch Logic
   //
   reg	[ 3: 0]	k_addr_reg;
   reg [31: 0] 	k_din_reg;

   assign k_addr = k_addr_reg;

   always @(posedge clk) begin
      //
      if (fsm_state == FSM_STATE_MULTIPLY_DOUBLE_TRIG)
	k_addr_reg <= 4'd11 - bit_counter[8:5];
      //
      if (fsm_state == FSM_STATE_MULTIPLY_ADD_TRIG)
	k_din_reg <= (bit_counter[4:0] == 5'd0) ? k_din : {k_din_reg[30:0], 1'bX};
      //
   end



   //
   // Copy Inhibit Logic
   //
   wire	move_inhibit = k_din_reg[31];

   reg copy_t2r_int;

	always @*
		//
		case (fsm_state)
			FSM_STATE_PRECOMPUTE_COPY_WAIT:	copy_t2r_int = 1'b1;
			FSM_STATE_MULTIPLY_COPY_WAIT:		copy_t2r_int = mover_wren_y & ~move_inhibit;
			default:									copy_t2r_int = 1'b0;
		endcase

   always @(*) begin
      //
      // Q
      //
      case (fsm_state)
	FSM_STATE_PRECOMPUTE_DOUBLE_WAIT,
	FSM_STATE_MULTIPLY_DOUBLE_WAIT:	rom_q_addr = worker_addr_q;
	FSM_STATE_MULTIPLY_ADD_WAIT:		rom_q_addr = worker_addr_q;
	FSM_STATE_PRECOMPUTE_INVERT_WAIT,
	FSM_STATE_MULTIPLY_INVERT_WAIT:	rom_q_addr = invertor_addr_q;
	FSM_STATE_PRECOMPUTE_CONVERT_WAIT,
	FSM_STATE_MULTIPLY_CONVERT_WAIT:	rom_q_addr = worker_addr_q;
	default:		rom_q_addr = worker_addr_q;
      endcase

      //
      // R(X,Y,Z)
      //
      case (fsm_state)
	//
	FSM_STATE_PRECOMPUTE_PREPARE_WAIT,
	FSM_STATE_MULTIPLY_PREPARE_WAIT: begin
	   //
	   bram_rx_rd_addr    <= {4{1'bX}};      bram_ry_rd_addr    <= {4{1'bX}};      bram_rz_rd_addr    <= {4{1'bX}};
	   bram_rx_wr_addr    <= worker_addr_rx; bram_ry_wr_addr    <= worker_addr_ry; bram_rz_wr_addr    <= worker_addr_rz;
	   bram_rx_wr_en      <= worker_wren_rx; bram_ry_wr_en      <= worker_wren_ry; bram_rz_wr_en      <= worker_wren_rz;
	   bram_rx_wr_data_in <= worker_dout_rx; bram_ry_wr_data_in <= worker_dout_ry; bram_rz_wr_data_in <= worker_dout_rz;
	   //
	end
	//
	FSM_STATE_PRECOMPUTE_DOUBLE_WAIT,
	FSM_STATE_MULTIPLY_DOUBLE_WAIT: begin
	   //
	   bram_rx_rd_addr    <= worker_addr_px; bram_ry_rd_addr    <= worker_addr_py; bram_rz_rd_addr    <= worker_addr_pz;
	   bram_rx_wr_addr    <= {4{1'bX}};      bram_ry_wr_addr    <= {4{1'bX}};      bram_rz_wr_addr    <= {4{1'bX}};
	   bram_rx_wr_en      <= 1'b0;           bram_ry_wr_en      <= 1'b0;           bram_rz_wr_en      <= 1'b0;
	   bram_rx_wr_data_in <= {32{1'bX}};     bram_ry_wr_data_in <= {32{1'bX}};     bram_rz_wr_data_in <= {32{1'bX}};
	   //
	end
	//
	FSM_STATE_MULTIPLY_ADD_WAIT: begin
	   //
	   bram_rx_rd_addr    <= {4{1'bX}};      bram_ry_rd_addr    <= {4{1'bX}};      bram_rz_rd_addr    <= {4{1'bX}};
	   bram_rx_wr_addr    <= worker_addr_rx; bram_ry_wr_addr    <= worker_addr_ry; bram_rz_wr_addr    <= worker_addr_rz;
	   bram_rx_wr_en      <= worker_wren_rx; bram_ry_wr_en      <= worker_wren_ry; bram_rz_wr_en      <= worker_wren_rz;
	   bram_rx_wr_data_in <= worker_dout_rx; bram_ry_wr_data_in <= worker_dout_ry; bram_rz_wr_data_in <= worker_dout_rz;
	   //
	end
	//
	FSM_STATE_PRECOMPUTE_COPY_WAIT,
	FSM_STATE_MULTIPLY_COPY_WAIT: begin
	   //
	   bram_rx_rd_addr    <= {4{1'bX}};       bram_ry_rd_addr    <= {4{1'bX}};       bram_rz_rd_addr    <= {4{1'bX}};
	   bram_rx_wr_addr    <= mover_addr_y;    bram_ry_wr_addr    <= mover_addr_y;    bram_rz_wr_addr    <= mover_addr_y;
	   bram_rx_wr_en      <= copy_t2r_int;    bram_ry_wr_en      <= copy_t2r_int;    bram_rz_wr_en      <= copy_t2r_int;
	   bram_rx_wr_data_in <= bram_tx_rd_data; bram_ry_wr_data_in <= bram_ty_rd_data; bram_rz_wr_data_in <= bram_tz_rd_data;
	   //
	end
	//
	FSM_STATE_PRECOMPUTE_INVERT_WAIT,
	FSM_STATE_MULTIPLY_INVERT_WAIT: begin
	   //
	   bram_rx_rd_addr    <= {4{1'bX}};  bram_ry_rd_addr    <= {4{1'bX}};  bram_rz_rd_addr    <= invertor_addr_a;
	   bram_rx_wr_addr    <= {4{1'bX}};  bram_ry_wr_addr    <= {4{1'bX}};  bram_rz_wr_addr    <= {4{1'bX}};
	   bram_rx_wr_en      <= 1'b0;       bram_ry_wr_en      <= 1'b0;       bram_rz_wr_en      <= 1'b0;
	   bram_rx_wr_data_in <= {32{1'bX}}; bram_ry_wr_data_in <= {32{1'bX}}; bram_rz_wr_data_in <= {32{1'bX}};
	   //
	end
	//
	FSM_STATE_PRECOMPUTE_CONVERT_WAIT,
	FSM_STATE_MULTIPLY_CONVERT_WAIT: begin
	   //
	   bram_rx_rd_addr    <= worker_addr_px; bram_ry_rd_addr    <= worker_addr_py; bram_rz_rd_addr    <= worker_addr_pz;
	   bram_rx_wr_addr    <= {4{1'bX}};      bram_ry_wr_addr    <= {4{1'bX}};      bram_rz_wr_addr    <= {4{1'bX}};
	   bram_rx_wr_en      <= 1'b0;           bram_ry_wr_en      <= 1'b0;           bram_rz_wr_en      <= 1'b0;
	   bram_rx_wr_data_in <= {32{1'bX}};     bram_ry_wr_data_in <= {32{1'bX}};     bram_rz_wr_data_in <= {32{1'bX}};
	   //
	end

	//
	default: begin
	   //
	   bram_rx_rd_addr    <= {4{1'bX}};  bram_ry_rd_addr    <= {4{1'bX}};  bram_rz_rd_addr    <= {4{1'bX}};
	   bram_rx_wr_addr    <= {4{1'bX}};  bram_ry_wr_addr    <= {4{1'bX}};  bram_rz_wr_addr    <= {4{1'bX}};
	   bram_rx_wr_en      <= 1'b0;       bram_ry_wr_en      <= 1'b0;       bram_rz_wr_en      <= 1'b0;
	   bram_rx_wr_data_in <= {32{1'bX}}; bram_ry_wr_data_in <= {32{1'bX}}; bram_rz_wr_data_in <= {32{1'bX}};
	   //
	end
	//
      endcase
      //
      // T(X,Y,Z)
      //
      case (fsm_state)
	//
	FSM_STATE_PRECOMPUTE_DOUBLE_WAIT,
	FSM_STATE_MULTIPLY_DOUBLE_WAIT: begin
	   //
	   bram_tx_rd_addr    <= {4{1'bX}};      bram_ty_rd_addr    <= {4{1'bX}};      bram_tz_rd_addr    <= {4{1'bX}};
	   bram_tx_wr_addr    <= worker_addr_rx; bram_ty_wr_addr    <= worker_addr_ry; bram_tz_wr_addr    <= worker_addr_rz;
	   bram_tx_wr_en      <= worker_wren_rx; bram_ty_wr_en      <= worker_wren_ry; bram_tz_wr_en      <= worker_wren_rz;
	   bram_tx_wr_data_in <= worker_dout_rx; bram_ty_wr_data_in <= worker_dout_ry; bram_tz_wr_data_in <= worker_dout_rz;
	   //
	end
	//
	FSM_STATE_MULTIPLY_ADD_WAIT: begin
	   //
	   bram_tx_rd_addr    <= worker_addr_px; bram_ty_rd_addr    <= worker_addr_py; bram_tz_rd_addr    <= worker_addr_pz;
	   bram_tx_wr_addr    <= {4{1'bX}};      bram_ty_wr_addr    <= {4{1'bX}};      bram_tz_wr_addr    <= {4{1'bX}};
	   bram_tx_wr_en      <= 1'b0;           bram_ty_wr_en      <= 1'b0;           bram_tz_wr_en      <= 1'b0;
	   bram_tx_wr_data_in <= {32{1'bX}};     bram_ty_wr_data_in <= {32{1'bX}};     bram_tz_wr_data_in <= {32{1'bX}};
	   //
	end
	//
	FSM_STATE_PRECOMPUTE_COPY_WAIT,
	FSM_STATE_MULTIPLY_COPY_WAIT: begin
	   //
	   bram_tx_rd_addr    <= mover_addr_x; bram_ty_rd_addr    <= mover_addr_x; bram_tz_rd_addr    <= mover_addr_x;
	   bram_tx_wr_addr    <= {4{1'bX}};    bram_ty_wr_addr    <= {4{1'bX}};    bram_tz_wr_addr    <= {4{1'bX}};
	   bram_tx_wr_en      <= 1'b0;         bram_ty_wr_en      <= 1'b0;         bram_tz_wr_en      <= 1'b0;
	   bram_tx_wr_data_in <= {32{1'bX}};   bram_ty_wr_data_in <= {32{1'bX}};   bram_tz_wr_data_in <= {32{1'bX}};
	   //
	end

	//
	default: begin
	   //
	   bram_tx_rd_addr    <= {4{1'bX}};  bram_ty_rd_addr    <= {4{1'bX}};  bram_tz_rd_addr    <= {4{1'bX}};
	   bram_tx_wr_addr    <= {4{1'bX}};  bram_ty_wr_addr    <= {4{1'bX}};  bram_tz_wr_addr    <= {4{1'bX}};
	   bram_tx_wr_en      <= 1'b0;       bram_ty_wr_en      <= 1'b0;       bram_tz_wr_en      <= 1'b0;
	   bram_tx_wr_data_in <= {32{1'bX}}; bram_ty_wr_data_in <= {32{1'bX}}; bram_tz_wr_data_in <= {32{1'bX}};
	   //
	end
	//
      endcase
      //
      // Worker
      //
      case (fsm_state)
	//
	FSM_STATE_PRECOMPUTE_DOUBLE_WAIT,
	FSM_STATE_MULTIPLY_DOUBLE_WAIT: begin
	   //
	   worker_din_px <= bram_rx_rd_data;     worker_din_py <= bram_ry_rd_data;     worker_din_pz <= bram_rz_rd_data;
	   worker_din_rx <= bram_tx_wr_data_out; worker_din_ry <= bram_ty_wr_data_out; worker_din_rz <= bram_tz_wr_data_out;
	   //
	end
	//
	FSM_STATE_MULTIPLY_ADD_WAIT: begin
	   //
	   worker_din_px <= bram_tx_rd_data;     worker_din_py <= bram_ty_rd_data;     worker_din_pz <= bram_tz_rd_data;
	   worker_din_rx <= bram_rx_wr_data_out; worker_din_ry <= bram_ry_wr_data_out; worker_din_rz <= bram_rz_wr_data_out;
	   //
	end
	//
	FSM_STATE_PRECOMPUTE_CONVERT_WAIT,
	FSM_STATE_MULTIPLY_CONVERT_WAIT: begin
	   //
	   worker_din_px <= bram_rx_rd_data; worker_din_py <= bram_ry_rd_data; worker_din_pz <= bram_rz_rd_data;
	   worker_din_rx <= {32{1'bX}};      worker_din_ry <= {32{1'bX}};      worker_din_rz <= {32{1'bX}};
	   //
	end
	//
	default: begin
	   //
	   worker_din_px <= {32{1'bX}}; worker_din_py <= {32{1'bX}}; worker_din_pz <= {32{1'bX}};
	   worker_din_rx <= {32{1'bX}}; worker_din_ry <= {32{1'bX}}; worker_din_rz <= {32{1'bX}};
	   //
	end
	//
      endcase
      //
   end


   	//
	// Internal Mapping
	//
	assign bram_hx_wr_en = worker_wren_rx && (fsm_state == FSM_STATE_PRECOMPUTE_CONVERT_WAIT);
	assign bram_hy_wr_en = worker_wren_ry && (fsm_state == FSM_STATE_PRECOMPUTE_CONVERT_WAIT);

   assign bram_hx_wr_data_in = worker_dout_rx;
   assign bram_hy_wr_data_in = worker_dout_ry;

   assign bram_hx_wr_addr = worker_addr_rx;
   assign bram_hy_wr_addr = worker_addr_ry;

   
   //
   // Output Mapping
   //
   assign	rx_wren = worker_wren_rx && (fsm_state == FSM_STATE_MULTIPLY_CONVERT_WAIT);
   assign	ry_wren = worker_wren_ry && (fsm_state == FSM_STATE_MULTIPLY_CONVERT_WAIT);

   assign	rx_dout = worker_dout_rx;
   assign	ry_dout = worker_dout_ry;

   assign	rx_addr = worker_addr_rx;
   assign	ry_addr = worker_addr_ry;


   //
   // Ready Flag Logic
   //
   reg rdy_reg = 1'b1;
   assign rdy = rdy_reg;

   always @(posedge clk or negedge rst_n)

     if (rst_n == 1'b0) rdy_reg <= 1'b1;
     else begin

	/* clear flag */
	if ((fsm_state == FSM_STATE_IDLE) && ena)
	  rdy_reg <= 1'b0;

	/* set flag */
	if (fsm_state == FSM_STATE_DONE)
	  rdy_reg <= 1'b1;

     end


endmodule


//------------------------------------------------------------------------------
// End-of-File
//------------------------------------------------------------------------------