Changeset 238 for soft/giet_vm/libs/stdio.c
- Timestamp:
- May 29, 2013, 1:24:09 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/libs/stdio.c
r237 r238 103 103 // This function returns the local processor time (clock cycles since boot) 104 104 //////////////////////////////////////////////////////////////////////////////////// 105 unsigned int giet_proctime() { 105 unsigned int giet_proctime() 106 { 106 107 return sys_call(SYSCALL_PROCTIME, 0, 0, 0, 0); 107 108 } … … 118 119 // - Returns 1 if the character has been written, 0 otherwise. 119 120 //////////////////////////////////////////////////////////////////////////////////// 120 unsigned int giet_tty_putc(char byte) { 121 unsigned int giet_tty_putc(char byte) 122 { 121 123 return sys_call(SYSCALL_TTY_WRITE, (unsigned int) (&byte), 1, 0, 0); 122 124 } … … 132 134 // - Returns the number of written characters. 133 135 //////////////////////////////////////////////////////////////////////////////////// 134 unsigned int giet_tty_puts(char * buf) { 136 unsigned int giet_tty_puts(char * buf) 137 { 135 138 unsigned int length = 0; 136 while (buf[length] != 0) { 137 length++; 138 } 139 while (buf[length] != 0) { length++; } 139 140 return sys_call(SYSCALL_TTY_WRITE, (unsigned int) buf, length, 0, 0); 140 141 } … … 149 150 // Returns the number of written characters (should be equal to ten). 150 151 //////////////////////////////////////////////////////////////////////////////////// 151 unsigned int giet_tty_putw(unsigned int val) { 152 unsigned int giet_tty_putw(unsigned int val) 153 { 152 154 char buf[10]; 153 155 unsigned int i; 154 for (i = 0; i < 10; i++) { 156 for (i = 0; i < 10; i++) 157 { 155 158 buf[9 - i] = (val % 10) + 0x30; 156 159 val = val / 10; … … 168 171 // - Returns 0 when completed. 169 172 //////////////////////////////////////////////////////////////////////////////////// 170 unsigned int giet_tty_getc(char * byte) { 173 unsigned int giet_tty_getc(char * byte) 174 { 171 175 unsigned int ret = 0; 172 while (ret == 0) { 176 while (ret == 0) 177 { 173 178 ret = sys_call(SYSCALL_TTY_READ, (unsigned int)byte, 1, 0, 0); 174 179 } … … 187 192 // will be copied into buffer, and the string is always completed by a NUL 188 193 // character. 189 // - The <LF> character is interpreted, a sthe function close the string with a194 // - The <LF> character is interpreted, and the function close the string with a 190 195 // NUL character if <LF> is read. 191 196 // - The <DEL> character is interpreted, and the corresponding character(s) are 192 197 // removed from the target buffer. 193 198 //////////////////////////////////////////////////////////////////////////////////// 194 unsigned int giet_tty_gets(char * buf, unsigned int bufsize) { 199 unsigned int giet_tty_gets( char* buf, 200 unsigned int bufsize) 201 { 195 202 unsigned int ret; 196 203 unsigned char byte; 197 204 unsigned int index = 0; 198 205 199 while (index < (bufsize - 1)) { 200 do { 201 ret = sys_call(SYSCALL_TTY_READ, (unsigned int) (&byte), 1, 0, 0); 202 } while (ret != 1); 203 204 if (byte == 0x0A) { 205 break; /* LF */ 206 } 207 else if ((byte == 0x7F) && (index > 0)) { 208 index--; /* DEL */ 209 } 210 else { 206 while (index < (bufsize - 1)) 207 { 208 do { ret = sys_call(SYSCALL_TTY_READ, (unsigned int) (&byte), 1, 0, 0); } 209 while (ret != 1); 210 211 if (byte == 0x0A) /* LF */ 212 { 213 break; 214 } 215 else if ((byte == 0x7F) && (index > 0)) /* DEL */ 216 { 217 index--; 218 } 219 else 220 { 211 221 buf[index] = byte; 212 222 index++; … … 236 246 // bits range, the zero value is returned. 237 247 //////////////////////////////////////////////////////////////////////////////////// 238 unsigned int giet_tty_getw(unsigned int * val) { 248 unsigned int giet_tty_getw(unsigned int * val) 249 { 239 250 unsigned char buf[32]; 240 251 unsigned char byte; … … 247 258 unsigned int ret; 248 259 249 while (done == 0) {250 do{251 ret = sys_call(SYSCALL_TTY_READ, (unsigned int) (&byte), 1, 0, 0);252 }while (ret != 1);253 254 if ((byte > 0x2F) && (byte < 0x3A)) {255 /* decimal character */260 while (done == 0) 261 { 262 do { ret = sys_call(SYSCALL_TTY_READ, (unsigned int) (&byte), 1, 0, 0); } 263 while (ret != 1); 264 265 if ((byte > 0x2F) && (byte < 0x3A)) /* decimal character */ 266 { 256 267 buf[max] = byte; 257 268 max++; 258 269 giet_tty_putc(byte); 259 270 } 260 else if ((byte == 0x0A) || (byte == 0x0D)) {261 /* LF or CR character */271 else if ((byte == 0x0A)) /* LF */ 272 { 262 273 done = 1; 263 274 } 264 else if (byte == 0x7F) { 265 /* DEL character */ 266 if (max > 0) { 267 max--; /* cancel the character */ 275 else if (byte == 0x7F) /* DEL */ 276 { 277 if (max > 0) 278 { 279 max--; /* cancel the character */ 268 280 giet_tty_putc(0x08); 269 281 giet_tty_putc(0x20); … … 271 283 } 272 284 } 273 if (max == 32) { 274 /* decimal string overflow */ 275 for (i = 0; i < max; i++) { 285 if (max == 32) /* decimal string overflow */ 286 { 287 for (i = 0; i < max; i++) 288 { 276 289 /* cancel the string */ 277 290 giet_tty_putc(0x08); … … 280 293 } 281 294 giet_tty_putc(0x30); 282 *val = 0; /* return 0 value */295 *val = 0; /* return 0 value */ 283 296 return 0; 284 297 } … … 286 299 287 300 /* string conversion */ 288 for (i = 0; i < max; i++) { 301 for (i = 0; i < max; i++) 302 { 289 303 dec = dec * 10 + (buf[i] - 0x30); 290 if (dec < save) { 291 overflow = 1; 292 } 304 if (dec < save) overflow = 1; 293 305 save = dec; 294 306 } 295 307 296 308 /* check overflow */ 297 if (overflow == 0) { 309 if (overflow == 0) 310 { 298 311 *val = dec; /* return decimal value */ 299 312 } 300 else { 301 for (i = 0; i < max; i++) { 313 else 314 { 315 for (i = 0; i < max; i++) 316 { 302 317 /* cancel the string */ 303 318 giet_tty_putc(0x08); … … 306 321 } 307 322 giet_tty_putc(0x30); 308 *val = 0; /* return 0 value */323 *val = 0; /* return 0 value */ 309 324 } 310 325 return 0; … … 326 341 // - Returns 0 if success, > 0 if error. 327 342 //////////////////////////////////////////////////////////////////////////////////// 328 unsigned int giet_tty_printf(char * format, ...) { 343 unsigned int giet_tty_printf(char * format, ...) 344 { 329 345 va_list ap; 330 346 va_start(ap, format); … … 333 349 printf_text: 334 350 335 while (*format) { 351 while (*format) 352 { 336 353 unsigned int i; 337 354 for (i = 0; format[i] && format[i] != '%'; i++); 338 if (i) { 355 if (i) 356 { 339 357 ret = sys_call(SYSCALL_TTY_WRITE, (unsigned int) format, i, 0, 0); 340 if (ret != i) { 341 return 1; /* return error */ 342 } 358 if (ret != i) return 1; /* return error */ 343 359 format += i; 344 360 } 345 if (*format == '%') { 361 if (*format == '%') 362 { 346 363 format++; 347 364 goto printf_arguments;
Note: See TracChangeset
for help on using the changeset viewer.