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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [libc.c] - Blame information for rev 149

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 138 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: ANSI C Library
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 12/17/05
5
 * FILENAME: clib.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Subset of the ANSI C library
11
 *--------------------------------------------------------------------*/
12
#define NO_ELLIPSIS
13
#include "plasma.h"
14
#include "rtos.h"
15
 
16
 
17
char *strcpy(char *dst, const char *src)
18
{
19
   int c;
20
   do
21
   {
22
      c = *dst++ = *src++;
23
   } while(c);
24
   return dst;
25
}
26
 
27
 
28
char *strncpy(char *dst, const char *src, int count)
29
{
30
   int c=1;
31
   while(count-- > 0 && c)
32
      c = *dst++ = *src++;
33
   *dst = 0;
34
   return dst;
35
}
36
 
37
 
38
char *strcat(char *dst, const char *src)
39
{
40
   int c;
41
   while(*dst)
42
      ++dst;
43
   do
44
   {
45
      c = *dst++ = *src++;
46
   } while(c);
47
   return dst;
48
}
49
 
50
 
51
char *strncat(char *dst, const char *src, int count)
52
{
53
   int c=1;
54
   while(*dst && --count > 0)
55
      ++dst;
56
   while(--count > 0 && c)
57
      c = *dst++ = *src++;
58
   *dst = 0;
59
   return dst;
60
}
61
 
62
 
63
int strcmp(const char *string1, const char *string2)
64
{
65
   int diff, c;
66
   for(;;)
67
   {
68
      diff = *string1++ - (c = *string2++);
69
      if(diff)
70
         return diff;
71
      if(c == 0)
72
         return 0;
73
   }
74
}
75
 
76
 
77
int strncmp(const char *string1, const char *string2, int count)
78
{
79
   int diff, c;
80
   while(count-- > 0)
81
   {
82
      diff = *string1++ - (c = *string2++);
83
      if(diff)
84
         return diff;
85
      if(c == 0)
86
         return 0;
87
   }
88
   return 0;
89
}
90
 
91
 
92 149 rhoads
char *strstr(const char *string, const char *find)
93 138 rhoads
{
94
   int i;
95
   for(;;)
96
   {
97
      for(i = 0; string[i] == find[i] && find[i]; ++i) ;
98
      if(find[i] == 0)
99 149 rhoads
         return (char*)string;
100 138 rhoads
      if(*string++ == 0)
101
         return NULL;
102
   }
103
}
104
 
105
 
106
int strlen(const char *string)
107
{
108
   const char *base=string;
109
   while(*string++) ;
110
   return string - base - 1;
111
}
112
 
113
 
114
void *memcpy(void *dst, const void *src, unsigned long bytes)
115
{
116
   uint8 *Dst = (uint8*)dst;
117
   uint8 *Src = (uint8*)src;
118
   while((int)bytes-- > 0)
119
      *Dst++ = *Src++;
120
   return dst;
121
}
122
 
123
 
124 144 rhoads
void *memmove(void *dst, const void *src, unsigned long bytes)
125
{
126
   uint8 *Dst = (uint8*)dst;
127
   uint8 *Src = (uint8*)src;
128
   if(Dst < Src)
129
   {
130
      while((int)bytes-- > 0)
131
         *Dst++ = *Src++;
132
   }
133
   else
134
   {
135
      Dst += bytes;
136
      Src += bytes;
137
      while((int)bytes-- > 0)
138
         *--Dst = *--Src;
139
   }
140
   return dst;
141
}
142
 
143
 
144 138 rhoads
int memcmp(const void *cs, const void *ct, unsigned long bytes)
145
{
146
   uint8 *Dst = (uint8*)cs;
147
   uint8 *Src = (uint8*)ct;
148
   int diff;
149
   while((int)bytes-- > 0)
150
   {
151
      diff = *Dst++ - *Src++;
152
      if(diff)
153
         return diff;
154
   }
155
   return 0;
156
}
157
 
158
 
159
void *memset(void *dst, int c, unsigned long bytes)
160
{
161
   uint8 *Dst = (uint8*)dst;
162
   while((int)bytes-- > 0)
163
      *Dst++ = (uint8)c;
164
   return dst;
165
}
166
 
