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
//! Reservation station.

use super::*;
use crate::gemmini::isa::*;
use crate::gemmini::local_addr::*;
use crate::hpanic;

const BLOCK_ROWS: usize = TILE_ROWS * MESH_ROWS;
const BLOCK_COLS: usize = TILE_COLS * MESH_COLS;

const CL_BLOCK_COLS: usize = clog2(BLOCK_COLS);

// Every cycle, at most two instructions of a single "type" (ld/st/ex) can be completed: one through the `completed` port, and the other if it is a "complete-on-issue" instruction.
const MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE: usize = 2;

/// Reservation station issue type.
#[derive(Debug, Clone, Copy)]
pub struct RsIssue {
    /// Command.
    pub cmd: GemminiCmd,
    /// Rob ID.
    pub rob_id: U<{ clog2(RS_ENTRIES) }>,
}

/// Interfaces issued from reservation stations.
#[derive(Debug, Interface)]
pub struct RsIssues {
    /// Issue from LDQ.
    pub ld: Vr<RsIssue>,
    /// Issue from EXQ.
    pub ex: Vr<RsIssue>,
    /// Issue from STQ.
    pub st: Vr<RsIssue>,
}

/// Number of completed instructions. It will be send to `LoopConv` and `LoopMatmul` modules.
#[derive(Debug, Interface)]
pub struct RsCompleted {
    /// Number of completed LD instructions to be sent to `LoopConv`.
    pub conv_ld: Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    /// Number of completed EX instructions to be sent to `LoopConv`.
    pub conv_ex: Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    /// Number of completed ST instructions to be sent to `LoopConv`.
    pub conv_st: Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,

    /// Number of completed LD instructions to be sent to `LoopMatmul`.
    pub matmul_ld: Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    /// Number of completed EX instructions to be sent to `LoopMatmul`.
    pub matmul_ex: Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    /// Number of completed ST instructions to be sent to `LoopMatmul`.
    pub matmul_st: Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
}

/// Queue type.
#[derive(Debug, Clone, Copy)]
pub enum Q {
    /// Load queue.
    Ld,
    /// Execute queue.
    Ex,
    /// Store queue.
    St,
}

/// Represents the SRAM addresses accessed by the operation.
#[derive(Debug, Clone, Copy)]
struct Op {
    /// Start address.
    start: LocalAddr,

    /// End address.
    end: LocalAddr,

    /// Indicates whether an address overflow occurs from `start` to `end`.
    ///
    /// - If this is `true`, the accessed SRAM addresses are `[start, MAX_ADDR)` and `[0, end)`.
    /// - If this is `false`, the accessed SRAM addresses are `[start, end)`.
    wraps_around: bool,
}

impl Op {
    /// Checks whether an SRAM address overlap occurred.
    fn overlaps(self, other: Op) -> bool {
        // TODO: `is_garbage` check might not really be necessary.
        ((other.start.le(self.start) && (self.start.lt(other.end) || other.wraps_around))
            || (self.start.le(other.start) && (other.start.lt(self.end) || self.wraps_around)))
            && !(self.start.is_garbage() || other.start.is_garbage())
    }
}

/// Represents dependencies between entries in the queue.
#[derive(Debug, Default, Clone, Copy)]
pub struct Deps {
    /// Dependencies between entries in LD queue.
    pub ld: Array<bool, RS_ENTRIES_LD>,
    /// Dependencies between entries in EX queue.
    pub ex: Array<bool, RS_ENTRIES_EX>,
    /// Dependencies between entries in ST queue.
    pub st: Array<bool, RS_ENTRIES_ST>,
}

impl Deps {
    /// Represents the dependencies are resolved or not.
    pub fn resolved(self) -> bool {
        self.ld == 0.into_u() && self.ex == 0.into_u() && self.st == 0.into_u()
    }

    /// Sets `idx`-th element of LD dependency to `elt`.
    pub fn set_ld(self, idx: U<{ clog2(RS_ENTRIES_LD) }>, elt: bool) -> Self {
        Self { ld: self.ld.set(idx, elt), ..self }
    }

    /// Sets `idx`-th element of EX dependency to `elt`.
    pub fn set_ex(self, idx: U<{ clog2(RS_ENTRIES_EX) }>, elt: bool) -> Self {
        Self { ex: self.ex.set(idx, elt), ..self }
    }

