BASIC: Find out if an Interface 1 is attached.

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

BASIC: Find out if an Interface 1 is attached.

Post by cha05e90 »

My rather naive idea is to PEEK some system variable(s) to find out if there's a ZX Interface 1 attached or not. Is there any reliable candidate for such a test?
48K/+/+/+/+128K/Vega/Next&80/81&88
User avatar
spider
Dynamite Dan
Posts: 1099
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: BASIC: Find out if an Interface 1 is attached.

Post by spider »

I either asked about this or meant to ask and forgot. Assuming the IF1 is vaguely alive you can try this for now although it is not what you wanted I know:

Do a NEW or reset then just enter RUN as the first direct command. Don't type anything else in first.

If it just comes back as "0 OK" , its not present or detected. If it comes back with "Program Finished" or "Microdrive Not Present", then it is half alive at least.

There is or probably should be an easy way to PEEK a value to determine if its there or not though. Unsure if differing values would be needed if said PEEK was tried on say a +3 (or model that does not support an IF1) etc
User avatar
spider
Dynamite Dan
Posts: 1099
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: BASIC: Find out if an Interface 1 is attached.

Post by spider »

Additional bit of info: I think on a real machine you get the 'Program Finished' error almost straight away if there are no Microdrive's actually connected. Otherwise you get a Microdrive not present if you have one or more and there's no cart in the first one, I think. :)

I do recall the former message on my 48K with the IF1 attached, the latter I've seen under emulation but that would include a minimum of one drive 'attached'
User avatar
jpnz
Manic Miner
Posts: 319
Joined: Tue Nov 14, 2017 4:07 pm
Location: Hamilt[r]on - City Of The Future - NZ

Re: BASIC: Find out if an Interface 1 is attached.

Post by jpnz »

cha05e90 wrote: Thu Sep 19, 2019 4:24 pm My rather naive idea is to PEEK some system variable(s) to find out if there's a ZX Interface 1 attached or not. Is there any reliable candidate for such a test?
There is a rudimentary method of checking NMIADD
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Re: BASIC: Find out if an Interface 1 is attached.

Post by cha05e90 »

Ah, thanks for the hints.

The use case is to check for a attached Interface 1 from inside a running BASIC program. So I might try (or alter...) the NMIADD stuff from Andrew. AFAIK the system variable area is different for a ZX Spectrum 48/128 with and without an Interface 1, so I thought there might be a simple check for something in that area to determine the existence of that interface.
48K/+/+/+/+128K/Vega/Next&80/81&88
User avatar
spider
Dynamite Dan
Posts: 1099
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: BASIC: Find out if an Interface 1 is attached.

Post by spider »

I would of thought so too but I've not seen anything posted.

I wonder although tis a pain perhaps to do a FOR/NEXT loop and print out all the values of the system variables (do a CLEAR then RUN) and then repeat with an IF1 attached, and then compare the results. Obviously under emulation for convenience. Would suspect a couple of them will of changed, Just a question of not mistaking anything "program related"

In theory it should be as simple as IF PEEK xyz <> (something) THEN... type of thing but in reality...

EDIT... I wonder if any of the ports behave completely different if an IF1 is there or not , as in say IN (xyz) ? Slight advantage of that is you can easily use it from Basic or Assembly.
User avatar
Pobulous
Dynamite Dan
Posts: 1358
Joined: Wed Nov 15, 2017 12:51 pm

Re: BASIC: Find out if an Interface 1 is attached.

Post by Pobulous »

I thought Interface I and II replaced the spectrum ROM with their own ROMs.

I remember when using interrupt vectors, a quick hack was to put a JR at location 65535, which would use the first byte of the ROM to pick the location to jump back to, and this value apparently would vary based on whether Interface I or II was connected.

So address 0 of ROM could be a place to check.
User avatar
spider
Dynamite Dan
Posts: 1099
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: BASIC: Find out if an Interface 1 is attached.

Post by spider »

I thought it was just paged in as and when ? I don't have (at this moment) time to check :(

I did think oddly enough about the Sweevo Editor (yes I know) as the RobCandy tips (tape) had some kind of microdrive only program on it for saving to that, I think there was some kind of check for IF1/Microdrive present maybe in that code. I don't have time to research this until tomorrow though. If you examine the instructions for said tape it may make sense but as I say I've not looked into it.
User avatar
Seven.FFF
Manic Miner
Posts: 744
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: BASIC: Find out if an Interface 1 is attached.

Post by Seven.FFF »

The extra sysvars are listed in the IF1 manual, but I can’t see anything obvious that would definitively indicate that BASIC has been extended for microdrives. The variables are, after all, intended to be variable. And there are many other ways that BASIC (or the hardware) can be extended.