167
 
168
int abs(int n)
169
{
170
   return n>=0 ? n : -n;
171
}
172
 
173
 
174
static uint32 Rand1=0x1f2bcda3, Rand2=0xdeafbeef, Rand3=0xc5134306;
175 149 rhoads
int rand(void)
176 138 rhoads
{
177
   int shift;
178
   Rand1 += 0x13423123 + Rand2;
179
   Rand2 += 0x2312fdea + Rand3;
180
   Rand3 += 0xf2a12de1;
181
   shift = Rand3 & 31;
182
   Rand1 = (Rand1 << (32 - shift)) | (Rand1 >> shift);
183
   Rand3 ^= Rand1;
184
   shift = (Rand3 >> 8) & 31;
185
   Rand2 = (Rand2 << (32 - shift)) | (Rand2 >> shift);
186
   return Rand1;
187
}
188
 
189
 
190
void srand(unsigned int seed)
191
{
192
   Rand1 = seed;
193
}
194
 
195
 
196
long strtol(const char *s, const char **end, int base)
197
{
198
   int i;
199
   unsigned long ch, value=0, neg=0;
200
 
201
   if(s[0] == '-')
202
   {
203
      neg = 1;
204
      ++s;
205
   }
206
   if(s[0] == '0' && s[1] == 'x')
207
   {
208
      base = 16;
209
      s += 2;
210
   }
211
   for(i = 0; i <= 8; ++i)
212
   {
213
      ch = *s++;
214
      if('0' <= ch && ch <= '9')
215
         ch -= '0';
216
      else if('A' <= ch && ch <= 'Z')
217
         ch = ch - 'A' + 10;
218
      else if('a' <= ch && ch <= 'z')
219
         ch = ch - 'a' + 10;
220
      else
221
         break;
222
      value = value * base + ch;
223
   }
224
   if(end)
225
      *end = s - 1;
226
   if(neg)
227
      value = -(int)value;
228
   return value;
229
}
230
 
231
 
232
int atoi(const char *s)
233
{
234
   return strtol(s, NULL, 10);
235
}
236
 
237
 
238 149 rhoads
char *itoa(int num, char *dst, int base)
239 138 rhoads
{
240 149 rhoads
   int digit, negate=0, place;
241
   char c, text[20];
242 138 rhoads
 
243
   if(base == 10 && num < 0)
244
   {
245
      num = -num;
246
      negate = 1;
247
   }
248 149 rhoads
   text[16] = 0;
249
   for(place = 15; place >= 0; --place)
250 138 rhoads
   {
251
      if(base == 10)
252
         digit = num % base;
253
      else
254
         digit = (unsigned int)num % (unsigned int)base;
255 149 rhoads
      if(num == 0 && place < 15 && base == 10 && negate)
256 138 rhoads
      {
257 149 rhoads
         c = '-';
258 138 rhoads
         negate = 0;
259
      }
260
      else if(digit < 10)
261
         c = (char)('0' + digit);
262
      else
263
         c = (char)('a' + digit - 10);
264 149 rhoads
      text[place] = c;
265
      num = (unsigned int)num / (unsigned int)base;
266
      if(num == 0 && negate == 0)
267
         break;
268 138 rhoads
   }
269 149 rhoads
   strcpy(dst, text + place);
270
   return dst;
271 138 rhoads
}
272
 
273
 
274
int sprintf(char *s, const char *format,
275
            int arg0, int arg1, int arg2, int arg3,
276
            int arg4, int arg5, int arg6, int arg7)
