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

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage

trait VecToTargetSpecUtils extends Utils {
  this: ChiselFunSpec =>

  class Foo extends RawModule {
    val vec = IO(Input(Vec(4, Bool())))

    // Index a Vec with a Scala literal.
    val scalaLit = 0
    val vecSubaccessScalaLit = vec(scalaLit)

    // Index a Vec with a Chisel literal.
    val chiselLit = 0.U
    val vecSubaccessChiselLit = vec(chiselLit)

    // Index a Vec with a node.
    val node = IO(Input(UInt(2.W)))
    val vecSubaccessNode = vec(node)

    // Put an otherwise un-targetable Vec subaccess into a temp.
    val vecSubaccessTmp = WireInit(vecSubaccessNode)
  }

  val expectedError = "You cannot target Vec subaccess:"

  def conversionSucceeds(data: InstanceId) = {
    describe(".toTarget") {
      it("should convert successfully") {
        data.toTarget
      }
    }

    describe(".toNamed") {
      it("should convert successfully") {
        data.toNamed
      }
    }
  }

  def conversionFails(data: InstanceId) = {
    describe(".toTarget") {
      it("should fail to convert with a useful error message") {
        (the[ChiselException] thrownBy extractCause[ChiselException] {
          data.toTarget
        }).getMessage should include(expectedError)
      }
    }

    describe(".toNamed") {
      it("should fail to convert with a useful error message") {
        (the[ChiselException] thrownBy extractCause[ChiselException] {
          data.toNamed
        }).getMessage should include(expectedError)
      }
    }
  }
}

class VecToTargetSpec extends ChiselFunSpec with VecToTargetSpecUtils {
  describe("Vec subaccess") {
    var foo: Foo = null
    ChiselStage.elaborate { foo = new Foo; foo }

    describe("with a Scala literal") {
      (it should behave).like(conversionSucceeds(foo.vecSubaccessScalaLit))
    }

    describe("with a Chisel literal") {
      (it should behave).like(conversionFails(foo.vecSubaccessChiselLit))
    }

    describe("with a Node") {
      (it should behave).like(conversionFails(foo.vecSubaccessNode))
    }

    describe("with an un-targetable construct that is assigned to a temporary") {
      (it should behave).like(conversionSucceeds(foo.vecSubaccessTmp))
    }
  }
}