home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / PKTDRVR / PDCLK207.ZIP / PDCLKSRC / BUFS.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-10-09  |  7.7 KB  |  331 lines

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