source: soft/giet_vm/giet_libs/stdio.c @ 766

Last change on this file since 766 was 766, checked in by alain, 8 years ago

Bug fix.

  • Property svn:executable set to *
File size: 37.6 KB
Line 
1/////////////////////////////////////////////////////////////////////////////////////
2// File     : stdio.c         
3// Date     : 01/04/2010
4// Author   : alain greiner & Joel Porquet
5// Copyright (c) UPMC-LIP6
6/////////////////////////////////////////////////////////////////////////////////////
7
8#include <stdarg.h>
9#include <stdio.h>
10#include <giet_config.h>
11
12/////////////////////////////////////////////////////////////////////////////////////
13// This function is called by both the giet_tty_printf() and
14// the giet_fat_printf() system calls.
15// It analyse the <format> and <args> arguments and returns a byte stream
16// in the <stream> buffer. The TO_STREAM macro checks buffer overflow.
17// It returns number of written bytes in case of success.
18// It returns 0xFFFFFFFF in case of error (illegal format, or buffer overflow).
19/////////////////////////////////////////////////////////////////////////////////////
20
21#define TO_STREAM(x) do { stream[ps] = (x); ps++; if(ps==maxlen) return -1; } while(0);
22
23static unsigned int xprintf( char*        stream,
24                             unsigned int maxlen,
25                             char*        format, 
26                             va_list*     args) 
27{
28    unsigned int ps = 0;    // write pointer to the stream buffer
29
30xprintf_text:
31
32    while ( *format != 0 ) 
33    {
34
35        if (*format == '%')   // copy argument to stream
36        {
37            format++;
38            goto xprintf_arguments;
39        }
40        else                  // copy one char to stream
41        {
42            TO_STREAM( *format );
43            format++;
44        }
45    }
46
47    return ps;
48
49xprintf_arguments:
50
51    {
52        char              buf[30];    // buffer to display one number
53        char *            pbuf;       // pointer on first char to display
54        unsigned int      len = 0;    // number of char to display
55        static const char HexaTab[] = "0123456789ABCDEF";
56        unsigned int      i;
57       
58        // Ignore fields width and precision
59        for ( ; *format >= '0' && *format <= '9'; format++ );
60
61        switch (*format) 
62        {
63            case ('c'):             // char conversion
64            {
65                int val = va_arg( *args, int );
66                buf[0] = val;
67                pbuf   = buf;
68                len    = 1;
69                break;
70            }
71            case ('d'):             // decimal signed integer
72            {
73                int val = va_arg( *args, int );
74                if (val < 0) 
75                {
76                    TO_STREAM( '-' );
77                    val = -val;
78                }
79                for(i = 0; i < 10; i++) 
80                {
81
82                    buf[9 - i] = HexaTab[val % 10];
83                    if (!(val /= 10)) break;
84                }
85                len =  i + 1;
86                pbuf = &buf[9 - i];
87                break;
88            }
89            case ('u'):             // decimal unsigned integer
90            {
91                unsigned int val = va_arg( *args, unsigned int );
92                for(i = 0; i < 10; i++) 
93                {
94                    buf[9 - i] = HexaTab[val % 10];
95                    if (!(val /= 10)) break;
96                }
97                len =  i + 1;
98                pbuf = &buf[9 - i];
99                break;
100            }
101            case ('x'):             // 32 bits hexadecimal
102            case ('l'):             // 64 bits hexadecimal
103            {
104                unsigned int       imax;
105                unsigned long long val;
106               
107                if ( *format == 'l' )   // 64 bits
108                {
109                    val = va_arg( *args, unsigned long long);
110                    imax = 16;
111                }
112                else                    // 32 bits
113                {
114                    val = va_arg( *args, unsigned int);
115                    imax = 8;
116                }
117               
118                TO_STREAM( '0' );
119                TO_STREAM( 'x' );
120               
121                for(i = 0; i < imax; i++) 
122                {
123                    buf[(imax-1) - i] = HexaTab[val % 16];
124                    if (!(val /= 16))  break;
125                }
126                len =  i + 1;
127                pbuf = &buf[(imax-1) - i];
128                break;
129            }
130            case ('s'):             /* string */
131            {
132                char* str = va_arg( *args, char* );
133                while (str[len]) { len++; }
134                pbuf = str;
135                break;
136            }
137            case ('e'):
138            case ('f'):
139            case ('g'):             // IEEE754 64 bits
140            {
141                union
142                {
143                    double d;
144                    unsigned long long ull;
145                } val;
146               
147                val.d = va_arg( *args, double );
148               
149                unsigned long long digits;
150                digits = val.ull & 0xFFFFFFFFFFFFFULL;    // mantisse
151               
152                unsigned int base;
153                base = (unsigned int)((val.ull & 0x7FF0000000000000ULL) >> 52); // exp
154
155                unsigned int intp;
156                intp = (unsigned int)val.d;         // integer part
157
158                unsigned int decp;                  // decimal part
159               
160                int isvalue = 0;
161               
162                if (base == 0x7FF) // special values
163                {
164                    if (digits & 0xFFFFFFFFFFFFFULL)  // Not a Number
165                    {
166                        buf[0] = 'N';
167                        buf[1] = 'a';
168                        buf[2] = 'N';
169                        len = 3;
170                        pbuf = buf;
171                    }
172                    else                              // infinite
173                    {
174                        /* inf */
175                        buf[0] = (val.ull & 0x8000000000000000ULL) ? '-' : '+';
176                        buf[1] = 'i';
177                        buf[2] = 'n';
178                        buf[3] = 'f';
179                        len = 4;
180                        pbuf = buf;
181                    }
182                    break;
183                }
184               
185                if (val.ull & 0x8000000000000000ULL)  // negative
186                {
187                    TO_STREAM( '-' );
188                    val.d = val.d * -1;
189                }
190                else                                  // positive
191                {
192                    TO_STREAM( '+' );
193                }
194               
195                if (val.d > 0xFFFFFFFF)              // overflow
196                {
197                    buf[0] = 'B';
198                    buf[1] = 'I';
199                    buf[2] = 'G';
200                    len = 3;
201                    pbuf = buf;
202                    break;
203                }
204               
205                val.d -= (double)intp;
206                decp = (unsigned int)(val.d * 1000000000);
207               
208                for(i = 0; i < 10; i++) 
209                {
210                    if ((!isvalue) && (intp % 10)) isvalue = 1;
211                    buf[9 - i] = HexaTab[intp % 10];
212                    if (!(intp /= 10)) break;
213                }
214                pbuf = &buf[9 - i];
215                len = i+11;
216                buf[10] = '.';
217               
218                for(i = 0; i < 9; i++)
219                {
220                    if ((!isvalue) && (decp % 10)) isvalue = 1;
221                    buf[19 - i] = HexaTab[decp % 10];
222                    decp /= 10;
223                }
224               
225                if (!isvalue)
226                {
227                    if (val.d != 0)                   // underflow
228                    {
229                        buf[0] = 'T';
230                        buf[1] = 'I';
231                        buf[2] = 'N';
232                        buf[3] = 'Y';
233                        len = 4;
234                        pbuf = buf;
235                    }
236                }
237
238                break;
239            }
240                   
241            default:       // unsupported argument type
242            {
243                return -1;
244            }
245        }  // end switch on  argument type
246
247        format++;
248
249        // copy argument to stream
250        for( i = 0 ; i < len ; i++ )
251        {
252            TO_STREAM( pbuf[i] );
253        }
254       
255        goto xprintf_text;
256    }
257
258} // end xprintf()
259
260
261//////////////////////////////////////////////////////////////////////////////
262//                     MIPS32 related system calls
263//////////////////////////////////////////////////////////////////////////////
264
265////////////////////////////////////////////
266void giet_proc_xyp( unsigned int* cluster_x,
267                    unsigned int* cluster_y,
268                    unsigned int* lpid )
269{
270    sys_call( SYSCALL_PROC_XYP,
271              (unsigned int)cluster_x,
272              (unsigned int)cluster_y,
273              (unsigned int)lpid,
274               0 );
275}
276
277////////////////////////////
278unsigned int giet_proctime() 
279{
280    return (unsigned int)sys_call( SYSCALL_PROC_TIME, 
281                                   0, 0, 0, 0 );
282}
283
284////////////////////////
285unsigned int giet_rand() 
286{
287    unsigned int x = (unsigned int)sys_call( SYSCALL_PROC_TIME,
288                                             0, 0, 0, 0);
289    if ((x & 0xF) > 7) 
290    {
291        return (x*x & 0xFFFF);
292    }
293    else 
294    {
295        return (x*x*x & 0xFFFF);
296    }
297}
298
299//////////////////////////////////////////////////////////////////////////////
300//                    Threads related  system calls
301//////////////////////////////////////////////////////////////////////////////
302
303#define THREAD_CMD_PAUSE     0
304#define THREAD_CMD_RESUME    1
305#define THREAD_CMD_CONTEXT   2
306
307//////////////////////////////////////////////////////////
308int giet_pthread_create( pthread_t*       buffer,
309                         pthread_attr_t*  attr,
310                         void*            function,
311                         void*            arg )
312{
313    return sys_call( SYSCALL_PTHREAD_CREATE,
314                     (unsigned int)buffer,
315                     (unsigned int)attr,
316                     (unsigned int)function,
317                     (unsigned int)arg );
318}             
319
320//////////////////////////////////////
321void giet_pthread_exit( void* string ) 
322{
323    sys_call( SYSCALL_PTHREAD_EXIT,
324              (unsigned int)string,
325              0, 0, 0 );
326}
327
328////////////////////////////////////////
329int giet_pthread_join( pthread_t  trdid,
330                       void**     ptr )
331{
332    return sys_call( SYSCALL_PTHREAD_JOIN,
333                     trdid,
334                     (unsigned int)ptr,
335                     0, 0 );
336}
337
338///////////////////////////////////////
339int giet_pthread_kill( pthread_t trdid,
340                       int       signal )
341{
342    return sys_call( SYSCALL_PTHREAD_KILL,
343                     trdid,
344                     signal,
345                     0, 0 );
346}
347
348/////////////////////////
349void giet_pthread_yield() 
350{
351    sys_call( SYSCALL_PTHREAD_YIELD,
352              0, 0, 0, 0 );
353}
354
355/////////////////////////////////////////////////
356void giet_pthread_assert( unsigned int condition,
357                          char*        string )
358{
359    if ( condition == 0 ) giet_pthread_exit( string );
360}
361
362////////////////////////////////////////////////
363void giet_pthread_control( unsigned int command, 
364                           char*        vspace_name,
365                           char*        thread_name )
366{
367    int ret = sys_call( SYSCALL_PTHREAD_CONTROL,
368                        command,
369                        (unsigned int) vspace_name,
370                        (unsigned int) thread_name,
371                        0 );
372
373    if ( ret == SYSCALL_VSPACE_NOT_FOUND )
374    {
375        giet_tty_printf("  ERROR in PTHREAD_CONTROL : "
376                        "vspace %s not found\n", vspace_name );
377    }
378    if ( ret == SYSCALL_THREAD_NOT_FOUND )
379    {
380        giet_tty_printf("  ERROR in PTHREAD_CONTROL : "
381                        "thread %s not found\n", thread_name );
382    }
383    if ( ret == SYSCALL_UNCOHERENT_THREAD_CONTEXT )
384    {
385        giet_tty_printf("  ERROR in PTHREAD_CONTROL : "
386                        "uncoherent context for thread %s\n", thread_name );
387    }
388    if ( ret == SYSCALL_ILLEGAL_THREAD_COMMAND_TYPE )
389    {
390        giet_tty_printf("  ERROR in PTHREAD_CONTROL : "
391                        "illegal command type %d\n", command );
392    }
393}
394
395
396//////////////////////////////////////////////////////////////////////////////
397//                    Applications related system calls
398//////////////////////////////////////////////////////////////////////////////
399
400///////////////////////////////////////
401int giet_kill_application( char* name ) 
402{
403    return ( sys_call( SYSCALL_KILL_APP,
404                       (unsigned int)name,
405                       0, 0, 0 ) );
406}
407
408///////////////////////////////////////
409int giet_exec_application( char* name ) 
410{
411    return ( sys_call( SYSCALL_EXEC_APP,
412                       (unsigned int)name,
413                       0, 0, 0 ) );
414}
415
416///////////////////////////////////////////
417void giet_applications_status( char* name )
418{
419    sys_call( SYSCALL_APPS_STATUS,
420              (unsigned int)name,
421              0, 0, 0 );
422}
423
424//////////////////////////////////////////////////////////////////////////////
425//                    Coprocessors related system calls
426//////////////////////////////////////////////////////////////////////////////
427
428///////////////////////////////////////////////////
429void giet_coproc_alloc( unsigned int   cluster_xy,
430                        unsigned int   coproc_type,
431                        unsigned int*  coproc_info )
432{
433    if ( sys_call( SYSCALL_COPROC_ALLOC,
434                   cluster_xy,
435                   coproc_type,
436                   (unsigned int)coproc_info,
437                   0 ) ) 
438        giet_pthread_exit("error in giet_coproc_alloc()");
439}
440
441//////////////////////////////////////////////////
442void giet_coproc_release( unsigned int cluster_xy,
443                          unsigned int coproc_type )
444{
445    if ( sys_call( SYSCALL_COPROC_RELEASE,
446                   cluster_xy,
447                   coproc_type,
448                   0 , 0 ) ) 
449        giet_pthread_exit("error in giet_coproc_release()");
450}
451
452//////////////////////////////////////////////////////////////////
453void giet_coproc_channel_init( unsigned int            cluster_xy,
454                               unsigned int            coproc_type,
455                               unsigned int            channel,
456                               giet_coproc_channel_t*  desc )
457{
458    if ( sys_call( SYSCALL_COPROC_CHANNEL_INIT,
459                   cluster_xy,
460                   coproc_type,
461                   channel,
462                   (unsigned int)desc ) )
463        giet_pthread_exit("error in giet_coproc_channel_init()");
464}
465
466//////////////////////////////////////////////
467void giet_coproc_run( unsigned int cluster_xy,
468                      unsigned int coproc_type )
469{
470    if ( sys_call( SYSCALL_COPROC_RUN,
471                   cluster_xy,
472                   coproc_type,
473                   0 , 0 ) ) 
474        giet_pthread_exit("error in giet_coproc_run()");
475}
476
477////////////////////////////////////////////////////
478void giet_coproc_completed( unsigned int cluster_xy,
479                            unsigned int coproc_type )
480{
481    if ( sys_call( SYSCALL_COPROC_COMPLETED,
482                   cluster_xy,
483                   coproc_type,
484                   0 , 0 ) ) 
485        giet_pthread_exit("error in giet_coproc_completed");
486}
487
488
489//////////////////////////////////////////////////////////////////////////////
490//                    TTY device related system calls
491//////////////////////////////////////////////////////////////////////////////
492
493//////////////////////////////////////////
494void giet_tty_alloc( unsigned int shared )
495{
496    if ( sys_call( SYSCALL_TTY_ALLOC,
497                   shared,
498                   0, 0, 0 ) )  giet_pthread_exit("error in giet_tty_alloc()");
499} 
500
501////////////////////////////////////////
502void giet_tty_printf( char* format, ...) 
503{
504    va_list      args;
505    char         stream[4096];
506    unsigned int length;
507
508    va_start( args, format );
509    length = xprintf( stream, 4096, format, &args ); 
510    va_end( args );
511
512    if ( length == 0xFFFFFFFF ) giet_pthread_exit("illegal format in giet_tty_printf()");
513   
514    if ( sys_call( SYSCALL_TTY_WRITE, 
515                   (unsigned int)stream,   // buffer address
516                   length,                 // number of characters
517                   0xFFFFFFFF,             // channel index from thread context
518                   0) != length )   giet_pthread_exit("error in giet_tty_printf()");
519
520}
521
522/////////////////////////////////
523void giet_tty_getc( char * byte ) 
524{
525    int ret;
526
527    do
528    {
529        ret = sys_call(SYSCALL_TTY_READ, 
530                      (unsigned int)byte,  // buffer address
531                      1,                   // number of characters
532                      0xFFFFFFFF,          // channel index from task context
533                      0);
534        if ( ret < 0 ) giet_pthread_exit("error in giet_tty_getc()");
535    }
536    while (ret != 1); 
537}
538
539/////////////////////////////////////
540void giet_tty_gets( char*        buf, 
541                    unsigned int bufsize ) 
542{
543    int           ret;                           // return value from syscalls
544    unsigned char byte;
545    unsigned int  index = 0;
546    unsigned int  string_cancel = 0x00082008;    // string containing BS/SPACE/BS
547 
548    while (index < (bufsize - 1)) 
549    {
550        // get one character
551        do 
552        { 
553            ret = sys_call(SYSCALL_TTY_READ, 
554                           (unsigned int)(&byte),
555                           1,
556                           0xFFFFFFFF,        // channel index from task context
557                           0);
558            if ( ret < 0 ) giet_pthread_exit("error in giet_tty_gets()");
559        } 
560        while (ret != 1);
561
562        // analyse character
563        if (byte == 0x0A)                          // LF  special character
564        {
565            break; 
566        }
567        else if ( (byte == 0x7F) ||                // DEL special character
568                  (byte == 0x08) )                 // BS  special character
569        {
570            if ( index > 0 )     
571            {
572                index--; 
573
574                // cancel character
575                ret = sys_call( SYSCALL_TTY_WRITE,
576                                (unsigned int)(&string_cancel),
577                                3,
578                                0XFFFFFFFF,        // channel index from task context
579                                0 );
580                if ( ret < 0 ) giet_pthread_exit("error in giet_tty_gets()");
581            }
582        }
583        else if ( (byte < 0x20) || (byte > 0x7F) )  // non printable characters
584        {
585        }
586        else                                       // take all other characters
587        {
588            buf[index] = byte;
589            index++;
590
591            // echo
592            ret = sys_call( SYSCALL_TTY_WRITE,
593                            (unsigned int)(&byte),
594                            1,
595                            0XFFFFFFFF,        // channel index from task context
596                            0 );
597            if ( ret < 0 ) giet_pthread_exit("error in giet_tty_gets()");
598     
599        }
600    }
601    buf[index] = 0;
602
603} 
604
605///////////////////////////////////////
606void giet_tty_getw( unsigned int* val ) 
607{
608    unsigned char buf[32];
609    unsigned int  string_byte   = 0x00000000;    // string containing one single byte
610    unsigned int  string_cancel = 0x00082008;    // string containing BS/SPACE/BS
611    unsigned int  save = 0;
612    unsigned int  dec = 0;
613    unsigned int  done = 0;
614    unsigned int  overflow = 0;
615    unsigned int  length = 0;
616    unsigned int  i;
617    int           ret;      // return value from syscalls
618 
619    // get characters
620    while (done == 0) 
621    {
622        // read one character
623        do 
624        { 
625            ret = sys_call( SYSCALL_TTY_READ,
626                            (unsigned int)(&string_byte),
627                            1,
628                            0xFFFFFFFF,    // channel index from task context
629                            0); 
630            if ( ret < 0 ) giet_pthread_exit("error in giet_tty_getw()");
631        } 
632        while (ret != 1);
633
634        // analyse character
635        if ((string_byte > 0x2F) && (string_byte < 0x3A))  // decimal character
636        {
637            buf[length] = (unsigned char)string_byte;
638            length++;
639
640            // echo
641            ret = sys_call( SYSCALL_TTY_WRITE, 
642                            (unsigned int)(&string_byte),
643                            1, 
644                            0xFFFFFFFF,    // channel index from task context
645                            0 );
646            if ( ret < 0 ) giet_pthread_exit("error in giet_tty_getw()");
647        }
648        else if (string_byte == 0x0A)                     // LF character
649        {
650            done = 1;
651        }
652        else if ( (string_byte == 0x7F) ||                // DEL character
653                  (string_byte == 0x08) )                 // BS  character
654        {
655            if ( length > 0 ) 
656            {
657                length--;    // cancel the character
658
659                ret = sys_call( SYSCALL_TTY_WRITE, 
660                                (unsigned int)(&string_cancel),
661                                3, 
662                                0xFFFFFFFF,    // channel index from task context
663                                0 );
664                if ( ret < 0 ) giet_pthread_exit("error in giet_tty_getw()");
665            }
666        }
667
668        // test buffer overflow
669        if ( length >= 32 ) 
670        {
671            overflow = 1;
672            done     = 1;
673        }
674    }  // end while characters
675
676    // string to int conversion with overflow detection
677    if ( overflow == 0 )
678    {
679        for (i = 0; (i < length) && (overflow == 0) ; i++) 
680        {
681            dec = dec * 10 + (buf[i] - 0x30);
682            if (dec < save)  overflow = 1; 
683            save = dec;
684        }
685    } 
686
687    // final evaluation
688    if ( overflow == 0 )
689    {
690        // return value
691        *val = dec;
692    }
693    else
694    {
695        // cancel all echo characters
696        for (i = 0; i < length ; i++) 
697        {
698            ret = sys_call( SYSCALL_TTY_WRITE, 
699                            (unsigned int)(&string_cancel),
700                            3, 
701                            0xFFFFFFFF,    // channel index from task context
702                            0 );
703            if ( ret < 0 ) giet_pthread_exit("error in giet_tty_getw()");
704        }
705        // echo character '0'
706        string_byte = 0x30;
707        ret = sys_call( SYSCALL_TTY_WRITE, 
708                        (unsigned int)(&string_byte),
709                        1, 
710                        0xFFFFFFFF,    // channel index from task context
711                        0 );
712        if ( ret < 0 ) giet_pthread_exit("error in giet_tty_getw()");
713
714        // return 0 value
715        *val = 0;
716    }
717}
718
719
720//////////////////////////////////////////////////////////////////////////////////
721//                     TIMER related system calls 
722//////////////////////////////////////////////////////////////////////////////////
723
724///////////////////////
725void giet_timer_alloc() 
726{
727    if ( sys_call( SYSCALL_TIM_ALLOC,
728                   0, 0, 0, 0 ) ) giet_pthread_exit("ERROR in TIMER_ALLOC");
729}
730
731////////////////////////////////////////////
732void giet_timer_start( unsigned int period ) 
733{
734    if ( sys_call( SYSCALL_TIM_START,
735                   period,
736                   0, 0, 0 ) ) giet_pthread_exit("ERROR in TIMER_START");
737}
738
739//////////////////////
740void giet_timer_stop() 
741{
742    if ( sys_call( SYSCALL_TIM_STOP,
743                   0, 0, 0, 0 ) ) giet_pthread_exit("ERROR in TIMER_STOP");
744}
745
746
747//////////////////////////////////////////////////////////////////////////////////
748//                   Frame buffer related system calls 
749//////////////////////////////////////////////////////////////////////////////////
750
751////////////////////////////////////////
752void giet_fbf_size( unsigned int* width,
753                    unsigned int* height )
754{
755    sys_call( SYSCALL_FBF_SIZE,
756              (unsigned int)width,
757              (unsigned int)height,
758              0, 0 );
759}
760                   
761/////////////////////
762void giet_fbf_alloc()
763{
764    if ( sys_call( SYSCALL_FBF_ALLOC, 
765                   0, 0, 0, 0 ) )    giet_pthread_exit("ERROR in FBF_ALLOC");
766}
767
768////////////////////////////////////////////
769void giet_fbf_cma_alloc( unsigned int nbufs )
770{
771    if ( sys_call( SYSCALL_FBF_CMA_ALLOC, 
772                   nbufs,
773                   0, 0, 0 ) )    giet_pthread_exit("ERROR in FBF_CMA_ALLOC");
774}
775
776///////////////////////////////////////////////
777void giet_fbf_cma_init_buf( unsigned int index,
778                            void*        buf_vaddr, 
779                            void*        sts_vaddr )
780{
781    if ( sys_call( SYSCALL_FBF_CMA_INIT_BUF,
782                   index,
783                   (unsigned int)buf_vaddr,
784                   (unsigned int)sts_vaddr, 
785                   0 ) )         giet_pthread_exit("ERROR in FBF_CMA_INIT_BUF");
786}
787
788/////////////////////////
789void giet_fbf_cma_start()
790{
791    if ( sys_call( SYSCALL_FBF_CMA_START,
792                   0, 0, 0, 0 ) )  giet_pthread_exit("ERROR in FBF_CMA_START");
793}
794
795///////////////////////////////////////////////
796void giet_fbf_cma_display( unsigned int index )
797{
798    if ( sys_call( SYSCALL_FBF_CMA_DISPLAY,
799                   index, 
800                   0, 0, 0 ) )   giet_pthread_exit("ERROR in FBF_CMA_DISPLAY");
801}
802
803/////////////////////////////////////////////
804void giet_fbf_cma_check( unsigned int index )
805{
806    if ( sys_call( SYSCALL_FBF_CMA_CHECK,
807                   index, 
808                   0, 0, 0 ) )   giet_pthread_exit("ERROR in FBF_CMA_CHECK");
809}
810
811////////////////////////
812void giet_fbf_cma_stop()
813{
814    if ( sys_call( SYSCALL_FBF_CMA_STOP, 
815                   0, 0, 0, 0 ) )  giet_pthread_exit("ERROR in FBF_CMA_STOP");
816}
817
818//////////////////////////////////////////////
819void giet_fbf_sync_write( unsigned int offset, 
820                          void *       buffer, 
821                          unsigned int length ) 
822{
823    if ( sys_call( SYSCALL_FBF_SYNC_WRITE, 
824                   offset, 
825                   (unsigned int)buffer, 
826                   length, 
827                   0 ) )  giet_pthread_exit("ERROR in FBF_SYNC_WRITE");
828}
829
830/////////////////////////////////////////////
831void giet_fbf_sync_read( unsigned int offset, 
832                         void *       buffer, 
833                         unsigned int length ) 
834{
835    if ( sys_call( SYSCALL_FBF_SYNC_READ, 
836                   offset, 
837                   (unsigned int)buffer, 
838                   length, 
839                   0 ) )   giet_pthread_exit("ERROR in FBF_SYNC_READ");
840}
841
842
843//////////////////////////////////////////////////////////////////////////////////
844//                      NIC related system calls 
845//////////////////////////////////////////////////////////////////////////////////
846
847//////////////////////////////////////////
848void giet_nic_rx_alloc( unsigned int xmax,
849                        unsigned int ymax )
850{
851    if ( sys_call( SYSCALL_NIC_ALLOC,
852                   1,                    // RX
853                   xmax,
854                   ymax,
855                   0 ) ) giet_pthread_exit("error in giet_nic_rx_alloc()");
856}
857
858//////////////////////////////////////////
859void giet_nic_tx_alloc( unsigned int xmax,
860                        unsigned int ymax )
861{
862    if ( sys_call( SYSCALL_NIC_ALLOC,
863                   0,                    // TX
864                   xmax,
865                   ymax,
866                   0 ) ) giet_pthread_exit("error in giet_nic_tx_alloc()");
867}
868
869////////////////////////
870void giet_nic_rx_start()
871{
872    if ( sys_call( SYSCALL_NIC_START,
873                   1,                    // RX
874                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_rx_start()");
875}
876
877////////////////////////
878void giet_nic_tx_start()
879{
880    if ( sys_call( SYSCALL_NIC_START,
881                   0,                    // TX
882                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_tx_start()");
883}
884
885/////////////////////////////////////
886void giet_nic_rx_move( void* buffer )
887{
888    if ( sys_call( SYSCALL_NIC_MOVE,
889                   1,                    // RX
890                   (unsigned int)buffer,
891                   0, 0 ) )  giet_pthread_exit("error in giet_nic_rx_move()");
892}
893
894/////////////////////////////////////
895void giet_nic_tx_move( void* buffer )
896{
897    if ( sys_call( SYSCALL_NIC_MOVE,
898                   0,                    // TX
899                   (unsigned int)buffer,
900                   0, 0 ) )  giet_pthread_exit("error in giet_nic_tx_move()");
901}
902
903///////////////////////
904void giet_nic_rx_stop()
905{
906    if ( sys_call( SYSCALL_NIC_STOP,
907                   1,                    // RX
908                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_rx_stop()");
909}
910
911///////////////////////
912void giet_nic_tx_stop()
913{
914    if ( sys_call( SYSCALL_NIC_STOP,
915                   0,                   // TX
916                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_tx_stop()");
917}
918
919////////////////////////
920void giet_nic_rx_stats()
921{
922    if ( sys_call( SYSCALL_NIC_STATS,
923                   1,                   // RX
924                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_rx_stats()");
925}
926
927////////////////////////
928void giet_nic_tx_stats()
929{
930    if ( sys_call( SYSCALL_NIC_STATS,
931                   0,                   // TX
932                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_tx_stats()");
933}
934
935////////////////////////
936void giet_nic_rx_clear()
937{
938    if ( sys_call( SYSCALL_NIC_CLEAR,
939                   1,                   // RX
940                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_rx_clear()");
941}
942
943////////////////////////
944void giet_nic_tx_clear()
945{
946    if ( sys_call( SYSCALL_NIC_CLEAR,
947                   0,                   // TX
948                   0, 0, 0 ) ) giet_pthread_exit("error in giet_nic_tx_clear()");
949}
950
951
952
953///////////////////////////////////////////////////////////////////////////////////
954//                   FAT related system calls
955///////////////////////////////////////////////////////////////////////////////////
956
957/////////////////////////////////////////
958int giet_fat_open( char*        pathname,
959                   unsigned int flags ) 
960{
961    return  sys_call( SYSCALL_FAT_OPEN, 
962                      (unsigned int)pathname, 
963                      flags,
964                      0, 0 );
965}
966
967/////////////////////////////////////////
968int giet_fat_close( unsigned int fd_id )
969{
970    return  sys_call( SYSCALL_FAT_CLOSE,
971                      fd_id,
972                      0, 0, 0 );
973}
974
975/////////////////////////////////////////////
976int giet_fat_file_info( unsigned int            fd_id,
977                        struct fat_file_info_s* info )
978{
979    return sys_call( SYSCALL_FAT_FINFO,
980                     fd_id,
981                     (unsigned int)info,
982                     0, 0 );
983}
984
985///////////////////////////////////////
986int giet_fat_read( unsigned int fd_id,     
987                   void*        buffer, 
988                   unsigned int count ) 
989{
990    return sys_call( SYSCALL_FAT_READ,
991                     fd_id,
992                     (unsigned int)buffer,
993                     count,
994                     0 );
995}
996
997///////////////////////////////////////
998int giet_fat_pread( unsigned int fd_id,     
999                    void*        buffer, 
1000                    unsigned int count, 
1001                    unsigned int offset ) 
1002{
1003    return sys_call( SYSCALL_FAT_PREAD,
1004                     fd_id,
1005                     (unsigned int)buffer,
1006                     count,
1007                     offset ); 
1008}
1009
1010////////////////////////////////////////
1011int giet_fat_write( unsigned int fd_id,
1012                    void*        buffer, 
1013                    unsigned int count )
1014{
1015    return sys_call( SYSCALL_FAT_WRITE, 
1016                     fd_id, 
1017                     (unsigned int)buffer,
1018                     count,
1019                     0 ); 
1020}
1021
1022////////////////////////////////////////
1023int giet_fat_lseek( unsigned int fd_id,
1024                    unsigned int offset, 
1025                    unsigned int whence )
1026{
1027    return sys_call( SYSCALL_FAT_LSEEK, 
1028                     fd_id, 
1029                     offset, 
1030                     whence,
1031                     0 ); 
1032}
1033
1034////////////////////////////////////////////
1035int giet_fat_remove( char*         pathname,
1036                     unsigned int  should_be_dir )
1037{
1038    return sys_call( SYSCALL_FAT_REMOVE,
1039                     (unsigned int)pathname,
1040                      should_be_dir,
1041                      0, 0 );
1042}
1043
1044/////////////////////////////////////
1045int giet_fat_rename( char*  old_path,
1046                     char*  new_path )
1047{
1048    return sys_call( SYSCALL_FAT_RENAME,
1049                     (unsigned int)old_path,
1050                     (unsigned int)new_path,
1051                      0, 0 );
1052}
1053
1054////////////////////////////////////
1055int giet_fat_mkdir( char* pathname )
1056{
1057    return sys_call( SYSCALL_FAT_MKDIR,
1058                     (unsigned int)pathname,
1059                      0, 0, 0 );
1060}
1061
1062////////////////////////////////////
1063int giet_fat_opendir( char* pathname )
1064{
1065    return sys_call( SYSCALL_FAT_OPENDIR,
1066                     (unsigned int)pathname,
1067                     0, 0, 0 );
1068}
1069
1070////////////////////////////////////
1071int giet_fat_closedir( unsigned int fd_id )
1072{
1073    return sys_call( SYSCALL_FAT_CLOSEDIR,
1074                     (unsigned int)fd_id,
1075                     0, 0, 0 );
1076}
1077
1078//////////////////////////////////////////
1079int giet_fat_readdir( unsigned int  fd_id,
1080                      fat_dirent_t* entry )
1081{
1082    return sys_call( SYSCALL_FAT_READDIR,
1083                     (unsigned int)fd_id,
1084                     (unsigned int)entry,
1085                     0, 0 );
1086}
1087
1088/////////////////////////////////////////
1089int giet_fat_fprintf( unsigned int fd_id,
1090                      char*        format, 
1091                      ... )
1092{
1093    va_list      args;
1094    char         stream[4096];
1095    unsigned int count;
1096
1097    va_start( args, format );
1098
1099    count = xprintf( stream, 4096, format, &args ); 
1100    va_end( args );
1101
1102    if ( count == 0xFFFFFFFF ) giet_pthread_exit("error in giet_fat_fprintf()");
1103
1104    return sys_call( SYSCALL_FAT_WRITE, 
1105                     fd_id, 
1106                     (unsigned int)stream,
1107                     count,
1108                     0 );      // no physical addressing required
1109}
1110
1111/////////////////////////////////////////
1112void* giet_fat_mmap( void*         vaddr,        // MAP_FIXED not supported
1113                     unsigned int  length,
1114                     unsigned int  prot,
1115                     unsigned int  flags,
1116                     unsigned int  fd_id,
1117                     unsigned int  offset )
1118{
1119    if ( flags & MAP_FIXED )     giet_pthread_exit("error in giet_fat_mmap()");
1120    if ( flags & MAP_PRIVATE )   giet_pthread_exit("error in giet_fat_mmap()");
1121    if ( flags & MAP_ANONYMOUS ) giet_pthread_exit("error in giet_fat_mmap()");
1122    if ( length & 0xFFF )        giet_pthread_exit("error in giet_fat_mmap()");
1123    if ( offset & 0xFFF )        giet_pthread_exit("error in giet_fat_mmap()");
1124
1125    return (void*)sys_call( SYSCALL_FAT_MMAP,
1126                            fd_id,
1127                            length>>12,
1128                            offset>>12,
1129                            prot );
1130}
1131
1132///////////////////////////////////////////
1133int giet_fat_munmap( void*         vaddr,
1134                     unsigned int  length )
1135{
1136    if ( length & 0xFFF )              giet_pthread_exit("error in giet_fat_mmap()");
1137    if ( (unsigned int)vaddr & 0xFFF ) giet_pthread_exit("error in giet_fat_mmap()");
1138
1139    return sys_call( SYSCALL_FAT_MMAP,
1140                     (unsigned int)vaddr,
1141                     length>>12,
1142                     0, 0 );
1143}
1144
1145//////////////////////////////////////////////////////////////////////////////////
1146//                      Miscellaneous system calls
1147//////////////////////////////////////////////////////////////////////////////////
1148
1149/////////////////////////////////////////////////
1150void giet_procs_number( unsigned int* x_size, 
1151                        unsigned int* y_size,
1152                        unsigned int* nprocs ) 
1153{
1154    if ( sys_call( SYSCALL_PROCS_NUMBER, 
1155                   (unsigned int)x_size, 
1156                   (unsigned int)y_size, 
1157                   (unsigned int)nprocs, 
1158                   0 ) )  giet_pthread_exit("error in giet_procs_number()");
1159}
1160
1161////////////////////////////////////////////////////
1162void giet_vobj_get_vbase( char*         vspace_name, 
1163                          char*         vobj_name, 
1164                          unsigned int* vbase ) 
1165{
1166    if ( sys_call( SYSCALL_VOBJ_GET_VBASE, 
1167                   (unsigned int) vspace_name,
1168                   (unsigned int) vobj_name,
1169                   (unsigned int) vbase,
1170                   0 ) )  giet_pthread_exit("error in giet_vobj_get_vbase()");
1171}
1172
1173////////////////////////////////////////////////////
1174void giet_vobj_get_length( char*         vspace_name, 
1175                           char*         vobj_name, 
1176                           unsigned int* length ) 
1177{
1178    if ( sys_call( SYSCALL_VOBJ_GET_LENGTH, 
1179                   (unsigned int) vspace_name,
1180                   (unsigned int) vobj_name,
1181                   (unsigned int) length,
1182                   0 ) )  giet_pthread_exit("error in giet_vobj_get_length()");
1183}
1184
1185/////////////////////////////////////////
1186void giet_heap_info( unsigned int* vaddr, 
1187                     unsigned int* length,
1188                     unsigned int  x,
1189                     unsigned int  y ) 
1190{
1191    sys_call( SYSCALL_HEAP_INFO, 
1192              (unsigned int)vaddr, 
1193              (unsigned int)length, 
1194              x,
1195              y );
1196}
1197
1198/////////////////////////////////////////
1199void giet_get_xy( void*         ptr,
1200                  unsigned int* px,
1201                  unsigned int* py )
1202{
1203    if ( sys_call( SYSCALL_GET_XY,
1204                   (unsigned int)ptr,
1205                   (unsigned int)px,
1206                   (unsigned int)py,
1207                   0 ) )  giet_pthread_exit("error in giet_get_xy()");
1208}
1209
1210// Local Variables:
1211// tab-width: 4
1212// c-basic-offset: 4
1213// c-file-offsets:((innamespace . 0)(inline-open . 0))
1214// indent-tabs-mode: nil
1215// End:
1216// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
1217
Note: See TracBrowser for help on using the repository browser.