jkotlinski / durexforth

Modern C64 Forth
Other
233 stars 28 forks source link

at-xy #438

Open lonetech opened 2 years ago

lonetech commented 2 years ago

I didn't see where in the library at-xy was, so I cobbled one together using kernal plot:

: at-xy xr c! yr c! 0 sr c! $fff0 sys ;

Do you think it might be worth including somewhere?

It doesn't preserve interrupt disable, which might be something to improve. In assembly it would use CLC.

The routine itself would be grouped with page and emit, so I guess the norm here is assembly routines in io.asm. I did the exercise to convert to code:

code at-xy ( x y -- )
  clc,
  w stx,
  \ w 1+ sty,
  lsb 1+ ldy,x
  lsb lda,x  \ had no ldx,x
  tax,
  $fff0 jsr,
  w ldx,
  \ w 1+ ldy,
  inx,
  inx,
;code

That also worked. It's rather cumbersome code, though, mostly dealing with ABI conversion. Still size reports the code version as smaller than the sys version.

Evidently, I am a total newbie at 6502 assembly.

Whammo commented 2 years ago

Nice work! I can't speak for the repositor, but I believe that unless a routine is required for compiling the build, it's inclusion into the codebase is contraindicated. That's not to say that submissions are unwelcome. Durexforth acquires great power very easily. I think it's the C64's best kept secret.

jkotlinski commented 2 years ago

Nice! Maybe it would fit in compat.fs as an optional include?

lonetech commented 2 years ago

It does fit the compat.fs pattern as a word with standard functionality that isn't currently in use. I'd be quite happy with that.

Whammo commented 2 years ago

Perhaps it's corollary would also be appropriate? XY-AT ? XY@ ? PLOT ?

EDIT: SETCUR in V.fs is this, but uses SYS?

Whammo commented 2 years ago
: setcur ( x y -- )  \ hidden in v
xr ! yr ! $e50c sys ; 

vs.

code setcur ( x y -- )
   lsb lda,x
       inx,
   lsb ldy,x
       inx,
     w stx,
       tax,
 $e50c jsr,
     w ldx,
;code
lonetech commented 2 years ago

That's jumping inside the PLOT routine, to the set part. The $fff0 vector points to $e50a, which presumably does a branch if the carry flag is set (bcs) before falling through to $e50c. This sort of shortcut might make it incompatible with e.g. Commodore 128 80-column mode.

It is a significant find, though. It may make sense to change its name to at-xy and unhide it.

Whammo commented 2 years ago

Fun, fun, fun!

: setcur ( x y -- )  \ hidden in v
xr ! yr ! $e50c sys ;

code at-xy ( x y -- )
   lsb lda,x
       inx,
   lsb ldy,x
       inx,
     w stx,
       tax,
 $e50c jsr,
     w ldx,
;code

size setcur \ 20
size at-xy  \ 15
jkotlinski commented 2 years ago

A pull request with this addition would be welcome. I suppose it should be added to documentation and changelog, too.

VOC-LINK commented 2 years ago

Guys, three things:

1 It can be done much simpler way on good old C-64: : get-xy ( – x y ) 211 c@ 214 c@ ; : goto-xy ( x y – ) 214 c! 211 c! ;

  1. The name at-xy fits better the word „get character code at X, Y on the screen” ( at-xy ( x y — char ) ).
  2. There's no need to include everything anyone finds useful; it's Forth! The users include the words by themselves.
Whammo commented 2 years ago

This doesn't set the screen pointers properly,

: goto-xy ( x y – ) 214 c! 211 c! ;

VOC-LINK commented 2 years ago

Alas, you're right; X works OK, but Y (214) seems to make sense just as read-only. Probably during my quick-check I used the value that moved cursor into the current line and on first glance it looked OK.

Whammo commented 2 years ago

It's great fun to jump into the Kernal.

: goto-xy ( x y – ) 214 c! 211 c! $e56c sys ;

: goto-xy ( x y -- ) 214 c! 211 c!
$e56c sys ;

: try page
40 0 do 25 0 do j i goto-xy
118 emit loop
loop ;
VOC-LINK commented 2 years ago

Indeed. I have to admit my second mistake: since Durex Forth strives to be ANS-standard compliant, that word has to be named AT-XY, unfortunately, not GOTO-XY, But my new proposal for the name of „a word, that returns character code at X, Y” is XY@ ( x y -- char ) (on PC it would return character code and attribute byte).

VOC-LINK commented 2 years ago

Its code will be rather simple: : XY@ ( x y -- charcode ) 40 + 53272 C@ 16 / 1024 + C@ ;

Whammo commented 2 years ago

It is fun to not presume from where screen memory may be obtained. : )