Connect to Ainclair BASIC arrays.

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
cha05e90
Member
Posts: 23
Joined: Fri Jan 04, 2019 11:06 am

Connect to Ainclair BASIC arrays.

Post by cha05e90 »

Hi!

While programming with Sinclair BASIC and C (z88dk) I was rather pleasantly surprised that it is rather easy to connect the C code with Sinclair BASIC by exchanging BASIC variables with zx_getstr() and the like. ATM I'm using this to pass information between the BASIC and C portion of my program. Now my question:

Is it possible to read/write array variables like c(1) from a DIM c(10)-array? Same question for strings arrays, something like zx_set/getstr('a(1)') with (f.ex.) DIM a$(10,10).

BTW: Thanks for the z88dk!
stefano
Well known member
Posts: 1635
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

I'm sorry, at the moment only numeric types and normal strings are supported.
This type of functions can be created, but to be reasonably effective a whole set of functions should be prepared (picking a single 'cell' from an array, copying the whole matrix in a C storage area, dealing with lists or a bi-dimensional matrix, etc.. and doing it for float values, integers, and strings).

Workarounds are possible but require extensive tests to be sure they are stable enough, e.g. you can try invoking a more complex BASIC portion via zx_line.

I'd try something like this (not tested, watch out !)

Suppose you need to read the array in a$ at a given position, you could use zx_setint to load the position to variables x and y and add an extra line to your BASIC program (colon separated statements are allowed only on the Spectrum or TS2068, the ZX81 is much weaker with this kind of things and could not work at all):

9999 LET r$=a$(x,y)

Using zx_line(9999) in C should execute the single program line and return to caller.
Last step, read r$ with the C function zx_getstr('r').
cha05e90
Member
Posts: 23
Joined: Fri Jan 04, 2019 11:06 am

Post by cha05e90 »

stefano wrote:9999 LET r$=a$(x,y)

Using zx_line(9999) in C should execute the single program line and return to caller.
Last step, read r$ with the C function zx_getstr('r').
Oh, thank you, that's nice! ;-) I'm rather sure that should work (around).
stefano
Well known member
Posts: 1635
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

supposing an array is used to share a relevant quantity of data, thus copying the data in an internal struct in c could be even worse :)
I just wonder if the run status of the basic interpreter changes after such a similar hyperspace jump.
Post Reply