Subversion Repositories Kolibri OS

Rev

Rev 381 | Blame | Last modification | View Log | Download | RSS feed

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