Jump to content

Advanced SYS: how to set parameters for ML routines


TomXP411
 Share

Recommended Posts

  • Super Administrators

A lot of the ML routines that we can access with the SYS command actually require the Accumulator, X, Y, or flags to be set to specific values. As it turns out, the BASIC SYS command actually has a way to pre-load these values into the CPU registers, as well as a way to read these back after the routine exits. 

These values are stored in $30C-30F, like this:
780/$30C: Accumulator
781/7$30D: X register
782/$30E: Y register
783/$30F: Status Register/Flags 

So if you wanted to call a routine like PLOT, which moves the cursor to column Y and row X, you'd set the registers like this: 

POKE 781, 10
POKE 782, 5
POKE 783, 0 : REM Clear the flags
SYS 65520

This will place the cursor on row 10, column 5. 

Or, if you wanted to read the cursor position, you can set the carry flag:

POKE 783,1
SYS 65520
X = PEEK(781)
Y=PEEK(782)

And yes - the X and Y are backward. The X register stores the row and Y stores the column. It probably has something to do with certain addressing modes only being available to the X register and others only being available to the Y register...

image.png.030f613a30c2d1554ac9b81408f48a20.png

Of course, the Commander has the LOCATE command, but if you're coding for BASIC 2 (C64, PET, etc), then this is the way to directly position the cursor. 

Another one that I find useful is the "stuff the keyboard" API:
image.png.327a1e0f6ac72c29d31b12bb1fed8532.png

This will print the appropriate commands, then position the cursor so that two presses of the RETURN key loads and runs the next program. And since the keyboard buffer conveniently has two CRs in it, your next program will run. 

Here's the output (assuming F$(C) is TREK.PRG):
image.png.e14ca941bacec521bfb23340e13b609a.png


 

  • Like 5
Link to comment
Share on other sites

I'm a bit familiar with the PLOT function. 😉 I may have mentioned it here on this forum once or twice...

But the keyboard stuff is quite interesting. I've seen it done many times; rarely have I seen an explanation.

  • Like 1
Link to comment
Share on other sites

  • Super Administrators
On 5/18/2022 at 8:18 AM, kelli217 said:

But the keyboard stuff is quite interesting. I've seen it done many times; rarely have I seen an explanation

Thanks.

Michael recently added the API to make it possible to do this. On the C64 (and prior to the API on the X16), you have to directly manipulate the keyboard buffer. That approach works fine when the buffer is in a known location, but the X16's ROM is still evolving, so the address of the keyboard buffer could change. 

On the 64, you'd do something like this:
POKE 631,13 : POKE 632,13 : POKE 198,2 

Locations 631-640 are the buffer itself (there's room for 10 keypresses), and the data at 192 is the length of the buffer. By setting that to 2, this causes the computer to process the first two values in the buffer. 

This technique would be basically the same on the X16, but since a bunch of things have changed locations, we can't count on the keyboard buffer being in one place. Hence the published API that pushes keys into the buffer. (I suspect the API was always there, just "unpublished".)

  • Like 1
Link to comment
Share on other sites

Can I use PLOT to position the cursor and then use a PRINT to print the text at that position?

What I'm seeing is that I can set the Row, but the column is reset before I do the PRINT:

10 POKE $30C, 30  :REM ROW = 30
20 POKE $30D, 40  :REM COL = 40
30 POKE $30E, 0    :REM CLEAR FLAGS
40 SYS $FFF0         :REM PLOT
50 PRINT "HELLO THERE"  :REM PRINTS AT ROW 30 COL 0

 

 

Link to comment
Share on other sites

  • Super Administrators
On 5/19/2022 at 2:46 PM, rje said:

Can I use PLOT to position the cursor and then use a PRINT to print the text at that position?

What I'm seeing is that I can set the Row, but the column is reset before I do the PRINT:

10 POKE $30C, 30  :REM ROW = 30
20 POKE $30D, 40  :REM COL = 40
30 POKE $30E, 0    :REM CLEAR FLAGS
40 SYS $FFF0         :REM PLOT
50 PRINT "HELLO THERE"  :REM PRINTS AT ROW 30 COL 0

Sorry... I looked this up on one of the Commodore 64 Wikis, which didn't spell it out very clearly, so I got the order wrong. 

I've fixed the Wiki entry and my examples above (with screen shots showing actual code and program runs).

https://www.c64-wiki.com/wiki/SYS#Passing_parameters_via_registers

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use