xref: /XiangShan/src/main/scala/device/AXI4Timer.scala (revision f320e0f01bd645f0a3045a8a740e60dd770734a9)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3*f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
4c6d43980SLemover*
5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2.
6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at:
8c6d43980SLemover*          http://license.coscl.org.cn/MulanPSL2
9c6d43980SLemover*
10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13c6d43980SLemover*
14c6d43980SLemover* See the Mulan PSL v2 for more details.
15c6d43980SLemover***************************************************************************************/
16c6d43980SLemover
1784226e46SZihao Yupackage device
1884226e46SZihao Yu
1984226e46SZihao Yuimport chisel3._
2063934268Slinjiaweiimport chipsalliance.rocketchip.config.Parameters
2163934268Slinjiaweiimport freechips.rocketchip.diplomacy.AddressSet
22f10a0bcbSZihao Yuimport utils._
2384226e46SZihao Yu
24891d22aaSZihao Yuclass TimerIO extends Bundle {
25891d22aaSZihao Yu  val mtip = Output(Bool())
26891d22aaSZihao Yu}
27891d22aaSZihao Yu
2863934268Slinjiaweiclass AXI4Timer
2963934268Slinjiawei(
3063934268Slinjiawei  sim: Boolean = false,
31a2e9bde6SAllen  address: Seq[AddressSet]
3263934268Slinjiawei)(implicit p: Parameters)
3363934268Slinjiawei  extends AXI4SlaveModule(address, executable = false, _extra = new TimerIO)
3463934268Slinjiawei{
3563934268Slinjiawei  override lazy val module = new AXI4SlaveModuleImp[TimerIO](this){
3687557494SZihao Yu    val mtime = RegInit(0.U(64.W))  // unit: us
37891d22aaSZihao Yu    val mtimecmp = RegInit(0.U(64.W))
38891d22aaSZihao Yu
3987557494SZihao Yu    val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000)
40ac65130dSZihao Yu    val freq = RegInit(clk.U(16.W))
415fd0e682SLinJiawei    val inc = RegInit(1000.U(16.W))
42ac65130dSZihao Yu
43ac65130dSZihao Yu    val cnt = RegInit(0.U(16.W))
44ac65130dSZihao Yu    val nextCnt = cnt + 1.U
45ac65130dSZihao Yu    cnt := Mux(nextCnt < freq, nextCnt, 0.U)
46ac65130dSZihao Yu    val tick = (nextCnt === freq)
47ac65130dSZihao Yu    when (tick) { mtime := mtime + inc }
48891d22aaSZihao Yu
49891d22aaSZihao Yu    val mapping = Map(
50434b30e4SZihao Yu      RegMap(0x4000, mtimecmp),
51ac65130dSZihao Yu      RegMap(0x8000, freq),
52ac65130dSZihao Yu      RegMap(0x8008, inc),
53434b30e4SZihao Yu      RegMap(0xbff8, mtime)
54891d22aaSZihao Yu    )
55434b30e4SZihao Yu    def getOffset(addr: UInt) = addr(15,0)
56891d22aaSZihao Yu
57434b30e4SZihao Yu    RegMap.generate(mapping, getOffset(raddr), in.r.bits.data,
58434b30e4SZihao Yu      getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb))
59891d22aaSZihao Yu
604c8d1f11SZihao Yu    io.extra.get.mtip := RegNext(mtime >= mtimecmp)
6184226e46SZihao Yu  }
6263934268Slinjiawei}
63