xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 51e45dbbf87325e45ff2af6ca86ed6c7eed04464)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
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 org.chipsalliance.cde.config.{Config, Parameters}
20import system.SoCParamsKey
21import xiangshan.{DebugOptionsKey, XSTileKey}
22
23import scala.annotation.tailrec
24import scala.sys.exit
25
26object ArgParser {
27  // TODO: add more explainations
28  val usage =
29    """
30      |XiangShan Options
31      |--xs-help                  print this help message
32      |--config <ConfigClassName>
33      |--num-cores <Int>
34      |--with-dramsim3
35      |--fpga-platform
36      |--enable-difftest
37      |--enable-log
38      |--with-chiseldb
39      |--with-rollingdb
40      |--disable-perf
41      |""".stripMargin
42
43  def getConfigByName(confString: String): Parameters = {
44    var prefix = "top." // default package is 'top'
45    if(confString.contains('.')){ // already a full name
46      prefix = ""
47    }
48    val c = Class.forName(prefix + confString).getConstructor(Integer.TYPE)
49    c.newInstance(1.asInstanceOf[Object]).asInstanceOf[Parameters]
50  }
51  def parse(args: Array[String]): (Parameters, Array[String], Array[String]) = {
52    val default = new DefaultConfig(1)
53    var firrtlOpts = Array[String]()
54    var firtoolOpts = Array[String]()
55    @tailrec
56    def nextOption(config: Parameters, list: List[String]): Parameters = {
57      list match {
58        case Nil => config
59        case "--xs-help" :: tail =>
60          println(usage)
61          if(tail == Nil) exit(0)
62          nextOption(config, tail)
63        case "--config" :: confString :: tail =>
64          nextOption(getConfigByName(confString), tail)
65        case "--num-cores" :: value :: tail =>
66          nextOption(config.alter((site, here, up) => {
67            case XSTileKey => (0 until value.toInt) map{ i =>
68              up(XSTileKey).head.copy(HartId = i)
69            }
70          }), tail)
71        case "--with-dramsim3" :: tail =>
72          nextOption(config.alter((site, here, up) => {
73            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
74          }), tail)
75        case "--with-chiseldb" :: tail =>
76          nextOption(config.alter((site, here, up) => {
77            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableChiselDB = true)
78          }), tail)
79        case "--with-rollingdb" :: tail =>
80          nextOption(config.alter((site, here, up) => {
81            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableRollingDB = true)
82          }), tail)
83        case "--with-constantin" :: tail =>
84          nextOption(config.alter((site, here, up) => {
85            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableConstantin = true)
86          }), tail)
87        case "--fpga-platform" :: tail =>
88          nextOption(config.alter((site, here, up) => {
89            case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true)
90          }), tail)
91        case "--enable-difftest" :: tail =>
92          nextOption(config.alter((site, here, up) => {
93            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true)
94          }), tail)
95        case "--enable-log" :: tail =>
96          nextOption(config.alter((site, here, up) => {
97            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true)
98          }), tail)
99        case "--disable-perf" :: tail =>
100          nextOption(config.alter((site, here, up) => {
101            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
102          }), tail)
103        case "--firtool-opt" :: option :: tail =>
104          firtoolOpts ++= option.split(" ").filter(_.nonEmpty)
105          nextOption(config, tail)
106        case option :: tail =>
107          // unknown option, maybe a firrtl option, skip
108          firrtlOpts :+= option
109          nextOption(config, tail)
110      }
111    }
112    var config = nextOption(default, args.toList)
113    (config, firrtlOpts, firtoolOpts)
114  }
115}
116