xref: /XiangShan/src/main/scala/device/RocketDebugWrapper.scala (revision 935edac446654a1880ac0112b2380315b5368504)
1d4aca96cSlqre/***************************************************************************************
2d4aca96cSlqre* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3d4aca96cSlqre* Copyright (c) 2020-2021 Peng Cheng Laboratory
4d4aca96cSlqre*
5d4aca96cSlqre* XiangShan is licensed under Mulan PSL v2.
6d4aca96cSlqre* You can use this software according to the terms and conditions of the Mulan PSL v2.
7d4aca96cSlqre* You may obtain a copy of Mulan PSL v2 at:
8d4aca96cSlqre*          http://license.coscl.org.cn/MulanPSL2
9d4aca96cSlqre*
10d4aca96cSlqre* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11d4aca96cSlqre* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12d4aca96cSlqre* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13d4aca96cSlqre*
14d4aca96cSlqre* See the Mulan PSL v2 for more details.
15d4aca96cSlqre***************************************************************************************/
16d4aca96cSlqre
17d4aca96cSlqrepackage device
18d4aca96cSlqre
19d4aca96cSlqreimport chisel3._
20d4aca96cSlqreimport xiangshan._
21510ae4eeSJiuyang Liuimport chisel3.experimental.{ExtModule, IntParam, noPrefix}
22d4aca96cSlqreimport chisel3.util._
23510ae4eeSJiuyang Liuimport chisel3.util.HasExtModuleResource
24d4aca96cSlqreimport freechips.rocketchip.config.{Field, Parameters}
25d4aca96cSlqreimport freechips.rocketchip.subsystem._
26d4aca96cSlqreimport freechips.rocketchip.amba.apb._
27d4aca96cSlqreimport freechips.rocketchip.diplomacy._
28d4aca96cSlqreimport freechips.rocketchip.jtag._
29d4aca96cSlqreimport freechips.rocketchip.util._
30510ae4eeSJiuyang Liuimport freechips.rocketchip.prci.{ClockSinkNode, ClockSinkParameters}
31d4aca96cSlqreimport freechips.rocketchip.tilelink._
32510ae4eeSJiuyang Liuimport freechips.rocketchip.devices.debug.{DebugCustomXbar, DebugIO, DebugTransportModuleJTAG, JtagDTMConfig, PSDIO, ResetCtrlIO, SystemJTAGIO, TLDebugModule}
33d4aca96cSlqreimport freechips.rocketchip.devices.debug._
34d4aca96cSlqre
35d4aca96cSlqre// this file uses code from rocketchip Periphery.scala
36d4aca96cSlqre// to simplify the code we remove options for apb, cjtag and dmi
37d4aca96cSlqre// this module creates wrapped dm and dtm
38d4aca96cSlqre
39bc63e578SLi Qianruo
40d4aca96cSlqreclass DebugModule(numCores: Int)(implicit p: Parameters) extends LazyModule {
41d4aca96cSlqre
42d4aca96cSlqre  val debug = LazyModule(new TLDebugModule(8)(p))
43d4aca96cSlqre
44d4aca96cSlqre//  debug.node := TLFragmenter() := peripheralXbar
45d4aca96cSlqre  val debugCustomXbarOpt = p(DebugModuleKey).map(params => LazyModule( new DebugCustomXbar(outputRequiresInput = false)))
46d4aca96cSlqre  debug.dmInner.dmInner.customNode := debugCustomXbarOpt.get.node
47d4aca96cSlqre
48d4aca96cSlqre//  debug.dmInner.dmInner.sb2tlOpt.foreach { sb2tl  =>
49d4aca96cSlqre//    l2xbar := TLBuffer() := TLWidthWidget(1) := sb2tl.node
50d4aca96cSlqre//  }
51*935edac4STang Haojin  class DebugModuleIO extends Bundle {
52d4aca96cSlqre    val resetCtrl = new ResetCtrlIO(numCores)(p)
53d4aca96cSlqre    val debugIO = new DebugIO()(p)
54d4aca96cSlqre    val clock = Input(Bool())
5567ba96b4SYinan Xu    val reset = Input(Reset())
56*935edac4STang Haojin  }
57*935edac4STang Haojin
58*935edac4STang Haojin  class DebugModuleImp(wrapper: LazyModule) extends LazyRawModuleImp(wrapper) {
59*935edac4STang Haojin    val io = IO(new DebugModuleIO)
60d4aca96cSlqre    debug.module.io.tl_reset := io.reset // this should be TL reset
61d4aca96cSlqre    debug.module.io.tl_clock := io.clock.asClock // this should be TL clock
62d4aca96cSlqre    debug.module.io.hartIsInReset := io.resetCtrl.hartIsInReset
63d4aca96cSlqre    io.resetCtrl.hartResetReq.foreach { rcio => debug.module.io.hartResetReq.foreach { rcdm => rcio := rcdm }}
64d4aca96cSlqre
65d4aca96cSlqre    io.debugIO.clockeddmi.foreach { dbg => debug.module.io.dmi.get <> dbg } // not connected in current case since we use dtm
66d4aca96cSlqre    debug.module.io.debug_reset := io.debugIO.reset
67d4aca96cSlqre    debug.module.io.debug_clock := io.debugIO.clock
68d4aca96cSlqre    io.debugIO.ndreset := debug.module.io.ctrl.ndreset
69d4aca96cSlqre    io.debugIO.dmactive := debug.module.io.ctrl.dmactive
70d4aca96cSlqre    debug.module.io.ctrl.dmactiveAck := io.debugIO.dmactiveAck
71d4aca96cSlqre    io.debugIO.extTrigger.foreach { x => debug.module.io.extTrigger.foreach {y => x <> y}}
72d4aca96cSlqre    debug.module.io.ctrl.debugUnavail.foreach { _ := false.B }
73d4aca96cSlqre
74d4aca96cSlqre    val dtm = io.debugIO.systemjtag.map(instantiateJtagDTM(_))
75d4aca96cSlqre
76d4aca96cSlqre    def instantiateJtagDTM(sj: SystemJTAGIO): DebugTransportModuleJTAG = {
77d4aca96cSlqre      val c = new JtagDTMKeyDefault
78d4aca96cSlqre      val dtm = Module(new DebugTransportModuleJTAG(p(DebugModuleKey).get.nDMIAddrSize, c))
79d4aca96cSlqre      dtm.io.jtag <> sj.jtag
80d4aca96cSlqre
81d4aca96cSlqre      io.debugIO.disableDebug.foreach { x => dtm.io.jtag.TMS := sj.jtag.TMS | x }  // force TMS high when debug is disabled
82d4aca96cSlqre
83d4aca96cSlqre      dtm.io.jtag_clock  := sj.jtag.TCK
84d4aca96cSlqre      dtm.io.jtag_reset  := sj.reset
85d4aca96cSlqre      dtm.io.jtag_mfr_id := sj.mfr_id
86d4aca96cSlqre      dtm.io.jtag_part_number := sj.part_number
87d4aca96cSlqre      dtm.io.jtag_version := sj.version
88d4aca96cSlqre      dtm.rf_reset := sj.reset
89d4aca96cSlqre      debug.module.io.dmi.get.dmi <> dtm.io.dmi
90d4aca96cSlqre      debug.module.io.dmi.get.dmiClock := sj.jtag.TCK
91d4aca96cSlqre      debug.module.io.dmi.get.dmiReset := sj.reset
92d4aca96cSlqre      dtm
93d4aca96cSlqre    }
94d4aca96cSlqre  }
95*935edac4STang Haojin
96*935edac4STang Haojin  lazy val module = new DebugModuleImp(this)
97d4aca96cSlqre}
98d4aca96cSlqre
99d4aca96cSlqreobject XSDebugModuleParams {
100d4aca96cSlqre
101d4aca96cSlqre  def apply(xlen:Int /*TODO , val configStringAddr: Int*/): DebugModuleParams = {
102d4aca96cSlqre    new DebugModuleParams().copy(
103d4aca96cSlqre      nAbstractDataWords   = (if (xlen == 32) 1 else if (xlen == 64) 2 else 4),
104d4aca96cSlqre      maxSupportedSBAccess = xlen,
105d4aca96cSlqre      hasBusMaster = true,
106d4aca96cSlqre      baseAddress = BigInt(0x38020000),
107d4aca96cSlqre      nScratch = 2
108d4aca96cSlqre    )
109d4aca96cSlqre  }
110d4aca96cSlqre}
111d4aca96cSlqre
112d4aca96cSlqrecase object EnableJtag extends Field[Bool]
113d4aca96cSlqre
114510ae4eeSJiuyang Liuclass SimJTAG(tickDelay: Int = 50)(implicit val p: Parameters) extends ExtModule(Map("TICK_DELAY" -> IntParam(tickDelay)))
115510ae4eeSJiuyang Liu  with HasExtModuleResource {
116d4aca96cSlqre
117510ae4eeSJiuyang Liu  val clock = IO(Input(Clock()))
11867ba96b4SYinan Xu  val reset = IO(Input(Reset()))
119510ae4eeSJiuyang Liu  val jtag = IO(new JTAGIO(hasTRSTn = true))
120510ae4eeSJiuyang Liu  val enable = IO(Input(Bool()))
121510ae4eeSJiuyang Liu  val init_done = IO(Input(Bool()))
122510ae4eeSJiuyang Liu  val exit = IO(Output(UInt(32.W)))
123d4aca96cSlqre
12467ba96b4SYinan Xu  def connect(dutio: JTAGIO, tbclock: Clock, tbreset: Reset, done: Bool, tbsuccess: Bool) = {
125510ae4eeSJiuyang Liu    dutio.TCK := jtag.TCK
126510ae4eeSJiuyang Liu    dutio.TMS := jtag.TMS
127510ae4eeSJiuyang Liu    dutio.TDI := jtag.TDI
128510ae4eeSJiuyang Liu    jtag.TDO := dutio.TDO
129d4aca96cSlqre
130510ae4eeSJiuyang Liu    clock := tbclock
131510ae4eeSJiuyang Liu    reset := tbreset
132510ae4eeSJiuyang Liu
133510ae4eeSJiuyang Liu    enable    := p(EnableJtag)
134510ae4eeSJiuyang Liu    init_done := done
135d4aca96cSlqre
136d4aca96cSlqre    // Success is determined by the gdbserver
137d4aca96cSlqre    // which is controlling this simulation.
138510ae4eeSJiuyang Liu    tbsuccess := exit === 1.U
139510ae4eeSJiuyang Liu    when (exit >= 2.U) {
140510ae4eeSJiuyang Liu      printf("*** FAILED *** (exit code = %d)\n", exit >> 1.U)
141*935edac4STang Haojin      stop()
142d4aca96cSlqre    }
143d4aca96cSlqre  }
144d4aca96cSlqre}
145