| 1 | /** |
|---|
| 2 | * \file stdio.c |
|---|
| 3 | * \date July 8, 2014 |
|---|
| 4 | * \author Cesar Fuguet |
|---|
| 5 | * |
|---|
| 6 | * \brief input/output functions |
|---|
| 7 | */ |
|---|
| 8 | #include <stdio.h> |
|---|
| 9 | #include <stdint.h> |
|---|
| 10 | #include <tty.h> |
|---|
| 11 | #include <cpu.h> |
|---|
| 12 | |
|---|
| 13 | static const char HexaTab[] = "0123456789ABCDEF"; |
|---|
| 14 | |
|---|
| 15 | void puts(const char *buffer) |
|---|
| 16 | { |
|---|
| 17 | sputs(buffer, 256); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | int getc(char *c) |
|---|
| 21 | { |
|---|
| 22 | return tty_getc(c); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | void putc(const char c) |
|---|
| 26 | { |
|---|
| 27 | if (c == '\n') { |
|---|
| 28 | tty_putc('\r'); |
|---|
| 29 | } |
|---|
| 30 | tty_putc(c); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void sputs(const char *buffer, unsigned int length) |
|---|
| 34 | { |
|---|
| 35 | unsigned int n; |
|---|
| 36 | for (n = 0; n < length; n++) { |
|---|
| 37 | if (buffer[n] == 0) { |
|---|
| 38 | break; |
|---|
| 39 | } |
|---|
| 40 | putc(buffer[n]); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void putx(unsigned int val) |
|---|
| 45 | { |
|---|
| 46 | char buf[9]; |
|---|
| 47 | unsigned int c; |
|---|
| 48 | |
|---|
| 49 | buf[8] = 0; |
|---|
| 50 | for (c = 0; c < 8; c++) { |
|---|
| 51 | buf[7 - c] = HexaTab[val & 0xF]; |
|---|
| 52 | val >>= 4; |
|---|
| 53 | } |
|---|
| 54 | puts(buf); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void putd(unsigned int val) |
|---|
| 58 | { |
|---|
| 59 | char buf[11]; |
|---|
| 60 | unsigned int i; |
|---|
| 61 | unsigned int first = 0; |
|---|
| 62 | |
|---|
| 63 | buf[10] = 0; |
|---|
| 64 | for (i = 0; i < 10; i++) { |
|---|
| 65 | if ((val == 0) && (i != 0)) { |
|---|
| 66 | break; |
|---|
| 67 | } |
|---|
| 68 | buf[9 - i] = HexaTab[val % 10]; |
|---|
| 69 | first = 9 - i; |
|---|
| 70 | val /= 10; |
|---|
| 71 | } |
|---|
| 72 | puts(&buf[first]); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void printf(const char *format, ...) |
|---|
| 76 | { |
|---|
| 77 | va_list ap; |
|---|
| 78 | va_start(ap, format); |
|---|
| 79 | |
|---|
| 80 | while (1) { |
|---|
| 81 | while (*format) { |
|---|
| 82 | unsigned int i; |
|---|
| 83 | for (i = 0; format[i] && (format[i] != '%'); i++) ; |
|---|
| 84 | if (i) { |
|---|
| 85 | sputs(format, i); |
|---|
| 86 | format += i; |
|---|
| 87 | } |
|---|
| 88 | if (*format == '%') { |
|---|
| 89 | format++; |
|---|
| 90 | goto printf_arguments; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | va_end(ap); |
|---|
| 94 | return; |
|---|
| 95 | |
|---|
| 96 | printf_arguments: |
|---|
| 97 | switch (*format++) { |
|---|
| 98 | case 'c': /* char conversion */ |
|---|
| 99 | { |
|---|
| 100 | char arg = (char)va_arg(ap, int); |
|---|
| 101 | putc(arg); |
|---|
| 102 | break; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | case 'd': /* 32 bits decimal signed */ |
|---|
| 106 | { |
|---|
| 107 | int arg = va_arg(ap, int); |
|---|
| 108 | if (arg < 0) { |
|---|
| 109 | putc('-'); |
|---|
| 110 | arg = -arg; |
|---|
| 111 | } |
|---|
| 112 | putd(arg); |
|---|
| 113 | break; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | case 'u': /* 32 bits decimal unsigned */ |
|---|
| 117 | { |
|---|
| 118 | unsigned int arg = va_arg(ap, unsigned int); |
|---|
| 119 | putd(arg); |
|---|
| 120 | break; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | case 'x': /* 32 bits hexadecimal unsigned */ |
|---|
| 124 | { |
|---|
| 125 | unsigned int arg = va_arg(ap, unsigned int); |
|---|
| 126 | puts("0x"); |
|---|
| 127 | putx(arg); |
|---|
| 128 | break; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | case 'l': /* 64 bits hexadecimal unsigned */ |
|---|
| 132 | { |
|---|
| 133 | uint64_t val = va_arg(ap, uint64_t); |
|---|
| 134 | puts("0x"); |
|---|
| 135 | putx(val >> 32); |
|---|
| 136 | putx(val & ((1ULL << 32) - 1)); |
|---|
| 137 | break; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | case 's': /* string */ |
|---|
| 141 | { |
|---|
| 142 | char *arg = va_arg(ap, char *); |
|---|
| 143 | puts(arg); |
|---|
| 144 | break; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | default: |
|---|
| 148 | goto return_error; |
|---|
| 149 | } /* end switch (format) */ |
|---|
| 150 | } /* end while(1) */ |
|---|
| 151 | |
|---|
| 152 | return_error: |
|---|
| 153 | puts("\n\n[ERROR] in printf()\n"); |
|---|
| 154 | exit(); |
|---|
| 155 | } /* end printf */ |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * exit function |
|---|
| 159 | */ |
|---|
| 160 | void exit() |
|---|
| 161 | { |
|---|
| 162 | cpu_wait(); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /* |
|---|
| 166 | * vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab |
|---|
| 167 | */ |
|---|