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