Target for Gameboy (z80)

I am working on a Gameboy emulator, and I noticed that there are certain operation codes that will never change any values, such as LD A, A , LD B, B , etc., as well as AND A The former, obviously, do not change anything, since they load the value of the registers into the same registers, and since AND compared with the register A , AND A always returns A Is there any purpose for these operations, or essentially the same as the NOP after each cycle?

+7
emulation opcode gameboy z80
source share
3 answers

As Jeffrey Bosbum and Hans Passant noted their comments, the reason is simplicity. More specifically, the simplicity of the hardware.

LD r,r' instructions copy the contents of the source register ( r' ) to the destination register ( r ). The LD r,r' operators follow this form:

  ------------------------------- BIT | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ------------------------------- OPCODE | 0 | 1 | r | r' | ------------------------------- 

The destination and source registers can take the following values:

  ----------- | BIT | REG | ----------- | 111 | A | ----------- | 000 | B | ----------- | 001 | C | ----------- | 010 | D | ----------- | 011 | E | ----------- | 100 | H | ----------- | 101 | L | ----------- 

To implement these instructions on hardware, we just need a multiplexer that accepts bits 0-2 to select the source register and another multiplexer that accepts bits 3-5 to select the destination register.

If you want to check if bits 0-2 and bits 3-5 indicate the same register, you will have to add more logic to the CPU. And, as we all know, ressources were more limited at 80: P

Please note that loading instructions such as LD A,A , LD B,B , LD C,C , LD D,D , LD E,E , LD H,H and LD L,L behave like NOP . However, AND A and OR A DO NOT behave like NOP , since they affect the flag case, and their execution can change the internal state of the machine.

+10
source share

Instructions like LD A,A and AND A may seem NOP , but they can also change processor flags and be used to check the value of a register.

Be sure to carefully check the documentation for the set of instructions for such side effects.

+6
source share

In fact, there is a goal in the AND A command (as well as OR A ) - it sets the Z flag when A is zero and otherwise cleared. Therefore, AND A and OR A are often used for this purpose.

+2
source share

All Articles