Mercurial > lcfOS
comparison test/testsamples.py @ 398:c0d9837acde8
x86 target refactor
author | Windel Bouwman |
---|---|
date | Thu, 29 May 2014 12:13:37 +0200 |
parents | d056b552d3f4 |
children |
comparison
equal
deleted
inserted
replaced
397:5d03c10fe19d | 398:c0d9837acde8 |
---|---|
3 import io | 3 import io |
4 from testemulation import runQemu, has_qemu | 4 from testemulation import runQemu, has_qemu |
5 from testzcc import relpath | 5 from testzcc import relpath |
6 from ppci.buildfunctions import assemble, c3compile, link | 6 from ppci.buildfunctions import assemble, c3compile, link |
7 | 7 |
8 startercode = """ | 8 mod_io_src = """ |
9 section reset | 9 module io; |
10 mov sp, 0x30000 ; setup stack pointer | 10 import arch; |
11 BL sample_start ; Branch to sample start | 11 |
12 local_loop: | 12 function void println(string txt) |
13 B local_loop | 13 { |
14 print(txt); | |
15 arch.putc(10); // Newline! | |
16 } | |
17 | |
18 function void print(string txt) | |
19 { | |
20 var int i; | |
21 i = 0; | |
22 | |
23 while (i < txt->len) | |
24 { | |
25 arch.putc(cast<int>(txt->txt[i])); | |
26 i = i + 1; | |
27 } | |
28 } | |
29 | |
30 // Print integer in hexadecimal notation: | |
31 function void print_int(int i) | |
32 { | |
33 print("0x"); | |
34 | |
35 // int txt[20]; | |
36 var int b; | |
37 var int c; | |
38 | |
39 for (b=28; b >= 0; b = b - 4) | |
40 { | |
41 c = (i >> b) & 0xF; | |
42 if (c < 10) | |
43 { | |
44 arch.putc( 48 + c ); | |
45 } | |
46 else | |
47 { | |
48 arch.putc( 65 - 10 + c ); | |
49 } | |
50 } | |
51 | |
52 arch.putc(10); // Newline! | |
53 } | |
54 | |
55 function void print2(string label, int value) | |
56 { | |
57 print(label); | |
58 print_int(value); | |
59 } | |
14 """ | 60 """ |
15 | 61 |
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 | |
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 | 62 |
39 class Samples: | 63 class Samples: |
40 def testPrint(self): | 64 def testPrint(self): |
41 snippet = """ | 65 snippet = """ |
42 module sample; | 66 module sample; |
130 do5(); | 154 do5(); |
131 do1(); | 155 do1(); |
132 do5(); | 156 do5(); |
133 } | 157 } |
134 """ | 158 """ |
135 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13]) | 159 res = "".join("G=0x{0:08X}\n".format(a) for a in [1, 2, 7, 8, 13]) |
136 self.do(snippet, res) | 160 self.do(snippet, res) |
137 | 161 |
138 | 162 |
139 class TestSamplesOnVexpress(unittest.TestCase, Samples): | 163 class TestSamplesOnVexpress(unittest.TestCase, Samples): |
140 def setUp(self): | 164 def setUp(self): |
141 if not has_qemu(): | 165 if not has_qemu(): |
142 self.skipTest('Not running qemu tests') | 166 self.skipTest('Not running qemu tests') |
143 | 167 |
144 def do(self, src, expected_output): | 168 def do(self, src, expected_output): |
169 startercode = """ | |
170 section reset | |
171 mov sp, 0x30000 ; setup stack pointer | |
172 BL sample_start ; Branch to sample start | |
173 local_loop: | |
174 B local_loop | |
175 """ | |
176 | |
177 modarchcode = """ | |
178 module arch; | |
179 | |
180 function void putc(int c) | |
181 { | |
182 var int *UART0DR; | |
183 UART0DR = cast<int*>(0x10009000); // UART0 DR register | |
184 *UART0DR = c; | |
185 } | |
186 | |
187 """ | |
188 | |
189 arch_mmap = """ | |
190 MEMORY image LOCATION=0x10000 SIZE=0x10000 { | |
191 SECTION(reset) | |
192 SECTION(code) | |
193 } | |
194 | |
195 MEMORY ram LOCATION=0x20000 SIZE=0x10000 { | |
196 SECTION(data) | |
197 } | |
198 """ | |
145 # Construct binary file from snippet: | 199 # Construct binary file from snippet: |
146 o1 = assemble(io.StringIO(startercode), 'arm') | 200 o1 = assemble(io.StringIO(startercode), 'arm') |
147 o2 = c3compile([ | 201 o2 = c3compile([ |
148 relpath('..', 'kernel', 'src', 'io.c3'), | 202 relpath('..', 'kernel', 'src', 'io.c3'), |
149 io.StringIO(modarchcode), | 203 io.StringIO(modarchcode), |
162 res = runQemu(sample_filename, machine='vexpress-a9') | 216 res = runQemu(sample_filename, machine='vexpress-a9') |
163 os.remove(sample_filename) | 217 os.remove(sample_filename) |
164 self.assertEqual(expected_output, res) | 218 self.assertEqual(expected_output, res) |
165 | 219 |
166 | 220 |
221 class TestSamplesOnX86(unittest.TestCase, Samples): | |
222 def setUp(self): | |
223 if not has_qemu(): | |
224 self.skipTest('Not running qemu tests') | |
225 self.skipTest('No x86 target yet') | |
226 | |
227 def do(self, src, expected_output): | |
228 # Construct binary file from snippet: | |
229 o1 = assemble(io.StringIO(startercode), 'x86') | |
230 o2 = c3compile([ | |
231 relpath('..', 'kernel', 'src', 'io.c3'), | |
232 io.StringIO(modarchcode), | |
233 io.StringIO(src)], [], 'x86') | |
234 o3 = link([o2, o1], io.StringIO(arch_mmap), 'x86') | |
235 | |
236 img_data = o3.get_image('image') | |
237 sample_filename = 'testsample.bin' | |
238 with open(sample_filename, 'wb') as f: | |
239 f.write(img_data) | |
240 | |
241 # Check bin file exists: | |
242 self.assertTrue(os.path.isfile(sample_filename)) | |
243 | |
244 # Run bin file in emulator: | |
245 res = runQemu(sample_filename, machine='vexpress-a9') | |
246 os.remove(sample_filename) | |
247 self.assertEqual(expected_output, res) | |
248 | |
167 # TODO: test samples on thumb target.. | 249 # TODO: test samples on thumb target.. |
168 | 250 |
169 | 251 |
170 if __name__ == '__main__': | 252 if __name__ == '__main__': |
171 unittest.main() | 253 unittest.main() |