Close

A debug console in 4 lines of code

A project log for eForth for cheap STM8S gadgets

Turn cheap modules from AliExpress into interactive development kits!

ThomasThomas 10/25/2020 at 08:230 Comments

A Forth system on a $0.20 MCU like the STM8S003F3 with 8K ROM and 1K RAM can offer surprising advantages. Based on an idea I found in the ROBOFORTH II tutorial I added of just 4 lines to the latest STM8 eForth release to provide a very simple debug console that allows examining the state of a Forth program in the middle of the execution

Here is a simple example:

#require BYE
: test ( n -- )
  FOR
    I DUP . 2 MOD 0= IF
      CR ."  Break - mind the stack - continue with BYE" CR
      OUTER THEN
  NEXT ."  done"
;

A debug session on a STM8S003F3, where a "FOR ... NEXT" loop is terminated manually by changing the loop counter on the return stack ($3FF growing down), may look like this:

66 7 test 7 6
 Break - mind the stack - continue with BYE
$03F0 10 dump
 3F0  23  0  0  A 8D 1E 8D 12  1 1D  0  6 8D 1E 8D 12  1_______________ ok
bye 5 4      
 Break - mind the stack - continue with BYE
$03F0 10 dump
 3F0  23  0  0  A 8D 1E 8D 12  1 1D  0  4 8D 1E 8D 12  1_______________ ok
$3FA ? 4 ok
0 $3FA ! ok  
bye done ok
. 66 ok

Note that a single misspelled word will clear both the data and the return stacks. This will not only reset the console but it will terminate the program and typing BYE in this situation will crash the system! There is clearly room for improvement in the implementation of the library word BYE here.

Edit: here is an improved version of BYE, one that will show a more friendly behavior:

#require OUTER
: BYE ( -- ) [
  \ exit the interpreter on the condition that OUTER was called
  $1605 ,           \ LDW Y,(5,SP)
  $905A ,           \ DECW Y
  $905A ,           \ DECW Y
  $90FE ,           \ LDW  Y,(Y)
  $90A3 , ' OUTER , \ CPW Y,#OUTER
  $2604 ,           \ JRNE +4
  $9085 ,           \ POPW Y
  $9085 ,           \ POPW Y
  ]
;

Discussions