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

Subversion Repositories plasma

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

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 399 rhoads
   while(*dst)
58 138 rhoads
      ++dst;
59 399 rhoads
   while(--count >= 0 && c)
60 138 rhoads
      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 399 rhoads
#ifndef _LIBC
187 379 rhoads
static uint32 Rand1=0x1f2bcda3;
188 149 rhoads
int rand(void)
189 138 rhoads
{
190 379 rhoads
   Rand1 = 1664525 * Rand1 + 1013904223;  //from D.E. Knuth and H.W. Lewis
191 138 rhoads
   return Rand1;
192
}
193
 
194
 
195
void srand(unsigned int seed)
196
{
197
   Rand1 = seed;
198
}
199
 
200
 
201 302 rhoads
long strtol(const char *s, char **end, int base)
202 138 rhoads
{
203
   int i;
204
   unsigned long ch, value=0, neg=0;
205
 
206
   if(s[0] == '-')
207
   {
208
      neg = 1;
209
      ++s;
210
   }
211
   if(s[0] == '0' && s[1] == 'x')
212
   {
213
      base = 16;
214
      s += 2;
215
   }
216
   for(i = 0; i <= 8; ++i)
217
   {
218
      ch = *s++;
219
      if('0' <= ch && ch <= '9')
220
         ch -= '0';
221
      else if('A' <= ch && ch <= 'Z')
222
         ch = ch - 'A' + 10;
223
      else if('a' <= ch && ch <= 'z')
224
         ch = ch - 'a' + 10;
225
      else
226
         break;
227
      value = value * base + ch;
228
   }
229
   if(end)
230 302 rhoads
      *end = (char*)s - 1;
231 138 rhoads
   if(neg)
232
      value = -(int)value;
233
   return value;
234
}
235
 
236
 
237
int atoi(const char *s)
238
{
239
   return strtol(s, NULL, 10);
240
}
241
 
242
 
243 149 rhoads
char *itoa(int num, char *dst, int base)
244 138 rhoads
{
245 149 rhoads
   int digit, negate=0, place;
246
   char c, text[20];
247 138 rhoads
 
248
   if(base == 10 && num < 0)
249
   {
250
      num = -num;
251
      negate = 1;
252
   }
253 149 rhoads
   text[16] = 0;
254
   for(place = 15; place >= 0; --place)
255 138 rhoads
   {
256 187 rhoads
      digit = (unsigned int)num % (unsigned int)base;
257 149 rhoads
      if(num == 0 && place < 15 && base == 10 && negate)
258 138 rhoads
      {
259 149 rhoads
         c = '-';
260 138 rhoads
         negate = 0;
261
      }
262
      else if(digit < 10)
263
         c = (char)('0' + digit);
264
      else
265
         c = (char)('a' + digit - 10);
266 149 rhoads
      text[place] = c;
267
      num = (unsigned int)num / (unsigned int)base;
268
      if(num == 0 && negate == 0)
269
         break;
270 138 rhoads
   }
271 149 rhoads
   strcpy(dst, text + place);
272
   return dst;
273 138 rhoads
}
274
 
275
 
276
int sprintf(char *s, const char *format,
277
            int arg0, int arg1, int arg2, int arg3,
278
            int arg4, int arg5, int arg6, int arg7)
