184226e46SZihao Yupackage device 284226e46SZihao Yu 384226e46SZihao Yuimport chisel3._ 484226e46SZihao Yuimport chisel3.util._ 584226e46SZihao Yu 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) { 14*87557494SZihao Yu val mtime = RegInit(0.U(64.W)) // unit: us 15891d22aaSZihao Yu val mtimecmp = RegInit(0.U(64.W)) 16891d22aaSZihao Yu 17*87557494SZihao Yu val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000) 1884226e46SZihao Yu val tick = Counter(true.B, clk)._2 19891d22aaSZihao Yu when (tick) { mtime := mtime + 1.U } 20891d22aaSZihao Yu 21891d22aaSZihao Yu val mapping = Map( 22434b30e4SZihao Yu RegMap(0x4000, mtimecmp), 23434b30e4SZihao Yu RegMap(0xbff8, mtime) 24891d22aaSZihao Yu ) 25434b30e4SZihao Yu def getOffset(addr: UInt) = addr(15,0) 26891d22aaSZihao Yu 27434b30e4SZihao Yu RegMap.generate(mapping, getOffset(raddr), in.r.bits.data, 28434b30e4SZihao Yu getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb)) 29891d22aaSZihao Yu 304c8d1f11SZihao Yu io.extra.get.mtip := RegNext(mtime >= mtimecmp) 3184226e46SZihao Yu} 32