OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [dhrystone/] [port/] [iolib.c] - Blame information for rev 280

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 280 hellwig
/*
2
 * iolib.c -- I/O library
3
 */
4
 
5
 
6
#include "types.h"
7
#include "stdarg.h"
8
#include "iolib.h"
9
#include "biolib.h"
10
 
11
 
12
/**************************************************************/
13
 
14
/* string functions */
15
 
16
 
17
int strlen(char *str) {
18
  int i;
19
 
20
  i = 0;
21
  while (*str++ != '\0') {
22
    i++;
23
  }
24
  return i;
25
}
26
 
27
 
28
int strcmp(char *s1, char *s2) {
29
  while (*s1 == *s2) {
30
    if (*s1 == '\0') {
31
      return 0;
32
    }
33
    s1++;
34
    s2++;
35
  }
36
  return *s1 - *s2;
37
}
38
 
39
 
40
void strcpy(char *dst, char *src) {
41
  while ((*dst++ = *src++) != '\0') ;
42
}
43
 
44
 
45
void memcpy(unsigned char *dst, unsigned char *src, unsigned int cnt) {
46
  while (cnt--) {
47
    *dst++ = *src++;
48
  }
49
}
50
 
51
 
52
/**************************************************************/
53
 
54
/* terminal I/O */
55
 
56
 
57
char getchar(void) {
58
  return getc();
59
}
60
 
61
 
62
void putchar(char c) {
63
  if (c == '\n') {
64
    putchar('\r');
65
  }
66
  putc(c);
67
}
68
 
69
 
70
void putString(char *s) {
71
  while (*s != '\0') {
72
    putchar(*s++);
73
  }
74
}
75
 
76
 
77
/**************************************************************/
78
 
79
/* get a line from the terminal */
80
 
81
 
82
void getLine(char *prompt, char *line, int max) {
83
  int index;
84
  char c;
85
 
86
  putString(prompt);
87
  putString(line);
88
  index = strlen(line);
89
  while (1) {
90
    c = getchar();
91
    switch (c) {
92
      case '\r':
93
        putchar('\n');
94
        line[index] = '\0';
95
        return;
96
      case '\b':
97
      case 0x7F:
98
        if (index == 0) {
99
          break;
100
        }
101
        putchar('\b');
102
        putchar(' ');
103
        putchar('\b');
104
        index--;
105
        break;
106
      default:
107
        if (c == '\t') {
108
          c = ' ';
109
        }
110
        if (c < 0x20 || c > 0x7E) {
111
          break;
112
        }
113
        putchar(c);
114
        line[index++] = c;
115
        break;
116
    }
117
  }
118
}
119
 
120
 
121
/**************************************************************/
122
 
123
/* scaled-down version of printf */
124
 
125
 
126
/*
127
 * Count the number of characters needed to represent
128
 * a given number in base 10.
129
 */
130
int countPrintn(long n) {
131
  long a;
132
  int res;
133
 
134
  res = 0;
135
  if (n < 0) {
136
    res++;
137
    n = -n;
138
  }
139
  a = n / 10;
140
  if (a != 0) {
141
    res += countPrintn(a);
142
  }
143
  return res + 1;
144
}
145
 
146
 
147
/*
148
 * Output a number in base 10.
149
 */
150
void printn(long n) {
151
  long a;
152
 
153
  if (n < 0) {
154
    putchar('-');
155
    n = -n;
156
  }
157
  a = n / 10;
158
  if (a != 0) {
159
    printn(a);
160
  }
161
  putchar(n % 10 + '0');
162
}
163
 
164
 
165
/*
166
 * Count the number of characters needed to represent
167
 * a given number in a given base.
168
 */
169
int countPrintu(unsigned long n, unsigned long b) {
170
  unsigned long a;
171
  int res;
172
 
173
  res = 0;
174
  a = n / b;
175
  if (a != 0) {
176
    res += countPrintu(a, b);
177
  }
178
  return res + 1;
179
}
180
 
181
 
182
/*
183
 * Output a number in a given base.
184
 */
