home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / packetdrivers.tar.gz / pd.tar / src / head.asm < prev    next >
Assembly Source File  |  1995-06-25  |  36KB  |  1,445 lines

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