xref: /XiangShan/src/main/scala/device/AXI4Timer.scala (revision 8891a219bbc84f568e1d134854d8d5ed86d6d560)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan 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._
20*8891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
2163934268Slinjiaweiimport freechips.rocketchip.diplomacy.AddressSet
22f10a0bcbSZihao Yuimport utils._
233c02ee8fSwakafaimport utility._
2484226e46SZihao Yu
25891d22aaSZihao Yuclass TimerIO extends Bundle {
26891d22aaSZihao Yu  val mtip = Output(Bool())
27891d22aaSZihao Yu}
28891d22aaSZihao Yu
2963934268Slinjiaweiclass AXI4Timer
3063934268Slinjiawei(
3163934268Slinjiawei  sim: Boolean = false,
32a2e9bde6SAllen  address: Seq[AddressSet]
3363934268Slinjiawei)(implicit p: Parameters)
3463934268Slinjiawei  extends AXI4SlaveModule(address, executable = false, _extra = new TimerIO)
3563934268Slinjiawei{
3663934268Slinjiawei  override lazy val module = new AXI4SlaveModuleImp[TimerIO](this){
3787557494SZihao Yu    val mtime = RegInit(0.U(64.W))  // unit: us
38891d22aaSZihao Yu    val mtimecmp = RegInit(0.U(64.W))
39891d22aaSZihao Yu
4087557494SZihao Yu    val clk = (if (!sim) 40 /* 40MHz / 1000000 */ else 10000)
41ac65130dSZihao Yu    val freq = RegInit(clk.U(16.W))
425fd0e682SLinJiawei    val inc = RegInit(1000.U(16.W))
43ac65130dSZihao Yu
44ac65130dSZihao Yu    val cnt = RegInit(0.U(16.W))
45ac65130dSZihao Yu    val nextCnt = cnt + 1.U
46ac65130dSZihao Yu    cnt := Mux(nextCnt < freq, nextCnt, 0.U)
47ac65130dSZihao Yu    val tick = (nextCnt === freq)
48ac65130dSZihao Yu    when (tick) { mtime := mtime + inc }
49891d22aaSZihao Yu
50891d22aaSZihao Yu    val mapping = Map(
51434b30e4SZihao Yu      RegMap(0x4000, mtimecmp),
52ac65130dSZihao Yu      RegMap(0x8000, freq),
53ac65130dSZihao Yu      RegMap(0x8008, inc),
54434b30e4SZihao Yu      RegMap(0xbff8, mtime)
55891d22aaSZihao Yu    )
56434b30e4SZihao Yu    def getOffset(addr: UInt) = addr(15,0)
57891d22aaSZihao Yu
58434b30e4SZihao Yu    RegMap.generate(mapping, getOffset(raddr), in.r.bits.data,
59935edac4STang Haojin      getOffset(waddr), in.w.fire, in.w.bits.data, MaskExpand(in.w.bits.strb))
60891d22aaSZihao Yu
614c8d1f11SZihao Yu    io.extra.get.mtip := RegNext(mtime >= mtimecmp)
6284226e46SZihao Yu  }
6363934268Slinjiawei}
64