Test OpenOCD step and resume.
[riscv-tests.git] / debug / openocd.py
1 #!/usr/bin/env python
2
3 """Test that OpenOCD can talk to a RISC-V target."""
4
5 import argparse
6 import sys
7
8 import targets
9 import testlib
10 from testlib import assertIn, assertEqual
11
12 class OpenOcdTest(testlib.BaseTest):
13 def __init__(self, target):
14 testlib.BaseTest.__init__(self, target)
15 self.gdb = None
16
17 def early_applicable(self):
18 return self.target.openocd_config
19
20 def setup(self):
21 # pylint: disable=attribute-defined-outside-init
22 self.cli = testlib.OpenocdCli()
23 self.cli.command("halt")
24
25 class RegTest(OpenOcdTest):
26 def test(self):
27 regs = self.cli.reg()
28 assertIn("x18", regs)
29
30 class StepTest(OpenOcdTest):
31 def test(self):
32 # 0x13 is nop
33 for address in range(self.target.ram, self.target.ram + 16, 4):
34 self.cli.command("mww 0x%x 0x13" % address)
35
36 self.cli.command("step 0x%x" % self.target.ram)
37 for i in range(4):
38 pc = self.cli.reg("pc")
39 assertEqual(pc, self.target.ram + 4 * (i+1))
40 self.cli.command("step")
41
42 class ResumeTest(OpenOcdTest):
43 def test(self):
44 # 0x13 is nop
45 for address in range(self.target.ram, self.target.ram + 32, 4):
46 self.cli.command("mww 0x%x 0x13" % address)
47
48 self.cli.command("bp 0x%x 4" % (self.target.ram + 12))
49 self.cli.command("bp 0x%x 4" % (self.target.ram + 24))
50
51 self.cli.command("resume 0x%x" % self.target.ram)
52 assertEqual(self.cli.reg("pc"), self.target.ram + 12)
53
54 self.cli.command("resume")
55 assertEqual(self.cli.reg("pc"), self.target.ram + 24)
56
57 self.cli.command("resume 0x%x" % self.target.ram)
58 assertEqual(self.cli.reg("pc"), self.target.ram + 12)
59
60 def main():
61 parser = argparse.ArgumentParser(
62 description="Test that OpenOCD can talk to a RISC-V target.")
63 targets.add_target_options(parser)
64 testlib.add_test_run_options(parser)
65
66 parsed = parser.parse_args()
67
68 target = parsed.target(parsed.cmd, parsed.run, parsed.isolate)
69 if parsed.xlen:
70 target.xlen = parsed.xlen
71
72 module = sys.modules[__name__]
73
74 return testlib.run_all_tests(module, target, parsed.test, parsed.fail_fast)
75
76 if __name__ == '__main__':
77 sys.exit(main())