xref: /XiangShan/src/main/scala/device/AXI4Timer.scala (revision c6d439803a044ea209139672b25e35fe8d7f4aa0)
1*c6d43980SLemover/***************************************************************************************
2*c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3*c6d43980SLemover*
4*c6d43980SLemover* XiangShan is licensed under Mulan PSL v2.
5*c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
6*c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at:
7*c6d43980SLemover*          http://license.coscl.org.cn/MulanPSL2
8*c6d43980SLemover*
9*c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
10*c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
11*c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
12*c6d43980SLemover*
13*c6d43980SLemover* See the Mulan PSL v2 for more details.
14*c6d43980SLemover***************************************************************************************/
15*c6d43980SLemover
1684226e46SZihao Yupackage device
1784226e46SZihao Yu
1884226e46SZihao Yuimport chisel3._
1963934268Slinjiaweiimport chipsalliance.rocketchip.config.Parameters
2063934268Slinjiaweiimport freechips.rocketchip.diplomacy.AddressSet
21f10a0bcbSZihao Yuimport utils._
2284226e46SZihao Yu
23891d22aaSZihao Yuclass TimerIO extends Bundle {
24891d22aaSZihao Yu  val mtip = Output(Bool())
25891d22aaSZihao Yu}
26891d22aaSZihao Yu
2763934268Slinjiaweiclass AXI4Timer
2863934268Slinjiawei(
2963934268Slinjiawei  sim: Boolean = false,
30a2e9bde6SAllen  address: Seq[AddressSet]
3163934268Slinjiawei)(implicit p: Parameters)
3263934268Slinjiawei  extends AXI4SlaveModule(address, executable = false, _extra = new TimerIO)
3363934268Slinjiawei{
3463934268Slinjiawei  override lazy val module = new AXI4SlaveModuleImp[TimerIO](this){
3587557494SZihao Yu    val mtime = RegInit(0.U(64.W))  // unit: us
36891d22aaSZihao Yu    val mtimecmp = RegInit(0.U(64.W))
37891d22aaSZihao Yu
3887557494SZihao Yu    val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000)
39ac65130dSZihao Yu    val freq = RegInit(clk.U(16.W))
405fd0e682SLinJiawei    val inc = RegInit(1000.U(16.W))
41ac65130dSZihao Yu
42ac65130dSZihao Yu    val cnt = RegInit(0.U(16.W))
43ac65130dSZihao Yu    val nextCnt = cnt + 1.U
44ac65130dSZihao Yu    cnt := Mux(nextCnt < freq, nextCnt, 0.U)
45ac65130dSZihao Yu    val tick = (nextCnt === freq)
46ac65130dSZihao Yu    when (tick) { mtime := mtime + inc }
47891d22aaSZihao Yu
48891d22aaSZihao Yu    val mapping = Map(
49434b30e4SZihao Yu      RegMap(0x4000, mtimecmp),
50ac65130dSZihao Yu      RegMap(0x8000, freq),
51ac65130dSZihao Yu      RegMap(0x8008, inc),
52434b30e4SZihao Yu      RegMap(0xbff8, mtime)
53891d22aaSZihao Yu    )
54434b30e4SZihao Yu    def getOffset(addr: UInt) = addr(15,0)
55891d22aaSZihao Yu
56434b30e4SZihao Yu    RegMap.generate(mapping, getOffset(raddr), in.r.bits.data,
57434b30e4SZihao Yu      getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb))
58891d22aaSZihao Yu
594c8d1f11SZihao Yu    io.extra.get.mtip := RegNext(mtime >= mtimecmp)
6084226e46SZihao Yu  }
6163934268Slinjiawei}
62