-
Notifications
You must be signed in to change notification settings - Fork 3
/
SharedFunctions.c
274 lines (216 loc) · 5.24 KB
/
SharedFunctions.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
#include "MainInclude.h"
void runDMA()
{
DMA(*targetAddress, *sourceAddress, dataLength);
}
void runRAM()
{
ramCopy(*targetAddress, *sourceAddress, dataLength);
}
void runMIO()
{
decodeMIO0(*sourceAddress, *targetAddress);
}
void runTKM()
{
decodeTKMK(*sourceAddress, tkmPoint, *targetAddress, 0xBE);
}
void loadEEPROM(uint Destination)
{
osEepromLongRead((void*)(0x8014F0B8), 0, (uchar*)(Destination), 512);
}
void saveEEPROM(uint Source)
{
osEepromLongWrite((void*)(0x8014F0B8), 0, (uchar*)Source, 512);
}
ushort GetRGBA16(int R, int G, int B, int A)
{
return (R & 0x1F)<<11 | (G & 0x1F)<<6 | (B & 0x1F)<<1 | (A & 0x01);
}
uint GetRGB32(int R, int G, int B)
{
return ( (R << 16) | (G << 8) | (B));
}
void SetFontColor(int FontR, int FontG, int FontB, int ShadowR, int ShadowG, int ShadowB)
{
ushort *ColorValues = (ushort*)&g_DebugTextPalette;
ColorValues[0] = 0; //0000 transparent
ColorValues[1] = GetRGBA16(FontR,FontG,FontB,1);
ColorValues[2] = GetRGBA16(ShadowR,ShadowG,ShadowB,1);
ColorValues[3] = 1; //0001 black
}
int LoadData (int SourceInput, int SizeData)
{
*sourceAddress = SourceInput;
*targetAddress = FreeSpaceAddress;
dataLength = SizeData;
GlobalShortA = 16 - (SizeData % 16);
FreeSpaceAddress = FreeSpaceAddress + SizeData + GlobalShortA;
runDMA();
return *targetAddress;
}
/* Get Status Timer */
uint getStarTimer(uchar playerID)
{
return (int)g_gameTimer-g_StarUseCounter[playerID];
}
uint getBooTimer(uchar playerID)
{
return (int)g_gameTimer-g_GhostUseCounter[playerID];
}
bool CheckPlatform()
{
// This is an abuse of the memory quirks between Console and Emulator.
// We're unsure of yet the exact cause
// Emulators have extremely fast memory access and no latency, that may be the cause.
// Either way, this can detect if a legitimate console is running and return TRUE if so.
*targetAddress = 0x80744000;
*sourceAddress = 0x30;
dataLength = 0xC;
runDMA();
*targetAddress = 0x8074400C;
*sourceAddress = *sourceAddress + 0xC;
runDMA();
if (*(long*)(0x80744014) == 0x40804800)
{
return false; //EMULATOR
}
else
{
return true; //CONSOLE
}
}
bool CheckEmulator()
{
// This is an abuse of the memory quirks between Console and Emulator.
// We're unsure of yet the exact cause
// Emulators have extremely fast memory access and no latency, that may be the cause.
// Either way, this can detect if a legitimate console is running and return TRUE if so.
*sourceAddress = 0xBFFFFC;
*targetAddress = 0x80744000;
dataLength = 8;
runDMA();
*sourceAddress = *(uint*)0x80744000;
*targetAddress = 0x80744010;
dataLength = 0x8;
runDMA();
if (*(int*)(0x80744010) == 0)
{
return false; //MUPEN
}
else
{
return true; //PROJECT64
}
}
void loadNiceFont()
{
*sourceAddress = (int)(&NiceFontROM);
*targetAddress = (int)(&ok_FreeSpace);
dataLength = 0x4400;
runDMA();
*sourceAddress = (int)(&ok_FreeSpace);
*targetAddress = (int)(&nicefont);
runMIO();
}
void ShiftVertColor(uint address,uint counter,uint v_buff,char alpha,char red,char green,char blue)
{
uint number= SegmentNumber(address);
uint offset= SegmentOffset(address);
Vtx_t *vt=(Vtx_t *)GetRealAddress(SegmentTable[number]+offset);
int i;
for(i=0;i<counter;i++)
{
vt->cn[0]+=red;
vt->cn[1]+=green;
vt->cn[2]+=blue;
vt->cn[3]+=alpha;
++vt;
}
}
int GetRealAddress(int RSPAddress)
{
RSPNumber = SegmentNumber(RSPAddress);
RSPOffset = SegmentOffset(RSPAddress);
return(PhysToK0(SegmentTable[RSPNumber] + RSPOffset));
}
void DrawBox(int X, int Y, int SizeX, int SizeY, int R, int G, int B, int A)
{
GraphPtr = FillRect1ColorF(GraphPtr, X, Y, X + SizeX, Y + SizeY, R, G, B, A);
}
void printFloat(int X, int Y, float Value)
{
int PrintOffset;
wholeNumber = (int) Value;
decimalNumber = (int) ((Value - wholeNumber) * 100);
PrintOffset = 8;
GlobalIntA = wholeNumber;
if (GlobalIntA < 0)
{
GlobalIntA *= -1;
}
if (GlobalIntA > 10)
{
PrintOffset = 16;
if (GlobalIntA > 100)
{
PrintOffset = 24;
if (GlobalIntA > 1000)
{
PrintOffset = 32;
}
}
}
if (decimalNumber < 0)
{
decimalNumber *= -1;
}
loadFont();
if (wholeNumber < 0)
{
X -= 8;
PrintOffset += 8;
}
printStringNumber(X,Y,"",wholeNumber);
printString(X + PrintOffset + 10, Y, ".");
if (decimalNumber < 10)
{
printStringNumber(X+PrintOffset + 8,Y,"",0);
PrintOffset += 8;
}
printStringNumber(X+PrintOffset + 8,Y,"",decimalNumber);
}
void ResetObject()
{
/*
for(int thisLoop = 0; thisLoop < 4; thisLoop++)
{
for(int subLoop = 0; subLoop < 4; subLoop++)
{
AffineMatrix[thisLoop][subLoop] = 0;
}
}
*/
for(int thisLoop = 0; thisLoop < 3; thisLoop++)
{
objectPosition[thisLoop] = 0;
objectAngle[thisLoop] = 0;
objectVelocity[thisLoop] = 0;
}
}
char* printHex(char *buf, int num, int nDigits) {
//print hex number into buffer.
//will zero-pad to specified number of digits.
//will truncate numbers larger than specified length.
//returns pointer to null terminator.
char *bufEnd = &buf[nDigits];
*bufEnd = 0;
while(nDigits--) {
buf[nDigits] = hex[num & 0xF];
num >>= 4;
}
return bufEnd;
}
//x,0,1,2,3,4,5,6,7
char CharacterConvert[9] = {-1,0,1,6,3,2,4,5,7};
char CharacterUnconvert[9] = {-1,0,1,4,3,5,6,2,7};