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