3

I'm working on a GameBoy emulator in C and currently I'm working on the CPU core. Nevertheless, I am not sure if I understand when the carry and half-carry flags are set, especially in operations like:

LD HL, SP+n

(http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html)

My implementation of this opcode is:

DELETED

Is it correct implementation or did I misunderstand flags?

EDIT: My implementation was wrong and this question duplicates another. Here is the correct implementation (I hope), it probably isn't very resource effective though:

case 0xF8:
{
        int8_t n = (int8_t)readmem(PC+1); /* Load n and use it as signed integer */
        /* HL and SP are declared as uint16_t */
        HL = SP + n; /* Put the value of SP + n to 16-bit register HL*/
        resetf(FZ); /* Reset zero flag*/
        resetf(FN); /* Reset subtract flag*/
        if((SP & 0x000F + n & 0x000F) & 0x00F0)
        {
            setf(FH); /* Set half-carry flag */
        }
        else
        {
            resetf(FH); /* Reset half-carry flag */
        }
        if((SP & 0x00FF + n & 0x00FF) & 0x0F00)
        {
            setf(FC); /* Set carry flag */
        }
        else
        {
            resetf(FC); /* Reset carry flag */
        }
        PC+=2; /* Increase program counter */
break;
}
MHN
  • 31
  • 1
  • 3

0 Answers0