| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: mpu401.cpp,v 1.29 2009-05-27 09:15:41 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include "dosbox.h" |
| 23 | #include "inout.h" |
| 24 | #include "pic.h" |
| 25 | #include "setup.h" |
| 26 | #include "cpu.h" |
| 27 | #include "support.h" |
| 28 | |
| 29 | void MIDI_RawOutByte(Bit8u data); |
| 30 | bool MIDI_Available(void); |
| 31 | |
| 32 | static void MPU401_Event(Bitu); |
| 33 | static void MPU401_Reset(void); |
| 34 | static void MPU401_EOIHandler(void); |
| 35 | |
| 36 | #define MPU401_VERSION 0x15 |
| 37 | #define MPU401_REVISION 0x01 |
| 38 | #define MPU401_QUEUE 32 |
| 39 | #define MPU401_TIMECONSTANT (60000000/1000.0f) |
| 40 | |
| 41 | enum MpuMode { M_UART,M_INTELLIGENT }; |
| 42 | enum MpuDataType {T_OVERFLOW,T_MARK,T_MIDI_SYS,T_MIDI_NORM,T_COMMAND}; |
| 43 | |
| 44 | static void MPU401_WriteData(Bitu port,Bitu val,Bitu iolen); |
| 45 | |
| 46 | /* Messages sent to MPU-401 from host */ |
| 47 | #define MSG_EOX 0xf7 |
| 48 | #define MSG_OVERFLOW 0xf8 |
| 49 | #define MSG_MARK 0xfc |
| 50 | |
| 51 | /* Messages sent to host from MPU-401 */ |
| 52 | #define MSG_MPU_OVERFLOW 0xf8 |
| 53 | #define MSG_MPU_COMMAND_REQ 0xf9 |
| 54 | #define MSG_MPU_END 0xfc |
| 55 | #define MSG_MPU_CLOCK 0xfd |
| 56 | #define MSG_MPU_ACK 0xfe |
| 57 | |
| 58 | static struct { |
| 59 | bool intelligent; |
| 60 | MpuMode mode; |
| 61 | Bitu irq; |
| 62 | Bit8u queue[MPU401_QUEUE]; |
| 63 | Bitu queue_pos,queue_used; |
| 64 | struct track { |
| 65 | Bits counter; |
| 66 | Bit8u value[8],sys_val; |
| 67 | Bit8u vlength,length; |
| 68 | MpuDataType type; |
| 69 | } playbuf[8],condbuf; |
| 70 | struct { |
| 71 | bool conductor,cond_req,cond_set, block_ack; |
| 72 | bool playing,reset; |
| 73 | bool wsd,wsm,wsd_start; |
| 74 | bool run_irq,irq_pending; |
| 75 | bool send_now; |
| 76 | Bits data_onoff; |
| 77 | Bitu command_byte; |
| 78 | Bit8u tmask,cmask,amask; |
| 79 | Bit16u midi_mask; |
| 80 | Bit16u req_mask; |
| 81 | Bit8u channel,old_chan; |
| 82 | } state; |
| 83 | struct { |
| 84 | Bit8u timebase,old_timebase; |
| 85 | Bit8u tempo,old_tempo; |
| 86 | Bit8u tempo_rel,old_tempo_rel; |
| 87 | Bit8u tempo_grad; |
| 88 | Bit8u cth_rate,cth_counter; |
| 89 | bool clock_to_host,cth_active; |
| 90 | } clock; |
| 91 | } mpu; |
| 92 | |
| 93 | |
| 94 | static void QueueByte(Bit8u data) { |
| 95 | if (mpu.state.block_ack) {mpu.state.block_ack=false;return;} |
| 96 | if (mpu.queue_used==0 && mpu.intelligent) { |
| 97 | mpu.state.irq_pending=true; |
| 98 | PIC_ActivateIRQ(mpu.irq); |
| 99 | } |
| 100 | if (mpu.queue_used<MPU401_QUEUE) { |
| 101 | Bitu pos=mpu.queue_used+mpu.queue_pos; |
| 102 | if (mpu.queue_pos>=MPU401_QUEUE) mpu.queue_pos-=MPU401_QUEUE; |
| 103 | if (pos>=MPU401_QUEUE) pos-=MPU401_QUEUE; |
| 104 | mpu.queue_used++; |
| 105 | mpu.queue[pos]=data; |
| 106 | } else LOG(LOG_MISC,LOG_NORMAL)("MPU401:Data queue full"); |
| 107 | } |
| 108 | |
| 109 | static void ClrQueue(void) { |
| 110 | mpu.queue_used=0; |
| 111 | mpu.queue_pos=0; |
| 112 | } |
| 113 | |
| 114 | static Bitu MPU401_ReadStatus(Bitu port,Bitu iolen) { |
| 115 | Bit8u ret=0x3f; /* Bits 6 and 7 clear */ |
| 116 | if (!mpu.queue_used) ret|=0x80; |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static void MPU401_WriteCommand(Bitu port,Bitu val,Bitu iolen) { |
| 121 | mpu.state.reset=0; |
| 122 | if (val<=0x2f) { |
| 123 | switch (val&3) { /* MIDI stop, start, continue */ |
| 124 | case 1: {MIDI_RawOutByte(0xfc);break;} |
| 125 | case 2: {MIDI_RawOutByte(0xfa);break;} |
| 126 | case 3: {MIDI_RawOutByte(0xfb);break;} |
| 127 | } |
| 128 | if (val&0x20) LOG(LOG_MISC,LOG_ERROR)("MPU-401:Unhandled Recording Command %x",val); |
| 129 | switch (val&0xc) { |
| 130 | case 0x4: /* Stop */ |
| 131 | PIC_RemoveEvents(MPU401_Event); |
| 132 | mpu.state.playing=false; |
| 133 | for (Bitu i=0xb0;i<0xbf;i++) { /* All notes off */ |
| 134 | MIDI_RawOutByte(i); |
| 135 | MIDI_RawOutByte(0x7b); |
| 136 | MIDI_RawOutByte(0); |
| 137 | } |
| 138 | break; |
| 139 | case 0x8: /* Play */ |
| 140 | LOG(LOG_MISC,LOG_NORMAL)("MPU-401:Intelligent mode playback started"); |
| 141 | mpu.state.playing=true; |
| 142 | PIC_RemoveEvents(MPU401_Event); |
| 143 | PIC_AddEvent(MPU401_Event,MPU401_TIMECONSTANT/(mpu.clock.tempo*mpu.clock.timebase)); |
| 144 | ClrQueue(); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | else if (val>=0xa0 && val<=0xa7) { /* Request play counter */ |
| 149 | if (mpu.state.cmask&(1<<(val&7))) QueueByte(mpu.playbuf[val&7].counter); |
| 150 | } |
| 151 | else if (val>=0xd0 && val<=0xd7) { /* Send data */ |
| 152 | mpu.state.old_chan=mpu.state.channel; |
| 153 | mpu.state.channel=val&7; |
| 154 | mpu.state.wsd=true; |
| 155 | mpu.state.wsm=false; |
| 156 | mpu.state.wsd_start=true; |
| 157 | } |
| 158 | else |
| 159 | switch (val) { |
| 160 | case 0xdf: /* Send system message */ |
| 161 | mpu.state.wsd=false; |
| 162 | mpu.state.wsm=true; |
| 163 | mpu.state.wsd_start=true; |
| 164 | break; |
| 165 | case 0x8e: /* Conductor */ |
| 166 | mpu.state.cond_set=false; |
| 167 | break; |
| 168 | case 0x8f: |
| 169 | mpu.state.cond_set=true; |
| 170 | break; |
| 171 | case 0x94: /* Clock to host */ |
| 172 | mpu.clock.clock_to_host=false; |
| 173 | break; |
| 174 | case 0x95: |
| 175 | mpu.clock.clock_to_host=true; |
| 176 | break; |
| 177 | case 0xc2: /* Internal timebase */ |
| 178 | mpu.clock.timebase=48; |
| 179 | break; |
| 180 | case 0xc3: |
| 181 | mpu.clock.timebase=72; |
| 182 | break; |
| 183 | case 0xc4: |
| 184 | mpu.clock.timebase=96; |
| 185 | break; |
| 186 | case 0xc5: |
| 187 | mpu.clock.timebase=120; |
| 188 | break; |
| 189 | case 0xc6: |
| 190 | mpu.clock.timebase=144; |
| 191 | break; |
| 192 | case 0xc7: |
| 193 | mpu.clock.timebase=168; |
| 194 | break; |
| 195 | case 0xc8: |
| 196 | mpu.clock.timebase=192; |
| 197 | break; |
| 198 | /* Commands with data byte */ |
| 199 | case 0xe0: case 0xe1: case 0xe2: case 0xe4: case 0xe6: |
| 200 | case 0xe7: case 0xec: case 0xed: case 0xee: case 0xef: |
| 201 | mpu.state.command_byte=val; |
| 202 | break; |
| 203 | /* Commands 0xa# returning data */ |
| 204 | case 0xab: /* Request and clear recording counter */ |
| 205 | QueueByte(MSG_MPU_ACK); |
| 206 | QueueByte(0); |
| 207 | return; |
| 208 | case 0xac: /* Request version */ |
| 209 | QueueByte(MSG_MPU_ACK); |
| 210 | QueueByte(MPU401_VERSION); |
| 211 | return; |
| 212 | case 0xad: /* Request revision */ |
| 213 | QueueByte(MSG_MPU_ACK); |
| 214 | QueueByte(MPU401_REVISION); |
| 215 | return; |
| 216 | case 0xaf: /* Request tempo */ |
| 217 | QueueByte(MSG_MPU_ACK); |
| 218 | QueueByte(mpu.clock.tempo); |
| 219 | return; |
| 220 | case 0xb1: /* Reset relative tempo */ |
| 221 | mpu.clock.tempo_rel=40; |
| 222 | break; |
| 223 | case 0xb9: /* Clear play map */ |
| 224 | case 0xb8: /* Clear play counters */ |
| 225 | for (Bitu i=0xb0;i<0xbf;i++) { /* All notes off */ |
| 226 | MIDI_RawOutByte(i); |
| 227 | MIDI_RawOutByte(0x7b); |
| 228 | MIDI_RawOutByte(0); |
| 229 | } |
| 230 | for (Bitu i=0;i<8;i++) { |
| 231 | mpu.playbuf[i].counter=0; |
| 232 | mpu.playbuf[i].type=T_OVERFLOW; |
| 233 | } |
| 234 | mpu.condbuf.counter=0; |
| 235 | mpu.condbuf.type=T_OVERFLOW; |
| 236 | if (!(mpu.state.conductor=mpu.state.cond_set)) mpu.state.cond_req=0; |
| 237 | mpu.state.amask=mpu.state.tmask; |
| 238 | mpu.state.req_mask=0; |
| 239 | mpu.state.irq_pending=true; |
| 240 | break; |
| 241 | case 0xff: /* Reset MPU-401 */ |
| 242 | LOG(LOG_MISC,LOG_NORMAL)("MPU-401:Reset %X",val); |
| 243 | mpu.state.reset=1; |
| 244 | if (CPU_Cycles > 5) { //It came from the desert wants a fast irq |
| 245 | CPU_CycleLeft += CPU_Cycles; |
| 246 | CPU_Cycles = 5; |
| 247 | } |
| 248 | MPU401_Reset(); |
| 249 | break; |
| 250 | case 0x3f: /* UART mode */ |
| 251 | LOG(LOG_MISC,LOG_NORMAL)("MPU-401:Set UART mode %X",val); |
| 252 | mpu.mode=M_UART; |
| 253 | break; |
| 254 | default:; |
| 255 | //LOG(LOG_MISC,LOG_NORMAL)("MPU-401:Unhandled command %X",val); |
| 256 | } |
| 257 | QueueByte(MSG_MPU_ACK); |
| 258 | } |
| 259 | |
| 260 | static Bitu MPU401_ReadData(Bitu port,Bitu iolen) { |
| 261 | Bit8u ret=MSG_MPU_ACK; |
| 262 | if (mpu.queue_used) { |
| 263 | if (mpu.queue_pos>=MPU401_QUEUE) mpu.queue_pos-=MPU401_QUEUE; |
| 264 | ret=mpu.queue[mpu.queue_pos]; |
| 265 | mpu.queue_pos++;mpu.queue_used--; |
| 266 | } |
| 267 | if (!mpu.intelligent) return ret; |
| 268 | |
| 269 | if (mpu.queue_used == 0) PIC_DeActivateIRQ(mpu.irq); |
| 270 | |
| 271 | if (ret>=0xf0 && ret<=0xf7) { /* MIDI data request */ |
| 272 | mpu.state.channel=ret&7; |
| 273 | mpu.state.data_onoff=0; |
| 274 | mpu.state.cond_req=false; |
| 275 | } |
| 276 | if (ret==MSG_MPU_COMMAND_REQ) { |
| 277 | mpu.state.data_onoff=0; |
| 278 | mpu.state.cond_req=true; |
| 279 | if (mpu.condbuf.type!=T_OVERFLOW) { |
| 280 | mpu.state.block_ack=true; |
| 281 | MPU401_WriteCommand(0x331,mpu.condbuf.value[0],1); |
| 282 | if (mpu.state.command_byte) MPU401_WriteData(0x330,mpu.condbuf.value[1],1); |
| 283 | } |
| 284 | mpu.condbuf.type=T_OVERFLOW; |
| 285 | } |
| 286 | if (ret==MSG_MPU_END || ret==MSG_MPU_CLOCK || ret==MSG_MPU_ACK) { |
| 287 | mpu.state.data_onoff=-1; |
| 288 | MPU401_EOIHandler(); |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static void MPU401_WriteData(Bitu port,Bitu val,Bitu iolen) { |
| 294 | if (mpu.mode==M_UART) {MIDI_RawOutByte(val);return;} |
| 295 | switch (mpu.state.command_byte) { /* 0xe# command data */ |
| 296 | case 0x00: |
| 297 | break; |
| 298 | case 0xe0: /* Set tempo */ |
| 299 | mpu.state.command_byte=0; |
| 300 | mpu.clock.tempo=val; |
| 301 | return; |
| 302 | case 0xe1: /* Set relative tempo */ |
| 303 | mpu.state.command_byte=0; |
| 304 | if (val!=0x40) //default value |
| 305 | LOG(LOG_MISC,LOG_ERROR)("MPU-401:Relative tempo change not implemented"); |
| 306 | return; |
| 307 | case 0xe7: /* Set internal clock to host interval */ |
| 308 | mpu.state.command_byte=0; |
| 309 | mpu.clock.cth_rate=val>>2; |
| 310 | return; |
| 311 | case 0xec: /* Set active track mask */ |
| 312 | mpu.state.command_byte=0; |
| 313 | mpu.state.tmask=val; |
| 314 | return; |
| 315 | case 0xed: /* Set play counter mask */ |
| 316 | mpu.state.command_byte=0; |
| 317 | mpu.state.cmask=val; |
| 318 | return; |
| 319 | case 0xee: /* Set 1-8 MIDI channel mask */ |
| 320 | mpu.state.command_byte=0; |
| 321 | mpu.state.midi_mask&=0xff00; |
| 322 | mpu.state.midi_mask|=val; |
| 323 | return; |
| 324 | case 0xef: /* Set 9-16 MIDI channel mask */ |
| 325 | mpu.state.command_byte=0; |
| 326 | mpu.state.midi_mask&=0x00ff; |
| 327 | mpu.state.midi_mask|=((Bit16u)val)<<8; |
| 328 | return; |
| 329 | //case 0xe2: /* Set graduation for relative tempo */ |
| 330 | //case 0xe4: /* Set metronome */ |
| 331 | //case 0xe6: /* Set metronome measure length */ |
| 332 | default: |
| 333 | mpu.state.command_byte=0; |
| 334 | return; |
| 335 | } |
| 336 | static Bitu length,cnt,posd; |
| 337 | if (mpu.state.wsd) { /* Directly send MIDI message */ |
| 338 | if (mpu.state.wsd_start) { |
| 339 | mpu.state.wsd_start=0; |
| 340 | cnt=0; |
| 341 | switch (val&0xf0) { |
| 342 | case 0xc0:case 0xd0: |
| 343 | mpu.playbuf[mpu.state.channel].value[0]=val; |
| 344 | length=2; |
| 345 | break; |
| 346 | case 0x80:case 0x90:case 0xa0:case 0xb0:case 0xe0: |
| 347 | mpu.playbuf[mpu.state.channel].value[0]=val; |
| 348 | length=3; |
| 349 | break; |
| 350 | case 0xf0: |
| 351 | LOG(LOG_MISC,LOG_ERROR)("MPU-401:Illegal WSD byte"); |
| 352 | mpu.state.wsd=0; |
| 353 | mpu.state.channel=mpu.state.old_chan; |
| 354 | return; |
| 355 | default: /* MIDI with running status */ |
| 356 | cnt++; |
| 357 | MIDI_RawOutByte(mpu.playbuf[mpu.state.channel].value[0]); |
| 358 | } |
| 359 | } |
| 360 | if (cnt<length) {MIDI_RawOutByte(val);cnt++;} |
| 361 | if (cnt==length) { |
| 362 | mpu.state.wsd=0; |
| 363 | mpu.state.channel=mpu.state.old_chan; |
| 364 | } |
| 365 | return; |
| 366 | } |
| 367 | if (mpu.state.wsm) { /* Directly send system message */ |
| 368 | if (val==MSG_EOX) {MIDI_RawOutByte(MSG_EOX);mpu.state.wsm=0;return;} |
| 369 | if (mpu.state.wsd_start) { |
| 370 | mpu.state.wsd_start=0; |
| 371 | cnt=0; |
| 372 | switch (val) { |
| 373 | case 0xf2:{ length=3; break;} |
| 374 | case 0xf3:{ length=2; break;} |
| 375 | case 0xf6:{ length=1; break;} |
| 376 | case 0xf0:{ length=0; break;} |
| 377 | default: |
| 378 | length=0; |
| 379 | } |
| 380 | } |
| 381 | if (!length || cnt<length) {MIDI_RawOutByte(val);cnt++;} |
| 382 | if (cnt==length) mpu.state.wsm=0; |
| 383 | return; |
| 384 | } |
| 385 | if (mpu.state.cond_req) { /* Command */ |
| 386 | switch (mpu.state.data_onoff) { |
| 387 | case -1: |
| 388 | return; |
| 389 | case 0: /* Timing byte */ |
| 390 | mpu.condbuf.vlength=0; |
| 391 | if (val<0xf0) mpu.state.data_onoff++; |
| 392 | else { |
| 393 | mpu.state.data_onoff=-1; |
| 394 | MPU401_EOIHandler(); |
| 395 | return; |
| 396 | } |
| 397 | if (val==0) mpu.state.send_now=true; |
| 398 | else mpu.state.send_now=false; |
| 399 | mpu.condbuf.counter=val; |
| 400 | break; |
| 401 | case 1: /* Command byte #1 */ |
| 402 | mpu.condbuf.type=T_COMMAND; |
| 403 | if (val==0xf8 || val==0xf9) mpu.condbuf.type=T_OVERFLOW; |
| 404 | mpu.condbuf.value[mpu.condbuf.vlength]=val; |
| 405 | mpu.condbuf.vlength++; |
| 406 | if ((val&0xf0)!=0xe0) MPU401_EOIHandler(); |
| 407 | else mpu.state.data_onoff++; |
| 408 | break; |
| 409 | case 2:/* Command byte #2 */ |
| 410 | mpu.condbuf.value[mpu.condbuf.vlength]=val; |
| 411 | mpu.condbuf.vlength++; |
| 412 | MPU401_EOIHandler(); |
| 413 | break; |
| 414 | } |
| 415 | return; |
| 416 | } |
| 417 | switch (mpu.state.data_onoff) { /* Data */ |
| 418 | case -1: |
| 419 | return; |
| 420 | case 0: /* Timing byte */ |
| 421 | if (val<0xf0) mpu.state.data_onoff=1; |
| 422 | else { |
| 423 | mpu.state.data_onoff=-1; |
| 424 | MPU401_EOIHandler(); |
| 425 | return; |
| 426 | } |
| 427 | if (val==0) mpu.state.send_now=true; |
| 428 | else mpu.state.send_now=false; |
| 429 | mpu.playbuf[mpu.state.channel].counter=val; |
| 430 | break; |
| 431 | case 1: /* MIDI */ |
| 432 | mpu.playbuf[mpu.state.channel].vlength++; |
| 433 | posd=mpu.playbuf[mpu.state.channel].vlength; |
| 434 | if (posd==1) { |
| 435 | switch (val&0xf0) { |
| 436 | case 0xf0: /* System message or mark */ |
| 437 | if (val>0xf7) { |
| 438 | mpu.playbuf[mpu.state.channel].type=T_MARK; |
| 439 | mpu.playbuf[mpu.state.channel].sys_val=val; |
| 440 | length=1; |
| 441 | } else { |
| 442 | LOG(LOG_MISC,LOG_ERROR)("MPU-401:Illegal message"); |
| 443 | mpu.playbuf[mpu.state.channel].type=T_MIDI_SYS; |
| 444 | mpu.playbuf[mpu.state.channel].sys_val=val; |
| 445 | length=1; |
| 446 | } |
| 447 | break; |
| 448 | case 0xc0: case 0xd0: /* MIDI Message */ |
| 449 | mpu.playbuf[mpu.state.channel].type=T_MIDI_NORM; |
| 450 | length=mpu.playbuf[mpu.state.channel].length=2; |
| 451 | break; |
| 452 | case 0x80: case 0x90: case 0xa0: case 0xb0: case 0xe0: |
| 453 | mpu.playbuf[mpu.state.channel].type=T_MIDI_NORM; |
| 454 | length=mpu.playbuf[mpu.state.channel].length=3; |
| 455 | break; |
| 456 | default: /* MIDI data with running status */ |
| 457 | posd++; |
| 458 | mpu.playbuf[mpu.state.channel].vlength++; |
| 459 | mpu.playbuf[mpu.state.channel].type=T_MIDI_NORM; |
| 460 | length=mpu.playbuf[mpu.state.channel].length; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | if (!(posd==1 && val>=0xf0)) mpu.playbuf[mpu.state.channel].value[posd-1]=val; |
| 465 | if (posd==length) MPU401_EOIHandler(); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static void MPU401_IntelligentOut(Bit8u chan) { |
| 470 | Bitu val; |
| 471 | switch (mpu.playbuf[chan].type) { |
| 472 | case T_OVERFLOW: |
| 473 | break; |
| 474 | case T_MARK: |
| 475 | val=mpu.playbuf[chan].sys_val; |
| 476 | if (val==0xfc) { |
| 477 | MIDI_RawOutByte(val); |
| 478 | mpu.state.amask&=~(1<<chan); |
| 479 | mpu.state.req_mask&=~(1<<chan); |
| 480 | } |
| 481 | break; |
| 482 | case T_MIDI_NORM: |
| 483 | for (Bitu i=0;i<mpu.playbuf[chan].vlength;i++) |
| 484 | MIDI_RawOutByte(mpu.playbuf[chan].value[i]); |
| 485 | break; |
| 486 | default: |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static void UpdateTrack(Bit8u chan) { |
| 492 | MPU401_IntelligentOut(chan); |
| 493 | if (mpu.state.amask&(1<<chan)) { |
| 494 | mpu.playbuf[chan].vlength=0; |
| 495 | mpu.playbuf[chan].type=T_OVERFLOW; |
| 496 | mpu.playbuf[chan].counter=0xf0; |
| 497 | mpu.state.req_mask|=(1<<chan); |
| 498 | } else { |
| 499 | if (mpu.state.amask==0 && !mpu.state.conductor) mpu.state.req_mask|=(1<<12); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void UpdateConductor(void) { |
| 504 | if (mpu.condbuf.value[0]==0xfc) { |
| 505 | mpu.condbuf.value[0]=0; |
| 506 | mpu.state.conductor=false; |
| 507 | mpu.state.req_mask&=~(1<<9); |
| 508 | if (mpu.state.amask==0) mpu.state.req_mask|=(1<<12); |
| 509 | return; |
| 510 | } |
| 511 | mpu.condbuf.vlength=0; |
| 512 | mpu.condbuf.counter=0xf0; |
| 513 | mpu.state.req_mask|=(1<<9); |
| 514 | } |
| 515 | |
| 516 | static void MPU401_Event(Bitu val) { |
| 517 | if (mpu.mode==M_UART) return; |
| 518 | if (mpu.state.irq_pending) goto next_event; |
| 519 | for (Bitu i=0;i<8;i++) { /* Decrease counters */ |
| 520 | if (mpu.state.amask&(1<<i)) { |
| 521 | mpu.playbuf[i].counter--; |
| 522 | if (mpu.playbuf[i].counter<=0) UpdateTrack(i); |
| 523 | } |
| 524 | } |
| 525 | if (mpu.state.conductor) { |
| 526 | mpu.condbuf.counter--; |
| 527 | if (mpu.condbuf.counter<=0) UpdateConductor(); |
| 528 | } |
| 529 | if (mpu.clock.clock_to_host) { |
| 530 | mpu.clock.cth_counter++; |
| 531 | if (mpu.clock.cth_counter >= mpu.clock.cth_rate) { |
| 532 | mpu.clock.cth_counter=0; |
| 533 | mpu.state.req_mask|=(1<<13); |
| 534 | } |
| 535 | } |
| 536 | if (!mpu.state.irq_pending && mpu.state.req_mask) MPU401_EOIHandler(); |
| 537 | next_event: |
| 538 | PIC_RemoveEvents(MPU401_Event); |
| 539 | Bitu new_time; |
| 540 | if ((new_time=mpu.clock.tempo*mpu.clock.timebase)==0) return; |
| 541 | PIC_AddEvent(MPU401_Event,MPU401_TIMECONSTANT/new_time); |
| 542 | } |
| 543 | |
| 544 | static void MPU401_EOIHandler(void) { |
| 545 | if (mpu.state.send_now) { |
| 546 | mpu.state.send_now=false; |
| 547 | if (mpu.state.cond_req) UpdateConductor(); |
| 548 | else UpdateTrack(mpu.state.channel); |
| 549 | } |
| 550 | mpu.state.irq_pending=false; |
| 551 | if (!mpu.state.playing || !mpu.state.req_mask) return; |
| 552 | Bitu i=0; |
| 553 | do { |
| 554 | if (mpu.state.req_mask&(1<<i)) { |
| 555 | QueueByte(0xf0+i); |
| 556 | mpu.state.req_mask&=~(1<<i); |
| 557 | break; |
| 558 | } |
| 559 | } while ((i++)<16); |
| 560 | } |
| 561 | |
| 562 | static void MPU401_Reset(void) { |
| 563 | PIC_DeActivateIRQ(mpu.irq); |
| 564 | mpu.mode=(mpu.intelligent ? M_INTELLIGENT : M_UART); |
| 565 | mpu.state.wsd=false; |
| 566 | mpu.state.wsm=false; |
| 567 | mpu.state.conductor=false; |
| 568 | mpu.state.cond_req=false; |
| 569 | mpu.state.cond_set=false; |
| 570 | mpu.state.playing=false; |
| 571 | mpu.state.run_irq=false; |
| 572 | mpu.state.irq_pending=false; |
| 573 | mpu.state.cmask=0xff; |
| 574 | mpu.state.amask=mpu.state.tmask=0; |
| 575 | mpu.state.midi_mask=0xffff; |
| 576 | mpu.state.data_onoff=0; |
| 577 | mpu.state.command_byte=0; |
| 578 | mpu.state.block_ack=false; |
| 579 | mpu.clock.tempo=mpu.clock.old_tempo=100; |
| 580 | mpu.clock.timebase=mpu.clock.old_timebase=120; |
| 581 | mpu.clock.tempo_rel=mpu.clock.old_tempo_rel=40; |
| 582 | mpu.clock.tempo_grad=0; |
| 583 | mpu.clock.clock_to_host=false; |
| 584 | mpu.clock.cth_rate=60; |
| 585 | mpu.clock.cth_counter=0; |
| 586 | ClrQueue(); |
| 587 | mpu.state.req_mask=0; |
| 588 | mpu.condbuf.counter=0; |
| 589 | mpu.condbuf.type=T_OVERFLOW; |
| 590 | for (Bitu i=0;i<8;i++) {mpu.playbuf[i].type=T_OVERFLOW;mpu.playbuf[i].counter=0;} |
| 591 | } |
| 592 | |
| 593 | class MPU401:public Module_base{ |
| 594 | private: |
| 595 | IO_ReadHandleObject ReadHandler[2]; |
| 596 | IO_WriteHandleObject WriteHandler[2]; |
| 597 | bool installed; /*as it can fail to install by 2 ways (config and no midi)*/ |
| 598 | public: |
| 599 | MPU401(Section* configuration):Module_base(configuration){ |
| 600 | installed = false; |
| 601 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 602 | const char* s_mpu = section->Get_string("mpu401"); |
| 603 | if(strcasecmp(s_mpu,"none") == 0) return; |
| 604 | if(strcasecmp(s_mpu,"off") == 0) return; |
| 605 | if(strcasecmp(s_mpu,"false") == 0) return; |
| 606 | if (!MIDI_Available()) return; |
| 607 | /*Enabled and there is a Midi */ |
| 608 | installed = true; |
| 609 | |
| 610 | WriteHandler[0].Install(0x330,&MPU401_WriteData,IO_MB); |
| 611 | WriteHandler[1].Install(0x331,&MPU401_WriteCommand,IO_MB); |
| 612 | ReadHandler[0].Install(0x330,&MPU401_ReadData,IO_MB); |
| 613 | ReadHandler[1].Install(0x331,&MPU401_ReadStatus,IO_MB); |
| 614 | |
| 615 | mpu.queue_used=0; |
| 616 | mpu.queue_pos=0; |
| 617 | mpu.mode=M_UART; |
| 618 | mpu.irq=9; /* Princess Maker 2 wants it on irq 9 */ |
| 619 | |
| 620 | mpu.intelligent = true; //Default is on |
| 621 | if(strcasecmp(s_mpu,"uart") == 0) mpu.intelligent = false; |
| 622 | if (!mpu.intelligent) return; |
| 623 | /*Set IRQ and unmask it(for timequest/princess maker 2) */ |
| 624 | PIC_SetIRQMask(mpu.irq,false); |
| 625 | MPU401_Reset(); |
| 626 | } |
| 627 | ~MPU401(){ |
| 628 | if(!installed) return; |
| 629 | Section_prop * section=static_cast<Section_prop *>(m_configuration); |
| 630 | if(strcasecmp(section->Get_string("mpu401"),"intelligent")) return; |
| 631 | PIC_SetIRQMask(mpu.irq,true); |
| 632 | } |
| 633 | }; |
| 634 | |
| 635 | static MPU401* test; |
| 636 | |
| 637 | void MPU401_Destroy(Section* sec){ |
| 638 | delete test; |
| 639 | } |
| 640 | |
| 641 | void MPU401_Init(Section* sec) { |
| 642 | test = new MPU401(sec); |
| 643 | sec->AddDestroyFunction(&MPU401_Destroy,true); |
| 644 | } |