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

import chisel3._
L
LinJiawei 已提交
4
import top.Parameters
L
LinJiawei 已提交
5
import xiangshan.HasXSParameter
L
LinJiawei 已提交
6
import utils.XSLogLevel.XSLogLevel
L
LinJiawei 已提交
7 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 {
L
LinJiawei 已提交
20
  def apply(debugLevel: XSLogLevel)
Y
Yinan Xu 已提交
21
           (prefix: Boolean, cond: Bool, pable: Printable)
22 23
           (implicit name: String): Any =
  {
24
    val logEnable = WireInit(false.B)
L
LinJiawei 已提交
25
    val logTimestamp = WireInit(0.U(64.W))
26
    ExcitingUtils.addSink(logEnable, "DISPLAY_LOG_ENABLE")
L
LinJiawei 已提交
27 28
    ExcitingUtils.addSink(logTimestamp, "logTimestamp")
    if(Parameters.get.envParameters.EnableDebug){
J
jinyue110 已提交
29
      when (cond && logEnable) {
L
LinJiawei 已提交
30
        val commonInfo = p"[$debugLevel][time=$logTimestamp] $name: "
31
        printf((if (prefix) commonInfo else p"") + pable)
32 33 34
        if (debugLevel >= XSLogLevel.ERROR) {
          assert(false.B)
        }
35
      }
L
LinJiawei 已提交
36 37
    }
  }
38 39 40 41 42 43 44

  def displayLog: Bool = {
    val logEnable = WireInit(false.B)
    ExcitingUtils.addSink(logEnable, "DISPLAY_LOG_ENABLE")
    val ret = WireInit(false.B)
    if(Parameters.get.envParameters.EnableDebug) {
      ret := logEnable
L
LinJiawei 已提交
45
    }
46
    ret
L
LinJiawei 已提交
47 48 49
  }
}

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

52
  def apply(cond: Bool, fmt: String, data: Bits*)(implicit name: String): Any =
L
LinJiawei 已提交
53
    apply(cond, Printable.pack(fmt, data:_*))
Y
Yinan Xu 已提交
54
  def apply(cond: Bool, pable: Printable)(implicit name: String): Any = apply(true, cond, pable)
55
  def apply(fmt: String, data: Bits*)(implicit name: String): Any =
Y
Yinan Xu 已提交
56 57 58 59 60 61
    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)
A
Allen 已提交
62 63 64 65

  // trigger log or not
  // used when user what to fine-control their printf output
  def trigger: Bool = {
66
    XSLog.displayLog
A
Allen 已提交
67
  }
A
Allen 已提交
68 69 70 71 72 73 74

  def printPrefix()(implicit name: String): Unit = {
    val commonInfo = p"[$logLevel][time=${GTimer()}] $name: "
    when (trigger) {
      printf(commonInfo)
    }
  }
75 76

  // dump under with certain prefix
77
  def exec(dump: () => Unit)(implicit name: String): Unit = {
78 79 80 81 82 83 84
    when (trigger) {
      printPrefix
      dump
    }
  }

  // dump under certain condition and with certain prefix
85
  def exec(cond: Bool, dump: () => Unit)(implicit name: String): Unit = {
86 87 88 89 90
    when (trigger && cond) {
      printPrefix
      dump
    }
  }
L
LinJiawei 已提交
91 92 93 94 95 96 97 98 99
}

object XSDebug extends LogHelper(XSLogLevel.DEBUG)

object XSInfo extends LogHelper(XSLogLevel.INFO)

object XSWarn extends LogHelper(XSLogLevel.WARN)

object XSError extends LogHelper(XSLogLevel.ERROR)