forked from parasyte/alt64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.c
359 lines (265 loc) · 6.66 KB
/
sys.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdint.h>
#include <dma.h>
#include <n64sys.h>
#include "sys.h"
#include "types.h"
#include "everdrive.h"
#include "errors.h"
#include "usb.h"
u32 asm_date;
Options_st options;
u32 native_tv_mode;
#define CIC_6101 1
#define CIC_6102 2
#define CIC_6103 3
#define CIC_6104 4
#define CIC_6105 5
#define CIC_6106 6
u16 strcon(u8 *str1, u8 *str2, u8 *dst, u16 max_len) {
u16 len = 0;
max_len -= 1;
while (*str1 != 0 && len < max_len) {
*dst++ = *str1++;
len++;
}
while (*str2 != 0 && len < max_len) {
*dst++ = *str2++;
len++;
}
*dst++ = 0;
return len;
}
void dma_read_s(void * ram_address, unsigned long pi_address, unsigned long len) {
u32 buff[256];
u32 *bptr;
u32 *rptr = (u32 *) ram_address;
// if(len==32768)
// rptr = (u32 *) 0x803F7988;
u16 i;
//u16 blen = 512;
//if (len < 512)blen = len;
//*(volatile u32*) 0x1FC007FC = 0x08;
IO_WRITE(PI_STATUS_REG, 3);
while (len) {
dma_read(buff, pi_address, 512);
while ((IO_READ(PI_STATUS_REG) & 3) != 0);
//while ((*((volatile u32*) PI_STATUS_REG) & 0x02) != 1);
data_cache_hit_invalidate(buff, 512);
bptr = buff;
for (i = 0; i < 512 && i < len; i += 4)*rptr++ = *bptr++;
len = len < 512 ? 0 : len - 512;
pi_address += 512;
}
}
void dma_write_s(void * ram_address, unsigned long pi_address, unsigned long len) {
//if(len==32768)
//ram_address = (u32 *) 0x803F7988;
data_cache_hit_writeback(ram_address, len);
dma_write(ram_address, pi_address, len);
}
/*
void showError(char *str, u32 code) {
console_printf("%s%u\n", str, code);
joyWait();
}
*/
void sleep(u32 ms) {
u32 current_ms = get_ticks_ms();
while (get_ticks_ms() - current_ms < ms);
}
void dma_read_sram(void *dest, u32 offset, u32 size) {
/*
PI_DMAWait();
IO_WRITE(PI_STATUS_REG, 0x03);
IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest));
IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset));
// data_cache_invalidate_all();
IO_WRITE(PI_WR_LEN_REG, (size - 1));
*/
/* 0xA8000000
* 0xb0000000
* 0x4000000
* */
dma_read_s(dest, 0xA8000000 + offset, size);
//data_cache_invalidate(dest,size);
}
void dma_write_sram(void* src, u32 offset, u32 size) {
/*
PI_DMAWait();
IO_WRITE(PI_STATUS_REG, 0x02);
IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src));
IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset));
// data_cache_invalidate_all();
IO_WRITE(PI_RD_LEN_REG, (size - 1));
*/
dma_write_s(src, 0xA8000000 + offset, size);
}
#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
#define DO2(buf) DO1(buf); DO1(buf);
#define DO4(buf) DO2(buf); DO2(buf);
#define DO8(buf) DO4(buf); DO4(buf);
unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len) {
static unsigned int crc_table[256];
static int make_crc_table = 1;
if (make_crc_table) {
unsigned int c, n;
int k;
unsigned int poly;
const unsigned char p[] = {0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26};
/* make exclusive-or pattern from polynomial (0xedb88320L) */
poly = 0L;
for (n = 0; n < sizeof (p) / sizeof (unsigned char); n++)
poly |= 1L << (31 - p[n]);
for (n = 0; n < 256; n++) {
c = n;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
crc_table[n] = c;
}
make_crc_table = 0;
}
if (buf == (void*) 0) return 0L;
crc = crc ^ 0xffffffffL;
while (len >= 8) {
DO8(buf);
len -= 8;
}
if (len)
do {
DO1(buf);
} while (--len);
return crc ^ 0xffffffffL;
}
u32 ii;
volatile u32 *pt;
void clean();
#define MEM32(addr) *((volatile u32 *)addr)
u8 str_buff[128];
u8 STR_intToDecString(u32 val, u8 *str) {
int len;
if (val < 10)len = 1;
else
if (val < 100)len = 2;
else
if (val < 1000)len = 3;
else
if (val < 10000)len = 4;
else
if (val < 100000)len = 5;
else
if (val < 1000000)len = 6;
else
if (val < 10000000)len = 7;
else
if (val < 100000000)len = 8;
else
if (val < 1000000000)len = 9;
else len = 10;
str += len;
str[0] = 0;
if (val == 0)*--str = '0';
while (val) {
*--str = '0' + val % 10;
val /= 10;
}
return len;
}
void STR_intToDecStringMin(u32 val, u8 *str, u8 min_size) {
int len;
u8 i;
if (val < 10)len = 1;
else
if (val < 100)len = 2;
else
if (val < 1000)len = 3;
else
if (val < 10000)len = 4;
else
if (val < 100000)len = 5;
else
if (val < 1000000)len = 6;
else
if (val < 10000000)len = 7;
else
if (val < 100000000)len = 8;
else
if (val < 1000000000)len = 9;
else len = 10;
if (len < min_size) {
i = min_size - len;
while (i--)str[i] = '0';
len = min_size;
}
str += len;
str[0] = 0;
if (val == 0)*--str = '0';
while (val) {
*--str = '0' + val % 10;
val /= 10;
}
}
u8 streq(u8 *str1, u8 *str2) {
u8 s1;
u8 s2;
for (;;) {
s1 = *str1++;
s2 = *str2++;
if (s1 >= 'a' && s1 <= 'z')s1 -= 0x20;
if (s2 >= 'a' && s2 <= 'z')s2 -= 0x20;
if (s1 != s2) return 0;
if (*str1 == 0 && *str2 == 0)return 1;
}
}
u8 streql(u8 *str1, u8 *str2, u8 len) {
u8 s1;
u8 s2;
while (len--) {
s1 = *str1++;
s2 = *str2++;
if (s1 >= 'a' && s1 <= 'z')s1 -= 0x20;
if (s2 >= 'a' && s2 <= 'z')s2 -= 0x20;
if (s1 != s2) return 0;
}
return 1;
}
u16 strContain(u8 *target, u8 *str) {
u16 targ_len = slen(target);
u16 eq_len;
for (eq_len = 0; eq_len < targ_len;) {
if (*str == 0)return 0;
if (*str++ == target[eq_len]) {
eq_len++;
} else {
eq_len = 0;
}
}
if (eq_len != targ_len)return 0;
return 1;
}
u8 slen(u8 *str) {
u8 len = 0;
while (*str++)len++;
return len;
}
u8 scopy(u8 *src, u8 *dst) {
u8 len = 0;
while (*src != 0) {
*dst++ = *src++;
len++;
}
*dst = 0;
return len;
}
void strhicase(u8 *str, u8 len) {
if (len) {
while (len--) {
if (*str >= 'a' && *str <= 'z')*str -= 0x20;
str++;
}
} else {
while (*str != 0) {
if (*str >= 'a' && *str <= 'z')*str -= 0x20;
str++;
}
}
}