    /// Sets `idx`-th element of ST dependency to `elt`.
    pub fn set_st(self, idx: U<{ clog2(RS_ENTRIES_ST) }>, elt: bool) -> Self {
        Self { st: self.st.set(idx, elt), ..self }
    }
}

/// Entry in the queue.
#[derive(Debug, Clone, Copy)]
pub struct Entry {
    /// Queue type.
    pub q: Q,

    /// Indicates whether the command modifies the config.
    is_config: bool,

    /// SRAM addresses accessed by operand A.
    opa: HOption<Op>,
    /// Indicates whether operand A is the destination operand.
    opa_is_dst: bool,
    /// SRAM addresses accessed by operand B.
    opb: HOption<Op>,

    /// Entry is issued or not.
    pub issued: bool,

    /// Indicates that this command is completed immediately when issued.
    complete_on_issue: bool,

    /// Command.
    cmd: GemminiCmd,

    /// Dependencies between entries in the queue.
    pub deps: Deps,
}

/// Returns the decoded command.
///
/// It returns the entry and whether it is norm or not.
fn decode_cmd(cmd: GemminiCmd, config: Config) -> (Entry, bool) {
    let funct = cmd.cmd.inst.funct;
    let funct_is_compute = matches!(funct, Funct::ComputeAndStayCmd | Funct::ComputeAndFlipCmd);
    let config_cmd_type = ConfigCmd::from(cmd.cmd.rs1.clip_const::<2>(0));

    let q = if matches!(funct, Funct::LoadCmd | Funct::Load2Cmd | Funct::Load3Cmd)
        || matches!((funct, config_cmd_type), (Funct::ConfigCmd, ConfigCmd::Load))
    {
        Q::Ld
    } else if matches!(funct, Funct::PreloadCmd)
        || funct_is_compute
        || matches!((funct, config_cmd_type), (Funct::ConfigCmd, ConfigCmd::Ex))
    {
        Q::Ex
    } else if matches!(funct, Funct::StoreCmd)
        || (matches!(funct, Funct::ConfigCmd) && matches!(config_cmd_type, ConfigCmd::Store | ConfigCmd::Norm))
    {
        Q::St
    } else {
        hpanic!("This funct should not come here")
    };

    // Normalization commands are a subset of store commands, so they still go in the ST queue.
    let is_norm = matches!((funct, config_cmd_type), (Funct::ConfigCmd, ConfigCmd::Norm));
    let is_config = matches!(funct, Funct::ConfigCmd);

    let op1_start = LocalAddr::from(cmd.cmd.rs1);
    let op1 = if matches!(funct, Funct::PreloadCmd) {
        // TODO: check `b_transpose` here if WS mode is enabled.
        let preload_rows = cmd.cmd.rs1.clip_const::<{ clog2(BLOCK_ROWS + 1) }>(48);
        let (end, wraps_around) = op1_start.add_with_overflow(preload_rows.resize());

        Some(Op { start: op1_start, end, wraps_around })
    } else if funct_is_compute {
        let rows = cmd.cmd.rs1.clip_const::<{ clog2(BLOCK_ROWS + 1) }>(48);
        let cols = cmd.cmd.rs1.clip_const::<{ clog2(BLOCK_COLS + 1) }>(32);
        let compute_rows = if config.a_transpose { cols } else { rows } * config.a_stride;
        let (end, wraps_around) = op1_start.add_with_overflow(compute_rows.resize());

        Some(Op { start: op1_start, end, wraps_around })
    } else {
        None
    };

    let op2_start = LocalAddr::from(cmd.cmd.rs2);
    let op2 = if funct_is_compute {
        let compute_rows = cmd.cmd.rs2.clip_const::<{ clog2(BLOCK_ROWS + 1) }>(48);
        let (end, wraps_around) = op2_start.add_with_overflow(compute_rows.resize());

        Some(Op { start: op2_start, end, wraps_around })
    } else if config.pooling_is_enabled && (funct_is_compute || matches!(funct, Funct::StoreCmd)) {
        // If pooling is enabled, then we assume that this command simply mvouts everything in this accumulator bank from
        // start to the end of the bank. TODO: This won't work when `ACC_BANKS != 2`.
        let acc_bank = op2_start.acc_bank();
        let next_bank_addr = LocalAddr {
            is_acc_addr: true,
            data: (acc_bank + 1.into_u()).resize() << ACC_BANK_ROW_BITS,
            ..LocalAddr::from(0.into_u())
        };

        Some(Op { start: op2_start, end: next_bank_addr, wraps_around: next_bank_addr.acc_bank() == 0.into_u() })
    } else if matches!(funct, Funct::StoreCmd) {
        let mvout_cols = cmd.cmd.rs2.clip_const::<{ clog2(MVOUT_COLS_BITS) }>(32);
        let mvout_rows = cmd.cmd.rs2.clip_const::<{ clog2(MVOUT_ROWS_BITS) }>(48);

        let mvout_mats = (mvout_cols >> CL_BLOCK_COLS)
            + if (mvout_cols & BLOCK_COLS.into_u()) != 0.into_u() { 1.into_u() } else { 0.into_u() };
        let total_mvout_rows = ((mvout_mats - 1.into_u()) * BLOCK_COLS.into_u::<5>()) + mvout_rows.resize();
        let (end, wraps_around) = op2_start.add_with_overflow(total_mvout_rows.resize());

        Some(Op { start: op2_start, end, wraps_around: config.pooling_is_enabled || wraps_around })
    } else {
        None
    };

    let dst_start = LocalAddr::from(cmd.cmd.rs2.clip_const::<32>(0).resize());
    let dst = if matches!(funct, Funct::PreloadCmd) {
        let preload_rows = cmd.cmd.rs2.clip_const::<{ clog2(BLOCK_ROWS + 1) }>(48) * config.c_stride;
        let (end, wraps_around) = dst_start.add_with_overflow(preload_rows.resize());

        Some(Op { start: dst_start, end, wraps_around })
    } else if matches!(funct, Funct::LoadCmd | Funct::Load2Cmd | Funct::Load3Cmd) {
        let id: U<{ clog2(LOAD_STATES) }> = if matches!(funct, Funct::Load2Cmd) {
            1.into_u()
        } else if matches!(funct, Funct::Load3Cmd) {
            2.into_u()
        } else {
            0.into_u()
        };
        let block_stride = config.ld_block_strides[id];
        let pixel_repeats = config.ld_pixel_repeats[id];

        let mvin_cols = cmd.cmd.rs2.clip_const::<MVIN_COLS_BITS>(32);
        let mvin_rows = cmd.cmd.rs2.clip_const::<MVIN_ROWS_BITS>(48);

        let mvin_mats =
            (mvin_cols >> CL_BLOCK_COLS) + (mvin_cols.clip_const::<CL_BLOCK_COLS>(0) != 0.into_u()).into_u();
        let total_mvin_rows = (mvin_mats - 1.into_u()) * block_stride + mvin_rows.resize();

        let start = if dst_start.is_acc_addr {
            dst_start
        } else if dst_start.full_sp_addr() > (SP_ROWS / 2).into_u() {
            dst_start.floor_sub(pixel_repeats.resize(), (SP_ROWS / 2).into_u()).0
        } else {
            dst_start.floor_sub(pixel_repeats.resize(), 0.into_u()).0
        };

        let (end, wraps_around) = start.add_with_overflow(total_mvin_rows.resize());

        Some(Op { start, end, wraps_around })
    } else {
        None
    };

    let new_entry = Entry {
        q,
        is_config,
        opa: if dst.is_some() { dst } else { op1.or(op2) },
        opa_is_dst: dst.is_some(),
        opb: if dst.is_some() { op1.or(op2) } else { op2 },
        issued: false,
        complete_on_issue: is_config && !matches!(q, Q::Ex),
        cmd,
        deps: Deps::default(),
    };

    (new_entry, is_norm)
}

