ArgParser.scala 3.6 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

L
LinJiawei 已提交
17 18 19 20
package top

import chipsalliance.rocketchip.config.{Config, Parameters}
import system.SoCParamsKey
J
Jiawei Lin 已提交
21
import xiangshan.{DebugOptionsKey, XSTileKey}
L
LinJiawei 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34

import scala.annotation.tailrec
import scala.sys.exit

object ArgParser {
  // TODO: add more explainations
  val usage =
    """
      |XiangShan Options
      |--xs-help                  print this help message
      |--config <ConfigClassName>
      |--num-cores <Int>
      |--with-dramsim3
35 36 37
      |--fpga-platform
      |--enable-difftest
      |--enable-log
L
LinJiawei 已提交
38 39 40 41 42 43 44 45 46 47 48
      |--disable-perf
      |""".stripMargin

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