Saving Machine Language to tape

Basic and Machine Language

Moderator: Moderators

Post Reply
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Saving Machine Language to tape

Post by Z0rb »

Maybe I cannot see the obvious here but, if I poke a bunch of values into memory, say a small machine code program and I wish to save those locations in memory to tape, how can I go about doing that. I do not see any literature regarding this.

Please help
Thanks
Last edited by Z0rb on Thu May 23, 2013 12:36 pm, edited 1 time in total.
User avatar
Stormcrow
Vic 20 Enthusiast
Posts: 177
Joined: Mon Dec 24, 2012 9:46 pm
Website: http://trimboli.name
Location: Ronkonkoma, NY

Post by Stormcrow »

The usual technique is, I believe, to write a small program that does the poking, then loads the next program. Save both programs to tape in order.

For example:

10 FOR I=0 TO 9
20 READ X: POKE {address of start of ML code}+I,X
30 NEXT
40 LOAD "{main program name}"
50 DATA {10 data values}

When encountered in a BASIC program, the LOAD command clears the current program, clears the current variables, and runs the new program. Values POKEd into memory remain.

Don't use the cassette buffer (828–1019) for POKEing if you're using the cassette drive!
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Hmmm

Post by Z0rb »

I understand what your saying but that seems almost silly to go through that whole extra step. Also what program would be loaded after the poking is done? A SYS command?

If I load a game I believe it loads everything up and when listed it will merely list one SYS command. That tells me that all the ML code was loaded into memory. How is that achieved?

If I manually poked a bunch of stuff into say location $1388 and just have a BASIC program of
10 SYS 5000
and I save it, it will only save the line 10 program and not all the data I manually poked into place. Is there a was to save memory locations on cassette?
User avatar
buzbard
Vic 20 Devotee
Posts: 213
Joined: Sun Jul 03, 2005 9:10 am

Post by buzbard »

To save a block of memory to tape, type these lines:

Code: Select all

s=828:e=900:sh%=s/256:sl=s-sh%*256:eh%=e/256:el=e-eh%*256
sys57809("filename"),1,1:poke193,sl:poke194,sh%
poke780,193:poke781,el:poke782,eh%:sys65496
Change s and e to where your code is located, and change the "filename".
Ray..
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

thanks

Post by Z0rb »

buzbard wrote:To save a block of memory to tape, type these lines:

Code: Select all

s=828:e=900:sh%=s/256:sl=s-sh%*256:eh%=e/256:el=e-eh%*256
sys57809("filename"),1,1:poke193,sl:poke194,sh%
poke780,193:poke781,el:poke782,eh%:sys65496
Change s and e to where your code is located, and change the "filename".
Ok that chunk of code was more what I was looking for thank you. Once more questions I can see for the most part what everything is doing, however with the omission of the context is the sys 57809 command just poking the SAVE keyword in the kernel?
User avatar
buzbard
Vic 20 Devotee
Posts: 213
Joined: Sun Jul 03, 2005 9:10 am

Post by buzbard »

No, it's setting up the filename, device number and secondary address for the save. The sys to 65496 is the actual kernal "SAVE" routine.
Ray..
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Post by Z0rb »

buzbard wrote:No, it's setting up the filename, device number and secondary address for the save. The sys to 65496 is the actual kernal "SAVE" routine.
Ok I was a bit curious, I was able to find ever poke address BUT 57809. Is there a complete list of the kernel somewhere?
User avatar
buzbard
Vic 20 Devotee
Posts: 213
Joined: Sun Jul 03, 2005 9:10 am

Post by buzbard »

Yeah, I use "Compute!'s Mapping the VIC", you can download the pdf from Bombjack and many others.
Ray..
User avatar
Mike
Herr VC
Posts: 4831
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

There's a nice sticky thread here in the Programming Section about ROM calls and other tricks.

Within that thread, SYS57809 to set up file name and device address to load and save blocks of memory was pointed out in this post dating from 2006.

