538caaf41d6dadcc5b4f47770b7b3b33051c79a5
[riscv-tests.git] / debug / targets.py
1 import os.path
2 import tempfile
3
4 import testlib
5
6 class Target(object):
7 name = "name"
8 xlen = 0
9 directory = None
10 timeout_sec = 2
11 temporary_files = []
12 temporary_binary = None
13 openocd_config = []
14 use_fpu = False
15
16 def __init__(self, cmd, run, isolate):
17 self.cmd = cmd
18 self.run = run
19 self.isolate = isolate
20
21 def target(self):
22 """Start the target, eg. a simulator."""
23 pass
24
25 def server(self):
26 """Start the debug server that gdb connects to, eg. OpenOCD."""
27 if self.openocd_config:
28 return testlib.Openocd(cmd=self.cmd, config=self.openocd_config)
29 else:
30 raise NotImplementedError
31
32 def compile(self, *sources):
33 binary_name = "%s_%s-%d" % (
34 self.name,
35 os.path.basename(os.path.splitext(sources[0])[0]),
36 self.xlen)
37 if self.isolate:
38 self.temporary_binary = tempfile.NamedTemporaryFile(
39 prefix=binary_name + "_")
40 binary_name = self.temporary_binary.name
41 Target.temporary_files.append(self.temporary_binary)
42 march = "RV%dIMA" % self.xlen
43 if self.use_fpu:
44 march += "FD"
45 testlib.compile(sources +
46 ("programs/entry.S", "programs/init.c",
47 "-I", "../env",
48 "-march=%s" % march,
49 "-T", "targets/%s/link.lds" % (self.directory or self.name),
50 "-nostartfiles",
51 "-mcmodel=medany",
52 "-o", binary_name),
53 xlen=self.xlen)
54 return binary_name
55
56 class SpikeTarget(Target):
57 # pylint: disable=abstract-method
58 directory = "spike"
59 ram = 0x80010000
60 ram_size = 5 * 1024 * 1024
61 instruction_hardware_breakpoint_count = 4
62 reset_vector = 0x1000
63
64 class Spike64Target(SpikeTarget):
65 name = "spike64"
66 xlen = 64
67
68 def server(self):
69 return testlib.Spike(self.cmd, halted=True)
70
71 class Spike32Target(SpikeTarget):
72 name = "spike32"
73 xlen = 32
74
75 def server(self):
76 return testlib.Spike(self.cmd, halted=True, xlen=32)
77
78 class FreedomE300Target(Target):
79 name = "freedom-e300"
80 xlen = 32
81 ram = 0x80000000
82 ram_size = 16 * 1024
83 instruction_hardware_breakpoint_count = 2
84 openocd_config = "targets/%s/openocd.cfg" % name
85
86 class FreedomE300SimTarget(Target):
87 name = "freedom-e300-sim"
88 xlen = 32
89 timeout_sec = 240
90 ram = 0x80000000
91 ram_size = 256 * 1024 * 1024
92 instruction_hardware_breakpoint_count = 2
93 openocd_config = "targets/%s/openocd.cfg" % name
94
95 def target(self):
96 return testlib.VcsSim(simv=self.run, debug=False)
97
98 class FreedomU500Target(Target):
99 name = "freedom-u500"
100 xlen = 64
101 ram = 0x80000000
102 ram_size = 16 * 1024
103 instruction_hardware_breakpoint_count = 2
104 openocd_config = "targets/%s/openocd.cfg" % name
105
106 class FreedomU500SimTarget(Target):
107 name = "freedom-u500-sim"
108 xlen = 64
109 timeout_sec = 240
110 ram = 0x80000000
111 ram_size = 256 * 1024 * 1024
112 instruction_hardware_breakpoint_count = 2
113 openocd_config = "targets/%s/openocd.cfg" % name
114
115 def target(self):
116 return testlib.VcsSim(simv=self.run, debug=False)
117
118 targets = [
119 Spike32Target,
120 Spike64Target,
121 FreedomE300Target,
122 FreedomU500Target,
123 FreedomE300SimTarget,
124 FreedomU500SimTarget]
125
126 def add_target_options(parser):
127 group = parser.add_mutually_exclusive_group(required=True)
128 for t in targets:
129 group.add_argument("--%s" % t.name, action="store_const", const=t,
130 dest="target")
131 parser.add_argument("--run",
132 help="The command to use to start the actual target (e.g. "
133 "simulation)")
134 parser.add_argument("--cmd",
135 help="The command to use to start the debug server.")
136
137 xlen_group = parser.add_mutually_exclusive_group()
138 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
139 help="Force the target to be 32-bit.")
140 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
141 help="Force the target to be 64-bit.")
142
143 parser.add_argument("--isolate", action="store_true",
144 help="Try to run in such a way that multiple instances can run at "
145 "the same time. This may make it harder to debug a failure if it "
146 "does occur.")