root/branches/ppalmers-streaming/tests/test-cycletimer.cpp

Revision 705, 10.6 kB (checked in by ppalmers, 15 years ago)

restructure the streaming directory

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