/// Updates the config.
fn update_config(new_entry: Entry, is_norm: bool, config: Config) -> Config {
    // If command is not config, return early.
    if !new_entry.is_config {
        return config;
    }

    match new_entry.q {
        Q::Ex => {
            let set_only_strides = new_entry.cmd.cmd.rs1[7];

            Config {
                a_stride: new_entry.cmd.cmd.rs1.clip_const::<16>(16).resize(),
                c_stride: new_entry.cmd.cmd.rs2.clip_const::<16>(48).resize(),
                a_transpose: if !set_only_strides { new_entry.cmd.cmd.rs1[8] } else { config.a_transpose },
                ..config
            }
        }
        Q::Ld => {
            let id = new_entry.cmd.cmd.rs1.clip_const::<2>(3);
            let block_stride = new_entry.cmd.cmd.rs1.clip_const::<16>(16);
            let repeat_pixels = new_entry.cmd.cmd.rs1.clip_const::<PIXEL_REPEATS_BITS>(8);
            let repeat_pixels = if repeat_pixels < 1.into_u() { 1.into_u() } else { repeat_pixels };

            Config {
                ld_block_strides: config.ld_block_strides.set(id, block_stride.resize()),
                ld_pixel_repeats: config.ld_pixel_repeats.set(id, repeat_pixels - 1.into_u()),
                ..config
            }
        }
        Q::St => {
            if is_norm {
                config
            } else {
                let pool_stride = new_entry.cmd.cmd.rs1.clip_const::<2>(4);
                Config { pooling_is_enabled: pool_stride != 0.into_u(), ..config }
            }
        }
    }
}

