How to locate and set the cursor on your Commodore C128

Commodore LogoSadly the Commodore machines don’t offer a routine to locate or set the current cursor position via BASIC. There is however a Kernel routine named PLOT which can do this in Machine Language.

Here’s how we can utilise it.

Getting the Cursor Position

. 01300  38       sec
. 01301  20 f0 ff jsr $fff0
. 01304  8e 00 14 stx $1400
. 01307  8c 01 14 sty $1401
. 0130a  60       rts

This snippet sets the carry flag, calls the PLOT routine at $FFF0 and returns the cursor position in the X and Y registers. We’ll put them in a safe place into $1400 and $1401 to use.

You can call it from BASIC with

SYS DEC("1300")
PRINT PEEK(DEC"1400") : rem row
PRINT PEEK (DEC("1401") : rem coumn

Setting the Cursor Position

. 01310  18       clc
. 01311  ae 00 14 ldx $1400
. 01314  ac 01 14 ldy $1401
. 01317  20 f0 ff jsr $fff0
. 0131a  60       rts

Call it from BASIC by POKEing your desired coordinates into $1400 and $1401, then call SYS DEC(“1310″).

This snippet will do the reverse of the above: populate the X and Y registers from our safe place and then call PLOT. We clear the carry flag first, because it decides if the position is read (carry clear) or set (carry set).

PLOT should work fine on the C64 and Plus/4 as well but I didn’t get a chance to test it yet.

You can leave a comment on my original post.