277
{
278
   int argv[8];
279
   int argc=0, width, length;
280 149 rhoads
   char f, text[20];
281 138 rhoads
 
282
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
283
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
284
 
285
   for(;;)
286
   {
287
      f = *format++;
288
      if(f == 0)
289
         return argc;
290
      else if(f == '%')
291
      {
292
         width = 0;
293
         f = *format++;
294
         if(f == 0)
295
            return argc;
296
         if('0' <= f && f <= '9')
297
         {
298
            width = f - '0';
299
            f = *format++;
300
            if(f == 0)
301
               return argc;
302
            if('0' <= f && f <= '9')
303
               width = width * 10 + f - '0';
304
         }
305
 
306
         if(f == 'd')
307 149 rhoads
         {
308
            memset(s, ' ', width);
309
            itoa(argv[argc++], text, 10);
310
            length = (int)strlen(text);
311
            if(width < length)
312
               width = length;
313
            strcpy(s + width - length, text);
314
         }
315 138 rhoads
         else if(f == 'x' || f == 'f')
316 149 rhoads
         {
317
            memset(s, '0', width);
318
            itoa(argv[argc++], text, 16);
319
            length = (int)strlen(text);
320
            if(width < length)
321
               width = length;
322
            strcpy(s + width - length, text);
323
         }
324 138 rhoads
         else if(f == 'c')
325
         {
326
            *s++ = (char)argv[argc++];
327
            *s = 0;
328
         }
329
         else if(f == 's')
330
         {
331
            length = strlen((char*)argv[argc]);
332
            if(width > length)
333
            {
334
               memset(s, ' ', width - length);
335
               s += width - length;
336
            }
337
            strcpy(s, (char*)argv[argc++]);
338
         }
339
         s += strlen(s);
340
      }
341
      else if(f == '\\')
342
      {
343
         f = *format++;
344
         if(f == 0)
345
            return argc;
346
         else if(f == 'n')
347
            *s++ = '\n';
348
         else if(f == 'r')
349
            *s++ = '\r';
350
         else if(f == 't')
351
            *s++ = '\t';
352
      }
353
      else
354
      {
355
         *s++ = f;
356
      }
357
      *s = 0;
358
   }
359
}
360
 
361
 
362
int sscanf(const char *s, const char *format,
363
           int arg0, int arg1, int arg2, int arg3,
364
           int arg4, int arg5, int arg6, int arg7)
365
{
366
   int argv[8];
367
   int argc=0, length;
368
   char f;
369
 
370
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
371
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
372
 
373
   for(;;)
374
   {
375
      if(*s == 0)
376
         return argc;
377
      f = *format++;
378
      if(f == 0)
379
         return argc;
380
      else if(f == '%')
381
      {
382
         while(isspace(*s))
383
            ++s;
384
         f = *format++;
385
         if(f == 0)
386
            return argc;
387
         if(f == 'd')
388
            *(int*)argv[argc++] = strtol(s, &s, 10);
389
         else if(f == 'x')
390
            *(int*)argv[argc++] = strtol(s, &s, 16);
391
         else if(f == 'c')
392
            *(char*)argv[argc++] = *s++;
393
         else if(f == 's')
394
         {
395
            length = 0;
396
            while(!isspace(s[length]))
397
               ++length;
398
            strncpy((char*)argv[argc++], s, length);
399
            s += length;
400
         }
401
      }
402
      else
403
      {
404
         if(f == '\\')
405
         {
406
            f = *format++;
407
            if(f == 0)
408
               return argc;
409
            else if(f == 'n')
410
               f = '\n';
411
            else if(f == 'r')
412
               f = '\r';
413
            else if(f == 't')
414
               f = '\t';
415
         }
416
         while(*s && *s != f)
417
            ++s;
418
         if(*s)
419
            ++s;
420
      }
421
   }
422
}
423
 
424 149 rhoads
 
425
void dump(const unsigned char *data, int length)
426
{
427
   int i, index=0, value;
428
   char string[80];
429
   memset(string, 0, sizeof(string));
430
   for(i = 0; i < length; ++i)
431
   {
432
      if((i & 15) == 0)
433
      {
434
         if(strlen(string))
435
            printf("%s\n", string);
436
         printf("%4x ", i);
437
         memset(string, 0, sizeof(string));
438
         index = 0;
439
      }
440
      value = data[i];
441
      printf("%2x ", value);
442
      if(isprint(value))
443
         string[index] = (char)value;
444
      else
445
         string[index] = '.';
446
      ++index;
447
   }
448
   for(; index < 16; ++index)
449
      printf("   ");
450
   printf("%s\n", string);
451
}
452
 
453
 
454
 

powered by: WebSVN 2.1.0

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