44 * (c)2000 Dan Potter
55 * modify BERO
66 * modified by Lawrence Sebald <bluecrab2887@netscape.net>
7+ * modified by SWAT 2024
78 */
89#include "aica.h"
910#include <kos.h>
2223#define CHNREG32 (ch , x ) (*(volatile unsigned long *)CHNREGADDR(ch,x))
2324#define CHNREG8 (ch , x ) (*(volatile unsigned long *)CHNREGADDR(ch,x))
2425
25- #define G2_LOCK (OLD ) \
26- do { \
27- if (!irq_inside_int()) \
28- OLD = irq_disable(); \
29- irq_enable(); \
30- /* suspend any G2 DMA here... */ \
31- while ((* (volatile unsigned int * )0xa05f688c ) & 0x20 ) \
32- ; \
33- } while (0 )
34-
35- #define G2_UNLOCK (OLD ) \
36- do { \
37- /* resume any G2 DMA here... */ \
38- if (!irq_inside_int()) \
39- irq_restore(OLD); \
40- } while(0)
41-
42-
4326void aica_init () {
44- int i , j , old = 0 ;
27+ int i , j ;
28+ g2_ctx_t ctx ;
4529
4630 /* Initialize AICA channels */
4731 g2_fifo_wait ();
48- G2_LOCK (old );
32+
33+ ctx = g2_lock ();
4934 SNDREG32 (0x2800 ) = 0x0000 ;
5035
5136 for (i = 0 ; i < 64 ; i ++ ) {
@@ -59,33 +44,9 @@ void aica_init() {
5944 }
6045
6146 SNDREG32 (0x2800 ) = 0x000f ;
62- G2_UNLOCK ( old );
47+ g2_unlock ( ctx );
6348}
6449
65- /* Translates a volume from linear form to logarithmic form (required by
66- the AICA chip */
67- /* int logs[] = {
68-
69- 0, 40, 50, 58, 63, 68, 73, 77, 80, 83, 86, 89, 92, 94, 97, 99, 101, 103,
70- 105, 107, 109, 111, 112, 114, 116, 117, 119, 120, 122, 123, 125, 126, 127,
71- 129, 130, 131, 133, 134, 135, 136, 137, 139, 140, 141, 142, 143, 144, 145,
72- 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159,
73- 160, 161, 162, 162, 163, 164, 165, 166, 166, 167, 168, 169, 170, 170, 171,
74- 172, 172, 173, 174, 175, 175, 176, 177, 177, 178, 179, 180, 180, 181, 182,
75- 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 188, 189, 190, 190, 191,
76- 191, 192, 193, 193, 194, 194, 195, 196, 196, 197, 197, 198, 198, 199, 199,
77- 200, 201, 201, 202, 202, 203, 203, 204, 204, 205, 205, 206, 206, 207, 207,
78- 208, 208, 209, 209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215,
79- 215, 216, 216, 217, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 222,
80- 222, 222, 223, 223, 224, 224, 225, 225, 225, 226, 226, 227, 227, 228, 228,
81- 228, 229, 229, 230, 230, 230, 231, 231, 232, 232, 232, 233, 233, 234, 234,
82- 234, 235, 235, 236, 236, 236, 237, 237, 238, 238, 238, 239, 239, 240, 240,
83- 240, 241, 241, 241, 242, 242, 243, 243, 243, 244, 244, 244, 245, 245, 245,
84- 246, 246, 247, 247, 247, 248, 248, 248, 249, 249, 249, 250, 250, 250, 251,
85- 251, 251, 252, 252, 252, 253, 253, 253, 254, 254, 254, 255
86-
87- }; */
88-
8950const static unsigned char logs [] = {
9051 0 , 15 , 22 , 27 , 31 , 35 , 39 , 42 , 45 , 47 , 50 , 52 , 55 , 57 , 59 , 61 ,
9152 63 , 65 , 67 , 69 , 71 , 73 , 74 , 76 , 78 , 79 , 81 , 82 , 84 , 85 , 87 , 88 ,
@@ -112,7 +73,6 @@ const static unsigned char logs[] = {
11273 figure out what these mean. */
11374#define AICA_PAN (x ) ((x)==0x80?(0):((x)<0x80?(0x1f):(0x0f)))
11475#define AICA_VOL (x ) (0xff - logs[128 + (((x) & 0xff) / 2)])
115- //#define AICA_VOL(x) (0xff - logs[x&255])
11676
11777static inline unsigned AICA_FREQ (unsigned freq ) {
11878 unsigned long freq_lo , freq_base = 5644800 ;
@@ -150,23 +110,17 @@ static inline unsigned AICA_FREQ(unsigned freq) {
150110 This routine (and the similar ones) owe a lot to Marcus' sound example --
151111 I hadn't gotten quite this far into dissecting the individual regs yet. */
152112void aica_play (int ch ,int mode ,unsigned long smpptr ,int loopst ,int loopend ,int freq ,int vol ,int pan ,int loopflag ) {
153- // int i;
113+
154114 int val ;
155- int old = 0 ;
115+ g2_ctx_t ctx ;
156116
157117 /* Stop the channel (if it's already playing) */
158118 aica_stop (ch );
159- /* doesn't seem to be needed, but it's here just in case */
160- /*
161- for (i=0; i<256; i++) {
162- asm("nop");
163- asm("nop");
164- asm("nop");
165- asm("nop");
166- }
167- */
168- g2_fifo_wait ();
169- G2_LOCK (old );
119+
120+ /* AICA channels stops with delay */
121+ timer_spin_sleep (1 );
122+
123+ ctx = g2_lock ();
170124 /* Envelope setup. The first of these is the loop point,
171125 e.g., where the sample starts over when it loops. The second
172126 is the loop end. This is the full length of the sample when
@@ -198,36 +152,29 @@ void aica_play(int ch,int mode,unsigned long smpptr,int loopst,int loopend,int f
198152 */
199153 CHNREG32 (ch , 16 ) = 0x1f ; /* No volume envelope */
200154
201-
202155 /* Set sample format, buffer address, and looping control. If
203156 0x0200 mask is set on reg 0, the sample loops infinitely. If
204157 it's not set, the sample plays once and terminates. We'll
205158 also set the bits to start playback here. */
206159 CHNREG32 (ch , 4 ) = smpptr & 0xffff ;
207160 val = 0xc000 | 0x0000 | (mode <<7 ) | (smpptr >> 16 );
208161 if (loopflag ) val |=0x200 ;
209-
162+
210163 CHNREG32 (ch , 0 ) = val ;
211-
212- G2_UNLOCK ( old );
164+
165+ g2_unlock ( ctx );
213166
214167 /* Enable playback */
215168 /* CHNREG32(ch, 0) |= 0xc000; */
216169
217- #if 0
218- for (i = 0xff ; i >=vol ; i -- ) {
219- if ((i & 7 )== 0 ) g2_fifo_wait ();
220- CHNREG32 (ch , 40 ) = 0x24 | (i <<8 );;
221- }
222-
223- g2_fifo_wait ();
224- #endif
225170}
226171
227172/* Stop the sound on a given channel */
228173void aica_stop (int ch ) {
229174 g2_fifo_wait ();
230175 g2_write_32 (CHNREGADDR (ch , 0 ),(g2_read_32 (CHNREGADDR (ch , 0 )) & ~0x4000 ) | 0x8000 );
176+ /* AICA channels stops with delay */
177+ timer_spin_sleep (1 );
231178}
232179
233180
@@ -256,17 +203,10 @@ void aica_freq(int ch,int freq) {
256203
257204/* Get channel position */
258205int aica_get_pos (int ch ) {
259- #if 1
260206 /* Observe channel ch */
261207 g2_fifo_wait ();
262208 g2_write_32 (SNDREGADDR (0x280c ),(g2_read_32 (SNDREGADDR (0x280c ))& 0xffff00ff ) | (ch <<8 ));
263209 g2_fifo_wait ();
264210 /* Update position counters */
265211 return g2_read_32 (SNDREGADDR (0x2814 )) & 0xffff ;
266- #else
267- /* Observe channel ch */
268- g2_write_8 (SNDREGADDR (0x280d ),ch );
269- /* Update position counters */
270- return g2_read_32 (SNDREGADDR (0x2814 )) & 0xffff ;
271- #endif
272212}
0 commit comments