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// } 51d4aca96cSlqre 52d4aca96cSlqre lazy val module = new LazyRawModuleImp(this) { 53d4aca96cSlqre val io = IO(new Bundle{ 54d4aca96cSlqre val resetCtrl = new ResetCtrlIO(numCores)(p) 55d4aca96cSlqre val debugIO = new DebugIO()(p) 56d4aca96cSlqre val clock = Input(Bool()) 57*67ba96b4SYinan Xu val reset = Input(Reset()) 58d4aca96cSlqre }) 59d4aca96cSlqre debug.module.io.tl_reset := io.reset // this should be TL reset 60d4aca96cSlqre debug.module.io.tl_clock := io.clock.asClock // this should be TL clock 61d4aca96cSlqre debug.module.io.hartIsInReset := io.resetCtrl.hartIsInReset 62d4aca96cSlqre io.resetCtrl.hartResetReq.foreach { rcio => debug.module.io.hartResetReq.foreach { rcdm => rcio := rcdm }} 63d4aca96cSlqre 64d4aca96cSlqre io.debugIO.clockeddmi.foreach { dbg => debug.module.io.dmi.get <> dbg } // not connected in current case since we use dtm 65d4aca96cSlqre debug.module.io.debug_reset := io.debugIO.reset 66d4aca96cSlqre debug.module.io.debug_clock := io.debugIO.clock 67d4aca96cSlqre io.debugIO.ndreset := debug.module.io.ctrl.ndreset 68d4aca96cSlqre io.debugIO.dmactive := debug.module.io.ctrl.dmactive 69d4aca96cSlqre debug.module.io.ctrl.dmactiveAck := io.debugIO.dmactiveAck 70d4aca96cSlqre io.debugIO.extTrigger.foreach { x => debug.module.io.extTrigger.foreach {y => x <> y}} 71d4aca96cSlqre debug.module.io.ctrl.debugUnavail.foreach { _ := false.B } 72d4aca96cSlqre 73d4aca96cSlqre val dtm = io.debugIO.systemjtag.map(instantiateJtagDTM(_)) 74d4aca96cSlqre 75d4aca96cSlqre def instantiateJtagDTM(sj: SystemJTAGIO): DebugTransportModuleJTAG = { 76d4aca96cSlqre val c = new JtagDTMKeyDefault 77d4aca96cSlqre val dtm = Module(new DebugTransportModuleJTAG(p(DebugModuleKey).get.nDMIAddrSize, c)) 78d4aca96cSlqre dtm.io.jtag <> sj.jtag 79d4aca96cSlqre 80d4aca96cSlqre io.debugIO.disableDebug.foreach { x => dtm.io.jtag.TMS := sj.jtag.TMS | x } // force TMS high when debug is disabled 81d4aca96cSlqre 82d4aca96cSlqre dtm.io.jtag_clock := sj.jtag.TCK 83d4aca96cSlqre dtm.io.jtag_reset := sj.reset 84d4aca96cSlqre dtm.io.jtag_mfr_id := sj.mfr_id 85d4aca96cSlqre dtm.io.jtag_part_number := sj.part_number 86d4aca96cSlqre dtm.io.jtag_version := sj.version 87d4aca96cSlqre dtm.rf_reset := sj.reset 88d4aca96cSlqre debug.module.io.dmi.get.dmi <> dtm.io.dmi 89d4aca96cSlqre debug.module.io.dmi.get.dmiClock := sj.jtag.TCK 90d4aca96cSlqre debug.module.io.dmi.get.dmiReset := sj.reset 91d4aca96cSlqre dtm 92d4aca96cSlqre } 93d4aca96cSlqre } 94d4aca96cSlqre} 95d4aca96cSlqre 96d4aca96cSlqreobject XSDebugModuleParams { 97d4aca96cSlqre 98d4aca96cSlqre def apply(xlen:Int /*TODO , val configStringAddr: Int*/): DebugModuleParams = { 99d4aca96cSlqre new DebugModuleParams().copy( 100d4aca96cSlqre nAbstractDataWords = (if (xlen == 32) 1 else if (xlen == 64) 2 else 4), 101d4aca96cSlqre maxSupportedSBAccess = xlen, 102d4aca96cSlqre hasBusMaster = true, 103d4aca96cSlqre baseAddress = BigInt(0x38020000), 104d4aca96cSlqre nScratch = 2 105d4aca96cSlqre ) 106d4aca96cSlqre } 107d4aca96cSlqre} 108d4aca96cSlqre 109d4aca96cSlqrecase object EnableJtag extends Field[Bool] 110d4aca96cSlqre 111510ae4eeSJiuyang Liuclass SimJTAG(tickDelay: Int = 50)(implicit val p: Parameters) extends ExtModule(Map("TICK_DELAY" -> IntParam(tickDelay))) 112510ae4eeSJiuyang Liu with HasExtModuleResource { 113d4aca96cSlqre 114510ae4eeSJiuyang Liu val clock = IO(Input(Clock())) 115*67ba96b4SYinan Xu val reset = IO(Input(Reset())) 116510ae4eeSJiuyang Liu val jtag = IO(new JTAGIO(hasTRSTn = true)) 117510ae4eeSJiuyang Liu val enable = IO(Input(Bool())) 118510ae4eeSJiuyang Liu val init_done = IO(Input(Bool())) 119510ae4eeSJiuyang Liu val exit = IO(Output(UInt(32.W))) 120d4aca96cSlqre 121*67ba96b4SYinan Xu def connect(dutio: JTAGIO, tbclock: Clock, tbreset: Reset, done: Bool, tbsuccess: Bool) = { 122510ae4eeSJiuyang Liu dutio.TCK := jtag.TCK 123510ae4eeSJiuyang Liu dutio.TMS := jtag.TMS 124510ae4eeSJiuyang Liu dutio.TDI := jtag.TDI 125510ae4eeSJiuyang Liu jtag.TDO := dutio.TDO 126d4aca96cSlqre 127510ae4eeSJiuyang Liu clock := tbclock 128510ae4eeSJiuyang Liu reset := tbreset 129510ae4eeSJiuyang Liu 130510ae4eeSJiuyang Liu enable := p(EnableJtag) 131510ae4eeSJiuyang Liu init_done := done 132d4aca96cSlqre 133d4aca96cSlqre // Success is determined by the gdbserver 134d4aca96cSlqre // which is controlling this simulation. 135510ae4eeSJiuyang Liu tbsuccess := exit === 1.U 136510ae4eeSJiuyang Liu when (exit >= 2.U) { 137510ae4eeSJiuyang Liu printf("*** FAILED *** (exit code = %d)\n", exit >> 1.U) 138d4aca96cSlqre stop(1) 139d4aca96cSlqre } 140d4aca96cSlqre } 141d4aca96cSlqre} 142