home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / midi / pc_midi / pc-midi / mpuio.asm < prev    next >
Encoding:
Assembly Source File  |  1988-11-24  |  12.6 KB  |  521 lines

  1.     TITLE   MPU401 Driver Interface Module
  2.     NAME    MPUIO
  3.     PAGE    46,132
  4.     ;****************************************************************
  5.     ;*                  MPUIO                *
  6.     ;*                                *
  7.     ;* This file contains a 'C' function for the MicroSoft 'C' com-    *
  8.     ;* piler v.4.0. It accesses the MPU401$$ driver using software    *
  9.     ;* interrupts.                            *
  10.     ;*                                *
  11.     ;* The two files FARNEAR.INC and SMALHUGE.INC each contain one    *
  12.     ;* equate. These will define the memory model to use for this    *
  13.     ;* module.                            *
  14.     ;****************************************************************
  15.     ;* Author: Bjorn Larsson                    *
  16.     ;* Revised:                            *
  17.     ;* 1.0:    Started:                    870111    *
  18.     ;*    Functional:                    870111    *
  19.     ;* 1.1:    Changed to do character I/O with the driver (this    *
  20.     ;*    was previously in 'C', but was moved to asm for        *
  21.     ;*    various reasons):                870115    *
  22.     ;* 1.2:    Use 68H as default soft interrupt, supports        *
  23.     ;*    exclusive buffer access:            870117    *
  24.     ;****************************************************************
  25.     ;
  26.     INCLUDE    FARNEAR.INC        ;DEFINE FAR OR NEAR CALL SEQUENCE
  27.     INCLUDE    SMALHUGE.INC        ;DEFINE FAR OR NEAR DATA ACCESS
  28.     ;
  29. _TEXT    SEGMENT  BYTE PUBLIC 'CODE'
  30.     ASSUME  CS: _TEXT, DS: _TEXT
  31.     ;
  32.     ;* MSDOS related equates
  33.     ;
  34. SYSTEM    EQU    21H            ;SYSTEM INT NUMBER
  35. OPNFIL    EQU    3DH            ;FILE OPEN FUNCTION
  36. CLOFIL    EQU    3EH            ;FILE CLOSE FUNCTION
  37. RDFIL    EQU    3FH            ;FILE READ FUNCTION
  38. OPNMOD    EQU    0            ;NO SPECIAL OPEN MODE
  39.     ;
  40.     ;* Function codes for soft ints to MPU401$$ driver
  41.     ;
  42. SINIT    EQU    0            ;DRIVER INIT
  43. SFINI    EQU    1            ;DRIVER FINIT
  44. SSTAT    EQU    2            ;GET DRIVER STATUS
  45. SRERR    EQU    3            ;CLEAR DRIVER ERROR FLAGS
  46. SSTIM    EQU    4            ;SET TIMER
  47. SRTIM    EQU    5            ;READ TIMER
  48. SFBUF    EQU    6            ;FLUSH BUFFER
  49. SRBUF    EQU    7            ;READ BUFFER
  50. STBUF    EQU    8            ;TEST BUFFER
  51. SDXBF    EQU    9            ;DEFINE EXCLUSIVE BUFFER
  52. SFXBF    EQU    10            ;FLUSH EXCLUSIVE BUFFER
  53. SRXBF    EQU    11            ;READ EXCLUSIVE BUFFER
  54. STXBF    EQU    12            ;TEST EXCLUSIVE BUFFER
  55. SOCMD    EQU    13            ;OUTPUT COMMAND BYTE
  56. SODAT    EQU    14            ;OUTPUT DATA BYTE
  57.     ;
  58.     ;* Return error codes from mpuinit()
  59.     ;
  60. BADOPN    EQU    1            ;DRIVER OPEN ERROR
  61. BADRD    EQU    2            ;DRIVER READ ERROR
  62. BADCLO    EQU    3            ;DRIVER CLOSE ERROR
  63. BADNUM    EQU    4            ;BAD SOFTWARE INT NUMBER
  64.     ;
  65.     ;* Miscellaneous
  66.     ;
  67. INTOPC    EQU    0CDH            ;SOFTWARE INTERRUPT OPCODE
  68. DFSFTI    EQU    68H            ;DEFAULT SOFT INTERRUPT
  69.     ;
  70.     ;****************************************************************
  71.     ;* Function entry and exit macros, and parameter fetch macro.    *
  72.     ;* Used by all functions.                    *
  73.     ;****************************************************************
  74.     ;
  75. c_entry    MACRO    f_name
  76.     ;
  77.     if far_call
  78. &f_name    proc far
  79.     else
  80. &f_name    proc near
  81.     endif
  82.     push    bp
  83.     mov    bp,sp
  84.     ;
  85.     ENDM
  86.     ;
  87. c_exit    MACRO    f_name
  88.     ;
  89.     pop    bp
  90.     ret
  91. &f_name    endp
  92.     ;
  93.     ENDM
  94.     ;
  95. g_parm    MACRO    reg,p_num
  96.     if    far_call
  97.     mov    ®,[bp+&p_num*2+4]
  98.     else
  99.     mov    ®,[bp+&p_num*2+2]
  100.     endif
  101.     ;
  102.     ENDM
  103.     ;
  104. REVCOD:    DB    'MPUIO v.1.2 - 870116 / B.Larsson '
  105. DRNAME:    DB    'MPU401$$',0        ;FILE NAME STRING
  106. RDBUF:    DB    0,0            ;FILE READ BUFFER
  107. FHANDL:    DW    0            ;FILE HANDLE DOING I/O
  108.     ;
  109.     ;****************************************************************
  110.     ;* mpuinit                            *
  111.     ;*                                *
  112.     ;* int mpuinit()                        *
  113.     ;*                                *
  114.     ;* Checks that the MPU401$$ driver is in the system, and works    *
  115.     ;* correctly. If so, gets the current software interrupt number    *
  116.     ;* and gives it to the other routines in this module.        *
  117.     ;* Returns non-zero value if an error occurs.            *
  118.     ;****************************************************************
  119.     ;
  120.     PUBLIC    _mpuinit
  121.     ;
  122.     c_entry _mpuinit
  123.     ;
  124.     PUSH    DS            ;SAVE REGS
  125.     PUSH    CX
  126.     PUSH    BX
  127.     PUSH    CS            ;SET DS = CS HERE
  128.     POP    DS
  129.     ;
  130.     ;* First try to open the MPU401$$ driver
  131.     ;
  132.     MOV    AH,OPNFIL        ;OPEN FILE REQUEST
  133.     MOV    AL,OPNMOD        ;OPEN ACCESS MODES
  134.     MOV    DX,OFFSET DRNAME    ;NAME POINTER
  135.     INT    SYSTEM
  136.     JNC    MINIT1            ;JUMP IF OK
  137.     ;
  138.     MOV    AX,BADOPN        ;DRIVER OPEN ERROR
  139.     JMP    MINITX
  140.     ;
  141. MINIT1:    MOV    WORD PTR FHANDL,AX    ;SAVE FILE HANDLE
  142.     ;
  143.     ;* Now read a pair of bytes from it
  144.     ;
  145.     MOV    BX,AX            ;FILE HANDLE
  146.     MOV    AH,RDFIL
  147.     MOV    CX,2            ;2 BYTES
  148.     MOV    DX,OFFSET RDBUF
  149.     INT    SYSTEM
  150.     JC    MINIT2            ;JUMP IF ERROR
  151.     ;
  152.     CMP    AX,2            ;CHECK 2 BYTES READ?
  153.     JZ    MINIT3            ;JUMP IF OK
  154.     ;
  155. MINIT2:    MOV    AX,BADRD        ;READ DEVICE ERROR
  156.     JMP    MINITX
  157.     ;
  158.     ;* Now close the driver
  159.     ;
  160. MINIT3:    MOV    AH,CLOFIL        ;CLOSE FILE
  161.     MOV    BX,WORD PTR FHANDL    ;FILE HANDLE
  162.     INT    SYSTEM
  163.     JNC    MINIT4            ;JUMP IF OK
  164.     ;
  165.     MOV    AX,BADCLO        ;DEVICE CLOSE ERROR
  166.     JMP    MINITX
  167.     ;
  168.     ;* Check the bytes are equal
  169.     ;
  170. MINIT4:    MOV    AX,WORD PTR RDBUF
  171.     CMP    AH,AL
  172.     JZ    MINIT5            ;JUMP IF OK
  173.     ;
  174.     MOV    AX,BADNUM        ;SOFT INT NUMBER ERROR
  175.     JMP    MINITX
  176.     ;
  177. MINIT5:    MOV    BYTE PTR SFTI0,AL    ;INSTALL IN OTHER FUNCTIONS
  178.     MOV    BYTE PTR SFTI1,AL
  179.     MOV    BYTE PTR SFTI2,AL
  180.     MOV    BYTE PTR SFTI3,AL
  181.     MOV    BYTE PTR SFTI4,AL
  182.     MOV    BYTE PTR SFTI5,AL
  183.     MOV    BYTE PTR SFTI6,AL
  184.     MOV    BYTE PTR SFTI7,AL
  185.     MOV    BYTE PTR SFTI8,AL
  186.     MOV    BYTE PTR SFTI9,AL
  187.     MOV    BYTE PTR SFTI10,AL
  188.     MOV    BYTE PTR SFTI11,AL
  189.     MOV    BYTE PTR SFTI13,AL
  190.     MOV    BYTE PTR SFTI14,AL
  191.     XOR    AX,AX            ;CLEAR LOWER HALF OF RESULT
  192.     ;
  193. MINITX:    POP    BX            ;RECOVER REGS
  194.     POP    CX
  195.     XOR    DX,DX            ;CLEAR UPPER 16 RESULT BITS
  196.     POP    DS
  197.     ;
  198.     c_exit    _mpuinit
  199.     ;
  200.     ;****************************************************************
  201.     ;* mpuon                            *
  202.     ;*                                *
  203.     ;* int mpuon(bufsiz,bufadr)                    *
  204.     ;*   int   bufsiz;                        *
  205.     ;*   char* bufadr;                        *
  206.     ;*                                *
  207.     ;* Activate the MPU401$$ driver. Defines a data buffer with    *
  208.     ;* size <bufsiz>, located at <bufadr>. <Bufsiz> must be 0 or    *
  209.     ;* 512 <= <bufsiz> <= 32768. If <bufsiz> is 0, the driver's    *
  210.     ;* internal buffer will be used. Returns non-zero if error.    *
  211.     ;****************************************************************
  212.     ;
  213.     PUBLIC    _mpuon
  214.     ;
  215.     c_entry _mpuon
  216.     ;
  217.     g_parm    BX,1            ;GET BUFFER SIZE
  218.     g_parm    CX,2            ;GET BUFFER OFFSET
  219.     if    huge_data
  220.     g_parm    DX,3            ;GET SEGMENT
  221.     else
  222.     MOV    DX,DS            ;USE DATA SEGMENT
  223.     endif
  224.     MOV    AX,SINIT        ;INIT FUNCTION CODE
  225.     DB    INTOPC
  226. SFTI0:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  227.     MOV    AX,DX            ;ERROR CODE TO AX
  228.     ;
  229.     c_exit    _mpuon
  230.     ;
  231.     ;****************************************************************
  232.     ;* mpuoff                            *
  233.     ;*                                *
  234.     ;* int mpuoff()                            *
  235.     ;*                                *
  236.     ;* Deactivates the MPU401$$ driver. Errors code returned in AX.    *
  237.     ;****************************************************************
  238.     ;
  239.     PUBLIC    _mpuoff
  240.     ;
  241.     c_entry _mpuoff
  242.     ;
  243.     MOV    AX,SFINI        ;FINIT FUNCTION CODE
  244.     DB    INTOPC
  245. SFTI1:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  246.     MOV    AX,DX            ;ERROR CODE TO AX
  247.     ;
  248.     c_exit    _mpuoff
  249.     ;
  250.     ;****************************************************************
  251.     ;* mpustat                            *
  252.     ;*                                *
  253.     ;* int mpustat()                        *
  254.     ;*                                *
  255.     ;* Returns current MPU401$$ driver status. No errors returned.    *
  256.     ;****************************************************************
  257.     ;
  258.     PUBLIC    _mpustat
  259.     ;
  260.     c_entry _mpustat
  261.     ;
  262.     MOV    AX,SSTAT        ;STATUS FUNCTION CODE
  263.     DB    INTOPC
  264. SFTI2:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  265.     ;
  266.     c_exit    _mpustat
  267.     ;
  268.     ;****************************************************************
  269.     ;* mpurerr                            *
  270.     ;*                                *
  271.     ;* void mpurerr()                        *
  272.     ;*                                *
  273.     ;* Resets error status of the MPU401$$ driver. No errors re-    *
  274.     ;* turned.                            *
  275.     ;****************************************************************
  276.     ;
  277.     PUBLIC    _mpurerr
  278.     ;
  279.     c_entry _mpurerr
  280.     ;
  281.     MOV    AX,SRERR        ;ERROR RESET FUNCTION CODE
  282.     DB    INTOPC
  283. SFTI3:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  284.     ;
  285.     c_exit    _mpurerr
  286.     ;
  287.     ;****************************************************************
  288.     ;* mpustim                            *
  289.     ;*                                *
  290.     ;* void mpustim(time)                        *
  291.     ;*   long time;                            *
  292.     ;*                                *
  293.     ;* Sets the MPU401'sinternal timer to <time>. No error re-    *
  294.     ;* turned.                            *
  295.     ;****************************************************************
  296.     ;
  297.     PUBLIC    _mpustim
  298.     ;
  299.     c_entry _mpustim
  300.     ;
  301.     g_parm    CX,1            ;GET LOW WORD
  302.     g_parm    DX,2            ;GET HIGH WORD
  303.     MOV    AX,SSTIM        ;SET TIMER FUNCTION CODE
  304.     DB    INTOPC
  305. SFTI4:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  306.     ;
  307.     c_exit    _mpustim
  308.     ;
  309.     ;****************************************************************
  310.     ;* mpurtim                            *
  311.     ;*                                *
  312.     ;* long mpurtim()                        *
  313.     ;*                                *
  314.     ;* Return the current value in the MPU401 internal timer.    *
  315.     ;* No error returned.                        *
  316.     ;****************************************************************
  317.     ;
  318.     PUBLIC    _mpurtim
  319.     ;
  320.     c_entry _mpurtim
  321.     ;
  322.     MOV    AX,SRTIM        ;READ TIMER FUNCTION CODE
  323.     DB    INTOPC
  324. SFTI5:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  325.     ;
  326.     c_exit    _mpurtim
  327.     ;
  328.     ;****************************************************************
  329.     ;* mpufbuf                            *
  330.     ;*                                *
  331.     ;* void mpufbuf()                        *
  332.     ;*                                *
  333.     ;* Flush the driver buffer. No error returned.            *
  334.     ;****************************************************************
  335.     ;
  336.     PUBLIC    _mpufbuf
  337.     ;
  338.     c_entry _mpufbuf
  339.     ;
  340.     MOV    AX,SFBUF        ;FLUSH FUNCTION CODE
  341.     DB    INTOPC
  342. SFTI6:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  343.     ;
  344.     c_exit    _mpufbuf
  345.     ;
  346.     ;****************************************************************
  347.     ;* mpurbuf                            *
  348.     ;*                                *
  349.     ;* long mpurbuf()                        *
  350.     ;*                                *
  351.     ;* Return a byte from the input buffer. If nothing there, it    *
  352.     ;* will be waited for. Return data in AX. Error code in DX.    *
  353.     ;****************************************************************
  354.     ;
  355.     PUBLIC    _mpurbuf
  356.     ;
  357.     c_entry _mpurbuf
  358.     ;
  359.     MOV    AX,SRBUF        ;READ BUFFER FUNCTION CODE
  360.     DB    INTOPC
  361. SFTI7:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  362.     ;
  363.     c_exit    _mpurbuf
  364.     ;
  365.     ;****************************************************************
  366.     ;* mputbuf                            *
  367.     ;*                                *
  368.     ;* inf mputbuf()                        *
  369.     ;*                                *
  370.     ;* Return a flag indicatiing if data is available in the input    *
  371.     ;* buffer, in AX. No error returned.                *
  372.     ;****************************************************************
  373.     ;
  374.     PUBLIC    _mputbuf
  375.     ;
  376.     c_entry _mputbuf
  377.     ;
  378.     MOV    AX,STBUF        ;TEST BUFFER FUNCTION CODE
  379.     DB    INTOPC
  380. SFTI8:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  381.     ;
  382.     c_exit    _mputbuf
  383.     ;
  384.     ;****************************************************************
  385.     ;* mpudxbf                            *
  386.     ;*                                *
  387.     ;* int mpudxbf(bufsiz,bufadr)                    *
  388.     ;*   int   bufsiz;                        *
  389.     ;*   char* bufadr;                        *
  390.     ;*                                *
  391.     ;* Define an exclusive buffer with size <bufsiz>, located at    *
  392.     ;* <bufadr>. If <bufsiz> is 0, any existent buffer is removed.    *
  393.     ;* <Bufsiz> must be 0 or 512 <= <bufsiz> <= 32768.        *
  394.     ;****************************************************************
  395.     ;
  396.     PUBLIC    _mpudxbf
  397.     ;
  398.     c_entry    _mpudxbf
  399.     g_parm    BX,1            ;GET BUFFER SIZE
  400.     g_parm    CX,2            ;GET BUFFER OFFSET
  401.     if    huge_data
  402.     g_parm    DX,3            ;GET SEGMENT
  403.     else
  404.     MOV    DX,DS            ;USE DATA SEGMENT
  405.     endif
  406.     MOV    AX,SDXBF        ;DEFINE EXCLUSIVE FUNCTION CODE
  407.     DB    INTOPC
  408. SFTI9:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  409.     MOV    AX,DX            ;ERROR CODE TO AX
  410.     ;
  411.     c_exit    _mpudxbf
  412.     ;
  413.     ;****************************************************************
  414.     ;* mpufxbf                            *
  415.     ;*                                *
  416.     ;* void mpufxbf()                        *
  417.     ;*                                *
  418.     ;* Flush the exclusive buffer. No error returned.        *
  419.     ;****************************************************************
  420.     ;
  421.     PUBLIC    _mpufxbf
  422.     ;
  423.     c_entry _mpufxbf
  424.     ;
  425.     MOV    AX,SFXBF        ;FLUSH EXCLUSIVE FUNCTION CODE
  426.     DB    INTOPC
  427. SFTI10:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  428.     ;
  429.     c_exit    _mpufxbf
  430.     ;
  431.     ;****************************************************************
  432.     ;* mpurxbf                            *
  433.     ;*                                *
  434.     ;* long mpurxbf()                        *
  435.     ;*                                *
  436.     ;* Return a byte from the exclusive buffer. If nothing there,    *
  437.     ;* it will be waited for. Return data in AX. Error code in DX.    *
  438.     ;****************************************************************
  439.     ;
  440.     PUBLIC    _mpurxbf
  441.     ;
  442.     c_entry _mpurxbf
  443.     ;
  444.     MOV    AX,SRXBF        ;READ ECLUSIVE FUNCTION CODE
  445.     DB    INTOPC
  446. SFTI11:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  447.     ;
  448.     c_exit    _mpurxbf
  449.     ;
  450.     ;****************************************************************
  451.     ;* mputxbf                            *
  452.     ;*                                *
  453.     ;* inf mputxbf()                        *
  454.     ;*                                *
  455.     ;* Return a flag indicatiing if data is available in the exclu-    *
  456.     ;* sive message buffer in AX. If the driver is inactive, AX    *
  457.     ;* returns 1, and DX has an error code.                *
  458.     ;****************************************************************
  459.     ;
  460.     PUBLIC    _mputxbf
  461.     ;
  462.     c_entry _mputxbf
  463.     ;
  464.     MOV    AX,STXBF        ;TEST EXCLUSIVE FUNCTION CODE
  465.     DB    INTOPC
  466. SFTI12:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  467.     ;
  468.     c_exit    _mputxbf
  469.     ;
  470.     ;****************************************************************
  471.     ;* mpuocmd                            *
  472.     ;*                                *
  473.     ;* int mpuocmd(cmd)                        *
  474.     ;*   char cmd;                            *
  475.     ;*                                *
  476.     ;* Send <cmd> as a command to MPU401. Return competion status    *
  477.     ;* in AX.                            *
  478.     ;****************************************************************
  479.     ;
  480.     PUBLIC    _mpuocmd
  481.     ;
  482.     c_entry _mpuocmd
  483.     ;
  484.     g_parm    DL,1            ;GET CMD BYTE
  485.     MOV    AX,SOCMD        ;COMMAND OUTPUT FUNCTION CODE
  486.     DB    INTOPC
  487. SFTI13:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  488.     MOV    AX,DX            ;COMPL CODE IN AX
  489.     ;
  490.     c_exit    _mpuocmd
  491.     ;
  492.     ;****************************************************************
  493.     ;* mpuodat                            *
  494.     ;*                                *
  495.     ;* int mpuodat(data)                        *
  496.     ;*   char data;                            *
  497.     ;*                                *
  498.     ;* Send <data> as a data byte to MPU401. Return competion    *
  499.     ;* status in AX.                        *
  500.     ;****************************************************************
  501.     ;
  502.     PUBLIC    _mpuodat
  503.     ;
  504.     c_entry _mpuodat
  505.     ;
  506.     g_parm    DL,1            ;GET DATA BYTE
  507.     MOV    AX,SODAT        ;DATA OUTPUT FUNCTION CODE
  508.     DB    INTOPC
  509. SFTI14:    DB    DFSFTI            ;DEFAULT SOFT INTERRUPT
  510.     MOV    AX,DX            ;COMPL CODE IN AX
  511.     ;
  512.     c_exit    _mpuodat
  513.     ;
  514. _TEXT    ENDS
  515.     if1
  516.     %OUT    Pass 1 Completed
  517.     else
  518.     %OUT    Assembly Completed
  519.     endif
  520.     END
  521.