/// Represents the reservation station entries.
#[derive(Debug, Default, Clone, Copy)]
struct Entries {
    /// LD entries.
    entries_ld: Array<HOption<Entry>, RS_ENTRIES_LD>,
    /// EX entries.
    entries_ex: Array<HOption<Entry>, RS_ENTRIES_EX>,
    /// ST entries.
    entries_st: Array<HOption<Entry>, RS_ENTRIES_ST>,
}

impl Entries {
    /// Computes dependencies between entries in the queue.
    fn get_deps(self, entry: Entry) -> Deps {
        let not_config = !entry.is_config;
        let entry_opa = entry.opa.unwrap();
        let entry_opb = entry.opb.unwrap();

        let ld = self.entries_ld.map(|e| {
            e.is_some_and(|e| match entry.q {
                Q::Ld => !e.issued,
                Q::Ex => not_config && e.opa.is_some_and(|opa| entry_opa.overlaps(opa) || entry_opb.overlaps(opa)),
                Q::St => not_config && e.opa.is_some_and(|opa| entry_opa.overlaps(opa)),
            })
        });

        let ex = self.entries_ex.map(|e| {
            e.is_some_and(|e| match entry.q {
                Q::Ld => {
                    not_config
                        && (e.opa.is_some_and(|opa| entry_opa.overlaps(opa))
                            || e.opb.is_some_and(|opb| entry_opa.overlaps(opb)))
                }
                Q::Ex => !e.issued,
                Q::St => not_config && e.opa_is_dst && e.opa.is_some_and(|opa| entry_opa.overlaps(opa)),
            })
        });

        let st = self.entries_st.map(|e| {
            e.is_some_and(|e| match entry.q {
                Q::Ld => not_config && e.opa.is_some_and(|opa| entry_opa.overlaps(opa)),
                Q::Ex => not_config && entry.opa_is_dst && e.opa.is_some_and(|opa| entry_opa.overlaps(opa)),
                Q::St => !e.issued,
            })
        });

        Deps { ld, ex, st }
    }

    /// Tries to allocates a new entry.
    ///
    /// It returns whether the allocation succeeded or not, and updates the entries.
    fn try_alloc(self, entry: Entry) -> (bool, Self) {
        let alloc_id_ld = self.entries_ld.find_idx(|e| e.is_none());
        let alloc_id_ex = self.entries_ex.find_idx(|e| e.is_none());
        let alloc_id_st = self.entries_st.find_idx(|e| e.is_none());

        let is_allocated_ld = matches!(entry.q, Q::Ld) && alloc_id_ld.is_some();
        let is_allocated_ex = matches!(entry.q, Q::Ex) && alloc_id_ex.is_some();
        let is_allocated_st = matches!(entry.q, Q::St) && alloc_id_st.is_some();

        let is_allocated = is_allocated_ld || is_allocated_ex || is_allocated_st;

        let entries_ld_next = self.entries_ld.set_cond(is_allocated_ld, alloc_id_ld.unwrap(), Some(entry));
        let entries_ex_next = self.entries_ex.set_cond(is_allocated_ex, alloc_id_ex.unwrap(), Some(entry));
        let entries_st_next = self.entries_st.set_cond(is_allocated_st, alloc_id_st.unwrap(), Some(entry));

        let entries_next =
            Entries { entries_ld: entries_ld_next, entries_ex: entries_ex_next, entries_st: entries_st_next };

        (is_allocated, entries_next)
    }

