Clean up .pyc files.
[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 def write_nops(self, count):
26 for address in range(self.target.ram, self.target.ram + 4 * count, 4):
27 # 0x13 is nop
28 self.cli.command("mww 0x%x 0x13" % address)
29
30 class RegTest(OpenOcdTest):
31 def test(self):
32 self.write_nops(4)
33
34 regs = self.cli.reg()
35 assertIn("x18", regs)
36
37 self.cli.command("reg x18 0x11782")
38 self.cli.command("step 0x%x" % self.target.ram)
39
40 assertEqual(self.cli.reg("x18"), 0x11782)
41
42 class StepTest(OpenOcdTest):
43 def test(self):
44 self.write_nops(4)
45
46 self.cli.command("step 0x%x" % self.target.ram)
47 for i in range(4):
48 pc = self.cli.reg("pc")
49 assertEqual(pc, self.target.ram + 4 * (i+1))
50 self.cli.command("step")
51
52 class ResumeTest(OpenOcdTest):
53 def test(self):
54 self.write_nops(16)
55
56 self.cli.command("bp 0x%x 4" % (self.target.ram + 12))
57 self.cli.command("bp 0x%x 4" % (self.target.ram + 24))
58
59 self.cli.command("resume 0x%x" % self.target.ram)
60 assertEqual(self.cli.reg("pc"), self.target.ram + 12)
61
62 self.cli.command("resume")
63 assertEqual(self.cli.reg("pc"), self.target.ram + 24)
64
65 self.cli.command("resume 0x%x" % self.target.ram)
66 assertEqual(self.cli.reg("pc"), self.target.ram + 12)
67
68 def main():
69 parser = argparse.ArgumentParser(
70 description="Test that OpenOCD can talk to a RISC-V target.")
71 targets.add_target_options(parser)
72 testlib.add_test_run_options(parser)
73
74 parsed = parser.parse_args()
75
76 target = parsed.target(parsed.cmd, parsed.run, parsed.isolate)
77 if parsed.xlen:
78 target.xlen = parsed.xlen
79
80 module = sys.modules[__name__]
81
82 return testlib.run_all_tests(module, target, parsed.test, parsed.fail_fast)
83
84 if __name__ == '__main__':
85 sys.exit(main())