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.
37 # Implements dmode in tdata1 as described in the spec. Targets that need
38 # this value set to False are not compliant with the spec (but still usable
39 # as long as running code doesn't try to mess with triggers set by an
41 honors_tdata1_hmode
= True
46 temporary_binary
= None
48 def __init__(self
, path
, parsed
):
51 self
.directory
= os
.path
.dirname(path
)
52 self
.server_cmd
= parsed
.server_cmd
53 self
.sim_cmd
= parsed
.sim_cmd
54 self
.isolate
= parsed
.isolate
56 self
.name
= type(self
).__name
__
57 # Default OpenOCD config file to <name>.cfg
58 if not self
.openocd_config_path
:
59 self
.openocd_config_path
= "%s.cfg" % self
.name
60 self
.openocd_config_path
= os
.path
.join(self
.directory
,
61 self
.openocd_config_path
)
62 # Default link script to <name>.lds
63 if not self
.link_script_path
:
64 self
.link_script_path
= "%s.lds" % self
.name
65 self
.link_script_path
= os
.path
.join(self
.directory
,
66 self
.link_script_path
)
69 """Create the target out of thin air, eg. start a simulator."""
73 """Start the debug server that gdb connects to, eg. OpenOCD."""
74 return testlib
.Openocd(server_cmd
=self
.server_cmd
,
75 config
=self
.openocd_config_path
)
77 def compile(self
, *sources
):
78 binary_name
= "%s_%s-%d" % (
80 os
.path
.basename(os
.path
.splitext(sources
[0])[0]),
83 self
.temporary_binary
= tempfile
.NamedTemporaryFile(
84 prefix
=binary_name
+ "_")
85 binary_name
= self
.temporary_binary
.name
86 Target
.temporary_files
.append(self
.temporary_binary
)
87 march
= "rv%dima" % self
.xlen
89 if self
.extensionSupported(letter
):
91 testlib
.compile(sources
+
92 ("programs/entry.S", "programs/init.c",
95 "-T", self
.link_script_path
,
98 "-DXLEN=%d" % self
.xlen
,
103 def extensionSupported(self
, letter
):
104 # target.misa is set by testlib.ExamineTarget
106 return self
.misa
& (1 << (ord(letter
.upper()) - ord('A')))
110 def add_target_options(parser
):
111 parser
.add_argument("target", help=".py file that contains definition for "
112 "the target to test with.")
113 parser
.add_argument("--sim_cmd",
114 help="The command to use to start the actual target (e.g. "
115 "simulation)", default
="spike")
116 parser
.add_argument("--server_cmd",
117 help="The command to use to start the debug server (e.g. OpenOCD)")
119 xlen_group
= parser
.add_mutually_exclusive_group()
120 xlen_group
.add_argument("--32", action
="store_const", const
=32, dest
="xlen",
121 help="Force the target to be 32-bit.")
122 xlen_group
.add_argument("--64", action
="store_const", const
=64, dest
="xlen",
123 help="Force the target to be 64-bit.")
125 parser
.add_argument("--isolate", action
="store_true",
126 help="Try to run in such a way that multiple instances can run at "
127 "the same time. This may make it harder to debug a failure if it "
131 directory
= os
.path
.dirname(parsed
.target
)
132 filename
= os
.path
.basename(parsed
.target
)
133 module_name
= os
.path
.splitext(filename
)[0]
135 sys
.path
.append(directory
)
136 module
= importlib
.import_module(module_name
)
138 for name
in dir(module
):
139 definition
= getattr(module
, name
)
140 if type(definition
) == type and issubclass(definition
, Target
):
141 found
.append(definition
)
142 assert len(found
) == 1, "%s does not define exactly one subclass of " \
143 "targets.Target" % parsed
.target
145 return found
[0](parsed
.target
, parsed
)