The usual advice in these general situations is to test for features rather than versions or magic numbers. In this case, that would involve making a microdrive API call, either from BASIC or more likely from asm, and treating anything but a definitive success result as a failure. The trick here is to pick something that had no bad side effects, doesn’t depend on a formatted cart being inserted, is unlikely to give a false negative if there is an IF1 but a bad cart, and doesn’t completely crash in the standard unexpanded scenario. And, hopefully, doesn’t crash if some other unexpected interface or BASiC extension is installed.

This is all quite a tall order, and when not done properly can be more disruptive and counterproductive than a simple manual option for the user to select microdrive operation.

For example, the Multiface 1 menu has manual entries for microdrive, Opus, Wafa, Betadisk etc, rather than jumping through all the hoops to do 100% reliable detection.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
User avatar
1024MAK
Bugaboo
Posts: 3115
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: BASIC: Find out if an Interface 1 is attached.

Post by 1024MAK »

Don’t forget that the interface 1 also has a RS232 port and network ports.

The interface 1 is only paged in when certain circumstances occur, one is when the BASIC ROM error handler is called (so that the interface 1 can ‘trap’ all the new commands). Hence trying to peek it from BASIC is not very practical.

I think reading the I/O ports is most likely to be the simplest way, if the value can help identify if it (or something similar) is present (I say this, because there are other hardware devices that support some of the interface 1 features).

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
jpnz
Manic Miner
Posts: 319
Joined: Tue Nov 14, 2017 4:07 pm
Location: Hamilt[r]on - City Of The Future - NZ

Re: BASIC: Find out if an Interface 1 is attached.

Post by jpnz »

There are some more suggestions from J.G. Hartson

His machine code approach is pretty versatile, and uses hook code 0x31 to initialize the IF1

It also traps for the non existence of the IF1 and will trigger a clean return to BASIC in either instance

There are other interfaces (Disciple and Plus D) that mirror the IF1 hook codes

Pretending I hadn't read what Seven.FFF had said about magic numbers(!),
you can potentially determine what interface (and version) has responded to hook code 0x31 by inspecting register pairs once control has returned to your own code...

For example, with an IF1 Version 2

de will be set at 0x0700 - the UNPAGE location that leaves the shadow ROM
hl will be set at 0x1e98 - the HOOK-31 location

With an IF1 Version 1, de will still be 0x0700, but hl will be set at a different location of 0x19a8 for HOOK-31
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Re: BASIC: Find out if an Interface 1 is attached.

Post by cha05e90 »

Wow, it doesn't seem that easy to retrieve that information from your Speccy..:-)

I'm still trying to make a test with pure Sinclair BASIC - maybe something from all your suggestions can be used. If that's really impossible, I'll give this or that method using some assembler a try - but that will be the last resort.

Thanks for all your suggestions and tips!
48K/+/+/+/+128K/Vega/Next&80/81&88
User avatar
spider
Dynamite Dan
Posts: 1099
Joined: Wed May 01, 2019 10:59 am
Location: Derby, UK
Contact:

Re: BASIC: Find out if an Interface 1 is attached.

Post by spider »

It is surprising to realise there's not a very easy IF PEEK xyz <> abc , or something similar like IF PEEK xyz = abc etc

Logic would say there should be, I can see it moves things about a bit, trouble is the contents of what has moved is variable anyway in its nature and content.

I don't *think* the following will be useful but...

23653-23654 STACKEND:
This system variable contains the address of where the spare part of memory starts. From reading this we can gain an idea of how much memory we have left by subtracting it from RAMtop
Try: PRINT PEEK 23730 + 256*PEEK 23731 - PEEK 23653 - 256*PEEK 23654 , with both an IF one connected *and* without it connected.

EDIT... No the above usually on a reset returns 41532 regardless and CLEAR is going to make that jump about. Hmm. There might be something similar that can be used. Have you looked at all the system variables in the Speccy Manual ?

RAMTOP is at 23730-23731

I don't (think) FLAGS at 23611 is going to be any help.

That's about as much erm 'sane' thoughts as I can put into this.
User avatar
cha05e90
Dizzy
Posts: 66
Joined: Sun Nov 19, 2017 7:02 pm

Re: BASIC: Find out if an Interface 1 is attached.

Post by cha05e90 »

jpnz wrote: Sat Sep 21, 2019 6:03 am There are some more suggestions from J.G. Hartson
I tested Jonathan's rather short Sinclair BASIC variant - indeed a test in the system variable area:

Code: Select all

10 CLOSE#0:LET inf1=(PEEK 23734<>244)
20 IF inf1 THEN PRINT "ZX Interface 1 detected."
If I put that in the beginning of the BASIC program it seems rather reliable in detecting an attached ZX Interface 1. I tried it with some emulators and with my real ZX Spectrum 48K/128K and it works. So'll stick with that for the moment!
48K/+/+/+/+128K/Vega/Next&80/81&88
Post Reply