# New Ticket Created by  Jerome Quelin 
# Please include the string:  [perl #18773]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=18773 >


Hi,

The following patch introduces a new file in the befunge interpreter 
that will hold all the debug-related material.

Soon, we'll have a fully-functionnal debugger within the befunge 
interpreter with breakpoints, dumping the playfield, status line, and 
even more... How exciting!

Jerome
-- 
[EMAIL PROTECTED]


-- attachment  1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/43497/34636/99218c/befunge_introduce_debug.patch

diff -urbN parrot.old/languages/befunge/befunge.pasm parrot/languages/befunge/befunge.pasm
--- parrot.old/languages/befunge/befunge.pasm	2002-11-30 10:48:14.000000000 +0100
+++ parrot/languages/befunge/befunge.pasm	2002-11-30 11:26:29.000000000 +0100
@@ -1,5 +1,6 @@
         branch MAIN
 
+.include "debug.pasm"
 .include "flow.pasm"
 .include "io.pasm"
 .include "load.pasm"
@@ -8,16 +9,17 @@
         
 MAIN:
         set I0, 0
-        set I4, 0               # verbose mode
+        set I5, 0               # debug mode
 ARGV_NEXT:        
         inc I0       
         set S10, P0[I0]
         substr S11, S10, 0, 1
         ne S11, "-", ARGV_DONE
-        eq S10, "-v", ARGV_VERBOSE
+        eq S10, "-d", ARGV_DEBUG
         branch ARGV_NEXT
-ARGV_VERBOSE:
-        inc I4
+ARGV_DEBUG:
+        inc I5
+        bsr DEBUG_INITIALIZE    # initialize P3
         branch ARGV_NEXT
 ARGV_DONE:
         set S10, P0[I0]
@@ -28,7 +30,7 @@
         set I0, 0               # x coord of the PC
         set I1, 0               # y coord of the PC
         set I2, 1               # direction of the PC
-        set I5, 0               # flag (1=string-mode,2=bridge,3=end)
+        set I4, 0               # flag (1=string-mode,2=bridge,3=end)
         time N0                 # random seed
         mod N0, N0, .RANDMAX
         set S0, " "             # current instruction
@@ -37,13 +39,13 @@
         
 TICK:
         substr S0, S1, I0, 1
-        eq I4, 0, TICK_NOVERBOSE
-        bsr VERBOSE
-TICK_NOVERBOSE:
+        eq I5, 0, TICK_NODEBUG
+        bsr DEBUG_CHECK_BREAKPOINT
+TICK_NODEBUG:
         eq S0, "\"", FLOW_TOGGLE_STRING_MODE
-        eq I5, 1, IO_PUSH_CHAR
-        eq I5, 2, MAIN_TRAMPOLINE
-        eq I5, 3, MAIN_END
+        eq I4, 1, IO_PUSH_CHAR
+        eq I4, 2, MAIN_TRAMPOLINE
+        eq I4, 3, MAIN_END
 
         # Sole number.
         lt S0, "0", NOT_NUM
@@ -89,7 +91,7 @@
         branch MOVE_PC
         
 MAIN_TRAMPOLINE:        
-        set I5, 0               # no more trampoline
+        set I4, 0               # no more trampoline
 MOVE_PC:
         eq I2, 1, MOVE_EAST
         eq I2, 2, MOVE_SOUTH
@@ -117,64 +119,3 @@
 MAIN_END:
         end
 
-VERBOSE:
-        # Coordinates.
-        print "("
-        print I0
-        print ","
-        print I1
-        print ")"
-        # Current char.
-        print " - '"
-        print S0
-        print "' (ord="
-        ord I10, S0
-        print I10
-        print ")"
-        # Direction.
-        print " dir="
-        print I2
-        # Flags:        
-        set S10, " \""
-        eq I5, 1, VERBOSE_PRINT_FLAG
-        set S10, " #"
-        eq I5, 2, VERBOSE_PRINT_FLAG
-        set S10, " @"
-        eq I5, 3, VERBOSE_PRINT_FLAG
-        set S10, "  "
-VERBOSE_PRINT_FLAG:
-        print S10
-        # Stack.
-        print " stack="
-        set I11, P2
-        set I10, 0
-        ge  I10, I11, VERBOSE_STACK_END
-VERBOSE_STACK_LOOP:       
-        set I12, P2[I10]
-        print I12
-        inc I10
-        ge I10, I11, VERBOSE_STACK_END
-        print ","
-        branch VERBOSE_STACK_LOOP
-VERBOSE_STACK_END:
-        print "\n"
-        ret
-
-DUMP_PLAYFIELD:
-        pushi
-        pushs
-        repeat S10, "-", 82
-        concat S10, "\n"        
-        print S10
-        set I10, 0
-DUMP_NEXT_LINE: 
-        set S11, P1[I10]
-        print "|"
-        print S11
-        print "|\n"
-        inc I10
-        lt I10, 25, DUMP_NEXT_LINE
-        print S10
-        pops
-        popi
-        ret
\ No newline at end of file
Binary files parrot.old/languages/befunge/befunge.pbc and parrot/languages/befunge/befunge.pbc differ
diff -urbN parrot.old/languages/befunge/Changes parrot/languages/befunge/Changes
--- parrot.old/languages/befunge/Changes	2002-11-30 10:48:14.000000000 +0100
+++ parrot/languages/befunge/Changes	2002-11-30 11:33:47.000000000 +0100
@@ -1,5 +1,11 @@
 Revision history for Befunge-93 interpreter written for Parrot.
 
