From e490f00d8dcf3a52d31efc4cc044d4862a7ef34f Mon Sep 17 00:00:00 2001
From: slack <slack@codemaniacs.com>
Date: Mon, 14 Jul 2008 04:49:16 +0200
Subject: [PATCH] various fixes

---
 GBVideo.cc | 62 +++++++++++++++++++++++++++++++++++++++++++-------
 GBVideo.h  | 41 ++++++---------------------------
 gbcore.cc  | 66 +++++++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 112 insertions(+), 57 deletions(-)

diff --git a/GBVideo.cc b/GBVideo.cc
index ea81429..4bf2f2e 100644
--- a/GBVideo.cc
+++ b/GBVideo.cc
@@ -59,6 +59,52 @@ void GBVideo::DMA_OAM (const u16 src)
 	}
 }
 
+u8   GBVideo::read_VRAM (int addr) const
+{ 
+	if (!VRAM_BUSY || 
+		!check_bit(core->memory.high[GBMemory::I_LCDC],7) ) 
+		return VRAM[addr-VRAM_BASE]; 
+	else return 0xFF;
+}
+
+u8   GBVideo::read_OAM  (int addr) const 
+{
+	if (!OAM_BUSY ||
+		!check_bit(core->memory.high[GBMemory::I_LCDC],7) ) 
+		return OAM.raw[addr-OAM_BASE]; 
+	else return 0xFF;			
+} 
+
+u16  GBVideo::read16_VRAM (int addr) const 
+{ 
+	if (!VRAM_BUSY ||
+		!check_bit(core->memory.high[GBMemory::I_LCDC],7) ) 
+		return VRAM[addr-VRAM_BASE]+(VRAM[addr-VRAM_BASE+1] << 8);
+	else return 0xFF;
+}
+
+u16  GBVideo::read16_OAM  (int addr) const 
+{
+	if (!OAM_BUSY ||
+		!check_bit(core->memory.high[GBMemory::I_LCDC],7) ) 
+			return OAM.raw[addr-OAM_BASE]+(OAM.raw[addr-OAM_BASE+1] << 8); 
+	else return 0xFF;
+}
+
+void GBVideo::write_VRAM(int addr, u8 value) 
+{
+	if (!VRAM_BUSY ||
+		!check_bit(core->memory.high[GBMemory::I_LCDC],7) ) 
+		VRAM[addr-VRAM_BASE] = value; 
+}
+
+void GBVideo::write_OAM (int addr, u8 value) 
+{ 
+	if (!OAM_BUSY ||
+		!check_bit(core->memory.high[GBMemory::I_LCDC],7) ) 
+		OAM.raw[addr-OAM_BASE] = value;
+}
+
 u32 GBVideo::update()
 {
 	//Mode 0 is present between 201-207 clks, 2 about 77-83 clks, and 3
@@ -86,8 +132,8 @@ u32 GBVideo::update()
 		case 0:
 			// HBlank (preserve bits 2-6, mode = 0)
 			STAT = (STAT&0xFC);
-			OAM_BUSY  = false;
-			VRAM_BUSY = false;
+			//OAM_BUSY  = false;
+			//VRAM_BUSY = false;
 			if (check_bit(STAT, 3))
 			{
 				logger.trace("Requesting IRQ_LCD_STAT -- HBLANK");
@@ -100,8 +146,8 @@ u32 GBVideo::update()
 				mode = 2;
 			break;
 		case 1:
-			OAM_BUSY  = false;
-			VRAM_BUSY = false;
+			//OAM_BUSY  = false;
+			//VRAM_BUSY = false;
 			if (LY == 144)
 			{
 				logger.trace("Requesting IRQ_VBLANK");
@@ -162,8 +208,8 @@ u32 GBVideo::update()
 				mode = 1;
 			break;
 		case 2: {
-			OAM_BUSY  = true;
-			VRAM_BUSY = false;
+			//OAM_BUSY  = true;
+			//VRAM_BUSY = false;
 			if (LY == LYC)
 			{
 				STAT = set_bit(STAT, 2); // set coincidence flag
@@ -188,8 +234,8 @@ u32 GBVideo::update()
 			break;
 		}
 		case 3:
-			OAM_BUSY  = false;
-			VRAM_BUSY = true;
+			//OAM_BUSY  = false;
+			//VRAM_BUSY = true;
 			draw();
 			// preserve bits 2-6, set mode 3
 			STAT = (STAT&0xFC) | 3;
diff --git a/GBVideo.h b/GBVideo.h
index ac9f265..02117c4 100644
--- a/GBVideo.h
+++ b/GBVideo.h
@@ -3,6 +3,7 @@
 
 #include "GBMemory.h"
 #include "SDL.h"
+#include "util.h"
 
 class GameBoy;
 
@@ -70,40 +71,12 @@ class GBVideo
 	void reset();
 
 	// VRAM/OAM access
-	inline u8   read_VRAM (int addr) const
-	{ 
-		if (!VRAM_BUSY) return VRAM[addr-VRAM_BASE]; 
-		else return 0xFF;
-	}
-
-	inline u8   read_OAM  (int addr) const 
-	{
-		if (!OAM_BUSY) return OAM.raw[addr-OAM_BASE]; 
-		else return 0xFF;			
-	} 
-
-	inline u16  read16_VRAM (int addr) const 
-	{ 
-		if (!VRAM_BUSY) return VRAM[addr-VRAM_BASE]+(VRAM[addr-VRAM_BASE+1] << 8);
-		else return 0xFF;
-	}
-
-	inline u16  read16_OAM  (int addr) const 
-	{
-		if (!OAM_BUSY) return OAM.raw[addr-OAM_BASE]+(OAM.raw[addr-OAM_BASE+1] << 8); 
-		else return 0xFF;
-	}
-
-	inline void write_VRAM(int addr, u8 value) 
-	{
-		if (!VRAM_BUSY) VRAM[addr-VRAM_BASE] = value; 
-	}
-
-	inline void write_OAM (int addr, u8 value) 
-	{ 
-		if (!OAM_BUSY) OAM.raw[addr-OAM_BASE] = value;
-	}
-
+	inline u8   read_VRAM (int addr) const;
+	inline u8   read_OAM  (int addr) const;
+	inline u16  read16_VRAM (int addr) const;
+	inline u16  read16_OAM  (int addr) const;
+	inline void write_VRAM(int addr, u8 value);
+	inline void write_OAM (int addr, u8 value);
 
 	// Write the whole OAM area via DMA
 	void DMA_OAM (const u16 src);
diff --git a/gbcore.cc b/gbcore.cc
index 62d72c7..9f7a217 100644
--- a/gbcore.cc
+++ b/gbcore.cc
@@ -188,14 +188,6 @@ GameBoy::run_status GameBoy::run_cycle()
 			memory.high[GBMemory::I_IF] = IF;
 		}
 
-		for(BreakpointMap::iterator i=breakpoints.begin();
-				i != breakpoints.end();
-				i++)
-		{
-			if (i->second.addr == regs.PC && i->second.enabled)
-				return BREAKPOINT;
-		}
-		
 		int opcode;
 		opcode = memory.read(regs.PC++);
 
@@ -1318,10 +1310,13 @@ GameBoy::run_status GameBoy::run_cycle()
 		}
 	}
 
-	// Video
-	if (video.cycles_until_next_update <= 0)
-		video.update();
-	video.cycles_until_next_update -= CYCLE_STEP;
+	// Update video, only if LCD is enabled
+	if (check_bit(memory.high[GBMemory::I_LCDC], 7))
+	{
+		if (video.cycles_until_next_update <= 0)
+			video.update();
+		video.cycles_until_next_update -= CYCLE_STEP;
+	}
 	
 	// Divider
 	divider_count++;
@@ -1362,7 +1357,18 @@ GameBoy::run_status GameBoy::run_cycle()
 
 	cycles_until_next_instruction -= CYCLE_STEP;
 	if (cycles_until_next_instruction > 0) return WAIT;
-	else return NORMAL;
+	else 
+	{
+		for(BreakpointMap::iterator i=breakpoints.begin();
+				i != breakpoints.end();
+				i++)
+		{
+			if (i->second.addr == regs.PC && i->second.enabled)
+				return BREAKPOINT;
+		}
+		
+		return NORMAL;
+	}
 }
 
 GameBoy::run_status GameBoy::run() 
