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

Subversion Repositories plasma

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

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 "rtos.h"
14
 
15
 
16
char *strcpy(char *dst, const char *src)
17
{
18 258 rhoads
   char *dstSave=dst;
19 138 rhoads
   int c;
20
   do
21
   {
22
      c = *dst++ = *src++;
23
   } while(c);
24 258 rhoads
   return dstSave;
25 138 rhoads
}
26
 
27
 
28
char *strncpy(char *dst, const char *src, int count)
29
{
30
   int c=1;
31 258 rhoads
   char *dstSave=dst;
32 138 rhoads
   while(count-- > 0 && c)
33
      c = *dst++ = *src++;
34
   *dst = 0;
35 258 rhoads
   return dstSave;
36 138 rhoads
}
37
 
38
 
39
char *strcat(char *dst, const char *src)
40
{
41
   int c;
42 258 rhoads
   char *dstSave=dst;
43 138 rhoads
   while(*dst)
44
      ++dst;
45
   do
46
   {
47
      c = *dst++ = *src++;
48
   } while(c);
49 258 rhoads
   return dstSave;
50 138 rhoads
}
51
 
52
 
53
char *strncat(char *dst, const char *src, int count)
54
{
55
   int c=1;
56 258 rhoads
   char *dstSave=dst;
57 138 rhoads
   while(*dst && --count > 0)
58
      ++dst;
59
   while(--count > 0 && c)
60
      c = *dst++ = *src++;
61
   *dst = 0;
62 258 rhoads
   return dstSave;
63 138 rhoads
}
64
 
65
 
66
int strcmp(const char *string1, const char *string2)
67
{
68
   int diff, c;
69
   for(;;)
70
   {
71
      diff = *string1++ - (c = *string2++);
72
      if(diff)
73
         return diff;
74
      if(c == 0)
75
         return 0;
76
   }
77
}
78
 
79
 
80
int strncmp(const char *string1, const char *string2, int count)
81
{
82
   int diff, c;
83
   while(count-- > 0)
84
   {
85
      diff = *string1++ - (c = *string2++);
86
      if(diff)
87
         return diff;
88
      if(c == 0)
89
         return 0;
90
   }
91
   return 0;
92
}
93
 
94
 
95 149 rhoads
char *strstr(const char *string, const char *find)
96 138 rhoads
{
97
   int i;
98
   for(;;)
99
   {
100
      for(i = 0; string[i] == find[i] && find[i]; ++i) ;
101
      if(find[i] == 0)
102 149 rhoads
         return (char*)string;
103 138 rhoads
      if(*string++ == 0)
104
         return NULL;
105
   }
106
}
107
 
108
 
109
int strlen(const char *string)
110
{
111
   const char *base=string;
112
   while(*string++) ;
113
   return string - base - 1;
114
}
115
 
116
 
117
void *memcpy(void *dst, const void *src, unsigned long bytes)
118
{
119 249 rhoads
   if(((uint32)dst | (uint32)src | bytes) & 3)
120
   {
121
      uint8 *Dst = (uint8*)dst, *Src = (uint8*)src;
122
      while((int)bytes-- > 0)
123
         *Dst++ = *Src++;
124
   }
125
   else
126
   {
127
      uint32 *Dst32 = (uint32*)dst, *Src32 = (uint32*)src;
128
      bytes >>= 2;
129
      while((int)bytes-- > 0)
130
         *Dst32++ = *Src32++;
131
   }
132 138 rhoads
   return dst;
133
}
134
 
135
 
136 144 rhoads
void *memmove(void *dst, const void *src, unsigned long bytes)
137
{
138
   uint8 *Dst = (uint8*)dst;
139
   uint8 *Src = (uint8*)src;
140
   if(Dst < Src)
141
   {
142
      while((int)bytes-- > 0)
143
         *Dst++ = *Src++;
144
   }
145
   else
146
   {
147
      Dst += bytes;
148
      Src += bytes;
149
      while((int)bytes-- > 0)
150
         *--Dst = *--Src;
151
   }
152
   return dst;
153
}
154
 
155
 
156 138 rhoads
int memcmp(const void *cs, const void *ct, unsigned long bytes)
157
{
158
   uint8 *Dst = (uint8*)cs;
159
   uint8 *Src = (uint8*)ct;
160
   int diff;
161
   while((int)bytes-- > 0)
162
   {
163
      diff = *Dst++ - *Src++;
164
      if(diff)
165
         return diff;
166
   }
167
   return 0;
168
}
169
 
