Page 1 of 4

Equivalent of the NEW basic command ?

Posted: Thu May 08, 2014 5:01 pm
by Hialmar
I have some problems when switching from my C program to a Basic program.

I think my C program is messing with the place where the Basic stores variables and arrays.

I'd like to restore those pointers before switching to the Basic program.

So basically what I would need is a C or ASM equivalent to the NEW basic command.

Maybe I just need to "call" some ROM routine for that ?

Re: Equivalent of the NEW basic command ?

Posted: Thu May 08, 2014 9:08 pm
by Dbug
Hialmar wrote:I have some problems when switching from my C program to a Basic program.
I think my C program is messing with the place where the Basic stores variables and arrays.
I'd like to restore those pointers before switching to the Basic program.
Something you could try first, is to see that the BASIC program correctly calls HIMEM to specify the top of memory it can use, and have you C or assembler programs only access behind that.
By default the ORIC Basic puts the variable toward the end of memory, so if you overwrite that the BASIC is not going to be happy.

Re: Equivalent of the NEW basic command ?

Posted: Thu May 08, 2014 9:58 pm
by Hialmar
The problem is that both the basic and C programs use almost all memory.

That's why I switch from one to another.

In fact, after several trial and error I have located my problem.

I use the line8.s line drawing routine (found on miniserve) and it is something there that breaks the switch to basic program.

If I replace the DrawLine8 call by a curset and a draw it works.

It's sad because the DrawLine8 calls were way faster than curset and draw :(

Re: Equivalent of the NEW basic command ?

Posted: Thu May 08, 2014 10:09 pm
by Dbug
Hialmar wrote:In fact, after several trial and error I have located my problem.
I use the line8.s line drawing routine (found on miniserve) and it is something there that breaks the switch to basic program.
If I replace the DrawLine8 call by a curset and a draw it works.
It's sad because the DrawLine8 calls were way faster than curset and draw :(
Could be an issue with zero page variables being overwritten, or perhaps some of the BSS tables where I generate the line addresses.
If you can produce a minimal test case that shows the problem, that could help :)

Re: Equivalent of the NEW basic command ?

Posted: Thu May 08, 2014 10:36 pm
by Chema
Yeso, or at least put the file generated by osdk_showmap. I bet something important is being overwritten in the zero page

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 8:08 am
by Hialmar
assinie on oric.org had the same problem, it was because the program overwrites the Basic stack pointer in #87.

I now put a 0 there and it works.

If you want to test you can use my small example here:
http://torguet.net/Hialmar/OSDK/
* http://torguet.net/Hialmar/OSDK/C2BasicTest.zip is the OSDK project.
* you'll also need either
http://torguet.net/Hialmar/OSDK/bas2tap.exe
or
http://torguet.net/Hialmar/OSDK/tap2dsk.exe
or run the namer.com program in the dsk so that the basic program is correctly called.

Thanks to everyone.

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 8:25 am
by Hialmar
Ah well, I now have another error farther in the real Basic code :(

At the end of the program we look at the free memory and it crashes the program trying to execute opcode #72 in #20C9.

I will try to generate a small program with that but so far it didn't work.

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 10:34 am
by Hialmar
Ok I have made another small program isolating the error.

It is available here:
http://torguet.net/Hialmar/OSDK/C2BasicTest.zip
(as before you either have to use the NAMER.COM program or my versions of tap2dsk or bas2tap which name correctly COMBAT.COM).

The problem is not exactly the same as with my complete programs. Here I end up in an infinite loop around #D6DA.

On the C side, I draw a diagonal line (which uses a different asm code as the horizontal/vertical lines that crashed the Basic stack).
If you comment out the last DrawLine8(); (line 86) the code works.

Edit: I just regenerated the zip file as I had forgotten to update the map.
If you only want to see the map it's here: http://torguet.net/Hialmar/OSDK/map.htm

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 10:54 am
by Hialmar
assinie, again, found a solution.

I saved bytes from 84 to 88 at the beginning of the C program and restored them just before switching to basic and it worked.
It had something to do with the keyboard buffer.

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 10:56 am
by Chema
That code could surely crash BASIC because it is overwritting some zero page pointers which are used by the ROM routines. According to Geoff Phillips' book (http://home.btconnect.com/geffers/files/chap5.htm):

Code: Select all

#86 – Address of last temporary string.
#88 – #90 – A table of temporary strings.
So the solution could be to store those vars in locations #00 up to #0B, which are unused by BASIC. How? Well, the OSDK compiler places the vars automatically when they are defined. You can replace (maybe) the code which does something like:

Code: Select all

.zero

curBit .byt 00
chunk .byt 00
lastSum .byt 00
with something like:

Code: Select all

#define curBit $00
#define chunk $01
#define lastSum $02
Something like that. You could also use the operator *=$00 after the .zero directive, but I think this would restart the location of zero page variables and I am not sure if there could be any lateral effects.

Or, of course, you could try to save the value of those pointers before calling the routine, and put them back afterwards...

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 1:48 pm
by Hialmar
Yes thank you I have added a few more bytes to the ones I save at the beginning of the program and restore just before loading the basic program.

So far so good.

My program works correctly. :)

Again thank you, Chema and Dbug.

Edit: I'll try to relocate things as well, as I might corrupt some data used by some ROM routines I use.

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 6:26 pm
by Dbug
Hmm, I guess a worthy addition to XA could be to have an automatic support for 'system safe' zero page allocation.
Could be an optional mode where instead of saying "start at $50" it would automatically allocate in a list of known "safe" areas?

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 8:03 pm
by Chema
Why don't they start at $00, btw?

Re: Equivalent of the NEW basic command ?

Posted: Fri May 09, 2014 8:32 pm
by Dbug
Chema wrote:Why don't they start at $00, btw?
That was the value they (Fabrice, Vangelis, Alexios) chose in the original C SDK :)

Re: Equivalent of the NEW basic command ?

Posted: Fri Jun 19, 2015 9:51 pm
by Hialmar
Looks like I still have some problems with interactions from my basic programs and my C programs.

I get some Out Of Memory errors when I switch from Basic to C and back about 15 times.

Is it possible to put a breakpoint with Oricutron before the Out Of Memory error message is print out so that I can check the Stack Pointer ?