xref: /XiangShan/src/main/scala/xiangshan/backend/rename/RenameTable.scala (revision 1b46b9591920008655d659ac88cd0250db769664)
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 utility.HasCircularQueuePtrHelper
23import utility.ParallelPriorityMux
24import utils.XSError
25import xiangshan._
26import xiangshan.backend.SnapshotGenerator
27
28class RatReadPort(implicit p: Parameters) extends XSBundle {
29  val hold = Input(Bool())
30  val addr = Input(UInt(5.W))
31  val data = Output(UInt(PhyRegIdxWidth.W))
32}
33
34class RatWritePort(implicit p: Parameters) extends XSBundle {
35  val wen = Bool()
36  val addr = UInt(5.W)
37  val data = UInt(PhyRegIdxWidth.W)
38}
39
40class RenameTable(float: Boolean)(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
41  val io = IO(new Bundle {
42    val redirect = Input(Bool())
43    val readPorts = Vec({if(float) 4 else 3} * RenameWidth, new RatReadPort)
44    val specWritePorts = Vec(CommitWidth, Input(new RatWritePort))
45    val archWritePorts = Vec(CommitWidth, Input(new RatWritePort))
46    val old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
47    val need_free = Vec(CommitWidth, Output(Bool()))
48    val snpt = Input(new SnapshotPort)
49    val debug_rdata = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
50  })
51
52  // speculative rename table
53  val rename_table_init = VecInit.tabulate(32)(i => (if (float) i else 0).U(PhyRegIdxWidth.W))
54  val spec_table = RegInit(rename_table_init)
55  val spec_table_next = WireInit(spec_table)
56  // arch state rename table
57  val arch_table = RegInit(rename_table_init)
58  val arch_table_next = WireDefault(arch_table)
59  // old_pdest
60  val old_pdest = RegInit(VecInit.fill(CommitWidth)(0.U(PhyRegIdxWidth.W)))
61  val need_free = RegInit(VecInit.fill(CommitWidth)(false.B))
62
63  // For better timing, we optimize reading and writing to RenameTable as follows:
64  // (1) Writing at T0 will be actually processed at T1.
65  // (2) Reading is synchronous now.
66  // (3) RAddr at T0 will be used to access the table and get data at T0.
67  // (4) WData at T0 is bypassed to RData at T1.
68  val t1_redirect = RegNext(io.redirect, false.B)
69  val t1_rdata = io.readPorts.map(p => RegNext(Mux(p.hold, p.data, spec_table_next(p.addr))))
70  val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold))
71  val t1_wSpec = RegNext(Mux(io.redirect, 0.U.asTypeOf(io.specWritePorts), io.specWritePorts))
72
73  val t1_snpt = RegNext(io.snpt, 0.U.asTypeOf(io.snpt))
74
75  val snapshots = SnapshotGenerator(spec_table, t1_snpt.snptEnq, t1_snpt.snptDeq, t1_redirect)
76
77  // WRITE: when instruction commits or walking
78  val t1_wSpec_addr = t1_wSpec.map(w => Mux(w.wen, UIntToOH(w.addr), 0.U))
79  for ((next, i) <- spec_table_next.zipWithIndex) {
80    val matchVec = t1_wSpec_addr.map(w => w(i))
81    val wMatch = ParallelPriorityMux(matchVec.reverse, t1_wSpec.map(_.data).reverse)
82    // When there's a flush, we use arch_table to update spec_table.
83    next := Mux(
84      t1_redirect,
85      Mux(t1_snpt.useSnpt, snapshots(t1_snpt.snptSelect)(i), arch_table(i)),
86      Mux(VecInit(matchVec).asUInt.orR, wMatch, spec_table(i))
87    )
88  }
89  spec_table := spec_table_next
90
91  // READ: decode-rename stage
92  for ((r, i) <- io.readPorts.zipWithIndex) {
93    // We use two comparisons here because r.hold has bad timing but addrs have better timing.
94    val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr))
95    val t1_bypass = RegNext(Mux(io.redirect, 0.U.asTypeOf(VecInit(t0_bypass)), VecInit(t0_bypass)))
96    val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse)
97    r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata(i))
98  }
99
100  for ((w, i) <- io.archWritePorts.zipWithIndex) {
101    when (w.wen) {
102      arch_table_next(w.addr) := w.data
103    }
104    val arch_mask = VecInit.fill(PhyRegIdxWidth)(w.wen).asUInt
105    old_pdest(i) :=
106      MuxCase(arch_table(w.addr) & arch_mask,
107              io.archWritePorts.take(i).reverse.map(x => (x.wen && x.addr === w.addr, x.data & arch_mask)))
108  }
109  arch_table := arch_table_next
110
111  for (((old, free), i) <- (old_pdest zip need_free).zipWithIndex) {
112    val hasDuplicate = old_pdest.take(i).map(_ === old)
113    val blockedByDup = if (i == 0) false.B else VecInit(hasDuplicate).asUInt.orR
114    free := VecInit(arch_table.map(_ =/= old)).asUInt.andR && !blockedByDup
115  }
116
117  io.old_pdest := old_pdest
118  io.need_free := need_free
119  io.debug_rdata := arch_table
120}
121
122class RenameTableWrapper(implicit p: Parameters) extends XSModule {
123  val io = IO(new Bundle() {
124    val redirect = Input(Bool())
125    val robCommits = Input(new RobCommitIO)
126    val intReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort))
127    val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
128    val fpReadPorts = Vec(RenameWidth, Vec(4, new RatReadPort))
129    val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
130    val int_old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
131    val fp_old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
132    val int_need_free = Vec(CommitWidth, Output(Bool()))
133    val snpt = Input(new SnapshotPort)
134    // for debug printing
135    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
136    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
137  })
138
139  val intRat = Module(new RenameTable(float = false))
140  val fpRat = Module(new RenameTable(float = true))
141
142  intRat.io.debug_rdata <> io.debug_int_rat
143  intRat.io.readPorts <> io.intReadPorts.flatten
144  intRat.io.redirect := io.redirect
145  fpRat.io.redirect := io.redirect
146  intRat.io.snpt := io.snpt
147  fpRat.io.snpt := io.snpt
148  io.int_old_pdest := intRat.io.old_pdest
149  io.fp_old_pdest := fpRat.io.old_pdest
150  io.int_need_free := intRat.io.need_free
151  val intDestValid = io.robCommits.info.map(_.rfWen)
152  for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) {
153    arch.wen  := io.robCommits.isCommit && io.robCommits.commitValid(i) && intDestValid(i)
154    arch.addr := io.robCommits.info(i).ldest
155    arch.data := io.robCommits.info(i).pdest
156    XSError(arch.wen && arch.addr === 0.U && arch.data =/= 0.U, "pdest for $0 should be 0\n")
157  }
158  for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) {
159    spec.wen  := io.robCommits.isWalk && io.robCommits.walkValid(i) && intDestValid(i)
160    spec.addr := io.robCommits.info(i).ldest
161    spec.data := io.robCommits.info(i).pdest
162    XSError(spec.wen && spec.addr === 0.U && spec.data =/= 0.U, "pdest for $0 should be 0\n")
163  }
164  for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) {
165    when (rename.wen) {
166      spec.wen  := true.B
167      spec.addr := rename.addr
168      spec.data := rename.data
169    }
170  }
171
172  // debug read ports for difftest
173  fpRat.io.debug_rdata <> io.debug_fp_rat
174  fpRat.io.readPorts <> io.fpReadPorts.flatten
175  for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) {
176    arch.wen  := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).fpWen
177    arch.addr := io.robCommits.info(i).ldest
178    arch.data := io.robCommits.info(i).pdest
179  }
180  for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) {
181    spec.wen  := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).fpWen
182    spec.addr := io.robCommits.info(i).ldest
183    spec.data := io.robCommits.info(i).pdest
184  }
185  for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) {
186    when (rename.wen) {
187      spec.wen  := true.B
188      spec.addr := rename.addr
189      spec.data := rename.data
190    }
191  }
192
193}
194