Skip to content

Commit 6b162a1

Browse files
authored
Fix verilator custom transforms not being run (freechipsproject#127)
* Custom transforms were not being run when using verilator backend * Fix custom transforms not being passed to compileAndEmit correctly Brings VCSBackend more in line with VerilatorBackend Some nit cleanup on VCSBackend Fixed broken VecFillSpec
1 parent 9389343 commit 6b162a1

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/main/scala/chisel3/iotesters/VCSBackend.scala

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// See LICENSE for license details.
22
package chisel3.iotesters
33

4-
import scala.collection.mutable.HashMap
5-
import scala.util.Random
6-
import java.io.{File, FileWriter, IOException, PrintStream, Writer}
4+
import java.io.{File, FileWriter, IOException, Writer}
75
import java.nio.file.{FileAlreadyExistsException, Files, Paths}
86
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
97

108
import chisel3.{ChiselExecutionFailure, ChiselExecutionSuccess}
9+
import firrtl.{ChirrtlForm, CircuitState, Transform}
1110
import firrtl.annotations.CircuitName
1211
import firrtl.transforms.{BlackBoxSourceHelper, BlackBoxTargetDir}
1312

@@ -27,11 +26,10 @@ object copyVpiFiles {
2726
Files.createFile(vpiCppFilePath)
2827
Files.createFile(vpiTabFilePath)
2928
} catch {
30-
case x: FileAlreadyExistsException =>
29+
case _: FileAlreadyExistsException =>
3130
System.out.format("")
32-
case x: IOException => {
31+
case x: IOException =>
3332
System.err.format("createFile error: %s%n", x)
34-
}
3533
}
3634

3735
Files.copy(getClass.getResourceAsStream("/sim_api.h"), simApiHFilePath, REPLACE_EXISTING)
@@ -70,10 +68,10 @@ object genVCSVerilogHarness {
7068
writer write " reg [1023:0] vpdfile = 0;\n"
7169

7270
writer write "\n /*** DUT instantiation ***/\n"
73-
writer write s" ${dutName} ${dutName}(\n"
71+
writer write s" $dutName $dutName(\n"
7472
writer write " .clock(clock),\n"
7573
writer write " .reset(reset),\n"
76-
writer write ((inputs ++ outputs).unzip._2 map (name => s" .${name}(${name}_delay)") mkString ",\n")
74+
writer write ((inputs ++ outputs).unzip._2 map (name => s" .$name(${name}_delay)") mkString ",\n")
7775
writer write " );\n\n"
7876

7977
writer write " initial begin\n"
@@ -112,7 +110,7 @@ object genVCSVerilogHarness {
112110
writer write " $vcdplusflush;\n"
113111
writer write " end\n\n"
114112
writer write "endmodule\n"
115-
writer.close
113+
writer.close()
116114
}
117115
}
118116

@@ -130,24 +128,31 @@ private[iotesters] object setupVCSBackend {
130128

131129
val chirrtl = firrtl.Parser.parse(emitted)
132130
val dut = getTopModule(circuit).asInstanceOf[T]
133-
val nodes = getChiselNodes(circuit)
134-
val annotations = firrtl.AnnotationMap(optionsManager.firrtlOptions.annotations ++ List(
131+
132+
/*
133+
The following block adds an annotation that tells the black box helper where the
134+
current build directory is, so that it can copy verilog resource files into the right place
135+
*/
136+
val annotationMap = firrtl.AnnotationMap(optionsManager.firrtlOptions.annotations ++ List(
135137
firrtl.annotations.Annotation(
136138
CircuitName(circuit.name),
137139
classOf[BlackBoxSourceHelper],
138140
BlackBoxTargetDir(optionsManager.targetDirName).serialize
139141
)
140142
))
141143

144+
val transforms = optionsManager.firrtlOptions.customTransforms
145+
142146
// Generate Verilog
143147
val verilogFile = new File(dir, s"${circuit.name}.v")
144148
val verilogWriter = new FileWriter(verilogFile)
145-
val verilogCompiler = new firrtl.VerilogCompiler
146-
verilogCompiler.compile(
147-
firrtl.CircuitState(chirrtl, firrtl.ChirrtlForm, Some(annotations)),
148-
verilogWriter,
149-
optionsManager.firrtlOptions.customTransforms
149+
150+
val compileResult = (new firrtl.VerilogCompiler).compileAndEmit(
151+
CircuitState(chirrtl, ChirrtlForm, Some(annotationMap)),
152+
customTransforms = transforms
150153
)
154+
val compiledStuff = compileResult.getEmittedCircuit
155+
verilogWriter.write(compiledStuff.value)
151156
verilogWriter.close()
152157

153158
// Generate Harness
@@ -160,8 +165,9 @@ private[iotesters] object setupVCSBackend {
160165

161166
val command = if(optionsManager.testerOptions.testCmd.nonEmpty) {
162167
optionsManager.testerOptions.testCmd
163-
} else {
164-
Seq(new File(dir, circuit.name).toString)
168+
}
169+
else {
170+
Seq(new File(dir, s"V${circuit.name}").toString)
165171
}
166172

167173
(dut, new VCSBackend(dut, command))

src/main/scala/chisel3/iotesters/VerilatorBackend.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ private[iotesters] object setupVerilatorBackend {
204204
val chirrtl = firrtl.Parser.parse(emitted)
205205
val dut = getTopModule(circuit).asInstanceOf[T]
206206

207+
/*
208+
The following block adds an annotation that tells the black box helper where the
209+
current build directory is, so that it can copy verilog resource files into the right place
210+
*/
207211
val annotationMap = firrtl.AnnotationMap(optionsManager.firrtlOptions.annotations ++ List(
208212
firrtl.annotations.Annotation(
209213
CircuitName(circuit.name),
@@ -212,19 +216,23 @@ private[iotesters] object setupVerilatorBackend {
212216
)
213217
))
214218

219+
val transforms = optionsManager.firrtlOptions.customTransforms
220+
215221
copyVerilatorHeaderFiles(optionsManager.targetDirName)
216222

217223
// Generate Verilog
218224
val verilogFile = new File(dir, s"${circuit.name}.v")
219225
val verilogWriter = new FileWriter(verilogFile)
220226

221227
val compileResult = (new firrtl.VerilogCompiler).compileAndEmit(
222-
CircuitState(chirrtl, ChirrtlForm, Some(annotationMap))
228+
CircuitState(chirrtl, ChirrtlForm, Some(annotationMap)),
229+
customTransforms = transforms
223230
)
224231
val compiledStuff = compileResult.getEmittedCircuit
225232
verilogWriter.write(compiledStuff.value)
226233
verilogWriter.close()
227234

235+
// Generate Harness
228236
val cppHarnessFileName = s"${circuit.name}-harness.cpp"
229237
val cppHarnessFile = new File(dir, cppHarnessFileName)
230238
val cppHarnessWriter = new FileWriter(cppHarnessFile)

src/test/scala/chisel3/iotesters/VecFillSpec.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ class VFTester(c: VF) extends PeekPokeTester(c) {
2424
expect(c.io.value, i)
2525
step(1)
2626
}
27-
for(i <- 11 until 111) {
28-
poke(c.io.addr, i)
29-
expect(c.io.value, i % 11)
30-
step(1)
31-
}
27+
// behavior of indexing past end of vec is undefined
3228
}
3329

3430
class VecFillSpec extends FreeSpec with Matchers {

0 commit comments

Comments
 (0)