-
Notifications
You must be signed in to change notification settings - Fork 603
/
OneHotMuxSpec.scala
317 lines (259 loc) · 7.32 KB
/
OneHotMuxSpec.scala
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// See LICENSE for license details.
package chiselTests
import chisel3._
import chisel3.experimental.FixedPoint
import chisel3.internal.ChiselException
import chisel3.testers.BasicTester
import chisel3.util.{Mux1H, UIntToOH}
import org.scalatest._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
class OneHotMuxSpec extends AnyFreeSpec with Matchers with ChiselRunners {
"simple one hot mux with uint should work" in {
assertTesterPasses(new SimpleOneHotTester)
}
"simple one hot mux with sint should work" in {
assertTesterPasses(new SIntOneHotTester)
}
"simple one hot mux with fixed point should work" in {
assertTesterPasses(new FixedPointOneHotTester)
}
"simple one hot mux with all same fixed point should work" in {
assertTesterPasses(new AllSameFixedPointOneHotTester)
}
"simple one hot mux with all same parameterized sint values should work" in {
val values: Seq[SInt] = Seq((-3).S, (-5).S, (-7).S, (-11).S)
assertTesterPasses(new ParameterizedOneHotTester(values, SInt(8.W), -5.S(8.W)))
}
"simple one hot mux with all same parameterized aggregates containing fixed values should work" in {
assertTesterPasses(new ParameterizedAggregateOneHotTester)
}
"simple one hot mux with all aggregates containing inferred width fixed values should NOT work" in {
intercept [ChiselException] {
assertTesterPasses(new InferredWidthAggregateOneHotTester)
}
}
"simple one hot mux with all fixed width bundles but with different bundles should Not work" in {
try {
assertTesterPasses(new DifferentBundleOneHotTester)
} catch {
case a: ChiselException => a.getCause match {
case _: IllegalArgumentException =>
}
}
}
"UIntToOH with output width greater than 2^(input width)" in {
assertTesterPasses(new UIntToOHTester)
}
"UIntToOH should not accept width of zero (until zero-width wires are fixed" in {
try {
assertTesterPasses(new BasicTester {
val out = UIntToOH(0.U, 0)
})
} catch {
case a: ChiselException => a.getCause match {
case _: IllegalArgumentException =>
}
}
}
}
class SimpleOneHotTester extends BasicTester {
val out = Wire(UInt())
out := Mux1H(Seq(
false.B -> 2.U,
false.B -> 4.U,
true.B -> 8.U,
false.B -> 11.U
))
assert(out === 8.U)
stop()
}
class SIntOneHotTester extends BasicTester {
val out = Wire(SInt())
out := Mux1H(Seq(
false.B -> (-3).S,
true.B -> (-5).S,
false.B -> (-7).S,
false.B -> (-11).S
))
assert(out === (-5).S)
stop()
}
class FixedPointOneHotTester extends BasicTester {
val out = Wire(FixedPoint(8.W, 4.BP))
out := Mux1H(Seq(
false.B -> (-1.5).F(1.BP),
true.B -> (-2.25).F(2.BP),
false.B -> (-4.125).F(3.BP),
false.B -> (-11.625).F(3.BP)
))
assert(out === (-2.25).F(4.BP))
stop()
}
class AllSameFixedPointOneHotTester extends BasicTester {
val out = Wire(FixedPoint(12.W, 3.BP))
out := Mux1H(Seq(
false.B -> (-1.5).F(12.W, 3.BP),
true.B -> (-2.25).F(12.W, 3.BP),
false.B -> (-4.125).F(12.W, 3.BP),
false.B -> (-11.625).F(12.W, 3.BP)
))
assert(out === (-2.25).F(14.W, 4.BP))
stop()
}
class ParameterizedOneHotTester[T <: Data](values: Seq[T], outGen: T, expected: T) extends BasicTester {
val dut = Module(new ParameterizedOneHot(values, outGen))
dut.io.selectors(0) := false.B
dut.io.selectors(1) := true.B
dut.io.selectors(2) := false.B
dut.io.selectors(3) := false.B
assert(dut.io.out.asUInt() === expected.asUInt())
stop()
}
class Agg1 extends Bundle {
val v = Vec(2, FixedPoint(8.W, 4.BP))
val a = new Bundle {
val f1 = FixedPoint(7.W, 3.BP)
val f2 = FixedPoint(9.W, 5.BP)
}
}
object Agg1 extends HasMakeLit[Agg1] {
def makeLit(n: Int): Agg1 = {
val x = n.toDouble / 4.0
val (d: Double, e: Double, f: Double, g: Double) = (x, x * 2.0, x * 3.0, x * 4.0)
val w = Wire(new Agg1)
w.v(0) := d.F(4.BP)
w.v(1) := e.F(4.BP)
w.a.f1 := f.F(3.BP)
w.a.f2 := g.F(5.BP)
w
}
}
class Agg2 extends Bundle {
val v = Vec(2, FixedPoint(8.W, 4.BP))
val a = new Bundle {
val f1 = FixedPoint(7.W, 3.BP)
val f2 = FixedPoint(9.W, 5.BP)
}
}
object Agg2 extends HasMakeLit[Agg2] {
def makeLit(n: Int): Agg2 = {
val x = n.toDouble / 4.0
val (d: Double, e: Double, f: Double, g: Double) = (x, x * 2.0, x * 3.0, x * 4.0)
val w = Wire(new Agg2)
w.v(0) := d.F(4.BP)
w.v(1) := e.F(4.BP)
w.a.f1 := f.F(3.BP)
w.a.f2 := g.F(5.BP)
w
}
}
class ParameterizedAggregateOneHotTester extends BasicTester {
val values = (0 until 4).map { n => Agg1.makeLit(n) }
val dut = Module(new ParameterizedAggregateOneHot(Agg1, new Agg1))
dut.io.selectors(0) := false.B
dut.io.selectors(1) := true.B
dut.io.selectors(2) := false.B
dut.io.selectors(3) := false.B
assert(dut.io.out.asUInt() === values(1).asUInt())
stop()
}
trait HasMakeLit[T] {
def makeLit(n: Int): T
}
class ParameterizedOneHot[T <: Data](values: Seq[T], outGen: T) extends Module {
val io = IO(new Bundle {
val selectors = Input(Vec(4, Bool()))
val out = Output(outGen)
})
val terms = io.selectors.zip(values)
io.out := Mux1H(terms)
}
class ParameterizedAggregateOneHot[T <: Data](valGen: HasMakeLit[T], outGen: T) extends Module {
val io = IO(new Bundle {
val selectors = Input(Vec(4, Bool()))
val out = Output(outGen)
})
val values = (0 until 4).map { n => valGen.makeLit(n) }
val terms = io.selectors.zip(values)
io.out := Mux1H(terms)
}
class Bundle1 extends Bundle {
val a = FixedPoint()
val b = new Bundle {
val c = FixedPoint()
}
}
class InferredWidthAggregateOneHotTester extends BasicTester {
val b0 = Wire(new Bundle1)
b0.a := -0.25.F(2.BP)
b0.b.c := -0.125.F(3.BP)
val b1 = Wire(new Bundle1)
b1.a := -0.0625.F(3.BP)
b1.b.c := -0.03125.F(4.BP)
val b2 = Wire(new Bundle1)
b2.a := -0.015625.F(5.BP)
b2.b.c := -0.0078125.F(6.BP)
val b3 = Wire(new Bundle1)
b3.a := -0.0078125.F(7.BP)
b3.b.c := -0.00390625.F(8.BP)
val o1 = Mux1H(Seq(
false.B -> b0,
false.B -> b1,
true.B -> b2,
false.B -> b3
))
assert(o1.a === -0.015625.F(5.BP))
assert(o1.b.c === -0.0078125.F(6.BP))
val o2 = Mux1H(Seq(
false.B -> b0,
true.B -> b1,
false.B -> b2,
false.B -> b3
))
assert(o2.a === -0.0625.F(3.BP))
assert(o2.b.c === -0.03125.F(4.BP))
stop()
}
class Bundle2 extends Bundle {
val a = FixedPoint(10.W, 4.BP)
val b = new Bundle {
val c = FixedPoint(10.W, 4.BP)
}
}
class Bundle3 extends Bundle {
val a = FixedPoint(10.W, 4.BP)
val b = new Bundle {
val c = FixedPoint(10.W, 4.BP)
}
}
class DifferentBundleOneHotTester extends BasicTester {
val b0 = Wire(new Bundle2)
b0.a := -0.25.F(2.BP)
b0.b.c := -0.125.F(3.BP)
val b1 = Wire(new Bundle2)
b1.a := -0.0625.F(3.BP)
b1.b.c := -0.03125.F(4.BP)
val b2 = Wire(new Bundle3)
b2.a := -0.015625.F(5.BP)
b2.b.c := -0.0078125.F(6.BP)
val b3 = Wire(new Bundle3)
b3.a := -0.0078125.F(7.BP)
b3.b.c := -0.00390625.F(8.BP)
val o1 = Mux1H(Seq(
false.B -> b0,
false.B -> b1,
true.B -> b2,
false.B -> b3
))
stop()
}
class UIntToOHTester extends BasicTester {
val out = UIntToOH(1.U, 3)
require(out.getWidth == 3)
assert(out === 2.U)
val out2 = UIntToOH(0.U, 1)
require(out2.getWidth == 1)
assert(out2 === 1.U)
stop()
}