Revs on the BBC Micro

```       Name: GetScreenAddress                                        [Show more]
Type: Subroutine
Category: Drawing pixels
Summary: Return the screen address for a specified screen coordinate
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* PrintCharacter calls entry point GetScreenAddress-2

Arguments:

A                    The screen x-coordinate in pixels (0 to 159)

Y                    The screen y-coordinate in pixels

Returns:

(Q P)                The address of the character block containing the screen
coordinates

Y                    The pixel row within the character block containing the
screen coordinates

Other entry points:

GetScreenAddress-2   Treat the x-coordinate as a character column number
rather than a pixel coordinate (0 to 39)

ASL A                  \ Set A = A << 2
ASL A                  \       = x-coord << 2
\
\ so in the following, (Q P) gets set to x-coord << 3,
\ or x-coord * 8, which gives us the correct byte number
\ for this coordinate on the character row, as each
\ character block contains eight bytes

STA P                  \ Set (Q P) = A << 1
LDA #0                 \           = x-coord << 1
ASL P                  \           = x-coord * 2
ROL A                  \
STA Q                  \ so (Q P) contains the correct byte number for this
\ coordinate as an offset from the start address of the
\ character row, as each character row contains 320
\ bytes, and the x-coordinate in A is in the range 0 to
\ 160 (i.e. each character block is two pixels wide)

TYA                    \ Set X = Y
LSR A                  \       = y-coord >> 3
LSR A                  \
LSR A                  \ so X is the character row number for this coordinate
TAX

\ The X-th entry in the (yLookupHi yLookupLo) table
\ contains the screen address of the start of character
\ row X in the custom screen, so we now add this to
\ (Q P) to get the screen address of the correct
\ character block on this row

LDA yLookupLo,X        \ Set (Q P) = (Q P) + X-th yLookup entry
CLC                    \
ADC P                  \ starting with the low bytes
STA P

LDA yLookupHi,X        \ And then the high bytes