355
|
1 import unittest
|
366
|
2 import os
|
|
3 import io
|
375
|
4 from testemulation import runQemu, has_qemu
|
366
|
5 from testzcc import relpath
|
377
|
6 from ppci.buildfunctions import assemble, c3compile, link
|
355
|
7
|
366
|
8 startercode = """
|
|
9 mov sp, 0x30000 ; setup stack pointer
|
|
10 BL sample_start ; Branch to sample start
|
|
11 local_loop:
|
|
12 B local_loop
|
|
13 """
|
355
|
14
|
358
|
15 class Samples:
|
355
|
16 def testPrint(self):
|
|
17 snippet = """
|
366
|
18 module sample;
|
355
|
19 import io;
|
|
20 function void start()
|
|
21 {
|
|
22 io.print("Hello world");
|
|
23 }
|
|
24 """
|
|
25 self.do(snippet, "Hello world")
|
|
26
|
|
27 def testForLoopPrint(self):
|
|
28 snippet = """
|
366
|
29 module sample;
|
355
|
30 import io;
|
|
31 function void start()
|
|
32 {
|
|
33 var int i;
|
366
|
34 for (i=0; i<10; i = i + 1)
|
355
|
35 {
|
362
|
36 io.print2("A = ", i);
|
355
|
37 }
|
|
38 }
|
|
39 """
|
362
|
40 res = "".join("A = 0x{0:08X}\n".format(a) for a in range(10))
|
|
41 self.do(snippet, res)
|
355
|
42
|
374
|
43 def testIfStatement(self):
|
|
44 snippet = """
|
|
45 module sample;
|
|
46 import io;
|
|
47 function void start()
|
|
48 {
|
|
49 var int i;
|
|
50 i = 13;
|
|
51 if (i*7 < 100)
|
|
52 {
|
|
53 io.print("Wow");
|
|
54 }
|
|
55 else
|
|
56 {
|
|
57 io.print("Outch");
|
|
58 }
|
|
59 }
|
|
60 """
|
|
61 res = "Wow"
|
|
62 self.do(snippet, res)
|
|
63
|
372
|
64 def testParameterPassing4(self):
|
|
65 snippet = """
|
|
66 module sample;
|
|
67 import io;
|
|
68 function void dump(int a, int b, int c, int d)
|
|
69 {
|
|
70 io.print2("a=", a);
|
|
71 io.print2("b=", b);
|
|
72 io.print2("c=", c);
|
|
73 io.print2("d=", d);
|
|
74 }
|
|
75 function void start()
|
|
76 {
|
|
77 dump(4,55,66,0x1337);
|
|
78 }
|
|
79 """
|
|
80 res = "a=0x{0:08X}\n".format(4)
|
|
81 res += "b=0x{0:08X}\n".format(55)
|
|
82 res += "c=0x{0:08X}\n".format(66)
|
|
83 res += "d=0x{0:08X}\n".format(0x1337)
|
|
84 self.do(snippet, res)
|
|
85
|
366
|
86 def testGlobalVariable(self):
|
364
|
87 snippet = """
|
366
|
88 module sample;
|
364
|
89 import io;
|
|
90 var int G;
|
|
91 function void do1()
|
|
92 {
|
|
93 G = G + 1;
|
|
94 io.print2("G=", G);
|
|
95 }
|
|
96 function void do5()
|
|
97 {
|
|
98 G = G + 5;
|
|
99 io.print2("G=", G);
|
|
100 }
|
|
101 function void start()
|
|
102 {
|
|
103 G = 0;
|
|
104 do1();
|
|
105 do1();
|
366
|
106 do5();
|
364
|
107 do1();
|
366
|
108 do5();
|
364
|
109 }
|
|
110 """
|
|
111 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13])
|
|
112 self.do(snippet, res)
|
|
113
|
355
|
114
|
358
|
115 class TestSamplesOnVexpress(unittest.TestCase, Samples):
|
375
|
116 def setUp(self):
|
|
117 if not has_qemu():
|
|
118 self.skipTest('Not running qemu tests')
|
|
119
|
355
|
120 def do(self, src, expected_output):
|
366
|
121 # Construct binary file from snippet:
|
377
|
122 o1 = assemble(io.StringIO(startercode), 'arm')
|
|
123 o2 = c3compile([
|
366
|
124 relpath('..', 'kernel', 'src', 'io.c3'),
|
|
125 relpath('..', 'kernel', 'arch', 'vexpressA9.c3'),
|
377
|
126 io.StringIO(src)], [], 'arm')
|
|
127 layout = {'code': 0x10000, 'data': 0x20000}
|
|
128 o3 = link([o1, o2], layout)
|
366
|
129
|
377
|
130 sample_filename = 'testsample.bin'
|
|
131 with open(sample_filename, 'wb') as f:
|
|
132 f.write(o3.get_section('code').data)
|
|
133
|
366
|
134 # Check bin file exists:
|
|
135 self.assertTrue(os.path.isfile(sample_filename))
|
|
136
|
|
137 # Run bin file in emulator:
|
|
138 res = runQemu(sample_filename, machine='vexpress-a9')
|
|
139 os.remove(sample_filename)
|
355
|
140 self.assertEqual(expected_output, res)
|
|
141
|
364
|
142
|
362
|
143 if __name__ == '__main__':
|
|
144 unittest.main()
|
377
|
145
|