Today you implement SRL and SRA, the two right shifts that differ only in what fills bit 7 - SRL always inserts 0, SRA preserves the sign bit - the distinction that lets the CPU divide signed numbers correctly.
Implement SRL and SRA, the logical and arithmetic right shifts.
Shifts move bits but, unlike rotates, do not wrap - a bit falls off one end into
carry and a fixed value fills in behind it. The two right shifts differ only
in what they feed into bit 7: SRL (logical) always inserts 0, while SRA
(arithmetic) preserves the old bit 7, keeping a number’s sign intact when
it is treated as signed. That one distinction is the whole idea today.
On 0x81, SRL gives 0x40 (top bit cleared) but SRA gives 0xC0 (top bit
copied down) - both push the old bit 0 into carry, and both set Z when the
result is zero. The third shift, SLA (CB 0x20), is just the left-shift
mirror: it feeds a 0 into bit 0 and pushes the old bit 7 into carry, exactly
like RL without the carry-in - add it in the same shape once the right shifts
work. Shifts are the CPU’s multiply and divide by powers of two, and SRA
is how it divides signed numbers correctly.
case 0x38: // SRL B - logical right shiftcase 0x28: // SRA B - arithmetic right shift// Both shift B right one bit and put the OLD bit 0 into C. The only// difference is what fills the vacated top bit: SRL shifts in a 0; SRA// keeps the old bit 7 (sign-preserving). For BOTH, set Z from the result// and clear N and H. (SLA, CB 0x20, is the left mirror - a later add.)
SRL vs. SRA - logical vs. arithmetic right shift and sign preservation.