Subversion Repositories Kolibri OS

Rev

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