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 $
29 * $DragonFly: src/sys/dev/sound/isa/i386/Attic/opl3.c,v 1.2 2003/06/17 04:28:38 dillon Exp $
34 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
39 #include <i386/isa/sound/sound_config.h>
42 #if defined(CONFIG_YM3812)
44 #include <i386/isa/sound/opl3.h>
45 #include <machine/clock.h>
60 typedef struct opl_devinfo {
61 int left_io, right_io;
63 int lv_map[MAX_VOICE];
65 struct voice_info voc[MAX_VOICE];
66 struct voice_alloc_info *v_alloc;
67 struct channel_info *chn_info;
69 struct sbi_instrument i_map[SBFM_MAXINSTR];
70 struct sbi_instrument *act_i[MAX_VOICE];
72 struct synth_info fm_info;
83 static struct opl_devinfo *devc = NULL;
86 static int detected_model;
88 static int store_instr(int instr_no, struct sbi_instrument * instr);
89 static void freq_to_fnum(int freq, int *block, int *fnum);
90 static void opl3_command(int io_addr, u_int addr, u_int val);
91 static int opl3_kill_note(int dev, int voice, int note, int velocity);
94 enable_opl3_mode(int left, int right, int both)
103 static int v4op[MAX_VOICE] =
104 {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
106 devc->cmask = 0x3f; /* Connect all possible 4 OP voice operators */
107 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
109 for (i = 0; i < 3; i++)
110 pv_map[i].voice_mode = 4;
111 for (i = 3; i < 6; i++)
112 pv_map[i].voice_mode = 0;
114 for (i = 9; i < 12; i++)
115 pv_map[i].voice_mode = 4;
116 for (i = 12; i < 15; i++)
117 pv_map[i].voice_mode = 0;
119 for (i = 0; i < 12; i++)
120 devc->lv_map[i] = v4op[i];
121 devc->v_alloc->max_voice = devc->nr_voice = 12;
126 u_int cmd, ioctl_arg arg)
130 case SNDCTL_FM_LOAD_INSTR:
132 struct sbi_instrument ins;
134 bcopy(&(((char *) arg)[0]), (char *) &ins, sizeof(ins));
136 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
137 printf("FM Error: Invalid instrument number %d\n", ins.channel);
140 pmgr_inform(dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
141 return store_instr(ins.channel, &ins);
145 case SNDCTL_SYNTH_INFO:
146 devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
147 bcopy(&devc->fm_info, &(((char *) arg)[0]), sizeof(devc->fm_info));
151 case SNDCTL_SYNTH_MEMAVL:
155 case SNDCTL_FM_4OP_ENABLE:
156 if (devc->model == 2)
168 opl3_detect(int ioaddr, sound_os_info * osp)
171 * This function returns 1 if the FM chip is present at the given
172 * I/O port The detection algorithm plays with the timer built in the
173 * FM chip and looks for a change in the status register.
175 * Note! The timers of the FM chip are not connected to AdLib (and
176 * compatible) boards.
178 * Note2! The chip is initialized if detected.
181 u_char stat1, stat2, signature;
187 devc = (struct opl_devinfo *) malloc(sizeof(*devc), M_DEVBUF, M_NOWAIT);
189 panic("SOUND: Cannot allocate memory\n");
192 printf("OPL3: Can't allocate memory for device control structure\n");
197 /* Reset timers 1 and 2 */
198 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
200 /* Reset the IRQ of the FM chip */
201 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
203 signature = stat1 = inb(ioaddr); /* Status register */
205 if ((stat1 & 0xE0) != 0x00) {
206 return 0; /* Should be 0x00 */
208 opl3_command(ioaddr, TIMER1_REGISTER, 0xff); /* Set timer1 to 0xff */
210 opl3_command(ioaddr, TIMER_CONTROL_REGISTER,
211 TIMER2_MASK | TIMER1_START); /* Unmask and start timer 1 */
214 DELAY(150); /* Now we have to delay at least 80 usec */
216 stat2 = inb(ioaddr); /* Read status after timers have expired */
222 /* Reset timers 1 and 2 */
223 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
224 /* Reset the IRQ of the FM chip */
225 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
227 if ((stat2 & 0xE0) != 0xc0) {
228 return 0; /* There is no YM3812 */
231 * There is a FM chicp in this address. Detect the type (OPL2 to
235 if (signature == 0x06) {/* OPL2 */
237 } else if (signature == 0x00) { /* OPL3 or OPL4 */
243 * Detect availability of OPL4 (_experimental_). Works
244 * propably only after a cold boot. In addition the OPL4 port
245 * of the chip may not be connected to the PC bus at all.
248 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
249 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
251 if ((tmp = inb(ioaddr)) == 0x02) { /* Have a OPL4 */
254 if (!0) { /* OPL4 port is free */ /* XXX check here lr970711 */
257 outb(ioaddr - 8, 0x02); /* Select OPL4 ID register */
259 tmp = inb(ioaddr - 7); /* Read it */
262 if (tmp == 0x20) { /* OPL4 should return 0x20 here */
265 outb(ioaddr - 8, 0xF8); /* Select OPL4 FM mixer control */
267 outb(ioaddr - 7, 0x1B); /* Write value */
272 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
275 for (i = 0; i < 9; i++)
276 opl3_command(ioaddr, KEYON_BLOCK + i, 0); /* Note off */
278 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
279 opl3_command(ioaddr, PERCUSSION_REGISTER, 0x00); /* Melodic mode. */
285 opl3_kill_note(int dev, int voice, int note, int velocity)
287 struct physical_voice_info *map;
289 if (voice < 0 || voice >= devc->nr_voice)
292 devc->v_alloc->map[voice] = 0;
294 map = &pv_map[devc->lv_map[voice]];
296 DEB(printf("Kill note %d\n", voice));
298 if (map->voice_mode == 0)
301 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num,
302 devc->voc[voice].keyon_byte & ~0x20);
304 devc->voc[voice].keyon_byte = 0;
305 devc->voc[voice].bender = 0;
306 devc->voc[voice].volume = 64;
307 devc->voc[voice].bender_range = 200; /* 200 cents = 2 semitones */
308 devc->voc[voice].orig_freq = 0;
309 devc->voc[voice].current_freq = 0;
310 devc->voc[voice].mode = 0;
320 #define UNDEFINED TOMTOM
321 #define DEFAULT TOMTOM
324 store_instr(int instr_no, struct sbi_instrument * instr)
327 if (instr->key !=FM_PATCH && (instr->key !=OPL3_PATCH || devc->model != 2))
328 printf("FM warning: Invalid patch format field (key) 0x%x\n",
330 bcopy((char *) instr, (char *) &(devc->i_map[instr_no]), sizeof(*instr));
336 opl3_set_instr(int dev, int voice, int instr_no)
338 if (voice < 0 || voice >= devc->nr_voice)
341 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
344 devc->act_i[voice] = &devc->i_map[instr_no];
349 * The next table looks magical, but it certainly is not. Its values have
350 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
351 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
352 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
353 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
354 * volume -8 it was implemented as a table because it is only 128 bytes and
355 * it saves a lot of log() calculations. (RH)
357 static char fm_volume_table[128] =
359 -64, -48, -40, -35, -32, -29, -27, -26,
360 -24, -23, -21, -20, -19, -18, -18, -17,
361 -16, -15, -15, -14, -13, -13, -12, -12,
362 -11, -11, -10, -10, -10, -9, -9, -8,
363 -8, -8, -7, -7, -7, -6, -6, -6,
364 -5, -5, -5, -5, -4, -4, -4, -4,
365 -3, -3, -3, -3, -2, -2, -2, -2,
366 -2, -1, -1, -1, -1, 0, 0, 0,
367 0, 0, 0, 1, 1, 1, 1, 1,
368 1, 2, 2, 2, 2, 2, 2, 2,
369 3, 3, 3, 3, 3, 3, 3, 4,
370 4, 4, 4, 4, 4, 4, 4, 5,
371 5, 5, 5, 5, 5, 5, 5, 5,
372 6, 6, 6, 6, 6, 6, 6, 6,
373 6, 7, 7, 7, 7, 7, 7, 7,
374 7, 7, 7, 8, 8, 8, 8, 8};
377 calc_vol(u_char *regbyte, int volume, int main_vol)
379 int level = (~*regbyte & 0x3f);
384 volume = (volume * main_vol) / 127;
387 level += fm_volume_table[volume];
389 RANGE (level, 0, 0x3f );
391 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
395 set_voice_volume(int voice, int volume, int main_vol)
397 u_char vol1, vol2, vol3, vol4;
398 struct sbi_instrument *instr;
399 struct physical_voice_info *map;
401 if (voice < 0 || voice >= devc->nr_voice)
404 map = &pv_map[devc->lv_map[voice]];
406 instr = devc->act_i[voice];
409 instr = &devc->i_map[0];
411 if (instr->channel < 0)
414 if (devc->voc[voice].mode == 0)
417 if (devc->voc[voice].mode == 2) {
419 vol1 = instr->operators[2];
420 vol2 = instr->operators[3];
422 if ((instr->operators[10] & 0x01)) {
423 calc_vol(&vol1, volume, main_vol);
425 calc_vol(&vol2, volume, main_vol);
427 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
428 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
429 } else { /* 4 OP voice */
432 vol1 = instr->operators[2];
433 vol2 = instr->operators[3];
434 vol3 = instr->operators[OFFS_4OP + 2];
435 vol4 = instr->operators[OFFS_4OP + 3];
438 * The connection method for 4 OP devc->voc is defined by the
439 * rightmost bits at the offsets 10 and 10+OFFS_4OP
442 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
444 switch (connection) {
446 calc_vol(&vol4, volume, main_vol);
450 calc_vol(&vol2, volume, main_vol);
451 calc_vol(&vol4, volume, main_vol);
455 calc_vol(&vol1, volume, main_vol);
456 calc_vol(&vol4, volume, main_vol);
460 calc_vol(&vol1, volume, main_vol);
461 calc_vol(&vol3, volume, main_vol);
462 calc_vol(&vol4, volume, main_vol);
468 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
469 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
470 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
471 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
476 opl3_start_note(int dev, int voice, int note, int volume)
479 int block, fnum, freq, voice_mode;
480 struct sbi_instrument *instr;
481 struct physical_voice_info *map;
483 if (voice < 0 || voice >= devc->nr_voice)
486 map = &pv_map[devc->lv_map[voice]];
488 if (map->voice_mode == 0)
491 if (note == 255) { /* Just change the volume */
492 set_voice_volume(voice, volume, devc->voc[voice].volume);
496 * Kill previous note before playing
498 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /* Carrier volume to min */
499 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /* Modulator volume to */
501 if (map->voice_mode == 4) {
502 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
503 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
505 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /* Note off */
507 instr = devc->act_i[voice];
510 instr = &devc->i_map[0];
512 if (instr->channel < 0) {
513 printf( "OPL3: Initializing voice %d with undefined instrument\n",
517 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
518 return 0; /* Cannot play */
520 voice_mode = map->voice_mode;
522 if (voice_mode == 4) {
525 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
526 voice_shift += map->voice_num;
528 if (instr->key != OPL3_PATCH) { /* Just 2 OP patch */
530 devc->cmask &= ~(1 << voice_shift);
532 devc->cmask |= (1 << voice_shift);
534 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
537 * Set Sound Characteristics
539 opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
540 opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
545 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
546 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
549 * Set Sustain/Release
551 opl3_command(map->ioaddr,SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
552 opl3_command(map->ioaddr,SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
557 opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
558 opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
561 * Set Feedback/Connection
563 fpc = instr->operators[10];
565 fpc |= 0x30; /* Ensure that at least one chn is enabled */
566 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
569 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
572 if (voice_mode == 4) {
575 * Set Sound Characteristics
577 opl3_command(map->ioaddr, AM_VIB + map->op[2],
578 instr->operators[OFFS_4OP + 0]);
579 opl3_command(map->ioaddr, AM_VIB + map->op[3],
580 instr->operators[OFFS_4OP + 1]);
585 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2],
586 instr->operators[OFFS_4OP + 4]);
587 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3],
588 instr->operators[OFFS_4OP + 5]);
591 * Set Sustain/Release
593 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2],
594 instr->operators[OFFS_4OP + 6]);
595 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3],
596 instr->operators[OFFS_4OP + 7]);
601 opl3_command(map->ioaddr, WAVE_SELECT + map->op[2],
602 instr->operators[OFFS_4OP + 8]);
603 opl3_command(map->ioaddr, WAVE_SELECT + map->op[3],
604 instr->operators[OFFS_4OP + 9]);
607 * Set Feedback/Connection
609 fpc = instr->operators[OFFS_4OP + 10];
611 fpc |= 0x30; /* Ensure that at least one chn is enabled */
612 opl3_command(map->ioaddr,FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
614 devc->voc[voice].mode = voice_mode;
616 set_voice_volume(voice, volume, devc->voc[voice].volume);
618 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
621 * Since the pitch bender may have been set before playing the note,
622 * we have to calculate the bending now.
625 freq = compute_finetune(devc->voc[voice].orig_freq,
626 devc->voc[voice].bender, devc->voc[voice].bender_range);
627 devc->voc[voice].current_freq = freq;
629 freq_to_fnum(freq, &block, &fnum);
635 data = fnum & 0xff; /* Least significant bits of fnumber */
636 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
638 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
639 devc->voc[voice].keyon_byte = data;
640 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
642 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
648 freq_to_fnum(int freq, int *block, int *fnum)
653 * Converts the note frequency to block and fnum values for the FM
657 * First try to compute the block -value (octave) where the note
672 } else if (f > 493) {
681 *fnum = freq * (1 << (20 - octave)) / 49716;
686 opl3_command(int io_addr, u_int addr, u_int val)
691 * The original 2-OP synth requires a quite long delay after writing
692 * to a register. The OPL-3 survives with just two INBs
695 outb(io_addr, (u_char) (addr & 0xff));
697 if (!devc->model != 2)
700 for (i = 0; i < 2; i++)
704 outb(io_addr + 0x100, (u_char) (val & 0xff));
706 outb(io_addr + 1, (u_char) (val & 0xff));
708 if (devc->model != 2)
711 for (i = 0; i < 2; i++)
720 for (i = 0; i < 18; i++)
723 for (i = 0; i < devc->nr_voice; i++) {
724 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
725 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
727 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
728 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
730 if (pv_map[devc->lv_map[i]].voice_mode == 4) {
731 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
732 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
734 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
735 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
737 opl3_kill_note(dev, i, 0, 64);
740 if (devc->model == 2) {
741 devc->v_alloc->max_voice = devc->nr_voice = 18;
743 for (i = 0; i < 18; i++)
744 pv_map[i].voice_mode = 2;
750 opl3_open(int dev, int mode)
758 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
759 devc->v_alloc->timestamp = 0;
761 for (i = 0; i < 18; i++) {
762 devc->v_alloc->map[i] = 0;
763 devc->v_alloc->alloc_times[i] = 0;
766 devc->cmask = 0x00; /* Just 2 OP mode */
767 if (devc->model == 2)
768 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
776 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
778 devc->fm_info.nr_drums = 0;
779 devc->fm_info.perc_mode = 0;
785 opl3_hw_control(int dev, u_char *event)
790 opl3_load_patch(int dev, int format, snd_rw_buf * addr,
791 int offs, int count, int pmgr_flag)
793 struct sbi_instrument ins;
795 if (count < sizeof(ins)) {
796 printf("FM Error: Patch record too short\n");
799 if (uiomove(&((char *) &ins)[offs], sizeof(ins) - offs, addr)) {
800 printf("sb: Bad copyin()!\n");
803 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
804 printf("FM Error: Invalid instrument number %d\n", ins.channel);
809 return store_instr(ins.channel, &ins);
813 opl3_panning(int dev, int voice, int pressure)
818 opl3_volume_method(int dev, int mode)
822 #define SET_VIBRATO(cell) { \
823 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
824 if (pressure > 110) \
825 tmp |= 0x40; /* Vibrato on */ \
826 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
829 opl3_aftertouch(int dev, int voice, int pressure)
832 struct sbi_instrument *instr;
833 struct physical_voice_info *map;
835 if (voice < 0 || voice >= devc->nr_voice)
838 map = &pv_map[devc->lv_map[voice]];
840 DEB(printf("Aftertouch %d\n", voice));
842 if (map->voice_mode == 0)
846 * Adjust the amount of vibrato depending the pressure
849 instr = devc->act_i[voice];
852 instr = &devc->i_map[0];
854 if (devc->voc[voice].mode == 4) {
855 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
857 switch (connection) {
880 * Not implemented yet
885 if ((instr->operators[10] & 0x01)) /* Additive synthesis */
893 bend_pitch(int dev, int voice, int value)
896 int block, fnum, freq;
897 struct physical_voice_info *map;
899 map = &pv_map[devc->lv_map[voice]];
901 if (map->voice_mode == 0)
904 devc->voc[voice].bender = value;
907 if (!(devc->voc[voice].keyon_byte & 0x20))
908 return; /* Not keyed on */
910 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range);
911 devc->voc[voice].current_freq = freq;
913 freq_to_fnum(freq, &block, &fnum);
915 data = fnum & 0xff; /* Least significant bits of fnumber */
916 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
918 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
919 /* KEYON|OCTAVE|MS bits of f-num */
920 devc->voc[voice].keyon_byte = data;
921 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
925 opl3_controller(int dev, int voice, int ctrl_num, int value)
927 if (voice < 0 || voice >= devc->nr_voice)
931 case CTRL_PITCH_BENDER:
932 bend_pitch(dev, voice, value);
935 case CTRL_PITCH_BENDER_RANGE:
936 devc->voc[voice].bender_range = value;
939 case CTL_MAIN_VOLUME:
940 devc->voc[voice].volume = value / 128;
946 opl3_patchmgr(int dev, struct patmgr_info * rec)
952 opl3_bender(int dev, int voice, int value)
954 if (voice < 0 || voice >= devc->nr_voice)
957 bend_pitch(dev, voice, value - 8192);
961 opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info * alloc)
963 int i, p, best, first, avail, best_time = 0x7fffffff;
964 struct sbi_instrument *instr;
968 if (chn < 0 || chn > 15)
971 instr_no = devc->chn_info[chn].pgm_num;
973 instr = &devc->i_map[instr_no];
974 if (instr->channel < 0 || /* Instrument not loaded */
975 devc->nr_voice != 12) /* Not in 4 OP mode */
977 else if (devc->nr_voice == 12) /* 4 OP mode */
978 is4op = (instr->key == OPL3_PATCH);
986 if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP
987 * only' operators first */
991 avail = devc->nr_voice;
995 * Now try to find a free voice
999 for (i = 0; i < avail; i++) {
1000 if (alloc->map[p] == 0) {
1003 if (alloc->alloc_times[p] < best_time) { /* Find oldest playing note */
1004 best_time = alloc->alloc_times[p];
1007 p = (p + 1) % avail;
1011 * Insert some kind of priority mechanism here.
1016 if (best > devc->nr_voice)
1017 best -= devc->nr_voice;
1019 return best; /* All devc->voc in use. Select the first
1024 opl3_setup_voice(int dev, int voice, int chn)
1026 struct channel_info *info =
1027 &synth_devs[dev]->chn_info[chn];
1029 opl3_set_instr(dev, voice, info->pgm_num);
1031 devc->voc[voice].bender = info->bender_value;
1032 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1035 static struct synth_operations opl3_operations =
1061 opl3_init(int ioaddr, sound_os_info * osp)
1065 if (num_synths >= MAX_SYNTH_DEV) {
1066 printf("OPL3 Error: Too many synthesizers\n");
1070 printf("OPL3: Device control structure not initialized.\n");
1073 bzero((char *) devc, sizeof(*devc));
1077 strcpy(devc->fm_info.name, "OPL2-");
1079 devc->fm_info.device = 0;
1080 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1081 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1082 devc->fm_info.perc_mode = 0;
1083 devc->fm_info.nr_voices = 9;
1084 devc->fm_info.nr_drums = 0;
1085 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1086 devc->fm_info.capabilities = 0;
1087 devc->left_io = ioaddr;
1088 devc->right_io = ioaddr + 2;
1090 if (detected_model <= 2)
1094 if (detected_model == 4)
1098 opl3_operations.info = &devc->fm_info;
1100 synth_devs[num_synths++] = &opl3_operations;
1101 devc->v_alloc = &opl3_operations.alloc;
1102 devc->chn_info = &opl3_operations.chn_info[0];
1104 if (devc->model == 2) {
1106 conf_printf2("Yamaha OPL4/OPL3 FM", ioaddr, 0, -1, -1);
1108 conf_printf2("Yamaha OPL3 FM", ioaddr, 0, -1, -1);
1110 devc->v_alloc->max_voice = devc->nr_voice = 18;
1111 devc->fm_info.nr_drums = 0;
1112 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1113 strcpy(devc->fm_info.name, "Yamaha OPL-3");
1115 for (i = 0; i < 18; i++)
1116 if (pv_map[i].ioaddr == USE_LEFT)
1117 pv_map[i].ioaddr = devc->left_io;
1119 pv_map[i].ioaddr = devc->right_io;
1121 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1122 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1124 conf_printf2("Yamaha OPL2 FM", ioaddr, 0, -1, -1);
1125 devc->v_alloc->max_voice = devc->nr_voice = 9;
1126 devc->fm_info.nr_drums = 0;
1128 for (i = 0; i < 18; i++)
1129 pv_map[i].ioaddr = devc->left_io;
1132 for (i = 0; i < SBFM_MAXINSTR; i++)
1133 devc->i_map[i].channel = -1;