Calculating screen address

If we are drawing a sprite, we use our pixel coordinates to define the upper-left coordinate of the sprite.

Since the CPC and KC Compact do not have sprite hardware, we will need to plot the sprites using a software function.

If we want to plot graphics to the display, e.g. to draw a sprite, then we will need to be able to calculate a memory address from our pixel coordinates.

This document will describe how this is done.

The CPC display hardware generates a memory address by using the CRTC MA (MA0-MA13) and RA (RA0-RA5) outputs in the following way:

Memory address SignalSignal SourceSignal name
A156845MA13
A146845MA12
A136845RA2
A126845RA1
A116845RA0
A106845MA9
A96845MA8
A86845MA7
A76845MA6
A66845MA5
A56845MA4
A46845MA3
A36845MA2
A26845MA1
A16845MA0
A0Gate-Array CCLK

Notes

The screen has the following form:

When R9 is 7:
line 00 + &000
line 10 + &800
line 20 + &1000
line 30 + &1800
line 40 + &2000
line 50 + &2800
line 60 + &3000
line 70 + &3800
line 8((R1*1)*2) + &000
line 9(R1*2) + &800
line 10(R1*2) + &1000
line 11(R1*2) + &1800
line 12(R1*2) + &2000
line 13(R1*2) + &2800
line 14(R1*2) + &3000
line 15(R1*2) + &3800
line 16(R1*2) + &000
line 17(R1*2) + &800
line 18(R1*2) + &1000
line 19(R1*2) + &1800
line 20(R1*2) + &2000
line 21(R1*2) + &2800
line 22(R1*2) + &3000
line 23(R1*2) + &3800

CRTC character row = (y_coordinate/(R9+1))

CRTC character scan line = y_coordinate - (CRTC character row*(R9+1));

Byte offset to CRTC row start = CRTC character row * (R1*2)

Byte offset to CRTC character scan line = CRTC character scan line * &0800

Byte offset to start of line = Byte offset to CRTC row start + Byte offset to CRTC character scan line

For a 16k screen:

screen start byte offset = (((R12 & 0x03)*256) + (R13 & 255))*2

For a 32k screen:

screen start byte offset = (((R12 & 15)*256) + (R13 & 255))*2

On a hardware scrolling screen, there is a problem:
C7FF->C000
CFFF->C800
D7FF->D000
DFFF->D800
E7FF->E000
EFFF->E800
F7FF->F000
FFFF->F800