root/trunk/libffado/tests/test-cycletimer.cpp

Revision 742, 10.5 kB (checked in by ppalmers, 13 years ago)

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <endian.h>
32
33 #include <signal.h>
34 #include "src/debugmodule/debugmodule.h"
35
36 #include <netinet/in.h>
37
38 #include "src/libstreaming/util/cycletimer.h"
39
40 #include "src/libstreaming/util/IsoHandler.h"
41 #include "src/libstreaming/generic/IsoStream.h"
42 #include "src/libstreaming/util/IsoHandlerManager.h"
43 #include "src/libutil/PosixThread.h"
44
45 #define TEST_PORT_0
46 // #define TEST_PORT_1
47 // #define TEST_PORT_2
48
49 using namespace Streaming;
50 using namespace Util;
51
52 DECLARE_GLOBAL_DEBUG_MODULE;
53
54 int run;
55
56 struct CYCLE_TIMER_REGISTER {
57     uint16_t seconds;
58     uint16_t cycles;
59     uint16_t offset;
60 };
61
62 uint64_t ctr_to_quadlet(struct CYCLE_TIMER_REGISTER x) {
63     uint64_t retval=0;
64
65     x.seconds &= 0x7F;
66
67     x.cycles &= 0x1FFF;
68     x.cycles %= 8000;
69
70     x.offset &= 0xFFF;
71     x.offset %= 3072;
72
73     retval = (x.seconds << 25) + (x.cycles << 12) + (x.offset);
74     return retval & 0xFFFFFFFF;
75 }
76
77 static void sighandler (int sig)
78 {
79     run = 0;
80 }
81
82 int do_cycletimer_test() {
83
84     struct CYCLE_TIMER_REGISTER cycle_timer;
85     uint32_t *cycle_timer_as_uint=(uint32_t *)&cycle_timer;
86
87     uint32_t i=0;
88     uint32_t targetval=0;
89
90     int failures=0;
91
92
93     // test 1
94     //
95
96     *cycle_timer_as_uint=0;
97     for (i=0;i<3072;i++) {
98         cycle_timer.offset=i;
99         targetval=CYCLE_TIMER_GET_OFFSET(ctr_to_quadlet(cycle_timer));
100
101         if(targetval != i) {
102             debugOutput(DEBUG_LEVEL_NORMAL, "  test1 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
103             failures++;
104         }
105     }
106
107     for (i=0;i<8000;i++) {
108         cycle_timer.cycles=i;
109         targetval=CYCLE_TIMER_GET_CYCLES(ctr_to_quadlet(cycle_timer));
110
111         if(targetval != i) {
112             debugOutput(DEBUG_LEVEL_NORMAL, "  test2 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
113             failures++;
114         }
115     }
116
117     for (i=0;i<128;i++) {
118         cycle_timer.seconds=i;
119         targetval=CYCLE_TIMER_GET_SECS(ctr_to_quadlet(cycle_timer));
120
121         if(targetval != i) {
122             debugOutput(DEBUG_LEVEL_NORMAL, "  test3 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
123             failures++;
124         }
125     }
126
127
128     // a value in ticks
129     // should be: 10sec, 1380cy, 640ticks
130     targetval=250000000L;
131     cycle_timer.seconds = TICKS_TO_SECS(targetval);
132     cycle_timer.cycles  = TICKS_TO_CYCLES(targetval);
133     cycle_timer.offset  = TICKS_TO_OFFSET(targetval);
134
135     if((cycle_timer.seconds != 10) |
136         (cycle_timer.cycles != 1380) |
137         (cycle_timer.offset != 640))
138         {
139         debugOutput(DEBUG_LEVEL_NORMAL, "  test4 failed: (%u,10)sec (%u,1380)cy (%u,640)ticks\n",
140             cycle_timer.seconds,cycle_timer.cycles,cycle_timer.offset);
141         failures++;
142     } else {
143          debugOutput(DEBUG_LEVEL_NORMAL, "  test4 ok\n");
144     }
145
146     i=TICKS_TO_CYCLE_TIMER(targetval);
147     if (i != 0x14564280) {
148          debugOutput(DEBUG_LEVEL_NORMAL, "  test5 failed: (0x%08X,0x14564280)\n",
149             i);
150         failures++;
151     } else {
152          debugOutput(DEBUG_LEVEL_NORMAL, "  test5 ok\n");
153     }
154
155     targetval=CYCLE_TIMER_TO_TICKS(i);
156     if (targetval!=250000000L) {
157          debugOutput(DEBUG_LEVEL_NORMAL, "  test6 failed: (%u,250000000)\n",
158             targetval);
159         failures++;
160     } else {
161          debugOutput(DEBUG_LEVEL_NORMAL, "  test6 ok\n");
162     }
163
164     int32_t subs;
165     subs=diffTicks(10, 8);
166     if (subs != 2) {
167          debugOutput(DEBUG_LEVEL_NORMAL, "  diffTicks(10, 8) != 2 : %ld\n",
168             subs);
169         failures++;
170     }
171
172     subs=diffTicks(10, 12);
173     if (subs != -2) {
174          debugOutput(DEBUG_LEVEL_NORMAL, "  diffTicks(10, 12) != -2 : %ld\n",
175             subs);
176         failures++;
177     }
178
179     subs=diffTicks(TICKS_PER_SECOND*128L + 10, 8);
180     if (subs != 2) {
181          debugOutput(DEBUG_LEVEL_NORMAL, "  diffTicks(TICKS_PER_SECOND*128L + 10, 8) != 2 : %ld\n",
182             subs);
183         failures++;
184     }
185
186     subs=diffTicks(TICKS_PER_SECOND*128L + 10, 12);
187     if (subs != -2) {
188          debugOutput(DEBUG_LEVEL_NORMAL, "  diffTicks(TICKS_PER_SECOND*128L + 10, 12) != -2 : %ld\n",
189             subs);
190         failures++;
191     }
192
193     subs=diffTicks(10, TICKS_PER_SECOND*128L + 8);
194     if (subs != 2) {
195          debugOutput(DEBUG_LEVEL_NORMAL, "  diffTicks(10, TICKS_PER_SECOND*128L + 8) != 2 : %ld\n",
196             subs);
197         failures++;
198     }
199
200     subs=diffTicks(10, TICKS_PER_SECOND*128L + 12);
201     if (subs != -2) {
202          debugOutput(DEBUG_LEVEL_NORMAL, "  diffTicks(10, TICKS_PER_SECOND*128L + 12) != -2 : %l011llu\n",
203             subs);
204         failures++;
205     }
206
207     //---------
208     // now = 10sec, 1380cy, 640ticks
209
210     uint32_t st=sytRecvToFullTicks(0x1234, 1000, 0x14564280);
211     if (st != 248860212LLU) {
212          debugOutput(DEBUG_LEVEL_NORMAL, "  sytToRecvFullTicks(0x1234, 1000, 0x14564280) != 248860212 : %011lu\n",
213             st);
214         failures++;
215     }
216
217     st=sytRecvToFullTicks(0xB2B6, 7000, TICKS_TO_CYCLE_TIMER(3118082282LU));
218     if (st != 3118089910LLU) {
219          debugOutput(DEBUG_LEVEL_NORMAL, "  sytToRecvFullTicks(0x1234, 1000, %08X) != 3118089910 : %011lu\n",
220             TICKS_TO_CYCLE_TIMER(3118082282LU), st);
221         failures++;
222     }
223
224     st=sytXmitToFullTicks(0xC4EA, 3000, TICKS_TO_CYCLE_TIMER(2958285668LU));
225     if (st != 2958349546LLU) {
226          debugOutput(DEBUG_LEVEL_NORMAL, "  sytToXmitFullTicks(0x1234, 1000, %08X) != 2958349546 : %011lu\n",
227             TICKS_TO_CYCLE_TIMER(2958285668LU), st);
228         failures++;
229     }
230
231     if (failures) {
232         debugOutput(DEBUG_LEVEL_NORMAL, " %d failures\n",failures);
233         return -1;
234     } else {
235         debugOutput(DEBUG_LEVEL_NORMAL, " no failures\n");
236     }
237     return 0;
238 }
239
240 int main(int argc, char *argv[])
241 {
242
243     run=1;
244
245     IsoHandlerManager *m_isoManager=NULL;
246
247 #ifdef TEST_PORT_0
248     IsoStream *s=NULL;
249 #endif
250 #ifdef TEST_PORT_1
251     IsoStream *s2=NULL;
252 #endif
253 #ifdef TEST_PORT_2
254     IsoStream *s3=NULL;
255 #endif
256
257     signal (SIGINT, sighandler);
258     signal (SIGPIPE, sighandler);
259
260     debugOutput(DEBUG_LEVEL_NORMAL, "FFADO Cycle timer test application\n");
261
262     debugOutput(DEBUG_LEVEL_NORMAL, "Testing cycle timer helper functions & macro's... \n");
263     if(do_cycletimer_test()) {
264        debugOutput(DEBUG_LEVEL_NORMAL, " !!! FAILED !!!\n");
265        exit(1);
266     } else {
267        debugOutput(DEBUG_LEVEL_NORMAL, " !!! PASSED !!!\n");
268     }
269
270 //     exit(1);
271
272     m_isoManager=new IsoHandlerManager();
273
274     if(!m_isoManager) {
275         debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoHandlerManager\n");
276         goto finish;
277     }
278
279     m_isoManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
280
281     if(!m_isoManager->init()) {
282         debugOutput(DEBUG_LEVEL_NORMAL, "Could not init() IsoHandlerManager\n");
283         goto finish;
284     }
285
286
287 #ifdef TEST_PORT_0
288     // add a stream to the manager so that it has something to do
289     s=new IsoStream(IsoStream::eST_Receive, 0);
290
291     if (!s) {
292         debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
293         goto finish;
294     }
295
296     s->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
297     s->setChannel(0);
298
299     if(!m_isoManager->registerStream(s)) {
300         debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
301         goto finish;
302     }
303 #endif
304
305 #ifdef TEST_PORT_1
306     // add a stream to the manager so that it has something to do
307     s2=new IsoStream(IsoStream::EST_Receive, 1);
308
309     if (!s2) {
310         debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
311         goto finish;
312     }
313
314     s2->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
315
316     if (!s2->init()) {
317         debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
318         goto finish;
319     }
320
321     s2->setChannel(0);
322
323     if(!m_isoManager->registerStream(s2)) {
324         debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
325         goto finish;
326     }
327 #endif
328
329 #ifdef TEST_PORT_2
330     // add a stream to the manager so that it has something to do
331     s3=new IsoStream(IsoStream::EST_Receive,2);
332
333     if (!s3) {
334         debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
335         goto finish;
336     }
337
338     s3->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
339
340     if (!s3->init()) {
341         debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
342         goto finish;
343     }
344
345     s3->setChannel(0);
346
347     if(!m_isoManager->registerStream(s3)) {
348         debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
349         goto finish;
350     }
351 #endif
352
353     debugOutput(DEBUG_LEVEL_NORMAL,   "Preparing IsoHandlerManager...\n");
354     if (!m_isoManager->prepare()) {
355         debugOutput(DEBUG_LEVEL_NORMAL, "Could not prepare isoManager\n");
356         goto finish;
357     }
358
359     debugOutput(DEBUG_LEVEL_NORMAL,   "Starting IsoHandler...\n");
360     if (!m_isoManager->startHandlers(0)) {
361         debugOutput(DEBUG_LEVEL_NORMAL, "Could not start handlers...\n");
362         goto finish;
363     }
364
365     while(run) {
366         sleep(1);
367         m_isoManager->dumpInfo();
368     }
369
370     debugOutput(DEBUG_LEVEL_NORMAL,   "Stopping handlers...\n");
371     if(!m_isoManager->stopHandlers()) {
372        debugOutput(DEBUG_LEVEL_NORMAL, "Could not stop ISO handlers\n");
373        goto finish;
374     }
375
376 #ifdef TEST_PORT_0
377     if(!m_isoManager->unregisterStream(s)) {
378         debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
379         goto finish;
380     }
381     delete s;
382 #endif
383
384 #ifdef TEST_PORT_1
385     if(!m_isoManager->unregisterStream(s1)) {
386         debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
387         goto finish;
388     }
389     delete s1;
390 #endif
391
392 #ifdef TEST_PORT_2
393     if(!m_isoManager->unregisterStream(s2)) {
394         debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
395         goto finish;
396     }
397     delete s2;
398 #endif
399
400     delete m_isoManager;
401
402 finish:
403     debugOutput(DEBUG_LEVEL_NORMAL, "Bye...\n");
404
405   return EXIT_SUCCESS;
406 }
Note: See TracBrowser for help on using the browser.