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

Subversion Repositories layer2

[/] [layer2/] [trunk/] [sw/] [lib/] [stdlib.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
/******************************************************************************
2
 * Standard Library                                                           *
3
 ******************************************************************************
4
 * Copyright (C)2011  Mathias Hörtnagl <mathias.hoertnagl@gmail.com>          *
5
 *                                                                            *
6
 * This program is free software: you can redistribute it and/or modify       *
7
 * it under the terms of the GNU General Public License as published by       *
8
 * the Free Software Foundation, either version 3 of the License, or          *
9
 * (at your option) any later version.                                        *
10
 *                                                                            *
11
 * This program is distributed in the hope that it will be useful,            *
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
14
 * GNU General Public License for more details.                               *
15
 *                                                                            *
16
 * You should have received a copy of the GNU General Public License          *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
18
 ******************************************************************************/
19
#include "stddef.h"
20
#include "stdlib.h"
21
 
22
/******************************************************************************
23
 * Timer                                                                      *
24
 ******************************************************************************/
25
/* Resets the counter. If you call reset before the counter has finished, it
26
   returns the count progress. */
27
uint pit_reset() {
28
   return PIT_ADDRESS[0];
29
}
30
 
31
/* Set PIT limit and start counting. */
32
void pit_run(uint cycles) {
33
   PIT_ADDRESS[0] = cycles;
34
}
35
 
36
 
37
/******************************************************************************
38
 * RS-232                                                                     *
39
 ******************************************************************************/
40
/* Wait for one byte of data. Return n reception. */
41
uchar rs232_receive() {
42
   return RS232_ADDRESS[0];
43
}
44
 
45
/* Send one byte of data. */
46
void rs232_transmit(uchar chr) {
47
   RS232_ADDRESS[1] = chr;
48
}
49
 
50
 
51
/******************************************************************************
52
 * Memory Operations                                                          *
53
 ******************************************************************************/
54
 
55
void memcpy(const void *src, void *dst, uint len) {
56
   for(char *s = src, *d = dst; len-- > 0; *d++ = *s++);
57
}
58
 
59
void memset(const void *ptr, int val, uint len) {
60
   for(char *p = ptr; len-- > 0; *p++ = val);
61
}
62
 
63
// 
64
// int memcmp(const void *src, void *dst, uint len) {
65
   // for(char *s = src, *d = dst; len > 0; len--)
66
      // if(*s++ != *p++) return 1;
67
   // return 0;
68
// }
69
 
70
 
71
/******************************************************************************
72
 * String Operations                                                          *
73
 ******************************************************************************/
74
/* Returns the length of a string */
75
uint strlen(const uchar *str) {
76
   uint c = 0;
77
   for(uchar *s = str; *s++; c++);
78
   return c;
79
}
80
 
81
/* Copys a string at location src to location dst. */
82
void strcpy(const uchar *src, uchar *dst) {
83
   for(uchar *s = src, *d = dst; *d++ = *s++; );
84
}
85
 
86
/* Returns a pointer to the leftmost occurence of character chr in
87
   string str or NULL, if not found. */
88
uchar *strchr(const uchar *str, const uchar chr) {
89
   for(uchar *s = str; *s; s++)
90
      if(*s == chr) return *s;
91
   return NULL;
92
}
93
 
94
 
95
/******************************************************************************
96
 * Number/String Conversion                                                   *
97
 ******************************************************************************/
98
 /* Convert a string containing a decimal number into a number. */
99
int atoi(const uchar *str) {
100
 
101
   int num = 0;
102
   uchar sign;
103
   uchar *s = str;
104
 
105
   for(; !( isdigit(*s) || (*s == '-') ) && (*s != NULL); s++);
106
   sign = *s++;
107
 
108
   for(; isdigit(*s); s++)
109
      num = x10(num) + (*s-'0');
110
   return (sign == '-') ? -num : num;
111
}
112
 
113
/* Returns a binary representation of an integer <num>. The buffer <str> must be
114
   at least 35 byte wide to hold the char sequence of the form '0bn...n\0'. */
115
uchar* itob(int num, uchar *str) {
116
 
117
   uchar *s = str;
118
   uint p = 0x80000000;
119
 
120
   *s++ = '0';
121
   *s++ = 'b';
122
   //while( !(num & p) ) p >>= 1;
123
   while(p) {
124
      *s++ = (num & p) ? '1' : '0';
125
      p >>= 1;
126
   }
127
   *s = '\0';
128
   return str;
129
}
130
 
131
/* Returns a hexadecimal representation of an integer <num>. The buffer <str>
132
   must be at least 11 byte wide to hold the char sequence of the form
133
   '0xn...n\0'. */
134
uchar* itox(int num, uchar *str) {
135
 
136
   uchar *s = str;
137
   uchar n;
138
   uint p = 0xf0000000;
139
 
140
   *s++ = '0';
141
   *s++ = 'x';
142
   //while( !(num & p) ) p >>= 1;
143
   for(int i=28; i>=0; i-=4) {
144
      n = ((uint) num & p) >> i;
145
      if ( n <= 9 )
146
         *s++ = n + '0';
147
      else
148
         *s++ = n - 10 + 'a';
149
      p >>= 4;
150
   }
151
   *s = '\0';
152
   return str;
153
}
154
 
155
 
156
/******************************************************************************
157
 * Nathematics                                                                *
158
 ******************************************************************************/
159
static uint x = 314159265;
160
/* Xorshift RNGs, George Marsaglia
161
   http://www.jstatsoft.org/v08/i14/paper */
162
uint rand() {
163
  x ^= x << 13;
164
  x ^= x >> 17;
165
  x ^= x << 5;
166
  return x;
167
}
168
 
169
/* Radix-4 Booth Multiplication Algorithm */
170
int mul(short a, short b) {
171
 
172
   int r = 0;
173
   int ai = a << 1;
174
 
175
   for(int i=0; i++<16; ai>>=1, b<<=1)
176
      switch (ai & 3) {
177
         case 1: r += b; break;
178
         case 2: r -= b; break;
179
      }
180
   return r;
181
}
182
 
183
 
184
short div(int a, int b) {
185
 
186
   char s = 0;
187
   short m = 0;
188
 
189
   if(a < 0) {
190
      s = 1;
191
      a = -a;
192
   }
193
   if(b < 0) {
194
      if(s == 1)
195
         s = 0;
196
      else
197
         s = 1;
198
 
199
      b = -b;
200
   }
201
   if(b == 0) {
202
      return 0;
203
   }
204
   while(a > b) {
205
      m++;
206
      a = a-b;
207
   }
208
   return m;
209
}

powered by: WebSVN 2.1.0

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