aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/CheckCombLoopsSpec.scala
blob: 16482560bfbf52a8bdd74e48922ed0ca6dda057d (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// See LICENSE for license details.

package firrtlTests

import firrtl._
import firrtl.ir._
import firrtl.passes._
import firrtl.Mappers._
import annotations._

class CheckCombLoopsSpec extends SimpleTransformSpec {

  def emitter = new LowFirrtlEmitter

  def transforms = Seq(
    new ChirrtlToHighFirrtl,
    new IRToWorkingIR,
    new ResolveAndCheck,
    new HighFirrtlToMiddleFirrtl,
    new MiddleFirrtlToLowFirrtl
  )

  "Simple combinational loop" should "throw an exception" in {
    val input = """circuit hasloops :
                   |  module hasloops :
                   |    input clk : Clock
                   |    input a : UInt<1>
                   |    input b : UInt<1>
                   |    output c : UInt<1>
                   |    output d : UInt<1>
                   |    wire y : UInt<1>
                   |    wire z : UInt<1>
                   |    c <= b
                   |    z <= y
                   |    y <= z
                   |    d <= z
                   |""".stripMargin

    val writer = new java.io.StringWriter
    intercept[CheckCombLoops.CombLoopException] {
      compile(CircuitState(parse(input), ChirrtlForm, None), writer)
    }
  }

  "Node combinational loop" should "throw an exception" in {
    val input = """circuit hasloops :
                   |  module hasloops :
                   |    input clk : Clock
                   |    input a : UInt<1>
                   |    input b : UInt<1>
                   |    output c : UInt<1>
                   |    output d : UInt<1>
                   |    wire y : UInt<1>
                   |    c <= b
                   |    node z = and(c,y)
                   |    y <= z
                   |    d <= z
                   |""".stripMargin

    val writer = new java.io.StringWriter
    intercept[CheckCombLoops.CombLoopException] {
      compile(CircuitState(parse(input), ChirrtlForm, None), writer)
    }
  }

  "Combinational loop through a combinational memory read port" should "throw an exception" in {
    val input = """circuit hasloops :
                   |  module hasloops :
                   |    input clk : Clock
                   |    input a : UInt<1>
                   |    input b : UInt<1>
                   |    output c : UInt<1>
                   |    output d : UInt<1>
                   |    wire y : UInt<1>
                   |    wire z : UInt<1>
                   |    c <= b
                   |    mem m :
                   |      data-type => UInt<1>
                   |      depth => 2
                   |      read-latency => 0
                   |      write-latency => 1
                   |      reader => r
                   |      read-under-write => undefined
                   |    m.r.clk <= clk
                   |    m.r.addr <= y
                   |    m.r.en <= UInt(1)
                   |    z <= m.r.data
                   |    y <= z
                   |    d <= z
                   |""".stripMargin

    val writer = new java.io.StringWriter
    intercept[CheckCombLoops.CombLoopException] {
      compile(CircuitState(parse(input), ChirrtlForm, None), writer)
    }
  }

  "Combination loop through an instance" should "throw an exception" in {
    val input = """circuit hasloops :
                   |  module thru :
                   |    input in : UInt<1>
                   |    output out : UInt<1>
                   |    out <= in
                   |  module hasloops :
                   |    input clk : Clock
                   |    input a : UInt<1>
                   |    input b : UInt<1>
                   |    output c : UInt<1>
                   |    output d : UInt<1>
                   |    wire y : UInt<1>
                   |    wire z : UInt<1>
                   |    c <= b
                   |    inst inner of thru
                   |    inner.in <= y
                   |    z <= inner.out
                   |    y <= z
                   |    d <= z
                   |""".stripMargin

    val writer = new java.io.StringWriter
    intercept[CheckCombLoops.CombLoopException] {
      compile(CircuitState(parse(input), ChirrtlForm, None), writer)
    }
  }


}