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
|
372
|
45 def testParameterPassing4(self):
|
|
46 snippet = """
|
|
47 module sample;
|
|
48 import io;
|
|
49 function void dump(int a, int b, int c, int d)
|
|
50 {
|
|
51 io.print2("a=", a);
|
|
52 io.print2("b=", b);
|
|
53 io.print2("c=", c);
|
|
54 io.print2("d=", d);
|
|
55 }
|
|
56 function void start()
|
|
57 {
|
|
58 dump(4,55,66,0x1337);
|
|
59 }
|
|
60 """
|
|
61 res = "a=0x{0:08X}\n".format(4)
|
|
62 res += "b=0x{0:08X}\n".format(55)
|
|
63 res += "c=0x{0:08X}\n".format(66)
|
|
64 res += "d=0x{0:08X}\n".format(0x1337)
|
|
65 self.do(snippet, res)
|
|
66
|
366
|
67 def testGlobalVariable(self):
|
364
|
68 snippet = """
|
366
|
69 module sample;
|
364
|
70 import io;
|
|
71 var int G;
|
|
72 function void do1()
|
|
73 {
|
|
74 G = G + 1;
|
|
75 io.print2("G=", G);
|
|
76 }
|
|
77 function void do5()
|
|
78 {
|
|
79 G = G + 5;
|
|
80 io.print2("G=", G);
|
|
81 }
|
|
82 function void start()
|
|
83 {
|
|
84 G = 0;
|
|
85 do1();
|
|
86 do1();
|
366
|
87 do5();
|
364
|
88 do1();
|
366
|
89 do5();
|
364
|
90 }
|
|
91 """
|
|
92 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13])
|
|
93 self.do(snippet, res)
|
|
94
|
355
|
95
|
358
|
96 class TestSamplesOnVexpress(unittest.TestCase, Samples):
|
355
|
97 def do(self, src, expected_output):
|
366
|
98 # Construct binary file from snippet:
|
|
99 o1 = ObjectFile()
|
|
100 o2 = ObjectFile()
|
|
101 asmb = Assemble(io.StringIO(startercode), 'arm', o1)
|
|
102 comp = Compile([
|
|
103 relpath('..', 'kernel', 'src', 'io.c3'),
|
|
104 relpath('..', 'kernel', 'arch', 'vexpressA9.c3'),
|
|
105 io.StringIO(src)], [], 'arm', o2)
|
|
106 sample_filename = 'testsample.bin'
|
|
107 layout = {'code': 0x10000, 'data':0x20000}
|
|
108 link = Link([o1, o2], layout, sample_filename)
|
|
109
|
|
110 # Create task executor:
|
355
|
111 runner = TaskRunner()
|
366
|
112 runner.add_task(asmb)
|
|
113 runner.add_task(comp)
|
|
114 runner.add_task(link)
|
355
|
115 runner.run_tasks()
|
366
|
116 # Check bin file exists:
|
|
117 self.assertTrue(os.path.isfile(sample_filename))
|
|
118
|
|
119 # Run bin file in emulator:
|
|
120 res = runQemu(sample_filename, machine='vexpress-a9')
|
|
121 os.remove(sample_filename)
|
355
|
122 self.assertEqual(expected_output, res)
|
|
123
|
364
|
124
|
362
|
125 if __name__ == '__main__':
|
|
126 unittest.main()
|