Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. format MS COFF
  9.  
  10. include 'config.inc'
  11.  
  12. ;structs----------------------------------------------------------
  13. struc IOCTL
  14. {  .handle      dd ?
  15.    .io_code     dd ?
  16.    .input       dd ?
  17.    .inp_size    dd ?
  18.    .output      dd ?
  19.    .out_size    dd ?
  20. }
  21.  
  22. virtual at 0
  23.   IOCTL IOCTL
  24. end virtual
  25.  
  26. ;something--------------------------------------------------------
  27. public START
  28. public service_proc
  29. public version
  30.  
  31. include '..\proc32.inc'
  32. include '..\imports.inc'
  33.  
  34. section '.flat' code readable align 16
  35.  
  36. include 'sb16.inc'
  37.  
  38. ;-------------------------------------------------------------------------------
  39. proc START stdcall, state:dword
  40.  
  41.            cmp  [state], 1
  42.            jne  .stop
  43. .entry:
  44.  
  45. if DEBUG
  46.            mov  esi, msgInit
  47.            call SysMsgBoardStr
  48. end if
  49.  
  50.            call detect           ;returns DSP version or zero if
  51.            test eax,eax          ;SB card not found
  52.            jz   .exit
  53.  
  54. if DEBUG
  55.            movzx eax,al          ;major version
  56.            dec eax
  57.            jz  .sb_say_about_found_dsp
  58.            mov dword[sb_DSP_description],'2.x '
  59.            dec eax
  60.            jz  .sb_say_about_found_dsp
  61.            mov dword[sb_DSP_description],'Pro '
  62.            dec eax
  63.            jz  .sb_say_about_found_dsp
  64.            mov dword[sb_DSP_description],'16  '
  65. .sb_say_about_found_dsp:
  66.            mov esi,msgDSPFound
  67.            call SysMsgBoardStr
  68. end if
  69.            xor  eax,eax
  70.            mov  ebx,[sb_base_port]
  71.            lea  ecx,[ebx+0xF]
  72.            call ReservePortArea  ;these ports must be mine!
  73. if DEBUG
  74.            dec  eax
  75.            jnz  @f
  76.            mov  esi,msgErrRsrvPorts
  77.            call SysMsgBoardStr
  78. @@:
  79. end if
  80.  
  81.            call sb_setup         ;clock it, etc
  82.  
  83.            stdcall AttachIntHandler, sb_irq_num, sb_irq, 0
  84.  
  85. if DEBUG
  86.            test eax,eax
  87.            jnz  @f
  88.  
  89.            mov  esi,msgErrAtchIRQ
  90.            call SysMsgBoardStr
  91.  
  92.            stdcall GetIntHandler, sb_irq_num
  93.            call SysMsgBoardNum
  94.  
  95.            jmp  .stop
  96. @@:
  97.            mov  esi,msgSucAtchIRQ
  98.            call SysMsgBoardStr
  99. end if
  100.            stdcall RegService, my_service, service_proc
  101.            ret
  102. .stop:
  103.            call sb_reset
  104. .exit:
  105.  
  106. if DEBUG
  107.            mov esi,msgExit
  108.            call SysMsgBoardStr
  109. end if
  110.  
  111.            xor  eax, eax
  112.            ret
  113. endp
  114. ;-------------------------------------------------------------------------------
  115.  
  116. handle     equ  IOCTL.handle
  117. io_code    equ  IOCTL.io_code
  118. input      equ  IOCTL.input
  119. inp_size   equ  IOCTL.inp_size
  120. output     equ  IOCTL.output
  121. out_size   equ  IOCTL.out_size
  122.  
  123. align 4
  124. proc service_proc stdcall, ioctl:dword
  125.            mov  edi,[ioctl]
  126.            mov  eax,[edi+io_code]
  127.            cmp  eax,SRV_GETVERSION
  128.            jne  @F
  129.  
  130.            mov  eax,[edi+output]
  131.            cmp  [edi+out_size],4
  132.            jne  .fail
  133.            mov  [eax],dword API_VERSION
  134.            xor  eax,eax
  135.            ret
  136. @@:
  137.            cmp  eax,DEV_PLAY
  138.            jne  @f
  139. if DEBUG
  140.            mov  esi,msgPlay
  141.            call SysMsgBoardStr
  142. end if
  143.            call sb_stop          ;to play smth new we must stop smth old
  144.  
  145.            call pre_fill_data    ;fill first and second half of the buffer
  146.            call pre_fill_data    ;
  147.  
  148.            call sb_set_dma       ;is it really needed here? Paranoia.
  149.            call sb_play
  150.            xor  eax,eax
  151.            ret
  152. ;@@:                             ;all this commented stuff in service proc
  153. ;           cmp  eax,DEV_STOP    ;is never used. Mixer do this virtually,
  154. ;           jne  @f              ;e.g. instead of stopping driver it
  155. ;if DEBUG                        ;outputs silence
  156. ;           mov  esi,msgStop
  157. ;           call SysMsgBoardStr
  158. ;end if
  159. ;           call sb_stop
  160. ;           xor  eax,eax
  161. ;           ret
  162. @@:
  163.            cmp  eax,DEV_CALLBACK
  164.            jne  @f
  165. if DEBUG
  166.            mov  esi,msgCallback
  167.            call SysMsgBoardStr
  168. end if
  169.            mov  edi,[edi+input]
  170.            mov  eax,[edi]
  171.            mov  [callback],eax
  172. if DEBUG
  173.            call SysMsgBoardNum
  174. end if
  175.            ret
  176. @@:
  177. ;           cmp  eax,DEV_SET_MASTERVOL
  178. ;           jne  @F
  179. ;if DEBUG
  180. ;           mov  esi,msgSetVol
  181. ;           call SysMsgBoardStr
  182. ;end if
  183. ;           mov  eax,[edi+input]
  184. ;           mov  eax,[eax]
  185. ;           mov  [sb_master_vol],eax
  186. ;           ret
  187. ;@@:
  188. ;           cmp  eax,DEV_GET_MASTERVOL
  189. ;           jne  @F
  190. ;if DEBUG
  191. ;           mov  esi,msgGetVol
  192. ;           call SysMsgBoardStr
  193. ;end if
  194. ;           mov  eax,[edi+output]
  195. ;           mov  edx,[sb_master_vol]
  196. ;           mov  [eax],edx
  197. ;           ret
  198.  
  199. .fail:
  200.            or eax, -1
  201.            ret
  202. endp
  203.  
  204. restore   handle
  205. restore   io_code
  206. restore   input
  207. restore   inp_size
  208. restore   output
  209. restore   out_size
  210.  
  211. ;-------------------------------------------------------------------------------
  212. align 4
  213. proc sb_irq
  214.            mov  edx,[sb_base_port] ;tell the DSP that we have processed IRQ
  215.            add  dl,0xF             ;0xF for 16 bit sound, 0xE for 8 bit sound
  216.            in   al,dx              ;for non-stop sound
  217.  
  218. pre_fill_data:
  219.            mov  eax,int_flip_flop
  220.            not  dword[eax]
  221.            mov  eax,[eax]
  222.            test eax,eax
  223.            jns  .fill_second_half
  224.  
  225.            stdcall [callback],SB16Buffer0 ;for 32k buffer
  226. ;           stdcall [callback],SB16Buffer0 ;for 64k buffer
  227. ;           stdcall [callback],SB16Buffer1 ;for 64k buffer
  228.  
  229.            xor  eax,eax
  230.            ret
  231.  
  232. .fill_second_half:
  233.            stdcall [callback],SB16Buffer1 ;for 32k buffer
  234. ;           stdcall [callback],SB16Buffer2 ;for 64k buffer
  235. ;           stdcall [callback],SB16Buffer3 ;for 64k buffer
  236.  
  237.            xor  eax,eax
  238.            ret
  239. endp
  240. ;-------------------------------------------------------------------------------
  241. align 4
  242. proc detect
  243. .sb_detect_next_port:
  244. if DEBUG
  245.            inc  dword[port_second_digit_num]
  246. end if
  247.            mov  edx,sb_base_port
  248.            add  byte[edx],10h
  249.            cmp  byte[edx],80h
  250.            jbe  .sb_try_to_detect_at_specified_port
  251. ;error - no SB card detected
  252. .sb_not_found_err:
  253.            xor  eax, eax
  254.            ret
  255.  
  256. .sb_try_to_detect_at_specified_port:
  257.            call sb_reset
  258.            add  dl,8
  259.            mov  ecx,100
  260. .sb_check_port:
  261.            in   al,dx
  262.            test al,al            ;is DSP port ready to be read?
  263.            jns  .sb_port_not_ready
  264.  
  265.            sub  dl,4
  266.            in   al,dx            ;check for AAh response
  267.            add  dl,4
  268.            cmp  al,0xAA
  269.            jne  .sb_port_not_ready
  270. .sb_card_found:
  271.            and  dl,0xF0
  272.            add  dl,0xC
  273.            sb_out 0xE1           ;get DSP version
  274.            add  dl,2
  275. @@:
  276.            in   al,dx
  277.            test al,al            ;is DSP port ready to be read?
  278.            jns  @b
  279.            sub  dl,4
  280.            in   al,dx            ;get major version
  281.            ror  eax,16
  282.            add  dl,4
  283. @@:
  284.            in   al,dx
  285.            test al,al            ;is DSP port ready to be read?
  286.            jns  @b
  287.            sub  dl,4
  288.            in   al,dx            ;get minor version
  289.            xor  edx,edx
  290.            mov  dl,10
  291.            div  dl
  292.            ror  eax,16
  293.            xor  ah,ah
  294. if DEBUG
  295.            add  [sb_DSP_version],eax
  296. end if
  297.            ret
  298.  
  299. .sb_port_not_ready:
  300.            loop .sb_check_port   ;100 retries (~100 microsec.)
  301.            jmp  .sb_detect_next_port
  302. endp
  303. ;-------------------------------------------------------------------------------
  304. if DEBUG
  305. proc SysMsgBoardNum
  306.            mov  ebx,eax
  307.            mov  ecx,8
  308.            mov  esi,(number_to_out+1)
  309. .1:
  310.            mov  eax,ebx
  311.            and  eax,0xF
  312.            add  al,'0'
  313.            cmp  al,(10+'0')
  314.            jb   @f
  315.            add  al,('A'-'0'-10)
  316. @@:
  317.            mov  [esi+ecx],al
  318.            shr  ebx,4
  319.            loop .1
  320.            dec  esi
  321.            call SysMsgBoardStr
  322.            ret
  323. endp
  324. end if
  325. ;all initialized data place here
  326.  
  327. align 4
  328. version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
  329.  
  330. sb_base_port: dd 200h
  331.  
  332. ;pTempBuf      dd 0
  333.  
  334. callback      dd 0
  335.  
  336. int_flip_flop dd 0
  337.  
  338. sound_dma     dd sb_dma_num
  339.  
  340. ;note that 4th DMA channel doesn't exist, it is used for cascade
  341. ;plugging the first DMA controler to the second
  342. dma_table     db 0x87,0x83,0x81,0x82,0xFF,0x8B,0x89,0x8A
  343.  
  344. ;sb_master_vol dd 0
  345.  
  346. my_service    db 'SOUND',0  ;max 16 chars include zero
  347.  
  348. if DEBUG
  349. number_to_out db '0x00000000',13,10,0
  350.  
  351. msgInit       db 'detecting hardware...',13,10,0
  352. msgExit       db 'exiting... May be some problems found?',13,10,0
  353. msgPlay       db 'start play',13,10,0
  354. ;msgStop       db 'stop play',13,10,0
  355. msgCallback   db 'set_callback received from the mixer!',13,10
  356.               db 'callback handler is: ',0
  357. msgErrAtchIRQ db 'failed to attach IRQ',(sb_irq_num+'0'),13,10
  358.               db 'owner',39,'s handler: ',0
  359. msgSucAtchIRQ db 'succesfully attached IRQ',(sb_irq_num+'0')
  360.               db ' as hardcoded',13,10,0
  361. msgErrRsrvPorts db 'failed to reserve needed ports.',13,10
  362.               db 'Driver may work unstable',13,10,0
  363. ;msgSetVol     db 'DEV_SET_MASTERVOL call came',13,10,0
  364. ;msgGetVol     db 'DEV_GET_MASTERVOL call came',13,10,0
  365. msgErrDMAsetup db 'failed to setup DMA - bad channel',13,10,0
  366. ;-------------------------------------------------------------------------------
  367. msgDSPFound   db 'DSP found at port 2'
  368. label port_second_digit_num dword at $
  369.               db '00h',13,10,'DSP version '
  370. sb_DSP_version: db '0.00 - SB'
  371. sb_DSP_description: db 32,32,32,32,13,10,0
  372. ;-------------------------------------------------------------------------------
  373. end if
  374. ;section '.data' data readable writable align 16
  375. ;all uninitialized data place here
  376.