    /// Tries to issue entry.
    ///
    /// It returns the command which has `q` type and ready to be issued, and updates the entries.
    fn try_issue(self, q: Q) -> (HOption<(RsIssue, bool)>, Self) {
        let issued_id = match q {
            Q::Ld => self
                .entries_ld
                .find_idx(|e| e.is_some_and(|e| e.deps.resolved() && !e.issued))
                .map(|id| id.resize::<{ clog2(RS_MAX_PER_TYPE) }>()),
            Q::Ex => self
                .entries_ex
                .find_idx(|e| e.is_some_and(|e| e.deps.resolved() && !e.issued))
                .map(|id| id.resize::<{ clog2(RS_MAX_PER_TYPE) }>()),
            Q::St => self
                .entries_st
                .find_idx(|e| e.is_some_and(|e| e.deps.resolved() && !e.issued))
                .map(|id| id.resize::<{ clog2(RS_MAX_PER_TYPE) }>()),
        };

        let issued_entry = issued_id.map(|id| {
            match q {
                Q::Ld => self.entries_ld[id],
                Q::Ex => self.entries_ex[id],
                Q::St => self.entries_st[id],
            }
            .unwrap() // `unwrap()` always success because of the logic of finding `issue_id`.
        });

        let issued = issued_id.zip(issued_entry).map(|(id, entry)| {
            let global_issue_id = id.append((q as usize).into_u::<2>());
            (RsIssue { cmd: entry.cmd, rob_id: global_issue_id }, entry.complete_on_issue)
        });

        let entries_next = if let Some((id, entry)) = issued_id.zip(issued_entry) {
            let entries_ld_next = self.entries_ld.set_cond(
                matches!(q, Q::Ld),
                id.resize::<{ clog2(RS_ENTRIES_LD) }>(),
                if entry.complete_on_issue { None } else { Some(Entry { issued: true, ..entry }) },
            );

            let entries_ex_next = self.entries_ex.set_cond(
                matches!(q, Q::Ex),
                id.resize::<{ clog2(RS_ENTRIES_EX) }>(),
                if entry.complete_on_issue { None } else { Some(Entry { issued: true, ..entry }) },
            );

            let entries_st_next = self.entries_st.set_cond(
                matches!(q, Q::St),
                id.resize::<{ clog2(RS_ENTRIES_ST) }>(),
                if entry.complete_on_issue { None } else { Some(Entry { issued: true, ..entry }) },
            );

            let entries_ld_next = entries_ld_next.map(|e| {
                e.map(|e| match q {
                    Q::Ld => Entry { deps: e.deps.set_ld(id.resize::<{ clog2(RS_ENTRIES_LD) }>(), false), ..e },
                    Q::Ex => {
                        if entry.complete_on_issue {
                            Entry { deps: e.deps.set_ex(id.resize::<{ clog2(RS_ENTRIES_EX) }>(), false), ..e }
                        } else {
                            e
                        }
                    }
                    Q::St => {
                        if entry.complete_on_issue {
                            Entry { deps: e.deps.set_st(id.resize::<{ clog2(RS_ENTRIES_ST) }>(), false), ..e }
                        } else {
                            e
                        }
                    }
                })
            });

            let entries_ex_next = entries_ex_next.map(|e| {
                e.map(|e| match q {
                    Q::Ld => {
                        if entry.complete_on_issue {
                            Entry { deps: e.deps.set_ld(id.resize::<{ clog2(RS_ENTRIES_LD) }>(), false), ..e }
                        } else {
                            e
                        }
                    }
                    Q::Ex => Entry { deps: e.deps.set_ex(id.resize::<{ clog2(RS_ENTRIES_EX) }>(), false), ..e },
                    Q::St => {
                        if entry.complete_on_issue {
                            Entry { deps: e.deps.set_st(id.resize::<{ clog2(RS_ENTRIES_ST) }>(), false), ..e }
                        } else {
                            e
                        }
                    }
                })
            });

            let entries_st_next = entries_st_next.map(|e| {
                e.map(|e| match q {
                    Q::Ld => {
                        if entry.complete_on_issue {
                            Entry { deps: e.deps.set_ld(id.resize::<{ clog2(RS_ENTRIES_LD) }>(), false), ..e }
                        } else {
                            e
                        }
                    }
                    Q::Ex => {
                        if entry.complete_on_issue {
                            Entry { deps: e.deps.set_ex(id.resize::<{ clog2(RS_ENTRIES_EX) }>(), false), ..e }
                        } else {
                            e
                        }
                    }
                    Q::St => Entry { deps: e.deps.set_st(id.resize::<{ clog2(RS_ENTRIES_ST) }>(), false), ..e },
                })
            });

            Entries { entries_ld: entries_ld_next, entries_ex: entries_ex_next, entries_st: entries_st_next }
        } else {
            self
        };

        (issued, entries_next)
    }

