Changeset 345 for soft/giet_vm/giet_libs/stdio.c
- Timestamp:
- Jun 25, 2014, 2:19:37 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/giet_libs/stdio.c
r331 r345 23 23 24 24 //////////////////////////////////////// 25 void giet_tty_printf( char* format, ...) 26 { 27 va_list args; 28 va_start( args, format ); 29 30 int ret; // return value from the syscalls 31 unsigned int channel = 0xFFFFFFFF; // channel defined in task context 25 static int __printf( char* format, unsigned int channel, va_list* args) 26 { 27 int ret; // return value from the syscalls 32 28 33 29 printf_text: … … 56 52 } 57 53 58 va_end( args ); 59 return; 54 return 0; 60 55 61 56 printf_arguments: … … 72 67 case ('c'): /* char conversion */ 73 68 { 74 int val = va_arg( args, int );69 int val = va_arg( *args, int ); 75 70 len = 1; 76 71 buf[0] = val; … … 80 75 case ('d'): /* 32 bits decimal signed integer */ 81 76 { 82 int val = va_arg( args, int );77 int val = va_arg( *args, int ); 83 78 if (val < 0) 84 79 { … … 102 97 case ('u'): /* 32 bits decimal unsigned integer */ 103 98 { 104 unsigned int val = va_arg( args, unsigned int );99 unsigned int val = va_arg( *args, unsigned int ); 105 100 for(i = 0; i < 10; i++) 106 101 { … … 114 109 case ('x'): /* 32 bits hexadecimal integer */ 115 110 { 116 unsigned int val = va_arg( args, unsigned int );111 unsigned int val = va_arg( *args, unsigned int ); 117 112 ret = sys_call(SYSCALL_TTY_WRITE, 118 113 (unsigned int)"0x", … … 132 127 case ('l'): /* 64 bits hexadecimal unsigned */ 133 128 { 134 unsigned long long val = va_arg( args, unsigned long long );129 unsigned long long val = va_arg( *args, unsigned long long ); 135 130 ret = sys_call(SYSCALL_TTY_WRITE, 136 131 (unsigned int)"0x", … … 150 145 case ('s'): /* string */ 151 146 { 152 char* str = va_arg( args, char* );147 char* str = va_arg( *args, char* ); 153 148 while (str[len]) 154 149 { … … 173 168 174 169 return_error: 175 170 return 1; 171 } // end __printf() 172 173 174 //////////////////////////////////////// 175 void giet_tty_printf( char* format, ...) 176 { 177 va_list args; 178 179 va_start( args, format ); 180 int ret = __printf(format, 0xFFFFFFFF, &args); 176 181 va_end( args ); 177 giet_exit("error in giet_tty_printf()"); 182 183 if (ret) 184 { 185 giet_exit("error in giet_tty_printf()"); 186 } 178 187 } // end giet_tty_printf() 179 188 … … 182 191 { 183 192 va_list args; 184 va_start( args, format ); 185 186 int ret; // return value from the syscalls 187 unsigned int channel = 0; 188 unsigned int sr_save; 193 const int channel = 0; 194 volatile unsigned int sr_save; 189 195 190 196 sys_call( SYSCALL_TTY_GET_LOCK, … … 193 199 0, 0 ); 194 200 195 printf_text: 196 197 while (*format) 198 { 199 unsigned int i; 200 for (i = 0 ; format[i] && (format[i] != '%') ; i++); 201 if (i) 202 { 203 ret = sys_call(SYSCALL_TTY_WRITE, 204 (unsigned int)format, 205 i, 206 channel, 207 0); 208 209 if (ret != i) goto return_error; 210 211 format += i; 212 } 213 if (*format == '%') 214 { 215 format++; 216 goto printf_arguments; 217 } 218 } 201 va_start( args, format ); 202 int ret = __printf(format, channel, &args); 203 va_end( args ); 219 204 220 205 sys_call( SYSCALL_TTY_RELEASE_LOCK, … … 222 207 (unsigned int)&sr_save, 223 208 0, 0 ); 224 225 va_end( args ); 226 return; 227 228 printf_arguments: 229 230 { 231 char buf[20]; 232 char * pbuf; 233 unsigned int len = 0; 234 static const char HexaTab[] = "0123456789ABCDEF"; 235 unsigned int i; 236 237 switch (*format++) 238 { 239 case ('c'): /* char conversion */ 240 { 241 int val = va_arg( args, int ); 242 len = 1; 243 buf[0] = val; 244 pbuf = &buf[0]; 245 break; 246 } 247 case ('d'): /* 32 bits decimal signed integer */ 248 { 249 int val = va_arg( args, int ); 250 if (val < 0) 251 { 252 val = -val; 253 ret = sys_call(SYSCALL_TTY_WRITE, 254 (unsigned int)"-", 255 1, 256 channel, 257 0); 258 if (ret != 1) goto return_error; 259 } 260 for(i = 0; i < 10; i++) 261 { 262 buf[9 - i] = HexaTab[val % 10]; 263 if (!(val /= 10)) break; 264 } 265 len = i + 1; 266 pbuf = &buf[9 - i]; 267 break; 268 } 269 case ('u'): /* 32 bits decimal unsigned integer */ 270 { 271 unsigned int val = va_arg( args, unsigned int ); 272 for(i = 0; i < 10; i++) 273 { 274 buf[9 - i] = HexaTab[val % 10]; 275 if (!(val /= 10)) break; 276 } 277 len = i + 1; 278 pbuf = &buf[9 - i]; 279 break; 280 } 281 case ('x'): /* 32 bits hexadecimal integer */ 282 { 283 unsigned int val = va_arg( args, unsigned int ); 284 ret = sys_call(SYSCALL_TTY_WRITE, 285 (unsigned int)"0x", 286 2, 287 channel, 288 0); 289 if (ret != 2) goto return_error; 290 for(i = 0; i < 8; i++) 291 { 292 buf[7 - i] = HexaTab[val % 16]; 293 if (!(val /= 16)) break; 294 } 295 len = i + 1; 296 pbuf = &buf[7 - i]; 297 break; 298 } 299 case ('l'): /* 64 bits hexadecimal unsigned */ 300 { 301 unsigned long long val = va_arg( args, unsigned long long ); 302 ret = sys_call(SYSCALL_TTY_WRITE, 303 (unsigned int)"0x", 304 2, 305 channel, 306 0); 307 if (ret != 2) goto return_error; 308 for(i = 0; i < 16; i++) 309 { 310 buf[15 - i] = HexaTab[val % 16]; 311 if (!(val /= 16)) break; 312 } 313 len = i + 1; 314 pbuf = &buf[15 - i]; 315 break; 316 } 317 case ('s'): /* string */ 318 { 319 char* str = va_arg( args, char* ); 320 while (str[len]) 321 { 322 len++; 323 } 324 pbuf = str; 325 break; 326 } 327 default: 328 goto return_error; 329 } 330 331 ret = sys_call(SYSCALL_TTY_WRITE, 332 (unsigned int)pbuf, 333 len, 334 channel, 335 0); 336 if (ret != len) goto return_error; 337 338 goto printf_text; 339 } 340 341 return_error: 342 343 sys_call( SYSCALL_TTY_RELEASE_LOCK, 344 channel, 345 (unsigned int)&sr_save, 346 0, 0 ); 347 348 va_end( args ); 349 giet_exit("error in giet_shr_printf()"); 350 } // end giet_shr_printf() 351 209 210 if (ret) 211 { 212 giet_exit("error in giet_shr_printf()"); 213 } 214 } // end giet_shr_printf() 352 215 353 216 /////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.