Problems/solutions with ftables

David McNab <[email protected]> Tue, 12 Oct 2004 02:53:06 +1300
Newsgroups gmane.comp.lang.forth.picforth
Message-ID <[email protected]>
Hi,

Just a few quick observations:

1. I've noticed that program-memory tables, created with 
ftable...end-table, only work if all the data in the table resides in 
the first 256 bytes of program memory. The ADDWF PCL,F trick used for 
indexing the table seems to set PCH to $00, regardless of the current IP.

2. If file1.fs includes file2.fs, and file2.fs contains an 
ftable..end-table construct, subsequent includes from file1.fs fail.

3. libstrings.fs doesn't work. Specifically, str-char is always 
returning $01. I've used exactly the code from the doco for 
'c".."'/'str-char'. No good.

Anyway, here's what I've done to work around these issues:

I'm working on a program that does lots of user interaction via buttons 
and LCD, and requires several strings to be stored. So I've chosen to 
put them in program memory.

What I've had to do is write a utility that automatically converts a 
file containing something like:

     fred = "hello, world"
     mary = "smaller"
     jim = "blah"

into:

   ftable mystrings
   $68 t, $65 t, $6c t, $6c t, $6f t, $2c t, $20 t, $77 t,
   $6f t, $72 t, $6c t, $64 t, 0 t, $62 t, $6c t, $61 t,
   $68 t, 0 t, $73 t, $6d t, $61 t, $6c t, $6c t, $65 t,
   $72 t, 0 t,
   end-table
   $0 constant fred
   $d constant jim
   $12 constant mary

Here, 'fred', 'jim' and 'mary' are offsets into this table, pointing to 
the start of null-terminated strings.

This allows any chosen offset to be passed to a loop, hardcoded to use 
'mystrings', which indexes this table and invokes a callback for each 
char in the string.

Finally, I've arrived at a way to enter strings in human-readable 
format, have them automatically translated to table decs, such that I 
can do stuff like:

   lcd-line1 fred lcd-put-string
   lcd-line2 mary lcd-put-string

And as long as I ensure that the whole table is residing in first 256 
bytes, all is ok. What's really great about ftable is that it takes 
advantage of the RETLW instruction, so that there is zero overhead for 
each string (apart from its null-terminator).

(Makes me wonder if i should hack up an encoder to store 2 7-bit ASCII 
chars in each machine word).

All this said, I find that overall, PicForth is a god-send for my PIC 
project. Between it and gpsim, I'm in PIC Hacking Heaven, especially 
after weaning myself off the insulting PICAXE BASIC environment. So 
thank you, Samuel, for a master-work! :))
I really hope you find the inspiration to keep going on it.

-- 
Cheers
David