2025-09-01 01:13:46 +02:00
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file :
* Copyright ( C ) 2025 NewRisingSun
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "mapinc.h"
2025-09-12 11:22:37 +02:00
# include "cartram.h"
2025-09-01 01:13:46 +02:00
2025-09-12 11:22:37 +02:00
static uint8 * CHRRAMData = NULL ;
2025-09-01 01:13:46 +02:00
static uint8 * WRAMData = NULL ;
2025-09-12 11:22:37 +02:00
uint32 CHRRAMSize = 0 ;
2025-09-01 01:13:46 +02:00
uint32 WRAMSize = 0 ;
2025-09-12 11:22:37 +02:00
void CartRAM_close ( void ) { /* Need to combine this in one function to avoid the problem of having to properly cascade two separate Close() functions for WRAM and CHR-RAM each */
2025-09-01 01:13:46 +02:00
if ( WRAMData ) {
FCEU_gfree ( WRAMData ) ;
2025-09-12 21:59:27 +02:00
WRAMData = NULL ;
2025-09-01 01:13:46 +02:00
}
2025-09-12 11:22:37 +02:00
if ( CHRRAMData ) {
FCEU_gfree ( CHRRAMData ) ;
2025-09-12 21:59:27 +02:00
CHRRAMData = NULL ;
2025-09-12 11:22:37 +02:00
}
2025-09-01 01:13:46 +02:00
}
2025-09-12 11:22:37 +02:00
void CartRAM_init ( CartInfo * info , uint8 defaultWRAMSizeKiB , uint8 defaultCHRRAMSizeKiB ) {
2025-09-12 21:59:27 +02:00
WRAMSize = info - > iNES2 ? ( info - > PRGRamSize + info - > PRGRamSaveSize ) : ( defaultWRAMSizeKiB * 1024 ) ;
2025-09-01 01:13:46 +02:00
if ( WRAMSize ) {
2025-09-12 21:59:27 +02:00
WRAMData = ( uint8 * ) FCEU_gmalloc ( WRAMSize ) ;
2025-09-01 01:13:46 +02:00
SetupCartPRGMapping ( 0x10 , WRAMData , WRAMSize , 1 ) ;
AddExState ( WRAMData , WRAMSize , 0 , " WRAM " ) ;
2025-09-12 21:59:27 +02:00
if ( info - > battery & & ( info - > PRGRamSaveSize | | ! info - > iNES2 ) ) {
info - > SaveGame [ 0 ] = WRAMData ;
info - > SaveGameLen [ 0 ] = info - > iNES2 ? info - > PRGRamSaveSize : WRAMSize ;
2025-09-01 01:13:46 +02:00
}
}
2025-09-12 21:59:27 +02:00
CHRRAMSize = info - > iNES2 ? ( info - > CHRRamSize + info - > CHRRamSaveSize ) : ( defaultCHRRAMSizeKiB * 1024 ) ;
2025-09-12 11:22:37 +02:00
if ( ROM_size = = 0 ) CHRRAMSize = 0 ; /* If there is no CHR-ROM, then any CHR-RAM will not be "extra" and therefore will be handled by ines.c, not here. */
if ( CHRRAMSize ) {
2025-09-12 21:59:27 +02:00
CHRRAMData = ( uint8 * ) FCEU_gmalloc ( CHRRAMSize ) ;
2025-09-12 11:22:37 +02:00
SetupCartCHRMapping ( 0x10 , CHRRAMData , CHRRAMSize , 1 ) ;
AddExState ( CHRRAMData , CHRRAMSize , 0 , " CRAM " ) ;
2025-09-12 21:59:27 +02:00
if ( info - > battery & & ( info - > CHRRamSaveSize | | ! info - > iNES2 ) ) {
info - > SaveGame [ info - > SaveGameLen [ 0 ] ? 1 : 0 ] = CHRRAMData ;
info - > SaveGameLen [ info - > SaveGameLen [ 0 ] ? 1 : 0 ] = info - > iNES2 ? info - > CHRRamSaveSize : CHRRAMSize ;
2025-09-12 11:22:37 +02:00
}
}
2025-09-12 21:59:27 +02:00
if ( WRAMSize | | CHRRAMSize ) info - > Close = CartRAM_close ;
2025-09-12 11:22:37 +02:00
}
void CHRRAM_init ( CartInfo * info , uint8 defaultCHRRAMSizeKiB ) {
CartRAM_init ( info , 0 , defaultCHRRAMSizeKiB ) ;
}
void WRAM_init ( CartInfo * info , uint8 defaultWRAMSizeKiB ) {
CartRAM_init ( info , defaultWRAMSizeKiB , 0 ) ;
2025-09-01 01:13:46 +02:00
}