blob: a16689147857f267152a7dc7d475afa68f5aded3 (
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// SPDX-License-Identifier: Apache-2.0
package chiselTests
import chisel3._
import chisel3.experimental.{DataMirror, FixedPoint}
import chisel3.testers.BasicTester
class AsTypeOfBundleTester extends BasicTester {
class MultiTypeBundle extends Bundle {
val u = UInt(4.W)
val s = SInt(4.W)
val fp = FixedPoint(4.W, 3.BP)
}
val bun = new MultiTypeBundle
val bunAsTypeOf = ((4 << 8) + (15 << 4) + (12 << 0)).U.asTypeOf(bun)
assert(bunAsTypeOf.u === 4.U)
assert(bunAsTypeOf.s === -1.S)
assert(bunAsTypeOf.fp === FixedPoint.fromDouble(-0.5, 4.W, 3.BP))
stop()
}
class AsTypeOfBundleZeroWidthTester extends BasicTester {
class ZeroWidthBundle extends Bundle {
val a = UInt(0.W)
val b = UInt(1.W)
val c = UInt(0.W)
}
val bun = new ZeroWidthBundle
val bunAsTypeOf = 1.U.asTypeOf(bun)
assert(bunAsTypeOf.a === 0.U)
assert(bunAsTypeOf.b === 1.U)
assert(bunAsTypeOf.c === 0.U)
stop()
}
class AsTypeOfVecTester extends BasicTester {
val vec = ((15 << 12) + (0 << 8) + (1 << 4) + (2 << 0)).U.asTypeOf(Vec(4, SInt(4.W)))
assert(vec(0) === 2.S)
assert(vec(1) === 1.S)
assert(vec(2) === 0.S)
assert(vec(3) === -1.S)
stop()
}
class AsTypeOfTruncationTester extends BasicTester {
val truncate = (64 + 3).U.asTypeOf(UInt(3.W))
val expand = 1.U.asTypeOf(UInt(3.W))
assert(DataMirror.widthOf(truncate).get == 3)
assert(truncate === 3.U)
assert(DataMirror.widthOf(expand).get == 3)
assert(expand === 1.U)
stop()
}
class ResetAsTypeOfBoolTester extends BasicTester {
assert(reset.asTypeOf(Bool()) === reset.asBool)
stop()
}
class AsTypeOfClockTester extends BasicTester {
class MyBundle extends Bundle {
val x = UInt(4.W)
val y = Clock()
}
assert(true.B.asTypeOf(Clock()).asUInt.asBool === true.B)
assert(0x1f.U.asTypeOf(new MyBundle).asUInt === 0x1f.U)
stop()
}
class AsChiselEnumTester extends BasicTester {
object MyEnum extends ChiselEnum {
val foo, bar = Value
val fizz = Value(2.U)
}
class MyBundle extends Bundle {
val a = Bool()
val b = Bool()
}
// To
assert(2.U.asTypeOf(MyEnum()) === MyEnum.fizz)
assert(VecInit(2.U.asBools).asTypeOf(MyEnum()) === MyEnum.fizz)
assert(2.U.asTypeOf(new MyBundle).asTypeOf(MyEnum()) === MyEnum.fizz)
// From
assert(MyEnum.foo.asUInt === 0.U)
val vec = MyEnum.bar.asTypeOf(Vec(2, Bool()))
assert(vec(0) === 1.U)
assert(vec(1) === 0.U)
val bun = MyEnum.fizz.asTypeOf(new MyBundle)
assert(bun.b === 0.U)
assert(bun.a === 1.U)
// In aggregate
class OtherBundle extends Bundle {
val enum = MyEnum()
val foo = Bool()
}
val wire = Wire(new OtherBundle)
wire.enum := MyEnum.fizz
wire.foo := true.B
assert(wire.asUInt === 5.U)
val other = 5.U.asTypeOf(new OtherBundle)
assert(other.enum === MyEnum.fizz)
assert(other.foo === true.B)
stop()
}
class AsTypeOfSpec extends ChiselFlatSpec {
behavior.of("asTypeOf")
it should "work with Bundles containing Bits Types" in {
assertTesterPasses { new AsTypeOfBundleTester }
}
it should "work with Bundles that have fields of zero width" in {
assertTesterPasses { new AsTypeOfBundleZeroWidthTester }
}
it should "work with Vecs containing Bits Types" in {
assertTesterPasses { new AsTypeOfVecTester }
}
it should "expand and truncate UInts of different width" in {
assertTesterPasses { new AsTypeOfTruncationTester }
}
it should "work for casting implicit Reset to Bool" in {
assertTesterPasses { new ResetAsTypeOfBoolTester }
}
it should "work for casting to and from ChiselEnums" in {
assertTesterPasses(new AsChiselEnumTester)
}
it should "work for casting to and from Clock" in {
assertTesterPasses(new AsTypeOfClockTester)
}
}
|