Classic Computer Magazine Archive COMPUTE! ISSUE 1 / FALL 1979 / PAGE 84

Trace For The PET

Brett Butler
3017 Arvida Circle
Mississauga, Ontario
Canada L5N 1R6

I wished I had a TRACE program when I first got my PET. Eventually, I wrote it myself.

TRACE allows you to actually see Basic executing. It resides in the high end of memory, occupying less than 340 bytes.

It displays each line as it's inperpreted. This means that it shows the actual Basic commands being performed, rather than just listing the line. If part of a line is not executed, you won't see it. For example, if you have a conditional statement such as:

100 ON A GOTO 200, 300, 400 and variable

A is 2, you'll see: 100 ON A GOTO 200, 300, or with an IF statement like:

100 IF A > 5 THEN B = B + 2

with A less than 5 you'll see: 100 IF A > 5 THEN B with A 5 or over you'll see: 100 IF A > 5 THEN B = B + 2

One more characteristic of TRACE: it also shows values that are being input.

TRACE comes as a Basic program, which POKEs the machine language instructions into the proper place. It finds the high end of memory, wherever it happens to be, and then builds the machine language up there. So it doesn't matter if your PET is fitted with 4K, 8K, 16K or more: TRACE will be packed into the right place.

1 PRINT "THIS PROGRAM LOCATES TRACE IN"
2 PRINT "ANY SIZE MEMORY THAT IS FITTED ..."
3 PRINT "THIS VERSION WORKS ONLY WITH"
4 PRINT "ORIGINAL R O M PETS - USE ANOTHER"
5 PRINT "VERSION FOR THE NEW (16K, 32K) MACHINES"
10 DATA -343, 162, 5, 189, 181, 224, 149, 194, 202, 16, 248, 169, 239, 133, 210, 96
11 DATA 173, -343, 133, 134, 173, -342, 133, 135, 169, 255, 133, 124, 160, 0, 162
12 DATA 3, 134, 125, 162, 3, 32, -272, 208, 249, 202, 208, 248, 32, -272, 32, -272
13 DATA 76, 106, 197, 162, 5, 189, -6, 149, 194, 202, 16, 248, 169, 242, 133, 210, 96
14 DATA 230, 124, 208, 2, 230, 125, 177, 124, 96, 230, 201, 208, 2, 230, 202, 96, 32
15 DATA 197, 0, 8, 72, 133, 79, 138, 72, 152, 72, 166, 137, 165, 136, 197, 77, 208, 4
16 DATA 228, 78, 240, 107, 133, 77, 133, 82, 134, 78, 134, 83, 173, 4, 2, 208, 14, 169
17 DATA 3, 133, 74, 202, 208, 253, 136, 208, 250, 198, 74, 16, 246, 32, -54, 169, 160
18 DATA 160, 80, 153, 255, 127, 136, 208, 250, 132, 76, 132, 84, 132, 85, 132, 86, 120
19 DATA 248, 160, 15, 6, 82, 38, 83, 162, 253, 181, 87, 117, 87, 149, 87, 232, 48, 247
20 DATA 136, 16, 238, 216, 88, 162, 2, 169, 48, 133, 89, 134, 88, 181, 84, 72, 74, 74
21 DATA 74, 74, 32, -44, 104, 44, 15, 32, -44, 166, 88, 202, 16, 233, 32, -38, 32, -38
22 DATA 165, 75, 197, 201, 240, 55, 165, 79, 208, 4, 133, 77, 240, 47, 16, 42, 201, 255
23 DATA 208, 8, 169, 94, 32, -30, 24, 144, 33, 41, 127, 170, 160, 0, 185, 145, 192, 48
24 DATA 3, 200, 208, 248, 200, 202, 16, 244, 185, 145, 192, 48, 6, 32, -32, 200, 208
25 DATA 245, 41, 127, 32, -32, 165, 201, 133, 75, 104, 168, 104, 170, 104, 40, 96, 168
26 DATA 173, 64, 232, 41, 32, 208, 249, 152, 96, 9, 48, 197, 89, 208, 4, 169, 32, 208
27 DATA 2, 198, 89, 41, 63, 9, 128, 132, 81, 32, -54, 164, 76, 153, 0, 128, 192, 79, 208
28 DATA 2, 160, 7, 200, 132, 76, 164, 81, 96, 76, -256, 32, -263
1000 S2 = PEEK (134) + PEEK (135) * 256 : S1 = S2 - 343
1010 FOR J = S1 TO S2 - 1
1020 READ X : IF X > = 0 GOTO 1050
1030 Y = X + S2 : X = INT (Y/256) : Z = Y - X * 256
1040 POKE J, Z : J = J + 1
1050 POKE J, X
1060 NEXT J
1070 PRINT " === TRACE === "
1080 REM BY BRETT BUTLER, TORONTO
1090 PRINT "TO INITIALIZE AFTER LOAD: SYS"; S1 + 17
1100 PRINT "TO ENABLE TRACE:      SYS" Sl + 56
1110 PRINT "TO DISABLE:   SYS;" S1 + 2
1120 PRINT "CHANGE SPEED WITH : POKE"; S1 + 124; ",X"
1130 PRINT "==MAKE A NOTE OF ABOVE COMMANDS=="
1140 PRINT "SAVE USING MACHINE LANGUAGE MONITOR:"
1150 PRINT "   .S 01, TRACE";
1160 S = INT (S1/256) : T = S1 - S * 256
1170 POKE 134, T : POKE 135, S
1180 POKE 130, T : POKE 131, S
1190 S = S1 : GOSUB1400
1200 S = S2 : GOSUB1400
1210 PRINT : END
1400 PRINT ","; : S = S/4096
1410 GOSUB1420
1420 GOSUB1430
1430 T = INT (S) : IF T > 9 THEN T = T + 7
1440 PRINT CHR$ (T + 48); : S = (S - INT (S)) * 16 : RETURN

