Maximising memory for BASIC program

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
User avatar
LordVaderUK
Posts: 208
Joined: Thu Jan 31, 2019 12:26 am
Location: Hampshire
Contact:

Maximising memory for BASIC program

Post by LordVaderUK » Mon Apr 20, 2020 4:00 pm

Can someone point me to an idiot's guide for maximising available memory for a BASIC programming running on the Beeb (or emulator). I found this thread which explains PAGE, LOMEM, HIMEM etc. but it's still a bit beyond me!

I am stuck with my adventure program, which runs in MODE 7 but crashes out shortly after starting due to a lack of memory...
Loving my BBC Master 128

User avatar
lurkio
Posts: 2907
Joined: Wed Apr 10, 2013 12:30 am
Location: Doomawangara
Contact:

Re: Maximising memory for BASIC program

Post by lurkio » Mon Apr 20, 2020 4:10 pm

LordVaderUK wrote:
Mon Apr 20, 2020 4:00 pm
Can someone point me to an idiot's guide for maximising available memory for a BASIC programming running on the Beeb (or emulator). I found this thread which explains PAGE, LOMEM, HIMEM etc. but it's still a bit beyond me!

I am stuck with my adventure program, which runs in MODE 7 but crashes out shortly after starting due to a lack of memory...
It would help to know a bit more about your program.

Is your program running from disc (as opposed to tape)?

If disc, then does your program need to save or load data to or from disc at any point?

:?:

User avatar
1024MAK
Posts: 10292
Joined: Mon Apr 18, 2011 5:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: Maximising memory for BASIC program

Post by 1024MAK » Mon Apr 20, 2020 4:16 pm

Remove all unnecessary white space (spaces).
Use the built in variables where possible, A% etc...
Remove all REM statements - keep a separate fully documented file.

Mark

User avatar
sydney
Posts: 2721
Joined: Wed May 18, 2005 10:09 am
Location: Newcastle upon Tyne
Contact:

Re: Maximising memory for BASIC program

Post by sydney » Mon Apr 20, 2020 4:25 pm

I've never used it in anger but could you use an emulated or real master 128 in conjunction with BAS128 and find out how much memory you actually need to save?

User avatar
LordVaderUK
Posts: 208
Joined: Thu Jan 31, 2019 12:26 am
Location: Hampshire
Contact:

Re: Maximising memory for BASIC program

Post by LordVaderUK » Mon Apr 20, 2020 5:09 pm

OK thanks folks for the replies so far.

All of the bulky program data (messages, room descriptions, etc) is stored in separate files. The main program uses a small index to jump straight to the appropriate point in whatever file and load the relevant bit of text. It will also have a load/save player function (when I write it). So it's definitely a disk based program.

I have already gone through and converted most of my beautifully named variables (RoomCount%, Item%, Oxygen%) to be the inbuilt integer variables (A%, B% etc), although I still have more work to do.

I will try and remove spaces, etc. as well as REM statements (I have lots) as I didn't realise these were going to be taking up memory space. Obvious when I think about it as the BASIC program is loaded into memory in order to RUN... :lol:

Currently I have no clue how far off I am in terms of needing to save memory. Is there a simple command (HIMEM - PAGE or something) which will give me free memory? I can obviously then keep an eye on that to see where the remaining free memory is being gobbled up.
Loving my BBC Master 128

User avatar
sydney
Posts: 2721
Joined: Wed May 18, 2005 10:09 am
Location: Newcastle upon Tyne
Contact:

Re: Maximising memory for BASIC program

Post by sydney » Mon Apr 20, 2020 5:20 pm

Code: Select all

PRINT HIMEM - TOP
Will give you the number of bytes between the end of your program and the screen but it won't include variables which I think are usually stored after the BASIC program.
I'm sure I asked a similar question in the past but can't remember when I asked nor the answer.

User avatar
sydney
Posts: 2721
Joined: Wed May 18, 2005 10:09 am
Location: Newcastle upon Tyne
Contact:

Re: Maximising memory for BASIC program

Post by sydney » Mon Apr 20, 2020 5:30 pm

Some threads that might be useful:

BBC BASIC memory usage
Using memory below &0E00 from BASIC

