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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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