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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [libc.c] - Blame information for rev 144

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
char *strstr(char *string, char *find)
93
{
94
   int i;
95
   for(;;)
96
   {
97
      for(i = 0; string[i] == find[i] && find[i]; ++i) ;
98
      if(find[i] == 0)
99
         return string;
100
      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 144 rhoads
unsigned 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
void itoa(char *dst, int num, int base, int width)
239
{
240
   int digit, negate=0, digits, widthSave;
241
   char c;
242
 
243
   if(width == 0)
244
      width = 16;
245
   widthSave = width;
246
   digits = width - 1;
247
   if(base == 10 && num < 0)
248
   {
249
      num = -num;
250
      negate = 1;
251
   }
252
   dst[width] = 0;
253
   while(width-- > 0)
254
   {
255
      if(base == 10)
256
         digit = num % base;
257
      else
258
         digit = (unsigned int)num % (unsigned int)base;
259
      if(base == 10 && num == 0 && dst[width+1] != 0)
260
      {
261
         if(negate)
262
            c = '-';
263
         else
264
            c = ' ';
265
         negate = 0;
266
      }
267
      else if(digit < 10)
268
         c = (char)('0' + digit);
269
      else
270
         c = (char)('a' + digit - 10);
271
      dst[width] = c;
272
      if(base == 10)
273
         num /= base;
274
      else
275
         num = (unsigned int)num / (unsigned int)base;
276
      if(c != ' ' && c != '0')
277
         digits = width;
278
   }
279
   if(widthSave > 15)
280
      strcpy(dst, dst + digits);
281
}
282
 
283
 
284
int sprintf(char *s, const char *format,
285
            int arg0, int arg1, int arg2, int arg3,
286
            int arg4, int arg5, int arg6, int arg7)
287
{
288
   int argv[8];
289
   int argc=0, width, length;
290
   char f;
291
 
292
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
293
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
294
 
295
   for(;;)
296
   {
297
      f = *format++;
298
      if(f == 0)
299
         return argc;
300
      else if(f == '%')
301
      {
302
         width = 0;
303
         f = *format++;
304
         if(f == 0)
305
            return argc;
306
         if('0' <= f && f <= '9')
307
         {
308
            width = f - '0';
309
            f = *format++;
310
            if(f == 0)
311
               return argc;
312
            if('0' <= f && f <= '9')
313
               width = width * 10 + f - '0';
314
         }
315
 
316
         if(f == 'd')
317
            itoa(s, argv[argc++], 10, width);
318
         else if(f == 'x' || f == 'f')
319
            itoa(s, argv[argc++], 16, width);
320
         else if(f == 'c')
321
         {
322
            *s++ = (char)argv[argc++];
323
            *s = 0;
324
         }
325
         else if(f == 's')
326
         {
327
            length = strlen((char*)argv[argc]);
328
            if(width > length)
329
            {
330
               memset(s, ' ', width - length);
331
               s += width - length;
332
            }
333
            strcpy(s, (char*)argv[argc++]);
334
         }
335
         s += strlen(s);
336
      }
337
      else if(f == '\\')
338
      {
339
         f = *format++;
340
         if(f == 0)
341
            return argc;
342
         else if(f == 'n')
343
            *s++ = '\n';
344
         else if(f == 'r')
345
            *s++ = '\r';
346
         else if(f == 't')
347
            *s++ = '\t';
348
      }
349
      else
350
      {
351
         *s++ = f;
352
      }
353
      *s = 0;
354
   }
355
}
356
 
357
 
358
int sscanf(const char *s, const char *format,
359
           int arg0, int arg1, int arg2, int arg3,
360
           int arg4, int arg5, int arg6, int arg7)
361
{
362
   int argv[8];
363
   int argc=0, length;
364
   char f;
365
 
366
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
367
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
368
 
369
   for(;;)
370
   {
371
      if(*s == 0)
372
         return argc;
373
      f = *format++;
374
      if(f == 0)
375
         return argc;
376
      else if(f == '%')
377
      {
378
         while(isspace(*s))
379
            ++s;
380
         f = *format++;
381
         if(f == 0)
382
            return argc;
383
         if(f == 'd')
384
            *(int*)argv[argc++] = strtol(s, &s, 10);
385
         else if(f == 'x')
386
            *(int*)argv[argc++] = strtol(s, &s, 16);
387
         else if(f == 'c')
388
            *(char*)argv[argc++] = *s++;
389
         else if(f == 's')
390
         {
391
            length = 0;
392
            while(!isspace(s[length]))
393
               ++length;
394
            strncpy((char*)argv[argc++], s, length);
395
            s += length;
396
         }
397
      }
398
      else
399
      {
400
         if(f == '\\')
401
         {
402
            f = *format++;
403
            if(f == 0)
404
               return argc;
405
            else if(f == 'n')
406
               f = '\n';
407
            else if(f == 'r')
408
               f = '\r';
409
            else if(f == 't')
410
               f = '\t';
411
         }
412
         while(*s && *s != f)
413
            ++s;
414
         if(*s)
415
            ++s;
416
      }
417
   }
418
   return argc;
419
}
420
 

powered by: WebSVN 2.1.0

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