ZX BASIC: Test for 48/128k system

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

ZX BASIC: Test for 48/128k system

Post by cha05e90 »

Hello,

is there any way I can test from standard ZX BASIC whether I'm running on a 48K or 128K machine? For example by PEEKing a special system variable or the like?

(I found some tips for this but all of them use some kind of additional assembler routine or are somewhat "destructive".)

Regards, Frank
48K/+/+/+/+128K/Vega/Next&80/81&88
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: ZX BASIC: Test for 48/128k system

Post by Ast A. Moore »

PRINT PEEK 23611. Returns 221 for 128 BASIC and 204 for 48K.

Not foolproof, but should be okay if you test it early enough in your code.

Note: it doesn’t really test for the actual hardware—just the currently running OS.

P.S. And it’s officially called Sinclair BASIC, not ZX BASIC. ;)
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Re: ZX BASIC: Test for 48/128k system

Post by cha05e90 »

Ast A. Moore wrote: Mon Jul 29, 2019 2:01 pm PRINT PEEK 23611. Returns 221 for 128 BASIC and 204 for 48K.
Oh, thank you very much. That should do it!
Ast A. Moore wrote: Mon Jul 29, 2019 2:01 pmP.S. And it’s officially called Sinclair BASIC, not ZX BASIC. ;)
Whaaaaaaat? Ok, I'll try to internalize that - I used that "ZX BASIC" term for >30 years ... :o
48K/+/+/+/+128K/Vega/Next&80/81&88
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: ZX BASIC: Test for 48/128k system

Post by Ast A. Moore »

cha05e90 wrote: Mon Jul 29, 2019 2:15 pm I used that "ZX BASIC" term for >30 years ... :o
I’m sorry for ruining your life, pal. :lol:
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
spider
Dynamite Dan
Posts: 1088
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: ZX BASIC: Test for 48/128k system

Post by spider »

As a random sidenote the PEEK above also returns correctly *if* it was a 128K machine in USR0 , as in the 128 'bits' still there and not locked out, by correctly saying it is in 48K mode (which it is) :)
User avatar
1024MAK
Bugaboo
Posts: 3104
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: ZX BASIC: Test for 48/128k system

Post by 1024MAK »

Which “ZX BASIC”?
The ZX80 BASIC (“old ROM”)
The ZX80 BASIC (“new ROM”, actually ZX81 BASIC)
The ZX81 BASIC
The ZX Spectrum 16K/48K/Plus BASIC?
The ZX Spectrum 128K BASIC?
......

They all have differences.

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
spider
Dynamite Dan
Posts: 1088
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: ZX BASIC: Test for 48/128k system

Post by spider »

I think it might get annoying if you wanted to be really specific such as:

Is this machine:

48K

128K in 128K mode ?

128K in 48K (USR0 so 128 bits enabled) mode ?

Then...

128K +2 (grey) , although aside from the (c) messages this might 'test' the same as the original 128.

128K +2A/B

128K +3A/B

... and repeat the above for USR0 mode! :oops: as well as 48K mode.
User avatar
Alessandro
Dynamite Dan
Posts: 1908
Joined: Wed Nov 15, 2017 11:10 am
Location: Messina, Italy
Contact:

Re: ZX BASIC: Test for 48/128k system

Post by Alessandro »

Some years ago I asked the same to Einar. He suggested me this method, which I have been employing ever since:

Code: Select all

PRINT PEEK 2899
Returns 165 for a 48K, 159 for a 128K/+2 (under 128 as well as 48 BASIC) and 126 for a +2A/+2B/+3 (ditto).
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Sinclair BASIC: Test for 48/128k system

Post by cha05e90 »

Alessandro wrote: Mon Jul 29, 2019 9:38 pm Some years ago I asked the same to Einar. He suggested me this method, which I have been employing ever since:

Code: Select all

PRINT PEEK 2899
Returns 165 for a 48K, 159 for a 128K/+2 (under 128 as well as 48 BASIC) and 126 for a +2A/+2B/+3 (ditto).
Hm, another one. I must admit my first (simple) idea was to distinguish between a *real* 48k and a *real* 128k machine. I totally forgot that someone can use the 128K as a 48K ZX Spectrum (USR 0) ...;-). Thanks to [mention]spider[/mention] and [mention]Alessandro[/mention] ! My use case is (up to now) rather simple: Is there a "silicon disc" or not?