    /// Returns completed entry and updates the entries.
    fn compute_completed(self, q: Q, id: U<{ clog2(RS_MAX_PER_TYPE) }>) -> (Entry, Self) {
        let completed_entry = match q {
            Q::Ld => self.entries_ld[id.resize::<{ clog2(RS_ENTRIES_LD) }>()],
            Q::Ex => self.entries_ex[id.resize::<{ clog2(RS_ENTRIES_EX) }>()],
            Q::St => self.entries_st[id.resize::<{ clog2(RS_ENTRIES_ST) }>()],
        }
        .unwrap(); // TODO: Why this `unwrap()` always success?

        let entries_ld_next =
            self.entries_ld.set_cond(matches!(q, Q::Ld), id.resize::<{ clog2(RS_ENTRIES_LD) }>(), None).map(|e| {
                e.map(|e| Entry {
                    deps: match q {
                        Q::Ld => e.deps.set_ld(id.resize::<{ clog2(RS_ENTRIES_LD) }>(), false),
                        Q::Ex => e.deps.set_ex(id.resize::<{ clog2(RS_ENTRIES_EX) }>(), false),
                        Q::St => e.deps.set_st(id.resize::<{ clog2(RS_ENTRIES_ST) }>(), false),
                    },
                    ..e
                })
            });

        let entries_ex_next =
            self.entries_ex.set_cond(matches!(q, Q::Ex), id.resize::<{ clog2(RS_ENTRIES_EX) }>(), None).map(|e| {
                e.map(|e| Entry {
                    deps: match q {
                        Q::Ld => e.deps.set_ld(id.resize::<{ clog2(RS_ENTRIES_LD) }>(), false),
                        Q::Ex => e.deps.set_ex(id.resize::<{ clog2(RS_ENTRIES_EX) }>(), false),
                        Q::St => e.deps.set_st(id.resize::<{ clog2(RS_ENTRIES_ST) }>(), false),
                    },
                    ..e
                })
            });

        let entries_st_next =
            self.entries_st.set_cond(matches!(q, Q::St), id.resize::<{ clog2(RS_ENTRIES_ST) }>(), None).map(|e| {
                e.map(|e| Entry {
                    deps: match q {
                        Q::Ld => e.deps.set_ld(id.resize::<{ clog2(RS_ENTRIES_LD) }>(), false),
                        Q::Ex => e.deps.set_ex(id.resize::<{ clog2(RS_ENTRIES_EX) }>(), false),
                        Q::St => e.deps.set_st(id.resize::<{ clog2(RS_ENTRIES_ST) }>(), false),
                    },
                    ..e
                })
            });

        let entries_next =
            Entries { entries_ld: entries_ld_next, entries_ex: entries_ex_next, entries_st: entries_st_next };

        (completed_entry, entries_next)
    }
}

/// Config values set by programmer.
#[derive(Debug, Default, Clone, Copy)]
struct Config {
    a_stride: U<A_STRIDE_BITS>,
    c_stride: U<C_STRIDE_BITS>,
    a_transpose: bool,
    ld_block_strides: Array<U<BLOCK_STRIDE_BITS>, LOAD_STATES>,
    pooling_is_enabled: bool,
    ld_pixel_repeats: Array<U<PIXEL_REPEATS_BITS>, LOAD_STATES>,
}

