Special Features of PicForth

String and Numeric Output

PicForth was developed for producing sophisticated applications involving string and numeric output to an LCD or other display. Therefore ." saves a string to a special table (at the end of program memory) and emits a call to TYPE. TYPE calls the word EMIT to output to the display, so EMIT must be defined before TYPE can be called. EMIT is crafted by the user to display to his particular hardware. Similarly, . (dot) also uses EMIT for output.

As an example of using ." and . (dot) let's assume that to display a character we simply write to the serial port.

\ Declare the important emit function.

code emit
; ( c --)
        periphbank BANK0

        ; poll TRMT to see if we can send
__POLL_TRMT
        btfss TXSTA, TRMT
        goto __POLL_TRMT      ; Bit not set, so loop.

        ; Send the byte.
        movfp TLOW, TXREG     ; Copy to the transmit register.
        call _poptop          ; Pop the top cell off the stack.
        return
endcode


fload disp.f         \ This loads the TYPE word.

: .intro
        ." PicForth is #" 1 . ;
After invoking .intro, the output of the serial port will be :

PicForth is #1

 

Using an External Rom for String Memory (Advanced Topic)

If program memory is at a premium, it is possible to place strings into an external rom (specifically it was designed to program the strings into a serial eeprom.) The compiler generates files that can be used to program another PIC to program the strings into the rom. The compiler also generates the appropriate calls in the main PIC to output the string from the rom to the display.

Setting this up is a bit more complicated than simply programming the strings into program memory, but if you run out of program memory, there may be no alternative. First the main program is compiled with the -r option. The compiler generates some special files to enable assembly and compilation of the setup program:
 
stringsp.f: routine to program the strings
strindex.inc: a string index that is also programmed into the rom
strbase.inc: defines the base of the string index
Next the setup program is compiled and programmed into a PIC MCU, which is used to program the eeprom. There is no reason that these files cannot be used to program other types of roms, but that has not been tested.

Example of The Setup Program

\ Fload all the necessary libraries.
\ ..... 
\ This file loads routines to type the strings in the rom.
fload romtype.f      
\ This file loads routines to program the strings into the rom.
fload romsetup.f     
\ This compiler generated file defines the routine program_strings.
fload stringsp.f     

\ Declare the main program.
: main
        program_strings  \ Programs the strings into the rom.
        
        \ Test that the strings are properly programmed.
        page ." Testing" CR
             ." 20 strings" CR 1000 MS
        
        20 0 do
           CR I . space
           I rtype 1000 MS
        loop

        begin again     \ Infinite loop.
;
 

Using Both External and Internal Strings (Advanced Topic)

The word PStrings declares that strings should by default be in program memory. RStrings declares that they should be in external rom memory. Using P." instead of ." explicitly puts a string into program memory, R." puts it into external rom memory.
: someword
   RStrings    \ Set default to strings in rom.

        P." This string goes in program memory" CR
        R." This string in external rom" CR
        ." This goes into rom as well"
;