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

Subversion Repositories plasma

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

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 171 rhoads
 * FILENAME: libc.c
6 138 rhoads
 * 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 171 rhoads
#ifdef INCLUDE_DUMP
426
/*********************** dump ***********************/
427 149 rhoads
void dump(const unsigned char *data, int length)
428
{
429
   int i, index=0, value;
430
   char string[80];
431
   memset(string, 0, sizeof(string));
432
   for(i = 0; i < length; ++i)
433
   {
434
      if((i & 15) == 0)
435
      {
436
         if(strlen(string))
437
            printf("%s\n", string);
438
         printf("%4x ", i);
439
         memset(string, 0, sizeof(string));
440
         index = 0;
441
      }
442
      value = data[i];
443
      printf("%2x ", value);
444
      if(isprint(value))
445
         string[index] = (char)value;
446
      else
447
         string[index] = '.';
448
      ++index;
449
   }
450
   for(; index < 16; ++index)
451
      printf("   ");
452
   printf("%s\n", string);
453
}
454 171 rhoads
#endif //INCLUDE_DUMP
455 149 rhoads
 
456
 
457 171 rhoads
#ifdef INCLUDE_QSORT
458
/*********************** qsort ***********************/
459
static void QsortSwap(char *base, long left, long right, long size)
460
{
461
   char buffer[256];
462
   if(size > sizeof(buffer))
463
   {
464
      printf("qsort_error");
465
      return;
466
   }
467
   memcpy(buffer, &base[left*size], size);
468
   memcpy(&base[left*size], &base[right*size], size);
469
   memcpy(&base[right*size], buffer, size);
470
}
471 149 rhoads
 
472 171 rhoads
 
473
//Modified from K&R
474
static void qsort2(void *base, long left, long right, long size,
475
      int (*cmp)(const void *,const void *))
476
{
477
   int i, last;
478
   char *base2=(char*)base;
479
   if(left >= right)
480
      return;
481
   QsortSwap(base2, left, (left + right)/2, size);
482
   last = left;
483
   for(i = left + 1; i <= right; ++i)
484
   {
485
      if(cmp(&base2[i*size], &base2[left*size]) < 0)
486
         QsortSwap(base2, ++last, i, size);
487
   }
488
   QsortSwap(base2, left, last, size);
489
   qsort2(base, left, last-1, size, cmp);
490
   qsort2(base, last+1, right, size, cmp);
491
}
492
 
493
 
494
void qsort(void *base,
495
           long n,
496
           long size,
497
           int (*cmp)(const void *,const void *))
498
{
499
   qsort2(base, 0, n-1, size, cmp);
500
}
501
 
502
 
503
void *bsearch(const void *key,
504
              const void *base,
505
              long n,
506
              long size,
507
              int (*cmp)(const void *,const void *))
508
{
509
   long cond, low=0, high=n-1, mid;
510
   char *base2=(char*)base;
511
   while(low <= high)
512
   {
513
      mid = (low + high)/2;
514
      cond = cmp(key, &base2[mid*size]);
515
      if(cond < 0)
516
         high = mid - 1;
517
      else if(cond > 0)
518
         low = mid + 1;
519
      else
520
         return &base2[mid * size];
521
   }
522
   return(NULL);
523
}
524
#endif //INCLUDE_QSORT
525
 
526
 
527
#ifdef INCLUDE_TIMELIB
528
/************************* time.h ***********************/
529
/* Day light savings first Sunday in April and last Sunday in October
530
   is_dst means hour has been compensated for day light savings
531
   leap year if year divisible by 4.  Centenary years should only be
532
   leap-years if they were divisible by 400. */
