Prev: 39773 Up: Map Next: 39873
39842: Calculate Screen Address
Converts the pixel position of a point into the address in the screen buffer that holds it. The bits that are left over are not used here, so the address is the one for the left-hand pixel of the character cell.
Input
H The Y position (the pixel line down from the top)
L The X position (the pixel across from the left)
Output
HL The screen buffer address
ScreenAddress 39842 LD A,L Fetch the X position.
39843 RRCA Rotate right three times, which divides the X position by 8.
39844 RRCA
39845 RRCA
39846 AND %00011111 Keep only bits 0-4.
39848 LD L,A Keep the column in L.
39849 LD A,H Fetch the Y position.
39850 RLCA Rotate left twice.
39851 RLCA
39852 AND %11100000 Keep only bits 5-7 (the character row within the third of the screen).
39854 OR L Combine it with the column.
39855 LD L,A Keep the low byte of the screen buffer address in L.
39856 LD A,H Fetch the Y position.
39857 AND %00000111 Keep only bits 0-2 (the pixel line within the character).
39859 EX AF,AF' Switch to the shadow AF register.
39860 LD A,H Fetch the Y position.
39861 RRCA Rotate right three times.
39862 RRCA
39863 RRCA
39864 AND %00011000 Keep only bits 3-4 (the third of the screen).
39866 OR %01000000 Set bit 6, so the address is in the screen buffer.
39868 LD H,A Keep the third of the screen in H.
39869 EX AF,AF' Switch back to the main AF register.
39870 OR H Combine the pixel line with the third of the screen.
39871 LD H,A Keep the high byte of the screen buffer address in H.
39872 RET Return.
Prev: 39773 Up: Map Next: 39873