State machines and pointer of functions with SDCC

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I've tried this and would be grateful for any pointers (excuse pun) where
I'm going wrong.  This is what I've tried but doesn't work.  FYI this is for
the DS80C400.
Thanks in advance.

***************************************
typedef enum { FUN1, FUN2, FUN3} state_type;

state_type current_state;

void (*state_table [] ) () = { fun1, fun2, fun3};

void fun1 (void)
{   printf("\nFun1");   current_state = FUN2; }

void fun2 (void)
{   printf("\nFun2");   current_state = FUN3; }

void fun3 (void)
{   printf("\nFun3");   current_state = FUN1; }

void main (void)
{
   current_state = FUN1;
   while(1)
   {   state_table[current_state](); }
}



Re: State machines and pointer of functions with SDCC
it works by me but when moving void (*state_table [] ) () = { fun1, fun2,
fun3}; line after funx functions or by adding prototypes before.
Compiled with DevC++ on x86 (notice that main function must return an
integer for ANSI compliant code)


Quoted text here. Click to load it



Re: State machines and pointer of functions with SDCC
Many thanks for your post.  I forgot I had DevC++ on my machine and got it
going after the mods you suggested.

However under SDCC it still will not produce the correct result and stops as
soon as the "pointed" function is called.

Although the DS80C400 is an 8051 the compiler produces Von Neumann code and
has an extended addressing which is a linear 24 bits.  The device has some
registers and code extensions to suit.  I suppose it's not entirely
surprising that there's a hiccup here.  Just that I would have thought such
a sample prog should have worked.

Has anyone else got pointers to functions to work with the DS80C400 or the
DS80C390?


Quoted text here. Click to load it
where



Re: State machines and pointer of functions with SDCC
Quoted text here. Click to load it
as

Perhaps you have to add a reserved word to the array declaration to tell the
compiler that the pointers are code addresses.

Meindert



Re: State machines and pointer of functions with SDCC
Quoted text here. Click to load it

With the following minor changes (some for my own satisfaction) it
works.  Note the addition of stdio.h, return 0 from main, movement
of state_table to a point where its components are defined, etc.

You should turn up the warning level on your compiler.

#include <stdio.h>

typedef enum {FUN1, FUN2, FUN3} state_type;

state_type fun1(void)
{   printf("\nFun1");   return FUN2; }

state_type fun2(void)
{   printf("\nFun2");   return FUN3; }

state_type fun3(void)
{   printf("\nFun3");   return FUN1; }

state_type (*state_table [] ) () = {fun1, fun2, fun3};

int main(void)
{
   int        i;
   state_type current_state;

   current_state = FUN1;
   for (i = 0; i < 10; i++) {
      current_state = state_table[current_state]();
   }
   return 0;
}

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: State machines and pointer of functions with SDCC

Quoted text here. Click to load it

Works fine with DevC++ and as it expected worked fine.

The SDCC compiler with the DS80C libraries also requires:
#include "reg400.h"

It still doesn't get further than the first call.  There aren't many flags
for the SDCC compiler which produce more helpful messages!



Re: State machines and pointer of functions with SDCC

Quoted text here. Click to load it

You could try looking at the generated code?

Unless there is some particular reason to use an array of function pointers
to run a state machine I would forget it and just switch on the state
variable. On a 51 it will be faster and probably smaller.


Site Timeline