Page 1 of 1

Advanced SYS: how to set parameters for ML routines

Posted: Tue May 17, 2022 6:12 am
by TomXP411

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



 


Advanced SYS: how to set parameters for ML routines

Posted: Wed May 18, 2022 3:18 pm
by kelli217

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.


Advanced SYS: how to set parameters for ML routines

Posted: Wed May 18, 2022 4:32 pm
by TomXP411


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".)


Advanced SYS: how to set parameters for ML routines

Posted: Thu May 19, 2022 9:46 pm
by rje

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

 

 


Advanced SYS: how to set parameters for ML routines

Posted: Thu May 19, 2022 9:58 pm
by desertfish

yeah I can reproduce this , this somehow doesn't work as advertised.

The obvious fix is to just use the LOCATE x,y command .  But should the above not work as well?


Advanced SYS: how to set parameters for ML routines

Posted: Thu May 19, 2022 10:02 pm
by kelli217

Yeah, I know why. Something got jumbled up somewhere. The order of registers for making a SYS call is A, X, Y, SR for $30C through $30F; Tom's original post has them all rotated.

 


Advanced SYS: how to set parameters for ML routines

Posted: Fri May 20, 2022 2:15 am
by TomXP411


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


Re: Advanced SYS: how to set parameters for ML routines

Posted: Sun Jun 25, 2023 8:07 pm
by Martin Schmalenbach
For some reason the graphics posted in the original pst are no longer available.

Could somebody repost them please?

Cheers!

Re: Advanced SYS: how to set parameters for ML routines

Posted: Tue Jun 27, 2023 8:15 am
by TomXP411
Those examples are all out of date, for one reason or another... I'll work up some new examples. If I don't have them in a couple of days, remind me.

Re: Advanced SYS: how to set parameters for ML routines

Posted: Tue Jun 27, 2023 8:15 am
by TomXP411
Those examples are all out of date, for one reason or another... I'll work up some new examples. If I don't have them in a couple of days, remind me.