xref: /XiangShan/src/main/scala/device/AXI4Timer.scala (revision 87557494ba3206fedbb6efe669c09bc67fa43b74)
1package device
2
3import chisel3._
4import chisel3.util._
5
6import bus.axi4._
7import utils._
8
9class TimerIO extends Bundle {
10  val mtip = Output(Bool())
11}
12
13class AXI4Timer(sim: Boolean = false) extends AXI4SlaveModule(new AXI4Lite, new TimerIO) {
14  val mtime = RegInit(0.U(64.W))  // unit: us
15  val mtimecmp = RegInit(0.U(64.W))
16
17  val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000)
18  val tick = Counter(true.B, clk)._2
19  when (tick) { mtime := mtime + 1.U }
20
21  val mapping = Map(
22    RegMap(0x4000, mtimecmp),
23    RegMap(0xbff8, mtime)
24  )
25  def getOffset(addr: UInt) = addr(15,0)
26
27  RegMap.generate(mapping, getOffset(raddr), in.r.bits.data,
28    getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb))
29
30  io.extra.get.mtip := RegNext(mtime >= mtimecmp)
31}
32