Skip to content

Commit baa38ed

Browse files
author
Oron Port
committed
tools refactoring and verilator simulation support
1 parent 1d416e7 commit baa38ed

File tree

5 files changed

+113
-17
lines changed

5 files changed

+113
-17
lines changed

compiler/ir/src/main/scala/dfhdl/compiler/ir/SourceFile.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object SourceType:
1414
case BlackBox
1515
case GlobalDef
1616
case DFHDLDef
17-
trait ToolConfig extends SourceType
17+
trait Tool extends SourceType
1818

1919
enum SourceOrigin derives CanEqual:
2020
// Compiled files are a result from a compilation process.

lib/src/main/scala/dfhdl/default.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ extension [D <: Design](cd: CompiledDesign[D])
1212
lo: LinterOptions
1313
): CompiledDesign[D] =
1414
co.backend match
15-
case _: backends.verilog => lo.verilogLinter.lint(lo.verilogLinter.preprocess(cd))
16-
case _: backends.vhdl => lo.vhdlLinter.lint(lo.vhdlLinter.preprocess(cd))
15+
case _: backends.verilog => lo.verilogLinter.lint(lo.verilogLinter.lintPreprocess(cd))
16+
case _: backends.vhdl => lo.vhdlLinter.lint(lo.vhdlLinter.lintPreprocess(cd))
1717
def simulate(using
1818
co: CompilerOptions,
1919
so: SimulatorOptions
@@ -22,13 +22,15 @@ extension [D <: Design](cd: CompiledDesign[D])
2222
import stagedDB.getSet
2323
if (stagedDB.inSimulation)
2424
co.backend match
25-
case _: backends.verilog => so.verilogSimulator.simulate(so.verilogSimulator.preprocess(cd))
26-
case _: backends.vhdl => so.vhdlSimulator.simulate(so.vhdlSimulator.preprocess(cd))
25+
case _: backends.verilog =>
26+
so.verilogSimulator.simulate(so.verilogSimulator.simulatePreprocess(cd))
27+
case _: backends.vhdl => so.vhdlSimulator.simulate(so.vhdlSimulator.simulatePreprocess(cd))
2728
else
2829
throw new Exception(
2930
s"The top design `${stagedDB.top.getName}` has ports and therefore cannot be simulated."
3031
)
32+
end simulate
3133

3234
def build(using builder: Builder)(using CompilerOptions, BuilderOptions): CompiledDesign[D] =
33-
builder.build(builder.preprocess(cd))
35+
builder.build(builder.buildPreprocess(cd))
3436
end extension

lib/src/main/scala/dfhdl/tools/toolsCore/Tool.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ trait Tool:
1818
if (osName.contains("windows")) windowsBinExec else binExec
1919
protected def binExec: String
2020
protected def windowsBinExec: String = s"$binExec.exe"
21-
protected[dfhdl] def preprocess[D <: Design](cd: CompiledDesign[D])(using
22-
CompilerOptions,
23-
ToolOptions
24-
): CompiledDesign[D] = cd
2521
final protected def addSourceFiles[D <: Design](
2622
cd: CompiledDesign[D],
2723
sourceFiles: List[SourceFile]
@@ -173,6 +169,10 @@ trait VerilogTool extends Tool:
173169
trait VHDLTool extends Tool
174170

175171
trait Linter extends Tool:
172+
protected[dfhdl] def lintPreprocess[D <: Design](cd: CompiledDesign[D])(using
173+
CompilerOptions,
174+
ToolOptions
175+
): CompiledDesign[D] = cd
176176
final def lint[D <: Design](
177177
cd: CompiledDesign[D]
178178
)(using CompilerOptions, ToolOptions): CompiledDesign[D] =
@@ -211,9 +211,13 @@ trait VHDLLinter extends Linter, VHDLTool:
211211
lintCmdLanguageFlag(co.backend.asInstanceOf[dfhdl.backends.vhdl].dialect)
212212

213213
trait Simulator extends Tool:
214+
protected[dfhdl] def simulatePreprocess[D <: Design](cd: CompiledDesign[D])(using
215+
CompilerOptions,
216+
SimulatorOptions
217+
): CompiledDesign[D] = cd
214218
val simRunsLint: Boolean = false
215219
protected def simRunExec: String = this.runExec
216-
final def simulate[D <: Design](
220+
def simulate[D <: Design](
217221
cd: CompiledDesign[D]
218222
)(using CompilerOptions, SimulatorOptions): CompiledDesign[D] =
219223
given MemberGetSet = cd.stagedDB.getSet
@@ -279,6 +283,10 @@ trait VHDLSimulator extends Simulator, VHDLTool:
279283
simulateCmdLanguageFlag(co.backend.asInstanceOf[dfhdl.backends.vhdl].dialect)
280284

281285
trait Builder extends Tool:
286+
protected[dfhdl] def buildPreprocess[D <: Design](cd: CompiledDesign[D])(using
287+
CompilerOptions,
288+
BuilderOptions
289+
): CompiledDesign[D] = cd
282290
def build[D <: Design](
283291
cd: CompiledDesign[D]
284292
)(using CompilerOptions, BuilderOptions): CompiledDesign[D]

lib/src/main/scala/dfhdl/tools/toolsCore/Verilator.scala

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Verilator extends VerilogLinter, VerilogSimulator:
2626

2727
override protected def toolFiles(using getSet: MemberGetSet): List[String] =
2828
getSet.designDB.srcFiles.collect {
29-
case SourceFile(SourceOrigin.Committed, VerilatorConfig, path, _) =>
29+
case SourceFile(SourceOrigin.Committed, _: VerilatorToolSource, path, _) =>
3030
path.convertWindowsToLinuxPaths
3131
}
3232

@@ -52,8 +52,10 @@ object Verilator extends VerilogLinter, VerilogSimulator:
5252
constructCommand(
5353
"--lint-only",
5454
"--quiet-stats",
55+
s"--top-module ${topName}",
5556
if (hasTiming) "--timing" else ""
5657
)
58+
end lintCmdPreLangFlags
5759

5860
override protected def lintCmdPostLangFlags(using
5961
CompilerOptions,
@@ -64,17 +66,56 @@ object Verilator extends VerilogLinter, VerilogSimulator:
6466
(!summon[LinterOptions].Werror.toBoolean).toFlag("-Wno-fatal")
6567
)
6668

67-
override protected[dfhdl] def preprocess[D <: Design](cd: CompiledDesign[D])(using
69+
override protected def simulateCmdPreLangFlags(using
70+
CompilerOptions,
71+
SimulatorOptions,
72+
MemberGetSet
73+
): String =
74+
constructCommand(
75+
"--binary",
76+
"--quiet-stats",
77+
s"--top-module ${topName}"
78+
)
79+
80+
override protected[dfhdl] def lintPreprocess[D <: Design](cd: CompiledDesign[D])(using
6881
CompilerOptions,
6982
ToolOptions
7083
): CompiledDesign[D] =
7184
addSourceFiles(
7285
cd,
7386
List(new VerilatorConfigPrinter(getInstalledVersion)(using cd.stagedDB.getSet).getSourceFile)
7487
)
88+
89+
// override protected[dfhdl] def simulatePreprocess[D <: Design](cd: CompiledDesign[D])(using
90+
// CompilerOptions,
91+
// SimulatorOptions
92+
// ): CompiledDesign[D] =
93+
// addSourceFiles(
94+
// cd,
95+
// List(new VerilatorSimMainPrinter(getInstalledVersion)(using cd.stagedDB.getSet).getSourceFile)
96+
// )
97+
98+
override protected def simulateCmdLanguageFlag(dialect: VerilogDialect): String =
99+
lintCmdLanguageFlag(dialect)
100+
101+
override def simulate[D <: Design](
102+
cd: CompiledDesign[D]
103+
)(using CompilerOptions, SimulatorOptions): CompiledDesign[D] =
104+
val ret = super.simulate(cd)
105+
given MemberGetSet = ret.stagedDB.getSet
106+
val unixExec =
107+
s"${Paths.get(execPath).toAbsolutePath()}${separatorChar}obj_dir${separatorChar}V${topName}"
108+
val runExec: String =
109+
val osName: String = sys.props("os.name").toLowerCase
110+
if (osName.contains("windows")) s"${unixExec}.exe" else unixExec
111+
exec(cmd = "", runExec = runExec)
112+
ret
113+
75114
end Verilator
76115

77-
case object VerilatorConfig extends SourceType.ToolConfig
116+
sealed trait VerilatorToolSource extends SourceType.Tool
117+
118+
case object VerilatorConfig extends VerilatorToolSource
78119

79120
class VerilatorConfigPrinter(verilatorVersion: String)(using
80121
getSet: MemberGetSet,
@@ -171,3 +212,48 @@ class VerilatorConfigPrinter(verilatorVersion: String)(using
171212
SourceFile(SourceOrigin.Compiled, VerilatorConfig, configFileName, contents)
172213

173214
end VerilatorConfigPrinter
215+
216+
case object VerilatorSimMain extends VerilatorToolSource
217+
218+
// class VerilatorSimMainPrinter(verilatorVersion: String)(using
219+
// getSet: MemberGetSet,
220+
// co: CompilerOptions,
221+
// so: SimulatorOptions
222+
// ):
223+
// val designDB: DB = getSet.designDB
224+
// val topName = designDB.top.dclName
225+
// def mainFileName: String = s"${topName}.cpp"
226+
// def contents: String =
227+
// s"""|#include "V${topName}.h"
228+
// |#include "verilated.h"
229+
// |#include "V${topName}___024root.h"
230+
// |
231+
// |int main(int argc, char** argv) {
232+
// | // Initialize Verilator
233+
// | Verilated::commandArgs(argc, argv);
234+
// |
235+
// | // Create instance of our module
236+
// | V${topName}* top = new V${topName};
237+
// |
238+
// | // Initialize simulation inputs
239+
// | V${topName}___024root* rootp = top->rootp;
240+
// | rootp->${topName}__DOT__rst = 1;
241+
// |
242+
// | while (!Verilated::gotFinish()) {
243+
// | // Toggle clock
244+
// | rootp->${topName}__DOT__clk = 0;
245+
// | top->eval();
246+
// | rootp->${topName}__DOT__clk = 1;
247+
// | top->eval();
248+
// | rootp->${topName}__DOT__rst = 0;
249+
// | }
250+
// |
251+
// | // Cleanup
252+
// | delete top;
253+
// |
254+
// | return 0;
255+
// |}
256+
// |""".stripMargin
257+
// def getSourceFile: SourceFile =
258+
// SourceFile(SourceOrigin.Compiled, VerilatorSimMain, mainFileName, contents)
259+
// end VerilatorSimMainPrinter

lib/src/main/scala/dfhdl/tools/toolsCore/Vivado.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ object Vivado extends Builder:
1919
versionPattern.findFirstMatchIn(cmdRetStr).map(_.group(1))
2020

2121
def filesCmdPart[D <: Design](cd: CompiledDesign[D]): String = ???
22-
override protected[dfhdl] def preprocess[D <: Design](cd: CompiledDesign[D])(using
22+
override protected[dfhdl] def buildPreprocess[D <: Design](cd: CompiledDesign[D])(using
2323
CompilerOptions,
24-
ToolOptions
24+
BuilderOptions
2525
): CompiledDesign[D] =
2626
addSourceFiles(
2727
cd,
@@ -37,7 +37,7 @@ object Vivado extends Builder:
3737
cd
3838
end Vivado
3939

40-
case object VivadoProjectTclConfig extends SourceType.ToolConfig
40+
case object VivadoProjectTclConfig extends SourceType.Tool
4141

4242
class VivadoProjectTclConfigPrinter(using getSet: MemberGetSet):
4343
val designDB: DB = getSet.designDB

0 commit comments

Comments
 (0)