Writing data files

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
loftcat
Posts: 19
Joined: Fri Mar 26, 2021 12:52 am
Location: Warwick, UK
Contact:

Writing data files

Post by loftcat »

Is there a way to write data files on the ZX81, similar to using OPENOUT / PRINT# in BBC BASIC?

http://www.riscos.com/support/developer ... files.html

I'm writing a ZX81 Basic game where I would like the player to be able to save their position. Is the standard Zeddy way to save the program and variables together, much in the same way described to me in a recent thread viewtopic.php?f=5&t=4186
User avatar
mrtinb
Posts: 1906
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Writing data files

Post by mrtinb »

  • ZX81 does not support data files. But if you SAVE within a program, the state of the variables and program position is saved as well. When the program is loaded, the program continues, where it left off.
  • Third party hardware might support saving any memory location to a file. Like ZXpand, ZXblast, ZX1541 or older disk systems. However this hardware is difficult to find.
  • A way to make a data file, is to allocate a space above RAMTOP for your data. Then make a small program maybe called SAVEDATA.P. When this program is loaded from within your program, the data above RAMTOP is saved. This program will copy the content from above RAMTOP into it's variable section. The program can ask for a name for the file, and save the data. The program copies the data back above RAMTOP, and ends with loading the original program. The original program can test whether there is data loaded above RAMTOP. Inspiration for this can be found on page 26-31 in Hint & Tips for the ZX81.
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
loftcat
Posts: 19
Joined: Fri Mar 26, 2021 12:52 am
Location: Warwick, UK
Contact:

Re: Writing data files

Post by loftcat »

Thanks for the info and ideas, much appreciated. I'm making quite a large game that will take up 16k. Would it be a fairly acceptable solution to save the program with variables in their current state. Then when the player wants to continue where they left off, load the entire game with variables in their current state, and use GOTO 100 (for example) to continue where they left off?
User avatar
mrtinb
Posts: 1906
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Writing data files

Post by mrtinb »

You don’t need GOTO 100, to restart, as the program will continue where it left off.
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
loftcat
Posts: 19
Joined: Fri Mar 26, 2021 12:52 am
Location: Warwick, UK
Contact:

Re: Writing data files

Post by loftcat »

Ah yes of course, I will take the player back to the main menu after saving the game.
User avatar
siggi
Posts: 988
Joined: Thu May 08, 2008 9:30 am
Location: Wetterau, Germany
Contact:

Re: Writing data files

Post by siggi »

One hint:
if you use SAVE within a running BASIC program (to continue the program there after LOAD), the SAVE command may be used only in the main program, not located a sub-routine!!!
The reason is, that the RETURN address of the sub-routine is stored on the BASIC stack, which is NOT saved by the SAVE command, thus RETURN will lead to an error.

Siggi
My ZX81 web-server: online since 2007, running since dec. 2020 using ZeddyNet hardware
http://zx81.ddns.net/ZxTeaM
loftcat
Posts: 19
Joined: Fri Mar 26, 2021 12:52 am
Location: Warwick, UK
Contact:

Re: Writing data files

Post by loftcat »

siggi wrote: Mon May 31, 2021 9:43 am One hint:
if you use SAVE within a running BASIC program (to continue the program there after LOAD), the SAVE command may be used only in the main program, not located a sub-routine!!!
The reason is, that the RETURN address of the sub-routine is stored on the BASIC stack, which is NOT saved by the SAVE command, thus RETURN will lead to an error.

Siggi
Oh thanks for letting me know, I wasn't aware and might have run into trouble with that!
Post Reply