aboutsummaryrefslogtreecommitdiff
path: root/rtl/modexpng_recombinator_block.v
blob: 7a9154a596abdcf40dce5f5ab4ae9f400f75005d (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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
module modexpng_recombinator_block
(
    clk, rst_n,
    ena, rdy,
    fsm_state_next,
    word_index_last,
    dsp_xy_ce_p,
    dsp_x_p, dsp_y_p,
    col_index, col_index_last,
    rd_narrow_xy_addr, rd_narrow_xy_bank,
    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,
    rdct_narrow_xy_bank, rdct_narrow_xy_addr, rdct_narrow_x_dout, rdct_narrow_y_dout, rdct_narrow_xy_valid
);


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


    input                        clk;
    input                        rst_n;
    input                        ena;
    output                       rdy;
    input  [MMM_FSM_STATE_W-1:0] fsm_state_next;
    input [7:0]                  word_index_last;
    input                        dsp_xy_ce_p;
    input  [9*47-1:0] dsp_x_p;
    input  [9*47-1:0] dsp_y_p;
    input  [     4:0] col_index;
    input  [     4:0] col_index_last;
    
    input  [     BANK_ADDR_W -1:0] rd_narrow_xy_bank;
    input  [     7:0] rd_narrow_xy_addr;

    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] rdct_narrow_xy_bank;
    output [     7:0] rdct_narrow_xy_addr;
    output [    17:0] rdct_narrow_x_dout;
    output [    17:0] rdct_narrow_y_dout;
    output            rdct_narrow_xy_valid;


    //
    // Latches
    //
    reg  [1*47-1:0] dsp_x_p_latch[0:8];
    reg  [1*47-1:0] dsp_y_p_latch[0:8];


    //
    // Mapping
    //
    wire [46:0] dsp_x_p_split[0:8];
    wire [46:0] dsp_y_p_split[0:8];
    
    genvar z;
    generate for (z=0; z<(NUM_MULTS+1); z=z+1)
        begin : gen_dsp_xy_p_split
            assign dsp_x_p_split[z] = dsp_x_p[47*z+:47];
            assign dsp_y_p_split[z] = dsp_y_p[47*z+:47];
        end
    endgenerate


    //
    // Delays
    //
    reg dsp_xy_ce_p_dly1 = 1'b0;

    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) dsp_xy_ce_p_dly1 <= 1'b0;
        else        dsp_xy_ce_p_dly1 <= dsp_xy_ce_p;


    //
    // Registers
    //
    
    // valid
    reg       xy_valid_lsb = 1'b0;
    reg       xy_aux_lsb   = 1'b0;
    reg       xy_valid_msb = 1'b0;
    
    // bitmap
    reg [7:0] xy_bitmap_lsb = {8{1'b0}};
    reg [7:0] xy_bitmap_msb = {8{1'b0}};
    
    // index
    reg [2:0] xy_index_lsb = 3'dX;
    
    // purge
    reg       xy_purge_lsb = 1'b0;
    reg       xy_purge_msb = 1'b0;
    
    // valid - latch
    reg       xy_valid_latch_lsb = 1'b0;
    
    // aux - latch
    reg       xy_aux_latch_lsb = 1'b0;
    
    // bitmap - latch
    reg [7:0] xy_bitmap_latch_lsb = {8{1'b0}};
    reg [7:0] xy_bitmap_latch_msb = {8{1'b0}};

    // index - latch
    reg [2:0] xy_index_latch_lsb = 3'dX;
    
    // purge - index
    reg       xy_purge_latch_lsb = 1'b0;
    reg       xy_purge_latch_msb = 1'b0;

    // 
    reg       xy_valid_lsb_adv[1:6];
    reg       xy_valid_msb_adv[1:6];
    reg       xy_aux_lsb_adv[1:6];
    reg [7:0] xy_bitmap_lsb_adv[1:6];
    reg [7:0] xy_bitmap_msb_adv[1:6];
    reg [2:0] xy_index_lsb_adv[1:6];
    reg [2:0] xy_index_msb_adv[1:6];
    reg       xy_purge_lsb_adv[1:6];
    reg       xy_purge_msb_adv[1:6];
    
    reg [1:0] rcmb_mode;
       
    always @(posedge clk)
       //
       if (ena)
           //
           case (fsm_state_next)
               MMM_FSM_STATE_MULT_SQUARE_COL_0_BUSY:        rcmb_mode <= 2'd1;
               MMM_FSM_STATE_MULT_TRIANGLE_COL_0_BUSY:      rcmb_mode <= 2'd2;
               MMM_FSM_STATE_MULT_RECTANGLE_COL_0_BUSY:     rcmb_mode <= 2'd3;
               default:                                 rcmb_mode <= 2'd0;
           endcase

               
    integer i;
    initial for (i=1; i<6; i=i+1) begin
        xy_valid_lsb_adv[i] = 1'b0;
        xy_valid_msb_adv[i] = 1'b0;
        xy_aux_lsb_adv[i] = 1'b0;
        xy_bitmap_lsb_adv[i] = {8{1'b0}};
        xy_bitmap_msb_adv[i] = {8{1'b0}};
        xy_index_lsb_adv[i] = 3'dX;
        xy_index_msb_adv[i] = 3'dX;
        xy_purge_lsb_adv[i] = 1'b0;
        xy_purge_msb_adv[i] = 1'b0;
    end
    
    function        calc_square_triangle_valid_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            //
            if (narrow_xy_addr_value[7:3] == col_index_value)
                calc_square_triangle_valid_lsb = 1'b1;
            else
                calc_square_triangle_valid_lsb = 1'b0;
            //
        end
    endfunction

    function        calc_square_valid_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_square_valid_lsb = calc_square_triangle_valid_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);   
        end
    endfunction

    function        calc_triangle_valid_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_triangle_valid_lsb = calc_square_triangle_valid_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);   
        end
    endfunction
    
    function        calc_rectangle_valid_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            //
            if (narrow_xy_addr_value[7:3] == col_index_value) 
                calc_rectangle_valid_lsb = narrow_xy_bank_value != BANK_NARROW_EXT;
            else
                calc_rectangle_valid_lsb = 1'b0;
            //
        end
    endfunction

    function        calc_triangle_aux_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            //
            if (narrow_xy_bank_value == BANK_NARROW_EXT)
                calc_triangle_aux_lsb = 1'b1;
            else
                calc_triangle_aux_lsb = 1'b0;
            //
        end
    endfunction
    
    function  [7:0] calc_square_triangle_bitmap_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_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_square_triangle_bitmap_lsb = 8'b00000001;
                    3'b001: calc_square_triangle_bitmap_lsb = 8'b00000010;
                    3'b010: calc_square_triangle_bitmap_lsb = 8'b00000100;
                    3'b011: calc_square_triangle_bitmap_lsb = 8'b00001000;
                    3'b100: calc_square_triangle_bitmap_lsb = 8'b00010000;
                    3'b101: calc_square_triangle_bitmap_lsb = 8'b00100000;
                    3'b110: calc_square_triangle_bitmap_lsb = 8'b01000000;
                    3'b111: calc_square_triangle_bitmap_lsb = 8'b10000000;
                endcase
                //
            else
                calc_square_triangle_bitmap_lsb = {8{1'b0}};
            //
        end
    endfunction

    function  [7:0] calc_square_bitmap_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_square_bitmap_lsb = calc_square_triangle_bitmap_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);
        end
    endfunction

    function  [7:0] calc_triangle_bitmap_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_triangle_bitmap_lsb = calc_square_triangle_bitmap_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);
        end
    endfunction

    function  [7:0] calc_rectangle_bitmap_lsb;
       input [4:0] col_index_value;
       input [4:0] col_index_last_value;
       input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
       input [7:0] narrow_xy_addr_value;
       begin
           //
           if ((narrow_xy_addr_value[7:3] == col_index_value) && (narrow_xy_bank_value != BANK_NARROW_EXT))
               //
               case (narrow_xy_addr_value[2:0])
                   3'b000: calc_rectangle_bitmap_lsb = 8'b00000001;
                   3'b001: calc_rectangle_bitmap_lsb = 8'b00000010;
                   3'b010: calc_rectangle_bitmap_lsb = 8'b00000100;
                   3'b011: calc_rectangle_bitmap_lsb = 8'b00001000;
                   3'b100: calc_rectangle_bitmap_lsb = 8'b00010000;
                   3'b101: calc_rectangle_bitmap_lsb = 8'b00100000;
                   3'b110: calc_rectangle_bitmap_lsb = 8'b01000000;
                   3'b111: calc_rectangle_bitmap_lsb = 8'b10000000;
               endcase
               //
           else
               calc_rectangle_bitmap_lsb = {8{1'b0}};
           //
        end
    endfunction
       
       /*
        * These can be simplified (the difference between square/triangle and
        * rectangle is that the bank is checked or not). A universal function would
        * accept a parameter that tells it whether it should check the bank or not.
        * Let's do it later, too early to optimize now, it seems.
        *
        *
        */
       
    function  [2:0] calc_square_triangle_index_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_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_square_triangle_index_lsb = 3'd0;
                    3'b001: calc_square_triangle_index_lsb = 3'd1;
                    3'b010: calc_square_triangle_index_lsb = 3'd2;
                    3'b011: calc_square_triangle_index_lsb = 3'd3;
                    3'b100: calc_square_triangle_index_lsb = 3'd4;
                    3'b101: calc_square_triangle_index_lsb = 3'd5;
                    3'b110: calc_square_triangle_index_lsb = 3'd6;
                    3'b111: calc_square_triangle_index_lsb = 3'd7;
                endcase
                //
            else
                calc_square_triangle_index_lsb = 3'dX;
            //
        end
    endfunction

    function  [2:0] calc_square_index_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_square_index_lsb = calc_square_triangle_index_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);
        end
    endfunction

    function  [2:0] calc_triangle_index_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_triangle_index_lsb = calc_square_triangle_index_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);
        end
    endfunction

    function  [2:0] calc_rectangle_index_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] slim_bram_xy_bank_value;
        input [7:0] slim_bram_xy_addr_value;
        begin
            //
            if ((slim_bram_xy_addr_value[7:3] == col_index_value) && (slim_bram_xy_bank_value != BANK_NARROW_EXT))
                //
                case (slim_bram_xy_addr_value[2:0])
                    3'b000: calc_rectangle_index_lsb = 3'd0;
                    3'b001: calc_rectangle_index_lsb = 3'd1;
                    3'b010: calc_rectangle_index_lsb = 3'd2;
                    3'b011: calc_rectangle_index_lsb = 3'd3;
                    3'b100: calc_rectangle_index_lsb = 3'd4;
                    3'b101: calc_rectangle_index_lsb = 3'd5;
                    3'b110: calc_rectangle_index_lsb = 3'd6;
                    3'b111: calc_rectangle_index_lsb = 3'd7;
                endcase
                //
            else
                calc_rectangle_index_lsb = 3'dX;
            //
        end
    endfunction
    
    function        calc_square_rectangle_purge_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            //
            if (narrow_xy_addr_value[7:3] == col_index_value)
                calc_square_rectangle_purge_lsb = narrow_xy_addr_value[7:3] == col_index_last_value;
            else
                calc_square_rectangle_purge_lsb = 1'b0;
            //
        end
    endfunction

    function        calc_square_purge_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_square_purge_lsb = calc_square_rectangle_purge_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);
        end
    endfunction

    function        calc_rectangle_purge_lsb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        begin
            calc_rectangle_purge_lsb = calc_square_rectangle_purge_lsb(col_index_value, col_index_last_value, narrow_xy_bank_value, narrow_xy_addr_value);
        end
    endfunction

    function        calc_square_valid_msb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        input [7:0] index_last_value;
        begin
            //
            if (narrow_xy_addr_value == index_last_value)
                calc_square_valid_msb = 1'b1;
            else
                calc_square_valid_msb = 1'b0;
            //
        end
    endfunction

    function        calc_rectangle_valid_msb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        input [7:0] index_last_value;
        begin
            //
            if ((narrow_xy_addr_value == 8'd1) && (narrow_xy_bank_value == BANK_NARROW_EXT))
                calc_rectangle_valid_msb = 1'b1;
            else
                calc_rectangle_valid_msb = 1'b0;
            //
        end
    endfunction
    
    function  [7:0] calc_square_bitmap_msb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        input [7:0] index_last_value;
        begin
            //
            if (narrow_xy_addr_value == index_last_value) begin
                calc_square_bitmap_msb[7] = col_index_value != col_index_last_value;
                calc_square_bitmap_msb[6:0] = 7'b1111111;
            end else
                calc_square_bitmap_msb[7:0] = 8'b00000000;
            //
        end
    endfunction

    function  [7:0] calc_rectangle_bitmap_msb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        input [7:0] index_last_value;
        begin
            //
            if ((narrow_xy_addr_value == 8'd1) && (narrow_xy_bank_value == BANK_NARROW_EXT)) begin
                calc_rectangle_bitmap_msb[7:0] = 8'b11111111;
            end else
                calc_rectangle_bitmap_msb[7:0] = 8'b00000000;
            //
        end
    endfunction

    function        calc_square_purge_msb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        input [7:0] index_last_value;
        begin
            //
            if (narrow_xy_addr_value == index_last_value)
                calc_square_purge_msb = col_index_value == col_index_last_value;
            else
                calc_square_purge_msb = 1'b0;
            //
        end
    endfunction

    function        calc_rectangle_purge_msb;
        input [4:0] col_index_value;
        input [4:0] col_index_last_value;
        input [BANK_ADDR_W -1:0] narrow_xy_bank_value;
        input [7:0] narrow_xy_addr_value;
        input [7:0] index_last_value;
        begin
            //
            if ((narrow_xy_addr_value == 8'd1) && (narrow_xy_bank_value == BANK_NARROW_EXT))
                calc_rectangle_purge_msb = col_index_value == col_index_last_value;
            else
                calc_rectangle_purge_msb = 1'b0;
            //
        end
    endfunction

    
    reg         rcmb_xy_lsb_ce = 1'b0;
    reg         rcmb_xy_lsb_ce_aux;
    reg  [ 2:0] rcmb_xy_lsb_ce_purge = 3'b000;
    wire        rcmb_xy_lsb_ce_combined = rcmb_xy_lsb_ce | rcmb_xy_lsb_ce_aux | rcmb_xy_lsb_ce_purge[0];
    reg         rcmb_xy_lsb_clr;

    reg  [46:0] rcmb_x_lsb_din;
    reg  [46:0] rcmb_y_lsb_din;
    wire [15:0] rcmb_x_lsb_dout;
    wire [15:0] rcmb_y_lsb_dout;

    reg         rcmb_xy_msb_ce = 1'b0;
    reg  [ 1:0] rcmb_xy_msb_ce_purge = 2'b00;
    wire        rcmb_xy_msb_ce_combined = rcmb_xy_msb_ce | rcmb_xy_msb_ce_purge[0];
    reg         rcmb_xy_msb_clr;
    
    reg  [46:0] rcmb_x_msb_din;
    reg  [46:0] rcmb_y_msb_din;
    wire [15:0] rcmb_x_msb_dout;
    wire [15:0] rcmb_y_msb_dout;
    
    modexpng_recombinator_cell recomb_x_lsb
    (
        .clk    (clk),
        .ce     (rcmb_xy_lsb_ce_combined),
        .clr    (rcmb_xy_lsb_clr),
        .din    (rcmb_x_lsb_din),
        .dout   (rcmb_x_lsb_dout)
    );
    modexpng_recombinator_cell recomb_y_lsb
    (
        .clk    (clk),
        .ce     (rcmb_xy_lsb_ce_combined),
        .clr    (rcmb_xy_lsb_clr),
        .din    (rcmb_y_lsb_din),
        .dout   (rcmb_y_lsb_dout)
    );

    modexpng_recombinator_cell recomb_x_msb
    (
        .clk    (clk),
        .ce     (rcmb_xy_msb_ce_combined),
        .clr    (rcmb_xy_msb_clr),
        .din    (rcmb_x_msb_din),
        .dout   (rcmb_x_msb_dout)
    );
    
    modexpng_recombinator_cell recomb_y_msb
    (
        .clk    (clk),
        .ce     (rcmb_xy_msb_ce_combined),
        .clr    (rcmb_xy_msb_clr),
        .din    (rcmb_y_msb_din),
        .dout   (rcmb_y_msb_dout)
    );

    always @(posedge clk) begin
        //
        rcmb_xy_lsb_ce <= xy_valid_latch_lsb;
        rcmb_xy_lsb_ce_aux <= xy_aux_latch_lsb;
        rcmb_xy_msb_ce <= xy_bitmap_latch_msb[0];
        //
        if (xy_purge_latch_lsb)
            rcmb_xy_lsb_ce_purge <= 3'b111;
        else
            rcmb_xy_lsb_ce_purge <= {1'b0, rcmb_xy_lsb_ce_purge[2:1]};
        //
        if (xy_purge_latch_msb && xy_bitmap_latch_msb[0] && !xy_bitmap_latch_msb[1])
            rcmb_xy_msb_ce_purge <= 2'b11;
        else
            rcmb_xy_msb_ce_purge <= {1'b0, rcmb_xy_msb_ce_purge[1]};
        //
    end


    always @(posedge clk)
        //
        if (ena) begin
            rcmb_xy_lsb_clr <= 1'b1;
            rcmb_xy_msb_clr <= 1'b1;
        end else begin
            if (rcmb_xy_lsb_ce) rcmb_xy_lsb_clr <= 1'b0;
            if (rcmb_xy_msb_ce) rcmb_xy_msb_clr <= 1'b0;
        end

    always @(posedge clk)
        //
        if (xy_valid_latch_lsb) begin
            rcmb_x_lsb_din <= dsp_x_p_latch[xy_index_latch_lsb];
            rcmb_y_lsb_din <= dsp_y_p_latch[xy_index_latch_lsb];
        end else if (xy_aux_latch_lsb) begin
            rcmb_x_lsb_din <= dsp_x_p_latch[8];
            rcmb_y_lsb_din <= dsp_y_p_latch[8];
        end else begin
            rcmb_x_lsb_din <= {47{1'b0}};
            rcmb_y_lsb_din <= {47{1'b0}};
        end

    always @(posedge clk)
        //
        if (xy_bitmap_latch_msb[0]) begin
            rcmb_x_msb_din <= dsp_x_p_latch[0];
            rcmb_y_msb_din <= dsp_y_p_latch[0];
        end else begin
            rcmb_x_msb_din <= {47{1'b0}};
            rcmb_y_msb_din <= {47{1'b0}};
        end


    always @(posedge clk)
        //
        case (fsm_state_next)
            //
            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: begin
                //
                xy_valid_lsb_adv [6] <= calc_square_valid_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_aux_lsb_adv   [6] <= 1'b0;
                xy_bitmap_lsb_adv[6] <= calc_square_bitmap_lsb(col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_index_lsb_adv [6] <= calc_square_index_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_purge_lsb_adv [6] <= calc_square_purge_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                //
                xy_valid_msb_adv [6] <= calc_square_valid_msb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr, word_index_last);
                xy_bitmap_msb_adv[6] <= calc_square_bitmap_msb(col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr, word_index_last);
                xy_purge_msb_adv [6] <= calc_square_purge_msb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr, word_index_last);
                //
            end
            //
            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: begin
                //
                xy_valid_lsb_adv [6] <= calc_triangle_valid_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_aux_lsb_adv   [6] <= calc_triangle_aux_lsb   (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_bitmap_lsb_adv[6] <= calc_triangle_bitmap_lsb(col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_index_lsb_adv [6] <= calc_triangle_index_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_purge_lsb_adv [6] <= 1'b0;
                //
                xy_valid_msb_adv [6] <= 1'b0;
                xy_bitmap_msb_adv[6] <= {8{1'b0}};
                xy_purge_msb_adv [6] <= 1'b0;
                //
            end
            //
            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: begin
                //
                xy_valid_lsb_adv [6] <= calc_rectangle_valid_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_aux_lsb_adv   [6] <= 1'b0;
                xy_bitmap_lsb_adv[6] <= calc_rectangle_bitmap_lsb(col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_index_lsb_adv [6] <= calc_rectangle_index_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                xy_purge_lsb_adv [6] <= calc_rectangle_purge_lsb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr);
                //
                xy_valid_msb_adv [6] <= calc_rectangle_valid_msb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr, word_index_last);
                xy_bitmap_msb_adv[6] <= calc_rectangle_bitmap_msb(col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr, word_index_last);
                xy_purge_msb_adv [6] <= calc_rectangle_purge_msb (col_index, col_index_last, rd_narrow_xy_bank, rd_narrow_xy_addr, word_index_last);
                //
            end
            //
            default: begin
                //
                xy_valid_lsb_adv [6] <= 1'b0;
                xy_aux_lsb_adv   [6] <= 1'b0;
                xy_bitmap_lsb_adv[6] <= {8{1'b0}};
                xy_index_lsb_adv [6] <= 3'dX;
                xy_purge_lsb_adv [6] <= 1'b0;
                //
                xy_valid_msb_adv [6] <= 1'b0;
                xy_bitmap_msb_adv[6] <= {8{1'b0}};
                xy_purge_msb_adv [6] <= 1'b0;
                //
            end
            //
        endcase


    always @(posedge clk) begin
        //
        xy_valid_lsb  <= xy_valid_lsb_adv [1];
        xy_aux_lsb    <= xy_aux_lsb_adv   [1];
        xy_bitmap_lsb <= xy_bitmap_lsb_adv[1];
        xy_index_lsb  <= xy_index_lsb_adv [1];
        xy_purge_lsb  <= xy_purge_lsb_adv [1];
        //
        xy_valid_latch_lsb  <= xy_valid_lsb;
        xy_aux_latch_lsb    <= xy_aux_lsb;
        xy_bitmap_latch_lsb <= xy_bitmap_lsb;
        xy_index_latch_lsb  <= xy_index_lsb;
        xy_purge_latch_lsb  <= xy_purge_lsb;
        //
        xy_valid_msb  <= xy_valid_msb_adv[1];
        xy_bitmap_msb <= xy_bitmap_msb_adv[1];
        xy_purge_msb  <= xy_purge_msb_adv[1];
        //
        if (xy_valid_msb) begin
            xy_bitmap_latch_msb <= xy_bitmap_msb;
            xy_purge_latch_msb  <= xy_purge_msb;
        end else begin
            xy_bitmap_latch_msb <= {1'b0, xy_bitmap_latch_msb[7:1]};
        end
        //
        //
        for (i=1; i<6; i=i+1) begin
            xy_valid_lsb_adv [i] <= xy_valid_lsb_adv [i+1];
            xy_aux_lsb_adv   [i] <= xy_aux_lsb_adv   [i+1];
            xy_bitmap_lsb_adv[i] <= xy_bitmap_lsb_adv[i+1];
            xy_index_lsb_adv [i] <= xy_index_lsb_adv [i+1];
            xy_purge_lsb_adv [i] <= xy_purge_lsb_adv [i+1];
            //
            xy_valid_msb_adv [i] <= xy_valid_msb_adv [i+1];
            xy_bitmap_msb_adv[i] <= xy_bitmap_msb_adv[i+1];
            xy_purge_msb_adv [i] <= xy_purge_msb_adv [i+1];
        end
        //
    end

    always @(posedge clk)
        //
        if (xy_bitmap_latch_msb[1])   // only shift 7 times
            //
            for (i=0; i<8; i=i+1)
                //            
                if (i < 7) begin
                    dsp_x_p_latch[i] <= dsp_x_p_latch[i+1];
                    dsp_y_p_latch[i] <= dsp_y_p_latch[i+1];
                end else begin
                    dsp_x_p_latch[i] <= {47{1'bX}};
                    dsp_y_p_latch[i] <= {47{1'bX}};
                end
            //
        else if (dsp_xy_ce_p_dly1) begin
            //
            for (i=0; i<8; i=i+1)
                //
                if (xy_bitmap_lsb[i]) begin
                    dsp_x_p_latch[i] <= dsp_x_p_split[i];
                    dsp_y_p_latch[i] <= dsp_y_p_split[i];
                end else if (xy_valid_msb && xy_bitmap_msb[i]) begin
                    dsp_x_p_latch[i] <= dsp_x_p_split[i];
                    dsp_y_p_latch[i] <= dsp_y_p_split[i];
                end
            //
            if (xy_aux_lsb) begin
                dsp_x_p_latch[8] <= dsp_x_p_split[8];
                dsp_y_p_latch[8] <= dsp_y_p_split[8];
            end
            //
        end

    reg rcmb_xy_lsb_valid = 1'b0;
    reg rcmb_xy_msb_valid = 1'b0;

    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) begin
            rcmb_xy_lsb_valid <= 1'b0;
            rcmb_xy_msb_valid <= 1'b0;        
        end else begin
            rcmb_xy_lsb_valid <= rcmb_xy_lsb_ce_combined;
            rcmb_xy_msb_valid <= rcmb_xy_msb_ce_combined;
        end        


    reg [ BANK_ADDR_W -1:0] wide_xy_bank;
    reg [ 7:0] wide_xy_addr;
    reg [17:0] wide_x_dout;
    reg [17:0] wide_y_dout;
    reg        wide_xy_valid = 1'b0;

    reg [ BANK_ADDR_W -1:0] narrow_xy_bank;
    reg [ 7:0] narrow_xy_addr;
    reg [17:0] narrow_x_dout;
    reg [17:0] narrow_y_dout;
    reg        narrow_xy_valid = 1'b0;

    reg [ BANK_ADDR_W -1:0] rdct_xy_bank;
    reg [ 7:0] rdct_xy_addr;
    reg [17:0] rdct_x_dout;
    reg [17:0] rdct_y_dout;
    reg        rdct_xy_valid = 1'b0;

    reg [ 7:0] cnt_lsb;
    reg [ 7:0] cnt_msb;
    
    reg        cnt_lsb_wrapped;
    reg        cnt_msb_wrapped;

    reg [31:0] rcmb_xy_msb_carry_0;
    reg [31:0] rcmb_xy_msb_carry_1;
    
    reg [31:0] rcmb_xy_msb_delay_0;
    reg [31:0] rcmb_xy_msb_delay_1;
    reg [31:0] rcmb_xy_msb_delay_2;
    
    reg [ 7:0] rcmb_msb_cnt_delay_0 = 8'd0;
    reg [ 7:0] rcmb_msb_cnt_delay_1 = 8'd0;
    reg [ 7:0] rcmb_msb_cnt_delay_2 = 8'd0;

    reg        rcmb_msb_flag_delay_0;
    reg        rcmb_msb_flag_delay_1;
    reg        rcmb_msb_flag_delay_2;
    
    assign rcmb_wide_xy_bank  = wide_xy_bank;
    assign rcmb_wide_xy_addr  = wide_xy_addr;
    assign rcmb_wide_x_dout   = wide_x_dout;
    assign rcmb_wide_y_dout   = wide_y_dout;
    assign rcmb_wide_xy_valid = wide_xy_valid;

    assign rcmb_narrow_xy_bank  = narrow_xy_bank;
    assign rcmb_narrow_xy_addr  = narrow_xy_addr;
    assign rcmb_narrow_x_dout   = narrow_x_dout;
    assign rcmb_narrow_y_dout   = narrow_y_dout;
    assign rcmb_narrow_xy_valid = narrow_xy_valid;

    assign rdct_narrow_xy_bank  = rdct_xy_bank;
    assign rdct_narrow_xy_addr  = rdct_xy_addr;
    assign rdct_narrow_x_dout   = rdct_x_dout;
    assign rdct_narrow_y_dout   = rdct_y_dout;
    assign rdct_narrow_xy_valid = rdct_xy_valid;
    
    reg rdy_reg = 1'b1;
    reg rdy_adv = 1'b1;
    
    assign rdy = rdy_reg;
    
    
    always @(posedge clk)
        //
        if (ena) rdy_reg <= 1'b0;
        else     rdy_reg <= rdy_adv;
            
    task advance_rcmb_msb_delay;
        input [15:0] dout_x;
        input [15:0] dout_y;
        input [ 7:0] cnt;
        input        flag;
        begin
            //
            rcmb_xy_msb_delay_0 <= {dout_y, dout_x};
            rcmb_xy_msb_delay_1 <= rcmb_xy_msb_delay_0;
            rcmb_xy_msb_delay_2 <= rcmb_xy_msb_delay_1;
            //
            rcmb_msb_cnt_delay_0 <= cnt;
            rcmb_msb_cnt_delay_1 <= rcmb_msb_cnt_delay_0;
            rcmb_msb_cnt_delay_2 <= rcmb_msb_cnt_delay_1;
            //
            rcmb_msb_flag_delay_0 <= flag;
            rcmb_msb_flag_delay_1 <= rcmb_msb_flag_delay_0;
            rcmb_msb_flag_delay_2 <= rcmb_msb_flag_delay_1;
            //
        end
    endtask
         
    task shift_rcmb_msb_carry;
        input [15:0] dout_x;
        input [15:0] dout_y;
        begin
            rcmb_xy_msb_carry_0 <= {dout_y, dout_x};
            rcmb_xy_msb_carry_1 <= rcmb_xy_msb_carry_0;
        end
    endtask
    
    task _update_wide;
        input [ BANK_ADDR_W -1:0] bank;
        input [ 7:0] addr;
        input [17:0] dout_x;
        input [17:0] dout_y;
        input        valid;
        begin
            wide_xy_bank       <= bank;
            wide_xy_addr       <= addr;
            wide_x_dout        <= dout_x;
            wide_y_dout        <= dout_y;
            wide_xy_valid <= valid;
        end
    endtask
    
    task _update_narrow;
        input [ BANK_ADDR_W -1:0] bank;
        input [ 7:0] addr;
        input [17:0] dout_x;
        input [17:0] dout_y;
        input        valid;
        begin
            narrow_xy_bank       <= bank;
            narrow_xy_addr       <= addr;
            narrow_x_dout        <= dout_x;
            narrow_y_dout        <= dout_y;
            narrow_xy_valid <= valid;
        end
    endtask

    task _update_rdct;
        input [ BANK_ADDR_W -1:0] bank;
        input [ 7:0] addr;
        input [17:0] dout_x;
        input [17:0] dout_y;
        input        valid;
        begin
            rdct_xy_bank       <= bank;
            rdct_xy_addr       <= addr;
            rdct_x_dout        <= dout_x;
            rdct_y_dout        <= dout_y;
            rdct_xy_valid <= valid;
        end
    endtask
            
    task set_wide;
        input [ BANK_ADDR_W -1:0] bank;
        input [ 7:0] addr;
        input [17:0] dout_x;
        input [17:0] dout_y;
        begin
            _update_wide(bank, addr, dout_x, dout_y, 1'b1);
        end
    endtask
    
    task set_narrow;
        input [ BANK_ADDR_W -1:0] bank;
        input [ 7:0] addr;
        input [17:0] dout_x;
        input [17:0] dout_y;
        begin
            _update_narrow(bank, addr, dout_x, dout_y, 1'b1);
        end
    endtask
    
    task set_rdct;
        input [ BANK_ADDR_W -1:0] bank;
        input [ 7:0] addr;
        input [17:0] dout_x;
        input [17:0] dout_y;
        begin
            _update_rdct(bank, addr, dout_x, dout_y, 1'b1);
        end
    endtask
    
    task clear_wide;
        begin
            _update_wide(BANK_DNC, OP_ADDR_DNC, WORD_EXT_DNC, WORD_EXT_DNC, 1'b0);
        end
    endtask

    task clear_narrow;
        begin
            _update_narrow(BANK_DNC, OP_ADDR_DNC, WORD_EXT_DNC, WORD_EXT_DNC, 1'b0);
        end
    endtask

    task clear_rdct;
        begin
            _update_rdct(BANK_DNC, OP_ADDR_DNC, WORD_EXT_DNC, WORD_EXT_DNC, 1'b0);
        end
    endtask
    
    task _set_cnt_lsb;
        input [7:0] cnt;
        input       wrapped;
        begin
            cnt_lsb <= cnt;
            cnt_lsb_wrapped <= wrapped;
        end
    endtask
    
    task _set_cnt_msb;
        input [7:0] cnt;
        input       wrapped;
        begin
            cnt_msb <= cnt;
            cnt_msb_wrapped <= wrapped;
        end
    endtask    

    task inc_cnt_lsb;
        begin
            if (cnt_lsb == word_index_last)
                _set_cnt_lsb(8'd0, 1'b1);
            else
                _set_cnt_lsb(cnt_lsb + 1'b1, cnt_lsb_wrapped);
        end
    endtask
    
    task inc_cnt_both;
        begin
            inc_cnt_lsb;
            inc_cnt_msb;
        end
    endtask
    
    task inc_cnt_msb;
        begin
            if (cnt_msb == word_index_last)
                _set_cnt_msb(8'd0, 1'b1);
            else
                _set_cnt_msb(cnt_msb + 1'b1, cnt_msb_wrapped);
        end
    endtask
    
    task clr_cnt_lsb;
        begin
            _set_cnt_lsb(8'd0, 1'b0);
        end
    endtask
    
    task clr_cnt_msb;
        begin
            _set_cnt_msb(8'd0, 1'b0);
        end
    endtask
    
   

    wire [1:0] rcmb_xy_valid = {rcmb_xy_msb_valid, rcmb_xy_lsb_valid}; 
    
    always @(posedge clk)
        //
        if (ena) begin
            clr_cnt_lsb();
            clr_cnt_msb();
        end else if (!rdy)
            //
            case (rcmb_mode)
                2'd1: recombine_square();
                2'd2: recombine_triangle();
                2'd3: recombine_rectangle();
            endcase
           
    wire [17:0] rcmb_x_lsb_dout_pad = {2'b00, rcmb_x_lsb_dout};
    wire [17:0] rcmb_y_lsb_dout_pad = {2'b00, rcmb_y_lsb_dout};

    wire [17:0] rcmb_x_msb_dout_pad = {2'b00, rcmb_x_msb_dout};
    wire [17:0] rcmb_y_msb_dout_pad = {2'b00, rcmb_y_msb_dout};
    
    wire [17:0] rcmb_x_msb_delay_2_pad = {2'b00, rcmb_xy_msb_delay_2[15: 0]};
    wire [17:0] rcmb_y_msb_delay_2_pad = {2'b00, rcmb_xy_msb_delay_2[31:16]};

    wire [17:0] rcmb_x_lsb_msb_carry_1_pad = {1'b0, {1'b0, rcmb_x_lsb_dout} + {1'b0, rcmb_xy_msb_carry_1[15: 0]}};
    wire [17:0] rcmb_y_lsb_msb_carry_1_pad = {1'b0, {1'b0, rcmb_y_lsb_dout} + {1'b0, rcmb_xy_msb_carry_1[31:16]}};
           
           
    task recombine_square;
        //
        begin
            //
            case (rcmb_xy_valid)
                //
                2'b01: inc_cnt_lsb; 
                2'b10: inc_cnt_msb;
                2'b11: inc_cnt_both;
                //
            endcase            
            //
            case (rcmb_xy_valid)
                //
                2'b00: if (rcmb_msb_flag_delay_2) set_wide(BANK_WIDE_H, rcmb_msb_cnt_delay_2, rcmb_x_msb_delay_2_pad, rcmb_y_msb_delay_2_pad);
                       else                       clear_wide;
                //
                2'b01:                            set_wide(BANK_WIDE_L, cnt_lsb, rcmb_x_lsb_dout_pad, rcmb_y_lsb_dout_pad);
                //
                2'b10: if (cnt_msb < 8'd2)        clear_wide;                        
                       else                       set_wide(BANK_WIDE_H, cnt_msb, rcmb_x_msb_dout_pad, rcmb_y_msb_dout_pad);
                //
                2'b11: if (cnt_lsb_wrapped)       set_wide(BANK_WIDE_H, cnt_lsb, rcmb_x_lsb_msb_carry_1_pad, rcmb_y_lsb_msb_carry_1_pad); 
                       else                       set_wide(BANK_WIDE_L, cnt_lsb, rcmb_x_lsb_dout_pad,        rcmb_y_lsb_dout_pad);
                //
            endcase            
            //
            case (rcmb_xy_valid)
                //
                2'b00: if (rcmb_msb_flag_delay_2) advance_rcmb_msb_delay(16'hXXXX, 16'hXXXX, 8'd0, 1'b0);
                2'b10: if (cnt_msb < 8'd2)        shift_rcmb_msb_carry(rcmb_x_msb_dout, rcmb_y_msb_dout);
                //
                2'b11: begin                      advance_rcmb_msb_delay(rcmb_x_msb_dout, rcmb_y_msb_dout, cnt_msb, 1'b1);
                       if (cnt_lsb_wrapped)       shift_rcmb_msb_carry({16{1'bX}}, {16{1'bX}});
                       end
                //
            endcase
            //        
        end
        //
    endtask
    
    
    task recombine_triangle;
        //
        begin
            //
            case (rcmb_xy_valid)
                //
                2'b01: inc_cnt_lsb(); 
               //
            endcase            
            //
            case (rcmb_xy_valid)
                //
                2'b00:                        clear_narrow;
                2'b01:  if (!cnt_lsb_wrapped) set_narrow(BANK_NARROW_Q,   cnt_lsb, rcmb_x_lsb_dout_pad, rcmb_y_lsb_dout_pad); 
                        else                  set_narrow(BANK_NARROW_EXT, 8'd1,    rcmb_x_lsb_dout_pad, rcmb_y_lsb_dout_pad);
                2'b10:                        clear_narrow;
                2'b11:                        clear_narrow;
                //
            endcase
            //        
        end
        //
    endtask


    task recombine_rectangle;
        //
        begin
            //
            case (rcmb_xy_valid)
                //
                2'b01:  inc_cnt_lsb; 
                2'b10:  inc_cnt_msb;
                2'b11:  inc_cnt_both;
                //
            endcase
//            //
            case (rcmb_xy_valid)
//                //
                2'b00:  if (rcmb_msb_flag_delay_2)  set_rdct(BANK_RCMB_MH, rcmb_msb_cnt_delay_2, rcmb_x_msb_delay_2_pad, rcmb_y_msb_delay_2_pad);
                        else                        clear_rdct;
                2'b01:                              set_rdct(BANK_RCMB_ML, cnt_lsb, rcmb_x_lsb_dout_pad, rcmb_y_lsb_dout_pad); 
                2'b10:  if (!cnt_msb_wrapped) begin 
                            if (cnt_msb < 8'd2)     clear_rdct;                        
                            else                    set_rdct(BANK_RCMB_MH, cnt_msb, rcmb_x_msb_dout_pad, rcmb_y_msb_dout_pad);
                        end else                    set_rdct(BANK_RCMB_EXT, 8'd0, rcmb_x_msb_dout_pad, rcmb_y_msb_dout_pad);
                            
                2'b11:  set_rdct(BANK_RCMB_MH, cnt_lsb, rcmb_x_lsb_msb_carry_1_pad, rcmb_y_lsb_msb_carry_1_pad); 
//                //
            endcase            
//            //
            case (rcmb_xy_valid)
//                //
                2'b00:  if (rcmb_msb_flag_delay_2)  advance_rcmb_msb_delay(16'hXXXX, 16'hXXXX, 8'd0, 1'b0);
                2'b10:  begin 
                            if ((cnt_msb < 8'd2) && !cnt_msb_wrapped) shift_rcmb_msb_carry(rcmb_x_msb_dout, rcmb_y_msb_dout);
                            if (cnt_msb_wrapped) advance_rcmb_msb_delay(16'hXXXX, 16'hXXXX, 8'd0, 1'b0);
                        end
//                //
                2'b11:  begin  advance_rcmb_msb_delay(rcmb_x_msb_dout, rcmb_y_msb_dout, cnt_msb, 1'b1);
                                   shift_rcmb_msb_carry({16{1'bX}}, {16{1'bX}});
                        end
//                //
            endcase
            //
        end
        //
    endtask
    
    
    always @(posedge clk)
        //
        if (ena) begin
            rdy_adv <= 1'b0;
        end else if (!rdy_reg) begin
            //
            case (rcmb_mode)
                //
                2'd1:   case (rcmb_xy_valid)
                            //
                            2'b00: begin
                                //
                                if (rcmb_msb_flag_delay_2) begin
                                    //
                                    rdy_adv <= ~rcmb_msb_flag_delay_1;
                                    //
                                end
                                //
                            end
                            //
                        endcase
                //
                2'd2:   case (rcmb_xy_valid)
                            //
                            2'b01: rdy_adv <= cnt_lsb_wrapped;                                //
                            //
                        endcase
                //
                2'd3: case (rcmb_xy_valid)
                                            //
                                            2'b00: begin
                                                //
                                                if (rcmb_msb_flag_delay_2) begin
                                                    //
                                                    rdy_adv <= ~rcmb_msb_flag_delay_1;
                                                    //
                                                end
                                                //
                                            end
                                            //
                                        endcase
                //
            endcase
            //        
        end
        
endmodule