Skip to content

Commit 9ad6c74

Browse files
chickjackkoenig
authored andcommitted
Connecting basic types wrong should error in chisel (#497)
1 parent 7d08528 commit 9ad6c74

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

chiselFrontend/src/main/scala/chisel3/core/MonoConnect.scala

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,31 @@ object MonoConnect {
5555
* during the recursive decent and then rethrow them with extra information added.
5656
* This gives the user a 'path' to where in the connections things went wrong.
5757
*/
58-
def connect(sourceInfo: SourceInfo, connectCompileOptions: CompileOptions, sink: Data, source: Data, context_mod: UserModule): Unit =
58+
//scalastyle:off cyclomatic.complexity method.length
59+
def connect(
60+
sourceInfo: SourceInfo,
61+
connectCompileOptions: CompileOptions,
62+
sink: Data,
63+
source: Data,
64+
context_mod: UserModule): Unit =
5965
(sink, source) match {
60-
// Handle element case (root case)
61-
case (sink_e: Element, source_e: Element) => {
66+
67+
// Handle legal element cases, note (Bool, Bool) is caught by the first two, as Bool is a UInt
68+
case (sink_e: Bool, source_e: UInt) =>
6269
elemConnect(sourceInfo, connectCompileOptions, sink_e, source_e, context_mod)
63-
// TODO(twigg): Verify the element-level classes are connectable
64-
}
70+
case (sink_e: UInt, source_e: Bool) =>
71+
elemConnect(sourceInfo, connectCompileOptions, sink_e, source_e, context_mod)
72+
case (sink_e: UInt, source_e: UInt) =>
73+
elemConnect(sourceInfo, connectCompileOptions, sink_e, source_e, context_mod)
74+
case (sink_e: SInt, source_e: SInt) =>
75+
elemConnect(sourceInfo, connectCompileOptions, sink_e, source_e, context_mod)
76+
case (sink_e: FixedPoint, source_e: FixedPoint) =>
77+
elemConnect(sourceInfo, connectCompileOptions, sink_e, source_e, context_mod)
78+
case (sink_e: Clock, source_e: Clock) =>
79+
elemConnect(sourceInfo, connectCompileOptions, sink_e, source_e, context_mod)
80+
6581
// Handle Vec case
66-
case (sink_v: Vec[Data @unchecked], source_v: Vec[Data @unchecked]) => {
82+
case (sink_v: Vec[Data @unchecked], source_v: Vec[Data @unchecked]) =>
6783
if(sink_v.length != source_v.length) { throw MismatchedVecException }
6884
for(idx <- 0 until sink_v.length) {
6985
try {
@@ -73,9 +89,9 @@ object MonoConnect {
7389
case MonoConnectException(message) => throw MonoConnectException(s"($idx)$message")
7490
}
7591
}
76-
}
92+
7793
// Handle Record case
78-
case (sink_r: Record, source_r: Record) => {
94+
case (sink_r: Record, source_r: Record) =>
7995
// For each field, descend with right
8096
for((field, sink_sub) <- sink_r.elements) {
8197
try {
@@ -91,7 +107,7 @@ object MonoConnect {
91107
case MonoConnectException(message) => throw MonoConnectException(s".$field$message")
92108
}
93109
}
94-
}
110+
95111
// Sink and source are different subtypes of data so fail
96112
case (sink, source) => throw MismatchedException(sink.toString, source.toString)
97113
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// See LICENSE for license details.
2+
3+
package chiselTests
4+
5+
import chisel3._
6+
import chisel3.experimental.{FixedPoint, Analog}
7+
import chisel3.testers.BasicTester
8+
9+
abstract class CrossCheck extends Bundle {
10+
val in: Data
11+
val out: Data
12+
}
13+
14+
class CrossConnects(inType: Data, outType: Data) extends Module {
15+
val io = IO(new Bundle {
16+
val in = Input(inType)
17+
val out = Output(outType)
18+
})
19+
io.out := io.in
20+
}
21+
22+
class CrossConnectTester(inType: Data, outType: Data) extends BasicTester {
23+
val dut = Module(new CrossConnects(inType, outType))
24+
stop()
25+
}
26+
27+
class ConnectSpec extends ChiselPropSpec {
28+
property("SInt := SInt should succeed") {
29+
assertTesterPasses{ new CrossConnectTester(SInt(16.W), SInt(16.W)) }
30+
}
31+
property("SInt := UInt should fail") {
32+
intercept[ChiselException]{ new CrossConnectTester(UInt(16.W), SInt(16.W)) }
33+
}
34+
property("SInt := FixedPoint should fail") {
35+
intercept[ChiselException]{ new CrossConnectTester(FixedPoint(16.W, 8.BP), UInt(16.W)) }
36+
}
37+
property("UInt := UInt should succeed") {
38+
assertTesterPasses{ new CrossConnectTester(UInt(16.W), UInt(16.W)) }
39+
}
40+
property("UInt := SInt should fail") {
41+
intercept[ChiselException]{ new CrossConnectTester(SInt(16.W), UInt(16.W)) }
42+
}
43+
property("UInt := FixedPoint should fail") {
44+
intercept[ChiselException]{ new CrossConnectTester(FixedPoint(16.W, 8.BP), UInt(16.W)) }
45+
}
46+
47+
property("Clock := Clock should succeed") {
48+
assertTesterPasses{ new CrossConnectTester(Clock(), Clock()) }
49+
}
50+
property("Clock := UInt should fail") {
51+
intercept[ChiselException]{ new CrossConnectTester(Clock(), UInt(16.W)) }
52+
}
53+
54+
property("FixedPoint := FixedPoint should succeed") {
55+
assertTesterPasses{ new CrossConnectTester(FixedPoint(16.W, 8.BP), FixedPoint(16.W, 8.BP)) }
56+
}
57+
property("FixedPoint := SInt should fail") {
58+
intercept[ChiselException]{ new CrossConnectTester(SInt(16.W), FixedPoint(16.W, 8.BP)) }
59+
}
60+
property("FixedPoint := UInt should fail") {
61+
intercept[ChiselException]{ new CrossConnectTester(UInt(16.W), FixedPoint(16.W, 8.BP)) }
62+
}
63+
64+
property("Analog := Analog should fail") {
65+
intercept[ChiselException]{ new CrossConnectTester(Analog(16.W), Analog(16.W)) }
66+
}
67+
property("Analog := FixedPoint should fail") {
68+
intercept[ChiselException]{ new CrossConnectTester(Analog(16.W), FixedPoint(16.W, 8.BP)) }
69+
}
70+
property("FixedPoint := Analog should fail") {
71+
intercept[ChiselException]{ new CrossConnectTester(FixedPoint(16.W, 8.BP), Analog(16.W)) }
72+
}
73+
property("Analog := UInt should fail") {
74+
intercept[ChiselException]{ new CrossConnectTester(Analog(16.W), UInt(16.W)) }
75+
}
76+
property("Analog := SInt should fail") {
77+
intercept[ChiselException]{ new CrossConnectTester(Analog(16.W), SInt(16.W)) }
78+
}
79+
property("UInt := Analog should fail") {
80+
intercept[ChiselException]{ new CrossConnectTester(UInt(16.W), Analog(16.W)) }
81+
}
82+
property("SInt := Analog should fail") {
83+
intercept[ChiselException]{ new CrossConnectTester(SInt(16.W), Analog(16.W)) }
84+
}
85+
}

0 commit comments

Comments
 (0)