View Single Post
Old 05-15-2012, 10:21 PM   #1
Junior Member
Join Date: May 2012
Posts: 1
Default Unconditional jumps and PC increment


I'm writing a simple GB emulator (wow, now that's something new, isn't it), since I'm really taking my first steps in emu.

What i don't seem to understand is how to correctly implement the CPU cycle and unconditional jumps.

Consider the command JP nn (unconditional jump to memory adress pointed out), like JP 1000h, if I have a basic loop of

increment PC
read opcode
execute command
Then after the JP opcode has been read and the command executed (read 1000h from memory and set PC = 1000h), the PC gets incremented and becomes 1001h, thus resulting in bad emulation.

tl;dr How do you emulate jumps in emulators, so that PC value stays correct, when having cpu loops that increment PC?

I've looked into source code in this GB emulator (written in C#) [url][/url] and it got me really confused. What it has is a cpu loop, similar to proposed above, then a switch statement to parse commands, but the JP nn command just loads the supplied value to PC, so I fail to understand how the emulator functions correctly, if the PC gets increased afterwards.

 int opCode = 0x00;
      if (!halted) {
        opCode = ReadByte(PC);
        if (stopCounting) {
          stopCounting = false;
        } else {


    case 0xC3: // JP N

    private void Jump() {
          PC = ReadWord(PC);
          ticks += 10;
I wish to thank in advance if someone points me to the right direction. Thank you!

Last edited by ajsimpson; 05-15-2012 at 10:26 PM. Reason: Forgot to say thanks... :/
ajsimpson is offline   Reply With Quote