Subversion Repositories Kolibri OS

Rev

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. DEBUG           equ 1
  11. DEBUG_IRQ       equ 0
  12.  
  13. include 'proc32.inc'
  14. include 'imports.inc'
  15.  
  16. API_VERSION     equ 0x01000100
  17.  
  18. USE_COM_IRQ     equ 0    ;make irq 3 and irq 4 available for PCI devices
  19.  
  20. ;irq 0,1,2,8,12,13 íåäîñòóïíû
  21. ;                   FEDCBA9876543210
  22. VALID_IRQ       equ 1100111011111000b
  23. ATTCH_IRQ       equ 0000111010100000b
  24.  
  25. if USE_COM_IRQ
  26. ATTCH_IRQ       equ 0000111010111000b
  27. end if
  28.  
  29. CPU_FREQ        equ  2000d
  30.  
  31. BIT0  EQU 0x00000001
  32. BIT1  EQU 0x00000002
  33. BIT2  EQU 0x00000004
  34. BIT3  EQU 0x00000008
  35. BIT4  EQU 0x00000010
  36. BIT5  EQU 0x00000020
  37. BIT6  EQU 0x00000040
  38. BIT7  EQU 0x00000080
  39. BIT8  EQU 0x00000100
  40. BIT9  EQU 0x00000200
  41. BIT10 EQU 0x00000400
  42. BIT11 EQU 0x00000800
  43. BIT12 EQU 0x00001000
  44. BIT13 EQU 0x00002000
  45. BIT14 EQU 0x00004000
  46. BIT15 EQU 0x00008000
  47. BIT16 EQU 0x00010000
  48. BIT17 EQU 0x00020000
  49. BIT18 EQU 0x00040000
  50. BIT19 EQU 0x00080000
  51. BIT20 EQU 0x00100000
  52. BIT21 EQU 0x00200000
  53. BIT22 EQU 0x00400000
  54. BIT23 EQU 0x00800000
  55. BIT24 EQU 0x00100000
  56. BIT25 EQU 0x02000000
  57. BIT26 EQU 0x04000000
  58. BIT27 EQU 0x08000000
  59. BIT28 EQU 0x10000000
  60. BIT29 EQU 0x20000000
  61. BIT30 EQU 0x40000000
  62. BIT31 EQU 0x80000000
  63.  
  64. VID_FM801         equ 0x1319
  65. CTRL_FM801        equ 0x0801
  66.  
  67. FM_PCM_VOLUME         equ 0x00
  68. FM_FM_VOLUME          equ 0x02
  69. FM_I2S_VOLUME         equ 0x04
  70. FM_RECORD_SOURCE      equ 0x06
  71.  
  72. FM_PLAY_CTL           equ 0x08
  73. FM_PLAY_RATE_MASK     equ 0x0f00
  74. FM_PLAY_BUF1_LAST     equ 0x0001
  75. FM_PLAY_BUF2_LAST     equ 0x0002
  76. FM_PLAY_START         equ 0x0020
  77. FM_PLAY_PAUSE         equ 0x0040
  78. FM_PLAY_STOPNOW       equ 0x0080
  79. FM_PLAY_16BIT         equ 0x4000
  80. FM_PLAY_STEREO        equ 0x8000
  81.  
  82. FM_PLAY_DMALEN        equ 0x0a
  83. FM_PLAY_DMABUF1       equ 0x0c
  84. FM_PLAY_DMABUF2       equ 0x10
  85.  
  86. FM_REC_CTL            equ 0x14
  87. FM_REC_RATE_MASK      equ 0x0f00
  88. FM_REC_BUF1_LAST      equ 0x0001
  89. FM_REC_BUF2_LAST      equ 0x0002
  90. FM_REC_START          equ 0x0020
  91. FM_REC_PAUSE          equ 0x0040
  92. FM_REC_STOPNOW        equ 0x0080
  93. FM_REC_16BIT          equ 0x4000
  94. FM_REC_STEREO         equ 0x8000
  95.  
  96. FM_REC_DMALEN         equ 0x16
  97. FM_REC_DMABUF1        equ 0x18
  98. FM_REC_DMABUF2        equ 0x1c
  99.  
  100. FM_CODEC_CTL          equ 0x22
  101. FM_VOLUME             equ 0x26
  102. FM_VOLUME_MUTE        equ 0x8000
  103.  
  104. FM_CODEC_CMD          equ 0x2a
  105. FM_CODEC_CMD_READ     equ 0x0080
  106. FM_CODEC_CMD_VALID    equ 0x0100
  107. FM_CODEC_CMD_BUSY     equ 0x0200
  108.  
  109. FM_CODEC_DATA         equ 0x2c
  110.  
  111. FM_IO_CTL             equ 0x52
  112. FM_CARD_CTL           equ 0x54
  113.  
  114. FM_INTMASK            equ 0x56
  115. FM_INTMASK_PLAY       equ 0x0001
  116. FM_INTMASK_REC        equ 0x0002
  117. FM_INTMASK_VOL        equ 0x0040
  118. FM_INTMASK_MPU        equ 0x0080
  119.  
  120. FM_INTSTATUS          equ 0x5a
  121. FM_INTSTATUS_PLAY     equ 0x0100
  122. FM_INTSTATUS_REC      equ 0x0200
  123. FM_INTSTATUS_VOL      equ 0x4000
  124. FM_INTSTATUS_MPU      equ 0x8000
  125.  
  126. CODEC_MASTER_VOL_REG         equ 0x02 ;
  127. CODEC_AUX_VOL                equ 0x04 ;
  128. CODEC_PCM_OUT_REG            equ 0x18 ; PCM output volume
  129. CODEC_EXT_AUDIO_REG          equ 0x28 ; extended audio
  130. CODEC_EXT_AUDIO_CTRL_REG     equ 0x2a ; extended audio control
  131. CODEC_PCM_FRONT_DACRATE_REG  equ 0x2c ; PCM out sample rate
  132. CODEC_PCM_SURND_DACRATE_REG  equ 0x2e ; surround sound sample rate
  133. CODEC_PCM_LFE_DACRATE_REG    equ 0x30 ; LFE sample rate
  134.  
  135. SRV_GETVERSION        equ  0
  136. DEV_PLAY              equ  1
  137. DEV_STOP              equ  2
  138. DEV_CALLBACK          equ  3
  139. DEV_SET_BUFF          equ  4
  140. DEV_NOTIFY            equ  5
  141. DEV_SET_MASTERVOL     equ  6
  142. DEV_GET_MASTERVOL     equ  7
  143. DEV_GET_INFO          equ  8
  144.  
  145. struc AC_CNTRL    ;AC controller base class
  146. { .bus                dd ?
  147.   .devfn              dd ?
  148.  
  149.   .vendor             dd ?
  150.   .dev_id             dd ?
  151.   .pci_cmd            dd ?
  152.   .pci_stat           dd ?
  153.  
  154.   .codec_io_base      dd ?
  155.   .codec_mem_base     dd ?
  156.  
  157.   .ctrl_io_base       dd ?
  158.   .ctrl_mem_base      dd ?
  159.   .cfg_reg            dd ?
  160.   .int_line           dd ?
  161.  
  162.   .vendor_ids         dd ?    ;vendor id string
  163.   .ctrl_ids           dd ?    ;hub id string
  164.  
  165.   .buffer             dd ?
  166.  
  167.   .notify_pos         dd ?
  168.   .notify_task        dd ?
  169.  
  170.   .lvi_reg            dd ?
  171.   .ctrl_setup         dd ?
  172.   .user_callback      dd ?
  173.   .codec_read16       dd ?
  174.   .codec_write16      dd ?
  175.  
  176.   .ctrl_read8         dd ?
  177.   .ctrl_read16        dd ?
  178.   .ctrl_read32        dd ?
  179.  
  180.   .ctrl_write8        dd ?
  181.   .ctrl_write16       dd ?
  182.   .ctrl_write32       dd ?
  183. }
  184.  
  185. struc CODEC   ;Audio Chip base class
  186. {
  187.   .chip_id            dd ?
  188.   .flags              dd ?
  189.   .status             dd ?
  190.  
  191.   .ac_vendor_ids      dd ?    ;ac vendor id string
  192.   .chip_ids           dd ?    ;chip model string
  193.  
  194.   .shadow_flag        dd ?
  195.                       dd ?
  196.  
  197.   .regs               dw ?     ; codec registers
  198.   .reg_master_vol     dw ?     ;0x02
  199.   .reg_aux_out_vol    dw ?     ;0x04
  200.   .reg_mone_vol       dw ?     ;0x06
  201.   .reg_master_tone    dw ?     ;0x08
  202.   .reg_beep_vol       dw ?     ;0x0A
  203.   .reg_phone_vol      dw ?     ;0x0C
  204.   .reg_mic_vol        dw ?     ;0x0E
  205.   .reg_line_in_vol    dw ?     ;0x10
  206.   .reg_cd_vol         dw ?     ;0x12
  207.   .reg_video_vol      dw ?     ;0x14
  208.   .reg_aux_in_vol     dw ?     ;0x16
  209.   .reg_pcm_out_vol    dw ?     ;0x18
  210.   .reg_rec_select     dw ?     ;0x1A
  211.   .reg_rec_gain       dw ?     ;0x1C
  212.   .reg_rec_gain_mic   dw ?     ;0x1E
  213.   .reg_gen            dw ?     ;0x20
  214.   .reg_3d_ctrl        dw ?     ;0X22
  215.   .reg_page           dw ?     ;0X24
  216.   .reg_powerdown      dw ?     ;0x26
  217.   .reg_ext_audio      dw ?     ;0x28
  218.   .reg_ext_st         dw ?     ;0x2a
  219.   .reg_pcm_front_rate dw ?     ;0x2c
  220.   .reg_pcm_surr_rate  dw ?     ;0x2e
  221.   .reg_lfe_rate       dw ?     ;0x30
  222.   .reg_pcm_in_rate    dw ?     ;0x32
  223.                       dw ?     ;0x34
  224.   .reg_cent_lfe_vol   dw ?     ;0x36
  225.   .reg_surr_vol       dw ?     ;0x38
  226.   .reg_spdif_ctrl     dw ?     ;0x3A
  227.                       dw ?     ;0x3C
  228.                       dw ?     ;0x3E
  229.                       dw ?     ;0x40
  230.                       dw ?     ;0x42
  231.                       dw ?     ;0x44
  232.                       dw ?     ;0x46
  233.                       dw ?     ;0x48
  234.                       dw ?     ;0x4A
  235.                       dw ?     ;0x4C
  236.                       dw ?     ;0x4E
  237.                       dw ?     ;0x50
  238.                       dw ?     ;0x52
  239.                       dw ?     ;0x54
  240.                       dw ?     ;0x56
  241.                       dw ?     ;0x58
  242.                       dw ?     ;0x5A
  243.                       dw ?     ;0x5C
  244.                       dw ?     ;0x5E
  245.   .reg_page_0         dw ?     ;0x60
  246.   .reg_page_1         dw ?     ;0x62
  247.   .reg_page_2         dw ?     ;0x64
  248.   .reg_page_3         dw ?     ;0x66
  249.   .reg_page_4         dw ?     ;0x68
  250.   .reg_page_5         dw ?     ;0x6A
  251.   .reg_page_6         dw ?     ;0x6C
  252.   .reg_page_7         dw ?     ;0x6E
  253.                       dw ?     ;0x70
  254.                       dw ?     ;0x72
  255.                       dw ?     ;0x74
  256.                       dw ?     ;0x76
  257.                       dw ?     ;0x78
  258.                       dw ?     ;0x7A
  259.   .reg_vendor_id_1    dw ?     ;0x7C
  260.   .reg_vendor_id_2    dw ?     ;0x7E
  261.  
  262.  
  263.   .reset              dd ?    ;virual
  264.   .set_master_vol     dd ?
  265. }
  266.  
  267. struc CTRL_INFO
  268. {   .pci_cmd          dd ?
  269.     .irq              dd ?
  270.     .glob_cntrl       dd ?
  271.     .glob_sta         dd ?
  272.     .codec_io_base    dd ?
  273.     .ctrl_io_base     dd ?
  274.     .codec_mem_base   dd ?
  275.     .ctrl_mem_base    dd ?
  276.     .codec_id         dd ?
  277. }
  278.  
  279. struc IOCTL
  280. {  .handle            dd ?
  281.    .io_code           dd ?
  282.    .input             dd ?
  283.    .inp_size          dd ?
  284.    .output            dd ?
  285.    .out_size          dd ?
  286. }
  287.  
  288. virtual at 0
  289.   IOCTL IOCTL
  290. end virtual
  291.  
  292. EVENT_NOTIFY      equ 0x00000200
  293.  
  294. public START
  295. public service_proc
  296. public version
  297.  
  298. section '.flat' code readable align 16
  299.  
  300. proc START stdcall, state:dword
  301.  
  302.            cmp [state], 1
  303.            jne .stop
  304.  
  305.      if DEBUG
  306.            mov eax, START
  307.            call dword2str
  308.            call SysMsgBoardStr
  309.            mov esi, msgInit
  310.            call SysMsgBoardStr
  311.      end if
  312.  
  313.            call detect_controller
  314.            test eax, eax
  315.            jz .fail
  316.  
  317.      if DEBUG
  318.            mov  esi, [ctrl.vendor_ids]
  319.            call SysMsgBoardStr
  320.            mov  esi, [ctrl.ctrl_ids]
  321.            call SysMsgBoardStr
  322.  
  323.      end if
  324.  
  325.            call init_controller
  326.            test eax, eax
  327.            jz .fail
  328.  
  329.            call init_codec
  330.            test eax, eax
  331.            jz .fail
  332.  
  333.            call reset_controller
  334.            call setup_codec
  335.  
  336.            mov esi, msgPrimBuff
  337.            call SysMsgBoardStr
  338.            call create_primary_buff
  339.  
  340.            mov esi, msgDone
  341.            call SysMsgBoardStr
  342.  
  343.            mov eax, VALID_IRQ
  344.            mov ebx, [ctrl.int_line]
  345.            mov esi, msgInvIRQ
  346.            bt eax, ebx
  347.            jnc .fail_msg
  348.            mov eax, ATTCH_IRQ
  349.            mov esi, msgAttchIRQ
  350.            bt eax, ebx
  351.            jnc .fail_msg
  352.  
  353.            stdcall AttachIntHandler, ebx, ac97_irq, dword 0
  354. .reg:
  355.  
  356.            stdcall RegService, sz_sound_srv, service_proc
  357.            ret
  358. .fail:
  359.    if DEBUG
  360.            mov esi, msgFail
  361.            call SysMsgBoardStr
  362.    end if
  363.            xor eax, eax
  364.            ret
  365. .fail_msg:
  366.            call SysMsgBoardStr
  367.            xor eax, eax
  368.            ret
  369. .stop:
  370.            call stop
  371.            xor eax, eax
  372.            ret
  373. endp
  374.  
  375. handle     equ  IOCTL.handle
  376. io_code    equ  IOCTL.io_code
  377. input      equ  IOCTL.input
  378. inp_size   equ  IOCTL.inp_size
  379. output     equ  IOCTL.output
  380. out_size   equ  IOCTL.out_size
  381.  
  382. align 4
  383. proc service_proc stdcall, ioctl:dword
  384.  
  385.            mov edi, [ioctl]
  386.            mov eax, [edi+io_code]
  387.  
  388.            cmp eax, SRV_GETVERSION
  389.            jne @F
  390.  
  391.            mov eax, [edi+output]
  392.            cmp [edi+out_size], 4
  393.            jne .fail
  394.  
  395.            mov [eax], dword API_VERSION
  396.            xor eax, eax
  397.            ret
  398. @@:
  399.            cmp eax, DEV_PLAY
  400.            jne @F
  401.      if DEBUG
  402.            mov esi, msgPlay
  403.            call SysMsgBoardStr
  404.      end if
  405.            call play
  406.            ret
  407. @@:
  408.            cmp eax, DEV_STOP
  409.            jne @F
  410.      if DEBUG
  411.            mov esi, msgStop
  412.            call SysMsgBoardStr
  413.      end if
  414.            call stop
  415.            ret
  416. @@:
  417.            cmp eax, DEV_CALLBACK
  418.            jne @F
  419.            mov ebx, [edi+input]
  420.            stdcall set_callback, [ebx]
  421.            ret
  422. @@:
  423.            cmp eax, DEV_SET_MASTERVOL
  424.            jne @F
  425.            mov eax, [edi+input]
  426.            mov eax, [eax]
  427.            call set_master_vol      ;eax= vol
  428.            ret
  429. @@:
  430.            cmp eax, DEV_GET_MASTERVOL
  431.            jne @F
  432.            mov ebx, [edi+output]
  433.            stdcall get_master_vol, ebx
  434.            ret
  435. ;@@:
  436. ;           cmp eax, DEV_GET_INFO
  437. ;           jne @F
  438. ;           mov ebx, [edi+output]
  439. ;           stdcall get_dev_info, ebx
  440. ;           ret
  441. @@:
  442. .fail:
  443.            or eax, -1
  444.            ret
  445. endp
  446.  
  447. restore   handle
  448. restore   io_code
  449. restore   input
  450. restore   inp_size
  451. restore   output
  452. restore   out_size
  453.  
  454. align 4
  455. proc fill_buffer
  456.  
  457.            cmp [ctrl.user_callback], 0
  458.            je .exit
  459.  
  460.            mov esi, [ctrl.buffer]
  461.            mov eax, int_flip_flop
  462.            inc dword [eax]
  463.            test dword [eax],1
  464.            je @f
  465.            add esi, 0x4000
  466. @@:
  467.            stdcall [ctrl.user_callback], esi
  468.  
  469.            mov edx, FM_PLAY_DMABUF1
  470.            mov eax, [buffer_pgaddr]
  471.            mov esi, int_flip_flop
  472.            test dword [esi],1
  473.            je @f
  474.            mov edx, FM_PLAY_DMABUF2
  475.            add eax, 0x4000
  476. @@:
  477.            call [ctrl.ctrl_write32]
  478.  
  479. .exit:
  480.            ret
  481. endp
  482.  
  483. align 4
  484. proc ac97_irq
  485.  
  486.      if DEBUG_IRQ
  487.            mov esi, msgIRQ
  488.            call SysMsgBoardStr
  489.      end if
  490.  
  491.            mov edx, FM_INTSTATUS
  492.            call [ctrl.ctrl_read16]
  493.  
  494.            test eax, FM_INTSTATUS_PLAY
  495.            je .exit
  496.  
  497.            push eax
  498.            call fill_buffer
  499.            pop eax
  500.  
  501. .exit:
  502.            mov edx, FM_INTSTATUS
  503.            call [ctrl.ctrl_write16]
  504.  
  505.            ret
  506. endp
  507.  
  508. align 4
  509. proc create_primary_buff
  510.  
  511.            stdcall KernelAlloc, 0x10000
  512.            mov [ctrl.buffer], eax
  513.  
  514.            mov edi, eax
  515.            mov ecx, 0x10000/4
  516.            xor eax, eax
  517.            cld
  518.            rep stosd
  519.  
  520.            mov eax, [ctrl.buffer]
  521.            call GetPgAddr
  522.            mov [buffer_pgaddr], eax
  523.  
  524.            ret
  525. endp
  526.  
  527. align 4
  528. proc detect_controller
  529.            locals
  530.              last_bus dd ?
  531.              bus      dd ?
  532.              devfn    dd ?
  533.            endl
  534.  
  535.            xor eax, eax
  536.            mov [bus], eax
  537.            inc eax
  538.            call PciApi
  539.            cmp eax, -1
  540.            je .err
  541.  
  542.            mov [last_bus], eax
  543.  
  544. .next_bus:
  545.            and [devfn], 0
  546. .next_dev:
  547.            stdcall PciRead32, [bus], [devfn], dword 0
  548.            test eax, eax
  549.            jz .next
  550.            cmp eax, -1
  551.            je .next
  552.  
  553.            push eax
  554.            stdcall PciRead32, [bus], [devfn], dword 0x09
  555.            and eax,0xffffff
  556.            cmp eax, 0x060100  ;pci-isa
  557.            jne .no_bridge
  558.  
  559.            mov eax, [bus]
  560.            mov [brg_bus], eax
  561.            mov eax, [devfn]
  562.            mov [brg_devfn],eax
  563. .no_bridge:pop eax
  564.  
  565.            mov edi, devices
  566. @@:
  567.            mov ebx, [edi]
  568.            test ebx, ebx
  569.            jz .next
  570.  
  571.            cmp eax, ebx
  572.            je .found
  573.            add edi, 12
  574.            jmp @B
  575. .next:
  576.            inc [devfn]
  577.            cmp [devfn], 256
  578.            jb  .next_dev
  579.            mov eax, [bus]
  580.            inc eax
  581.            mov [bus], eax
  582.            cmp eax, [last_bus]
  583.            jna .next_bus
  584.            xor eax, eax
  585.            ret
  586. .found:
  587.            mov ebx, [bus]
  588.            mov [ctrl.bus], ebx
  589.  
  590.            mov ecx, [devfn]
  591.            mov [ctrl.devfn], ecx
  592.  
  593.            mov edx, eax
  594.            and edx, 0xFFFF
  595.            mov [ctrl.vendor], edx
  596.            shr eax, 16
  597.            mov [ctrl.dev_id], eax
  598.  
  599.            mov ebx, [edi+4]
  600.            mov [ctrl.ctrl_ids], ebx
  601.            mov [ctrl.vendor_ids], msg_FM
  602.  
  603.            mov esi, [edi+8]
  604.            mov [ctrl.ctrl_setup], esi
  605.            ret
  606. .err:
  607.            xor eax, eax
  608.            ret
  609. endp
  610.  
  611. align 4
  612. proc init_controller
  613.  
  614.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 4
  615.            mov ebx, eax
  616.            and eax, 0xFFFF
  617.            mov [ctrl.pci_cmd], eax
  618.            shr ebx, 16
  619.            mov [ctrl.pci_stat], ebx
  620.  
  621.            mov esi, msgPciCmd
  622.            call SysMsgBoardStr
  623.            call dword2str
  624.            call SysMsgBoardStr
  625.  
  626.            mov esi, msgPciStat
  627.            call SysMsgBoardStr
  628.            mov eax, [ctrl.pci_stat]
  629.            call dword2str
  630.            call SysMsgBoardStr
  631.  
  632.            mov esi, msgCtrlIsaIo
  633.            call SysMsgBoardStr
  634.  
  635.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
  636.  
  637.            call dword2str
  638.            call SysMsgBoardStr
  639.  
  640.            and eax,0xFFFE
  641.            mov [ctrl.ctrl_io_base], eax
  642.  
  643.            mov esi, msgIrqNum
  644.            call SysMsgBoardStr
  645.  
  646.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
  647.            and eax, 0xFF
  648.            mov [ctrl.int_line], eax
  649.  
  650.            call dword2str
  651.            call SysMsgBoardStr
  652.  
  653.            call [ctrl.ctrl_setup]
  654.            xor eax, eax
  655.            inc eax
  656.            ret
  657. endp
  658.  
  659. align 4
  660. proc set_FM
  661.            mov [ctrl.codec_read16],  codec_io_r16    ;virtual
  662.            mov [ctrl.codec_write16], codec_io_w16    ;virtual
  663.  
  664.            mov [ctrl.ctrl_read8 ],  ctrl_io_r8      ;virtual
  665.            mov [ctrl.ctrl_read16],  ctrl_io_r16      ;virtual
  666.            mov [ctrl.ctrl_read32],  ctrl_io_r32      ;virtual
  667.  
  668.            mov [ctrl.ctrl_write8 ], ctrl_io_w8      ;virtual
  669.            mov [ctrl.ctrl_write16], ctrl_io_w16      ;virtual
  670.            mov [ctrl.ctrl_write32], ctrl_io_w32      ;virtual
  671.            ret
  672. endp
  673.  
  674. align 4
  675. proc reset_controller
  676.  
  677.            mov esi, msgInitCtrl
  678.            call SysMsgBoardStr
  679.  
  680.            mov edx, FM_CARD_CTL
  681.            call [ctrl.ctrl_read8]
  682.            push eax
  683.            or al,1
  684.            mov edx, FM_CARD_CTL
  685.            call [ctrl.ctrl_write8]
  686.            mov eax, 10
  687.            call StallExec
  688.            pop eax
  689.            and al,0xFE
  690.            mov edx, FM_CARD_CTL
  691.            call [ctrl.ctrl_write8]
  692.            mov eax, 10
  693.            call StallExec
  694.  
  695.            mov eax, 0x0404
  696.            mov edx, FM_PCM_VOLUME
  697.            call [ctrl.ctrl_write16]
  698.            mov edx, FM_FM_VOLUME
  699.            call [ctrl.ctrl_write16]
  700.            mov edx, FM_I2S_VOLUME
  701.            call [ctrl.ctrl_write16]
  702.  
  703.            mov edx, FM_INTMASK
  704.            call [ctrl.ctrl_read16]
  705.            and eax, not FM_INTMASK_PLAY
  706.            or eax, FM_INTMASK_REC or FM_INTMASK_MPU or FM_INTMASK_VOL
  707.            mov edx, FM_INTMASK
  708.            call [ctrl.ctrl_write16]
  709.  
  710.            mov eax, FM_INTMASK_PLAY or FM_INTMASK_REC or FM_INTMASK_MPU or FM_INTMASK_VOL
  711.            mov edx, FM_INTSTATUS
  712.            call [ctrl.ctrl_write16]
  713.  
  714.            ret
  715. endp
  716.  
  717. align 4
  718. proc init_codec
  719.  
  720.            mov esi, msgInitCodec
  721.            call SysMsgBoardStr
  722.  
  723.            mov al, FM_CODEC_CMD_READ
  724.            mov edx, FM_CODEC_CMD
  725.            call [ctrl.ctrl_write8]
  726.  
  727.            call reset_codec
  728.  
  729.            call detect_codec
  730.  
  731.            xor eax, eax
  732.            inc eax
  733.            ret
  734. endp
  735.  
  736. align 4
  737. proc reset_codec
  738.  
  739.            mov ecx, 255
  740. .L1:
  741.            mov edx, FM_CODEC_CMD
  742.            call [ctrl.ctrl_read16]
  743.            test ah, FM_CODEC_CMD_VALID shr 8
  744.            jne .L2
  745.            loop .L1
  746. .L2:
  747.            mov edx, FM_CODEC_CTL
  748.            call [ctrl.ctrl_read8]
  749.            push eax
  750.            or al, 0x20
  751.            mov edx, FM_CODEC_CTL
  752.            call [ctrl.ctrl_write8]
  753.            pop eax
  754.            and al,0xDF
  755.            mov edx, FM_CODEC_CTL
  756.            call [ctrl.ctrl_write8]
  757.  
  758.            xor eax, eax
  759.            inc eax
  760.            ret
  761. endp
  762.  
  763. align 4
  764. play:
  765.            mov eax, 0x4000-1
  766.            mov edx, FM_PLAY_DMALEN
  767.            call [ctrl.ctrl_write16]
  768.  
  769.            call fill_buffer
  770.  
  771.            mov eax, FM_PLAY_START or FM_PLAY_STOPNOW or FM_PLAY_STEREO or FM_PLAY_16BIT or 0xA00
  772.            mov edx, FM_PLAY_CTL
  773.            call [ctrl.ctrl_write16]
  774.  
  775.            xor eax, eax
  776.            ret
  777.  
  778. align 4
  779. stop:
  780.            mov edx, FM_PLAY_CTL
  781.            call [ctrl.ctrl_read16]
  782.            and eax, not (FM_PLAY_START or FM_PLAY_STOPNOW)
  783.            or eax, FM_PLAY_BUF1_LAST or FM_PLAY_BUF2_LAST
  784.            mov edx, FM_PLAY_CTL
  785.            call [ctrl.ctrl_write16]
  786.  
  787.            xor eax, eax
  788.            ret
  789.  
  790. align 4
  791. proc get_dev_info stdcall, p_info:dword
  792.            virtual at esi
  793.              CTRL_INFO CTRL_INFO
  794.            end virtual
  795.  
  796.            mov esi, [p_info]
  797.            mov eax, [ctrl.int_line]
  798.            mov ebx, [ctrl.codec_io_base]
  799.            mov ecx, [ctrl.ctrl_io_base]
  800.            mov edx, [ctrl.codec_mem_base]
  801.            mov edi, [ctrl.ctrl_mem_base]
  802.  
  803.            mov [CTRL_INFO.irq], eax
  804.            mov [CTRL_INFO.codec_io_base], ebx
  805.            mov [CTRL_INFO.ctrl_io_base], ecx
  806.            mov [CTRL_INFO.codec_mem_base], edx
  807.            mov [CTRL_INFO.ctrl_mem_base], edi
  808.  
  809.            mov eax, [codec.chip_id]
  810.            mov [CTRL_INFO.codec_id], eax
  811.  
  812.            mov ebx, [ctrl.pci_cmd]
  813.            mov [CTRL_INFO.pci_cmd], ebx
  814.            ret
  815. endp
  816.  
  817. align 4
  818. proc set_callback stdcall, handler:dword
  819.            mov eax, [handler]
  820.            mov [ctrl.user_callback], eax
  821.            ret
  822. endp
  823.  
  824. align 4
  825. proc codec_read stdcall, ac_reg:dword   ; reg = edx, reval = eax
  826.  
  827.            mov edx, [ac_reg]
  828.  
  829.            mov ebx, edx
  830.            shr ebx, 1
  831.            bt [codec.shadow_flag], ebx
  832.            jc .use_shadow
  833.  
  834.            call [ctrl.codec_read16]  ;change edx !!!
  835.            mov ecx, eax
  836.  
  837. .read_ok:
  838.            mov edx, [ac_reg]
  839.            mov [codec.regs+edx], cx
  840.            bts [codec.shadow_flag], ebx
  841.            mov eax, ecx
  842.            ret
  843. .use_shadow:
  844.            movzx eax, word [codec.regs+edx]
  845.            ret
  846.  
  847. endp
  848.  
  849. align 4
  850. proc codec_write stdcall, ac_reg:dword
  851.  
  852.            mov esi, [ac_reg]
  853.  
  854.            mov edx, esi
  855.  
  856.            call [ctrl.codec_write16]
  857.  
  858.            mov [codec.regs+esi], ax
  859.            shr esi, 1
  860.            bts [codec.shadow_flag], esi
  861.  
  862.            ret
  863. endp
  864.  
  865. align 4
  866. proc check_semafore
  867. align 4
  868. .ok:
  869.            xor eax,eax
  870.            inc eax
  871.            ret
  872. endp
  873.  
  874. align 4
  875. proc StallExec
  876.            push ecx
  877.            push edx
  878.            push ebx
  879.            push eax
  880.  
  881.            mov ecx, CPU_FREQ
  882.            mul ecx
  883.            mov ebx, eax       ;low
  884.            mov ecx, edx       ;high
  885.            rdtsc
  886.            add ebx, eax
  887.            adc ecx, edx
  888. @@:
  889.            rdtsc
  890.            sub eax, ebx
  891.            sbb edx, ecx
  892.            js @B
  893.  
  894.            pop eax
  895.            pop ebx
  896.            pop edx
  897.            pop ecx
  898.            ret
  899. endp
  900.  
  901. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  902. ;          CONTROLLER IO functions
  903. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  904.  
  905. align 4
  906. proc codec_io_r16
  907.  
  908.            push edx
  909.            mov ecx, 255
  910. .L1:
  911.            mov edx, FM_CODEC_CMD
  912.            call [ctrl.ctrl_read16]
  913.            test ah, FM_CODEC_CMD_BUSY shr 8
  914.            je .L2
  915.            loop .L1
  916. .L2:
  917.            pop eax
  918.            or al, FM_CODEC_CMD_READ
  919.            mov edx, FM_CODEC_CMD
  920.            call [ctrl.ctrl_write8]
  921.  
  922.            mov ecx, 255
  923. .L3:
  924.            mov edx, FM_CODEC_CMD
  925.            call [ctrl.ctrl_read16]
  926.            test ah, FM_CODEC_CMD_VALID shr 8
  927.            jne .L4
  928.            loop .L3
  929. .L4:
  930.            mov edx, FM_CODEC_DATA
  931.            call [ctrl.ctrl_read16]
  932.  
  933.            ret
  934. endp
  935.  
  936. align 4
  937. proc codec_io_w16
  938.  
  939.            push edx
  940.            push eax
  941.            mov ecx, 255
  942. .L1:
  943.            mov edx, FM_CODEC_CMD
  944.            call [ctrl.ctrl_read16]
  945.            test ah, FM_CODEC_CMD_BUSY shr 8
  946.            je .L2
  947.            loop .L1
  948. .L2:
  949.            pop eax
  950.            mov edx, FM_CODEC_DATA
  951.            call [ctrl.ctrl_write16]
  952.  
  953.            pop eax
  954.            mov edx, FM_CODEC_CMD
  955.            call [ctrl.ctrl_write16]
  956.  
  957.            ret
  958. endp
  959.  
  960. align 4
  961. proc ctrl_io_r8
  962.            add edx, [ctrl.ctrl_io_base]
  963.            in  al, dx
  964.            ret
  965. endp
  966.  
  967. align 4
  968. proc ctrl_io_r16
  969.            add edx, [ctrl.ctrl_io_base]
  970.            in  ax, dx
  971.            ret
  972. endp
  973.  
  974. align 4
  975. proc ctrl_io_r32
  976.            add edx, [ctrl.ctrl_io_base]
  977.            in  eax, dx
  978.            ret
  979. endp
  980.  
  981. align 4
  982. proc ctrl_io_w8
  983.            add edx, [ctrl.ctrl_io_base]
  984.            out dx, al
  985.            ret
  986. endp
  987.  
  988. align 4
  989. proc ctrl_io_w16
  990.            add edx, [ctrl.ctrl_io_base]
  991.            out dx, ax
  992.            ret
  993. endp
  994.  
  995. align 4
  996. proc ctrl_io_w32
  997.            add edx, [ctrl.ctrl_io_base]
  998.            out dx, eax
  999.            ret
  1000. endp
  1001.  
  1002. align 4
  1003. dword2str:
  1004.       mov  esi, hex_buff
  1005.       mov ecx, -8
  1006. @@:
  1007.       rol eax, 4
  1008.       mov ebx, eax
  1009.       and ebx, 0x0F
  1010.       mov bl, [ebx+hexletters]
  1011.       mov [8+esi+ecx], bl
  1012.       inc ecx
  1013.       jnz @B
  1014.       ret
  1015.  
  1016. hexletters   db '0123456789ABCDEF'
  1017. hex_buff     db 8 dup(0),13,10,0
  1018. brg_bus      dd ?
  1019. brg_devfn    dd ?
  1020. include "codec.inc"
  1021.  
  1022. align 4
  1023. devices dd (CTRL_FM801 shl 16)+VID_FM801, msg_FM801, set_FM
  1024.         dd 0
  1025.  
  1026. version      dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1027.  
  1028. msg_FM801    db 'FM801 AC97 controller',13,10, 0
  1029. msg_FM       db 'Forte Media',13,10, 0
  1030.  
  1031. sz_sound_srv db 'SOUND',0
  1032.  
  1033. msgInit       db 'detect hardware...',13,10,0
  1034. msgFail       db 'device not found',13,10,0
  1035. msgAttchIRQ   db 'IRQ line not supported', 13,10, 0
  1036. msgInvIRQ     db 'IRQ line not assigned or invalid', 13,10, 0
  1037. msgPlay       db 'start play', 13,10,0
  1038. msgStop       db 'stop play',  13,10,0
  1039. ;msgNotify    db 'call notify',13,10,0
  1040. msgIRQ        db 'AC97 IRQ', 13,10,0
  1041. msgInitCtrl  db 'init controller',13,10,0
  1042. msgInitCodec db 'init codec',13,10,0
  1043. msgPrimBuff   db 'create primary buffer ...',0
  1044. msgDone       db 'done',13,10,0
  1045. ;msgReg       db 'set service handler',13,10,0
  1046. ;msgOk        db 'service installed',13,10,0
  1047. ;msgStatus    db 'global status   ',0
  1048. ;msgControl   db 'global control  ',0
  1049. msgPciCmd     db 'PCI command     ',0
  1050. msgPciStat    db 'PCI status      ',0
  1051. msgCtrlIsaIo  db 'controller io base   ',0
  1052. msgIrqNum     db 'IRQ default          ',0
  1053. ;msgIrqMap    db 'AC97 irq map as      ',0
  1054.  
  1055. section '.data' data readable writable align 16
  1056.  
  1057. codec CODEC
  1058. ctrl AC_CNTRL
  1059.  
  1060. int_flip_flop      rd 1
  1061. buffer_pgaddr      rd 1
  1062.