-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusf.c
413 lines (341 loc) · 9.45 KB
/
usf.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "main.h"
#include "audio.h"
#include "psftag.h"
#include "cpu.h"
#include "memory.h"
#include "usf.h"
uint8_t cpu_running = 0;
uint8_t use_audiohle = 0;
uint8_t fake_seek_stopping = 0;
// Fade Type: 1: linear; 2: logarithmic; 3: half of a sin wave
uint8_t fade_type = 0;
uint32_t fade_time = 10000;
// Round Frequency - if 1: changes playback frequency to a more standard value, rather than the odd values that games use
int8_t round_frequency = 0;
// default track time: 360000 ms = 360 s = 6 min
// if MSB set: play forever
uint32_t track_time = 360000;
double play_time = 0.0;
// initialise with -1, indicating that vars have not been set
int8_t enablecompare = -1;
int8_t enableFIFOfull = -1;
/* METADATA VARs */
char title[100]="UNKNOWNTITLE";
char genre[100]="UNKNOWNGENRE";
char artist[100]="UNKNOWNARTIST";
char copyright[100]="UNKNOWNCOPYRIGHT";
char game[100]="UNKNOWNCOPYRIGHT";
char year[25]="UNKNOWNYEAR";
extern int32_t RSP_Cpu;
uint32_t get_length_from_string(uint8_t * str_length)
{
uint32_t ttime = 0, temp = 0, mult = 1, level = 1;
char Source[1024];
char * src = Source + strlen((char*)str_length);
strcpy(&Source[1], (char*)str_length);
Source[0] = 0;
while(*src)
{
if((*src >= '0') && (*src <= '9'))
{
temp += ((*src - '0') * mult);
mult *= 10;
}
else
{
mult = 1;
if(*src == '.')
{
ttime = temp;
temp = 0;
}
else if(*src == ':')
{
ttime += (temp * (1000 * level));
temp = 0;
level *= 60;
}
}
src--;
}
ttime += (temp * (1000 * level));
return ttime;
}
int LoadUSF(const char * fn)
{
FILE * fil = NULL;
uint32_t reservedsize = 0, codesize = 0, crc = 0, tagstart = 0, reservestart = 0;
uint32_t filesize = 0, tagsize = 0, temp = 0;
char buffer[16], * buffer2 = NULL, * tagbuffer = NULL;
play_time = 0;
fil = fopen(fn, "rb");
if(!fil)
{
printf("Could not open USF!\n");
return 0;
}
fread(buffer,4 ,1 ,fil);
if(buffer[0] != 'P' && buffer[1] != 'S' && buffer[2] != 'F' && buffer[3] != 0x21)
{
printf("Invalid header in file!\n");
fclose(fil);
return 0;
}
fread(&reservedsize, 4, 1, fil);
fread(&codesize, 4, 1, fil);
fread(&crc, 4, 1, fil);
fseek(fil, 0, SEEK_END);
filesize = ftell(fil);
reservestart = 0x10;
tagstart = reservestart + reservedsize;
tagsize = filesize - tagstart;
if(tagsize)
{
fseek(fil, tagstart, SEEK_SET);
fread(buffer, 5, 1, fil);
if(buffer[0] != '[' && buffer[1] != 'T' && buffer[2] != 'A' && buffer[3] != 'G' && buffer[4] != ']')
{
printf("Errornous data in tag area! %ld\n", (long int)tagsize);
fclose(fil);
return 0;
}
buffer2 = malloc(50001);
tagbuffer = malloc(tagsize);
fread(tagbuffer, tagsize, 1, fil);
psftag_raw_getvar(tagbuffer,"_lib",buffer2,50000);
if(strlen(buffer2))
{
char path[512];
int pathlength = 0;
if(strrchr(fn, '/')) //linux
{
pathlength = strrchr(fn, '/') - fn + 1;
}
else if(strrchr(fn, '\\')) //windows
{
pathlength = strrchr(fn, '\\') - fn + 1;
}
else //no path
{
pathlength = strlen(fn);
}
strncpy(path, fn, pathlength);
path[pathlength] = 0;
strcat(path, buffer2);
// load the usflib
LoadUSF(path);
}
psftag_raw_getvar(tagbuffer,"_enablecompare",buffer2,50000);
if(enablecompare == -1) // check if this has already set
{
if(strlen(buffer2))
{
enablecompare = 1;
}
else
{
enablecompare = 0;
}
}
psftag_raw_getvar(tagbuffer,"_enableFIFOfull",buffer2,50000);
if(enableFIFOfull == -1)
{
if(strlen(buffer2))
{
enableFIFOfull = 1;
}
else
{
enableFIFOfull = 0;
}
}
psftag_raw_getvar(tagbuffer, "length", buffer2, 50000);
if(strlen(buffer2))
{
track_time = get_length_from_string((uint8_t*)buffer2);
}
psftag_raw_getvar(tagbuffer, "fade", buffer2, 50000);
if(strlen(buffer2))
{
fade_time = get_length_from_string((uint8_t*)buffer2);
}
psftag_raw_getvar(tagbuffer, "title", buffer2, 50000);
if(strlen(buffer2))
{
strcpy(title, buffer2);
}
else
{
int pathlength = 0;
if(strrchr(fn, '/')) //linux
{
pathlength = strrchr(fn, '/') - fn + 1;
}
else if(strrchr(fn, '\\')) //windows
{
pathlength = strrchr(fn, '\\') - fn + 1;
}
else //no path
{
pathlength = 7;
}
strcpy(title, &fn[pathlength]);
}
psftag_raw_getvar(tagbuffer, "game", buffer2, 50000);
if(strlen(buffer2))
{
strcpy(game, buffer2);
}
psftag_raw_getvar(tagbuffer, "copyright", buffer2, 50000);
if(strlen(buffer2))
{
strcpy(copyright, buffer2);
}
psftag_raw_getvar(tagbuffer, "artist", buffer2, 50000);
if(strlen(buffer2))
{
strcpy(artist, buffer2);
}
psftag_raw_getvar(tagbuffer, "genre", buffer2, 50000);
if(strlen(buffer2))
{
strcpy(genre, buffer2);
}
psftag_raw_getvar(tagbuffer, "year", buffer2, 50000);
if(strlen(buffer2))
{
strcpy(year, buffer2);
}
free(buffer2);
buffer2 = NULL;
free(tagbuffer);
tagbuffer = NULL;
}
fseek(fil, reservestart, SEEK_SET);
fread(&temp, 4, 1, fil);
if(temp == 0x34365253) //there is a rom section
{
int len = 0, start = 0;
fread(&len, 4, 1, fil);
while(len)
{
fread(&start, 4, 1, fil);
while(len)
{
int page = start >> 16;
int readLen = ( ((start + len) >> 16) > page) ? (((page + 1) << 16) - start) : len;
if(ROMPages[page] == 0)
{
ROMPages[page] = malloc(0x10000);
memset(ROMPages[page], 0, 0x10000);
}
fread(ROMPages[page] + (start & 0xffff), readLen, 1, fil);
start += readLen;
len -= readLen;
}
fread(&len, 4, 1, fil);
}
}
fread(&temp, 4, 1, fil);
if(temp == 0x34365253)
{
int len = 0, start = 0;
fread(&len, 4, 1, fil);
while(len)
{
fread(&start, 4, 1, fil);
fread(savestatespace + start, len, 1, fil);
fread(&len, 4, 1, fil);
}
}
// Detect the Ramsize before the memory allocation
if(*(uint32_t*)(savestatespace + 4) == 0x400000)
{
RdramSize = 0x400000;
savestatespace = realloc(savestatespace, 0x40275c);
}
else if(*(uint32_t*)(savestatespace + 4) == 0x800000)
{
RdramSize = 0x800000;
}
fclose(fil);
return 1;
}
bool usf_init(char fn[])
{
if(!fn)
{
return false;
}
// Defaults (which would be overriden by Tags / playing
savestatespace = NULL;
cpu_running = fake_seek_stopping = 0;
play_time = 0.0;
// Allocate main memory after usf loads (to determine ram size)
if (PreAllocate_Memory())
{
if(!LoadUSF(fn))
{
Release_Memory();
return false;
}
// in case they are still unset, e.g. both files miniusf and usflib have no tag area, set them to 0 (default)
if(enablecompare == -1 || enableFIFOfull == -1)
{
enablecompare = 0;
enableFIFOfull = 0;
}
return true;
}
return false;
}
bool usf_play()
{
if(Allocate_Memory() != 0 )
{
while(1)
{
play_time = 0;
printf("Start Emulation using ");
if(CPU_Type==CPU_Interpreter && RSP_Cpu==CPU_Interpreter)
{
printf("Interpreter\n");
}
else if (CPU_Type==CPU_Recompiler && RSP_Cpu==CPU_Recompiler)
{
printf("Recompiler\n");
}
else
{
printf("different or unknown Main-CPU-Type and RSP-CPU-Type:\n");
printf("Main-CPU-Type: %d\n", CPU_Type);
printf("RSP-CPU-Type : %d\n", RSP_Cpu);
}
StartEmulationFromSave(savestatespace);
if(!fake_seek_stopping)
{
break;
}
while(fake_seek_stopping != 2)
{
usleep(1);
}
fake_seek_stopping = 4;
}
}
else
{
return false;
}
Release_Memory();
return true;
}
double usf_get_time()
{
return play_time;
}