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

Subversion Repositories plasma

[/] [plasma/] [tags/] [V3_0/] [kernel/] [libc.c] - Blame information for rev 393

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
int memcmp(const void *cs, const void *ct, unsigned long bytes)
125
{
126
   uint8 *Dst = (uint8*)cs;
127
   uint8 *Src = (uint8*)ct;
128
   int diff;
129
   while((int)bytes-- > 0)
130
   {
131
      diff = *Dst++ - *Src++;
132
      if(diff)
133
         return diff;
134
   }
135
   return 0;
136
}
137
 
138
 
139
void *memset(void *dst, int c, unsigned long bytes)
140
{
141
   uint8 *Dst = (uint8*)dst;
142
   while((int)bytes-- > 0)
143
      *Dst++ = (uint8)c;
144
   return dst;
145
}
146
 
147
 
148
int abs(int n)
149
{
150
   return n>=0 ? n : -n;
151
}
152
 
153
 
154
static uint32 Rand1=0x1f2bcda3, Rand2=0xdeafbeef, Rand3=0xc5134306;
155
int rand(void)
156
{
157
   int shift;
158
   Rand1 += 0x13423123 + Rand2;
159
   Rand2 += 0x2312fdea + Rand3;
160
   Rand3 += 0xf2a12de1;
161
   shift = Rand3 & 31;
162
   Rand1 = (Rand1 << (32 - shift)) | (Rand1 >> shift);
163
   Rand3 ^= Rand1;
164
   shift = (Rand3 >> 8) & 31;
165
   Rand2 = (Rand2 << (32 - shift)) | (Rand2 >> shift);
166
   return Rand1;
167
}
168
 
169
 
170
void srand(unsigned int seed)
171
{
172
   Rand1 = seed;
173
}
174
 
175
 
176
long strtol(const char *s, const char **end, int base)
177
{
178
   int i;
179
   unsigned long ch, value=0, neg=0;
180
 
181
   if(s[0] == '-')
182
   {
183
      neg = 1;
184
      ++s;
185
   }
186
   if(s[0] == '0' && s[1] == 'x')
187
   {
188
      base = 16;
189
      s += 2;
190
   }
191
   for(i = 0; i <= 8; ++i)
192
   {
193
      ch = *s++;
194
      if('0' <= ch && ch <= '9')
195
         ch -= '0';
196
      else if('A' <= ch && ch <= 'Z')
197
         ch = ch - 'A' + 10;
198
      else if('a' <= ch && ch <= 'z')
199
         ch = ch - 'a' + 10;
200
      else
201
         break;
202
      value = value * base + ch;
203
   }
204
   if(end)
205
      *end = s - 1;
206
   if(neg)
207
      value = -(int)value;
208
   return value;
209
}
210
 
211
 
212
int atoi(const char *s)
213
{
214
   return strtol(s, NULL, 10);
215
}
216
 
217
 
218
void itoa(char *dst, int num, int base, int width)
219
{
220
   int digit, negate=0, digits, widthSave;
221
   char c;
222
 
223
   if(width == 0)
224
      width = 16;
225
   widthSave = width;
226
   digits = width - 1;
227
   if(base == 10 && num < 0)
228
   {
229
      num = -num;
230
      negate = 1;
231
   }
232
   dst[width] = 0;
233
   while(width-- > 0)
234
   {
235
      if(base == 10)
236
         digit = num % base;
237
      else
238
         digit = (unsigned int)num % (unsigned int)base;
239
      if(base == 10 && num == 0 && dst[width+1] != 0)
240
      {
241
         if(negate)
242
            c = '-';
243
         else
244
            c = ' ';
245
         negate = 0;
246
      }
247
      else if(digit < 10)
248
         c = (char)('0' + digit);
249
      else
250
         c = (char)('a' + digit - 10);
251
      dst[width] = c;
252
      if(base == 10)
253
         num /= base;
254
      else
255
         num = (unsigned int)num / (unsigned int)base;
256
      if(c != ' ' && c != '0')
257
         digits = width;
258
   }
259
   if(widthSave > 15)
260
      strcpy(dst, dst + digits);
261
}
262
 
263
 
264
int sprintf(char *s, const char *format,
265
            int arg0, int arg1, int arg2, int arg3,
266
            int arg4, int arg5, int arg6, int arg7)
267
{
268
   int argv[8];
269
   int argc=0, width, length;
270
   char f;
271
 
272
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
273
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
274
 
275
   for(;;)
276
   {
277
      f = *format++;
278
      if(f == 0)
279
         return argc;
280
      else if(f == '%')
281
      {
282
         width = 0;
283
         f = *format++;
284
         if(f == 0)
285
            return argc;
286
         if('0' <= f && f <= '9')
287
         {
288
            width = f - '0';
289
            f = *format++;
290
            if(f == 0)
291
               return argc;
292
            if('0' <= f && f <= '9')
293
               width = width * 10 + f - '0';
294
         }
295
 
296
         if(f == 'd')
297
            itoa(s, argv[argc++], 10, width);
298
         else if(f == 'x' || f == 'f')
299
            itoa(s, argv[argc++], 16, width);
300
         else if(f == 'c')
301
         {
302
            *s++ = (char)argv[argc++];
303
            *s = 0;
304
         }
305
         else if(f == 's')
306
         {
307
            length = strlen((char*)argv[argc]);
308
            if(width > length)
309
            {
310
               memset(s, ' ', width - length);
311
               s += width - length;
312
            }
313
            strcpy(s, (char*)argv[argc++]);
314
         }
315
         s += strlen(s);
316
      }
317
      else if(f == '\\')
318
      {
319
         f = *format++;
320
         if(f == 0)
321
            return argc;
322
         else if(f == 'n')
323
            *s++ = '\n';
324
         else if(f == 'r')
325
            *s++ = '\r';
326
         else if(f == 't')
327
            *s++ = '\t';
328
      }
329
      else
330
      {
331
         *s++ = f;
332
      }
333
      *s = 0;
334
   }
335
}
336
 
337
 
338
int sscanf(const char *s, const char *format,
339
           int arg0, int arg1, int arg2, int arg3,
340
           int arg4, int arg5, int arg6, int arg7)
341
{
342
   int argv[8];
343
   int argc=0, length;
344
   char f;
345
 
346
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
347
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
348
 
349
   for(;;)
350
   {
351
      if(*s == 0)
352
         return argc;
353
      f = *format++;
354
      if(f == 0)
355
         return argc;
356
      else if(f == '%')
357
      {
358
         while(isspace(*s))
359
            ++s;
360
         f = *format++;
361
         if(f == 0)
362
            return argc;
363
         if(f == 'd')
364
            *(int*)argv[argc++] = strtol(s, &s, 10);
365
         else if(f == 'x')
366
            *(int*)argv[argc++] = strtol(s, &s, 16);
367
         else if(f == 'c')
368
            *(char*)argv[argc++] = *s++;
369
         else if(f == 's')
370
         {
371
            length = 0;
372
            while(!isspace(s[length]))
373
               ++length;
374
            strncpy((char*)argv[argc++], s, length);
375
            s += length;
376
         }
377
      }
378
      else
379
      {
380
         if(f == '\\')
381
         {
382
            f = *format++;
383
            if(f == 0)
384
               return argc;
385
            else if(f == 'n')
386
               f = '\n';
387
            else if(f == 'r')
388
               f = '\r';
389
            else if(f == 't')
390
               f = '\t';
391
         }
392
         while(*s && *s != f)
393
            ++s;
394
         if(*s)
395
            ++s;
396
      }
397
   }
398
   return argc;
399
}
400
 

powered by: WebSVN 2.1.0

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