9 # pylint: disable=too-many-instance-attributes
11 # Name of the target. Defaults to the name of the class.
14 # XLEN of the target. May be overridden with --32 or --64 command line
18 # GDB remotetimeout setting.
21 # Path to OpenOCD configuration file relative to the .py file where the
22 # target is defined. Defaults to <name>.cfg.
23 openocd_config_path
= None
25 # Path to linker script relative to the .py file where the target is
26 # defined. Defaults to <name>.lds.
27 link_script_path
= None
29 # Will be autodetected (by running ExamineTarget) if left unset. Set to
36 temporary_binary
= None
38 def __init__(self
, path
, parsed
):
41 self
.directory
= os
.path
.dirname(path
)
42 self
.server_cmd
= parsed
.server_cmd
43 self
.sim_cmd
= parsed
.sim_cmd
44 self
.isolate
= parsed
.isolate
46 self
.name
= type(self
).__name
__
47 # Default OpenOCD config file to <name>.cfg
48 if not self
.openocd_config_path
:
49 self
.openocd_config_path
= "%s.cfg" % self
.name
50 self
.openocd_config_path
= os
.path
.join(self
.directory
,
51 self
.openocd_config_path
)
52 # Default link script to <name>.lds
53 if not self
.link_script_path
:
54 self
.link_script_path
= "%s.lds" % self
.name
55 self
.link_script_path
= os
.path
.join(self
.directory
,
56 self
.link_script_path
)
59 """Create the target out of thin air, eg. start a simulator."""
63 """Start the debug server that gdb connects to, eg. OpenOCD."""
64 return testlib
.Openocd(server_cmd
=self
.server_cmd
,
65 config
=self
.openocd_config_path
)
67 def compile(self
, *sources
):
68 binary_name
= "%s_%s-%d" % (
70 os
.path
.basename(os
.path
.splitext(sources
[0])[0]),
73 self
.temporary_binary
= tempfile
.NamedTemporaryFile(
74 prefix
=binary_name
+ "_")
75 binary_name
= self
.temporary_binary
.name
76 Target
.temporary_files
.append(self
.temporary_binary
)
77 march
= "rv%dima" % self
.xlen
79 if self
.extensionSupported(letter
):
81 testlib
.compile(sources
+
82 ("programs/entry.S", "programs/init.c",
85 "-T", self
.link_script_path
,
88 "-DXLEN=%d" % self
.xlen
,
93 def extensionSupported(self
, letter
):
94 # target.misa is set by testlib.ExamineTarget
96 return self
.misa
& (1 << (ord(letter
.upper()) - ord('A')))
100 def add_target_options(parser
):
101 parser
.add_argument("target", help=".py file that contains definition for "
102 "the target to test with.")
103 parser
.add_argument("--sim_cmd",
104 help="The command to use to start the actual target (e.g. "
105 "simulation)", default
="spike")
106 parser
.add_argument("--server_cmd",
107 help="The command to use to start the debug server (e.g. OpenOCD)")
109 xlen_group
= parser
.add_mutually_exclusive_group()
110 xlen_group
.add_argument("--32", action
="store_const", const
=32, dest
="xlen",
111 help="Force the target to be 32-bit.")
112 xlen_group
.add_argument("--64", action
="store_const", const
=64, dest
="xlen",
113 help="Force the target to be 64-bit.")
115 parser
.add_argument("--isolate", action
="store_true",
116 help="Try to run in such a way that multiple instances can run at "
117 "the same time. This may make it harder to debug a failure if it "
121 directory
= os
.path
.dirname(parsed
.target
)
122 filename
= os
.path
.basename(parsed
.target
)
123 module_name
= os
.path
.splitext(filename
)[0]
125 sys
.path
.append(directory
)
126 module
= importlib
.import_module(module_name
)
128 for name
in dir(module
):
129 definition
= getattr(module
, name
)
130 if type(definition
) == type and issubclass(definition
, Target
):
131 found
.append(definition
)
132 assert len(found
) == 1, "%s does not define exactly one subclass of " \
133 "targets.Target" % parsed
.target
135 return found
[0](parsed
.target
, parsed
)