xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 175bcfe9ae63c4f6c46e0386e26656f6fa9fe4d3)
1package top
2
3import chipsalliance.rocketchip.config.{Config, Parameters}
4import system.SoCParamsKey
5import xiangshan.DebugOptionsKey
6
7import scala.annotation.tailrec
8import scala.sys.exit
9
10object ArgParser {
11  // TODO: add more explainations
12  val usage =
13    """
14      |XiangShan Options
15      |--xs-help                  print this help message
16      |--config <ConfigClassName>
17      |--num-cores <Int>
18      |--dual-core                same as '--num-cores 2'
19      |--with-dramsim3
20      |--disable-log
21      |--disable-perf
22      |""".stripMargin
23
24  def getConfigByName(confString: String): Parameters = {
25    var prefix = "top." // default package is 'top'
26    if(confString.contains('.')){ // already a full name
27      prefix = ""
28    }
29    val c = Class.forName(prefix + confString).getConstructor(Integer.TYPE)
30    c.newInstance(1.asInstanceOf[Object]).asInstanceOf[Parameters]
31  }
32  def parse(args: Array[String], fpga: Boolean = true): (Parameters, Array[String]) = {
33    val default = new DefaultConfig(1)
34    var firrtlOpts = Array[String]()
35    @tailrec
36    def nextOption(config: Parameters, list: List[String]): Parameters = {
37      list match {
38        case Nil => config
39        case "--xs-help" :: tail =>
40          println(usage)
41          if(tail == Nil) exit(0)
42          nextOption(config, tail)
43        case "--config" :: confString :: tail =>
44          nextOption(getConfigByName(confString), tail)
45        case "--num-cores" :: value :: tail =>
46          nextOption(config.alter((site, here, up) => {
47            case SoCParamsKey => up(SoCParamsKey).copy(
48              cores = List.tabulate(value.toInt){ i => up(SoCParamsKey).cores.head.copy(HartId = i) }
49            )
50          }), tail)
51        case "--dual-core" :: tail =>
52          nextOption(config, "--num-cores" :: "2" :: tail)
53        case "--with-dramsim3" :: tail =>
54          nextOption(config.alter((site, here, up) => {
55            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
56          }), tail)
57        case "--disable-log" :: tail =>
58          nextOption(config.alter((site, here, up) => {
59            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = false)
60          }), tail)
61        case "--disable-perf" :: tail =>
62          nextOption(config.alter((site, here, up) => {
63            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
64          }), tail)
65        case option :: tail =>
66          // unknown option, maybe a firrtl option, skip
67          firrtlOpts :+= option
68          nextOption(config, tail)
69      }
70    }
71    var config = nextOption(default, args.toList)
72    if(!fpga){
73      config = config.alter((site, here, up) => {
74        case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = false)
75      })
76    }
77    (config, firrtlOpts)
78  }
79}
80