xref: /XiangShan/src/main/scala/xiangshan/backend/rename/RenameTable.scala (revision 7fa2c198f1e99afdc20903de4fd3323056e5642f)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.backend.rename
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import utils.{ParallelPriorityMux, XSError}
23import xiangshan._
24
25class RatReadPort(implicit p: Parameters) extends XSBundle {
26  val hold = Input(Bool())
27  val addr = Input(UInt(5.W))
28  val data = Output(UInt(PhyRegIdxWidth.W))
29}
30
31class RatWritePort(implicit p: Parameters) extends XSBundle {
32  val wen = Bool()
33  val addr = UInt(5.W)
34  val data = UInt(PhyRegIdxWidth.W)
35}
36
37class RenameTable(float: Boolean)(implicit p: Parameters) extends XSModule {
38  val io = IO(new Bundle() {
39    val flush = Input(Bool())
40    val readPorts = Vec({if(float) 4 else 3} * RenameWidth, new RatReadPort)
41    val specWritePorts = Vec(CommitWidth, Input(new RatWritePort))
42    val archWritePorts = Vec(CommitWidth, Input(new RatWritePort))
43    val debug_rdata = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
44  })
45
46  // speculative rename table
47  val spec_table = RegInit(VecInit(Seq.tabulate(32)(i => i.U(PhyRegIdxWidth.W))))
48  val spec_table_next = WireInit(spec_table)
49  // arch state rename table
50  val arch_table = RegInit(VecInit(Seq.tabulate(32)(i => i.U(PhyRegIdxWidth.W))))
51
52  // For better timing, we optimize reading and writing to RenameTable as follows:
53  // (1) Writing at T0 will be actually processed at T1.
54  // (2) Reading is synchronous now.
55  // (3) RAddr at T0 will be used to access the table and get data at T0.
56  // (4) WData at T0 is bypassed to RData at T1.
57  val t1_rdata = io.readPorts.map(p => RegNext(Mux(p.hold, p.data, spec_table_next(p.addr))))
58  val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold))
59  val t1_wSpec = RegNext(io.specWritePorts)
60
61  // WRITE: when instruction commits or walking
62  val t1_flush = RegNext(io.flush)
63  val t1_wSpec_addr = t1_wSpec.map(w => Mux(w.wen, UIntToOH(w.addr), 0.U))
64  for ((next, i) <- spec_table_next.zipWithIndex) {
65    val matchVec = t1_wSpec_addr.map(w => w(i))
66    val wMatch = ParallelPriorityMux(matchVec.reverse, t1_wSpec.map(_.data).reverse)
67    // When there's a flush, we use arch_table to update spec_table.
68    next := Mux(t1_flush, arch_table(i), Mux(VecInit(matchVec).asUInt.orR, wMatch, spec_table(i)))
69  }
70  spec_table := spec_table_next
71
72  // READ: decode-rename stage
73  for ((r, i) <- io.readPorts.zipWithIndex) {
74    // We use two comparisons here because r.hold has bad timing but addrs have better timing.
75    val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr))
76    val t1_bypass = RegNext(VecInit(t0_bypass))
77    val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse)
78    r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata(i))
79  }
80
81  for (w <- io.archWritePorts) {
82    when (w.wen) {
83      arch_table(w.addr) := w.data
84    }
85  }
86
87  io.debug_rdata := arch_table
88}
89
90class RenameTableWrapper(implicit p: Parameters) extends XSModule {
91  val io = IO(new Bundle() {
92    val flush = Input(Bool())
93    val robCommits = Flipped(new RobCommitIO)
94    val intReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort))
95    val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
96    val fpReadPorts = Vec(RenameWidth, Vec(4, new RatReadPort))
97    val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
98    // for debug printing
99    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
100    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
101  })
102
103  val intRat = Module(new RenameTable(float = false))
104  val fpRat = Module(new RenameTable(float = true))
105
106  intRat.io.flush := io.flush
107  intRat.io.debug_rdata <> io.debug_int_rat
108  intRat.io.readPorts <> io.intReadPorts.flatten
109  val intDestValid = io.robCommits.info.map(info => info.rfWen && info.ldest =/= 0.U)
110  for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) {
111    arch.wen  := !io.robCommits.isWalk && io.robCommits.valid(i) && intDestValid(i)
112    arch.addr := io.robCommits.info(i).ldest
113    arch.data := io.robCommits.info(i).pdest
114  }
115  for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) {
116    spec.wen  := io.robCommits.isWalk && io.robCommits.valid(i) && intDestValid(i)
117    spec.addr := io.robCommits.info(i).ldest
118    spec.data := io.robCommits.info(i).old_pdest
119  }
120  for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) {
121    when (rename.wen) {
122      spec.wen  := true.B
123      spec.addr := rename.addr
124      spec.data := rename.data
125    }
126  }
127
128  fpRat.io.flush := io.flush
129  // debug read ports for difftest
130  fpRat.io.debug_rdata <> io.debug_fp_rat
131  fpRat.io.readPorts <> io.fpReadPorts.flatten
132  for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) {
133    arch.wen  := !io.robCommits.isWalk && io.robCommits.valid(i) && io.robCommits.info(i).fpWen
134    arch.addr := io.robCommits.info(i).ldest
135    arch.data := io.robCommits.info(i).pdest
136  }
137  for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) {
138    spec.wen  := io.robCommits.isWalk && io.robCommits.valid(i) && io.robCommits.info(i).fpWen
139    spec.addr := io.robCommits.info(i).ldest
140    spec.data := io.robCommits.info(i).old_pdest
141  }
142  for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) {
143    when (rename.wen) {
144      spec.wen  := true.B
145      spec.addr := rename.addr
146      spec.data := rename.data
147    }
148  }
149
150}
151