-
Notifications
You must be signed in to change notification settings - Fork 13
/
MIDI.C
608 lines (545 loc) · 21.4 KB
/
MIDI.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* Copyright (C) 2002-2013 The DOSBox Team
* Copyright (C) 2013-2014 bjt, elianda
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* ------------------------------------------
* SoftMPU by bjt - Software MPU-401 Emulator
* ------------------------------------------
*
* Based on original midi.c from DOSBox
*
*/
/* SOFTMPU: Moved exported functions & types to header */
#include "export.h"
/* SOFTMPU: Don't need these includes */
/*#include <assert.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include "SDL.h"
#include "dosbox.h"
#include "cross.h"
#include "support.h"
#include "setup.h"
#include "mapper.h"
#include "pic.h"
#include "hardware.h"
#include "timer.h"*/
/* SOFTMPU: Additional defines, typedefs etc. for C */
typedef unsigned long Bit32u;
typedef int Bits;
#define SYSEX_SIZE 1024
#define RAWBUF 1024
/* SOFTMPU: Note tracking for RA-50 */
#define MAX_TRACKED_CHANNELS 16
#define MAX_TRACKED_NOTES 8
static char* MIDI_welcome_msg = "\xf0\x41\x10\x16\x12\x20\x00\x00 SoftMPU v1.9 \x24\xf7"; /* SOFTMPU */
static Bit8u MIDI_note_off[3] = { 0x80,0x00,0x00 }; /* SOFTMPU */
static Bit8u MIDI_evt_len[256] = {
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x00
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x10
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x20
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x30
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x40
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x50
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x60
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x70
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0x80
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0x90
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xa0
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xb0
2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, // 0xc0
2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, // 0xd0
3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xe0
0,2,3,2, 0,0,1,0, 1,0,1,1, 1,0,1,0 // 0xf0
};
/* SOFTMPU: Note tracking for RA-50 */
typedef struct {
Bit8u used;
Bit8u next;
Bit8u notes[MAX_TRACKED_NOTES];
} channel;
channel tracked_channels[MAX_TRACKED_CHANNELS];
static struct {
Bitu mpuport;
Bitu sbport;
Bitu serialport;
Bitu status;
Bitu cmd_len;
Bitu cmd_pos;
Bit8u cmd_buf[8];
Bit8u rt_buf[8];
struct {
Bit8u buf[SYSEX_SIZE];
Bitu used;
Bitu usedbufs;
Bitu delay;
Bit32u start;
} sysex;
bool fakeallnotesoff;
bool available;
/*MidiHandler * handler;*/ /* SOFTMPU */
} midi;
/* SOFTMPU: Sysex delay is decremented from PIC_Update */
Bitu MIDI_sysex_delay;
/* SOFTMPU: Also used by MPU401_ReadStatus */
OutputMode MIDI_output_mode;
/* SOFTMPU: Initialised in mpu401.c */
extern QEMMInfo qemm;
static void PlayMsg_SBMIDI(Bit8u* msg,Bitu len)
{
/* Output a MIDI message to the hardware using SB-MIDI */
/* Wait for WBS clear, then output a byte */
_asm
{
mov bx,msg
mov cx,len ; Assume len < 2^16
add cx,bx ; Get end ptr
mov dx,midi.sbport
add dx,0Ch ; Select DSP write port
NextByte: cmp bx,cx
je End
WaitWBS: cmp qemm.installed,1
jne WaitWBSUntrappedIN
push bx
mov ax,01A00h ; QPI_UntrappedIORead
call qemm.qpi_entry
mov al,bl
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitWBSUntrappedIN:
in al,dx
or al,al
js WaitWBS
mov al,038h ; Normal mode MIDI output
cmp qemm.installed,1
jne WaitWBSUntrappedOUT
push bx
mov bl,al ; bl = value
mov ax,01A01h ; QPI_UntrappedIOWrite
call qemm.qpi_entry
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitWBSUntrappedOUT:
out dx,al
WaitWBS2: cmp qemm.installed,1
jne WaitWBS2UntrappedIN
push bx
mov ax,01A00h ; QPI_UntrappedIORead
call qemm.qpi_entry
mov al,bl
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitWBS2UntrappedIN:
in al,dx
or al,al
js WaitWBS2
mov al,[bx]
cmp qemm.installed,1
jne WaitWBS2UntrappedOUT
push bx
mov bl,al ; bl = value
mov ax,01A01h ; QPI_UntrappedIOWrite
call qemm.qpi_entry
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitWBS2UntrappedOUT:
out dx,al
inc bx
jmp NextByte
; Nothing more to send
End: nop
}
};
static void PlayMsg_Serial(Bit8u* msg,Bitu len)
{
/* Output a MIDI message to a serial port */
/* Wait for transmit register clear, then output a byte */
_asm
{
mov bx,msg
mov cx,len ; Assume len < 2^16
add cx,bx ; Get end ptr
mov dx,midi.serialport
NextByte: add dx,05h ; Select line status register
cmp bx,cx
je End
WaitTransmit: cmp qemm.installed,1
jne WaitTransmitUntrappedIN
push bx
mov ax,01A00h ; QPI_UntrappedIORead
call qemm.qpi_entry
mov al,bl
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitTransmitUntrappedIN:
in al,dx
and al,040h ; Shift register empty?
jz WaitTransmit
sub dx,05h ; Select transmit data register
mov al,[bx]
cmp qemm.installed,1
jne WaitTransmitUntrappedOUT
push bx
mov bl,al ; bl = value
mov ax,01A01h ; QPI_UntrappedIOWrite
call qemm.qpi_entry
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitTransmitUntrappedOUT:
out dx,al
inc bx
jmp NextByte
; Nothing more to send
End: nop
}
};
static void PlayMsg(Bit8u* msg,Bitu len)
{
switch (MIDI_output_mode)
{
case M_MPU401:
/* Output a MIDI message to the hardware */
/* Wait for DRR clear, then output a byte */
_asm
{
mov bx,msg
mov cx,len ; Assume len < 2^16
add cx,bx ; Get end ptr
mov dx,midi.mpuport
NextByte: cmp bx,cx
je End
inc dx ; Set cmd port
WaitDRR: cmp qemm.installed,1
jne WaitDRRUntrappedIN
push bx
mov ax,01A00h ; QPI_UntrappedIORead
call qemm.qpi_entry
mov al,bl
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitDRRUntrappedIN:
in al,dx
test al,040h
jnz WaitDRR
dec dx ; Set data port
mov al,[bx]
cmp qemm.installed,1
jne WaitDRRUntrappedOUT
push bx
mov bl,al ; bl = value
mov ax,01A01h ; QPI_UntrappedIOWrite
call qemm.qpi_entry
pop bx
_emit 0A8h ; Emit test al,(next opcode byte)
; Effectively skips next instruction
WaitDRRUntrappedOUT:
out dx,al
inc bx
jmp NextByte
; Nothing more to send
End: nop
}
break;
case M_SBMIDI:
return PlayMsg_SBMIDI(msg,len);
case M_SERIAL:
return PlayMsg_Serial(msg,len);
default:
break;
}
};
/* SOFTMPU: Fake "All Notes Off" for Roland RA-50 */
static void FakeAllNotesOff(Bitu chan)
{
Bitu note;
channel* pChan;
MIDI_note_off[0] &= 0xf0;
MIDI_note_off[0] |= (Bit8u)chan;
pChan=&tracked_channels[chan];
for (note=0;note<pChan->used;note++)
{
MIDI_note_off[1]=pChan->notes[note];
PlayMsg(MIDI_note_off,3);
}
pChan->used=0;
pChan->next=0;
}
void MIDI_RawOutByte(Bit8u data) {
channel* pChan; /* SOFTMPU */
if (midi.sysex.start && MIDI_sysex_delay) {
_asm
{
; Bit 4 of port 061h toggles every 15.085us
; Use this to time the remaining sysex delay
mov ax,MIDI_sysex_delay
mov bx,17 ; Assume 4kHz RTC
mul bx ; Convert to ticks, result in ax
mov cx,ax
in al,061h
and al,010h ; Get initial value
mov bl,al
TestPort: in al,061h
and al,010h
cmp al,bl
je TestPort ; Loop until toggled
xor bl,010h ; Invert
loop TestPort
mov MIDI_sysex_delay,0 ; Set original delay to zero
}
/*Bit32u passed_ticks = GetTicks() - midi.sysex.start;
if (passed_ticks < midi.sysex.delay) SDL_Delay(midi.sysex.delay - passed_ticks);*/ /* SOFTMPU */
}
/* Test for a realtime MIDI message */
if (data>=0xf8) {
midi.rt_buf[0]=data;
PlayMsg(midi.rt_buf,1);
return;
}
/* Test for a active sysex tranfer */
if (midi.status==0xf0) {
if (!(data&0x80)) {
/* SOFTMPU: Large sysex support */
/*if (midi.sysex.used<(SYSEX_SIZE-1))*/ midi.sysex.buf[midi.sysex.used++] = data;
if (midi.sysex.used==SYSEX_SIZE)
{
PlayMsg(midi.sysex.buf, SYSEX_SIZE);
midi.sysex.used = 0;
midi.sysex.usedbufs++;
}
return;
} else {
midi.sysex.buf[midi.sysex.used++] = 0xf7;
if ((midi.sysex.start) && (midi.sysex.usedbufs == 0) && (midi.sysex.used >= 4) && (midi.sysex.used <= 9) && (midi.sysex.buf[1] == 0x41) && (midi.sysex.buf[3] == 0x16)) {
/*LOG(LOG_ALL,LOG_ERROR)("MIDI:Skipping invalid MT-32 SysEx midi message (too short to contain a checksum)");*/ /* SOFTMPU */
} else {
/*LOG(LOG_ALL,LOG_NORMAL)("Play sysex; address:%02X %02X %02X, length:%4d, delay:%3d", midi.sysex.buf[5], midi.sysex.buf[6], midi.sysex.buf[7], midi.sysex.used, midi.sysex.delay);*/
PlayMsg(midi.sysex.buf, midi.sysex.used); /* SOFTMPU */
if (midi.sysex.start) {
if (midi.sysex.usedbufs == 0 && midi.sysex.buf[5] == 0x7F) {
/*midi.sysex.delay = 290;*/ /* SOFTMPU */ // All Parameters reset
MIDI_sysex_delay = 290*(RTCFREQ/1000);
} else if (midi.sysex.usedbufs == 0 && midi.sysex.buf[5] == 0x10 && midi.sysex.buf[6] == 0x00 && midi.sysex.buf[7] == 0x04) {
/*midi.sysex.delay = 145;*/ /* SOFTMPU */ // Viking Child
MIDI_sysex_delay = 145*(RTCFREQ/1000);
} else if (midi.sysex.usedbufs == 0 && midi.sysex.buf[5] == 0x10 && midi.sysex.buf[6] == 0x00 && midi.sysex.buf[7] == 0x01) {
/*midi.sysex.delay = 30;*/ /* SOFTMPU */ // Dark Sun 1
MIDI_sysex_delay = 30*(RTCFREQ/1000);
} else MIDI_sysex_delay = ((((midi.sysex.usedbufs*SYSEX_SIZE)+midi.sysex.used)/2)+2)*(RTCFREQ/1000); /*(Bitu)(((float)(midi.sysex.used) * 1.25f) * 1000.0f / 3125.0f) + 2;
midi.sysex.start = GetTicks();*/ /* SOFTMPU */
}
}
/*LOG(LOG_ALL,LOG_NORMAL)("Sysex message size %d",midi.sysex.used);*/ /* SOFTMPU */
/*if (CaptureState & CAPTURE_MIDI) {
CAPTURE_AddMidi( true, midi.sysex.used-1, &midi.sysex.buf[1]);
}*/ /* SOFTMPU */
}
}
if (data&0x80) {
midi.status=data;
midi.cmd_pos=0;
midi.cmd_len=MIDI_evt_len[data];
if (midi.status==0xf0) {
midi.sysex.buf[0]=0xf0;
midi.sysex.used=1;
midi.sysex.usedbufs=0;
}
}
if (midi.cmd_len) {
midi.cmd_buf[midi.cmd_pos++]=data;
if (midi.cmd_pos >= midi.cmd_len) {
/*if (CaptureState & CAPTURE_MIDI) {
CAPTURE_AddMidi(false, midi.cmd_len, midi.cmd_buf);
}*/ /* SOFTMPU */
if (midi.fakeallnotesoff)
{
/* SOFTMPU: Test for "Note On" */
if ((midi.status&0xf0)==0x90)
{
if (midi.cmd_buf[2]>0)
{
pChan=&tracked_channels[midi.status&0x0f];
pChan->notes[pChan->next++]=midi.cmd_buf[1];
if (pChan->next==MAX_TRACKED_NOTES) pChan->next=0;
if (pChan->used<MAX_TRACKED_NOTES) pChan->used++;
}
PlayMsg(midi.cmd_buf,midi.cmd_len);
}
/* SOFTMPU: Test for "All Notes Off" */
else if (((midi.status&0xf0)==0xb0) &&
(midi.cmd_buf[1]>=0x7b) &&
(midi.cmd_buf[1]<=0x7f))
{
FakeAllNotesOff(midi.status&0x0f);
}
else
{
PlayMsg(midi.cmd_buf,midi.cmd_len);
}
}
else
{
PlayMsg(midi.cmd_buf,midi.cmd_len);
}
midi.cmd_pos=1; //Use Running status
}
}
}
bool MIDI_Available(void) {
return midi.available;
}
/* SOFTMPU: Initialisation */
void MIDI_Init(Bitu mpuport,Bitu sbport,Bitu serialport,OutputMode outputmode,bool delaysysex,bool fakeallnotesoff){
Bitu i; /* SOFTMPU */
midi.sysex.delay = 0;
midi.sysex.start = 0;
MIDI_sysex_delay = 0; /* SOFTMPU */
if (delaysysex==true)
{
midi.sysex.start = 1; /*GetTicks();*/ /* SOFTMPU */
/*LOG_MSG("MIDI:Using delayed SysEx processing");*/ /* SOFTMPU */
}
midi.mpuport=mpuport;
midi.sbport=sbport;
midi.serialport=serialport;
midi.status=0x00;
midi.cmd_pos=0;
midi.cmd_len=0;
midi.fakeallnotesoff=fakeallnotesoff;
midi.available=true;
MIDI_output_mode=outputmode;
/* SOFTMPU: Display welcome message on MT-32 */
for (i=0;i<30;i++)
{
MIDI_RawOutByte(MIDI_welcome_msg[i]);
}
/* SOFTMPU: Init note tracking */
for (i=0;i<MAX_TRACKED_CHANNELS;i++)
{
tracked_channels[i].used=0;
tracked_channels[i].next=0;
}
}
/* DOSBox initialisation code */
#if 0
class MIDI:public Module_base{
public:
MIDI(Section* configuration):Module_base(configuration){
Section_prop * section=static_cast<Section_prop *>(configuration);
const char * dev=section->Get_string("mididevice");
std::string fullconf=section->Get_string("midiconfig");
/* If device = "default" go for first handler that works */
MidiHandler * handler;
// MAPPER_AddHandler(MIDI_SaveRawEvent,MK_f8,MMOD1|MMOD2,"caprawmidi","Cap MIDI");
midi.sysex.delay = 0;
midi.sysex.start = 0;
if (fullconf.find("delaysysex") != std::string::npos) {
midi.sysex.start = GetTicks();
fullconf.erase(fullconf.find("delaysysex"));
LOG_MSG("MIDI:Using delayed SysEx processing");
}
std::remove(fullconf.begin(), fullconf.end(), ' ');
const char * conf = fullconf.c_str();
midi.status=0x00;
midi.cmd_pos=0;
midi.cmd_len=0;
if (!strcasecmp(dev,"default")) goto getdefault;
handler=handler_list;
while (handler) {
if (!strcasecmp(dev,handler->GetName())) {
if (!handler->Open(conf)) {
LOG_MSG("MIDI:Can't open device:%s with config:%s.",dev,conf);
goto getdefault;
}
midi.handler=handler;
midi.available=true;
LOG_MSG("MIDI:Opened device:%s",handler->GetName());
return;
}
handler=handler->next;
}
LOG_MSG("MIDI:Can't find device:%s, finding default handler.",dev);
getdefault:
handler=handler_list;
while (handler) {
if (handler->Open(conf)) {
midi.available=true;
midi.handler=handler;
LOG_MSG("MIDI:Opened device:%s",handler->GetName());
return;
}
handler=handler->next;
}
/* This shouldn't be possible */
}
~MIDI(){
if(midi.available) midi.handler->Close();
midi.available = false;
midi.handler = 0;
}
};
static MIDI* test;
void MIDI_Destroy(Section* /*sec*/){
delete test;
}
void MIDI_Init(Section * sec) {
test = new MIDI(sec);
sec->AddDestroyFunction(&MIDI_Destroy,true);
}
#endif
/* DOSBox MIDI handler code */
#if 0
class MidiHandler;
MidiHandler * handler_list=0;
class MidiHandler {
public:
MidiHandler() {
next=handler_list;
handler_list=this;
};
virtual bool Open(const char * /*conf*/) { return true; };
virtual void Close(void) {};
virtual void PlayMsg(Bit8u * /*msg*/) {};
virtual void PlaySysex(Bit8u * /*sysex*/,Bitu /*len*/) {};
virtual const char * GetName(void) { return "none"; };
virtual ~MidiHandler() { };
MidiHandler * next;
};
MidiHandler Midi_none;
/* Include different midi drivers, lowest ones get checked first for default */
#if defined(MACOSX)
#include "midi_coremidi.h"
#include "midi_coreaudio.h"
#elif defined (WIN32)
#include "midi_win32.h"
#else
#include "midi_oss.h"
#endif
#if defined (HAVE_ALSA)
#include "midi_alsa.h"
#endif
#endif /* if 0 */