-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.c
356 lines (298 loc) · 8.71 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
#include <iapetus.h>
#include <stdio.h>
#include <sys/stat.h>
#include "satiator.h"
#include <string.h>
#include "cdparse.h"
#include "fade.h"
#include "syscall.h"
#include "gmenu.h"
#include "jhloader.h"
void elf_launch(const char *filename);
extern const char *elf_error;
int image_file_filter(file_ent *entry) {
if (entry->isdir)
return 1;
int len = strlen(entry->name);
if (!strcasecmp(&entry->name[len-4], ".cue"))
return 1;
if (!strcasecmp(&entry->name[len-4], ".iso"))
return 1;
if (!strcasecmp(&entry->name[len-4], ".elf"))
return 1;
return 0;
}
int is_multidisc_dir(file_ent *entry, int nents) {
// precondition: list must be sorted
char *basename = entry[0].name;
char *discnum = strrchr(basename, '1');
if (!discnum)
return 0;
if (nents > 9)
return 0;
int is_multidisc = 1;
for (int i=1; i<nents; i++) {
*discnum += 1;
if (strcasecmp(entry[i].name, basename)) {
is_multidisc = 0;
break;
}
}
*discnum = '1';
return is_multidisc;
}
const file_ent multidisc_menu_options[] = {
{"Disc 1", 0, NULL},
{"Disc 2", 0, NULL},
{"Disc 3", 0, NULL},
{"Disc 4", 0, NULL},
{"Disc 5", 0, NULL},
{"Disc 6", 0, NULL},
{"Disc 7", 0, NULL},
{"Disc 8", 0, NULL},
{"Disc 9", 0, NULL},
};
char *multidisc_menu(file_ent *list, int nents) {
char *basename = list[0].name;
char *discnum = strrchr(basename, '1');
if (!discnum)
return NULL;
if (nents > 9)
return NULL;
int entry = menu_picklist(multidisc_menu_options, nents, "Select disc");
if (entry < 0)
return NULL;
char *multidisc_desc = "out1.desc";
for (int i=0; i<nents; i++) {
multidisc_desc[3] = *discnum;
int ret = image2desc(basename, multidisc_desc);
if (ret) {
menu_error("Disc load failed!", cdparse_error_string ? cdparse_error_string : "Unknown error");
return NULL;
}
*discnum += 1;
}
multidisc_desc[3] = '1' + entry;
return strdup(multidisc_desc);
}
void restore_vdp_mem(void);
void launch_game(const char *filename) {
int len = strlen(filename);
if (!strcasecmp(&filename[len-4], ".elf")) {
elf_launch(filename);
if (elf_error)
menu_error("ELF load failed!", elf_error);
return;
}
int is_desc = !strcasecmp(&filename[len-5], ".desc");
const char *descname;
if (is_desc) {
descname = filename;
} else {
descname = "out.desc";
int ret = image2desc(filename, descname);
if (ret) {
menu_error("Disc load failed!", cdparse_error_string ? cdparse_error_string : "Unknown error");
return;
}
}
fadeout(0x20);
satiator_cart_header_t *cart = s_find_cartridge();
if (cart && cart->header_version >= 1)
cart->install_soft_reset_hook();
restore_vdp_mem();
s_emulate(descname);
while (is_cd_present());
while (!is_cd_present());
s_mode(s_cdrom);
int ret = boot_disc();
s_mode(s_api); // failed, restore order
s_emulate(""); // close the old file
fadein(0x20);
const char *error = "Unknown error";
switch(ret) {
case BOOT_BAD_HEADER:
error = "Bad disc header. Bad image?";
break;
case BOOT_BAD_REGION:
error = "Wrong region.";
break;
case BOOT_BAD_SECURITY_CODE:
error = "Bad security code. Bad image?";
break;
case BOOT_UNRECOGNISED_BIOS:
error = "Unrecognised BIOS version! Contact Abrasive [email protected]";
break;
}
menu_error("Boot failed!", error);
}
void update_region_txt(void) {
int fd = s_open("/region.txt", FA_WRITE|FA_CREATE_ALWAYS);
s_write(fd, region_string, 1);
s_close(fd);
}
static int file_exists(const char *name) {
struct stat st;
int ret = stat(name, &st);
return ret == 0;
}
void try_autoboot(void) {
if (file_exists("autoboot.elf")) {
elf_launch("autoboot.elf");
if (elf_error)
menu_error("ELF load failed!", elf_error);
return;
}
if (file_exists("autoboot.iso")) {
launch_game("autoboot.iso");
return;
}
if (file_exists("autoboot.cue")) {
launch_game("autoboot.cue");
return;
}
}
char pathbuf[512];
void image_menu(void) {
char *name = NULL;
strcpy(pathbuf, "/");
for(;;) {
int nents;
file_ent *list = file_list_create(".", &nents, image_file_filter);
if (nents == 1 && strcmp(pathbuf, "/") && !list[0].isdir)
launch_game(list[0].name);
file_list_sort(list, nents);
char namebuf[32];
strcpy(namebuf, "Satiator - ");
char *dirname = strrchr(pathbuf, '/');
if (dirname[1])
dirname++;
strlcat(namebuf, dirname, sizeof(namebuf));
if (is_multidisc_dir(list, nents)) {
name = multidisc_menu(list, nents);
if (!name)
goto updir;
goto chosen;
}
int entry = menu_picklist(list, nents, namebuf);
int ret;
if (entry == -1) {
if (!strcmp(pathbuf, "/")) {
return;
} else {
char *last_slash;
updir:
last_slash = strrchr(pathbuf, '/');
if (last_slash == pathbuf)
last_slash++;
*last_slash = '\0';
s_chdir(pathbuf);
}
} else if (list[entry].isdir) {
// Got to strip the slash :(
list[entry].name[strlen(list[entry].name) - 1] = '\0';
if (pathbuf[1])
strcat(pathbuf, "/");
strcat(pathbuf, list[entry].name);
ret = s_chdir(pathbuf);
if (ret != FR_OK) {
menu_error("chdir", "Can't change directory, corrupt SD card?");
}
}
else
name = strdup(list[entry].name);
chosen:
file_list_free(list, nents);
if (name) {
launch_game(name);
free(name);
name = NULL;
}
}
}
const file_ent format_confirm_menu_options[] = {
{"No", 0, NULL},
{"No", 0, NULL},
{"No", 0, NULL},
{"No", 0, NULL},
{"No", 0, NULL},
{"No", 0, NULL},
{"No", 0, NULL},
{"Yes", 0, (void*)1},
};
extern uint8_t vdp1_stash[];
void format_confirm(int flags) {
int entry = menu_picklist(format_confirm_menu_options, sizeof(format_confirm_menu_options)/sizeof(*format_confirm_menu_options), "Select format_confirm");
if (entry == -1)
return;
if (!format_confirm_menu_options[entry].priv)
return;
menu_progress_begin("Formatting...", 3);
int menu_size = 0;
int fd = s_open("/menu.bin", FA_READ);
int ret;
uint8_t *p = vdp1_stash;
while ((ret = s_read(fd, p, 2048)) > 0) {
p += ret;
menu_size += ret;
}
s_close(fd);
menu_progress_update(1);
int mkfs_ret = s_format_sd_card(flags);
menu_progress_update(2);
fd = s_open("/menu.bin", FA_WRITE|FA_CREATE_ALWAYS);
p = vdp1_stash;
while (menu_size > 0) {
int count = menu_size;
if (count > 2048)
count = 2048;
ret = s_write(fd, p, count);
if (ret < 0)
break;
p += count;
menu_size -= count;
}
s_close(fd);
menu_progress_complete();
char msg_buf[64];
if (mkfs_ret != FR_OK)
sprintf(msg_buf, "Error %d", mkfs_ret);
else
sprintf(msg_buf, "Success!");
menu_error("SD formatting complete", msg_buf);
}
const file_ent format_menu_options[] = {
{"FAT32", 0, &format_confirm, 1},
{"exFAT", 0, &format_confirm, 0},
};
void format_menu(void) {
int entry = menu_picklist(format_menu_options, sizeof(format_menu_options)/sizeof(*format_menu_options), "Select format");
if (entry == -1)
return;
void (*submenu)(int) = format_menu_options[entry].priv;
submenu(format_menu_options[entry].extra);
}
void ar_menu(void);
void diagnostic_menu(void);
const file_ent top_menu_options[] = {
{"Browse images", 0, &image_menu},
{"Action Replay tools", 0, &ar_menu},
{"Diagnostics", 0, &diagnostic_menu},
{"Format SD card", 0, &format_menu},
};
extern uint32_t boot_arg;
void main_menu(void) {
s_chdir("\\");
update_region_txt();
if (boot_arg != S_BOOT_NO_AUTOLOAD)
try_autoboot();
menu_init();
image_menu();
while (1) {
int entry = menu_picklist(top_menu_options, sizeof(top_menu_options)/sizeof(*top_menu_options), "Satiator");
if (entry == -1)
continue;
void (*submenu)(void) = top_menu_options[entry].priv;
submenu();
}
}