cbit-manipulationemulationintel-8080

What is doing this bitwise operation in order to emulate a RRC instruction


I'm working on a personal project to improve my knowledge on how a CPU works. So I'm doing a Intel 8080 emulator, which is a 8 bits microprocessor.

In the implementation of a RRC instruction, which example is this:

case 0x0f: {    
  uint8_t x = state->a;    
  state->a = ((x & 1) << 7) | (x >> 1);    
  state->cc.cy = (1 == (x&1));    
}

I can't understand how this line is working.

state->a = ((x & 1) << 7) | (x >> 1);

I know it's supposed to move all the bits to the right by 1 position, but I can't figure out how.

I would appreciate if someone could provide me an example of what it's actually doing step by step.



Solution

  • Lets study the steps in order:

    The effect of these steps is a rotation of the 8 bits one step to the right, with the low order bit landing into the carry flag. The 8080 reference card describes this as Rotate Accumulator Right thru Carry.

    Note that the steps can be simplified: