summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/util/PriorityMuxSpec.scala
blob: 32cf24317c7797c7962a1fb1aaa8f589cbbcb184 (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
// SPDX-License-Identifier: Apache-2.0

package chiselTests.util

import chisel3._
import chisel3.util.{is, switch, Counter, PriorityMux}
import chisel3.testers.BasicTester
import chisel3.stage.ChiselStage.emitChirrtl

import chiselTests.ChiselFlatSpec

class PriorityMuxTester extends BasicTester {

  val sel = Wire(UInt(3.W))
  sel := 0.U // default

  val elts = Seq(5.U, 6.U, 7.U)
  val muxed = PriorityMux(sel, elts)

  // Priority is given to lowest order bit
  val tests = Seq(
    1.U -> elts(0),
    2.U -> elts(1),
    3.U -> elts(0),
    4.U -> elts(2),
    5.U -> elts(0),
    6.U -> elts(1),
    7.U -> elts(0)
  )
  val (cycle, done) = Counter(0 until tests.size + 1)

  for (((in, out), idx) <- tests.zipWithIndex) {
    when(cycle === idx.U) {
      sel := in
      assert(muxed === out)
    }
  }

  when(done) {
    stop()
  }
}

class PriorityMuxSpec extends ChiselFlatSpec {
  behavior.of("PriorityMux")

  it should "be functionally correct" in {
    assertTesterPasses(new PriorityMuxTester)
  }

  it should "be stack safe" in {
    emitChirrtl(new RawModule {
      val n = 1 << 15
      val in = IO(Input(Vec(n, UInt(8.W))))
      val sel = IO(Input(UInt(n.W)))
      val out = IO(Output(UInt(8.W)))
      out := PriorityMux(sel, in)
    })
  }
}