Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP]: Loading verilator as shared libraries #233

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/scala/chisel3/iotesters/ChiselMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object chiselMain {
Seq(),
new File(dir, s"$dutName-harness.cpp")).! == 0)
// Compile Verilator
assert(chisel3.Driver.cppToExe(dutName, dir).! == 0)
assert(setupVerilatorBackend.cppToSo(dutName, dir).! == 0)
case "vcs" | "glsim" =>
// Copy API files
copyVpiFiles(context.targetDir.toString)
Expand Down
57 changes: 55 additions & 2 deletions src/main/scala/chisel3/iotesters/PeekPokeTesterUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,25 @@ private[iotesters] object verilogToVerilator extends EditableBuildCSimulatorComm
moreVerilatorFlags: Seq[String] = Seq.empty[String],
moreVerilatorCFlags: Seq[String] = Seq.empty[String]): (Seq[String], Seq[String]) = {

val javaHome = System.getProperty("java.home") match {
case s: String if s.endsWith("/jre") => s.dropRight(4)
case s: String => s
}
val osIncludeName = System.getProperty("os.name") match {
case "Mac OS X" => "darwin"
case "Linux" => "linux"
case s: String => s
}

val ccFlags = Seq(
"-Wno-undefined-bool-conversion",
"-O1",
s"-DTOP_TYPE=V$topModule",
"-DVL_USER_FINISH",
"-fPIC",
"-shared",
s"-I$javaHome/include",
s"-I$javaHome/include/$osIncludeName",
s"-include V$topModule.h"
) ++ moreVerilatorCFlags

Expand Down Expand Up @@ -385,6 +399,45 @@ private[iotesters] case class BackendException(b: String)
private[iotesters] case class TestApplicationException(exitVal: Int, lastMessage: String)
extends RuntimeException(lastMessage)


class TesterSharedLib(libPath: String) {
Predef.printf(s"TesterSharedLib: loading $libPath ")
try {
System.load(new File(libPath).getCanonicalPath())
println(" ok")
} catch {
case e: Throwable =>
println(" failed: " + e.toString)
throw e
}

private val state: Long = 0

@native private def sim_init(): Unit
@native def reset(): Unit
@native def step(): Unit
@native def update(): Unit
@native def poke(id: Int, value: Int): Unit
@native def peek(id: Int): Int
@native def force(): Unit
@native def getid(path: String): Int
@native def getchk(id: Int): Int
@native def finish(): Unit
@native def start(): Unit

println(s"State before: $state")
sim_init()
println(s"State after: $state")
}

private[iotesters] object TesterSharedLib {
def apply(cmd: Seq[String], logs: ArrayBuffer[String]): TesterSharedLib = {

require(new java.io.File(cmd.head + ".dylib").exists, s"${cmd.head}.dylib doesn't exist")
new TesterSharedLib(cmd.head + ".dylib")
}
}

private[iotesters] object TesterProcess {
def apply(cmd: Seq[String], logs: ArrayBuffer[String]): Process = {
require(new java.io.File(cmd.head).exists, s"${cmd.head} doesn't exist")
Expand All @@ -393,8 +446,8 @@ private[iotesters] object TesterProcess {
processBuilder run processLogger
}
def kill(sim: SimApiInterface) {
while(!sim.exitValue.isCompleted) sim.process.destroy
println("Exit Code: %d".format(sim.process.exitValue))
// while(!sim.exitValue.isCompleted) sim.process.destroy
// println("Exit Code: %d".format(sim.process.exitValue))
}
def kill(p: IVLBackend) {
kill(p.simApiInterface)
Expand Down
Loading