opm.c revision 1.2.12.1 1 /* $NetBSD: opm.c,v 1.2.12.1 1997/10/14 10:20:30 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1995 Masanobu Saitoh, Takuya Harakawa.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Masanobu Saitoh.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "fd.h"
36 /*#include "bsdaudio.h"*/
37 #include "bell.h"
38
39 #if ((NBSDAUDIO > 0) || (NFD > 0) || (NBELL > 0))
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <x68k/dev/opmreg.h>
44 #include <x68k/dev/bsd_audioreg.h>
45 #include <x68k/x68k/iodevice.h>
46
47 static u_char opmreg[0x100];
48 static struct opm_voice vdata[8];
49
50 void opm_set_volume __P((int, int));
51 void opm_set_key __P((int, int));
52 void opm_set_voice __P((int, struct opm_voice *));
53 void opm_set_voice_sub __P((int, struct opm_operator *));
54 __inline static void writeopm __P((int, int));
55 __inline static int readopm __P((int));
56 void opm_key_on __P((u_char));
57 void opm_key_off __P((u_char));
58 int opmopen __P((dev_t, int, int));
59 int opmclose __P((dev_t));
60
61 __inline static void
62 writeopm(reg, dat)
63 int reg, dat;
64 {
65 while (OPM.data & 0x80);
66 OPM.reg = reg;
67 while (OPM.data & 0x80);
68 OPM.data = opmreg[reg] = dat;
69 }
70
71 __inline static int
72 readopm(reg)
73 int reg;
74 {
75 return opmreg[reg];
76 }
77
78 void
79 adpcm_chgclk(clk)
80 u_char clk;
81 {
82 writeopm(0x1b, readopm(0x1b) & ~OPM1B_CT1MSK | clk);
83 }
84
85 void
86 fdc_force_ready(rdy)
87 u_char rdy;
88 {
89 writeopm(0x1b, readopm(0x1b) & ~OPM1B_CT2MSK | rdy);
90 }
91
92 void
93 opm_key_on(channel)
94 u_char channel;
95 {
96 writeopm(0x08, vdata[channel].sm << 3 | channel);
97 }
98
99 void
100 opm_key_off(channel)
101 u_char channel;
102 {
103 writeopm(0x08, channel);
104 }
105
106 void
107 opm_set_voice(channel, voice)
108 int channel;
109 struct opm_voice *voice;
110 {
111 bcopy(voice, &vdata[channel], sizeof(struct opm_voice));
112
113 opm_set_voice_sub(0x40 + channel, &voice->m1);
114 opm_set_voice_sub(0x48 + channel, &voice->m2);
115 opm_set_voice_sub(0x50 + channel, &voice->c1);
116 opm_set_voice_sub(0x58 + channel, &voice->c2);
117 writeopm(0x20 + channel, 0xc0 | (voice->fb & 0x7) << 3 | (voice->con & 0x7));
118 }
119
120 void
121 opm_set_voice_sub(reg, op)
122 register int reg;
123 struct opm_operator *op;
124 {
125 /* DT1/MUL */
126 writeopm(reg, (op->dt1 & 0x7) << 3 | (op->mul & 0x7));
127
128 /* TL */
129 writeopm(reg + 0x20, op->tl & 0x7f);
130
131 /* KS/AR */
132 writeopm(reg + 0x40, (op->ks & 0x3) << 6 | (op->ar & 0x1f));
133
134 /* AMS/D1R */
135 writeopm(reg + 0x60, (op->ame & 0x1) << 7 | (op->d1r & 0x1f));
136
137 /* DT2/D2R */
138 writeopm(reg + 0x80, (op->dt2 & 0x3) << 6 | (op->d2r & 0x1f));
139
140 /* D1L/RR */
141 writeopm(reg + 0xa0, (op->d1l & 0xf) << 4 | (op->rr & 0xf));
142 }
143
144 void
145 opm_set_volume(channel, volume)
146 int channel;
147 int volume;
148 {
149 int value;
150
151 switch (vdata[channel].con) {
152 case 7:
153 value = vdata[channel].m1.tl + volume;
154 writeopm(0x60 + channel, ((value > 0x7f) ? 0x7f : value));
155 case 6:
156 case 5:
157 value = vdata[channel].m2.tl + volume;
158 writeopm(0x68 + channel, ((value > 0x7f) ? 0x7f : value));
159 case 4:
160 value = vdata[channel].c1.tl + volume;
161 writeopm(0x70 + channel, ((value > 0x7f) ? 0x7f : value));
162 case 3:
163 case 2:
164 case 1:
165 case 0:
166 value = vdata[channel].c2.tl + volume;
167 writeopm(0x78 + channel, ((value > 0x7f) ? 0x7f : value));
168 }
169 }
170
171 void
172 opm_set_key(channel, tone)
173 int channel;
174 int tone;
175 {
176 writeopm(0x28 + channel, tone >> 8);
177 writeopm(0x30 + channel, tone & 0xff);
178 }
179
180 /*ARGSUSED*/
181 int
182 opmopen(dev, flag, mode)
183 dev_t dev;
184 int flag, mode;
185 {
186 return 0;
187 }
188
189 /*ARGSUSED*/
190 int
191 opmclose(dev)
192 dev_t dev;
193 {
194 return 0;
195 }
196
197 #endif
198