summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/BlackBox.scala
blob: 344754e18279bbc9da23abce5e05b87b60dc1053 (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
// See LICENSE for license details.

package chiselTests

import java.io.File
import org.scalatest._

import chisel3._
import chisel3.testers.BasicTester
import chisel3.util._
//import chisel3.core.ExplicitCompileOptions.Strict

class BlackBoxInverter extends BlackBox {
  val io = IO(new Bundle() {
    val in = Input(Bool())
    val out = Output(Bool())
  })
}

class BlackBoxPassthrough extends BlackBox {
  val io = IO(new Bundle() {
    val in = Input(Bool())
    val out = Output(Bool())
  })
}

class BlackBoxRegister extends BlackBox {
  val io = IO(new Bundle() {
    val clock = Input(Clock())
    val in = Input(Bool())
    val out = Output(Bool())
  })
}

class BlackBoxTester extends BasicTester {
  val blackBoxPos = Module(new BlackBoxInverter)
  val blackBoxNeg = Module(new BlackBoxInverter)

  blackBoxPos.io.in := UInt(1)
  blackBoxNeg.io.in := UInt(0)

  assert(blackBoxNeg.io.out === UInt(1))
  assert(blackBoxPos.io.out === UInt(0))
  stop()
}

/** Instantiate multiple BlackBoxes with similar interfaces but different
  * functionality. Used to detect failures in BlackBox naming and module
  * deduplication.
  */

class MultiBlackBoxTester extends BasicTester {
  val blackBoxInvPos = Module(new BlackBoxInverter)
  val blackBoxInvNeg = Module(new BlackBoxInverter)
  val blackBoxPassPos = Module(new BlackBoxPassthrough)
  val blackBoxPassNeg = Module(new BlackBoxPassthrough)

  blackBoxInvPos.io.in := UInt(1)
  blackBoxInvNeg.io.in := UInt(0)
  blackBoxPassPos.io.in := UInt(1)
  blackBoxPassNeg.io.in := UInt(0)

  assert(blackBoxInvNeg.io.out === UInt(1))
  assert(blackBoxInvPos.io.out === UInt(0))
  assert(blackBoxPassNeg.io.out === UInt(0))
  assert(blackBoxPassPos.io.out === UInt(1))
  stop()
}

class BlackBoxWithClockTester extends BasicTester {
  val blackBox = Module(new BlackBoxRegister)
  val model = Reg(Bool())

  val (cycles, end) = Counter(Bool(true), 15)

  val impetus = cycles(0)
  blackBox.io.clock := clock
  blackBox.io.in := impetus
  model := impetus

  when(cycles > UInt(0)) {
    assert(blackBox.io.out === model)
  }
  when(end) { stop() }
}

/*
// Must determine how to handle parameterized Verilog
class BlackBoxConstant(value: Int) extends BlackBox {
  val io = IO(new Bundle() {
    val out = Output(UInt(width=log2Up(value)))
  })
  override val name = s"#(WIDTH=${log2Up(value)},VALUE=$value) "
}

class BlackBoxWithParamsTester extends BasicTester {
  val blackBoxOne  = Module(new BlackBoxConstant(1))
  val blackBoxFour = Module(new BlackBoxConstant(4))

  val (cycles, end) = Counter(Bool(true), 4)

  assert(blackBoxOne.io.out  === UInt(1))
  assert(blackBoxFour.io.out === UInt(4))

  when(end) { stop() }
}
*/

class BlackBoxSpec extends ChiselFlatSpec {
  "A BlackBoxed inverter" should "work" in {
    assertTesterPasses({ new BlackBoxTester },
        Seq("/BlackBoxTest.v"))
  }
  "Multiple BlackBoxes" should "work" in {
    assertTesterPasses({ new MultiBlackBoxTester },
        Seq("/BlackBoxTest.v"))
  }
  "A BlackBoxed register" should "work" in {
    assertTesterPasses({ new BlackBoxWithClockTester },
        Seq("/BlackBoxTest.v"))
  }
}