@@ -1410,6 +1416,29 @@ std::string GameBoy::get_port_name(int port) const
 
 	switch (port)
 	{
+		case 0x04: port_name = "DIV "; break;
+		case 0x05: port_name = "TIMA"; break;
+		case 0x06: port_name = "TMA "; break;
+		case 0x07: port_name = "TAC "; break;
+		case 0x10: port_name = "CH1_ENT";          break;
+		case 0x11: port_name = "CH1_WAVE";         break;
+		case 0x12: port_name = "CH1_ENV";          break;
+		case 0x13: port_name = "CH1_FREQ_LO";      break;
+		case 0x14: port_name = "CH1_FREQ_HI_KICK"; break;
+		case 0x16: port_name = "CH2_WAVE";         break;
+		case 0x17: port_name = "CH2_ENV";          break;
+		case 0x18: port_name = "CH2_FREQ_LO";      break;
+		case 0x19: port_name = "CH2_FREQ_HI_KICK"; break;
+		case 0x1A: port_name = "CH3_ONOFF";        break;
+		case 0x1C: port_name = "CH3_VOLUME";       break;
+		case 0x1D: port_name = "CH3_FREQ_LO";      break;
+		case 0x1E: port_name = "CH3_FREQ_HI_KICK"; break;
+		case 0x21: port_name = "CH4_ENV";          break;
+		case 0x22: port_name = "CH4_POLY";         break;
+		case 0x23: port_name = "CH4_KICK";         break;
+		case 0x24: port_name = "SND_VIN";          break;
+		case 0x25: port_name = "SND_STEREO";       break;
+		case 0x26: port_name = "SND_STAT";         break;
 		case 0x40: port_name = "LCDC"; break;
 		case 0x41: port_name = "STAT"; break;
 		case 0x42: port_name = "SCY "; break; 
@@ -1425,9 +1454,11 @@ std::string GameBoy::get_port_name(int port) const
 		case 0x0F: port_name = "IF  "; break;
 		case 0xFF: port_name = "IE  "; break;
 		default:
-				   if (port >= 0x80 && port <= 0xFE) {
+				   if (port >= 0x80 && port <= 0xFE) 
 					   port_name = "HRAM";
-				   }
+				   else if (port >= 0x30 && port <= 0x3F)
+					   port_name = "Wave Pattern RAM";
+
 	}
 	return port_name;
 }
@@ -1450,6 +1481,11 @@ std::string GameBoy::status_string() const
 		" H = " << std::hex << std::setw(2) << std::setfill('0') << int(regs.H) <<
 		" L = " << std::hex << std::setw(2) << std::setfill('0') << int(regs.L) <<
 		"\tflags = " << int(regs.flags) << "\tZF = " << check_flag(ZERO_FLAG) << std::endl <<
+		"AF = " << std::hex << std::setw(4) << std::setfill('0') << int(regs.AF) <<
+		" BC = " << std::hex << std::setw(4) << std::setfill('0') << int(regs.BC) <<
+		" DE = " << std::hex << std::setw(4) << std::setfill('0') << int(regs.DE) <<
+		" HL = " << std::hex << std::setw(4) << std::setfill('0') << int(regs.HL) <<
+		"\tSP = " << std::hex << std::setw(4) << std::setfill('0') << int(regs.SP) << std::endl <<
 		"IME = " << int(IME) << " IE = " << int(memory.read(0xFFFF)) << " IF = " << int(memory.read(0xFF0F));
 	return result.str();
 }
-- 
2.34.1