The mystery comment investigated by Beej’s Bit Bucket reminded me of a similar trick in an Apple ][ telephone directory program.
The base Apple ][ was sold without a floppy drive. The only supported external storage device in the base unit was a cassette I/O port. Yet there was a database program for digitizing your phone list.¹ How did it do database stuff when the only I/O was a cassette drive? It could not programmatically start or stop the tape, or programmatically rewind or advance it.
If you took a listing of the program (which was written in BASIC), it began with a comment block saying not to edit any lines before line 1000. And then you saw your own telephone directory stored in DATA statements.² Followed by a bunch of dummy DATA statements. And then the program began.
Wait, how did your telephone directory show up as DATA statements? The BASIC DATA statement was a place for the program developer to store tables of precalculated read-only data, to be consumed by the READ statement at runtime. They shipped the program to you without your telephone directory in it, and yet somehow your telephone data ended up in the program.
That’s because the program used its own DATA statements as the memory for a database.
When it wanted to read from the database, it read the data via the usual READ command. And when it wanted to write to the database, it used self-modifying code to POKE the information back into its own DATA statements.
When you told the program that you wanted to save your changes, it told you to turn on the cassette player, and then it invoked the SAVE command to save the program to cassette. And since the program modified itself to contain your telephone data, saving the program also saved your data that was embedded inside it.
When you wanted to use your telephone directory, you loaded the program from cassette and ran it.
Now, the Apple ][ did have routines in the monitor ROM to save a block of memory to cassette, so it could have saved the database to tape in a more traditional manner. But that would have meant that when you wanted to use your telephone directory, you first had to load the program from cassette and run it, and then you had to set up the cassette a second time to load the database. Saving the entire program meant that the two load operations (loading the program and loading the data) were combined into a single cassette pass.
¹ This was back in the early days when the sales pitch for personal computers included “Organize your recipes!” and “Digitize your telephone directory!” Things that nobody ever actually did. (I don’t know how many people used it for personal finance, but it was definitely very popular for business finance.)
² At least, I think it was DATA statements. Maybe it was comments. But the same principle applies.
That approach reminds me of how Redis’s (optional) persistence support works. Do everything in memory and write everything out when checkpointing.
My first concern when hearing about the program design however is that the address book has a limited capacity. I wonder if the program stopped you from adding too many contacts or if it let you clobber the program.
The base Apple II had 4 KB of RAM, of which 2 KB were used by system and video, leaving just 2 KB for program *and* data. IIRC, the program required at least 16 KB, but even with that, memory would be tight. When you wrote software for 8-bit machines, you usually ended up counting bytes - in many cases literally.
I had the luck of starting with an Apple IIc, which had a "huge" 128 KB of RAM. But you would end doing work in the main 64 KB bank, which was shared with the OS, command interpreter and VRAM,...
Back in those days, I had a TRS-80. IIRC, the tape format, even for saved programs, had blocks with address, length and data…probably also a header with how many block were available to read or maybe a termination marker.
Anyway, I remember a chess playing system that was so strapped for memory that the final data block addressed the screen directly, giving the user a one-time look at the help information before starting.
There's a lot of things one can fondly look back to "retro" computing for -- a simple instruction set, hardware that a single person can fully understand, blinkenlights -- but limited and unreliable storage is not one of them.
This technique reminds me a little bit of how some old games did save files. One in particular I remember was Starflight, which overwrote its own executable in-place to include the user's progress in the game. This turned out to be a problem if the save was corrupted (happened to me at least once) and you didn't create a backup...
I loved starlight. If only there were still games like that. I had bought it Sydney and caught the train home for two hours. I started playing and three days later I woke face down on keyboard and the keyboard beeping.
I played it again a year ago.
I hope it told you to flip the cassette or otherwise choose an alternate region of the tape before saving. Because writing a program incorporating your data over the same place you loaded from is how you lose both your data *and* the program.
In case the saving fails, you would indeed lose both. If it worked, you do not lose anything, you would just get a new recording of the program/data.
Of course, it would be wise not to record anything on the original tape the program was shipped on. If shipped properly it would have the copy protection tabs removed anyway.
I elided a few steps: If you get into the habit of writing your next revision over your last, *eventually* you lose.
And, in my childhood, we never *had* original tapes. Borrow a cassette from a friend, make a copy, return. Next time, lend them your cassette, get it back. (Glory to Bill Gilbert.)
Stopping the tape after loading completes places the start of the next write immediately after the end of the file that was read. Automatic version control. Rewinding the tape just enough to load the latest revision could be tricky but the tape counter can help.
Once the tape nears the end, it is time to either flip it over or use a new tape depending on how many revisions one needs to keep.