Finite State Machines Implementation
bmentink
Posts: 107
Hi All,
I would like to implement finite state-machine execution tables in Spin, but I see no way of getting pointers to methods, does this mean this is not possible?
What I would like to implement is the following:
A 2-dimentional table who's horiz axis is indexed by inputs 0..n, and who's vertical axis is states 0..n.
Each cell in the table would hold a pointer to an output method and a pointer to a transition method, a FSM method given an input event would look up the correct
cell in the table and execute the output method then the transition method (which would update the state variable).
This sort of efficient FSM handling is easy to do in Forth (You can create a compile word that creates FSM tables .. see Julian Noble's paper)
but if we can't get pointers to methods, then I believe this is a serious deficiency in spin.
Maybe someone knows how this can me achieved in assembly?
Thanks.
I would like to implement finite state-machine execution tables in Spin, but I see no way of getting pointers to methods, does this mean this is not possible?
What I would like to implement is the following:
A 2-dimentional table who's horiz axis is indexed by inputs 0..n, and who's vertical axis is states 0..n.
Each cell in the table would hold a pointer to an output method and a pointer to a transition method, a FSM method given an input event would look up the correct
cell in the table and execute the output method then the transition method (which would update the state variable).
This sort of efficient FSM handling is easy to do in Forth (You can create a compile word that creates FSM tables .. see Julian Noble's paper)
but if we can't get pointers to methods, then I believe this is a serious deficiency in spin.
Maybe someone knows how this can me achieved in assembly?
Thanks.
Comments
Then use a CASE to call the routine assigned to the used integer value.
regards peter
That method is very messy, slow and hard to follow , I am looking to do this direct with method pointers. I want to create a table like this:
Input: | 0 | 1 | 2 |
state:
0 out1 -->2 out4 -->0 out2 -->1
1 out1 -->1 out6 -->0 out0 -->2
2 etc
The "outn" methods should be more descriptive names, the "-->n" are descriptive method names showing what state is transitioned to for that combination of input and current state.
Hope this is clear, you can't represent this "self documenting" clear table with integers and a case statement.
Cheers,
Bernard
In spin, you must emulate those pointers with integer values. Each value
represents a method. The code that reads the table, uses the integer value
to call the appropiate method.
In assembly you can of course store the address of a method,·and
call that address using selfmodifying code.
regards peter
All objects in such an example of course will require a method named function. I've never actually tested this mainly for having no real reason to try [noparse]:)[/noparse] Maybe you can follow up with results if you try it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
In this way it would be quite easy to have·state-machine type operation in one cog (the JDForth cog) and still have your spin objects in the other cogs.
If this comes up often enough, then it may be worthwhile extending the compiler to recognise the word ,' or the 16 bit variant H,' which could then be used to make the state table at compile time. As per:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Carl Jacobs
JDForth - Forth to Spin Compiler http://www.jacobsdesign.com.au/software/jdforth/jdforth.php·
Your implementation is quit messy still and not flexible enough to have the transition execute code as well as changing state. I have implemented Julian Nobles FSM for amforth, it works like this:
The transition words ">0" etc don't have to be constants, they can be normal 4th words performing some action, as long as they leave a state variable on the stack.
Hey Carl, when are you going to make JDForth a proper forth interpreter/compiler? I would be the 1st one to use it then. It is the interactiveness of forth that I like, being able to
interactively test words as you build up to the final word, that and being able to extend the compiler is Forth's main strength IMHO.
Jazzed, your example makes absolutely no sense to me at all, sorry!
I'm sorry that you didn't notice that my implementation does execute code as well as change state. Although the code you've presented looks "clean", you have not shown your word fsm: which would have in essence the same functionality performed by my word next_state - It really just moves the "messiness" (or algorithm)·from one place to another.
I'm not planning at any stage to turn the compiler into a interpreting compler, but I am planning to release a version later this year that allows the compilation of interprative environments. Personally I probably wont use the interprative environment - the edit-compile-run cycle being more than quick enough for me (even with just spin).
I guess my primary goal is a faster language that allows easy inclusion of PASM words without consuming extra cogs. It's probably not going to please·everyone, but it has been designed·as a direct result of the needs that I've personally experienced while programming with the Propeller chip.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Carl Jacobs
JDForth - Forth to Spin Compiler http://www.jacobsdesign.com.au/software/jdforth/jdforth.php·
Still missing it. In your "init_state_table" word and the line "[noparse][[/noparse]'] out1 2 --> "
the "2" seems to be a number not a function. In my example the >2 is a function (forth word), this allows exit code to be written before changing state,
this is a function that is called " in addition" to the action word (in your example out1).
I didn't include the fsm: code, because I didn't think it was relevant since it was written for amforth, but here it is:
I'm sorry, I did misread your initial post. Oh well, I guess it could be written as:
If placed in a file, most of the words could be surrounded with <PRIVATE PRIVATE>, and the internal mechanism of the state-machine could then be protected. The shortcut word proposed in my earlier post would·still be of benefit, and it would be possible to include the width and current state of the state machine as part of state_table at the expense of slightly more tricky control words.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Carl Jacobs
JDForth - Forth to Spin Compiler http://www.jacobsdesign.com.au/software/jdforth/jdforth.php·
http://obex.parallax.com/object/816
The declaration syntax is a bit clunky, but it works. Here is an example state from the Soda Machine demo in the object archive:
S15
word "##!##", FN_showDeposit, 15
word "##NICKEL##", @S20
word "##DIME##", @S25
word "##QUARTER##", @S25, FN_refundDime, "%%", FN_refundNickel
word "@@"
S20
word "##!##", FN_showDeposit, 20
word "##NICKEL##", @S25
word "##DIME##", @S25, FN_refundNickel
word "##QUARTER##", @S25, FN_refundDime, "%%", FN_refundDime
word "@@"
Hum, I have my own variant [post=1326203]here[/post]. State [post=1326389]table[/post] its built from.
... Tim