Subversion Repositories Kolibri OS

Rev

Rev 109 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;
  2. ;    TFTP Wave Player
  3. ;
  4. ;    Compile with FASM for Menuet
  5. ;
  6. ;
  7. ;    12.7.2002 - Audio system calls by VT
  8. ;
  9.    
  10. use32
  11.  org    0x0
  12.  db     'MENUET01'    ; header
  13.  dd     0x01          ; header version
  14.  dd     START         ; entry point
  15.  dd     I_END         ; image size
  16.  dd     I_END+0x10000 ; required memory
  17.  dd     I_END+0x10000 ; esp
  18.  dd     0x0 , 0x0     ; I_Param , I_Path
  19.  
  20. include 'lang.inc'
  21. include 'macros.inc'
  22.    
  23. delay      dd  145
  24. wait_for   dd  0x0
  25.    
  26. START:                          ; start of execution
  27.    
  28.     mov  dword [prompt], p9
  29.     mov  dword [promptlen], p9len - p9
  30.    
  31.     call draw_window            ; at first, draw the window
  32.    
  33. still:
  34.    
  35.     mov  eax,10                 ; wait here for event
  36.     int  0x40
  37.    
  38.     cmp  eax,1                  ; redraw request ?
  39.     jz   red
  40.     cmp  eax,2                  ; key in buffer ?
  41.     jz   key
  42.     cmp  eax,3                  ; button in buffer ?
  43.     jz   button
  44.    
  45.     jmp  still
  46.    
  47. red:                           ; redraw
  48.     call draw_window
  49.     jmp  still
  50.    
  51. key:                           ; Keys are not valid at this part of the
  52.     mov  eax,2                  ; loop. Just read it and ignore
  53.     int  0x40
  54.     jmp  still
  55.    
  56. button:                        ; button
  57.     mov  eax,17                 ; get id
  58.     int  0x40
  59.    
  60.     cmp  ah,1                   ; button id=1 ?
  61.     jnz  noclose
  62.    
  63.    
  64.     ; close socket before exiting
  65.  mov  eax, 53
  66.  mov  ebx, 1
  67.  mov  ecx, [socketNum]
  68.     int   0x40
  69.    
  70.  mov  [socketNum], dword 0
  71.    
  72.    
  73.     mov  eax,0xffffffff         ; close this program
  74.     int  0x40
  75.    
  76. noclose:
  77.     cmp  ah,2                   ; copy file to local machine?
  78.     jnz  nocopyl
  79.    
  80.     mov   dword [prompt], p5
  81.     mov  dword [promptlen], p5len - p5
  82.     call  draw_window            ;
  83.    
  84.     ; Copy File from Remote Host to this machine
  85.     call translateData  ; Convert Filename & IP address
  86.     mov  edi, tftp_filename + 1
  87.     mov  [edi], byte 0x01 ; setup tftp msg
  88.     call copyFromRemote
  89.    
  90.     jmp  still
  91.    
  92. nocopyl:
  93.    
  94.    
  95.     cmp  ah,4
  96.     jz   f1
  97.     cmp  ah,5
  98.     jz   f2
  99.     jmp  nof12
  100.    
  101.   f1:
  102.     mov  [addr],dword source
  103.     mov  [ya],dword 35
  104.     jmp  rk
  105.    
  106.   f2:
  107.     mov  [addr],dword destination
  108.     mov  [ya],dword 35+16
  109.    
  110.   rk:
  111.     mov  ecx,15
  112.     mov  edi,[addr]
  113.     mov  al,' '
  114.     rep  stosb
  115.    
  116.     call print_text
  117.    
  118.     mov  edi,[addr]
  119.    
  120.   f11:
  121.     mov  eax,10
  122.     int  0x40
  123.     cmp  eax,2
  124.     jz   fbu
  125.     jmp  still
  126.   fbu:
  127.     mov  eax,2
  128.     int  0x40  ; get key
  129.     shr  eax,8
  130.     cmp  eax,8
  131.     jnz  nobs
  132.     cmp  edi,[addr]
  133.     jz   f11
  134.     sub  edi,1
  135.     mov  [edi],byte ' '
  136.     call print_text
  137.     jmp  f11
  138.   nobs:
  139.     cmp  eax,dword 31
  140.     jbe  f11
  141.     cmp  eax,dword 95
  142.     jb   keyok
  143.     sub  eax,32
  144.   keyok:
  145.     mov  [edi],al
  146.    
  147.     call print_text
  148.    
  149.     add  edi,1
  150.     mov  esi,[addr]
  151.     add  esi,15
  152.     cmp  esi,edi
  153.     jnz  f11
  154.    
  155.     jmp  still
  156.    
  157. print_text:
  158.    
  159.     mov  eax,13
  160.     mov  ebx,103*65536+15*6
  161.     mov  ecx,[ya]
  162.     shl  ecx,16
  163.     mov  cx,8
  164.     mov  edx,0x224466
  165.     int  0x40
  166.    
  167.     mov  eax,4
  168.     mov  ebx,103*65536
  169.     add  ebx,[ya]
  170.     mov  ecx,0xffffff
  171.     mov  edx,[addr]
  172.     mov  esi,15
  173.     int  0x40
  174.    
  175.     ret
  176.    
  177.    
  178.   nof12:
  179.     jmp  still
  180.    
  181.    
  182. ;***************************************************************************
  183. ;   Function
  184. ;      translateData
  185. ;
  186. ;   Description
  187. ;      Coverts the filename and IP address typed in by the user into
  188. ;      a format suitable for the IP layer.
  189. ;
  190. ;    The filename, in source, is converted and stored in tftp_filename
  191. ;      The host ip, in destination, is converted and stored in tftp_IP
  192. ;
  193. ;***************************************************************************
  194. translateData:
  195.    
  196.  ; first, build up the tftp command string. This includes the filename
  197.  ; and the transfer protocol
  198.    
  199.    
  200.  ; First, write 0,0
  201.  mov  al, 0
  202.  mov  edi, tftp_filename
  203.  mov  [edi], al
  204.  inc  edi
  205.  mov  [edi], al
  206.  inc  edi
  207.    
  208.  ; Now, write the file name itself, and null terminate it
  209.  mov  ecx, 15
  210.  mov  ah, ' '
  211.  mov  esi, source
  212.    
  213. td001:
  214.  lodsb
  215.  stosb
  216.  cmp  al, ah
  217.  loopnz td001
  218.    
  219.  cmp  al,ah  ; Was the entire buffer full of characters?
  220.  jne  td002
  221.  dec  edi   ; No - so remove ' ' character
  222.    
  223. td002:
  224.  mov  [edi], byte 0
  225.  inc  edi
  226.  mov  [edi], byte 'O'
  227.  inc  edi
  228.  mov  [edi], byte 'C'
  229.  inc  edi
  230.  mov  [edi], byte 'T'
  231.  inc  edi
  232.  mov  [edi], byte 'E'
  233.  inc  edi
  234.  mov  [edi], byte 'T'
  235.  inc  edi
  236.  mov  [edi], byte 0
  237.    
  238.  mov  esi, tftp_filename
  239.  sub  edi, esi
  240.  mov  [tftp_len], edi
  241.    
  242.    
  243.  ; Now, convert the typed IP address into a real address
  244.  ; No validation is done on the number entered
  245.  ; ip addresses must be typed in normally, eg
  246.  ; 192.1.45.24
  247.    
  248.  xor  eax, eax
  249.  mov  dh, 10
  250.  mov  dl, al
  251.  mov  [tftp_IP], eax
  252.    
  253.  ; 192.168.24.1   1.1.1.1       1. 9.2.3.
  254.    
  255.  mov  esi, destination
  256.  mov  edi, tftp_IP
  257.    
  258.  mov  ecx, 4
  259.    
  260. td003:
  261.  lodsb
  262.  sub  al, '0'
  263.  add  dl, al
  264.  lodsb
  265.  cmp  al, '.'
  266.  je  ipNext
  267.  cmp  al, ' '
  268.  je  ipNext
  269.  mov  dh, al
  270.  sub  dh, '0'
  271.  mov  al, 10
  272.  mul  dl
  273.  add  al, dh
  274.  mov  dl, al
  275.  lodsb
  276.  cmp  al, '.'
  277.  je  ipNext
  278.  cmp  al, ' '
  279.  je  ipNext
  280.  mov  dh, al
  281.  sub  dh, '0'
  282.  mov  al, 10
  283.  mul  dl
  284.  add  al, dh
  285.  mov  dl, al
  286.  lodsb
  287.    
  288. ipNext:
  289.  mov  [edi], dl
  290.  inc  edi
  291.  mov  dl, 0
  292.  loop td003
  293.    
  294.  ret
  295.    
  296.    
  297.    
  298. ;***************************************************************************
  299. ;   Function
  300. ;      copyFromRemote
  301. ;
  302. ;   Description
  303. ;
  304. ;***************************************************************************
  305. copyFromRemote:
  306.    
  307.  mov  eax,0x20000-512
  308.  mov  [fileposition], eax
  309.    
  310.  ; Get a random # for the local socket port #
  311.  mov  eax, 3
  312.  int  0x40
  313.  mov  ecx, eax
  314.  shr  ecx, 8    ; Set up the local port # with a random #
  315.    
  316.    ; open socket
  317.  mov  eax, 53
  318.  mov  ebx, 0
  319.  mov  edx, 69    ; remote port
  320.  mov  esi, [tftp_IP]  ; remote IP ( in intenet format )
  321.  int  0x40
  322.    
  323.  mov  [socketNum], eax
  324.    
  325.  ; make sure there is no data in the socket - there shouldn't be..
  326.    
  327. cfr001:
  328.  mov  eax, 53
  329.  mov  ebx, 3
  330.  mov  ecx, [socketNum]
  331.  int  0x40    ; read byte
  332.    
  333.  mov  eax, 53
  334.  mov  ebx, 2
  335.  mov  ecx, [socketNum]
  336.  int  0x40    ; any more data?
  337.    
  338.  cmp  eax, 0
  339.  jne  cfr001    ; yes, so get it
  340.    
  341.  ; Now, request the file
  342.  mov  eax, 53
  343.  mov  ebx, 4
  344.  mov  ecx, [socketNum]
  345.  mov  edx, [tftp_len]
  346.  mov  esi, tftp_filename
  347.  int  0x40
  348.    
  349. cfr002:
  350.    
  351.     mov  eax,23                 ; wait here for event
  352.     mov  ebx,1                  ; Time out after 10ms
  353.     int  0x40
  354.    
  355.     cmp  eax,1                  ; redraw request ?
  356.     je   cfr003
  357.     cmp  eax,2                  ; key in buffer ?
  358.     je   cfr004
  359.     cmp  eax,3                  ; button in buffer ?
  360.     je   cfr005
  361.    
  362.     ; Any data to fetch?
  363.  mov  eax, 53
  364.  mov  ebx, 2
  365.  mov  ecx, [socketNum]
  366.  int   0x40
  367.    
  368.  cmp  eax, 0
  369.  je  cfr002
  370.    
  371.  push eax     ; eax holds # chars
  372.    
  373.  ; Update the text on the display - once
  374.  mov  eax, [prompt]
  375.  cmp  eax, p3
  376.  je  cfr008
  377.  mov   dword [prompt], p3
  378.  mov  dword [promptlen], p3len - p3
  379.  call  draw_window            ;
  380.    
  381. cfr008:
  382.  ; we have data - this will be a tftp frame
  383.    
  384.  ; read first two bytes - opcode
  385.  mov  eax, 53
  386.  mov  ebx, 3
  387.  mov  ecx, [socketNum]
  388.  int  0x40   ; read byte
  389.    
  390.  mov  eax, 53
  391.  mov  ebx, 3
  392.  mov  ecx, [socketNum]
  393.  int  0x40   ; read byte
  394.    
  395.  pop  eax
  396.  ; bl holds tftp opcode. Can only be 3 (data) or 5 ( error )
  397.    
  398.  cmp  bl, 3
  399.  jne  cfrerr
  400.    
  401.  push eax
  402.    
  403.  ; do data stuff. Read block #. Read data. Send Ack.
  404.  mov  eax, 53
  405.  mov  ebx, 3
  406.  mov  ecx, [socketNum]
  407.  int  0x40   ; read byte
  408.    
  409.  mov  [blockNumber], bl
  410.    
  411.  mov  eax, 53
  412.  mov  ebx, 3
  413.  mov  ecx, [socketNum]
  414.  int  0x40   ; read byte
  415.    
  416.  mov  [blockNumber+1], bl
  417.    
  418. cfr007:
  419.  mov  eax, 53
  420.  mov  ebx, 3
  421.  mov  ecx, [socketNum]
  422.  int  0x40   ; read byte
  423.    
  424.  mov  esi, [fileposition]
  425.  mov  [esi], bl
  426.  mov  [esi+1],bl
  427.  add  dword [fileposition],2
  428.    
  429.  mov  eax, 53
  430.  mov  ebx, 2
  431.  mov  ecx, [socketNum]
  432.  int  0x40   ; any more data?
  433.    
  434.  cmp  eax, 0
  435.  jne  cfr007  ; yes, so get it
  436.    
  437.  cmp  [fileposition],0x20000+0xffff
  438.  jb   get_more_stream
  439.    
  440. wait_more:
  441.    
  442.  mov  eax,5    ; wait for correct timer position
  443.                ; to trigger new play block
  444.  mov  ebx,1
  445.  int  0x40
  446.    
  447.  mov  eax,26
  448.  mov  ebx,9
  449.  int  0x40
  450.    
  451.  cmp  eax,[wait_for]
  452.  jb   wait_more
  453.    
  454.  add  eax,[delay]
  455.  mov  [wait_for],eax
  456.    
  457.  mov  esi,0x20000
  458.  mov  edi,0x10000
  459.  mov  ecx,65536
  460.  cld
  461.  rep  movsb
  462.    
  463.  mov  eax,55
  464.  mov  ebx,0
  465.  mov  ecx,0x10000
  466.  int  0x40
  467.    
  468.  mov  eax,55
  469.  mov  ebx,1
  470.  int  0x40
  471.    
  472.  mov  [fileposition],0x20000
  473.    
  474. get_more_stream:
  475.    
  476.  ; write the block number into the ack
  477.  mov  al, [blockNumber]
  478.  mov  [ack + 2], al
  479.    
  480.  mov  al, [blockNumber+1]
  481.  mov  [ack + 3], al
  482.    
  483.  ; send an 'ack'
  484.  mov  eax, 53
  485.  mov  ebx, 4
  486.  mov  ecx, [socketNum]
  487.  mov  edx, ackLen - ack
  488.  mov  esi, ack
  489.  int   0x40
  490.    
  491.  ; If # of chars in the frame is less that 516,
  492.  ; this frame is the last
  493.  pop  eax
  494.  cmp  eax, 516
  495.  je  cfr002
  496.    
  497.  ; Write the file
  498.  mov  eax, 33
  499.  mov  ebx, source
  500.  mov  edx, [filesize]
  501.  mov  ecx, I_END + 512
  502.  mov  esi, 0
  503.  int  0x40
  504.    
  505.  jmp  cfrexit
  506.    
  507. cfrerr:
  508.  ; simple implementation on error - just read all data, and return
  509.  mov  eax, 53
  510.  mov  ebx, 3
  511.  mov  ecx, [socketNum]
  512.     int   0x40    ; read byte
  513.    
  514.  mov  eax, 53
  515.  mov  ebx, 2
  516.  mov  ecx, [socketNum]
  517.     int   0x40    ; any more data?
  518.    
  519.  cmp  eax, 0
  520.  jne  cfrerr    ; yes, so get it
  521.    
  522.  jmp  cfr006    ; close socket and close app
  523.    
  524. cfr003:                         ; redraw request
  525.     call draw_window
  526.     jmp  cfr002
  527.    
  528. cfr004:                         ; key pressed
  529.     mov  eax,2                  ; just read it and ignore
  530.     int  0x40
  531.     jmp  cfr002
  532.    
  533. cfr005:                        ; button
  534.     mov  eax,17                 ; get id
  535.     int  0x40
  536.    
  537.     cmp  ah,1                   ; button id=1 ?
  538.     jne  cfr002     ; If not, ignore.
  539.    
  540. cfr006:
  541.     ; close socket
  542.  mov  eax, 53
  543.  mov  ebx, 1
  544.  mov  ecx, [socketNum]
  545.     int   0x40
  546.    
  547.  mov  [socketNum], dword 0
  548.    
  549.     mov  eax,-1                 ; close this program
  550.     int  0x40
  551.    
  552.     jmp $
  553.    
  554. cfrexit:
  555.     ; close socket
  556.  mov  eax, 53
  557.  mov  ebx, 1
  558.  mov  ecx, [socketNum]
  559.     int   0x40
  560.    
  561.  mov  [socketNum], dword 0
  562.    
  563.     mov   dword [prompt], p4
  564.     mov  dword [promptlen], p4len - p4
  565.     call  draw_window            ;
  566.    
  567.  ret
  568.    
  569.    
  570.    
  571.    
  572. ;   *********************************************
  573. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  574. ;   *********************************************
  575.    
  576.    
  577. draw_window:
  578.    
  579.     mov  eax,12                    ; function 12:tell os about windowdraw
  580.     mov  ebx,1                     ; 1, start of draw
  581.     int  0x40
  582.    
  583.                                    ; DRAW WINDOW
  584.     mov  eax,0                     ; function 0 : define and draw window
  585.     mov  ebx,100*65536+230         ; [x start] *65536 + [x size]
  586.     mov  ecx,100*65536+170         ; [y start] *65536 + [y size]
  587.     mov  edx,0x13224466            ; color of work area RRGGBB
  588.     mov  edi,labelt
  589.     int  0x40
  590.    
  591.  
  592.     mov  eax,8              ; COPY BUTTON
  593.     mov  ebx,20*65536+190
  594.     mov  ecx,79*65536+15
  595.     mov  edx,3
  596.     mov  esi,0x557799
  597. ;    int  0x40
  598.    
  599.     mov  eax,8              ; DELETE BUTTON
  600.     mov  ebx,20*65536+190
  601.     mov  ecx,111*65536+15
  602.     mov  edx,2
  603.     mov  esi,0x557799
  604.     int  0x40
  605.    
  606.     mov  eax,8
  607.     mov  ebx,200*65536+10
  608.     mov  ecx,34*65536+10
  609.     mov  edx,4
  610.     mov  esi,0x557799
  611.     int  0x40
  612.    
  613.     mov  eax,8
  614.     mov  ebx,200*65536+10
  615.     mov  ecx,50*65536+10
  616.     mov  edx,5
  617.     mov  esi,0x557799
  618.     int  0x40
  619.    
  620.    
  621.  ; Copy the file name to the screen buffer
  622.  ; file name is same length as IP address, to
  623.  ; make the math easier later.
  624.     cld
  625.     mov  esi,source
  626.     mov  edi,text+13
  627.     mov  ecx,15
  628.     rep  movsb
  629.    
  630.    
  631.  ; copy the IP address to the screen buffer
  632.     mov  esi,destination
  633.     mov  edi,text+40+13
  634.     mov  ecx,15
  635.     rep  movsb
  636.    
  637.   ; copy the prompt to the screen buffer
  638.     mov  esi,[prompt]
  639.     mov  edi,text+280
  640.     mov  ecx,[promptlen]
  641.     rep  movsb
  642.    
  643.     ; Re-draw the screen text
  644.     cld
  645.     mov  ebx,25*65536+35           ; draw info text with function 4
  646.     mov  ecx,0xffffff
  647.     mov  edx,text
  648.     mov  esi,40
  649.   newline:
  650.     mov  eax,4
  651.     int  0x40
  652.     add  ebx,16
  653.     add  edx,40
  654.     cmp  [edx],byte 'x'
  655.     jnz  newline
  656.    
  657.    
  658.     mov  eax,12                    ; function 12:tell os about windowdraw
  659.     mov  ebx,2                     ; 2, end of draw
  660.     int  0x40
  661.    
  662.     ret
  663.    
  664.    
  665. ; DATA AREA
  666.    
  667. source       db  'HEAT8M22.WAV   '
  668. destination  db  '192.168.1.24   '
  669.    
  670.    
  671. tftp_filename:  times 15 + 9 db 0
  672. tftp_IP:   dd 0
  673. tftp_len:   dd 0
  674.    
  675. addr  dd  0x0
  676. ya    dd  0x0
  677.    
  678. fileposition dd 0 ; Points to the current point in the file
  679. filesize  dd 0 ; The number of bytes written / left to write
  680. fileblocksize dw 0 ; The number of bytes to send in this frame
  681.    
  682. text:
  683.     db 'SOURCE FILE: xxxxxxxxxxxxxxx            '
  684.     db 'HOST IP ADD: xxx.xxx.xxx.xxx            '
  685.     db '                                        '
  686.     db 'WAVE FORMAT: 8 BIT,MONO,22050HZ         '
  687.     db '                                        '
  688.     db '     SERVER -> PLAY FILE                '
  689.     db '                                        '
  690.     db '                                        '
  691.     db 'x' ; <- END MARKER, DONT DELETE
  692.    
  693.    
  694. labelt  db   'TFTP Wave Player',0  
  695.    
  696. prompt: dd 0
  697. promptlen: dd 0
  698.    
  699.    
  700. p1:  db 'Waiting for Command '
  701. p1len:
  702.    
  703. p9:  db 'Define SB with setup'
  704. p9len:
  705.    
  706. p2:  db 'Sending File        '
  707. p2len:
  708.    
  709. p3:  db 'Playing File        '
  710. p3len:
  711.    
  712. p4:  db 'Complete            '
  713. p4len:
  714.    
  715. p5:  db 'Contacting Host...  '
  716. p5len:
  717.    
  718. p6:  db 'File not found.     '
  719. p6len:
  720.    
  721. ack:
  722.  db 00,04,0,1
  723. ackLen:
  724.    
  725. socketNum:
  726.  dd 0
  727.    
  728. blockNumber:
  729.  dw 0
  730.    
  731. ; This must be the last part of the file, because the blockBuffer
  732. ; continues at I_END.
  733. blockBuffer:
  734.  db 00, 03, 00, 01
  735. I_END:
  736.    
  737.    
  738.    
  739.    
  740.    
  741.    
  742.