comparison romdb.c @ 1326:071e761bcdcf

Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
author Michael Pavone <pavone@retrodev.com>
date Fri, 21 Apr 2017 23:35:32 -0700
parents 2fc444b69351
children ae3b1721b226
comparison
equal deleted inserted replaced
1325:58bfbed6cdb5 1326:071e761bcdcf
641 int index; 641 int index;
642 int num_els; 642 int num_els;
643 uint16_t ptr_index; 643 uint16_t ptr_index;
644 } map_iter_state; 644 } map_iter_state;
645 645
646 void eeprom_read_fun(char *key, tern_val val, void *data) 646 void eeprom_read_fun(char *key, tern_val val, uint8_t valtype, void *data)
647 { 647 {
648 int bit = atoi(key); 648 int bit = atoi(key);
649 if (bit < 0 || bit > 15) { 649 if (bit < 0 || bit > 15) {
650 fprintf(stderr, "bit %s is out of range", key); 650 fprintf(stderr, "bit %s is out of range", key);
651 return; 651 return;
652 } 652 }
653 if (valtype != TVAL_PTR) {
654 fprintf(stderr, "bit %s has a non-scalar value", key);
655 return;
656 }
653 char *pin = val.ptrval; 657 char *pin = val.ptrval;
654 if (strcmp(pin, "sda")) { 658 if (strcmp(pin, "sda")) {
655 fprintf(stderr, "bit %s is connected to unrecognized read pin %s", key, pin); 659 fprintf(stderr, "bit %s is connected to unrecognized read pin %s", key, pin);
656 return; 660 return;
657 } 661 }
658 eeprom_map *map = data; 662 eeprom_map *map = data;
659 map->sda_read_bit = bit; 663 map->sda_read_bit = bit;
660 } 664 }
661 665
662 void eeprom_write_fun(char *key, tern_val val, void *data) 666 void eeprom_write_fun(char *key, tern_val val, uint8_t valtype, void *data)
663 { 667 {
664 int bit = atoi(key); 668 int bit = atoi(key);
665 if (bit < 0 || bit > 15) { 669 if (bit < 0 || bit > 15) {
666 fprintf(stderr, "bit %s is out of range", key); 670 fprintf(stderr, "bit %s is out of range", key);
671 return;
672 }
673 if (valtype != TVAL_PTR) {
674 fprintf(stderr, "bit %s has a non-scalar value", key);
667 return; 675 return;
668 } 676 }
669 char *pin = val.ptrval; 677 char *pin = val.ptrval;
670 eeprom_map *map = data; 678 eeprom_map *map = data;
671 if (!strcmp(pin, "sda")) { 679 if (!strcmp(pin, "sda")) {
680 } 688 }
681 689
682 void process_sram_def(char *key, map_iter_state *state) 690 void process_sram_def(char *key, map_iter_state *state)
683 { 691 {
684 if (!state->info->save_size) { 692 if (!state->info->save_size) {
685 char * size = tern_find_path(state->root, "SRAM\0size\0").ptrval; 693 char * size = tern_find_path(state->root, "SRAM\0size\0", TVAL_PTR).ptrval;
686 if (!size) { 694 if (!size) {
687 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); 695 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);
688 } 696 }
689 state->info->save_size = atoi(size); 697 state->info->save_size = atoi(size);
690 if (!state->info->save_size) { 698 if (!state->info->save_size) {
691 fatal_error("SRAM size %s is invalid\n", size); 699 fatal_error("SRAM size %s is invalid\n", size);
692 } 700 }
693 state->info->save_mask = nearest_pow2(state->info->save_size)-1; 701 state->info->save_mask = nearest_pow2(state->info->save_size)-1;
694 state->info->save_buffer = malloc(state->info->save_size); 702 state->info->save_buffer = malloc(state->info->save_size);
695 memset(state->info->save_buffer, 0, state->info->save_size); 703 memset(state->info->save_buffer, 0, state->info->save_size);
696 char *bus = tern_find_path(state->root, "SRAM\0bus\0").ptrval; 704 char *bus = tern_find_path(state->root, "SRAM\0bus\0", TVAL_PTR).ptrval;
697 if (!strcmp(bus, "odd")) { 705 if (!strcmp(bus, "odd")) {
698 state->info->save_type = RAM_FLAG_ODD; 706 state->info->save_type = RAM_FLAG_ODD;
699 } else if(!strcmp(bus, "even")) { 707 } else if(!strcmp(bus, "even")) {
700 state->info->save_type = RAM_FLAG_EVEN; 708 state->info->save_type = RAM_FLAG_EVEN;
701 } else { 709 } else {
705 } 713 }
706 714
707 void process_eeprom_def(char * key, map_iter_state *state) 715 void process_eeprom_def(char * key, map_iter_state *state)
708 { 716 {
709 if (!state->info->save_size) { 717 if (!state->info->save_size) {
710 char * size = tern_find_path(state->root, "EEPROM\0size\0").ptrval; 718 char * size = tern_find_path(state->root, "EEPROM\0size\0", TVAL_PTR).ptrval;
711 if (!size) { 719 if (!size) {
712 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); 720 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);
713 } 721 }
714 state->info->save_size = atoi(size); 722 state->info->save_size = atoi(size);
715 if (!state->info->save_size) { 723 if (!state->info->save_size) {
716 fatal_error("EEPROM size %s is invalid\n", size); 724 fatal_error("EEPROM size %s is invalid\n", size);
717 } 725 }
718 char *etype = tern_find_path(state->root, "EEPROM\0type\0").ptrval; 726 char *etype = tern_find_path(state->root, "EEPROM\0type\0", TVAL_PTR).ptrval;
719 if (!etype) { 727 if (!etype) {
720 etype = "i2c"; 728 etype = "i2c";
721 } 729 }
722 if (!strcmp(etype, "i2c")) { 730 if (!strcmp(etype, "i2c")) {
723 state->info->save_type = SAVE_I2C; 731 state->info->save_type = SAVE_I2C;
735 { 743 {
736 eeprom_map *eep_map = state->info->eeprom_map + state->info->num_eeprom; 744 eeprom_map *eep_map = state->info->eeprom_map + state->info->num_eeprom;
737 eep_map->start = start; 745 eep_map->start = start;
738 eep_map->end = end; 746 eep_map->end = end;
739 eep_map->sda_read_bit = 0xFF; 747 eep_map->sda_read_bit = 0xFF;
740 tern_node * bits_read = tern_find_ptr(node, "bits_read"); 748 tern_node * bits_read = tern_find_node(node, "bits_read");
741 if (bits_read) { 749 if (bits_read) {
742 tern_foreach(bits_read, eeprom_read_fun, eep_map); 750 tern_foreach(bits_read, eeprom_read_fun, eep_map);
743 } 751 }
744 tern_node * bits_write = tern_find_ptr(node, "bits_write"); 752 tern_node * bits_write = tern_find_node(node, "bits_write");
745 if (bits_write) { 753 if (bits_write) {
746 tern_foreach(bits_write, eeprom_write_fun, eep_map); 754 tern_foreach(bits_write, eeprom_write_fun, eep_map);
747 } 755 }
748 printf("EEPROM address %X: sda read: %X, sda write: %X, scl: %X\n", start, eep_map->sda_read_bit, eep_map->sda_write_mask, eep_map->scl_mask); 756 printf("EEPROM address %X: sda read: %X, sda write: %X, scl: %X\n", start, eep_map->sda_read_bit, eep_map->sda_write_mask, eep_map->scl_mask);
749 state->info->num_eeprom++; 757 state->info->num_eeprom++;
750 } 758 }
751 759
752 void map_iter_fun(char *key, tern_val val, void *data) 760 void map_iter_fun(char *key, tern_val val, uint8_t valtype, void *data)
753 { 761 {
754 map_iter_state *state = data; 762 map_iter_state *state = data;
755 tern_node *node = tern_get_node(val); 763 if (valtype != TVAL_NODE) {
756 if (!node) {
757 fatal_error("ROM DB map entry %d with address %s is not a node\n", state->index, key); 764 fatal_error("ROM DB map entry %d with address %s is not a node\n", state->index, key);
758 } 765 }
766 tern_node *node = val.ptrval;
759 uint32_t start = strtol(key, NULL, 16); 767 uint32_t start = strtol(key, NULL, 16);
760 uint32_t end = strtol(tern_find_ptr_default(node, "last", "0"), NULL, 16); 768 uint32_t end = strtol(tern_find_ptr_default(node, "last", "0"), NULL, 16);
761 if (!end || end < start) { 769 if (!end || end < start) {
762 fatal_error("'last' value is missing or invalid for ROM DB map entry %d with address %s\n", state->index, key); 770 fatal_error("'last' value is missing or invalid for ROM DB map entry %d with address %s\n", state->index, key);
763 } 771 }
817 state->info->mapper_start_index = state->ptr_index++; 825 state->info->mapper_start_index = state->ptr_index++;
818 state->info->map_chunks+=7; 826 state->info->map_chunks+=7;
819 state->info->map = realloc(state->info->map, sizeof(memmap_chunk) * state->info->map_chunks); 827 state->info->map = realloc(state->info->map, sizeof(memmap_chunk) * state->info->map_chunks);
820 memset(state->info->map + state->info->map_chunks - 7, 0, sizeof(memmap_chunk) * 7); 828 memset(state->info->map + state->info->map_chunks - 7, 0, sizeof(memmap_chunk) * 7);
821 map = state->info->map + state->index; 829 map = state->info->map + state->index;
822 char *save_device = tern_find_path(node, "save\0device\0").ptrval; 830 char *save_device = tern_find_path(node, "save\0device\0", TVAL_PTR).ptrval;
823 if (save_device && !strcmp(save_device, "EEPROM")) { 831 if (save_device && !strcmp(save_device, "EEPROM")) {
824 process_eeprom_def(key, state); 832 process_eeprom_def(key, state);
825 add_eeprom_map(node, start & map->mask, end & map->mask, state); 833 add_eeprom_map(node, start & map->mask, end & map->mask, state);
826 } 834 }
827 for (int i = 0; i < 7; i++, state->index++, map++) 835 for (int i = 0; i < 7; i++, state->index++, map++)
890 uint8_t raw_hash[20]; 898 uint8_t raw_hash[20];
891 sha1(vrom, rom_size, raw_hash); 899 sha1(vrom, rom_size, raw_hash);
892 uint8_t hex_hash[41]; 900 uint8_t hex_hash[41];
893 bin_to_hex(hex_hash, raw_hash, 20); 901 bin_to_hex(hex_hash, raw_hash, 20);
894 printf("SHA1: %s\n", hex_hash); 902 printf("SHA1: %s\n", hex_hash);
895 tern_node * entry = tern_find_ptr(rom_db, hex_hash); 903 tern_node * entry = tern_find_node(rom_db, hex_hash);
896 if (!entry) { 904 if (!entry) {
897 entry = tern_find_ptr(rom_db, product_id); 905 entry = tern_find_node(rom_db, product_id);
898 } 906 }
899 if (!entry) { 907 if (!entry) {
900 puts("Not found in ROM DB, examining header\n"); 908 puts("Not found in ROM DB, examining header\n");
901 if (xband_detect(rom, rom_size)) { 909 if (xband_detect(rom, rom_size)) {
902 return xband_configure_rom(rom_db, rom, rom_size, lock_on, lock_on_size, base_map, base_chunks); 910 return xband_configure_rom(rom_db, rom, rom_size, lock_on, lock_on_size, base_map, base_chunks);
925 } 933 }
926 if (!info.regions) { 934 if (!info.regions) {
927 info.regions = get_header_regions(rom); 935 info.regions = get_header_regions(rom);
928 } 936 }
929 937
930 tern_node *map = tern_find_ptr(entry, "map"); 938 tern_node *map = tern_find_node(entry, "map");
931 if (map) { 939 if (map) {
932 info.save_type = SAVE_NONE; 940 info.save_type = SAVE_NONE;
933 info.map_chunks = tern_count(map); 941 info.map_chunks = tern_count(map);
934 if (info.map_chunks) { 942 if (info.map_chunks) {
935 info.map_chunks += base_chunks; 943 info.map_chunks += base_chunks;
957 } 965 }
958 } else { 966 } else {
959 add_memmap_header(&info, rom, rom_size, base_map, base_chunks); 967 add_memmap_header(&info, rom, rom_size, base_map, base_chunks);
960 } 968 }
961 969
962 tern_node *device_overrides = tern_find_ptr(entry, "device_overrides"); 970 tern_node *device_overrides = tern_find_node(entry, "device_overrides");
963 if (device_overrides) { 971 if (device_overrides) {
964 info.port1_override = tern_find_ptr(device_overrides, "1"); 972 info.port1_override = tern_find_ptr(device_overrides, "1");
965 info.port2_override = tern_find_ptr(device_overrides, "2"); 973 info.port2_override = tern_find_ptr(device_overrides, "2");
966 info.ext_override = tern_find_ptr(device_overrides, "ext"); 974 info.ext_override = tern_find_ptr(device_overrides, "ext");
967 } else { 975 } else {