home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / FPC355_5.ZIP / TCOM96.ZIP / TCOM / DEBUGGER / CDBG.SEQ next >
Encoding:
Text File  |  1991-04-09  |  14.4 KB  |  395 lines

  1. \\ CDBG96.SEQ       A debugger for 80c196                    by Mike Mayo
  2. Adapted from CODEBUG.SEQ, a CODE debugger for F-PC by Tom Zimmer
  3. ─────────────────────────────────────────────────────────────────────────────
  4. $Header:   F:/tcom/debugger/logs/cdbg.sev   1.6   23 Apr 1991 11:00:24   MikeM  $
  5. ─────────────────────────────────────────────────────────────────────────────
  6.  
  7. This file contains a simple debugger for Intel 80c196 to allow debugging
  8. a Forth program compiled by TCOM96.
  9.  
  10. It uses the COM port of a PC to communicate with the serial port
  11. of the 80c196.
  12. Code in the target supports several commands,
  13. e.g. fetch, store, call-subroutine.
  14. It also has code to handle breakpoint traps.
  15.  
  16. Limited operations are supported while in the debugger,
  17. read the screen while debugging and press F1 for some help.
  18.  
  19. ─────────────────────────────────────────────────────────────────────────────
  20. $Log:   F:/tcom/debugger/logs/cdbg.sev  $
  21. \    Rev 1.6   23 Apr 1991 11:00:24   MikeM
  22. \ Improved
  23. ─────────────────────────────────────────────────────────────────────────────
  24. {
  25.  
  26. anew cdbgwords
  27.  
  28. target          \ set target byte fetch for disassembler
  29.  
  30. }
  31. ─────────────────────────────────────────────────────────────────────────────
  32.   Words to allow setting the debugging registers
  33. ─────────────────────────────────────────────────────────────────────────────
  34. {
  35.  
  36. : reg!          ( a1 | <name> -- )      \ defining word to make register
  37.                                         \ assignment words
  38.                 create , does> @ ! ;
  39.  
  40. : reg@          ( a1 | <name> -- )      \ defining word to make register
  41.                                         \ fetch words
  42.                 create , does> @ @ ;
  43.  
  44.  
  45. fload break96   \ target-specific breakpoint definitions
  46.  
  47. }
  48. ─────────────────────────────────────────────────────────────────────────────
  49.   Some handy debugging utilities
  50. ─────────────────────────────────────────────────────────────────────────────
  51. {
  52.  
  53. : dbeeol ( -- ) \ fill the screen with spaces to the end of the box
  54.         #out @ rboxcol
  55.         < if    rboxcol 1- sp>col
  56.         else    cols 1- sp>col
  57.         then
  58.         ;
  59.  
  60. : .dbheader ( -- ) \ draw outline of debugging screen
  61.         >norm
  62.         0 dbtop rboxcol 1- rows box&fill
  63.         at? 1+ dplace 2!
  64.         ." \1 Addr   Instruction                  Data  Code          "
  65.         rboxcol dbtop cols 1- rows box&fill
  66.         at? 2+ rplace 2!
  67.         at? 1- at
  68.         ." \3  80c196 State  \0"
  69.         ;
  70.  
  71. : .code ( a1 n1 -- ) \ show a string of code numbers that are in the target
  72.         0max bounds
  73.         ?do  i dt@ H.4  loop  ;
  74.  
  75. : Forth-command ( -- ) \ Provide for one forth command line
  76.         savescr
  77.         savecursor cursor-on
  78.         0 rows 3 - 2>r
  79.         2r@ at                  \ place it at the bottom of the debug window
  80.         rboxcol sp>col cr
  81.         rboxcol sp>col cr
  82.         rboxcol sp>col
  83.         2r> at
  84.         ." \1 Ready for a \`Forth\` command " cr
  85.         ."  ok" cr
  86.         query interpret         \ get and execute a Forth command line
  87.         cr ." \2 Press a key "
  88.         key drop
  89.         restcursor
  90.         restscr ;
  91.  
  92. }
  93. ─────────────────────────────────────────────────────────────────────────────
  94.   Display current instruction followed by data stack
  95. ─────────────────────────────────────────────────────────────────────────────
  96. {
  97.  
  98. 0 value ipprev \ used for holding the address of the prior instruction
  99.  
  100. : show-inst ( -- ) \ disassemble and show one instruction
  101.         save> base  hex
  102.         at?  dbeeol  at         \ blank the line
  103.         cp @ dup>r              \ get instruction address
  104.         space h.4 inst          \ disassemble instruction
  105.         44 #line @ at           \ jump to code column
  106.         r> cp @ over - 2 min .code      \ show code
  107.         restore> base
  108.         >norm  ;
  109.  
  110. : .inst ( -- ) \ display the next instruction to single-step
  111.         trapadr-@ cp !
  112.         next-break if >rev else >ATTRIB3 then
  113.         show-inst
  114.         cp @ !> nextinline
  115.         ;
  116.  
  117. : .ninst ( n -- ) \ display the nth instruction after the next to single-step
  118.         1+ next-break
  119.         = if    >attrib3
  120.                 cp @  !> nextinline    \ set up for "run to" option
  121.           then
  122.         show-inst  ;
  123.  
  124. : .pinst ( -- ) \ display the last executed instruction
  125.         ipprev 0=
  126.         if      dbeeol
  127.         else    ipprev cp !  show-inst
  128.         then
  129.         ;
  130. }
  131. ─────────────────────────────────────────────────────────────────────────────
  132.   Break point registers and control words.
  133.   Allow setting, removing, and displaying the current break point.
  134. ─────────────────────────────────────────────────────────────────────────────
  135. {
  136. #trapbytes 2+ value #breakbytes
  137. create breakregs    #breakbytes #breakregs * allot
  138.        breakregs    #breakbytes #breakregs * erase
  139. 0 value breakreg        \ breakpoint selector, 0 <= breakreg < #breakregs
  140.  
  141. : breakaddr ( -- a ) \ return pointer to the target address of
  142.                      \ the breakpoint selected by breakreg.
  143.         breakreg
  144.         0max #breakregs 1- min
  145.         #breakbytes *
  146.         breakregs + ;
  147.  
  148. : breaksave ( -- a ) \ return pointer to the target image of
  149.                      \ the breakpoint selected by breakreg.
  150.         breakaddr 2+ ;
  151.  
  152. : ?breakset ( n -- f ) \ return true flag f if breakpoint n is set.
  153.                        \ In addition it selects breakpoint n for
  154.                        \ subsequent use of breakaddr and breaksave
  155.         !> breakreg
  156.         breakaddr @ 0<> ;
  157.  
  158. : unbreak ( n -- ) \ remove break point n
  159.         ?breakset       \ if break register is set
  160.         if              \ restore words at break point
  161.                 breakaddr @  breaksave  tunbreak
  162.                 breakaddr off   \ make address = 0, to indicate
  163.                                 \ breakpoint no longer set
  164.         then  ;
  165.  
  166. : unbreak-all ( -- ) \ remove all break points
  167.         #breakregs 0 do  i unbreak  loop ;
  168.  
  169. : setbreak ( break-address n -- ) \ set breakpoint n
  170.         unbreak                 \ clear breakpoint
  171.         breakaddr !             \ save target-address of breakpoint
  172.         breaksave breakaddr @ tsetbreak   \ save instructions at breakpoint
  173.         ;                                 \ and set trap
  174.  
  175. : .breaks ( -- ) \ display the current break points
  176.         cr
  177.         0
  178.         #breakregs
  179.         0 do    breakaddr @
  180.                 ?dup if ." Break point " i .
  181.                         ." set at " h. ." h"
  182.                         1+ cr
  183.                      then
  184.                 loop
  185.         0= if ." No break point set"  then
  186.         ;
  187.  
  188. }
  189. ─────────────────────────────────────────────────────────────────────────────
  190.   Set a trap after the next instruction, return from the prior trap,
  191.   then receive the next trap
  192. ─────────────────────────────────────────────────────────────────────────────
  193. {
  194. 0 value waiting-at-break  \ flag to show the target is waiting in a breakpoint
  195.  
  196. : receive-trap ( -- )   \ wait till it hits the next breakpoint
  197.                         \ at which time it sends a 't' followed by the SP
  198.                         \ and we copy the machine image from its stack.
  199.  
  200.         begin   key? if  key $1b = if Forth-command then  then
  201.                 com-cnt 0<> if  getbyte 't' =
  202.                           else  false
  203.                           then
  204.                 until      \ wait for the trap call to happen
  205.  
  206.         getword                 \ get the trap stack address
  207.         read-break-stack        \ now read in all the stacked items
  208.         unbreak-all             \ restore memory at the break location(s)
  209.         on> waiting-at-break    \ flag the target is waiting in a breakpoint
  210.         ;
  211.  
  212. : gofromtrap ( -- ) \ Make the target return from a trap
  213.                     \ First put (modified) values back into target
  214.         restore-break-stack
  215.         untrap                  \ excute from previous breakpoint
  216.         off> waiting-at-break   \ flag the target is no longer waiting
  217.         ;
  218.  
  219. : exec>trap ( -- ) \ make the target return from a trap,
  220.                    \  and wait for the next trap.
  221.         gofromtrap              \ excute from previous breakpoint
  222.         receive-trap            \ wait till it hits the next breakpoint
  223.                                 \ and get all the data
  224.         ;
  225.  
  226. : one-step ( -- ) \ execute the next instruction
  227.         TRAPADR-@ !> ipprev     \ present instruction will be the previous
  228.         next-break
  229.         0= if
  230.                 branch-type
  231.                 case
  232.                         0 of    nextinline   0 setbreak   endof
  233.                         1 of    nextonbranch 0 setbreak   endof
  234.                         2 of    nextinline   0 setbreak
  235.                                 nextonbranch 1 setbreak   endof
  236.                         3 of    nextonbranch 0 setbreak   endof
  237.                 drop endcase
  238.            else
  239.                 nextinline 0 setbreak
  240.            then
  241.         off> next-break
  242.         exec>trap
  243.         ;
  244.  
  245. : one-step/skip ( -- ) \ execute to the next instruction in line
  246.                        \ i.e. do not nest into CALLs
  247.         TRAPADR-@ !> ipprev     \ present instruction will be the previous
  248.         next-break
  249.         0= if
  250.                 branch-type
  251.                 case
  252.                         0 of    nextinline   0 setbreak   endof
  253.                         1 of    nextonbranch 0 setbreak   endof
  254.                         2 of    nextinline   0 setbreak
  255.                                 nextonbranch 1 setbreak   endof
  256.                         3 of    nextinline   0 setbreak   endof
  257.                 drop endcase
  258.            else
  259.                 nextinline 0 setbreak
  260.            then
  261.         off> next-break
  262.         exec>trap
  263.         ;
  264.  
  265. }
  266. ─────────────────────────────────────────────────────────────────────────────
  267.   Show the current registers,
  268.   and a series of instructions as they will be executed.
  269. ─────────────────────────────────────────────────────────────────────────────
  270. {
  271. defer abrowse   \ to enter the browser with a specific address
  272. 0 value browse-addr
  273.  
  274. : show_debug    ( -- )
  275.         .regs
  276.         dplace 2@ at
  277.         .pinst dcr .inst dcr        \ show past and present instructions
  278.         at? 2>r
  279.         1 dbtop at
  280.         next-break
  281.                 if  ." \3 Press \4 Space \3  to run to highlighted instruction "
  282.               else
  283.                         branch-type 3
  284.                         = if
  285.                                 ." \4 Space \3 next in line \0────"
  286.                                 ." \4 Enter \3 enter subroutine "
  287.                         else
  288.                                 ." \3 Press \4 Space "
  289.                                 ." \3 to execute highlighted instruction "
  290.                         then
  291.               then
  292.         2r> at
  293.         rows  dbtop -  5 -
  294.         0 do   i .ninst dcr    loop
  295.         next-break
  296.                 if      nextinline !> browse-addr
  297.               else      trapadr-@  !> browse-addr
  298.               then
  299.         abrowse    \ show source listing
  300.         ;
  301.  
  302. : set_register  ( -- )
  303.         savescr
  304.         savecursor cursor-on
  305.         0 rows 1- 2dup at cols 1- sp>col at
  306.         ." \1 { In the form e.g. $23 =W0 } command: "
  307.         query interpret \ user may modify a register
  308.         restcursor
  309.         restscr
  310.         .regs           \ redisplay the registers
  311.         ;
  312.  
  313. : up_dbline ( -- ) \ move the breakpoint up one instruction
  314.         next-break 1- 0max !> next-break
  315.         next-break 1 = if off> next-break then      \ skip 1
  316.         show_debug
  317.         ;
  318.  
  319. : down_dbline ( -- ) \ move the breakpoint down one instruction
  320.         next-break 0= if incr> next-break then      \ skip 1
  321.         incr> next-break
  322.         show_debug
  323.         ;
  324. }
  325. ─────────────────────────────────────────────────────────────────────────────
  326.   Additional MINI help for the debugger.
  327. ─────────────────────────────────────────────────────────────────────────────
  328. {
  329. : debug_help     ( -- )
  330. \        savecursor cursor-off savescr
  331.         0 5 59 19 box&fill
  332.         bcr ."   Debugger commands:" bcr
  333.         bcr ."   \1 SPACE \0 = Go to highlighted instruction"
  334.         bcr ."   \1 F \0     = Forth command line"
  335.         bcr ."   \1 R \0     = Set a Register ( Forth command line )"
  336.         bcr ."   \1 Enter \0 = Nest into a CALL"
  337. \        bcr ."   \1 F2 \0    = Switch to full-screen browse"
  338.         bcr ."   \1 Esc \0   = Pull-down menus"
  339.         bcr
  340.         bcr ."   \1 Press ESC to continue, or SPACE for more help "
  341.         key $1B
  342.   <> if
  343.         0 5 59 19 box&fill
  344.         bcr ."   After \1 R \0 = Set a Register ( FORTH command line )"
  345.         bcr ."   Using \`<number> =W0\` will set W0 to <number>."
  346.         bcr ."   Examples of registers that can be set...."
  347.         bcr ."   =W0  =W2  =TTOS  =TTOS2  =flags"
  348.         bcr ."   Or use T! TC! tedit etc. to change target memory."
  349.         bcr bcr ." \S10\1 Press ANY key to continue " key drop
  350.      then
  351. \        restscr restcursor
  352.         ;
  353.  
  354.  
  355. : DB-endfunc  \ ( To be done when leaving the debugger )
  356.                 \ Maybe let the target run if it is waiting at a breakpoint
  357.         waiting-at-break
  358.         if      \ if the target is waiting in a breakpoint
  359.                 10 dbtop rows + 2/ 60 over 2+ box&fill
  360.                 ."   \2 Do you wish to let the TMS320 run? \3 (Y/N) "
  361.                 begin
  362.                         key upc
  363.                         case
  364.                                 'Y' of  true  true   endof
  365.                                 'N' of  false true   endof
  366.                                 beep drop false
  367.                                 endcase
  368.                         until
  369.                 if gofromtrap then      \ run from the breakpoint
  370.         then
  371.         ;
  372. }
  373. ─────────────────────────────────────────────────────────────────────────────
  374.   The main trace loop.
  375.   It single-steps through instructions
  376. ─────────────────────────────────────────────────────────────────────────────
  377. {
  378.  
  379. : dsteps ( -- ) \  This is the debugger.
  380.                 \  It is to be used after setting a breakpoint
  381.                 \  and causing it to be executed in the target.
  382.         dbtop !> browselines
  383.         0 ?breakset if
  384.                         0 dbtop rboxcol rows 1- box&fill
  385.                         ." \2 Waiting for target-trap "  bcr
  386.                         ." \1 Press \3 ESC \1 for options \0   "
  387.                         receive-trap
  388.                    then
  389.         .dbheader
  390.         show_debug
  391.         on> showingdebug
  392.         ;
  393.  
  394.