forked from madcock/sf2000_multicore
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.c
357 lines (289 loc) · 7.02 KB
/
lib.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "debug.h"
#include "stockfw.h"
#include "dirent.h"
// NOTE: cache flushing for a specific memory range is currently not stable!
/*
void cache_flush(void *addr, size_t size)
{
uintptr_t idx;
uintptr_t begin = (uintptr_t)addr;
uintptr_t end = begin + size - 1;
// Hit_Writeback_D
for (idx = begin; idx <= end; idx += 16)
asm volatile("cache 0x19, 0(%0)" : : "r"(idx));
asm volatile("sync 0; nop; nop");
// Hit_Invalidate_I
for (idx = begin; idx <= end; idx += 16)
asm volatile("cache 0x10, 0(%0)" : : "r"(idx));
asm volatile("nop; nop; nop; nop; nop; nop; nop; nop"); // ehb may be nop on this core
}*/
void full_cache_flush()
{
unsigned idx;
// Index_Writeback_Inv_D
for (idx = 0x80000000; idx <= 0x80004000; idx += 16) // all of D-cache
asm volatile("cache 1, 0(%0); cache 1, 0(%0)" : : "r"(idx));
asm volatile("sync 0; nop; nop");
// Index_Invalidate_I
for (idx = 0x80000000; idx <= 0x80004000; idx += 16) // all of I-cache
asm volatile("cache 0, 0(%0); cache 0, 0(%0)" : : "r"(idx));
asm volatile("nop; nop; nop; nop; nop"); // ehb may be nop on this core
}
// a call to this function is generated by gcc when __builtin__clear_cache is used
void _flush_cache(void *buf, size_t sz, int flags)
{
// note: params are ignored and *all* the cache is cleared instead.
// this seems to produce the most stable behavior for running dynarec code.
full_cache_flush();
}
extern int g_errno;
extern void (*__xlog)(const char *fmt, ...);
// #define __xlog(...)
void *sbrk(ptrdiff_t incr)
{
static void *s_heap_end;
static void *s_heap_ptr = NULL;
if (!s_heap_ptr)
{
s_heap_ptr = gp_buf_64m; // use stock's 64MB scratch buffer
s_heap_end = gp_buf_64m + 0x4000000;
}
void *curr_ptr = s_heap_ptr;
void *new_ptr = s_heap_ptr + incr;
if (new_ptr >= s_heap_end)
lcd_bsod("sbrk: out of memory");
s_heap_ptr = new_ptr;
// __xlog("sbrk: ret=%p incr=%d s_heap_ptr=%p\n", curr_ptr, (int)incr, s_heap_ptr);
return curr_ptr;
}
int open(const char *path, int flags, ...)
{
int fs_flags = 0;
int fs_perms = 0666; // all access?
if (flags & O_RDONLY) fs_flags |= FS_O_RDONLY;
if (flags & O_WRONLY) fs_flags |= FS_O_WRONLY;
if (flags & O_RDWR) fs_flags |= FS_O_RDWR;
if (flags & O_APPEND) fs_flags |= FS_O_APPEND;
if (flags & O_CREAT) fs_flags |= FS_O_CREAT;
if (flags & O_TRUNC) fs_flags |= FS_O_TRUNC;
// __xlog("open: path=%s flags=%d fs_flags=%d\n", path, flags, fs_flags);
int ret = fs_open(path, fs_flags, fs_perms);
if (ret < 0)
errno = g_errno;
else
ret += 5;
// __xlog("open: ret=%d\n", ret);
return ret;
}
ssize_t read(int fd, void *buf, size_t count)
{
if (fd == 0 || fd == 1 || fd == 2)
return 0;
fd -= 5;
// __xlog("read: fd=%d buf=%p count=%u\n", fd, buf, count);
ssize_t ret = fs_read(fd, buf, count);
if (ret < 0)
errno = g_errno;
// __xlog("read: ret=%d\n", ret);
return ret;
}
ssize_t write(int fd, const void *buf, size_t count)
{
if (fd == 0)
return -1;
else if (fd == 1 || fd == 2)
{
xlog("%.*s", count, buf);
return count;
}
fd -= 5;
// __xlog("write: fd=%d buf=%p count=%u\n", fd, buf, count);
ssize_t ret = fs_write(fd, buf, count);
if (ret < 0)
errno = g_errno;
// __xlog("write: ret=%d\n", ret);
return ret;
}
off_t lseek(int fd, off_t offset, int whence)
{
if (fd == 0 || fd == 1 || fd == 2)
return -1;
// __xlog("lseek: fd=%d offset=%d whence=%d\n", fd, (int)offset, whence);
fd -= 5;
int64_t ret = fs_lseek(fd, offset, whence);
if (ret < 0)
errno = g_errno;
// __xlog("lseek: ret=%d\n", (int)ret);
return ret;
}
int close(int fd)
{
if (fd == 0 || fd == 1 || fd == 2)
return -1;
// __xlog("close: fd=%d\n", fd);
fd -= 5;
int ret = fs_close(fd);
if (ret < 0)
errno = g_errno;
// __xlog("close: ret=%d\n", ret);
return ret;
}
typedef struct {
union {
struct {
uint8_t _1[0x18]; // type is at offset 0x18
uint32_t type; // 0x81b6 - file, 0x41ff - dir
};
struct {
uint8_t _2[0x38]; // size is at offset 0x38
uint32_t size; // filesize if type is a file
};
uint8_t __[160]; // total struct size is 160
};
} fs_stat_t;
static int stat_common(int ret, fs_stat_t *buffer, struct stat *sbuf)
{
if (ret == 0)
{
memset(sbuf, 0, sizeof(*sbuf));
sbuf->st_mode = S_ISREG(buffer->type)*S_IFREG | S_ISDIR(buffer->type)*S_IFDIR | (S_IRUSR|S_IWUSR);
sbuf->st_size = buffer->size;
return 0;
}
else
return -1;
}
// wrap fs_stat to supply a more standard stat implementation
// for now only `type` (for dir or file) and `size` fields of `struct stat` are filled
int stat(const char *path, struct stat *sbuf)
{
fs_stat_t buffer = {0};
int ret = fs_stat(path, &buffer);
return stat_common(ret, &buffer, sbuf);
}
int fstat(int fd, struct stat *sbuf)
{
fs_stat_t buffer = {0};
int ret = fs_fstat(fd, &buffer);
return stat_common(ret, &buffer, sbuf);
}
int access(const char *path, int mode)
{
struct stat buffer;
return stat(path, &buffer);
}
int mkdir(const char *path, mode_t mode)
{
return fs_mkdir(path, mode);
}
char *getcwd(char *buf, size_t size)
{
return NULL;
}
int chdir(const char *path)
{
return -1;
}
int rmdir(const char *path)
{
return -1;
}
int unlink(const char *path)
{
return -1;
}
int chmod(const char *path, mode_t mode)
{
return -1;
}
int kill(pid_t pid, int sig)
{
return -1;
}
pid_t getpid(void)
{
return 1;
}
void abort(void)
{
unsigned ra;
asm volatile ("move %0, $ra" : "=r" (ra));
lcd_bsod("abort() called from 0x%08x", ra);
}
void exit(int status)
{
unsigned ra;
asm volatile ("move %0, $ra" : "=r" (ra));
lcd_bsod("exit(%d) called from 0x%08x", status, ra);
}
/* wrappers were not compiled in, but vfs drivers likely support these ops */
int rename(const char *old, const char* new)
{
return -1;
}
int isatty(int fd)
{
return 0;
}
clock_t clock(void)
{
// clock function should return cpu clock ticks, so since os_get_tick_count() returns milliseconds,
// we devide by 1000 to get the seconds and multiply by CLOCKS_PER_SEC to get the clock ticks.
return (clock_t)(os_get_tick_count() * CLOCKS_PER_SEC / 1000);
}
int gettimeofday(struct timeval *tv, void *tz)
{
if (tv == NULL)
return -1;
uint32_t msec = os_get_tick_count();
tv->tv_sec = msec / 1000;
tv->tv_usec = (msec % 1000) * 1000;
return 0;
}
DIR *opendir(const char *path)
{
int fd = fs_opendir(path);
if (fd < 0)
return NULL;
return (DIR*)(fd + 1);
}
int closedir(DIR *dir)
{
int fd = (int)dir - 1;
if (fd < 0)
return -1;
return fs_closedir(fd);
}
struct dirent *readdir(DIR *dir)
{
int fd = (int)dir - 1;
if (fd < 0)
return NULL;
struct {
union {
struct {
uint8_t _1[0x10];
uint32_t type;
};
struct {
uint8_t _2[0x22];
char d_name[0x225];
};
uint8_t __[0x428];
};
} buffer = {0};
if (fs_readdir(fd, &buffer) < 0)
return NULL;
// TODO: not thread safe
static struct dirent d;
d.d_type = S_ISREG(buffer.type)*DTYPE_FILE | S_ISDIR(buffer.type)*DTYPE_DIRECTORY;
strncpy(d.d_name, buffer.d_name, sizeof(d.d_name));
return &d;
}