annotate test/testsamples.py @ 383:173e20a47fda

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