279
{
280
   int argv[8];
281
   int argc=0, width, length;
282 187 rhoads
   char f, text[20], fill;
283 138 rhoads
 
284
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
285
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
286
 
287
   for(;;)
288
   {
289
      f = *format++;
290
      if(f == 0)
291
         return argc;
292
      else if(f == '%')
293
      {
294
         width = 0;
295 187 rhoads
         fill = ' ';
296 138 rhoads
         f = *format++;
297 187 rhoads
         while('0' <= f && f <= '9')
298 138 rhoads
         {
299 187 rhoads
            width = width * 10 + f - '0';
300 138 rhoads
            f = *format++;
301
         }
302 187 rhoads
         if(f == '.')
303
         {
304
            fill = '0';
305
            f = *format++;
306
         }
307
         if(f == 0)
308
            return argc;
309 138 rhoads
 
310
         if(f == 'd')
311 149 rhoads
         {
312 187 rhoads
            memset(s, fill, width);
313 149 rhoads
            itoa(argv[argc++], text, 10);
314
            length = (int)strlen(text);
315
            if(width < length)
316
               width = length;
317
            strcpy(s + width - length, text);
318
         }
319 138 rhoads
         else if(f == 'x' || f == 'f')
320 149 rhoads
         {
321
            memset(s, '0', width);
322
            itoa(argv[argc++], text, 16);
323
            length = (int)strlen(text);
324
            if(width < length)
325
               width = length;
326
            strcpy(s + width - length, text);
327
         }
328 138 rhoads
         else if(f == 'c')
329
         {
330
            *s++ = (char)argv[argc++];
331
            *s = 0;
332
         }
333
         else if(f == 's')
334
         {
335
            length = strlen((char*)argv[argc]);
336
            if(width > length)
337
            {
338
               memset(s, ' ', width - length);
339
               s += width - length;
340
            }
341
            strcpy(s, (char*)argv[argc++]);
342
         }
343
         s += strlen(s);
344
      }
345 328 rhoads
      else
346 138 rhoads
      {
347 328 rhoads
         if(f == '\n')
348 138 rhoads
            *s++ = '\r';
349
         *s++ = f;
350 328 rhoads
         if(f == '\r' && *format == '\n')
351
            *s++ = *format++;
352 138 rhoads
      }
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 187 rhoads
   int argc=0;
364
   char f, *ptr;
365 138 rhoads
 
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 302 rhoads
            *(int*)argv[argc++] = strtol(s, (char**)&s, 10);
385 138 rhoads
         else if(f == 'x')
386 302 rhoads
            *(int*)argv[argc++] = strtol(s, (char**)&s, 16);
387 138 rhoads
         else if(f == 'c')
388
            *(char*)argv[argc++] = *s++;
389
         else if(f == 's')
390
         {
391 207 rhoads
            ptr = (char*)argv[argc++];
392 187 rhoads
            while(!isspace(*s))
393
               *ptr++ = *s++;
394
            *ptr = 0;
395 138 rhoads
         }
396
      }
397
      else
398
      {
399
         while(*s && *s != f)
400
            ++s;
401
         if(*s)
402
            ++s;
403
      }
404
   }
405
}
406 399 rhoads
#endif //_LIBC
407 138 rhoads
 
408 149 rhoads
 
409 171 rhoads
#ifdef INCLUDE_DUMP
410
/*********************** dump ***********************/
411 149 rhoads
void dump(const unsigned char *data, int length)
412
{
413
   int i, index=0, value;
414
   char string[80];
415
   memset(string, 0, sizeof(string));
416
   for(i = 0; i < length; ++i)
417
   {
418
      if((i & 15) == 0)
419
      {
420
         if(strlen(string))
421
            printf("%s\n", string);
422
         printf("%4x ", i);
423
         memset(string, 0, sizeof(string));
424
         index = 0;
425
      }
426
      value = data[i];
427
      printf("%2x ", value);
428
      if(isprint(value))
429
         string[index] = (char)value;
430
      else
431
         string[index] = '.';
432
      ++index;
433
   }
434
   for(; index < 16; ++index)
435
      printf("   ");
436
   printf("%s\n", string);
437
}
438 171 rhoads
#endif //INCLUDE_DUMP
439 149 rhoads
 
440
 
441 171 rhoads
#ifdef INCLUDE_QSORT
442
/*********************** qsort ***********************/
443
static void QsortSwap(char *base, long left, long right, long size)
444
{
445 249 rhoads
   int temp, i;
446
   char *ptrLeft, *ptrRight;
447
   ptrLeft = base + left * size;
448
   ptrRight = base + right * size;
449
   for(i = 0; i < size; ++i)
450
   {
451
      temp = ptrLeft[i];
452
      ptrLeft[i] = ptrRight[i];
453
      ptrRight[i] = (char)temp;
454
   }
455 171 rhoads
}
456 149 rhoads
 
457 171 rhoads
 
458
//Modified from K&R
459
static void qsort2(void *base, long left, long right, long size,
460
      int (*cmp)(const void *,const void *))
