xref: /XiangShan/src/main/scala/xiangshan/frontend/RAS.scala (revision e11e6a4cbf230cd653dbcaf0c87e2e7d8f33d7e8)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import xiangshan.backend.ALUOpType
7import utils._
8import chisel3.experimental.chiselName
9
10@chiselName
11class RAS extends BasePredictor
12{
13    class RASResp extends Resp
14    {
15        val target =UInt(VAddrBits.W)
16    }
17
18    class RASBranchInfo extends Meta
19    {
20        val rasSp = UInt(log2Up(RasSize).W)
21        val rasTopCtr = UInt(8.W)
22        val rasToqAddr = UInt(VAddrBits.W)
23    }
24
25    class RASIO extends DefaultBasePredictorIO
26    {
27        val is_ret = Input(Bool())
28        val callIdx = Flipped(ValidIO(UInt(log2Ceil(PredictWidth).W)))
29        val isRVC = Input(Bool())
30        val isLastHalfRVI = Input(Bool())
31        val recover =  Flipped(ValidIO(new BranchUpdateInfo))
32        val out = ValidIO(new RASResp)
33        val branchInfo = Output(new RASBranchInfo)
34    }
35
36    class RASEntry() extends XSBundle {
37        val retAddr = UInt(VAddrBits.W)
38        val ctr = UInt(8.W) // layer of nested call functions
39    }
40
41    def rasEntry() = new RASEntry
42
43    object RASEntry {
44        def apply(retAddr: UInt, ctr: UInt): RASEntry = {
45            val e = Wire(rasEntry())
46            e.retAddr := retAddr
47            e.ctr := ctr
48            e
49        }
50    }
51
52    override val io = IO(new RASIO)
53
54    @chiselName
55    class RASStack(val rasSize: Int) extends XSModule {
56        val io = IO(new Bundle {
57            val push_valid = Input(Bool())
58            val pop_valid = Input(Bool())
59            val new_addr = Input(UInt(VAddrBits.W))
60            val top_addr = Output(UInt(VAddrBits.W))
61            val is_empty = Output(Bool())
62            val is_full = Output(Bool())
63            val copy_valid = Input(Bool())
64            val copy_in_mem  = Input(Vec(rasSize, rasEntry()))
65            val copy_in_sp   = Input(UInt(log2Up(rasSize).W))
66            val copy_out_mem = Output(Vec(rasSize, rasEntry()))
67            val copy_out_sp  = Output(UInt(log2Up(rasSize).W))
68        })
69        @chiselName
70        class Stack(val size: Int) extends XSModule {
71            val io = IO(new Bundle {
72                val rIdx = Input(UInt(log2Up(size).W))
73                val rdata = Output(rasEntry())
74                val wen = Input(Bool())
75                val wIdx = Input(UInt(log2Up(size).W))
76                val wdata = Input(rasEntry())
77                val copyen = Input(Bool())
78                val copy_in = Input(Vec(size, rasEntry()))
79                val copy_out = Output(Vec(size, rasEntry()))
80            })
81            val mem = Reg(Vec(size, rasEntry()))
82            when (io.wen)  {
83                mem(io.wIdx) := io.wdata
84            }
85            io.rdata := mem(io.rIdx)
86            (0 until size).foreach { i => io.copy_out(i) := mem(i) }
87            when (io.copyen) {
88                (0 until size).foreach {i => mem(i) := io.copy_in(i) }
89            }
90        }
91        val sp = RegInit(0.U(log2Up(rasSize).W))
92        val stack = Module(new Stack(rasSize)).io
93
94        stack.rIdx := sp - 1.U
95        val top_entry = stack.rdata
96        val top_addr = top_entry.retAddr
97        val top_ctr = top_entry.ctr
98        val alloc_new = io.new_addr =/= top_addr
99        stack.wen := io.push_valid || io.pop_valid && top_ctr =/= 1.U
100        stack.wIdx := Mux(io.pop_valid && top_ctr =/= 1.U, sp - 1.U, Mux(alloc_new, sp, sp - 1.U))
101        stack.wdata := Mux(io.pop_valid && top_ctr =/= 1.U,
102                            RASEntry(top_addr, top_ctr - 1.U),
103                            Mux(alloc_new, RASEntry(io.new_addr, 1.U), RASEntry(top_addr, top_ctr + 1.U)))
104
105        when (io.push_valid && alloc_new) {
106            sp := sp + 1.U
107        }
108
109        when (io.pop_valid && top_ctr === 1.U) {
110            sp := Mux(sp === 0.U, 0.U, sp - 1.U)
111        }
112
113        io.copy_out_mem := stack.copy_out
114        io.copy_out_sp  := sp
115        stack.copyen := io.copy_valid
116        stack.copy_in := io.copy_in_mem
117        when (io.copy_valid) {
118            sp := io.copy_in_sp
119        }
120
121        io.top_addr := top_addr
122        io.is_empty := sp === 0.U
123        io.is_full  := sp === (RasSize - 1).U
124    }
125
126    // val ras_0 = Reg(Vec(RasSize, rasEntry()))  //RegInit(0.U)asTypeOf(Vec(RasSize,rasEntry)) cause comb loop
127    // val ras_1 = Reg(Vec(RasSize, rasEntry()))
128    // val sp_0 = RegInit(0.U(log2Up(RasSize).W))
129    // val sp_1 = RegInit(0.U(log2Up(RasSize).W))
130    // val choose_bit = RegInit(false.B)   //start with 0
131    // val spec_ras = Mux(choose_bit, ras_1, ras_0)
132    // val spec_sp = Mux(choose_bit,sp_1,sp_0)
133    // val commit_ras = Mux(choose_bit, ras_0, ras_1)
134    // val commit_sp = Mux(choose_bit,sp_0,sp_1)
135
136    // val spec_ras = Reg(Vec(RasSize, rasEntry()))
137    // val spec_sp = RegInit(0.U(log2Up(RasSize).W))
138    // val commit_ras = Reg(Vec(RasSize, rasEntry()))
139    // val commit_sp = RegInit(0.U(log2Up(RasSize).W))
140
141    val spec_ras   = Module(new RASStack(RasSize)).io
142
143    val spec_push = WireInit(false.B)
144    val spec_pop = WireInit(false.B)
145    val spec_new_addr = WireInit(bankAligned(io.pc.bits) + (io.callIdx.bits << 1.U) + Mux(io.isRVC,2.U,Mux(io.isLastHalfRVI, 2.U, 4.U)))
146    spec_ras.push_valid := spec_push
147    spec_ras.pop_valid  := spec_pop
148    spec_ras.new_addr   := spec_new_addr
149    val spec_is_empty = spec_ras.is_empty
150    val spec_is_full = spec_ras.is_full
151    val spec_top_addr = spec_ras.top_addr
152
153    spec_push := !spec_is_full && io.callIdx.valid && io.pc.valid
154    spec_pop  := !spec_is_empty && io.is_ret && io.pc.valid
155
156    val commit_ras = Module(new RASStack(RasSize)).io
157
158    val commit_push = WireInit(false.B)
159    val commit_pop = WireInit(false.B)
160    val commit_new_addr = Mux(io.recover.bits.pd.isRVC,io.recover.bits.pc + 2.U,io.recover.bits.pc + 4.U)
161    commit_ras.push_valid := commit_push
162    commit_ras.pop_valid  := commit_pop
163    commit_ras.new_addr   := commit_new_addr
164    val commit_is_empty = commit_ras.is_empty
165    val commit_is_full = commit_ras.is_full
166    val commit_top_addr = commit_ras.top_addr
167
168    commit_push := !commit_is_full  && io.recover.valid && io.recover.bits.pd.isCall
169    commit_pop  := !commit_is_empty && io.recover.valid && io.recover.bits.pd.isRet
170
171
172    io.out.valid := !spec_is_empty
173    io.out.bits.target := spec_top_addr
174    // TODO: back-up stack for ras
175    // use checkpoint to recover RAS
176
177    val copy_valid = io.recover.valid && io.recover.bits.isMisPred
178    val copy_next = RegNext(copy_valid)
179    spec_ras.copy_valid := copy_next
180    spec_ras.copy_in_mem := commit_ras.copy_out_mem
181    spec_ras.copy_in_sp  := commit_ras.copy_out_sp
182    commit_ras.copy_valid := DontCare
183    commit_ras.copy_in_mem := DontCare
184    commit_ras.copy_in_sp  := DontCare
185
186    //no need to pass the ras branchInfo
187    io.branchInfo.rasSp := DontCare
188    io.branchInfo.rasTopCtr := DontCare
189    io.branchInfo.rasToqAddr := DontCare
190
191    if (BPUDebug && debug) {
192        // XSDebug("----------------RAS(spec)----------------\n")
193        // XSDebug("  index       addr           ctr \n")
194        // for(i <- 0 until RasSize){
195        //     XSDebug("  (%d)   0x%x      %d",i.U,spec_ras(i).retAddr,spec_ras(i).ctr)
196        //     when(i.U === spec_sp){XSDebug(false,true.B,"   <----sp")}
197        //     XSDebug(false,true.B,"\n")
198        // }
199        // XSDebug("----------------RAS(commit)----------------\n")
200        // XSDebug("  index       addr           ctr \n")
201        // for(i <- 0 until RasSize){
202        //     XSDebug("  (%d)   0x%x      %d",i.U,commit_ras(i).retAddr,commit_ras(i).ctr)
203        //     when(i.U === commit_sp){XSDebug(false,true.B,"   <----sp")}
204        //     XSDebug(false,true.B,"\n")
205        // }
206
207        // XSDebug(spec_push, "(spec_ras)push  inAddr: 0x%x  inCtr: %d |  allocNewEntry:%d |   sp:%d \n",spec_ras_write.retAddr,spec_ras_write.ctr,sepc_alloc_new,spec_sp.asUInt)
208        // XSDebug(spec_pop, "(spec_ras)pop outValid:%d  outAddr: 0x%x \n",io.out.valid,io.out.bits.target)
209        // XSDebug(commit_push, "(commit_ras)push  inAddr: 0x%x  inCtr: %d |  allocNewEntry:%d |   sp:%d \n",commit_ras_write.retAddr,commit_ras_write.ctr,sepc_alloc_new,commit_sp.asUInt)
210        // XSDebug(commit_pop, "(commit_ras)pop outValid:%d  outAddr: 0x%x \n",io.out.valid,io.out.bits.target)
211        // XSDebug("copyValid:%d copyNext:%d \n",copy_valid,copy_next)
212    }
213
214
215    // val recoverSp = io.recover.bits.brInfo.rasSp
216    // val recoverCtr = io.recover.bits.brInfo.rasTopCtr
217    // val recoverAddr = io.recover.bits.brInfo.rasToqAddr
218    // val recover_top = ras(recoverSp - 1.U)
219    // when (recover_valid) {
220    //     sp := recoverSp
221    //     recover_top.ctr := recoverCtr
222    //     recover_top.retAddr := recoverAddr
223    //     XSDebug("RAS update: SP:%d , Ctr:%d \n",recoverSp,recoverCtr)
224    // }
225    // val recover_and_push = recover_valid && push
226    // val recover_and_pop = recover_valid && pop
227    // val recover_alloc_new = new_addr =/= recoverAddr
228    // when(recover_and_push)
229    // {
230    //     when(recover_alloc_new){
231    //         sp := recoverSp + 1.U
232    //         ras(recoverSp).retAddr := new_addr
233    //         ras(recoverSp).ctr := 1.U
234    //         recover_top.retAddr := recoverAddr
235    //         recover_top.ctr := recoverCtr
236    //     } .otherwise{
237    //         sp := recoverSp
238    //         recover_top.ctr := recoverCtr + 1.U
239    //         recover_top.retAddr := recoverAddr
240    //     }
241    // } .elsewhen(recover_and_pop)
242    // {
243    //     io.out.bits.target := recoverAddr
244    //     when ( recover_top.ctr === 1.U) {
245    //         sp := recoverSp - 1.U
246    //     }.otherwise {
247    //         sp := recoverSp
248    //        recover_top.ctr := recoverCtr - 1.U
249    //     }
250    // }
251
252}
253