Skip to content

Commit

Permalink
Much better flash loading/saving.
Browse files Browse the repository at this point in the history
  • Loading branch information
FluBBaOfWard committed Sep 24, 2021
1 parent f85ad09 commit e32337c
Show file tree
Hide file tree
Showing 14 changed files with 1,350 additions and 61 deletions.
3 changes: 2 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ NGPDS revision history
-=-=-=-=-=-=-=-=-=-=-=-


V0.4.9 - 2021-09-12 (FluBBa)
V0.4.9 - 2021-09-24 (FluBBa)
Much better flash emulation.

V0.4.8 - 2021-09-11 (FluBBa)
Added stereo sound.
Expand Down
4 changes: 4 additions & 0 deletions NGPDS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
9CBC0C8D26DD7BD50039D17E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
9CBC0C8E26DD7C2A0039D17E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
9CBC0C8F26DD7D0F0039D17E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
9CBEE11526FBB7F30095347A /* GameInfo.s */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.asm; path = GameInfo.s; sourceTree = "<group>"; };
9CBEE17A26FCE7F10095347A /* NGPFlash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGPFlash.h; sourceTree = "<group>"; };
9CC013FC2668FDD9005714F7 /* Emubase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Emubase.h; sourceTree = "<group>"; };
9CC013FE26694C6C005714F7 /* TLCS900H_int.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TLCS900H_int.h; sourceTree = "<group>"; };
9CC0F92B26B87B570035478F /* NGPHW.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = NGPHW.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -263,6 +265,7 @@
9CC0F92E26D1073F0035478F /* AsmHleBios.s */,
9C50820525D45A9A00A8E1BA /* Cart.h */,
9C50820F25D45A9B00A8E1BA /* Cart.s */,
9CBEE17A26FCE7F10095347A /* NGPFlash.h */,
9C790E762670EDAA000A3506 /* NGPFlash.i */,
9C790E752670EDA9000A3506 /* NGPFlash.s */,
35E683AD1C66453300FCFE8A /* cpu.h */,
Expand All @@ -279,6 +282,7 @@
9C50820B25D45A9B00A8E1BA /* FileHandling.h */,
9C50820A25D45A9B00A8E1BA /* FileHandling.c */,
9C4C21C226E12781007D0280 /* SpeedHacks.s */,
9CBEE11526FBB7F30095347A /* GameInfo.s */,
);
path = source;
sourceTree = "<group>";
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ This is a SNK Neogeo Pocket (Color) emulator for the Nintendo DS.

## How to use:

First DLDI patch the emulator for your flashcard if that is needed.
Depending on your flashcart you might have to DLDI patch the emulator.
Create a folder named "ngpds" in either the root of your flash card or in the
data folder. Now put game/bios files into a folder where you have roms.
Note! You need a bios to be able to save in game.
The save file should be compatible with most other NeoGeo Pocket emulators.

When the emulator starts, you can either press L+R or tap on the screen to open
up the menu.
Expand Down Expand Up @@ -78,6 +79,7 @@ To select between the tabs use R & L or the touchscreen.
Huge thanks to Loopy for the incredible PocketNES, without it this emu would
probably never have been made.
Thanks to:
Flavor & Koyote for NGP info.
Dwedit for help and inspiration with a lot of things.


Expand Down
2 changes: 1 addition & 1 deletion source/Cart.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern u8 g_lang;
extern u8 g_paletteBank;

extern u8 biosSpace[0x10000];
extern void *romSpacePtr;
extern u8 *romSpacePtr;
extern void *g_BIOSBASE_COLOR;
extern void *g_BIOSBASE_BW;

Expand Down
217 changes: 200 additions & 17 deletions source/FileHandling.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
#include "io.h"
#include "Memory.h"
#include "NeoGeoPocket.h"
#include "NGPFlash.h"

static const char *const folderName = "ngpds";
static const char *const settingName = "settings.cfg";

int getSaveStartAddress(void);

ConfigData cfg;

