xref: /XiangShan/src/main/scala/device/RocketDebugWrapper.scala (revision 20957846171de148318edbfc667a5748881f7e03)
1d4aca96cSlqre/***************************************************************************************
23a520554STang Haojin* Copyright (c) 2021-2025 Beijing Institute of Open Source Chip (BOSC)
33a520554STang Haojin* Copyright (c) 2020-2025 Institute of Computing Technology, Chinese Academy of Sciences
4d4aca96cSlqre* Copyright (c) 2020-2021 Peng Cheng Laboratory
5d4aca96cSlqre*
6d4aca96cSlqre* XiangShan is licensed under Mulan PSL v2.
7d4aca96cSlqre* You can use this software according to the terms and conditions of the Mulan PSL v2.
8d4aca96cSlqre* You may obtain a copy of Mulan PSL v2 at:
9d4aca96cSlqre*          http://license.coscl.org.cn/MulanPSL2
10d4aca96cSlqre*
11d4aca96cSlqre* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12d4aca96cSlqre* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13d4aca96cSlqre* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14d4aca96cSlqre*
15d4aca96cSlqre* See the Mulan PSL v2 for more details.
16d4aca96cSlqre***************************************************************************************/
17d4aca96cSlqre
18d4aca96cSlqrepackage device
19d4aca96cSlqre
20d4aca96cSlqreimport chisel3._
21d4aca96cSlqreimport xiangshan._
22510ae4eeSJiuyang Liuimport chisel3.experimental.{ExtModule, IntParam, noPrefix}
23d4aca96cSlqreimport chisel3.util._
24510ae4eeSJiuyang Liuimport chisel3.util.HasExtModuleResource
258891a219SYinan Xuimport org.chipsalliance.cde.config.{Field, Parameters}
26d4aca96cSlqreimport freechips.rocketchip.subsystem._
27d4aca96cSlqreimport freechips.rocketchip.amba.apb._
28d4aca96cSlqreimport freechips.rocketchip.diplomacy._
29d4aca96cSlqreimport freechips.rocketchip.jtag._
30d4aca96cSlqreimport freechips.rocketchip.util._
31510ae4eeSJiuyang Liuimport freechips.rocketchip.prci.{ClockSinkNode, ClockSinkParameters}
32d4aca96cSlqreimport freechips.rocketchip.tilelink._
33510ae4eeSJiuyang Liuimport freechips.rocketchip.devices.debug.{DebugCustomXbar, DebugIO, DebugTransportModuleJTAG, JtagDTMConfig, PSDIO, ResetCtrlIO, SystemJTAGIO, TLDebugModule}
34d4aca96cSlqreimport freechips.rocketchip.devices.debug._
35d4aca96cSlqre
36d4aca96cSlqre// this file uses code from rocketchip Periphery.scala
37d4aca96cSlqre// to simplify the code we remove options for apb, cjtag and dmi
38d4aca96cSlqre// this module creates wrapped dm and dtm
39d4aca96cSlqre
40bc63e578SLi Qianruo
41d4aca96cSlqreclass DebugModule(numCores: Int)(implicit p: Parameters) extends LazyModule {
42d4aca96cSlqre
43d4aca96cSlqre  val debug = LazyModule(new TLDebugModule(8)(p))
44d4aca96cSlqre
45d4aca96cSlqre//  debug.node := TLFragmenter() := peripheralXbar
46d4aca96cSlqre  val debugCustomXbarOpt = p(DebugModuleKey).map(params => LazyModule( new DebugCustomXbar(outputRequiresInput = false)))
47d4aca96cSlqre  debug.dmInner.dmInner.customNode := debugCustomXbarOpt.get.node
48d4aca96cSlqre
49d4aca96cSlqre//  debug.dmInner.dmInner.sb2tlOpt.foreach { sb2tl  =>
50d4aca96cSlqre//    l2xbar := TLBuffer() := TLWidthWidget(1) := sb2tl.node
51d4aca96cSlqre//  }
52935edac4STang Haojin  class DebugModuleIO extends Bundle {
53d4aca96cSlqre    val resetCtrl = new ResetCtrlIO(numCores)(p)
54d4aca96cSlqre    val debugIO = new DebugIO()(p)
55*20957846SZihao Yu    val clock = Input(Clock())
5667ba96b4SYinan Xu    val reset = Input(Reset())
57935edac4STang Haojin  }
58935edac4STang Haojin
59935edac4STang Haojin  class DebugModuleImp(wrapper: LazyModule) extends LazyRawModuleImp(wrapper) {
60935edac4STang Haojin    val io = IO(new DebugModuleIO)
61d4aca96cSlqre    debug.module.io.tl_reset := io.reset // this should be TL reset
62*20957846SZihao Yu    debug.module.io.tl_clock := io.clock // this should be TL clock
63*20957846SZihao Yu    withClock(io.clock) {
64f7af4c74Schengguanghui      debug.module.io.hartIsInReset := RegNext(io.resetCtrl.hartIsInReset)
65f7af4c74Schengguanghui    }
66d4aca96cSlqre    io.resetCtrl.hartResetReq.foreach { rcio => debug.module.io.hartResetReq.foreach { rcdm => rcio := rcdm }}
67d4aca96cSlqre
68d4aca96cSlqre    io.debugIO.clockeddmi.foreach { dbg => debug.module.io.dmi.get <> dbg } // not connected in current case since we use dtm
69d4aca96cSlqre    debug.module.io.debug_reset := io.debugIO.reset
70d4aca96cSlqre    debug.module.io.debug_clock := io.debugIO.clock
71d4aca96cSlqre    io.debugIO.ndreset := debug.module.io.ctrl.ndreset
72d4aca96cSlqre    io.debugIO.dmactive := debug.module.io.ctrl.dmactive
73d4aca96cSlqre    debug.module.io.ctrl.dmactiveAck := io.debugIO.dmactiveAck
74d4aca96cSlqre    io.debugIO.extTrigger.foreach { x => debug.module.io.extTrigger.foreach {y => x <> y}}
75d4aca96cSlqre    debug.module.io.ctrl.debugUnavail.foreach { _ := false.B }
76d4aca96cSlqre
77d4aca96cSlqre    val dtm = io.debugIO.systemjtag.map(instantiateJtagDTM(_))
78d4aca96cSlqre
79d4aca96cSlqre    def instantiateJtagDTM(sj: SystemJTAGIO): DebugTransportModuleJTAG = {
80d4aca96cSlqre      val c = new JtagDTMKeyDefault
81d4aca96cSlqre      val dtm = Module(new DebugTransportModuleJTAG(p(DebugModuleKey).get.nDMIAddrSize, c))
82d4aca96cSlqre      dtm.io.jtag <> sj.jtag
83d4aca96cSlqre
84d4aca96cSlqre      io.debugIO.disableDebug.foreach { x => dtm.io.jtag.TMS := sj.jtag.TMS | x }  // force TMS high when debug is disabled
85d4aca96cSlqre
86d4aca96cSlqre      dtm.io.jtag_clock  := sj.jtag.TCK
87d4aca96cSlqre      dtm.io.jtag_reset  := sj.reset
88d4aca96cSlqre      dtm.io.jtag_mfr_id := sj.mfr_id
89d4aca96cSlqre      dtm.io.jtag_part_number := sj.part_number
90d4aca96cSlqre      dtm.io.jtag_version := sj.version
91d4aca96cSlqre      dtm.rf_reset := sj.reset
92d4aca96cSlqre      debug.module.io.dmi.get.dmi <> dtm.io.dmi
93d4aca96cSlqre      debug.module.io.dmi.get.dmiClock := sj.jtag.TCK
94d4aca96cSlqre      debug.module.io.dmi.get.dmiReset := sj.reset
95d4aca96cSlqre      dtm
96d4aca96cSlqre    }
97d4aca96cSlqre  }
98935edac4STang Haojin
99935edac4STang Haojin  lazy val module = new DebugModuleImp(this)
100d4aca96cSlqre}
101d4aca96cSlqre
102d4aca96cSlqrecase object EnableJtag extends Field[Bool]
103d4aca96cSlqre
104510ae4eeSJiuyang Liuclass SimJTAG(tickDelay: Int = 50)(implicit val p: Parameters) extends ExtModule(Map("TICK_DELAY" -> IntParam(tickDelay)))
105510ae4eeSJiuyang Liu  with HasExtModuleResource {
106d4aca96cSlqre
107510ae4eeSJiuyang Liu  val clock = IO(Input(Clock()))
10867ba96b4SYinan Xu  val reset = IO(Input(Reset()))
109510ae4eeSJiuyang Liu  val jtag = IO(new JTAGIO(hasTRSTn = true))
110510ae4eeSJiuyang Liu  val enable = IO(Input(Bool()))
111510ae4eeSJiuyang Liu  val init_done = IO(Input(Bool()))
112510ae4eeSJiuyang Liu  val exit = IO(Output(UInt(32.W)))
113d4aca96cSlqre
11467ba96b4SYinan Xu  def connect(dutio: JTAGIO, tbclock: Clock, tbreset: Reset, done: Bool, tbsuccess: Bool) = {
115f7af4c74Schengguanghui    if (!dutio.TRSTn.isEmpty) {
116f7af4c74Schengguanghui      dutio.TRSTn.get := jtag.TRSTn.getOrElse(false.B) || !tbreset.asBool
117f7af4c74Schengguanghui    }
118510ae4eeSJiuyang Liu    dutio.TCK := jtag.TCK
119510ae4eeSJiuyang Liu    dutio.TMS := jtag.TMS
120510ae4eeSJiuyang Liu    dutio.TDI := jtag.TDI
121510ae4eeSJiuyang Liu    jtag.TDO := dutio.TDO
122d4aca96cSlqre
123510ae4eeSJiuyang Liu    clock := tbclock
124510ae4eeSJiuyang Liu    reset := tbreset
125510ae4eeSJiuyang Liu
126510ae4eeSJiuyang Liu    enable    := p(EnableJtag)
127510ae4eeSJiuyang Liu    init_done := done
128d4aca96cSlqre
129d4aca96cSlqre    // Success is determined by the gdbserver
130d4aca96cSlqre    // which is controlling this simulation.
131510ae4eeSJiuyang Liu    tbsuccess := exit === 1.U
132510ae4eeSJiuyang Liu    when (exit >= 2.U) {
133510ae4eeSJiuyang Liu      printf("*** FAILED *** (exit code = %d)\n", exit >> 1.U)
134935edac4STang Haojin      stop()
135d4aca96cSlqre    }
136d4aca96cSlqre  }
137d4aca96cSlqre}
138