1package device 2 3import chisel3._ 4import chisel3.util.experimental.BoringUtils 5import chipsalliance.rocketchip.config.Parameters 6import freechips.rocketchip.diplomacy.AddressSet 7import utils._ 8 9class TimerIO extends Bundle { 10 val mtip = Output(Bool()) 11} 12 13class AXI4Timer 14( 15 sim: Boolean = false, 16 address: AddressSet 17)(implicit p: Parameters) 18 extends AXI4SlaveModule(address, executable = false, _extra = new TimerIO) 19{ 20 override lazy val module = new AXI4SlaveModuleImp[TimerIO](this){ 21 val mtime = RegInit(0.U(64.W)) // unit: us 22 val mtimecmp = RegInit(0.U(64.W)) 23 24 val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000) 25 val freq = RegInit(clk.U(16.W)) 26 val inc = RegInit(1000.U(16.W)) 27 28 val cnt = RegInit(0.U(16.W)) 29 val nextCnt = cnt + 1.U 30 cnt := Mux(nextCnt < freq, nextCnt, 0.U) 31 val tick = (nextCnt === freq) 32 when (tick) { mtime := mtime + inc } 33 34 val mapping = Map( 35 RegMap(0x4000, mtimecmp), 36 RegMap(0x8000, freq), 37 RegMap(0x8008, inc), 38 RegMap(0xbff8, mtime) 39 ) 40 def getOffset(addr: UInt) = addr(15,0) 41 42 RegMap.generate(mapping, getOffset(raddr), in.r.bits.data, 43 getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb)) 44 45 io.extra.get.mtip := RegNext(mtime >= mtimecmp) 46 } 47} 48