Skip to content

Commit d94fa34

Browse files
Refactor RAM management and update savestate consistency
- Consolidate WRAM/CHRRAM logic into new cartram.c handler. - Remove deprecated mappers.c and update Makefile.common. - Add iNES 2.0 size calculation helpers to cart.c. - Deploy FCEUSTATE_RLSB flags to standardize 16-bit savestate registers. - Increase SFMDATA_SIZE to 256 for larger state structures. - Update and fix miscellaneous mapper logic.
1 parent c98ec01 commit d94fa34

583 files changed

Lines changed: 1081 additions & 891 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile.common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ endif
7171

7272
SOURCES_C += \
7373
$(SOURCE_DIR)/cart.c \
74+
$(SOURCE_DIR)/cartram.c \
7475
$(SOURCE_DIR)/cheat.c \
7576
$(SOURCE_DIR)/crc32.c \
7677
$(SOURCE_DIR)/drawing.c \
@@ -83,7 +84,6 @@ SOURCES_C += \
8384
$(SOURCE_DIR)/gamegenie.c \
8485
$(SOURCE_DIR)/general.c \
8586
$(SOURCE_DIR)/input.c \
86-
$(SOURCE_DIR)/mappers.c \
8787
$(SOURCE_DIR)/md5.c \
8888
$(SOURCE_DIR)/nsf.c \
8989
$(SOURCE_DIR)/nsfe.c \

src/cart.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,15 @@ void SetupCartMirroring(int m, int hard, uint8_t *extra) {
449449
}
450450
mirrorhard = hard;
451451
}
452+
453+
uint32_t GetWRAMSize(const CartInfo *info, uint32_t default_size) {
454+
if (info->iNES2)
455+
return (uint32_t)(info->PRGRamSize + info->PRGRamSaveSize);
456+
return info->battery ? SIZE_8K : default_size;
457+
}
458+
459+
uint32_t GetCHRRAMSize(const CartInfo *info, uint32_t default_size) {
460+
if (info->iNES2)
461+
return (uint32_t)(info->CHRRamSize + info->CHRRamSaveSize);
462+
return default_size;
463+
}

src/cart.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,7 @@ extern uint32_t WRAMSIZE;
160160

161161
extern romData_t ROM;
162162

163+
/* */
164+
uint32_t GetWRAMSize(const CartInfo *info, uint32_t default_size);
165+
uint32_t GetCHRRAMSize(const CartInfo *info, uint32_t default_size);
163166
#endif

src/cartram.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

src/cartram.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
#ifndef _CARTRAM_H
22+
#define _CARTRAM_H
23+
24+
extern uint32_t CHRRAMSRAM;
25+
extern uint32_t WRAMSIZE;
26+
void CartRAM_Init(CartInfo *info,
27+
uint8_t min_wram_kb,
28+
uint8_t min_chrram_kb);
29+
void CartRAM_Close(void);
30+
void CHRRAM_Init(CartInfo *info, uint8_t min_chrram_kb);
31+
void WRAM_Init(CartInfo *info, uint8_t min_wram_kb);
32+
33+
#endif

src/ines.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "state.h"
4242
#include "unif.h"
4343
#include "vsuni.h"
44+
#include "cartram.h"
4445

4546
#include "ines-correct.h"
4647

@@ -180,14 +181,7 @@ static void Cleanup(void) {
180181
FCEU_free(ROM.misc.data);
181182
ROM.misc.data = NULL;
182183
}
183-
if (WRAM) {
184-
FCEU_free(WRAM);
185-
WRAM = NULL;
186-
}
187-
if (CHRRAM) {
188-
FCEU_free(CHRRAM);
189-
CHRRAM = NULL;
190-
}
184+
CartRAM_Close();
191185
}
192186

193187
static void iNESGI(int h) {

src/mappers.c

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/mappers/hw/eeprom_24C0x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* FCEUmm - NES/Famicom Emulator
22
*
33
* Copyright notice for this file:
4-
* Copyright (C) 2025 negativeExponent
4+
* Copyright (C) 2025-2026 negativeExponent
55
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by

src/mappers/hw/eeprom_93Cx6.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* FCEUmm - NES/Famicom Emulator
22
*
33
* Copyright notice for this file:
4-
* Copyright (C) 2023-2024 negativeExponent
4+
* Copyright (C) 2023-2024-2026 negativeExponent
55
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by

src/mappers/hw/fcg.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright notice for this file:
44
* Copyright (C) 2007 CaH4e3
55
* Copyright (C) 2011 FCEUX team
6-
* Copyright (C) 2023-2024 negativeExponent
6+
* Copyright (C) 2023-2024-2026 negativeExponent
77
*
88
* This program is free software; you can redistribute it and/or modify
99
* it under the terms of the GNU General Public License as published by
@@ -53,8 +53,8 @@ static SFORMAT StateRegs[] = {
5353
{ &fcg.mirror, 1, "MIRR" },
5454
{ &fcg.wramEnabled, 1, "WREN" },
5555
{ &fcg.IRQa, 1, "IRQA" },
56-
{ &fcg.IRQCount, 2, "IRQC" },
57-
{ &fcg.IRQLatch, 2, "IRQL" }, /* need for Famicom Jump II - Saikyou no 7 Nin (J) [!] */
56+
{ &fcg.IRQCount, 2 | FCEUSTATE_RLSB, "IRQC" },
57+
{ &fcg.IRQLatch, 2 | FCEUSTATE_RLSB, "IRQL" }, /* need for Famicom Jump II - Saikyou no 7 Nin (J) [!] */
5858
{ 0 }
5959
};
6060

0 commit comments

Comments
 (0)