|
| 1 | +/* FCEUmm - NES/Famicom Emulator |
| 2 | + * |
| 3 | + * Copyright notice for this file: |
| 4 | + * Copyright (C) 2026 negativeExponent |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include "mapinc.h" |
| 22 | +#include "cartram.h" |
| 23 | + |
| 24 | +uint8_t *WRAM = NULL; |
| 25 | +uint8_t *CHRRAM = NULL; |
| 26 | + |
| 27 | +uint32_t WRAMSIZE = 0; |
| 28 | +uint32_t CHRRAMSIZE = 0; |
| 29 | + |
| 30 | +void CartRAM_Close(void) { |
| 31 | + if (WRAM) { |
| 32 | + FCEU_gfree(WRAM); |
| 33 | + WRAM = NULL; |
| 34 | + } |
| 35 | + if (CHRRAM) { |
| 36 | + FCEU_gfree(CHRRAM); |
| 37 | + CHRRAM = NULL; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void CartRAM_Init(CartInfo *info, |
| 42 | + uint8_t min_wram_kb, |
| 43 | + uint8_t min_chrram_kb) { |
| 44 | + WRAMSIZE = GetWRAMSize(info, (uint32_t)min_wram_kb * 1024); |
| 45 | + if (WRAMSIZE) { |
| 46 | + WRAM = (uint8_t *)FCEU_gmalloc(WRAMSIZE); |
| 47 | + SetupCartPRGMapping(0x10, WRAM, WRAMSIZE, TRUE); |
| 48 | + AddExState(WRAM, WRAMSIZE, 0, "WRAM"); |
| 49 | + if (info->battery && (info->PRGRamSaveSize || !info->iNES2)) { |
| 50 | + info->SaveGame[0] = WRAM; |
| 51 | + info->SaveGameLen[0] = info->iNES2? (uint32_t)info->PRGRamSaveSize: WRAMSIZE; |
| 52 | + } |
| 53 | + } |
| 54 | + CHRRAMSIZE = GetCHRRAMSize(info, (uint32_t)min_chrram_kb * 1024); |
| 55 | + if (ROM.chr.size == 0) { |
| 56 | + CHRRAMSIZE = |
| 57 | + 0; /* If there is no CHR-ROM, then any CHR-RAM will not be "extra" |
| 58 | + and therefore will be handled by ines.c, not here. */ |
| 59 | + } |
| 60 | + if (CHRRAMSIZE) { |
| 61 | + CHRRAM = (uint8_t *)FCEU_gmalloc(CHRRAMSIZE); |
| 62 | + SetupCartCHRMapping(0x10, CHRRAM, CHRRAMSIZE, TRUE); |
| 63 | + AddExState(CHRRAM, CHRRAMSIZE, 0, "CRAM"); |
| 64 | + if (info->battery && (info->CHRRamSaveSize || !info->iNES2)) { |
| 65 | + info->SaveGame[info->SaveGameLen[0] ? 1 : 0] = CHRRAM; |
| 66 | + info->SaveGameLen[info->SaveGameLen[0] ? 1 : 0] = |
| 67 | + info->iNES2 ? (uint32_t)info->CHRRamSaveSize : CHRRAMSIZE; |
| 68 | + } |
| 69 | + } |
| 70 | + FCEU_printf("cart ram, wram size : %d\n", WRAMSIZE); |
| 71 | +} |
| 72 | + |
| 73 | +void CHRRAM_Init(CartInfo *info, uint8_t min_chrram_kb) { |
| 74 | + CartRAM_Init(info, 0, min_chrram_kb); |
| 75 | +} |
| 76 | + |
| 77 | +void WRAM_Init(CartInfo *info, uint8_t min_wram_kb) { |
| 78 | + CartRAM_Init(info, min_wram_kb, 0); |
| 79 | +} |
0 commit comments