xref: /XiangShan/src/main/scala/xiangshan/frontend/RAS.scala (revision 4971335ec649e6764f5ba85b082c492bc17e4663)
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 CfiUpdateInfo))
32        val out = ValidIO(new RASResp)
33        val meta = 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    override val debug = true
54
55    @chiselName
56    class RASStack(val rasSize: Int) extends XSModule {
57        val io = IO(new Bundle {
58            val push_valid = Input(Bool())
59            val pop_valid = Input(Bool())
60            val new_addr = Input(UInt(VAddrBits.W))
61            val top_addr = Output(UInt(VAddrBits.W))
62            val is_empty = Output(Bool())
63            val is_full = Output(Bool())
64            val copy_valid = Input(Bool())
65            val copy_in_mem  = Input(Vec(rasSize, rasEntry()))
66            val copy_in_sp   = Input(UInt(log2Up(rasSize).W))
67            val copy_in_top  = Input(rasEntry())
68            val copy_out_mem = Output(Vec(rasSize, rasEntry()))
69            val copy_out_sp  = Output(UInt(log2Up(rasSize).W))
70            val copy_out_top  = Output(rasEntry())
71
72        })
73        val debugIO = IO(new Bundle{
74            val write_entry = Output(rasEntry())
75            val alloc_new = Output(Bool())
76            val sp = Output(UInt(log2Up(rasSize).W))
77            //val topRegister = Output(rasEntry())
78        })
79        @chiselName
80        class Stack(val size: Int) extends XSModule {
81            val io = IO(new Bundle {
82                val rIdx = Input(UInt(log2Up(size).W))
83                val rdata = Output(rasEntry())
84                val wen = Input(Bool())
85                val wIdx = Input(UInt(log2Up(size).W))
86                val wdata = Input(rasEntry())
87                val copyen = Input(Bool())
88                val copy_in = Input(Vec(size, rasEntry()))
89                val copy_out = Output(Vec(size, rasEntry()))
90            })
91            val mem = Reg(Vec(size, rasEntry()))
92            when (io.wen)  {
93                mem(io.wIdx) := io.wdata
94            }
95            io.rdata := mem(io.rIdx)
96            (0 until size).foreach { i => io.copy_out(i) := mem(i) }
97            when (io.copyen) {
98                (0 until size).foreach {i => mem(i) := io.copy_in(i) }
99            }
100        }
101        val sp = RegInit(RasSize.U((log2Up(rasSize) + 1).W))
102        val topRegister = RegInit(0.U.asTypeOf(new RASEntry))
103        val stack = Module(new Stack(rasSize)).io
104
105        stack.rIdx := sp - 1.U
106        val top_addr = topRegister.retAddr
107        val top_ctr = topRegister.ctr
108        val alloc_new = io.new_addr =/= top_addr
109        // stack.wen := io.push_valid || io.pop_valid && top_ctr =/= 1.U
110        // stack.wIdx := Mux(io.pop_valid && top_ctr =/= 1.U, sp - 1.U, Mux(alloc_new, sp, sp - 1.U))
111        // val write_addr = Mux(io.pop_valid && top_ctr =/= 1.U, top_addr, io.new_addr)
112        // val write_ctr  = Mux(io.pop_valid && top_ctr =/= 1.U, top_ctr - 1.U, Mux(alloc_new, 1.U, top_ctr + 1.U))
113
114        stack.wen := io.push_valid && !io.is_empty
115        stack.wIdx := sp
116        val write_addr = topRegister.retAddr
117        val write_ctr  = topRegister.ctr
118
119        val write_entry = RASEntry(write_addr, write_ctr)
120        stack.wdata := write_entry
121        debugIO.write_entry := write_entry
122        debugIO.alloc_new := alloc_new
123        debugIO.sp := sp
124
125        val is_empty = sp === RasSize.U
126        val is_full  = sp === (RasSize - 1).U
127
128        when (io.push_valid && alloc_new) {
129            sp := Mux(is_full, sp, Mux(is_empty, 0.U,sp + 1.U))
130            top_addr := io.new_addr
131            top_ctr := 1.U
132        } .elsewhen(io.push_valid) {
133            top_ctr := top_ctr + 1.U
134        }
135
136        when (io.pop_valid && top_ctr === 1.U) {
137            sp := Mux(is_empty, sp ,Mux(sp === 0.U, RasSize.U,sp - 1.U))
138            top_addr := stack.rdata.retAddr
139            top_ctr := stack.rdata.ctr
140        } .elsewhen(io.pop_valid) {
141            top_ctr := top_ctr - 1.U
142        }
143
144        io.copy_out_mem := stack.copy_out
145        io.copy_out_sp  := sp
146        io.copy_out_top  := topRegister
147        stack.copyen := io.copy_valid
148        stack.copy_in := io.copy_in_mem
149        when (io.copy_valid) {
150            sp := io.copy_in_sp
151            topRegister := io.copy_in_top
152        }
153
154        io.top_addr := top_addr
155        io.is_empty := is_empty
156        io.is_full  := is_full
157    }
158
159    // val ras_0 = Reg(Vec(RasSize, rasEntry()))  //RegInit(0.U)asTypeOf(Vec(RasSize,rasEntry)) cause comb loop
160    // val ras_1 = Reg(Vec(RasSize, rasEntry()))
161    // val sp_0 = RegInit(0.U(log2Up(RasSize).W))
162    // val sp_1 = RegInit(0.U(log2Up(RasSize).W))
163    // val choose_bit = RegInit(false.B)   //start with 0
164    // val spec_ras = Mux(choose_bit, ras_1, ras_0)
165    // val spec_sp = Mux(choose_bit,sp_1,sp_0)
166    // val commit_ras = Mux(choose_bit, ras_0, ras_1)
167    // val commit_sp = Mux(choose_bit,sp_0,sp_1)
168
169    // val spec_ras = Reg(Vec(RasSize, rasEntry()))
170    // val spec_sp = RegInit(0.U(log2Up(RasSize).W))
171    // val commit_ras = Reg(Vec(RasSize, rasEntry()))
172    // val commit_sp = RegInit(0.U(log2Up(RasSize).W))
173
174    val spec = Module(new RASStack(RasSize))
175    val spec_ras = spec.io
176
177
178    val spec_push = WireInit(false.B)
179    val spec_pop = WireInit(false.B)
180    val spec_new_addr = packetAligned(io.pc.bits) + (io.callIdx.bits << instOffsetBits.U) + Mux(!io.isRVC && !io.isLastHalfRVI && HasCExtension.B, 2.U, 4.U)
181    spec_ras.push_valid := spec_push
182    spec_ras.pop_valid  := spec_pop
183    spec_ras.new_addr   := spec_new_addr
184    val spec_is_empty = spec_ras.is_empty
185    val spec_is_full = spec_ras.is_full
186    val spec_top_addr = spec_ras.top_addr
187
188    spec_push := !spec_is_full && io.callIdx.valid && io.pc.valid
189    spec_pop  := !spec_is_empty && io.is_ret && io.pc.valid
190
191    val commit = Module(new RASStack(RasSize))
192    val commit_ras = commit.io
193
194    val commit_push = WireInit(false.B)
195    val commit_pop = WireInit(false.B)
196    val commit_new_addr = Mux(io.recover.bits.pd.isRVC && HasCExtension.B, io.recover.bits.pc + 2.U, io.recover.bits.pc + 4.U)
197    commit_ras.push_valid := commit_push
198    commit_ras.pop_valid  := commit_pop
199    commit_ras.new_addr   := commit_new_addr
200    val commit_is_empty = commit_ras.is_empty
201    val commit_is_full = commit_ras.is_full
202    val commit_top_addr = commit_ras.top_addr
203
204    commit_push := !commit_is_full  && io.recover.valid && !io.recover.bits.isReplay && io.recover.bits.pd.isCall
205    commit_pop  := !commit_is_empty && io.recover.valid && !io.recover.bits.isReplay && io.recover.bits.pd.isRet
206
207
208    io.out.valid := !spec_is_empty
209    io.out.bits.target := spec_top_addr
210    // TODO: back-up stack for ras
211    // use checkpoint to recover RAS
212
213    val copy_valid = io.recover.valid && (io.recover.bits.isMisPred || io.recover.bits.isReplay)
214    val copy_next = RegNext(copy_valid)
215    spec_ras.copy_valid := copy_next
216    spec_ras.copy_in_mem := commit_ras.copy_out_mem
217    spec_ras.copy_in_sp  := commit_ras.copy_out_sp
218    spec_ras.copy_in_top := commit_ras.copy_out_top
219    commit_ras.copy_valid := false.B
220    commit_ras.copy_in_mem := DontCare
221    commit_ras.copy_in_sp  := DontCare
222    commit_ras.copy_in_top := DontCare
223
224    //no need to pass the ras branchInfo
225    io.meta.rasSp := DontCare
226    io.meta.rasTopCtr := DontCare
227    io.meta.rasToqAddr := DontCare
228
229    if (BPUDebug && debug) {
230        val spec_debug = spec.debugIO
231        val commit_debug = commit.debugIO
232        XSDebug("----------------RAS(spec)----------------\n")
233        XSDebug("  index       addr           ctr \n")
234        for(i <- 0 until RasSize){
235            XSDebug("  (%d)   0x%x      %d",i.U,spec_ras.copy_out_mem(i).retAddr,spec_ras.copy_out_mem(i).ctr)
236            when(i.U === spec_ras.copy_out_sp){XSDebug(false,true.B,"   <----sp")}
237            XSDebug(false,true.B,"\n")
238        }
239        XSDebug("----------------RAS(commit)----------------\n")
240        XSDebug("  index       addr           ctr \n")
241        for(i <- 0 until RasSize){
242            XSDebug("  (%d)   0x%x      %d",i.U,commit_ras.copy_out_mem(i).retAddr,commit_ras.copy_out_mem(i).ctr)
243            when(i.U === commit_ras.copy_out_sp){XSDebug(false,true.B,"   <----sp")}
244            XSDebug(false,true.B,"\n")
245        }
246
247        XSDebug(spec_push, "(spec_ras)push  inAddr: 0x%x  inCtr: %d |  allocNewEntry:%d |   sp:%d \n",spec_new_addr,spec_debug.write_entry.ctr,spec_debug.alloc_new,spec_debug.sp.asUInt)
248        XSDebug(spec_pop, "(spec_ras)pop outValid:%d  outAddr: 0x%x \n",io.out.valid,io.out.bits.target)
249        XSDebug(commit_push, "(commit_ras)push  inAddr: 0x%x  inCtr: %d |  allocNewEntry:%d |   sp:%d \n",commit_new_addr,commit_debug.write_entry.ctr,commit_debug.alloc_new,commit_debug.sp.asUInt)
250        XSDebug(commit_pop, "(commit_ras)pop outValid:%d  outAddr: 0x%x \n",io.out.valid,io.out.bits.target)
251        XSDebug("copyValid:%d copyNext:%d \n",copy_valid,copy_next)
252    }
253
254
255    // val recoverSp = io.recover.bits.brInfo.rasSp
256    // val recoverCtr = io.recover.bits.brInfo.rasTopCtr
257    // val recoverAddr = io.recover.bits.brInfo.rasToqAddr
258    // val recover_top = ras(recoverSp - 1.U)
259    // when (recover_valid) {
260    //     sp := recoverSp
261    //     recover_top.ctr := recoverCtr
262    //     recover_top.retAddr := recoverAddr
263    //     XSDebug("RAS update: SP:%d , Ctr:%d \n",recoverSp,recoverCtr)
264    // }
265    // val recover_and_push = recover_valid && push
266    // val recover_and_pop = recover_valid && pop
267    // val recover_alloc_new = new_addr =/= recoverAddr
268    // when(recover_and_push)
269    // {
270    //     when(recover_alloc_new){
271    //         sp := recoverSp + 1.U
272    //         ras(recoverSp).retAddr := new_addr
273    //         ras(recoverSp).ctr := 1.U
274    //         recover_top.retAddr := recoverAddr
275    //         recover_top.ctr := recoverCtr
276    //     } .otherwise{
277    //         sp := recoverSp
278    //         recover_top.ctr := recoverCtr + 1.U
279    //         recover_top.retAddr := recoverAddr
280    //     }
281    // } .elsewhen(recover_and_pop)
282    // {
283    //     io.out.bits.target := recoverAddr
284    //     when ( recover_top.ctr === 1.U) {
285    //         sp := recoverSp - 1.U
286    //     }.otherwise {
287    //         sp := recoverSp
288    //        recover_top.ctr := recoverCtr - 1.U
289    //     }
290    // }
291
292}
293