forked from lukaszdk/ps2doom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_main.c
624 lines (535 loc) · 17.5 KB
/
i_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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
// $Log:$
//
// DESCRIPTION:
// Main program, simply calls D_DoomMain high level loop.
//
//-----------------------------------------------------------------------------
static const char
rcsid[] = "$Id: i_main.c,v 1.4 1997/02/03 22:45:10 b1 Exp $";
#include "SDL.h"
#include "doomdef.h"
#include "m_argv.h"
#include "libconfig.h"
#include "d_main.h"
#include <sifrpc.h>
//#ifdef PS2HDD
#include <fileio.h>
#include <fileXio.h>
#include <debug.h>
#include <libhdd.h>
#include <libpwroff.h>
#include <fileXio_rpc.h>
#include <loadfile.h>
#include <tamtypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_PARTITIONS 100
//#endif
//Declare usbd module //
extern unsigned char usbd[];
extern unsigned int size_usbd;
//Declare usbhdfsd module //
extern unsigned char usbhdfsd[];
extern unsigned int size_usbhdfsd;
extern unsigned char SJPCM[];
extern unsigned int size_SJPCM;
extern unsigned char freesd[];
extern unsigned int size_freesd;
//#ifdef PS2HDD
/*Declare iomanX module*/
extern unsigned char iomanX[];
extern unsigned int size_iomanX;
/*Declare fileXio module*/
extern unsigned char fileXio[];
extern unsigned int size_fileXio;
/*Declare ps2dev9 module*/
extern unsigned char ps2dev9[];
extern unsigned int size_ps2dev9;
/*Declare ps2atad module*/
extern unsigned char ps2atad[];
extern unsigned int size_ps2atad;
/*Declare ps2hdd module*/
extern unsigned char ps2hdd[];
extern unsigned int size_ps2hdd;
/*Declare ps2fsmodule*/
extern unsigned char ps2fs[];
extern unsigned int size_ps2fs;
/*Declare poweroff module*/
extern unsigned char poweroff[];
extern unsigned int size_poweroff;
/*Declare cdvd module*/
extern unsigned char cdvd[];
extern unsigned int size_cdvd;
//#endif
static char s_pUDNL [] __attribute__( ( section( ".data" ), aligned( 1 ) ) ) = "rom0:UDNL rom0:EELOADCNF";
char elfFilename[100];
char deviceName[10];
char fullPath[256];
int use_hdd;
int swap_analogsticks;
#include "sjpcm.h"
// cosmitoMixer
#include <sifrpc.h>
#include "mixer.h"
#include "mixer_thread.h"
#include <kernel.h> //for GetThreadId
#include <libmc.h>
#include "cosmitoFileIO.h"
extern int SAMPLECOUNT;
int getDisplayModeFromELFName(char **argv);
/// ------------------------- por em .h
//typedef enum
//{
// square,
// cross,
// circle,
// triangle,
// select,
// start,
// l1,
// r1,
// l2,
// r2,
// l3,
// r3,
// analog1left,
// analog1right,
// analog2left,
// analog2right
//
//} config_buttons;
/// these two are pairs
typedef struct
{
char* name;
char* defaultaction;
} config_buttons_element;
config_buttons_element config_buttons[] =
{
// button name, default action
// both button names and action names as appearing at the libconfig config file
{"square", "previous weapon"},
{"cross", "run"},
{"circle", "next weapon"},
{"triangle", "escape"},
{"select", "map"},
{"start", "enter"},
{"l1", "n"},
{"r1", "fire"},
{"l2", "y"},
{"r2", "open"},
{"l3", ""},
{"r3", "gamma"},
{"dpadleft", "leftarrow"},
{"dpadright", "rightarrow"},
{"dpadup", "uparrow"},
{"dpaddown", "downarrow"},
{"analog1left", ""}, /// TBD : yet not used
{"analog1right", ""}, /// TBD : yet not used
{"analog2left", "strafe left"}, // index 18
{"analog2right", "strafe right"}
};
// Store place to be built at config loading. Have the key codes for each button.
int config_buttons_int[] =
{
//-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0xac, 0xae, 0xad, 0xaf, -1, -1, -1, -1
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
// KEY_RIGHTARROW 0xae
// KEY_LEFTARROW 0xac
// KEY_UPARROW 0xad
// KEY_DOWNARROW 0xaf
///
typedef struct
{
char* name;
int value;
} config_actions_element;
config_actions_element config_actions[] =
{
// actually the key mapping for the actions :
// action name, mapped key
{"previous weapon", SDLK_o},
{"run", KEY_RSHIFT},
{"next weapon", SDLK_p},
{"escape", KEY_ESCAPE},
{"map", KEY_TAB},
{"enter", KEY_ENTER},
{"n", SDLK_n},
{"fire", KEY_RCTRL},
{"y", SDLK_y},
{"open", SDLK_SPACE},
{"gamma", KEY_F11},
{"leftarrow", KEY_LEFTARROW},
{"rightarrow", KEY_RIGHTARROW},
{"uparrow", KEY_UPARROW},
{"downarrow", KEY_DOWNARROW},
{"strafe left", STRAFELEFT},
{"strafe right", STRAFERIGHT},
/// to add a new : add entry here with default
};
char config_probestring[200];
/// -------------------------
#define DEBUG_LIBCONFIG
const char *hdd_wads_folder;
int main( int argc, char** argv )
{
FILE *fp;
int i, j, nj;
const char *s;
char configfile[256];
config_t cfg; // libconfig
const char *hdd_path_to_partition;
use_hdd = CONFIG_FALSE;
GetElfFilename(argv[0], deviceName, fullPath, elfFilename);
int forceDisplayMode = -1;
myargc = argc;
myargv = argv;
s32 main_thread_id;
main_thread_id = GetThreadId ();
SifInitRpc(0);
init_scr();
scr_printf_nocursor(" --==== PS2DOOM v1.0.5.0 ====--\n\n\n");
int ret;
printf("sample: kicking IRXs\n");
//ret = SifLoadModule("rom0:LIBSD", 0, NULL);
ret = SifExecModuleBuffer(freesd, size_freesd, 0, NULL, &ret);
printf("freesd loadmodule %d\n", ret);
/*
SifInitRpc(0);
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();
SifResetIop();
SifIopReset(s_pUDNL, 0);
SifInitRpc(0);
*/
//Load embedded modules
// SJPCM
SifExecModuleBuffer(SJPCM, size_SJPCM, 0, NULL, &ret);
//#ifdef PS2HDD
// SifExecModuleBuffer(poweroff, size_poweroff, 0, NULL, &ret);
// SifExecModuleBuffer(iomanX, size_iomanX, 0, NULL, &ret);
// SifExecModuleBuffer(fileXio, size_fileXio, 0, NULL, &ret);
// SifExecModuleBuffer(ps2dev9, size_ps2dev9, 0, NULL, &ret);
// SifExecModuleBuffer(ps2atad, size_ps2atad, 0, NULL, &ret);
// static char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
// SifExecModuleBuffer(ps2hdd, size_ps2hdd, sizeof(hddarg), hddarg, &ret);
// if (ret < 0)
// {
// printf("Failed to load module: PS2HDD.IRX");
// scr_printf("Failed to load module: PS2HDD.IRX");
// }
// static char pfsarg[] = "-m" "\0" "4" "\0" "-o" "\0" "10" "\0" "-n" "\0" "40";
// SifExecModuleBuffer(ps2fs, size_ps2fs, sizeof(pfsarg), pfsarg, &ret);
// if (ret < 0)
// {
// scr_printf("Failed to load module: PS2FS.IRX");
// printf("Failed to load module: PS2FS.IRX");
// }
//#endif
// USB mass support
SifExecModuleBuffer(usbd, size_usbd, 0, NULL, &ret);
SifExecModuleBuffer(usbhdfsd, size_usbhdfsd, 0, NULL, &ret);
// MC support (from ps2sdk mc_example.c)
int mc_Type, mc_Free, mc_Format;
ret = SifLoadModule("rom0:XSIO2MAN", 0, NULL);
if (ret < 0) {
printf("Failed to load module: SIO2MAN");
scr_printf("Failed to load module: SIO2MAN");
SleepThread();
}
ret = SifLoadModule("rom0:XMCMAN", 0, NULL);
if (ret < 0) {
printf("Failed to load module: MCMAN");
scr_printf("Failed to load module: MCMAN");
SleepThread();
}
ret = SifLoadModule("rom0:XMCSERV", 0, NULL);
if (ret < 0) {
printf("Failed to load module: MCSERV");
SleepThread();
}
if(mcInit(MC_TYPE_XMC) < 0) {
printf("Failed to initialise memcard server!\n");
SleepThread();
}
// Since this is the first call, -1 should be returned.
mcGetInfo(0, 0, &mc_Type, &mc_Free, &mc_Format);
mcSync(0, NULL, &ret);
printf("mcGetInfo returned %d\n",ret);
printf("Type: %d Free: %d Format: %d\n\n", mc_Type, mc_Free, mc_Format);
// Assuming that the same memory card is connected, this should return 0
mcGetInfo(0,0,&mc_Type,&mc_Free,&mc_Format);
mcSync(0, NULL, &ret);
printf("mcGetInfo returned %d\n",ret);
printf("Type: %d Free: %d Format: %d\n\n", mc_Type, mc_Free, mc_Format);
if (ret != 0)
printf("mc0 trouble... should save to other device... To implement\n"); /// TBD
// create save/load dir (mc0:PS2DOOM)
int handle = fioOpen ("mc0:PS2DOOM/doomsav0.dsg", O_RDONLY);
if (handle < 0)
{
fioMkdir("mc0:PS2DOOM"); // Make sure it exists
printf(" ... created mc0:PS2DOOM ...\n");
}
else
fioClose(handle);
/// config
sprintf(configfile, "%s%s", fullPath, "ps2doom.config");
// First, try to load from localpath. If fails, try from 'mc0:'
int configLoadSuccess = 0;
fp = fopen(configfile, "rb");
if(!fp)
{
printf("file '%s' not found. Going to try 'mc0:PS2DOOM/ps2doom.config'\n", configfile);
sprintf(configfile, "%s", "mc0:PS2DOOM/ps2doom.config");
fp = fopen(configfile, "rb");
if(!fp)
{
// Using default actions for buttons
#ifdef DEBUG_LIBCONFIG
printf("Unable to open '%s'. Using defaults\n", configfile);
#endif
int nConfig_buttonsEntries = sizeof(config_buttons)/sizeof(config_buttons[0]);
for (i=0; i<nConfig_buttonsEntries; i++)
{
nj = sizeof(config_actions)/sizeof(config_actions[0]);
for(j=0; j<nj; j++)
{
if(strcmp(config_actions[j].name, config_buttons[i].defaultaction) == 0)
{
int value = config_actions[j].value;
config_buttons_int[i] = value;
}
}
}
}
else
configLoadSuccess = 1;
}
else
configLoadSuccess = 1;
if(configLoadSuccess == 1)
{
// Get the actions for buttons from config
int x;
config_init(&cfg);
x = config_read(&cfg, fp);
fclose(fp);
if(x)
{
// Process each ps2doom.controls config entries
int nConfigEntries = sizeof(config_buttons)/sizeof(config_buttons[0]);
for (i=0; i<nConfigEntries; i++)
{
sprintf(config_probestring, "%s%s", "ps2doom.controls.", config_buttons[i].name);
if(! config_lookup_string(&cfg, config_probestring, &s))
{
#ifdef DEBUG_LIBCONFIG
printf("NOT FOUND %s\n", config_probestring);
#endif
}
else
{
#ifdef DEBUG_LIBCONFIG
printf("found: %s = %s\n", config_probestring, s);
#endif
nj = sizeof(config_actions)/sizeof(config_actions[0]);
for(j=0; j<nj; j++)
{
if(strcmp(config_actions[j].name, s) == 0)
{
int value = config_actions[j].value;
config_buttons_int[i] = value;
}
}
}
}
}
else
printf("error on line %d: %s\n", cfg.error_line, cfg.error_text);
use_hdd = CONFIG_FALSE;
sprintf(config_probestring, "%s", "ps2doom.hdd.use_hdd");
if(!config_lookup_bool(&cfg, config_probestring, &use_hdd))
{
use_hdd = CONFIG_FALSE;
printf("NOT FOUND %s\n", config_probestring);
}
else
{
printf("found: %s = %d\n", config_probestring, use_hdd);
}
if(use_hdd == CONFIG_TRUE)
{
sprintf(config_probestring, "%s", "ps2doom.hdd.path_to_partition");
if(!config_lookup_string(&cfg, config_probestring, &hdd_path_to_partition))
{
printf("NOT FOUND %s\n", config_probestring);
scr_printf("Error: Value '%s' at ps2doom.config not found\n", config_probestring);
SleepThread();
}
else
{
printf("found: %s = %s\n", config_probestring, hdd_path_to_partition);
}
sprintf(config_probestring, "%s", "ps2doom.hdd.wads_folder");
if(!config_lookup_string(&cfg, config_probestring, &hdd_wads_folder))
{
printf("NOT FOUND %s\n", config_probestring);
scr_printf("Error: Value '%s' at ps2doom.config not found\n", config_probestring);
SleepThread();
}
else
{
printf("found: %s = %s\n", config_probestring, hdd_wads_folder);
}
}
//
swap_analogsticks = CONFIG_FALSE;
sprintf(config_probestring, "%s", "ps2doom.controls.switches.swap_analogsticks");
if(!config_lookup_bool(&cfg, config_probestring, &swap_analogsticks))
{
swap_analogsticks = CONFIG_FALSE;
#ifdef DEBUG_LIBCONFIG
printf("NOT FOUND %s\n", config_probestring);
#endif
}
else
{
#ifdef DEBUG_LIBCONFIG
printf("found: %s = %d\n", config_probestring, swap_analogsticks);
#endif
}
}
#ifdef DEBUG_LIBCONFIG
for(i=0; i<sizeof(config_buttons)/sizeof(config_buttons[0]); i++)
{
printf("%d (%s)\n", config_buttons_int[i], config_buttons[i].name);
}
#endif
if(use_hdd == CONFIG_TRUE)
{
SifExecModuleBuffer(poweroff, size_poweroff, 0, NULL, &ret);
SifExecModuleBuffer(iomanX, size_iomanX, 0, NULL, &ret);
SifExecModuleBuffer(fileXio, size_fileXio, 0, NULL, &ret);
SifExecModuleBuffer(ps2dev9, size_ps2dev9, 0, NULL, &ret);
SifExecModuleBuffer(ps2atad, size_ps2atad, 0, NULL, &ret);
static char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
SifExecModuleBuffer(ps2hdd, size_ps2hdd, sizeof(hddarg), hddarg, &ret);
if (ret < 0)
{
printf("Failed to load module: PS2HDD.IRX");
scr_printf("Failed to load module: PS2HDD.IRX");
}
static char pfsarg[] = "-m" "\0" "4" "\0" "-o" "\0" "10" "\0" "-n" "\0" "40";
SifExecModuleBuffer(ps2fs, size_ps2fs, sizeof(pfsarg), pfsarg, &ret);
if (ret < 0)
{
scr_printf("Failed to load module: PS2FS.IRX");
printf("Failed to load module: PS2FS.IRX");
}
//#endif
//#ifdef PS2HDD
//#ifdef PS2HDD
//char ps2HDDPartition[] = "hdd0:+Test"; // TBD : name of partition only
//char folderpath[] = "abc"; // The folder path without any '/'
//sprintf(fullPath, "%s/%s/", hdd_path_to_partition, hdd_wads_folder);
sprintf(fullPath, "pfs0:%s/", hdd_wads_folder);
printf(">>>>>>>>%s\n", fullPath);
//#endif
if( hddCheckPresent() < 0 )
{
printf( "NO HDD FOUND!\n" );
scr_printf( "NO HDD FOUND!\n" );
return -1;
}
else
{
printf( "Found HDD!\n" );
}
if( hddCheckFormatted() < 0 )
{
printf( "HDD Not Formatted!\n" );
scr_printf( "HDD Not Formatted!\n" );
return -1;
}
else
{
printf( "HDD Is Formatted!\n" );
}
int mountErr = 0;
mountErr = fileXioMount( "pfs0:", hdd_path_to_partition, FILEXIO_MOUNT );
if( mountErr < 0 )
{
printf( "Mount Error: %d while trying to mount partition '%s'. Check if path is correct.\n", mountErr, hdd_path_to_partition);
scr_printf( "Mount Error: %d while trying to mount partition '%s'. Check if path is correct.\n", mountErr, hdd_path_to_partition);
SleepThread();
}
//#endif
}
SjPCM_Init(1); // sync mode
printf("SjPCM_Setvol ...\n");
SjPCM_Setvol(0x3fff);
ChangeThreadPriority ( GetThreadId (), 42 );
Mixer_Init(); // TBD : arg number channels
// Until sdl isn't fixed
int PAL = detect_signal();
if (PAL == 1)
PS2SDL_ForceSignal(0);
else
PS2SDL_ForceSignal(1);
// Changes accordingly to filename
forceDisplayMode = getDisplayModeFromELFName(argv);
if (forceDisplayMode != -1)
PS2SDL_ForceSignal(forceDisplayMode);
// Sets SAMPLECOUNT accordingly to system
if (PAL == 1)
SAMPLECOUNT = 960;
else
SAMPLECOUNT = 800;
D_DoomMain ();
return 0;
}
// Check if ELF name ends with PAL, pal, NTSC, ntsc.
int getDisplayModeFromELFName(char **argv)
{
char tmp[10];
int ln = 0,i,j;
ln = strlen(argv[0]);
if (ln>=8)
{
// check for PAL
j=0;
for(i=ln-7; i<=ln-5; i++)
tmp[j++] = argv[0][i];
tmp[j] = 0;
if (strcmp(tmp, "PAL") == 0 || strcmp(tmp, "pal") == 0)
return 0;
// check for NTSC
j=0;
for(i=ln-8; i<=ln-5; i++)
tmp[j++] = argv[0][i];
tmp[j] = 0;
if (strcmp(tmp, "NTSC") == 0 || strcmp(tmp, "ntsc") == 0)
return 1;
}
return -1;
}