+0.05  Sat Nov 30 11:31:25 CET 2002
+        - new file debug.pasm that will handle all the debugging
+          capabilities of the interpreter.
+        - the verbose flag (-v) is replaced with a debug flag (-d) to
+          activate the integrated debugger.
+
 0.04  Sat Nov 23 10:22:51 CET 2002
         - now using the push and pop instructions of the PerlArray
           PMC, and thanks go to Steve Fink for his hack to pop an
diff -urbN parrot.old/languages/befunge/debug.pasm parrot/languages/befunge/debug.pasm
--- parrot.old/languages/befunge/debug.pasm	1970-01-01 01:00:00.000000000 +0100
+++ parrot/languages/befunge/debug.pasm	2002-11-30 11:42:34.000000000 +0100
@@ -0,0 +1,114 @@
+# Initialize the debug structure.
+# P3 = [ 1, "000 ... 000", [x1, x2, ...], [y1, y2, ...], [x1,y1, x2,y2, ...] ]
+# P3[0] = stop at each step
+# P3[1] = a 128 chars length string, 0 or 1 depending wether the
+#         interpreter should stop at the corresponding character.
+# P3[2] = a PerlArray of column index that should stop the interpreter.
+# P3[3] = a PerlArray of row index that should stop the interpreter.
+# P3[4] = a PerlArray of 2d coord that should stop the interpreter.
+DEBUG_INITIALIZE:
+        pushi
+        pushs
+        new P3, .PerlArray
+        set P3[0], 1          # Stop at first step.
+        repeat S10, "0", 128  # No char to break on.
+        set P3[1], S10
+        new P4, .PerlArray    # No col to break on.
+        set P3[2], P4
+        new P4, .PerlArray    # No row to break on.
+        set P3[3], P4
+        new P4, .PerlArray    # No coord to break on.
+        set P3[4], P4
+        pops
+        popi
+        ret
+
+        
+# Check wether we should stop the interpreter at the current
+# moment, allowing user to play with the debugger.
+DEBUG_CHECK_BREAKPOINT:
+        pushi
+        pushs
+        set I10, P3[0]
+        eq 0, I10, DEBUG_CHECK_BREAKPOINT_CHAR
+        bsr DEBUG_INTERACT
+        branch DEBUG_CHECK_BREAKPOINT_END
+DEBUG_CHECK_BREAKPOINT_CHAR:
+DEBUG_CHECK_BREAKPOINT_END:     
+        pops
+        popi
+        ret
+
+        
+# The interpreter has reached a breakpoint. Let's
+# stop and interact with user.
+DEBUG_INTERACT:
+        bsr DEBUG_PRINT_STATUS
+        ret
+
+        
+# Print the status of the instruction pointer:
+# coordinates, current char, direction, flags and stack.
+DEBUG_PRINT_STATUS:     
+        # Coordinates.
+        print "("
+        print I0
+        print ","
+        print I1
+        print ")"
+        # Current char.
+        print " - '"
+        print S0
+        print "' (ord="
+        ord I10, S0
+        print I10
+        print ")"
+        # Direction.
+        print " dir="
+        print I2
+        # Flags:        
+        set S10, " \""
+        eq I4, 1, DEBUG_PRINT_STATUS_FLAG
+        set S10, " #"
+        eq I4, 2, DEBUG_PRINT_STATUS_FLAG
+        set S10, " @"
+        eq I4, 3, DEBUG_PRINT_STATUS_FLAG
+        set S10, "  "
+DEBUG_PRINT_STATUS_FLAG:
+        print S10
+        # Stack.
+        print " stack="
+        set I11, P2
+        set I10, 0
+        ge  I10, I11, DEBUG_PRINT_STATUS_STACK_END
+DEBUG_PRINT_STATUS_STACK_LOOP:       
+        set I12, P2[I10]
+        print I12
+        inc I10
+        ge I10, I11, DEBUG_PRINT_STATUS_STACK_END
+        print ","
+        branch DEBUG_PRINT_STATUS_STACK_LOOP
+DEBUG_PRINT_STATUS_STACK_END:
+        print "\n"
+        ret
+
+
+# Dump the playfield on stdout.
+DEBUG_DUMP_PLAYFIELD:
+        pushi
+        pushs
+        repeat S10, "-", 82
+        concat S10, "\n"        
+        print S10
+        set I10, 0
+DEBUG_DUMP_PLAYFIELD_NEXT_LINE: 
+        set S11, P1[I10]
+        print "|"
+        print S11
+        print "|\n"
+        inc I10
+        lt I10, 25, DEBUG_DUMP_PLAYFIELD_NEXT_LINE
+        print S10
+        pops
+        popi
+        ret
diff -urbN parrot.old/languages/befunge/flow.pasm parrot/languages/befunge/flow.pasm
--- parrot.old/languages/befunge/flow.pasm	2002-11-30 10:48:14.000000000 +0100
+++ parrot/languages/befunge/flow.pasm	2002-11-30 11:44:39.000000000 +0100
@@ -90,24 +90,23 @@
 # Toggle string mode.
 # Befunge stack unchanged.
 FLOW_TOGGLE_STRING_MODE:
