summaryrefslogtreecommitdiff
path: root/src/main/scala/Chisel/CoreUtil.scala
blob: 9027711bc42231ccc82521fe6636ef8b88006e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// See LICENSE for license details.

package Chisel

import internal._
import internal.Builder.pushCommand
import firrtl._

object assert {
  /** Checks for a condition to be valid in the circuit at all times. If the
    * condition evaluates to false, the circuit simulation stops with an error.
    *
    * Does not fire when in reset (defined as the encapsulating Module's
    * reset). If your definition of reset is not the encapsulating Module's
    * reset, you will need to gate this externally.
    *
    * May be called outside of a Module (like defined in a function), so
    * functions using assert make the standard Module assumptions (single clock
    * and single reset).
    *
    * @param cond condition, assertion fires (simulation fails) when false
    * @param message optional message to print when the assertion fires
    */
  def apply(cond: Bool, message: String) {
    when (!Builder.dynamicContext.currentModule.get.reset) {
      when(!cond) {
        if (message.isEmpty()) {
          printf(s"Assertion failed: (TODO: code / lineno)")
        } else {
          printf(s"Assertion failed: (TODO: code / lineno): $message")
        }
        pushCommand(Stop(Node(Builder.dynamicContext.currentModule.get.clock), 1))
      }
    }
  }

  /** A workaround for default-value overloading problems in Scala, just
    * 'assert(cond, "")' */
  def apply(cond: Bool) {
    assert(cond, "")
  }

  /** An elaboration-time assertion, otherwise the same as the above run-time
    * assertion. */
  def apply(cond: Boolean, message: String) {
    Predef.assert(cond, message)
  }

  /** A workaround for default-value overloading problems in Scala, just
    * 'assert(cond, "")' */
  def apply(cond: Boolean) {
    Predef.assert(cond, "")
  }
}

object printf {
  /** Prints a message in simulation.
    *
    * Does not fire when in reset (defined as the encapsulating Module's
    * reset). If your definition of reset is not the encapsulating Module's
    * reset, you will need to gate this externally.
    *
    * May be called outside of a Module (like defined in a function), so
    * functions using printf make the standard Module assumptions (single clock
    * and single reset).
    *
    * @param fmt printf format string
    * @param data format string varargs containing data to print
    */
  def apply(fmt: String, data: Bits*) {
    when (!Builder.dynamicContext.currentModule.get.reset) {
      pushCommand(Printf(Node(Builder.dynamicContext.currentModule.get.clock),
          fmt, data.map((d: Bits) => d.ref)))
    }
  }
}