commanderx16 / x16-rom

Other
153 stars 43 forks source link

Implement Math library function VAL_1 #242

Open mist64 opened 2 years ago

mist64 commented 2 years ago

The X16 ROM supports the C128/C65 Math package, as described here:

The only missing function is VAL_1, which converts an ASCII string to floating point. The C128 implementation calls FIN, which depends on both the string library and CHRGET, both of which are part of BASIC not of the Math package, so the backport isn't straightforward.

We need to write an implementation that is independent of BASIC, pulls in as little of BASIC as possible, and, if possible, have BASIC use it.

irmen commented 2 years ago

How does basic do string to float parsing now; can't we just jump to that routine to implement VAL_1?

mist64 commented 2 years ago

We could (and that's what the C128 does), but it would break the separation between BASIC and the Math library, as I've attempted it in the X16.

The original Microsoft BASIC 6502 source code had Math package almost 100% separate from the actual BASIC interpreter. So I thought it would be great to separate it out for the X16: If a machine code application wants to use floating point Math, but does not use the BASIC interpreter, it can reuse the BASIC zero page area, as long as it avoids the Math library's area:

https://github.com/commanderx16/x16-docs/blob/master/Commander%20X16%20Programmer's%20Reference%20Guide.md#zero-page

The Commodore computers never had such a separation. If you wanted to use floating point but not BASIC, you had to be either avoid the complete BASIC zero page area, or be very careful about which addresses to use.

The problem is that VAL_1 ultimately relies on FIN, which uses CHRGET, which uses a lot of BASIC zero page space.

irmen commented 2 years ago

thank you for the explanation