-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
567 lines (490 loc) · 11.7 KB
/
main.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include <tamtypes.h>
#include <kernel.h>
#include <fileio.h>
#include <string.h>
#include <libpad.h>
#include <loadfile.h>
#include <sifrpc.h>
#include "demo_parts.h"
#include "dernc.h"
#include "harness.h"
#include "stream_ee/streamload.h"
int sound_enabled = 0;
#define DEF_PART "hdd:+PS2MENU"
#define BIN_LOADADDR 0x1000000
u32 *loadptr = (u32 *) BIN_LOADADDR;
typedef u32 (*main_func)(demo_init_t *p);
main_func call_main = (main_func) BIN_LOADADDR;
#define MAX_DEMOS 16
/* Bit of a hack, stops funny mallocs though */
u32 *rnc_buffer = (u32 *) 0x800000;
#define RNC_SIGNATURE 0x01434E52
char hdd_part[64] = DEF_PART;
static u8 padBuf[256] __attribute__((aligned(64)));
void check_pad();
typedef struct _demo_entry
{
char *name;
u32 demo_time;
} demo_entry_t;
#define TH_STACK_SIZE (8 * 1024)
u8 th_stack[TH_STACK_SIZE];
int vb_id;
int th_id;
int th_sema = -1;
int demo_count = 0;
int demo_starttime = 0;
int pad_enabled = 0;
demo_entry_t demos[MAX_DEMOS];
u32 curr_demotime = 10;
u32 set_noprintf = 0;
int palmode;
u16 fft_nodata[1024];
extern int TOTAL_SYNCS;
extern u32 sync_samp[];
demo_init_t init;
int init_gs(int scr_mode);
int is_pal();
u16 *get_fft()
{
if(sound_enabled)
{
return StreamLoad_GetFFT();
}
return fft_nodata;
}
int vblank_handler(int cause)
{
iSignalSema(th_sema);
asm("sync.l ; ei");
return 0;
}
void enable_vblank()
{
DI();
vb_id = AddIntcHandler(2, vblank_handler, 0);
EnableIntc(2);
EI();
}
void disable_vblank()
{
DisableIntc(2);
RemoveIntcHandler(2, vb_id);
}
int update_thread_pal(void *arg)
{
for(;;)
{
WaitSema(th_sema);
init.curr_frame++;
init.frame_count--;
init.time_count -= (1.0f / 50.0f);
init.time_count_i -= (65536 / 50);
init.curr_time += (1.0f / 50.0f);
init.curr_time_i += (65536 / 50);
if(pad_enabled)
{
check_pad();
}
}
}
int update_thread_ntsc(void *arg)
{
for(;;)
{
WaitSema(th_sema);
init.curr_frame++;
init.frame_count--;
init.time_count -= (1.0f / 60.0f);
init.time_count_i -= (65536 / 60);
init.curr_time += (1.0f / 60.0f);
init.curr_time_i += (65536 / 60);
if(pad_enabled)
{
check_pad();
}
}
}
void create_updateth(int scr_mode)
{
extern void *_gp;
ee_thread_t th;
ee_sema_t sema;
sema.init_count = 0;
sema.max_count = 1;
th_sema = CreateSema(&sema);
if(th_sema < 0)
{
printf("Error creating sema\n");
SleepThread();
}
if(scr_mode == SCRMODE_PAL)
{
th.func = update_thread_pal;
}
else
{
th.func = update_thread_ntsc;
}
th.stack = th_stack;
th.stack_size = TH_STACK_SIZE;
th.gp_reg = _gp;
th.initial_priority = 42;
if((th_id = CreateThread(&th)) < 0)
{
printf("Error creating thread\n");
SleepThread();
}
StartThread(th_id, NULL);
}
int dummy()
{
return 0;
}
int get_pos_nosound()
{
return (int) (init.curr_time * 48000.0f);
}
void reset_init()
{
if(set_noprintf)
{
init.printf = (printf_t) dummy;
}
else
{
init.printf = printf;
}
init.get_fft = get_fft;
if(sound_enabled)
init.get_pos = StreamLoad_Position;
else
init.get_pos = get_pos_nosound;
init.curr_frame = 0;
init.frame_count = 0;
init.time_count = 0;
init.time_count_i = 0;
init.curr_time = 0;
init.curr_time_i = 0;
init.sync_points = NULL;
init.no_syncs = 0;
}
void print_usage()
{
printf("Demo Harness Usage:\n");
printf("harness.elf <options> <demos>\n");
printf("Options:\n");
printf("-pal : Set pal mode\n");
printf("-ntsc : Set NTSC mode\n");
printf("-sound : Enable Sound\n");
printf("-tX : Time in seconds to run each demo\n");
printf("-sX : Time in seconds to start tune at\n");
printf("-dX : Starts the demo at a number from sync_list.txt\n");
printf("-noprintf : Disables the printf function\n");
printf("-mPART : Specifies the partition to mount for sound files\n");
printf("-p : Enables pad support\n");
printf("Demos:\n");
printf("List of host files to run.\n");
}
int process_args(int argc, char **argv)
{
int arg_loop;
if(argc < 2)
{
return -1;
}
for(arg_loop = 1; arg_loop < argc; arg_loop++)
{
if(argv[arg_loop][0] == '-')
{
if(strcmp("pal", &argv[arg_loop][1]) == 0)
{
init.screen_mode = SCRMODE_PAL;
palmode = 1;
printf("Set PAL mode\n");
}
else if(strcmp("ntsc", &argv[arg_loop][1]) == 0)
{
init.screen_mode = SCRMODE_NTSC;
palmode = 0;
printf("Set NTSC mode\n");
}
else if(strcmp("noprintf", &argv[arg_loop][1]) == 0)
{
set_noprintf = 1;
printf("Disabling printf\n");
}
else if(strcmp("sound", &argv[arg_loop][1]) == 0)
{
sound_enabled = 1;
printf("Enable Sound\n");
}
else if(argv[arg_loop][1] == 'm')
{
if(strlen(argv[arg_loop]) > 2)
{
strcpy(hdd_part, &argv[arg_loop][2]);
printf("Set HDD Partition %s\n", hdd_part);
}
else
{
printf("Invalid argument %s\n", argv[arg_loop]);
return -1;
}
}
else if(argv[arg_loop][1] == 'p')
{
pad_enabled = 1;
printf("Enable pad\n");
}
else if(argv[arg_loop][1] == 's')
{
if((argv[arg_loop][2] >= '0') && (argv[arg_loop][2] <= '9'))
{
char *endp;
demo_starttime = strtol(&argv[arg_loop][2], &endp, 10);
demo_starttime *= 48000;
printf("Set start time %d\n", demo_starttime);
}
else
{
printf("Invalid argument %s\n", argv[arg_loop]);
return -1;
}
}
else if(argv[arg_loop][1] == 't')
{
if((argv[arg_loop][2] >= '0') && (argv[arg_loop][2] <= '9'))
{
char *endp;
curr_demotime = strtol(&argv[arg_loop][2], &endp, 10);
printf("Set time %d\n", curr_demotime);
}
else
{
printf("Invalid argument %s\n", argv[arg_loop]);
return -1;
}
}
else if(argv[arg_loop][1] == 'd')
{
if((argv[arg_loop][2] >= '0') && (argv[arg_loop][2] <= '9'))
{
char *endp;
int part;
part = strtol(&argv[arg_loop][2], &endp, 10);
if(part < MAX_PARTS)
{
demo_starttime = demo_parts[part].start;
curr_demotime = demo_parts[part].time;
printf("Set Demo Part %d start=%d time=%d\n", part, demo_starttime, curr_demotime);
}
else
{
printf("Invalid part number %d\n", part);
return -1;
}
}
else
{
printf("Invalid argument %s\n", argv[arg_loop]);
return -1;
}
}
else
{
printf("Invalid argument %s\n", argv[arg_loop]);
return -1;
}
}
else
{
if((argv[arg_loop] != 0) && (demo_count < MAX_DEMOS))
{
demos[demo_count].demo_time = curr_demotime;
demos[demo_count++].name = argv[arg_loop];
printf("Adding %s\n", argv[arg_loop]);
}
else
{
printf("Invalid file name %s\n", argv[arg_loop]);
return -1;
}
}
}
if(demo_count == 0)
{
return -1;
}
return 0;
}
void check_pad()
{
int ret;
u32 paddata;
static u32 oldpad = 0;
u32 newpad;
struct padButtonStatus buttons;
ret = padGetState(0, 0);
if(ret == PAD_STATE_STABLE)
{
ret = padRead(0, 0, &buttons);
if(ret != 0)
{
paddata = 0xFFFF ^ ((buttons.btns[0] << 8) | buttons.btns[1]);
newpad = paddata & ~oldpad;
oldpad = paddata;
if(newpad & PAD_CROSS)
{
printf("Stream Position = %d\n", StreamLoad_Position());
}
}
}
}
void setup_pad()
{
int ret;
SifLoadModule("rom0:SIO2MAN", 0, NULL);
SifLoadModule("rom0:PADMAN", 0, NULL);
padInit(0);
if((ret = padPortOpen(0, 0, padBuf)) == 0)
{
printf("Pad open failed. Disabling pad support ret=%d\n", ret);
pad_enabled = 0;
return;
}
while((ret = padGetState(0, 0)) != PAD_STATE_STABLE)
{
}
}
void setup_syncs(u32 time)
{
u32 start, end;
int loop;
if(sound_enabled)
start = StreamLoad_Position();
else
start = (int) init.curr_time;
end = start + time * 48000;
for(loop = 0; loop < TOTAL_SYNCS; loop++)
{
if(sync_samp[loop] > start)
break;
}
if(loop == TOTAL_SYNCS)
{
init.sync_points = sync_samp;
init.no_syncs = 0;
return;
}
init.sync_points = &sync_samp[loop];
for(loop += 1; loop < TOTAL_SYNCS; loop++)
{
if(sync_samp[loop] > end)
{
loop -= 1;
break;
}
}
if(loop == TOTAL_SYNCS)
{
loop--;
}
init.no_syncs = &sync_samp[loop] - init.sync_points + 1;
}
int main(int argc, char **argv)
{
int fd;
int demo_loop;
SifInitRpc(0);
if(is_pal())
{
init.screen_mode = SCRMODE_PAL;
palmode = 1;
}
else
{
init.screen_mode = SCRMODE_NTSC;
palmode = 0;
}
if(process_args(argc, argv) < 0)
{
print_usage();
SleepThread();
}
ResetEE(0xFF);
init_gs(init.screen_mode);
reset_init();
create_updateth(init.screen_mode);
if(pad_enabled)
{
setup_pad();
}
if(sound_enabled)
{
StreamLoad_Init(0,hdd_part,palmode);
// StreamLoad_SetupTune("HALFDEAPH");// hdd:+PS2MENU/HALFDEAPHL.RAW AND HALFDEAPHR.RAW
StreamLoad_SetupTune("UNSEEN"); // hdd:+PS2MENU/UNSEENL.RAW AND UNSEENR.RAW
StreamLoad_SetPosition(demo_starttime);
//StreamLoad_Play(0x3fff);
}
//if(sound_enabled)
//{
// u16 *ptr = StreamLoad_GetFFT();
//
// printf("%04X, %04X\n", ptr[0], ptr[1023]);
// }
for(demo_loop = 0; demo_loop < demo_count; demo_loop++)
{
fd = fioOpen(demos[demo_loop].name, O_RDONLY);
if(fd < 0)
{
printf("Couldn't open file %s\n", demos[demo_loop].name);
}
else
{
u32 len;
u32 ret;
u32 sig;
len = fioLseek(fd, 0, SEEK_END);
fioLseek(fd, 0, SEEK_SET);
fioRead(fd, &sig, 4);
if(sig == RNC_SIGNATURE)
{
printf("Load RNCed file\n");
rnc_buffer[0] = sig;
fioRead(fd, &rnc_buffer[1], len - 4);
rnc_unpack(rnc_buffer, loadptr);
}
else
{
loadptr[0] = sig;
fioRead(fd, &loadptr[1], len - 4);
}
fioClose(fd);
if(init.screen_mode == SCRMODE_PAL)
{
init.frame_count = demos[demo_loop].demo_time * 50;
}
else
{
init.frame_count = demos[demo_loop].demo_time * 60;
}
init.time_count = (float) demos[demo_loop].demo_time;
init.time_count_i = (demos[demo_loop].demo_time << 16);
init.curr_time = (float) demo_starttime / 48000.0f;
setup_syncs(demos[demo_loop].demo_time);
if(sound_enabled)
StreamLoad_Play(0x3fff);
ResetEE(0xFF);
enable_vblank();
FlushCache(0);
FlushCache(2);
ret = call_main(&init);
printf("%s ret=%d\n", demos[demo_loop].name, ret);
disable_vblank();
}
}
SleepThread();
return 0;
}