BASIC - how to allocate memory for asm/data?

By skumlerud

Resident (58)

skumlerud's picture

15-01-2023, 21:51

I'm playing around with BASIC/XBASIC and machine code subroutines. I've figured out the parameter passing, and I am using CLEAR to allocate some memory between MEMTOP and 0xf1c9. However, what I don't understand is how to know how much you can safely lower MEMTOP. E.g. I'd like to reserve 5k for data and assembler routines, then I would set MEMTOP to 0xf1c9-0x1400=0xddc9, right? But how do I know if this "eats" into BASIC/XBASIC RAM?

Another thing - on my SVI738 with MSX2+ ROMs MEMTOP is at 58656/0xe520. So there is already some reserved space between MEMTOP and 61897/0xf1c9 where DOS/Disk-BASIC work area begins (according to https://www.msx.org/wiki/The_Memory), correct? Would this always be the case?

Edit: Seems like I've misunderstood the memory map above. If I try to raise MEMTOP to slightly below 0xf1c9 the disk operations no longer works. So it looks like the default MEMTOP indeed is MEMTOP and should not be touched.

Login or register to post comments

By pgimeno

Champion (328)

pgimeno's picture

16-01-2023, 21:58

skumlerud wrote:

I'm playing around with BASIC/XBASIC and machine code subroutines. I've figured out the parameter passing, and I am using CLEAR to allocate some memory between MEMTOP and 0xf1c9. However, what I don't understand is how to know how much you can safely lower MEMTOP. E.g. I'd like to reserve 5k for data and assembler routines, then I would set MEMTOP to 0xf1c9-0x1400=0xddc9, right? But how do I know if this "eats" into BASIC/XBASIC RAM?

Easy - a CLEAR is controlled by BASIC and it will give an error if it's lower than the minimum you can set it to. It reduces available memory and will cause an OOM if your variables consume all the available space between the end of BASIC and HIMEM (there's no system variable called MEMTOP).

So, read HIMEM, subtract 5K from it and do a CLEAR with the result. The CLEAR will fail if there isn't enough memory.

By Manuel

Ascended (19683)

Manuel's picture

16-01-2023, 21:34

As ICP/5 put it, for a machine code program of length 207 bytes:

50 ' PAS TOP OF BASIC MEMORY AAN ****
60 CLEAR 200,(PEEK(&HFC4A)+256*PEEK(&HfC4B))-207

An alternative on machines with at least 32kB RAM is to move the start of basic (TXTTAB) to a higher address and put your machine code routine before that, from 0x8000 onwards.
E.g. make a basic program that first checks on TXTTAB and if it's not what you want it to be, changes it and runs itself again:

10 IF PEEK&HF677<>&HC0:POKE&HF677,&HC0:POKE&HC000,0:RUN"MYPROGRAM"
20 REM THE REST OF THE PROGRAM

many ROM cracks use this trick to free up 16kB in page 2. But you can of course also modify it for a smaller amount.
It's important that the program gets (re)loaded after changing TXTTAB.

By SvaboMSX

Resident (40)

SvaboMSX's picture

16-01-2023, 21:24

In some notes on my side I found something relating to your question.
Occupied (basic) memory End is saved in the byte locations hFC4A and hFC4B
Expressed as integer value you could calculate first this adress and subtract the to be freed memory size of 5k.

E.g.
AN = PEEK(&HFC4A)+PEEK(&HFC4B)*256
PRINT AN
resulting in 57976

Subtracting 5*1024 would be 57976-5120 = 52856th memory location.
To set BASIC End Memory lower than this you would say after calculating AN above:

clear 200, AN-5120

your org of the assembly programs would be hCE78 (52856 in hex)

hope this is correct. @Others: correct me please if I am wrong.
.
In general, if no disk drive is plugged in the systemmemory begins on hF380
If a disk drive is plugged in the systemmoery begins lower starting on hE278

By SvaboMSX

Resident (40)

SvaboMSX's picture

16-01-2023, 21:25

Manuel was faster. Yes. :-)

By skumlerud

Resident (58)

skumlerud's picture

17-01-2023, 22:09

Thanks folks, I have tested this and it works fine. I would need to make sure that my assembler code does not contain any absolute jumps though, but that's something I'm used to from the 68k so I'm ok with that.

But... Not so sure about this when it comes to XBASIC:

pgimeno wrote:

So, read HIMEM, subtract 5K from it and do a CLEAR with the result. The CLEAR will fail if there isn't enough memory.

CLEAR himem-5k works fine, but with XBASIC active file functions like BLOAD stops working if I lower himem by 6k. However, there are no warnings/errors from CLEAR or any "out of memory" errors, things just stops working. My guess is that XBASIC is messing with the disk work area when there's too little available memory. So is there a way to figure out how much memory XBASIC actually requires?