4 * A low level driver for Yamaha YM3812 and OPL-3 -chips
6 * Copyright by Hannu Savolainen 1993
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/i386/isa/sound/opl3.c,v 1.20 1999/12/27 04:37:18 tanimura Exp $
33 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
38 #include <i386/isa/sound/sound_config.h>
41 #if defined(CONFIG_YM3812)
43 #include <i386/isa/sound/opl3.h>
44 #include <machine/clock.h>
59 typedef struct opl_devinfo {
60 int left_io, right_io;
62 int lv_map[MAX_VOICE];
64 struct voice_info voc[MAX_VOICE];
65 struct voice_alloc_info *v_alloc;
66 struct channel_info *chn_info;
68 struct sbi_instrument i_map[SBFM_MAXINSTR];
69 struct sbi_instrument *act_i[MAX_VOICE];
71 struct synth_info fm_info;
82 static struct opl_devinfo *devc = NULL;
85 static int detected_model;
87 static int store_instr(int instr_no, struct sbi_instrument * instr);
88 static void freq_to_fnum(int freq, int *block, int *fnum);
89 static void opl3_command(int io_addr, u_int addr, u_int val);
90 static int opl3_kill_note(int dev, int voice, int note, int velocity);
93 enable_opl3_mode(int left, int right, int both)
102 static int v4op[MAX_VOICE] =
103 {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
105 devc->cmask = 0x3f; /* Connect all possible 4 OP voice operators */
106 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
108 for (i = 0; i < 3; i++)
109 pv_map[i].voice_mode = 4;
110 for (i = 3; i < 6; i++)
111 pv_map[i].voice_mode = 0;
113 for (i = 9; i < 12; i++)
114 pv_map[i].voice_mode = 4;
115 for (i = 12; i < 15; i++)
116 pv_map[i].voice_mode = 0;
118 for (i = 0; i < 12; i++)
119 devc->lv_map[i] = v4op[i];
120 devc->v_alloc->max_voice = devc->nr_voice = 12;
125 u_int cmd, ioctl_arg arg)
129 case SNDCTL_FM_LOAD_INSTR:
131 struct sbi_instrument ins;
133 bcopy(&(((char *) arg)[0]), (char *) &ins, sizeof(ins));
135 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
136 printf("FM Error: Invalid instrument number %d\n", ins.channel);
139 pmgr_inform(dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
140 return store_instr(ins.channel, &ins);
144 case SNDCTL_SYNTH_INFO:
145 devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
146 bcopy(&devc->fm_info, &(((char *) arg)[0]), sizeof(devc->fm_info));
150 case SNDCTL_SYNTH_MEMAVL:
154 case SNDCTL_FM_4OP_ENABLE:
155 if (devc->model == 2)
167 opl3_detect(int ioaddr, sound_os_info * osp)
170 * This function returns 1 if the FM chip is present at the given
171 * I/O port The detection algorithm plays with the timer built in the
172 * FM chip and looks for a change in the status register.
174 * Note! The timers of the FM chip are not connected to AdLib (and
175 * compatible) boards.
177 * Note2! The chip is initialized if detected.
180 u_char stat1, stat2, signature;
186 devc = (struct opl_devinfo *) malloc(sizeof(*devc), M_DEVBUF, M_NOWAIT);
188 panic("SOUND: Cannot allocate memory\n");
191 printf("OPL3: Can't allocate memory for device control structure\n");
196 /* Reset timers 1 and 2 */
197 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
199 /* Reset the IRQ of the FM chip */
200 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
202 signature = stat1 = inb(ioaddr); /* Status register */
204 if ((stat1 & 0xE0) != 0x00) {
205 return 0; /* Should be 0x00 */
207 opl3_command(ioaddr, TIMER1_REGISTER, 0xff); /* Set timer1 to 0xff */
209 opl3_command(ioaddr, TIMER_CONTROL_REGISTER,
210 TIMER2_MASK | TIMER1_START); /* Unmask and start timer 1 */
213 DELAY(150); /* Now we have to delay at least 80 usec */
215 stat2 = inb(ioaddr); /* Read status after timers have expired */
221 /* Reset timers 1 and 2 */
222 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
223 /* Reset the IRQ of the FM chip */
224 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
226 if ((stat2 & 0xE0) != 0xc0) {
227 return 0; /* There is no YM3812 */
230 * There is a FM chicp in this address. Detect the type (OPL2 to
234 if (signature == 0x06) {/* OPL2 */
236 } else if (signature == 0x00) { /* OPL3 or OPL4 */
242 * Detect availability of OPL4 (_experimental_). Works
243 * propably only after a cold boot. In addition the OPL4 port
244 * of the chip may not be connected to the PC bus at all.
247 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
248 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
250 if ((tmp = inb(ioaddr)) == 0x02) { /* Have a OPL4 */
253 if (!0) { /* OPL4 port is free */ /* XXX check here lr970711 */
256 outb(ioaddr - 8, 0x02); /* Select OPL4 ID register */
258 tmp = inb(ioaddr - 7); /* Read it */
261 if (tmp == 0x20) { /* OPL4 should return 0x20 here */
264 outb(ioaddr - 8, 0xF8); /* Select OPL4 FM mixer control */
266 outb(ioaddr - 7, 0x1B); /* Write value */
271 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
274 for (i = 0; i < 9; i++)
275 opl3_command(ioaddr, KEYON_BLOCK + i, 0); /* Note off */
277 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
278 opl3_command(ioaddr, PERCUSSION_REGISTER, 0x00); /* Melodic mode. */
284 opl3_kill_note(int dev, int voice, int note, int velocity)
286 struct physical_voice_info *map;
288 if (voice < 0 || voice >= devc->nr_voice)
291 devc->v_alloc->map[voice] = 0;
293 map = &pv_map[devc->lv_map[voice]];
295 DEB(printf("Kill note %d\n", voice));
297 if (map->voice_mode == 0)
300 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num,
301 devc->voc[voice].keyon_byte & ~0x20);
303 devc->voc[voice].keyon_byte = 0;
304 devc->voc[voice].bender = 0;
305 devc->voc[voice].volume = 64;
306 devc->voc[voice].bender_range = 200; /* 200 cents = 2 semitones */
307 devc->voc[voice].orig_freq = 0;
308 devc->voc[voice].current_freq = 0;
309 devc->voc[voice].mode = 0;
319 #define UNDEFINED TOMTOM
320 #define DEFAULT TOMTOM
323 store_instr(int instr_no, struct sbi_instrument * instr)
326 if (instr->key !=FM_PATCH && (instr->key !=OPL3_PATCH || devc->model != 2))
327 printf("FM warning: Invalid patch format field (key) 0x%x\n",
329 bcopy((char *) instr, (char *) &(devc->i_map[instr_no]), sizeof(*instr));
335 opl3_set_instr(int dev, int voice, int instr_no)
337 if (voice < 0 || voice >= devc->nr_voice)
340 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
343 devc->act_i[voice] = &devc->i_map[instr_no];
348 * The next table looks magical, but it certainly is not. Its values have
349 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
350 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
351 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
352 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
353 * volume -8 it was implemented as a table because it is only 128 bytes and
354 * it saves a lot of log() calculations. (RH)
356 static char fm_volume_table[128] =
358 -64, -48, -40, -35, -32, -29, -27, -26,
359 -24, -23, -21, -20, -19, -18, -18, -17,
360 -16, -15, -15, -14, -13, -13, -12, -12,
361 -11, -11, -10, -10, -10, -9, -9, -8,
362 -8, -8, -7, -7, -7, -6, -6, -6,
363 -5, -5, -5, -5, -4, -4, -4, -4,
364 -3, -3, -3, -3, -2, -2, -2, -2,
365 -2, -1, -1, -1, -1, 0, 0, 0,
366 0, 0, 0, 1, 1, 1, 1, 1,
367 1, 2, 2, 2, 2, 2, 2, 2,
368 3, 3, 3, 3, 3, 3, 3, 4,
369 4, 4, 4, 4, 4, 4, 4, 5,
370 5, 5, 5, 5, 5, 5, 5, 5,
371 6, 6, 6, 6, 6, 6, 6, 6,
372 6, 7, 7, 7, 7, 7, 7, 7,
373 7, 7, 7, 8, 8, 8, 8, 8};
376 calc_vol(u_char *regbyte, int volume, int main_vol)
378 int level = (~*regbyte & 0x3f);
383 volume = (volume * main_vol) / 127;
386 level += fm_volume_table[volume];
388 RANGE (level, 0, 0x3f );
390 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
394 set_voice_volume(int voice, int volume, int main_vol)
396 u_char vol1, vol2, vol3, vol4;
397 struct sbi_instrument *instr;
398 struct physical_voice_info *map;
400 if (voice < 0 || voice >= devc->nr_voice)
403 map = &pv_map[devc->lv_map[voice]];
405 instr = devc->act_i[voice];
408 instr = &devc->i_map[0];
410 if (instr->channel < 0)
413 if (devc->voc[voice].mode == 0)
416 if (devc->voc[voice].mode == 2) {
418 vol1 = instr->operators[2];
419 vol2 = instr->operators[3];
421 if ((instr->operators[10] & 0x01)) {
422 calc_vol(&vol1, volume, main_vol);
424 calc_vol(&vol2, volume, main_vol);
426 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
427 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
428 } else { /* 4 OP voice */
431 vol1 = instr->operators[2];
432 vol2 = instr->operators[3];
433 vol3 = instr->operators[OFFS_4OP + 2];
434 vol4 = instr->operators[OFFS_4OP + 3];
437 * The connection method for 4 OP devc->voc is defined by the
438 * rightmost bits at the offsets 10 and 10+OFFS_4OP
441 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
443 switch (connection) {
445 calc_vol(&vol4, volume, main_vol);
449 calc_vol(&vol2, volume, main_vol);
450 calc_vol(&vol4, volume, main_vol);
454 calc_vol(&vol1, volume, main_vol);
455 calc_vol(&vol4, volume, main_vol);
459 calc_vol(&vol1, volume, main_vol);
460 calc_vol(&vol3, volume, main_vol);
461 calc_vol(&vol4, volume, main_vol);
467 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
468 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
469 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
470 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
475 opl3_start_note(int dev, int voice, int note, int volume)
478 int block, fnum, freq, voice_mode;
479 struct sbi_instrument *instr;
480 struct physical_voice_info *map;
482 if (voice < 0 || voice >= devc->nr_voice)
485 map = &pv_map[devc->lv_map[voice]];
487 if (map->voice_mode == 0)
490 if (note == 255) { /* Just change the volume */
491 set_voice_volume(voice, volume, devc->voc[voice].volume);
495 * Kill previous note before playing
497 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /* Carrier volume to min */
498 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /* Modulator volume to */
500 if (map->voice_mode == 4) {
501 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
502 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
504 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /* Note off */
506 instr = devc->act_i[voice];
509 instr = &devc->i_map[0];
511 if (instr->channel < 0) {
512 printf( "OPL3: Initializing voice %d with undefined instrument\n",
516 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
517 return 0; /* Cannot play */
519 voice_mode = map->voice_mode;
521 if (voice_mode == 4) {
524 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
525 voice_shift += map->voice_num;
527 if (instr->key != OPL3_PATCH) { /* Just 2 OP patch */
529 devc->cmask &= ~(1 << voice_shift);
531 devc->cmask |= (1 << voice_shift);
533 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
536 * Set Sound Characteristics
538 opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
539 opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
544 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
545 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
548 * Set Sustain/Release
550 opl3_command(map->ioaddr,SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
551 opl3_command(map->ioaddr,SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
556 opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
557 opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
560 * Set Feedback/Connection
562 fpc = instr->operators[10];
564 fpc |= 0x30; /* Ensure that at least one chn is enabled */
565 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
568 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
571 if (voice_mode == 4) {
574 * Set Sound Characteristics
576 opl3_command(map->ioaddr, AM_VIB + map->op[2],
577 instr->operators[OFFS_4OP + 0]);
578 opl3_command(map->ioaddr, AM_VIB + map->op[3],
579 instr->operators[OFFS_4OP + 1]);
584 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2],
585 instr->operators[OFFS_4OP + 4]);
586 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3],
587 instr->operators[OFFS_4OP + 5]);
590 * Set Sustain/Release
592 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2],
593 instr->operators[OFFS_4OP + 6]);
594 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3],
595 instr->operators[OFFS_4OP + 7]);
600 opl3_command(map->ioaddr, WAVE_SELECT + map->op[2],
601 instr->operators[OFFS_4OP + 8]);
602 opl3_command(map->ioaddr, WAVE_SELECT + map->op[3],
603 instr->operators[OFFS_4OP + 9]);
606 * Set Feedback/Connection
608 fpc = instr->operators[OFFS_4OP + 10];
610 fpc |= 0x30; /* Ensure that at least one chn is enabled */
611 opl3_command(map->ioaddr,FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
613 devc->voc[voice].mode = voice_mode;
615 set_voice_volume(voice, volume, devc->voc[voice].volume);
617 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
620 * Since the pitch bender may have been set before playing the note,
621 * we have to calculate the bending now.
624 freq = compute_finetune(devc->voc[voice].orig_freq,
625 devc->voc[voice].bender, devc->voc[voice].bender_range);
626 devc->voc[voice].current_freq = freq;
628 freq_to_fnum(freq, &block, &fnum);
634 data = fnum & 0xff; /* Least significant bits of fnumber */
635 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
637 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
638 devc->voc[voice].keyon_byte = data;
639 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
641 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
647 freq_to_fnum(int freq, int *block, int *fnum)
652 * Converts the note frequency to block and fnum values for the FM
656 * First try to compute the block -value (octave) where the note
671 } else if (f > 493) {
680 *fnum = freq * (1 << (20 - octave)) / 49716;
685 opl3_command(int io_addr, u_int addr, u_int val)
690 * The original 2-OP synth requires a quite long delay after writing
691 * to a register. The OPL-3 survives with just two INBs
694 outb(io_addr, (u_char) (addr & 0xff));
696 if (!devc->model != 2)
699 for (i = 0; i < 2; i++)
703 outb(io_addr + 0x100, (u_char) (val & 0xff));
705 outb(io_addr + 1, (u_char) (val & 0xff));
707 if (devc->model != 2)
710 for (i = 0; i < 2; i++)
719 for (i = 0; i < 18; i++)
722 for (i = 0; i < devc->nr_voice; i++) {
723 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
724 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
726 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
727 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
729 if (pv_map[devc->lv_map[i]].voice_mode == 4) {
730 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
731 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
733 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
734 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
736 opl3_kill_note(dev, i, 0, 64);
739 if (devc->model == 2) {
740 devc->v_alloc->max_voice = devc->nr_voice = 18;
742 for (i = 0; i < 18; i++)
743 pv_map[i].voice_mode = 2;
749 opl3_open(int dev, int mode)
757 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
758 devc->v_alloc->timestamp = 0;
760 for (i = 0; i < 18; i++) {
761 devc->v_alloc->map[i] = 0;
762 devc->v_alloc->alloc_times[i] = 0;
765 devc->cmask = 0x00; /* Just 2 OP mode */
766 if (devc->model == 2)
767 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
775 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
777 devc->fm_info.nr_drums = 0;
778 devc->fm_info.perc_mode = 0;
784 opl3_hw_control(int dev, u_char *event)
789 opl3_load_patch(int dev, int format, snd_rw_buf * addr,
790 int offs, int count, int pmgr_flag)
792 struct sbi_instrument ins;
794 if (count < sizeof(ins)) {
795 printf("FM Error: Patch record too short\n");
798 if (uiomove(&((char *) &ins)[offs], sizeof(ins) - offs, addr)) {
799 printf("sb: Bad copyin()!\n");
802 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
803 printf("FM Error: Invalid instrument number %d\n", ins.channel);
808 return store_instr(ins.channel, &ins);
812 opl3_panning(int dev, int voice, int pressure)
817 opl3_volume_method(int dev, int mode)
821 #define SET_VIBRATO(cell) { \
822 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
823 if (pressure > 110) \
824 tmp |= 0x40; /* Vibrato on */ \
825 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
828 opl3_aftertouch(int dev, int voice, int pressure)
831 struct sbi_instrument *instr;
832 struct physical_voice_info *map;
834 if (voice < 0 || voice >= devc->nr_voice)
837 map = &pv_map[devc->lv_map[voice]];
839 DEB(printf("Aftertouch %d\n", voice));
841 if (map->voice_mode == 0)
845 * Adjust the amount of vibrato depending the pressure
848 instr = devc->act_i[voice];
851 instr = &devc->i_map[0];
853 if (devc->voc[voice].mode == 4) {
854 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
856 switch (connection) {
879 * Not implemented yet
884 if ((instr->operators[10] & 0x01)) /* Additive synthesis */
892 bend_pitch(int dev, int voice, int value)
895 int block, fnum, freq;
896 struct physical_voice_info *map;
898 map = &pv_map[devc->lv_map[voice]];
900 if (map->voice_mode == 0)
903 devc->voc[voice].bender = value;
906 if (!(devc->voc[voice].keyon_byte & 0x20))
907 return; /* Not keyed on */
909 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range);
910 devc->voc[voice].current_freq = freq;
912 freq_to_fnum(freq, &block, &fnum);
914 data = fnum & 0xff; /* Least significant bits of fnumber */
915 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
917 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
918 /* KEYON|OCTAVE|MS bits of f-num */
919 devc->voc[voice].keyon_byte = data;
920 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
924 opl3_controller(int dev, int voice, int ctrl_num, int value)
926 if (voice < 0 || voice >= devc->nr_voice)
930 case CTRL_PITCH_BENDER:
931 bend_pitch(dev, voice, value);
934 case CTRL_PITCH_BENDER_RANGE:
935 devc->voc[voice].bender_range = value;
938 case CTL_MAIN_VOLUME:
939 devc->voc[voice].volume = value / 128;
945 opl3_patchmgr(int dev, struct patmgr_info * rec)
951 opl3_bender(int dev, int voice, int value)
953 if (voice < 0 || voice >= devc->nr_voice)
956 bend_pitch(dev, voice, value - 8192);
960 opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info * alloc)
962 int i, p, best, first, avail, best_time = 0x7fffffff;
963 struct sbi_instrument *instr;
967 if (chn < 0 || chn > 15)
970 instr_no = devc->chn_info[chn].pgm_num;
972 instr = &devc->i_map[instr_no];
973 if (instr->channel < 0 || /* Instrument not loaded */
974 devc->nr_voice != 12) /* Not in 4 OP mode */
976 else if (devc->nr_voice == 12) /* 4 OP mode */
977 is4op = (instr->key == OPL3_PATCH);
985 if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP
986 * only' operators first */
990 avail = devc->nr_voice;
994 * Now try to find a free voice
998 for (i = 0; i < avail; i++) {
999 if (alloc->map[p] == 0) {
1002 if (alloc->alloc_times[p] < best_time) { /* Find oldest playing note */
1003 best_time = alloc->alloc_times[p];
1006 p = (p + 1) % avail;
1010 * Insert some kind of priority mechanism here.
1015 if (best > devc->nr_voice)
1016 best -= devc->nr_voice;
1018 return best; /* All devc->voc in use. Select the first
1023 opl3_setup_voice(int dev, int voice, int chn)
1025 struct channel_info *info =
1026 &synth_devs[dev]->chn_info[chn];
1028 opl3_set_instr(dev, voice, info->pgm_num);
1030 devc->voc[voice].bender = info->bender_value;
1031 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1034 static struct synth_operations opl3_operations =
1060 opl3_init(int ioaddr, sound_os_info * osp)
1064 if (num_synths >= MAX_SYNTH_DEV) {
1065 printf("OPL3 Error: Too many synthesizers\n");
1069 printf("OPL3: Device control structure not initialized.\n");
1072 bzero((char *) devc, sizeof(*devc));
1076 strcpy(devc->fm_info.name, "OPL2-");
1078 devc->fm_info.device = 0;
1079 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1080 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1081 devc->fm_info.perc_mode = 0;
1082 devc->fm_info.nr_voices = 9;
1083 devc->fm_info.nr_drums = 0;
1084 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1085 devc->fm_info.capabilities = 0;
1086 devc->left_io = ioaddr;
1087 devc->right_io = ioaddr + 2;
1089 if (detected_model <= 2)
1093 if (detected_model == 4)
1097 opl3_operations.info = &devc->fm_info;
1099 synth_devs[num_synths++] = &opl3_operations;
1100 devc->v_alloc = &opl3_operations.alloc;
1101 devc->chn_info = &opl3_operations.chn_info[0];
1103 if (devc->model == 2) {
1105 conf_printf2("Yamaha OPL4/OPL3 FM", ioaddr, 0, -1, -1);
1107 conf_printf2("Yamaha OPL3 FM", ioaddr, 0, -1, -1);
1109 devc->v_alloc->max_voice = devc->nr_voice = 18;
1110 devc->fm_info.nr_drums = 0;
1111 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1112 strcpy(devc->fm_info.name, "Yamaha OPL-3");
1114 for (i = 0; i < 18; i++)
1115 if (pv_map[i].ioaddr == USE_LEFT)
1116 pv_map[i].ioaddr = devc->left_io;
1118 pv_map[i].ioaddr = devc->right_io;
1120 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1121 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1123 conf_printf2("Yamaha OPL2 FM", ioaddr, 0, -1, -1);
1124 devc->v_alloc->max_voice = devc->nr_voice = 9;
1125 devc->fm_info.nr_drums = 0;
1127 for (i = 0; i < 18; i++)
1128 pv_map[i].ioaddr = devc->left_io;
1131 for (i = 0; i < SBFM_MAXINSTR; i++)
1132 devc->i_map[i].channel = -1;