Subversion Repositories Kolibri OS

Rev

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