home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Basic / MAXONB32.DMS / in.adf / Includes.lha / BH / BLib / HookEntryTask.bas < prev    next >
Encoding:
BASIC Source File  |  1994-05-12  |  3.4 KB  |  108 lines

  1. ''
  2. '' $Id: HookEntryTask.bas,v 1.6 1994/05/12 14:21:29 alex Rel $
  3. ''
  4. '' BASIC task creation function
  5. ''
  6. '' (c) Copyright 1994 HiSoft
  7. ''
  8.  
  9. 'REM $INCLUDE Exec.bh
  10. 'REM $INCLUDE Utility.bc
  11.  
  12. 'REM $INCLUDE BLib/ExecSupport.bas
  13.  
  14. ''
  15. '' CreateHookEntryTask
  16. ''
  17. ''    Create task with given name, priority, stacksize
  18. ''
  19. ''     NOTE: initPC _can_ be a BASIC entry point (VARPTRS(...))
  20. ''
  21. FUNCTION CreateHookEntryTask&(BYVAL tname&, BYVAL pri%, BYVAL initPC&, _
  22.   BYVAL stackSize&)
  23.     LOCAL tempmemlist%((Node_sizeof% + 3 + 3 * MemEntry_sizeof%) \ 2)
  24.     LOCAL newTask&, fakememlist&, ml&, junk&, mlme&, h&, tcb&
  25.     DIM    TaskHookEntry&(6)
  26.  
  27.     CreateHookEntryTask& = NULL&
  28.  
  29.     ' The guts of TaskHookEntry.s translated to a TAGLIST via OToTagList:
  30.     '
  31.     ' movea.l    (4).w,a6            ; connect to Exec
  32.     ' suba.l    a1,a1                ; find this task
  33.     ' CALLSYS    FindTask            ; go find the task
  34.     ' movea.l    d0,a6                ; the task control block pointer
  35.     ' lea        TC_Userdata(a6),a6    ; where we stashed the Hook
  36.     ' movea.l    (a6),a0                ; pointer to the Hook
  37.     ' move.l    a2,(a6)                ; clear out user data
  38.     ' move.l    h_Entry(a0),-(sp)    ; the Hook entry point
  39.     ' rts                            ; run the hook
  40.     '
  41.     IF TaskHookEntry&(0) = 0 THEN
  42.         TAGLIST VARPTR(TaskHookEntry&(0)), _
  43.             &H2C780004, &H93C94EAE, &HFEDA2C40, &H4DEE0058, &H20562C8A, &H2F280008, _
  44.             &H4E750000
  45.  
  46.         IF PEEKW(LIBRARY("exec.library") + LibNode + lib_Version) >= 37 THEN
  47.             ' CacheClearE is >= V37 only, without the flush this code will
  48.             ' certainly not work on a 68040 (but does 1.3 work on an '040?)
  49.  
  50.             CacheClearE VARPTR(TaskHookEntry&(0)), 28, CACRF_ClearI& OR CACRF_ClearD&
  51.         END IF
  52.     END IF
  53.  
  54.     stackSize& = (stackSize& + 3) AND NOT 3
  55.     fakememlist& = VARPTR(tempmemlist%(0))
  56.  
  57.     ' Allocate 3 entries in the Task MemList; the first is its Task structure, the
  58.     ' second its stack, and the third the Hook used to retrieve the BASIC runtime
  59.     ' context
  60.  
  61.     POKEW fakememlist& + Node_sizeof%, 3    ' number of entries
  62.  
  63.     POKEL fakememlist& + Node_sizeof% + 2 + meu_Reqs%, MEMF_PUBLIC& OR MEMF_CLEAR&
  64.     POKEL fakememlist& + Node_sizeof% + 2 + me_Length%, Task_sizeof%
  65.  
  66.     POKEL fakememlist& + Node_sizeof% + 2 + MemEntry_sizeof% + meu_Reqs%, MEMF_CLEAR&
  67.     POKEL fakememlist& + Node_sizeof% + 2 + MemEntry_sizeof% + me_Length%, stackSize&
  68.  
  69.     POKEL fakememlist& + Node_sizeof% + 2 + MemEntry_sizeof% * 2 + meu_Reqs%, MEMF_CLEAR&
  70.     POKEL fakememlist& + Node_sizeof% + 2 + MemEntry_sizeof% * 2 + me_Length%, Hook_sizeof
  71.  
  72.     ml& = AllocEntry&(fakememlist&)
  73.  
  74.     IF ml& = NULL& THEN EXIT FUNCTION
  75.  
  76.     mlme& = ml& + MemList_sizeof%    ' address of the first MemEntry
  77.  
  78.     newTask& = PEEKL(mlme& + meu_Addr%)
  79.  
  80.     ' set up the stack
  81.     POKEL newTask& + tc_SPLower%, PEEKL(mlme& + MemEntry_sizeof% + meu_Addr%)
  82.     POKEL newTask& + tc_SPUpper%, PEEKL(newTask& + tc_SPLower%) + stackSize&
  83.     POKEL newTask& + tc_SPReg%, PEEKL(newTask& + tc_SPUpper%)
  84.  
  85.     ' set up the Hook
  86.     h& = PEEKL(mlme& + MemEntry_sizeof% * 2 + meu_Addr%)
  87.     InitHook h&, initPC&
  88.     POKEL newTask& + tc_UserData, h&
  89.  
  90.     ' misc task structures
  91.     POKEB newTask& + tc_Node% + ln_Type%, NT_TASK&
  92.     POKEB newTask& + tc_Node% + ln_Pri%, pri%
  93.     POKEL newTask& + tc_Node% + ln_Name%, tname&
  94.  
  95.     ' add the memory to the tasks memory list
  96.     NewList newTask& + tc_MemEntry%
  97.     AddHead newTask& + tc_MemEntry%, ml&
  98.  
  99.     ' add the task to the system, with the default PC
  100.     tcb& = AddTask&(newTask&, VARPTR(TaskHookEntry&(0)), NULL&)
  101.     IF PEEKW(LIBRARY("exec.library") + lib_Version%) >= 37 AND tcb& = NULL&
  102.         FreeEntry ml&
  103.         EXIT FUNCTION
  104.     END IF
  105.  
  106.     CreateHookEntryTask& = newTask&
  107. END FUNCTION
  108.