184226e46SZihao Yupackage device 284226e46SZihao Yu 384226e46SZihao Yuimport chisel3._ 484226e46SZihao Yuimport chisel3.util._ 50161df2aSZihao Yuimport chisel3.util.experimental.BoringUtils 6ce6a2d5bSZihao Yuimport bus.axi4._ 7f10a0bcbSZihao Yuimport utils._ 884226e46SZihao Yu 9891d22aaSZihao Yuclass TimerIO extends Bundle { 10891d22aaSZihao Yu val mtip = Output(Bool()) 11891d22aaSZihao Yu} 12891d22aaSZihao Yu 13891d22aaSZihao Yuclass AXI4Timer(sim: Boolean = false) extends AXI4SlaveModule(new AXI4Lite, new TimerIO) { 1487557494SZihao Yu val mtime = RegInit(0.U(64.W)) // unit: us 15891d22aaSZihao Yu val mtimecmp = RegInit(0.U(64.W)) 16891d22aaSZihao Yu 1787557494SZihao Yu val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000) 18ac65130dSZihao Yu val freq = RegInit(clk.U(16.W)) 19*5fd0e682SLinJiawei val inc = RegInit(1000.U(16.W)) 20ac65130dSZihao Yu 21ac65130dSZihao Yu val cnt = RegInit(0.U(16.W)) 22ac65130dSZihao Yu val nextCnt = cnt + 1.U 23ac65130dSZihao Yu cnt := Mux(nextCnt < freq, nextCnt, 0.U) 24ac65130dSZihao Yu val tick = (nextCnt === freq) 25ac65130dSZihao Yu when (tick) { mtime := mtime + inc } 26891d22aaSZihao Yu 270161df2aSZihao Yu if (sim) { 280161df2aSZihao Yu val isWFI = WireInit(false.B) 290161df2aSZihao Yu BoringUtils.addSink(isWFI, "isWFI") 300161df2aSZihao Yu when (isWFI) { mtime := mtime + 100000.U } 310161df2aSZihao Yu } 320161df2aSZihao Yu 33891d22aaSZihao Yu val mapping = Map( 34434b30e4SZihao Yu RegMap(0x4000, mtimecmp), 35ac65130dSZihao Yu RegMap(0x8000, freq), 36ac65130dSZihao Yu RegMap(0x8008, inc), 37434b30e4SZihao Yu RegMap(0xbff8, mtime) 38891d22aaSZihao Yu ) 39434b30e4SZihao Yu def getOffset(addr: UInt) = addr(15,0) 40891d22aaSZihao Yu 41434b30e4SZihao Yu RegMap.generate(mapping, getOffset(raddr), in.r.bits.data, 42434b30e4SZihao Yu getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb)) 43891d22aaSZihao Yu 444c8d1f11SZihao Yu io.extra.get.mtip := RegNext(mtime >= mtimecmp) 4584226e46SZihao Yu} 46