• Dynamic declarations

    From niclash@21:1/5 to I can not on Tue Dec 26 15:50:17 2023
    Hi, everyone.

    I am using Forth on a piece of hardware that I have constructed. It has a "motherboard" with 0..N slots (connectors on a bus), where slot 0 is for the controlling microcontroller, and each other slot is to interface with the real world, such as Pt1000
    sensors, 0-10V, 0-20mA, Triac outputs and so on.

    So the microcontroller shouldn't need to know in advance how to "handle" these expansion boards, and shouldn't need "upgrade" when a new type of expansion board comes into existance. And therefor the idea is to have a Forth program sitting in an EEPROM
    on each expansion board, containing 4 words each;

    : (init) Called after each RESET of the board.

    : (tick) Called periodically (~20ms) and used for scanning hardware and such

    : (read) Reads and formats the inputs and places the result in buffers to be sent by LoraWAN

    : (write) Takes values arrived over LoraWAN and make use of them to output to hardware (in (tick) )

    So, the programs are executed by the main (slot0) microcontroller, on behalf of the expansion board.


    Now, to keep it simple, I thought I just store the Forth source code in the EEPROM on the expansion board, read out the string and call "evaluate" and in principle that works.

    BUT, I can not write to program that executes the Forth words that are in the eeprom, since that doesn't exist yet. Here was my first attempt at this;


    : slot-powerup ( slot# -- ) Called on Colibri powerup for each slot, ONLY ONCE.
    dup slot-on 1 delay Power up the slot, wait
    dup slot-reset 1 delay RESET cycle
    dup slot-select Select slot

    1 block-read read the EEPROM
    if .yellow< ." EEPROM not present in slot. " >.

    2dup #1024 evaluate execute forth code from EEPROM. EEPROM should have been filled with SPACE at end.
    if swap ." Error interpreting EEPROM in slot " . free exit then EEPROM program must have ( slot# c-addr len -- err? )

    dup 1- cells
    attach hooks
    ' (init) over cells slot-init + !
    ' (tick) over cells slot-tick + !
    ' (read) over cells slot-read + !
    ' (write) over cells slot-write + !

    slot-init @ execute call "init" for slot
    ( slot# channels )

    ;

    (Variant; MECRISP Forth for STM32)

    Basically, (init), (tick), (read) and (write) are "not found", and even if I "pre-define" them, this snippet of code will be pointing to the old ones, not the words that were just read out from the EEPROM.


    So, can someone think of any smarter way to do this, before I try to manually walk the dictionary to locate the word I am looking for?

    Thanks in advance for any suggestions

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to niclash on Tue Dec 26 17:17:53 2023
    niclas@hedhman.org (niclash) writes:
    So the microcontroller shouldn't need to know in advance how to "handle" these expansion boards, and shouldn't need "upgrade" when a new type of expansion board comes into existance. And therefor the idea is to have a Forth program sitting in an EEPROM
    on each expansion board, containing 4 words each;

    This looks like a very similar problem to the one that was solved with
    Open Firmware, so you might want to take a look at that. Note that
    Open Firmware uses a tokenized form of source code, but from what I
    read, it is still source code.

    BUT, I can not write to program that executes the Forth words that are in the eeprom, since that doesn't exist yet. Here was my first attempt at this;


    : slot-powerup ( slot# -- ) Called on Colibri powerup for each slot, ONLY ONCE.
    dup slot-on 1 delay Power up the slot, wait
    dup slot-reset 1 delay RESET cycle
    dup slot-select Select slot

    1 block-read read the EEPROM
    if .yellow< ." EEPROM not present in slot. " >.

    2dup #1024 evaluate execute forth code from EEPROM. EEPROM should have been filled with SPACE at end.
    if swap ." Error interpreting EEPROM in slot " . free exit then EEPROM program must have ( slot# c-addr len -- err? )

    dup 1- cells
    attach hooks
    ' (init) over cells slot-init + !
    ' (tick) over cells slot-tick + !
    ' (read) over cells slot-read + !
    ' (write) over cells slot-write + !

    slot-init @ execute call "init" for slot
    ( slot# channels )

    ;

    (Variant; MECRISP Forth for STM32)

    Basically, (init), (tick), (read) and (write) are "not found", and even if I "pre-define" them, this snippet of code will be pointing to the old ones, not the words that were just read out from the EEPROM.


    So, can someone think of any smarter way to do this, before I try to manually walk the dictionary to locate the word I am looking for?

    One way would be to use SEARCH-WORDLIST, FIND-NAME, FIND, or the like
    in SLOT-POWERUP.

    Alternatively, the EEPROM could contain source code that leaves the
    xts of the four words you want on the stack, in addition to the other
    values. Then the code above would look like

    ...
    2dup #1024 evaluate ( xt-write xt-read xt-tick xt-init ... err? )
    if swap ." Error interpreting EEPROM in slot " . free exit then

    dup 1- cells
    attach hooks >r ( xt-write xt-read xt-tick xt-init R: slot# )
    r@ cells slot-init + !
    r@ cells slot-tick + !
    r@ cells slot-read + !
    r@ cells slot-write + !
    r>
    ...

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2023: https://euro.theforth.net/2023

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From niclash@21:1/5 to Anton Ertl on Tue Dec 26 20:15:43 2023
    Anton Ertl wrote:


    Alternatively, the EEPROM could contain source code that leaves the
    xts of the four words you want on the stack, in addition to the other
    values. Then the code above would look like

    Ahhhh! Why didn't I think of that!

    SOLVED! Thanks.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)