/// Internal queues logic.
///
/// It returns (1) issued command, (2) completed id to conv, and (3) completed id to matmul for each queue type.
#[allow(clippy::type_complexity)]
fn queues(
    alloc: Vr<Entry>,
    completed_ld: Valid<U<{ clog2(RS_MAX_PER_TYPE) }>>,
    completed_ex: Valid<U<{ clog2(RS_MAX_PER_TYPE) }>>,
    completed_st: Valid<U<{ clog2(RS_MAX_PER_TYPE) }>>,
) -> (
    (
        Vr<RsIssue>,
        Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
        Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    ),
    (
        Vr<RsIssue>,
        Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
        Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    ),
    (
        Vr<RsIssue>,
        Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
        Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
    ),
    Valid<bool>,
) {
    unsafe {
        (alloc, completed_ld, completed_ex, completed_st).fsm::<(
            (
                Vr<RsIssue>,
                Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
                Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
            ),
            (
                Vr<RsIssue>,
                Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
                Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
            ),
            (
                Vr<RsIssue>,
                Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
                Valid<U<{ clog2(MAX_INSTRUCTIONS_COMPLETED_PER_TYPE_PER_CYCLE + 1) }>>,
            ),
            Valid<bool>,
        ), Entries>(
            Entries::default(),
            |(alloc, completed_ld, completed_ex, completed_st),
             ((er_ld, ..), (er_ex, ..), (er_st, ..), ()),
             entries| {
                let new_entry = alloc.map(|new_entry| Entry { deps: entries.get_deps(new_entry), ..new_entry });

                let (is_allocated, entries_next) =
                    if let Some(new_entry) = new_entry { entries.try_alloc(new_entry) } else { (false, entries) };

                let ir = (Ready::new(is_allocated, ()), (), (), ());

                let (issued_ld, conv_ld_issue_completed, matmul_ld_issue_completed, entries_next) = if er_ld.ready {
                    let (issued, entries_next) = entries_next.try_issue(Q::Ld);
                    let complete_on_issue = issued.is_some_and(|p| p.1);
                    let from_conv_fsm = issued.is_some_and(|p| p.0.cmd.from_conv_fsm);
                    let from_matmul_fsm = issued.is_some_and(|p| p.0.cmd.from_matmul_fsm);

                    (
                        issued.map(|p| p.0),
                        complete_on_issue && from_conv_fsm,
                        complete_on_issue && from_matmul_fsm,
                        entries_next,
                    )
                } else {
                    (None, false, false, entries_next)
                };
                let (issued_ex, conv_ex_issue_completed, matmul_ex_issue_completed, entries_next) = if er_ex.ready {
                    let (issued, entries_next) = entries_next.try_issue(Q::Ex);
                    let complete_on_issue = issued.is_some_and(|p| p.1);
                    let from_conv_fsm = issued.is_some_and(|p| p.0.cmd.from_conv_fsm);
                    let from_matmul_fsm = issued.is_some_and(|p| p.0.cmd.from_matmul_fsm);

                    (
                        issued.map(|p| p.0),
                        complete_on_issue && from_conv_fsm,
                        complete_on_issue && from_matmul_fsm,
                        entries_next,
                    )
                } else {
                    (None, false, false, entries_next)
                };
                let (issued_st, conv_st_issue_completed, matmul_st_issue_completed, entries_next) = if er_st.ready {
                    let (issued, entries_next) = entries_next.try_issue(Q::St);
                    let complete_on_issue = issued.is_some_and(|p| p.1);
                    let from_conv_fsm = issued.is_some_and(|p| p.0.cmd.from_conv_fsm);
                    let from_matmul_fsm = issued.is_some_and(|p| p.0.cmd.from_matmul_fsm);

                    (
                        issued.map(|p| p.0),
                        complete_on_issue && from_conv_fsm,
                        complete_on_issue && from_matmul_fsm,
                        entries_next,
                    )
                } else {
                    (None, false, false, entries_next)
                };

                let (conv_ld_completed, matmul_ld_completed, entries_next) = if let Some(id) = completed_ld {
                    let (entry, entries_next) = entries_next.compute_completed(Q::Ld, id);
                    (entry.cmd.from_conv_fsm, entry.cmd.from_matmul_fsm, entries_next)
                } else {
                    (false, false, entries_next)
                };
                let (conv_ex_completed, matmul_ex_completed, entries_next) = if let Some(id) = completed_ex {
                    let (entry, entries_next) = entries_next.compute_completed(Q::Ex, id);
                    (entry.cmd.from_conv_fsm, entry.cmd.from_matmul_fsm, entries_next)
                } else {
                    (false, false, entries_next)
                };
                let (conv_st_completed, matmul_st_completed, entries_next) = if let Some(id) = completed_st {
                    let (entry, entries_next) = entries_next.compute_completed(Q::St, id);
                    (entry.cmd.from_conv_fsm, entry.cmd.from_matmul_fsm, entries_next)
                } else {
                    (false, false, entries_next)
                };

                let conv_ld_completed = conv_ld_issue_completed.into_u::<1>() + conv_ld_completed.into_u::<1>();
                let conv_ex_completed = conv_ex_issue_completed.into_u::<1>() + conv_ex_completed.into_u::<1>();
                let conv_st_completed = conv_st_issue_completed.into_u::<1>() + conv_st_completed.into_u::<1>();

                let matmul_ld_completed = matmul_ld_issue_completed.into_u::<1>() + matmul_ld_completed.into_u::<1>();
                let matmul_ex_completed = matmul_ex_issue_completed.into_u::<1>() + matmul_ex_completed.into_u::<1>();
                let matmul_st_completed = matmul_st_issue_completed.into_u::<1>() + matmul_st_completed.into_u::<1>();

                let ep = (
                    (issued_ld, Some(conv_ld_completed), Some(matmul_ld_completed)),
                    (issued_ex, Some(conv_ex_completed), Some(matmul_ex_completed)),
                    (issued_st, Some(conv_st_completed), Some(matmul_st_completed)),
                    Some(
                        entries.entries_ld.any(|e| e.is_some())
                            || entries.entries_ex.any(|e| e.is_some())
                            || entries.entries_st.any(|e| e.is_some()),
                    ),
                );

                (ep, ir, entries_next)
            },
        )
    }
}

