home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / BACKGRND.ASM < prev    next >
Assembly Source File  |  1990-10-29  |  3KB  |  207 lines

  1. PAGE    66,132
  2. ;
  3. ;
  4. ;
  5. ;
  6. ;
  7. ;
  8. ;
  9. ;
  10. ;  backgrnd.asm
  11. ;
  12. ;  usage:
  13. ;        extern init_bkg( int (*routine)(), char *stack, int stacklen)
  14. ;        extern swap_bkg()
  15. ;        extern kill_bkg()
  16. ;
  17. ;  (c) 1990 University of Waterloo,
  18. ;           Faculty of Engineering,
  19. ;           Engineering Microcomputer Network Development Office
  20. ;
  21. ;
  22.     include masmdefs.hsm
  23.     include    model.hsm
  24.  
  25. codedef BACKGRND
  26.  
  27. SETVEC    equ    25h        ; function to set interrupt vector
  28. GETVEC    equ    35h
  29. TICKER    equ    1ch
  30. DOS    equ    21h
  31.  
  32.  
  33. datadef
  34.  
  35. cstart  BACKGRND
  36.  
  37. oldint    dq 0
  38.  
  39. inside    db 0
  40. stkbase dw 0        ; used to check stack depth
  41. stkptr    dw 0
  42. stkseg    dw 0
  43. retstk    dw 0,0
  44. bkgptr    dw 0, 0
  45.  
  46. swapstk macro
  47.     cli
  48.     mov    AX, SS
  49.     xchg    CS:stkseg, AX
  50.     mov    SS, AX
  51.     xchg    CS:stkptr, SP
  52.     sti
  53.     endm
  54.  
  55.     ; note, we do not push AX!!!!!!, assembly routine must
  56. push_em    macro
  57.     push    BX
  58.     push    CX
  59.     push    DX
  60.     push    BP
  61.     push    SI
  62.     push    DI
  63.     push    DS
  64.     push    ES
  65.     endm
  66.  
  67. pop_em    macro
  68.     pop    ES
  69.     pop    DS
  70.     pop    DI
  71.     pop    SI
  72.     pop    BP
  73.     pop    DX
  74.     pop    CX
  75.     pop    BX
  76.     pop    AX
  77.     endm
  78.  
  79.     ; bkg_int - the interrupt function called by the timer tick
  80. bkg_int proc far
  81.     ; first chain
  82.     pushf
  83.     call    dword ptr CS:[oldint]
  84.  
  85.     ; establish if we are active
  86.     push    AX
  87.     mov    AL, 1
  88.     xchg    CS:inside, AL
  89.     or    AL, AL
  90.     jz    @1        ; not busy, go ahead
  91.  
  92.     ; must be already active
  93.     pop    AX
  94.     iret
  95.  
  96. @1:    push_em
  97.  
  98.     ; switch over to the new stack
  99.     swapstk
  100.  
  101.     pop_em
  102.  
  103. IF PTR_L
  104.     retf
  105. ELSE
  106.     retn
  107. ENDIF
  108. bkg_int endp
  109.  
  110.     ; use cproc, does not use BP
  111. cproc    swap_bkg
  112.     push    AX
  113.     push_em           ; save them on
  114.     swapstk
  115.     pop_em
  116.  
  117.     xor    AL, AL        ; clear flag
  118.     mov    CS:inside, AL
  119.  
  120.     iret
  121. creturn    swap_bkg
  122.  
  123. IF FUNC_L
  124. OFSSTK    equ    4
  125. OFSLEN    equ    OFSSTK + 4
  126. ELSE
  127. OFSSTK    equ    2
  128. OFSLEN    equ    OFSSTK + 2
  129. ENDIF
  130.  
  131. cpublic init_bkg    ; int (*routine)(), char *stack, int stacklen
  132.     ; get pointer to routine
  133.     mov    DX, +@AB [BP]
  134.     mov    CS:bkgptr, DX
  135. IF FUNC_L
  136.     mov     CX, +@AB + 2 [BP]
  137. ELSE
  138.     mov    CX, CS
  139. ENDIF
  140.     mov    CS:bkgptr+2, CX
  141.  
  142.     ; get stack area
  143.     mov    AX, +@AB + OFSSTK [BP]
  144.     mov    CS:stkbase, AX
  145. IF PTR_L
  146.     mov    BX, +@AB + OFSSTK + 2 [BP]
  147.     mov    CS:stkseg, BX
  148. ELSE
  149.     mov    CS:stkseg, DS
  150. ENDIF
  151.     ; get length
  152.     add    AX, +@AB + OFSLEN [BP]
  153.     sub    AX, 2
  154.     mov    CS:stkptr, AX
  155.  
  156.     ; get the stack area and set up the first call
  157.     swapstk
  158.     push    CX
  159.     push    DX
  160.     push    AX
  161.     push_em
  162.  
  163.     ; done with that stack
  164.     swapstk
  165.  
  166.     ; set up the interrupt
  167.     push    ES
  168.     push    DS
  169.     mov     AH, GETVEC
  170.     mov    AL, TICKER
  171.     int    DOS
  172.     mov    word ptr CS:oldint, BX
  173.     mov    word ptr CS:oldint+2, ES
  174.  
  175.     push    CS
  176.     pop    DS
  177.     mov    DX, offset bkg_int
  178.     mov    AH, SETVEC
  179.     mov    AL, TICKER
  180.     int    DOS
  181.  
  182.     mov    DS, DX
  183.  
  184.     pop    DS
  185.     pop    ES
  186.     xor     AX, AX
  187. creturn init_bkg
  188.  
  189. cpublic    kill_bkg
  190.     push    DS
  191.     xor    AX, AX
  192.     mov    DS, AX
  193.     mov    BX, TICKER * 4
  194.     cli
  195.     mov    CX, word ptr CS:oldint
  196.     mov    DX, word ptr CS:oldint+2
  197.     mov    DS:[BX], CX
  198.     mov    DS:[BX+2], DX
  199.     sti
  200.     pop    DS
  201. creturn    kill_bkg
  202. cend    BACKGRND
  203.  
  204.         end
  205.  
  206.  
  207.