comparison blastem.c @ 1472:d2d637dbacfb segacd

Change load_rom into load_media with some interface changes in preparation for CD support
author Michael Pavone <pavone@retrodev.com>
date Sat, 21 Oct 2017 11:51:24 -0700
parents 4929325c3ce0
children 5dacaef602a7
comparison
equal deleted inserted replaced
1467:d51230205405 1472:d2d637dbacfb
63 filesize -= SMD_BLOCK_SIZE; 63 filesize -= SMD_BLOCK_SIZE;
64 } 64 }
65 return rom_size; 65 return rom_size;
66 } 66 }
67 67
68 uint32_t load_rom(char * filename, void **dst, system_type *stype) 68 uint32_t load_media(char * filename, system_media *dst, system_type *stype)
69 { 69 {
70 uint8_t header[10]; 70 uint8_t header[10];
71 FILE * f = fopen(filename, "rb"); 71 FILE * f = fopen(filename, "rb");
72 if (!f) { 72 if (!f) {
73 return 0; 73 return 0;
76 fatal_error("Error reading from %s\n", filename); 76 fatal_error("Error reading from %s\n", filename);
77 } 77 }
78 fseek(f, 0, SEEK_END); 78 fseek(f, 0, SEEK_END);
79 long filesize = ftell(f); 79 long filesize = ftell(f);
80 fseek(f, 0, SEEK_SET); 80 fseek(f, 0, SEEK_SET);
81 uint32_t ret = 0;
81 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) { 82 if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) {
82 int i; 83 int i;
83 for (i = 3; i < 8; i++) { 84 for (i = 3; i < 8; i++) {
84 if (header[i] != 0) { 85 if (header[i] != 0) {
85 break; 86 break;
90 fatal_error("%s is a split SMD ROM which is not currently supported", filename); 91 fatal_error("%s is a split SMD ROM which is not currently supported", filename);
91 } 92 }
92 if (stype) { 93 if (stype) {
93 *stype = SYSTEM_GENESIS; 94 *stype = SYSTEM_GENESIS;
94 } 95 }
95 return load_smd_rom(filesize, f, dst); 96 ret = load_smd_rom(filesize, f, &dst->buffer);
96 } 97 }
97 } 98 }
98 *dst = malloc(nearest_pow2(filesize)); 99 if (!ret) {
99 if (filesize != fread(*dst, 1, filesize, f)) { 100 dst->buffer = malloc(nearest_pow2(filesize));
100 fatal_error("Error reading from %s\n", filename); 101 if (filesize != fread(dst->buffer, 1, filesize, f)) {
101 } 102 fatal_error("Error reading from %s\n", filename);
103 }
104 ret = filesize;
105 }
106 dst->dir = path_dirname(filename);
107 dst->name = basename_no_extension(filename);
108 dst->extension = path_extension(filename);
109 dst->size = ret;
102 fclose(f); 110 fclose(f);
103 return filesize; 111 return ret;
104 } 112 }
105 113
106 114
107 115
108 int break_on_sync = 0; 116 int break_on_sync = 0;
226 reload_media(); 234 reload_media();
227 cart.chain = &lock_on; 235 cart.chain = &lock_on;
228 free(lock_on.dir); 236 free(lock_on.dir);
229 free(lock_on.name); 237 free(lock_on.name);
230 free(lock_on.extension); 238 free(lock_on.extension);
231 lock_on.dir = path_dirname(lock_on_path); 239 load_media(lock_on_path, &lock_on, NULL);
232 lock_on.name = basename_no_extension(lock_on_path);
233 lock_on.extension = path_extension(lock_on_path);
234 lock_on.size = load_rom(lock_on_path, &lock_on.buffer, NULL);
235 } 240 }
236 241
237 int main(int argc, char ** argv) 242 int main(int argc, char ** argv)
238 { 243 {
239 set_exe_str(argv[0]); 244 set_exe_str(argv[0]);
331 case 'o': { 336 case 'o': {
332 i++; 337 i++;
333 if (i >= argc) { 338 if (i >= argc) {
334 fatal_error("-o must be followed by a lock on cartridge filename\n"); 339 fatal_error("-o must be followed by a lock on cartridge filename\n");
335 } 340 }
336 lock_on.size = load_rom(argv[i], &lock_on.buffer, NULL); 341 if (!load_media(argv[i], &lock_on, NULL)) {
337 if (!lock_on.size) {
338 fatal_error("Failed to load lock on cartridge %s\n", argv[i]); 342 fatal_error("Failed to load lock on cartridge %s\n", argv[i]);
339 } 343 }
340 lock_on.name = basename_no_extension(argv[i]);
341 lock_on.extension = path_extension(argv[i]);
342 cart.chain = &lock_on; 344 cart.chain = &lock_on;
343 break; 345 break;
344 } 346 }
345 case 'h': 347 case 'h':
346 info_message( 348 info_message(
365 return 0; 367 return 0;
366 default: 368 default:
367 fatal_error("Unrecognized switch %s\n", argv[i]); 369 fatal_error("Unrecognized switch %s\n", argv[i]);
368 } 370 }
369 } else if (!loaded) { 371 } else if (!loaded) {
370 if (!(cart.size = load_rom(argv[i], &cart.buffer, stype == SYSTEM_UNKNOWN ? &stype : NULL))) { 372 if (!load_media(argv[i], &cart, stype == SYSTEM_UNKNOWN ? &stype : NULL)) {
371 fatal_error("Failed to open %s for reading\n", argv[i]); 373 fatal_error("Failed to open %s for reading\n", argv[i]);
372 } 374 }
373 cart.dir = path_dirname(argv[i]);
374 cart.name = basename_no_extension(argv[i]);
375 cart.extension = path_extension(argv[i]);
376 romfname = argv[i]; 375 romfname = argv[i];
377 loaded = 1; 376 loaded = 1;
378 } else if (width < 0) { 377 } else if (width < 0) {
379 width = atoi(argv[i]); 378 width = atoi(argv[i]);
380 } else if (height < 0) { 379 } else if (height < 0) {
387 romfname = tern_find_path(config, "ui\0rom\0", TVAL_PTR).ptrval; 386 romfname = tern_find_path(config, "ui\0rom\0", TVAL_PTR).ptrval;
388 if (!romfname) { 387 if (!romfname) {
389 romfname = "menu.bin"; 388 romfname = "menu.bin";
390 } 389 }
391 if (is_absolute_path(romfname)) { 390 if (is_absolute_path(romfname)) {
392 if (!(cart.size = load_rom(romfname, &cart.buffer, &stype))) { 391 if (!load_media(romfname, &cart, &stype)) {
393 fatal_error("Failed to open UI ROM %s for reading", romfname); 392 fatal_error("Failed to open UI ROM %s for reading", romfname);
394 } 393 }
395 } else { 394 } else {
396 cart.buffer = (uint16_t *)read_bundled_file(romfname, &cart.size); 395 cart.buffer = (uint16_t *)read_bundled_file(romfname, &cart.size);
397 if (!cart.buffer) { 396 if (!cart.buffer) {
400 uint32_t rom_size = nearest_pow2(cart.size); 399 uint32_t rom_size = nearest_pow2(cart.size);
401 if (rom_size > cart.size) { 400 if (rom_size > cart.size) {
402 cart.buffer = realloc(cart.buffer, rom_size); 401 cart.buffer = realloc(cart.buffer, rom_size);
403 cart.size = rom_size; 402 cart.size = rom_size;
404 } 403 }
404 cart.dir = path_dirname(romfname);
405 cart.name = basename_no_extension(romfname);
406 cart.extension = path_extension(romfname);
405 } 407 }
406 //force system detection, value on command line is only for games not the menu 408 //force system detection, value on command line is only for games not the menu
407 stype = detect_system_type(&cart); 409 stype = detect_system_type(&cart);
408 cart.dir = path_dirname(romfname);
409 cart.name = basename_no_extension(romfname);
410 cart.extension = path_extension(romfname);
411 loaded = 1; 410 loaded = 1;
412 } 411 }
413 412
414 int def_width = 0, def_height = 0; 413 int def_width = 0, def_height = 0;
415 char *config_width = tern_find_path(config, "video\0width\0", TVAL_PTR).ptrval; 414 char *config_width = tern_find_path(config, "video\0width\0", TVAL_PTR).ptrval;
484 game_system->free_context(game_system); 483 game_system->free_context(game_system);
485 } else { 484 } else {
486 //start a new arena and save old one in suspended genesis context 485 //start a new arena and save old one in suspended genesis context
487 current_system->arena = start_new_arena(); 486 current_system->arena = start_new_arena();
488 } 487 }
489 if (!(cart.size = load_rom(next_rom, &cart.buffer, &stype))) {
490 fatal_error("Failed to open %s for reading\n", next_rom);
491 }
492 free(cart.dir); 488 free(cart.dir);
493 free(cart.name); 489 free(cart.name);
494 free(cart.extension); 490 free(cart.extension);
495 cart.dir = path_dirname(next_rom); 491 if (!load_media(next_rom, &cart, &stype)) {
496 cart.name = basename_no_extension(next_rom); 492 fatal_error("Failed to open %s for reading\n", next_rom);
497 cart.extension = path_extension(next_rom); 493 }
498 stype = force_stype; 494 stype = force_stype;
499 if (stype == SYSTEM_UNKNOWN) { 495 if (stype == SYSTEM_UNKNOWN) {
500 stype = detect_system_type(&cart); 496 stype = detect_system_type(&cart);
501 } 497 }
502 if (stype == SYSTEM_UNKNOWN) { 498 if (stype == SYSTEM_UNKNOWN) {