GameBoy 16-bit load into 8-bit memory
Asked Answered
S

2

8

I have begun programming an emulator for the Gameboy classic, my next project after a successful Chip 8 Emulator.

As a reference I use the GameBoy CPU Manual.

Now on page 66 it says:

LD  A,(HL)  7E  8

Basically, load the value HL into register A.

However, as I understand this, this would load the 16-bit value HL into the 8-bit register A. Which of course doesnt fit.

Do you have any idea how this is meant? All other references are just simple tables without explaination, but say the same thing.

Thanks for your answers!

Shuddering answered 4/1, 2016 at 13:19 Comment(0)
O
9

With this instruction the value pointed to by (HL) is loaded into A not the value of HL itself. For example if HL has the value 0xABCD and the value of the memory at address 0xABCD is 0x50 then 0x50 is loaded into register A.

Pseudo implementation

register.A = memory.ReadByte(register.HL);
Orthopter answered 4/1, 2016 at 13:37 Comment(7)
LD r1,r2 Description: Put value r2 into r1. page 66, explains the LD operation. Please read and revise your answer.Cysteine
Thanks a bunch. I just checked out another Z80 reference I found and it said the same thing.Shuddering
That was a problem, everything suggested here is plausible, I also thank you for your answer. But it says that in this manual: mct.de/download/zilog/z80cpu.pdfShuddering
@Saibot Yes im sure. You can check some open source gameboy emulators.Orthopter
github.com/CTurt/Cinoop/blob/master/source/cpu.c#L1183 here is an example implemented as described above.Orthopter
@Saibot It seems to fail to mention that braces '(XX)' indicate that value inside the braces is a memory address.Orthopter
@Orthopter Yeah, I know :-)Cysteine
P
1

I think LD A,(HL) is a synonym for something more widely written as LD a,[hl] based on the documentation for a similar instruction on page 71.

  1. LDD A,(HL) Description: Put value at address HL into A. Decrement HL. Same as: LD A,(HL) - DEC HL

Therefore, LD A,(HL) means "Put value at address HL into A." HL is a 16 bit value, but the address it references is 8 bit, so it fits into A.

Pattiepattin answered 4/1, 2016 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.