source: buchla-68k/ram/msm.c@ 6dc5ea7

Last change on this file since 6dc5ea7 was f40a309, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Unix line breaks.

  • Property mode set to 100644
File size: 17.9 KB
Line 
1/*
2 =============================================================================
3 msm.c -- MIDAS-VII -- MIDI state machine
4 Version 75 -- 1988-11-02 -- D.N. Lynx Crowe
5
6 List with pr -e4 option to expand tabs to 4 spaces instead of 8.
7 =============================================================================
8*/
9
10#include "biosdefs.h"
11#include "stddefs.h"
12#include "graphdef.h"
13#include "hwdefs.h"
14#include "smdefs.h"
15#include "sclock.h"
16#include "score.h"
17#include "vsdd.h"
18
19#include "midas.h"
20#include "scfns.h"
21
22extern short ancmsw; /* analog variable r/p control source */
23extern short angroup; /* analog variable group being displayed */
24extern short clkctl; /* clock control sources */
25extern short ctrsw; /* se_disp center update switch */
26extern short curasg; /* current assignment table */
27extern short curvce; /* current voice being edited */
28extern short dsp_ok; /* display update OK this cycle flag */
29extern short editsw; /* edit switch state */
30extern short mascntr; /* MIDI active sensing timeout counter */
31extern short mdb1; /* current MIDI data byte 1 */
32extern short mdb2; /* current MIDI data byte 2 */
33extern short michan; /* current MIDI channel */
34extern short midiclk; /* MIDI clock switch */
35extern short midigo; /* MIDI run switch */
36extern short mistat; /* current MIDI status */
37extern short ndisp; /* current display */
38extern short recsw; /* record/play switch */
39extern short prgchan; /* MIDI program change channel */
40
41/*
42
43*/
44
45extern char masens[]; /* MIDI port active sensing flags */
46extern char mctlval[]; /* MIDI controller values */
47extern char mdbyte[]; /* MIDI data byte buffers */
48extern char mpsust[]; /* MIDI sustain status, by port and channel */
49extern char mrstat[]; /* MIDI running status */
50extern char trgtab[]; /* trigger status */
51
52extern short grpmode[]; /* group mode table */
53extern short grpstat[]; /* group status table */
54extern short key2grp[]; /* key to group map */
55extern short vce2grp[]; /* voice to group map */
56extern short veltab[]; /* velocity */
57extern short mctlnum[]; /* MIDI controller number map */
58extern short mpbend[]; /* MIDI pitch bend data, by port and channel */
59extern short msmstv[]; /* MIDI state machine state variables */
60extern short prstab[]; /* pressure */
61extern short src2var[]; /* source to variable number map */
62extern short tuntab[]; /* current tuning table */
63extern short vce2trg[]; /* voice to trigger map (-1 EQ NULL) */
64
65extern short grp2prt[][2]; /* group to port/channel map */
66extern short varmode[][16]; /* analog variable record mode */
67
68extern struct gdsel *gdstbc[]; /* group status table - center slice */
69
70/* initialized stuff */
71
72/* mpmap[] -- MIDI port to BIOS device table */
73
74short mpmap[3] = {MC1_DEV, MC2_DEV, CON_DEV};
75
76/* msmnext[] -- MIDI state machine -- channel message next state table */
77
78short msmnext[8] = {2, 2, 2, 2, 1, 1, 2, 0};
79
80/* msmcnxt[] -- MIDI state machine -- system message next state table */
81
82short msmcnxt[8] = {0, 0, 2, 1, 0, 0, 0, 0};
83
84/* ctl2src -- controller to source table */
85
86short ctl2src[] = {SM_VTMW, SM_LPBR, SM_CTL1, SM_PED1};
87
88/*
89
90*/
91
92/*
93 =============================================================================
94 msm() -- MIDI state machine
95 =============================================================================
96*/
97
98msm()
99{
100 register short miport;
101 register short midiin;
102 register short i;
103 register short t1;
104 register short t2;
105 register struct s_entry *ep;
106 short j, nvel;
107 long where;
108
109 for (miport = 0; miport < NMPORTS; miport++) { /* for each port ... */
110
111 while (BIOS(B_RDAV, mpmap[miport])) { /* anything there ? */
112
113 dsp_ok = FALSE; /* disallow display update */
114
115 midiin = 0x00FF & BIOS(B_GETC, mpmap[miport]); /* get input */
116
117 if (masens[miport]) /* update active sensing */
118 masens[miport] |= 0x0F;
119
120 switch (msmstv[miport]) { /* switch on state */
121/*
122
123*/
124 case 0: /* waiting for status */
125
126 if (0x0080 & midiin) { /* status */
127
128 michan = 0x000F & midiin;
129 mistat = 0x000F & (midiin >> 4);
130
131 if (midiin < 0x00F0) { /* channel */
132
133 msmstv[miport] = msmnext[mistat & 0x07];
134 mrstat[miport] = midiin;
135
136 } else if (midiin < 0x00F8) { /* common */
137
138 msmstv[miport] = msmcnxt[michan & 0x07];
139 mrstat[miport] = midiin;
140/*
141
142*/
143 } else { /* real-time */
144
145 switch (0x0007 & midiin) {
146
147 case 0: /* clock */
148
149 if (midiclk AND midigo)
150 if ((fc_val += 2) > 0x00FFFFFFL)
151 fc_val = 0x00FFFFFFL;
152
153 break;
154
155 case 2: /* start */
156
157 if ((clkctl EQ CK_MIDI) AND
158 (NOT midigo)) {
159
160 midigo = TRUE;
161
162 if (fc_val)
163 sc_goto(fc_val = 0L);
164
165 clkset(TRUE);
166 dsclk();
167 }
168
169 break;
170
171 case 3: /* continue */
172
173 if ((clkctl EQ CK_MIDI) AND
174 (NOT midigo)) {
175
176 midigo = TRUE;
177 clkset(TRUE);
178 dsclk();
179 }
180
181 break;
182/*
183
184*/
185 case 4: /* stop */
186
187 if ((clkctl EQ CK_MIDI) AND
188 midigo) {
189
190 midigo = FALSE;
191 clkset(FALSE);
192 dsclk();
193 }
194
195 break;
196
197 case 6: /* active sensing */
198
199 masens[miport] |= 0x00FF;
200 break;
201
202 case 7: /* reset */
203
204 for (i = 0; i < NMPORTS; i++) {
205
206 mrstat[i] = 0;
207 msmstv[i] = 0;
208 masens[i] = 0;
209 }
210
211 break;
212 }
213 }
214 }
215
216 continue;
217/*
218
219*/
220 case 1: /* waiting for 1 data byte */
221
222 if (0x80 & midiin) { /* status */
223
224 michan = 0x0F & midiin;
225 mistat = 0x0F & (midiin >> 4);
226
227 if (midiin < 0x00F0) { /* channel */
228
229 msmstv[miport] = msmnext[mistat & 0x07];
230 mrstat[miport] = midiin;
231
232 } else if (midiin < 0x00F8) { /* common */
233
234 msmstv[miport] = msmcnxt[michan & 0x07];
235 mrstat[miport] = midiin;
236/*
237
238*/
239 } else { /* real-time */
240
241 switch (0x0007 & midiin) {
242
243 case 0: /* clock */
244
245 if (midiclk AND midigo)
246 if ((fc_val += 2) > 0x00FFFFFFL)
247 fc_val = 0x00FFFFFFL;
248
249 break;
250
251 case 2: /* start */
252
253 if ((clkctl EQ CK_MIDI) AND
254 (NOT midigo)) {
255
256 midigo = TRUE;
257
258 if (fc_val)
259 sc_goto(fc_val = 0L);
260
261 clkset(TRUE);
262 dsclk();
263 }
264
265 break;
266
267 case 3: /* continue */
268
269 if ((clkctl EQ CK_MIDI) AND
270 (NOT midigo)) {
271
272 midigo = TRUE;
273 clkset(TRUE);
274 dsclk();
275 }
276
277 break;
278/*
279
280*/
281 case 4: /* stop */
282
283 if ((clkctl EQ CK_MIDI) AND
284 midigo) {
285
286 midigo = FALSE;
287 clkset(FALSE);
288 dsclk();
289 }
290
291 break;
292
293 case 6: /* active sensing */
294
295 masens[miport] |= 0x00FF;
296 break;
297
298 case 7: /* reset */
299
300 for (i = 0; i < NMPORTS; i++) {
301
302 mrstat[i] = 0;
303 msmstv[i] = 0;
304 masens[i] = 0;
305 }
306
307 break;
308 }
309 }
310/*
311
312*/
313 } else { /* data */
314
315 michan = 0x000F & mrstat[miport];
316 mistat = 0x000F & (mrstat[miport] >> 4);
317
318 if (mistat EQ 0x0C) {
319
320 /* program change */
321
322 if ((miport EQ 0) AND
323 ((michan + 1) EQ prgchan) AND
324 (midiin LT NASGS)) {
325
326 getasg(curasg = midiin);
327 mpcupd();
328 }
329/*
330
331*/
332 } else if (mistat EQ 0x0D) {
333
334 /* channel pressure */
335
336 mdb1 = SM_SCALE(midiin);
337 t1 = (miport << 11) | (michan << 7);
338 t2 = t1 + 128;
339
340 for (i = t1; i < t2; i++)
341 prstab[i] = mdb1;
342
343 for (i = 0; i < 12; i++) {
344
345 if ((grp2prt[i][0] EQ (miport + 1)) AND
346 (grp2prt[i][1] EQ (michan + 1))) {
347
348 if (newsv(i, SM_KPRS, mdb1)) {
349
350 if (recsw AND grpstat[i] AND
351 (2 EQ (ancmsw ? varmode[5][i] : grpmode[i]))) {
352
353 if (E_NULL NE (ep = e_alc(E_SIZE2))) {
354
355 ep->e_time = t_cur;
356 ep->e_type = EV_ANVL;
357 ep->e_data1 = 0x0050 | i;
358 ep->e_dn = (struct s_entry *)((long)mdb1 << 16);
359 p_cur = e_ins(ep, ep_adj(p_cur, 0, t_cur))->e_fwd;
360 ctrsw = TRUE;
361 se_disp(ep, D_FWD, gdstbc, 1);
362 ctrsw = FALSE;
363 }
364
365 } else if ((angroup - 1) EQ i) {
366
367 dsanval(5);
368 }
369 }
370 }
371 }
372/*
373
374*/
375 } else if ((mistat EQ 0x0F) AND
376 (michan EQ 3) AND
377 (NOT midigo)) { /* song select */
378
379 if (midiin < N_SCORES) {
380
381 selscor(midiin);
382
383 if (ndisp EQ 2)
384 sdwins();
385 }
386
387 } else { /* error -- state mismatch */
388
389 msmstv[miport] = 0;
390 mrstat[miport] = 0;
391 }
392 }
393
394 continue;
395/*
396
397*/
398 case 2: /* waiting for 1st data byte */
399
400 if (0x80 & midiin) { /* status */
401
402 michan = 0x0F & midiin;
403 mistat = 0x0F & (midiin >> 4);
404
405 if (midiin < 0x00F0) { /* channel */
406
407 msmstv[miport] = msmnext[mistat & 0x07];
408 mrstat[miport] = midiin;
409
410 } else if (midiin < 0x00F8) { /* common */
411
412 msmstv[miport] = msmcnxt[michan & 0x07];
413 mrstat[miport] = midiin;
414/*
415
416*/
417 } else { /* real-time */
418
419 switch (0x0007 & midiin) {
420
421 case 0: /* clock */
422
423 if (midiclk AND midigo)
424 if ((fc_val += 2) > 0x00FFFFFFL)
425 fc_val = 0x00FFFFFFL;
426
427 break;
428
429 case 2: /* start */
430
431 if ((clkctl EQ CK_MIDI) AND
432 (NOT midigo)) {
433
434 midigo = TRUE;
435
436 if (fc_val)
437 sc_goto(fc_val = 0L);
438
439 clkset(TRUE);
440 dsclk();
441 }
442
443 break;
444
445 case 3: /* continue */
446
447 if ((clkctl EQ CK_MIDI) AND
448 (NOT midigo)) {
449
450 midigo = TRUE;
451 clkset(TRUE);
452 dsclk();
453 }
454
455 break;
456/*
457
458*/
459 case 4: /* stop */
460
461 if ((clkctl EQ CK_MIDI) AND
462 midigo) {
463
464 midigo = FALSE;
465 clkset(FALSE);
466 dsclk();
467 }
468
469 break;
470
471 case 6: /* active sensing */
472
473 masens[miport] |= 0x00FF;
474 break;
475
476 case 7: /* reset */
477
478 for (i = 0; i < NMPORTS; i++) {
479
480 mrstat[i] = 0;
481 msmstv[i] = 0;
482 masens[i] = 0;
483 }
484
485 break;
486 }
487 }
488
489 } else { /* data */
490
491 mdbyte[miport] = midiin;
492 msmstv[miport] = 3;
493 }
494
495 continue;
496/*
497
498*/
499 case 3: /* waiting for 2nd data byte */
500
501 if (0x80 & midiin) { /* status */
502
503 michan = 0x0F & midiin;
504 mistat = 0x0F & (midiin >> 4);
505
506 if (midiin < 0x00F0) { /* channel */
507
508 msmstv[miport] = msmnext[mistat & 0x07];
509 mrstat[miport] = midiin;
510
511 } else if (midiin < 0x00F8) { /* common */
512
513 msmstv[miport] = msmcnxt[michan & 0x07];
514 mrstat[miport] = midiin;
515/*
516
517*/
518 } else { /* real-time */
519
520 switch (0x0007 & midiin) {
521
522 case 0: /* clock */
523
524 if (midiclk AND midigo)
525 if ((fc_val += 2) > 0x00FFFFFFL)
526 fc_val = 0x00FFFFFFL;
527
528 break;
529
530 case 2: /* start */
531
532 if ((clkctl EQ CK_MIDI) AND
533 (NOT midigo)) {
534
535 midigo = TRUE;
536
537 if (fc_val)
538 sc_goto(fc_val = 0L);
539
540 clkset(TRUE);
541 dsclk();
542 }
543
544 break;
545
546 case 3: /* continue */
547
548 if ((clkctl EQ CK_MIDI) AND
549 (NOT midigo)) {
550
551 midigo = TRUE;
552 clkset(TRUE);
553 dsclk();
554 }
555
556 break;
557/*
558
559*/
560 case 4: /* stop */
561
562 if ((clkctl EQ CK_MIDI) AND
563 midigo) {
564
565 midigo = FALSE;
566 clkset(FALSE);
567 dsclk();
568 }
569
570 break;
571
572 case 6: /* active sensing */
573
574 masens[miport] |= 0x00FF;
575 break;
576
577 case 7: /* reset */
578
579 for (i = 0; i < NMPORTS; i++) {
580
581 mrstat[i] = 0;
582 msmstv[i] = 0;
583 masens[i] = 0;
584 }
585
586 break;
587 }
588 }
589/*
590
591*/
592 } else { /* data */
593
594 michan = 0x0F & mrstat[miport];
595 mistat = 0x0F & (mrstat[miport] >> 4);
596 mdb1 = mdbyte[miport];
597 mdb2 = midiin;
598 msmstv[miport] = 2;
599
600 switch (mistat) { /* switch on status */
601
602 case 9: /* note on */
603
604 if (mdb2) {
605
606 t1 = (miport << 11) + (michan << 7) + mdb1;
607 trgtab[t1] |= mpsust[(miport << 4) + michan] | M_KSTATE;
608 veltab[t1] = nvel = SM_SCALE(mdb2);
609 prstab[t1] = 0;
610
611 stmproc(t1); /* process as a patch stimulus */
612
613 if (editsw) { /* edit mode */
614
615 execkey(t1, tuntab[mdb1], curvce, 0);
616 break;
617 }
618
619 if (miport) { /* port 1 or 2 */
620
621 for (i = 0; i < 12; i++)
622 if ((grp2prt[i][0] EQ (miport + 1)) AND
623 (grp2prt[i][1] EQ (michan + 1))) {
624
625 asgvce(i, miport, michan, mdb1, nvel);
626 ne_bgn(i, mdb1, nvel);
627 }
628
629 } else { /* port 0 */
630
631 t2 = ((mdb1 < 21) OR (mdb1 > 108)) ?
632 0 : key2grp[mdb1 - 21];
633
634 for (i = 0; i < 12; i++)
635 if ((t2 & (0x0001 << i)) AND
636 (grp2prt[i][1] EQ (michan + 1))) {
637
638 asgvce(i, miport, michan, mdb1, nvel);
639 ne_bgn(i, mdb1, nvel);
640 }
641 }
642
643 break;
644 }
645/*
646
647*/
648 case 8: /* note off */
649
650 t1 = (miport << 11) + (michan << 7) + mdb1;
651 trgtab[t1] &= ~M_KSTATE;
652
653 if (0 EQ trgtab[t1]) {
654
655 for (i = 0; i < 12; i++) {
656
657 if (vce2trg[i] EQ t1) {
658
659 vce2trg[i] = -1;
660 procpfl(t1);
661 }
662
663 if ((grp2prt[i][0] EQ (miport + 1)) AND
664 (grp2prt[i][1] EQ (michan + 1)))
665 ne_end(t1, i);
666 }
667
668 stmproc(0x8000 | t1); /* process as a patch stimulus */
669 }
670
671 break;
672
673 case 10: /* poly pressure */
674
675 t1 = (miport << 11) + (michan << 7) + mdb1;
676 t2 = SM_SCALE(mdb2);
677 prstab[t1] = t2;
678
679 for (i = 0; i < 12; i++)
680 if ((grp2prt[i][0] EQ (miport + 1)) AND
681 (grp2prt[i][1] EQ (michan + 1)))
682 newpps(t1, i, SM_KPRS, t2);
683
684 break;
685/*
686
687*/
688 case 11: /* control change */
689
690 mctlval[(miport << 11) + (michan << 7) + mdb1] = mdb2;
691
692 if (mdb1 EQ MIDISUS) {
693
694 /* sustain changed */
695
696 t1 = (miport << 11) | (michan << 7);
697 t2 = t1 + 128;
698
699 if (mdb2 GT MSW_ON) { /* switch on */
700
701 mpsust[(miport << 4) + michan] = M_CHNSUS;
702
703 for (i = t1; i < t2; i++)
704 if (trgtab[i])
705 trgtab[i] |= M_CHNSUS;
706
707 } else if (mdb2 LT MSW_OFF) { /* switch off */
708
709 mpsust[(miport << 4) + michan] = 0;
710
711 for (i = t1; i < t2; i++) {
712
713 if (trgtab[i]) {
714
715 trgtab[i] &= ~M_CHNSUS;
716
717 if (0 EQ trgtab[i]) {
718
719 for (t1 = 0; t1 < 12; t1++) {
720
721 if (vce2trg[t1] EQ i) {
722
723 vce2trg[t1] = -1;
724 procpfl(i);
725 }
726
727 if ((grp2prt[t1][0] EQ (miport + 1)) AND
728 (grp2prt[t1][1] EQ (michan + 1)))
729 ne_end(i, t1);
730 }
731 }
732 }
733 }
734 }
735/*
736
737*/
738 } else if (mdb1 EQ MIDIHLD) {
739
740 /* hold changed */
741
742 t1 = (miport << 11) | (michan << 7);
743 t2 = t1 + 128;
744
745 if (mdb2 GT MSW_ON) { /* switch on */
746
747 for (i = t1; i < t2; i++)
748 if (trgtab[i] & M_KSTATE) {
749
750 for (t1 = 0; t1 < 12; t1++)
751 if ((vce2trg[t1] & ~(MKEYHELD << 8)) EQ i)
752 vce2trg[t1] |= (M_CHNHLD << 8);
753 }
754
755 } else if (mdb2 LT MSW_OFF) { /* switch off */
756
757 for (i = 0; i <12; i++) {
758
759 if (vce2trg[i] NE -1) {
760
761 vce2trg[i] &= ~(M_CHNHLD << 8);
762
763 if (0 EQ (vce2trg[i] & (MKEYHELD << 8))) {
764
765 t1 = vce2trg[i];
766
767 if (0 EQ trgtab[t1]) {
768
769 vce2trg[i] = -1;
770 procpfl(t1);
771 }
772 }
773 }
774 }
775 }
776/*
777
778*/
779 } else { /* other controllers */
780
781 t1 = (miport << 11) | (michan << 7) | mdb1;
782 t2 = SM_SCALE(mdb2);
783 mctlval[t1] = t2;
784
785 for (j = 0; j < 4; j++) {
786
787 if ((mctlnum[j] & 0x00FF) EQ mdb1) {
788
789 i = ctl2src[j];
790
791 for (t1 = 0; t1 < 12; t1++) {
792
793 if ((mctlnum[j] & CTAG1) OR
794 ((grp2prt[t1][0] EQ (miport + 1)) AND
795 (grp2prt[t1][1] EQ (michan + 1)))) {
796
797 if (newsv(t1, i, t2)) {
798
799 if (recsw AND grpstat[t1] AND
800 (2 EQ (ancmsw ? varmode[src2var[i]][t1] : grpmode[t1]))) {
801
802 if (E_NULL NE (ep = e_alc(E_SIZE2))) {
803
804 ep->e_time = t_cur;
805 ep->e_type = EV_ANVL;
806 ep->e_data1 = (src2var[i] << 4) | t1;
807 ep->e_dn = (struct s_entry *)((long)t2 << 16);
808 p_cur = e_ins(ep, ep_adj(p_cur, 0, t_cur))->e_fwd;
809 ctrsw = TRUE;
810 se_disp(ep, D_FWD, gdstbc, 1);
811 ctrsw = FALSE;
812 }
813
814 } else if ((angroup - 1) EQ t1) {
815
816 dsanval(src2var[i]);
817 }
818 }
819 }
820 }
821 }
822 }
823 }
824
825 break;
826/*
827
828*/
829 case 14: /* pitch bend */
830
831 t1 = (miport << 4) | michan;
832 t2 = ((mdb2 << 9) | (mdb1 << 2)) ^ 0x8000;
833 mpbend[t1] = t2;
834
835 for (i = 0; i < 12; i++) {
836
837 if ((grp2prt[i][0] EQ (miport + 1)) AND
838 (grp2prt[i][1] EQ (michan + 1))) {
839
840 if (newsv(i, SM_HTPW, t2)) {
841
842 if (recsw AND grpstat[i] AND
843 (2 EQ (ancmsw ? varmode[0][i] : grpmode[i]))) {
844
845 if (E_NULL NE (ep = e_alc(E_SIZE2))) {
846
847 ep->e_time = t_cur;
848 ep->e_type = EV_ANVL;
849 ep->e_data1 = i;
850 ep->e_dn = (struct s_entry *)((long)t2 << 16);
851 p_cur = e_ins(ep, ep_adj(p_cur, 0, t_cur))->e_fwd;
852 ctrsw = TRUE;
853 se_disp(ep, D_FWD, gdstbc, 1);
854 ctrsw = FALSE;
855 }
856
857 } else if ((angroup - 1) EQ i) {
858
859 dsanval(0);
860 }
861 }
862 }
863 }
864
865 break;
866/*
867
868*/
869 case 15: /* common */
870
871 if (michan EQ 2) { /* song position */
872
873 if ((clkctl EQ CK_MIDI) AND
874 (NOT midigo)) {
875
876 where = 12 * ((mdb1 << 7) | mdb2);
877
878 if (fc_val NE where)
879 sc_goto(fc_val = where);
880 }
881
882 break;
883
884 } else { /* bad running status */
885
886 msmstv[miport] = 0;
887 mrstat[miport] = 0;
888 break;
889 }
890
891 default: /* error -- bad status */
892
893 msmstv[miport] = 0;
894 mrstat[miport] = 0;
895 break;
896
897 } /* end switch (on status) */
898
899 } /* end if (status) */
900
901 continue;
902
903 default: /* error -- bad state */
904
905 msmstv[miport] = 0;
906 mrstat[miport] = 0;
907 break;
908
909 } /* end switch (on state) */
910 } /* end while (data available) */
911 } /* end for (each port) */
912}
Note: See TracBrowser for help on using the repository browser.