Mercurial > repos > blastem
comparison romdb.c @ 792:724bbec47f86
Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 25 Jul 2015 18:22:07 -0700 |
parents | 51c3e571eeb1 |
children | e6f2c9dbf6c8 |
comparison
equal
deleted
inserted
replaced
791:60686f8d5e48 | 792:724bbec47f86 |
---|---|
315 void * write_eeprom_i2c_w(uint32_t address, void * context, uint16_t value) | 315 void * write_eeprom_i2c_w(uint32_t address, void * context, uint16_t value) |
316 { | 316 { |
317 genesis_context *gen = ((m68k_context *)context)->system; | 317 genesis_context *gen = ((m68k_context *)context)->system; |
318 eeprom_map *map = find_eeprom_map(address, gen); | 318 eeprom_map *map = find_eeprom_map(address, gen); |
319 if (!map) { | 319 if (!map) { |
320 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | 320 fatal_error("Could not find EEPROM map for address %X\n", address); |
321 exit(1); | |
322 } | 321 } |
323 if (map->scl_mask) { | 322 if (map->scl_mask) { |
324 set_scl(&gen->eeprom, (value & map->scl_mask) != 0); | 323 set_scl(&gen->eeprom, (value & map->scl_mask) != 0); |
325 } | 324 } |
326 if (map->sda_write_mask) { | 325 if (map->sda_write_mask) { |
332 void * write_eeprom_i2c_b(uint32_t address, void * context, uint8_t value) | 331 void * write_eeprom_i2c_b(uint32_t address, void * context, uint8_t value) |
333 { | 332 { |
334 genesis_context *gen = ((m68k_context *)context)->system; | 333 genesis_context *gen = ((m68k_context *)context)->system; |
335 eeprom_map *map = find_eeprom_map(address, gen); | 334 eeprom_map *map = find_eeprom_map(address, gen); |
336 if (!map) { | 335 if (!map) { |
337 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | 336 fatal_error("Could not find EEPROM map for address %X\n", address); |
338 exit(1); | |
339 } | 337 } |
340 | 338 |
341 uint16_t expanded, mask; | 339 uint16_t expanded, mask; |
342 if (address & 1) { | 340 if (address & 1) { |
343 expanded = value; | 341 expanded = value; |
358 uint16_t read_eeprom_i2c_w(uint32_t address, void * context) | 356 uint16_t read_eeprom_i2c_w(uint32_t address, void * context) |
359 { | 357 { |
360 genesis_context *gen = ((m68k_context *)context)->system; | 358 genesis_context *gen = ((m68k_context *)context)->system; |
361 eeprom_map *map = find_eeprom_map(address, gen); | 359 eeprom_map *map = find_eeprom_map(address, gen); |
362 if (!map) { | 360 if (!map) { |
363 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | 361 fatal_error("Could not find EEPROM map for address %X\n", address); |
364 exit(1); | |
365 } | 362 } |
366 uint16_t ret = 0; | 363 uint16_t ret = 0; |
367 if (map->sda_read_bit < 16) { | 364 if (map->sda_read_bit < 16) { |
368 ret = get_sda(&gen->eeprom) << map->sda_read_bit; | 365 ret = get_sda(&gen->eeprom) << map->sda_read_bit; |
369 } | 366 } |
373 uint8_t read_eeprom_i2c_b(uint32_t address, void * context) | 370 uint8_t read_eeprom_i2c_b(uint32_t address, void * context) |
374 { | 371 { |
375 genesis_context *gen = ((m68k_context *)context)->system; | 372 genesis_context *gen = ((m68k_context *)context)->system; |
376 eeprom_map *map = find_eeprom_map(address, gen); | 373 eeprom_map *map = find_eeprom_map(address, gen); |
377 if (!map) { | 374 if (!map) { |
378 fprintf(stderr, "Could not find EEPROM map for address %X\n", address); | 375 fatal_error("Could not find EEPROM map for address %X\n", address); |
379 exit(1); | |
380 } | 376 } |
381 uint8_t bit = address & 1 ? map->sda_read_bit : map->sda_read_bit - 8; | 377 uint8_t bit = address & 1 ? map->sda_read_bit : map->sda_read_bit - 8; |
382 uint8_t ret = 0; | 378 uint8_t ret = 0; |
383 if (bit < 8) { | 379 if (bit < 8) { |
384 ret = get_sda(&gen->eeprom) << bit; | 380 ret = get_sda(&gen->eeprom) << bit; |
388 | 384 |
389 tern_node *load_rom_db() | 385 tern_node *load_rom_db() |
390 { | 386 { |
391 char *exe_dir = get_exe_dir(); | 387 char *exe_dir = get_exe_dir(); |
392 if (!exe_dir) { | 388 if (!exe_dir) { |
393 fputs("Failed to find executable path\n", stderr); | 389 fatal_error("Failed to find executable path\n"); |
394 exit(1); | |
395 } | 390 } |
396 char *path = alloc_concat(exe_dir, "/rom.db"); | 391 char *path = alloc_concat(exe_dir, "/rom.db"); |
397 tern_node *db = parse_config_file(path); | 392 tern_node *db = parse_config_file(path); |
398 free(path); | 393 free(path); |
399 if (!db) { | 394 if (!db) { |
400 fputs("Failed to load ROM DB\n", stderr); | 395 fatal_error("Failed to load ROM DB\n"); |
401 } | 396 } |
402 return db; | 397 return db; |
403 } | 398 } |
404 | 399 |
405 char *get_header_name(uint8_t *rom) | 400 char *get_header_name(uint8_t *rom) |
613 void process_sram_def(char *key, map_iter_state *state) | 608 void process_sram_def(char *key, map_iter_state *state) |
614 { | 609 { |
615 if (!state->info->save_size) { | 610 if (!state->info->save_size) { |
616 char * size = tern_find_path(state->root, "SRAM\0size\0").ptrval; | 611 char * size = tern_find_path(state->root, "SRAM\0size\0").ptrval; |
617 if (!size) { | 612 if (!size) { |
618 fprintf(stderr, "ROM DB map entry %d with address %s has device type SRAM, but the SRAM size is not defined\n", state->index, key); | 613 fatal_error("ROM DB map entry %d with address %s has device type SRAM, but the SRAM size is not defined\n", state->index, key); |
619 exit(1); | |
620 } | 614 } |
621 state->info->save_size = atoi(size); | 615 state->info->save_size = atoi(size); |
622 if (!state->info->save_size) { | 616 if (!state->info->save_size) { |
623 fprintf(stderr, "SRAM size %s is invalid\n", size); | 617 fatal_error("SRAM size %s is invalid\n", size); |
624 exit(1); | |
625 } | 618 } |
626 state->info->save_mask = nearest_pow2(state->info->save_size)-1; | 619 state->info->save_mask = nearest_pow2(state->info->save_size)-1; |
627 state->info->save_buffer = malloc(state->info->save_size); | 620 state->info->save_buffer = malloc(state->info->save_size); |
628 memset(state->info->save_buffer, 0, state->info->save_size); | 621 memset(state->info->save_buffer, 0, state->info->save_size); |
629 char *bus = tern_find_path(state->root, "SRAM\0bus\0").ptrval; | 622 char *bus = tern_find_path(state->root, "SRAM\0bus\0").ptrval; |
640 void process_eeprom_def(char * key, map_iter_state *state) | 633 void process_eeprom_def(char * key, map_iter_state *state) |
641 { | 634 { |
642 if (!state->info->save_size) { | 635 if (!state->info->save_size) { |
643 char * size = tern_find_path(state->root, "EEPROM\0size\0").ptrval; | 636 char * size = tern_find_path(state->root, "EEPROM\0size\0").ptrval; |
644 if (!size) { | 637 if (!size) { |
645 fprintf(stderr, "ROM DB map entry %d with address %s has device type EEPROM, but the EEPROM size is not defined\n", state->index, key); | 638 fatal_error("ROM DB map entry %d with address %s has device type EEPROM, but the EEPROM size is not defined\n", state->index, key); |
646 exit(1); | |
647 } | 639 } |
648 state->info->save_size = atoi(size); | 640 state->info->save_size = atoi(size); |
649 if (!state->info->save_size) { | 641 if (!state->info->save_size) { |
650 fprintf(stderr, "EEPROM size %s is invalid\n", size); | 642 fatal_error("EEPROM size %s is invalid\n", size); |
651 exit(1); | |
652 } | 643 } |
653 char *etype = tern_find_path(state->root, "EEPROM\0type\0").ptrval; | 644 char *etype = tern_find_path(state->root, "EEPROM\0type\0").ptrval; |
654 if (!etype) { | 645 if (!etype) { |
655 etype = "i2c"; | 646 etype = "i2c"; |
656 } | 647 } |
657 if (!strcmp(etype, "i2c")) { | 648 if (!strcmp(etype, "i2c")) { |
658 state->info->save_type = SAVE_I2C; | 649 state->info->save_type = SAVE_I2C; |
659 } else { | 650 } else { |
660 fprintf(stderr, "EEPROM type %s is invalid\n", etype); | 651 fatal_error("EEPROM type %s is invalid\n", etype); |
661 exit(1); | |
662 } | 652 } |
663 state->info->save_buffer = malloc(state->info->save_size); | 653 state->info->save_buffer = malloc(state->info->save_size); |
664 memset(state->info->save_buffer, 0xFF, state->info->save_size); | 654 memset(state->info->save_buffer, 0xFF, state->info->save_size); |
665 state->info->eeprom_map = malloc(sizeof(eeprom_map) * state->num_els); | 655 state->info->eeprom_map = malloc(sizeof(eeprom_map) * state->num_els); |
666 memset(state->info->eeprom_map, 0, sizeof(eeprom_map) * state->num_els); | 656 memset(state->info->eeprom_map, 0, sizeof(eeprom_map) * state->num_els); |
688 void map_iter_fun(char *key, tern_val val, void *data) | 678 void map_iter_fun(char *key, tern_val val, void *data) |
689 { | 679 { |
690 map_iter_state *state = data; | 680 map_iter_state *state = data; |
691 tern_node *node = tern_get_node(val); | 681 tern_node *node = tern_get_node(val); |
692 if (!node) { | 682 if (!node) { |
693 fprintf(stderr, "ROM DB map entry %d with address %s is not a node\n", state->index, key); | 683 fatal_error("ROM DB map entry %d with address %s is not a node\n", state->index, key); |
694 exit(1); | |
695 } | 684 } |
696 uint32_t start = strtol(key, NULL, 16); | 685 uint32_t start = strtol(key, NULL, 16); |
697 uint32_t end = strtol(tern_find_ptr_default(node, "last", "0"), NULL, 16); | 686 uint32_t end = strtol(tern_find_ptr_default(node, "last", "0"), NULL, 16); |
698 if (!end || end < start) { | 687 if (!end || end < start) { |
699 fprintf(stderr, "'last' value is missing or invalid for ROM DB map entry %d with address %s\n", state->index, key); | 688 fatal_error("'last' value is missing or invalid for ROM DB map entry %d with address %s\n", state->index, key); |
700 exit(1); | |
701 } | 689 } |
702 char * dtype = tern_find_ptr_default(node, "device", "ROM"); | 690 char * dtype = tern_find_ptr_default(node, "device", "ROM"); |
703 uint32_t offset = strtol(tern_find_ptr_default(node, "offset", "0"), NULL, 16); | 691 uint32_t offset = strtol(tern_find_ptr_default(node, "offset", "0"), NULL, 16); |
704 memmap_chunk *map = state->info->map + state->index; | 692 memmap_chunk *map = state->info->map + state->index; |
705 map->start = start; | 693 map->start = start; |
767 map->end = 0xA13100; | 755 map->end = 0xA13100; |
768 map->mask = 0xFF; | 756 map->mask = 0xFF; |
769 map->write_16 = (write_16_fun)write_bank_reg_w; | 757 map->write_16 = (write_16_fun)write_bank_reg_w; |
770 map->write_8 = (write_8_fun)write_bank_reg_b; | 758 map->write_8 = (write_8_fun)write_bank_reg_b; |
771 } else { | 759 } else { |
772 fprintf(stderr, "Invalid device type for ROM DB map entry %d with address %s\n", state->index, key); | 760 fatal_error("Invalid device type for ROM DB map entry %d with address %s\n", state->index, key); |
773 exit(1); | |
774 } | 761 } |
775 state->index++; | 762 state->index++; |
776 } | 763 } |
777 | 764 |
778 rom_info configure_rom(tern_node *rom_db, void *vrom, uint32_t rom_size, memmap_chunk const *base_map, uint32_t base_chunks) | 765 rom_info configure_rom(tern_node *rom_db, void *vrom, uint32_t rom_size, memmap_chunk const *base_map, uint32_t base_chunks) |