summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/experimental/DataViewIntegrationSpec.scala
blob: 4704a94273ed7f709ec17de8edcd873e2ba00ba3 (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
// See LICENSE for license details.

package chiselTests.experimental

import chisel3._
import chisel3.experimental.{BaseModule, ExtModule}
import chisel3.experimental.dataview._
import chisel3.util.{log2Ceil, Decoupled, DecoupledIO, Queue, QueueIO}
import chiselTests.ChiselFlatSpec
import firrtl.transforms.DontTouchAnnotation

// Let's put it all together!
object DataViewIntegrationSpec {

  class QueueIntf[T <: Data](gen: T, entries: Int) extends Bundle {
    val ports = new QueueIO(gen, entries)
    // Let's grab a reference to something internal too
    // Output because can't have directioned and undirectioned stuff
    val enq_ptr = Output(UInt(log2Ceil(entries).W))
  }

  // It's not clear if a view of a Module ever _can_ be total since internal nodes are part of the Module
  implicit def queueView[T <: Data] = PartialDataView[Queue[T], QueueIntf[T]](
    q => new QueueIntf(q.gen, q.entries),
    _.io -> _.ports,
    // Some token internal signal
    _.enq_ptr.value -> _.enq_ptr
  )

  object MyQueue {
    def apply[T <: Data](enq: DecoupledIO[T], n: Int): QueueIntf[T] = {
      val queue = Module(new Queue[T](enq.bits.cloneType, n))
      val view = queue.viewAs[QueueIntf[T]]
      view.ports.enq <> enq
      view
    }
  }

  class MyModule extends Module {
    val enq = IO(Flipped(Decoupled(UInt(8.W))))
    val deq = IO(Decoupled(UInt(8.W)))

    val queue = MyQueue(enq, 4)
    deq <> queue.ports.deq
    dontTouch(queue.enq_ptr)
  }
}

class DataViewIntegrationSpec extends ChiselFlatSpec {
  import DataViewIntegrationSpec.MyModule

  "Users" should "be able to view and annotate Modules" in {
    val (_, annos) = getFirrtlAndAnnos(new MyModule)
    val ts = annos.collect { case DontTouchAnnotation(t) => t.serialize }
    ts should equal(Seq("~MyModule|Queue>enq_ptr_value"))
  }
}