1c6d43980SLemover/*************************************************************************************** 22993c5ecSHaojin Tang* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 32993c5ecSHaojin Tang* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 5c6d43980SLemover* 6c6d43980SLemover* XiangShan is licensed under Mulan PSL v2. 7c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 8c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at: 9c6d43980SLemover* http://license.coscl.org.cn/MulanPSL2 10c6d43980SLemover* 11c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14c6d43980SLemover* 15c6d43980SLemover* See the Mulan PSL v2 for more details. 16c6d43980SLemover***************************************************************************************/ 17c6d43980SLemover 188b037849SYinan Xupackage top 198b037849SYinan Xu 208b037849SYinan Xuimport chisel3._ 218b037849SYinan Xuimport chisel3.util._ 222993c5ecSHaojin Tangimport chisel3.experimental.dataview._ 232316cea8SJiuyue Maimport difftest.DifftestModule 248b037849SYinan Xuimport xiangshan._ 2594c92d92SYinan Xuimport utils._ 269672f0b7Swakafaimport huancun.{HCCacheParameters, HCCacheParamsKey, HuanCun, PrefetchRecv, TPmetaResp} 274b40434cSzhanglinjuanimport coupledL2.EnableCHI 285c060727Ssumailyycimport coupledL2.tl2chi.CHILogger 295c060727Ssumailyycimport openLLC.{OpenLLC, OpenLLCParamKey, OpenNCB} 305c060727Ssumailyycimport openLLC.TargetBinder._ 315c060727Ssumailyycimport cc.xiangshan.openncb._ 323c02ee8fSwakafaimport utility._ 338b037849SYinan Xuimport system._ 34d4aca96cSlqreimport device._ 358b037849SYinan Xuimport chisel3.stage.ChiselGeneratorAnnotation 368891a219SYinan Xuimport org.chipsalliance.cde.config._ 378b037849SYinan Xuimport freechips.rocketchip.diplomacy._ 384daa5bf3SYangyu Chenimport freechips.rocketchip.tile._ 398b037849SYinan Xuimport freechips.rocketchip.tilelink._ 408bc90631SZehao Liuimport freechips.rocketchip.interrupts._ 414b40434cSzhanglinjuanimport freechips.rocketchip.amba.axi4._ 42d4aca96cSlqreimport freechips.rocketchip.jtag.JTAGIO 43a5b77de4STang Haojinimport chisel3.experimental.{annotate, ChiselAnnotation} 44a5b77de4STang Haojinimport sifive.enterprise.firrtl.NestedPrefixModulesAnnotation 455c060727Ssumailyycimport scala.collection.mutable.{Map} 46d4aca96cSlqre 47afcc4f2aSJiawei Linabstract class BaseXSSoc()(implicit p: Parameters) extends LazyModule 48afcc4f2aSJiawei Lin with BindingScope 49afcc4f2aSJiawei Lin{ 504b40434cSzhanglinjuan // val misc = LazyModule(new SoCMisc()) 51afcc4f2aSJiawei Lin lazy val dts = DTS(bindingTree) 524f0a2459Swakafa lazy val json = JSON(bindingTree) 538b037849SYinan Xu} 548b037849SYinan Xu 5573be64b3SJiawei Linclass XSTop()(implicit p: Parameters) extends BaseXSSoc() with HasSoCParameter 568b037849SYinan Xu{ 574b40434cSzhanglinjuan val nocMisc = if (enableCHI) Some(LazyModule(new MemMisc())) else None 584b40434cSzhanglinjuan val socMisc = if (!enableCHI) Some(LazyModule(new SoCMisc())) else None 594b40434cSzhanglinjuan val misc: MemMisc = if (enableCHI) nocMisc.get else socMisc.get 604b40434cSzhanglinjuan 61afcc4f2aSJiawei Lin ResourceBinding { 62afcc4f2aSJiawei Lin val width = ResourceInt(2) 63ce34d21eSJiuyue Ma val model = "xiangshan," + os.read(os.resource / "publishVersion") 64ce34d21eSJiuyue Ma val compatible = "freechips,rocketchip-unknown" 65afcc4f2aSJiawei Lin Resource(ResourceAnchors.root, "model").bind(ResourceString(model)) 66ce34d21eSJiuyue Ma Resource(ResourceAnchors.root, "compat").bind(ResourceString(compatible + "-dev")) 67ce34d21eSJiuyue Ma Resource(ResourceAnchors.soc, "compat").bind(ResourceString(compatible + "-soc")) 68afcc4f2aSJiawei Lin Resource(ResourceAnchors.root, "width").bind(width) 69afcc4f2aSJiawei Lin Resource(ResourceAnchors.soc, "width").bind(width) 70afcc4f2aSJiawei Lin Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1)) 71afcc4f2aSJiawei Lin def bindManagers(xbar: TLNexusNode) = { 72afcc4f2aSJiawei Lin ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager => 73afcc4f2aSJiawei Lin manager.resources.foreach(r => r.bind(manager.toResource)) 74afcc4f2aSJiawei Lin } 75afcc4f2aSJiawei Lin } 7678a8cd25Szhanglinjuan if (!enableCHI) { 771bf9a05aSzhanglinjuan bindManagers(misc.l3_xbar.get.asInstanceOf[TLNexusNode]) 7878a8cd25Szhanglinjuan bindManagers(misc.peripheralXbar.get.asInstanceOf[TLNexusNode]) 7978a8cd25Szhanglinjuan } 80afcc4f2aSJiawei Lin } 818b037849SYinan Xu 822225d46eSJiawei Lin println(s"FPGASoC cores: $NumCores banks: $L3NBanks block size: $L3BlockSize bus size: $L3OuterBusWidth") 838b037849SYinan Xu 8434ab1ae9SJiawei Lin val core_with_l2 = tiles.map(coreParams => 85bb2f3f51STang Haojin LazyModule(new XSTile()(p.alter((site, here, up) => { 862225d46eSJiawei Lin case XSCoreParamsKey => coreParams 87bb2f3f51STang Haojin case PerfCounterOptionsKey => up(PerfCounterOptionsKey).copy(perfDBHartID = coreParams.HartId) 882225d46eSJiawei Lin }))) 892225d46eSJiawei Lin ) 908b037849SYinan Xu 9134ab1ae9SJiawei Lin val l3cacheOpt = soc.L3CacheParamsOpt.map(l3param => 9234ab1ae9SJiawei Lin LazyModule(new HuanCun()(new Config((_, _, _) => { 9334f38695STang Haojin case HCCacheParamsKey => l3param.copy( 9434f38695STang Haojin hartIds = tiles.map(_.HartId), 9534f38695STang Haojin FPGAPlatform = debugOpts.FPGAPlatform 9634f38695STang Haojin ) 974daa5bf3SYangyu Chen case MaxHartIdBits => p(MaxHartIdBits) 98bb2f3f51STang Haojin case LogUtilsOptionsKey => p(LogUtilsOptionsKey) 99bb2f3f51STang Haojin case PerfCounterOptionsKey => p(PerfCounterOptionsKey) 10034ab1ae9SJiawei Lin }))) 10134ab1ae9SJiawei Lin ) 10234ab1ae9SJiawei Lin 1035c060727Ssumailyyc val chi_llcBridge_opt = Option.when(enableCHI)( 1045c060727Ssumailyyc LazyModule(new OpenNCB()(p.alter((site, here, up) => { 1055c060727Ssumailyyc case NCBParametersKey => new NCBParameters( 106af532009Ssumailyyc outstandingDepth = 64, 1075c060727Ssumailyyc axiMasterOrder = EnumAXIMasterOrder.WriteAddress, 1085c060727Ssumailyyc readCompDMT = false, 1095c060727Ssumailyyc writeCancelable = false, 1105c060727Ssumailyyc writeNoError = true, 111*881e32f5SZifei Zhang axiBurstAlwaysIncr = true, 112*881e32f5SZifei Zhang chiDataCheck = EnumCHIDataCheck.OddParity 1135c060727Ssumailyyc ) 1145c060727Ssumailyyc }))) 1155c060727Ssumailyyc ) 1165c060727Ssumailyyc 1175c060727Ssumailyyc val chi_mmioBridge_opt = Seq.fill(NumCores)(Option.when(enableCHI)( 1185c060727Ssumailyyc LazyModule(new OpenNCB()(p.alter((site, here, up) => { 1195c060727Ssumailyyc case NCBParametersKey => new NCBParameters( 120af532009Ssumailyyc outstandingDepth = 32, 1215c060727Ssumailyyc axiMasterOrder = EnumAXIMasterOrder.None, 1225c060727Ssumailyyc readCompDMT = false, 1235c060727Ssumailyyc writeCancelable = false, 1245c060727Ssumailyyc writeNoError = true, 1255c060727Ssumailyyc asEndpoint = false, 1265c060727Ssumailyyc acceptOrderEndpoint = true, 1275c060727Ssumailyyc acceptMemAttrDevice = true, 1285c060727Ssumailyyc readReceiptAfterAcception = true, 129*881e32f5SZifei Zhang axiBurstAlwaysIncr = true, 130*881e32f5SZifei Zhang chiDataCheck = EnumCHIDataCheck.OddParity 1315c060727Ssumailyyc ) 1325c060727Ssumailyyc }))) 1335c060727Ssumailyyc )) 13478a8cd25Szhanglinjuan 13578a8cd25Szhanglinjuan // receive all prefetch req from cores 1360d32f713Shappy-lx val memblock_pf_recv_nodes: Seq[Option[BundleBridgeSink[PrefetchRecv]]] = core_with_l2.map(_.core_l3_pf_port).map{ 1370d32f713Shappy-lx x => x.map(_ => BundleBridgeSink(Some(() => new PrefetchRecv))) 1380d32f713Shappy-lx } 1390d32f713Shappy-lx 1400d32f713Shappy-lx val l3_pf_sender_opt = soc.L3CacheParamsOpt.getOrElse(HCCacheParameters()).prefetch match { 1410d32f713Shappy-lx case Some(pf) => Some(BundleBridgeSource(() => new PrefetchRecv)) 1420d32f713Shappy-lx case None => None 1430d32f713Shappy-lx } 1448bc90631SZehao Liu val nmiIntNode = IntSourceNode(IntSourcePortSimple(1, NumCores, (new NonmaskableInterruptIO).elements.size)) 1458bc90631SZehao Liu val nmi = InModuleBody(nmiIntNode.makeIOs()) 1460d32f713Shappy-lx 1478b037849SYinan Xu for (i <- 0 until NumCores) { 1484e12f40bSzhanglinjuan core_with_l2(i).clint_int_node := misc.clint.intnode 1494e12f40bSzhanglinjuan core_with_l2(i).plic_int_node :*= misc.plic.intnode 1504e12f40bSzhanglinjuan core_with_l2(i).debug_int_node := misc.debugModule.debug.dmOuter.dmOuter.intnode 1518bc90631SZehao Liu core_with_l2(i).nmi_int_node := nmiIntNode 152cac098b4SJiawei Lin misc.plic.intnode := IntBuffer() := core_with_l2(i).beu_int_source 1534b40434cSzhanglinjuan if (!enableCHI) { 15478a8cd25Szhanglinjuan misc.peripheral_ports.get(i) := core_with_l2(i).tl_uncache 1554b40434cSzhanglinjuan } 15678a8cd25Szhanglinjuan core_with_l2(i).memory_port.foreach(port => (misc.core_to_l3_ports.get)(i) :=* port) 1570d32f713Shappy-lx memblock_pf_recv_nodes(i).map(recv => { 1580d32f713Shappy-lx println(s"Connecting Core_${i}'s L1 pf source to L3!") 1590d32f713Shappy-lx recv := core_with_l2(i).core_l3_pf_port.get 1600d32f713Shappy-lx }) 1618b037849SYinan Xu } 1628b037849SYinan Xu 16378a8cd25Szhanglinjuan l3cacheOpt.map(_.ctlnode.map(_ := misc.peripheralXbar.get)) 16438005240SJiawei Lin l3cacheOpt.map(_.intnode.map(int => { 16538005240SJiawei Lin misc.plic.intnode := IntBuffer() := int 16638005240SJiawei Lin })) 16734ab1ae9SJiawei Lin 16834ab1ae9SJiawei Lin val core_rst_nodes = if(l3cacheOpt.nonEmpty && l3cacheOpt.get.rst_nodes.nonEmpty){ 16934ab1ae9SJiawei Lin l3cacheOpt.get.rst_nodes.get 17034ab1ae9SJiawei Lin } else { 1718a167be7SHaojin Tang core_with_l2.map(_ => BundleBridgeSource(() => Reset())) 17234ab1ae9SJiawei Lin } 17334ab1ae9SJiawei Lin 17434ab1ae9SJiawei Lin core_rst_nodes.zip(core_with_l2.map(_.core_reset_sink)).foreach({ 17534ab1ae9SJiawei Lin case (source, sink) => sink := source 17634ab1ae9SJiawei Lin }) 177a1ea7f76SJiawei Lin 1784f94c0c6SJiawei Lin l3cacheOpt match { 1794f94c0c6SJiawei Lin case Some(l3) => 1801bf9a05aSzhanglinjuan misc.l3_out :*= l3.node :*= misc.l3_banked_xbar.get 1810d32f713Shappy-lx l3.pf_recv_node.map(recv => { 1820d32f713Shappy-lx println("Connecting L1 prefetcher to L3!") 1830d32f713Shappy-lx recv := l3_pf_sender_opt.get 1840d32f713Shappy-lx }) 1859672f0b7Swakafa l3.tpmeta_recv_node.foreach(recv => { 1869672f0b7Swakafa for ((core, i) <- core_with_l2.zipWithIndex) { 1879672f0b7Swakafa println(s"Connecting core_$i\'s L2 TPmeta request to L3!") 1889672f0b7Swakafa recv := core.core_l3_tpmeta_source_port.get 1899672f0b7Swakafa } 1909672f0b7Swakafa }) 1919672f0b7Swakafa l3.tpmeta_send_node.foreach(send => { 1929672f0b7Swakafa val broadcast = LazyModule(new ValidIOBroadcast[TPmetaResp]()) 1939672f0b7Swakafa broadcast.node := send 1949672f0b7Swakafa for ((core, i) <- core_with_l2.zipWithIndex) { 1959672f0b7Swakafa println(s"Connecting core_$i\'s L2 TPmeta response to L3!") 1969672f0b7Swakafa core.core_l3_tpmeta_sink_port.get := broadcast.node 1979672f0b7Swakafa } 1989672f0b7Swakafa }) 19973be64b3SJiawei Lin case None => 2009d5a2027SYinan Xu } 2018b037849SYinan Xu 2025c060727Ssumailyyc chi_llcBridge_opt match { 2035c060727Ssumailyyc case Some(ncb) => 2045c060727Ssumailyyc misc.soc_xbar.get := ncb.axi4node 20578a8cd25Szhanglinjuan case None => 20678a8cd25Szhanglinjuan } 20778a8cd25Szhanglinjuan 2085c060727Ssumailyyc chi_mmioBridge_opt.foreach { e => 2095c060727Ssumailyyc e match { 2105c060727Ssumailyyc case Some(ncb) => 2115c060727Ssumailyyc misc.soc_xbar.get := ncb.axi4node 2125c060727Ssumailyyc case None => 2135c060727Ssumailyyc } 2145c060727Ssumailyyc } 2155c060727Ssumailyyc 216935edac4STang Haojin class XSTopImp(wrapper: LazyModule) extends LazyRawModuleImp(wrapper) { 217a5b77de4STang Haojin soc.XSTopPrefix.foreach { prefix => 218a5b77de4STang Haojin val mod = this.toNamed 219a5b77de4STang Haojin annotate(new ChiselAnnotation { 220a5b77de4STang Haojin def toFirrtl = NestedPrefixModulesAnnotation(mod, prefix, true) 221a5b77de4STang Haojin }) 222a5b77de4STang Haojin } 223a5b77de4STang Haojin 224876196b7SMaxpicca-Li FileRegisters.add("dts", dts) 225876196b7SMaxpicca-Li FileRegisters.add("graphml", graphML) 226876196b7SMaxpicca-Li FileRegisters.add("json", json) 227876196b7SMaxpicca-Li FileRegisters.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) 2284f0a2459Swakafa 2292993c5ecSHaojin Tang val dma = socMisc.map(m => IO(Flipped(new VerilogAXI4Record(m.dma.elts.head.params)))) 2301bf9a05aSzhanglinjuan val peripheral = IO(new VerilogAXI4Record(misc.peripheral.elts.head.params)) 2312993c5ecSHaojin Tang val memory = IO(new VerilogAXI4Record(misc.memory.elts.head.params)) 23273be64b3SJiawei Lin 2334b40434cSzhanglinjuan socMisc match { 2344b40434cSzhanglinjuan case Some(m) => 2352993c5ecSHaojin Tang m.dma.elements.head._2 <> dma.get.viewAs[AXI4Bundle] 2364b40434cSzhanglinjuan dontTouch(dma.get) 2374b40434cSzhanglinjuan case None => 2384b40434cSzhanglinjuan } 2394b40434cSzhanglinjuan 2402993c5ecSHaojin Tang memory.viewAs[AXI4Bundle] <> misc.memory.elements.head._2 24178a8cd25Szhanglinjuan peripheral.viewAs[AXI4Bundle] <> misc.peripheral.elements.head._2 24273be64b3SJiawei Lin 2438b037849SYinan Xu val io = IO(new Bundle { 24420957846SZihao Yu val clock = Input(Clock()) 24567ba96b4SYinan Xu val reset = Input(AsyncReset()) 24634ab1ae9SJiawei Lin val sram_config = Input(UInt(16.W)) 2478b037849SYinan Xu val extIntrs = Input(UInt(NrExtIntr.W)) 24834ab1ae9SJiawei Lin val pll0_lock = Input(Bool()) 24934ab1ae9SJiawei Lin val pll0_ctrl = Output(Vec(6, UInt(32.W))) 250d4aca96cSlqre val systemjtag = new Bundle { 251d4aca96cSlqre val jtag = Flipped(new JTAGIO(hasTRSTn = false)) 25267ba96b4SYinan Xu val reset = Input(AsyncReset()) // No reset allowed on top 253d4aca96cSlqre val mfr_id = Input(UInt(11.W)) 254d4aca96cSlqre val part_number = Input(UInt(16.W)) 255d4aca96cSlqre val version = Input(UInt(4.W)) 256d4aca96cSlqre } 25777bc15a2SYinan Xu val debug_reset = Output(Bool()) 2589e56439dSHazard val rtc_clock = Input(Bool()) 25998c71602SJiawei Lin val cacheable_check = new TLPMAIO() 260b6900d94SYinan Xu val riscv_halt = Output(Vec(NumCores, Bool())) 26185a8d7caSZehao Liu val riscv_critical_error = Output(Vec(NumCores, Bool())) 2620700cab2STang Haojin val riscv_rst_vec = Input(Vec(NumCores, UInt(soc.PAddrBits.W))) 263725e8ddcSchengguanghui val traceCoreInterface = Vec(NumCores, new Bundle { 264725e8ddcSchengguanghui val fromEncoder = Input(new Bundle { 265725e8ddcSchengguanghui val enable = Bool() 266725e8ddcSchengguanghui val stall = Bool() 267725e8ddcSchengguanghui }) 268725e8ddcSchengguanghui val toEncoder = Output(new Bundle { 269725e8ddcSchengguanghui val cause = UInt(TraceCauseWidth.W) 270725e8ddcSchengguanghui val tval = UInt(TraceTvalWidth.W) 271725e8ddcSchengguanghui val priv = UInt(TracePrivWidth.W) 272725e8ddcSchengguanghui val iaddr = UInt((TraceTraceGroupNum * TraceIaddrWidth).W) 273725e8ddcSchengguanghui val itype = UInt((TraceTraceGroupNum * TraceItypeWidth).W) 274725e8ddcSchengguanghui val iretire = UInt((TraceTraceGroupNum * TraceIretireWidthCompressed).W) 275725e8ddcSchengguanghui val ilastsize = UInt((TraceTraceGroupNum * TraceIlastsizeWidth).W) 276725e8ddcSchengguanghui }) 277725e8ddcSchengguanghui }) 2788b037849SYinan Xu }) 27967ba96b4SYinan Xu 28020957846SZihao Yu val reset_sync = withClockAndReset(io.clock, io.reset) { ResetGen() } 28167ba96b4SYinan Xu val jtag_reset_sync = withClockAndReset(io.systemjtag.jtag.TCK, io.systemjtag.reset) { ResetGen() } 2825c060727Ssumailyyc val chi_openllc_opt = Option.when(enableCHI)( 28320957846SZihao Yu withClockAndReset(io.clock, io.reset) { 2845c060727Ssumailyyc Module(new OpenLLC()(p.alter((site, here, up) => { 285186eb48dSsumailyyc case OpenLLCParamKey => soc.OpenLLCParamsOpt.get.copy( 286186eb48dSsumailyyc hartIds = tiles.map(_.HartId), 287186eb48dSsumailyyc FPGAPlatform = debugOpts.FPGAPlatform 288186eb48dSsumailyyc ) 2895c060727Ssumailyyc }))) 2905c060727Ssumailyyc } 2915c060727Ssumailyyc ) 29267ba96b4SYinan Xu 29377bc15a2SYinan Xu // override LazyRawModuleImp's clock and reset 29420957846SZihao Yu childClock := io.clock 29567ba96b4SYinan Xu childReset := reset_sync 29677bc15a2SYinan Xu 29777bc15a2SYinan Xu // output 29877bc15a2SYinan Xu io.debug_reset := misc.module.debug_module_io.debugIO.ndreset 29977bc15a2SYinan Xu 30077bc15a2SYinan Xu // input 30108bf93ffSrvcoresjw dontTouch(io) 30208bf93ffSrvcoresjw dontTouch(memory) 30373be64b3SJiawei Lin misc.module.ext_intrs := io.extIntrs 3049e56439dSHazard misc.module.rtc_clock := io.rtc_clock 30534ab1ae9SJiawei Lin misc.module.pll0_lock := io.pll0_lock 30698c71602SJiawei Lin misc.module.cacheable_check <> io.cacheable_check 30734ab1ae9SJiawei Lin 30834ab1ae9SJiawei Lin io.pll0_ctrl <> misc.module.pll0_ctrl 309c0bc1ee4SYinan Xu 310e156f460SHaojin Tang val msiInfo = WireInit(0.U.asTypeOf(ValidIO(new MsiInfoBundle))) 311e156f460SHaojin Tang 312007f6122SXuan Hu 31377bc15a2SYinan Xu for ((core, i) <- core_with_l2.zipWithIndex) { 31477bc15a2SYinan Xu core.module.io.hartId := i.U 315e156f460SHaojin Tang core.module.io.msiInfo := msiInfo 3163bf5eac7SXuan Hu core.module.io.clintTime := misc.module.clintTime 317b6900d94SYinan Xu io.riscv_halt(i) := core.module.io.cpu_halt 31885a8d7caSZehao Liu io.riscv_critical_error(i) := core.module.io.cpu_crtical_error 3193ad9f3ddSchengguanghui // trace Interface 3203ad9f3ddSchengguanghui val traceInterface = core.module.io.traceCoreInterface 3213ad9f3ddSchengguanghui traceInterface.fromEncoder := io.traceCoreInterface(i).fromEncoder 3223ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.priv := traceInterface.toEncoder.priv 3233ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.cause := traceInterface.toEncoder.trap.cause 3243ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.tval := traceInterface.toEncoder.trap.tval 3253ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.iaddr := VecInit(traceInterface.toEncoder.groups.map(_.bits.iaddr)).asUInt 3263ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.itype := VecInit(traceInterface.toEncoder.groups.map(_.bits.itype)).asUInt 3273ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.iretire := VecInit(traceInterface.toEncoder.groups.map(_.bits.iretire)).asUInt 3283ad9f3ddSchengguanghui io.traceCoreInterface(i).toEncoder.ilastsize := VecInit(traceInterface.toEncoder.groups.map(_.bits.ilastsize)).asUInt 3293ad9f3ddSchengguanghui 330c4b44470SGuokai Chen core.module.io.reset_vector := io.riscv_rst_vec(i) 3315c060727Ssumailyyc } 3325c060727Ssumailyyc 33320957846SZihao Yu withClockAndReset(io.clock, io.reset) { 3345c060727Ssumailyyc Option.when(enableCHI)(true.B).foreach { _ => 3355c060727Ssumailyyc for ((core, i) <- core_with_l2.zipWithIndex) { 3365c060727Ssumailyyc val mmioLogger = CHILogger(s"L2[${i}]_MMIO", true) 3375c060727Ssumailyyc val llcLogger = CHILogger(s"L2[${i}]_LLC", true) 3385c060727Ssumailyyc dontTouch(core.module.io.chi.get) 3395c060727Ssumailyyc bind( 3405c060727Ssumailyyc route( 3415c060727Ssumailyyc core.module.io.chi.get, Map((AddressSet(0x0L, 0x00007fffffffL), NumCores + i)) ++ AddressSet(0x0L, 3425c060727Ssumailyyc 0xffffffffffffL).subtract(AddressSet(0x0L, 0x00007fffffffL)).map(addr => (addr, NumCores * 2)).toMap 3435c060727Ssumailyyc ), 3445c060727Ssumailyyc Map((NumCores + i) -> mmioLogger.io.up, (NumCores * 2) -> llcLogger.io.up) 3455c060727Ssumailyyc ) 3465c060727Ssumailyyc chi_mmioBridge_opt(i).get.module.io.chi.connect(mmioLogger.io.down) 3475c060727Ssumailyyc chi_openllc_opt.get.io.rn(i) <> llcLogger.io.down 348*881e32f5SZifei Zhang require(core.module.io.chi.get.getWidth == llcLogger.io.up.getWidth) 349*881e32f5SZifei Zhang require(llcLogger.io.down.getWidth == chi_openllc_opt.get.io.rn(i).getWidth) 3505c060727Ssumailyyc } 3515c060727Ssumailyyc val memLogger = CHILogger(s"LLC_MEM", true) 3525c060727Ssumailyyc chi_openllc_opt.get.io.sn.connect(memLogger.io.up) 3535c060727Ssumailyyc chi_llcBridge_opt.get.module.io.chi.connect(memLogger.io.down) 3545c060727Ssumailyyc chi_openllc_opt.get.io.nodeID := (NumCores * 2).U 355186eb48dSsumailyyc chi_openllc_opt.foreach { l3 => 356186eb48dSsumailyyc l3.io.debugTopDown.robHeadPaddr := core_with_l2.map(_.module.io.debugTopDown.robHeadPaddr) 357186eb48dSsumailyyc } 358186eb48dSsumailyyc core_with_l2.zip(chi_openllc_opt.get.io.debugTopDown.addrMatch).foreach { case (tile, l3Match) => 359186eb48dSsumailyyc tile.module.io.debugTopDown.l3MissMatch := l3Match 360186eb48dSsumailyyc } 361e836c770SZhaoyang You core_with_l2.zip(chi_openllc_opt).foreach { case (tile, l3) => 362e836c770SZhaoyang You tile.module.io.l3Miss := l3.io.l3Miss 363e836c770SZhaoyang You } 36478a8cd25Szhanglinjuan } 3658b037849SYinan Xu } 3668b037849SYinan Xu 36734ab1ae9SJiawei Lin if(l3cacheOpt.isEmpty || l3cacheOpt.get.rst_nodes.isEmpty){ 36834ab1ae9SJiawei Lin // tie off core soft reset 36934ab1ae9SJiawei Lin for(node <- core_rst_nodes){ 370935edac4STang Haojin node.out.head._1 := false.B.asAsyncReset 37134ab1ae9SJiawei Lin } 37234ab1ae9SJiawei Lin } 37334ab1ae9SJiawei Lin 37460ebee38STang Haojin l3cacheOpt match { 37560ebee38STang Haojin case Some(l3) => 3760d32f713Shappy-lx l3.pf_recv_node match { 3770d32f713Shappy-lx case Some(recv) => 3780d32f713Shappy-lx l3_pf_sender_opt.get.out.head._1.addr_valid := VecInit(memblock_pf_recv_nodes.map(_.get.in.head._1.addr_valid)).asUInt.orR 3790d32f713Shappy-lx for (i <- 0 until NumCores) { 3800d32f713Shappy-lx when(memblock_pf_recv_nodes(i).get.in.head._1.addr_valid) { 3810d32f713Shappy-lx l3_pf_sender_opt.get.out.head._1.addr := memblock_pf_recv_nodes(i).get.in.head._1.addr 3820d32f713Shappy-lx l3_pf_sender_opt.get.out.head._1.l2_pf_en := memblock_pf_recv_nodes(i).get.in.head._1.l2_pf_en 3830d32f713Shappy-lx } 3840d32f713Shappy-lx } 38560ebee38STang Haojin case None => 3860d32f713Shappy-lx } 38760ebee38STang Haojin l3.module.io.debugTopDown.robHeadPaddr := core_with_l2.map(_.module.io.debugTopDown.robHeadPaddr) 38860ebee38STang Haojin core_with_l2.zip(l3.module.io.debugTopDown.addrMatch).foreach { case (tile, l3Match) => tile.module.io.debugTopDown.l3MissMatch := l3Match } 389e836c770SZhaoyang You core_with_l2.foreach(_.module.io.l3Miss := l3.module.io.l3Miss) 390186eb48dSsumailyyc case None => 391186eb48dSsumailyyc } 392186eb48dSsumailyyc 393186eb48dSsumailyyc (chi_openllc_opt, l3cacheOpt) match { 394e836c770SZhaoyang You case (None, None) => 395e836c770SZhaoyang You core_with_l2.foreach(_.module.io.debugTopDown.l3MissMatch := false.B) 396e836c770SZhaoyang You core_with_l2.foreach(_.module.io.l3Miss := false.B) 397186eb48dSsumailyyc case _ => 39860ebee38STang Haojin } 3990d32f713Shappy-lx 400c51f1a7bSsumailyyc core_with_l2.zipWithIndex.foreach { case (tile, i) => 4014b40434cSzhanglinjuan tile.module.io.nodeID.foreach { case nodeID => 402c51f1a7bSsumailyyc nodeID := i.U 4034b40434cSzhanglinjuan dontTouch(nodeID) 4044b40434cSzhanglinjuan } 4054b40434cSzhanglinjuan } 4064b40434cSzhanglinjuan 4073a3744e4Schengguanghui misc.module.debug_module_io.resetCtrl.hartIsInReset := core_with_l2.map(_.module.io.hartIsInReset) 40873be64b3SJiawei Lin misc.module.debug_module_io.clock := io.clock 40967ba96b4SYinan Xu misc.module.debug_module_io.reset := reset_sync 410d4aca96cSlqre 41167ba96b4SYinan Xu misc.module.debug_module_io.debugIO.reset := misc.module.reset 41220957846SZihao Yu misc.module.debug_module_io.debugIO.clock := io.clock 41377bc15a2SYinan Xu // TODO: delay 3 cycles? 41477bc15a2SYinan Xu misc.module.debug_module_io.debugIO.dmactiveAck := misc.module.debug_module_io.debugIO.dmactive 415d4aca96cSlqre // jtag connector 41673be64b3SJiawei Lin misc.module.debug_module_io.debugIO.systemjtag.foreach { x => 417d4aca96cSlqre x.jtag <> io.systemjtag.jtag 41867ba96b4SYinan Xu x.reset := jtag_reset_sync 419d4aca96cSlqre x.mfr_id := io.systemjtag.mfr_id 420d4aca96cSlqre x.part_number := io.systemjtag.part_number 421d4aca96cSlqre x.version := io.systemjtag.version 422d4aca96cSlqre } 42377bc15a2SYinan Xu 42420957846SZihao Yu withClockAndReset(io.clock, reset_sync) { 42577bc15a2SYinan Xu // Modules are reset one by one 42625cb35b6SJiawei Lin // reset ----> SYNC --> {SoCMisc, L3 Cache, Cores} 4273a3744e4Schengguanghui val resetChain = Seq(Seq(misc.module) ++ l3cacheOpt.map(_.module)) 4289eee369fSKamimiao ResetGen(resetChain, reset_sync, !debugOpts.ResetGen) 4293a3744e4Schengguanghui // Ensure that cores could be reset when DM disable `hartReset` or l3cacheOpt.isEmpty. 4303a3744e4Schengguanghui val dmResetReqVec = misc.module.debug_module_io.resetCtrl.hartResetReq.getOrElse(0.U.asTypeOf(Vec(core_with_l2.map(_.module).length, Bool()))) 4313a3744e4Schengguanghui val syncResetCores = if(l3cacheOpt.nonEmpty) l3cacheOpt.map(_.module).get.reset.asBool else misc.module.reset.asBool 4323a3744e4Schengguanghui (core_with_l2.map(_.module)).zip(dmResetReqVec).map { case(core, dmResetReq) => 4333a3744e4Schengguanghui ResetGen(Seq(Seq(core)), (syncResetCores || dmResetReq).asAsyncReset, !debugOpts.ResetGen) 4343a3744e4Schengguanghui } 4358b037849SYinan Xu } 43677bc15a2SYinan Xu 4378b037849SYinan Xu } 438935edac4STang Haojin 439935edac4STang Haojin lazy val module = new XSTopImp(this) 4409d5a2027SYinan Xu} 4418b037849SYinan Xu 442935edac4STang Haojinobject TopMain extends App { 44351e45dbbSTang Haojin val (config, firrtlOpts, firtoolOpts) = ArgParser.parse(args) 44493610df3SMaxpicca-Li 44593610df3SMaxpicca-Li // tools: init to close dpi-c when in fpga 44693610df3SMaxpicca-Li val envInFPGA = config(DebugOptionsKey).FPGAPlatform 447cacfe229STang Haojin val enableDifftest = config(DebugOptionsKey).EnableDifftest || config(DebugOptionsKey).AlwaysBasicDiff 44862129679Swakafa val enableChiselDB = config(DebugOptionsKey).EnableChiselDB 449047e34f9SMaxpicca-Li val enableConstantin = config(DebugOptionsKey).EnableConstantin 450047e34f9SMaxpicca-Li Constantin.init(enableConstantin && !envInFPGA) 45162129679Swakafa ChiselDB.init(enableChiselDB && !envInFPGA) 45293610df3SMaxpicca-Li 453c33deca9Sklin02 if (config(SoCParamsKey).UseXSNoCDiffTop) { 454c33deca9Sklin02 Generator.execute(firrtlOpts, DisableMonitors(p => new XSNoCDiffTop()(p))(config), firtoolOpts) 455c33deca9Sklin02 } else { 456720dd621STang Haojin val soc = if (config(SoCParamsKey).UseXSNoCTop) 457720dd621STang Haojin DisableMonitors(p => LazyModule(new XSNoCTop()(p)))(config) 458720dd621STang Haojin else 459720dd621STang Haojin DisableMonitors(p => LazyModule(new XSTop()(p)))(config) 460720dd621STang Haojin 46151e45dbbSTang Haojin Generator.execute(firrtlOpts, soc.module, firtoolOpts) 4622316cea8SJiuyue Ma 4632316cea8SJiuyue Ma // generate difftest bundles (w/o DifftestTopIO) 4642316cea8SJiuyue Ma if (enableDifftest) { 4652316cea8SJiuyue Ma DifftestModule.finish("XiangShan", false) 4662316cea8SJiuyue Ma } 467c33deca9Sklin02 } 4682316cea8SJiuyue Ma 469876196b7SMaxpicca-Li FileRegisters.write(fileDir = "./build", filePrefix = "XSTop.") 4708b037849SYinan Xu} 471