Zophar's Message Domain

Go Back   Zophar's Message Domain > Emulation Talk > General Emulation

Reply
 
Thread Tools Display Modes
Old 05-15-2012, 10:21 PM   #1
ajsimpson
Junior Member
 
Join Date: May 2012
Posts: 1
Default Unconditional jumps and PC increment

Hello,

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

Code:
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]http://meatfighter.com/gameboy/[/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.

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

      ...


    case 0xC3: // JP N
          Jump();
          break;

    ...
    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
Old 05-27-2012, 11:53 PM   #2
hcs
Senior Member
 
hcs's Avatar
 
Join Date: Oct 2001
Location: California
Posts: 1,585
Default

It's actually a slightly different order:

1. Read opcode
2. Increment PC (you are now pointing at the next opcode or the data for the current opcode)
3. Do whatever

You'll notice that this is the order that the GB emulator you linked to uses.

The PC in general will be incremented whenever something is read from the instruction stream, so that it will point to the next byte to be read.

This does get somewhat confusing when the instruction does something with PC before the increment happens, like the 6502's JSR/RTS instructions. The Z80 Call/Return is more straightforward than that, fortunately.
hcs is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:55 AM.

Contact Us - Zophar's Domain - Archive - Top

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.