461
{
462
   int i, last;
463 187 rhoads
   char *base2=(char*)base, *pivot;
464 171 rhoads
   if(left >= right)
465
      return;
466
   QsortSwap(base2, left, (left + right)/2, size);
467
   last = left;
468 187 rhoads
   pivot = &base2[left*size];
469 171 rhoads
   for(i = left + 1; i <= right; ++i)
470
   {
471 187 rhoads
      if(cmp(&base2[i*size], pivot) < 0)
472 171 rhoads
         QsortSwap(base2, ++last, i, size);
473
   }
474
   QsortSwap(base2, left, last, size);
475
   qsort2(base, left, last-1, size, cmp);
476
   qsort2(base, last+1, right, size, cmp);
477
}
478
 
479
 
480
void qsort(void *base,
481
           long n,
482
           long size,
483
           int (*cmp)(const void *,const void *))
484
{
485
   qsort2(base, 0, n-1, size, cmp);
486
}
487
 
488
 
489
void *bsearch(const void *key,
490
              const void *base,
491
              long n,
492
              long size,
493
              int (*cmp)(const void *,const void *))
494
{
495
   long cond, low=0, high=n-1, mid;
496
   char *base2=(char*)base;
497
   while(low <= high)
498
   {
499
      mid = (low + high)/2;
500
      cond = cmp(key, &base2[mid*size]);
501
      if(cond < 0)
502
         high = mid - 1;
503
      else if(cond > 0)
504
         low = mid + 1;
505
      else
506
         return &base2[mid * size];
507
   }
508 187 rhoads
   return NULL;
509 171 rhoads
}
510
#endif //INCLUDE_QSORT
511
 
512
 
513
#ifdef INCLUDE_TIMELIB
514
/************************* time.h ***********************/
515
#define SEC_PER_YEAR (365L*24*60*60)
516
#define SEC_PER_DAY (24L*60*60)
517
//typedef unsigned long time_t;  //start at 1/1/80
518
//struct tm {
519
//   int tm_sec;      //(0,59)
520
//   int tm_min;      //(0,59)
521
//   int tm_hour;     //(0,23)
522
//   int tm_mday;     //(1,31)
523
//   int tm_mon;      //(0,11)
524 187 rhoads
//   int tm_year;     //(0,n) from 1900
525 171 rhoads
//   int tm_wday;     //(0,6)     calculated
526
//   int tm_yday;     //(0,365)   calculated
527 199 rhoads
//   int tm_isdst;    //hour adjusted for day light savings
528 171 rhoads
//};
529
static const unsigned short DaysUntilMonth[]=
530
   {0,31,59,90,120,151,181,212,243,273,304,334,365};
531
static const unsigned short DaysInMonth[]=
532
   {31,28,31,30,31,30,31,31,30,31,30,31};
533 187 rhoads
static time_t DstTimeIn, DstTimeOut;
534 171 rhoads
 
535 199 rhoads
 
536
/* Leap year if divisible by 4.  Centenary years should only be
537
   leap-years if they were divisible by 400. */
