LogUtils.scala 1.9 KB
Newer Older
L
LinJiawei 已提交
1
package utils
L
LinJiawei 已提交
2 3

import chisel3._
4
import chisel3.util.experimental.BoringUtils
L
LinJiawei 已提交
5
import xiangshan.HasXSParameter
L
LinJiawei 已提交
6
import utils.XSLogLevel.XSLogLevel
7

L
LinJiawei 已提交
8 9 10
object XSLogLevel extends Enumeration {
  type XSLogLevel = Value

11
  val ALL   = Value(0, "ALL  ")
L
LinJiawei 已提交
12
  val DEBUG = Value("DEBUG")
Y
Yinan Xu 已提交
13 14
  val INFO  = Value("INFO ")
  val WARN  = Value("WARN ")
L
LinJiawei 已提交
15
  val ERROR = Value("ERROR")
Y
Yinan Xu 已提交
16
  val OFF   = Value("OFF  ")
L
LinJiawei 已提交
17 18
}

19
object XSLog {
20
  var generateLog: Boolean = false
L
LinJiawei 已提交
21
  def apply(debugLevel: XSLogLevel)
Y
Yinan Xu 已提交
22
           (prefix: Boolean, cond: Bool, pable: Printable)
23 24
           (implicit name: String): Any =
  {
25
    val commonInfo = p"[$debugLevel][time=${GTimer()}] $name: "
26
    val logEnable = WireInit(false.B)
27
    ExcitingUtils.addSink(logEnable, "DISPLAY_LOG_ENABLE")
28
    if(generateLog){
J
jinyue110 已提交
29
      when (cond ){//&& logEnable) {
30
        printf((if (prefix) commonInfo else p"") + pable)
31 32 33
        if (debugLevel >= XSLogLevel.ERROR) {
          assert(false.B)
        }
34
      }
L
LinJiawei 已提交
35 36 37 38
    }
  }
}

W
William Wang 已提交
39
sealed abstract class LogHelper(val logLevel: XSLogLevel) extends HasXSParameter {
L
LinJiawei 已提交
40

41
  def apply(cond: Bool, fmt: String, data: Bits*)(implicit name: String): Any =
L
LinJiawei 已提交
42
    apply(cond, Printable.pack(fmt, data:_*))
Y
Yinan Xu 已提交
43
  def apply(cond: Bool, pable: Printable)(implicit name: String): Any = apply(true, cond, pable)
44
  def apply(fmt: String, data: Bits*)(implicit name: String): Any =
Y
Yinan Xu 已提交
45 46 47 48 49 50
    apply(Printable.pack(fmt, data:_*))
  def apply(pable: Printable)(implicit name: String): Any = apply(true.B, pable)
  def apply(prefix: Boolean, cond: Bool, fmt: String, data: Bits*)(implicit name: String): Any =
    apply(prefix, cond, Printable.pack(fmt, data:_*))
  def apply(prefix: Boolean, cond: Bool, pable: Printable)(implicit name: String): Any =
    XSLog(logLevel)(prefix, cond, pable)
L
LinJiawei 已提交
51 52 53 54 55 56 57 58 59
}

object XSDebug extends LogHelper(XSLogLevel.DEBUG)

object XSInfo extends LogHelper(XSLogLevel.INFO)

object XSWarn extends LogHelper(XSLogLevel.WARN)

object XSError extends LogHelper(XSLogLevel.ERROR)