From 67d25d837ac55f28a366c0a3b262e439a6e75fc3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 19 Aug 2017 12:15:28 +0200 Subject: Add AmForth --- amforth-6.5/common/lib/fsm.frt | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 amforth-6.5/common/lib/fsm.frt (limited to 'amforth-6.5/common/lib/fsm.frt') diff --git a/amforth-6.5/common/lib/fsm.frt b/amforth-6.5/common/lib/fsm.frt new file mode 100644 index 0000000..94af976 --- /dev/null +++ b/amforth-6.5/common/lib/fsm.frt @@ -0,0 +1,67 @@ +\ FSM routines, from a paper presented by J . Noble----- +\ --------------------------------------------------------- +\ you can find the paper at http://www.forth.org/literature/noble.html +\ The fsm: word has been modified to correctly increment addresses by +\ word, instead of byte. and to also use the i@ dictionary fetch word. +\ Ver 1.1 fsm: now implements "The best FSM so far" (see paper). State +\ is now a heap variable with it's address in the dictionary of the fsm. +\ Written by Bernard Mentink + +marker _fsm_ + +hex + +\ dummy nop word for readability +: wide ; + +\ perform word that executes vector +: perform + @i execute ; + +\ Get 2 cells from dictionary space +: 2@i + dup 1+ @i swap @i ; + +\ comment if you have this word. +: tuck swap over ; + +\ Allocate a RAM variable on the data space, init with x, and return RAM address +: >ram ( x -- addr ) here 1 cells allot tuck ! ; + + +\ This word creates FSM transition tables +: fsm: ( width -- ) + create 0 >ram , , ] \ ram addr of state stored in dict,also width. + does> ( col# adr -- ) + dup dup >r 2@i @ * 2* + ( -- col#+width*state ) + swap 2* 1+ 1+ + ( -- offset-to-action) + dup >r ( -- offset-to-action) + perform ( ? ) + r> 1+ ( -- offset-to-update) + perform ( -- state') + r> @i ! ; \ update state + + +\ ......... some test code .............. +\ un-comment the code lines to test state changes dependant on input supplied +\ e.g 2 test_fsm, 0 test_fsm etc +\ If you want the address of the state variable associated with your state +\ machine, create the following word : mystate ['] test_fsm 1+ i@ ; + +\ : one ." one " ; +\ : two ." two " ; +\ : three ." three " ; +\ : four ." four " ; +\ : nop ." nop " ; + +\ 0 constant >0 +\ 1 constant >1 +\ 2 constant >2 + +\ a test state-machine table +\ 4 wide fsm: test_fsm +\ input: | 0 | 1 | 2 | 3 | +\ state: --------------------------------------------- +\ ( 0 ) nop >0 one >1 one >1 two >2 +\ ( 1 ) four >1 one >1 nop >1 two >2 +\ ( 2 ) nop >2 two >2 nop >2 nop >2 ; -- cgit v1.2.3