[Later I maybe want to know whether there's a +3 (disk drive) or not...:-)]
48K/+/+/+/+128K/Vega/Next&80/81&88
User avatar
Alessandro
Dynamite Dan
Posts: 1908
Joined: Wed Nov 15, 2017 11:10 am
Location: Messina, Italy
Contact:

Re: ZX BASIC: Test for 48/128k system

Post by Alessandro »

The silicon disk is only accessible on the 128K models. The syntax to do that is different however:
  • On the 128K/+2, commands must be followed by an exclamation mark, e.g. SAVE! "program" LINE 10, LOAD! "code" CODE etc.
  • On the +2A/+3, filenames must be preceded by m:, e.g. SAVE "m:program" LINE 10, LOAD "m:code" CODE etc. Alternatively, and for SAVE/LOAD operations only (no VERIFY or MERGE), you can switch the default drive to the silicon disk by entering a SAVE m: or LOAD m: command respectively. All subsequent SAVE or LOAD operations, depending on what you entered before, will deal with the silicon disk. To switch back to tape, enter SAVE "t:" (or LOAD "t:").
For more information, refer to page 9 of the Spectrum 128 introductive manual and to Chapter 8, part 20 of the +3 manual.
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Re: ZX BASIC: Test for 48/128k system

Post by cha05e90 »

Oh dear, they changed syntax from 128/+2 to +3/+2x? :o

I only know the first 128 (Toastrack) and assumed using the silicon disk is the same syntax with all 128K ZX Spectrums...:-(
48K/+/+/+/+128K/Vega/Next&80/81&88
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: ZX BASIC: Test for 48/128k system

Post by AndyC »

No, unfortunately not. I do prefer the +3DOS syntax, as it makes it much easier to allow a user to choose where to save, but the lack of support for the "old" 128K syntax makes it difficult to write a BASIC program which uses the extra memory and runs on all machines.
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Re: ZX BASIC: Test for 48/128k system

Post by cha05e90 »

AndyC wrote: Tue Jul 30, 2019 7:49 pm... makes it difficult to write a BASIC program which uses the extra memory and runs on all machines.
Oh, yes, seems so! Up to now I can determine whether there is a silicon disk or not with:

Code: Select all

1000 LET sys48=1: REM 48K as default, no silicon disk
1010 IF PEEK 2899=159 AND PEEK 23611=221 THEN LET sys48=0
...alas I learned now that this will only work on ZX Spectrums up to the +2. *sigh* :-)
48K/+/+/+/+128K/Vega/Next&80/81&88
XoRRoX
Manic Miner
Posts: 231
Joined: Wed Jul 11, 2018 6:34 am

Re: ZX BASIC: Test for 48/128k system

Post by XoRRoX »

I'm now doing it like this from my BASIC loader:

Code: Select all

...
10 POKE 49152,123: POKE 23388,17: OUT 32765,17: IF PEEK 49152<>123 THEN GOTO 20

{NOT a 128k machine - do somethings and NEW / STOP or sth.}

20 {a 128k machine}
User avatar
TMD2003
Rick Dangerous
Posts: 2032
Joined: Fri Apr 10, 2020 9:23 am
Location: Airstrip One
Contact:

Re: ZX BASIC: Test for 48/128k system

Post by TMD2003 »

AndyC wrote: Tue Jul 30, 2019 7:49 pm No, unfortunately not. I do prefer the +3DOS syntax, as it makes it much easier to allow a user to choose where to save, but the lack of support for the "old" 128K syntax makes it difficult to write a BASIC program which uses the extra memory and runs on all machines.
If anyone ever wonders why I had to make two different tape files for Corona Capers (not that anyone did...), refer to this post.

The only changes are because I needed to save and reload the loading screen from the RAM disc because I knew a CLEAR command was going to wipe it out. So, one version uses SAVE! and LOAD! and the other uses SAVE "m:" and LOAD "m:" (SCREEN$ both times, obviously). Other than that, they're identical...
Spectribution: Dr. Jim's Sinclair computing pages.
Features my own programs, modified type-ins, RZXs, character sets & UDGs, and QL type-ins... so far!
User avatar
Seven.FFF
Manic Miner
Posts: 735
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: ZX BASIC: Test for 48/128k system

Post by Seven.FFF »

On the +2A/+3/Next you can unmount/resize the RAMdisk with the DOS_SET_1346 call and even return to BASIC, so don't assume the RAMdisk is always available or has sufficient space.

On the Next the PEEK 23611 (sysvar) passes in all modes, but the PEEK 2899 (ROM) gives a different number in NextBASIC. You can't rightly expect an extended BASIC to have exactly the same bytes in every single ROM address, otherwise you wouldn't have any BASIC extensions. However NextBASIC is backwards compatible with 128K BASIC syntax and the hardware in NextBASIC mode is functionally a +3, so that ROM peek test needs expanding a little depending on what you are actually trying to test for.

ROM magic numbers are always best avoided unless you have no other mechanism to test. It's always best to test for the feature you plan to use, rather than an arbitrary attribute of a machine you expect to contain the feature. For features it's far better to test officially documented sysvars and API calls.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
XoRRoX
Manic Miner
Posts: 231
Joined: Wed Jul 11, 2018 6:34 am

Re: ZX BASIC: Test for 48/128k system

Post by XoRRoX »

Seven.FFF wrote: Fri Jul 10, 2020 9:31 pmIt's always best to test for the feature you plan to use

As I need to use the memory banks, how do you find my approach?
Alone Coder
Manic Miner
Posts: 401
Joined: Fri Jan 03, 2020 10:00 am

Re: ZX BASIC: Test for 48/128k system

Post by Alone Coder »

Mr Gluk Reset Service, ERS (and several TR-DOS boots and commanders AFAIR) run programs in USR0 mode. So, the only correct way to test for 128K is to switch banks, write numbers in them and compare.
User avatar
Seven.FFF
Manic Miner
Posts: 735
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: ZX BASIC: Test for 48/128k system

Post by Seven.FFF »

Alone Coder wrote: Mon Jul 13, 2020 5:43 am the only correct way to test for 128K is to switch banks, write numbers in them and compare.
I concur.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
C.Born
Manic Miner
Posts: 202
Joined: Sat Dec 09, 2017 4:09 pm

Re: ZX BASIC: Test for 48/128k system

Post by C.Born »

MODULA-2

Code: Select all

MODULE ZX;
FROM InOut IMPORT WriteString,WriteLn,WriteInt;
VAR [2899]    ZX : SHORTCARD;
BEGIN
WriteInt(ZX,3);
WriteLn;
WriteString("ZX Spectrum ");
IF ZX = 165 THEN
WriteString("48k");
WriteLn;
END;
IF ZX = 159 THEN
WriteString("128k");
WriteLn;
END;
IF ZX = 126 THEN
WriteString("+3");
WriteLn;
END;
END ZX.
Post Reply