From 6a5d376999c3c83685e958ae8f44a783166d5f5a Mon Sep 17 00:00:00 2001
From: Jorge Gorbe <slack@codemaniacs.com>
Date: Mon, 27 May 2013 20:24:36 +0200
Subject: [PATCH] Fixed opcodes E8 and F8

There was a problem with the carry flags due to lack of documentation.
Once I found http://stackoverflow.com/questions/5159603/gbz80-how-does-ld-hl-spe-affect-h-and-c-flags
it was easy :S
---
 core/GameBoy.cc | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/core/GameBoy.cc b/core/GameBoy.cc
index 85b6df8..6dd7b88 100644
--- a/core/GameBoy.cc
+++ b/core/GameBoy.cc
@@ -613,11 +613,13 @@ GameBoy::run_status GameBoy::run_cycle()
 					case 0xF8: {
 						s8 offset = memory.read(regs.PC++);
 						int res = regs.SP + offset;
+						int res8 = (regs.SP & 0xFF) + (offset & 0xFF);
+						int half_res = (regs.SP & 0xF) + (offset & 0xF);
+						// Do the same as 0xE8 for the carry flags
 						
-						// TODO: Verificar si los flags van asi
-						set_flag_if (res > 0xFFFF, CARRY_FLAG);
+						set_flag_if (res8 > 0xFF, CARRY_FLAG);
+						set_flag_if (half_res > 0xF, HALF_CARRY_FLAG);
 
-						// TODO: hacer lo apropiado con el half-carry flag
 						reset_flag(ADD_SUB_FLAG);
 						reset_flag(ZERO_FLAG);
 
@@ -864,13 +866,18 @@ GameBoy::run_status GameBoy::run_cycle()
 
 					// ADD SP, #
 					case 0xE8: {
-						// FIXME: No se que hacer con el half carry, en 4 o en 11?
+						// Carry flag: set when carry from bit 7 to 8
+						// Half-carry flag: set when carry from bit 3 to 4
+						// http://stackoverflow.com/questions/5159603/gbz80-how-does-ld-hl-spe-affect-h-and-c-flags
 						int n = static_cast<s8>(memory.read(regs.PC++));
 						int res = regs.SP + n;
+						int res8 = (regs.SP & 0xFF) + (n & 0xFF);
+						int half_res = (regs.SP & 0xF) + (n & 0xF);
 						regs.SP = static_cast<u16>(res);
 						reset_flag(ZERO_FLAG);
 						reset_flag(ADD_SUB_FLAG);
-						set_flag_if(res > 0xFFFF, CARRY_FLAG);
+						set_flag_if(res8 > 0xFF, CARRY_FLAG);
+						set_flag_if(half_res > 0xF, HALF_CARRY_FLAG);
 						cycles_until_next_instruction = 16; 
 						break;
 					}
-- 
2.34.1