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
|
#include "Core.h"
#if defined CC_BUILD_N64
#include "_PlatformBase.h"
#include "Stream.h"
#include "ExtMath.h"
#include "Funcs.h"
#include "Window.h"
#include "Utils.h"
#include "Errors.h"
#include "Options.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <libdragon.h>
#include <cop1.h>
//#include <dragonfs.h>
//#include <rtc.h>
//#include <timer.h>
#include <unistd.h>
#include <sys/time.h>
#include "_PlatformConsole.h"
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
const cc_result ReturnCode_FileNotFound = ENOENT;
const cc_result ReturnCode_SocketInProgess = EINPROGRESS;
const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK;
const cc_result ReturnCode_DirectoryExists = EEXIST;
const char* Platform_AppNameSuffix = " N64";
/*########################################################################################################################*
*------------------------------------------------------Logging/Time-------------------------------------------------------*
*#########################################################################################################################*/
cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) {
if (end < beg) return 0;
cc_uint64 delta = end - beg;
return TIMER_MICROS_LL(delta);
}
cc_uint64 Stopwatch_Measure(void) {
return timer_ticks();
}
void Platform_Log(const char* msg, int len) {
write(STDERR_FILENO, msg, len);
write(STDERR_FILENO, "\n", 1);
}
TimeMS DateTime_CurrentUTC(void) {
return 0;
}
void DateTime_CurrentLocal(struct DateTime* t) {
rtc_time_t curTime = { 0 };
rtc_get(&curTime);
t->year = curTime.year;
t->month = curTime.month;
t->day = curTime.day;
t->hour = curTime.hour;
t->minute = curTime.min;
t->second = curTime.sec;
}
/*########################################################################################################################*
*-----------------------------------------------------Directory/File------------------------------------------------------*
*#########################################################################################################################*/
static const cc_string root_path = String_FromConst("/");
static void GetNativePath(char* str, const cc_string* path) {
// TODO temp hack
cc_string path_ = *path;
int idx = String_IndexOf(path, '/');
if (idx >= 0) path_ = String_UNSAFE_SubstringAt(&path_, idx + 1);
Mem_Copy(str, root_path.buffer, root_path.length);
str += root_path.length;
String_EncodeUtf8(str, &path_);
}
cc_result Directory_Create(const cc_string* path) {
return ERR_NOT_SUPPORTED;
}
int File_Exists(const cc_string* path) {
return false;
}
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
return ERR_NOT_SUPPORTED; // TODO add support
}
static cc_result File_Do(cc_file* file, const cc_string* path) {
char str[NATIVE_STR_LEN];
GetNativePath(str, path);
//*file = -1;
//return ReturnCode_FileNotFound;
// TODO: Why does trying this code break everything
int ret = dfs_open(str);
Platform_Log2("Opened %c = %i", str, &ret);
if (ret < 0) { *file = -1; return ret; }
*file = ret;
return 0;
}
cc_result File_Open(cc_file* file, const cc_string* path) {
return File_Do(file, path);
}
cc_result File_Create(cc_file* file, const cc_string* path) {
*file = -1;
return ERR_NOT_SUPPORTED;
//return File_Do(file, path);
}
cc_result File_OpenOrCreate(cc_file* file, const cc_string* path) {
*file = -1;
return ERR_NOT_SUPPORTED;
//return File_Do(file, path);
}
cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) {
int ret = dfs_read(data, 1, count, file);
if (ret < 0) { *bytesRead = -1; return ret; }
*bytesRead = ret;
return 0;
}
cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) {
return ERR_NOT_SUPPORTED;
}
cc_result File_Close(cc_file file) {
if (file < 0) return 0;
return dfs_close(file);
}
cc_result File_Seek(cc_file file, int offset, int seekType) {
static cc_uint8 modes[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
return dfs_seek(file, offset, modes[seekType]);
}
cc_result File_Position(cc_file file, cc_uint32* pos) {
int ret = dfs_tell(file);
if (ret < 0) { *pos = -1; return ret; }
*pos = ret;
return 0;
}
cc_result File_Length(cc_file file, cc_uint32* len) {
int ret = dfs_size(file);
if (ret < 0) { *len = -1; return ret; }
*len = ret;
return 0;
}
/*########################################################################################################################*
*--------------------------------------------------------Threading--------------------------------------------------------*
*#########################################################################################################################*/
// !!! NOTE: PSP uses cooperative multithreading (not preemptive multithreading) !!!
void Thread_Sleep(cc_uint32 milliseconds) {
wait_ms(milliseconds);
}
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
*handle = NULL;
}
void Thread_Detach(void* handle) {
}
void Thread_Join(void* handle) {
}
void* Mutex_Create(const char* name) {
return NULL;
}
void Mutex_Free(void* handle) {
}
void Mutex_Lock(void* handle) {
}
void Mutex_Unlock(void* handle) {
}
void* Waitable_Create(const char* name) {
return NULL;
}
void Waitable_Free(void* handle) {
}
void Waitable_Signal(void* handle) {
}
void Waitable_Wait(void* handle) {
}
void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
}
/*########################################################################################################################*
*---------------------------------------------------------Socket----------------------------------------------------------*
*#########################################################################################################################*/
cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* addrs, int* numValidAddrs) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_Connect(cc_socket* s, cc_sockaddr* addr, cc_bool nonblocking) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_Read(cc_socket s, cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_Write(cc_socket s, const cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
return ERR_NOT_SUPPORTED;
}
void Socket_Close(cc_socket s) { }
cc_result Socket_CheckReadable(cc_socket s, cc_bool* readable) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*--------------------------------------------------------Platform---------------------------------------------------------*
*#########################################################################################################################*/
// See src/n64sys.c
static void DisableFpuExceptions(void) {
uint32_t fcr31 = C1_FCR31();
fcr31 &= ~(C1_CAUSE_OVERFLOW | C1_CAUSE_UNDERFLOW | C1_CAUSE_NOT_IMPLEMENTED | C1_CAUSE_INEXACT_OP);
fcr31 |= C1_ENABLE_DIV_BY_0 | C1_ENABLE_INVALID_OP;
fcr31 |= C1_FCR31_FS;
C1_WRITE_FCR31(fcr31);
}
void Platform_Init(void) {
debug_init_isviewer();
debug_init_usblog();
DisableFpuExceptions();
Platform_ReadonlyFilesystem = true;
dfs_init(DFS_DEFAULT_LOCATION);
timer_init();
rtc_init();
}
void Platform_Free(void) { }
cc_bool Platform_DescribeError(cc_result res, cc_string* dst) {
char chars[NATIVE_STR_LEN];
int len;
/* For unrecognised error codes, strerror_r might return messages */
/* such as 'No error information', which is not very useful */
/* (could check errno here but quicker just to skip entirely) */
if (res >= 1000) return false;
len = strerror_r(res, chars, NATIVE_STR_LEN);
if (len == -1) return false;
len = String_CalcLen(chars, NATIVE_STR_LEN);
String_AppendUtf8(dst, chars, len);
return true;
}
cc_bool Process_OpenSupported = false;
cc_result Process_StartOpen(const cc_string* args) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*-------------------------------------------------------Encryption--------------------------------------------------------*
*#########################################################################################################################*/
static cc_result GetMachineID(cc_uint32* key) {
return ERR_NOT_SUPPORTED;
}
#endif
|