Add test of compare-sections command.
[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 "-DXLEN=%d" % self.xlen,
53 "-o", binary_name),
54 xlen=self.xlen)
55 return binary_name
56
57 class SpikeTarget(Target):
58 # pylint: disable=abstract-method
59 directory = "spike"
60 ram = 0x80010000
61 ram_size = 5 * 1024 * 1024
62 instruction_hardware_breakpoint_count = 4
63 reset_vector = 0x1000
64
65 class Spike64Target(SpikeTarget):
66 name = "spike64"
67 xlen = 64
68 use_fpu = True
69
70 def server(self):
71 return testlib.Spike(self.cmd, halted=True)
72
73 class Spike32Target(SpikeTarget):
74 name = "spike32"
75 xlen = 32
76
77 def server(self):
78 return testlib.Spike(self.cmd, halted=True, xlen=32)
79
80 class FreedomE300Target(Target):
81 name = "freedom-e300"
82 xlen = 32
83 ram = 0x80000000
84 ram_size = 16 * 1024
85 instruction_hardware_breakpoint_count = 2
86 openocd_config = "targets/%s/openocd.cfg" % name
87
88 class FreedomE300SimTarget(Target):
89 name = "freedom-e300-sim"
90 xlen = 32
91 timeout_sec = 240
92 ram = 0x80000000
93 ram_size = 256 * 1024 * 1024
94 instruction_hardware_breakpoint_count = 2
95 openocd_config = "targets/%s/openocd.cfg" % name
96
97 def target(self):
98 return testlib.VcsSim(simv=self.run, debug=False)
99
100 class FreedomU500Target(Target):
101 name = "freedom-u500"
102 xlen = 64
103 ram = 0x80000000
104 ram_size = 16 * 1024
105 instruction_hardware_breakpoint_count = 2
106 openocd_config = "targets/%s/openocd.cfg" % name
107
108 class FreedomU500SimTarget(Target):
109 name = "freedom-u500-sim"
110 xlen = 64
111 timeout_sec = 240
112 ram = 0x80000000
113 ram_size = 256 * 1024 * 1024
114 instruction_hardware_breakpoint_count = 2
115 openocd_config = "targets/%s/openocd.cfg" % name
116
117 def target(self):
118 return testlib.VcsSim(simv=self.run, debug=False)
119
120 targets = [
121 Spike32Target,
122 Spike64Target,
123 FreedomE300Target,
124 FreedomU500Target,
125 FreedomE300SimTarget,
126 FreedomU500SimTarget]
127
128 def add_target_options(parser):
129 group = parser.add_mutually_exclusive_group(required=True)
130 for t in targets:
131 group.add_argument("--%s" % t.name, action="store_const", const=t,
132 dest="target")
133 parser.add_argument("--run",
134 help="The command to use to start the actual target (e.g. "
135 "simulation)")
136 parser.add_argument("--cmd",
137 help="The command to use to start the debug server.")
138
139 xlen_group = parser.add_mutually_exclusive_group()
140 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
141 help="Force the target to be 32-bit.")
142 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
143 help="Force the target to be 64-bit.")
144
145 parser.add_argument("--isolate", action="store_true",
146 help="Try to run in such a way that multiple instances can run at "
147 "the same time. This may make it harder to debug a failure if it "
148 "does occur.")