Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/src/pc/ultra_reimplementation.c
7857 views
1
#include <stdio.h>
2
#include <string.h>
3
#include <stdlib.h>
4
#include "lib/src/libultra_internal.h"
5
#include "macros.h"
6
7
#ifdef TARGET_WEB
8
#include <emscripten.h>
9
#endif
10
11
#define SAVE_PATH "savedata.bin"
12
13
extern OSMgrArgs piMgrArgs;
14
15
u64 osClockRate = 62500000;
16
17
s32 osPiStartDma(UNUSED OSIoMesg *mb, UNUSED s32 priority, UNUSED s32 direction,
18
uintptr_t devAddr, void *vAddr, size_t nbytes,
19
UNUSED OSMesgQueue *mq) {
20
memcpy(vAddr, (const void *) devAddr, nbytes);
21
return 0;
22
}
23
24
void osCreateMesgQueue(OSMesgQueue *mq, OSMesg *msgBuf, s32 count) {
25
mq->validCount = 0;
26
mq->first = 0;
27
mq->msgCount = count;
28
mq->msg = msgBuf;
29
return;
30
}
31
32
void osSetEventMesg(UNUSED OSEvent e, UNUSED OSMesgQueue *mq, UNUSED OSMesg msg) {
33
}
34
s32 osJamMesg(UNUSED OSMesgQueue *mq, UNUSED OSMesg msg, UNUSED s32 flag) {
35
return 0;
36
}
37
s32 osSendMesg(UNUSED OSMesgQueue *mq, UNUSED OSMesg msg, UNUSED s32 flag) {
38
#if defined(VERSION_EU) || defined(VERSION_SH)
39
s32 index;
40
if (mq->validCount >= mq->msgCount) {
41
return -1;
42
}
43
index = (mq->first + mq->validCount) % mq->msgCount;
44
mq->msg[index] = msg;
45
mq->validCount++;
46
#endif
47
return 0;
48
}
49
s32 osRecvMesg(UNUSED OSMesgQueue *mq, UNUSED OSMesg *msg, UNUSED s32 flag) {
50
#if defined(VERSION_EU) || defined(VERSION_SH)
51
if (mq->validCount == 0) {
52
return -1;
53
}
54
if (msg != NULL) {
55
*msg = *(mq->first + mq->msg);
56
}
57
mq->first = (mq->first + 1) % mq->msgCount;
58
mq->validCount--;
59
#endif
60
return 0;
61
}
62
63
uintptr_t osVirtualToPhysical(void *addr) {
64
return (uintptr_t) addr;
65
}
66
67
void osCreateViManager(UNUSED OSPri pri) {
68
}
69
void osViSetMode(UNUSED OSViMode *mode) {
70
}
71
void osViSetEvent(UNUSED OSMesgQueue *mq, UNUSED OSMesg msg, UNUSED u32 retraceCount) {
72
}
73
void osViBlack(UNUSED u8 active) {
74
}
75
void osViSetSpecialFeatures(UNUSED u32 func) {
76
}
77
void osViSwapBuffer(UNUSED void *vaddr) {
78
}
79
80
OSTime osGetTime(void) {
81
return 0;
82
}
83
84
void osWritebackDCacheAll(void) {
85
}
86
87
void osWritebackDCache(UNUSED void *a, UNUSED size_t b) {
88
}
89
90
void osInvalDCache(UNUSED void *a, UNUSED size_t b) {
91
}
92
93
u32 osGetCount(void) {
94
static u32 counter;
95
return counter++;
96
}
97
98
s32 osAiSetFrequency(u32 freq) {
99
u32 a1;
100
s32 a2;
101
u32 D_8033491C;
102
103
#ifdef VERSION_EU
104
D_8033491C = 0x02E6025C;
105
#else
106
D_8033491C = 0x02E6D354;
107
#endif
108
109
a1 = D_8033491C / (float) freq + .5f;
110
111
if (a1 < 0x84) {
112
return -1;
113
}
114
115
a2 = (a1 / 66) & 0xff;
116
if (a2 > 16) {
117
a2 = 16;
118
}
119
120
return D_8033491C / (s32) a1;
121
}
122
123
s32 osEepromProbe(UNUSED OSMesgQueue *mq) {
124
return 1;
125
}
126
127
s32 osEepromLongRead(UNUSED OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
128
u8 content[512];
129
s32 ret = -1;
130
131
#ifdef TARGET_WEB
132
if (EM_ASM_INT({
133
var s = localStorage.sm64_save_file;
134
if (s && s.length === 684) {
135
try {
136
var binary = atob(s);
137
if (binary.length === 512) {
138
for (var i = 0; i < 512; i++) {
139
HEAPU8[$0 + i] = binary.charCodeAt(i);
140
}
141
return 1;
142
}
143
} catch (e) {
144
}
145
}
146
return 0;
147
}, content)) {
148
memcpy(buffer, content + address * 8, nbytes);
149
ret = 0;
150
}
151
#else
152
FILE *fp = fopen(SAVE_PATH, "rb");
153
if (fp == NULL) {
154
return -1;
155
}
156
if (fread(content, 1, 512, fp) == 512) {
157
memcpy(buffer, content + address * 8, nbytes);
158
ret = 0;
159
}
160
fclose(fp);
161
#endif
162
return ret;
163
}
164
165
s32 osEepromLongWrite(UNUSED OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
166
u8 content[512] = {0};
167
if (address != 0 || nbytes != 512) {
168
osEepromLongRead(mq, 0, content, 512);
169
}
170
memcpy(content + address * 8, buffer, nbytes);
171
172
#ifdef TARGET_WEB
173
EM_ASM({
174
var str = "";
175
for (var i = 0; i < 512; i++) {
176
str += String.fromCharCode(HEAPU8[$0 + i]);
177
}
178
localStorage.sm64_save_file = btoa(str);
179
}, content);
180
s32 ret = 0;
181
#else
182
FILE* fp = fopen(SAVE_PATH, "wb");
183
if (fp == NULL) {
184
return -1;
185
}
186
s32 ret = fwrite(content, 1, 512, fp) == 512 ? 0 : -1;
187
fclose(fp);
188
#endif
189
return ret;
190
}
191
192
s32 gNumVblanks;
193
194
OSPiHandle *osCartRomInit(void) {
195
static OSPiHandle handle;
196
return &handle;
197
}
198
199
OSPiHandle *osDriveRomInit(void) {
200
static OSPiHandle handle;
201
return &handle;
202
}
203
204
s32 osEPiStartDma(UNUSED OSPiHandle *pihandle, OSIoMesg *mb, UNUSED s32 direction) {
205
memcpy(mb->dramAddr, (const void *) mb->devAddr, mb->size);
206
osSendMesg(mb->hdr.retQueue, mb, OS_MESG_NOBLOCK);
207
}
208
209