annotate test/testsamples.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 9667d78ba79e
children 173e20a47fda
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
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
27 class Samples:
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
28 def testPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
29 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
30 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
31 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
32 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
33 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
34 io.print("Hello world");
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
35 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
36 """
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
37 self.do(snippet, "Hello world")
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
38
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
39 def testForLoopPrint(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 var int i;
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
46 for (i=0; i<10; i = i + 1)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
47 {
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
48 io.print2("A = ", i);
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
49 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
50 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
51 """
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
52 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
53 self.do(snippet, res)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
54
374
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
55 def testIfStatement(self):
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
56 snippet = """
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
57 module sample;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
58 import io;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
59 function void start()
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
60 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
61 var int i;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
62 i = 13;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
63 if (i*7 < 100)
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
64 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
65 io.print("Wow");
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
66 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
67 else
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
68 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
69 io.print("Outch");
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
70 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
71 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
72 """
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
73 res = "Wow"
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
74 self.do(snippet, res)
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
75
372
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
76 def testParameterPassing4(self):
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
77 snippet = """
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
78 module sample;
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
79 import io;
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
80 function void dump(int a, int b, int c, int d)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
81 {
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
82 io.print2("a=", a);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
83 io.print2("b=", b);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
84 io.print2("c=", c);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
85 io.print2("d=", d);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
86 }
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
87 function void start()
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
88 {
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
89 dump(4,55,66,0x1337);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
90 }
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
91 """
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
92 res = "a=0x{0:08X}\n".format(4)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
93 res += "b=0x{0:08X}\n".format(55)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
94 res += "c=0x{0:08X}\n".format(66)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
95 res += "d=0x{0:08X}\n".format(0x1337)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
96 self.do(snippet, res)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
97
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
98 def testGlobalVariable(self):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
99 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
100 module sample;
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
101 import io;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
102 var int G;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
103 function void do1()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
104 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
105 G = G + 1;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
106 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
107 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
108 function void do5()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
109 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
110 G = G + 5;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
111 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
112 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
113 function void start()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
114 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
115 G = 0;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
116 do1();
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
117 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
118 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
119 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
120 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
121 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
122 """
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
123 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
124 self.do(snippet, res)
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
125
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
126
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
127 class TestSamplesOnVexpress(unittest.TestCase, Samples):
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
128 def setUp(self):
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
129 if not has_qemu():
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
130 self.skipTest('Not running qemu tests')
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
131
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
132 def do(self, src, expected_output):
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
133 # Construct binary file from snippet:
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
134 o1 = assemble(io.StringIO(startercode), 'arm')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
135 o2 = c3compile([
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
136 relpath('..', 'kernel', 'src', 'io.c3'),
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
137 io.StringIO(modarchcode),
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
138 io.StringIO(src)], [], 'arm')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
139 layout = {'code': 0x10000, 'data': 0x20000}
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
140 o3 = link([o1, o2], layout)
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
141
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
142 sample_filename = 'testsample.bin'
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
143 with open(sample_filename, 'wb') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
144 f.write(o3.get_section('code').data)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
145
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
146 # Check bin file exists:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
147 self.assertTrue(os.path.isfile(sample_filename))
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
148
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
149 # Run bin file in emulator:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
150 res = runQemu(sample_filename, machine='vexpress-a9')
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
151 os.remove(sample_filename)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
152 self.assertEqual(expected_output, res)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
153
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
154
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
155 if __name__ == '__main__':
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
156 unittest.main()
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
157