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