170
 
171
void *memset(void *dst, int c, unsigned long bytes)
172
{
173
   uint8 *Dst = (uint8*)dst;
174
   while((int)bytes-- > 0)
175
      *Dst++ = (uint8)c;
176
   return dst;
177
}
178
 
179
 
180
int abs(int n)
181
{
182
   return n>=0 ? n : -n;
183
}
184
 
185
 
186
static uint32 Rand1=0x1f2bcda3, Rand2=0xdeafbeef, Rand3=0xc5134306;
187 149 rhoads
int rand(void)
188 138 rhoads
{
189
   int shift;
190
   Rand1 += 0x13423123 + Rand2;
191
   Rand2 += 0x2312fdea + Rand3;
192
   Rand3 += 0xf2a12de1;
193
   shift = Rand3 & 31;
194
   Rand1 = (Rand1 << (32 - shift)) | (Rand1 >> shift);
195
   Rand3 ^= Rand1;
196
   shift = (Rand3 >> 8) & 31;
197
   Rand2 = (Rand2 << (32 - shift)) | (Rand2 >> shift);
198
   return Rand1;
199
}
200
 
201
 
202
void srand(unsigned int seed)
203
{
204
   Rand1 = seed;
205
}
206
 
207
 
208 302 rhoads
long strtol(const char *s, char **end, int base)
209 138 rhoads
{
210
   int i;
211
   unsigned long ch, value=0, neg=0;
212
 
213
   if(s[0] == '-')
214
   {
215
      neg = 1;
216
      ++s;
217
   }
218
   if(s[0] == '0' && s[1] == 'x')
219
   {
220
      base = 16;
221
      s += 2;
222
   }
223
   for(i = 0; i <= 8; ++i)
224
   {
225
      ch = *s++;
226
      if('0' <= ch && ch <= '9')
227
         ch -= '0';
228
      else if('A' <= ch && ch <= 'Z')
229
         ch = ch - 'A' + 10;
230
      else if('a' <= ch && ch <= 'z')
231
         ch = ch - 'a' + 10;
232
      else
233
         break;
234
      value = value * base + ch;
235
   }
236
   if(end)
237 302 rhoads
      *end = (char*)s - 1;
238 138 rhoads
   if(neg)
239
      value = -(int)value;
240
   return value;
241
}
242
 
243
 
244
int atoi(const char *s)
245
{
246
   return strtol(s, NULL, 10);
247
}
248
 
249
 
250 149 rhoads
char *itoa(int num, char *dst, int base)
251 138 rhoads
{
252 149 rhoads
   int digit, negate=0, place;
253
   char c, text[20];
254 138 rhoads
 
255
   if(base == 10 && num < 0)
256
   {
257
      num = -num;
258
      negate = 1;
259
   }
260 149 rhoads
   text[16] = 0;
261
   for(place = 15; place >= 0; --place)
262 138 rhoads
   {
263 187 rhoads
      digit = (unsigned int)num % (unsigned int)base;
264 149 rhoads
      if(num == 0 && place < 15 && base == 10 && negate)
265 138 rhoads
      {
266 149 rhoads
         c = '-';
267 138 rhoads
         negate = 0;
268
      }
269
      else if(digit < 10)
270
         c = (char)('0' + digit);
271
      else
272
         c = (char)('a' + digit - 10);
273 149 rhoads
      text[place] = c;
274
      num = (unsigned int)num / (unsigned int)base;
275
      if(num == 0 && negate == 0)
276
         break;
277 138 rhoads
   }
278 149 rhoads
   strcpy(dst, text + place);
279
   return dst;
280 138 rhoads
}
281
 
282
 
283
int sprintf(char *s, const char *format,
284
            int arg0, int arg1, int arg2, int arg3,
285
            int arg4, int arg5, int arg6, int arg7)
