refactor: 修改exe级跳转为flush

This commit is contained in:
Liphen 2023-12-01 16:22:49 +08:00
parent 36840d6abe
commit 005880f152
4 changed files with 28 additions and 27 deletions

View File

@ -16,7 +16,7 @@ class ExecuteUnit(implicit val config: CpuConfig) extends Module {
val csr = Flipped(new CsrExecuteUnit()) val csr = Flipped(new CsrExecuteUnit())
val bpu = new ExecuteUnitBranchPredictor() val bpu = new ExecuteUnitBranchPredictor()
val fetchUnit = Output(new Bundle { val fetchUnit = Output(new Bundle {
val branch = Bool() val flush = Bool()
val target = UInt(PC_WID.W) val target = UInt(PC_WID.W)
}) })
val decoderUnit = new Bundle { val decoderUnit = new Bundle {
@ -57,8 +57,7 @@ class ExecuteUnit(implicit val config: CpuConfig) extends Module {
io.ctrl.inst(0).reg_waddr := io.executeStage.inst0.info.reg_waddr io.ctrl.inst(0).reg_waddr := io.executeStage.inst0.info.reg_waddr
io.ctrl.inst(1).mem_wreg := io.executeStage.inst1.info.mem_wreg io.ctrl.inst(1).mem_wreg := io.executeStage.inst1.info.mem_wreg
io.ctrl.inst(1).reg_waddr := io.executeStage.inst1.info.reg_waddr io.ctrl.inst(1).reg_waddr := io.executeStage.inst1.info.reg_waddr
io.ctrl.branch := valid(0) && io.ctrl.allow_to_go && io.ctrl.branch := io.fetchUnit.flush
(fu.branch.jump_regiser || fu.branch.pred_fail)
io.csr.in.valid := is_csr.asUInt.orR io.csr.in.valid := is_csr.asUInt.orR
io.csr.in.info := MuxCase( io.csr.in.info := MuxCase(
@ -116,7 +115,8 @@ class ExecuteUnit(implicit val config: CpuConfig) extends Module {
io.bpu.branch := fu.branch.branch io.bpu.branch := fu.branch.branch
io.bpu.branch_inst := io.executeStage.inst0.jb_info.branch_inst io.bpu.branch_inst := io.executeStage.inst0.jb_info.branch_inst
io.fetchUnit.branch := io.ctrl.branch io.fetchUnit.flush := valid(0) && io.ctrl.allow_to_go &&
fu.branch.flush
io.fetchUnit.target := fu.branch.target io.fetchUnit.target := fu.branch.target
io.ctrl.fu_stall := fu.stall_req io.ctrl.fu_stall := fu.stall_req
@ -151,8 +151,8 @@ class ExecuteUnit(implicit val config: CpuConfig) extends Module {
) )
) )
io.memoryStage.inst0.ex.exception(instrAddrMisaligned) := io.executeStage.inst0.ex.exception(instrAddrMisaligned) || io.memoryStage.inst0.ex.exception(instrAddrMisaligned) := io.executeStage.inst0.ex.exception(instrAddrMisaligned) ||
io.fetchUnit.branch && io.fetchUnit.target(1, 0).orR io.fetchUnit.flush && io.fetchUnit.target(1, 0).orR
when(io.fetchUnit.branch && io.fetchUnit.target(1, 0).orR) { when(io.fetchUnit.flush && io.fetchUnit.target(1, 0).orR) {
io.memoryStage.inst0.ex.tval := io.fetchUnit.target io.memoryStage.inst0.ex.tval := io.fetchUnit.target
} }
@ -178,8 +178,8 @@ class ExecuteUnit(implicit val config: CpuConfig) extends Module {
) )
) )
io.memoryStage.inst1.ex.exception(instrAddrMisaligned) := io.executeStage.inst1.ex.exception(instrAddrMisaligned) || io.memoryStage.inst1.ex.exception(instrAddrMisaligned) := io.executeStage.inst1.ex.exception(instrAddrMisaligned) ||
io.fetchUnit.branch && io.fetchUnit.target(1, 0).orR io.fetchUnit.flush && io.fetchUnit.target(1, 0).orR
when(io.fetchUnit.branch && io.fetchUnit.target(1, 0).orR) { when(io.fetchUnit.flush && io.fetchUnit.target(1, 0).orR) {
io.memoryStage.inst1.ex.tval := io.fetchUnit.target io.memoryStage.inst1.ex.tval := io.fetchUnit.target
} }

View File

@ -31,7 +31,7 @@ class Fu(implicit val config: CpuConfig) extends Module {
val jump_regiser = Input(Bool()) val jump_regiser = Input(Bool())
val branch_target = Input(UInt(PC_WID.W)) val branch_target = Input(UInt(PC_WID.W))
val branch = Output(Bool()) val branch = Output(Bool())
val pred_fail = Output(Bool()) val flush = Output(Bool())
val target = Output(UInt(PC_WID.W)) val target = Output(UInt(PC_WID.W))
} }
}) })
@ -46,7 +46,7 @@ class Fu(implicit val config: CpuConfig) extends Module {
branchCtrl.in.jump_regiser := io.branch.jump_regiser branchCtrl.in.jump_regiser := io.branch.jump_regiser
branchCtrl.in.branch_target := io.branch.branch_target branchCtrl.in.branch_target := io.branch.branch_target
io.branch.branch := branchCtrl.out.branch io.branch.branch := branchCtrl.out.branch
io.branch.pred_fail := branchCtrl.out.pred_fail io.branch.flush := (branchCtrl.out.pred_fail || io.branch.jump_regiser)
io.branch.target := branchCtrl.out.target io.branch.target := branchCtrl.out.target
for (i <- 0 until (config.fuNum)) { for (i <- 0 until (config.fuNum)) {

View File

@ -5,20 +5,21 @@ import chisel3.util._
import cpu.defines.Const._ import cpu.defines.Const._
import cpu.CpuConfig import cpu.CpuConfig
class FetchUnit(implicit class FetchUnit(
val config: CpuConfig, implicit
) extends Module { val config: CpuConfig)
extends Module {
val io = IO(new Bundle { val io = IO(new Bundle {
val memory = new Bundle { val memory = new Bundle {
val flush = Input(Bool()) val flush = Input(Bool())
val flush_pc = Input(UInt(PC_WID.W)) val target = Input(UInt(PC_WID.W))
} }
val decoder = new Bundle { val decoder = new Bundle {
val branch = Input(Bool()) val branch = Input(Bool())
val target = Input(UInt(PC_WID.W)) val target = Input(UInt(PC_WID.W))
} }
val execute = new Bundle { val execute = new Bundle {
val branch = Input(Bool()) val flush = Input(Bool())
val target = Input(UInt(PC_WID.W)) val target = Input(UInt(PC_WID.W))
} }
val instFifo = new Bundle { val instFifo = new Bundle {
@ -48,10 +49,10 @@ class FetchUnit(implicit
io.iCache.pc_next := MuxCase( io.iCache.pc_next := MuxCase(
pc_next_temp, pc_next_temp,
Seq( Seq(
io.memory.flush -> io.memory.flush_pc, io.memory.flush -> io.memory.target,
io.execute.branch -> io.execute.target, io.execute.flush -> io.execute.target,
io.decoder.branch -> io.decoder.target, io.decoder.branch -> io.decoder.target,
io.instFifo.full -> pc, io.instFifo.full -> pc
), )
) )
} }

View File

@ -15,7 +15,7 @@ class MemoryUnit(implicit val config: CpuConfig) extends Module {
val memoryStage = Input(new ExecuteUnitMemoryUnit()) val memoryStage = Input(new ExecuteUnitMemoryUnit())
val fetchUnit = Output(new Bundle { val fetchUnit = Output(new Bundle {
val flush = Bool() val flush = Bool()
val flush_pc = UInt(PC_WID.W) val target = UInt(PC_WID.W)
}) })
val decoderUnit = Output(Vec(config.fuNum, new RegWrite())) val decoderUnit = Output(Vec(config.fuNum, new RegWrite()))
val csr = Flipped(new CsrMemoryUnit()) val csr = Flipped(new CsrMemoryUnit())
@ -81,7 +81,7 @@ class MemoryUnit(implicit val config: CpuConfig) extends Module {
) )
io.fetchUnit.flush := io.csr.out.flush && io.ctrl.allow_to_go io.fetchUnit.flush := io.csr.out.flush && io.ctrl.allow_to_go
io.fetchUnit.flush_pc := Mux(io.csr.out.flush, io.csr.out.flush_pc, io.writeBackStage.inst0.pc + 4.U) io.fetchUnit.target := Mux(io.csr.out.flush, io.csr.out.flush_pc, io.writeBackStage.inst0.pc + 4.U)
io.ctrl.flush_req := io.fetchUnit.flush io.ctrl.flush_req := io.fetchUnit.flush
} }