From ef26d5442a8bf0b9d00ca02aad9a885432e630af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Wed, 6 Jul 2022 12:21:32 +0100
Subject: [PATCH] fix: more fixes and better VRAM debug

---
 examples/sdl/src/main.rs   |  2 +-
 examples/web/index.html    |  2 +-
 examples/web/index.ts      | 12 ++++++++++--
 examples/web/tsconfig.json |  2 +-
 src/gb.rs                  |  8 ++++++++
 src/pad.rs                 |  2 +-
 src/ppu.rs                 | 20 ++++++++++++++++----
 7 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs
index a6133ff7..965230e0 100644
--- a/examples/sdl/src/main.rs
+++ b/examples/sdl/src/main.rs
@@ -87,7 +87,7 @@ fn main() {
     //game_boy.load_rom_file("../../res/roms.prop/alleyway.gb");
 
     //game_boy.load_rom_file("../../res/roms/firstwhite.gb");
-    game_boy.load_rom_file("../../res/roms/opus5.gb");
+    //game_boy.load_rom_file("../../res/roms/opus5.gb");
 
     //game_boy.load_rom_file("../../res/roms/paradius/cpu/01-special.gb"); // PASSED
     //game_boy.load_rom_file("../../res/roms/paradius/cpu/02-interrupts.gb"); // NO FINISH
diff --git a/examples/web/index.html b/examples/web/index.html
index 521cf0df..17dc8202 100644
--- a/examples/web/index.html
+++ b/examples/web/index.html
@@ -119,7 +119,7 @@
                     </span>
                     <span id="button-upload" class="tiny-button border padded file">
                         <img src="res/upload.svg" alt="upload" /><span>Load ROM</span>
-                        <input type="file" id="button-upload-file" name="button-upload-file" accept=".ch8">
+                        <input type="file" id="button-upload-file" name="button-upload-file" accept=".gb">
                     </span>
                 </div>
             </div>
diff --git a/examples/web/index.ts b/examples/web/index.ts
index c2b983a8..5f20f7c0 100644
--- a/examples/web/index.ts
+++ b/examples/web/index.ts
@@ -544,9 +544,7 @@ const registerButtons = () => {
                 const pixels = state.gameBoy.get_tile_buffer(index);
                 const line = Math.floor(index / 16);
                 const column = index % 16;
-                console.info(`${canvasTiles.width}`);
                 let offset = ((line * canvasTiles.width * 8) + (column * 8)) * PixelFormat.RGBA;
-                console.info(`${offset}`);
                 let counter = 0;
                 for (let index = 0; index < pixels.length; index += format) {
                     const color =
@@ -570,6 +568,16 @@ const registerButtons = () => {
             for (let index = 0; index < 256; index++) {
                 drawSprite(index);
             }
+
+            const vram = state.gameBoy.vram_eager();
+            const step = 16;
+            for (let index = 0; index < vram.length; index += step) {
+                let line = `${(index + 0x8000).toString(16).padStart(4, "0")}`;
+                for (let j = 0; j < step; j++) {
+                    line += ` ${vram[index + j].toString(16).padStart(2, "0")}`;
+                }
+                console.info(line);
+            }
         }
     });
 
diff --git a/examples/web/tsconfig.json b/examples/web/tsconfig.json
index 0bc8cec0..54b8f23f 100644
--- a/examples/web/tsconfig.json
+++ b/examples/web/tsconfig.json
@@ -9,7 +9,7 @@
         "sourceMap": true,
         "outDir": ".",
         "baseUrl": ".",
-        "lib": ["es2015", "dom"],
+        "lib": ["es2017", "dom"],
         "paths": {
             "*": ["node_modules/*", "src/types/*"]
         }
diff --git a/src/gb.rs b/src/gb.rs
index 4ef33166..b8fb338d 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -111,6 +111,14 @@ impl GameBoy {
         self.load_boot_dmg_bootix();
     }
 
+    pub fn vram_eager(&mut self) -> Vec<u8> {
+        self.ppu().vram().to_vec()
+    }
+
+    pub fn hram_eager(&mut self) -> Vec<u8> {
+        self.ppu().vram().to_vec()
+    }
+
     pub fn frame_buffer_eager(&mut self) -> Vec<u8> {
         self.frame_buffer().to_vec()
     }
diff --git a/src/pad.rs b/src/pad.rs
index 62ad7ad3..1b7e5412 100644
--- a/src/pad.rs
+++ b/src/pad.rs
@@ -73,7 +73,7 @@ impl Pad {
                     PadSelection::Action
                 }
             }
-            addr => panic!("Reading from unknown Pad location 0x{:04x}", addr),
+            addr => panic!("Writing to unknown Pad location 0x{:04x}", addr),
         }
     }
 }
diff --git a/src/ppu.rs b/src/ppu.rs
index 8159ad45..d0e97be2 100644
--- a/src/ppu.rs
+++ b/src/ppu.rs
@@ -1,5 +1,8 @@
 use core::fmt;
-use std::fmt::{Display, Formatter};
+use std::{
+    borrow::BorrowMut,
+    fmt::{Display, Formatter},
+};
 
 #[cfg(feature = "wasm")]
 use wasm_bindgen::prelude::*;
@@ -381,8 +384,16 @@ impl Ppu {
         }
     }
 
-    pub fn tiles(&self) -> [Tile; TILE_COUNT] {
-        self.tiles
+    pub fn vram(&self) -> &[u8; VRAM_SIZE] {
+        &self.vram
+    }
+
+    pub fn hram(&self) -> &[u8; HRAM_SIZE] {
+        &self.hram
+    }
+
+    pub fn tiles(&self) -> &[Tile; TILE_COUNT] {
+        &self.tiles
     }
 
     pub fn palette(&self) -> Palette {
@@ -428,13 +439,14 @@ impl Ppu {
     pub fn update_tile(&mut self, addr: u16, _value: u8) {
         let addr = (addr & 0x1ffe) as usize;
         let tile_index = ((addr >> 4) & 0x01ff) as usize;
+        let tile = self.tiles[tile_index].borrow_mut();
         let y = ((addr >> 1) & 0x0007) as usize;
 
         let mut mask;
 
         for x in 0..8 {
             mask = 1 << (7 - x);
-            self.tiles[tile_index].set(
+            tile.set(
                 x,
                 y,
                 if self.vram[addr] & mask > 0 { 0x1 } else { 0x0 }
-- 
GitLab