source: soft/giet_vm/giet_common/tty0.c @ 466

Last change on this file since 466 was 466, checked in by alain, 10 years ago

1) Introducing a hierarchical, distributed lock, implemented as a

Sliced Binary Tree (sbt_lock_t) in the locks.c and locks.h files.

2) Introducing a kernel level distributed, remote_malloc() service,

In the kernel_malloc.c and kernel_malloc.h files.
This service requires one heap[x][y] global vseg per cluster.
This service is used by the distributed lock.

File size: 7.2 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File     : tty0.c
3// Date     : 02/12/2014
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The tty0.c and tty0.h files are part of the GIET-VM nano-kernel.
8///////////////////////////////////////////////////////////////////////////////////
9
10#include <hard_config.h>
11#include <tty0.h>
12#include <stdarg.h>
13#include <tty_driver.h>
14#include <utils.h>
15#include <locks.h>
16
17//////////////////////////////////////////////
18unsigned int _tty0_write( char*        buffer,
19                          unsigned int nbytes )
20{
21    unsigned int n;
22
23    for ( n = 0 ; n < nbytes ; n++ ) 
24    {
25        // return error if TTY_TX buffer full
26        if ( (_tty_get_register( 0, TTY_STATUS ) & 0x2) ) return 1;
27
28        // write one byte
29        if (buffer[n] == '\n') _tty_set_register( 0, TTY_WRITE, (unsigned int)'\r' );
30        _tty_set_register( 0, TTY_WRITE, (unsigned int)buffer[n] );
31    }
32    return 0;
33}
34
35//////////////////////////
36void _puts( char* string ) 
37{
38    unsigned int n = 0;
39
40    while ( string[n] > 0 ) n++;
41
42    _tty0_write( string, n );
43}
44
45
46//////////////////////////////
47void _putx( unsigned int val )
48{
49    static const char HexaTab[] = "0123456789ABCDEF";
50    char buf[10];
51    unsigned int c;
52
53    buf[0] = '0';
54    buf[1] = 'x';
55
56    for (c = 0; c < 8; c++) 
57    { 
58        buf[9 - c] = HexaTab[val & 0xF];
59        val = val >> 4;
60    }
61    _tty0_write( buf, 10 );
62}
63
64////////////////////////////////////
65void _putl( unsigned long long val )
66{
67    static const char HexaTab[] = "0123456789ABCDEF";
68    char buf[18];
69    unsigned int c;
70
71    buf[0] = '0';
72    buf[1] = 'x';
73
74    for (c = 0; c < 16; c++) 
75    { 
76        buf[17 - c] = HexaTab[(unsigned int)val & 0xF];
77        val = val >> 4;
78    }
79    _tty0_write( buf, 18 );
80}
81
82//////////////////////////////
83void _putd( unsigned int val ) 
84{
85    static const char DecTab[] = "0123456789";
86    char buf[10];
87    unsigned int i;
88    unsigned int first = 0;
89
90    for (i = 0; i < 10; i++) 
91    {
92        if ((val != 0) || (i == 0)) 
93        {
94            buf[9 - i] = DecTab[val % 10];
95            first = 9 - i;
96        }
97        else 
98        {
99            break;
100        }
101        val /= 10;
102    }
103    _tty0_write( &buf[first], 10 - first );
104}
105
106/////////////////////////
107void _getc( char*  byte )
108{
109    // test status register
110    while ( _tty_get_register( 0 , TTY_STATUS ) == 0 );
111
112    // read one byte
113    *byte = (char)_tty_get_register( 0 , TTY_READ );
114}
115
116//////////////////////////////////////////////////////////
117static void _kernel_printf( char * format, va_list* args ) 
118{
119
120printf_text:
121
122    while (*format) 
123    {
124        unsigned int i;
125        for (i = 0 ; format[i] && (format[i] != '%') ; i++);
126        if (i) 
127        {
128            if ( _tty0_write( format, i ) ) goto return_error;
129            format += i;
130        }
131        if (*format == '%') 
132        {
133            format++;
134            goto printf_arguments;
135        }
136    }
137
138    return;
139
140printf_arguments:
141
142    {
143        char buf[20];
144        char * pbuf;
145        unsigned int len = 0;
146        static const char HexaTab[] = "0123456789ABCDEF";
147        unsigned int i;
148
149        switch (*format++) 
150        {
151            case ('c'):             /* char conversion */
152            {
153                int val = va_arg( *args , int );
154                len = 1;
155                buf[0] = val;
156                pbuf = &buf[0];
157                break;
158            }
159            case ('d'):             /* 32 bits decimal signed  */
160            {
161                int val = va_arg( *args , int );
162                if (val < 0) 
163                {
164                    val = -val;
165                    if ( _tty0_write( "-" , 1 ) ) goto return_error;
166                }
167                for(i = 0; i < 10; i++) 
168                {
169                    buf[9 - i] = HexaTab[val % 10];
170                    if (!(val /= 10)) break;
171                }
172                len =  i + 1;
173                pbuf = &buf[9 - i];
174                break;
175            }
176            case ('u'):             /* 32 bits decimal unsigned  */
177            {
178                unsigned int val = va_arg( *args , unsigned int );
179                for(i = 0; i < 10; i++) 
180                {
181                    buf[9 - i] = HexaTab[val % 10];
182                    if (!(val /= 10)) break;
183                }
184                len =  i + 1;
185                pbuf = &buf[9 - i];
186                break;
187            }
188            case ('x'):             /* 32 bits hexadecimal unsigned */
189            {
190                unsigned int val = va_arg( *args , unsigned int );
191                if ( _tty0_write( "0x" , 2 ) ) goto return_error;
192                for(i = 0; i < 8; i++) 
193                {
194                    buf[7 - i] = HexaTab[val % 16];
195                    if (!(val /= 16))  break;
196                }
197                len =  i + 1;
198                pbuf = &buf[7 - i];
199                break;
200            }
201            case ('l'):            /* 64 bits hexadecimal unsigned */
202            {
203                unsigned long long val = va_arg( *args , unsigned long long );
204                if ( _tty0_write( "0x" , 2 ) ) goto return_error;
205                for(i = 0; i < 16; i++) 
206                {
207                    buf[15 - i] = HexaTab[val % 16];
208                    if (!(val /= 16))  break;
209                }
210                len =  i + 1;
211                pbuf = &buf[15 - i];
212                break;
213            }
214            case ('s'):             /* string */
215            {
216                char* str = va_arg( *args , char* );
217                while (str[len]) 
218                {
219                    len++;
220                }
221                pbuf = str;
222                break;
223            }
224            default:
225                goto return_error;
226        }
227
228        if ( _tty0_write( pbuf, len ) ) goto return_error;
229       
230        goto printf_text;
231    }
232
233return_error:
234
235    {
236        // try to print an error message and exit...
237        unsigned int procid     = _get_procid();
238        unsigned int x          = (procid >> (Y_WIDTH + P_WIDTH)) & ((1<<X_WIDTH)-1);
239        unsigned int y          = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1);
240        unsigned int lpid       = procid & ((1<<P_WIDTH)-1);
241        _puts("\n\n[GIET ERROR] in _printf() for processor[");
242        _putd( x );
243        _puts(",");
244        _putd( y );
245        _puts(",");
246        _putd( lpid );
247        _puts("]\n");
248
249        _exit();
250    }
251}  // end _kernel_printf()
252
253///////////////////////////////////////
254void _nolock_printf( char* format, ...)
255{
256    va_list   args;
257
258    va_start( args , format );
259    _kernel_printf( format , &args );
260    va_end( args );
261}
262////////////////////////////////
263void _printf( char* format, ...)
264{
265    va_list       args;
266    unsigned int  save_sr;
267
268    // get TTY0 lock
269    _it_disable( &save_sr );
270    _sbt_lock_acquire( &_tty_tx_lock[0] );
271
272    va_start( args , format );
273    _kernel_printf( format , &args );
274    va_end( args );
275
276    // release TTY0 lock
277    _sbt_lock_release( &_tty_tx_lock[0] );
278    _it_restore( &save_sr );
279}
280
281
282// Local Variables:
283// tab-width: 4
284// c-basic-offset: 4
285// c-file-offsets:((innamespace . 0)(inline-open . 0))
286// indent-tabs-mode: nil
287// End:
288// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
289
Note: See TracBrowser for help on using the repository browser.