xref: /XiangShan/src/main/scala/top/YamlParser.scala (revision 881e32f5b63c435bafbaf5dc1d792ffcc9ea103e)
1/***************************************************************************************
2* Copyright (c) 2025 Beijing Institute of Open Source Chip (BOSC)
3* Copyright (c) 2025 Institute of Computing Technology, Chinese Academy of Sciences
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 top
18
19import io.circe.generic.extras.Configuration
20import io.circe.generic.extras.auto._
21
22import org.chipsalliance.cde.config.Parameters
23import system.SoCParamsKey
24import xiangshan.backend.fu.{MemoryRange, PMAConfigEntry}
25import freechips.rocketchip.devices.debug.DebugModuleKey
26import freechips.rocketchip.util.AsyncQueueParams
27
28case class YamlConfig(
29  PmemRanges: Option[List[MemoryRange]],
30  PMAConfigs: Option[List[PMAConfigEntry]],
31  EnableCHIAsyncBridge: Option[Boolean],
32  L2CacheConfig: Option[L2CacheConfig],
33  L3CacheConfig: Option[L3CacheConfig],
34  DebugModuleBaseAddr: Option[BigInt]
35)
36
37object YamlParser {
38  implicit val customParserConfig: Configuration = Configuration.default.withDefaults
39  def parseYaml(config: Parameters, yamlFile: String): Parameters = {
40    val yaml = scala.io.Source.fromFile(yamlFile).mkString
41    val json = io.circe.yaml.parser.parse(yaml) match {
42      case Left(value) => throw value
43      case Right(value) => value
44    }
45    val yamlConfig = json.as[YamlConfig] match {
46      case Left(value) => throw value
47      case Right(value) => value
48    }
49    var newConfig = config
50    yamlConfig.PmemRanges.foreach { ranges =>
51      newConfig = newConfig.alter((site, here, up) => {
52        case SoCParamsKey => up(SoCParamsKey).copy(PmemRanges = ranges)
53      })
54    }
55    yamlConfig.PMAConfigs.foreach { pmaConfigs =>
56      newConfig = newConfig.alter((site, here, up) => {
57        case SoCParamsKey => up(SoCParamsKey).copy(PMAConfigs = pmaConfigs)
58      })
59    }
60    yamlConfig.EnableCHIAsyncBridge.foreach { enable =>
61      newConfig = newConfig.alter((site, here, up) => {
62        case SoCParamsKey => up(SoCParamsKey).copy(
63          EnableCHIAsyncBridge = Option.when(enable)(AsyncQueueParams(depth = 16, sync = 3, safe = false))
64        )
65      })
66    }
67    yamlConfig.L2CacheConfig.foreach(l2 => newConfig = newConfig.alter(l2))
68    yamlConfig.L3CacheConfig.foreach(l3 => newConfig = newConfig.alter(l3))
69    yamlConfig.DebugModuleBaseAddr.foreach { addr =>
70      newConfig = newConfig.alter((site, here, up) => {
71        case DebugModuleKey => up(DebugModuleKey).map(_.copy(baseAddress = addr))
72      })
73    }
74    newConfig
75  }
76}
77