Classic Computer Magazine Archive COMPUTE! ISSUE 29 / OCTOBER 1982 / PAGE 170

PET Self-Starting Programs

Richard Mansfield
Senior Editor

For PETICBM, Upgrade or 4.0 BASICs, with disk, this program will make the changes necessary so that any program will start itself RUNning. It need not be the first program on the disk – the program itself, when LOADed, will take control of the computer.

There are some programs which are used so often that it is convenient to put them first on a disk if you use BASIC 4.0. Located on the disk as the first program, it will then automatically LOAD and RUN if you hit the RUN key. (Upgrade BASIC'S RUN key tries to load from the tape drive.) "Boot-fixer," however, will make any program self-starting.

If you have a disk of frequently used utilities, it might be worthwhile making every one of them self-starting. With 4.0 BASIC, you could have any one of them up and running with a simple dL"name. Also, people who have never used a computer would find this method of RUNning simpler to learn. All they would need to do is turn on the machine, insert a disk, and type in the name of the program they wanted: then the computer takes over. Built-in disaster prevention, such as a program with a disabled STOP key, should eliminate many of the start-up crashes experienced by novices.

Making The Mock Stack

Before Bootfixer can change another, target program, you must first prepare a special, slightly longer version of the target. It will include page one (memory from 256 to 511), which is the secret of automatic RUNs. It's easy. If your target program is called "HEXDUP," LOAD it normally and then type SYS 4 to get into the monitor. Type: .M 00C9 00C9 (RETURN)(this shows you the program's highest location in memory), and you will see something like:

00C9 C5 04 00 00 00 00 00 00

We only care about those first two hex numbers. To reSAVE the program with a different name (HEXDUP 1), switch the two hex numbers and add one. In this case, HEXDUP ended in memory at 04C5, so we make it 04C6 during the monitor SAVE. Normally, a BASIC program starts at 0401 hex, but we are going to SAVE this special version from 0100, the bottom of the stack. So, type in the following fashion (substituting your program's new name and the correct end address plus one found at 00C9):

.S "HEXDUP 1", 08, 0100, 04C6

That's it. We now have a version of HEXDUP Which contains a false page one, a mock stack, which will be loaded in whenever HEXDUP 1 is loaded. The computer puts all of its machine language RTS addresses (the same as BASIC'S RETURN) on the stack. Bootfixer will now do two things to HEXDUP 1. It will replace part of the false stack (on HEXDUP 1 while it sits on the disk) with 60 03 60 03, etc. This has the effect of sending control of the computer to address 0361 when HEXDUP 1 is loaded into the machine. Second, a little machine language routine is inserted into HEXDUP 1 at 0361 to make it start a BASIC RUN when control is sent via the false stack to 0361.

To transform HEXDUP 1, just LOAD and RUN Bootfixer. It will ask you for the name of the program you want fixed and then move into the disk and make the necessary changes. If you accidentally give it the name of a program not yet prepared to be fixed, it will report that to you and close all files without doing any damage. Replace lines 480 and 490 with 481 and 491 if you use Upgrade BASIC.

Machine language programs can be made self-starting too. Find out the starting address of the machine language program, and replace the CHR$(96) in line 420 with the least significant byte plus one and the CHR$(3) in line 440 with the most significant byte. This will send control directly to the machine language program following a LOAD.

100 PRINT"{CLEAR} BOOTFIXER" : T = 18 : S = 1 : D$ = "0" OPEN 15, 8, 15, "I"+D$
110 OPEN2, 8, 2,"#"+"0" : REM      OPEN CHANNEL 2
120 REM **** LOCATE TARGET
130 INPUT"FILENAME";NA$ : LN=LEN(NA$)
140 GOSUB 210 : GOSUB 300
150 IFT = 0THENPRINTNA$" NOT FOUND" : GOTO540
160 GOTO140
170 GOTO540
180 REM ***POINT TO BYTE AND GET IT INTO X.
190 PRINT#15, "B-P : "2, L : GET#2, A$ : IFA$ = ""THENA$=CHR$(0)
200 X = ASC(A$) : RETURN
210 PRINT"TRACK"T" SECTOR'S : REM *** CHANGE TRACK/SECTOR
220 PRINT#15, "U1:2, "D$;T;S : REM      PUT T/S INTO DISK BUFFER
230 L=0 : GOSUB180 : T=X:L=1 : GOSUB180 : S=X:RETURN
240 REM *** CHECK FOR FULL MATCH
250 FORJ = I TO I+LN : L=J : GOSUB180 : IFX=0ORX=160 THEN 270
260 X$=X$+CHR$(X) : NEXTJ
270 IFX$<>NA$THENX$ = "":RETURN
280 L=I-2 : GOSUB180 : TT=X:L=I-1 : GOSUB180 : SS=X:PRINT 290 GOTO 340
300 REM *** CHECK THROUGH ONE BLOCK FOR NAME MATCH
310 FORI=5TO230STEP32
320 L=I : GOSUB180:IFCHR$(X)=LEFT$(NA$, 1)THENGOSUB 240
330 NEXTI : RETURN
340 REM *** ACCESS 1ST SECTOR OF TARGET PROGRAM
350 T=TT : S=SS : GOSUB 210
360 L=2:GOSUB 180 : AL=X:L=3 : GOSUB180 : AH=X : SA=AL+AH*256
370 IFSA<>256THENPRINT:PRINTNA$" IS NOT PREPARED FOR BOOTFIX" : GOTO540
380 REM *** ESTABLISH FALSE STACK
400 PRINT#15, "U1 : 2";DR;TT;SS:PRINT
410 FORPB=173 TO 254 STEP2 : PRINT#15, "B-P : 2";PB
420 PRINT#2,CHR$ (96);
430 PRINT#15,"B-P : 2";PB+1
440 PRINT#2,CHR$(3); : PRINT"*"; : NEXT : PRINT
450 PRINT#15,"U2 : 2";DR;TT;SS
460 GOSUB 210 : PRINT
470 REM ***PUT AUTOBOOT CODE ONTO PAGE THREE
480 DATA 165, 202, 133, 43, 165, 201, 133, 42, 32, 233, 181, 32
481 REM FOR UPGRADE DATA 165, 202, 133, 43, 16 5, 201, 133, 42, 32, 114, 197, 32
490 DATA 182, 180, 76, 74, 183
491 REM FOR UPGRADE DATA 66, 196, 76, 196, 198
500 PRINT#15,"U1:2";DR;T;S
510 FORPB=105 TO 121 : READBY : PRINT#15,"B-P : 2";PB
520 PRINT#2,CHR$(BY); : PRINT"."; : NEXT : PRINT : PRINTNA$" CAN NOW BOOT ITSELF"
530 PRINT#15,"U2 : 2";DR;T;S
540 CLOSE2 : CLOSE15