Subversion Repositories Kolibri OS

Rev

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

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