355
|
1 import unittest
|
366
|
2 import os
|
|
3 import io
|
355
|
4 from testemulation import runQemu
|
366
|
5 from testzcc import relpath
|
355
|
6 from ppci.tasks import TaskRunner
|
366
|
7 from ppci.buildtasks import Assemble, Compile, Link
|
|
8 from ppci.objectfile import ObjectFile
|
355
|
9
|
366
|
10 startercode = """
|
|
11 mov sp, 0x30000 ; setup stack pointer
|
|
12 BL sample_start ; Branch to sample start
|
|
13 local_loop:
|
|
14 B local_loop
|
|
15 """
|
355
|
16
|
358
|
17 class Samples:
|
355
|
18 def testPrint(self):
|
|
19 snippet = """
|
366
|
20 module sample;
|
355
|
21 import io;
|
|
22 function void start()
|
|
23 {
|
|
24 io.print("Hello world");
|
|
25 }
|
|
26 """
|
|
27 self.do(snippet, "Hello world")
|
|
28
|
|
29 def testForLoopPrint(self):
|
|
30 snippet = """
|
366
|
31 module sample;
|
355
|
32 import io;
|
|
33 function void start()
|
|
34 {
|
|
35 var int i;
|
366
|
36 for (i=0; i<10; i = i + 1)
|
355
|
37 {
|
362
|
38 io.print2("A = ", i);
|
355
|
39 }
|
|
40 }
|
|
41 """
|
362
|
42 res = "".join("A = 0x{0:08X}\n".format(a) for a in range(10))
|
|
43 self.do(snippet, res)
|
355
|
44
|
366
|
45 def testGlobalVariable(self):
|
364
|
46 snippet = """
|
366
|
47 module sample;
|
364
|
48 import io;
|
|
49 var int G;
|
|
50 function void do1()
|
|
51 {
|
|
52 G = G + 1;
|
|
53 io.print2("G=", G);
|
|
54 }
|
|
55 function void do5()
|
|
56 {
|
|
57 G = G + 5;
|
|
58 io.print2("G=", G);
|
|
59 }
|
|
60 function void start()
|
|
61 {
|
|
62 G = 0;
|
|
63 do1();
|
|
64 do1();
|
366
|
65 do5();
|
364
|
66 do1();
|
366
|
67 do5();
|
364
|
68 }
|
|
69 """
|
|
70 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13])
|
|
71 self.do(snippet, res)
|
|
72
|
355
|
73
|
358
|
74 class TestSamplesOnVexpress(unittest.TestCase, Samples):
|
355
|
75 def do(self, src, expected_output):
|
366
|
76 # Construct binary file from snippet:
|
|
77 o1 = ObjectFile()
|
|
78 o2 = ObjectFile()
|
|
79 asmb = Assemble(io.StringIO(startercode), 'arm', o1)
|
|
80 comp = Compile([
|
|
81 relpath('..', 'kernel', 'src', 'io.c3'),
|
|
82 relpath('..', 'kernel', 'arch', 'vexpressA9.c3'),
|
|
83 io.StringIO(src)], [], 'arm', o2)
|
|
84 sample_filename = 'testsample.bin'
|
|
85 layout = {'code': 0x10000, 'data':0x20000}
|
|
86 link = Link([o1, o2], layout, sample_filename)
|
|
87
|
|
88 # Create task executor:
|
355
|
89 runner = TaskRunner()
|
366
|
90 runner.add_task(asmb)
|
|
91 runner.add_task(comp)
|
|
92 runner.add_task(link)
|
355
|
93 runner.run_tasks()
|
366
|
94 # Check bin file exists:
|
|
95 self.assertTrue(os.path.isfile(sample_filename))
|
|
96
|
|
97 # Run bin file in emulator:
|
|
98 res = runQemu(sample_filename, machine='vexpress-a9')
|
|
99 os.remove(sample_filename)
|
355
|
100 self.assertEqual(expected_output, res)
|
|
101
|
364
|
102
|
362
|
103 if __name__ == '__main__':
|
|
104 unittest.main()
|