Classic Computer Magazine Archive COMPUTE! ISSUE 78 / NOVEMBER 1986 / PAGE 72


Dennis J. Jarvis and Michael L Zinicola

This tiny, but powerful program for the Commodore 128 and 64 allows you to resurrect a BASIC program after an inadvertent NEW command or system crash. Although it's written in machine language, you can use "Unnew" even if you're not a machine language programmer.

It's happened to virtually every BASIC programmer. You type NEW and press RETURN without thinking, only to realize that you forgot to save the program in memory. If you don't have a backup copy, that small slip can mean hours of wasted effort. However, NEW doesn't actually erase the program from memory—it only resets the computer's BASIC pointers so that no program appears to be present. Nothing appears when you LIST the program, but all of the program lines still remain in memory. To resurrect the program, you need only reset the computer's BASIC pointers so that it recognizes the program's existence again.

"Unnew" automatically restores any BASIC program after an accidental NEW. It can also recover many programs which appear to be lost as the result of a system crash. We've included two versions, one for the Commodore 64 and one for the 128. If you have a 64, type in and save Program 1. If you have a 128, type and save both programs.

Both programs are BASIC loaders which write a machine language program to disk. To use either program, load it, type RUN, and press RETURN. Program 1 creates a disk file named UNNEW 64.OBJ. Program 2 creates a disk file named UNNEW 128.OBJ. If the loader program detects a typing error, it deletes the file from disk and displays an error message instructing you to check for errors. If no errors occur, the program asks whether you wish to create another copy of the machine language program. In this way, you can create as many copies of Unnew as you need. After you've created the machine language files on disk, you don't need the BASIC programs again (unless you need to make additional copies of the machine language files).

Resurrecting BASIC

Apart from an accidental NEW, there are many different ways that a program can seem to disappear. For instance, a programming error may cause the computer to lock up, or fail to respond to the keyboard. When this happens, you can often regain control and return the computer to BASIC ready mode by pressing the RUN/STOP and RESTORE keys together. If you can list the program after pressing RUN/ STOP-RESTORE, you may not need to use Unnew at all. Save a backup copy of the program before doing anything else.

The instructions for using Programs 1 and 2 are different. We'll begin with the 64 version, Program 1. The 64 version works on the 64 and on the 128 in 64 mode. To use this program, you must be able to return the computer to ready mode. If the 64 seems to be locked up, try pressing RUN/STOP-RESTORE. If you're using a 128 in 64 mode and RUN/STOP-RESTORE does not work, press the reset button while holding down the Commodore key. Once the 64 has returned to ready mode, type this command and press RETURN:

LOAD "UNNEW 64 .OBJ",8,1

When the program has finished loading, type this command and press RETURN:

SYS 828

After the ready prompt appears, you should be able to list and save the program as usual.

The 128 has a reset button that can be very useful in recovering a program. If the computer seems to lock up and RUN/STOP-RESTORE does not return you to ready mode, hold down RUN/STOP and press the reset button located on the right side of the computer. This operation resets the 128 and places you in the built-in machine language monitor. To exit the monitor and return to BASIC, type X and press RETURN. Try to list the program; if it can be listed, save a backup copy immediately.

Unnew is needed only for cases in which nothing appears when you list the program. After you return the computer to ready mode, place the disk containing UNNEW 128.OBJ in the drive. Then type this command and press RETURN:


The 128 automatically loads UNNEW 128.OBJ from disk and runs it. When the process is complete, the computer prints READY as usual. List the program to confirm that it has been recovered; then save it as usual. This procedure should work even in cases when the 128 has relocated the program during graphics operations.

How Unnew Works

You can use Unnew to recover programs without knowing how it works. However, an explanation of the technique will also illustrate something about how the computer stores BASIC programs. The usual starting address for a BASIC program is location 7168 ($1COO) on the 128 or location 2048 ($0800) on the 64. The first location in BASIC program space must always contain a zero to mark the beginning of BASIC text. Commodore BASIC programs are stored in memory line by line, in ascending order. At the beginning of each line is a two-byte link address, in low-byte/high-byte format, which tells the computer where the next program line begins in memory. Among other things, this linking scheme allows the computer to quickly scan through the program when it performs operations such as GOTO and GOSUB, which refer to a specific program line.

The next portion of the program line contains the line number. The line number is stored in two bytes, also in low-byte/high-byte format. Following the line number are the tokenized BASIC keywords and other characters that make up the rest of the program line. The end of each line is marked with a byte containing zero. This sequence of links, line numbers, program text, and zero markers continues until the last program line is reached. The last line contains another special marker: Instead of a link address, you'll find two zero bytes which indicate the end of BASIC program text.

When you perform NEW, the computer stores two zero bytes immediately after the zero that marks the start of BASIC. When it finds two zeros in these locations instead of a non-zero link address, the computer concludes that there is no program in memory (the end-of-program marker coincides with the start-of-program marker, so the program text consists of nothing). In addition, it resets separate pointers that mark the end of program text and the beginning of variables.

On the Commodore 64, BASIC variables are stored in the same general area as the program, beginning immediately above the end of the program. Thus, a single pointer is used to mark where program text ends and variable storage begins. On the 128 (in 128 mode), variables are stored in a different memory bank. However, the computer still maintains a pointer to the end of program text so that it knows where to stop when saving the program.

To restore a program after NEW, you must put a nonzero link address at the beginning of the program and reset the end-of-program pointer to point to the actual end of the program. In a nutshell, that's how Unnew works. More specifically, it begins by turning on the ROMs and the character generator (just in case a program crash turned them off). Then it decrements the start-of-BASIC pointer by one and stores a zero in that location. This insures that the computer will find a zero in the first byte of BASIC program space. Then it adds one to the pointer and stores a nonzero value in the two bytes that form the first line's link address. Once this has been done, the program calls an internal ROM routine that corrects all the program's line link addresses. Finally, it resets the end-of-program pointer to point to the true end of program text and ends by printing the usual READY prompt.

Worst-Case Scenario

Of course, there are some program crashes which Unnew can't fix. The 128 can always recover from a crash via the reset button. But if the crash POKEd garbage values into the program area, the program text may be irreparably garbled. That's why it's important to list the program after recovering it with Unnew, to make sure that the entire program is intact. The 64 does not have a reset button, and may be unable to recover from a hard crash—when the system locks up completely and cannot be recovered with RUN/STOP-RESTORE. To see what is meant by a hard crash, type this line and press RETURN (before you do so, make sure that you save any program that's in memory):

POKE 1,52

This POKE turns off the 64's BASIC and Kernal ROMs, making it impossible to use the computer at all. Unnew cannot be used, since you must be able to enter direct mode commands to load and activate the program. Unless you've installed a hardware reset switch, you have no recourse but to turn the power off and on. Once the power is turned off, the contents of memory are lost completely.

For instructions on entering these listings, please refer to "COMPUTE!'s Guide to Typing In Programs" in this issue of COMPUTE!.