From assembly the KERNAL routines SETLFS ($FFBA) and SETNAM ($FFBD) are used instead to set up file name and device addresses - SYS57809 calls both routines from BASIC, but wants to scan the text behind the SYS number and that text isn't necessary available from a running (pure) ML program -, then the KERNAL routines LOAD ($FFD5) or SAVE ($FFD8) are called.

You find a comprehensive list of the standard KERNAL calls here.

From BASIC, the addresses 780, 781, 782 and 783 are used to set up the CPU registers A, X, Y and status respectively for a SYS. When the machine code ends with RTS, the values in 780..783 are updated.
Stormcrow wrote:When encountered in a BASIC program, the LOAD command clears the current program, clears the current variables, [...]
Sorry, but no, the old variables are kept, even worse, the pointer to the start of variables (a.k.a. "end of program") *IS* *NOT* *UPDATED* - and if the newly loaded program is longer than the program before, variable accesses will clobber the new program.
Z0rbVIC wrote:If I manually poked a bunch of stuff into say location $1388 and just have a BASIC program of

Code: Select all

10 SYS 5000

and I save it, it will only save the line 10 program and not all the data I manually poked into place. Is there a was to save memory locations on cassette?
With the block save routines already mentioned, you can additionally save an area behind the visible BASIC listing, which then contains the ML part and other data. This short BASIC listing commonly is referred to as '(BASIC) stub'. It is not recommended to edit that BASIC line, as this will shift around the appended data, so the SYS points to the wrong address - and 65xx code normally isn't relocatable anyway.

When you use an assembler like DASM, ACME or ca65, it's quite easy to prepend such a BASIC stub, see here: 'How to compile assembly with a basic start line?'

Greetings,

Michael
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Post by Z0rb »

Mike wrote:There's a nice sticky thread here in the Programming Section about ROM calls and other tricks.
Wow, I definitely have some reading to do.

thanks
User avatar
freshlamb
Vic 20 Dabbler
Posts: 76
Joined: Sun Apr 04, 2004 5:38 pm
Website: http://www.rufnoiz.com
Location: Prince Albert SK Can

Post by freshlamb »

This works as well (and is what I usually do) although it is a bit of typing. From the direct mode I change all the pointers for BASIC to the bottom and top of my ML. For example if my program was from 6566 to7168 I would POKE43,0:POKE44,26:POKE55,0:POKE56,28 then just SAVE it regularly. This only a bit more difficult if your program ends at 7679, because there will be no RAM for the save filename. There is quick way around this as well.

You will have to reset you VIC after saving because your BASIC programs probably won't like the new locations and do funny things. Just a simple SYS64802 will do.
User avatar
Mike
Herr VC
Posts: 4831
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

freshlamb wrote:For example if my program was from 6566 to 7168 I would POKE43,0:POKE44,26:POKE55,0:POKE56,28 [...]
The last two should read POKE45,0:POKE46,28.

POKEs to the addresses 55 and 56 set the top of available memory for BASIC, which in themselves come in quite handy to protect user defined graphics from being overwritten by the string heap. Such rearrangement of memory should be placed right at the start of the program, so a CLR command after these POKEs can also adjust some other pointers (mainly 51/52).

Lowering the roof is the preferred method for protecting memory areas with an unexpanded or +3K RAM extended VIC-20. With bigger RAM expansions, it is more sensible to increase the BASIC start address in 43/44 to set aside memory for data. Especially graphics data only can go to $1000 .. $1FFF ($0000 .. $03FF would also work, but big hunks of data placed there can easily interfer with the workings of BASIC interpreter and KERNAL). Increasing the start address can't easily be incorporated into a single program though, as the program then would need to be shifted around in RAM. Usually, a boot loader is used instead.
User avatar
freshlamb
Vic 20 Dabbler
Posts: 76
Joined: Sun Apr 04, 2004 5:38 pm
Website: http://www.rufnoiz.com
Location: Prince Albert SK Can

Post by freshlamb »

OOoops yes!!

The last 2 should be the pointers to the top of BASIC (45,46). This is also the easiest way to have your BASIC line 1 SYS4118 with ML. Just adjust the top of BASIC to the end of your ML and save.
Post Reply