-        eq I5, 1, FLOW_TOGGLE_STRING_MODE_OFF
-        set I5, 1
+        eq I4, 1, FLOW_TOGGLE_STRING_MODE_OFF
+        set I4, 1
         branch MOVE_PC
 FLOW_TOGGLE_STRING_MODE_OFF:      
-        set I5, 0
+        set I4, 0
         branch MOVE_PC
         
 # Trampoline.
 # Befunge stack unchanged.
 # Skip next instruction (pos < pos + delta)
 FLOW_BRIDGE:
-        set I5, 2
+        set I4, 2
         branch MOVE_PC
 
 # Stop.
 # Befunge stack unchanged.
 # End program.
 FLOW_END:
-        set I5, 3
+        set I4, 3
         branch MOVE_PC
-        
\ No newline at end of file
diff -urbN parrot.old/languages/befunge/README parrot/languages/befunge/README
--- parrot.old/languages/befunge/README	2002-11-30 10:48:14.000000000 +0100
+++ parrot/languages/befunge/README	2002-11-30 11:35:24.000000000 +0100
@@ -1,6 +1,6 @@
 DESCRIPTION
 -----------
-This is a Befunge interpreter written in Parrot assembler, version 0.04
+This is a Befunge interpreter written in Parrot assembler, version 0.05
 
 This interpreter should be Befunge-93 compliant. This means the
 playfield is limited to 80x25 and can hold *only bytes*. This means
@@ -13,18 +13,20 @@
 
 Then you can run your Befunge program with: 
 
-     $ ../../parrot befunge.pbc [-v] foo.bef
+     $ ../../parrot befunge.pbc [-d] foo.bef
 
-The -v flag makes the befunge interpreter more verbose.
+The -d flag enables debugging within the befunge interpreter (not yet
+fully implemented, this is a work in progress).
 
 
 FILES
 -----
 The files are the following:
         befunge.pasm    the main loop
-        load.pasm       function to load the code from source file
+        debug.pasm      routines for the debugger
         flow.pasm       handles the flow-control instructions
         io.pasm         handles the io related instructions
+        load.pasm       function to load the code from source file
         maths.pasm      handles all the maths instructions
         stack.pasm      handles the stack instructions
         Makefile        a tiny, little Makefile to help (me) during
@@ -44,7 +46,7 @@
 * more error checking
 * better rand() methods
 * more tests (with Perl and Test::Harness)
-* debugging options
+* debugging options (work in progress)
 * use an array of arrays instead of an array of strings
 * implement Befunge 98
 
diff -urbN parrot.old/MANIFEST parrot/MANIFEST
--- parrot.old/MANIFEST	2002-11-30 10:47:55.000000000 +0100
+++ parrot/MANIFEST	2002-11-30 11:36:55.000000000 +0100
@@ -1288,6 +1288,7 @@
 languages/befunge/Makefile
 languages/befunge/README
 languages/befunge/befunge.pasm
+languages/befunge/debug.pasm
 languages/befunge/flow.pasm
 languages/befunge/io.pasm
 languages/befunge/load.pasm

Reply via email to