| 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 <tty_driver.h> |
|---|
| 13 | #include <stdarg.h> |
|---|
| 14 | #include <utils.h> |
|---|
| 15 | #include <locks.h> |
|---|
| 16 | |
|---|
| 17 | ////////////////////////////////////////////// |
|---|
| 18 | unsigned 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 | ////////////////////////// |
|---|
| 36 | void _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 | ////////////////////////////// |
|---|
| 47 | void _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 | //////////////////////////////////// |
|---|
| 65 | void _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 | ////////////////////////////// |
|---|
| 83 | void _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 | ///////////////////////// |
|---|
| 107 | void _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 | ////////////////////////////////// |
|---|
| 117 | void _printf( char * format, ... ) |
|---|
| 118 | { |
|---|
| 119 | va_list ap; |
|---|
| 120 | va_start(ap, format); |
|---|
| 121 | unsigned int save_sr; // to save SR value in critical section |
|---|
| 122 | |
|---|
| 123 | // get TTY lock |
|---|
| 124 | _it_disable( &save_sr ); |
|---|
| 125 | _simple_lock_acquire( &_tty_tx_lock[0] ); |
|---|
| 126 | |
|---|
| 127 | printf_text: |
|---|
| 128 | |
|---|
| 129 | while (*format) |
|---|
| 130 | { |
|---|
| 131 | unsigned int i; |
|---|
| 132 | for (i = 0 ; format[i] && (format[i] != '%') ; i++); |
|---|
| 133 | if (i) |
|---|
| 134 | { |
|---|
| 135 | if ( _tty0_write( format, i ) ) goto return_error; |
|---|
| 136 | format += i; |
|---|
| 137 | } |
|---|
| 138 | if (*format == '%') |
|---|
| 139 | { |
|---|
| 140 | format++; |
|---|
| 141 | goto printf_arguments; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | // release TTY lock |
|---|
| 146 | _simple_lock_release( &_tty_tx_lock[0] ); |
|---|
| 147 | _it_restore( &save_sr ); |
|---|
| 148 | |
|---|
| 149 | va_end(ap); |
|---|
| 150 | return; |
|---|
| 151 | |
|---|
| 152 | printf_arguments: |
|---|
| 153 | |
|---|
| 154 | { |
|---|
| 155 | char buf[20]; |
|---|
| 156 | char * pbuf; |
|---|
| 157 | unsigned int len = 0; |
|---|
| 158 | static const char HexaTab[] = "0123456789ABCDEF"; |
|---|
| 159 | unsigned int i; |
|---|
| 160 | |
|---|
| 161 | switch (*format++) |
|---|
| 162 | { |
|---|
| 163 | case ('c'): /* char conversion */ |
|---|
| 164 | { |
|---|
| 165 | int val = va_arg( ap, int ); |
|---|
| 166 | len = 1; |
|---|
| 167 | buf[0] = val; |
|---|
| 168 | pbuf = &buf[0]; |
|---|
| 169 | break; |
|---|
| 170 | } |
|---|
| 171 | case ('d'): /* 32 bits decimal signed */ |
|---|
| 172 | { |
|---|
| 173 | int val = va_arg( ap, int ); |
|---|
| 174 | if (val < 0) |
|---|
| 175 | { |
|---|
| 176 | val = -val; |
|---|
| 177 | if ( _tty0_write( "-" , 1 ) ) goto return_error; |
|---|
| 178 | } |
|---|
| 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 ('u'): /* 32 bits decimal unsigned */ |
|---|
| 189 | { |
|---|
| 190 | unsigned int val = va_arg( ap, unsigned int ); |
|---|
| 191 | for(i = 0; i < 10; i++) |
|---|
| 192 | { |
|---|
| 193 | buf[9 - i] = HexaTab[val % 10]; |
|---|
| 194 | if (!(val /= 10)) break; |
|---|
| 195 | } |
|---|
| 196 | len = i + 1; |
|---|
| 197 | pbuf = &buf[9 - i]; |
|---|
| 198 | break; |
|---|
| 199 | } |
|---|
| 200 | case ('x'): /* 32 bits hexadecimal unsigned */ |
|---|
| 201 | { |
|---|
| 202 | unsigned int val = va_arg( ap, unsigned int ); |
|---|
| 203 | if ( _tty0_write( "0x" , 2 ) ) goto return_error; |
|---|
| 204 | for(i = 0; i < 8; i++) |
|---|
| 205 | { |
|---|
| 206 | buf[7 - i] = HexaTab[val % 16]; |
|---|
| 207 | if (!(val /= 16)) break; |
|---|
| 208 | } |
|---|
| 209 | len = i + 1; |
|---|
| 210 | pbuf = &buf[7 - i]; |
|---|
| 211 | break; |
|---|
| 212 | } |
|---|
| 213 | case ('l'): /* 64 bits hexadecimal unsigned */ |
|---|
| 214 | { |
|---|
| 215 | unsigned long long val = va_arg( ap, unsigned long long ); |
|---|
| 216 | if ( _tty0_write( "0x" , 2 ) ) goto return_error; |
|---|
| 217 | for(i = 0; i < 16; i++) |
|---|
| 218 | { |
|---|
| 219 | buf[15 - i] = HexaTab[val % 16]; |
|---|
| 220 | if (!(val /= 16)) break; |
|---|
| 221 | } |
|---|
| 222 | len = i + 1; |
|---|
| 223 | pbuf = &buf[15 - i]; |
|---|
| 224 | break; |
|---|
| 225 | } |
|---|
| 226 | case ('s'): /* string */ |
|---|
| 227 | { |
|---|
| 228 | char* str = va_arg( ap, char* ); |
|---|
| 229 | while (str[len]) |
|---|
| 230 | { |
|---|
| 231 | len++; |
|---|
| 232 | } |
|---|
| 233 | pbuf = str; |
|---|
| 234 | break; |
|---|
| 235 | } |
|---|
| 236 | default: |
|---|
| 237 | goto return_error; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if ( _tty0_write( pbuf, len ) ) goto return_error; |
|---|
| 241 | |
|---|
| 242 | goto printf_text; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | return_error: |
|---|
| 246 | |
|---|
| 247 | { |
|---|
| 248 | // release TTY lock |
|---|
| 249 | _simple_lock_release( &_tty_tx_lock[0] ); |
|---|
| 250 | _it_restore( &save_sr ); |
|---|
| 251 | |
|---|
| 252 | // try to print a non protected error message... |
|---|
| 253 | unsigned int procid = _get_procid(); |
|---|
| 254 | unsigned int x = (procid >> (Y_WIDTH + P_WIDTH)) & ((1<<X_WIDTH)-1); |
|---|
| 255 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
|---|
| 256 | unsigned int lpid = procid & ((1<<P_WIDTH)-1); |
|---|
| 257 | |
|---|
| 258 | _puts("\n\n[GIET ERROR] in _printf() for processor["); |
|---|
| 259 | _putd( x ); |
|---|
| 260 | _puts(","); |
|---|
| 261 | _putd( y ); |
|---|
| 262 | _puts(","); |
|---|
| 263 | _putd( lpid ); |
|---|
| 264 | _puts("]\n"); |
|---|
| 265 | |
|---|
| 266 | _exit(); |
|---|
| 267 | } |
|---|
| 268 | } // end _printf() |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | // Local Variables: |
|---|
| 272 | // tab-width: 4 |
|---|
| 273 | // c-basic-offset: 4 |
|---|
| 274 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 275 | // indent-tabs-mode: nil |
|---|
| 276 | // End: |
|---|
| 277 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 278 | |
|---|