Subversion Repositories Kolibri OS

Rev

Rev 285 | 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. PROC_BASE          equ OS_BASE+0x0080000
  248. new_app_base       equ 0x80000000
  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 eax, [edi+input]
  365.            mov eax, [eax]
  366.            call set_master_vol      ;eax= vol
  367.            ret
  368. @@:
  369.            cmp eax, DEV_GET_MASTERVOL
  370.            jne @F
  371.            mov ebx, [edi+output]
  372.            add ebx, new_app_base
  373.            stdcall get_master_vol, ebx
  374.            ret
  375. @@:
  376.            cmp eax, DEV_GET_INFO
  377.            jne @F
  378.            mov ebx, [edi+output]
  379.            stdcall get_dev_info, ebx
  380.            ret
  381. @@:
  382. .fail:
  383.            or eax, -1
  384.            ret
  385. endp
  386.  
  387. restore   handle
  388. restore   io_code
  389. restore   input
  390. restore   inp_size
  391. restore   output
  392. restore   out_size
  393.  
  394. align 4
  395. proc ac97_irq
  396.  
  397. ;     if DEBUG
  398. ;           mov esi, msgIRQ
  399. ;           call SysMsgBoardStr
  400. ;     end if
  401.  
  402.            mov edx, PCM_OUT_CR_REG
  403.            mov al, 0x10
  404.            call [ctrl.ctrl_write8]
  405.  
  406.            mov ax, 0x1c
  407.            mov edx, PCM_OUT_SR_REG
  408.            call [ctrl.ctrl_write16]
  409.  
  410.            mov edx, PCM_OUT_CIV_REG
  411.            call [ctrl.ctrl_read8]
  412.  
  413.            and eax, 0x1F
  414.            cmp eax, [civ_val]
  415.            je .skip
  416.  
  417.            mov [civ_val], eax
  418.            dec eax
  419.            and eax, 0x1F
  420.            mov [ctrl.lvi_reg], eax
  421.  
  422.            mov edx, PCM_OUT_LVI_REG
  423.            call [ctrl.ctrl_write8]
  424.  
  425.            mov edx, PCM_OUT_CR_REG
  426.            mov ax, 0x11
  427.            call [ctrl.ctrl_write8]
  428.  
  429.            mov eax, [civ_val]
  430.            add eax, 1
  431.            and eax, 31
  432.            mov ebx, dword [buff_list+eax*4]
  433.  
  434.            cmp [ctrl.user_callback], 0
  435.            je @f
  436.  
  437.            stdcall [ctrl.user_callback], ebx
  438. @@:
  439.            ret
  440.  
  441. .skip:
  442.            mov edx, PCM_OUT_CR_REG
  443.            mov ax, 0x11
  444.            call [ctrl.ctrl_write8]
  445.            ret
  446. endp
  447.  
  448. align 4
  449. proc create_primary_buff
  450.  
  451.            stdcall KernelAlloc, 0x10000
  452.            mov [ctrl.buffer], eax
  453.  
  454.            mov edi, eax
  455.            mov ecx, 0x10000/4
  456.            xor eax, eax
  457.            cld
  458.            rep stosd
  459.  
  460.            mov eax, [ctrl.buffer]
  461.            call GetPgAddr
  462.  
  463.            mov ebx, 0xC0004000
  464.            mov ecx, 4
  465.            mov edi, pcmout_bdl
  466. @@:
  467.            mov [edi], eax
  468.            mov [edi+4], ebx
  469.            mov [edi+32], eax
  470.            mov [edi+4+32], ebx
  471.            mov [edi+64], eax
  472.            mov [edi+4+64], ebx
  473.            mov [edi+96], eax
  474.            mov [edi+4+96], ebx
  475.            mov [edi+128], eax
  476.            mov [edi+4+128], ebx
  477.            mov [edi+160], eax
  478.            mov [edi+4+160], ebx
  479.            mov [edi+192], eax
  480.            mov [edi+4+192], ebx
  481.            mov [edi+224], eax
  482.            mov [edi+4+224], ebx
  483.  
  484.            add eax, 0x4000
  485.            add edi, 8
  486.            loop @B
  487.  
  488.            mov edi, buff_list
  489.            mov eax, [ctrl.buffer]
  490.            mov ecx, 4
  491. @@:
  492.            mov [edi], eax
  493.            mov [edi+16], eax
  494.            mov [edi+32], eax
  495.            mov [edi+48], eax
  496.            mov [edi+64], eax
  497.            mov [edi+80], eax
  498.            mov [edi+96], eax
  499.            mov [edi+112], eax
  500.  
  501.            add eax, 0x4000
  502.            add edi, 4
  503.            loop @B
  504.  
  505.            mov eax, pcmout_bdl
  506.            mov ebx, eax
  507.            call GetPgAddr     ;eax
  508.            and ebx, 0xFFF
  509.            add eax, ebx
  510.  
  511.            mov edx, PCM_OUT_BDL
  512.            call [ctrl.ctrl_write32]
  513.  
  514.            mov eax, 16
  515.            mov [ctrl.lvi_reg], eax
  516.            mov edx, PCM_OUT_LVI_REG
  517.            call [ctrl.ctrl_write8]
  518.  
  519.            mov edx, GLOB_CTRL
  520.            call [ctrl.ctrl_read32]
  521.            and eax, not 0x000000C0
  522.            mov edx, GLOB_CTRL
  523.            call [ctrl.ctrl_write32]
  524.  
  525.  
  526.            ret
  527. endp
  528.  
  529. align 4
  530. proc detect_controller
  531.            locals
  532.              last_bus dd ?
  533.              bus      dd ?
  534.              devfn    dd ?
  535.            endl
  536.  
  537.            xor eax, eax
  538.            mov [bus], eax
  539.            inc eax
  540.            call PciApi
  541.            cmp eax, -1
  542.            je .err
  543.  
  544.            mov [last_bus], eax
  545.  
  546. .next_bus:
  547.            and [devfn], 0
  548. .next_dev:
  549.            stdcall PciRead32, [bus], [devfn], dword 0
  550.            test eax, eax
  551.            jz .next
  552.            cmp eax, -1
  553.            je .next
  554.            mov edi, devices
  555. @@:
  556.            mov ebx, [edi]
  557.            test ebx, ebx
  558.            jz .next
  559.  
  560.            cmp eax, ebx
  561.            je .found
  562.            add edi, 12
  563.            jmp @B
  564.  
  565. .next:
  566.            inc [devfn]
  567.            cmp [devfn], 256
  568.            jb  .next_dev
  569.            mov eax, [bus]
  570.            inc eax
  571.            mov [bus], eax
  572.            cmp eax, [last_bus]
  573.            jna .next_bus
  574.            xor eax, eax
  575.            ret
  576. .found:
  577.            mov ebx, [bus]
  578.            mov [ctrl.bus], ebx
  579.  
  580.            mov ecx, [devfn]
  581.            mov [ctrl.devfn], ecx
  582.  
  583.            mov edx, eax
  584.            and edx, 0xFFFF
  585.            mov [ctrl.vendor], edx
  586.            shr eax, 16
  587.            mov [ctrl.dev_id], eax
  588.  
  589.            mov ebx, [edi+4]
  590.            mov [ctrl.ctrl_ids], ebx
  591.            mov [ctrl.vendor_ids], msg_SIS
  592.  
  593.            mov esi, [edi+8]
  594.            mov [ctrl.ctrl_setup], esi
  595.            ret
  596. .err:
  597.            xor eax, eax
  598.            ret
  599. endp
  600.  
  601. align 4
  602. proc init_controller
  603.  
  604.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 4
  605.            mov ebx, eax
  606.            and eax, 0xFFFF
  607.            mov [ctrl.pci_cmd], eax
  608.            shr ebx, 16
  609.            mov [ctrl.pci_stat], ebx
  610.  
  611.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x10
  612.            and eax,0xFFFE
  613.            mov [ctrl.codec_io_base], eax
  614.  
  615.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x14
  616.            and eax, 0xFFC0
  617.            mov [ctrl.ctrl_io_base], eax
  618.  
  619.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x18
  620.            mov [ctrl.codec_mem_base], eax
  621.  
  622.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x1C
  623.            mov [ctrl.ctrl_mem_base], eax
  624.  
  625.            stdcall PciRead32, [ctrl.bus], [ctrl.devfn], dword 0x3C
  626.            and eax, 0xFF
  627.            mov [ctrl.int_line], eax
  628.  
  629.            stdcall PciRead8, [ctrl.bus], [ctrl.devfn], dword 0x41
  630.            and eax, 0xFF
  631.            mov [ctrl.cfg_reg], eax
  632.  
  633.            call [ctrl.ctrl_setup]
  634.            xor eax, eax
  635.            inc eax
  636.            ret
  637. endp
  638.  
  639. align 4
  640. proc set_SIS
  641.            mov [ctrl.codec_read16],  codec_io_r16    ;virtual
  642.            mov [ctrl.codec_write16], codec_io_w16    ;virtual
  643.  
  644.            mov [ctrl.ctrl_read8 ],  ctrl_io_r8      ;virtual
  645.            mov [ctrl.ctrl_read16],  ctrl_io_r16      ;virtual
  646.            mov [ctrl.ctrl_read32],  ctrl_io_r32      ;virtual
  647.  
  648.            mov [ctrl.ctrl_write8 ], ctrl_io_w8      ;virtual
  649.            mov [ctrl.ctrl_write16], ctrl_io_w16      ;virtual
  650.            mov [ctrl.ctrl_write32], ctrl_io_w32      ;virtual
  651.            ret
  652. endp
  653.  
  654. align 4
  655. proc reset_controller
  656.  
  657.            xor eax, eax
  658.            mov edx, PCM_IN_CR_REG
  659.            call [ctrl.ctrl_write8]
  660.  
  661.            mov edx, PCM_OUT_CR_REG
  662.            call [ctrl.ctrl_write8]
  663.  
  664.            mov edx, MC_IN_CR_REG
  665.            call [ctrl.ctrl_write8]
  666.  
  667.            mov eax, RR
  668.            mov edx, PCM_IN_CR_REG
  669.            call [ctrl.ctrl_write8]
  670.  
  671.            mov edx, PCM_OUT_CR_REG
  672.            call [ctrl.ctrl_write8]
  673.  
  674.            mov edx, MC_IN_CR_REG
  675.            call [ctrl.ctrl_write8]
  676.            ret
  677. endp
  678.  
  679. align 4
  680. proc init_codec
  681.            locals
  682.              counter dd ?
  683.            endl
  684.  
  685.            mov esi, msgControl
  686.            call SysMsgBoardStr
  687.  
  688.            mov edx, GLOB_CTRL
  689.            call [ctrl.ctrl_read32]
  690.            call dword2str
  691.            call SysMsgBoardStr
  692.  
  693.            mov esi, msgStatus
  694.            call SysMsgBoardStr
  695.  
  696.            mov edx, CTRL_STAT
  697.            call [ctrl.ctrl_read32]
  698.  
  699.            call dword2str
  700.            call SysMsgBoardStr
  701.  
  702.            test eax, CTRL_ST_CREADY
  703.            jnz .ready
  704.  
  705.            call reset_codec
  706.            and eax, eax
  707.            jz .err
  708.  
  709.            xor edx, edx     ;ac_reg_0
  710.            call [ctrl.codec_write16]
  711.  
  712.            xor eax, eax
  713.            mov edx, CODEC_REG_POWERDOWN
  714.            call [ctrl.codec_write16]
  715.  
  716.            mov [counter], 200     ; total 200*5 ms = 1s
  717. .wait:
  718.            mov edx, CODEC_REG_POWERDOWN
  719.            call [ctrl.codec_read16]
  720.            and eax, 0x0F
  721.            cmp eax, 0x0F
  722.            je .ready
  723.  
  724.            mov eax, 5000          ; wait 5 ms
  725.            call StallExec
  726.            sub [counter] , 1
  727.            jnz .wait
  728. .err:
  729.            xor eax, eax        ; timeout error
  730.            ret
  731. .ready:
  732.            call detect_codec
  733.  
  734.            xor eax, eax
  735.            inc eax
  736.            ret
  737. endp
  738.  
  739. align 4
  740. proc reset_codec
  741.            mov edx, GLOB_CTRL
  742.            call [ctrl.ctrl_read32]
  743.  
  744.            test eax, 0x02
  745.            jz .cold
  746.  
  747.            call warm_reset
  748.            jnc .ok
  749. .cold:
  750.            call cold_reset
  751.            jnc .ok
  752.  
  753.      if DEBUG
  754.            mov esi, msgCFail
  755.            call SysMsgBoardStr
  756.      end if
  757.            xor eax, eax     ; timeout error
  758.            ret
  759. .ok:
  760.            xor eax, eax
  761.            inc eax
  762.            ret
  763. endp
  764.  
  765. align 4
  766. proc warm_reset
  767.            locals
  768.              counter dd ?
  769.            endl
  770.  
  771.            mov eax, 0x06
  772.            mov edx, GLOB_CTRL
  773.            call [ctrl.ctrl_write32]
  774.  
  775.      if DEBUG
  776.            mov esi, msgWarm
  777.            call SysMsgBoardStr
  778.      end if
  779.  
  780.            mov [counter], 10    ; total 10*100 ms = 1s
  781. .wait:
  782.            mov eax, 100000    ; wait 100 ms
  783.            call StallExec
  784.  
  785.            mov edx, GLOB_CTRL
  786.            call [ctrl.ctrl_read32]
  787.            test eax, 4
  788.            jz .ok
  789.            sub [counter], 1
  790.            jnz .wait
  791.  
  792.      if DEBUG
  793.            mov esi, msgWRFail
  794.            call SysMsgBoardStr
  795.      end if
  796.            stc
  797.            ret
  798. .ok:
  799.            mov edx, CTRL_STAT
  800.            call [ctrl.ctrl_read32]
  801.            and eax, CTRL_ST_CREADY
  802.            jz .fail
  803.            clc
  804.            ret
  805. .fail:
  806.            stc
  807.            ret
  808. endp
  809.  
  810. align 4
  811. proc cold_reset
  812.            locals
  813.              counter dd ?
  814.             endl
  815.  
  816.            mov edx, GLOB_CTRL
  817.            call [ctrl.ctrl_read32]
  818.            and eax, not 0x08
  819.            or eax, 0x02
  820.            mov edx, GLOB_CTRL
  821.            call [ctrl.ctrl_write32]
  822.  
  823.      if DEBUG
  824.            mov esi, msgCold
  825.            call SysMsgBoardStr
  826.      end if
  827.  
  828.            mov [counter], 10    ; total 10*100 ms = 1s
  829. .wait:
  830.            mov eax, 100000    ; wait 100 ms
  831.            call StallExec
  832.  
  833.            mov edx, GLOB_CTRL
  834.            call [ctrl.ctrl_read32]
  835.            test eax, 4
  836.            jz .ok
  837.            sub [counter], 1
  838.            jnz .wait
  839.  
  840.      if DEBUG
  841.            mov esi, msgCRFail
  842.            call SysMsgBoardStr
  843.      end if
  844. .fail:
  845.            stc
  846.            ret
  847. .ok:
  848.            mov edx, CTRL_STAT
  849.            call [ctrl.ctrl_read32]
  850.            and eax, CTRL_ST_CREADY
  851.            jz .fail
  852.            clc
  853.            ret
  854. endp
  855.  
  856. align 4
  857. proc play
  858.            xor eax, eax
  859.            mov [civ_val], eax
  860.            mov edx, PCM_OUT_CIV_REG
  861.            call [ctrl.ctrl_write8]
  862.  
  863.            mov eax, 16
  864.            mov [ctrl.lvi_reg], eax
  865.            mov edx, PCM_OUT_LVI_REG
  866.            call [ctrl.ctrl_write8]
  867.  
  868.            mov edx, PCM_OUT_CR_REG
  869.            mov ax, 0x1D
  870.            call [ctrl.ctrl_write8]
  871.            ret
  872. endp
  873.  
  874. align 4
  875. proc stop
  876.            mov edx, PCM_OUT_CR_REG
  877.            mov ax, 0x0
  878.            call [ctrl.ctrl_write8]
  879.  
  880.            mov ax, 0x1c
  881.            mov edx, PCM_OUT_SR_REG
  882.            call [ctrl.ctrl_write16]
  883.            ret
  884. endp
  885.  
  886. align 4
  887. proc get_dev_info stdcall, p_info:dword
  888.            virtual at esi
  889.              CTRL_INFO CTRL_INFO
  890.            end virtual
  891.  
  892.            mov esi, [p_info]
  893.            mov eax, [ctrl.int_line]
  894.            mov ebx, [ctrl.codec_io_base]
  895.            mov ecx, [ctrl.ctrl_io_base]
  896.            mov edx, [ctrl.codec_mem_base]
  897.            mov edi, [ctrl.ctrl_mem_base]
  898.  
  899.            mov [CTRL_INFO.irq], eax
  900.            mov [CTRL_INFO.codec_io_base], ebx
  901.            mov [CTRL_INFO.ctrl_io_base], ecx
  902.            mov [CTRL_INFO.codec_mem_base], edx
  903.            mov [CTRL_INFO.ctrl_mem_base], edi
  904.  
  905.            mov eax, [codec.chip_id]
  906.            mov [CTRL_INFO.codec_id], eax
  907.  
  908.            mov edx, GLOB_CTRL
  909.            call [ctrl.ctrl_read32]
  910.            mov [CTRL_INFO.glob_cntrl], eax
  911.  
  912.            mov edx, CTRL_STAT
  913.            call [ctrl.ctrl_read32]
  914.            mov [CTRL_INFO.glob_sta], eax
  915.  
  916.            mov ebx, [ctrl.pci_cmd]
  917.            mov [CTRL_INFO.pci_cmd], ebx
  918.  
  919.            ret
  920. endp
  921.  
  922. align 4
  923. proc set_callback stdcall, handler:dword
  924.            mov eax, [handler]
  925.            mov [ctrl.user_callback], eax
  926.            ret
  927. endp
  928.  
  929. align 4
  930. proc codec_read stdcall, ac_reg:dword      ; reg = edx, reval = eax
  931.  
  932.            mov edx, [ac_reg]
  933.  
  934.            mov ebx, edx
  935.            shr ebx, 1
  936.            bt [codec.shadow_flag], ebx
  937.            jc .use_shadow
  938.  
  939.            call [ctrl.codec_read16]  ;change edx !!!
  940.            mov ecx, eax
  941.  
  942.            mov edx, CTRL_STAT
  943.            call [ctrl.ctrl_read32]
  944.            test eax, CTRL_ST_RCS
  945.            jz .read_ok
  946.  
  947.            mov edx, CTRL_STAT
  948.            call [ctrl.ctrl_write32]
  949.            xor eax,eax
  950.            not eax  ;timeout
  951.            ret
  952. .read_ok:
  953.            mov edx, [ac_reg]
  954.            mov [codec.regs+edx], cx
  955.            bts [codec.shadow_flag], ebx
  956.            mov eax, ecx
  957.            ret
  958. .use_shadow:
  959.            movzx eax, word [codec.regs+edx]
  960.            ret
  961. endp
  962.  
  963. align 4
  964. proc codec_write stdcall, ac_reg:dword
  965.            push eax
  966.            call check_semafore
  967.            and eax, eax
  968.            jz .err
  969.            pop eax
  970.  
  971.            mov esi, [ac_reg]
  972.            mov edx, esi
  973.            call [ctrl.codec_write16]
  974.            mov [codec.regs+esi], ax
  975.            shr esi, 1
  976.            bts [codec.shadow_flag], esi
  977.            ret
  978. .err:
  979.            pop eax
  980.            ret
  981. endp
  982.  
  983.  
  984. align 4
  985. proc codec_check_ready
  986.  
  987.            mov edx, CTRL_ST
  988.            call [ctrl.ctrl_read32]
  989.            and eax, CTRL_ST_CREADY
  990.            jz .not_ready
  991.  
  992.            xor eax, wax
  993.            inc eax
  994.            ret
  995.  
  996. align 4
  997. .not_ready:
  998.            xor eax, eax
  999.            ret
  1000. endp
  1001.  
  1002.  
  1003. align 4
  1004. proc check_semafore
  1005.            local counter:DWORD
  1006.  
  1007.            mov [counter], 100
  1008. .l1:
  1009.            mov edx, CTRL_CAS
  1010.            call [ctrl.ctrl_read8]
  1011.            and eax, CAS_FLAG
  1012.            jz .ok
  1013.  
  1014.            mov eax, 1
  1015.            call StallExec
  1016.            sub [counter], 1
  1017.            jnz .l1
  1018.            xor eax, eax
  1019.            ret
  1020. align 4
  1021. .ok:
  1022.            xor eax,eax
  1023.            inc eax
  1024.            ret
  1025. endp
  1026.  
  1027. align 4
  1028. proc StallExec
  1029.            push ecx
  1030.            push edx
  1031.            push ebx
  1032.            push eax
  1033.  
  1034.            mov ecx, CPU_FREQ
  1035.            mul ecx
  1036.            mov ebx, eax       ;low
  1037.            mov ecx, edx       ;high
  1038.            rdtsc
  1039.            add ebx, eax
  1040.            adc ecx, edx
  1041. @@:
  1042.            rdtsc
  1043.            sub eax, ebx
  1044.            sbb edx, ecx
  1045.            js @B
  1046.  
  1047.            pop eax
  1048.            pop ebx
  1049.            pop edx
  1050.            pop ecx
  1051.            ret
  1052. endp
  1053.  
  1054. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1055. ;          CONTROLLER IO functions
  1056. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1057.  
  1058. align 4
  1059. proc codec_io_r16
  1060.            add edx, [ctrl.codec_io_base]
  1061.            in  ax, dx
  1062.            ret
  1063. endp
  1064.  
  1065. align 4
  1066. proc codec_io_w16
  1067.            add edx, [ctrl.codec_io_base]
  1068.            out dx, ax
  1069.            ret
  1070. endp
  1071.  
  1072. align 4
  1073. proc ctrl_io_r8
  1074.            add edx, [ctrl.ctrl_io_base]
  1075.            in  al, dx
  1076.            ret
  1077. endp
  1078.  
  1079. align 4
  1080. proc ctrl_io_r16
  1081.            add edx, [ctrl.ctrl_io_base]
  1082.            in  ax, dx
  1083.            ret
  1084. endp
  1085.  
  1086. align 4
  1087. proc ctrl_io_r32
  1088.            add edx, [ctrl.ctrl_io_base]
  1089.            in  eax, dx
  1090.            ret
  1091. endp
  1092.  
  1093. align 4
  1094. proc ctrl_io_w8
  1095.            add edx, [ctrl.ctrl_io_base]
  1096.            out dx, al
  1097.            ret
  1098. endp
  1099.  
  1100. align 4
  1101. proc ctrl_io_w16
  1102.            add edx, [ctrl.ctrl_io_base]
  1103.            out dx, ax
  1104.            ret
  1105. endp
  1106.  
  1107. align 4
  1108. proc ctrl_io_w32
  1109.            add edx, [ctrl.ctrl_io_base]
  1110.            out dx, eax
  1111.            ret
  1112. endp
  1113.  
  1114. align 4
  1115. dword2str:
  1116.       mov  esi, hex_buff
  1117.       mov ecx, -8
  1118. @@:
  1119.       rol eax, 4
  1120.       mov ebx, eax
  1121.       and ebx, 0x0F
  1122.       mov bl, [ebx+hexletters]
  1123.       mov [8+esi+ecx], bl
  1124.       inc ecx
  1125.       jnz @B
  1126.       ret
  1127.  
  1128.  
  1129. include "codec.inc"
  1130.  
  1131. align 4
  1132. devices dd (CTRL_SIS  shl 16)+VID_SIS,msg_AC, set_SIS
  1133.         dd 0
  1134.  
  1135. version      dd 0x00040004
  1136.  
  1137. msg_AC       db '7012 AC97 controller',13,10, 0
  1138. msg_SIS      db 'Silicon Integrated Systems',13,10, 0
  1139.  
  1140. sz_sound_srv        db 'SOUND',0
  1141.  
  1142. msgInit      db 'detect hardware...',13,10,0
  1143. msgFail      db 'device not found',13,10,0
  1144. msgPlay      db 'start play', 13,10,0
  1145. msgStop      db 'stop play',  13,10,0
  1146. msgNotify    db 'call notify',13,10,0
  1147. msgIRQ       db 'AC97 IRQ', 13,10,0
  1148. msgInitCtrl  db 'init controller',13,10,0
  1149. msgInitCodec db 'init codec',13,10,0
  1150. msgPrimBuff  db 'create primary buffer',13,10,0
  1151. msgReg       db 'set service handler',13,10,0
  1152. msgOk        db 'service installed',13,10,0
  1153. msgCold      db 'cold resret',13,10,0
  1154. msgWarm      db 'warm reset',13,10,0
  1155. msgWRFail    db 'warm reset failed',13,10,0
  1156. msgCRFail    db 'cold reset failed',13,10,0
  1157. msgCFail     db 'codec not ready',13,10,0
  1158. msgStatus    db 'global status   ',0
  1159. msgControl   db 'global control  ',0
  1160.  
  1161. hexletters   db '0123456789ABCDEF'
  1162. hex_buff     db 8 dup(0),13,10,0
  1163.  
  1164.  
  1165. section '.data' data readable writable align 16
  1166.  
  1167. pcmout_bdl       rq 32
  1168. buff_list        rd 32
  1169.  
  1170. codec CODEC
  1171. ctrl AC_CNTRL
  1172.  
  1173. lpc_bus  rd 1
  1174. civ_val  rd 1
  1175.