Subversion Repositories Kolibri OS

Rev

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