/[pcsx2_0.9.7]/trunk/fps2bios/kernel/iopload/sysclib/sysclib.c
ViewVC logotype

Contents of /trunk/fps2bios/kernel/iopload/sysclib/sysclib.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 5 months ago) by william
File MIME type: text/plain
File size: 12716 byte(s)
exported r3113 from ./upstream/trunk
1 #include <tamtypes.h>
2 #include <stddef.h>
3 #include <limits.h>
4 #include <errno.h>
5
6 #include "kloadcore.h"
7 #include "stdarg.h"
8 #include "ksysclib.h"
9
10
11 typedef struct _jmp_buf{
12 int ra,
13 sp,
14 fp,
15 s0,
16 s1,
17 s2,
18 s3,
19 s4,
20 s5,
21 s6,
22 s7,
23 gp;
24 } *jmp_buf;
25
26 int _start(int argc, char* argv[]);
27
28
29 unsigned char ctype_table[128]={
30 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
31 0x20, 0x08, 0x08, 0x08, 0x08, 0x08, 0x20, 0x20,
32 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
33 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
34
35 0x18, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
36 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
37 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
38 0x04, 0x04, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
39
40 0x10, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x01,
41 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
42 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
43 0x01, 0x01, 0x01, 0x10, 0x10, 0x10, 0x10, 0x10,
44
45 0x10, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x02,
46 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
47 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
48 0x02, 0x02, 0x02, 0x10, 0x10, 0x10, 0x10, 0x20
49 };
50
51 int setjmp (jmp_buf jb){
52 asm __volatile__(
53 "sw $31, 0($4)\n" //$ra, JUMP_BUFFER.ra($a0)
54 "sw $28, 4($4)\n" //$gp, JUMP_BUFFER.gp($a0)
55 "sw $29, 8($4)\n" //$sp, JUMP_BUFFER.sp($a0)
56 "sw $30, 12($4)\n" //$fp, JUMP_BUFFER.fp($a0)
57 "sw $16, 16($4)\n" //$s0, JUMP_BUFFER.s0($a0)
58 "sw $17, 20($4)\n" //$s1, JUMP_BUFFER.s1($a0)
59 "sw $18, 24($4)\n" //$s2, JUMP_BUFFER.s2($a0)
60 "sw $19, 28($4)\n" //$s3, JUMP_BUFFER.s3($a0)
61 "sw $20, 32($4)\n" //$s4, JUMP_BUFFER.s4($a0)
62 "sw $21, 36($4)\n" //$s5, JUMP_BUFFER.s5($a0)
63 "sw $22, 40($4)\n" //$s6, JUMP_BUFFER.s6($a0)
64 "sw $23, 44($4)\n" //$s7, JUMP_BUFFER.s7($a0)
65 );
66 return 0;
67 }
68
69 int longjmp(void *jb, int u){
70 asm __volatile__(
71 "lw $31, 0($4)\n" //$ra, JUMP_BUFFER.ra($a0)
72 "lw $28, 4($4)\n" //$gp, JUMP_BUFFER.gp($a0)
73 "lw $29, 8($4)\n" //$sp, JUMP_BUFFER.sp($a0)
74 "lw $30, 12($4)\n" //$fp, JUMP_BUFFER.fp($a0)
75 "lw $16, 16($4)\n" //$s0, JUMP_BUFFER.s0($a0)
76 "lw $17, 20($4)\n" //$s1, JUMP_BUFFER.s1($a0)
77 "lw $18, 24($4)\n" //$s2, JUMP_BUFFER.s2($a0)
78 "lw $19, 28($4)\n" //$s3, JUMP_BUFFER.s3($a0)
79 "lw $20, 32($4)\n" //$s4, JUMP_BUFFER.s4($a0)
80 "lw $21, 36($4)\n" //$s5, JUMP_BUFFER.s5($a0)
81 "lw $22, 40($4)\n" //$s6, JUMP_BUFFER.s6($a0)
82 "lw $23, 44($4)\n" //$s7, JUMP_BUFFER.s7($a0)
83 );
84 return u;
85 }
86
87 #define CHAR(a) (((a)<<24)>>24)
88 unsigned char look_ctype_table(int);
89
90 int toupper (int ch){
91 if (look_ctype_table(CHAR(ch)) & 0x0002)
92 return CHAR(ch-32);
93 return CHAR(ch);
94 }
95
96 int tolower (int ch){
97 if (look_ctype_table(CHAR(ch)) & 0x0001)
98 return CHAR(ch+32);
99 return CHAR(ch);
100 }
101
102 unsigned char look_ctype_table(int pos){
103 return ctype_table[pos];
104 }
105
106 void* get_ctype_table(){
107 return ctype_table;
108 }
109
110 void* memchr(void *s, int ch, int len){
111 if (s && len>0) {
112 ch &= 0xFF;
113 do {
114 if (*(unsigned char*)s == ch)
115 return s;
116 } while(s++, --len>0);
117
118 }
119 return NULL;
120 }
121
122 int memcmp(const void *a, const void *b, size_t len){
123 if (len == 0) return 0;
124
125 do {
126 if (*(unsigned char*)a != *(unsigned char*)b) {
127 if (*(unsigned char*)a < *(unsigned char*)b) return 1;
128 else return -1;
129 }
130 } while (a++, b++, --len>0);
131
132 return 0;
133 }
134
135 void* memcpy(void *dest, const void *src, size_t len){
136 void *temp;
137
138 if (dest == NULL) return NULL;
139
140 // if (((int)dest | (int)src | len) & 3 == 0)
141 // return wmemcopy(dest, src, len);
142 for(temp=dest; len>0; len--)
143 *(unsigned char*)temp++ = *(unsigned char*)src++;
144 return dest;
145 }
146
147 void *memmove (void *dest, const void *src, size_t len){
148 void *temp;
149
150 if (dest==NULL) return NULL;
151
152 temp=dest;
153 if (dest>=src)
154 for (len--; len>=0; len--)
155 *(unsigned char*)(temp+len)=*(unsigned char*)(src+len);
156 else
157 for ( ; len>0; len--)
158 *(unsigned char*)temp++ =*(unsigned char*)src++;
159 return dest;
160 }
161
162 void *memset (void *dest, int ch, size_t len){
163 void *temp;
164
165 if (dest == NULL) return NULL;
166
167 /* if ((ch==0) && (((int)dest | len) & 3 == 0))
168 return wmemset(dest, 0, len);
169 else
170 */ for (temp=dest; len>0; len--)
171 *(unsigned char*)temp++ = ch;
172 return dest;
173 }
174
175 int bcmp (const void *a, const void *b, size_t len){
176 return memcmp(a, b, len);
177 }
178
179 void bcopy (const void *src, void *dest, size_t len){
180 memmove(dest, src, len);
181 }
182
183 void bzero (void *dest, size_t len){
184 memset(dest, 0, len);
185 }
186
187 char *strcat(char *dest, const char *src) {
188 char *s = dest;
189
190 while (*dest) dest++;
191 while (*dest++ = *src++);
192
193 return s;
194 }
195
196 char *strncat(char *dest, const char *src, size_t n) {
197 char *s = dest;
198
199 while (*dest) dest++;
200 while (n-- > 0 && (*dest++ = *src++));
201 *dest = '\0';
202
203 return s;
204 }
205
206 char *strchr(const char *s, int c) {
207 unsigned char _c = (unsigned int)c;
208
209 while (*s && *s != c) s++;
210 if (*s != c) s = NULL;
211
212 return (char *)s;
213 }
214
215 char *strrchr(const char *s, int c) {
216 const char *last = NULL;
217
218 if (c) {
219 while (s = strchr(s, c)) {
220 last = s++;
221 }
222 } else {
223 last = strchr(s, c);
224 }
225
226 return (char *)last;
227 }
228
229 int strcmp(const char *s1, const char *s2) {
230 while (*s1 != '\0' && *s1 == *s2) {
231 s1++; s2++;
232 }
233
234 return (*(unsigned char *)s1) - (*(unsigned char *)s2);
235 }
236
237 int strncmp(const char *s1, const char *s2, size_t n) {
238 if (n == 0) return 0;
239
240 while (n-- != 0 && *s1 == *s2) {
241 if (n == 0 || *s1 == '\0') break;
242 s1++; s2++;
243 }
244
245 return (*(unsigned char *)s1) - (*(unsigned char *)s2);
246 }
247
248 char *strcpy(char *dest, const char *src) {
249 char *s = dest;
250
251 while (*dest++ = *src++);
252
253 return s;
254 }
255
256 char *strncpy(char *dest, const char *src, size_t n) {
257 char *s = dest;
258
259 while (*dest++ = *src++);
260
261 return s;
262 }
263
264 size_t strspn(const char *s, const char *accept) {
265 const char *_s = s;
266 const char *c;
267
268 while (*s) {
269 for (c = accept; *c; c++) {
270 if (*s == *c) break;
271 }
272 if (*c == '\0') break;
273 s++;
274 }
275
276 return s - _s;
277 }
278
279 size_t strcspn(const char *s, const char *reject) {
280 const char *_s = s;
281 const char *c;
282
283 while (*s) {
284 for (c = reject; *c; c++) {
285 if (*s == *c) break;
286 }
287 if (*c) break;
288 s++;
289 }
290
291 return s - _s;
292 }
293
294 char *index(const char *s, int c) {
295 return strchr(s, c);
296 }
297
298 char *rindex(const char *s, int c) {
299 return strrchr(s, c);
300 }
301
302 size_t strlen(const char *s) {
303 const char *start = s;
304
305 while (*s) s++;
306
307 return s - start;
308 }
309
310 char *strpbrk(const char *s, const char *accept) {
311 const char *c = accept;
312
313 if (!*s) return NULL;
314
315 while (*s) {
316 for (c = accept; *c; c++) {
317 if (*s == *c) break;
318 }
319 if (*c) break;
320 s++;
321 }
322
323 if (*c == '\0') s = NULL;
324
325 return (char *)s;
326 }
327
328 char *strstr(const char *haystack, const char *needle) {
329 if (*haystack == 0) {
330 if (*needle) return (char *) NULL;
331 return (char *) haystack;
332 }
333
334 while (*haystack) {
335 size_t i;
336 i = 0;
337
338 for (;;) {
339 if (needle[i] == 0) {
340 return (char *) haystack;
341 }
342
343 if (needle[i] != haystack[i]) break;
344 i++;
345 }
346 haystack++;
347 }
348
349 return (char *) NULL;
350 }
351
352 #define _CTYPE_DATA_0_127 \
353 _C, _C, _C, _C, _C, _C, _C, _C, \
354 _C, _C|_S, _C|_S, _C|_S, _C|_S, _C|_S, _C, _C, \
355 _C, _C, _C, _C, _C, _C, _C, _C, \
356 _C, _C, _C, _C, _C, _C, _C, _C, \
357 _S|_B, _P, _P, _P, _P, _P, _P, _P, \
358 _P, _P, _P, _P, _P, _P, _P, _P, \
359 _N, _N, _N, _N, _N, _N, _N, _N, \
360 _N, _N, _P, _P, _P, _P, _P, _P, \
361 _P, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U, \
362 _U, _U, _U, _U, _U, _U, _U, _U, \
363 _U, _U, _U, _U, _U, _U, _U, _U, \
364 _U, _U, _U, _P, _P, _P, _P, _P, \
365 _P, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L, \
366 _L, _L, _L, _L, _L, _L, _L, _L, \
367 _L, _L, _L, _L, _L, _L, _L, _L, \
368 _L, _L, _L, _P, _P, _P, _P, _C
369
370 #define _CTYPE_DATA_128_256 \
371 0, 0, 0, 0, 0, 0, 0, 0, \
372 0, 0, 0, 0, 0, 0, 0, 0, \
373 0, 0, 0, 0, 0, 0, 0, 0, \
374 0, 0, 0, 0, 0, 0, 0, 0, \
375 0, 0, 0, 0, 0, 0, 0, 0, \
376 0, 0, 0, 0, 0, 0, 0, 0, \
377 0, 0, 0, 0, 0, 0, 0, 0, \
378 0, 0, 0, 0, 0, 0, 0, 0, \
379 0, 0, 0, 0, 0, 0, 0, 0, \
380 0, 0, 0, 0, 0, 0, 0, 0, \
381 0, 0, 0, 0, 0, 0, 0, 0, \
382 0, 0, 0, 0, 0, 0, 0, 0, \
383 0, 0, 0, 0, 0, 0, 0, 0, \
384 0, 0, 0, 0, 0, 0, 0, 0, \
385 0, 0, 0, 0, 0, 0, 0, 0, \
386 0, 0, 0, 0, 0, 0, 0, 0
387
388
389 const char _ctype_[1 + 256] = {
390 0,
391 _CTYPE_DATA_0_127,
392 _CTYPE_DATA_128_256
393 };
394
395 char *strtok(char *s, const char *delim) {
396 }
397
398 long _strtol_r(const char *nptr, char **endptr, int base) {
399 register const char *s = nptr;
400 register unsigned long acc;
401 register int c;
402 register unsigned long cutoff;
403 register int neg = 0, any, cutlim;
404
405 /*
406 * Skip white space and pick up leading +/- sign if any.
407 * If base is 0, allow 0x for hex and 0 for octal, else
408 * assume decimal; if base is already 16, allow 0x.
409 */
410 do {
411 c = *s++;
412 } while (isspace(c));
413 if (c == '-') {
414 neg = 1;
415 c = *s++;
416 } else if (c == '+')
417 c = *s++;
418 if ((base == 0 || base == 16) &&
419 c == '0' && (*s == 'x' || *s == 'X')) {
420 c = s[1];
421 s += 2;
422 base = 16;
423 }
424 if (base == 0)
425 base = c == '0' ? 8 : 10;
426
427 /*
428 * Compute the cutoff value between legal numbers and illegal
429 * numbers. That is the largest legal value, divided by the
430 * base. An input number that is greater than this value, if
431 * followed by a legal input character, is too big. One that
432 * is equal to this value may be valid or not; the limit
433 * between valid and invalid numbers is then based on the last
434 * digit. For instance, if the range for longs is
435 * [-2147483648..2147483647] and the input base is 10,
436 * cutoff will be set to 214748364 and cutlim to either
437 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
438 * a value > 214748364, or equal but the next digit is > 7 (or 8),
439 * the number is too big, and we will return a range error.
440 *
441 * Set any if any `digits' consumed; make it negative to indicate
442 * overflow.
443 */
444 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
445 cutlim = cutoff % (unsigned long)base;
446 cutoff /= (unsigned long)base;
447 for (acc = 0, any = 0;; c = *s++) {
448 if (isdigit(c))
449 c -= '0';
450 else if (isalpha(c))
451 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
452 else
453 break;
454 if (c >= base)
455 break;
456 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
457 any = -1;
458 else {
459 any = 1;
460 acc *= base;
461 acc += c;
462 }
463 }
464 if (any < 0) {
465 acc = neg ? LONG_MIN : LONG_MAX;
466 // rptr->_errno = ERANGE;
467 } else if (neg)
468 acc = -acc;
469 if (endptr != 0)
470 *endptr = (char *) (any ? s - 1 : nptr);
471 return (acc);
472 }
473
474 long int strtol(const char *nptr, char **endptr, int base) {
475 return _strtol_r (nptr, endptr, base);
476 }
477
478 unsigned long int strtoul(const char *nptr, char **endptr, int base) {
479 }
480
481 void atob() {
482 }
483
484 wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, size_t n) {
485 }
486
487 wchar_t *wmemset(wchar_t *wcs, wchar_t wc, size_t n) {
488 }
489
490 void prnt() {
491 }
492
493 int sprintf(char *str, const char *format, ...) {
494 }
495
496 int vsprintf(char *str, const char *format, va_list ap) {
497 }
498
499
500
501
502 ///////////////////////////////////////////////////////////////////////
503 void _retonly(){}
504
505 struct export sysclib_stub={
506 0x41C00000,
507 0,
508 VER(1, 1), // 1.1 => 0x101
509 0,
510 "sysclib",
511 (func)_start, // entrypoint
512 (func)_retonly,
513 (func)_retonly,
514 (func)_retonly,
515 (func)setjmp,
516 (func)longjmp,
517 (func)toupper,
518 (func)tolower,
519 (func)look_ctype_table,
520 (func)get_ctype_table,
521 (func)memchr,
522 (func)memcmp,
523 (func)memcpy,
524 (func)memmove,
525 (func)memset,
526 (func)bcmp,
527 (func)bcopy,
528 (func)bzero,
529 (func)prnt,
530 (func)sprintf,
531 (func)strcat,
532 (func)strchr,
533 (func)strcmp,
534 (func)strcpy,
535 (func)strcspn,
536 (func)index,
537 (func)rindex,
538 (func)strlen,
539 (func)strncat,
540 (func)strncmp,
541 (func)strncpy,
542 (func)strpbrk,
543 (func)strrchr,
544 (func)strspn,
545 (func)strstr,
546 (func)strtok,
547 (func)strtol,
548 (func)atob,
549 (func)strtoul,
550 (func)wmemcpy,
551 (func)wmemset,
552 (func)vsprintf,
553 0
554 };
555
556 struct export stdio_stub={
557 0x41C00000,
558 0,
559 VER(1, 2), // 1.2 => 0x102
560 0,
561 "stdio",
562 (func)_retonly,
563 (func)_retonly,
564 (func)_retonly,
565 (func)_retonly,
566 (func)_retonly,
567 (func)_retonly,
568 (func)_retonly,
569 (func)_retonly,
570 (func)_retonly,
571 (func)_retonly,
572 (func)_retonly,
573 (func)_retonly,
574 (func)_retonly,
575 (func)_retonly,
576 };
577
578 //////////////////////////////entrypoint///////////////////////////////
579 int _start(int argc, char* argv[]){
580 int ret = RegisterLibraryEntries(&sysclib_stub);
581 return RegisterLibraryEntries(&stdio_stub) + ret;
582 }
583

  ViewVC Help
Powered by ViewVC 1.1.22