286
{
287
   int argv[8];
288
   int argc=0, width, length;
289 187 rhoads
   char f, text[20], fill;
290 138 rhoads
 
291
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
292
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
293
 
294
   for(;;)
295
   {
296
      f = *format++;
297
      if(f == 0)
298
         return argc;
299
      else if(f == '%')
300
      {
301
         width = 0;
302 187 rhoads
         fill = ' ';
303 138 rhoads
         f = *format++;
304 187 rhoads
         while('0' <= f && f <= '9')
305 138 rhoads
         {
306 187 rhoads
            width = width * 10 + f - '0';
307 138 rhoads
            f = *format++;
308
         }
309 187 rhoads
         if(f == '.')
310
         {
311
            fill = '0';
312
            f = *format++;
313
         }
314
         if(f == 0)
315
            return argc;
316 138 rhoads
 
317
         if(f == 'd')
318 149 rhoads
         {
319 187 rhoads
            memset(s, fill, width);
320 149 rhoads
            itoa(argv[argc++], text, 10);
321
            length = (int)strlen(text);
322
            if(width < length)
323
               width = length;
324
            strcpy(s + width - length, text);
325
         }
326 138 rhoads
         else if(f == 'x' || f == 'f')
327 149 rhoads
         {
328
            memset(s, '0', width);
329
            itoa(argv[argc++], text, 16);
330
            length = (int)strlen(text);
331
            if(width < length)
332
               width = length;
333
            strcpy(s + width - length, text);
334
         }
335 138 rhoads
         else if(f == 'c')
336
         {
337
            *s++ = (char)argv[argc++];
338
            *s = 0;
339
         }
340
         else if(f == 's')
341
         {
342
            length = strlen((char*)argv[argc]);
343
            if(width > length)
344
            {
345
               memset(s, ' ', width - length);
346
               s += width - length;
347
            }
348
            strcpy(s, (char*)argv[argc++]);
349
         }
350
         s += strlen(s);
351
      }
352 328 rhoads
      else
353 138 rhoads
      {
354 328 rhoads
         if(f == '\n')
355 138 rhoads
            *s++ = '\r';
356
         *s++ = f;
357 328 rhoads
         if(f == '\r' && *format == '\n')
358
            *s++ = *format++;
359 138 rhoads
      }
360
      *s = 0;
361
   }
362
}
363
 
364
 
365
int sscanf(const char *s, const char *format,
366
           int arg0, int arg1, int arg2, int arg3,
367
           int arg4, int arg5, int arg6, int arg7)
368
{
369
   int argv[8];
370 187 rhoads
   int argc=0;
371
   char f, *ptr;
372 138 rhoads
 
373
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
374
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
375
 
376
   for(;;)
377
   {
378
      if(*s == 0)
379
         return argc;
380
      f = *format++;
381
      if(f == 0)
382
         return argc;
383
      else if(f == '%')
384
      {
385
         while(isspace(*s))
386
            ++s;
387
         f = *format++;
388
         if(f == 0)
389
            return argc;
390
         if(f == 'd')
391 302 rhoads
            *(int*)argv[argc++] = strtol(s, (char**)&s, 10);
392 138 rhoads
         else if(f == 'x')
393 302 rhoads
            *(int*)argv[argc++] = strtol(s, (char**)&s, 16);
394 138 rhoads
         else if(f == 'c')
395
            *(char*)argv[argc++] = *s++;
396
         else if(f == 's')
397
         {
398 207 rhoads
            ptr = (char*)argv[argc++];
399 187 rhoads
            while(!isspace(*s))
400
               *ptr++ = *s++;
401
            *ptr = 0;
402 138 rhoads
         }
403
      }
404
      else
405
      {
406
         while(*s && *s != f)
407
            ++s;
408
         if(*s)
409
            ++s;
410
      }
411
   }
412
}
413
 
414 149 rhoads
 
415 171 rhoads
#ifdef INCLUDE_DUMP
416
/*********************** dump ***********************/
417 149 rhoads
void dump(const unsigned char *data, int length)
418
{
419
   int i, index=0, value;
420
   char string[80];
421
   memset(string, 0, sizeof(string));
422
   for(i = 0; i < length; ++i)
423
   {
424
      if((i & 15) == 0)
425
      {
426
         if(strlen(string))
427
            printf("%s\n", string);
428
         printf("%4x ", i);
429
         memset(string, 0, sizeof(string));
430
         index = 0;
431
      }
432
      value = data[i];
433
      printf("%2x ", value);
434
      if(isprint(value))
435
         string[index] = (char)value;
436
      else
437
         string[index] = '.';
438
      ++index;
439
   }
440
   for(; index < 16; ++index)
441
      printf("   ");
442
   printf("%s\n", string);
443
}
444 171 rhoads
#endif //INCLUDE_DUMP
445 149 rhoads
 
446
 