And if you wanted to use sideways ram a poll showing how common it is:
Sideways ram: Who had it and who's got it?

User avatar
lurkio
Posts: 2907
Joined: Wed Apr 10, 2013 12:30 am
Location: Doomawangara
Contact:

Re: Maximising memory for BASIC program

Post by lurkio » Mon Apr 20, 2020 5:36 pm

LordVaderUK wrote:
Mon Apr 20, 2020 5:09 pm
So it's definitely a disk based program.
With Acorn DFS, you could still lower PAGE to &1300 if you can guarantee that only one file will be open at any one time. But if more than one file will be open simultaneously, then you'll have to raise PAGE accordingly: See this post.

LordVaderUK wrote:
Mon Apr 20, 2020 5:09 pm
I have already gone through and converted most of my beautifully named variables (RoomCount%, Item%, Oxygen%) to be the inbuilt integer variables (A%, B% etc), although I still have more work to do.
You could store your program listing in a source text file on your modern OS, with long variable names and all your REMs intact, and then use the Pack routine in the PRES Advanced BASIC Editor ROMs to compress the variable names, and also to remove REMS and spaces, etc., whenever you transfer the program to the Beeb (emulator). I highly recommend the use of the PRES Advanced BASIC Editor for compressing BASIC programs!

LordVaderUK wrote:
Mon Apr 20, 2020 5:09 pm
I will try and remove spaces, etc. as well as REM statements (I have lots) as I didn't realise these were going to be taking up memory space.
The Pack routine in the PRES Advanced BASIC Editor can do all that for you!

