Prev: 45951 Up: Map Next: 46006
45972: Calculate Screen Address
Finds the address in the display file of the byte that holds a pixel. The pixel y position counts up from the bottom of the screen, so the first step is to invert it. A y position of 192 or more is above the top of the screen, and gives an address below 16384.
Input
B The pixel y position
C The pixel x position
Output
DE The address on the screen
Work out the byte from the position.
CalculateScreenAddress 45972 LD A,C Divide C by eight.
45973 RRCA
45974 RRCA
45975 RRCA
45976 AND %00011111 Keep only the five bits of the column.
45978 LD E,A Set E to the column.
Find the line within the character square.
45979 LD A,B Invert the y co-ordinate, to count down from the top of the screen.
45980 CPL
45981 AND %00000111 Keep the three bits of the line within the character square.
45983 EX AF,AF' Copy it to A'.
Find the character row within the third of the screen, and merge it with the column to make the low byte of the address.
45984 LD A,B Fetch B and invert it.
45985 CPL
45986 RLCA Move the character row into bits 5 to 7.
45987 RLCA
45988 AND %11100000 Keep bits 5 to 7.
45990 OR E Combine them with the column in E.
45991 LD E,A Copy it to E.
Find the third of the screen and merge it with the line to make the high byte of the address.
45992 LD A,B Fetch B and invert it.
45993 CPL
45994 RRCA Move the third into bits 3 and 4.
45995 RRCA
45996 RRCA
45997 AND %00011000 Keep bits 3 and 4.
45999 LD D,A Copy it to D.
46000 EX AF,AF' Fetch the line within the character square.
46001 OR D Merge D.
46002 ADD A,56 Add 56, which corrects for the inverted y co-ordinate and adds the display file base of 64.
46004 LD D,A Copy it to D.
46005 RET Return.
View the equivalent code in;
Prev: 45951 Up: Map Next: 46006