447 171 rhoads
#ifdef INCLUDE_QSORT
448
/*********************** qsort ***********************/
449
static void QsortSwap(char *base, long left, long right, long size)
450
{
451 249 rhoads
   int temp, i;
452
   char *ptrLeft, *ptrRight;
453
   ptrLeft = base + left * size;
454
   ptrRight = base + right * size;
455
   for(i = 0; i < size; ++i)
456
   {
457
      temp = ptrLeft[i];
458
      ptrLeft[i] = ptrRight[i];
459
      ptrRight[i] = (char)temp;
460
   }
461 171 rhoads
}
462 149 rhoads
 
463 171 rhoads
 
464
//Modified from K&R
465
static void qsort2(void *base, long left, long right, long size,
466
      int (*cmp)(const void *,const void *))
467
{
468
   int i, last;
469 187 rhoads
   char *base2=(char*)base, *pivot;
470 171 rhoads
   if(left >= right)
471
      return;
472
   QsortSwap(base2, left, (left + right)/2, size);
473
   last = left;
474 187 rhoads
   pivot = &base2[left*size];
475 171 rhoads
   for(i = left + 1; i <= right; ++i)
476
   {
477 187 rhoads
      if(cmp(&base2[i*size], pivot) < 0)
478 171 rhoads
         QsortSwap(base2, ++last, i, size);
479
   }
480
   QsortSwap(base2, left, last, size);
481
   qsort2(base, left, last-1, size, cmp);
482
   qsort2(base, last+1, right, size, cmp);
483
}
484
 
485
 
486
void qsort(void *base,
487
           long n,
488
           long size,
489
           int (*cmp)(const void *,const void *))
490
{
491
   qsort2(base, 0, n-1, size, cmp);
492
}
493
 
494
 
495
void *bsearch(const void *key,
496
              const void *base,
497
              long n,
498
              long size,
499
              int (*cmp)(const void *,const void *))
500
{
501
   long cond, low=0, high=n-1, mid;
502
   char *base2=(char*)base;
503
   while(low <= high)
504
   {
505
      mid = (low + high)/2;
506
      cond = cmp(key, &base2[mid*size]);
507
      if(cond < 0)
508
         high = mid - 1;
509
      else if(cond > 0)
510
         low = mid + 1;
511
      else
512
         return &base2[mid * size];
513
   }
514 187 rhoads
   return NULL;
515 171 rhoads
}
516
#endif //INCLUDE_QSORT
517
 
518
 
519
#ifdef INCLUDE_TIMELIB
520
/************************* time.h ***********************/
521
#define SEC_PER_YEAR (365L*24*60*60)
522
#define SEC_PER_DAY (24L*60*60)
523
//typedef unsigned long time_t;  //start at 1/1/80
524
//struct tm {
525
//   int tm_sec;      //(0,59)
526
//   int tm_min;      //(0,59)
527
//   int tm_hour;     //(0,23)
528
//   int tm_mday;     //(1,31)
529
//   int tm_mon;      //(0,11)
530 187 rhoads
//   int tm_year;     //(0,n) from 1900
531 171 rhoads
//   int tm_wday;     //(0,6)     calculated
532
//   int tm_yday;     //(0,365)   calculated
533 199 rhoads
//   int tm_isdst;    //hour adjusted for day light savings
534 171 rhoads
//};
535
static const unsigned short DaysUntilMonth[]=
536
   {0,31,59,90,120,151,181,212,243,273,304,334,365};
537
static const unsigned short DaysInMonth[]=
538
   {31,28,31,30,31,30,31,31,30,31,30,31};
539 187 rhoads
static time_t DstTimeIn, DstTimeOut;
540 171 rhoads
 
541 199 rhoads
 
542
/* Leap year if divisible by 4.  Centenary years should only be
543
   leap-years if they were divisible by 400. */
