aboutsummaryrefslogtreecommitdiff
path: root/rtl/modexpng_mmm_dual.v
blob: 9b6695aa99b814c7d23e12bcb3e42c27525912c0 (plain) (blame)
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
module modexpng_mmm_dual
(
    clk, rst_n,
    
    ena, rdy,
        
    ladder_mode,
    word_index_last,
    word_index_last_minus1,
    force_unity_b,
    only_reduce,
    just_multiply,
    
    sel_wide_in, sel_narrow_in,
    
    rd_wide_xy_ena,
    rd_wide_xy_ena_aux,
    rd_wide_xy_bank,
    rd_wide_xy_bank_aux,
    rd_wide_xy_addr,
    rd_wide_xy_addr_aux,
    rd_wide_x_din,
    rd_wide_y_din,
    rd_wide_x_din_aux,
    rd_wide_y_din_aux,
    
    rd_narrow_xy_ena,
    rd_narrow_xy_bank,
    rd_narrow_xy_addr,
    rd_narrow_x_din,
    rd_narrow_y_din,
    
    rcmb_wide_xy_bank,
    rcmb_wide_xy_addr,
    rcmb_wide_x_dout,
    rcmb_wide_y_dout,
    rcmb_wide_xy_valid,
    
    rcmb_narrow_xy_bank,
    rcmb_narrow_xy_addr,
    rcmb_narrow_x_dout,
    rcmb_narrow_y_dout,
    rcmb_narrow_xy_valid,
    
    rcmb_xy_bank,
    rcmb_xy_addr,
    rcmb_x_dout,
    rcmb_y_dout,
    rcmb_xy_valid,
    
    rdct_ena, rdct_rdy
);


    //
    // Headers
    //
    `include "modexpng_parameters.vh"
    `include "modexpng_mmm_dual_fsm.vh"


    //
    // Ports
    //
    input                        clk;
    input                        rst_n;
    
    input                        ena;
    output                       rdy;
    
    input                   ladder_mode;
    input [7:0] word_index_last;
    input [7:0] word_index_last_minus1;
    input       force_unity_b;
    input only_reduce;
    input just_multiply;
        
    input [BANK_ADDR_W-1:0] sel_wide_in;
    input [BANK_ADDR_W-1:0] sel_narrow_in;
    
    output                     rd_wide_xy_ena;
    output                     rd_wide_xy_ena_aux;
    output  [             BANK_ADDR_W -1:0] rd_wide_xy_bank;
    output  [             BANK_ADDR_W -1:0] rd_wide_xy_bank_aux;
    output  [ 8*NUM_MULTS/2-1:0] rd_wide_xy_addr;
    output  [           8-1:0] rd_wide_xy_addr_aux;
    input  [18*NUM_MULTS/2-1:0] rd_wide_x_din;
    input  [18*NUM_MULTS/2-1:0] rd_wide_y_din;
    input  [          18-1:0] rd_wide_x_din_aux;
    input  [          18-1:0] rd_wide_y_din_aux;

    output                    rd_narrow_xy_ena;
    output [             BANK_ADDR_W -1:0] rd_narrow_xy_bank;
    output [ 7:0] rd_narrow_xy_addr;
    input  [18-1:0] rd_narrow_x_din;
    input  [18-1:0] rd_narrow_y_din;

    output [BANK_ADDR_W -1:0] rcmb_wide_xy_bank;
    output [ 7:0] rcmb_wide_xy_addr;
    output [17:0] rcmb_wide_x_dout;
    output [17:0] rcmb_wide_y_dout;
    output        rcmb_wide_xy_valid;

    output [BANK_ADDR_W -1:0] rcmb_narrow_xy_bank;
    output [ 7:0] rcmb_narrow_xy_addr;
    output [17:0] rcmb_narrow_x_dout;
    output [17:0] rcmb_narrow_y_dout;
    output        rcmb_narrow_xy_valid;

    output [BANK_ADDR_W -1:0] rcmb_xy_bank;
    output [ 7:0] rcmb_xy_addr;
    output [17:0] rcmb_x_dout;
    output [17:0] rcmb_y_dout;
    output        rcmb_xy_valid;
    
    output        rdct_ena;
    input         rdct_rdy;

    
    //
    // FSM Declaration
    //
    reg [MMM_FSM_STATE_W-1:0] fsm_state = MMM_FSM_STATE_IDLE;
    reg [MMM_FSM_STATE_W-1:0] fsm_state_next;

    wire [MMM_FSM_STATE_W-1:0] fsm_state_after_idle;    
    wire [MMM_FSM_STATE_W-1:0] fsm_state_after_mult_square;
    wire [MMM_FSM_STATE_W-1:0] fsm_state_after_mult_triangle;
    wire [MMM_FSM_STATE_W-1:0] fsm_state_after_mult_rectangle;
    wire [MMM_FSM_STATE_W-1:0] fsm_state_after_square_holdoff;

    
    //
    // FSM Process
    //
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) fsm_state <= MMM_FSM_STATE_IDLE;
        else        fsm_state <= fsm_state_next;

        
    //
    // Storage Control Interface
    //
    reg         wide_xy_ena = 1'b0;
    reg         wide_xy_ena_aux = 1'b0;
    reg  [ BANK_ADDR_W -1:0] wide_xy_bank;
    reg  [ BANK_ADDR_W -1:0] wide_xy_bank_aux;
    reg  [ 8-1:0] wide_xy_addr[0:3];
    reg  [ 8-1:0] wide_xy_addr_aux;
    
    reg         narrow_xy_ena = 1'b0;
    reg  [ BANK_ADDR_W -1:0] narrow_xy_bank;
    reg  [ 7:0] narrow_xy_addr;
    reg  [ 7:0] narrow_xy_addr_dly;
    
    assign rd_wide_xy_ena  = wide_xy_ena;
    assign rd_wide_xy_ena_aux  = wide_xy_ena_aux;
    assign rd_wide_xy_bank = wide_xy_bank;
    assign rd_wide_xy_bank_aux = wide_xy_bank_aux;
    assign rd_wide_xy_addr_aux = wide_xy_addr_aux;

    assign rd_narrow_xy_ena  = narrow_xy_ena;
    assign rd_narrow_xy_bank = narrow_xy_bank;
    assign rd_narrow_xy_addr = narrow_xy_addr;

    genvar z;
    generate for (z=0; z<(NUM_MULTS/2); z=z+1)
        begin : gen_rd_wide_xy_addr
            assign rd_wide_xy_addr[8*z+:8] = wide_xy_addr[z];
        end
    endgenerate
        
    //
    // Column Counter
    //
    reg  [4:0] col_index;       // current column index
    reg  [4:0] col_index_prev;  // delayed column index value
    reg  [4:0] col_index_last;  // index of the very last column
    reg  [4:0] col_index_next;  // precomputed next column index
    reg        col_is_last;     // flag set during the very last column

    always @(posedge clk)
        //
        col_index_prev <= col_index;

    //
    // Column Counter Increment Logic
    //
    always @(posedge clk)
        //
        case (fsm_state_next)
            //
            MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT: begin
                col_index       <= 5'd0;
                col_index_last  <= word_index_last[7:3];
                col_index_next  <= 5'd1;
                col_is_last     <= 1'b0;
                
            end
            //
            MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT,
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT: begin
                col_index <= col_index_next;
                col_is_last <= col_index_next == col_index_last;
                col_index_next <= col_index_next == col_index_last ? 5'd0 : col_index_next + 5'd1;   
            end
            //
        endcase


    //
    // Completion Flags
    //
    wire square_almost_done_comb;
    reg  square_almost_done_flop = 1'b0;
    reg  square_surely_done_flop = 1'b0;

    wire triangle_almost_done_comb;
    reg  triangle_almost_done_flop = 1'b0;
    reg  triangle_surely_done_flop = 1'b0;
    reg  triangle_tardy_done_flop = 1'b0;

    wire rectangle_almost_done_comb;
    reg  rectangle_almost_done_flop = 1'b0;        
    reg  rectangle_surely_done_flop = 1'b0;
    reg  rectangle_tardy_done_flop = 1'b0;

    assign square_almost_done_comb = narrow_xy_addr == word_index_last_minus1;
    assign triangle_almost_done_comb = (narrow_xy_addr[2:0] == word_index_last_minus1[2:0]) && (narrow_xy_addr[7:3] == col_index);
    assign rectangle_almost_done_comb = narrow_xy_addr == word_index_last_minus1;

    //
    // Square Completion Flags
    //
    always @(posedge clk) begin
        //
        case (fsm_state)
            //
            MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
            MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:
                square_almost_done_flop <= square_almost_done_comb;
            //
            default:
               square_almost_done_flop <= 1'b0;
           //
        endcase
        //
        square_surely_done_flop <= square_almost_done_flop;
        //
    end

    //
    // Triangle Completion Flags
    //
    always @(posedge clk) begin
        //
        case (fsm_state)
            //
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:
                triangle_almost_done_flop <= triangle_almost_done_comb;
            //
            default:
                triangle_almost_done_flop <= 1'b0;
            //
        endcase
        //
        triangle_surely_done_flop <= triangle_almost_done_flop;
        triangle_tardy_done_flop  <= triangle_surely_done_flop;
        //
    end
      
    //
    // Rectangle Completion Flags
    //
    always @(posedge clk) begin
        //
        case (fsm_state)
            //
            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY:
                rectangle_almost_done_flop <= rectangle_almost_done_comb;
            //
            default:
                rectangle_almost_done_flop <= 1'b0;
            //
        endcase
        //
        rectangle_surely_done_flop <= rectangle_almost_done_flop;
        rectangle_tardy_done_flop  <= rectangle_surely_done_flop;
        //
    end


    //
    // Narrow Storage Control Logic
    //
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) narrow_xy_ena <= 1'b0;
        else begin
            //
            // Narrow Address
            //
            case (fsm_state_next)
                //
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT:   narrow_xy_addr <= 8'd0;
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:   narrow_xy_addr <= !square_almost_done_flop ? narrow_xy_addr + 1'b1 : 8'd0;
                //
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT: narrow_xy_addr <= 8'd0;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY: narrow_xy_addr <= triangle_almost_done_flop || (col_is_last && triangle_surely_done_flop) ?
                    8'd0 :  narrow_xy_addr + 1'b1;
                //
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT: narrow_xy_addr <= 8'd0;
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY: narrow_xy_addr <= rectangle_almost_done_flop || rectangle_surely_done_flop ?
                    8'd1 :  narrow_xy_addr + 1'b1;            
                //
                default:                            narrow_xy_addr <= 8'dX;
                //
            endcase
            //
            // Narrow Bank
            //
            case (fsm_state_next)
                //
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:   narrow_xy_bank <= sel_narrow_in;
                //
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY: narrow_xy_bank <= col_is_last && (triangle_almost_done_flop || triangle_surely_done_flop) ?
                    BANK_NARROW_EXT : BANK_NARROW_COEFF;
                //
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY: narrow_xy_bank <= rectangle_almost_done_flop || rectangle_surely_done_flop ?
                    BANK_NARROW_EXT : BANK_NARROW_Q;            
                //
                default:                            narrow_xy_bank <= 2'bXX;
                //
            endcase        
            //
            case (fsm_state_next)
                //
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG:   narrow_xy_ena <= 1'b1;
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:   narrow_xy_ena <= ~square_almost_done_flop;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG:   narrow_xy_ena <= 1'b1;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:   narrow_xy_ena <= !col_is_last ? ~triangle_almost_done_flop : ~triangle_surely_done_flop; 
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG:   narrow_xy_ena <= 1'b1;
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY:   narrow_xy_ena <= ~rectangle_surely_done_flop;
                //
                default:                              narrow_xy_ena <= 1'b0;
                //
            endcase
            //
        end


    //
    // Wide Storage Control Logic
    //

    wire [2:0] wide_offset_rom[0:3];
    
    generate for (z=1; z<NUM_MULTS; z=z+2)
        begin : gen_wide_offset_rom
            assign wide_offset_rom[(z-1)/2] = z[2:0];
        end
    endgenerate    

    function  [7:0] wide_xy_addr_next;
        input [7:0] wide_xy_addr_current;
        input [7:0] wide_xy_addr_last;
        begin
            if (wide_xy_addr_current > 8'd0)
                wide_xy_addr_next = wide_xy_addr_current - 1'b1;
            else
                wide_xy_addr_next = wide_xy_addr_last;
        end
    endfunction
    
    integer j;
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) begin
            wide_xy_ena <= 1'b0;
            wide_xy_ena_aux <= 1'b0;
        end else begin
            //
            // Wide Address
            //        
            for (j=0; j<(NUM_MULTS/2); j=j+1)
                //
                case (fsm_state_next)
                    //
                    // this can be reworked by having 8 address regs instead of 4 and using shifts instead of subtractions!
                    //
                    MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT:   wide_xy_addr[j] <= {5'd0, wide_offset_rom[j]};
                    MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT:   wide_xy_addr[j] <= {col_index_next, wide_offset_rom[j]};
                    MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                    MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                    MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                    MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:   wide_xy_addr[j] <= wide_xy_addr_next(wide_xy_addr[j], word_index_last);
                    //
                    MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT:   wide_xy_addr[j] <= {5'd0, wide_offset_rom[j]};
                    MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT:   wide_xy_addr[j] <= {col_index_next, wide_offset_rom[j]};
                    MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                    MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG,
                    MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                    MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:   wide_xy_addr[j] <= wide_xy_addr_next(wide_xy_addr[j], word_index_last);
                    //
                    MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT:   wide_xy_addr[j] <= {5'd0, wide_offset_rom[j]};
                    MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT:   wide_xy_addr[j] <= {col_index_next, wide_offset_rom[j]};
                    MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                    MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,
                    MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                    MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY:   wide_xy_addr[j] <= wide_xy_addr_next(wide_xy_addr[j], word_index_last);
                    //
                    default:                            wide_xy_addr[j] <= 8'dX;
                endcase
            //
            // Wide Aux Address
            //
            case (fsm_state_next)
                //
                // this can be reworked by having 8 address regs instead of 4 and using shifts instead of subtractions!
                //
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT:   wide_xy_addr_aux <= {5'd0, 3'd1};
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT:   wide_xy_addr_aux <= {5'd0, 3'd1};
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:   wide_xy_addr_aux <= wide_xy_addr_next(wide_xy_addr_aux, word_index_last);
                //
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT:   wide_xy_addr_aux <= {5'd0, 3'd1};
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT:   wide_xy_addr_aux <= {5'd0, 3'd1};
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:   wide_xy_addr_aux <= wide_xy_addr_next(wide_xy_addr_aux, word_index_last);
                //
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT:   wide_xy_addr_aux <= 8'dX;//{5'd0, 3'd0};
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_HOLDOFF:    wide_xy_addr_aux <= rcmb_xy_valid ? rcmb_xy_addr : 8'dX;
                //recomb_fat_bram_xy_dout_valid && (recomb_fat_bram_xy_bank == BANK_FAT_ML) ?
                    //mac_fat_bram_xy_addr[4] + 1'b1 : mac_fat_bram_xy_addr[4];
                //
                default:                            wide_xy_addr_aux <= 8'dX;
            endcase
            //
            // Wide Bank
            //
            case (fsm_state_next)
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:    wide_xy_bank <= sel_wide_in;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG:  wide_xy_bank <= BANK_WIDE_L;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:   wide_xy_bank <= BANK_WIDE_L;
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,    
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY:   wide_xy_bank <= BANK_WIDE_N;            
                default:                             wide_xy_bank <= 3'bXXX;
            endcase
            //
            // Wide Aux Bank
            //
            case (fsm_state_next)
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:   wide_xy_bank_aux <= sel_wide_in;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG: wide_xy_bank_aux <= BANK_WIDE_H;
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:   wide_xy_bank_aux <= BANK_WIDE_L;
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,    
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_HOLDOFF:   if (rcmb_xy_valid) // rewrite using "Kolya-style" here (get rid of too many xxx's)
                    case (rcmb_xy_bank)
                        BANK_RCMB_ML: wide_xy_bank_aux <= BANK_WIDE_L;
                        BANK_RCMB_MH: wide_xy_bank_aux <= BANK_WIDE_H;
                        //BANK_RDCT_EXT: wide_xy_bank_aux <= BANK_WIDE_EXT; '3bXXX
                        default: wide_xy_bank_aux <= 3'bXXX; 
                     endcase
                     else wide_xy_bank_aux <= 3'bXXX;
                default:                            wide_xy_bank_aux <= 3'bXXX;
            endcase
            //
            // Wide Enable
            //
            case (fsm_state_next)
                MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY:   wide_xy_ena <= 1'b1;
                default:                               wide_xy_ena <= 1'b0;
            endcase
            //
            // Wide Aux Enable
            //
            case (fsm_state_next)
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:  wide_xy_ena_aux <= 1'b1;
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT: wide_xy_ena_aux <= 1'b0;//1'b1;
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY,
                MMM_FSM_STATE_MULT_RECTANGLE_HOLDOFF:   wide_xy_ena_aux <= rcmb_xy_valid;// && (recomb_fat_bram_xy_bank == BANK_FAT_ML);
                default:                            wide_xy_ena_aux <= 1'b0;
            endcase
            //
        end
        
        
    //
    // Delay Lines
    //
    always @(posedge clk)
        //
        narrow_xy_addr_dly <= narrow_xy_addr;

    
    //
    // DSP Array Logic
    //
    reg             dsp_xy_ce_a = 1'b0;
    reg             dsp_xy_ce_b = 1'b0;
    reg             dsp_xy_ce_b_dly = 1'b0;
    reg             dsp_xy_ce_m = 1'b0;
    reg             dsp_xy_ce_p = 1'b0;
    reg             dsp_xy_ce_mode = 1'b0;
    
    reg  [9   -1:0] dsp_xy_mode_z = {9{1'b1}};
    
    wire [5*18-1:0] dsp_x_a;
    wire [5*18-1:0] dsp_y_a;

    reg  [1*16-1:0] dsp_x_b;
    reg  [1*16-1:0] dsp_y_b;
    
    reg  [ 1:0] dsp_xy_b_carry;

    wire [9*47-1:0] dsp_x_p;            
    wire [9*47-1:0] dsp_y_p;
        
    //generate for (z=0; z<(NUM_MULTS/2); z=z+1)
        //begin : gen_dsp_xy_a_split
            //assign dsp_x_a[18*z+:18] = rd_wide_x_dout[z];
            //assign dsp_y_a[18*z+:18] = rd_wide_y_dout[z];
        //end
    //endgenerate
    
    assign dsp_x_a = {rd_wide_x_din_aux, rd_wide_x_din};
    assign dsp_y_a = {rd_wide_y_din_aux, rd_wide_y_din};
    
    //assign dsp_x_a[18*4+:18] = rd_wide_x_dout_aux;
    //assign dsp_y_a[18*4+:18] = rd_wide_y_dout_aux;
            
    always @(posedge clk)
        //
        dsp_xy_ce_b_dly <= dsp_xy_ce_b;
    

    modexpng_dsp_array_block dsp_array_block_x
    (
        .clk            (clk),
        
        .ce_a           (dsp_xy_ce_a),
        .ce_b           (dsp_xy_ce_b),
        .ce_m           (dsp_xy_ce_m),
        .ce_p           (dsp_xy_ce_p),
        .ce_mode        (dsp_xy_ce_mode),

        .mode_z         (dsp_xy_mode_z),
        
        .a              (dsp_x_a),
        .b              (dsp_x_b),
        .p              (dsp_x_p)
    );

    modexpng_dsp_array_block dsp_array_block_y
    (
        .clk            (clk),
        
        .ce_a           (dsp_xy_ce_a),
        .ce_b           (dsp_xy_ce_b),
        .ce_m           (dsp_xy_ce_m),
        .ce_p           (dsp_xy_ce_p),
        .ce_mode        (dsp_xy_ce_mode),

        .mode_z         (dsp_xy_mode_z),
        
        .a              (dsp_y_a),
        .b              (dsp_y_b),
        .p              (dsp_y_p)
    );


   

    //
    // DSP Control Logic
    //
    reg narrow_xy_ena_dly1 = 1'b0;
    reg narrow_xy_ena_dly2 = 1'b0;
    
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) begin
            //
            narrow_xy_ena_dly1 <= 1'b0;
            narrow_xy_ena_dly2 <= 1'b0;
            //
            dsp_xy_ce_a    <= 1'b0;
            dsp_xy_ce_b    <= 1'b0;
            dsp_xy_ce_m    <= 1'b0;
            dsp_xy_ce_p    <= 1'b0;
            dsp_xy_ce_mode <= 1'b0;
            //
        end else begin
            //
            narrow_xy_ena_dly1 <= narrow_xy_ena;
            narrow_xy_ena_dly2 <= narrow_xy_ena_dly1; 
            //
            dsp_xy_ce_a    <= narrow_xy_ena_dly1 | narrow_xy_ena_dly2;
            dsp_xy_ce_b    <= narrow_xy_ena_dly2;
            dsp_xy_ce_m    <= dsp_xy_ce_b_dly;
            dsp_xy_ce_p    <= dsp_xy_ce_m;
            dsp_xy_ce_mode <= dsp_xy_ce_b_dly;
            //
        end    
        
    //
    // DSP Feed Logic
    //
    reg dsp_merge_xy_b;
    reg dsp_merge_xy_b_first;
    
    always @(posedge clk) begin
        //
        case (fsm_state)
            MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG:   dsp_merge_xy_b <= 1'b1;
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG: dsp_merge_xy_b <= 1'b0;
        endcase
        //
        case (fsm_state)
            MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
            MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG: dsp_merge_xy_b_first <= 1'b1;
            default:                          dsp_merge_xy_b_first <= 1'b0;
        endcase
        //
    end

    //
    // On-the-fly Carry Recombination
    //
    wire [17:0] rd_narrow_x_din_carry = rd_narrow_x_din + {{16{1'b0}}, dsp_xy_b_carry};
    wire [17:0] rd_narrow_y_din_carry = rd_narrow_y_din + {{16{1'b0}}, dsp_xy_b_carry};
    wire [17:0] rd_narrow_xy_din_carry_mux = ladder_mode ? rd_narrow_y_din_carry : rd_narrow_x_din_carry;
  
    wire [15:0] rd_narrow_xy_dout_carry_mux_or_unity = !force_unity_b ?
        rd_narrow_xy_din_carry_mux[15:0] : dsp_merge_xy_b_first ? WORD_ONE : WORD_ZERO;
  
    always @(posedge clk)
        //
        if (narrow_xy_ena_dly2) begin // rewrite
            //
            if (!dsp_merge_xy_b) begin
                dsp_x_b <= rd_narrow_x_din[15:0];
                dsp_y_b <= rd_narrow_y_din[15:0];
                dsp_xy_b_carry <= 2'b00;
            end else begin
                dsp_x_b <= rd_narrow_xy_dout_carry_mux_or_unity;
                dsp_y_b <= rd_narrow_xy_dout_carry_mux_or_unity;
                dsp_xy_b_carry <= rd_narrow_xy_din_carry_mux[17:16];
            end                 
            //
        end else begin
            //
            dsp_x_b <= WORD_DNC;
            dsp_y_b <= WORD_DNC;
            //
            dsp_xy_b_carry <= 2'b00;
            //
        end

        
    reg  [9   -1:0] dsp_xy_mode_z_adv1 = {9{1'b1}};
    reg  [9   -1:0] dsp_xy_mode_z_adv2 = {9{1'b1}};
    reg  [9   -1:0] dsp_xy_mode_z_adv3 = {9{1'b1}};
    reg  [9   -1:0] dsp_xy_mode_z_adv4 = {9{1'b1}};
        
         function  [NUM_MULTS:0] calc_mac_mode_z_square;
        input [        4:0] col_index_value;
        input [        7:0] narrow_xy_addr_value;
        begin
            if (narrow_xy_addr_value[7:3] == col_index_value)
                case (narrow_xy_addr_value[2:0])
                    3'b000: calc_mac_mode_z_square = {1'b1, 8'b11111110};
                    3'b001: calc_mac_mode_z_square = {1'b1, 8'b11111101};
                    3'b010: calc_mac_mode_z_square = {1'b1, 8'b11111011};
                    3'b011: calc_mac_mode_z_square = {1'b1, 8'b11110111};
                    3'b100: calc_mac_mode_z_square = {1'b1, 8'b11101111};
                    3'b101: calc_mac_mode_z_square = {1'b1, 8'b11011111};
                    3'b110: calc_mac_mode_z_square = {1'b1, 8'b10111111};
                    3'b111: calc_mac_mode_z_square = {1'b1, 8'b01111111};
                endcase
            else
                calc_mac_mode_z_square = {1'b1, {NUM_MULTS{1'b1}}};
        end
    endfunction
    
    function  [NUM_MULTS:0] calc_mac_mode_z_rectangle;
        input [        4:0] col_index_value;
        input [        7:0] narrow_xy_addr_value;
        begin
            if (narrow_xy_addr_value[7:3] == col_index_value)
                case (narrow_xy_addr_value[2:0])
                    3'b000: calc_mac_mode_z_rectangle = {1'b1, 8'b11111110};
                    3'b001: calc_mac_mode_z_rectangle = {1'b1, 8'b11111101};
                    3'b010: calc_mac_mode_z_rectangle = {1'b1, 8'b11111011};
                    3'b011: calc_mac_mode_z_rectangle = {1'b1, 8'b11110111};
                    3'b100: calc_mac_mode_z_rectangle = {1'b1, 8'b11101111};
                    3'b101: calc_mac_mode_z_rectangle = {1'b1, 8'b11011111};
                    3'b110: calc_mac_mode_z_rectangle = {1'b1, 8'b10111111};
                    3'b111: calc_mac_mode_z_rectangle = {1'b1, 8'b01111111};
                endcase
            else
                calc_mac_mode_z_rectangle = {1'b1, {NUM_MULTS{1'b1}}};
        end
    endfunction
        
    always @(posedge clk)
        //
        case (fsm_state_next)
            MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG,
            MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG:       dsp_xy_mode_z_adv4 <= {9{1'b0}};
            MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY,
            MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY:       dsp_xy_mode_z_adv4 <= calc_mac_mode_z_square(col_index_prev, narrow_xy_addr_dly);
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG,
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG:     dsp_xy_mode_z_adv4 <= {9{1'b0}};    // so easy
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY,
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY:     dsp_xy_mode_z_adv4 <= {9{1'b1}};
            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG,
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG:     dsp_xy_mode_z_adv4 <= {9{1'b0}};    // so easy
            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY,
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY:     dsp_xy_mode_z_adv4 <= calc_mac_mode_z_rectangle(col_index_prev, narrow_xy_addr_dly);
            default:                                dsp_xy_mode_z_adv4 <= {9{1'b1}};
        endcase

    always @(posedge clk) begin
        dsp_xy_mode_z <= dsp_xy_mode_z_adv1;
        //
        dsp_xy_mode_z_adv1 <= dsp_xy_mode_z_adv2;
        dsp_xy_mode_z_adv2 <= dsp_xy_mode_z_adv3;
        dsp_xy_mode_z_adv3 <= dsp_xy_mode_z_adv4;
    end
        


    
    
    //
    // Recombinator
    //
    reg  rcmb_ena = 1'b0;
    wire rcmb_rdy;

    modexpng_recombinator_block recombinator_block
    (
        .clk                            (clk),
        .rst_n                          (rst_n),
        
        .ena                            (rcmb_ena),
        .rdy                            (rcmb_rdy),
        
        .fsm_state_next                 (fsm_state_next),
        
        .word_index_last                (word_index_last),
        
        .dsp_xy_ce_p                    (dsp_xy_ce_p),
        .dsp_x_p                        (dsp_x_p),
        .dsp_y_p                        (dsp_y_p),
        
        .col_index                      (col_index),
        .col_index_last                 (col_index_last),
        
        .rd_narrow_xy_addr                 (narrow_xy_addr),
        .rd_narrow_xy_bank                 (narrow_xy_bank),
        
        .rcmb_wide_xy_bank          (rcmb_wide_xy_bank),
        .rcmb_wide_xy_addr          (rcmb_wide_xy_addr),
        .rcmb_wide_x_dout           (rcmb_wide_x_dout),
        .rcmb_wide_y_dout           (rcmb_wide_y_dout),
        .rcmb_wide_xy_valid         (rcmb_wide_xy_valid),
        
        .rcmb_narrow_xy_bank        (rcmb_narrow_xy_bank),
        .rcmb_narrow_xy_addr        (rcmb_narrow_xy_addr),
        .rcmb_narrow_x_dout         (rcmb_narrow_x_dout),
        .rcmb_narrow_y_dout         (rcmb_narrow_y_dout),
        .rcmb_narrow_xy_valid       (rcmb_narrow_xy_valid),
        
        .rdct_narrow_xy_bank        (rcmb_xy_bank),
        .rdct_narrow_xy_addr        (rcmb_xy_addr),
        .rdct_narrow_x_dout         (rcmb_x_dout),
        .rdct_narrow_y_dout         (rcmb_y_dout),
        .rdct_narrow_xy_valid       (rcmb_xy_valid)

    );
    
    
    //
    // Recombinator Enable Logic
    //    
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) rcmb_ena <= 1'b0;
        else        rcmb_ena <= dsp_xy_ce_a && !dsp_xy_ce_b && !dsp_xy_ce_m && !dsp_xy_ce_p;

        
    //
    // Handy Completion Flags
    //    
    wire square_done    = square_surely_done_flop;
    wire triangle_done  = !col_is_last ? triangle_surely_done_flop : triangle_tardy_done_flop;
    wire rectangle_done = rectangle_tardy_done_flop;
    

    //
    // FSM Transition Logic
    //
    assign fsm_state_after_idle = !only_reduce ? MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT : MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT;
    assign fsm_state_after_mult_square    = col_is_last ? MMM_FSM_STATE_MULT_SQUARE_HOLDOFF   : MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT;
    assign fsm_state_after_mult_triangle  = col_is_last ? MMM_FSM_STATE_MULT_TRIANGLE_HOLDOFF : MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT;
    assign fsm_state_after_mult_rectangle = col_is_last ? MMM_FSM_STATE_MULT_RECTANGLE_HOLDOFF : MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT;
    assign fsm_state_after_square_holdoff = just_multiply ? MMM_FSM_STATE_STOP : MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT;
     

    always @* begin
        //
        fsm_state_next = MMM_FSM_STATE_IDLE;
        //
        case (fsm_state)
            MMM_FSM_STATE_IDLE:                   fsm_state_next = ena                   ? fsm_state_after_idle /*MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT*/ : MMM_FSM_STATE_IDLE;
                        
            MMM_FSM_STATE_MULT_SQUARE_COL_0_INIT: fsm_state_next =                         MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG ;
            MMM_FSM_STATE_MULT_SQUARE_COL_0_TRIG: fsm_state_next =                         MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY ;
            MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY: fsm_state_next = square_done ? MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT : MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY;
            
            MMM_FSM_STATE_MULT_SQUARE_COL_N_INIT: fsm_state_next =                         MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG ;
            MMM_FSM_STATE_MULT_SQUARE_COL_N_TRIG: fsm_state_next =                         MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY ;
            MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY: fsm_state_next = square_done ? fsm_state_after_mult_square    : MMM_FSM_STATE_MULT_SQUARE_COL_N_BUSY;
            
            MMM_FSM_STATE_MULT_SQUARE_HOLDOFF:    fsm_state_next =                         rcmb_rdy ? fsm_state_after_square_holdoff : MMM_FSM_STATE_MULT_SQUARE_HOLDOFF;

            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_INIT: fsm_state_next =                         MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG ;
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_TRIG: fsm_state_next =                         MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY ;
            MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY: fsm_state_next = triangle_done ? MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT : MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY;     
            
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_INIT: fsm_state_next =                         MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG ;
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_TRIG: fsm_state_next =                         MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY ;
            MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY: fsm_state_next = triangle_done ? fsm_state_after_mult_triangle : MMM_FSM_STATE_MULT_TRIANGLE_COL_N_BUSY;
            
            MMM_FSM_STATE_MULT_TRIANGLE_HOLDOFF:    fsm_state_next =                         rcmb_rdy ? MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT : MMM_FSM_STATE_MULT_TRIANGLE_HOLDOFF;

            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT: fsm_state_next =                         MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG ;
            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_TRIG: fsm_state_next =                         MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY ;
            MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY: fsm_state_next = rectangle_done ? MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT : MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY;     
            
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_INIT: fsm_state_next =                         MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG ;
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_TRIG: fsm_state_next =                         MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY ;
            MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY: fsm_state_next = rectangle_done ? fsm_state_after_mult_rectangle : MMM_FSM_STATE_MULT_RECTANGLE_COL_N_BUSY;
            
            MMM_FSM_STATE_MULT_RECTANGLE_HOLDOFF:    fsm_state_next =                         rcmb_rdy ? MMM_FSM_STATE_WAIT_REDUCTOR : MMM_FSM_STATE_MULT_RECTANGLE_HOLDOFF;
            
            MMM_FSM_STATE_WAIT_REDUCTOR:             fsm_state_next =                         rdct_rdy ? MMM_FSM_STATE_STOP : MMM_FSM_STATE_WAIT_REDUCTOR;
            
            MMM_FSM_STATE_STOP:                     fsm_state_next =                         MMM_FSM_STATE_IDLE                   ;
            
            default:                             fsm_state_next =                         MMM_FSM_STATE_IDLE                   ;

        endcase
        //
    end


    //
    // Reductor Control Logic
    //
    reg rdct_ena_reg = 1'b0;

    assign rdct_ena = rdct_ena_reg; 
    
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n)                                rdct_ena_reg <= 1'b0;
        else case (fsm_state)
           MMM_FSM_STATE_MULT_RECTANGLE_COL_0_INIT: rdct_ena_reg <= 1'b1;
           default:                             rdct_ena_reg <= 1'b0;
        endcase
    
    
    //
    // Ready Logic
    //
    reg rdy_reg = 1'b1;
    
    assign rdy = rdy_reg;
    
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) rdy_reg <= 1'b1;
        else begin
            if (rdy && ena) rdy_reg <= 1'b0;
            if (!rdy && (fsm_state == MMM_FSM_STATE_STOP)) rdy_reg <= 1'b1; 
        end


    //
    // Debug
    //
    `ifdef MODEXPNG_ENABLE_DEBUG
        real load_cyc_mult = 0.0;
        always @(posedge clk)
            //
            if (dsp_xy_ce_m)
                load_cyc_mult <= load_cyc_mult + 1.0;    
    `endif
    
endmodule