538 171 rhoads
static int IsLeapYear(int year)
539
{
540
   return(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
541
}
542
 
543
time_t mktime(struct tm *tp)
544
{
545
   time_t seconds;
546
   unsigned long days, y, year;
547
 
548
   days = tp->tm_mday - 1 + DaysUntilMonth[tp->tm_mon] +
549
      365 * (tp->tm_year - 80);
550
   seconds = (unsigned long)tp->tm_sec + 60L * (tp->tm_min +
551
      60L * (tp->tm_hour + 24L * days));
552 199 rhoads
   if(tp->tm_isdst)
553
      seconds -= 60 * 60;
554 171 rhoads
   year = 1900 + tp->tm_year - (tp->tm_mon < 2);
555
   for(y = 1980; y <= year; y += 4)
556
   {
557
      if(y % 100 != 0 || y % 400 == 0)
558
         seconds += SEC_PER_DAY;
559
   }
560
   return seconds;
561
}
562
 
563 187 rhoads
 
564 171 rhoads
void gmtime_r(const time_t *tp, struct tm *out)
565
{
566 187 rhoads
   time_t seconds, delta, secondsIn=*tp;
567
   int isLeapYear;
568 171 rhoads
   unsigned long year, month;
569
 
570 187 rhoads
   out->tm_isdst = 0;
571
   if(DstTimeIn <= secondsIn && secondsIn < DstTimeOut)
572
   {
573 199 rhoads
      secondsIn += 60 * 60;
574 187 rhoads
      out->tm_isdst = 1;
575
   }
576
   seconds = secondsIn;
577 171 rhoads
   for(year = 0; ; ++year)
578
   {
579
      delta = SEC_PER_YEAR + IsLeapYear(1980 + year) * SEC_PER_DAY;
580
      if(seconds >= delta)
581
         seconds -= delta;
582
      else
583
         break;
584
   }
585 187 rhoads
   out->tm_year = year + 80;
586 171 rhoads
   isLeapYear = IsLeapYear(1980 + year);
587
   for(month = 0; ; ++month)
588
   {
589
      delta = SEC_PER_DAY * (DaysInMonth[month] + (isLeapYear && (month == 1)));
590
      if(seconds >= delta)
591
         seconds -= delta;
592
      else
593
         break;
594
   }
595
   out->tm_mon = month;
596
   out->tm_mday = seconds / SEC_PER_DAY;
597 187 rhoads
   out->tm_yday = DaysUntilMonth[month] + out->tm_mday;
598 171 rhoads
   seconds -= out->tm_mday * SEC_PER_DAY;
599 187 rhoads
   ++out->tm_mday;
600 171 rhoads
   out->tm_hour = seconds / (60 * 60);
601
   seconds -= out->tm_hour * (60 * 60);
602
   out->tm_min = seconds / 60;
603
   seconds -= out->tm_min * 60;
604
   out->tm_sec = seconds;
605 187 rhoads
   seconds = secondsIn % (SEC_PER_DAY * 7);
606
   out->tm_wday = (seconds / SEC_PER_DAY + 2) % 7; /* 1/1/80 is a Tue */
607
   //printf("%4.d/%2.d/%2.d:%2.d:%2.d:%2.d\n", 
608
   //         out->tm_year+1900, out->tm_mon+1, out->tm_mday,
609
   //         out->tm_hour, out->tm_min, out->tm_sec);
610
}
611 171 rhoads
 
612 187 rhoads
 
613
void gmtimeDst(time_t dstTimeIn, time_t dstTimeOut)
614
{
615
   DstTimeIn = dstTimeIn;
616
   DstTimeOut = dstTimeOut;
617 171 rhoads
}
618 199 rhoads
 
619
 
620
//DST from 2am on the second Sunday in March to 2am first Sunday in November
621
void gmtimeDstSet(time_t *tp, time_t *dstTimeIn, time_t *dstTimeOut)
622
{
623
   time_t seconds, timeIn, timeOut;
624
   struct tm tmDate;
625
   int year, days;
626
 
627
   DstTimeIn = 0;
628
   DstTimeOut = 0;
629
   gmtime_r(tp, &tmDate);
630
   year = tmDate.tm_year;
631
 
632
   //March 1, year, 2AM -> second Sunday
633
   tmDate.tm_year = year;
634
   tmDate.tm_mon = 2;
635
   tmDate.tm_mday = 1;
636
   tmDate.tm_hour = 2;
637
   tmDate.tm_min = 0;
638
   tmDate.tm_sec = 0;
639
   seconds = mktime(&tmDate);
640
   gmtime_r(&seconds, &tmDate);
641
   days = 7 - tmDate.tm_wday + 7;
642
   *dstTimeIn = timeIn = seconds + days * SEC_PER_DAY;
643
 
644
   //November 1, year, 2AM -> first Sunday
645
   tmDate.tm_year = year;
646
   tmDate.tm_mon = 10;
647
   tmDate.tm_mday = 1;
648
   tmDate.tm_hour = 2;
649
   tmDate.tm_min = 0;
650
   tmDate.tm_sec = 0;
651
   seconds = mktime(&tmDate);
652
   gmtime_r(&seconds, &tmDate);
653
   days = 7 - tmDate.tm_wday;
654
   *dstTimeOut = timeOut = seconds + days * SEC_PER_DAY;
655
 
656
   DstTimeIn = timeIn;
657
   DstTimeOut = timeOut;
658
}
659 171 rhoads
#endif //INCLUDE_TIMELIB
660
 

powered by: WebSVN 2.1.0

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