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