LordVaderUK wrote:
Mon Apr 20, 2020 5:09 pm
Currently I have no clue how far off I am in terms of needing to save memory. Is there a simple command (HIMEM - PAGE or something) which will give me free memory? I can obviously then keep an eye on that to see where the remaining free memory is being gobbled up.
To determine the amount of RAM between the end of your BASIC program and the start of screen memory (if you haven't manually tinkered with HIMEM yourself):

Code: Select all

PRINT ~HIMEM - TOP
To determine the amount of RAM between the top of the BASIC heap (the area where the variables are stored) and the start of screen memory (if you haven't manually tinkered with HIMEM yourself), first RUN your program, wait till it finishes initialising variables and loading data, and then press Escape, and then type this:

Code: Select all

PRINT ~HIMEM - (!2AND&FFFF)
:idea:

User avatar
Lardo Boffin
Posts: 2171
Joined: Thu Aug 06, 2015 7:47 am
Contact:

Re: Maximising memory for BASIC program

Post by Lardo Boffin » Mon Apr 20, 2020 7:57 pm

Remove as many separate lines as you can - each line takes some bytes for the line number and another for the end of line marker. Be careful with combining IF statements though - that can get very confusing!

Also short PROCedure and FN names.

Mine without game logic is about 6K and I have not used any compacting tools yet.
Adventure Language on GitHub
Atom, issue 5
Elk
A number of econetted (is that a word?) Beebs
BBC Master, Datacentre + HDD, pi co-proc, econet, NULA

User avatar
lurkio
Posts: 2907
Joined: Wed Apr 10, 2013 12:30 am
Location: Doomawangara
Contact:

Re: Maximising memory for BASIC program

Post by lurkio » Tue Apr 21, 2020 1:07 am

Lardo Boffin wrote:
Mon Apr 20, 2020 7:57 pm
Remove as many separate lines as you can - each line takes some bytes for the line number and another for the end of line marker. Be careful with combining IF statements though - that can get very confusing!
That's why you should let the Pack routine in the PRES Advanced BASIC Editor do it for you -- because it can concatenate lines safely, in addition to its other features!

Lardo Boffin wrote:
Mon Apr 20, 2020 7:57 pm
Also short PROCedure and FN names.
Yep. PRES ABE Pack can do that too.

I'm aware that I must have mentioned PRES ABE Pack more than anyone else on this forum and quite probably more than anyone else in history. Wish I was on commission.

:idea:

User avatar
daveejhitchins
Posts: 5913
Joined: Wed Jun 13, 2012 6:23 pm
Location: Newton Aycliffe, County Durham
Contact:

Re: Maximising memory for BASIC program

Post by daveejhitchins » Tue Apr 21, 2020 7:49 am

lurkio wrote:
Tue Apr 21, 2020 1:07 am
I'm aware that I must have mentioned PRES ABE Pack more than anyone else on this forum and quite probably more than anyone else in history. Wish I was on commission.
I'm sure we can sort some sort of royalty out for you :D . . .Oh! Wait a minute - it's free to use - Darn :mrgreen: But I agree, it's a great piece of kit. I use it all the time - especially with the MGC code.

Dave H :D

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Maximising memory for BASIC program

Post by AJW » Fri Jun 05, 2020 11:08 pm

With my Dominion text game I loaded in room data in blocks and compressed the long room descriptions. Tbe decompression toutine was in BASIC so slow but worked

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: Maximising memory for BASIC program

Post by Richard Russell » Fri Jun 05, 2020 11:51 pm

lurkio wrote:
Mon Apr 20, 2020 5:36 pm

Code: Select all

PRINT ~HIMEM - (!2AND&FFFF)
Eek, that's highly non-portable. The legal way to do it is:

Code: Select all

DIM P% -1 : PRINT ~ HIMEM-P%

Adam James
Posts: 196
Joined: Tue May 26, 2020 2:32 pm
Contact:

Re: Maximising memory for BASIC program

Post by Adam James » Wed Jun 17, 2020 11:54 pm

I'm writing my first BBC BASIC program, learning as I go:)

I wanted it to run in MODE 0 but had set up several variables so it could also run in lower res modes if I ran out of memory.

Sure enough I just got a NO ROOM... error message. So I switched to a lower res mode, but was not happy with the resolution - I feel for this game it's MODE 0 or go home. Ironically in order to reduce the program size I've now removed the variables that let me use different modes - so now I'm totally committed to MODE 0.

So I started to reduce the size of the program, and even resigned myself to ditching certain ideas, though it may make for a more elegant game in the end so I'm quite optimistic.

Anyway, reading this thread, it seems to me that the best simple first step after encountering NO ROOM is to set the PAGE to &1100. It's quick to do and gets you about 2K more memory, is that right? If so it may have saved me a big headache.

Is it okay/safe for the first program to display instructions, then set PAGE to &1100 then CHAIN in the next program - the first program disappears and the next one gets loaded in starting at a lower address? Any pitfalls to look out for?

User avatar
daveejhitchins
Posts: 5913
Joined: Wed Jun 13, 2012 6:23 pm
Location: Newton Aycliffe, County Durham
Contact:

Re: Maximising memory for BASIC program

Post by daveejhitchins » Thu Jun 18, 2020 8:35 am

Why not try writing it in an emulator (I use BeebEm) with Master selected. You can switch to Shadow Mode and write Verbose. Then save - Pack (use ABE - Also use the editor!) - switch machines (within BeebEm) and make sure you can still run your program. And lastly check on physical hardware.

Takes a little more time, but does work.

Dave H :D

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Maximising memory for BASIC program

Post by AJW » Thu Jun 18, 2020 9:14 am

Look at the memory map on 8BS. You can use pages &0900-A00 with some sacrifices. Also i can only get PAGe down to &1400 using 14-16 without the program not working. Im not sure why as it only loads in one data-file.

Adam James
Posts: 196
Joined: Tue May 26, 2020 2:32 pm
Contact:

Re: Maximising memory for BASIC program

Post by Adam James » Thu Jun 18, 2020 10:42 am

Thanks guys.

I don't want to use trickery like compaction or emulators unless I get desperate. A big part of the fun for me is nostalgia; I'm writing it on a real BBC B and imagining it's the early 80's and I've just got hold of a BBC:)

I am willing to go that route if I must but it will sap a lot of the sense of satisfaction.

Looking at that memory map on 8BS, it seems positively 'the idea' / 'encouraged' to lower PAGE then? I just want to make sure it was safe to do so and that I'm not going to be heading down a route which will take even more time to undo. I don't think I'll need to load any data files, it's not that sort of program.

User avatar
lurkio
Posts: 2907
Joined: Wed Apr 10, 2013 12:30 am
Location: Doomawangara
Contact:

Re: Maximising memory for BASIC program

Post by lurkio » Thu Jun 18, 2020 12:34 pm

Adam James wrote:
Thu Jun 18, 2020 10:42 am
I don't want to use trickery like compaction or emulators unless I get desperate. A big part of the fun for me is nostalgia; I'm writing it on a real BBC B and imagining it's the early 80's and I've just got hold of a BBC
Fair enough. Just to say, though, that the PRES Advanced BASIC Editor, which includes the "Pack" compaction routine, was a pair of ROMs on a daughter-board(?) that was sold commercially and installed in real Beebs back in the day. So it is definitely an "authentic" vintage Beeb experience! (Of course, to install it in your real Beeb today you'd need to either blow the two ROM images onto EPROMs or load them into Sideways RAM.)

Adam James wrote:
Thu Jun 18, 2020 10:42 am
Looking at that memory map on 8BS, it seems positively 'the idea' / 'encouraged' to lower PAGE then? I just want to make sure it was safe to do so and that I'm not going to be heading down a route which will take even more time to undo.
In a post upthread I think I've linked to another post of mine in which I quote Colin Pharo on the effects of lowering PAGE to various different values.

Adam James wrote:
Thu Jun 18, 2020 10:42 am
I don't think I'll need to load any data files, it's not that sort of program.
What sort of program is it?

:?:

User avatar
LordVaderUK
Posts: 208
Joined: Thu Jan 31, 2019 12:26 am
Location: Hampshire
Contact:

Re: Maximising memory for BASIC program

Post by LordVaderUK » Thu Jun 18, 2020 12:53 pm

Adam James wrote:
Thu Jun 18, 2020 10:42 am
Thanks guys.

I don't want to use trickery like compaction or emulators unless I get desperate. A big part of the fun for me is nostalgia; I'm writing it on a real BBC B and imagining it's the early 80's and I've just got hold of a BBC:)

I am willing to go that route if I must but it will sap a lot of the sense of satisfaction.

Looking at that memory map on 8BS, it seems positively 'the idea' / 'encouraged' to lower PAGE then? I just want to make sure it was safe to do so and that I'm not going to be heading down a route which will take even more time to undo. I don't think I'll need to load any data files, it's not that sort of program.
As the OP I have made good use of the PACK utility. I develop and debug code in BBC BASIC using the SDLIDE editor, then copy it across to the B2 emulator for testing where (if necessary) I PACK the code. Finally I'll port it across to the actual BBC hardware for a final test.

I appreciate your want to keep things 'pure', but with all the advantages of fast computers these days it seems a bit mad not to make use of them for development. It will take you a lot longer if you just restrict yourself to developing on the actual hardware.
Loving my BBC Master 128

Adam James
Posts: 196
Joined: Tue May 26, 2020 2:32 pm
Contact:

Re: Maximising memory for BASIC program

Post by Adam James » Thu Jun 18, 2020 3:19 pm

lurkio wrote:
Thu Jun 18, 2020 12:34 pm
Fair enough. Just to say, though, that the PRES Advanced BASIC Editor, which includes the "Pack" compaction routine, was a pair of ROMs on a daughter-board(?) that was sold commercially and installed in real Beebs back in the day. So it is definitely an "authentic" vintage Beeb experience! (Of course, to install it in your real Beeb today you'd need to either blow the two ROM images onto EPROMs or load them into Sideways RAM.)

In a post upthread I think I've linked to another post of mine in which I quote Colin Pharo on the effects of lowering PAGE to various different values.

What sort of program is it?
Point taken, and I'm willing to even switch to emulation if really necessary. But I intend to fight against it tooth and nail:)

TBH with all the learning I've been doing recently my head hurts, I think I'll just lower PAGE and if it works it works. I'm sure I haven't grasped all of the issues but it seems to be the done thing if you don't need to load in multiple data files, and I don't need to load in any, or do anything else that appears to be out of the ordinary.

As it happens I haven't needed to lower PAGE yet, I've made the program smaller and removed some features of the game. Frankly the features that I've removed I was struggling to get working properly anyway, due to limits of both processing speed and my brain.

It's an 'artillery' type game but with enough 'new stuff' in it compared to other similar games I could find on the BBC that I'm hoping it will be worth sharing here when finished.
LordVaderUK wrote:
Thu Jun 18, 2020 12:53 pm
As the OP I have made good use of the PACK utility. I develop and debug code in BBC BASIC using the SDLIDE editor, then copy it across to the B2 emulator for testing where (if necessary) I PACK the code. Finally I'll port it across to the actual BBC hardware for a final test.

I appreciate your want to keep things 'pure', but with all the advantages of fast computers these days it seems a bit mad not to make use of them for development. It will take you a lot longer if you just restrict yourself to developing on the actual hardware.
Yep I've started to realise the extra time it takes to use the real BBC. From copying entire lines just to make one change, to reorganising line numbers, and now virtually re-writing it to 'make it fit' and strip out features.

Added to that, the number of times I've reached for an imaginary mouse, or tried to continue copying a line only to realise I've been holding down the 'delete' key that's right next to 'copy'.

But I will prevail! Once I've learned my lesson from this, if I write anything else, I will probably be sensible rather than nostalgic. The SDLIDE editor looks great. But for now, it's as much the process as the result that I'm interested in.

User avatar
hjalfi
Posts: 138
Joined: Sat May 13, 2017 11:17 pm
Location: Zürich, Switzelrand
Contact:

Re: Maximising memory for BASIC program

Post by hjalfi » Sun Jun 21, 2020 7:46 pm

Incidentally, one space-saving tip which I learnt back in the day: floating point variables use less memory than integer variables, even though they're bigger.

A float variable (like 'a') takes five bytes, and an integer variable (like 'a%') takes four. So it seems reasonable that integer variables would use less space, right? Except that integer variables require the '%' suffix every time you refer to them, which more that outweighs the savings. If you refer to an integer variable fifty times in the program, that's fifty bytes of % that your program contains. Float variables don't have a suffix and so are cheaper to use. (They are, of course, slower, but that shouldn't matter for an adventure game.)
David Given
http://cowlark.com

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Maximising memory for BASIC program

Post by AJW » Sun Jun 21, 2020 7:58 pm

Interesting.

jregel
Posts: 218
Joined: Fri Dec 20, 2013 6:39 pm
Location: Gloucestershire
Contact:

Re: Maximising memory for BASIC program

Post by jregel » Mon Jun 22, 2020 10:59 am

Richard Russell wrote:
Fri Jun 05, 2020 11:51 pm
lurkio wrote:
Mon Apr 20, 2020 5:36 pm

Code: Select all

PRINT ~HIMEM - (!2AND&FFFF)
Eek, that's highly non-portable. The legal way to do it is:

Code: Select all

DIM P% -1 : PRINT ~ HIMEM-P%
Please could you explain what's happening here?

As I read it, you're creating a byte array that is 0 bytes long? Would this be added to the end of stored variables, so removing HIMEM from it would give the top most variable address used?
BBC Master Turbo, Retroclinic External Datacentre, VideoNuLA, PiTubeDirect with Pi Zero, Gotek USB Floppy Emulator

User avatar
jgharston
Posts: 4120
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Maximising memory for BASIC program

Post by jgharston » Mon Jun 22, 2020 11:24 am

jregel wrote:
Mon Jun 22, 2020 10:59 am
Richard Russell wrote:
Fri Jun 05, 2020 11:51 pm

Code: Select all

DIM P% -1 : PRINT ~ HIMEM-P%
Please could you explain what's happening here?
As I read it, you're creating a byte array that is 0 bytes long? Would this be added to the end of stored variables, so removing HIMEM from it would give the top most variable address used?
No, removes it from HIMEM. It's giving you the amount of space left in the heap (the area used to store variables) - between the end of variables and the end of the heap (the end of the area used to store variables). It assumes there's nothing on the stack - the area just underneath HIMEM - but in most cases that's an adequate assumption.

For example, DIM P% -1 might set P% to &48CC. HIMEM might be &1C800. Consequently, HIMEM-P% would give you &1C800-&48CC, the amount of space between P% (the top of the used space in the heap) and HIMEM (the top of the workspace).

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,2005-2020
>_

Post Reply

Return to “programming”