//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -68,7 +67,8 @@ int initSettings() {
g_paletteBank = col;
return 0;
}
int updateSettingsFromNGP() {

bool updateSettingsFromNGP() {
int val;
bool changed = false;

Expand Down Expand Up @@ -115,6 +115,7 @@ int updateSettingsFromNGP() {

return changed;
}

int loadSettings() {
FILE *file;

Expand Down Expand Up @@ -144,6 +145,7 @@ int loadSettings() {
infoOutput("Settings loaded.");
return 0;
}

void saveSettings() {
FILE *file;

Expand All @@ -168,50 +170,230 @@ void saveSettings() {
}
}

void loadNVRAM() {
int adr;
int len;
void loadNVRAM_() {
// int adr;
// int len;
void *space;
FILE *file;
char flashName[FILENAMEMAXLENGTH];
NgpFlashFile flashHdr;

if (findFolder(folderName)) {
return;
}
strlcpy(flashName, currentFilename, sizeof(flashName));
strlcat(flashName, ".fla", sizeof(flashName));
if ( (file = fopen(flashName, "r")) ) {
fread(&adr, 1, 4, file);
fread(&len, 1, 4, file);
space = romSpacePtr + adr;
fread(space, 1, len, file);
infoOutput("Loaded flash.");
fread(&flashHdr, 1, sizeof(flashHdr), file);
if (flashHdr.magic == NGPF_MAGIC) {
memcpy(getFlashLOBlocksAddress(), &flashHdr.blocksLOInfo, 35);
memcpy(getFlashHIBlocksAddress(), &flashHdr.blocksHIInfo, 35);
space = romSpacePtr + flashHdr.addressLO;
fread(space, 1, flashHdr.sizeLO, file);
if ( flashHdr.sizeHI != 0) {
fread(space, 1, flashHdr.sizeHI, file);
}
infoOutput("Loaded flash.");
}
fclose(file);
}
return;
}

void saveNVRAM() {
void saveNVRAM_() {
int adr = getSaveStartAddress();
int len = g_romSize - adr;
void *space = romSpacePtr + adr;
FILE *file;
char flashName[FILENAMEMAXLENGTH];
NgpFlashFile flashHdr;

flashHdr.magic = NGPF_MAGIC;
memcpy(&flashHdr.blocksLOInfo, getFlashLOBlocksAddress(), 35);
memcpy(&flashHdr.blocksHIInfo, getFlashHIBlocksAddress(), 35);
flashHdr.addressLO = adr;
flashHdr.sizeLO = len;
flashHdr.addressHI = 0;
flashHdr.sizeHI = 0;

if (findFolder(folderName)) {
return;
}
strlcpy(flashName, currentFilename, sizeof(flashName));
strlcat(flashName, ".fla", sizeof(flashName));
if ( (file = fopen(flashName, "w")) ) {
fwrite(&adr, 1, 4, file);
fwrite(&len, 1, 4, file);
fwrite((void *)space, 1, len, file);
fwrite(&flashHdr, 1, sizeof(flashHdr), file);
fwrite(space, 1, flashHdr.sizeLO, file);
if ( flashHdr.sizeHI != 0) {
fwrite(space, 1, flashHdr.sizeHI, file);
}
infoOutput("Saved flash.");
fclose(file);
}
}


//void writeSaveGameFile() {
void saveNVRAM() {
// Find the dirty blocks and write them to the .fla file
int totalBlocks = MAX_BLOCKS;
int i;
int chip;
FILE *ngfFile;
char flashName[FILENAMEMAXLENGTH];

int bytes;
int chipCount = (flashSize != 0x400000) ? 1 : 2;
NgfHeader header;
NgfBlock block;

header.version = 0x53;
header.blockCount = 0;
header.fileLen = sizeof(NgfHeader);

// Add them all up, first
for (chip=0; chip<chipCount; chip++) {
for (i=0; i<totalBlocks; i++) {
if (isBlockDirty(chip,i)) {
header.blockCount++;
header.fileLen += getBlockSize(i);
}
}
}

header.fileLen += header.blockCount * sizeof(NgfBlock);

if (findFolder(folderName)) {
return;
}
strlcpy(flashName, currentFilename, sizeof(flashName));
strlcat(flashName, ".fla", sizeof(flashName));
if ( !(ngfFile = fopen(flashName, "w")) ) {
infoOutput("Couldn't open file:");
infoOutput(flashName);
return;
}

if (fwrite(&header, 1, sizeof(NgfHeader), ngfFile) != sizeof(NgfHeader)) {
infoOutput("Couldn't write correct number of bytes.");
fclose(ngfFile);
return;
}

for (chip=0; chip<chipCount; chip++) {
for (i=0; i<totalBlocks; i++) {
if (isBlockDirty(chip,i)) {
block.len = getBlockSize(i);
if (chip == 0) {
block.ngpAddr = getBlockOffset(i)+0x200000;
}
else {
block.ngpAddr = getBlockOffset(i)+0x800000;
}

if (fwrite(&block, 1, sizeof(NgfBlock), ngfFile) != sizeof(NgfBlock)) {
infoOutput("Couldn't write correct number of bytes.");
fclose(ngfFile);
return;
}

if (chip == 0) {
bytes = fwrite(&romSpacePtr[getBlockOffset(i)], 1, block.len, ngfFile);
}
else {
bytes = fwrite(&romSpacePtr[getBlockOffset(i)+0x200000], 1, block.len, ngfFile);
}
if (bytes != block.len)
{
infoOutput("Couldn't write correct number of bytes.");
fclose(ngfFile);
return;
}
}
}
}

infoOutput("Saved flash.");
fclose(ngfFile);
}

//void loadSaveGameFile()
void loadNVRAM() {
// Find the NGF file and read it in
FILE *ngfFile;
int i;
NgfHeader header;
NgfBlock block;
char flashName[FILENAMEMAXLENGTH];

if (findFolder(folderName)) {
return;
}
strlcpy(flashName, currentFilename, sizeof(flashName));
strlcat(flashName, ".fla", sizeof(flashName));
if ( !(ngfFile = fopen(flashName, "r")) ) {
infoOutput("Couldn't open flash file:");
infoOutput(flashName);
return;
}

if (fread(&header, 1, sizeof(NgfHeader), ngfFile) != sizeof(NgfHeader)) {
infoOutput("Bad flash file:");
infoOutput(flashName);
fclose(ngfFile);
return;
}

if (header.version != 0x53) {
infoOutput("Bad flash file version:");
infoOutput(flashName);
fclose(ngfFile);
return;
}

if (header.blockCount > MAX_BLOCKS) {
infoOutput("Too many blocks in flash file:");
infoOutput(flashName);
fclose(ngfFile);
return;
}

// Loop through the blocks and insert them into mainrom
for (i=0; i < header.blockCount; i++) {
if (fread(&block, 1, sizeof(NgfBlock), ngfFile) != sizeof(NgfBlock)) {
infoOutput("Couldn't read correct number of header bytes.");
fclose(ngfFile);
return;
}

if (!((block.ngpAddr >= 0x200000 && block.ngpAddr < 0x400000)
|| (block.ngpAddr >= 0x800000 && block.ngpAddr < 0xA00000) )) {
infoOutput("Invalid block header in flash.");
char errAdr[8];
int2HexStr(errAdr, block.ngpAddr);
infoOutput(errAdr);
fclose(ngfFile);
return;
}
if (block.ngpAddr >= 0x800000) {
block.ngpAddr -= 0x600000;
markBlockDirty(1, getBlockFromAddress(block.ngpAddr-0x200000));
}
else if (block.ngpAddr >= 0x200000) {
block.ngpAddr -= 0x200000;
markBlockDirty(0, getBlockFromAddress(block.ngpAddr));
}
if (fread(&romSpacePtr[block.ngpAddr], 1, block.len, ngfFile) != block.len) {
infoOutput("Couldn't read correct number of block bytes.");
fclose(ngfFile);
return;
}
}

infoOutput("Loaded flash.");
fclose(ngfFile);
}

void loadState(void) {
u32 *statePtr;
FILE *file;
Expand All @@ -235,6 +417,7 @@ void loadState(void) {
fclose(file);
}
}

void saveState(void) {
u32 *statePtr;
FILE *file;
Expand All @@ -260,7 +443,7 @@ void saveState(void) {
}
}

// Hold down the power button for ~40 frames.
/// Hold down the power button for ~40 frames.
static void turnPowerOff(void) {
int i;
if ( g_BIOSBASE_COLOR != NULL ) {
Expand All @@ -275,7 +458,7 @@ static void turnPowerOff(void) {
}
}

// Hold down the power button for ~40 frames.
/// Hold down the power button for ~40 frames.
static void turnPowerOn(void) {
int i;
if ( g_BIOSBASE_COLOR != NULL ) {
Expand Down
Loading

0 comments on commit e32337c

Please sign in to comment.