| 639 | } |
| 640 | |
| 641 | int loadSave() |
| 642 | { |
| 643 | if (saveFile != NULL) { |
| 644 | fclose(saveFile); |
| 645 | saveFile = NULL; |
| 646 | } |
| 647 | if (externRam != NULL) { |
| 648 | free(externRam); |
| 649 | externRam = NULL; |
| 650 | } |
| 651 | |
| 652 | if (gbsMode) |
| 653 | numRamBanks = 1; |
| 654 | else { |
| 655 | // Get the game's external memory size and allocate the memory |
| 656 | switch(ramSize) |
| 657 | { |
| 658 | case 0: |
| 659 | numRamBanks = 0; |
| 660 | break; |
| 661 | case 1: |
| 662 | case 2: |
| 663 | numRamBanks = 1; |
| 664 | break; |
| 665 | case 3: |
| 666 | numRamBanks = 4; |
| 667 | break; |
| 668 | case 4: |
| 669 | numRamBanks = 16; |
| 670 | break; |
| 671 | default: |
| 672 | printLog("Invalid RAM bank number: %x\nDefaulting to 4 banks\n", ramSize); |
| 673 | numRamBanks = 4; |
| 674 | break; |
| 675 | } |
| 676 | if (MBC == MBC2) |
| 677 | numRamBanks = 1; |
| 678 | } |
| 679 | |
| 680 | if (numRamBanks == 0) |
| 681 | return 0; |
| 682 | |
| 683 | externRam = (u8*)malloc(numRamBanks*0x2000); |
| 684 | |
| 685 | if (gbsMode) |
| 686 | return 0; // GBS files don't get to save. |
| 687 | |
| 688 | // Now load the data. |
| 689 | saveFile = fopen(savename, "r+b"); |
| 690 | int neededFileSize = numRamBanks*0x2000; |
| 691 | if (MBC == MBC3 || MBC == HUC3) |
| 692 | neededFileSize += sizeof(clockStruct); |
| 693 | |
| 694 | int fileSize = 0; |
| 695 | if (saveFile) { |
| 696 | fseek(saveFile, 0, SEEK_END); |
| 697 | fileSize = ftell(saveFile); |
| 698 | fseek(saveFile, 0, SEEK_SET); |