I'm trying to emulate the Z80 of the GameBoy but I have questions (my first real attempt to write a GB emulator )
I have this this function that handles the SBC:
{
T_8bit l_regvalue; // Value to SUB (T_8bit is unsigned char )
T_8bit l_accvalue; // ACC value (T_8bit is unsigned char )
l_regvalue = GetHigh(l_RegX); // Value from a Register
l_accvalue = GetHigh(m_AF);
if ( IsCFlagSet() ) //If the Carry flag is set it is +1 to the value to sub.
++l_regvalue;
(l_accvalue < l_regvalue) ? SetCFlag(): ClearCFlag(); // SET THE CARRY FLAG... THIS IS WHAT I THINK IS WRONG!..
l_accvalue -= l_regvalue; // do the SUB operation A = A - X
l_accvalue ? ClearZFlag() : SetZFlag(); // If the result is zero.. clear the Z flag else set it.
SetNFlag(); // Set the N Flag
//Set the value in the ACC
SetHigh(m_AF,l_accvalue);
}
The question is how the Z80 handles the negative values in registers? And how to check exactly if there Carry/borro in SBC opcodes with negative / positive values? I'm a bit lost here...
And another question.. It's true that to emulate the Z80 we don't need to calculate the Half CArry flag? I can't find any opcode that uses it..
Sorry for my english!
Thanks!
I have this this function that handles the SBC:
{
T_8bit l_regvalue; // Value to SUB (T_8bit is unsigned char )
T_8bit l_accvalue; // ACC value (T_8bit is unsigned char )
l_regvalue = GetHigh(l_RegX); // Value from a Register
l_accvalue = GetHigh(m_AF);
if ( IsCFlagSet() ) //If the Carry flag is set it is +1 to the value to sub.
++l_regvalue;
(l_accvalue < l_regvalue) ? SetCFlag(): ClearCFlag(); // SET THE CARRY FLAG... THIS IS WHAT I THINK IS WRONG!..
l_accvalue -= l_regvalue; // do the SUB operation A = A - X
l_accvalue ? ClearZFlag() : SetZFlag(); // If the result is zero.. clear the Z flag else set it.
SetNFlag(); // Set the N Flag
//Set the value in the ACC
SetHigh(m_AF,l_accvalue);
}
The question is how the Z80 handles the negative values in registers? And how to check exactly if there Carry/borro in SBC opcodes with negative / positive values? I'm a bit lost here...
And another question.. It's true that to emulate the Z80 we don't need to calculate the Half CArry flag? I can't find any opcode that uses it..
Sorry for my english!
Thanks!