xref: /XiangShan/src/main/scala/xiangshan/backend/fu/Branch.scala (revision 83ba63b34cf09b33c0a9e1b3203138e51af4491b)
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.fu
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utility.LookupTree
23import xiangshan._
24
25class BranchModule(implicit p: Parameters) extends XSModule {
26  val io = IO(new Bundle() {
27    val src = Vec(2, Input(UInt(XLEN.W)))
28    val func = Input(FuOpType())
29    val pred_taken = Input(Bool())
30    val taken, mispredict = Output(Bool())
31  })
32  val (src1, src2, func) = (io.src(0), io.src(1), io.func)
33
34  val subModule = Module(new SubModule)
35  subModule.io.src(0) := src1
36  subModule.io.src(1) := src2
37  val sub  = subModule.io.sub
38  val sltu    = !sub(XLEN)
39  val slt     = src1(XLEN - 1) ^ src2(XLEN - 1) ^ sltu
40  val xor     = src1 ^ src2
41  // branch
42  val branchOpTable = List(
43    BRUOpType.getBranchType(BRUOpType.beq)  -> !xor.orR,
44    BRUOpType.getBranchType(BRUOpType.blt)  -> slt,
45    BRUOpType.getBranchType(BRUOpType.bltu) -> sltu
46  )
47  val taken = LookupTree(BRUOpType.getBranchType(func), branchOpTable) ^ BRUOpType.isBranchInvert(func)
48
49  io.taken := taken
50  io.mispredict := io.pred_taken ^ taken
51}
52