compiletoflash

: 0-foldable $40 setflags immediate ;
: 1-foldable $41 setflags immediate ;
: 2-foldable $42 setflags immediate ;
: 3-foldable $43 setflags immediate ;

: ( [char] ) parse drop immediate 0-foldable ;  (  Forth-Style-Comment )
: { [char] } parse drop immediate 0-foldable ;  { Pascal-Style-Comment }
: \        0 parse drop immediate 0-foldable ;  \ Comment till end of line

32 constant bl
: cr    10 emit ;
: space bl emit ;
: spaces 0 ?do space loop ;

\ Just in case if you don't have it already in your flash...


: hex.  0 <# # # # # #> type ;
: chex. 0 <# # # #> type ;

: hexdump ( -- ) \ Dumps complete Flash
  cr hex

  \ MSP430F2274: Complete: $FFFF $8000 Dictionary only: $DBFF $8000
  \ MSP430G2553: Complete: $FFFF $C000 Dictionary only: $DBFF $C000

  $FFFF $8000
  do
    \ Check if it would be $FFFF only:
    0                 \ Not worthy to print
    i #16 + i do      \ Scan data
      i c@ $FF <> or  \ Set flag if there is a non-$FF byte
    loop

    if
      ." :10" i hex. ." 00" \ Write record-intro with 4 digits.
      $10                   \ Begin checksum
      i    $FF and +        \ Sum the address bytes
      i >< $FF and +        \ separately into the checksum

      i #16 + i do
        i c@ chex. \ Print data with 2 digits
        i c@ +     \ Sum it up for Checksum
      loop

      negate chex.  \ Write Checksum
      cr
    then

  #16 +loop
  ." :00000001FF" cr
;
