home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / p / pcrte224.zip / SOURCE.ZIP / TIMER.INC < prev    next >
Text File  |  1992-06-09  |  7KB  |  212 lines

  1. ;;*************************************************************************
  2. ;;                        timer.inc      timer.inc
  3. ;;*************************************************************************
  4. ;;  Copyright (C) 1989 Northwestern University, Vance Morrison
  5. ;;
  6. ;;
  7. ;; Permission to view, compile, and modify for LOCAL (intra-organization) 
  8. ;; USE ONLY is hereby granted, provided that this copyright and permission 
  9. ;; notice appear on all copies.  Any other use by permission only.
  10. ;;
  11. ;; Northwestern University makes no representations about the suitability 
  12. ;; of this software for any purpose.  It is provided "as is" without expressed 
  13. ;; or implied warranty.  See the copywrite notice file for complete details.
  14. ;;
  15. ;;*************************************************************************
  16. ;; timer.inc implements functions that will allow a task to wait a specific
  17. ;; amount of time
  18. ;;
  19. ;; Routines provided by this module
  20. ;;
  21. ;;  TIMER_DECLARE name, task
  22. ;;  TIMER_DEFINE name
  23. ;;  TIMER_MARK_in_AX_const_CX_BP_ES name, code_label
  24. ;;  TIMER_RETURN name
  25. ;;
  26. ;; AUTHOR: Vance Morrison
  27. ;; DATE  : 4/22/89
  28. ;;*************************************************************************
  29.  
  30. TIMER_MAX            = 32        ;; number of outstanding requests
  31.  
  32. timer_entry struc
  33.     timer_next   dw 0            ;; this MUST be first !!!
  34.     timer_time   dw 0
  35.     timer_jmp    dw 0
  36. timer_entry ends
  37.  
  38. timer_data struc
  39.     timer_next_time  dw ?
  40.     timer_list       dw ?
  41.     timer_free       dw ?
  42.     timer_table      timer_entry TIMER_MAX dup (<>)  
  43.     timer_switch_ctr db ?        ;; counts number of context switches
  44. timer_data ends
  45.  
  46.  
  47. ;;************************************************************************
  48. ;; TIMER_DECLARE declares a structure called 'name' that can be used to
  49. ;;   wait for a certain time.  'task' is the name of the task needed
  50. ;;   by this module
  51. ;;
  52. TIMER_DECLARE  MACRO name, task
  53.     .errb <task>
  54.  
  55.     .DATA
  56.     timer_&name&_task equ task
  57.     global timer_&name&_data:timer_data 
  58.     .CODE
  59. ENDM
  60.  
  61.  
  62. ;;************************************************************************
  63. ;; TIMER_DEFINE defines all the data storage and does initialization 
  64. ;; functions for the timer object 'name'.  
  65.  
  66. TIMER_DEFINE MACRO name
  67.     local around, start
  68.     .errb <name>
  69.  
  70.     .data
  71.     timer_&name&_data timer_data <>
  72.  
  73.     .code
  74.     jmp around
  75.         start:
  76.         TIMER_TASK name, %timer_&name&_task
  77.             ;; this does not fall through
  78.     around:
  79.  
  80.     mov CX, TIMER_MAX
  81.     xor DI, DI
  82.     mov SI, offset timer_&name&_data.timer_table+(TIMER_MAX*(size timer_entry))
  83.     init_loop:
  84.         sub SI, size timer_entry
  85.         mov [SI+timer_next], DI
  86.  
  87.         mov DI, SI
  88.         dec CX
  89.     jnz init_loop
  90.     mov timer_&name&_data.timer_free, SI
  91.     mov timer_&name&_data.timer_list, 0
  92.     mov timer_&name&_data.timer_switch_ctr, 1
  93.  
  94.         ;; start the task
  95.     TASK_DEFINE %timer_&name&_task, start
  96. ENDM
  97.  
  98.  
  99. ;;************************************************************************
  100. ;; TIMER_MARK_in_AX_const_CX_BP_ES sets the mark point so that 
  101. ;;  the code at 'code_label' will be called aftger AX ticks have elapsed.  
  102. ;;  (a tick is 18th of a second).  The code should call TIMER_RETURN
  103. ;;  when it is done processing
  104. ;;  (Note that AX is limited to be 1024 seconds (32K ticks))
  105. ;;  
  106. TIMER_MARK_in_AX_const_CX_BP_ES MACRO name, code_label
  107.     local done, sort_loop, found
  108.     .errb <code_name>
  109.  
  110.         ;; get current time
  111.     TIMER_GET_TICK_out_BX_const_AX_CX_BP_SI_DI_ES 
  112.     add AX, BX
  113.     mov DX, AX                              ;; DX holds expiration time
  114.  
  115.     mov SI, offset timer_&name&_data.timer_list
  116.     mov DI, timer_&name&_data.timer_list
  117.     sort_loop:                              ;; find the position in the list
  118.         or DI, DI
  119.         jz found
  120.         cmp DX, [DI+timer_time]
  121.         js found
  122.  
  123.         mov SI, DI
  124.         mov DI, [SI+timer_next]
  125.         jmp sort_loop
  126.     found:
  127.  
  128.     mov BX, timer_&name&_data.timer_free
  129.     or BX, BX                               ;; anything on the free list?
  130.     jz done
  131.  
  132.     mov AX, [BX+timer_next]                  ;; insert the record 
  133.     mov timer_&name&_data.timer_free, AX
  134.     mov [BX+timer_time], DX
  135.     mov [BX+timer_jmp], offset code_label
  136.     mov [BX+timer_next], DI
  137.     mov [SI+timer_next], BX
  138.  
  139.     mov BX, timer_&name&_data.timer_list
  140.     mov AX, [BX+timer_time]
  141.     mov timer_&name&_data.timer_next_time, AX
  142.     done:
  143. ENDM
  144.  
  145. TIMER_RETURN MACRO name
  146.     .errb <name>
  147.  
  148.     TASK_RETURN %timer_&name&_task
  149. ENDM
  150.  
  151.  
  152. ;;************************************************************************
  153. ;; TIMER_TASK is the code that the TIMER task runs.  This task basicly just
  154. ;; waits for the next event, and starts it when its time.
  155. ;;
  156. TIMER_TASK MACRO name
  157.     local done, no_more, do_stuff
  158.     .errb <name>
  159.  
  160.    dec timer_&name&_data.timer_switch_ctr  ;; only check every 20 context
  161.    jz do_stuff
  162.         TASK_RETURN %timer_&name&_task
  163.    do_stuff:
  164.    mov timer_&name&_data.timer_switch_ctr, 20
  165.  
  166.    TIMER_GET_TICK_out_BX_const_AX_CX_BP_SI_DI_ES 
  167.    cmp BX, timer_&name&_data.timer_next_time
  168.    js done                          ;; if time is not up.  Note that
  169.                                     ;; this comparison works correctly
  170.                                     ;; in the face of wraparound as long
  171.                                     ;; as the delta T is < 32K
  172.  
  173.    mov SI, timer_&name&_data.timer_list
  174.    or SI, SI
  175.    jz done       
  176.  
  177.         ;; take event off the queue
  178.    mov BX, [SI+timer_next]
  179.    mov timer_&name&_data.timer_list,  BX
  180.    or BX, BX
  181.    jz no_more
  182.        mov AX, [BX+timer_time]
  183.        mov timer_&name&_data.timer_next_time, AX
  184.    no_more:
  185.    mov AX, timer_&name&_data.timer_free
  186.    mov [SI+timer_next], AX
  187.    mov timer_&name&_data.timer_free, SI
  188.    jmp [SI+timer_jmp]
  189.  
  190.    done:
  191.        TASK_RETURN %timer_&name&_task
  192. ENDM
  193.  
  194.  
  195. ;;*************************************************************************
  196. ;; dig into low memory and get me the tick counter.
  197. ;;
  198. ;;  This is  where IBM Bios puts its tick counter.  (18 ticks/sec)
  199. ;;  Note that this location may be different on clones, but as far
  200. ;;  I I can tell, most clones put the ticks here too.  
  201. IBM_BIOS_TICK   equ 46CH
  202.  
  203. TIMER_GET_TICK_out_BX_const_AX_CX_BP_SI_DI_ES MACRO  
  204.     mov DX, DS
  205.     xor BX, BX
  206.     mov DS, BX
  207.     mov BX, IBM_BIOS_TICK
  208.     mov BX, [BX]
  209.     mov DS, DX
  210. ENDM
  211.  
  212.