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