From 8c03fffbf0c246b77b5d60604405aa898c58e6ea Mon Sep 17 00:00:00 2001 From: Fabio Battaglia Date: Thu, 7 Jan 2010 16:07:22 +0000 Subject: Nintendo64: Add FlashRAM support for saving if a compatible cart is available. svn-id: r47126 --- backends/platform/n64/Makefile | 4 +- backends/platform/n64/framfs_save_manager.cpp | 72 ++++++++++++++ backends/platform/n64/framfs_save_manager.h | 132 ++++++++++++++++++++++++++ backends/platform/n64/osys_n64_base.cpp | 36 ++++--- backends/platform/n64/pakfs_save_manager.cpp | 2 +- backends/platform/n64/pakfs_save_manager.h | 21 ++-- 6 files changed, 244 insertions(+), 23 deletions(-) create mode 100644 backends/platform/n64/framfs_save_manager.cpp create mode 100644 backends/platform/n64/framfs_save_manager.h diff --git a/backends/platform/n64/Makefile b/backends/platform/n64/Makefile index 8aa0a1e145..b5e0d2d6a4 100644 --- a/backends/platform/n64/Makefile +++ b/backends/platform/n64/Makefile @@ -15,7 +15,7 @@ AR = $(GCCN64PREFIX)ar cru RANLIB = $(GCCN64PREFIX)ranlib DEFINES += -D__N64__ -DLIMIT_FPS -DNONSTANDARD_PORT -DDISABLE_DEFAULT_SAVEFILEMANAGER -DDISABLE_TEXT_CONSOLE -DDISABLE_COMMAND_LINE -DDISABLE_FANCY_THEMES -DDISABLE_DOSBOX_OPL -DENABLE_VKEYBD -DUSE_ZLIB -LIBS += -lpakfs -ln64 -ln64utils -lromfs +LIBS += -lpakfs -lframfs -ln64 -ln64utils -lromfs DEFINES += -D_ENABLE_DEBUG_ @@ -64,7 +64,7 @@ ENABLE_SCUMM=$(ENABLED) #ENABLE_MADE = $(ENABLED) #ENABLE_SAGA = $(ENABLED) -OBJS := nintendo64.o osys_n64_base.o osys_n64_events.o osys_n64_utilities.o pakfs_save_manager.o +OBJS := nintendo64.o osys_n64_base.o osys_n64_events.o osys_n64_utilities.o pakfs_save_manager.o framfs_save_manager.o include $(srcdir)/Makefile.common diff --git a/backends/platform/n64/framfs_save_manager.cpp b/backends/platform/n64/framfs_save_manager.cpp new file mode 100644 index 0000000000..206425943e --- /dev/null +++ b/backends/platform/n64/framfs_save_manager.cpp @@ -0,0 +1,72 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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 + +#include "framfs_save_manager.h" + +bool fram_deleteSaveGame(const char *filename) { + int res = framfs_removeFile(filename); + + return (res == 0); +} + +uint32 InFRAMSave::read(void *buf, uint32 cnt) { + return framfs_read(buf, 1, cnt, fd); +} + +bool InFRAMSave::seek(int32 offs, int whence) { + framfs_seek(fd, offs, whence); + + return true; +} + +bool InFRAMSave::skip(uint32 offset) { + framfs_seek(fd, offset, SEEK_CUR); + + return true; +} + +uint32 OutFRAMSave::write(const void *buf, uint32 cnt) { + return framfs_write(buf, 1, cnt, fd); +} + +Common::StringList FRAMSaveManager::listSavefiles(const Common::String &pattern) { + FRAMDIR *dirp = framfs_opendir(); + framfs_dirent *dp; + Common::StringList list; + Common::String *fname; + + while ((dp = framfs_readdir(dirp)) != NULL) { + fname = new Common::String(dp->entryname); + if (fname->matchString(pattern, false, false)) + list.push_back(dp->entryname); + + delete fname; + free(dp); + } + + framfs_closedir(dirp); + + return list; +} + diff --git a/backends/platform/n64/framfs_save_manager.h b/backends/platform/n64/framfs_save_manager.h new file mode 100644 index 0000000000..194dec3fc6 --- /dev/null +++ b/backends/platform/n64/framfs_save_manager.h @@ -0,0 +1,132 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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. + * + */ + +#ifndef __FRAMFS_SAVE_MANAGER__ +#define __FRAMFS_SAVE_MANAGER__ + +#include +#include + +#include // N64 FramFS library + +bool fram_deleteSaveGame(const char *filename); + +class InFRAMSave : public Common::InSaveFile { +private: + FRAMFILE *fd; + + uint32 read(void *buf, uint32 cnt); + bool skip(uint32 offset); + bool seek(int32 offs, int whence); + +public: + InFRAMSave() : fd(NULL) { } + + ~InFRAMSave() { + if (fd != NULL) + framfs_close(fd); + } + + bool eos() const { + return framfs_eof(fd); + } + void clearErr() { + framfs_clearerr(fd); + } + int32 pos() const { + return framfs_tell(fd); + } + int32 size() const { + return fd->size; + } + + bool readSaveGame(const char *filename) { + fd = framfs_open(filename, "r"); + return (fd != NULL); + } +}; + +class OutFRAMSave : public Common::OutSaveFile { +private: + FRAMFILE *fd; + +public: + uint32 write(const void *buf, uint32 cnt); + + OutFRAMSave(const char *_filename) : fd(NULL) { + fd = framfs_open(_filename, "w"); + } + + ~OutFRAMSave() { + if (fd != NULL) { + finalize(); + framfs_close(fd); + } + } + + bool err() const { + if (fd) + return (framfs_error(fd) == 1); + else + return true; + } + void clearErr() { + framfs_clearerr(fd); + } + void finalize() { + framfs_flush(fd); + } +}; + +class FRAMSaveManager : public Common::SaveFileManager { +public: + + virtual Common::OutSaveFile *openForSaving(const Common::String &filename) { + OutFRAMSave *s = new OutFRAMSave(filename.c_str()); + if (!s->err()) { + return Common::wrapCompressedWriteStream(s); + } else { + delete s; + return 0; + } + } + + virtual Common::InSaveFile *openForLoading(const Common::String &filename) { + InFRAMSave *s = new InFRAMSave(); + if (s->readSaveGame(filename.c_str())) { + return Common::wrapCompressedReadStream(s); + } else { + delete s; + return 0; + } + } + + virtual bool removeSavefile(const Common::String &filename) { + return ::fram_deleteSaveGame(filename.c_str()); + } + + virtual Common::StringList listSavefiles(const Common::String &pattern); +}; + + +#endif + diff --git a/backends/platform/n64/osys_n64_base.cpp b/backends/platform/n64/osys_n64_base.cpp index b39fcb3295..c5fa1a5d58 100644 --- a/backends/platform/n64/osys_n64_base.cpp +++ b/backends/platform/n64/osys_n64_base.cpp @@ -26,6 +26,7 @@ #include "osys_n64.h" #include "pakfs_save_manager.h" +#include "framfs_save_manager.h" #include "backends/fs/n64/n64-fs-factory.h" extern uint8 _romfs; // Defined by linker (used to calculate position of romfs image) @@ -55,19 +56,6 @@ OSystem_N64::OSystem_N64() { // Init PI interface PI_Init(); - // Init Controller Pak - initPakFs(); - - // Use the first controller pak found - uint8 ctrl_num; - for (ctrl_num = 0; ctrl_num < 4; ctrl_num++) { - int8 pak_type = identifyPak(ctrl_num); - if (pak_type == 1) { - loadPakData(ctrl_num); - break; - } - } - // Screen size _screenWidth = 320; _screenHeight = 240; @@ -164,7 +152,27 @@ void OSystem_N64::initBackend() { ConfMan.setBool("FM_medium_quality", true); ConfMan.set("gui_theme", "modern"); // In case of modern theme being present, use it. - _savefile = new PAKSaveManager(); + FRAM_Init(); + + if (FRAM_Detect()) { // Use FlashRAM + initFramFS(); + _savefile = new FRAMSaveManager(); + } else { // Use PakFS + // Init Controller Pak + initPakFs(); + + // Use the first controller pak found + uint8 ctrl_num; + for (ctrl_num = 0; ctrl_num < 4; ctrl_num++) { + int8 pak_type = identifyPak(ctrl_num); + if (pak_type == 1) { + loadPakData(ctrl_num); + break; + } + } + + _savefile = new PAKSaveManager(); + } _mixer = new Audio::MixerImpl(this); _mixer->setReady(false); diff --git a/backends/platform/n64/pakfs_save_manager.cpp b/backends/platform/n64/pakfs_save_manager.cpp index 5499462d68..39bb1b4b98 100644 --- a/backends/platform/n64/pakfs_save_manager.cpp +++ b/backends/platform/n64/pakfs_save_manager.cpp @@ -24,7 +24,7 @@ #include "pakfs_save_manager.h" -bool deleteSaveGame(const char *filename) { +bool pakfs_deleteSaveGame(const char *filename) { int res = removeFileOnPak(filename); flushCurrentPakData(); diff --git a/backends/platform/n64/pakfs_save_manager.h b/backends/platform/n64/pakfs_save_manager.h index d3e4b94d39..d31e6fcfd8 100644 --- a/backends/platform/n64/pakfs_save_manager.h +++ b/backends/platform/n64/pakfs_save_manager.h @@ -28,7 +28,7 @@ #include // N64 PakFS library -bool deleteSaveGame(const char *filename); +bool pakfs_deleteSaveGame(const char *filename); class InPAKSave : public Common::InSaveFile { private: @@ -39,7 +39,7 @@ private: bool seek(int32 offs, int whence); public: - InPAKSave() : fd(0) { } + InPAKSave() : fd(NULL) { } ~InPAKSave() { if (fd != NULL) @@ -72,7 +72,7 @@ private: public: uint32 write(const void *buf, uint32 cnt); - OutPAKSave(const char *_filename) { + OutPAKSave(const char *_filename) : fd(NULL) { fd = pakfs_open(_filename, "w"); } @@ -85,7 +85,10 @@ public: } bool err() const { - return pakfs_error(fd); + if (fd) + return (pakfs_error(fd) == 1); + else + return true; } void clearErr() { pakfs_clearerr(fd); @@ -99,7 +102,13 @@ class PAKSaveManager : public Common::SaveFileManager { public: virtual Common::OutSaveFile *openForSaving(const Common::String &filename) { - return Common::wrapCompressedWriteStream(new OutPAKSave(filename.c_str())); + OutPAKSave *s = new OutPAKSave(filename.c_str()); + if (!s->err()) { + return Common::wrapCompressedWriteStream(s); + } else { + delete s; + return NULL; + } } virtual Common::InSaveFile *openForLoading(const Common::String &filename) { @@ -113,7 +122,7 @@ public: } virtual bool removeSavefile(const Common::String &filename) { - return ::deleteSaveGame(filename.c_str()); + return ::pakfs_deleteSaveGame(filename.c_str()); } virtual Common::StringList listSavefiles(const Common::String &pattern); -- cgit v1.2.3