URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [examples/] [event.as] - Rev 133
Compare with Previous | Blame | View Log
/**************************** events.as ************************************** Author: Agner Fog* date created: 2018-03-23* Last modified: 2018-03-23* Version: 1.00* Project: ForwardCom example* Description: Test event handler system** Copyright 2018 GNU General Public License http://www.gnu.org/licenses******************************************************************************/// define event IDs%EVT_CONSTRUCT = 1 // call static constructors and initialization procedures before calling main%EVT_DESTRUCT = 2 // call static destructors and clean up after return from main%EVT_MY_DEF = 0x100 // arbitrary user-defined event id%KEY = 1 // arbitrary key for my event%DEFAULT_PRIORITY = 0x1000 // default event priority/*Event records must be stored in a special read-only section with attribute 'event_hand'.These records will be reordered and combined by the linker.Each record contains:1. A scaled relative pointer to the event handler function2. Event priority3. Key4. Event ID*/events section read event_handint32 (constructor-__ip_base)/4, DEFAULT_PRIORITY, 0, EVT_CONSTRUCTint32 (destructor-__ip_base)/4, DEFAULT_PRIORITY, 0, EVT_DESTRUCTint32 (event1-__ip_base)/4, DEFAULT_PRIORITY, KEY, EVT_MY_DEFint32 (event2-__ip_base)/4, DEFAULT_PRIORITY+1, KEY, EVT_MY_DEFevents endconst section read ip // read-only data section// char strings with terminating zerohello: int8 "\nMain called", 0constr: int8 "\nTesting event handler system:\n\n\nConstructor called", 0destr: int8 "\nDestructor called\n\n", 0texte1: int8 "\nEvent handler 1 called", 0texte2: int8 "\nEvent handler 2 called (higher priority)", 0const endcode section execute align = 4 // code sectionextern __ip_base: ip // reference pointextern _puts: function // library function: write string to stdoutextern _raise_event: function // library function: make an event_main function public // main is the program entry called by the startup codeint64 r0 = address([hello]) // calculate address of stringcall _puts // call puts: write string pointed to by r0// now create an event with key 1 and event ID = EVT_MY_DEFint64 r0 = KEY + (EVT_MY_DEF << 32) // constant expression calculated by assemblercall _raise_event // make the eventint64 r0 = 0 // program return valuereturn_main end// this function is assigned to the EVT_CONSTRUCT event to be called before mainconstructor functionint64 r0 = address([constr]) // calculate address of stringcall _puts // call puts. parameter is in r0int64 r0 = 1 // return 1 to allow further event handlersreturnconstructor end// this function is assigned to the EVT_DESTRUCT event to be called after maindestructor functionint64 r0 = address([destr]) // calculate address of stringcall _puts // call puts. parameter is in r0int64 r0 = 1 // return 1 to allow further event handlersreturndestructor end// this function is assigned to the user-defined EVT_MY_DEF event to be called at this custum eventevent1 functionint64 r0 = address([texte1]) // calculate address of stringcall _puts // call puts. parameter is in r0int64 r0 = 1 // return 1 to allow further event handlersreturnevent1 end// this function is also assigned to the user-defined EVT_MY_DEF, but with a higher priority. It will be called firstevent2 functionint64 r0 = address([texte2]) // calculate address of stringcall _puts // call puts. parameter is in r0int64 r0 = 1 // return 1 to allow further event handlersreturnevent2 endcode end
