MPU401.C 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /**********************************************************************
  16. module: MPU401.C
  17. author: James R. Dose
  18. date: January 1, 1994
  19. Low level routines to support sending of MIDI data to MPU401
  20. compatible MIDI interfaces.
  21. (c) Copyright 1994 James R. Dose. All Rights Reserved.
  22. **********************************************************************/
  23. #include <conio.h>
  24. #include <dos.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "dpmi.h"
  28. #include "user.h"
  29. #include "mpu401.h"
  30. #define MIDI_NOTE_OFF 0x80
  31. #define MIDI_NOTE_ON 0x90
  32. #define MIDI_POLY_AFTER_TCH 0xA0
  33. #define MIDI_CONTROL_CHANGE 0xB0
  34. #define MIDI_PROGRAM_CHANGE 0xC0
  35. #define MIDI_AFTER_TOUCH 0xD0
  36. #define MIDI_PITCH_BEND 0xE0
  37. #define MIDI_META_EVENT 0xFF
  38. #define MIDI_END_OF_TRACK 0x2F
  39. #define MIDI_TEMPO_CHANGE 0x51
  40. #define MIDI_MONO_MODE_ON 0x7E
  41. #define MIDI_ALL_NOTES_OFF 0x7B
  42. int MPU_BaseAddr = MPU_DefaultAddress;
  43. //unsigned MPU_Delay = 500;
  44. //unsigned MPU_Delay = 5000;
  45. unsigned MPU_Delay = 0x5000;
  46. /**********************************************************************
  47. Memory locked functions:
  48. **********************************************************************/
  49. #define MPU_LockStart MPU_SendMidi
  50. /*---------------------------------------------------------------------
  51. Function: MPU_SendMidi
  52. Sends a byte of MIDI data to the music device.
  53. ---------------------------------------------------------------------*/
  54. void MPU_SendMidi
  55. (
  56. int data
  57. )
  58. {
  59. int port = MPU_BaseAddr + 1;
  60. unsigned count;
  61. count = MPU_Delay;
  62. while( count > 0 )
  63. {
  64. // check if status port says we're ready for write
  65. if ( !( inp( port ) & MPU_ReadyToWrite ) )
  66. {
  67. break;
  68. }
  69. count--;
  70. }
  71. port--;
  72. // Send the midi data
  73. outp( port, data );
  74. }
  75. /*---------------------------------------------------------------------
  76. Function: MPU_NoteOff
  77. Sends a full MIDI note off event out to the music device.
  78. ---------------------------------------------------------------------*/
  79. void MPU_NoteOff
  80. (
  81. int channel,
  82. int key,
  83. int velocity
  84. )
  85. {
  86. MPU_SendMidi( MIDI_NOTE_OFF | channel );
  87. MPU_SendMidi( key );
  88. MPU_SendMidi( velocity );
  89. }
  90. /*---------------------------------------------------------------------
  91. Function: MPU_NoteOn
  92. Sends a full MIDI note on event out to the music device.
  93. ---------------------------------------------------------------------*/
  94. void MPU_NoteOn
  95. (
  96. int channel,
  97. int key,
  98. int velocity
  99. )
  100. {
  101. MPU_SendMidi( MIDI_NOTE_ON | channel );
  102. MPU_SendMidi( key );
  103. MPU_SendMidi( velocity );
  104. }
  105. /*---------------------------------------------------------------------
  106. Function: MPU_PolyAftertouch
  107. Sends a full MIDI polyphonic aftertouch event out to the music device.
  108. ---------------------------------------------------------------------*/
  109. void MPU_PolyAftertouch
  110. (
  111. int channel,
  112. int key,
  113. int pressure
  114. )
  115. {
  116. MPU_SendMidi( MIDI_POLY_AFTER_TCH | channel );
  117. MPU_SendMidi( key );
  118. MPU_SendMidi( pressure );
  119. }
  120. /*---------------------------------------------------------------------
  121. Function: MPU_ControlChange
  122. Sends a full MIDI control change event out to the music device.
  123. ---------------------------------------------------------------------*/
  124. void MPU_ControlChange
  125. (
  126. int channel,
  127. int number,
  128. int value
  129. )
  130. {
  131. MPU_SendMidi( MIDI_CONTROL_CHANGE | channel );
  132. MPU_SendMidi( number );
  133. MPU_SendMidi( value );
  134. }
  135. /*---------------------------------------------------------------------
  136. Function: MPU_ProgramChange
  137. Sends a full MIDI program change event out to the music device.
  138. ---------------------------------------------------------------------*/
  139. void MPU_ProgramChange
  140. (
  141. int channel,
  142. int program
  143. )
  144. {
  145. MPU_SendMidi( MIDI_PROGRAM_CHANGE | channel );
  146. MPU_SendMidi( program );
  147. }
  148. /*---------------------------------------------------------------------
  149. Function: MPU_ChannelAftertouch
  150. Sends a full MIDI channel aftertouch event out to the music device.
  151. ---------------------------------------------------------------------*/
  152. void MPU_ChannelAftertouch
  153. (
  154. int channel,
  155. int pressure
  156. )
  157. {
  158. MPU_SendMidi( MIDI_AFTER_TOUCH | channel );
  159. MPU_SendMidi( pressure );
  160. }
  161. /*---------------------------------------------------------------------
  162. Function: MPU_PitchBend
  163. Sends a full MIDI pitch bend event out to the music device.
  164. ---------------------------------------------------------------------*/
  165. void MPU_PitchBend
  166. (
  167. int channel,
  168. int lsb,
  169. int msb
  170. )
  171. {
  172. MPU_SendMidi( MIDI_PITCH_BEND | channel );
  173. MPU_SendMidi( lsb );
  174. MPU_SendMidi( msb );
  175. }
  176. /*---------------------------------------------------------------------
  177. Function: MPU_SendCommand
  178. Sends a command to the MPU401 card.
  179. ---------------------------------------------------------------------*/
  180. void MPU_SendCommand
  181. (
  182. int data
  183. )
  184. {
  185. int port = MPU_BaseAddr + 1;
  186. unsigned count;
  187. count = 0xffff;
  188. while( count > 0 )
  189. {
  190. // check if status port says we're ready for write
  191. if ( !( inp( port ) & MPU_ReadyToWrite ) )
  192. {
  193. break;
  194. }
  195. count--;
  196. }
  197. outp( port, data );
  198. }
  199. /*---------------------------------------------------------------------
  200. Function: MPU_Reset
  201. Resets the MPU401 card.
  202. ---------------------------------------------------------------------*/
  203. int MPU_Reset
  204. (
  205. void
  206. )
  207. {
  208. int port = MPU_BaseAddr + 1;
  209. unsigned count;
  210. // Output "Reset" command via Command port
  211. MPU_SendCommand( MPU_CmdReset );
  212. // Wait for status port to be ready for read
  213. count = 0xffff;
  214. while( count > 0 )
  215. {
  216. if ( !( inp( port ) & MPU_ReadyToRead ) )
  217. {
  218. port--;
  219. // Check for a successful reset
  220. if ( inp( port ) == MPU_CmdAcknowledge )
  221. {
  222. return( MPU_Ok );
  223. }
  224. port++;
  225. }
  226. count--;
  227. }
  228. // Failed to reset : MPU-401 not detected
  229. return( MPU_NotFound );
  230. }
  231. /*---------------------------------------------------------------------
  232. Function: MPU_EnterUART
  233. Sets the MPU401 card to operate in UART mode.
  234. ---------------------------------------------------------------------*/
  235. int MPU_EnterUART
  236. (
  237. void
  238. )
  239. {
  240. int port = MPU_BaseAddr + 1;
  241. unsigned count;
  242. // Output "Enter UART" command via Command port
  243. MPU_SendCommand( MPU_CmdEnterUART );
  244. // Wait for status port to be ready for read
  245. count = 0xffff;
  246. while( count > 0 )
  247. {
  248. if ( !( inp( port ) & MPU_ReadyToRead ) )
  249. {
  250. port--;
  251. // Check for a successful reset
  252. if ( inp( port ) == MPU_CmdAcknowledge )
  253. {
  254. return( MPU_Ok );
  255. }
  256. port++;
  257. }
  258. count--;
  259. }
  260. // Failed to reset : MPU-401 not detected
  261. return( MPU_UARTFailed );
  262. }
  263. /*---------------------------------------------------------------------
  264. Function: MPU_Init
  265. Detects and initializes the MPU401 card.
  266. ---------------------------------------------------------------------*/
  267. int MPU_Init
  268. (
  269. int addr
  270. )
  271. {
  272. int status;
  273. int count;
  274. char *ptr;
  275. ptr = USER_GetText( "MPUDELAY" );
  276. if ( ptr != NULL )
  277. {
  278. MPU_Delay = ( unsigned )atol( ptr );
  279. }
  280. MPU_BaseAddr = addr;
  281. count = 4;
  282. while( count > 0 )
  283. {
  284. status = MPU_Reset();
  285. if ( status == MPU_Ok )
  286. {
  287. return( MPU_EnterUART() );
  288. }
  289. count--;
  290. }
  291. return( status );
  292. }
  293. /*---------------------------------------------------------------------
  294. Function: MPU_LockEnd
  295. Used for determining the length of the functions to lock in memory.
  296. ---------------------------------------------------------------------*/
  297. static void MPU_LockEnd
  298. (
  299. void
  300. )
  301. {
  302. }
  303. /*---------------------------------------------------------------------
  304. Function: MPU_UnlockMemory
  305. Locks all neccessary data.
  306. ---------------------------------------------------------------------*/
  307. void MPU_UnlockMemory
  308. (
  309. void
  310. )
  311. {
  312. DPMI_UnlockMemoryRegion( MPU_LockStart, MPU_LockEnd );
  313. DPMI_Unlock( MPU_BaseAddr );
  314. DPMI_Unlock( MPU_Delay );
  315. }
  316. /*---------------------------------------------------------------------
  317. Function: MPU_LockMemory
  318. Locks all neccessary data.
  319. ---------------------------------------------------------------------*/
  320. int MPU_LockMemory
  321. (
  322. void
  323. )
  324. {
  325. int status;
  326. status = DPMI_LockMemoryRegion( MPU_LockStart, MPU_LockEnd );
  327. status |= DPMI_Lock( MPU_BaseAddr );
  328. status |= DPMI_Lock( MPU_Delay );
  329. if ( status != DPMI_Ok )
  330. {
  331. MPU_UnlockMemory();
  332. return( MPU_Error );
  333. }
  334. return( MPU_Ok );
  335. }