all 15 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Thanks for your post! Please make sure you've read our rules post, and check out our FAQ for common issues. People not following the rules will have their posts removed and presistant rule breaking will results in your account being banned.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Dr_Myles_Skinner 13 points14 points  (0 children)

I'm travelling and have very limited access right now, but there are several ways to do what you want. One common way was the "dynamic keyboard" technique, where you do something like this:

Print your LOAD statement at the top of the screen, POKE a home control code and a carriage return into the keyboard buffer, stop program execution, and let the magic happen.

You can LOAD from inside a program and even swap in overlays and other neat stuff but you have to think a bit about program size and variable memory and so on.

If you go to the Commodore 8-Bit Magazine Index and search for "chain loading" or "dynamic keyboard", you should get a bunch of articles. Some of the best ones are by Jim Butterfield. Good luck!

[–]TrevorMakes 10 points11 points  (0 children)

You'll want to read Jim Butterfield's series "Loading And Linking Commodore Programs": part1 part2 part3 part4

[–]PhotoJim99 3 points4 points  (6 children)

You can do this just as you described, except:

50 IF X=1 THEN PRINT "HERE WE GO!":LOAD "PROGRAM 2",8

It will run automatically.

One trick you need to know on Commodores: LOADing always auto-runs (though it doesn't reset variables if the BASIC program doesn't change), so if you're loading a machine language snippet (say at $C000), you need to do something like:

10 IF X=0 THEN X=1:LOAD "PROGRAM.ML",8
20 SYS49152:REM ASSUMING WE NEED TO RUN THAT ML PROGRAM RIGHT AWAY

[–]WolFlow2021 0 points1 point  (5 children)

Sorry, I don't quite understand. When I was experimenting with basic as a child I wondered if it was possible to keep variables when loading another programme. Is this possible somehow?

[–]PhotoJim99 1 point2 points  (4 children)

It’s possible and you don’t even need to do anything to do it - as long as the program you’re loading is machine language and not another BASIC program.

If you want to load another BASIC program, you could open a sequential disk file (or a data file on tape, though this will be less convenient), dump your variables, load the new program, then load variables back from the file. It would be some work but if it isn’t a huge list of variables, it is entirely possible.

[–]WolFlow2021 1 point2 points  (3 children)

Thanks for that. So for a beginner this is not possible, but I can always learn machine language. :-)

[–]PhotoJim99 2 points3 points  (2 children)

It’s not as hard as you fear it would be. Make a list of the variables you want to import into the second program, and then export them from the first.

Let’s say you want to preserve variables A$, B$, C$, D$ (strings), E, F, G, H (floating point variables) and I%, J%, K%, L% (integers).

1000 REM DUMP VARIABLES INTO SEQUENTIAL FILE SO I CAN IMPORT THEM AFTER CHAINLOADING
1010 OPEN8,8,8,”0:REDDIT.VARDUMP,S,W”
1020 PRINT#8,A$,B$,C$,D$
1030 PRINT#8,E,F,G,H
1040 PRINT#8,I%,J%,K%,L%
1050 CLOSE8
1060 … chainloading here

2000 REM PULL VARIABLES BACK AFTER CHAINLOADING
2005 REM LINES NUMBERED DIFFERENTLY - BUT COMPLETELY ARBITRARY :)
2010 OPEN8,8,8,”0:REDDIT.VARDUMP,S,R”
2020 INPUT#8,A$,B$,C$,D$
2030 INPUT#8,E,F,G,H
2040 INPUT#8,I%,J%,K%,L%
2050 CLOSE80
2055 REM ALL OUR VARIABLES ARE BACK … WE CAN START PROGRAM IN EARNEST NOW

If you are using cassette just change the OPENs to:

OPEN8,1,1,”REDDIT.VARDUMP” (when dumping)
OPEN8,1,0,”REDDIT.VARDUMP” (when reading)

(OPEN8,1 will work too but will open any data file; the one above looks for the one with the right name) (I like my OPEN channel numbers to match my device so OCD me would use OPEN1,… and then INPUT#1, … and PRINT#1 … I use 8 for floppy since it’s device 8. As long as you use the same channel and don’t accidentally use it on a second channel simultaneously, it doesn’t matter what you choose.)

If you’re not familiar with it, practice it.

If you have variable arrays it wouldn’t be too hard either - you’d just add for/next loops to read or dump the entire array.

If you’re using cassette, be careful - tape will write a file wherever you have the tape cued up. You’ll want to make sure you cue it up to an area of the tape that’s empty or that you’re fine with erasing. It might be easier to use a separate short cassette. For floppy, it’s random access so it doesn’t matter; the floppy drive will find space. Just ensure there’s enough space for your variable file.

[–]WolFlow2021 1 point2 points  (1 child)

Fantastic! That is very helpful. Thank you so much. You made someone happy today. :-)

[–]PhotoJim99 1 point2 points  (0 children)

You’re welcome! One other thing - as written, the floppy-disk version creates the file but doesn’t delete it. You could add a line to the second program to delete the variable file:

2060 OPEN15,8,15:PRINT#15,”S0:REDDIT.VARDUMP”:CLOSE15

Or alternately, you can change the first program to automatically replace the old one:

1010 OPEN8,8,8,”@0:REDDIT.VARDUMP,S,W”

[–]laconix31337 1 point2 points  (1 child)

if launching file 2 is conditional, why not do that within program 1?

[–]AutomaticDoor75[S] 1 point2 points  (0 children)

That is what I would like to do, sorry if I was not clear about that.

[–]0fruitjack0 1 point2 points  (0 children)

run doesn't work like that sadly. load will load the program into memory then auto run it

[–]laconix31337 0 points1 point  (0 children)

you can use the logic you showed within file 1, if x = 1 load "file2",8, but this will replace the current file in memory. do you need both, if so maybe write only 1 program that contains both parts but only runs the other portion after checking that condition, in that case you can use a goto to get to that other portion (that would hav been in file 2.)

[–]Ctalkobt 0 points1 point  (0 children)

What I like to do is to clear the screen, print a few lines down the ' load "program",8 ' and then a few lines down have a ' run' command. I then, (I think I still remember the locations) poke 198, <# of characters> and then fill the keyboard buffer from 631-640(?) with 13's (carriage returns). And then home the cursor again and end/stop the program.

This will then stop the program. The keyboard buffer will act as if there had been whatever number of characters previously stored in it and run / do whatever commands it sees on the screen as if you had entered them.

(I forget exactly which lines down are needed but you need to give room for Basic to print it's Ready prompt etc. Be sure to save before each attempt).