From 7a7d16b4f7b717e639182ae21a7bcb3c6e474ac6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sun, 6 Aug 2023 23:54:37 +0100
Subject: [PATCH] fix: issue with the DMA transfer on DMG

---
 src/mmu.rs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/mmu.rs b/src/mmu.rs
index a2f92b7a..9b02f33f 100644
--- a/src/mmu.rs
+++ b/src/mmu.rs
@@ -244,8 +244,14 @@ impl Mmu {
         }
 
         // @TODO: Implement DMA transfer in a better way
-        let data = self.read_many(self.dma.source(), self.dma.length());
-        self.write_many(self.dma.destination(), &data);
+
+        // only runs the DMA transfer if the system is in CGB mode
+        // this avoid issues when writing to DMG unmapped registers
+        // that would otherwise cause the system to crash
+        if self.mode == GameBoyMode::Cgb {
+            let data = self.read_many(self.dma.source(), self.dma.length());
+            self.write_many(self.dma.destination(), &data);
+        }
         self.dma.set_active(false);
     }
 
-- 
GitLab