aica_arm.c revision 1.7 1 1.7 ryo /* $NetBSD: aica_arm.c,v 1.7 2022/09/25 21:19:20 ryo Exp $ */
2 1.1 marcus
3 1.1 marcus /*
4 1.1 marcus * Copyright (c) 2003 SHIMIZU Ryo <ryo (at) misakimix.org>
5 1.1 marcus * All rights reserved.
6 1.1 marcus *
7 1.1 marcus * Redistribution and use in source and binary forms, with or without
8 1.1 marcus * modification, are permitted provided that the following conditions
9 1.1 marcus * are met:
10 1.1 marcus *
11 1.1 marcus * 1. Redistributions of source code must retain the above copyright
12 1.1 marcus * notice, this list of conditions and the following disclaimer.
13 1.1 marcus * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 marcus * notice, this list of conditions and the following disclaimer in the
15 1.1 marcus * documentation and/or other materials provided with the distribution.
16 1.1 marcus * 3. The name of the author may not be used to endorse or promote products
17 1.1 marcus * derived from this software without specific prior written permission.
18 1.1 marcus *
19 1.1 marcus * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 marcus * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 marcus * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 marcus * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 marcus * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 marcus * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 marcus * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 marcus * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 marcus * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 1.1 marcus * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 marcus */
30 1.1 marcus
31 1.2 tsutsui typedef unsigned char uint8_t;
32 1.2 tsutsui typedef unsigned short uint16_t;
33 1.2 tsutsui typedef unsigned long uint32_t;
34 1.1 marcus
35 1.1 marcus #include <arch/dreamcast/dev/g2/aicavar.h>
36 1.1 marcus
37 1.1 marcus #define DC_REG_ADDR 0x00800000
38 1.1 marcus
39 1.1 marcus #define REG_READ_1(off) \
40 1.2 tsutsui (*(volatile uint8_t *)(DC_REG_ADDR + (off)))
41 1.1 marcus #define REG_READ_2(off) \
42 1.2 tsutsui (*(volatile uint16_t *)(DC_REG_ADDR + (off)))
43 1.1 marcus #define REG_READ_4(off) \
44 1.2 tsutsui (*(volatile uint32_t *)(DC_REG_ADDR + (off)))
45 1.1 marcus #define REG_WRITE_1(off,val) \
46 1.2 tsutsui ((*(volatile uint8_t *)(DC_REG_ADDR + (off))) = (val))
47 1.1 marcus #define REG_WRITE_2(off,val) \
48 1.2 tsutsui ((*(volatile uint16_t *)(DC_REG_ADDR + (off))) = (val))
49 1.1 marcus #define REG_WRITE_4(off,val) \
50 1.2 tsutsui ((*(volatile uint32_t *)((DC_REG_ADDR)+(off))) = (val))
51 1.1 marcus
52 1.1 marcus #define CH_READ_1(ch,off) REG_READ_1(((ch) << 7) + (off))
53 1.1 marcus #define CH_READ_2(ch,off) REG_READ_2(((ch) << 7) + (off))
54 1.1 marcus #define CH_READ_4(ch,off) REG_READ_4(((ch) << 7) + (off))
55 1.1 marcus #define CH_WRITE_1(ch,off,val) REG_WRITE_1(((ch) << 7) + (off), val)
56 1.1 marcus #define CH_WRITE_2(ch,off,val) REG_WRITE_2(((ch) << 7) + (off), val)
57 1.1 marcus #define CH_WRITE_4(ch,off,val) REG_WRITE_4(((ch) << 7) + (off), val)
58 1.1 marcus
59 1.7 ryo void *memset(void *, int, unsigned long);
60 1.7 ryo
61 1.1 marcus void aica_init(void);
62 1.4 perry inline int in_first_half(unsigned int);
63 1.4 perry inline int in_second_half(unsigned int);
64 1.2 tsutsui uint32_t rate2reg(unsigned int);
65 1.1 marcus void aica_stop(void);
66 1.1 marcus void aica_main(void);
67 1.1 marcus
68 1.1 marcus void
69 1.5 cegger aica_init(void)
70 1.1 marcus {
71 1.1 marcus int ch, off;
72 1.1 marcus
73 1.1 marcus /* Initialize AICA channels */
74 1.1 marcus REG_WRITE_4(0x2800, 0x0000); /* Master volume: Min */
75 1.1 marcus
76 1.1 marcus for (ch = 0; ch < 64; ch++) {
77 1.1 marcus CH_WRITE_4(ch, 0x00, 0x8000); /* Key off */
78 1.1 marcus CH_WRITE_4(ch, 0x04, 0x0000); /* DataAddress (low) */
79 1.1 marcus CH_WRITE_4(ch, 0x08, 0x0000); /* LoopStartPosition */
80 1.1 marcus CH_WRITE_4(ch, 0x0c, 0x0000); /* LoopEndPosition */
81 1.1 marcus CH_WRITE_4(ch, 0x10, 0x001f); /* AR = 0x1f = 0 msec */
82 1.1 marcus CH_WRITE_4(ch, 0x14, 0x001f); /* RR = 0x1f = 0 msec */
83 1.1 marcus CH_WRITE_4(ch, 0x18, 0x0000); /* Pitch */
84 1.1 marcus CH_WRITE_4(ch, 0x1c, 0x0000); /* LFO Control */
85 1.1 marcus CH_WRITE_4(ch, 0x20, 0x0000); /* DSP Channel to send */
86 1.1 marcus CH_WRITE_4(ch, 0x24, 0x0000); /* Pan & Volume */
87 1.1 marcus CH_WRITE_4(ch, 0x28, 0x0024); /* Volume & LowPassFilter */
88 1.1 marcus CH_WRITE_4(ch, 0x2c, 0x0000); /* LowPassFilter for Attack */
89 1.1 marcus CH_WRITE_4(ch, 0x30, 0x0000); /* LowPassFilter for Decay */
90 1.1 marcus CH_WRITE_4(ch, 0x34, 0x0000); /* LowPassFilter for Sustain */
91 1.1 marcus CH_WRITE_4(ch, 0x38, 0x0000); /* LowPassFilter for Keyoff */
92 1.1 marcus CH_WRITE_4(ch, 0x3c, 0x0000); /* LowPassFilter for Release */
93 1.1 marcus CH_WRITE_4(ch, 0x40, 0x0000); /* LowPassFilter transition
94 1.1 marcus for Attack, Decay */
95 1.1 marcus CH_WRITE_4(ch, 0x44, 0x0000); /* LowPassFilter transition
96 1.1 marcus for Decay, Release */
97 1.1 marcus
98 1.1 marcus for (off = 0x48; off < 0x80; off+=4) {
99 1.1 marcus CH_WRITE_4(ch, off, 0x0000); /* other = 0 */
100 1.1 marcus }
101 1.1 marcus }
102 1.1 marcus
103 1.1 marcus REG_WRITE_4(0x2800, 0x000f); /* Master volume: Max */
104 1.1 marcus }
105 1.1 marcus
106 1.1 marcus
107 1.4 perry inline int
108 1.1 marcus in_first_half(unsigned int loophalf)
109 1.1 marcus {
110 1.1 marcus
111 1.1 marcus REG_WRITE_1(0x280d, 0); /* select channel 0 */
112 1.1 marcus return REG_READ_4(0x2814) < loophalf;
113 1.1 marcus }
114 1.1 marcus
115 1.4 perry inline int
116 1.1 marcus in_second_half(unsigned int loophalf)
117 1.1 marcus {
118 1.1 marcus
119 1.1 marcus REG_WRITE_1(0x280d, 0); /* select channel 0 */
120 1.1 marcus return REG_READ_4(0x2814) >= loophalf;
121 1.1 marcus }
122 1.1 marcus
123 1.2 tsutsui uint32_t
124 1.1 marcus rate2reg(unsigned int rate)
125 1.1 marcus {
126 1.2 tsutsui uint32_t base, fns;
127 1.1 marcus int oct;
128 1.1 marcus
129 1.1 marcus base = 44100 << 7;
130 1.1 marcus for (oct = 7; oct >= -8 && rate < base; oct--)
131 1.1 marcus base >>= 1;
132 1.1 marcus
133 1.1 marcus if (rate < base)
134 1.1 marcus return (oct << 11) & 0xf800;
135 1.1 marcus
136 1.1 marcus rate -= base;
137 1.1 marcus
138 1.1 marcus #if 0
139 1.1 marcus /* (base / 2) : round off */
140 1.1 marcus fns = (rate * 1024 + (base / 2)) / base;
141 1.1 marcus #else
142 1.1 marcus /* avoid using udivsi3() */
143 1.1 marcus {
144 1.2 tsutsui uint32_t tmp = (rate * 1024 + (base / 2));
145 1.1 marcus for (fns = 0; tmp >= base; tmp -= base, fns++)
146 1.1 marcus ;
147 1.1 marcus }
148 1.1 marcus #endif
149 1.1 marcus
150 1.1 marcus /* adjustment */
151 1.1 marcus if (fns == 1024) {
152 1.1 marcus oct++;
153 1.1 marcus fns = 0;
154 1.1 marcus } else {
155 1.1 marcus if ((rate > base * fns / 1024) &&
156 1.1 marcus (fns < 1023) &&
157 1.1 marcus (rate == base * (fns + 1) / 1024)) {
158 1.1 marcus fns++;
159 1.1 marcus } else if ((rate < base * fns / 1024) &&
160 1.1 marcus (fns > 0) &&
161 1.1 marcus (rate == base * (fns - 1)/ 1024)) {
162 1.1 marcus fns--;
163 1.1 marcus }
164 1.1 marcus }
165 1.1 marcus
166 1.1 marcus return ((oct << 11) & 0xf800) + fns;
167 1.1 marcus }
168 1.1 marcus
169 1.1 marcus
170 1.1 marcus
171 1.1 marcus void
172 1.5 cegger aica_stop(void)
173 1.1 marcus {
174 1.1 marcus
175 1.1 marcus CH_WRITE_4(0, 0x00, 0x8000);
176 1.1 marcus CH_WRITE_4(1, 0x00, 0x8000);
177 1.7 ryo memset((void *)AICA_DMABUF_LEFT, 0, AICA_DMABUF_SIZE);
178 1.7 ryo memset((void *)AICA_DMABUF_RIGHT, 0, AICA_DMABUF_SIZE);
179 1.1 marcus }
180 1.1 marcus
181 1.1 marcus void
182 1.5 cegger aica_main(void)
183 1.1 marcus {
184 1.1 marcus volatile aica_cmd_t *aicacmd = (volatile aica_cmd_t *)AICA_ARM_CMD;
185 1.1 marcus int play_state;
186 1.1 marcus unsigned int loopend = 0,loophalf = 0;
187 1.1 marcus unsigned int blksize = 0, ratepitch;
188 1.2 tsutsui uint32_t cmd, serial;
189 1.1 marcus
190 1.1 marcus aica_init();
191 1.1 marcus
192 1.1 marcus REG_WRITE_4(0x28b4, 0x0020); /* INT Enable to SH4 */
193 1.1 marcus
194 1.7 ryo memset((void *)AICA_DMABUF_LEFT, 0, AICA_DMABUF_SIZE);
195 1.7 ryo memset((void *)AICA_DMABUF_RIGHT, 0, AICA_DMABUF_SIZE);
196 1.1 marcus
197 1.1 marcus play_state = 0;
198 1.1 marcus serial = aicacmd->serial = 0;
199 1.1 marcus
200 1.1 marcus for (;;) {
201 1.1 marcus if (serial != aicacmd->serial) {
202 1.1 marcus serial = aicacmd->serial;
203 1.1 marcus cmd = aicacmd->command;
204 1.1 marcus aicacmd->command = AICA_COMMAND_NOP;
205 1.1 marcus } else {
206 1.1 marcus cmd = AICA_COMMAND_NOP;
207 1.1 marcus }
208 1.1 marcus
209 1.1 marcus switch (cmd) {
210 1.1 marcus case AICA_COMMAND_NOP:
211 1.1 marcus /*
212 1.1 marcus * AICA_COMMAND_NOP - Idle process
213 1.1 marcus */
214 1.1 marcus switch (play_state) {
215 1.1 marcus case 0: /* not playing */
216 1.1 marcus break;
217 1.1 marcus case 1: /* first half */
218 1.1 marcus if (in_second_half(loophalf)) {
219 1.1 marcus /* Send INT to SH4 */
220 1.1 marcus REG_WRITE_4(0x28b8, 0x0020);
221 1.1 marcus play_state = 2;
222 1.1 marcus }
223 1.1 marcus break;
224 1.1 marcus case 2: /* second half */
225 1.1 marcus if (in_first_half(loophalf)) {
226 1.1 marcus /* Send INT to SH4 */
227 1.1 marcus REG_WRITE_4(0x28b8, 0x0020);
228 1.1 marcus play_state = 1;
229 1.1 marcus }
230 1.1 marcus break;
231 1.1 marcus case 3:
232 1.1 marcus if (in_second_half(loophalf)) {
233 1.1 marcus aica_stop();
234 1.1 marcus play_state = 0;
235 1.1 marcus }
236 1.1 marcus break;
237 1.1 marcus case 4:
238 1.1 marcus if (in_first_half(loophalf)) {
239 1.1 marcus aica_stop();
240 1.1 marcus play_state = 0;
241 1.1 marcus }
242 1.1 marcus break;
243 1.1 marcus }
244 1.1 marcus break;
245 1.1 marcus
246 1.1 marcus case AICA_COMMAND_PLAY:
247 1.6 ryo aica_stop();
248 1.6 ryo play_state = 0;
249 1.6 ryo
250 1.1 marcus blksize = aicacmd->blocksize;
251 1.1 marcus
252 1.6 ryo REG_WRITE_4(0x28b4, 0x0020); /* INT Enable to SH4 */
253 1.6 ryo
254 1.1 marcus CH_WRITE_4(0, 0x00, 0x8000);
255 1.1 marcus CH_WRITE_4(1, 0x00, 0x8000);
256 1.1 marcus
257 1.1 marcus switch (aicacmd->precision) {
258 1.1 marcus case 16:
259 1.1 marcus loopend = blksize;
260 1.1 marcus break;
261 1.1 marcus case 8:
262 1.1 marcus loopend = blksize * 2;
263 1.1 marcus break;
264 1.1 marcus case 4:
265 1.1 marcus loopend = blksize * 4;
266 1.1 marcus break;
267 1.1 marcus }
268 1.1 marcus loophalf = loopend / 2;
269 1.1 marcus
270 1.1 marcus ratepitch = rate2reg(aicacmd->rate);
271 1.1 marcus
272 1.1 marcus /* setup left */
273 1.1 marcus CH_WRITE_4(0, 0x08, 0); /* loop start */
274 1.1 marcus CH_WRITE_4(0, 0x0c, loopend); /* loop end */
275 1.1 marcus CH_WRITE_4(0, 0x18, ratepitch); /* SamplingRate */
276 1.1 marcus CH_WRITE_4(0, 0x24, 0x0f1f); /* volume MAX,
277 1.1 marcus right PAN */
278 1.1 marcus
279 1.1 marcus /* setup right */
280 1.1 marcus CH_WRITE_4(1, 0x08,0); /* loop start */
281 1.1 marcus CH_WRITE_4(1, 0x0c, loopend); /* loop end */
282 1.1 marcus CH_WRITE_4(1, 0x18, ratepitch); /* SamplingRate */
283 1.1 marcus CH_WRITE_4(1, 0x24, 0x0f0f); /* volume MAX,
284 1.1 marcus right PAN */
285 1.1 marcus
286 1.1 marcus {
287 1.2 tsutsui uint32_t mode, lparam, rparam;
288 1.1 marcus
289 1.1 marcus if (aicacmd->precision == 4)
290 1.1 marcus mode = 3 << 7; /* 4bit ADPCM */
291 1.1 marcus else if (aicacmd->precision == 8)
292 1.1 marcus mode = 1 << 7; /* 8bit */
293 1.1 marcus else
294 1.1 marcus mode = 0; /* 16bit */
295 1.1 marcus
296 1.1 marcus switch (aicacmd->channel) {
297 1.1 marcus case 2:
298 1.1 marcus CH_WRITE_4(0, 0x04,
299 1.1 marcus AICA_DMABUF_LEFT & 0xffff);
300 1.1 marcus CH_WRITE_4(1, 0x04,
301 1.1 marcus AICA_DMABUF_RIGHT & 0xffff);
302 1.1 marcus lparam = 0xc000 /*PLAY*/ |
303 1.1 marcus 0x0200 /*LOOP*/ | mode |
304 1.1 marcus (AICA_DMABUF_LEFT >> 16);
305 1.1 marcus rparam = 0xc000 /*PLAY*/ |
306 1.1 marcus 0x0200 /*LOOP*/ | mode |
307 1.1 marcus (AICA_DMABUF_RIGHT >> 16);
308 1.1 marcus CH_WRITE_4(0, 0x00, lparam);
309 1.1 marcus CH_WRITE_4(1, 0x00, rparam);
310 1.1 marcus break;
311 1.1 marcus case 1:
312 1.1 marcus CH_WRITE_1(0, 0x24, 0); /* middle
313 1.1 marcus balance */
314 1.1 marcus CH_WRITE_4(0, 0x04,
315 1.1 marcus AICA_DMABUF_LEFT & 0xffff);
316 1.1 marcus CH_WRITE_4(0, 0x00, 0xc000 /*PLAY*/ |
317 1.1 marcus 0x0200 /*LOOP*/ | mode |
318 1.1 marcus (AICA_DMABUF_LEFT >> 16));
319 1.1 marcus break;
320 1.1 marcus }
321 1.1 marcus }
322 1.1 marcus play_state = 1;
323 1.1 marcus break;
324 1.1 marcus
325 1.1 marcus case AICA_COMMAND_STOP:
326 1.1 marcus switch (play_state) {
327 1.1 marcus case 1:
328 1.7 ryo memset((void *)(AICA_DMABUF_LEFT + blksize), 0,
329 1.1 marcus blksize);
330 1.7 ryo memset((void *)(AICA_DMABUF_RIGHT + blksize), 0,
331 1.1 marcus blksize);
332 1.1 marcus play_state = 3;
333 1.1 marcus break;
334 1.1 marcus case 2:
335 1.7 ryo memset((void *)AICA_DMABUF_LEFT, 0, blksize);
336 1.7 ryo memset((void *)AICA_DMABUF_RIGHT, 0, blksize);
337 1.1 marcus play_state = 4;
338 1.1 marcus break;
339 1.1 marcus default:
340 1.1 marcus aica_stop();
341 1.1 marcus play_state = 0;
342 1.1 marcus break;
343 1.1 marcus }
344 1.1 marcus break;
345 1.1 marcus
346 1.1 marcus case AICA_COMMAND_INIT:
347 1.1 marcus aica_stop();
348 1.1 marcus play_state = 0;
349 1.1 marcus break;
350 1.1 marcus
351 1.1 marcus case AICA_COMMAND_MVOL:
352 1.1 marcus REG_WRITE_4(0x2800, L256TO16(aicacmd->l_param));
353 1.1 marcus break;
354 1.1 marcus
355 1.1 marcus case AICA_COMMAND_VOL:
356 1.1 marcus CH_WRITE_1(0, 0x29, 0xff - (aicacmd->l_param & 0xff));
357 1.1 marcus CH_WRITE_1(1, 0x29, 0xff - (aicacmd->r_param & 0xff));
358 1.1 marcus break;
359 1.1 marcus
360 1.1 marcus }
361 1.1 marcus }
362 1.1 marcus }
363