From 7b5938e99213de50f7337f2689c339e781280615 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 23 Jul 2023 12:37:02 +0100
Subject: [PATCH] chore: initial hardcoded ram loading support

---
 frontends/sdl/src/main.rs |  5 ++++-
 src/gb.rs                 | 20 +++++++++++++++-----
 src/rom.rs                |  8 ++++++--
 src/test.rs               |  2 +-
 4 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs
index 6e510715..d0b54912 100644
--- a/frontends/sdl/src/main.rs
+++ b/frontends/sdl/src/main.rs
@@ -203,7 +203,10 @@ impl Emulator {
 
     pub fn load_rom(&mut self, path: Option<&str>) {
         let path_res = path.unwrap_or(&self.rom_path);
-        let rom = self.system.load_rom_file(path_res);
+        let rom = self.system.load_rom_file(
+            path_res,
+            Some("C:/repo.other/boytacean/res/roms.prop/super_mario_2.sav"),
+        );
         println!(
             "========= Cartridge =========\n{}\n=============================",
             rom
diff --git a/src/gb.rs b/src/gb.rs
index 87b2379f..6fc26c29 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -642,7 +642,7 @@ impl GameBoy {
     }
 
     pub fn set_ram_data(&mut self, ram_data: Vec<u8>) {
-        self.mmu().rom().set_ram_data(ram_data)
+        self.mmu().rom().set_ram_data(&ram_data)
     }
 
     pub fn registers(&mut self) -> Registers {
@@ -974,15 +974,25 @@ impl GameBoy {
         self.load_boot_file(BootRom::Cgb);
     }
 
-    pub fn load_rom(&mut self, data: &[u8]) -> &mut Cartridge {
-        let rom = Cartridge::from_data(data);
+    pub fn load_rom(&mut self, data: &[u8], ram_data: Option<&[u8]>) -> &mut Cartridge {
+        let mut rom = Cartridge::from_data(data);
+        match ram_data {
+            Some(ram_data) => rom.set_ram_data(ram_data),
+            None => (),
+        }
         self.mmu().set_rom(rom);
         self.mmu().rom()
     }
 
-    pub fn load_rom_file(&mut self, path: &str) -> &mut Cartridge {
+    pub fn load_rom_file(&mut self, path: &str, ram_path: Option<&str>) -> &mut Cartridge {
         let data = read_file(path);
-        self.load_rom(&data)
+        match ram_path {
+            Some(ram_path) => {
+                let ram_data = read_file(ram_path);
+                self.load_rom(&data, Some(&ram_data))
+            }
+            None => self.load_rom(&data, None),
+        }
     }
 
     pub fn attach_serial(&mut self, device: Box<dyn SerialDevice>) {
diff --git a/src/rom.rs b/src/rom.rs
index 90d0545d..f793667f 100644
--- a/src/rom.rs
+++ b/src/rom.rs
@@ -587,12 +587,16 @@ impl Cartridge {
         )
     }
 
+    pub fn rom_data_eager(&self) -> Vec<u8> {
+        self.rom_data.clone()
+    }
+
     pub fn ram_data_eager(&self) -> Vec<u8> {
         self.ram_data.clone()
     }
 
-    pub fn set_ram_data(&mut self, ram_data: Vec<u8>) {
-        self.ram_data = ram_data;
+    pub fn set_ram_data(&mut self, data: &[u8]) {
+        self.ram_data = data.to_vec();
     }
 
     pub fn description(&self, column_length: usize) -> String {
diff --git a/src/test.rs b/src/test.rs
index 43f4ab89..ff38d784 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -30,7 +30,7 @@ pub fn run_test(rom_path: &str, max_cycles: Option<u64>, options: TestOptions) -
     let max_cycles = max_cycles.unwrap_or(u64::MAX);
 
     let mut game_boy = build_test(options);
-    game_boy.load_rom_file(rom_path);
+    game_boy.load_rom_file(rom_path, None);
 
     loop {
         cycles += game_boy.clock() as u64;
-- 
GitLab