home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CASM.ARJ / SOUND.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-02-23  |  2.1 KB  |  113 lines

  1. ;_ sound.asm   Fri Oct  2 1987   Modified by: Walter Bright */
  2.  
  3. include    MACROS.ASM
  4.  
  5.     begcode    sound
  6.  
  7.     c_public    sound_tone,sound_beep,sound_click
  8.  
  9.  
  10. ppi_port    equ    61h    ;8255A-5 PPI port
  11. timer        equ    40h    ;timer 8253-5
  12.  
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ; Generate a tone on the speaker
  15. ; Use:
  16. ;    void sound_tone(int cycles,int uptime,int dntime);
  17. ;    cycles =    # of cycles
  18. ;    uptime =    one half-cycle time
  19. ;    dntime =    other half-cycle time
  20. ; BUGS:
  21. ;    dependent on clock speed of CPU - should be rewritten to use
  22. ;    timer chip.
  23.  
  24. func    sound_tone
  25.     push    BP
  26.     mov    BP,SP
  27.  
  28. ;    cli
  29.     in    AL,ppi_port
  30.     push    AX        ;save original configuration of port
  31.     mov    BX,P[BP]
  32.  
  33. L3:    and    AL,0FCh
  34.     out    ppi_port,AL    ;turn off speaker
  35.     mov    CX,P+2[BP]
  36. L1:    loop    L1
  37.     or    AL,2
  38.     out    ppi_port,AL    ;turn on speaker
  39.     mov    CX,P+4[BP]
  40. L2:    loop    L2
  41.     dec    BX        ;for cycles times
  42.     jne    L3
  43.     pop    AX
  44.     out    ppi_port,AL    ;restore port to original configuration
  45. ;    sti
  46.  
  47.     pop    BP
  48.     ret
  49. c_endp    sound_tone
  50.  
  51.  
  52. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  53. ; Beep the speaker.
  54. ; Use:
  55. ;    void sound_beep(int freq);
  56. ;    freq = frequency (1331 = 1000 Hz)
  57. ; BUGS:
  58. ;    The duration of the beep is dependent on the CPU clock speed.
  59.  
  60. func    sound_beep
  61.     push    BP
  62.     mov    BP,SP
  63.  
  64.     mov    BL,2        ;delay count
  65.     mov    AL,10110110b    ;sel tim 2,lsb,msb,binary
  66.     out    timer+3,AL    ;write the timer mode reg
  67.     mov    AX,P[BP]    ;get freq
  68.     out    timer+2,AL    ;write timer 2 count, lsb
  69.     mov    AL,AH
  70.     out    timer+2,AL    ;write timer 2 count, msb
  71.     in    AL,ppi_port    ;get current setting of port
  72.     mov    AH,AL        ;save it
  73.     or    AL,3        ;turn speaker on
  74.     out    ppi_port,AL
  75.     clr    CX        ;set count to wait 500ms
  76. B1:    loop    B1
  77.     dec    BL        ;delay count
  78.     jne    B1
  79.     mov    AL,AH        ;original value of port
  80.     out    ppi_port,AL
  81.     pop    BP
  82.     ret
  83. c_endp    sound_beep
  84.  
  85.  
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ; Generate a click on the speaker. This is a degenerate version of sound_tone().
  88. ; Use:
  89. ;    void sound_click();
  90. ;
  91.  
  92. func    sound_click
  93.     in    AL,ppi_port
  94.     push    AX
  95.  
  96.     and    AL,0FCh
  97.     out    ppi_port,AL
  98.     mov    CX,40
  99. C1:    loop    C1
  100.     or    AL,2
  101.     out    ppi_port,AL
  102.     mov    CX,40
  103. C2:    loop    C2
  104.     pop    AX
  105.     out    ppi_port,AL
  106.     ret
  107. c_endp    sound_click
  108.  
  109.     endcode    sound
  110.  
  111.     end
  112.  
  113.