Menu

Pointers to Functions, Jump Table, IndCall command, State / Event dispatcher

2016-01-24
2016-01-24
  • Simon Whittam

    Simon Whittam - 2016-01-24

    ~~~~

    '''A demonstration program for GCGB and GCB.
    '''--------------------------------------------------------------------------------------------------------------------------------
    '''This program demonstrates one way of implementing pointers to pseudo
    ''' Sub/Functions accessed via a Jump Table using the IndCall command.
    '''
    '''The code shown, uses the example of a State/Event machine.
    '''
    '''@author SimonW
    '''@licence GPL
    '''@version 1.0
    '''@date 24.01.2016
    '''**************

    ; ----- Configuration

    #chip tiny85
    ' #config :'[todo]

    ' #include :'[todo]

    ; ----- Constants

    #define EventsPerState = 2

    ; ----- Define Hardware settings
    ' [todo]

    ; ----- Variables
    DIM StateEventPointer AS Word
    DIM State AS Byte
    DIM Event AS Byte
    DIM MyCount AS Byte

    MyCount = 1

    ; ----- Quick Command Reference:
    ' [todo]

    ; ----- Main body of program commences here.
    State = 1
    Event = 2
    StateEventPointer = SetStateEventPointer(State, Event)
    IndCall StateEventPointer

    State = 2
    Event = 2
    StateEventPointer = SetStateEventPointer(State, Event)
    IndCall StateEventPointer

    State = 1
    Event = 1
    StateEventPointer = SetStateEventPointer(State, Event)
    IndCall StateEventPointer

    State = 2
    Event = 1
    StateEventPointer = SetStateEventPointer(State, Event)
    IndCall StateEventPointer

    End

    StateEventTable:
    GOTO State1Event1
    GOTO State1Event2

    GOTO State2Event1
    GOTO State2Event2

    ; ----- Support methods. Subroutines and Functions
    State1Event1:
    MyCount = MyCount + 1
    Return

    State1Event2:
    ;MyCount = MyCount + 2
    Return

    State2Event1:
    MyCount = MyCount + 4
    Return

    State2Event2:
    MyCount = MyCount + 8
    Return

    Function SetStateEventPointer(IN State, IN Event)
    ; Calculate address within StateEventTable to jump to.
    SetStateEventPointer = @StateEventTable + ((State-1) * EventsPerState) + (Event-1)
    END Function
    ~~~~~

     

    Last edit: Anobium 2016-01-24
  • Anobium

    Anobium - 2016-01-24

    That is clever. What is the application ?

     
  • Simon Whittam

    Simon Whittam - 2016-01-24

    I've created state/event machines in assembler to handle events from timers and switches, etc.
    Fed the events into a First In First Out circular buffer and controlled I/O etc within a DO WHILE loop.

    Has the advantage most of the time that you can free the processor to do other tasks instead of hogging all the processor resource.

    One can use an idle event to complete other tasks, ie. sending out state/event debugging info via a serial port while waiting for events from the main application.

    I'm in the process of trying to implement it GCB.

    It's taken a while to try and figure out how do it with GCB as I'm new to this compiler.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.