/// Reservation Station
///
/// Due to Gemmini's decoupled access-execute architecture, instructions in the `Load``, `Store`, and `Execute` may operate concurrently and out-of-order with respect to instructions in other modules.
/// This module detects hazards between instructions in `Load`, `Store`, and `Execute`
/// The instructions in the Reservation Station are only issued to their respective modules once they have no dependencies on instructions in other modules.
///
/// Note:
/// Instructions that are destined for the same modules are issued in-order.
/// Reservation Station does not check hazards between instructions within the same module(`Load`, `Store`, and `Execute`)
/// Each module is obligated to handle it's own dependencies and hazards internally, assuming that it receives it's own instructions in program-order.
///
/// # Inputs
///
/// - `alloc`
///     + Allocated instructions from the `LoopMatmul` module.
/// - `completed`
///     + Completed and arbitered instructions from the `Load`, `Store`, and `Execute` modules.
///     + Note that this signal comes from the egress of the `Load`, `Store`, and `Execute` modules.
///     + This implementation preassumes that `module_split` will be applied to this module.
///     + For more information, see the `mod.rs` file in the `crate::gemmini`.
///
/// # Outputs
///
/// - `RsIssues`
///    + Instructions to be issued to the `Load`, `Execute`, and `Store` modules.
/// - `RsCompleted`
///    + Completed instruction IDs from the Reservation Station.
///    + This is sent to `LoopMatmul` and `LoopConv` modules.
/// - `busy`
///    + Reservation station is busy or not.
#[synthesize]
pub fn reservation_station(
    alloc: Vr<GemminiCmd>,
    completed: Valid<U<{ clog2(RS_ENTRIES) }>>,
) -> (RsIssues, RsCompleted, Valid<bool>) {
    let alloc = alloc.fsm_map::<Entry, Config>(Config::default(), |ip, s| {
        let (new_entry, is_norm) = decode_cmd(ip, s);
        let s_next = update_config(new_entry, is_norm, s);

        (new_entry, s_next)
    });

    let [completed_ld, completed_ex, completed_st] = completed
        .map::<(U<{ clog2(RS_MAX_PER_TYPE) }>, BoundedU<3>)>(|p| {
            let q = p.clip_const::<2>(CL_RS_MAX_PER_TYPE);
            let sel = BoundedU::new(q);
            (p.clip_const::<{ clog2(RS_MAX_PER_TYPE) }>(0), sel)
        })
        .branch();

    let ((issue_ld, conv_ld, matmul_ld), (issue_ex, conv_ex, matmul_ex), (issue_st, conv_st, matmul_st), busy) =
        (alloc, completed_ld, completed_ex, completed_st).comb(move |(i1, i2, i3, i4)| queues(i1, i2, i3, i4));

    let rs_issues = RsIssues { ld: issue_ld, ex: issue_ex, st: issue_st };
    let rs_completed = RsCompleted { conv_ld, conv_ex, conv_st, matmul_ld, matmul_ex, matmul_st };

    (rs_issues, rs_completed, busy)
}