10

Hello I'm writing an emulator for Game Boy.

I use this reference : Gameboy CPU (LR35902) instruction set

This document states that opcodes :

0xE2 LD (C),A

and

0xF2 LD A,(C)

have a length of 2.

The Game Boy CPU Manual says that these instructions respectively :

Put value at address $FF00 + register C into A.

and

Put A into address $FF00 + register C.

I think it has length of 1 because the opcode is self sufficient, it doesn't need another value to be interpreted. If it has a length of 2, what's the purpose of the second byte ?

Demeter Purjon
  • 373
  • 1
  • 12
  • Why do you think that? – Blorgbeard Dec 28 '16 at 00:35
  • The diagram states that the instructions are 8-bit. I don't see any reason to disbelieve. – Malcolm McLean Dec 28 '16 at 00:43
  • Try this [gameboy cpu reference](http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf) . – rcgldr Dec 28 '16 at 00:44
  • All the sources say 2 bytes, but none say what the second byte is used for. The gbemulator source doesn't use it for anything, so maybe it's undefined and just a leftover from the original z80 encodings which were repurposed for these gameboy specific instructions. – Jester Dec 28 '16 at 00:54
  • 2
    I may have badly understood the question, but I've never seen 0xE2 and 0xF2 use a second byte. Here's an example of 0xE2 in GBC: http://datacrystal.romhacking.net/wiki/Yu-Gi-Oh!_Dark_Duel_Stories:ROM_map#LCD_Control – Méga Lag Dec 28 '16 at 05:46
  • [pandocs](http://bgb.bircd.org/pandocs.htm#cpuinstructionset) say that they are 1 byte instructions, and that's what I treat them as in my emulator. – Michael Dec 28 '16 at 07:10
  • 3
    Given the example given by @MégaLag and what [pandocs](http://bgb.bircd.org/pandocs.htm#cpuinstructionset) says, I'll consider it a 1 byte instruction. – Demeter Purjon Dec 28 '16 at 08:20

1 Answers1

13

You are absolutely right. LD (C),A and LD A,(C) are 1 byte long.

I have even emailed the responsible (Pastraiser) asking for clarification (or if he could fixed the website) but I got no answer.

Another thing that bothers me is the fact that prefix CB is considered to be 1 byte long and consume 4 cycles. And all the extended instructions are considered to be 2 bytes long and consume 8 cycles (16 if they access (HL)). It would make more sense to me to leave blank the space below Prefix CB so it is clear that all extended instructions already include the size and processing cycles of their prefix.

And the other thing that bothers me is the fact that STOP length is 2. It is actually just one byte long. There is a hardware bug on Gameboy Classic that causes the instruction following a STOP to be skipped. So Nintendo started to tell developers to add a NOP always after a STOP. So, to be 100% correct I would consider STOP to be (1,4) instead of (2,4). This issue does not exist on Gameboy Color.

GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57