-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.c
145 lines (129 loc) · 2.8 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* babelfish -- misc (C) utility functions */
#include "types.h"
#include "utils.h"
#include "hollywood.h"
#include <stdarg.h>
extern void debug_output(u8 byte); // from start.S
static inline void delay(u32 d)
{
write32(HW_TIMER, 0);
while(read32(HW_TIMER) < d);
}
void panic(u8 v)
{
while(1) {
debug_output(v);
delay(1000000);
debug_output(0);
delay(1000000);
}
}
/*
void *memcpy(void *dst, const void *src, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
((unsigned char *)dst)[i] = ((unsigned char *)src)[i];
return dst;
}
*/
void *memcpyr(void *dst, const void *src, size_t len)
{
size_t i;
for (i = len; i > 0; i--)
((unsigned char *)dst)[i-1] = ((unsigned char *)src)[i-1];
return dst;
}
int memcmp(const void *s1, const void *s2, size_t len)
{
size_t i;
const unsigned char * p1 = (const unsigned char *) s1;
const unsigned char * p2 = (const unsigned char *) s2;
for (i = 0; i < len; i++)
if (p1[i] != p2[i]) return p1[i] - p2[i];
return 0;
}
/*size_t strlcat(char *dest, const char *src, size_t maxlen)
{
size_t len;
maxlen--;
for (len = 0; len < maxlen; len++) if (!dest[len]) break;
for (; len < maxlen && *src; len++) dest[len] = *src++;
dest[len] = '\0';
return len;
}
*/
s32 printf (const char* str, ...) {
va_list arp;
u8 c, f, r;
u32 val, pos;
char s[16];
s32 i, w, res, cc;
va_start(arp, str);
for (cc = pos = 0;; ) {
c = *str++;
if (c == 0) break; /* End of string */
if (c != '%') { /* Non escape cahracter */
gecko_putc(c);
pos++;
continue;
}
w = f = 0;
c = *str++;
if (c == '0') { /* Flag: '0' padding */
f = 1; c = *str++;
}
while (c >= '0' && c <= '9') { /* Precision */
w = w * 10 + (c - '0');
c = *str++;
}
if (c == 's') { /* Type is string */
char *param = va_arg(arp, char*);
for (i=0; param[i]; i++) {
gecko_putc(param[i]);
pos++;
}
continue;
}
r = 0;
if (c == 'd') r = 10; /* Type is signed decimal */
if (c == 'u') r = 10; /* Type is unsigned decimal */
if (c == 'X' || c == 'x') r = 16; /* Type is unsigned hexdecimal */
if (r == 0) {
break; /* Unknown type */
}
val = (c == 'd') ? (u32)(long)va_arg(arp, int) :
(u32)va_arg(arp, unsigned int);
/* Put numeral string */
if (c == 'd') {
if (val & 0x80000000) {
val = 0 - val;
f |= 4;
}
}
// if ((maxlen - pos) <= sizeof(s)) continue;
i = sizeof(s) - 1; s[i] = 0;
do {
// c = (u8)(val % r + '0');
c = (u8)((val & 15) + '0');
if (c > '9') c += 7;
s[--i] = c;
// val /= r;
val >>= 4;
} while (i && val);
if (i && (f & 4)) s[--i] = '-';
w = sizeof(s) - 1 - w;
while (i && i > w) s[--i] = (f & 1) ? '0' : ' ';
for (; s[i] ; i++) {
gecko_putc(s[i]);
pos++;
}
}
va_end(arp);
// gecko_puts(output);
return pos;
}
int puts(const char *s) {
gecko_puts(s);
return 0;
}