544 171 rhoads
static int IsLeapYear(int year)
545
{
546
   return(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
547
}
548
 
549
time_t mktime(struct tm *tp)
550
{
551
   time_t seconds;
552
   unsigned long days, y, year;
553
 
554
   days = tp->tm_mday - 1 + DaysUntilMonth[tp->tm_mon] +
555
      365 * (tp->tm_year - 80);
556
   seconds = (unsigned long)tp->tm_sec + 60L * (tp->tm_min +
557
      60L * (tp->tm_hour + 24L * days));
558 199 rhoads
   if(tp->tm_isdst)
559
      seconds -= 60 * 60;
560 171 rhoads
   year = 1900 + tp->tm_year - (tp->tm_mon < 2);
561
   for(y = 1980; y <= year; y += 4)
562
   {
563
      if(y % 100 != 0 || y % 400 == 0)
564
         seconds += SEC_PER_DAY;
565
   }
566
   return seconds;
567
}
568
 
569 187 rhoads
 
570 171 rhoads
void gmtime_r(const time_t *tp, struct tm *out)
571
{
572 187 rhoads
   time_t seconds, delta, secondsIn=*tp;
573
   int isLeapYear;
574 171 rhoads
   unsigned long year, month;
575
 
576 187 rhoads
   out->tm_isdst = 0;
577
   if(DstTimeIn <= secondsIn && secondsIn < DstTimeOut)
578
   {
579 199 rhoads
      secondsIn += 60 * 60;
580 187 rhoads
      out->tm_isdst = 1;
581
   }
582
   seconds = secondsIn;
583 171 rhoads
   for(year = 0; ; ++year)
584
   {
585
      delta = SEC_PER_YEAR + IsLeapYear(1980 + year) * SEC_PER_DAY;
586
      if(seconds >= delta)
587
         seconds -= delta;
588
      else
589
         break;
590
   }
591 187 rhoads
   out->tm_year = year + 80;
592 171 rhoads
   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 187 rhoads
   out->tm_yday = DaysUntilMonth[month] + out->tm_mday;
604 171 rhoads
   seconds -= out->tm_mday * SEC_PER_DAY;
605 187 rhoads
   ++out->tm_mday;
606 171 rhoads
   out->tm_hour = seconds / (60 * 60);
607
   seconds -= out->tm_hour * (60 * 60);
608
   out->tm_min = seconds / 60;
609
   seconds -= out->tm_min * 60;
610
   out->tm_sec = seconds;
611 187 rhoads
   seconds = secondsIn % (SEC_PER_DAY * 7);
612
   out->tm_wday = (seconds / SEC_PER_DAY + 2) % 7; /* 1/1/80 is a Tue */
613
   //printf("%4.d/%2.d/%2.d:%2.d:%2.d:%2.d\n", 
614
   //         out->tm_year+1900, out->tm_mon+1, out->tm_mday,
615
   //         out->tm_hour, out->tm_min, out->tm_sec);
616
}
617 171 rhoads
 
618 187 rhoads
 
619
void gmtimeDst(time_t dstTimeIn, time_t dstTimeOut)
620
{
621
   DstTimeIn = dstTimeIn;
622
   DstTimeOut = dstTimeOut;
623 171 rhoads
}
624 199 rhoads
 
625
 
626
//DST from 2am on the second Sunday in March to 2am first Sunday in November
627
void gmtimeDstSet(time_t *tp, time_t *dstTimeIn, time_t *dstTimeOut)
628
{
629
   time_t seconds, timeIn, timeOut;
630
   struct tm tmDate;
631
   int year, days;
632
 
633
   DstTimeIn = 0;
634
   DstTimeOut = 0;
635
   gmtime_r(tp, &tmDate);
636
   year = tmDate.tm_year;
637
 
638
   //March 1, year, 2AM -> second Sunday
639
   tmDate.tm_year = year;
640
   tmDate.tm_mon = 2;
641
   tmDate.tm_mday = 1;
642
   tmDate.tm_hour = 2;
643
   tmDate.tm_min = 0;
644
   tmDate.tm_sec = 0;
645
   seconds = mktime(&tmDate);
646
   gmtime_r(&seconds, &tmDate);
647
   days = 7 - tmDate.tm_wday + 7;
648
   *dstTimeIn = timeIn = seconds + days * SEC_PER_DAY;
649
 
650
   //November 1, year, 2AM -> first Sunday
651
   tmDate.tm_year = year;
652
   tmDate.tm_mon = 10;
653
   tmDate.tm_mday = 1;
654
   tmDate.tm_hour = 2;
655
   tmDate.tm_min = 0;
656
   tmDate.tm_sec = 0;
657
   seconds = mktime(&tmDate);
658
   gmtime_r(&seconds, &tmDate);
659
   days = 7 - tmDate.tm_wday;
660
   *dstTimeOut = timeOut = seconds + days * SEC_PER_DAY;
661
 
662
   DstTimeIn = timeIn;
663
   DstTimeOut = timeOut;
664
}
665 171 rhoads
#endif //INCLUDE_TIMELIB
666
 

powered by: WebSVN 2.1.0

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