BASIC - detect the presence of a disk

Page 1/3
| 2 | 3

By skumlerud

Resident (58)

skumlerud's picture

05-01-2023, 21:31

I'm writing a small game in pure BASIC, and I want to save a hiscore file in the current directory when the game is loaded from disk. So how do I cleanly detect this under BASIC?

My idea was to use dskf(0), which should throw an error if the function call or disk isn't available. And this works fine with plain MSX BASIC, both on an emulator (tested BASIC 1.0 with and without Disk BASIC, and BASIC 2.0 with and without a floppy available) and on my SVI738 with BASIC 3.0. However, with my Carnivore 2 w. Nextor 2.10 installed it does not work correctly. Calling dskf(0) with the floppy as current drive and no disk inserted will reset the machine.

So am I misusing dskf, or is this a Nextor bug? Is there a better/more correct way to do this?

Login or register to post comments

By Manuel

Ascended (19678)

Manuel's picture

05-01-2023, 22:40

You can probably also try with 'files' Smile Or other disk basic commands.

By djh1697

Paragon (1736)

djh1697's picture

05-01-2023, 22:52

I *think* if you use a routine like
10 on error goto 1000
20 a$=DSKI$ (0,1)
30 print "disk in drive"
999 end
1000 print"no disk in drive"
If you specify the drive as 0, it will work on whichever is the current drive. a:=1, b:=2, etc
The error code for a disk i/o error is number 69

https://konamiman.github.io/MSX2-Technical-Handbook/md/Chapter2.html#12-instructions-of-msx-disk-basic

By gdx

Enlighted (6436)

gdx's picture

06-01-2023, 09:34

If for example you create the file "MYDISK" with BSAVE"MYDISK",0,0,S the following program will be able to detect if this is your disk that is inserted or not.

10 ON ERROR GOTO 1000
20 BLOAD"MYDISK",S
30 PRINT"This is the right disk. :)"
40 END
1000 IF ERL<>20 THEN PRINT"Error";ERR;"in line";ERL: END
1010 IF ERR=70 THEN PRINT"Insert a disk!"
1020 IF ERR=53 THEN PRINT"This disk is not the right one!"
1030 RESUME40

By PingPong

Enlighted (4155)

PingPong's picture

06-01-2023, 11:18

Any disk access throw a runtime error, in basic you have the ability to catch it with this line:

10 ON ERROR GOTO 10000

(Also called ON HORROR GOTO 10000 ;-) ) due to it unstructured/horrible mode of working

By gdx

Enlighted (6436)

gdx's picture

06-01-2023, 12:51

PingPong wrote:

(Also called ON HORROR GOTO 10000 ;-) ) due to it unstructured/horrible mode of working

Why?

By djh1697

Paragon (1736)

djh1697's picture

06-01-2023, 16:15

@gdk
A more elegant way, however, it is over 10 years since I programmed in MSX basic. Perhaps a line after the detection could be ON ERROR OFF

By mars2000you

Enlighted (6557)

mars2000you's picture

06-01-2023, 16:24

djh1697 wrote:

@gdk
A more elegant way, however, it is over 10 years since I programmed in MSX basic. Perhaps a line after the detection could be ON ERROR OFF

ON ERROR OFF does not exist in MSX-BASIC, but indeed a final line with ON ERROR GOTO 0 is missing.

By skumlerud

Resident (58)

skumlerud's picture

06-01-2023, 19:53

Thanks for all the suggestions Smile I went for the DSKI$ approach, this works fine both on my MSX1 with tape only and my MSX2 with floppy and IDE.

By gdx

Enlighted (6436)

gdx's picture

07-01-2023, 08:36

I added the ON ERROR GOTO 0 in the wiki example here to stop error handling for others errors.

By mars2000you

Enlighted (6557)

mars2000you's picture

07-01-2023, 09:42

gdx wrote:

I added the ON ERROR GOTO 0 in the wiki example here to stop error handling for others errors.

I can't see it in this example.

And in the other example, i! will not work when it's not the last line of the program.

Page 1/3
| 2 | 3