xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 57bb43b5f11c3f1e89ac52f232fe73056b35d9bd)
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 chipsalliance.rocketchip.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      |--disable-perf
39      |""".stripMargin
40
41  def getConfigByName(confString: String): Parameters = {
42    var prefix = "top." // default package is 'top'
43    if(confString.contains('.')){ // already a full name
44      prefix = ""
45    }
46    val c = Class.forName(prefix + confString).getConstructor(Integer.TYPE)
47    c.newInstance(1.asInstanceOf[Object]).asInstanceOf[Parameters]
48  }
49  def parse(args: Array[String]): (Parameters, Array[String]) = {
50    val default = new DefaultConfig(1)
51    var firrtlOpts = Array[String]()
52    @tailrec
53    def nextOption(config: Parameters, list: List[String]): Parameters = {
54      list match {
55        case Nil => config
56        case "--xs-help" :: tail =>
57          println(usage)
58          if(tail == Nil) exit(0)
59          nextOption(config, tail)
60        case "--config" :: confString :: tail =>
61          nextOption(getConfigByName(confString), tail)
62        case "--num-cores" :: value :: tail =>
63          nextOption(config.alter((site, here, up) => {
64            case XSTileKey => (0 until value.toInt) map{ i =>
65              up(XSTileKey).head.copy(HartId = i)
66            }
67          }), tail)
68        case "--with-dramsim3" :: tail =>
69          nextOption(config.alter((site, here, up) => {
70            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
71          }), tail)
72        case "--fpga-platform" :: tail =>
73          nextOption(config.alter((site, here, up) => {
74            case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true)
75          }), tail)
76        case "--enable-difftest" :: tail =>
77          nextOption(config.alter((site, here, up) => {
78            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true)
79          }), tail)
80        case "--enable-log" :: tail =>
81          nextOption(config.alter((site, here, up) => {
82            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true)
83          }), tail)
84        case "--disable-perf" :: tail =>
85          nextOption(config.alter((site, here, up) => {
86            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
87          }), tail)
88        case option :: tail =>
89          // unknown option, maybe a firrtl option, skip
90          firrtlOpts :+= option
91          nextOption(config, tail)
92      }
93    }
94    var config = nextOption(default, args.toList)
95    (config, firrtlOpts)
96  }
97}
98