Properly clean up spike.
[riscv-isa-sim.git] / tests / testlib.py
1 import os.path
2 import pexpect
3 import subprocess
4 import tempfile
5 import testlib
6 import unittest
7
8 # Note that gdb comes with its own testsuite. I was unable to figure out how to
9 # run that testsuite against the spike simulator.
10
11 def find_file(path):
12 for directory in (os.getcwd(), os.path.dirname(testlib.__file__)):
13 fullpath = os.path.join(directory, path)
14 if os.path.exists(fullpath):
15 return fullpath
16 raise ValueError("Couldn't find %r." % path)
17
18 def compile(src, dst):
19 """Compile a single .c file into a binary."""
20 cc = os.path.expandvars("$RISCV/bin/riscv64-unknown-elf-gcc")
21 return os.system("%s -g -o %s %s" % (cc, dst, find_file(src)))
22
23 def spike(binary, halted=False):
24 cmd = [find_file("spike")]
25 if halted:
26 cmd.append('-H')
27 cmd += ['pk', binary]
28 logfile = open("spike.log", "w")
29 return subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=logfile, stderr=logfile)
30
31 class Gdb(object):
32 def __init__(self):
33 path = os.path.expandvars("$RISCV/bin/riscv64-unknown-elf-gdb")
34 self.child = pexpect.spawn(path)
35 self.child.logfile = file("gdb.log", "w")
36 self.wait()
37 self.command("set width 0")
38 self.command("set height 0")
39
40 def wait(self):
41 """Wait for prompt."""
42 self.child.expect("\(gdb\)")
43
44 def command(self, command):
45 self.child.sendline(command)
46 self.child.expect("\n")
47 self.child.expect("\(gdb\)")
48 return self.child.before.strip()