home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 128 #29 / q29.d81 / t.st demo < prev    next >
Encoding:
Text File  |  1992-01-01  |  16.1 KB  |  353 lines

  1.  
  2.  
  3.                             S T E P T R A C E R
  4.  
  5.                        Program and Text by e. g. bell
  6.  
  7.  
  8. FENDER'S PREMUMBLE: StepTracer is a programmer's utility and is not
  9. designed to be something you can access from LOADSTAR 128's menu.  However,
  10. ed has provided a revised version of CONTROL80's demo, which shows you that
  11. StepTracer works with CONTROL80 programs -- at least on a C-128D.  I
  12. couldn't get it to work right on a flat C-128, so I don't recommend looking
  13. at the demo on a flat C-128 unless you like a blank screen half way through
  14. the demo.  My theory is that CONTROL80's VLOAD command is a little too
  15. weird for StepTracer.  It's also hardcoded for drive 8.  I don't consider
  16. VSAVE and VLOAD to be among CONTROL80's more useful commands so you should
  17. still be able to use StepTracer with your CONTROL80 programs.  Even on a
  18. C-128D there is a spot at the end of the demo where it asks you to press a
  19. key while the screen scrolls horizontally.  Press RESTORE instead and you
  20. get back to the demo's title screen where you can return to LOADSTAR 128.
  21.  
  22.  
  23. *** StepTracer
  24.  
  25.     When debugging a BASIC program, it is often necessary to follow the
  26. flow of logic.  When you get unexpected results, is the program doing what
  27. you want it to do or is it just doing what you told it to do?  There are
  28. several utilities of varying degrees of usefulness in determining this.
  29.  
  30. *** Dump
  31.  
  32.     There are 'dump' utilities which dump the names and current values of
  33. all variables.  Using such a utility usually requires frequent STOPs to
  34. check these values at various points in the program operation.
  35.  
  36.     This can be useful in determining why or where something is happening
  37. in a program, but a computer executes a program at an extremely high rate
  38. of speed.  Couple that with the many twists and turns the program can take
  39. while getting from here to there and it can be a long, time-consuming
  40. process to isolate and fix some problems using only a dump utility.
  41.  
  42. *** Follow Me
  43.  
  44.     More useful are the 'trace' utilities.  Most of these trace program
  45. flow by printing the line number, to the screen inside brackets, of the
  46. BASIC statement currently being executed.  For example, consider the
  47. following code:
  48.  
  49.       10 print chr$(147):na$="":cnt=0:input "Enter Name  ";na$
  50.  
  51. If you used the BASIC 7.0 TRON command, it would produce the following
  52. screen display:
  53.  
  54.       [10][10][10][10]Enter Name  ?
  55.  
  56. If you have never tried this with your Commodore 128 BASIC programs, type
  57. TRON then run any BASIC program.  [TROFF toggles this function off.]  
  58.  
  59.     There have been many such utilities published for the C64 over the
  60. years also.  Most, if not all of these, function exactly the same.  As
  61. helpful as this type of utility is, as you may have noticed from the
  62. example, there are some serious limitations.
  63.  
  64. *** Drawbacks
  65.  
  66.     First, using this type of trace, screen display is hopelessly disrupted
  67. as rows and rows of line numbers are dutifully displayed onscreen starting
  68. wherever the cursor happened to be resting at the time.  There is much
  69. information presented, but it is confusing if you can use it at all.  This
  70. is particularly true in the case of deeply nested loops or GOSUB/GOTO
  71. procedures.  The information can be simply overwhelming.
  72.  
  73.     Second, often you know where something is happening, but cannot locate
  74. where the offending code is being called.  This can be particularly
  75. troublesome to find if the code is called from several places.  Which call
  76. is causing the problem?  StepTracer will help you isolate such calls
  77. quickly.  This capability of StepTracer will help you find and correct the
  78. problem areas instead of wasting a lot of time looking at the wrong place
  79. in your code.
  80.  
  81.     Third, the trace, like the program being traced, is operating at
  82. extreme speeds.  It is virtually impossible to visually follow the
  83. execution unless frequently stopped by user input or STOP statements.  This
  84. can be irritating and confusing.  If sections of the code being traced
  85. clear the screen, it can be quite impossible.  If you STOP the program, you
  86. must start it again with a GOTO, CONT, or RUN.  If you merely request user
  87. input, the screen display problem mentioned above presents itself,
  88. particularly if you are trying to find out why something is being displayed
  89. WHERE it is being displayed.
  90.  
  91.     Finally, you learn little from only a line number unless you are
  92. working with a program listing in front of you, especially when a problem
  93. involves several lines or routines.  Since you are usually the person who
  94. wrote the program, it is your logic.  If the logic in the listing was
  95. working as expected, you wouldn't need to trace the flow.  In addition, if
  96. the program flow jumps around a lot, with GOSUBs for example, it can become
  97. difficult to follow the logical flow from a listing alone.  So a listing by
  98. itself is often of limited help in battling some bugs.
  99.  
  100. *** A Solution
  101.  
  102.     There is a solution.  StepTracer is a short single-step trace routine
  103. for the Commodore 128 in 80 column mode.  It will not work in 40 column
  104. mode.   StepTracer will lead you along the precise route your program is
  105. taking.  But it does more!
  106.  
  107.     More than just the line numbers, the complete segment of BASIC code TO
  108. BE EXECUTED is displayed for you prior to its execution.  You know what
  109. will take place and in which line it was commanded to do so just BEFORE it
  110. actually happens.
  111.  
  112.     If that is not enough, there is virtually no disruption of screen
  113. display.  Using some interesting capabilities of the VDC chip in your
  114. Commodore 128, StepTracer gives you some extra lines at the bottom of your
  115. screen IN ADDITION TO the normal 25 lines.  But it does still more.
  116.  
  117. *** Single Step
  118.  
  119.     Even displaying a segment of code would be of minimal value if it
  120. happened at extreme speed, so StepTracer is an adjustable speed trace
  121. utility AND a single-stepper.  You may slog through your code at various
  122. speeds OR step through your program one segment at a time if you like.  
  123.  
  124.     In single-step mode, after the program segment is displayed, you must
  125. press a SHIFT Key to continue.  (In single-step mode, a '*' is displayed
  126. after the line number and before the program text to flag that user input
  127. is expected before the program can continue)  When you do, the segment of
  128. code you are looking at is executed and the next segment is displayed.  If,
  129. for example, the current segment says GOTO 50 the next thing you will see
  130. in the trace window is the first code segment of line 50.  This feature
  131. could also be helpful to novice computer programmers wondering what
  132. different segments of someone else's code do.
  133.  
  134.     In single-step mode, if you want to stop the program for editing, you
  135. must press the STOP key and then press the SHIFT key to resume execution.
  136. The program will then stop normally.
  137.  
  138.     Note that I refer to code 'segments' above rather than just 'lines'.
  139. This is because BASIC evaluates lines of code in this manner when there are
  140. multiple commands on a single line.  In cases like this, StepTracer lists
  141. each such command individually, just as BASIC will execute it.  The line
  142. number remains onscreen though, so regardless of which segment is being
  143. displayed, the appropriate line number is always evident.
  144.  
  145. *** Tron
  146.  
  147.     Using StepTracer is very simple and will interfere with no BASIC
  148. programs.  First, you must install it using the BLOAD command:
  149.  
  150.     bload "StepTracer"
  151.  
  152. Next, you simply activate it: 
  153.  
  154.     sys 2816,a,b
  155.  
  156.     a =    0-Trace Disabled    1-Trace Enabled
  157.  
  158.     b =    0-6 Speed of trace display   0-fastest  6-slowest
  159. or  b =    128 Requires pressing SHIFT after each segment displayed
  160.  
  161. So, to turn the trace on with a speed factor of 3, the command would be:
  162.  
  163.     sys 2816,1,3
  164.  
  165. To turn the trace on with a speed factor of 0, the fastest possible, the
  166. command would be:
  167.  
  168.     sys 2816,1,0
  169.  
  170. To turn the trace on and specify that you must press the SHIFT key after
  171. each segment is displayed, the command would be:
  172.  
  173.     sys 2816,1,128
  174.  
  175. If you just want to toggle trace mode on with the current speed setting
  176. unchanged, you may use the BASIC 7.0 'TRON' command in your BASIC program.
  177. StepTracer recognizes this command as its own and resumes the trace.  For
  178. this reason, the normal function of TRON is disabled if used in program
  179. mode.  If you use TRON in immediate mode, simply typing it and pressing
  180. RETURN, it will function normally until StepTracer is activated.
  181.  
  182. *** Troff
  183.  
  184.     To turn the trace off, disabling StepTracer, the command would be:
  185.  
  186.     sys 2816,0
  187.  or sys 2819
  188.  
  189. If you just want to toggle trace mode off with the current speed setting
  190. unchanged and StepTracer enabled, you may use the BASIC 7.0 'TROFF' command
  191. in your BASIC program.  StepTracer recognizes this command as its own and
  192. stops tracing.  The last command executed before the TROFF command will
  193. remain on display on the trace line, but line by line tracing will stop.
  194. For this reason, the normal function of TROFF is disabled if used in
  195. program mode. 
  196.  
  197. *** Tweaking
  198.  
  199.     You can adjust StepTracer trace speed from within your program without
  200. using the SYS command if you like.  To do this, POKE the appropriate value,
  201. from 0 through 6 (or 128 for prompted mode), into address 2822.  This POKE
  202. does the same thing as the 'sys 2816,1,#' command.  This is useful when you
  203. want a loop to proceed at full speed and then slow the trace down again
  204. afterward.  For example:
  205.  
  206.     100 poke 2822,.
  207.     110 for x=1 to 1000:read a:poke b+z,a:z=z+1:next
  208.     120 poke 2822,1
  209.  
  210. This example is the same as the one above using the SYS call with the speed
  211. factor set to zero.  The two methods may be used interchangeably.
  212.  
  213.     You can also do some dynamic temporary tweaking to the trace speed.
  214. This technique is primarily for programmers who write structured code using
  215. subroutines.  You may want to zip through a section of code that you are
  216. tracing.  You can do this in one of several ways.
  217.  
  218.   o As mentioned above,  you can POKE a zero into 2822, and a 1 or
  219.     other value after the routine is complete.
  220.  
  221.   o You can use the TROFF and TRON commands before and after the
  222.     routine.
  223.  
  224.   o You can also tap the RESTORE key.
  225.  
  226. The RESTORE key will drop the speed setting of the trace to zero until the
  227. next RETURN statement is encountered in the BASIC code.  At that point, the
  228. original speed setting is restored.  Using this feature, if your programs
  229. are very structured, you can set the trace speed once and interactively
  230. 'hurry' through areas that are repetitive and unnecessarily slow down
  231. StepTracer.  An example of this situation would be a READ/POKE routine that
  232. installs sprite data or machine language routines.
  233.  
  234. *** Caveat
  235.  
  236.     There are some situations that StepTracer cannot handle, or which will
  237. cause StepTracer to fail.  One of them is to try to bload StepTracer while
  238. it is already active.  This will cause a lockup.  The recommended procedure
  239. is to bload StepTracer prior to running the program you want to trace.
  240.  
  241.     You may activate StepTracer with the SYS 2816,1,# command in immediate
  242. mode before running the program or from within the program itself.  You may
  243. even use this to change the speed of the trace during the program
  244. execution.  It is only reloading of StepTracer over itself that causes the
  245. problem.
  246.  
  247. *** REU and RAMDOS
  248.  
  249.     StepTracer will work with any BASIC program that does not use the 4
  250. memory pages $0b00 through $0eff.  If you are using a Ram Expansion Unit
  251. and RAMDOS, you should not use any of these for the interface page.  Select
  252. page 15 ($0f00) instead and you will have no problems.  A good workaround
  253. for this is to simply disable those parts of your program that use RAMDOS
  254. until the other parts are debugged if you absolutely must have RAMDOS
  255. installed in this area.
  256.  
  257.     Also note that if you are using RAMDOS, you should install StepTracer
  258. BEFORE you install RAMDOS.  The first time StepTracer is loaded and
  259. installed, a help screen is displayed.  This screen is disabled after the
  260. first display, but most of it lives in page $0f00, where RAMDOS must be
  261. installed.  Therefore, StepTracer MUST be BLOADed and installed before
  262. RAMDOS.
  263.  
  264. *** VDC Considerations
  265.  
  266.     It is the VDC chip that gives the Commodore 128 its 80 column
  267. capabilities.  One of these is the ability to display more than 25 lines on
  268. the screen.  StepTracer mines this treasure, and this is the reason it only
  269. works in 80 column mode.
  270.  
  271.     StepTracer makes some adjustments to the VDC attribute settings in
  272. order to tweak several extra lines onto your screen.  It is here that the
  273. trace display appears.  Some programs that use other buffers in the VDC for
  274. storage, etc. may have some problems with StepTracer enabled.  These
  275. problems are not fatal to StepTracer.  They might affect, for example,
  276. programs that use RAM in the VDC chip to store and recall program screens.
  277. They also may affect the status line display of StepTracer.  Some of the
  278. effects in the Control80 demo cause the display of StepTracer to be
  279. momentarily off the screen or unreadable.  Again, simply use StepTracer to
  280. debug the other parts of your program first, disabling these spots in your
  281. code.  When the other problems are removed, complete your debugging without
  282. StepTracer.  I suspect that most BASIC programmers will never find this to
  283. be a problem.
  284.  
  285. *** The Getaway
  286.  
  287.     When you are finished debugging your program, you should make sure that
  288. StepTracer is disabled using one of the two methods described above.
  289. (Either 'sys 2819' or 'sys 2816,0')  This will assure that your VDC is
  290. properly reset.  You can't hurt anything if you don't reset the VDC, but it
  291. may cause display problems for subsequent programs.
  292.  
  293.     More important than the VDC is the BASIC execution vector which MUST be
  294. restored to its default state.  This is very important and can cause some
  295. very weird things if you don't reset the vector.
  296.  
  297. *** Harmony
  298.  
  299.     StepTracer can exist quite peacefully with Control80 and programs that
  300. use it.  There are no memory contentions and I added the code to recognize
  301. and trace the Control80 commands.  (The lone exception to this MIGHT be the
  302. 'MINE' command.  Control80 is flexible in that add-on routines can be
  303. written to execute anywhere in memory, requiring only a POKE to the main
  304. code to direct Control80 to the new code.  Any such add-ons that were
  305. written to load and execute where StepTracer lives are obviously going to
  306. cause a serious burp if used with StepTracer.)
  307.  
  308.     mEGaBasic is another matter.  If you are using my mEGaBasic utility,
  309. make sure you disable StepTracer (sys 2816,0  or  sys 2819) before enabling
  310. mEGaBasic.  All of mEGaBasic's command functions use the same memory as
  311. StepTracer.  The problem this will cause relates back to the restoration of
  312. the BASIC execution vector.  This vector under StepTracer points to a new
  313. section of code.  mEGaBasic is going to change the code. The problem will
  314. show up immediately when you try to press RESTORE, RUN/STOP RESTORE, or run
  315. your program.  The simple solution is to make sure StepTracer is disabled
  316. before you enable mEGaBasic.  This also means, obviously, that StepTracer
  317. will have to be reloaded and reinstalled the next time it is needed.
  318.  
  319.     If you make a mistake and activate something that causes a problem,
  320. there is something you may be able to do to make a spot correction to at
  321. least allow you to save your work and do a RESET.  If you are using
  322. mEGaBasic, you can do a reset then use the OLD command  (or the '@o'
  323. command if you have JiffyDos).  If you don't have either of these at your
  324. disposal, try this:
  325.  
  326.     First, type 'MONITOR' and press RETURN to enter the machine language
  327. monitor of your Commodore 128.
  328.  
  329.     Next, type the command 'm 0308'.  This will bring up a dump of memory
  330. starting at address $0308.
  331.  
  332.     Now, move your cursor up to the start of the hex code after address
  333. $0308.  Replace the first two values with the values 'a2 4a'.  This will
  334. repair the BASIC execution vector.
  335.  
  336.     Next, move your cursor to the line starting with address $0318.
  337.  
  338.     Replace the first two values in that line with the values '40 fa'.
  339. This will restore the NMI vector to normal.
  340.  
  341.     From here you should be back in business.  Save whatever you were
  342. working on (using a new name so as to protect your disk backup) and reset
  343. the computer.  Now reload the new version and start again.
  344.  
  345. *** A Perfect World
  346.  
  347.     In a perfect world, there would be no need for trace utilities.  We
  348. would write things that worked perfectly the first time out.  That is not
  349. yet the case.  In the meantime, though, there is StepTracer.
  350.  
  351.                 \\\\\ RETURN - Menu   R - Run \\\\\
  352.  
  353.