State machines and pointer of functions with SDCC

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](); } }

Reply to
Fred
Loading thread data ...

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)

"Fred" schrieb im Newsbeitrag news:41e3d7c1$0$14275$ snipped-for-privacy@news.zen.co.uk...

Reply to
Mouarf

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?

where

Reply to
Fred

as

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

Meindert

Reply to
Meindert Sprang

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

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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

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!

Reply to
Fred

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.

Reply to
nospam

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.