CIRCT 22.0.0git
Loading...
Searching...
No Matches
questa.py
Go to the documentation of this file.
1# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2# See https://llvm.org/LICENSE.txt for license information.
3# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5import os
6from pathlib import Path
7from typing import List, Optional, Callable, Dict
8
9from .simulator import CosimCollateralDir, Simulator, SourceFiles
10
11
13 """Run and compile funcs for Questasim."""
14
15 DefaultDriver = CosimCollateralDir / "driver.sv"
16
18 self,
19 sources: SourceFiles,
20 run_dir: Path,
21 debug: bool,
22 run_stdout_callback: Optional[Callable[[str], None]] = None,
23 run_stderr_callback: Optional[Callable[[str], None]] = None,
24 compile_stdout_callback: Optional[Callable[[str], None]] = None,
25 compile_stderr_callback: Optional[Callable[[str], None]] = None,
26 make_default_logs: bool = True,
27 macro_definitions: Optional[Dict[str, str]] = None,
28 # An optional list of questa error codes to suppress
29 suppressed_questa_errors: Optional[List[int]] = None):
30 super().__init__(
31 sources=sources,
32 run_dir=run_dir,
33 debug=debug,
34 run_stdout_callback=run_stdout_callback,
35 run_stderr_callback=run_stderr_callback,
36 compile_stdout_callback=compile_stdout_callback,
37 compile_stderr_callback=compile_stderr_callback,
38 make_default_logs=make_default_logs,
39 macro_definitions=macro_definitions,
40 )
41 self.suppressed_questa_errors = suppressed_questa_errors
42
43 # Questa doesn't use stderr for error messages. Everything goes to stdout.
44 UsesStderr = False
45
46 def internal_compile_commands(self) -> List[str]:
47 cmds = [
48 "onerror { quit -f -code 1 }",
49 ]
50 sources = self.sources.rtl_sources
51 sources.append(Questa.DefaultDriver)
52
53 # Format macro definition command
54 if self.macro_definitions:
55 macro_definitions_cmd = " ".join([
56 f"+define+{k}={v}" if v is not None else f"+define+{k}"
57 for k, v in self.macro_definitions.items()
58 ])
59 else:
60 macro_definitions_cmd = ""
61
62 # Format error suppression command
64 suppressed_questa_errors_cmd = " ".join(
65 [f"-suppress {ec}" for ec in self.suppressed_questa_errors])
66 else:
67 suppressed_questa_errors_cmd = ""
68
69 for src in sources:
70 cmds.append(
71 f"vlog -incr +acc -sv {macro_definitions_cmd} {suppressed_questa_errors_cmd} +define+TOP_MODULE={self.sources.top}"
72 f" +define+SIMULATION {src.as_posix()}")
73 cmds.append(f"vopt -incr driver -o driver_opt +acc")
74 return cmds
75
76 def compile_commands(self) -> List[List[str]]:
77 with open("compile.do", "w") as f:
78 for cmd in self.internal_compile_commands():
79 f.write(cmd)
80 f.write("\n")
81 f.write("quit\n")
82 return [
83 ["vsim", "-batch", "-do", "compile.do"],
84 ]
85
86 def run_command(self, gui: bool) -> List[str]:
87 vsim = "vsim"
88 # Note: vsim exit codes say nothing about the test run's pass/fail even
89 # if $fatal is encountered in the simulation.
90 if gui:
91 cmd = [
92 vsim,
93 "driver_opt",
94 ]
95 else:
96 cmd = [
97 vsim,
98 "driver_opt",
99 "-batch",
100 ]
101
102 if self.debug:
103 # Create waveform dump .do file
104 wave_file = Path("wave.do")
105 with wave_file.open("w") as f:
106 f.write("log -r /*\n")
107 cmd += [
108 "-do",
109 str(wave_file.resolve()),
110 ]
111 # Questa will by default log to a vsim.wlf file in the current
112 # directory.
113 print(
114 f"Debug mode enabled - waveform will be in {wave_file.resolve().parent / 'vsim.wlf'}"
115 )
116
117 cmd += [
118 "-do",
119 "run -all",
120 ]
121
122 for lib in self.sources.dpi_so_paths():
123 svLib = os.path.splitext(lib)[0]
124 cmd.append("-sv_lib")
125 cmd.append(svLib)
126 return cmd
127
128 def run(self,
129 inner_command: str,
130 gui: bool = False,
131 server_only: bool = False) -> int:
132 """Override the Simulator.run() to add a soft link in the run directory (to
133 the work directory) before running vsim the usual way."""
134
135 # Create a soft link to the work directory.
136 workDir = self.run_dir / "work"
137 if not workDir.exists():
138 os.symlink(Path(os.getcwd()) / "work", workDir)
139
140 # Run the simulation.
141 return super().run(inner_command, gui, server_only=server_only)
static void print(TypedAttr val, llvm::raw_ostream &os)
List[List[str]] compile_commands(self)
Definition questa.py:76
__init__(self, SourceFiles sources, Path run_dir, bool debug, Optional[Callable[[str], None]] run_stdout_callback=None, Optional[Callable[[str], None]] run_stderr_callback=None, Optional[Callable[[str], None]] compile_stdout_callback=None, Optional[Callable[[str], None]] compile_stderr_callback=None, bool make_default_logs=True, Optional[Dict[str, str]] macro_definitions=None, Optional[List[int]] suppressed_questa_errors=None)
Definition questa.py:29
List[str] internal_compile_commands(self)
Definition questa.py:46
suppressed_questa_errors
Definition questa.py:41
List[str] run_command(self, bool gui)
Definition questa.py:86
int run(self, str inner_command, bool gui=False, bool server_only=False)
Definition questa.py:131