summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/DecoupledSpec.scala
blob: 69d74aabba6199157b6eecbbfddd2055ac7255da (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
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage
import chisel3.util.Decoupled

class DecoupledSpec extends ChiselFlatSpec {
  "Decoupled() and Decoupled.empty" should "give DecoupledIO with empty payloads" in {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val in = Flipped(Decoupled())
        val out = Decoupled.empty
      })
      io.out <> io.in
      assert(io.asUInt.widthOption.get === 4)
    })
  }

  "Decoupled.map" should "apply a function to a wrapped Data" in {
    val chirrtl = ChiselStage
      .emitChirrtl(new Module {
        val enq = IO(Flipped(Decoupled(UInt(8.W))))
        val deq = IO(Decoupled(UInt(8.W)))
        deq <> enq.map(_ + 1.U)
      })

    // Check for data assignment
    chirrtl should include("""node _deq_map_bits_T = add(enq.bits, UInt<1>("h1")""")
    chirrtl should include("""node _deq_map_bits = tail(_deq_map_bits_T, 1)""")
    chirrtl should include("""_deq_map.bits <= _deq_map_bits""")
    chirrtl should include("""deq <= _deq_map""")

    // Check for back-pressure (ready signal is driven in the opposite direction of bits + valid)
    chirrtl should include("""enq.ready <= _deq_map.ready""")
  }

  "Decoupled.map" should "apply a function to a wrapped Bundle" in {
    class TestBundle extends Bundle {
      val foo = UInt(8.W)
      val bar = UInt(8.W)
      val fizz = Bool()
      val buzz = Bool()
    }

    // Add one to foo, subtract one from bar, set fizz to false and buzz to true
    def func(t: TestBundle): TestBundle = {
      val res = Wire(new TestBundle)

      res.foo := t.foo + 1.U
      res.bar := t.bar - 1.U
      res.fizz := false.B
      res.buzz := true.B

      res
    }

    val chirrtl = ChiselStage
      .emitChirrtl(new Module {
        val enq = IO(Flipped(Decoupled(new TestBundle)))
        val deq = IO(Decoupled(new TestBundle))
        deq <> enq.map(func)
      })

    // Check for data assignment
    chirrtl should include("""wire _deq_map_bits : { foo : UInt<8>, bar : UInt<8>, fizz : UInt<1>, buzz : UInt<1>}""")

    chirrtl should include("""node _deq_map_bits_res_foo_T = add(enq.bits.foo, UInt<1>("h1")""")
    chirrtl should include("""node _deq_map_bits_res_foo_T_1 = tail(_deq_map_bits_res_foo_T, 1)""")
    chirrtl should include("""_deq_map_bits.foo <= _deq_map_bits_res_foo_T_1""")

    chirrtl should include("""node _deq_map_bits_res_bar_T = sub(enq.bits.bar, UInt<1>("h1")""")
    chirrtl should include("""node _deq_map_bits_res_bar_T_1 = tail(_deq_map_bits_res_bar_T, 1)""")
    chirrtl should include("""_deq_map_bits.bar <= _deq_map_bits_res_bar_T_1""")

    chirrtl should include("""_deq_map_bits.fizz <= UInt<1>("h0")""")
    chirrtl should include("""_deq_map_bits.buzz <= UInt<1>("h1")""")

    chirrtl should include("""_deq_map.bits <= _deq_map_bits""")
    chirrtl should include("""deq <= _deq_map""")

    // Check for back-pressure (ready signal is driven in the opposite direction of bits + valid)
    chirrtl should include("""enq.ready <= _deq_map.ready""")
  }

  "Decoupled.map" should "apply a function to a wrapped Bundle and return a different typed DecoupledIO" in {
    class TestBundle extends Bundle {
      val foo = UInt(8.W)
      val bar = UInt(8.W)
    }

    val chirrtl = ChiselStage
      .emitChirrtl(new Module {
        val enq = IO(Flipped(Decoupled(new TestBundle)))
        val deq = IO(Decoupled(UInt(8.W)))
        deq <> enq.map(bundle => bundle.foo & bundle.bar)
      })

    // Check that the _map wire wraps a UInt and not a TestBundle
    chirrtl should include("""wire _deq_map : { flip ready : UInt<1>, valid : UInt<1>, bits : UInt<8>}""")

    // Check for data assignment
    chirrtl should include("""node _deq_map_bits = and(enq.bits.foo, enq.bits.bar)""")
    chirrtl should include("""_deq_map.bits <= _deq_map_bits""")
    chirrtl should include("""deq <= _deq_map""")

    // Check for back-pressure (ready signal is driven in the opposite direction of bits + valid)
    chirrtl should include("""enq.ready <= _deq_map.ready""")
  }
}