aboutsummaryrefslogtreecommitdiff
path: root/src/GBACart.cpp
diff options
context:
space:
mode:
authorRaphaël Zumer <rzumer@tebako.net>2019-12-08 13:46:51 -0500
committerRaphaël Zumer <rzumer@tebako.net>2019-12-08 15:30:07 -0500
commitd86ee1d5bfb76d4efd89f4056beece374926500a (patch)
treee1a660ef2b9581e1f2e483a18412f26299700997 /src/GBACart.cpp
parentf21347c918934441a90a2b18a928535e0dc854a9 (diff)
Add GBA cart model and allow reading from it
Diffstat (limited to 'src/GBACart.cpp')
-rw-r--r--src/GBACart.cpp197
1 files changed, 197 insertions, 0 deletions
diff --git a/src/GBACart.cpp b/src/GBACart.cpp
new file mode 100644
index 0000000..7c2faad
--- /dev/null
+++ b/src/GBACart.cpp
@@ -0,0 +1,197 @@
+/*
+ Copyright 2019 Arisotura, Raphaël Zumer
+
+ This file is part of melonDS.
+
+ melonDS 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 3 of the License, or (at your option)
+ any later version.
+
+ melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include "GBACart.h"
+#include "CRC32.h"
+#include "Platform.h"
+
+
+namespace GBACart_SRAM
+{
+
+u8* SRAM;
+u32 SRAMLength;
+
+char SRAMPath[1024];
+
+
+bool Init()
+{
+ SRAM = NULL;
+ return true;
+}
+
+void DeInit()
+{
+ if (SRAM) delete[] SRAM;
+}
+
+void Reset()
+{
+ if (SRAM) delete[] SRAM;
+ SRAM = NULL;
+}
+
+void DoSavestate(Savestate* file)
+{
+ // TODO?
+}
+
+void LoadSave(const char* path)
+{
+ if (SRAM) delete[] SRAM;
+
+ strncpy(SRAMPath, path, 1023);
+ SRAMPath[1023] = '\0';
+
+ FILE* f = Platform::OpenFile(path, "rb");
+ if (f)
+ {
+ fseek(f, 0, SEEK_END);
+ SRAMLength = (u32)ftell(f);
+ SRAM = new u8[SRAMLength];
+
+ fseek(f, 0, SEEK_SET);
+ fread(SRAM, SRAMLength, 1, f);
+
+ fclose(f);
+ }
+ else
+ {
+ int SRAMLength = 65536; // max GBA SRAM size
+
+ SRAM = new u8[SRAMLength];
+ memset(SRAM, 0xFF, SRAMLength);
+ }
+}
+
+void RelocateSave(const char* path, bool write)
+{
+ if (!write)
+ {
+ LoadSave(path); // lazy
+ return;
+ }
+
+ strncpy(SRAMPath, path, 1023);
+ SRAMPath[1023] = '\0';
+
+ FILE* f = Platform::OpenFile(path, "wb");
+ if (!f)
+ {
+ printf("GBACart_SRAM::RelocateSave: failed to create new file. fuck\n");
+ return;
+ }
+
+ fwrite(SRAM, SRAMLength, 1, f);
+ fclose(f);
+}
+
+}
+
+
+namespace GBACart
+{
+
+bool CartInserted;
+u8* CartROM;
+u32 CartROMSize;
+u32 CartCRC;
+u32 CartID;
+
+
+bool Init()
+{
+ if (!GBACart_SRAM::Init()) return false;
+
+ CartROM = NULL;
+
+ return true;
+}
+
+void DeInit()
+{
+ if (CartROM) delete[] CartROM;
+
+ GBACart_SRAM::DeInit();
+}
+
+void Reset()
+{
+ CartInserted = false;
+ if (CartROM) delete[] CartROM;
+ CartROM = NULL;
+ CartROMSize = 0;
+
+ GBACart_SRAM::Reset();
+}
+
+void DoSavestate(Savestate* file)
+{
+ // TODO?
+}
+
+bool LoadROM(const char* path, const char* sram)
+{
+ FILE* f = Platform::OpenFile(path, "rb");
+ if (!f)
+ {
+ return false;
+ }
+
+ fseek(f, 0, SEEK_END);
+ u32 len = (u32)ftell(f);
+
+ CartROMSize = 0x200;
+ while (CartROMSize < len)
+ CartROMSize <<= 1;
+
+ u32 gamecode;
+ fseek(f, 0xAC, SEEK_SET);
+ fread(&gamecode, 4, 1, f);
+ printf("Game code: %c%c%c%c\n", gamecode&0xFF, (gamecode>>8)&0xFF, (gamecode>>16)&0xFF, gamecode>>24);
+
+ CartROM = new u8[CartROMSize];
+ memset(CartROM, 0, CartROMSize);
+ fseek(f, 0, SEEK_SET);
+ fread(CartROM, 1, len, f);
+
+ fclose(f);
+ //CartROM = f;
+
+ CartCRC = CRC32(CartROM, CartROMSize);
+ printf("ROM CRC32: %08X\n", CartCRC);
+
+ CartInserted = true;
+
+ // save
+ printf("Save file: %s\n", sram);
+ GBACart_SRAM::LoadSave(sram);
+
+ return true;
+}
+
+void RelocateSave(const char* path, bool write)
+{
+ // derp herp
+ GBACart_SRAM::RelocateSave(path, write);
+}
+
+}