Length of instruction LD A,(C) in gameboy ~Z80 processor
Asked Answered
S

1

11

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 ?

Satin answered 28/12, 2016 at 0:29 Comment(7)
Why do you think that?Icy
The diagram states that the instructions are 8-bit. I don't see any reason to disbelieve.Hombre
Try this gameboy cpu reference .Zaslow
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.Oersted
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: datacrystal.romhacking.net/wiki/…Joyejoyful
pandocs say that they are 1 byte instructions, and that's what I treat them as in my emulator.Torras
Given the example given by @MégaLag and what pandocs says, I'll consider it a 1 byte instruction.Satin
P
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.

Pickel answered 2/1, 2017 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.