新建SBT工程
sbt.version = 1.2.8
name := "ScalaExample"
//
version := "0.1"
//
scalaVersion := "2.12.8"
//
//--------------------------------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------------------------------//
//
crossScalaVersions := Seq("2.12.8")
//
scalacOptions ++= Seq("-deprecation", "-feature", "-unchecked", "-language:reflectiveCalls")
//
// Provide a managed dependency on X if -DXVersion="" is supplied on the command line.
// The following are the current "release" versions.
val defaultVersions = Map(
"chisel3" -> "3.1.+",
"chisel-iotesters" -> "1.2.+",
"treadle" -> "1.0.1"
)
//
//网络jar包
libraryDependencies ++= (Seq("chisel3","chisel-iotesters").map {
dep: String => "edu.berkeley.cs" %% dep % sys.props.getOrElse(dep + "Version", defaultVersions(dep)) })
//
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.1" % "test"
)
//
//本地jar包
unmanagedJars in Compile ++= Seq(
Attributed.blank[File](file(baseDirectory.value + "/libs/log4j-1.2.16.jar")),
Attributed.blank[File](file(baseDirectory.value + "/libs/slf4j-api-1.7.25.jar")),
Attributed.blank[File](file(baseDirectory.value + "/libs/slf4j-nop-1.7.25.jar"))
)
//
//不上传snapshots
//resolvers ++= Seq(
// Resolver.sonatypeRepo("snapshots"),
// Resolver.sonatypeRepo("releases")
//)
//
// Recommendations from http://www.scalatest.org/user_guide/using_scalatest_with_sbt
//logBuffered in Test := true
//
// Disable parallel execution when running tests.
// Running tests in parallel on Jenkins currently fails.
//parallelExecution in Test := false
//
//publishMavenStyle := true
//
//publishArtifact in Test := true
//
pomIncludeRepository := { x => false }
//
pomExtra := (
http://chisel.eecs.berkeley.edu/
BSD-style
http://www.opensource.org/licenses/bsd-license.php
repo
https://github.com/ucb-bar/chisel-testers2.git
scm:git:github.com/ucb-bar/chisel-testers2.git
)
// Recommendations from http://www.scalatest.org/user_guide/using_scalatest_with_sbt
//logBuffered in Test := true
//
// Disable parallel execution when running tests.
// Running tests in parallel on JeparallelExecution in Test := false
//
//publishMavenStyle := true
//
//publishArtifact in Test := true
//
//pomIncludeRepository := { x => false }
//
//pomExtra := (
// http://chisel.eecs.berkeley.edu/
//
//
// BSD-style
// http://www.opensource.org/licenses/bsd-license.php
// repo
//
//
//
// https://github.com/ucb-bar/chisel-testers2.git
// scm:git:github.com/ucb-bar/chisel-testers2.git
//
// )nkins currently fails.
//
//
//publishTo := {
// val v = version.value
// val nexus = "https://oss.sonatype.org/"
// if (v.trim.endsWith("SNAPSHOT")) {
// Some("snapshots" at nexus + "content/repositories/snapshots")
// }
// else {
// Some("releases" at nexus + "service/local/staging/deploy/maven2")
// }
//}
//
//trapExit := false
测试Chisel3用例:
import chisel3._
import chisel3.iotesters.{ChiselFlatSpec, SteppedHWIOTester}
import chisel3.testers.TesterDriver
class MyBundle(val w: Int) extends Bundle {
val in0 = Input(UInt(w.W))
val in1 = Input(UInt(w.W))
val out = Output(UInt(w.W))
}
//Adder
class Adder(val w: Int) extends Module {
val io = IO(new MyBundle(w))
io.out := io.in0 + io.in1
}
//TestBench
class AdderTester extends SteppedHWIOTester {
//测试8位宽的加法器
val device_under_test = Module( new Adder(8) )
val c = device_under_test
//打印全部
enable_all_debug = true
//随机种子
rnd.setSeed(0L)
//循环三次
for (i <- 0 until 3) {
val in0 = rnd.nextInt(1 << c.w)
val in1 = rnd.nextInt(1 << c.w)
poke(c.io.in0, in0)
poke(c.io.in1, in1)
expect(c.io.out, (in0 + in1) & ((1 << c.w) - 1))
//
step(10)
}
}
class AdderSpec extends ChiselFlatSpec {
"Adder" should "compile and run without incident" in {
assertTesterPasses { new AdderTester }
}
}
//-----------------------------------------------------------//
//测试Chisel3加法器
object Adder {
//
def main(args: Array[String]): Unit = {
TesterDriver.execute {
() => new AdderTester
}
}
}
//-----------------------------------------------------------//
启动SBT-Shell,再运行run:
@echo off
echo "sbt-shell"
:: "C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=localhost:59195,suspend=n,server=y -Xdebug -server -Xmx1536M -XX:MaxPermSize=384M -Dfile.encoding=UTF-8 -Didea.runid=2017.2 -Didea.managed=true -jar "D:\Android\IntelliJ IDEA Community Edition 2017.3.4\plugins\Scala\launcher\sbt-launch.jar" idea-shell
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=localhost:59195,suspend=n,server=y -Xdebug -server -Xmx1536M -XX:MaxPermSize=384M -Didea.runid=2017.2 -Didea.managed=true -jar "D:\Android\IntelliJ IDEA Community Edition 2017.3.4\plugins\Scala\launcher\sbt-launch.jar" idea-shell
pause
运行测试:
"sbt-shell"
Listening for transport dt_socket at address: 59195
[info] Loading settings for project global-plugins from idea.sbt ...
[info] Loading global plugins from C:\Users\houwenbin\.sbt\1.0\plugins
[info] Loading project definition from D:\J2EE\ScalaExample\project
[info] Loading settings for project scalaexample from build.sbt ...
[info] Set current project to ScalaExample (in build file:/D:/J2EE/ScalaExample/)
[IJ]sbt:ScalaExample> run
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
Multiple main classes detected, select one to run:
[1] Adder
[2] Hello
Enter number: 1
[info] Running Adder
[info] [0.004] Elaborating design...
================================================================================
Device under test: io bundle
# Dir D/V Used Name Parent
--------------------------------------------------------------------------------
0 I y in0
1 I y in1
2 O y out
================================================================================
================================================================================
UnitTester state table
step in1 in0 out
--------------------------------------------------------------------------------
0 212 187 143
1 - - -
2 - - -
3 - - -
4 - - -
5 - - -
6 - - -
7 - - -
8 - - -
9 - - -
10 155 61 216
11 - - -
12 - - -
13 - - -
14 - - -
15 - - -
16 - - -
17 - - -
18 - - -
19 - - -
20 79 163 242
21 - - -
22 - - -
23 - - -
24 - - -
25 - - -
26 - - -
27 - - -
28 - - -
29 - - -
30 - - -
================================================================================
[info] [1.424] Done elaborating.
Total FIRRTL Compile Time: 899.3 ms
verilator --cc D:\J2EE\ScalaExample\test_run_dir\AdderTester\202004072142333006115844216379006/AdderTester.v --assert -Wno-fatal -Wno-WIDTH -Wno-STMTDLY --trace -O1 --top-module AdderTester +define+TOP_TYPE=VAdderTester +define+PRINTF_COND=!AdderTester.reset +define+STOP_COND=!AdderTester.reset -CFLAGS -Wno-undefined-bool-conversion -O1 -DTOP_TYPE=VAdderTester -DVL_USER_FINISH -include VAdderTester.h -Mdir D:\J2EE\ScalaExample\test_run_dir\AdderTester\202004072142333006115844216379006 --exe D:\J2EE\ScalaExample\test_run_dir\AdderTester\202004072142333006115844216379006\top.cpp
make: Entering directory '/d/J2EE/ScalaExample/test_run_dir/AdderTester/202004072142333006115844216379006'
g++ -I. -MMD -ID:/AI/asenv/msys2/usr/share/verilator/include -ID:/AI/asenv/msys2/usr/share/verilator/include/vltstd -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=1 -faligned-new -fcf-protection=none -Wno-bool-operation -Wno-sign-compare -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-variable -Wno-shadow -Wno-undefined-bool-conversion -O1 -DTOP_TYPE=VAdderTester -DVL_USER_FINISH -include VAdderTester.h -c -o D:\J2EE\ScalaExample\test_run_dir\AdderTester\202004072142333006115844216379006\top.o D:\J2EE\ScalaExample\test_run_dir\AdderTester\202004072142333006115844216379006\top.cpp
g++ -I. -MMD -ID:/AI/asenv/msys2/usr/share/verilator/include -ID:/AI/asenv/msys2/usr/share/verilator/include/vltstd -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=1 -faligned-new -fcf-protection=none -Wno-bool-operation -Wno-sign-compare -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-variable -Wno-shadow -Wno-undefined-bool-conversion -O1 -DTOP_TYPE=VAdderTester -DVL_USER_FINISH -include VAdderTester.h -c -o verilated.o D:/AI/asenv/msys2/usr/share/verilator/include/verilated.cpp
g++ -I. -MMD -ID:/AI/asenv/msys2/usr/share/verilator/include -ID:/AI/asenv/msys2/usr/share/verilator/include/vltstd -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=1 -faligned-new -fcf-protection=none -Wno-bool-operation -Wno-sign-compare -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-variable -Wno-shadow -Wno-undefined-bool-conversion -O1 -DTOP_TYPE=VAdderTester -DVL_USER_FINISH -include VAdderTester.h -c -o verilated_vcd_c.o D:/AI/asenv/msys2/usr/share/verilator/include/verilated_vcd_c.cpp
/usr/bin/perl D:/AI/asenv/msys2/usr/share/verilator/bin/verilator_includer -DVL_INCLUDE_OPT=include VAdderTester.cpp > VAdderTester__ALLcls.cpp
/usr/bin/perl D:/AI/asenv/msys2/usr/share/verilator/bin/verilator_includer -DVL_INCLUDE_OPT=include VAdderTester__Trace.cpp VAdderTester__Syms.cpp VAdderTester__Trace__Slow.cpp > VAdderTester__ALLsup.cpp
g++ -I. -MMD -ID:/AI/asenv/msys2/usr/share/verilator/include -ID:/AI/asenv/msys2/usr/share/verilator/include/vltstd -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=1 -faligned-new -fcf-protection=none -Wno-bool-operation -Wno-sign-compare -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-variable -Wno-shadow -Wno-undefined-bool-conversion -O1 -DTOP_TYPE=VAdderTester -DVL_USER_FINISH -include VAdderTester.h -c -o VAdderTester__ALLsup.o VAdderTester__ALLsup.cpp
g++ -I. -MMD -ID:/AI/asenv/msys2/usr/share/verilator/include -ID:/AI/asenv/msys2/usr/share/verilator/include/vltstd -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=1 -faligned-new -fcf-protection=none -Wno-bool-operation -Wno-sign-compare -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-variable -Wno-shadow -Wno-undefined-bool-conversion -O1 -DTOP_TYPE=VAdderTester -DVL_USER_FINISH -include VAdderTester.h -c -o VAdderTester__ALLcls.o VAdderTester__ALLcls.cpp
ar -cr VAdderTester__ALL.a VAdderTester__ALLcls.o VAdderTester__ALLsup.o
ranlib VAdderTester__ALL.a
g++ D:\J2EE\ScalaExample\test_run_dir\AdderTester\202004072142333006115844216379006\top.o verilated.o verilated_vcd_c.o VAdderTester__ALL.a -o VAdderTester -lm -lstdc++
make: Leaving directory '/d/J2EE/ScalaExample/test_run_dir/AdderTester/202004072142333006115844216379006'
[error] (run-main-0) java.io.IOException: Cannot run program "./VAdderTester" (in directory "test_run_dir\AdderTester\202004072142333006115844216379006"): CreateProcess error=2, 系统找不到指定的文件。
[error] java.io.IOException: Cannot run program "./VAdderTester" (in directory "test_run_dir\AdderTester\202004072142333006115844216379006"): CreateProcess error=2, 系统找不到指定的文件。
[error] at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
[error] at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:75)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.run(ProcessBuilderImpl.scala:106)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$anonfun$runBuffered$1(ProcessBuilderImpl.scala:154)
[error] at scala.runtime.java8.JFunction0$mcI$sp.apply(JFunction0$mcI$sp.java:23)
[error] at scala.sys.process.ProcessLogger$$anon$1.buffer(ProcessLogger.scala:103)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.runBuffered(ProcessBuilderImpl.scala:154)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang(ProcessBuilderImpl.scala:120)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingFailure(BackendCompilationUtilities.scala:164)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingFailure$(BackendCompilationUtilities.scala:157)
[error] at chisel3.testers.TesterDriver$.executeExpectingFailure(TesterDriver.scala:10)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingSuccess(BackendCompilationUtilities.scala:174)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingSuccess$(BackendCompilationUtilities.scala:173)
[error] at chisel3.testers.TesterDriver$.executeExpectingSuccess(TesterDriver.scala:10)
[error] at chisel3.testers.TesterDriver$.execute(TesterDriver.scala:46)
[error] at Adder$.main(Adder.scala:49)
[error] at Adder.main(Adder.scala)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.lang.reflect.Method.invoke(Method.java:498)
[error] Caused by: java.io.IOException: CreateProcess error=2, 系统找不到指定的文件。
[error] at java.lang.ProcessImpl.create(Native Method)
[error] at java.lang.ProcessImpl.(ProcessImpl.java:386)
[error] at java.lang.ProcessImpl.start(ProcessImpl.java:137)
[error] at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
[error] at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:75)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.run(ProcessBuilderImpl.scala:106)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$anonfun$runBuffered$1(ProcessBuilderImpl.scala:154)
[error] at scala.runtime.java8.JFunction0$mcI$sp.apply(JFunction0$mcI$sp.java:23)
[error] at scala.sys.process.ProcessLogger$$anon$1.buffer(ProcessLogger.scala:103)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.runBuffered(ProcessBuilderImpl.scala:154)
[error] at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang(ProcessBuilderImpl.scala:120)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingFailure(BackendCompilationUtilities.scala:164)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingFailure$(BackendCompilationUtilities.scala:157)
[error] at chisel3.testers.TesterDriver$.executeExpectingFailure(TesterDriver.scala:10)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingSuccess(BackendCompilationUtilities.scala:174)
[error] at firrtl.util.BackendCompilationUtilities.executeExpectingSuccess$(BackendCompilationUtilities.scala:173)
[error] at chisel3.testers.TesterDriver$.executeExpectingSuccess(TesterDriver.scala:10)
[error] at chisel3.testers.TesterDriver$.execute(TesterDriver.scala:46)
[error] at Adder$.main(Adder.scala:49)
[error] at Adder.main(Adder.scala)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.lang.reflect.Method.invoke(Method.java:498)
[error] Nonzero exit code: 1
[error] (Compile / run) Nonzero exit code: 1
[error] Total time: 23 s, completed 2020-4-7 21:42:42
[IJ]sbt:ScalaExample>
Enabling waves...
Starting simulation!
passed step 0 -- out: 143
passed step 10 -- out: 216
passed step 20 -- out: 242
Stopping, end of tests, 31 steps
上面的错误,主要是Windows平台下的可执行文件带了exe后缀,进入目录直接执行exe即可。