xref: /XiangShan/src/main/scala/top/YamlParser.scala (revision 53bd4e1cb2bbe049a6887a8f3c75c296803c14b0)
15bd65c56STang Haojin/***************************************************************************************
25bd65c56STang Haojin* Copyright (c) 2025 Beijing Institute of Open Source Chip (BOSC)
35bd65c56STang Haojin* Copyright (c) 2025 Institute of Computing Technology, Chinese Academy of Sciences
45bd65c56STang Haojin*
55bd65c56STang Haojin* XiangShan is licensed under Mulan PSL v2.
65bd65c56STang Haojin* You can use this software according to the terms and conditions of the Mulan PSL v2.
75bd65c56STang Haojin* You may obtain a copy of Mulan PSL v2 at:
85bd65c56STang Haojin*          http://license.coscl.org.cn/MulanPSL2
95bd65c56STang Haojin*
105bd65c56STang Haojin* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
115bd65c56STang Haojin* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
125bd65c56STang Haojin* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
135bd65c56STang Haojin*
145bd65c56STang Haojin* See the Mulan PSL v2 for more details.
155bd65c56STang Haojin***************************************************************************************/
165bd65c56STang Haojin
175bd65c56STang Haojinpackage top
185bd65c56STang Haojin
195bd65c56STang Haojinimport io.circe.generic.extras.Configuration
205bd65c56STang Haojinimport io.circe.generic.extras.auto._
215bd65c56STang Haojin
228cfc24b2STang Haojinimport aia.IMSICParams
235bd65c56STang Haojinimport org.chipsalliance.cde.config.Parameters
245bd65c56STang Haojinimport system.SoCParamsKey
255bd65c56STang Haojinimport xiangshan.backend.fu.{MemoryRange, PMAConfigEntry}
264c0658aeSTang Haojinimport xiangshan.XSTileKey
2769b78670STang Haojinimport freechips.rocketchip.devices.debug.{DebugAttachParams, ExportDebug}
2869b78670STang Haojinimport freechips.rocketchip.devices.debug.{DMI, JTAG, CJTAG, APB}
2969b78670STang Haojinimport freechips.rocketchip.devices.debug.{DebugModuleKey, DebugModuleParams}
3016ae9ddcSTang Haojinimport freechips.rocketchip.diplomacy.AddressSet
3169b78670STang Haojinimport freechips.rocketchip.tile.MaxHartIdBits
324c0658aeSTang Haojinimport freechips.rocketchip.util.AsyncQueueParams
338cfc24b2STang Haojinimport device.IMSICBusType
345bd65c56STang Haojin
355bd65c56STang Haojincase class YamlConfig(
3669b78670STang Haojin  Config: Option[String],
375bd65c56STang Haojin  PmemRanges: Option[List[MemoryRange]],
385bd65c56STang Haojin  PMAConfigs: Option[List[PMAConfigEntry]],
390964a977STang Haojin  EnableCHIAsyncBridge: Option[Boolean],
405bd65c56STang Haojin  L2CacheConfig: Option[L2CacheConfig],
415bd65c56STang Haojin  L3CacheConfig: Option[L3CacheConfig],
4269b78670STang Haojin  HartIDBits: Option[Int],
4369b78670STang Haojin  DebugAttachProtocals: Option[List[String]],
4469b78670STang Haojin  DebugModuleParams: Option[DebugModuleParams],
454c0658aeSTang Haojin  WFIResume: Option[Boolean],
4616ae9ddcSTang Haojin  SeperateDM: Option[Boolean],
4716ae9ddcSTang Haojin  SeperateTLBus: Option[Boolean],
488cfc24b2STang Haojin  SeperateTLBusRanges: Option[List[AddressSet]],
4969b78670STang Haojin  EnableSeperateTLBusAsyncBridge: Option[Boolean],
508cfc24b2STang Haojin  IMSICBusType: Option[String],
518cfc24b2STang Haojin  IMSICParams: Option[IMSICParams],
5269b78670STang Haojin  CHIIssue: Option[String],
5369b78670STang Haojin  WFIClockGate: Option[Boolean],
5469b78670STang Haojin  EnablePowerDown: Option[Boolean],
5569b78670STang Haojin  XSTopPrefix: Option[String],
5669b78670STang Haojin  EnableDFX: Option[Boolean],
5769b78670STang Haojin  EnableSramCtl: Option[Boolean],
5869b78670STang Haojin  EnableCHINS: Option[Boolean],
59*53bd4e1cSTang Haojin  CHIAddrWidth: Option[Int],
605bd65c56STang Haojin)
615bd65c56STang Haojin
625bd65c56STang Haojinobject YamlParser {
635bd65c56STang Haojin  implicit val customParserConfig: Configuration = Configuration.default.withDefaults
645bd65c56STang Haojin  def parseYaml(config: Parameters, yamlFile: String): Parameters = {
655bd65c56STang Haojin    val yaml = scala.io.Source.fromFile(yamlFile).mkString
665bd65c56STang Haojin    val json = io.circe.yaml.parser.parse(yaml) match {
675bd65c56STang Haojin      case Left(value) => throw value
685bd65c56STang Haojin      case Right(value) => value
695bd65c56STang Haojin    }
705bd65c56STang Haojin    val yamlConfig = json.as[YamlConfig] match {
715bd65c56STang Haojin      case Left(value) => throw value
725bd65c56STang Haojin      case Right(value) => value
735bd65c56STang Haojin    }
745bd65c56STang Haojin    var newConfig = config
7569b78670STang Haojin    yamlConfig.Config.foreach { config =>
7669b78670STang Haojin      newConfig = ArgParser.getConfigByName(config)
7769b78670STang Haojin    }
785bd65c56STang Haojin    yamlConfig.PmemRanges.foreach { ranges =>
795bd65c56STang Haojin      newConfig = newConfig.alter((site, here, up) => {
805bd65c56STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(PmemRanges = ranges)
815bd65c56STang Haojin      })
825bd65c56STang Haojin    }
835bd65c56STang Haojin    yamlConfig.PMAConfigs.foreach { pmaConfigs =>
845bd65c56STang Haojin      newConfig = newConfig.alter((site, here, up) => {
855bd65c56STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(PMAConfigs = pmaConfigs)
865bd65c56STang Haojin      })
875bd65c56STang Haojin    }
880964a977STang Haojin    yamlConfig.EnableCHIAsyncBridge.foreach { enable =>
895bd65c56STang Haojin      newConfig = newConfig.alter((site, here, up) => {
905bd65c56STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(
910964a977STang Haojin          EnableCHIAsyncBridge = Option.when(enable)(AsyncQueueParams(depth = 16, sync = 3, safe = false))
925bd65c56STang Haojin        )
935bd65c56STang Haojin      })
945bd65c56STang Haojin    }
955bd65c56STang Haojin    yamlConfig.L2CacheConfig.foreach(l2 => newConfig = newConfig.alter(l2))
965bd65c56STang Haojin    yamlConfig.L3CacheConfig.foreach(l3 => newConfig = newConfig.alter(l3))
9769b78670STang Haojin    yamlConfig.DebugAttachProtocals.foreach { protocols =>
985bd65c56STang Haojin      newConfig = newConfig.alter((site, here, up) => {
9969b78670STang Haojin        case ExportDebug => DebugAttachParams(protocols = protocols.map {
10069b78670STang Haojin          case "DMI" => DMI
10169b78670STang Haojin          case "JTAG" => JTAG
10269b78670STang Haojin          case "CJTAG" => CJTAG
10369b78670STang Haojin          case "APB" => APB
10469b78670STang Haojin        }.toSet)
10569b78670STang Haojin      })
10669b78670STang Haojin    }
10769b78670STang Haojin    yamlConfig.HartIDBits.foreach { bits =>
10869b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
10969b78670STang Haojin        case MaxHartIdBits => bits
11069b78670STang Haojin      })
11169b78670STang Haojin    }
11269b78670STang Haojin    yamlConfig.DebugModuleParams.foreach { params =>
11369b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
11469b78670STang Haojin        case DebugModuleKey => Some(params)
1155bd65c56STang Haojin      })
1165bd65c56STang Haojin    }
1174c0658aeSTang Haojin    yamlConfig.WFIResume.foreach { enable =>
1184c0658aeSTang Haojin      newConfig = newConfig.alter((site, here, up) => {
1194c0658aeSTang Haojin        case XSTileKey => up(XSTileKey).map(_.copy(wfiResume = enable))
1204c0658aeSTang Haojin      })
1214c0658aeSTang Haojin    }
12216ae9ddcSTang Haojin    yamlConfig.SeperateDM.foreach { enable =>
12316ae9ddcSTang Haojin      newConfig = newConfig.alter((site, here, up) => {
12416ae9ddcSTang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(SeperateDM = enable)
12516ae9ddcSTang Haojin      })
12616ae9ddcSTang Haojin    }
12716ae9ddcSTang Haojin    yamlConfig.SeperateTLBus.foreach { enable =>
12816ae9ddcSTang Haojin      newConfig = newConfig.alter((site, here, up) => {
12916ae9ddcSTang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(SeperateTLBus = enable)
13016ae9ddcSTang Haojin      })
13116ae9ddcSTang Haojin    }
13216ae9ddcSTang Haojin    yamlConfig.SeperateTLBusRanges.foreach { ranges =>
13316ae9ddcSTang Haojin      newConfig = newConfig.alter((site, here, up) => {
13416ae9ddcSTang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(SeperateTLBusRanges = ranges)
13516ae9ddcSTang Haojin      })
13616ae9ddcSTang Haojin    }
13769b78670STang Haojin    yamlConfig.EnableSeperateTLBusAsyncBridge.foreach { enable =>
13869b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
13969b78670STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(
14069b78670STang Haojin          SeperateTLAsyncBridge = Option.when(enable)(AsyncQueueParams(depth = 1, sync = 3, safe = false))
14169b78670STang Haojin        )
14269b78670STang Haojin      })
14369b78670STang Haojin    }
1448cfc24b2STang Haojin    yamlConfig.IMSICBusType.foreach { busType =>
1458cfc24b2STang Haojin      newConfig = newConfig.alter((site, here, up) => {
1468cfc24b2STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(IMSICBusType = device.IMSICBusType.withName(busType))
1478cfc24b2STang Haojin      })
1488cfc24b2STang Haojin    }
1498cfc24b2STang Haojin    yamlConfig.IMSICParams.foreach { params =>
1508cfc24b2STang Haojin      newConfig = newConfig.alter((site, here, up) => {
1518cfc24b2STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(IMSICParams = params)
1528cfc24b2STang Haojin      })
1538cfc24b2STang Haojin    }
15469b78670STang Haojin    yamlConfig.CHIIssue.foreach { issue =>
15569b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
15669b78670STang Haojin        case coupledL2.tl2chi.CHIIssue => issue
15769b78670STang Haojin      })
15869b78670STang Haojin    }
15969b78670STang Haojin    yamlConfig.WFIClockGate.foreach { enable =>
16069b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
16169b78670STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(WFIClockGate = enable)
16269b78670STang Haojin      })
16369b78670STang Haojin    }
16469b78670STang Haojin    yamlConfig.EnablePowerDown.foreach { enable =>
16569b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
16669b78670STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(EnablePowerDown = enable)
16769b78670STang Haojin      })
16869b78670STang Haojin    }
16969b78670STang Haojin    yamlConfig.XSTopPrefix.foreach { prefix =>
17069b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
17169b78670STang Haojin        case SoCParamsKey => up(SoCParamsKey).copy(XSTopPrefix = Option.when(prefix.nonEmpty)(prefix))
17269b78670STang Haojin      })
17369b78670STang Haojin    }
17469b78670STang Haojin    yamlConfig.EnableDFX.foreach { enable =>
17569b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
17669b78670STang Haojin        case XSTileKey => up(XSTileKey).map(_.copy(hasMbist = enable))
17769b78670STang Haojin      })
17869b78670STang Haojin    }
17969b78670STang Haojin    yamlConfig.EnableSramCtl.foreach { enable =>
18069b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
18169b78670STang Haojin        case XSTileKey => up(XSTileKey).map(_.copy(hasSramCtl = enable))
18269b78670STang Haojin      })
18369b78670STang Haojin    }
18469b78670STang Haojin    yamlConfig.EnableCHINS.foreach { enable =>
18569b78670STang Haojin      newConfig = newConfig.alter((site, here, up) => {
18669b78670STang Haojin        case coupledL2.tl2chi.NonSecureKey => enable
18769b78670STang Haojin      })
18869b78670STang Haojin    }
189*53bd4e1cSTang Haojin    yamlConfig.CHIAddrWidth.foreach { width =>
190*53bd4e1cSTang Haojin      newConfig = newConfig.alter((site, here, up) => {
191*53bd4e1cSTang Haojin        case coupledL2.tl2chi.CHIAddrWidthKey => width
192*53bd4e1cSTang Haojin      })
193*53bd4e1cSTang Haojin    }
1945bd65c56STang Haojin    newConfig
1955bd65c56STang Haojin  }
1965bd65c56STang Haojin}
197