Bitwise operation for state machine


 
Thread Tools Search this Thread
Top Forums Programming Bitwise operation for state machine
# 1  
Old 12-24-2013
Bitwise operation for state machine

Hello All,
I am writing basic state machine which maintains 8 different states and there is posibility that system may be in multiple states at a time (Except for state1 to state3. menas only once state can be active at a time from state1 to state3).
I have declared enum like one below

Code:
enum
{
   state1 1
   state2 1 << 1
   state3 1 << 2
   state4 1 << 4
    ....
    ....
}

Now my requirement is that state1 to state3 can not be active at a given instance.
Example if machine is in state1 then; state variabale would be -->
Code:
 xxxx x001

... then when system changes its state to to state3 ... state variable should become
Code:
xxxx x100.

.

Note :- "x'" indicates other state variable which should not get affected because of above operations.

Thank you very much in advance.

Regards,
Anand Shah
# 2  
Old 12-24-2013
So you have a variable (e.g. state) that presumably should always have no more than one of the three low order bits set. (Or, maybe you mean it should always have exactly one of the three low order bits set. Your specification is ambiguous as to which of these you mean.)

How will you tell your machine to change state?
Will you call a function to change the state? If so, why is that function not just an assignment to state? Is it that you want to verify that the requested new state has no more than one (or exactly one) of the three low order bits set?

Maybe you want a function that takes two arguments, states to add to state and states to remove from state.

What language are you using? (I could guess C, but it isn't the only language that includes enums.)

What have you tried so far?
# 3  
Old 12-24-2013
Dear Don Cragun,
Thank you very much for your reply.
I am using C macro to accomplish this. I can not pass states to remove from
Code:
state

as I am not aware of previous state. What I want is their should always have no more than one of the three low order bits set.
Example :- If I am in
Code:
state1

currently then my state variable would be
Code:
 xxxx x001

. Now If I want to set state to
Code:
state3

then my state variable should become
Code:
 xxxx x100.

Thank you.
# 4  
Old 12-24-2013
Quote:
Originally Posted by anand.shah
Dear Don Cragun,
Thank you very much for your reply.
I am using C macro to accomplish this. I can not pass states to remove from
Code:
state

as I am not aware of previous state. What I want is their should always have no more than one of the three low order bits set.
Example :- If I am in
Code:
state1

currently then my state variable would be
Code:
 xxxx x001

. Now If I want to set state to
Code:
state3

then my state variable should become
Code:
 xxxx x100.

Thank you.
This makes no sense. If your program isn't aware of your current state (previous state when you want to change to a new set of states), then setting the new state is just a statement like:
Code:
state = state1;
    or
state = state3 | state4 | state7;

If you can't restrain yourself from writing code like:
Code:
state = state1 | state2;

and you aren't going to set up a function to perform verification of state changes, I have no idea what you are trying to do. And, doing this, there are never any unknown (or unchanged states); you replace (rather than update) states every time to assign a new value to state. So you won't have xxxx x100, you'll have 0000 0001, 0100 1100, or (the undesired) 0000 0011, respectively, for the three samples above; but never any xs.

I must be missing something in your description of what you're trying to do.
# 5  
Old 12-24-2013
Dear ,
I am really sorry for my idiot reply in last post.
Yes I store the last state which I am exposing to my application through another macro.
Thus there are basically two macros each for saving the state and getting the current state.
# 6  
Old 12-24-2013
Quote:
Originally Posted by anand.shah
Dear ,
I am really sorry for my idiot reply in last post.
Yes I store the last state which I am exposing to my application through another macro.
Thus there are basically two macros each for saving the state and getting the current state.
OK. So, show us the macros! And, tell us what you want each of those macros to do that they are not doing now.
# 7  
Old 12-24-2013
Here are the macros :-
Code:
#define INA_SAVE_APP_STATE(STATE)  appState = (appState | STATE); 
#define INA_GET_APP_STATE() appState

The problem with above macro is that when i switch from
Code:
state1

to
Code:
state3

; state variable becomes
Code:
xxxx x101

that means both
Code:
state1

and
Code:
state3

are active where as what I want is when I switch from
Code:
state1

to
Code:
state3

state variable should be
Code:
xxxx x100

meaning only
Code:
state3

is active
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vim function to generate RTL Code(finite state machine) in verilog

Hi I wanted to call the AutoFsm function (given below) in vim to generate a code something like: **********verilog code to generate ************* always @(posedge clk or negedge rst_n) begin if(!rst_n) begin state_r <= #1 next_stateascii_r; ... (0 Replies)
Discussion started by: dll_fpga
0 Replies

2. AIX

Open firmware state to running state

Hi Admins, I am having a whole system lpar in open firmware state on HMC. How can I bring it to running state ? Let me know. Thanks. (2 Replies)
Discussion started by: snchaudhari2
2 Replies
Login or Register to Ask a Question