185
void printu(unsigned long n, unsigned long b, Bool upperCase) {
186
  unsigned long a;
187
 
188
  a = n / b;
189
  if (a != 0) {
190
    printu(a, b, upperCase);
191
  }
192
  if (upperCase) {
193
    putchar("0123456789ABCDEF"[n % b]);
194
  } else {
195
    putchar("0123456789abcdef"[n % b]);
196
  }
197
}
198
 
199
 
200
/*
201
 * Output a number of filler characters.
202
 */
203
void fill(int numFillers, char filler) {
204
  while (numFillers-- > 0) {
205
    putchar(filler);
206
  }
207
}
208
 
209
 
210
/*
211
 * Formatted output with a variable argument list.
212
 */
213
void vprintf(char *fmt, va_list ap) {
214
  char c;
215
  int n;
216
  long ln;
217
  unsigned int u;
218
  unsigned long lu;
219
  char *s;
220
  Bool negFlag;
221
  char filler;
222
  int width, count;
223
 
224
  while (1) {
225
    while ((c = *fmt++) != '%') {
226
      if (c == '\0') {
227
        return;
228
      }
229
      putchar(c);
230
    }
231
    c = *fmt++;
232
    if (c == '-') {
233
      negFlag = TRUE;
234
      c = *fmt++;
235
    } else {
236
      negFlag = FALSE;
237
    }
238
    if (c == '0') {
239
      filler = '0';
240
      c = *fmt++;
241
    } else {
242
      filler = ' ';
243
    }
244
    width = 0;
245
    while (c >= '0' && c <= '9') {
246
      width *= 10;
247
      width += c - '0';
248
      c = *fmt++;
249
    }
250
    if (c == 'd') {
251
      n = va_arg(ap, int);
252
      count = countPrintn(n);
253
      if (width > 0 && !negFlag) {
254
        fill(width - count, filler);
255
      }
256
      printn(n);
257
      if (width > 0 && negFlag) {
258
        fill(width - count, filler);
259
      }
260
    } else
261
    if (c == 'u' || c == 'o' || c == 'x' || c == 'X') {
262
      u = va_arg(ap, int);
263
      count = countPrintu(u,
264
                c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10));
265
      if (width > 0 && !negFlag) {
266
        fill(width - count, filler);
267
      }
268
      printu(u,
269
             c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10),
270
             c == 'X');
271
      if (width > 0 && negFlag) {
272
        fill(width - count, filler);
273
      }
274
    } else
275
    if (c == 'l') {
276
      c = *fmt++;
277
      if (c == 'd') {
278
        ln = va_arg(ap, long);
279
        count = countPrintn(ln);
280
        if (width > 0 && !negFlag) {
281
          fill(width - count, filler);
282
        }
283
        printn(ln);
284
        if (width > 0 && negFlag) {
285
          fill(width - count, filler);
286
        }
287
      } else
288
      if (c == 'u' || c == 'o' || c == 'x' || c == 'X') {
289
        lu = va_arg(ap, long);
290
        count = countPrintu(lu,
291
                  c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10));
292
        if (width > 0 && !negFlag) {
293
          fill(width - count, filler);
294
        }
295
        printu(lu,
296
               c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10),
297
               c == 'X');
298
        if (width > 0 && negFlag) {
299
          fill(width - count, filler);
300
        }
301
      } else {
302
        putchar('l');
303
        putchar(c);
304
      }
305
    } else
306
    if (c == 's') {
307
      s = va_arg(ap, char *);
308
      count = strlen(s);
309
      if (width > 0 && !negFlag) {
310
        fill(width - count, filler);
311
      }
312
      while ((c = *s++) != '\0') {
313
        putchar(c);
314
      }
315
      if (width > 0 && negFlag) {
316
        fill(width - count, filler);
317
      }
318
    } else
319
    if (c == 'c') {
320
      c = va_arg(ap, char);
321
      putchar(c);
322
    } else {
323
      putchar(c);
324
    }
325
  }
326
}
327
 
328
 
329
/*
330
 * Formatted output.
331
 * This is a scaled-down version of the C library's
332
 * printf. Used to print diagnostic information on
333
 * the console (and optionally to a logfile).
334
 */
335
void printf(char *fmt, ...) {
336
  va_list ap;
337
 
338
  va_start(ap, fmt);
339
  vprintf(fmt, ap);
340
  va_end(ap);
341
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.