Programs may be changed, or new programs loaded, without affecting TRACE. It will stay in there until you cut power off. All Basic functions operate normally (but slower). If you use the STOP key to stop a program, hold it down for a few moments until it "catches."

There are two versions of TRACE: one for original ROM and one for the new upgrade (16K, 32K) ROM.

Once the machine language version of TRACE is written to fit your machine, it may be used right away or saved with the Machine Language Monitor ... Basic TRACE tells you how to do this. The machine language version is handier, since it will load more quickly–and it may be loaded without disturbing other Basic programs previously in memory.

There are four locations you need to know to run TRACE properly. The Basic TRACE loader gives you the addresses that apply to your machine.

5 PRINT "THIS PROGRAM LOCATES TRACE IN"
6 PRINT "ANY SIZE MEMORY THAT IS FITTED ..."
7 PRINT "THIS VERSION WORKS ONLY WITH"
8 PRINT "UPGRADE R O M (32K) PETS – USE
9 PRINT "ANOTHER VERSION FOR ORIGINAL R O M"
10 PRINT "MACHINES."
11 DATA -342, 162, 5, 189, 249, 224, 149, 112, 202, 16, 248, 169, 239, 133, 128, 96
12 DATA 173, -342, 133, 52, 173, -341, 133, 53, 169, 255, 133, 42, 160, 0, 162, 3
13 DATA 134, 43, 162, 3, 32, -271, 208, 249, 202, 208, 248, 32, -271, 32, -271, 76
14 DATA 121, 197, 162, 5, 189, -6, 149, 112, 202, 16, 248, 169, 242, 133, 128, 96
15 DATA 230, 42, 208, 2, 230, 43, 177, 42, 96, 230, 119, 208, 2, 230, 120, 96
16 DATA 32, 115, 0, 8, 72, 133, 195, 138, 72, 152, 72, 166, 55, 165, 54, 197
17 DATA 253, 208, 4, 228, 254, 240, 106, 133, 253, 133, 35, 134, 254, 134, 36, 165
18 DATA 152, 208, 14, 169, 3, 133, 107, 202, 208, 253, 136, 208, 250, 198, 107, 208
19 DATA 246, 32, -54, 169, 160, 160, 80, 153, 255, 127, 136, 208, 250, 132, 182, 132
20 DATA 37, 132, 38, 132, 39, 120, 248, 160, 15, 6, 35, 38, 36, 162, 253, 181
21 DATA 40, 117, 40, 149, 40, 232, 48, 247, 136, 16, 238, 216, 88, 162, 2, 169
22 DATA 48, 133, 103, 134, 102, 181, 37, 72, 74, 74, 74, 74, 32, -44, 104, 41
23 DATA 15, 32, -44, 166, 102, 202, 16, 233, 32, -38, 32, -38, 165, 184, 197, 119
24 DATA 240, 55, 165, 195, 208, 4, 133, 253, 240, 47, 16, 42, 201, 255, 208, 8
25 DATA 169, 105, 32, -30, 24, 144, 33, 41, 127, 170, 160, 0, 185, 145, 192, 48
26 DATA 3, 200, 208, 248, 200, 202, 16, 244, 185, 145, 192, 48, 6, 32, -32, 200
27 DATA 208, 245, 41, 127, 32, -32, 165, 119, 133, 184, 104, 168, 104, 170, 104, 40
28 DATA 96, 168, 173, 64, 232, 41, 32, 208, 249, 152, 96, 9, 48, 197, 103, 208
29 DATA 4, 169, 32, 208, 2, 198, 103, 41, 63, 9, 128, 132, 106, 32, -54, 164, 182
30 DATA 153, 0, 128, 192, 195, 208, 2, 167, 7, 200, 132, 182, 164, 106, 96, 76
31 DATA -255, 32, -262
1000 S2 = PEEK(52) + PEEK (53) * 256: S1 = S2 - 342
1010 FOR J = S1 TO S2 - 1
1020 READ X : IF X > = 0 GOTO 1050
1030 Y = X + S2 : X = INT (Y/256)  : Z = Y - X * 256
1040 POKE J, Z : J = J + 1
1050 POKE J, X
1060 NEXT J
1070 PRINT " === TRACE === "
1080 REMARK : BY BRETT BUTLER, TORONTO
1090 PRINT "TO INITIALIZE AFTER LOAD : SYS" ; S1 + 17
1100 PRINT "TO ENABLE TRACE :      SYS" ; S1 + 56
1110 PRINT "TO DISABLE :   SYS" ; S1 + 2
1120 PRINT "CHANGE SPEED WITH:    POKE" ; S1 + 123; ", X"
1130 PRINT " == MAKE A NOTE OF ABOVE COMMANDS == "
1140 PRINT "SAVE USING MACHINE LANGUAGE MONITOR :"
1150 PRINT "      .S ";
1160 S = INT (S1/256) : T = S1 - S * 256
1170 POKE 52, T : POKE 53, S
1180 POKE 48, T : POKE 49, S
1190 PRINTCHR $ (34) ;"TRACE" ;CHR $(34); ", 01";
1200 S = S1 : GOSUB1400
1210 S = S2 : GOSUB1400
1220 PRINT : END
1400 PRINT ", " ; : S = S/4096
1410 GOSUB1420
1420 GOSUB1430
1430 T = INT(S) : IF T > 9 THEN T = T + 7
1440 PRINTCHR $ (T + 48); : S = (S - INT (S)) * 16 :RETURN

INITIALIZE — seals TRACE into high memory and restores any existing Basic programs. Use once after loading the machine language TRACE.

ARM — sets TRACE on. From this point on, Basic programs can be TRACE'd

DISARM — sets TRACE off. TRACE remains locked in high memory, but does not act on your Basic program.

Speed Location — Poke any value from 1 to 255 here, to control the speed of the TRACE display.

The SYS commands for ARM and DISARM may be given directly from a program. So when you're debugging, you can have your program turn TRACE on at a certain point, and turn it off again later.

If you're tracing a dull part of your program, hold down the SHIFT key. This will speed things up a bit.

Special thanks to Jim Butterfield, Toronto; without his encouragement and assistance TRACE would still be just an idea.