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
33 # List of commands that should be executed in gdb after connecting but
34 # before starting the test.
40 temporary_binary
= None
42 def __init__(self
, path
, parsed
):
45 self
.directory
= os
.path
.dirname(path
)
46 self
.server_cmd
= parsed
.server_cmd
47 self
.sim_cmd
= parsed
.sim_cmd
48 self
.isolate
= parsed
.isolate
50 self
.name
= type(self
).__name
__
51 # Default OpenOCD config file to <name>.cfg
52 if not self
.openocd_config_path
:
53 self
.openocd_config_path
= "%s.cfg" % self
.name
54 self
.openocd_config_path
= os
.path
.join(self
.directory
,
55 self
.openocd_config_path
)
56 # Default link script to <name>.lds
57 if not self
.link_script_path
:
58 self
.link_script_path
= "%s.lds" % self
.name
59 self
.link_script_path
= os
.path
.join(self
.directory
,
60 self
.link_script_path
)
63 """Create the target out of thin air, eg. start a simulator."""
67 """Start the debug server that gdb connects to, eg. OpenOCD."""
68 return testlib
.Openocd(server_cmd
=self
.server_cmd
,
69 config
=self
.openocd_config_path
)
71 def compile(self
, *sources
):
72 binary_name
= "%s_%s-%d" % (
74 os
.path
.basename(os
.path
.splitext(sources
[0])[0]),
77 self
.temporary_binary
= tempfile
.NamedTemporaryFile(
78 prefix
=binary_name
+ "_")
79 binary_name
= self
.temporary_binary
.name
80 Target
.temporary_files
.append(self
.temporary_binary
)
81 march
= "rv%dima" % self
.xlen
83 if self
.extensionSupported(letter
):
85 testlib
.compile(sources
+
86 ("programs/entry.S", "programs/init.c",
89 "-T", self
.link_script_path
,
92 "-DXLEN=%d" % self
.xlen
,
97 def extensionSupported(self
, letter
):
98 # target.misa is set by testlib.ExamineTarget
100 return self
.misa
& (1 << (ord(letter
.upper()) - ord('A')))
104 def add_target_options(parser
):
105 parser
.add_argument("target", help=".py file that contains definition for "
106 "the target to test with.")
107 parser
.add_argument("--sim_cmd",
108 help="The command to use to start the actual target (e.g. "
109 "simulation)", default
="spike")
110 parser
.add_argument("--server_cmd",
111 help="The command to use to start the debug server (e.g. OpenOCD)")
113 xlen_group
= parser
.add_mutually_exclusive_group()
114 xlen_group
.add_argument("--32", action
="store_const", const
=32, dest
="xlen",
115 help="Force the target to be 32-bit.")
116 xlen_group
.add_argument("--64", action
="store_const", const
=64, dest
="xlen",
117 help="Force the target to be 64-bit.")
119 parser
.add_argument("--isolate", action
="store_true",
120 help="Try to run in such a way that multiple instances can run at "
121 "the same time. This may make it harder to debug a failure if it "
125 directory
= os
.path
.dirname(parsed
.target
)
126 filename
= os
.path
.basename(parsed
.target
)
127 module_name
= os
.path
.splitext(filename
)[0]
129 sys
.path
.append(directory
)
130 module
= importlib
.import_module(module_name
)
132 for name
in dir(module
):
133 definition
= getattr(module
, name
)
134 if type(definition
) == type and issubclass(definition
, Target
):
135 found
.append(definition
)
136 assert len(found
) == 1, "%s does not define exactly one subclass of " \
137 "targets.Target" % parsed
.target
139 return found
[0](parsed
.target
, parsed
)