RainbowCookie
RainbowCookie

Reputation: 15

Relative Jump in GameBoy emulator isn't jumping to where it should

I'm working on a GameBoy emulator on C#, and testing it with Blargg's test ROMs (cpu_instrs from here). I'm going through tests individually, and now I'm using 07-jr,jp,call,ret,and rst. I am comparing my results with bgb's debugger. Now, when I get to 0209, where it runs the opcode 0x20 (JR NZ), bgb jumps to 0x206, but my emulator jumps to 0x306.

This is my current code for that instruction:

if (Processor.GetZeroFlag() == 0)
{
    byte jumpOffset = Processor.ReadNextByte();
    ushort targetAddr = (ushort)(Processor.GetPC() + 2 + jumpOffset);

    Processor.SetPC(targetAddr);
    Processor.FinishedIntruction(0, 8, 2);
    break;
}
else
{
    Console.WriteLine("Not taking the branch");
    Processor.FinishedIntruction(2, 8, 2);
    break;
}

ReadNextByte() returns the next byte from memory (reads from PC + 1), in this case it's FB (which, according to bgb, is correct). As far as I understood, I have to add FB to the current PC, and the instruction's length (2), but doing that I end up in 306 as I already explained.

Is there something here that I'm missing — some error in my code that I can't see?

Upvotes: 0

Views: 815

Answers (1)

creker
creker

Reputation: 9570

byte jumpOffset = Processor.ReadNextByte();

It should be sbyte giving -5 instead of 0xFB.

Upvotes: 1

Related Questions