3

I think I am struggling to define correctly the following ambiguous opcodes: LD HL,SP+r8 and JP (HL) opcodes (0xE9 and 0xF8 respectively)

In my implementation, LD HL,SP+r8 sets HL to the value of SP+r8, but I have a feeling that it may be to do with loading memory from RAM. JP (HL), I have as PUSHing the PC onto the stack and setting the Program Counter to the value of HL (like JP a16, except with the value of HL), but I've read a few forums that seem to say that that's wrong.

Any clarification of what either of these instructions do would be great, as I'm pretty lost at the moment.

Nabushika
  • 364
  • 1
  • 17
  • 1
    maybe this helps you somehow [0xE9](https://github.com/drhelius/Gearboy/blob/master/src/opcodes.cpp#L1700) and [0xF8](https://github.com/drhelius/Gearboy/blob/master/src/opcodes.cpp#L1799) – Dodge May 04 '16 at 17:47
  • @Dodge I've been looking for an open source emulator to see what the source is, but the only one I found implemented some opcodes entirely wrong so I didn't really trust it. Has this emulator been tested? If so, thank you, you've been very helpful! – Nabushika May 04 '16 at 17:53
  • You can checkout mine https://github.com/creker/Cookieboy It passes all blargg's tests - CPU, timings, LCD, sound. Even those that test hardware bugs – creker May 04 '16 at 23:12
  • @creker thanks, as I said it's been pretty hard to find an open source emulator that actually works. Just gotta sort through all those files now. ^~^ But that'll only take a few minutes, so thanks. – Nabushika May 06 '16 at 18:25

1 Answers1

5

In my implementation, LD HL,SP+r8 sets HL to the value of SP+r8, but I have a feeling that it may be to do with loading memory from RAM.

No. It just takes an 8-bit immediate, sign-extends it, adds the value of SP to it and stores the result in HL.

JP (HL), I have as PUSHing the PC onto the stack and setting the Program Counter to the value of HL (like JP a16, except with the value of HL)

JP doesn't push the current PC on the stack (maybe you're confusing it with CALL). What JP (HL) does is just PC = HL.

Michael
  • 57,169
  • 9
  • 80
  • 125