home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 426 / 3C509A.ZIP / HEAD.ASM < prev    next >
Assembly Source File  |  1993-02-04  |  36KB  |  1,399 lines

  1.     include    defs.asm
  2.  
  3. ;  Copyright, 1988-1992, Russell Nelson, Crynwr Software
  4.  
  5. ;   This program is free software; you can redistribute it and/or modify
  6. ;   it under the terms of the GNU General Public License as published by
  7. ;   the Free Software Foundation, version 1.
  8. ;
  9. ;   This program is distributed in the hope that it will be useful,
  10. ;   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;   GNU General Public License for more details.
  13. ;
  14. ;   You should have received a copy of the GNU General Public License
  15. ;   along with this program; if not, write to the Free Software
  16. ;   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. code    segment word public
  19.     assume    cs:code, ds:code
  20.  
  21.     public    phd_environ
  22.     org    2ch
  23. phd_environ    dw    ?
  24.  
  25.     public    phd_dioa
  26.     org    80h
  27. phd_dioa    label    byte
  28.  
  29.     org    100h
  30. start:
  31.     jmp    start_1
  32.     extrn    start_1: near
  33.  
  34.     even                ;put the stack on a word boundary.
  35.     dw    128 dup(?)        ;128 words of stack.
  36. our_stack    label    byte
  37.  
  38.  
  39.     extrn    int_no: byte
  40.  
  41.     public    entry_point, sys_features, flagbyte, is_186, is_286, is_386
  42. entry_point    db    ?,?,?,?        ; interrupt to communicate.
  43. sys_features    db    0        ; 2h = MC   40h = 2nd 8259
  44. is_186        db    0        ;=0 if 808[68], =1 if 80[123]86.
  45. is_286        db    0        ;=0 if 80[1]8[68], =1 if 80[234]86.
  46. is_386        db    0        ;=0 if 80[12]8[68], =1 if 80[34]86.
  47. flagbyte    db    0
  48. original_mask    db    0        ;=0 if interrupt was originally on.
  49.     even
  50.  
  51. functions    label    word
  52.     dw    f_not_implemented    ;0
  53.     dw    f_driver_info        ;1
  54.     dw    f_access_type        ;2
  55.     dw    f_release_type        ;3
  56.     dw    f_send_pkt        ;4
  57.     dw    f_terminate        ;5
  58.     dw    f_get_address        ;6
  59.     dw    f_reset_interface    ;7
  60.     dw    f_stop            ;8
  61.     dw    f_not_implemented    ;9
  62.     dw    f_get_parameters    ;10
  63.     dw    f_not_implemented    ;11
  64.     dw    f_as_send_pkt        ;12
  65.     dw    f_drop_pkt        ;13
  66.     dw    f_not_implemented    ;14
  67.     dw    f_not_implemented    ;15
  68.     dw    f_not_implemented    ;16
  69.     dw    f_not_implemented    ;17
  70.     dw    f_not_implemented    ;18
  71.     dw    f_not_implemented    ;19
  72.     dw    f_set_rcv_mode        ;20
  73.     dw    f_get_rcv_mode        ;21
  74.     dw    f_set_multicast_list    ;22
  75.     dw    f_get_multicast_list    ;23
  76.     dw    f_get_statistics    ;24
  77.     dw    f_set_address        ;25
  78.  
  79. ;external data supplied by device-dependent module:
  80.     extrn    driver_class: byte
  81.     extrn    driver_type: byte
  82.     extrn    driver_name: byte
  83.     extrn    driver_function: byte
  84.     extrn    parameter_list: byte
  85.     extrn    rcv_modes: word        ;count of modes followed by mode handles.
  86.  
  87. ;external code supplied by device-dependent module:
  88.     extrn    send_pkt: near
  89.     extrn    as_send_pkt: near
  90.     extrn    drop_pkt: near
  91.     extrn    set_address: near
  92.     extrn    terminate: near
  93.     extrn    reset_interface: near
  94.     extrn    xmit: near
  95.     extrn    recv: near
  96.     extrn    etopen: near
  97.     extrn    set_multicast_list: near
  98.  
  99. per_handle    struc
  100. in_use        db    0        ;non-zero if this handle is in use.
  101. packet_type    db    MAX_P_LEN dup(0);associated packet type.
  102. packet_type_len    dw    0        ;associated packet type length.
  103. receiver    dd    0        ;receiver handler.
  104. receiver_sig    db    8 dup(?)    ;signature at the receiver handler.
  105. class        db    ?        ;interface class
  106. per_handle    ends
  107.  
  108. handles        per_handle MAX_HANDLE dup(<>)
  109. end_handles    label    byte
  110.  
  111.     public    multicast_count, multicast_addrs, multicast_broad
  112. multicast_count    dw    0        ;count of stored multicast addresses.
  113. multicast_broad    db    0ffh,0ffh,0ffh,0ffh,0ffh,0ffh    ; entry for broadcast
  114. multicast_addrs    db    MAX_MULTICAST*EADDR_LEN dup(?)
  115.  
  116. ;the device-dependent code reads the board's address from ROM in the
  117. ;initialization code.
  118.     public    address_len, rom_address, my_address
  119. address_len    dw    EADDR_LEN        ;default to Ethernet.
  120. rom_address    db    MAX_ADDR_LEN dup(?)    ;our address in ROM.
  121. my_address    db    MAX_ADDR_LEN dup(?)    ;our current address.
  122.  
  123. rcv_mode_num    dw    3
  124.  
  125. free_handle    dw    0        ; temp, a handle not in use
  126. found_handle    dw    0        ; temp, handle for our packet
  127. receive_ptr    dd    0        ; the pkt receive service routine
  128.  
  129.     public    send_head, send_tail
  130. send_head    dd    0        ; head of transmit queue
  131. send_tail    dd    0        ; tail of transmit queue
  132.  
  133. statistics_list    label    dword
  134. packets_in    dw    ?,?
  135. packets_out    dw    ?,?
  136. bytes_in    dw    ?,?
  137. bytes_out    dw    ?,?
  138. errors_in    dw    ?,?
  139. errors_out    dw    ?,?
  140. packets_dropped    dw    ?,?        ;dropped due to no type handler.
  141.  
  142. savespss    label    dword
  143. savesp        dw    ?        ;saved during the stack swap.
  144. savess        dw    ?
  145.  
  146. ;
  147. ; The following structure is used to access the registers pushed by the
  148. ; packet driver interrupt handler.  Don't change this structure without also
  149. ; changing the "bytes" structure given below.
  150. ;
  151. regs    struc                ; stack offsets of incoming regs
  152. _ES    dw    ?
  153. _DS    dw    ?
  154. _BP    dw    ?
  155. _DI    dw    ?
  156. _SI    dw    ?
  157. _DX    dw    ?
  158. _CX    dw    ?
  159. _BX    dw    ?
  160. _AX    dw    ?
  161. _IP    dw    ?
  162. _CS    dw    ?
  163. _F    dw    ?            ; flags, Carry flag is bit 0
  164. regs    ends
  165.  
  166. ;
  167. ; bits in the _F register.
  168. ;
  169. CY    equ    0001h
  170. EI    equ    0200h
  171.  
  172.  
  173. ;
  174. ; This structure is a bytewise version of the "regs" structure above.
  175. ;
  176. bytes    struc                ; stack offsets of incoming regs
  177.     dw    ?            ; es, ds, bp, di, si are 16 bits
  178.     dw    ?
  179.     dw    ?
  180.     dw    ?
  181.     dw    ?
  182. _DL    db    ?
  183. _DH    db    ?
  184. _CL    db    ?
  185. _CH    db    ?
  186. _BL    db    ?
  187. _BH    db    ?
  188. _AL    db    ?
  189. _AH    db    ?
  190. bytes    ends
  191.  
  192.     public    their_isr
  193. their_isr    dd    0        ; original owner of pkt driver int
  194.  
  195.     public    our_isr
  196. our_isr:
  197.     jmp    short our_isr_0        ;the required signature.
  198.     nop
  199.     db    'PKT DRVR',0
  200. our_isr_0:
  201.     assume    ds:nothing
  202.     push    ax
  203.     push    bx
  204.     push    cx
  205.     push    dx
  206.     push    si
  207.     push    di
  208.     push    bp
  209.     push    ds
  210.     push    es
  211.     cld
  212.     mov    bx,cs            ;set up ds.
  213.     mov    ds,bx
  214.     assume    ds:code
  215.     mov    bp,sp            ;we use bp to access the original regs.
  216.     and    _F[bp],not CY        ;start by clearing the carry flag.
  217.  
  218.   if 0
  219.     test    _F[bp],EI        ;were interrupt on?
  220.     jz    our_isr_ei        ;no, don't turn them back on.
  221.     sti                ;yes, turn them back on.
  222. our_isr_ei:
  223.   endif
  224.  
  225.     test    flagbyte,CALLED_ETOPEN    ; have we initialized the card?
  226.     jnz    our_isr_cont        ; yes
  227.     push    ax            ; save lots of registers
  228.     push    bx
  229.     push    cx
  230.     push    dx
  231.     push    si
  232.     push    di
  233.     push    bp
  234.     push    ds
  235.     push    es
  236.  
  237.     call    etopen            ; init the card
  238.  
  239.     mov    si,offset rom_address    ;copy their original address to
  240.     push    ds
  241.     pop    es
  242.     mov    di,offset my_address    ;  their current address.
  243.     mov    cx,MAX_ADDR_LEN/2
  244.     rep    movsw
  245.  
  246.     pop    es            ; restore lots of registers
  247.     pop    ds
  248.     pop    bp
  249.     pop    di
  250.     pop    si
  251.     pop    dx
  252.     pop    cx
  253.     pop    bx
  254.     pop    ax
  255.     mov    dh,CANT_RESET        ; (actually can't initialize)
  256.     jc    our_isr_error
  257.     or    flagbyte,CALLED_ETOPEN    ; remember this fact
  258.  
  259.     cmp    rcv_modes+2[3*2],0    ;does mode 3 exist?
  260.     je    our_isr_cont        ;no.
  261.     call    rcv_modes+2[3*2]    ;  call it.
  262.  
  263. our_isr_cont:
  264.  
  265.     mov    bl,ah            ;jump to the correct function.
  266.     mov    bh,0
  267.     cmp    bx,25            ;only twenty five functions right now.
  268.     ja    f_bad_command
  269.     add    bx,bx            ;*2
  270. ;
  271. ; The functions are called with all the original registers except
  272. ; BX, DH, and BP.  They do not need to preserve any of them.  If the
  273. ; function returns with cy clear, all is well.  Otherwise dh=error number.
  274. ;
  275.     call    functions[bx]
  276.     jnc    our_isr_return
  277. our_isr_error:
  278.     mov    bp,sp            ;we use bp to access the original regs.
  279.     mov    _DH[bp],dh
  280.     or    _F[bp],CY        ;return their carry flag.
  281. our_isr_return:
  282.     pop    es
  283.     pop    ds
  284.     pop    bp
  285.     pop    di
  286.     pop    si
  287.     pop    dx
  288.     pop    cx
  289.     pop    bx
  290.     pop    ax
  291.     iret
  292.  
  293. f_bad_command:
  294.     extrn    bad_command_intercept: near
  295.     mov    bx,_BX[bp]
  296.     call    bad_command_intercept
  297.     mov    _BX[bp],bx
  298.     mov    _DX[bp],dx
  299.     jnc    our_isr_return
  300.     jmp    our_isr_error
  301.  
  302.     public    re_enable_interrupts
  303. re_enable_interrupts:
  304. ; Possibly re-enable interrupts.  We put this here so that other routines
  305. ; don't need to know how we put things on the stack.
  306.     test    _F[bp], EI        ; Were interrupts enabled on pkt driver entry?
  307.     je    re_enable_interrupts_1    ; No.
  308.     sti                ; Yes, re-enable interrupts now.
  309. re_enable_interrupts_1:
  310.     ret
  311.  
  312.  
  313. f_not_implemented:
  314.     mov    dh,BAD_COMMAND
  315.     stc
  316.     ret
  317.  
  318.  
  319. f_driver_info:
  320. ;    As of 1.08, the handle is optional, so we no longer verify it.
  321. ;    call    verify_handle
  322.     cmp    _AL[bp],0ffh        ; correct calling convention?
  323.     jne    f_driver_info_1        ; ne = incorrect, fail
  324.  
  325.                     ;For enhanced PD, if they call
  326.     cmp    _BX[bp],offset handles    ;with a handle, give them the
  327.                     ;class they think it is
  328.     jb    default_handle
  329.     cmp    _BX[bp],offset end_handles ;otherwise default to first class
  330.     jae    default_handle
  331.     mov    bx, _BX[bp]
  332.     cmp    [bx].in_use,0        ;if it's not in use, it's bad.
  333.     je    default_handle
  334.     mov    al, [bx].class
  335.     mov    _CH[bp], al
  336.     jmp    short got_handle
  337.  
  338. default_handle:
  339.     mov    al,driver_class
  340.     mov    _CH[bp],al
  341. got_handle:
  342.  
  343.     mov    _BX[bp],majver        ;version
  344.     mov    al,driver_type
  345.     cbw
  346.     mov    _DX[bp],ax
  347.     mov    _CL[bp],0        ;number zero.
  348.     mov    _DS[bp],ds        ; point to our name in their ds:si
  349.     mov    _SI[bp],offset driver_name
  350.     mov    al,driver_function
  351.     mov    _AL[bp],al
  352.     clc
  353.     ret
  354. f_driver_info_1:
  355.     stc
  356.     ret
  357.  
  358.  
  359. f_set_rcv_mode:
  360.     call    verify_handle
  361.     cmp    cx,rcv_mode_num        ;are we already using that mode?
  362.     je    f_set_rcv_mode_4    ;yes, no need to check anything.
  363.  
  364.     mov    dx,bx            ;remember our handle.
  365.  
  366.     call    count_handles        ;is ours the only open handle?
  367.     cmp    cl,1
  368.     jne    f_set_rcv_mode_1    ;no, don't change the receive mode.
  369.  
  370.     mov    cx,_CX[bp]        ;get the desired receive mode.
  371.     cmp    cx,rcv_modes        ;do they have this many modes?
  372.     jae    f_set_rcv_mode_1    ;no - must be a bad mode for us.
  373.     mov    bx,cx
  374.     add    bx,bx            ;we're accessing words, not bytes.
  375.     mov    ax,rcv_modes[bx]+2    ;get the handler for this mode.
  376.     or    ax,ax            ;do they have one?
  377.     je    f_set_rcv_mode_1    ;no - must be a bad mode for us.
  378.     mov    rcv_mode_num,cx        ;yes - remember the number and
  379.     call    ax            ;  call it.
  380. f_set_rcv_mode_4:
  381.     clc
  382.     ret
  383. f_set_rcv_mode_1:
  384.     mov    dh,BAD_MODE
  385.     stc
  386.     ret
  387.  
  388.  
  389. f_get_rcv_mode:
  390.     call    verify_handle
  391.     mov    ax,rcv_mode_num        ;return the current receive mode.
  392.     mov    _AX[bp],ax
  393.     clc
  394.     ret
  395.  
  396.  
  397. f_set_multicast_list:
  398. ;following instruction not needed because cx hasn't been changed.
  399. ;    mov    cx,_CX[bp]        ;Tell them how much room they have.
  400.  
  401. ;verify that they supplied an even number of EADDR's.
  402.     mov    ax,cx
  403.     xor    dx,dx
  404.     mov    bx,EADDR_LEN
  405.     div    bx
  406.     or    dx,dx            ;zero remainder?
  407.     jne    f_set_multicast_list_2    ;no, we don't have an even number of
  408.                     ;  addresses.
  409.  
  410.     cmp    ax,MAX_MULTICAST    ;is this too many?
  411.     ja    f_set_multicast_list_3    ;yes - return NO_SPACE
  412. f_set_multicast_list_1:
  413.     mov    multicast_count,ax    ;remember the number of addresses.
  414.     push    cs
  415.     pop    es
  416.     mov    di,offset multicast_addrs
  417.     push    ds
  418.     mov    ds,_ES[bp]        ; get ds:si -> new list.
  419.     mov    si,_DI[bp]
  420.     push    cx
  421.     rep    movsb
  422.     pop    cx
  423.     pop    ds
  424.  
  425.     mov    si,offset multicast_addrs
  426.     call    set_multicast_list
  427.     ret
  428. f_set_multicast_list_2:
  429.     mov    dh,BAD_ADDRESS
  430.     stc
  431.     ret
  432. f_set_multicast_list_3:
  433.     mov    dh,NO_SPACE
  434.     stc
  435.     ret
  436.  
  437.  
  438. f_get_multicast_list:
  439.     mov    _ES[bp],ds        ;return what we have remembered.
  440.     mov    _DI[bp],offset multicast_addrs
  441.     mov    ax,EADDR_LEN        ;multiply the count by the length.
  442.     mul    multicast_count
  443.     mov    _CX[bp],ax        ;because they want total bytes.
  444.     clc
  445.     ret
  446.  
  447.  
  448. f_get_statistics:
  449.     call    verify_handle        ;just in case.
  450.     mov    _DS[bp],ds
  451.     mov    _SI[bp],offset statistics_list
  452.     clc
  453.     ret
  454.  
  455.  
  456. access_type_class:
  457.     mov    dh,NO_CLASS
  458.     stc
  459.     ret
  460.  
  461. access_type_type:
  462.     mov    dh,NO_TYPE
  463.     stc
  464.     ret
  465.  
  466. access_type_number:
  467.     mov    dh,NO_NUMBER
  468.     stc
  469.     ret
  470.  
  471. access_type_bad:
  472.     mov    dh,BAD_TYPE
  473.     stc
  474.     ret
  475.  
  476. ;register caller of pkt TYPE
  477. f_access_type:
  478.     mov    bx, offset driver_class
  479. access_type_9:
  480.     mov    al, [bx]        ;get the next class.
  481.     inc    bx
  482.     or    al,al            ;end of the list?
  483.     je    access_type_class    ;class failed (story of my life)
  484.     cmp    _AL[bp],al        ;our class?
  485.     jne    access_type_9        ;no, try again
  486. access_type_1:
  487.     cmp    _BX[bp],-1        ;generic type?
  488.     je    access_type_2        ;yes.
  489.     mov    al,driver_type
  490.     cbw
  491.     cmp    _BX[bp],ax        ;our type?
  492.     jne    access_type_type    ;no.
  493. access_type_2:
  494.     cmp    _DL[bp],0        ;generic number?
  495.     je    access_type_3
  496.     cmp    _DL[bp],1        ;our number?
  497.     jne    access_type_number
  498. access_type_3:
  499.     cmp    _CX[bp],MAX_P_LEN    ;is the type length too long?
  500.     ja    access_type_bad        ;yes - can't be ours.
  501.  
  502. ; now we do two things--look for an open handle, and check the existing
  503. ; handles to see if they're replicating a packet type.
  504.  
  505.     mov    free_handle,0        ;remember no free handle yet.
  506.     mov    bx,offset handles
  507. access_type_4:
  508.     cmp    [bx].in_use,0        ;is this handle in use?
  509.     je    access_type_5        ;no - don't check the type.
  510.     mov    al, _AL[bp]        ;is this handle the same class as
  511.     cmp    al, [bx].class        ;  they're want?
  512.     jne    short access_type_6
  513.     mov    es,_DS[bp]        ;get a pointer to their type
  514.     mov    di,_SI[bp]        ;  from their ds:si to our es:di
  515.     mov    cx,_CX[bp]        ;get the minimum of their length
  516.                     ;  and our length.  As currently
  517.                     ;  implemented, only one receiver
  518.                     ;  gets the packets, so we have to
  519.                     ;  ensure that the shortest prefix
  520.                     ;  is unique.
  521.     cmp    cx,[bx].packet_type_len    ;Are we less specific than they are?
  522.     jb    access_type_8        ;no.
  523.     mov    cx,[bx].packet_type_len    ;yes - use their count.
  524. access_type_8:
  525.     lea    si,[bx].packet_type
  526.     or    cx,cx            ; pass-all TYPE? (zero TYPE length)
  527.     jne    access_type_7        ; ne = no
  528.     mov    bx,offset handles+(MAX_HANDLE-1)*(size per_handle)
  529.     jmp    short access_type_5    ; put pass-all last
  530. access_type_7:
  531.     repe    cmpsb
  532.     jne    short access_type_6    ;go look at the next one.
  533. access_type_inuse:
  534.     mov    dh,TYPE_INUSE        ;a handle has been assigned for TYPE
  535.     stc                ;and we can't assign another
  536.     ret
  537. access_type_5:                ;handle is not in use
  538.     cmp    free_handle,0        ;found a free handle yet?
  539.     jne    access_type_6        ;yes.
  540.     mov    free_handle,bx        ;remember a free handle
  541. access_type_6:
  542.     add    bx,(size per_handle)    ;go to the next handle.
  543.     cmp    bx,offset end_handles    ;examined all handles?
  544.     jb    access_type_4        ;no, continue.
  545.  
  546.     mov    bx,free_handle        ;did we find a free handle?
  547.     or    bx,bx
  548.     je    access_type_space    ;no - return error.
  549.  
  550.     mov    [bx].in_use,1        ;remember that we're using it.
  551.  
  552.     mov    ax,_DI[bp]        ;remember the receiver type.
  553.     mov    [bx].receiver.offs,ax
  554.     mov    ax,_ES[bp]
  555.     mov    [bx].receiver.segm,ax
  556.  
  557.     push    ds
  558.     mov    ax,ds
  559.     mov    es,ax
  560.     mov    ds,_DS[bp]        ;remember their type.
  561.     mov    si,_SI[bp]
  562.     mov    cx,_CX[bp]
  563.     mov    es:[bx].packet_type_len,cx    ; remember the TYPE length
  564.     lea    di,[bx].packet_type
  565.     rep    movsb
  566.  
  567.     lds    si,es:[bx].receiver    ;copy the first 8 bytes
  568.     lea    di,[bx].receiver_sig    ; to the receiver signature.
  569.     mov    cx,8/2
  570.     rep    movsw
  571.  
  572.     pop    ds
  573.  
  574.     mov    al, _AL[bp]
  575.     mov    [bx].class, al
  576.  
  577.     mov    _AX[bp],bx        ;return the handle to them.
  578.  
  579.     clc
  580.     ret
  581.  
  582.  
  583. access_type_space:
  584.     mov    dh,NO_SPACE
  585.     stc
  586.     ret
  587.  
  588. f_release_type:
  589.     call    verify_handle        ;mark this handle as being unused.
  590.     mov    [bx].in_use,0
  591.  
  592.     call    count_handles        ;All handles gone now?
  593.     cmp    cl,0
  594.     jne    f_release_type_1    ;no, don't change the receive mode.
  595.  
  596.   if 0
  597.     cmp    rcv_modes+2[3*2],0    ;does mode 3 exist?
  598.     je    f_release_type_1    ;no.
  599.     mov    rcv_mode_num,3        ;yes - remember the number and
  600.     call    rcv_modes+2[3*2]    ;  call it.
  601.   endif
  602. f_release_type_1:
  603.     clc
  604.     ret
  605.  
  606.  
  607. f_send_pkt:
  608. ;ds:si -> buffer, cx = length
  609. ; XXX Should re-enable interrupts here, but some drivers are broken.
  610. ; Possibly re-enable interrupts.
  611. ;    test _F[bp], EI        ; Were interrupts enabled on pkt driver entry?
  612. ;    je    f_send_pkt_1    ; No.
  613. ;    sti            ; Yes, re-enable interrupts now.
  614. ;f_send_pkt_1:
  615.  
  616. ;following two instructions not needed because si and cx haven't been changed.
  617. ;    mov    si,_SI[bp]
  618. ;    mov    cx,_CX[bp]    ; count of bytes in the packet.
  619.     add2    packets_out,1
  620.     add2    bytes_out,cx        ;add up the received bytes.
  621.  
  622.     push    ds        ; set up proper ds for the buffer
  623.     mov    ds,_DS[bp]    ; address of buffer from caller's ds.
  624.     assume    ds:nothing, es:nothing
  625.  
  626. ; If -n option take Ethernet encapsulated Novell IPX packets (from BYU's 
  627. ; PDSHELL) and change them to be IEEE 802.3 encapsulated.
  628. EPROT_OFF    equ    EADDR_LEN*2
  629.     test    cs:flagbyte,N_OPTION
  630.     jz    f_send_pkt_2
  631.     cmp    ds:[si].EPROT_OFF,3781h ; if not Novell (prot 8137)
  632.     jne    f_send_pkt_2        ;  don't tread on it
  633.     push    ax            ; get scratch reg
  634.     mov    ax,[si].EPROT_OFF+4    ; get len
  635.     xchg    ah,al
  636.     inc    ax            ; make even (rounding up)
  637.     and    al,0feh
  638.     xchg    ah,al
  639.     mov    ds:[si].EPROT_OFF,ax    ; save in prot field
  640.     pop    ax            ; restore old contents
  641. f_send_pkt_2:
  642.     call    send_pkt
  643.     pop    ds
  644.     assume    ds:code
  645.     ret
  646.  
  647.  
  648. f_as_send_pkt:
  649. ;es:di -> iocb.
  650.     test    driver_function,4    ; is this a high-performance driver?
  651.     je    f_as_send_pkt_2        ; no.
  652. ; Possibly re-enable interrupts.
  653.     test _F[bp], EI            ; Were interrupts enabled on pkt driver entry?
  654.     je    f_as_send_pkt_1        ; No.
  655.     sti                ; Yes, re-enable interrupts now.
  656. f_as_send_pkt_1:
  657.     push    ds            ; set up proper ds for the buffer
  658.     lds    si,es:[di].buffer    ; ds:si -> buffer
  659.     assume    ds:nothing
  660.     mov    cx,es:[di].len        ; cx = length
  661.     add2    packets_out,1
  662.     add2    bytes_out,cx        ; add up the received bytes.
  663.  
  664. ;ds:si -> buffer, cx = length, es:di -> iocb.
  665.     call    as_send_pkt
  666.     pop    ds
  667.     assume    ds:code
  668.     ret
  669. f_as_send_pkt_2:
  670.     mov dh,    BAD_COMMAND        ; return an error.
  671.     stc
  672.     ret
  673.  
  674.  
  675. f_drop_pkt:
  676. ; es:di -> iocb.
  677.     test    driver_function,4    ; is this a high-performance driver?
  678.     je    f_as_send_pkt_2        ; no.
  679.     push    ds            ; Preserve ds
  680.     mov    si,offset send_head    ; Get head offset
  681. dp_loop:
  682.     mov    ax,ds:[si]        ; Get offset
  683.     mov    dx,ds:[si+2]        ; Get segment
  684.     mov    bx,ax
  685.     or    bx,dx            ; End of list?
  686.     je    dp_endlist        ; Yes
  687.     cmp    ax,di            ; Offsets equal?
  688.     jne    dp_getnext        ; No
  689.     mov    bx,es
  690.     cmp    dx,bx            ; Segments equal?
  691.     jne    dp_getnext        ; No
  692.     call    drop_pkt        ; Pass to driver
  693.     les    di,es:[di].next        ; Get next segment:offset
  694.     mov    ds:[si],di        ; Set next offset
  695.     mov    ds:[si+2],es        ; Set next segment
  696.     pop    ds            ; Restore ds
  697.     clc
  698.     ret
  699. dp_getnext:
  700.     mov    ds,dx            ; Get next segment
  701.     mov    si,ax            ; Get next iocb offset
  702.     lea    si,ds:[si].next        ; Get next iocb next ptr offset
  703.     jmp    dp_loop            ; Try again
  704. dp_endlist:
  705.     pop    ds            ; Restore ds
  706.     mov    dh,BAD_IOCB        ; Return error
  707.     stc                ; Set carry
  708.     ret
  709.  
  710.  
  711. f_terminate:
  712.     call    verify_handle        ; must have a handle
  713.  
  714.     mov    [bx].in_use,0        ; mark handle as free
  715.     call    count_handles        ; all handles gone?
  716.     or    cl,cl
  717.     jne    f_terminate_4        ; no, can't exit completely
  718.  
  719. ;
  720. ; Now disable interrupts
  721. ;
  722.     mov    al,int_no
  723.     or    al,al            ;are they using a hardware interrupt?
  724.     je    f_terminate_no_irq    ;no.
  725.     cmp    original_mask,0        ;was it enabled?
  726.     je    f_terminate_no_mask    ;yes, don't mask it now.
  727.     call    maskint
  728. f_terminate_no_mask:
  729.  
  730. ;
  731. ; Now return the interrupt to their handler.
  732. ;
  733.     mov    ah,25h            ;get the old interrupt into es:bx
  734.     mov    al,int_no
  735.     add    al,8
  736.     cmp    al,8+8            ;is it a slave 8259 interrupt?
  737.     jb    f_terminate_3        ;no.
  738.     add    al,70h - (8+8)        ;map it to the real interrupt.
  739. f_terminate_3:
  740.     push    ds
  741.     lds    dx,their_recv_isr
  742.     int    21h
  743.     pop    ds
  744.  
  745. f_terminate_no_irq:
  746.     call    terminate        ;terminate the hardware.
  747.  
  748.     mov    al,entry_point    ;release our_isr.
  749.     mov    ah,25h
  750.     push    ds
  751.     lds    dx,their_isr
  752.     int    21h
  753.     pop    ds
  754.  
  755. ;
  756. ; Now free our memory
  757. ;
  758.     push    cs
  759.     pop    es
  760.     mov    ah,49h
  761.     int    21h
  762.     clc
  763.     ret
  764. f_terminate_4:
  765.     mov    dh, CANT_TERMINATE
  766.     stc
  767.     ret
  768.  
  769.  
  770. f_get_address:
  771. ;    call    verify_handle
  772. ;    mov    es,_ES[bp]        ; get new one
  773. ;    mov    di,_DI[bp]        ; get pointer, es:di is ready
  774. ;    mov    cx,_CX[bp]        ;Tell them how much room they have.
  775.     cmp    cx,address_len        ;is there enough room for our address?
  776.     jb    get_address_space    ;no.
  777.     mov    cx,address_len        ;yes - get our address length.
  778.     mov    _CX[bp],cx        ;Tell them how long our address is.
  779.     mov    si,offset my_address    ;copy it into their area.
  780.     rep    movsb
  781.     clc
  782.     ret
  783.  
  784. get_address_space:
  785.     mov    dh,NO_SPACE
  786.     stc
  787.     ret
  788.  
  789.  
  790. f_set_address:
  791.     call    count_handles
  792.     cmp    cl,1            ;more than one handle in use?
  793.     ja    f_set_address_inuse    ;yes - we can't set the address
  794.  
  795.     mov    cx,_CX[bp]        ;get the desired address length.
  796.     cmp    ch,0
  797.     cmp    cl,parameter_list[3]    ;is it the right length?
  798.     ja    f_set_address_too_long    ;no.
  799.  
  800.     mov    ds,_ES[bp]        ; set new one
  801.     assume    ds:nothing
  802.     mov    si,_DI[bp]        ; set pointer, ds:si is ready
  803.     mov    ax,cs
  804.     mov    es,ax
  805.     mov    di,offset my_address
  806.     rep    movsb
  807.     mov    ds,ax            ;restore ds.
  808.     assume    ds:code
  809.  
  810.     mov    cx,_CX[bp]        ;get the desired address length.
  811.     mov    si,offset my_address
  812.     call    set_address
  813.     jc    f_set_address_err    ;Did it work?
  814.  
  815.     mov    cl,parameter_list[3]
  816.     xor    ch,ch
  817.     mov    _CX[bp],cx        ;yes - return our address length.
  818.  
  819.     clc
  820.     ret
  821. f_set_address_inuse:
  822.     mov    dh,CANT_SET
  823.     stc
  824.     ret
  825. f_set_address_too_long:
  826.     mov    dh,NO_SPACE
  827.     stc
  828.     ret
  829. f_set_address_err:
  830. ;we get here with cy set - leave it set.
  831.     mov    si,offset rom_address    ;we can't set the address, restore
  832.     mov    di,offset my_address    ;  to original.
  833.     mov    cx,MAX_ADDR_LEN/2
  834.     rep    movsw
  835.     ret
  836.  
  837.  
  838. f_reset_interface:
  839.     call    verify_handle
  840.     call    reset_interface
  841.     clc
  842.     ret
  843.  
  844.  
  845. ; Stop the packet driver doing upcalls. Also a following terminate will
  846. ; always succed (no in use handles any longer).
  847. f_stop:
  848.     mov    bx,offset handles
  849. f_stop_2:
  850.     mov    [bx].in_use,0
  851.     add    bx,(size per_handle)    ; next handle
  852.     cmp    bx,offset end_handles
  853.     jb    f_stop_2
  854.     clc
  855.     ret
  856.  
  857.  
  858. f_get_parameters:
  859. ;strictly speaking, this function only works for high-performance drivers.
  860.     test    driver_function,4    ;is this a high-performance driver?
  861.     jne    f_get_parameters_1    ;yes.
  862.     mov    dh,BAD_COMMAND        ;no - return an error.
  863.     stc
  864.     ret
  865. f_get_parameters_1:
  866.     mov    _ES[bp],cs
  867.     mov    _DI[bp],offset parameter_list
  868.     clc
  869.     ret
  870.  
  871.  
  872. count_handles:
  873. ;exit with cl = number of handles currently in use.
  874.     mov    bx,offset handles
  875.     mov    cl,0            ;number of handles in use.
  876. count_handles_1:
  877.     add    cl,[bx].in_use        ;is this handle in use?
  878.     add    bx,(size per_handle)    ;go to the next handle.
  879.     cmp    bx,offset end_handles
  880.     jb    count_handles_1
  881.     ret
  882.  
  883.  
  884. verify_handle:
  885. ;Ensure that their handle is real.  If it isn't, we pop off our return
  886. ;address, and return to *their* return address with cy set.
  887.     mov    bx,_BX[bp]        ;get the handle they gave us
  888.     cmp    bx,offset handles
  889.     jb    verify_handle_bad    ;no - must be bad.
  890.     cmp    bx,offset end_handles
  891.     jae    verify_handle_bad    ;no - must be bad.
  892.     cmp    [bx].in_use,1        ;if it's not in use, it's bad.
  893.     jne    verify_handle_bad
  894.     ret
  895. verify_handle_bad:
  896.     mov    dh,BAD_HANDLE
  897.     add    sp,2            ;pop off our return address.
  898.     stc
  899.     ret
  900.  
  901.  
  902.     public    set_recv_isr
  903. set_recv_isr:
  904.     mov    ah,35h            ;get the old interrupt into es:bx
  905.     mov    al,int_no        ; board's interrupt vector
  906.     or    al,al
  907.     je    set_isr_no_irq
  908.     add    al,8
  909.     cmp    al,8+8            ;is it a slave 8259 interrupt?
  910.     jb    set_recv_isr_1        ;no.
  911.     add    al,70h - 8 - 8        ;map it to the real interrupt.
  912. set_recv_isr_1:
  913.     int    21h
  914.     mov    their_recv_isr.offs,bx    ;remember the old seg:off.
  915.     mov    their_recv_isr.segm,es
  916.  
  917.     mov    ah,25h            ;now set our recv interrupt.
  918.     mov    dx,offset recv_isr
  919.     int    21h
  920.  
  921.     mov    al,int_no        ; Now enable interrupts
  922.     call    unmaskint
  923.     mov    original_mask,al
  924.  
  925. set_isr_no_irq:
  926.     ret
  927.  
  928.     public    count_in_err
  929. count_in_err:
  930.     assume    ds:nothing
  931.     add2    errors_in,1
  932.     ret
  933.  
  934.     public    count_out_err
  935. count_out_err:
  936.     assume    ds:nothing
  937.     add2    errors_out,1
  938.     ret
  939.  
  940. recv_isr_frame    struc
  941. recv_isr_ds    dw    ?
  942. recv_isr_dx    dw    ?
  943. recv_isr_ax    dw    ?
  944. recv_isr_ip    dw    ?
  945. recv_isr_cs    dw    ?
  946. recv_isr_f    dw    ?
  947. recv_isr_frame    ends
  948.  
  949. their_recv_isr    dd    0        ; original owner of board int
  950.  
  951. ;
  952. ; I have had a problem with some hardware which under extreme LAN loading
  953. ; conditions will re-enter the recv_isr. Since the 8259 interrupts for
  954. ; the card are masked off, and the card's interrupt mask register is 
  955. ; cleared (in 8390.asm at least) disabling the card from interrupting, this
  956. ; is clearly a hardware problem. Due to the low frequencey of occurance, and
  957. ; extreme conditions under which this happens, it is not lilely to be fixed
  958. ; in hardware any time soon, plus retrofitting of hardware in the field will
  959. ; not happen. To protect the driver from the adverse effects of this I am
  960. ; adding a simple re-entrancy trap here.  - gft - 910617
  961. ;
  962.  
  963. in_recv_isr    db     0    ; flag to trap re-entrancy
  964.  
  965. recv_isr:
  966.     cmp    in_recv_isr, 0
  967.     je    no_re_enter
  968.     iret
  969.  
  970. no_re_enter:
  971.     mov    in_recv_isr, 1
  972.  
  973. ; I realize this re-entrancy trap is not perfect, you could be re-entered
  974. ; anytime before the above instruction. However since the stacks have not
  975. ; been swapped re-entrancy here will only appear to be a spurious interrupt.
  976. ; - gft - 910617
  977.  
  978. ; In order to achieve back-to-back packet transmissions, we handle the
  979. ; latency-critical portion of transmit interrupts first.  The xmit
  980. ; interrupt routine should only start the next transmission, but do
  981. ; no other work.  It may only touch ax and dx (the only register necessary
  982. ; for doing "out" instructions) unless it first pushes any other registers
  983. ; itself.
  984.     push    ax
  985.     push    dx
  986.     call    xmit
  987.  
  988. ; Now switch stacks, push remaining registers, and do remaining interrupt work.
  989.     push    ds
  990.     mov    ax,cs            ;ds = cs.
  991.     mov    ds,ax
  992.     assume    ds:code
  993.  
  994.     mov    savesp,sp
  995.     mov    savess,ss
  996.  
  997.     mov    ss,ax
  998.     mov    sp,offset our_stack
  999.     cld
  1000.  
  1001.     push    bx
  1002.     push    cx
  1003.     push    si
  1004.     push    di
  1005.     push    bp
  1006.     push    es
  1007.  
  1008. ; The following comment is wrong in that we now do a specific EOI command,
  1009. ; and because we don't enable interrupts (even though we should).
  1010.  
  1011. ; Chips & Technologies 8259 clone chip seems to be very broken.  If you
  1012. ; send it a Non Specific EOI command, it clears all In Service Register
  1013. ; bits instead of just the one with the highest priority (as the Intel
  1014. ; chip does and clones should do).  This bug causes our interrupt
  1015. ; routine to be reentered if: 1. we reenable processor interrupts;
  1016. ; 2. we reenable device interrupts; 3. a timer or other higher priority
  1017. ; device interrupt now comes in; 4. the new interrupting device uses
  1018. ; a Non Specific EOI; 5. our device interrupts again.  Because of
  1019. ; this bug, we now completely mask our interrupts around the call
  1020. ; to "recv", the real device interrupt handler.  This allows us
  1021. ; to send an EOI instruction to the 8259 early, before we actually
  1022. ; reenable device interrupts.  Since the interrupt is masked, we
  1023. ; are still guaranteed not to get another interrupt from our device
  1024. ; until the interrupt handler returns.  This has another benefit:
  1025. ; we now no longer prevent other devices from interrupting while our
  1026. ; interrupt handler is running.  This is especially useful if we have
  1027. ; other (multiple) packet drivers trying to do low-latency transmits.
  1028.     mov    al,int_no    ; Disable further device interrupts
  1029.     call    maskint
  1030.  
  1031. ; The following is from Bill Rust, <wjr@ftp.com>
  1032. ; this code dismisses the interrupt at the 8259. if the interrupt number
  1033. ;  is > 8 then it requires fondling two PICs instead of just one.
  1034.     mov    al, int_no    ; get hardware int #
  1035.     cmp    al, 8        ; see if its on secondary PIC
  1036.     jg    recv_isr_4
  1037.     add    al, 60h        ; make specific EOI dismissal
  1038.     out    20h, al
  1039.     jmp    recv_isr_3    ; all done
  1040. recv_isr_4:
  1041.     add    al,60h - 8    ; make specific EOI (# between 9 & 15).
  1042.     out    0a0h,al        ; Secondary 8259 (PC/AT only)
  1043.     mov    al,62h        ; Acknowledge on primary 8259.
  1044.     out    20h,al
  1045. recv_isr_3:
  1046.  
  1047. ;    sti                ; Interrupts are now completely safe
  1048.     call    recv
  1049.  
  1050.     cli                ;interrupts *must* be off between
  1051.                     ;here and the stack restore, because
  1052.                     ;if we have one of our interrupts
  1053.                     ;pending, we would trash our stack.
  1054.  
  1055.     mov    al,int_no    ; Now reenable device interrupts
  1056.     call    unmaskint
  1057.  
  1058.     pop    es
  1059.     pop    bp
  1060.     pop    di
  1061.     pop    si
  1062.     pop    cx
  1063.     pop    bx
  1064.  
  1065.     mov    ss,savess
  1066.     mov    sp,savesp
  1067.  
  1068.     pop    ds
  1069.     assume    ds:nothing
  1070.     pop    dx
  1071.     pop    ax
  1072.     mov    in_recv_isr, 0    ; clear the re-entrancy flag - gft - 901617
  1073.     iret
  1074.  
  1075. recv_exiting_flag    db    0    ;nonzero if recv_exiting will be run.
  1076. recv_exiting_addr    dd    ?
  1077.  
  1078.     public    schedule_exiting
  1079. schedule_exiting:
  1080. ;call this routine to schedule a subroutine that gets run after the
  1081. ;recv_isr.  This is done by stuffing routine's address in place
  1082. ;of the recv_isr iret's address.  This routine should push the flags when it
  1083. ;is entered, and should jump to recv_exiting_exit to leave.
  1084. ;enter with ax = address of routine to run.
  1085.     cmp    recv_exiting_flag,0    ;is it already scheduled?
  1086.     jne    schedule_exiting_1    ;yes, don't do it again!
  1087.     inc    recv_exiting_flag    ;set the flag.
  1088.     les    di,savespss        ;make es:di -> their stack.
  1089.     xchg    ax,es:[di].recv_isr_ip    ;stuff our routine's address in,
  1090.     mov    recv_exiting_addr.offs,ax    ;and save the original address.
  1091.     mov    ax,cs
  1092.     xchg    ax,es:[di].recv_isr_cs
  1093.     mov    recv_exiting_addr.segm,ax
  1094. schedule_exiting_1:
  1095.     ret
  1096.  
  1097.     public    recv_exiting_exit
  1098. recv_exiting_exit:
  1099. ;recv_exiting jumps here to exit, after pushing the flags.
  1100.     cli                ;protect the following semaphore.
  1101.     mov    recv_exiting_flag,0
  1102.     push    recv_exiting_addr.segm
  1103.     push    recv_exiting_addr.offs
  1104.     iret
  1105.  
  1106.  
  1107.     public    maskint
  1108. maskint:
  1109.     or    al,al            ;are they using a hardware interrupt?
  1110.     je    maskint_1        ;no, don't mask off the timer!
  1111.  
  1112.     assume    ds:code
  1113.     mov    dx,21h            ;assume the master 8259.
  1114.     cmp    al,8            ;using the slave 8259 on an AT?
  1115.     jb    mask_not_irq2
  1116.     mov    dx,0a1h            ;go disable it on slave 8259
  1117.     sub    al,8
  1118. mask_not_irq2:
  1119.     mov    cl,al
  1120.  
  1121.     in    al,dx            ;disable them on the correct 8259.
  1122.     mov    ah,1            ;set the bit.
  1123.     shl    ah,cl
  1124.     or    al,ah
  1125. ;
  1126. ; 500ns Stall required here, per INTEL documentation for eisa machines
  1127. ; - gft - 910617
  1128. ;
  1129.     push    ax
  1130.     in    al,61h    ; 1.5 - 3 uS should be plenty
  1131.     in    al,61h
  1132.     in    al,61h
  1133.     pop    ax
  1134.     out    dx,al
  1135. maskint_1:
  1136.     ret
  1137.  
  1138.  
  1139.     public    unmaskint
  1140. unmaskint:
  1141. ;exit with cl = 0 if the interrupt had been enabled.
  1142.     assume    ds:code
  1143.     mov    dx,21h            ;assume the master 8259.
  1144.     mov    cl,al
  1145.     cmp    cl,8            ;using the slave 8259 on an AT?
  1146.     jb    unmask_not_irq2        ;no
  1147.     in    al,dx            ;get master mask
  1148.     and    al,not (1 shl 2)    ; and clear slave cascade bit in mask
  1149.     out    dx,al            ;set new master mask (enable slave int)
  1150. ;
  1151. ; 500ns Stall required here, per INTEL documentation for eisa machines
  1152. ; - gft - 910617
  1153. ;
  1154.     push    ax
  1155.     in    al,61h    ; 1.5 - 3 uS should be plenty
  1156.     in    al,61h
  1157.     in    al,61h
  1158.     pop    ax
  1159.     mov    dx,0a1h            ;go enable int on slave 8259
  1160.     sub    cl,8
  1161. unmask_not_irq2:
  1162.  
  1163.     in    al,dx            ;enable interrupts on the correct 8259.
  1164.     mov    ah,1            ;clear the bit.
  1165.     shl    ah,cl
  1166.     mov    cl,al            ;remember the original mask.
  1167.     and    cl,ah
  1168.     not    ah
  1169.     and    al,ah
  1170. ;
  1171. ; 500ns Stall required here, per INTEL documentation for eisa machines
  1172. ; - gft - 910617
  1173. ;
  1174.     push    ax
  1175.     in    al,61h    ; 1.5 - 3 uS should be plenty
  1176.     in    al,61h
  1177.     in    al,61h
  1178.     pop    ax
  1179.     out    dx,al
  1180.  
  1181.     ret
  1182.  
  1183.  
  1184.     public    recv_locate
  1185. recv_locate:
  1186. ;called when we want to determine what to do with a received packet.
  1187. ;enter with es:di -> packet type, dl = packet class.
  1188. ;exit with cy if the packet is not desired, or nc if we know its type.
  1189.  
  1190. ; If -n option take IEEE 802.3 encapsulated packets that could be Novell IPX 
  1191. ; and make them Ethernet encapsulated Novell IPX packets (for PDSHELL).
  1192.     test    flagbyte,N_OPTION
  1193.     jz    not_n_op
  1194.  
  1195. ; Make IEEE 802.3-like packets that could be Novell IPX into BlueBook class
  1196. ; Novell type 8137 packets.
  1197.     cmp    dl,IEEE8023        ;Is this an IEEE 802.3 packet?
  1198.     jne    recv_not_802_3        ;no
  1199.     cmp    word ptr es:[di],0ffffh    ;if this word not ffff
  1200.     jne    recv_not_8137        ;  then not Novell
  1201.     sub    di,2            ; back it up to the 8137 word.
  1202.     mov    es:[di],3781h        ; fake as Novell protocol (8137)
  1203.     mov    dl,BLUEBOOK
  1204.     jmp    short recv_not_8137
  1205. recv_not_802_3:
  1206. ; Convert incoming Ethernet type 8137 IPX packets to type 8138, as with -n in 
  1207. ; effect we can't send type 8137, and it will only confuse Netware.
  1208.     cmp    dl,BLUEBOOK        ;Is this a BLUEBOOK packet?
  1209.     jne    recv_not_8137        ;no, don't change it.
  1210.     cmp    word ptr es:[di],3781h    ;Is it an 8137 packet?
  1211.     jne    recv_not_8137        ;no, don't change it.
  1212.     mov    es:[di],word ptr 3881h    ;yes, mung it slightly.
  1213. recv_not_8137:
  1214. not_n_op:
  1215.  
  1216.     mov    bx,offset handles
  1217. recv_find_1:
  1218.     cmp    [bx].in_use,0        ;is this handle in use?
  1219.     je    recv_find_2        ;no - don't check the type.
  1220.  
  1221.     mov    ax,[bx].receiver.offs    ;do they have a receiver?
  1222.     or    ax,[bx].receiver.segm
  1223.     je    recv_find_2        ;no - they're not serious about it.
  1224.  
  1225. ;per request by the UK people, we match on IEEE 802.3 classes, then types.
  1226. ;for all others, we match on type, then class.  This lets their software work
  1227. ;without breaking BLUEBOOK type length=0 clients.
  1228.     cmp    [bx].class,IEEE8023    ;is this an IEEE 802.3 handle
  1229.     jne    recv_find_7        ;no.
  1230.     cmp    dl,IEEE8023        ;is the packet also IEEE 802.3?
  1231.     jne    recv_find_2        ;no, give up on it now.
  1232. recv_find_7:
  1233.  
  1234.     mov    cx,[bx].packet_type_len    ;compare the packets.
  1235.     lea    si,[bx].packet_type
  1236.     jcxz    recv_find_3        ;if cx is zero, they want them all.
  1237.  
  1238.     cmp    [bx].class, dl        ;is this the right class?
  1239.     jne    recv_find_2        ;no- don't bother
  1240.  
  1241.     push    di
  1242.     repe    cmpsb
  1243.     pop    di
  1244.     je    recv_find_3        ;we've got it!
  1245. recv_find_2:
  1246.     add    bx,(size per_handle)    ;go to the next handle.
  1247.     cmp    bx,offset end_handles
  1248.     jb    recv_find_1
  1249.  
  1250.     add2    packets_dropped,1    ;count it as dropped.
  1251.     stc
  1252.     ret
  1253. recv_find_3:
  1254.     mov    found_handle,bx        ;remember what our handle was.
  1255.     clc
  1256.     ret
  1257.  
  1258.  
  1259.     public    recv_find, recv_found
  1260. recv_find:
  1261. ;called when we want to determine what to do with a received packet.
  1262. ;enter with cx = packet length, es:di -> packet type, dl = packet class.
  1263. ;exit with es:di = 0 if the packet is not desired, or es:di -> packet buffer
  1264. ;  to be filled by the driver.
  1265.     assume    ds:code, es:nothing
  1266.     push    cx            ;preserve packet length.
  1267.     call    recv_locate        ;search for the packet type.
  1268.     pop    cx
  1269.     jc    recv_find_5        ;we didn't find it -- discard it.
  1270. ;
  1271. recv_found:
  1272. ;called to do the first upcall.
  1273. ;exit with es:di = 0 if the packet is not desired, or es:di -> packet buffer
  1274. ;  to be filled by the driver.
  1275.  
  1276.     add2    packets_in,1
  1277.     add2    bytes_in,cx        ;add up the received bytes.
  1278.  
  1279.     mov    bx,found_handle
  1280.     les    di,[bx].receiver    ;remember the receiver upcall.
  1281.     mov    receive_ptr.offs,di
  1282.     mov    receive_ptr.segm,es
  1283.  
  1284.     test    flagbyte,W_OPTION    ;did they select the Windows option?
  1285.     je    recv_find_6        ;no, don't check for the upcall.
  1286.  
  1287. ; does the receiver signature match whats currently in memory?  if not,
  1288. ; jump to fake return
  1289.     push    si
  1290.     push    cx
  1291.     lea    si,[bx].receiver_sig
  1292.     mov    cx,8/2
  1293.     repe    cmpsw
  1294.     pop    cx
  1295.     pop    si
  1296.     je    recv_find_6
  1297. recv_find_5:
  1298.     xor    di,di            ;"return" a null pointer.
  1299.     mov    es,di
  1300.     ret
  1301. recv_find_6:
  1302.  
  1303.     mov    ax,0            ;allocate request.
  1304.     stc                ;with stc, flags must be an odd number
  1305.     push    ax            ; save a number that cant be flags
  1306.     pushf                ;save flags in case iret used.
  1307.     call    receive_ptr        ;ask the client for a buffer.
  1308.     ; on return, flags should be at top of stack. if an IRET has been used,
  1309.     ; then 0 will be at the top of the stack
  1310.     pop    bx
  1311.     cmp    bx,0
  1312.     je    recv_find_4        ;0 is at top of stack
  1313.     add    sp,2
  1314. recv_find_4:
  1315.     ret
  1316.  
  1317.  
  1318.     public    recv_copy
  1319. recv_copy:
  1320. ;called after we have copied the packet into the buffer.
  1321. ;enter with ds:si ->the packet, cx = length of the packet.
  1322. ;preserve bx.
  1323.     assume    ds:nothing, es:nothing
  1324.  
  1325.     push    bx
  1326.     mov    bx,found_handle
  1327.     mov    ax,1            ;store request.
  1328.     clc                ;with clc, flags must be an even number
  1329.     push    ax            ; save a number that can't be flags
  1330.     pushf                ;save flags incase iret used.
  1331.     call    receive_ptr        ;ask the client for a buffer.
  1332.     pop    bx
  1333.     cmp    bx,1            ;if this is a 1, IRET was used.
  1334.     je    recv_copy_1
  1335.     pop    bx
  1336. recv_copy_1:
  1337.     pop    bx
  1338.     ret
  1339.  
  1340.     public    send_queue
  1341. send_queue:
  1342. ; Queue an iocb.
  1343. ; Enter with es:di -> iocb, interrupts disabled.
  1344. ; Destroys ds:si.
  1345.     assume    ds:nothing, es:nothing
  1346.     mov    es:[di].next.offs,0    ; Zero next offset
  1347.     mov    es:[di].next.segm,0    ; Zero next segment
  1348.     mov    si,send_head.offs    ; Queue empty?
  1349.     or    si,send_head.segm
  1350.     jnz    sq_notempty        ; No
  1351.     mov    send_head.offs,di    ; Set head offset
  1352.     mov    send_head.segm,es    ; Set head segment
  1353.     jmp    sq_settail
  1354. sq_notempty:                ; Queue is not empty
  1355.     lds    si,send_tail        ; Get tail segment:offset
  1356.     mov    ds:[si].next.offs,di    ; Set next offset
  1357.     mov    ds:[si].next.segm,es    ; Set next segment
  1358. sq_settail:
  1359.     mov    send_tail.offs,di    ; Set tail offset
  1360.     mov    send_tail.segm,es    ; Set tail segment
  1361.     ret
  1362.  
  1363.  
  1364.     public    send_dequeue
  1365. send_dequeue:
  1366. ; Dequeue an iocb and possibly call its upcall.
  1367. ; Enter with device or processor interrupts disabled, ah = return code.
  1368. ; Exits with es:di -> iocb; destroys ds:si, ax, bx, cx, dx, bp.
  1369.     assume    ds:nothing, es:nothing
  1370.     les    di,send_head        ; Get head segment:offset
  1371.     lds    si,es:[di].next        ; Get next segment:offset
  1372.     mov    send_head.offs, si    ; Set head offset
  1373.     mov    send_head.segm, ds    ; Set head segment
  1374.     or    es:flags[di], DONE    ; Mark done
  1375.     mov    es:ret_code[di], ah    ; Set retcode
  1376.     test    es:[di].flags,CALLME    ; Does he want an upcall?
  1377.     je    send_dequeue_1        ; No.
  1378.     push    es            ; Push iocb segment
  1379.     push    di            ;  and offset
  1380.     clc                ; Clear carry.
  1381.     mov    ax,1            ; Push a number that cant be flags.
  1382.     push    ax
  1383.     pushf                ; Save flags in case iret used.
  1384.     call    es:[di].upcall        ; Call the client.
  1385.     pop    ax            ; Pop first word.
  1386.     cmp    ax,1            ; If this is a 1, IRET was used.
  1387.     je    send_dequeue_2        ; Far return used.
  1388.     add    sp,2            ; Pop flags.
  1389. send_dequeue_2:
  1390.     pop    di            ; Pop iocb segment
  1391.     pop    es            ;  and offset
  1392. send_dequeue_1:
  1393.     ret
  1394.  
  1395.  
  1396. code    ends
  1397.  
  1398.     end    start
  1399.