|
(This article was first published in the ICPUG
Journal SEPT/OCT 1988 issue.
Permission from A.L. Minter to republish on the Internet has been
received.)
Managing programs is a serious business, and lots of people have developed
lots of ideas over the years. Some of these are encapsulated in PET Library
disks; others are scattered through the ICPUG Newsletter, and still more
are on members' own disks. This contribution discusses a few worth-while
ideas.
Saving programs
Anyone who has saved a program under the wrong name, getting two copies of
one program and none of another will appreciate this routine. Decide on
a line number (I use 30000) and include in every program the following
two lines:
30000 OPEN 1,8,15:PRINT#1, "S0:progname" :CLOSE1
30001 SAVE "O: progname, 8":END
'progname' is the current program. To save it, simply enter: RUN 30000,
and the program is saved under its right name. You can use GOTO 30000 if
you do not want to lose variables from the last RUN. That way, all you
have to remember is 30000 (or whatever your chosen line number may be).
You do not use "@" to save, because if the disk does not have enough room
left for the saved copy of the program ('@' does not over-write) the save
will fail.
Merging programs
This has to be done by a fiddle, because there is no MERGE command in PET
BASIC. First, you save the program as an ASCII file. Key in:
OPEN 1,8,5, ''O: prognameX, S, W'' <RETURN>
CMD 1:LIST <RETURN>
The program is now sent to the disk just as it would go the the printer
or screen. Modify the name somewhat (by X) to distinguish from the normal
save. When all has finished, enter:
PRINT#l:CLOSE1 <RETURN>
To merge, you need the following short program:
63000 rem program to add another program to the one already in memory.
The program to be added must have been saved as ascii by listing.
63001 rem do this by open1,8,5,"0:name,s,w":cmdl:1ist. Remember to
print#1:close1 at end of listing
63002 rem leave on screen and load main program. add minimerge in usual
way. Merge programs by RUN 63992
63003:
63992 input filename ;n$:nS=0:+n$:openl,8,2,n$+" ,s,r" :pokeO,O
63993 pri nt"<clr><down><down>";
63994 pokel74,1: ifpeek(O) thenclose1 :goto63998
63995 get#1,a$: pokeO, st: pri nta$;: i fa$<>chr$(13)goto63994
63996 printgoto63993: pokel58,5: fori =Oto5: poke623+i,13: next
63997 print"<down><down><down><down>"goto63993:
print"<home>';:
end
63998 print"<clr><down><down>": fori =Oto7: print63992+i:
next: poke158,10
63999 fori =Oto9: poke623+i,13: next: pri nt"list": print"<home>":
end
<cir> = clear screen <home> = home <down> = cursor down
The program is loaded first and displayed on the screen. Next, load
the host program, and insert the merge program by visiting each line from
63992 onwards with the cursor and pressing <RETURN>. Then you RUN 63992
and give the name of the incoming program, already saved in ASCII. The
incoming program is then merged into the program in memory and the merge
lines are deleted. PET Library disk U2 contains other merge routines.
Chaining Programs
If you want to load and run a program while already running another, then
the command LOAD "progname" as a program line does this. But only so long
as the incoming program is no larger than the cailing program. Variable
values are transferred in this case. This means all programs in a suite
must be the same size as the largest one. If you do not want this, and
do not want to transfer variables, then you make the first line of each
program like this:
1 CLR: POKE42, XXX: POKE43, YYY
The values XXX and YYY are obtained by having the program in memory,
complete with the line as above. Then enter:
PRINT PEEK(42), PEEK(43) <RETURN>
and put the results into line 1. Save it. When run from another program,
which is smaller, program space is correctly adjusted for the new program
by line 1.
Automatic Loading
Sometimes it is useful to have a front-end program load a machine-code
program and then call the main program. This can be done by the following:
10 print"<cir>please wait while the program":print"is loaded"
20 print"<down>load"chr$ ( 34 ) "0: progname"chr$ (34) ", 8"
30 print: print"<down><down><down><down>new"
40 print "<down><down>load" chr$(34)"0:mainprog chr$(34) ,8
50 print <down><down><down><down>"run
60 fori = 623to627: poke i ,13 : next: poke158, 5
70 print <home> :end
The screen is set up with the commands you would key in by hand; the
cursor is<homed> and the PET is fooled into thinking it is all happening
through the keyboard by poking the keyboard buffer with carriage retums.
You can extend this idea by having the front end program read the directory,
accept data input (eg a filename) and preserve the name by poking it into
the top of memory, having first lowered the top of BASIC by:
10 poke53,peek(53)-1 :rem lower top of basic
This partitions off 256 bytes at the top of memory. You can preserve
things there without them being trampled over by BASIC.
DATA statements
On PET library disk U2 there is a program called Data Get which peeks the
contents of a chosen area of memory and translates it into DATA statements,
ready for inclusion in a program. When you run that program, you can recreate
the area of memory (eg a piece of machine code) without having to load
it. This is an example of a program which generates a another program.
Arrays
Once dimensioned, you cannot usually get rid of an array. However, by
organising
your arrays so that the ones you want to keep are DlM'd first, and then
the one you want to re-dimension, you can do just this, by the following
code included at the appropriate place in your program. At the start of
the program:
10 P1=O:P2=0
20 DIM A$(15) rem you want to keep this array
21 P2=PEEK(46)+PEEK(47)*256:P1=PEEK(44)+PEEK(45)*256
22 rem P1 and P2 are start and finish of the array A$()
30 DIM B$(20) rem you want to be able to change this array later
.
.
100 Z=PEEK(44)+PEEK(45)*256:Z=Z+P2-P1:P1=INT(Z/256)
101 POKE 47,Pl:POKE46,Z-P1*256
102 rem the array B$() has now gone, and you can safely DIM it again
to the size you want without a re-dimensioned array error
Cursor location on the screen
To position the cursor in a specific place on the screen you could have
two string constants, made up of <home> + 80 <right> and 24 <down>
symbols. Use these by LEFT$ for the number of moves you want. There is
a better way:
100 rem locate cursor to line L, column C
lO1 POKE 216,L:POKE 198,C:SYS XXXXX
102 RETURN
Where XXXXX = 57949 for BASIC2
57471 for BASIC4 40-col
57447 for BASIC4 80-col
Immediately you retum from this GOSUB you must either print or input
some thing at the chosen location.
Screen Input
You can open a file to the sereen to write to it by OPEN 1,3. It then works
the same as if going to a printer. The user is given a choice by a routine
like this:
100 INPUT Screen or Printer ;Z$
101 IF Z$<> "S" AND Z$<> "P" GOTO 100
102 H=4 : IF Z$= "S" THEN H=3
103 OPEN 1,H
rem routine to print, using PRINT$1 follows here
200 CLOSE 1
You can do the same with GWBASIC/BASICA; don't use LPRINT, but open
a file to Z$ for output as 1. Make Z$ equal to SCRN: or LPT1: as appropriate.
Less well known is that, having opened a file to the screen, you can
use it for input as well. This allows you to design a data input screen
without the INPUT fall-out problem, and you can control exactly which
field you want to use for the data input.
10 poke216, sc( i ,1) :poke198, sc( i , 2 ): sys57471 : open9, 3: input#9
, zz$
11 close9:zz$=left$(zz$, sc(i ,3) )
sc(i,1) is the line number on the screen for the ith field; sc(i,2)
is the column, and sc(i,3) is the length of the field to be input. OPEN
9,3 opens an input channel at this location. Into ZZ$ goes everything on
the screen line from the start position to the screen's edge. The unwanted
bits are then cut off.
Un-NEW
There are several programs which will recover from a mistaken NEW command.
Here is a routine to be kept on a piece of paper and keyed in direct.
Clear the screen first, and then type:
POKE 1026, 4: SYS XXXXX
where XXXXX = 50242 for BASIC2
46262 for BASIC4
Toolkits
Toolkits come on a chip for BASIC4, and on tape/disk for BASIC2. However,
be warned! If you have a program written with a piece of machine code sitting
at the top of its BASIC content (and such code is usually re-locatable,
because it will be moved if you edit the BASIC), then use of ANY toolkit
commands will throw away the machine code, and the program will not run.
If you have saved it, over-writing the original version, hard luck!
Variable dumps
PET Library disk U2 contains programs which read through a BASIC program
on disk and make a list of all the variables it contains.
|