533
#define SEC_PER_YEAR (365L*24*60*60)
534
#define SEC_PER_DAY (24L*60*60)
535
//typedef unsigned long time_t;  //start at 1/1/80
536
//struct tm {
537
//   int tm_sec;      //(0,59)
538
//   int tm_min;      //(0,59)
539
//   int tm_hour;     //(0,23)
540
//   int tm_mday;     //(1,31)
541
//   int tm_mon;      //(0,11)
542
//   int tm_year;     //(0,n) from 1990
543
//   int tm_wday;     //(0,6)     calculated
544
//   int tm_yday;     //(0,365)   calculated
545
//   int tm_isdst;    //          calculated
546
//};
547
static const unsigned short DaysUntilMonth[]=
548
   {0,31,59,90,120,151,181,212,243,273,304,334,365};
549
static const unsigned short DaysInMonth[]=
550
   {31,28,31,30,31,30,31,31,30,31,30,31};
551
 
552
static int IsLeapYear(int year)
553
{
554
   return(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
555
}
556
 
557
time_t mktime(struct tm *tp)
558
{
559
   time_t seconds;
560
   unsigned long days, y, year;
561
 
562
   days = tp->tm_mday - 1 + DaysUntilMonth[tp->tm_mon] +
563
      365 * (tp->tm_year - 80);
564
   seconds = (unsigned long)tp->tm_sec + 60L * (tp->tm_min +
565
      60L * (tp->tm_hour + 24L * days));
566
   year = 1900 + tp->tm_year - (tp->tm_mon < 2);
567
   for(y = 1980; y <= year; y += 4)
568
   {
569
      if(y % 100 != 0 || y % 400 == 0)
570
         seconds += SEC_PER_DAY;
571
   }
572
   return seconds;
573
}
574
 
575
void gmtime_r(const time_t *tp, struct tm *out)
576
{
577
   time_t seconds, delta;
578
   int wday, isLeapYear;
579
   unsigned long year, month;
580
 
581
   seconds = *tp;
582
   for(year = 0; ; ++year)
583
   {
584
      delta = SEC_PER_YEAR + IsLeapYear(1980 + year) * SEC_PER_DAY;
585
      if(seconds >= delta)
586
         seconds -= delta;
587
      else
588
         break;
589
   }
590
   out->tm_year = year;
591
   out->tm_yday = seconds / SEC_PER_DAY;
592
   isLeapYear = IsLeapYear(1980 + year);
593
   for(month = 0; ; ++month)
594
   {
595
      delta = SEC_PER_DAY * (DaysInMonth[month] + (isLeapYear && (month == 1)));
596
      if(seconds >= delta)
597
         seconds -= delta;
598
      else
599
         break;
600
   }
601
   out->tm_mon = month;
602
   out->tm_mday = seconds / SEC_PER_DAY;
603
   seconds -= out->tm_mday * SEC_PER_DAY;
604
   out->tm_hour = seconds / (60 * 60);
605
   seconds -= out->tm_hour * (60 * 60);
606
   out->tm_min = seconds / 60;
607
   seconds -= out->tm_min * 60;
608
   out->tm_sec = seconds;
609
   seconds = *tp % (SEC_PER_DAY * 7);
610
   out->tm_wday = seconds / SEC_PER_DAY;
611
   out->tm_wday = (out->tm_wday + 2) % 7;   /* 1/1/80 is a Tue */
612
 
613
   /*DST from first Sunday in April to last Sunday in October at 2am*/
614
   out->tm_isdst = 0;
615
   wday = (out->tm_mday % 7) + out->tm_wday;    /* wday of the 1st */
616
   if(out->tm_mon > 3 || (out->tm_mon == 3 && (wday == 0 || out->tm_wday + wday > 6)))
617
      out->tm_isdst = 1;
618
   if(out->tm_mon > 9 || (out->tm_mon == 9 && (out->tm_mday - wday == 21 ||
619
         out->tm_mday + wday == 34)))
620
      out->tm_isdst = 0;
621
   ++out->tm_mday;
622
   out->tm_year += 80;
623
}
624
#endif //INCLUDE_TIMELIB
625
 

powered by: WebSVN 2.1.0

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