| 1 |
133 |
Agner |
/**************************** events.as *************************************
|
| 2 |
|
|
* Author: Agner Fog
|
| 3 |
|
|
* date created: 2018-03-23
|
| 4 |
|
|
* Last modified: 2018-03-23
|
| 5 |
|
|
* Version: 1.00
|
| 6 |
|
|
* Project: ForwardCom example
|
| 7 |
|
|
* Description: Test event handler system
|
| 8 |
|
|
*
|
| 9 |
|
|
* Copyright 2018 GNU General Public License http://www.gnu.org/licenses
|
| 10 |
|
|
******************************************************************************/
|
| 11 |
|
|
|
| 12 |
|
|
// define event IDs
|
| 13 |
|
|
%EVT_CONSTRUCT = 1 // call static constructors and initialization procedures before calling main
|
| 14 |
|
|
%EVT_DESTRUCT = 2 // call static destructors and clean up after return from main
|
| 15 |
|
|
%EVT_MY_DEF = 0x100 // arbitrary user-defined event id
|
| 16 |
|
|
%KEY = 1 // arbitrary key for my event
|
| 17 |
|
|
%DEFAULT_PRIORITY = 0x1000 // default event priority
|
| 18 |
|
|
|
| 19 |
|
|
/*
|
| 20 |
|
|
Event records must be stored in a special read-only section with attribute 'event_hand'.
|
| 21 |
|
|
These records will be reordered and combined by the linker.
|
| 22 |
|
|
Each record contains:
|
| 23 |
|
|
1. A scaled relative pointer to the event handler function
|
| 24 |
|
|
2. Event priority
|
| 25 |
|
|
3. Key
|
| 26 |
|
|
4. Event ID
|
| 27 |
|
|
*/
|
| 28 |
|
|
events section read event_hand
|
| 29 |
|
|
int32 (constructor-__ip_base)/4, DEFAULT_PRIORITY, 0, EVT_CONSTRUCT
|
| 30 |
|
|
int32 (destructor-__ip_base)/4, DEFAULT_PRIORITY, 0, EVT_DESTRUCT
|
| 31 |
|
|
int32 (event1-__ip_base)/4, DEFAULT_PRIORITY, KEY, EVT_MY_DEF
|
| 32 |
|
|
int32 (event2-__ip_base)/4, DEFAULT_PRIORITY+1, KEY, EVT_MY_DEF
|
| 33 |
|
|
events end
|
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
const section read ip // read-only data section
|
| 37 |
|
|
// char strings with terminating zero
|
| 38 |
|
|
hello: int8 "\nMain called", 0
|
| 39 |
|
|
constr: int8 "\nTesting event handler system:\n\n\nConstructor called", 0
|
| 40 |
|
|
destr: int8 "\nDestructor called\n\n", 0
|
| 41 |
|
|
texte1: int8 "\nEvent handler 1 called", 0
|
| 42 |
|
|
texte2: int8 "\nEvent handler 2 called (higher priority)", 0
|
| 43 |
|
|
const end
|
| 44 |
|
|
|
| 45 |
|
|
code section execute align = 4 // code section
|
| 46 |
|
|
|
| 47 |
|
|
extern __ip_base: ip // reference point
|
| 48 |
|
|
extern _puts: function // library function: write string to stdout
|
| 49 |
|
|
extern _raise_event: function // library function: make an event
|
| 50 |
|
|
|
| 51 |
|
|
_main function public // main is the program entry called by the startup code
|
| 52 |
|
|
int64 r0 = address([hello]) // calculate address of string
|
| 53 |
|
|
call _puts // call puts: write string pointed to by r0
|
| 54 |
|
|
// now create an event with key 1 and event ID = EVT_MY_DEF
|
| 55 |
|
|
int64 r0 = KEY + (EVT_MY_DEF << 32) // constant expression calculated by assembler
|
| 56 |
|
|
call _raise_event // make the event
|
| 57 |
|
|
|
| 58 |
|
|
int64 r0 = 0 // program return value
|
| 59 |
|
|
return
|
| 60 |
|
|
_main end
|
| 61 |
|
|
|
| 62 |
|
|
// this function is assigned to the EVT_CONSTRUCT event to be called before main
|
| 63 |
|
|
constructor function
|
| 64 |
|
|
int64 r0 = address([constr]) // calculate address of string
|
| 65 |
|
|
call _puts // call puts. parameter is in r0
|
| 66 |
|
|
int64 r0 = 1 // return 1 to allow further event handlers
|
| 67 |
|
|
return
|
| 68 |
|
|
constructor end
|
| 69 |
|
|
|
| 70 |
|
|
// this function is assigned to the EVT_DESTRUCT event to be called after main
|
| 71 |
|
|
destructor function
|
| 72 |
|
|
int64 r0 = address([destr]) // calculate address of string
|
| 73 |
|
|
call _puts // call puts. parameter is in r0
|
| 74 |
|
|
int64 r0 = 1 // return 1 to allow further event handlers
|
| 75 |
|
|
return
|
| 76 |
|
|
destructor end
|
| 77 |
|
|
|
| 78 |
|
|
// this function is assigned to the user-defined EVT_MY_DEF event to be called at this custum event
|
| 79 |
|
|
event1 function
|
| 80 |
|
|
int64 r0 = address([texte1]) // calculate address of string
|
| 81 |
|
|
call _puts // call puts. parameter is in r0
|
| 82 |
|
|
int64 r0 = 1 // return 1 to allow further event handlers
|
| 83 |
|
|
return
|
| 84 |
|
|
event1 end
|
| 85 |
|
|
|
| 86 |
|
|
// this function is also assigned to the user-defined EVT_MY_DEF, but with a higher priority. It will be called first
|
| 87 |
|
|
event2 function
|
| 88 |
|
|
int64 r0 = address([texte2]) // calculate address of string
|
| 89 |
|
|
call _puts // call puts. parameter is in r0
|
| 90 |
|
|
int64 r0 = 1 // return 1 to allow further event handlers
|
| 91 |
|
|
return
|
| 92 |
|
|
event2 end
|
| 93 |
|
|
|
| 94 |
|
|
code end
|