Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. format MS COFF
  3.  
  4. include 'proc32.inc'
  5. include 'imports.inc'
  6.  
  7. DEBUG       equ 1
  8.  
  9. CPU_FREQ    equ  2000d   ;cpu freq in MHz
  10.  
  11. BIT0  EQU 0x00000001
  12. BIT1  EQU 0x00000002
  13. BIT2  EQU 0x00000004
  14. BIT3  EQU 0x00000008
  15. BIT4  EQU 0x00000010
  16. BIT5  EQU 0x00000020
  17. BIT6  EQU 0x00000040
  18. BIT7  EQU 0x00000080
  19. BIT8  EQU 0x00000100
  20. BIT9  EQU 0x00000200
  21. BIT10 EQU 0x00000400
  22. BIT11 EQU 0x00000800
  23. BIT12 EQU 0x00001000
  24. BIT13 EQU 0x00002000
  25. BIT14 EQU 0x00004000
  26. BIT15 EQU 0x00008000
  27. BIT16 EQU 0x00010000
  28. BIT17 EQU 0x00020000
  29. BIT18 EQU 0x00040000
  30. BIT19 EQU 0x00080000
  31. BIT20 EQU 0x00100000
  32. BIT21 EQU 0x00200000
  33. BIT22 EQU 0x00400000
  34. BIT23 EQU 0x00800000
  35. BIT24 EQU 0x00100000
  36. BIT25 EQU 0x02000000
  37. BIT26 EQU 0x04000000
  38. BIT27 EQU 0x08000000
  39. BIT28 EQU 0x10000000
  40. BIT29 EQU 0x20000000
  41. BIT30 EQU 0x40000000
  42. BIT31 EQU 0x80000000
  43.  
  44. VID_SIS           equ 0x1039
  45. CTRL_SIS          equ 0x7012
  46.  
  47. PCM_OUT_BDL       equ  0x10      ; PCM out buffer descriptors list
  48. PCM_OUT_CR_REG    equ  0x1b      ; PCM out Control Register
  49. PCM_OUT_LVI_REG   equ  0x15      ; PCM last valid index
  50. PCM_OUT_SR_REG    equ  0x18      ; PCM out Status register
  51. PCM_OUT_PIV_REG   equ  0x1a      ; PCM out prefetched index
  52. PCM_OUT_CIV_REG   equ  0x14      ; PCM out current index
  53.  
  54. PCM_IN_CR_REG     equ  0x0b      ; PCM in Control Register
  55. MC_IN_CR_REG      equ  0x2b      ; MIC in Control Register
  56. RR                equ  BIT1      ; reset registers.  Nukes all regs
  57.  
  58. CODEC_MASTER_VOL_REG            equ     0x02
  59. CODEC_AUX_VOL                   equ     0x04    ;
  60. CODEC_PCM_OUT_REG               equ     18h     ; PCM output volume
  61. CODEC_EXT_AUDIO_REG             equ     28h     ; extended audio
  62. CODEC_EXT_AUDIO_CTRL_REG        equ     2ah     ; extended audio control
  63. CODEC_PCM_FRONT_DACRATE_REG     equ     2ch     ; PCM out sample rate
  64. CODEC_PCM_SURND_DACRATE_REG     equ     2eh     ; surround sound sample rate
  65. CODEC_PCM_LFE_DACRATE_REG       equ     30h     ; LFE sample rate
  66.  
  67.  
  68. GLOB_CTRL      equ  0x2C        ;   Global Control
  69. CTRL_STAT       equ  0x30        ;   Global Status
  70. CTRL_CAS        equ  0x34        ;   Codec Access Semiphore
  71.  
  72. CAS_FLAG       equ  0x01        ;   Codec Access Semiphore Bit
  73.  
  74. CTRL_ST_CREADY  equ  BIT8+BIT9+BIT28 ;   Primary Codec Ready
  75.  
  76. CTRL_ST_RCS     equ  0x00008000  ;   Read Completion Status
  77.  
  78. CTRL_CNT_CRIE   equ  BIT4+BIT5+BIT6  ;   Codecs Resume Interrupt Enable
  79. CTRL_CNT_AC_OFF equ  0x00000008  ;   ACLINK Off
  80. CTRL_CNT_WARM   equ  0x00000004  ;   AC97 Warm Reset
  81. CTRL_CNT_COLD   equ  0x00000002  ;   AC97 Cold Reset
  82. CTRL_CNT_GIE    equ  0x00000001  ;   GPI Interrupt Enable
  83.  
  84. CODEC_REG_POWERDOWN   equ 0x26
  85. CODEC_REG_ST          equ 0x26
  86.  
  87.  
  88. DEV_PLAY              equ  1
  89. DEV_STOP              equ  2
  90. DEV_CALLBACK          equ  3
  91. DEV_SET_BUFF          equ  4
  92. DEV_NOTIFY            equ  5
  93. DEV_SET_MASTERVOL     equ  6
  94. DEV_GET_MASTERVOL     equ  7
  95. DEV_GET_INFO          equ  8
  96.  
  97. struc AC_CNTRL              ;AC controller base class
  98. { .bus                dd ?
  99.   .devfn              dd ?
  100.  
  101.   .vendor             dd ?
  102.   .dev_id             dd ?
  103.   .pci_cmd            dd ?
  104.   .pci_stat           dd ?
  105.  
  106.   .codec_io_base      dd ?
  107.   .codec_mem_base     dd ?
  108.  
  109.   .ctrl_io_base       dd ?
  110.   .ctrl_mem_base      dd ?
  111.   .cfg_reg            dd ?
  112.   .int_line           dd ?
  113.  
  114.   .vendor_ids         dd ?    ;vendor id string
  115.   .ctrl_ids           dd ?    ;hub id string
  116.  
  117.   .buffer             dd ?
  118.  
  119.   .notify_pos         dd ?
  120.   .notify_task        dd ?
  121.  
  122.   .lvi_reg            dd ?
  123.   .ctrl_setup         dd ?
  124.   .user_callback      dd ?
  125.   .codec_read16       dd ?
  126.   .codec_write16      dd ?
  127.  
  128.   .ctrl_read8         dd ?
  129.   .ctrl_read16        dd ?
  130.   .ctrl_read32        dd ?
  131.  
  132.   .ctrl_write8        dd ?
  133.   .ctrl_write16       dd ?
  134.   .ctrl_write32       dd ?
  135. }
  136.  
  137. struc CODEC                ;Audio Chip base class
  138. {
  139.   .chip_id            dd ?
  140.   .flags              dd ?
  141.   .status             dd ?
  142.  
  143.   .ac_vendor_ids      dd ?    ;ac vendor id string
  144.   .chip_ids           dd ?    ;chip model string
  145.  
  146.   .shadow_flag        dd ?
  147.                       dd ?
  148.  
  149.   .regs               dw ?     ; codec registers
  150.   .reg_master_vol     dw ?     ;0x02
  151.   .reg_aux_out_vol    dw ?     ;0x04
  152.   .reg_mone_vol       dw ?     ;0x06
  153.   .reg_master_tone    dw ?     ;0x08
  154.   .reg_beep_vol       dw ?     ;0x0A
  155.   .reg_phone_vol      dw ?     ;0x0C
  156.   .reg_mic_vol        dw ?     ;0x0E
  157.   .reg_line_in_vol    dw ?     ;0x10
  158.   .reg_cd_vol         dw ?     ;0x12
  159.   .reg_video_vol      dw ?     ;0x14
  160.   .reg_aux_in_vol     dw ?     ;0x16
  161.   .reg_pcm_out_vol    dw ?     ;0x18
  162.   .reg_rec_select     dw ?     ;0x1A
  163.   .reg_rec_gain       dw ?     ;0x1C
  164.   .reg_rec_gain_mic   dw ?     ;0x1E
  165.   .reg_gen            dw ?     ;0x20
  166.   .reg_3d_ctrl        dw ?     ;0X22
  167.   .reg_page           dw ?     ;0X24
  168.   .reg_powerdown      dw ?     ;0x26
  169.   .reg_ext_audio      dw ?     ;0x28
  170.   .reg_ext_st         dw ?     ;0x2a
  171.   .reg_pcm_front_rate dw ?     ;0x2c
  172.   .reg_pcm_surr_rate  dw ?     ;0x2e
  173.   .reg_lfe_rate       dw ?     ;0x30
  174.   .reg_pcm_in_rate    dw ?     ;0x32
  175.                       dw ?     ;0x34
  176.   .reg_cent_lfe_vol   dw ?     ;0x36
  177.   .reg_surr_vol       dw ?     ;0x38
  178.   .reg_spdif_ctrl     dw ?     ;0x3A
  179.                       dw ?     ;0x3C
  180.                       dw ?     ;0x3E
  181.                       dw ?     ;0x40
  182.                       dw ?     ;0x42
  183.                       dw ?     ;0x44
  184.                       dw ?     ;0x46
  185.                       dw ?     ;0x48
  186.                       dw ?     ;0x4A
  187.                       dw ?     ;0x4C
  188.                       dw ?     ;0x4E
  189.                       dw ?     ;0x50
  190.                       dw ?     ;0x52
  191.                       dw ?     ;0x54
  192.                       dw ?     ;0x56
  193.                       dw ?     ;0x58
  194.                       dw ?     ;0x5A
  195.                       dw ?     ;0x5C
  196.                       dw ?     ;0x5E
  197.   .reg_page_0         dw ?     ;0x60
  198.   .reg_page_1         dw ?     ;0x62
  199.   .reg_page_2         dw ?     ;0x64
  200.   .reg_page_3         dw ?     ;0x66
  201.   .reg_page_4         dw ?     ;0x68
  202.   .reg_page_5         dw ?     ;0x6A
  203.   .reg_page_6         dw ?     ;0x6C
  204.   .reg_page_7         dw ?     ;0x6E
  205.                       dw ?     ;0x70
  206.                       dw ?     ;0x72
  207.                       dw ?     ;0x74
  208.                       dw ?     ;0x76
  209.                       dw ?     ;0x78
  210.                       dw ?     ;0x7A
  211.   .reg_vendor_id_1    dw ?     ;0x7C
  212.   .reg_vendor_id_2    dw ?     ;0x7E
  213.  
  214.  
  215.   .reset              dd ?    ;virual
  216.   .set_master_vol     dd ?
  217. }
  218.  
  219. struc CTRL_INFO
  220. {   .pci_cmd        dd  ?
  221.     .irq            dd  ?
  222.     .glob_cntrl     dd  ?
  223.     .glob_sta       dd  ?
  224.     .codec_io_base  dd  ?
  225.     .ctrl_io_base   dd  ?
  226.     .codec_mem_base dd  ?
  227.     .ctrl_mem_base  dd  ?
  228.     .codec_id       dd  ?
  229. }
  230.  
  231. struc IOCTL
  232. {  .handle           dd ?
  233.    .io_code          dd ?
  234.    .input            dd ?
  235.    .inp_size         dd ?
  236.    .output           dd ?
  237.    .out_size         dd ?
  238. }
  239.  
  240. virtual at 0
  241.   IOCTL IOCTL
  242. end virtual
  243.  
  244. EVENT_NOTIFY          equ 0x00000200
  245.  
  246. OS_BASE               equ 0;  0x80400000
  247. new_app_base          equ 0x60400000;   0x01000000
  248. PROC_BASE             equ OS_BASE+0x0080000
  249.  
  250. public START
  251. public service_proc
  252. public version
  253.  
  254. section '.flat' code readable align 16
  255.  
  256. proc START stdcall, state:dword
  257.  
  258.            cmp [state], 1
  259.            jne .stop
  260.  
  261.      if DEBUG
  262.            mov esi, msgInit
  263.            call SysMsgBoardStr
  264.      end if
  265.  
  266.            call detect_controller
  267.            test eax, eax
  268.            jz .fail
  269.  
  270.      if DEBUG
  271.            mov esi,[ctrl.vendor_ids]
  272.            call SysMsgBoardStr
  273.            mov  esi, [ctrl.ctrl_ids]
  274.            call SysMsgBoardStr
  275.      end if
  276.  
  277.            call init_controller
  278.            test eax, eax
  279.            jz .fail
  280.  
  281.      if DEBUG
  282.            mov esi, msgInitCodec
  283.            call SysMsgBoardStr
  284.      end if
  285.  
  286.            call init_codec
  287.            test eax, eax
  288.            jz .fail
  289.  
  290.      if DEBUG
  291.            mov esi, [codec.ac_vendor_ids]
  292.            call SysMsgBoardStr
  293.  
  294.            mov esi, [codec.chip_ids]
  295.            call SysMsgBoardStr
  296.      end if
  297.  
  298.            call reset_controller
  299.            call setup_codec
  300.  
  301.            mov esi, msgPrimBuff
  302.            call SysMsgBoardStr
  303.  
  304.            call create_primary_buff
  305.  
  306.            stdcall AttachIntHandler, [ctrl.int_line], ac97_irq
  307.  
  308.            stdcall RegService, sz_sound_srv, service_proc
  309.  
  310.            mov esi, msgOk
  311.            call SysMsgBoardStr
  312.            ret
  313. .fail:
  314.    if DEBUG
  315.            mov esi, msgFail
  316.            call SysMsgBoardStr
  317.    end if
  318.            xor eax, eax
  319.            ret
  320. .stop:
  321.            call stop
  322.            xor eax, eax
  323.            ret
  324. endp
  325.  
  326. handle     equ  IOCTL.handle
  327. io_code    equ  IOCTL.io_code
  328. input      equ  IOCTL.input
  329. inp_size   equ  IOCTL.inp_size
  330. output     equ  IOCTL.output
  331. out_size   equ  IOCTL.out_size
  332.  
  333. align 4
  334. proc service_proc stdcall, ioctl:dword
  335.  
  336.            mov edi, [ioctl]
  337.            mov eax, [edi+io_code]
  338.            cmp eax, DEV_PLAY
  339.            jne @F
  340.      if DEBUG
  341.            mov esi, msgPlay
  342.            call SysMsgBoardStr
  343.      end if
  344.            call play
  345.            ret
  346. @@:
  347.            cmp eax, DEV_STOP
  348.            jne @F
  349.      if DEBUG
  350.            mov esi, msgStop
  351.            call SysMsgBoardStr
  352.      end if
  353.            call stop
  354.            ret
  355. @@:
  356.            cmp eax, DEV_CALLBACK
  357.            jne @F
  358.            mov ebx, [edi+input]
  359.            stdcall set_callback, [ebx]
  360.            ret
  361. @@:
  362.            cmp eax, DEV_SET_MASTERVOL
  363.            jne @F
  364.            mov ebx, [edi+input]
  365.            stdcall set_master_vol, [ebx]
  366.            ret
  367. @@:
  368.            cmp eax, DEV_GET_MASTERVOL
  369.            jne @F
  370.            mov ebx, [edi+output]
  371.            test ebx, ebx
  372.            jz .fail
  373.  
  374.            stdcall get_master_vol, ebx
  375.            ret
  376. @@:
  377.            cmp eax, DEV_GET_INFO
  378.            jne @F
  379.            mov ebx, [edi+output]
  380.            stdcall get_dev_info, ebx
  381.            ret
  382. @@:
  383. .fail:
  384.            xor eax, eax
  385.            ret
  386. endp
  387.  
  388. restore   handle
  389. restore   io_code
  390. restore   input
  391. restore   inp_size
  392. restore   output
  393. restore   out_size
  394.  
  395. align 4
  396. proc ac97_irq
  397.  
  398. ;     if DEBUG
  399. ;           mov esi, msgIRQ
  400. ;           call SysMsgBoardStr
  401. ;     end if
  402.  
  403.            mov edx, PCM_OUT_CR_REG
  404.            mov al, 0x14
  405.            call [ctrl.ctrl_write8]
  406.  
  407.            mov ax, 0x1c
  408.            mov edx, PCM_OUT_SR_REG
  409.            call [ctrl.ctrl_write16]
  410.  
  411.            mov edx, PCM_OUT_CIV_REG
  412.            call [ctrl.ctrl_read8]
  413.  
  414.            and eax, 0x1F
  415.            cmp eax, [civ_val]
  416.            je .skip
  417.  
  418.            mov [civ_val], eax
  419.            dec eax
  420.            and eax, 0x1F
  421.            mov [ctrl.lvi_reg], eax
  422.  
  423.            mov edx, PCM_OUT_LVI_REG
  424.            call [ctrl.ctrl_write8]
  425.  
  426.            mov edx, PCM_OUT_CR_REG
  427.            mov ax, 0x1D
  428.            call [ctrl.ctrl_write8]
  429.  
  430.            mov eax, [civ_val]
  431.            add eax, 2
  432.            and eax, 31
  433.            mov ebx, dword [buff_list+eax*4]
  434.  
  435.            cmp [ctrl.user_callback], 0
  436.            je @f
  437.  
  438.            stdcall [ctrl.user_callback], ebx
  439. @@:
  440.            ret
  441.  
  442. .skip:
  443.            mov edx, PCM_OUT_CR_REG
  444.            mov ax, 0x1D
  445.            call [ctrl.ctrl_write8]
  446.            ret
  447. endp
  448.  
  449. align 4
  450. proc create_primary_buff
  451.  
  452.            stdcall KernelAlloc, 0x10000
  453.            mov [ctrl.buffer], eax
  454.  
  455.            mov edi, eax
  456.            mov ecx, 0x10000/4
  457.            xor eax, eax
  458.            rep stosd
  459.  
  460.            stdcall GetPgAddr, [ctrl.buffer]
  461.  
  462.            mov ebx, 0xC0004000
  463.            mov ecx, 4
  464.            mov edi, pcmout_bdl
  465. @@:
  466.            mov [edi], eax
  467.            mov [edi+4], ebx
  468.            mov [edi+32], eax
  469.            mov [edi+4+32], ebx
  470.            mov [edi+64], eax
  471.            mov [edi+4+64], ebx
  472.            mov [edi+96], eax
  473.            mov [edi+4+96], ebx
  474.            mov [edi+128], eax
  475.            mov [edi+4+128], ebx
  476.            mov [edi+160], eax
  477.            mov [edi+4+160], ebx
  478.            mov [edi+192], eax
  479.            mov [edi+4+192], ebx
  480.            mov [edi+224], eax
  481.            mov [edi+4+224], ebx
  482.  
  483.            add eax, 0x4000
  484.            add edi, 8
  485.            loop @B
  486.  
  487.            mov edi, buff_list
  488.            mov eax, [ctrl.buffer]
  489.            mov ecx, 4
  490. @@:
  491.            mov [edi], eax
  492.            mov [edi+16], eax
  493.            mov [edi+32], eax
  494.            mov [edi+48], eax
  495.            mov [edi+64], eax
  496.            mov [edi+80], eax
  497.            mov [edi+96], eax
  498.            mov [edi+112], eax
  499.  
  500.            add eax, 0x4000
  501.            add edi, 4
  502.            loop @B
  503.  
  504.            mov ecx, pcmout_bdl
  505.            stdcall GetPgAddr, ecx
  506.            and ecx, 0xFFF
  507.            add eax, ecx
  508.  
  509.            mov edx, PCM_OUT_BDL
  510.            call [ctrl.ctrl_write32]
  511.  
  512.            mov eax, 16
  513.            mov [ctrl.lvi_reg], eax
  514.            mov edx, PCM_OUT_LVI_REG
  515.            call [ctrl.ctrl_write8]
  516.  
  517.            mov edx, GLOB_CTRL
  518.            call [ctrl.ctrl_read32]
  519.            and eax, not 0x000000C0
  520.            mov edx, GLOB_CTRL
  521.            call [ctrl.ctrl_write32]
  522.  
  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.            mov edi, devices
  553. @@:
  554.            mov ebx, [edi]
  555.            test ebx, ebx
  556.            jz .next
  557.  
  558.            cmp eax, ebx
  559.            je .found
  560.            add edi, 12
  561.            jmp @B
  562.  
  563. .next:
  564.            inc [devfn]
  565.            cmp [devfn], 256
  566.            jb  .next_dev
  567.            mov eax, [bus]
  568.            inc eax
  569.            mov [bus], eax
  570.            cmp eax, [last_bus]
  571.            jna .next_bus
  572.            xor eax, eax
  573.            ret
  574. .found:
  575.            mov ebx, [bus]
  576.            mov [ctrl.bus], ebx
  577.  
  578.            mov ecx, [devfn]
  579.            mov [ctrl.devfn], ecx
  580.  
  581.            mov edx, eax
  582.            and edx, 0xFFFF
  583.            mov [ctrl.vendor], edx
  584.            shr eax, 16
  585.            mov [ctrl.dev_id], eax
  586.  
  587.            mov ebx, [edi+4]
  588.            mov [ctrl.ctrl_ids], ebx
  589.            mov [ctrl.vendor_ids], msg_SIS
  590.  
  591.            mov esi, [edi+8]
  592.            mov [ctrl.ctrl_setup], esi
  593.            ret
  594. .err:
  595.            xor eax, eax
  596.            ret
  597. endp
  598.  
  599. align 4
  600. proc init_controller
  601.  
  602.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 4
  603.            mov ebx, eax
  604.            and eax, 0xFFFF
  605.            mov [ctrl.pci_cmd], eax
  606.            shr ebx, 16
  607.            mov [ctrl.pci_stat], ebx
  608.  
  609.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
  610.            and eax,0xFFFE
  611.            mov [ctrl.codec_io_base], eax
  612.  
  613.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x14
  614.            and eax, 0xFFC0
  615.            mov [ctrl.ctrl_io_base], eax
  616.  
  617.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x18
  618.            mov [ctrl.codec_mem_base], eax
  619.  
  620.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x1C
  621.            mov [ctrl.ctrl_mem_base], eax
  622.  
  623.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
  624.            and eax, 0xFF
  625.            mov [ctrl.int_line], eax
  626.  
  627.            stdcall PciRead8, [ctrl.bus], [ctrl.devfn], dword 0x41
  628.            and eax, 0xFF
  629.            mov [ctrl.cfg_reg], eax
  630.  
  631.            call [ctrl.ctrl_setup]
  632.            xor eax, eax
  633.            inc eax
  634.            ret
  635. endp
  636.  
  637. align 4
  638. proc set_SIS
  639.            mov [ctrl.codec_read16],  codec_io_r16    ;virtual
  640.            mov [ctrl.codec_write16], codec_io_w16    ;virtual
  641.  
  642.            mov [ctrl.ctrl_read8 ],  ctrl_io_r8      ;virtual
  643.            mov [ctrl.ctrl_read16],  ctrl_io_r16      ;virtual
  644.            mov [ctrl.ctrl_read32],  ctrl_io_r32      ;virtual
  645.  
  646.            mov [ctrl.ctrl_write8 ], ctrl_io_w8      ;virtual
  647.            mov [ctrl.ctrl_write16], ctrl_io_w16      ;virtual
  648.            mov [ctrl.ctrl_write32], ctrl_io_w32      ;virtual
  649.            ret
  650. endp
  651.  
  652. align 4
  653. proc reset_controller
  654.  
  655.            xor eax, eax
  656.            mov edx, PCM_IN_CR_REG
  657.            call [ctrl.ctrl_write8]
  658.  
  659.            mov edx, PCM_OUT_CR_REG
  660.            call [ctrl.ctrl_write8]
  661.  
  662.            mov edx, MC_IN_CR_REG
  663.            call [ctrl.ctrl_write8]
  664.  
  665.            mov eax, RR
  666.            mov edx, PCM_IN_CR_REG
  667.            call [ctrl.ctrl_write8]
  668.  
  669.            mov edx, PCM_OUT_CR_REG
  670.            call [ctrl.ctrl_write8]
  671.  
  672.            mov edx, MC_IN_CR_REG
  673.            call [ctrl.ctrl_write8]
  674.            ret
  675. endp
  676.  
  677. align 4
  678. proc init_codec
  679.            locals
  680.              counter dd ?
  681.            endl
  682.  
  683.            mov esi, msgControl
  684.            call SysMsgBoardStr
  685.  
  686.            mov edx, GLOB_CTRL
  687.            call [ctrl.ctrl_read32]
  688.            call dword2str
  689.            call SysMsgBoardStr
  690.  
  691.            mov esi, msgStatus
  692.            call SysMsgBoardStr
  693.  
  694.            mov edx, CTRL_STAT
  695.            call [ctrl.ctrl_read32]
  696.  
  697.            call dword2str
  698.            call SysMsgBoardStr
  699.  
  700.            test eax, CTRL_ST_CREADY
  701.            jnz .ready
  702.  
  703.            call reset_codec
  704.            and eax, eax
  705.            jz .err
  706.  
  707.            xor edx, edx     ;ac_reg_0
  708.            call [ctrl.codec_write16]
  709.  
  710.            xor eax, eax
  711.            mov edx, CODEC_REG_POWERDOWN
  712.            call [ctrl.codec_write16]
  713.  
  714.            mov [counter], 200     ; total 200*5 ms = 1s
  715. .wait:
  716.            mov edx, CODEC_REG_POWERDOWN
  717.            call [ctrl.codec_read16]
  718.            and eax, 0x0F
  719.            cmp eax, 0x0F
  720.            je .ready
  721.  
  722.            mov eax, 5000          ; wait 5 ms
  723.            call StallExec
  724.            sub [counter] , 1
  725.            jnz .wait
  726. .err:
  727.            xor eax, eax        ; timeout error
  728.            ret
  729. .ready:
  730.            call detect_codec
  731.  
  732.            xor eax, eax
  733.            inc eax
  734.            ret
  735. endp
  736.  
  737. align 4
  738. proc reset_codec
  739.            mov edx, GLOB_CTRL
  740.            call [ctrl.ctrl_read32]
  741.  
  742.            test eax, 0x02
  743.            jz .cold
  744.  
  745.            call warm_reset
  746.            jnc .ok
  747. .cold:
  748.            call cold_reset
  749.            jnc .ok
  750.  
  751.      if DEBUG
  752.            mov esi, msgCFail
  753.            call SysMsgBoardStr
  754.      end if
  755.            xor eax, eax     ; timeout error
  756.            ret
  757. .ok:
  758.            xor eax, eax
  759.            inc eax
  760.            ret
  761. endp
  762.  
  763. align 4
  764. proc warm_reset
  765.            locals
  766.              counter dd ?
  767.            endl
  768.  
  769.            mov eax, 0x06
  770.            mov edx, GLOB_CTRL
  771.            call [ctrl.ctrl_write32]
  772.  
  773.      if DEBUG
  774.            mov esi, msgWarm
  775.            call SysMsgBoardStr
  776.      end if
  777.  
  778.            mov [counter], 10    ; total 10*100 ms = 1s
  779. .wait:
  780.            mov eax, 100000    ; wait 100 ms
  781.            call StallExec
  782.  
  783.            mov edx, GLOB_CTRL
  784.            call [ctrl.ctrl_read32]
  785.            test eax, 4
  786.            jz .ok
  787.            sub [counter], 1
  788.            jnz .wait
  789.  
  790.      if DEBUG
  791.            mov esi, msgWRFail
  792.            call SysMsgBoardStr
  793.      end if
  794.            stc
  795.            ret
  796. .ok:
  797.            mov edx, CTRL_STAT
  798.            call [ctrl.ctrl_read32]
  799.            and eax, CTRL_ST_CREADY
  800.            jz .fail
  801.            clc
  802.            ret
  803. .fail:
  804.            stc
  805.            ret
  806. endp
  807.  
  808. align 4
  809. proc cold_reset
  810.            locals
  811.              counter dd ?
  812.             endl
  813.  
  814.            mov edx, GLOB_CTRL
  815.            call [ctrl.ctrl_read32]
  816.            and eax, not 0x08
  817.            or eax, 0x02
  818.            mov edx, GLOB_CTRL
  819.            call [ctrl.ctrl_write32]
  820.  
  821.      if DEBUG
  822.            mov esi, msgCold
  823.            call SysMsgBoardStr
  824.      end if
  825.  
  826.            mov [counter], 10    ; total 10*100 ms = 1s
  827. .wait:
  828.            mov eax, 100000    ; wait 100 ms
  829.            call StallExec
  830.  
  831.            mov edx, GLOB_CTRL
  832.            call [ctrl.ctrl_read32]
  833.            test eax, 4
  834.            jz .ok
  835.            sub [counter], 1
  836.            jnz .wait
  837.  
  838.      if DEBUG
  839.            mov esi, msgCRFail
  840.            call SysMsgBoardStr
  841.      end if
  842. .fail:
  843.            stc
  844.            ret
  845. .ok:
  846.            mov edx, CTRL_STAT
  847.            call [ctrl.ctrl_read32]
  848.            and eax, CTRL_ST_CREADY
  849.            jz .fail
  850.            clc
  851.            ret
  852. endp
  853.  
  854. align 4
  855. proc play
  856.            xor eax, eax
  857.            mov [civ_val], eax
  858.            mov edx, PCM_OUT_CIV_REG
  859.            call [ctrl.ctrl_write8]
  860.  
  861.            mov eax, 16
  862.            mov [ctrl.lvi_reg], eax
  863.            mov edx, PCM_OUT_LVI_REG
  864.            call [ctrl.ctrl_write8]
  865.  
  866.            mov edx, PCM_OUT_CR_REG
  867.            mov ax, 0x1D
  868.            call [ctrl.ctrl_write8]
  869.            ret
  870. endp
  871.  
  872. align 4
  873. proc stop
  874.            mov edx, PCM_OUT_CR_REG
  875.            mov ax, 0x0
  876.            call [ctrl.ctrl_write8]
  877.  
  878.            mov ax, 0x1c
  879.            mov edx, PCM_OUT_SR_REG
  880.            call [ctrl.ctrl_write16]
  881.            ret
  882. endp
  883.  
  884. align 4
  885. proc get_dev_info stdcall, p_info:dword
  886.            virtual at esi
  887.              CTRL_INFO CTRL_INFO
  888.            end virtual
  889.  
  890.            mov esi, [p_info]
  891.            mov eax, [ctrl.int_line]
  892.            mov ebx, [ctrl.codec_io_base]
  893.            mov ecx, [ctrl.ctrl_io_base]
  894.            mov edx, [ctrl.codec_mem_base]
  895.            mov edi, [ctrl.ctrl_mem_base]
  896.  
  897.            mov [CTRL_INFO.irq], eax
  898.            mov [CTRL_INFO.codec_io_base], ebx
  899.            mov [CTRL_INFO.ctrl_io_base], ecx
  900.            mov [CTRL_INFO.codec_mem_base], edx
  901.            mov [CTRL_INFO.ctrl_mem_base], edi
  902.  
  903.            mov eax, [codec.chip_id]
  904.            mov [CTRL_INFO.codec_id], eax
  905.  
  906.            mov edx, GLOB_CTRL
  907.            call [ctrl.ctrl_read32]
  908.            mov [CTRL_INFO.glob_cntrl], eax
  909.  
  910.            mov edx, CTRL_STAT
  911.            call [ctrl.ctrl_read32]
  912.            mov [CTRL_INFO.glob_sta], eax
  913.  
  914.            mov ebx, [ctrl.pci_cmd]
  915.            mov [CTRL_INFO.pci_cmd], ebx
  916.  
  917.            ret
  918. endp
  919.  
  920. align 4
  921. proc set_callback stdcall, handler:dword
  922.            mov eax, [handler]
  923.            mov [ctrl.user_callback], eax
  924.            ret
  925. endp
  926.  
  927. align 4
  928. proc codec_read stdcall, ac_reg:dword      ; reg = edx, reval = eax
  929.  
  930.            mov edx, [ac_reg]
  931.  
  932.            mov ebx, edx
  933.            shr ebx, 1
  934.            bt [codec.shadow_flag], ebx
  935.            jc .use_shadow
  936.  
  937.            call [ctrl.codec_read16]  ;change edx !!!
  938.            mov ecx, eax
  939.  
  940.            mov edx, CTRL_STAT
  941.            call [ctrl.ctrl_read32]
  942.            test eax, CTRL_ST_RCS
  943.            jz .read_ok
  944.  
  945.            mov edx, CTRL_STAT
  946.            call [ctrl.ctrl_write32]
  947.            xor eax,eax
  948.            not eax  ;timeout
  949.            ret
  950. .read_ok:
  951.            mov edx, [ac_reg]
  952.            mov [codec.regs+edx], cx
  953.            bts [codec.shadow_flag], ebx
  954.            mov eax, ecx
  955.            ret
  956. .use_shadow:
  957.            movzx eax, word [codec.regs+edx]
  958.            ret
  959. endp
  960.  
  961. align 4
  962. proc codec_write stdcall, ac_reg:dword
  963.            push eax
  964.            call check_semafore
  965.            and eax, eax
  966.            jz .err
  967.            pop eax
  968.  
  969.            mov esi, [ac_reg]
  970.            mov edx, esi
  971.            call [ctrl.codec_write16]
  972.            mov [codec.regs+esi], ax
  973.            shr esi, 1
  974.            bts [codec.shadow_flag], esi
  975.            ret
  976. .err:
  977.            pop eax
  978.            ret
  979. endp
  980.  
  981.  
  982. align 4
  983. proc codec_check_ready
  984.  
  985.            mov edx, CTRL_ST
  986.            call [ctrl.ctrl_read32]
  987.            and eax, CTRL_ST_CREADY
  988.            jz .not_ready
  989.  
  990.            xor eax, wax
  991.            inc eax
  992.            ret
  993.  
  994. align 4
  995. .not_ready:
  996.            xor eax, eax
  997.            ret
  998. endp
  999.  
  1000.  
  1001. align 4
  1002. proc check_semafore
  1003.            local counter:DWORD
  1004.  
  1005.            mov [counter], 100
  1006. .l1:
  1007.            mov edx, CTRL_CAS
  1008.            call [ctrl.ctrl_read8]
  1009.            and eax, CAS_FLAG
  1010.            jz .ok
  1011.  
  1012.            mov eax, 1
  1013.            call StallExec
  1014.            sub [counter], 1
  1015.            jnz .l1
  1016.            xor eax, eax
  1017.            ret
  1018. align 4
  1019. .ok:
  1020.            xor eax,eax
  1021.            inc eax
  1022.            ret
  1023. endp
  1024.  
  1025. align 4
  1026. proc StallExec
  1027.            push ecx
  1028.            push edx
  1029.            push ebx
  1030.            push eax
  1031.  
  1032.            mov ecx, CPU_FREQ
  1033.            mul ecx
  1034.            mov ebx, eax       ;low
  1035.            mov ecx, edx       ;high
  1036.            rdtsc
  1037.            add ebx, eax
  1038.            adc ecx, edx
  1039. @@:
  1040.            rdtsc
  1041.            sub eax, ebx
  1042.            sbb edx, ecx
  1043.            js @B
  1044.  
  1045.            pop eax
  1046.            pop ebx
  1047.            pop edx
  1048.            pop ecx
  1049.            ret
  1050. endp
  1051.  
  1052. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1053. ;          CONTROLLER IO functions
  1054. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1055.  
  1056. align 4
  1057. proc codec_io_r16
  1058.            add edx, [ctrl.codec_io_base]
  1059.            in  ax, dx
  1060.            ret
  1061. endp
  1062.  
  1063. align 4
  1064. proc codec_io_w16
  1065.            add edx, [ctrl.codec_io_base]
  1066.            out dx, ax
  1067.            ret
  1068. endp
  1069.  
  1070. align 4
  1071. proc ctrl_io_r8
  1072.            add edx, [ctrl.ctrl_io_base]
  1073.            in  al, dx
  1074.            ret
  1075. endp
  1076.  
  1077. align 4
  1078. proc ctrl_io_r16
  1079.            add edx, [ctrl.ctrl_io_base]
  1080.            in  ax, dx
  1081.            ret
  1082. endp
  1083.  
  1084. align 4
  1085. proc ctrl_io_r32
  1086.            add edx, [ctrl.ctrl_io_base]
  1087.            in  eax, dx
  1088.            ret
  1089. endp
  1090.  
  1091. align 4
  1092. proc ctrl_io_w8
  1093.            add edx, [ctrl.ctrl_io_base]
  1094.            out dx, al
  1095.            ret
  1096. endp
  1097.  
  1098. align 4
  1099. proc ctrl_io_w16
  1100.            add edx, [ctrl.ctrl_io_base]
  1101.            out dx, ax
  1102.            ret
  1103. endp
  1104.  
  1105. align 4
  1106. proc ctrl_io_w32
  1107.            add edx, [ctrl.ctrl_io_base]
  1108.            out dx, eax
  1109.            ret
  1110. endp
  1111.  
  1112. align 4
  1113. dword2str:
  1114.       mov  esi, hex_buff
  1115.       mov ecx, -8
  1116. @@:
  1117.       rol eax, 4
  1118.       mov ebx, eax
  1119.       and ebx, 0x0F
  1120.       mov bl, [ebx+hexletters]
  1121.       mov [8+esi+ecx], bl
  1122.       inc ecx
  1123.       jnz @B
  1124.       ret
  1125.  
  1126.  
  1127. include "codec.inc"
  1128.  
  1129. align 4
  1130. devices dd (CTRL_SIS  shl 16)+VID_SIS,msg_AC, set_SIS
  1131.         dd 0
  1132.  
  1133. version      dd 0x00020002
  1134.  
  1135. msg_AC       db '7012 AC97 controller',13,10, 0
  1136. msg_SIS      db 'Silicon Integrated Systems',13,10, 0
  1137.  
  1138. sz_sound_srv        db 'SOUND',0
  1139.  
  1140. msgInit      db 'detect hardware...',13,10,0
  1141. msgFail      db 'device not found',13,10,0
  1142. msgPlay      db 'start play', 13,10,0
  1143. msgStop      db 'stop play',  13,10,0
  1144. msgNotify    db 'call notify',13,10,0
  1145. msgIRQ       db 'AC97 IRQ', 13,10,0
  1146. msgInitCtrl  db 'init controller',13,10,0
  1147. msgInitCodec db 'init codec',13,10,0
  1148. msgPrimBuff  db 'create primary buffer',13,10,0
  1149. msgReg       db 'set service handler',13,10,0
  1150. msgOk        db 'service installed',13,10,0
  1151. msgCold      db 'cold resret',13,10,0
  1152. msgWarm      db 'warm reset',13,10,0
  1153. msgWRFail    db 'warm reset failed',13,10,0
  1154. msgCRFail    db 'cold reset failed',13,10,0
  1155. msgCFail     db 'codec not ready',13,10,0
  1156. msgStatus    db 'global status   ',0
  1157. msgControl   db 'global control  ',0
  1158.  
  1159. hexletters   db '0123456789ABCDEF'
  1160. hex_buff     db 8 dup(0),13,10,0
  1161.  
  1162.  
  1163. section '.data' data readable writable align 16
  1164.  
  1165. pcmout_bdl       rq 32
  1166. buff_list        rd 32
  1167.  
  1168. codec CODEC
  1169. ctrl AC_CNTRL
  1170.  
  1171. lpc_bus  rd 1
  1172. civ_val  rd 1
  1173.