CIRCT 24.0.0git
Loading...
Searching...
No Matches
esi-cosim.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# ===- esi-cosim.py - ESI cosimulation launch utility --------*- python -*-===//
4#
5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6# See https://llvm.org/LICENSE.txt for license information.
7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8#
9# ===----------------------------------------------------------------------===//
10#
11# Utility script to start a simulation and launch a command to interact with it
12# via ESI cosimulation.
13#
14# ===----------------------------------------------------------------------===//
15
16import argparse
17from pathlib import Path
18import sys
19import textwrap
20
21from esiaccel.cosim.simulator import (get_simulator, load_macro_definitions,
22 SourceFiles)
23
24
26 name, separator, macro_value = value.partition("=")
27 if not name:
28 raise argparse.ArgumentTypeError("macro name cannot be empty")
29 return name, macro_value if separator else None
30
31
32def __main__(args):
33 argparser = argparse.ArgumentParser(
34 description="Wrap a 'inner_cmd' in an ESI cosimulation environment.",
35 formatter_class=argparse.RawDescriptionHelpFormatter,
36 epilog=textwrap.dedent("""
37 Notes:
38 - For Verilator, libEsiCosimDpiServer.so must be in the dynamic
39 library runtime search path (LD_LIBRARY_PATH) and link time path
40 (LIBRARY_PATH). If it is installed to a standard location (e.g.
41 /usr/lib), this should be handled automatically.
42 - This script needs to sit in the same directory as the ESI support
43 SystemVerilog (e.g. Cosim_DpiPkg.sv, Cosim_MMIO.sv, etc.). It can,
44 however, be soft linked to a different location.
45 - The simulator executable(s) must be in your PATH.
46 """))
47
48 argparser.add_argument(
49 "--sim",
50 type=str,
51 default="verilator",
52 help="Name of the RTL simulator to use or path to an executable.")
53 argparser.add_argument("--rundir",
54 default="run",
55 help="Directory in which simulation should be run.")
56 argparser.add_argument(
57 "--top",
58 default="ESI_Cosim_Top",
59 help="Name of the 'top' module to use in the simulation.")
60 argparser.add_argument("--no-compile",
61 action="store_true",
62 help="Do not run the compile.")
63 argparser.add_argument("--debug",
64 action="store_true",
65 help="Enable debug output.")
66 argparser.add_argument(
67 "--save-waveform",
68 action="store_true",
69 help="Save waveform dumps (format depends on simulator). Requires --debug."
70 )
71 argparser.add_argument("--gui",
72 action="store_true",
73 help="Run the simulator in GUI mode (if supported).")
74 argparser.add_argument("--source",
75 help="Directories containing the source files.",
76 default="hw")
77 argparser.add_argument(
78 "-D",
79 "--define",
80 dest="macro_definitions",
81 action="append",
82 default=[],
83 metavar="NAME[=VALUE]",
84 type=_parse_macro_definition,
85 help="Define an RTL macro during compilation. May be specified multiple "
86 "times.")
87 argparser.add_argument(
88 "--define-file",
89 dest="macro_definitions_file",
90 metavar="FILE",
91 help="Read RTL macro definitions from a JSON file: an object mapping "
92 "macro name to value, where null defines the macro without one. Useful "
93 "when the macros are produced by the same build that generated the "
94 "sources. Any -D on the command line overrides what the file defines.")
95
96 argparser.add_argument("inner_cmd",
97 nargs=argparse.REMAINDER,
98 help="Command to run in the simulation environment.")
99
100 argparser.add_argument(
101 "--server-only",
102 action="store_true",
103 help="Only run the cosim server, and do not run any inner command.")
104
105 if len(args) <= 1:
106 argparser.print_help()
107 return
108 args = argparser.parse_args(args[1:])
109
110 # Validate that save_waveform requires debug
111 if args.save_waveform and not args.debug:
112 print("ERROR: --save-waveform requires --debug to be enabled",
113 file=sys.stderr)
114 return 1
115
116 sources = SourceFiles(args.top)
117 sources.add_dir(Path(args.source))
118
119 macro_definitions = {}
120 if args.macro_definitions_file is not None:
121 macro_definitions.update(
122 load_macro_definitions(Path(args.macro_definitions_file)))
123 macro_definitions.update(args.macro_definitions)
124
125 sim = get_simulator(args.sim,
126 sources,
127 Path(args.rundir),
128 args.debug,
129 args.save_waveform,
130 macro_definitions=macro_definitions)
131 if not args.no_compile:
132 rc = sim.compile()
133 if rc != 0:
134 return rc
135 return sim.run(args.inner_cmd[1:], gui=args.gui, server_only=args.server_only)
136
137
138if __name__ == '__main__':
139 sys.exit(__main__(sys.argv))
static void print(TypedAttr val, llvm::raw_ostream &os)
_parse_macro_definition(value)
Definition esi-cosim.py:25
__main__(args)
Definition esi-cosim.py:32