home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / PKTDRVR / PDTST217.ZIP / PDCLKSRC / BUFS.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-04-07  |  6.2 KB  |  280 lines

  1. ;        bufs.asm
  2. ;========================================================================
  3.  
  4. ; Copyright (C) 1991-94 by Jan.Engvald@ldc.lu.se, see file COPYING.
  5.  
  6. ;************************************************************************
  7. ;*        BufAlloc
  8. ;*    Output:     if OK: non-zero and DS:BX = buffer addr
  9. ;*                        DS:DI = pkt addr
  10. ;*                        ES = DS
  11. ;*    Destroys:    DS, BX, SI, DI, ES, flags
  12. ;*            or just DI and flags if the list was empty
  13. ;************************************************************************
  14.  
  15. BufAlloc    proc    near
  16. if DEBUG ge 2
  17.                 mov     di,cs:FreeBufs.lBufsAvail
  18.                 cmp     di,cs:MinAvail
  19.                 ja      BufAlNotMin
  20.                 mov     cs:MinAvail,di
  21.   BufAlNotMin:
  22. endif ; DEBUG ge 2
  23.         mov    di,offset FreeBufs    ; free buffer chain
  24.  
  25. ifdef SMALLBUFS
  26.         jmp    short BufAl
  27.  
  28. BufAlSml:
  29.         mov    di,offset FreeSmal
  30. endif ; SMALLBUFS
  31.  
  32.   BufAl:
  33.         call    GetFromList
  34.         jz    BufAllocRet
  35.  
  36.         mov    [bx].dPtrDes,bx     ; fill in descriptor ptrs
  37.         lea    di,[bx+DESCRLEN]
  38.         mov    [bx].dPtrPhys,di    ; must be di return value
  39.   BufAllocRet:
  40.         ret
  41. BufAlloc    endp
  42.  
  43.  
  44.  
  45. ;************************************************************************
  46. ;*        GetFromList
  47. ;*    Input:        CS:DI = listhead address
  48. ;*    Output:     if OK: non-zero and DS:BX = buffer addr
  49. ;*                        ES = DS
  50. ;*    Destroys:    DS, BX, SI, DI, ES, flags
  51. ;*            or just flags if the list was empty
  52. ;************************************************************************
  53.  
  54. GetFromList    proc    near
  55.         PushfDI
  56.         dec    cs:[di].lBufsAvail      ; one less available bufs
  57.         js    GetFromEmpty        ; empty?
  58.  
  59.         lds    bx,cs:[di].lNext    ; - no, unlink first buf
  60.         les    si,[bx].dNext
  61.         mov    cs:[di].lNext.offs,si
  62.         mov    cs:[di].lNext.segm,es
  63.         mov    es:[si].dPrev.offs,di
  64.         mov    es:[si].dPrev.segm,cs
  65.         mov    si,ds
  66.         mov    es,si
  67. if DEBUG
  68.         mov    [bx].dPrev.offs,0
  69. endif ; DEBUG
  70.         PopfEI
  71.         or    sp,sp            ; non-zero (OK) return
  72.         ret
  73.  
  74.   GetFromEmpty:
  75.         inc    cs:[di].lBufsAvail      ; restore available bufs
  76.         PopfEI
  77.         cmp    bx,bx            ; ensure zero flag
  78.         ret
  79. GetFromList    endp
  80.  
  81.  
  82.  
  83. ;************************************************************************
  84. ;*        BufRelease
  85. ;*    Input:        DS:BX = buffer addr
  86. ;*            CS:SI = List to add to (AddToList only)
  87. ;*    Destroys:    SI, DI, ES (but all flags are saved)
  88. ;************************************************************************
  89.  
  90. BufRelease    proc    near
  91.         mov    si,[bx].dHomeList    ; free buffer chain
  92. if DEBUG ge 2
  93.         pushf
  94.         cmp    si,offset FreeBufs    ; did DS:BX point to a descriptor?
  95.         je    BufRelOK
  96. ifdef SMALLBUFS
  97.         cmp    si,offset FreeSmal
  98.         je    BufRelOK
  99. endif ; SMALLBUFS
  100.   BufRelErr:
  101.         mov    al,'Z'-'0'
  102.         call    Terminate
  103.   BufRelOK:
  104.         popf
  105. endif ; DEBUG ge 2
  106.  
  107. if DEBUG ge 3
  108.         .386
  109.         pushf
  110.         push    ds
  111.         pop    es
  112.         push    eax
  113.         push    cx
  114.         mov    cx,(dHwDst-dPtrIp)/4
  115.  
  116. if DEBUG ge 7
  117.         mov    cx,((dHwDst-dPtrIp)+(BUFSIZESML-DESCRLEN))/4
  118.         cmp    si,offset FreeBufs
  119.         jne    BufRelSml
  120.         mov    cx,((dHwDst-dPtrIp)+(BUFSIZE-DESCRLEN))/4
  121.   BufRelSml:
  122. endif ; DEBUG ge 7
  123.  
  124.         lea    di,[bx].dPtrPhys
  125.         mov    eax,0aaaaaaaah        ; trash old contents
  126.         stosw
  127.         rep    stosd
  128.  
  129.         pop    cx
  130.         pop    eax
  131.         popf
  132.         .8086
  133. endif ; DEBUG ge 3
  134.  
  135.  
  136. AddToList:
  137.         pushf
  138. if DEBUG ge 2
  139.         cmp    [bx].dPrev.offs,0    ; already on a list?
  140.         jne    BufRelErr
  141.         mov    di,ds
  142.         cmp    di,cs:MySegm        ; reasonable segment?
  143.         jb    BufRelErr
  144. endif ; DEBUG ge 2
  145.         cli
  146.         inc    cs:[si].lBufsAvail     ; one more buf is now available
  147.  
  148.         les    di,cs:[si].lPrev    ; link in buffer
  149.         mov    cs:[si].lPrev.offs,bx
  150.         mov    cs:[si].lPrev.segm,ds
  151.         mov    es:[di].dNext.offs,bx    ;   at end of chain
  152.         mov    es:[di].dNext.segm,ds
  153.         mov    [bx].dPrev.offs,di
  154.         mov    [bx].dPrev.segm,es
  155.         mov    [bx].dNext.offs,si
  156.         mov    [bx].dNext.segm,cs
  157.  
  158.         popf
  159.         ret
  160. BufRelease    endp
  161.  
  162.  
  163.  
  164. ;************************************************************************
  165. ;*        BufInit
  166. ;************************************************************************
  167.  
  168. BufInit     proc    near
  169.         mov    ax,cs
  170.         mov    ds,ax
  171.         assume    ds:code_s
  172.         mov    es,ax
  173.         mov    MySegm,ax
  174.  
  175.         mov    cx,LINKHEADS
  176.         mov    di,offset SendToDo.lNext.segm
  177.   BufSegmLoop:
  178.         stosw                ; lNext.segm = cs
  179.         scasw                ; skip over lPrev.offs
  180.         stosw                ; lPrev.segm = cs
  181.         add    di,LINKHEADLEN-6
  182.         loop    BufSegmLoop
  183.  
  184.                 test    ArgFlags,TR_GIANT    ; Ethernet or Token Ring size?
  185.                 jz      BufStandard
  186.                 mov     MyNbufs,NBUFSTR
  187.                 mov     MyGiant,GIANTTR
  188.                 mov     MyBufSize,BUFSIZETR
  189.   BufStandard:
  190.         mov    ax,MyBufSize        ; initialize cs segm buffers
  191.         mov    cx,MyNbufs
  192.         mov    bx,offset BufStart
  193.   BufInitLoop:
  194.         mov    [bx].dHomeList,offset FreeBufs
  195. if DEBUG ge 2
  196.         mov    [bx].dPrev.offs,0
  197. endif ; DEBUG ge 2
  198.         call    BufRelease
  199.         add    bx,ax
  200.         loop    BufInitLoop
  201.  
  202. ifdef SMALLBUFS
  203.         mov    cx,NBUFSMALL
  204.   BufSmlLoop:
  205.         mov    [bx].dHomeList,offset FreeSmal
  206.         mov    [bx].dPrev.offs,0
  207.         call    BufRelease
  208.         add    bx,BUFSIZESML
  209.         loop    BufSmlLoop
  210. endif ; SMALLBUFS
  211.  
  212. if TBLBUILD or PINGCLIENT
  213. if DEBUG
  214.         test    ArgFlags,NOT_ALL_MEM
  215.         jnz    BufInitRet
  216. endif ; DEBUG
  217.         mov    dx,ArgFlags
  218.         and    dx,TERM_WAIT+MAKE_TABLE
  219.         or    dx,EchoTarget
  220.         jz    BufInitRet
  221.  
  222.         mov    dx,cs            ; use all free memory as buffers
  223.         add    dx,1000h
  224. if TBLBUILD
  225.         test    ArgFlags,MAKE_TABLE
  226.         jz    BufInNoTbl
  227.  
  228.         add    dx,1000h*TableSegs
  229. if HOPCHK
  230.         test    MoreFlags,HOP_CHK
  231.         jz    BufInNoHop
  232.         and    ArgFlags,not MAKE_TABLE
  233.   BufInNoHop:
  234. endif ; HOPCHK
  235.   BufInNoTbl:
  236. endif ; TBLBUILD
  237.         assume    ds:nothing
  238.   BufHeapL1:
  239.         mov    bx,4            ; don't want offset 0
  240.         mov    ds,dx
  241.         add    dx,1000h
  242.   BufHeapL2:
  243.         lea    si,[bx+15]        ; convert addr to paragrafs
  244.         add    si,ax
  245.         mov    cl,4
  246.         shr    si,cl
  247.         mov    cx,ds
  248.         add    cx,si
  249.         add    cx,0c00h        ; room for COMMAND.COM
  250.         cmp    cx,cs:PspTopMem        ; above the top?
  251.         ja    BufInitRet
  252.         
  253.         mov    [bx].dHomeList,offset FreeBufs
  254. if DEBUG ge 2
  255.         mov    [bx].dPrev.offs,0
  256. endif ; DEBUG ge 2
  257.         call    BufRelease
  258.                 add     bx,ax
  259.                 add     bx,ax
  260.                 jc      BufHeapL1        ; next 64k segment
  261.                 sub     bx,ax
  262.         jmp    BufHeapL2
  263.         
  264.   BufInitRet:
  265. endif ; TBLBUILD or PINGCLIENT
  266.  
  267.         push    cs
  268.         push    cs
  269.         pop    ds
  270.         pop    es
  271. if DEBUG
  272.         mov    ax,FreeBufs.lBufsAvail
  273.         mov    MaxAvail,ax
  274. endif ; DEBUG
  275.         ret
  276. BufInit     endp
  277.  
  278. ;========================================================================
  279. ;        endinclude
  280.