|
| 1 | +package treecorel2 |
| 2 | + |
| 3 | +import chisel3._ |
| 4 | +import chisel3.util._ |
| 5 | + |
| 6 | +import difftest._ |
| 7 | + |
| 8 | +import treecorel2.common.ConstVal |
| 9 | +import treecorel2.common.InstConfig |
| 10 | + |
| 11 | +class WBU extends Module with InstConfig { |
| 12 | + val io = IO(new Bundle { |
| 13 | + val globalEn = Input(Bool()) |
| 14 | + val socEn = Input(Bool()) |
| 15 | + val mem2wb = Flipped(new MEM2WBIO) |
| 16 | + val wbdata = new WBDATAIO |
| 17 | + val gpr = Input(Vec(32, UInt(64.W))) |
| 18 | + }) |
| 19 | + |
| 20 | + protected val wbReg = RegEnable(io.mem2wb, WireInit(0.U.asTypeOf(new MEM2WBIO())), io.globalEn) |
| 21 | + protected val valid = wbReg.valid |
| 22 | + protected val inst = wbReg.inst |
| 23 | + protected val pc = wbReg.pc |
| 24 | + protected val isa = wbReg.isa |
| 25 | + protected val src1 = wbReg.src1 |
| 26 | + protected val src2 = wbReg.src2 |
| 27 | + protected val imm = wbReg.imm |
| 28 | + protected val wen = wbReg.wen |
| 29 | + protected val wdest = wbReg.wdest |
| 30 | + protected val aluRes = wbReg.aluRes |
| 31 | + protected val branch = wbReg.branch |
| 32 | + protected val tgt = wbReg.tgt |
| 33 | + protected val link = wbReg.link |
| 34 | + protected val auipc = wbReg.auipc |
| 35 | + protected val loadData = wbReg.loadData |
| 36 | + protected val csrData = wbReg.csrData |
| 37 | + protected val cvalid = wbReg.cvalid |
| 38 | + protected val timeIntrEn = wbReg.timeIntrEn |
| 39 | + protected val ecallEn = wbReg.ecallEn |
| 40 | + protected val csr = wbReg.csr |
| 41 | + |
| 42 | + protected val cycleCnt = RegInit(0.U(64.W)) |
| 43 | + protected val instrCnt = RegInit(0.U(64.W)) |
| 44 | + cycleCnt := cycleCnt + 1.U |
| 45 | + when(io.globalEn && valid) { instrCnt := instrCnt + 1.U } |
| 46 | + |
| 47 | + protected val wbdata = aluRes | link | auipc | loadData | csrData |
| 48 | + |
| 49 | + io.wbdata.wen := valid && wen |
| 50 | + io.wbdata.wdest := wdest |
| 51 | + io.wbdata.data := wbdata |
| 52 | + |
| 53 | + protected val printVis = inst(6, 0) === "h7b".U(7.W) |
| 54 | + protected val haltVis = inst(6, 0) === "h6b".U(7.W) |
| 55 | + |
| 56 | + when(~io.socEn) { |
| 57 | + when(io.globalEn && valid && printVis) { |
| 58 | + printf("%c", io.gpr(10)) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // for difftest commit |
| 63 | + protected val mmioEn = cvalid |
| 64 | + protected val csrVis = isa.CSRRW || isa.CSRRS || isa.CSRRC || isa.CSRRWI || isa.CSRRSI || isa.CSRRCI |
| 65 | + protected val mcycleVis = csrVis && (inst(31, 20) === ConstVal.mcycleAddr) |
| 66 | + protected val mipVis = csrVis && (inst(31, 20) === ConstVal.mipAddr) |
| 67 | + protected val timeIntrEnReg = RegEnable(timeIntrEn, false.B, io.globalEn) |
| 68 | + protected val diffValid = io.globalEn && (RegEnable(valid, false.B, io.globalEn) || timeIntrEnReg) |
| 69 | + |
| 70 | + if (!SoCEna) { |
| 71 | + val instComm = Module(new DifftestInstrCommit) |
| 72 | + val archIntRegState = Module(new DifftestArchIntRegState) |
| 73 | + val csrState = Module(new DifftestCSRState) |
| 74 | + val trapEvt = Module(new DifftestTrapEvent) |
| 75 | + val archFpRegState = Module(new DifftestArchFpRegState) |
| 76 | + val archEvt = Module(new DifftestArchEvent) |
| 77 | + |
| 78 | + instComm.io.clock := clock |
| 79 | + instComm.io.coreid := 0.U |
| 80 | + instComm.io.index := 0.U |
| 81 | + instComm.io.valid := diffValid && ~timeIntrEnReg |
| 82 | + instComm.io.pc := RegEnable(pc, 0.U, io.globalEn) |
| 83 | + instComm.io.instr := RegEnable(inst, 0.U, io.globalEn) |
| 84 | + instComm.io.special := 0.U |
| 85 | + instComm.io.skip := diffValid && RegEnable(printVis || mcycleVis || mmioEn || mipVis, false.B, io.globalEn) |
| 86 | + instComm.io.isRVC := false.B |
| 87 | + instComm.io.scFailed := false.B |
| 88 | + instComm.io.wen := RegEnable(wen, false.B, io.globalEn) |
| 89 | + instComm.io.wdata := RegEnable(wbdata, 0.U, io.globalEn) |
| 90 | + instComm.io.wdest := RegEnable(wdest, 0.U, io.globalEn) |
| 91 | + |
| 92 | + archIntRegState.io.clock := clock |
| 93 | + archIntRegState.io.coreid := 0.U |
| 94 | + archIntRegState.io.gpr := io.gpr |
| 95 | + |
| 96 | + csrState.io.clock := clock |
| 97 | + csrState.io.coreid := 0.U |
| 98 | + csrState.io.mstatus := csr.mstatus |
| 99 | + csrState.io.mcause := csr.mcause |
| 100 | + csrState.io.mepc := csr.mepc |
| 101 | + csrState.io.sstatus := csr.mstatus & "h8000_0003_000d_e122".U |
| 102 | + csrState.io.scause := 0.U |
| 103 | + csrState.io.sepc := 0.U |
| 104 | + csrState.io.satp := 0.U |
| 105 | + csrState.io.mip := 0.U |
| 106 | + csrState.io.mie := csr.mie |
| 107 | + csrState.io.mscratch := csr.mscratch |
| 108 | + csrState.io.sscratch := 0.U |
| 109 | + csrState.io.mideleg := 0.U |
| 110 | + csrState.io.medeleg := csr.medeleg |
| 111 | + csrState.io.mtval := 0.U |
| 112 | + csrState.io.stval := 0.U |
| 113 | + csrState.io.mtvec := csr.mtvec |
| 114 | + csrState.io.stvec := 0.U |
| 115 | + csrState.io.priviledgeMode := 3.U |
| 116 | + |
| 117 | + archEvt.io.clock := clock |
| 118 | + archEvt.io.coreid := 0.U |
| 119 | + archEvt.io.intrNO := Mux(diffValid && timeIntrEnReg, 7.U, 0.U) |
| 120 | + archEvt.io.cause := 0.U |
| 121 | + archEvt.io.exceptionPC := RegEnable(pc, 0.U, io.globalEn) |
| 122 | + archEvt.io.exceptionInst := RegEnable(inst, 0.U, io.globalEn) |
| 123 | + |
| 124 | + trapEvt.io.clock := clock |
| 125 | + trapEvt.io.coreid := 0.U |
| 126 | + trapEvt.io.valid := diffValid && RegEnable(haltVis, false.B, io.globalEn) |
| 127 | + trapEvt.io.code := io.gpr(10)(7, 0) |
| 128 | + trapEvt.io.pc := RegEnable(pc, 0.U, io.globalEn) |
| 129 | + trapEvt.io.cycleCnt := cycleCnt |
| 130 | + trapEvt.io.instrCnt := instrCnt |
| 131 | + |
| 132 | + archFpRegState.io.clock := clock |
| 133 | + archFpRegState.io.coreid := 0.U |
| 134 | + archFpRegState.io.fpr := RegInit(VecInit(Seq.fill(32)(0.U(64.W)))) |
| 135 | + } |
| 136 | +} |
0 commit comments