Assembly language - Loading / Saving save game files

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
fuzzel
Posts: 660
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Assembly language - Loading / Saving save game files

Post by fuzzel » Sun Aug 23, 2020 7:27 pm

I'm trying to save some space in my text adventure games (see LAND and MIST topics) and at the moment I've reserved an area of memory for the save game file (&300 bytes). The game data is copied to this area and the file saved. The reverse is the case for restoring (loading) your position. I wondered if it was possible to save using this space by using BPUT and BGET instead? Unfortunately I don't know the assembly language equivalents of these basic commands.
As an example could someone give me the assembly language version of the following please, one byte (L%) to be sent to disc (I can then tailor the routine to suit multiple bytes):

A%=OPENOUT("LANDSAV")
BPUT#A%,L%
CLOSE#A%

For the example, the byte I'd like to transfer is at memory location &C00.

Also what would the equivalent be for the reverse (loading):

A%=OPENIN("LANDSAV")
BGET#A%,L%
CLOSE#A%

User avatar
sweh
Posts: 2262
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: Assembly language - Loading / Saving save game files

Post by sweh » Sun Aug 23, 2020 7:31 pm

OSBPUT (does BPUT#) - http://beebwiki.mdfs.net/OSBPUT
OSBGET (does BGET#) - http://beebwiki.mdfs.net/OSBGET

Basically LDY to set the channel, then OSBPUT will write the accumulator to the file, and OSBGET will read the file and put the results in the accumulator.

You would call OSFIND (http://beebwiki.mdfs.net/OSFIND) to open/close the file.
Rgds
Stephen

fuzzel
Posts: 660
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Assembly language - Loading / Saving save game files

Post by fuzzel » Sun Aug 23, 2020 9:07 pm

Many thanks for this. beebwiki looks like an excellent resource btw.

fuzzel
Posts: 660
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Assembly language - Loading / Saving save game files

Post by fuzzel » Sat Aug 29, 2020 11:29 am

I'm in the process of implementing the new BPUT / BGET save and load routines into my game but for some inexplicable reason it crashes.
The game appears to successfully save the file $.LANDSAV as intended and I then get the "What now?" prompt but when I try to LOOK or go in a direction the game crashes. Can anyone see if I'm doing anything obvious wrong? The code I've used is as follows for the save routine with one byte saved for testing purposes:

start1 is a memory location storing the string "$.LANDSAV"

.save
LDA#&80:LDX#start1 MOD 256:LDY#start1 DIV256:JSR&FFCE (Y=OPENOUT"$.LANDSAV")
LDA#65:JSR&FFD4 (BPUT "A")
LDA#0:JSR&FFCE (CLOSE#Y)
RTS

Note that I'm using sideways ram banks for the location text (stored in &F4) and &75 for the location number. These both appear to be retained.

EDIT: My guess as to what's going wrong is that OPENIN/OPENOUT/CLOSE#/BPUT/BGET utilise some memory locations (zero page?) and this is interfering with my program.

User avatar
sweh
Posts: 2262
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: Assembly language - Loading / Saving save game files

Post by sweh » Sat Aug 29, 2020 5:05 pm

fuzzel wrote:
Sat Aug 29, 2020 11:29 am
Note that I'm using sideways ram banks for the location text (stored in &F4) and &75 for the location number. These both appear to be retained.
If "start1" is in SWRam then that'll likely be the problem, 'cos when DFS is active _that_ ROM will be paged in and your data paged out.
Rgds
Stephen

fuzzel
Posts: 660
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Assembly language - Loading / Saving save game files

Post by fuzzel » Sat Aug 29, 2020 5:32 pm

Start1 is in normal ram.

Kweepa
Posts: 25
Joined: Mon Dec 16, 2013 11:45 pm
Contact:

Re: Assembly language - Loading / Saving save game files

Post by Kweepa » Sat Aug 29, 2020 5:43 pm

If you are using disk routines, they will likely touch some of &90-&ff, &300-&3ff, &800-&8ff or &d00-&dff.

fuzzel
Posts: 660
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Assembly language - Loading / Saving save game files

Post by fuzzel » Sat Aug 29, 2020 6:23 pm

I'd assumed, sweh, that you were on to something with the disc access changing the current sideways ram bank but I've just checked and at the start of my save routine I saved the current SWR bank to &CF5 and at the end of the save routine restored it to &F4 and &FE30. No joy though.
So I'm still stumped. Kweepa, I am using some memory locations at &90 onwards so will check if any of these have changed next.

EDIT: My old save routine also (obviously) accessed the DFS so it must be something that OSFIND (FFCE) & OSBPUT (FFD4) affects that OSCLI (FFF7) doesn't.

User avatar
sweh
Posts: 2262
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: Assembly language - Loading / Saving save game files

Post by sweh » Sat Aug 29, 2020 6:43 pm

Are you using memory from &1100->&1300? When you open a file on DFS then it'll use page &11 to store filehandle data, and then the first open file uses page &12 to hold data itself.
Rgds
Stephen

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

Re: Assembly language - Loading / Saving save game files

Post by jgharston » Sat Aug 29, 2020 6:45 pm

fuzzel wrote:
Sat Aug 29, 2020 11:29 am
start1 is a memory location storing the string "$.LANDSAV"
It must be a CR-terminated string, otherwise the call doesn't know where the string ends.

.filenamer
EQUS "LANDSAV":EQUB 13

Plus, don't kick the user in the balls and force the file to be in a directory they may not want it to be. If you don't give the user the option to change the filename, it ***MUST*** be in the current directory, not somewhere else.

Why not just use OSFILE?

\ Load game state
.stateload
LDX #4
.stateload_lp
LDA fileload,X:STA fileblock+2,X
DEX:BPL stateload_lp
LDX #fileblock AND 255
LDY #fileblock DIV 256
LDA #255:JMP OSFILE
:
\ Save game state
.statesave
LDX #15
.statesave_lp
LDA filesave,X:STA fileblock+2,X
DEX:BPL statesave_lp
LDX #fileblock AND 255
LDY #fileblock DIV 256
LDA #0:JMP OSFILE
:
.filesave
EQUD -1
EQUD -1
EQUD gamestate
EQUD gamestate_end
.fileload
EQUD gamestate
EQUB 0
:
.filename
EQUS "LANDSAV":EQUB 13
.fileblock
EQUW filename
EQUD 0:EQUD 0:EQUD 0:EQUD 0
:
.gamestate
blah blah blah
.gamestate_end

(untested)

Code: Select all

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

fuzzel
Posts: 660
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Assembly language - Loading / Saving save game files

Post by fuzzel » Sat Aug 29, 2020 6:57 pm

My program starts at &1100 so that sounds like it could be the problem. I’ll MZAP 1100 to see if the code has been corrupted. I’ll also investigate using OSFILE.

Post Reply

Return to “programming”