annotate test/testemulation.py @ 365:98ff43cfdd36

Nasty bug in adr instruction
author Windel Bouwman
date Wed, 19 Mar 2014 22:32:04 +0100
parents c49459768aaa
children 39bf68bf1891
rev   line source
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
1 import unittest
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
2 import os
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
3 import sys
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
4 import subprocess
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
5 import socket
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
6 import time
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
7
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
8 from testzcc import ZccBaseTestCase
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
9
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
10 # Store testdir for safe switch back to directory:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
11 testdir = os.path.dirname(os.path.abspath(__file__))
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
12
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
13 def runQemu(kernel, machine='lm3s811evb'):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
14 """ Runs qemu on a given kernel file """
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
15 try:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
16 os.remove('qemucontrol.sock')
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
17 except FileNotFoundError:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
18 pass
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
19
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
20 try:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
21 os.remove('qemuserial.sock')
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
22 except FileNotFoundError:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
23 pass
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
24
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
25 # Listen to the control socket:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
26 qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
27 qemu_control.bind('qemucontrol.sock')
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
28 qemu_control.listen(0)
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
29
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
30 # Listen to the serial output:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
31 qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
32 qemu_serial.bind('qemuserial.sock')
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
33 qemu_serial.listen(0)
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
34
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
35 args = ['qemu-system-arm', '-M', machine, '-m', '16M',
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
36 '-nographic', '-kernel', kernel, '-monitor',
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
37 'unix:qemucontrol.sock',
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
38 '-serial', 'unix:qemuserial.sock', '-S']
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
39 p = subprocess.Popen(args)
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
40 #stdout=subprocess.DEVNULL,
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
41 #stderr=subprocess.DEVNULL)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
42
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
43 # Give process some time to boot:
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
44 qemu_serial, address_peer = qemu_serial.accept()
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
45 qemu_control, address_peer = qemu_control.accept()
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
46 qemu_control.send('cont\n'.encode('ascii'))
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
47
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
48 qemu_serial.settimeout(0.2)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
49
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
50 # Receive all data:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
51 data = bytearray()
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
52 for i in range(400):
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
53 try:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
54 data += qemu_serial.recv(1)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
55 except socket.timeout as e:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
56 break
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
57 data = data.decode('ascii')
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
58 # print(data)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
59
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
60 # Send quit command:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
61 qemu_control.send("quit\n".encode('ascii'))
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
62 try:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
63 p.wait(timeout=3)
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
64 except subprocess.TimeoutExpired:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
65 p.kill()
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
66 print(p.communicate())
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
67 qemu_control.close()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
68 qemu_serial.close()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
69
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
70 try:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
71 os.remove('qemucontrol.sock')
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
72 except FileNotFoundError:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
73 pass
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
74
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
75 try:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
76 os.remove('qemuserial.sock')
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
77 except FileNotFoundError:
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
78 pass
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
79
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
80 # Check that output was correct:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
81 return data
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
82
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
83
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
84 class EmulationTestCase(ZccBaseTestCase):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
85 """ Tests the compiler driver """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
86 def setUp(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
87 if 'TESTEMU' not in os.environ:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
88 self.skipTest('Not running emulation tests')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
89
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
90 def testM3Bare(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
91 """ Build bare m3 binary and emulate it """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
92 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
93 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
94 data = runQemu('m3_bare/bare.bin')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
95 self.assertEqual('Hello worle', data)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
96
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
97 def testA9Bare(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
98 """ Build vexpress cortex-A9 binary and emulate it """
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
99 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
100 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
101 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
102 self.assertEqual('Hello worle', data)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
103
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
104 def testKernelVexpressA9(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
105 """ Build vexpress cortex-A9 binary and emulate it """
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
106 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
107 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
108 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
361
614a7f6d4d4d Fixed test
Windel Bouwman
parents: 356
diff changeset
109 self.assertEqual('Welcome to lcfos!', data[:17])
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
110
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
111
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
112 if __name__ == '__main__':
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
113 unittest.main()