aboutsummaryrefslogtreecommitdiff
path: root/rtl/modexpng_uop_engine.v
blob: c7b064a2f402d31b8ca3758744f1b4a1bd64ebe8 (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
module modexpng_uop_engine
(
    clk,
    rst_n,

    ena,
    rdy,
 
    `ifdef MODEXPNG_ENABLE_DEBUG
    uop_decoded_stop,
    `endif
    
    crt_mode,
    
    word_index_last_n,
    word_index_last_pq,

    bit_index_last_n,
    bit_index_last_pq,
    
    io_mgr_ena,
    io_mgr_rdy,
    io_mgr_sel_crt,
    io_mgr_sel_aux,
    io_mgr_sel_in,
    io_mgr_sel_out,
    io_mgr_word_index_last,
    io_mgr_opcode,
    io_mgr_ladder_steps,
    io_mgr_ladder_d,
    io_mgr_ladder_p,
    io_mgr_ladder_q,
    io_mgr_ladder_done,
    
    mmm_ena_x,
    mmm_ena_y,  
    mmm_rdy_x,
    mmm_rdy_y,
    mmm_word_index_last_x,
    mmm_word_index_last_y,
    mmm_word_index_last_minus1_x,
    mmm_word_index_last_minus1_y,
    mmm_ladder_mode_x,
    mmm_ladder_mode_y,
    mmm_sel_wide_in_x,
    mmm_sel_wide_in_y,
    mmm_sel_narrow_in_x,
    mmm_sel_narrow_in_y,
    mmm_force_unity_b_x,
    mmm_force_unity_b_y,
    mmm_only_reduce_x,
    mmm_only_reduce_y,
    mmm_just_multiply_x,
    mmm_just_multiply_y,

    rdct_word_index_last_x,
    rdct_word_index_last_y,
    rdct_sel_wide_out_x,
    rdct_sel_narrow_out_x,
    rdct_sel_wide_out_y,
    rdct_sel_narrow_out_y,

    wrk_ena,
    wrk_rdy,
    wrk_sel_wide_in,
    wrk_sel_wide_out,
    wrk_sel_narrow_in,
    wrk_sel_narrow_out,
    wrk_word_index_last,
    wrk_word_index_last_half,
    wrk_opcode
);


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


    //
    // Ports
    //
    input                      clk;
    input                      rst_n;
    
    input                      ena;
    output                     rdy;

    `ifdef MODEXPNG_ENABLE_DEBUG
    output                     uop_decoded_stop;
    `endif
    
    input                      crt_mode;
    
    input  [OP_ADDR_W    -1:0] word_index_last_n;
    input  [OP_ADDR_W    -1:0] word_index_last_pq;

    input  [BIT_INDEX_W  -1:0] bit_index_last_n;
    input  [BIT_INDEX_W  -1:0] bit_index_last_pq;

    output                     io_mgr_ena;
    input                      io_mgr_rdy;
    output [UOP_CRT_W    -1:0] io_mgr_sel_crt;
    output [UOP_AUX_W    -1:0] io_mgr_sel_aux;
    output [BANK_ADDR_W  -1:0] io_mgr_sel_in;
    output [BANK_ADDR_W  -1:0] io_mgr_sel_out;
    output [OP_ADDR_W    -1:0] io_mgr_word_index_last;
    output [UOP_OPCODE_W -1:0] io_mgr_opcode;
    output [BIT_INDEX_W  -1:0] io_mgr_ladder_steps;
    input                      io_mgr_ladder_d;
    input                      io_mgr_ladder_p;
    input                      io_mgr_ladder_q;
    input                      io_mgr_ladder_done;
    
    output                     mmm_ena_x;
    output                     mmm_ena_y;  
    input                      mmm_rdy_x;
    input                      mmm_rdy_y; 
    output [OP_ADDR_W    -1:0] mmm_word_index_last_x;
    output [OP_ADDR_W    -1:0] mmm_word_index_last_y;
    output [OP_ADDR_W    -1:0] mmm_word_index_last_minus1_x;
    output [OP_ADDR_W    -1:0] mmm_word_index_last_minus1_y;
    output                     mmm_ladder_mode_x;
    output                     mmm_ladder_mode_y;
    output [BANK_ADDR_W  -1:0] mmm_sel_wide_in_x;
    output [BANK_ADDR_W  -1:0] mmm_sel_wide_in_y;
    output [BANK_ADDR_W  -1:0] mmm_sel_narrow_in_x;
    output [BANK_ADDR_W  -1:0] mmm_sel_narrow_in_y;
    output                     mmm_force_unity_b_x;
    output                     mmm_force_unity_b_y;
    output                     mmm_only_reduce_x;
    output                     mmm_only_reduce_y;
    output                     mmm_just_multiply_x;
    output                     mmm_just_multiply_y;
   
    output [OP_ADDR_W    -1:0] rdct_word_index_last_x;
    output [OP_ADDR_W    -1:0] rdct_word_index_last_y;
    output [BANK_ADDR_W  -1:0] rdct_sel_wide_out_x;
    output [BANK_ADDR_W  -1:0] rdct_sel_narrow_out_x;
    output [BANK_ADDR_W  -1:0] rdct_sel_wide_out_y;
    output [BANK_ADDR_W  -1:0] rdct_sel_narrow_out_y;
   
    output                     wrk_ena;
    input                      wrk_rdy;
    output [BANK_ADDR_W  -1:0] wrk_sel_wide_in;
    output [BANK_ADDR_W  -1:0] wrk_sel_wide_out;
    output [BANK_ADDR_W  -1:0] wrk_sel_narrow_in;
    output [BANK_ADDR_W  -1:0] wrk_sel_narrow_out;
    output [OP_ADDR_W    -1:0] wrk_word_index_last;
    output [OP_ADDR_W    -1:0] wrk_word_index_last_half;
    output [UOP_OPCODE_W -1:0] wrk_opcode;


    //
    // Registers
    //
    reg                     io_mgr_ena_r = 1'b0;
    reg [UOP_CRT_W    -1:0] io_mgr_sel_crt_r;
    reg [UOP_AUX_W    -1:0] io_mgr_sel_aux_r;
    reg [BANK_ADDR_W  -1:0] io_mgr_sel_in_r;
    reg [BANK_ADDR_W  -1:0] io_mgr_sel_out_r;
    reg [OP_ADDR_W    -1:0] io_mgr_word_index_last_r;
    reg [UOP_OPCODE_W -1:0] io_mgr_opcode_r;
    reg [BIT_INDEX_W  -1:0] io_mgr_ladder_steps_r;
    
    reg                     mmm_ena_x_r = 1'b0;
    reg                     mmm_ena_y_r = 1'b0;  
    reg [OP_ADDR_W    -1:0] mmm_word_index_last_x_r;
    reg [OP_ADDR_W    -1:0] mmm_word_index_last_y_r;
    reg [OP_ADDR_W    -1:0] mmm_word_index_last_minus1_x_r;
    reg [OP_ADDR_W    -1:0] mmm_word_index_last_minus1_y_r;
    reg                     mmm_ladder_mode_x_r;
    reg                     mmm_ladder_mode_y_r;
    reg [BANK_ADDR_W  -1:0] mmm_sel_wide_in_x_r;
    reg [BANK_ADDR_W  -1:0] mmm_sel_wide_in_y_r;
    reg [BANK_ADDR_W  -1:0] mmm_sel_narrow_in_x_r;
    reg [BANK_ADDR_W  -1:0] mmm_sel_narrow_in_y_r;
    reg                     mmm_force_unity_b_x_r;
    reg                     mmm_force_unity_b_y_r;
    reg                     mmm_only_reduce_x_r;
    reg                     mmm_only_reduce_y_r;
    reg                     mmm_just_multiply_x_r;
    reg                     mmm_just_multiply_y_r;
   
    reg [OP_ADDR_W    -1:0] rdct_word_index_last_x_r;
    reg [OP_ADDR_W    -1:0] rdct_word_index_last_y_r;
    reg [BANK_ADDR_W  -1:0] rdct_sel_wide_out_x_r;
    reg [BANK_ADDR_W  -1:0] rdct_sel_narrow_out_x_r;
    reg [BANK_ADDR_W  -1:0] rdct_sel_wide_out_y_r;
    reg [BANK_ADDR_W  -1:0] rdct_sel_narrow_out_y_r;

    reg                     wrk_ena_r = 1'b0;
    reg [BANK_ADDR_W  -1:0] wrk_sel_wide_in_r;
    reg [BANK_ADDR_W  -1:0] wrk_sel_wide_out_r;
    reg [BANK_ADDR_W  -1:0] wrk_sel_narrow_in_r;
    reg [BANK_ADDR_W  -1:0] wrk_sel_narrow_out_r;
    reg [OP_ADDR_W    -1:0] wrk_word_index_last_r;
    reg [OP_ADDR_W    -1:0] wrk_word_index_last_half_r;
    reg [UOP_OPCODE_W -1:0] wrk_opcode_r;
    
    
    //
    // Mapping
    //
    assign io_mgr_ena                   = io_mgr_ena_r;
    assign io_mgr_sel_crt               = io_mgr_sel_crt_r;
    assign io_mgr_sel_aux               = io_mgr_sel_aux_r;
    assign io_mgr_sel_in                = io_mgr_sel_in_r;
    assign io_mgr_sel_out               = io_mgr_sel_out_r;
    assign io_mgr_word_index_last       = io_mgr_word_index_last_r;
    assign io_mgr_opcode                = io_mgr_opcode_r;
    assign io_mgr_ladder_steps          = io_mgr_ladder_steps_r;
   
    assign mmm_ena_x                    = mmm_ena_x_r;
    assign mmm_ena_y                    = mmm_ena_y_r;
    assign mmm_word_index_last_x        = mmm_word_index_last_x_r;
    assign mmm_word_index_last_y        = mmm_word_index_last_y_r;
    assign mmm_word_index_last_minus1_x = mmm_word_index_last_minus1_x_r;
    assign mmm_word_index_last_minus1_y = mmm_word_index_last_minus1_y_r;
    assign mmm_ladder_mode_x            = mmm_ladder_mode_x_r;
    assign mmm_ladder_mode_y            = mmm_ladder_mode_y_r;
    assign mmm_sel_wide_in_x            = mmm_sel_wide_in_x_r;
    assign mmm_sel_wide_in_y            = mmm_sel_wide_in_y_r;
    assign mmm_sel_narrow_in_x          = mmm_sel_narrow_in_x_r;
    assign mmm_sel_narrow_in_y          = mmm_sel_narrow_in_y_r;
    assign mmm_force_unity_b_x          = mmm_force_unity_b_x_r;
    assign mmm_force_unity_b_y          = mmm_force_unity_b_y_r;
    assign mmm_only_reduce_x            = mmm_only_reduce_x_r;
    assign mmm_only_reduce_y            = mmm_only_reduce_y_r;
    assign mmm_just_multiply_x          = mmm_just_multiply_x_r;
    assign mmm_just_multiply_y          = mmm_just_multiply_y_r;
   
    assign rdct_word_index_last_x       = rdct_word_index_last_x_r;
    assign rdct_word_index_last_y       = rdct_word_index_last_y_r;
    assign rdct_sel_wide_out_x          = rdct_sel_wide_out_x_r;
    assign rdct_sel_wide_out_y          = rdct_sel_wide_out_y_r;
    assign rdct_sel_narrow_out_x        = rdct_sel_narrow_out_x_r;
    assign rdct_sel_narrow_out_y        = rdct_sel_narrow_out_y_r;

    assign wrk_ena                      = wrk_ena_r;
    assign wrk_sel_wide_in              = wrk_sel_wide_in_r;
    assign wrk_sel_wide_out             = wrk_sel_wide_out_r;
    assign wrk_sel_narrow_in            = wrk_sel_narrow_in_r;
    assign wrk_sel_narrow_out           = wrk_sel_narrow_out_r;
    assign wrk_word_index_last          = wrk_word_index_last_r;
    assign wrk_word_index_last_half     = wrk_word_index_last_half_r;
    assign wrk_opcode                   = wrk_opcode_r;

    
    //
    // UOP_FSM
    //
    localparam [1:0] UOP_FSM_STATE_IDLE     = 2'b00;
    localparam [1:0] UOP_FSM_STATE_FETCH    = 2'b01;
    localparam [1:0] UOP_FSM_STATE_DECODE   = 2'b10;
    localparam [1:0] UOP_FSM_STATE_BUSY     = 2'b11;

    reg [1:0] uop_fsm_state = UOP_FSM_STATE_IDLE;
    reg [1:0] uop_fsm_state_next;
    
    
    //
    // UOP ROM
    //
    reg  [UOP_ADDR_W   -1:0] uop_addr;
    wire [UOP_W        -1:0] uop_data;
    
    modexpng_uop_rom uop_rom
    (
        .clk    (clk),
        .addr   (uop_addr),
        .data   (uop_data)
    );
    
    
    //
    // UOP ROM Data Decoder
    //
    wire [UOP_OPCODE_W -1:0] uop_data_opcode         = uop_data[UOP_W                                                                             -1-: UOP_OPCODE_W];
    wire [UOP_CRT_W    -1:0] uop_data_crt            = uop_data[UOP_W -UOP_OPCODE_W                                                               -1-: UOP_CRT_W   ];
    wire [UOP_NPQ_W    -1:0] uop_data_npq            = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W                                                    -1-: UOP_NPQ_W   ];
    wire [UOP_AUX_W    -1:0] uop_data_aux            = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W -UOP_NPQ_W                                         -1-: UOP_AUX_W   ];
    wire [UOP_LADDER_W -1:0] uop_data_ladder         = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W -UOP_NPQ_W -UOP_AUX_W                              -1-: UOP_LADDER_W];
    wire [BANK_ADDR_W  -1:0] uop_data_sel_wide_in    = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W -UOP_NPQ_W -UOP_AUX_W -UOP_LADDER_W                -1-: BANK_ADDR_W ];
    wire [BANK_ADDR_W  -1:0] uop_data_sel_narrow_in  = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W -UOP_NPQ_W -UOP_AUX_W -UOP_LADDER_W -1*BANK_ADDR_W -1-: BANK_ADDR_W ];
    wire [BANK_ADDR_W  -1:0] uop_data_sel_wide_out   = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W -UOP_NPQ_W -UOP_AUX_W -UOP_LADDER_W -2*BANK_ADDR_W -1-: BANK_ADDR_W ];
    wire [BANK_ADDR_W  -1:0] uop_data_sel_narrow_out = uop_data[UOP_W -UOP_OPCODE_W -UOP_CRT_W -UOP_NPQ_W -UOP_AUX_W -UOP_LADDER_W -3*BANK_ADDR_W -1-: BANK_ADDR_W ];
    
    wire uop_opcode_is_stop   =  uop_data_opcode == UOP_OPCODE_STOP                ;
    wire uop_opcode_is_in     = (uop_data_opcode == UOP_OPCODE_INPUT_TO_WIDE       ) ||
                                (uop_data_opcode == UOP_OPCODE_INPUT_TO_NARROW     ) ;
    wire uop_opcode_is_out    =  uop_data_opcode == UOP_OPCODE_OUTPUT_FROM_NARROW  ;
    wire uop_opcode_is_wrk    = (uop_data_opcode == UOP_OPCODE_COPY_CRT_Y2X        ) ||
                                (uop_data_opcode == UOP_OPCODE_COPY_LADDERS_X2Y    ) ||
                                (uop_data_opcode == UOP_OPCODE_CROSS_LADDERS_X2Y   ) ||
                                (uop_data_opcode == UOP_OPCODE_MODULAR_SUBTRACT    ) ||
                                (uop_data_opcode == UOP_OPCODE_MODULAR_REDUCE_INIT ) ||
                                (uop_data_opcode == UOP_OPCODE_PROPAGATE_CARRIES   ) ||
                                (uop_data_opcode == UOP_OPCODE_MERGE_LH            ) ||
                                (uop_data_opcode == UOP_OPCODE_REGULAR_ADD_UNEVEN  ) ;
    wire uop_opcode_is_mmm    = (uop_data_opcode == UOP_OPCODE_MODULAR_MULTIPLY    ) ||
                                (uop_data_opcode == UOP_OPCODE_MODULAR_REDUCE_PROC ) ||
                                (uop_data_opcode == UOP_OPCODE_REGULAR_MULTIPLY    ) ;
    wire uop_opcode_is_ladder = (uop_data_opcode == UOP_OPCODE_LADDER_INIT         ) ||
                                (uop_data_opcode == UOP_OPCODE_LADDER_STEP         ) ;


    //
    // Debug Signal
    //
    `ifdef MODEXPNG_ENABLE_DEBUG
    assign uop_decoded_stop = (uop_fsm_state == UOP_FSM_STATE_DECODE) && uop_opcode_is_stop;
    `endif
    
        

    //
    // UOP ROM Address Increment Logic
    //
    wire                   uop_loop_now;
    
    wire [UOP_ADDR_W -1:0] uop_addr_plus1  = uop_addr + 1'b1;
    wire [UOP_ADDR_W -1:0] uop_addr_minus1 = uop_addr - 1'b1;
    
    wire [UOP_ADDR_W -1:0] uop_addr_next   = uop_loop_now ? uop_addr_minus1           : uop_addr_plus1             ;
    wire [UOP_ADDR_W -1:0] uop_addr_offset = crt_mode     ? UOP_ADDR_OFFSET_USING_CRT : UOP_ADDR_OFFSET_WITHOUT_CRT;

    always @(posedge clk)
        //
        if (uop_fsm_state_next == UOP_FSM_STATE_FETCH)
            uop_addr <= (uop_fsm_state == UOP_FSM_STATE_IDLE) ? uop_addr_offset : uop_addr_next;


    //
    // Handy Signals 
    //
    wire mmm_ena = mmm_ena_x & mmm_ena_y;
    wire mmm_rdy = mmm_rdy_x & mmm_rdy_y;
    
    assign uop_loop_now = (uop_data_opcode == UOP_OPCODE_LADDER_STEP) && !io_mgr_ladder_done;
    
    reg [1:0] uop_data_ladder_mux; 
    
    always @(uop_data_ladder, io_mgr_ladder_p, io_mgr_ladder_q, io_mgr_ladder_d)
        //
        case (uop_data_ladder)
            UOP_LADDER_00: uop_data_ladder_mux = 2'b00;
            UOP_LADDER_11: uop_data_ladder_mux = 2'b11;
            UOP_LADDER_D:  uop_data_ladder_mux = {~io_mgr_ladder_d, io_mgr_ladder_d};
            UOP_LADDER_PQ: uop_data_ladder_mux = { io_mgr_ladder_p, io_mgr_ladder_q};
        endcase

    reg [OP_ADDR_W-1:0] word_index_last_mux;
    
    always @(uop_data_npq, word_index_last_n, word_index_last_pq)
        //
        if (uop_data_npq == UOP_NPQ_N) word_index_last_mux = word_index_last_n;
        else                           word_index_last_mux = word_index_last_pq;
    
    reg [BIT_INDEX_W-1:0] bit_index_last_mux;
    
    always @(crt_mode, bit_index_last_pq, bit_index_last_n)
        if (crt_mode) bit_index_last_mux = bit_index_last_pq;
        else          bit_index_last_mux = bit_index_last_n;


    //
    // UOP Trigger Logic
    //
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) begin
            io_mgr_ena_r <= 1'b0;
            mmm_ena_x_r  <= 1'b0;
            mmm_ena_y_r  <= 1'b0;
            wrk_ena_r    <= 1'b0;
        end else begin
            io_mgr_ena_r <= uop_fsm_state == UOP_FSM_STATE_DECODE ? (uop_opcode_is_in    ||
                                                                     uop_opcode_is_out   ||
                                                                     uop_opcode_is_ladder): 1'b0;
            mmm_ena_x_r  <= uop_fsm_state == UOP_FSM_STATE_DECODE ?  uop_opcode_is_mmm    : 1'b0;
            mmm_ena_y_r  <= uop_fsm_state == UOP_FSM_STATE_DECODE ?  uop_opcode_is_mmm    : 1'b0;
            wrk_ena_r    <= uop_fsm_state == UOP_FSM_STATE_DECODE ? (uop_opcode_is_wrk   ||
                                                                     uop_opcode_is_out   ): 1'b0;
        end


    //
    // UOP Completion Detector 
    //
    reg uop_exit_from_busy;

    always @* begin
        //
        uop_exit_from_busy = 0;
        //
        if (uop_opcode_is_in    ) uop_exit_from_busy =  ~io_mgr_ena & io_mgr_rdy  ;
        if (uop_opcode_is_out   ) uop_exit_from_busy = (~io_mgr_ena & io_mgr_rdy) &
                                                       (~wrk_ena    & wrk_rdy   ) ;
        if (uop_opcode_is_mmm   ) uop_exit_from_busy =  ~mmm_ena    & mmm_rdy     ;
        if (uop_opcode_is_wrk   ) uop_exit_from_busy =  ~wrk_ena    & wrk_rdy     ;
        if (uop_opcode_is_ladder) uop_exit_from_busy =  ~io_mgr_ena & io_mgr_rdy  ;
        //
    end


    //
    // UOP Parameters
    //
    task update_io_mgr_params;
        input [UOP_CRT_W   -1:0] sel_crt;
        input [UOP_AUX_W   -1:0] sel_aux;
        input [BANK_ADDR_W -1:0] sel_in;
        input [BANK_ADDR_W -1:0] sel_out;
        input [UOP_OPCODE_W-1:0] opcode;
        begin
            io_mgr_sel_crt_r      <= sel_crt;
            io_mgr_sel_aux_r      <= sel_aux;
            io_mgr_sel_in_r       <= sel_in;
            io_mgr_sel_out_r      <= sel_out;
            io_mgr_opcode_r       <= opcode;
        end
    endtask
    
    task update_wrk_params;
        input [BANK_ADDR_W -1:0] wide_in;
        input [BANK_ADDR_W -1:0] narrow_in;
        input [BANK_ADDR_W -1:0] wide_out;
        input [BANK_ADDR_W -1:0] narrow_out;
        input [UOP_OPCODE_W-1:0] opcode;
        begin
            wrk_sel_wide_in_r    <= wide_in;
            wrk_sel_narrow_in_r  <= narrow_in;
            wrk_sel_wide_out_r   <= wide_out;
            wrk_sel_narrow_out_r <= narrow_out;
            wrk_opcode_r         <= opcode;
        end
    endtask
    
    task update_mmm_params;
        input [            1:0] ladder_mode;
        input [BANK_ADDR_W-1:0] sel_wide_in;
        input [BANK_ADDR_W-1:0] sel_narrow_in;
        input                   force_unity_b;
        input                   only_reduce;
        input                   just_multiply;
        begin
            {mmm_ladder_mode_x_r,   mmm_ladder_mode_y_r  } <=    ladder_mode;
            {mmm_sel_wide_in_x_r,   mmm_sel_wide_in_y_r  } <= {2{sel_wide_in  }};
            {mmm_sel_narrow_in_x_r, mmm_sel_narrow_in_y_r} <= {2{sel_narrow_in}};
            {mmm_force_unity_b_x_r, mmm_force_unity_b_y_r} <= {2{force_unity_b}};
            {mmm_only_reduce_x_r,   mmm_only_reduce_y_r  } <= {2{only_reduce  }};            
            {mmm_just_multiply_x_r, mmm_just_multiply_y_r} <= {2{just_multiply}};
        end
    endtask
        
    task update_rdct_params;
        input [BANK_ADDR_W-1:0] sel_wide_out;
        input [BANK_ADDR_W-1:0] sel_narrow_out;
        begin
            {rdct_sel_wide_out_x_r,   rdct_sel_wide_out_y_r  } <= {2{sel_wide_out}};
            {rdct_sel_narrow_out_x_r, rdct_sel_narrow_out_y_r} <= {2{sel_narrow_out}};
        end
    endtask
        
    always @(posedge clk)
        //
        if (uop_fsm_state == UOP_FSM_STATE_DECODE)
            //
            case (uop_data_opcode)
                //
                UOP_OPCODE_INPUT_TO_WIDE:
                    update_io_mgr_params(uop_data_crt, uop_data_aux, uop_data_sel_narrow_in, uop_data_sel_wide_out, uop_data_opcode);
                //
                UOP_OPCODE_INPUT_TO_NARROW:
                    update_io_mgr_params(uop_data_crt, uop_data_aux, uop_data_sel_narrow_in, uop_data_sel_narrow_out, uop_data_opcode);
                //
                UOP_OPCODE_OUTPUT_FROM_NARROW: begin
                    update_io_mgr_params(uop_data_crt, uop_data_aux, BANK_DNC, uop_data_sel_narrow_out, uop_data_opcode);
                    update_wrk_params(BANK_DNC, uop_data_sel_narrow_in, BANK_DNC, BANK_DNC, uop_data_opcode);                
                end
                //
                UOP_OPCODE_COPY_CRT_Y2X,
                UOP_OPCODE_COPY_LADDERS_X2Y,
                UOP_OPCODE_CROSS_LADDERS_X2Y:
                    update_wrk_params(uop_data_sel_wide_in, uop_data_sel_narrow_in, uop_data_sel_wide_out, uop_data_sel_narrow_out, uop_data_opcode);
                //
                UOP_OPCODE_MODULAR_MULTIPLY: begin
                    update_mmm_params(uop_data_ladder_mux, uop_data_sel_wide_in, uop_data_sel_narrow_in, uop_data_aux, 1'b0, 1'b0);
                    update_rdct_params(uop_data_sel_wide_out, uop_data_sel_narrow_out);
                end
                //
                UOP_OPCODE_MODULAR_SUBTRACT:
                    update_wrk_params(BANK_DNC, uop_data_sel_narrow_in, uop_data_sel_wide_out, uop_data_sel_narrow_out, uop_data_opcode);
                //
                UOP_OPCODE_MODULAR_REDUCE_INIT:
                    update_wrk_params(BANK_DNC, uop_data_sel_narrow_in, BANK_DNC, BANK_DNC, uop_data_opcode);
                //
                UOP_OPCODE_MODULAR_REDUCE_PROC: begin
                    update_mmm_params(2'bXX, BANK_DNC, BANK_DNC, 1'b0, 1'b1, 1'b0);
                    update_rdct_params(uop_data_sel_wide_out, uop_data_sel_narrow_out);                                    
                end
                //
                UOP_OPCODE_PROPAGATE_CARRIES:
                    update_wrk_params(BANK_DNC, uop_data_sel_narrow_in, BANK_DNC, uop_data_sel_narrow_out, uop_data_opcode);
                //
                UOP_OPCODE_MERGE_LH:
                    update_wrk_params(BANK_DNC, BANK_DNC, BANK_DNC, uop_data_sel_narrow_out, uop_data_opcode);
                //
                UOP_OPCODE_REGULAR_MULTIPLY: begin
                    update_mmm_params(2'b11, uop_data_sel_wide_in, uop_data_sel_narrow_in, 1'b0, 1'b0, 1'b1);
                    update_rdct_params(uop_data_sel_wide_out, uop_data_sel_narrow_out);
                end
                //
                UOP_OPCODE_REGULAR_ADD_UNEVEN:
                    update_wrk_params(uop_data_sel_wide_in, uop_data_sel_narrow_in, BANK_DNC, uop_data_sel_narrow_out, uop_data_opcode);
                //
                UOP_OPCODE_LADDER_INIT,
                UOP_OPCODE_LADDER_STEP:
                    update_io_mgr_params(UOP_CRT_DNC, UOP_AUX_DNC, BANK_DNC, BANK_DNC, uop_data_opcode);
                //
            endcase
     
            
    //
    // UOP Lengths
    //
    task update_io_mgr_length;
        input [OP_ADDR_W  -1:0] word_index_last;
        input [BIT_INDEX_W-1:0] ladder_steps;
        begin
            io_mgr_word_index_last_r <= word_index_last;
            io_mgr_ladder_steps_r    <= ladder_steps;
        end
    endtask
    
    task update_wrk_length;
        input [OP_ADDR_W-1:0] word_index_last;
        input [OP_ADDR_W-1:0] word_index_last_half;
        begin
            wrk_word_index_last_r      <= word_index_last;
            wrk_word_index_last_half_r <= word_index_last_half;
        end
    endtask
    
    task update_mmm_length;
        input [OP_ADDR_W-1:0] word_index_last;
        begin
            mmm_word_index_last_x_r        <= word_index_last;
            mmm_word_index_last_y_r        <= word_index_last;
            mmm_word_index_last_minus1_x_r <= word_index_last - 1'b1;
            mmm_word_index_last_minus1_y_r <= word_index_last - 1'b1;
        end
    endtask
    
    task update_rdct_length;
        input [OP_ADDR_W-1:0] word_index_last;
        begin
            rdct_word_index_last_x_r <= word_index_last;
            rdct_word_index_last_y_r <= word_index_last;
        end
    endtask
    
    always @(posedge clk)
        //
        if (uop_fsm_state == UOP_FSM_STATE_DECODE)
            //
            case (uop_data_opcode)
                //
                UOP_OPCODE_INPUT_TO_WIDE,    
                UOP_OPCODE_INPUT_TO_NARROW,
                UOP_OPCODE_OUTPUT_FROM_NARROW:
                    update_io_mgr_length(word_index_last_mux, BIT_INDEX_DNC);
                //
                UOP_OPCODE_COPY_CRT_Y2X,
                UOP_OPCODE_COPY_LADDERS_X2Y,
                UOP_OPCODE_CROSS_LADDERS_X2Y:
                    update_wrk_length(word_index_last_mux, OP_ADDR_DNC);
                //
                UOP_OPCODE_MODULAR_MULTIPLY: begin
                    update_mmm_length(word_index_last_mux);
                    update_rdct_length(word_index_last_mux);
                end
                //            
                UOP_OPCODE_MODULAR_SUBTRACT:
                    update_wrk_length(word_index_last_mux, OP_ADDR_DNC);
                //
                UOP_OPCODE_MODULAR_REDUCE_INIT:
                    update_wrk_length(word_index_last_n, word_index_last_pq);
                //
                UOP_OPCODE_MODULAR_REDUCE_PROC: begin
                    update_mmm_length(word_index_last_pq);
                    update_rdct_length(word_index_last_pq);
                end
                //
                UOP_OPCODE_PROPAGATE_CARRIES:
                    update_wrk_length(word_index_last_mux, OP_ADDR_DNC);
                //
                UOP_OPCODE_MERGE_LH:
                    update_wrk_length(word_index_last_n, word_index_last_pq);
                //
                UOP_OPCODE_REGULAR_MULTIPLY: begin
                    update_mmm_length(word_index_last_pq);
                    update_rdct_length(word_index_last_pq);
                end
                //
                UOP_OPCODE_REGULAR_ADD_UNEVEN:
                    update_wrk_length(word_index_last_n, word_index_last_pq);
                //
                UOP_OPCODE_LADDER_INIT,
                UOP_OPCODE_LADDER_STEP:
                    update_io_mgr_length(OP_ADDR_LADDER_LAST, bit_index_last_mux);
                //
            endcase
            
    //
    // UOP FSM Process
    //
    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n) uop_fsm_state <= UOP_FSM_STATE_IDLE;
        else        uop_fsm_state <= uop_fsm_state_next;
            
    
    //
    // UOP FSM Transition Logic
    //
    always @* begin
        //
        case (uop_fsm_state)
            UOP_FSM_STATE_IDLE:   uop_fsm_state_next = ena                ? UOP_FSM_STATE_FETCH  : UOP_FSM_STATE_IDLE;
            UOP_FSM_STATE_FETCH:  uop_fsm_state_next =                      UOP_FSM_STATE_DECODE ;
            UOP_FSM_STATE_DECODE: uop_fsm_state_next = uop_opcode_is_stop ? UOP_FSM_STATE_IDLE   : UOP_FSM_STATE_BUSY;
            UOP_FSM_STATE_BUSY:   uop_fsm_state_next = uop_exit_from_busy ? UOP_FSM_STATE_FETCH  : UOP_FSM_STATE_BUSY;
        endcase
        //
    end


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

    always @(posedge clk or negedge rst_n)
        //
        if (!rst_n)               rdy_r <= 1'b1;
        else case (uop_fsm_state)
            UOP_FSM_STATE_IDLE:   rdy_r <= ~ena;
            UOP_FSM_STATE_DECODE: rdy_r <= uop_opcode_is_stop;
        endcase


endmodule