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

  1. version    equ    1
  2. ;History:306,1
  3.  
  4. ;  Copyright, 1989-1992, Russell Nelson, Crynwr Software
  5.  
  6. ;   This program is free software; you can redistribute it and/or modify
  7. ;   it under the terms of the GNU General Public License as published by
  8. ;   the Free Software Foundation, version 1.
  9. ;
  10. ;   This program is distributed in the hope that it will be useful,
  11. ;   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;   GNU General Public License for more details.
  14. ;
  15. ;   You should have received a copy of the GNU General Public License
  16. ;   along with this program; if not, write to the Free Software
  17. ;   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     include    defs.asm
  20.  
  21. code    segment word public
  22.     assume    cs:code, ds:code
  23.  
  24.     org    2h
  25. phd_memsize    label    word
  26.  
  27.     org    80h
  28. phd_dioa    label    byte
  29.  
  30.     org    100h
  31. start:
  32.     jmp    start_1
  33. copyleft_msg    label    byte
  34.  db "Packet sender version ",'0'+(majver / 10),'0'+(majver mod 10),".",'0'+version," copyright 1990-92, Russell Nelson.",CR,LF
  35.  db "This program is free software; see the file COPYING for details.",CR,LF
  36.  db "NO WARRANTY; see the file COPYING for details.",CR,LF
  37. crlf_msg    db    CR,LF,'$'
  38.  
  39. their_isr    dd    ?
  40. entry_point    db    ?,?,?,?
  41. handle        dw    ?
  42. packet_flag    dw    0
  43. repeat_switch    db    0
  44. quiet_switch    db    0
  45. async_switch    db    0
  46. delay_count    dd    1
  47. delay_counter    dd    0
  48.  
  49. pkt_length    dw    ?,?
  50. pkt_count    dd    1
  51.  
  52. driver_class    db    ?
  53.  
  54. async_iocb1    iocb    <0, 0, DONE>    ;Two iocbs
  55. async_iocb2    iocb    <0, 0, DONE>
  56.  
  57. signature    db    'PKT DRVR',0
  58. signature_len    equ    $-signature
  59.  
  60. no_signature_msg    db    "No packet driver at that address",'$'
  61. usage_msg    db    "usage: pktsend <packet_int_no>",CR,LF
  62.         db    "[-r [-q] | -n number] [ -d delay ] [-f filename | -l length | packet]",'$'
  63. sending_msg    label    byte
  64.     db    "Press space to send packet(s).  Any other key exits.",CR,LF,'$'
  65. repeat_msg    label    byte
  66.     db    "Sending repeat packets.  Any key exits.",CR,LF,'$'
  67. file_not_found    db    "File not found",'$'
  68. read_trouble    db    "Trouble reading the file",'$'
  69. non_number_msg    db    "Non-numeric input found",'$'
  70. mem_trashed_msg    db    CR,LF,"Found trashed memory at ",'$'
  71.  
  72. line_buffer    db    128 dup(?)
  73.  
  74. usage_error:
  75.     mov    dx,offset usage_msg
  76. error:
  77.     mov    ah,9
  78.     int    21h
  79.     int    20h
  80.  
  81. start_1:
  82.     mov    dx,offset copyleft_msg
  83.     mov    ah,9
  84.     int    21h
  85.  
  86.     mov    si,offset phd_dioa+1
  87.     call    skip_blanks
  88.     cmp    al,CR            ;end of line?
  89.     je    usage_error
  90.  
  91.     mov    di,offset entry_point
  92.     call    get_number
  93.  
  94. another_switch:
  95.     call    skip_blanks
  96.     cmp    al,'-'            ;did they specify a switch?
  97.     je    have_switch
  98.     jmp    not_switch
  99. have_switch:
  100.     cmp    byte ptr [si+1],'r'    ;did they specify '-r'?
  101.     je    got_repeat_switch
  102.     cmp    byte ptr [si+1],'q'    ;did they specify '-q'?
  103.     je    got_quiet_switch
  104.     cmp    byte ptr [si+1],'n'    ;did they specify '-n'?
  105.     je    got_number_switch
  106.     cmp    byte ptr [si+1],'l'    ;did they specify '-l'?
  107.     je    got_length_switch
  108.     cmp    byte ptr [si+1],'f'    ;did they specify '-f'?
  109.     je    start_file
  110.     cmp    byte ptr [si+1],'a'    ;did they specify '-a'?
  111.     je    got_async_switch
  112.     cmp    byte ptr [si+1],'d'    ;did they specify '-d'?
  113.     je    got_delay_switch
  114.     jmp    usage_error        ;no, must be an error.
  115. got_repeat_switch:
  116.     mov    repeat_switch,1
  117.     add    si,2
  118.     jmp    another_switch
  119. got_quiet_switch:
  120.     mov    quiet_switch,1
  121.     add    si,2
  122.     jmp    another_switch
  123. got_number_switch:
  124.     add    si,2
  125.     mov    di,offset pkt_count
  126.     call    get_number
  127.     jmp    another_switch
  128. got_delay_switch:
  129.     add    si,2
  130.     mov    di,offset delay_count
  131.     call    get_number
  132.     mov    ax,delay_count.offs
  133.     or    ax,delay_count.segm
  134.     jne    another_switch
  135.     mov    delay_count.offs,1
  136.     mov    delay_count.segm,0
  137.     jmp    another_switch
  138. got_length_switch:
  139.     add    si,2
  140.     mov    di,offset pkt_length
  141.     call    get_number
  142.     mov    di,offset our_buffer
  143.     add    di,pkt_length
  144.     jmp    start_gotit
  145. got_async_switch:
  146.     mov    async_switch,1
  147.     add    si,2
  148.     jmp    another_switch
  149. not_switch:
  150.     jmp    start_inline        ;yes.
  151.  
  152. start_file:
  153.     add    si,2
  154.     call    skip_blanks
  155.     mov    dx,si            ;remember where the filename starts.
  156. start_3:
  157.     lodsb
  158.     cmp    al,' '
  159.     je    start_4
  160.     cmp    al,CR
  161.     jne    start_3
  162. start_4:
  163.     dec    si
  164.     mov    byte ptr [si],0
  165.  
  166. ;read the packet bytes from the named file.
  167.  
  168.     mov    ax,3d00h        ;open for reading.
  169.     int    21h
  170.     jnc    file_found
  171.     mov    dx,offset file_not_found
  172.     jmp    error
  173.  
  174. file_found:
  175.     mov    handle,ax
  176.     mov    di,offset our_buffer
  177. start_line:
  178.     mov    si,offset line_buffer
  179. again_line:
  180.     mov    ah,3fh            ;read a single character.
  181.     mov    bx,handle
  182.     mov    cx,1
  183.     mov    dx,si
  184.     int    21h
  185.     jnc    no_trouble
  186.     mov    dx,offset read_trouble
  187.     jmp    error
  188.  
  189. no_trouble:
  190.     cmp    ax,1            ;did we actually read one?
  191.     je    not_eof
  192.     cmp    si,offset line_buffer    ;did we read anything this time?
  193.     je    start_file_eof        ;no, it's really eof this time.
  194.     mov    [si],byte ptr CR    ;add an extra CR, just in case.
  195.     jmp    short done_reading
  196. not_eof:
  197.     lodsb                ;get the character we just read.
  198.     cmp    al,1ah            ;^Z?
  199.     je    start_file_eof        ;yes, eof now.
  200.     cmp    al,LF            ;got the LF?
  201.     jne    again_line        ;no, read again.
  202.  
  203. done_reading:
  204.     mov    si,offset line_buffer
  205. again_chars:
  206.     call    get_number
  207.     jnc    got_a_number
  208.     mov    dx,offset non_number_msg
  209.     jmp    error
  210. got_a_number:
  211.     inc    di
  212.     call    skip_blanks
  213.     cmp    al,CR
  214.     jne    again_chars        ;keep going to the end.
  215.  
  216.     jmp    start_line
  217.  
  218. start_file_eof:
  219.     mov    ah,3eh            ;close the file.
  220.     mov    bx,handle
  221.     int    21h
  222.     jmp    short start_gotit
  223.  
  224. start_inline:
  225. ;read the packet bytes off the command line.
  226.     mov    di,offset our_buffer-1
  227. start_2:
  228.     inc    di            ;pre-increment
  229.     call    get_number        ;get a byte.
  230.     jnc    start_2            ;keep going to the end.
  231.  
  232. start_gotit:
  233.  
  234.     sub    di,offset our_buffer
  235.     mov    pkt_length,di
  236.  
  237.     mov    sp,offset start        ;now that we're finished with
  238.                     ;the parameters, put our stack there.
  239.  
  240.     mov    di,offset our_buffer + GIANT * 2
  241.     inc    di
  242.     and    di,not 1
  243.     movseg    es,cs
  244. init_memory:
  245.     mov    ax,055aah        ;initialize a segment to 055aah.
  246.     mov    cx,di
  247.     neg    cx
  248.     shr    cx,1
  249.     stosw
  250.     sub    cx,2
  251.     rep    stosw
  252.     mov    ax,es
  253.     add    ax,1000h
  254.     mov    es,ax
  255.     add    ax,1000h
  256.     cmp    ax,phd_memsize        ;don't go past the end of memory.
  257.     jbe    init_memory
  258.  
  259.     mov    ah,35h            ;get their packet interrupt.
  260.     mov    al,entry_point
  261.     int    21h
  262.     mov    their_isr.offs,bx
  263.     mov    their_isr.segm,es
  264.  
  265.     lea    di,3[bx]
  266.     mov    si,offset signature
  267.     mov    cx,signature_len
  268.     repe    cmpsb
  269.     je    signature_ok
  270.     jmp    no_signature_err
  271. signature_ok:
  272.  
  273.     mov    ax,1ffh            ;driver_info
  274.     call    int_pkt
  275.     call    fatal_error
  276.     mov    driver_class,ch
  277.  
  278.     mov    ah,2            ;access all packets.
  279.     mov    al,ch            ;their class from driver_info().
  280.     mov    bx,dx            ;their type from driver_info().
  281.     mov    dl,cl            ;their number from driver_info().
  282.     mov    cx,0            ;type length of zero.
  283.     movseg    es,cs
  284.     mov    di,offset our_recv
  285.     call    int_pkt
  286.     call    fatal_error
  287.     mov    handle,ax
  288.  
  289.     cmp    driver_class,1        ;Ethernet II?
  290.     je    get_address
  291.     cmp    driver_class,11        ;IEEE 802.3?
  292.     jne    no_get_address
  293. get_address:
  294.     mov    ah,6            ;get the destination's address.
  295.     mov    bx,handle
  296.     mov    cx,EADDR_LEN
  297.     movseg    es,cs
  298.     mov    di,offset our_buffer+6    ;set the 'from' address.
  299.     call    int_pkt
  300.     call    fatal_error
  301. no_get_address:
  302.  
  303.     cmp    repeat_switch,0
  304.     je    send_not_repeat
  305.     mov    dx,offset repeat_msg
  306.     mov    ah,9
  307.     int    21h
  308.     jmp    short load_count
  309. send_not_repeat:
  310.     mov    dx,offset sending_msg
  311.     mov    ah,9
  312.     int    21h
  313. wait_for_event:
  314.  
  315. ; Did we receive a packet?
  316.  
  317.     cmp    packet_flag,0
  318.     je    no_packet        ;no.
  319.  
  320.     mov    al,'R'
  321.     call    chrout
  322.  
  323.     mov    packet_flag,0
  324.  
  325. no_packet:
  326.  
  327. ; Did they press a key?
  328.  
  329.     mov    ah,1
  330.     int    16h
  331.     jz    no_key            ;no.
  332.  
  333.     mov    ah,0            ;get the key.
  334.     int    16h
  335.  
  336.     cmp    repeat_switch,0        ;are they repeating?
  337.     jne    no_packet_1        ;yes, any key means exit.
  338.  
  339.     cmp    al,' '            ;space means retransmit.
  340.     je    load_count
  341. no_packet_1:
  342.     jmp    send_done        ;send a packet.
  343.  
  344. no_key:
  345.  
  346. ; Time to send another packet?
  347.  
  348.     mov    ax,delay_counter.segm
  349.     or    ax,delay_counter.offs
  350.     jne    count_not_zero
  351.  
  352.     cmp    repeat_switch,0        ;are we auto-repeating?
  353.     je    wait_for_event        ;no, wait for an event.
  354.  
  355. load_count:
  356.     mov    ax,delay_count.offs
  357.     mov    dx,delay_count.segm
  358.     mov    delay_counter.offs,ax
  359.     mov    delay_counter.segm,dx
  360.  
  361. count_not_zero:
  362.  
  363. ; Finished with delay?
  364.  
  365.     dec    delay_counter.offs
  366.     jne    wait_for_event
  367.     cmp    delay_counter.segm,0
  368.     je    send_delay_1
  369.     dec    delay_counter.segm
  370.     jne    wait_for_event
  371. send_delay_1:
  372.  
  373.     mov    ax,pkt_count.offs    ;send another series of packets.
  374.     mov    dx,pkt_count.segm
  375.  
  376. send_again:
  377.     push    ax
  378.     push    dx
  379.  
  380.     mov    ah,4            ;send_pkt
  381.     mov    si,offset our_buffer    ;ds:si -> buffer.
  382.     mov    cx,pkt_length
  383.     cmp    async_switch,0        ;async?
  384.     je    send_pkt        ;no
  385.     mov    ah,12            ;as_send_pkt
  386. find_iocb:                ;find a free iocb
  387.     mov    di,offset async_iocb1    ;try first
  388.     test    [di].flags,DONE        ;free?
  389.     jnz    got_iocb        ;yes
  390.     mov    di,offset async_iocb2    ;no, try second
  391.     test    [di].flags,DONE        ;free?
  392.     jz    find_iocb        ;no, try first again
  393. got_iocb:                ;set up iocb
  394.     mov    word ptr [di].buffer,si
  395.     mov    word ptr [di].buffer+2,ds
  396.     mov    es,word ptr [di].buffer+2
  397.     mov    [di].len,cx
  398.     mov    [di].flags,0        ;clear flags (i.e. DONE bit)
  399. send_pkt:
  400.     call    int_pkt
  401.     jnc    pkt_sent_ok
  402.     mov    al,'X'
  403.     call    chrout
  404.     cmp    dh,CANT_SEND        ;did the driver just complain a little?
  405.     je    pkt_sent_ok        ;yes, pretend all is okay.
  406.     jmp    pkt_sent_bad
  407. pkt_sent_ok:
  408.     pop    dx
  409.     pop    ax
  410.     sub    ax,1            ;decrement low word
  411.     sbb    dx,0            ;decrement high word
  412.     mov    bx,ax            ;check if we reached zero
  413.     or    bx,dx
  414.     jne    send_again        ;we didn't
  415.  
  416. check_repeat:
  417.     cmp    quiet_switch,0
  418.     jne    chrout_done
  419.     mov    al,'T'
  420.     call    chrout
  421.  
  422. chrout_done:
  423.     jmp    wait_for_event
  424.  
  425.  
  426. send_done:
  427.     mov    ah,3            ;release the handle.
  428.     mov    bx,handle
  429.     call    int_pkt
  430.     call    fatal_error
  431.  
  432.     mov    di,offset our_buffer + GIANT * 2
  433.     inc    di
  434.     and    di,not 1
  435.     movseg    es,cs
  436. compare_memory:
  437.     mov    ax,055aah        ;compare a segment against 055aah.
  438.     mov    cx,di
  439.     neg    cx
  440.     shr    cx,1
  441.     scasw
  442.     jne    memory_bad
  443.     sub    cx,2
  444.     repe    scasw
  445.     jne    memory_bad
  446.     mov    ax,es
  447.     add    ax,1000h
  448.     mov    es,ax
  449.     add    ax,1000h
  450.     cmp    ax,phd_memsize        ;don't go past the end of memory.
  451.     jbe    compare_memory
  452.  
  453.     int    20h
  454. pkt_sent_bad:
  455.     call    print_error
  456.  
  457.     mov    ah,3            ;release the handle.
  458.     mov    bx,handle
  459.     call    int_pkt
  460.     call    fatal_error
  461.  
  462.     int    20h
  463.  
  464. memory_bad:
  465.     mov    dx,offset mem_trashed_msg
  466.     mov    ah,9
  467.     int    21h
  468.     mov    ax,es
  469.     call    wordout
  470.     mov    al,':'
  471.     call    chrout
  472.     mov    ax,di
  473.     dec    ax
  474.     call    wordout
  475.     mov    dx,offset crlf_msg
  476.     mov    ah,9
  477.     int    21h
  478.     int    20h
  479.  
  480. no_signature_err:
  481.     mov    dx,offset no_signature_msg
  482.     mov    ah,9
  483.     int    21h
  484.     int    20h
  485.  
  486.  
  487. our_recv:
  488.     or    ax,ax            ;first or second call?
  489.     jne    our_recv_1        ;second -- we ignore the packet
  490.     cmp    cs:packet_flag,0    ;Do we already have one?
  491.     jne    our_recv_2        ;yes - return zero.
  492.     movseg    es,cs
  493.     mov    di,offset our_buffer + GIANT
  494.     retf
  495. our_recv_2:
  496.     xor    di,di
  497.     mov    es,ax
  498.     mov    cx,0
  499.     loop    $
  500.     retf
  501. our_recv_1:
  502.     inc    cs:packet_flag
  503.     retf
  504.  
  505.  
  506. int_pkt:
  507.     push    ds
  508.     push    es
  509.     pushf
  510.     cli
  511.     call    their_isr
  512.     pop    es
  513.     pop    ds
  514.     ret
  515.  
  516.     include    pkterr.asm
  517.     include    getnum.asm
  518.     include    getdig.asm
  519.     include    skipblk.asm
  520.     include    chrout.asm
  521.     include    digout.asm
  522.  
  523.     align    4
  524. our_buffer    label    byte
  525.  
  526. code    ends
  527.  
  528.     end    start
  529.