I'm currently developing an emulator for the good old GameBoy and I'm facing some problems understandig how some basic operation codes have to be implemented.
Right now I'm implementing the AND operations; the first few (0xA0 -> 0xA3; 0xA6 & 0xA7) are pretty straight forward but the AND operations for the registers H, L are a bit different.
You can download the documention of the z80 under this link: um0080.pdf (page 172)
Here are some examples to show what I mean (with pseudo-code) and basically what I do:
AND A,H (note the bit-shifting)
(read HL register; >> 8) save in cache C
R->C = R->HL >> 8;
perform AND operation with cache
AND_H(R->C);
R->A &= R->C;
AND A,L (note the bit-masking)
(read HL register; &0xFF) save in cache C
R->C = R->HL &0xFF;
I know all the bit-operations and I know what they do, but it seems that I can't figure out why its need to be done like that. I have some theories (correct me if I'm wrong :-)):
What I already understood is, that the registers H and L are basically the register HL, which is an 16-Bit register. Since the CPU/Bus can only handle 8-Bit operations, it needs to be splitted up; or the more logic suggestion: since its only one register, the values of H and L are masked in the register and they simply need to be separated from each other (higher/lower nibble?).
I would be deeply grateful if someone can make this more clear to me because I just want to have some more background knowledge (how all this stuff works internally) so its very important to me that I know what I'm doing.