|
|
1.1 root 1: /*
2: Hatari - dmaSnd.c
3:
1.1.1.11! root 4: This file is distributed under the GNU General Public License, version 2
! 5: or at your option any later version. Read the file gpl.txt for details.
1.1 root 6:
7: STE DMA sound emulation. Does not seem to be very hard at first glance,
8: but since the DMA sound has to be mixed together with the PSG sound and
9: the output frequency of the host computer differs from the DMA sound
10: frequency, the copy function is a little bit complicated.
1.1.1.7 root 11: The update function also triggers the ST interrupts (Timer A and MFP-i7)
12: which are often used in ST programs for setting a new sound frame after
13: the old one has finished.
14:
1.1.1.8 root 15: To support programs that write into the frame buffer while it's played,
16: we should update dma sound on each video HBL.
17: This is also how it works on a real STE : bytes are read by the DMA
18: at the end of each HBL and stored in a small FIFO (8 bytes) that is sent
19: to the DAC depending on the chosen DMA output freq.
20:
1.1.1.7 root 21: Falcon sound emulation is all taken into account in crossbar.c
1.1 root 22:
23:
24: Hardware I/O registers:
25:
26: $FF8900 (word) : DMA sound control register
27: $FF8903 (byte) : Frame Start Hi
28: $FF8905 (byte) : Frame Start Mi
29: $FF8907 (byte) : Frame Start Lo
30: $FF8909 (byte) : Frame Count Hi
31: $FF890B (byte) : Frame Count Mi
32: $FF890D (byte) : Frame Count Lo
33: $FF890F (byte) : Frame End Hi
34: $FF8911 (byte) : Frame End Mi
35: $FF8913 (byte) : Frame End Lo
36: $FF8920 (word) : Sound Mode Control (frequency, mono/stereo)
37: $FF8922 (byte) : Microwire Data Register
38: $FF8924 (byte) : Microwire Mask Register
1.1.1.7 root 39:
40:
41: The Microwire and LMC 1992 commands :
42:
43: a command looks like: 10 CCC DDD DDD
44:
45: chipset address : 10
46: command :
47: 000 XXX XDD Mixing
48: 00 : DMA and (YM2149 - 12dB) mixing
49: 01 : DMA and YM2149 mixing
50: 10 : DMA only
51: 11 : Reserved
52:
53: 001 XXD DDD Bass
54: 0 000 : -12 dB
55: 0 110 : 0 dB
56: 1 100 : +12 dB
57:
58: 002 XXD DDD Treble
59: 0 000 : -12 dB
60: 0 110 : 0 dB
61: 1 100 : +12 dB
62:
63: 003 DDD DDD Master volume
64: 000 000 : -80 dB
65: 010 100 : -40 dB
66: 101 XXX : 0 dB
67:
68: 004 XDD DDD Right channel volume
69: 00 000 : -40 dB
70: 01 010 : -20 dB
71: 10 1XX : 0 dB
72:
73: 005 XDD DDD Left channel volume
74: 00 000 : -40 dB
75: 01 010 : -20 dB
76: 10 1XX : 0 dB
77:
78: Other : undefined
79:
80: LMC1992 IIR code Copyright by David Savinkoff 2010
81:
82: A first order bass filter is multiplied with a
83: first order treble filter to make a single
84: second order IIR shelf filter.
85:
86: Sound is stereo filtered by Boosting or Cutting
87: the Bass and Treble by +/-12dB in 2dB steps.
88:
89: This filter sounds exactly as the Atari TT or STE.
90: Sampling frequency = selectable
91: Bass turnover = 118.276Hz (8.2nF on LM1992 bass)
92: Treble turnover = 8438.756Hz (8.2nF on LM1992 treble)
1.1 root 93: */
1.1.1.7 root 94:
95:
1.1.1.5 root 96: const char DmaSnd_fileid[] = "Hatari dmaSnd.c : " __DATE__ " " __TIME__;
1.1 root 97:
98: #include "main.h"
99: #include "audio.h"
1.1.1.3 root 100: #include "configuration.h"
1.1 root 101: #include "dmaSnd.h"
1.1.1.7 root 102: #include "cycInt.h"
1.1 root 103: #include "ioMem.h"
1.1.1.6 root 104: #include "log.h"
1.1 root 105: #include "memorySnapShot.h"
106: #include "mfp.h"
107: #include "sound.h"
1.1.1.3 root 108: #include "stMemory.h"
1.1.1.10 root 109: #include "crossbar.h"
1.1.1.11! root 110: #include "screen.h"
! 111: #include "video.h"
! 112: #include "m68000.h"
1.1.1.7 root 113:
114: #define TONE_STEPS 13
115:
1.1.1.8 root 116: #define DMASND_FIFO_SIZE 8 /* 8 bytes : size of the DMA Audio's FIFO, filled on every HBL */
117: #define DMASND_FIFO_SIZE_MASK (DMASND_FIFO_SIZE-1) /* mask to keep FIFO_pos in 0-7 range */
118:
1.1.1.7 root 119:
120: /* Global variables that can be changed/read from other parts of Hatari */
121:
1.1.1.8 root 122: static void DmaSnd_Apply_LMC(int nMixBufIdx, int nSamplesToGenerate);
1.1.1.7 root 123: static void DmaSnd_Set_Tone_Level(int set_bass, int set_treb);
124: static float DmaSnd_IIRfilterL(float xn);
125: static float DmaSnd_IIRfilterR(float xn);
126: static struct first_order_s *DmaSnd_Treble_Shelf(float g, float fc, float Fs);
127: static struct first_order_s *DmaSnd_Bass_Shelf(float g, float fc, float Fs);
1.1.1.8 root 128: static Sint16 DmaSnd_LowPassFilterLeft(Sint16 in);
129: static Sint16 DmaSnd_LowPassFilterRight(Sint16 in);
130: static bool DmaSnd_LowPass;
1.1 root 131:
132:
133: Uint16 nDmaSoundControl; /* Sound control register */
134:
1.1.1.7 root 135: struct first_order_s { float a1, b0, b1; };
136: struct second_order_s { float a1, a2, b0, b1, b2; };
137:
138: struct dma_s {
139: Uint16 soundMode; /* Sound mode register */
140: Uint32 frameStartAddr; /* Sound frame start */
141: Uint32 frameEndAddr; /* Sound frame end */
1.1.1.8 root 142: Uint32 frameCounterAddr; /* Sound frame current address counter */
143:
144: /* Internal 8 byte FIFO */
145: Sint8 FIFO[ DMASND_FIFO_SIZE ];
146: Uint16 FIFO_Pos; /* from 0 to DMASND_FIFO_SIZE-1 */
147: Uint16 FIFO_NbBytes; /* from 0 to DMASND_FIFO_SIZE */
148:
1.1.1.9 root 149: Sint16 FrameLeft; /* latest values read from the FIFO */
1.1.1.8 root 150: Sint16 FrameRight;
1.1.1.7 root 151: };
152:
1.1.1.8 root 153: Sint64 frameCounter_float = 0;
154: bool DmaInitSample = false;
155:
156:
1.1.1.7 root 157: struct microwire_s {
158: Uint16 data; /* Microwire Data register */
159: Uint16 mask; /* Microwire Mask register */
1.1.1.9 root 160: Uint16 mwTransferSteps; /* Microwire shifting counter */
161: Uint16 pendingCyclesOver; /* Number of delayed cycles for the interrupt */
1.1.1.7 root 162: Uint16 mixing; /* Mixing command */
163: Uint16 bass; /* Bass command */
164: Uint16 treble; /* Treble command */
165: Uint16 masterVolume; /* Master volume command */
166: Uint16 leftVolume; /* Left channel volume command */
167: Uint16 rightVolume; /* Right channel volume command */
168: };
169:
170: struct lmc1992_s {
171: struct first_order_s bass_table[TONE_STEPS];
172: struct first_order_s treb_table[TONE_STEPS];
1.1.1.8 root 173: float coef[5]; /* IIR coefficients */
174: float left_gain;
175: float right_gain;
1.1.1.7 root 176: };
177:
178: static struct dma_s dma;
179: static struct microwire_s microwire;
180: static struct lmc1992_s lmc1992;
181:
182: /* dB = 20log(gain) : gain = antilog(dB/20) */
183: /* Table gain values = (int)(powf(10.0, dB/20.0)*65536.0 + 0.5) 2dB steps */
184:
185: /* Values for LMC1992 Master volume control (*65536) */
186: static const Uint16 LMC1992_Master_Volume_Table[64] =
187: {
188: 7, 8, 10, 13, 16, 21, 26, 33, 41, 52, /* -80dB */
189: 66, 83, 104, 131, 165, 207, 261, 328, 414, 521, /* -60dB */
190: 655, 825, 1039, 1308, 1646, 2072, 2609, 3285, 4135, 5206, /* -40dB */
191: 6554, 8250, 10387, 13076, 16462, 20724, 26090, 32846, 41350, 52057, /* -20dB */
192: 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, /* 0dB */
193: 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, /* 0dB */
194: 65535, 65535, 65535, 65535 /* 0dB */
195: };
1.1.1.6 root 196:
1.1.1.7 root 197: /* Values for LMC1992 Left and right volume control (*65536) */
198: static const Uint16 LMC1992_LeftRight_Volume_Table[32] =
199: {
200: 655, 825, 1039, 1308, 1646, 2072, 2609, 3285, 4135, 5206, /* -40dB */
201: 6554, 8250, 10387, 13076, 16462, 20724, 26090, 32846, 41350, 52057, /* -20dB */
202: 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, /* 0dB */
203: 65535, 65535 /* 0dB */
1.1 root 204: };
205:
1.1.1.7 root 206: /* Values for LMC1992 BASS and TREBLE */
207: static const Sint16 LMC1992_Bass_Treble_Table[16] =
208: {
209: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12
210: };
1.1 root 211:
1.1.1.7 root 212: static const int DmaSndSampleRates[4] =
1.1.1.3 root 213: {
1.1.1.8 root 214: 6258, 12517, 25033, 50066
1.1.1.3 root 215: };
216:
217:
1.1.1.8 root 218:
219: /*--------------------------------------------------------------*/
220: /* Local functions prototypes */
221: /*--------------------------------------------------------------*/
222:
223: static void DmaSnd_FIFO_Refill(void);
224: static Sint8 DmaSnd_FIFO_PullByte(void);
225: static void DmaSnd_FIFO_SetStereo(void);
226:
227: static int DmaSnd_DetectSampleRate(void);
228: static void DmaSnd_StartNewFrame(void);
229: static inline int DmaSnd_EndOfFrameReached(void);
230:
231:
1.1.1.7 root 232: /**
1.1.1.3 root 233: * Reset DMA sound variables.
234: */
1.1.1.4 root 235: void DmaSnd_Reset(bool bCold)
1.1 root 236: {
237: nDmaSoundControl = 0;
1.1.1.8 root 238: dma.soundMode = 0;
239:
240: /* [NP] Set start/end to 0 even on warm reset ? (fix 'Brace' by Diamond Design) */
241: IoMem[0xff8903] = 0; /* frame start addr = 0 */
242: IoMem[0xff8905] = 0;
243: IoMem[0xff8907] = 0;
244: IoMem[0xff890f] = 0; /* frame end addr = 0 */
245: IoMem[0xff8911] = 0;
246: IoMem[0xff8913] = 0;
247:
248: dma.FIFO_Pos = 0;
249: dma.FIFO_NbBytes = 0;
250: dma.FrameLeft = 0;
251: dma.FrameRight = 0;
1.1 root 252:
1.1.1.8 root 253: if ( bCold )
1.1 root 254: {
1.1.1.8 root 255: /* Microwire has no reset signal, it will keep its values on warm reset */
256: microwire.masterVolume = 7; /* -80 dB ; TOS 1.62 will put 0x28 (ie 65535) = 0 dB (max volume) */
257: microwire.leftVolume = 655; /* -40 dB ; TOS 1.62 will put 0x14 (ie 65535) = 0 dB (max volume) */
258: microwire.rightVolume = 655; /* -40 db ; TOS 1.62 will put 0x14 (ie 65535) = 0 dB (max volume) */
1.1.1.7 root 259: microwire.mixing = 0;
1.1.1.8 root 260: microwire.bass = 6; /* 0 dB (flat) */
261: microwire.treble = 6; /* 0 dB (flat) */
1.1 root 262: }
1.1.1.4 root 263:
1.1.1.7 root 264: /* Initialise microwire LMC1992 IIR filter parameters */
265: DmaSnd_Init_Bass_and_Treble_Tables();
1.1.1.6 root 266:
1.1.1.7 root 267: microwire.mwTransferSteps = 0;
1.1.1.9 root 268: microwire.pendingCyclesOver = 8;
1.1 root 269: }
270:
271: /*-----------------------------------------------------------------------*/
1.1.1.3 root 272: /**
273: * Save/Restore snapshot of local variables ('MemorySnapShot_Store' handles type)
274: */
1.1.1.4 root 275: void DmaSnd_MemorySnapShot_Capture(bool bSave)
1.1 root 276: {
277: /* Save/Restore details */
278: MemorySnapShot_Store(&nDmaSoundControl, sizeof(nDmaSoundControl));
1.1.1.7 root 279: MemorySnapShot_Store(&dma, sizeof(dma));
280: MemorySnapShot_Store(µwire, sizeof(microwire));
281: MemorySnapShot_Store(&lmc1992, sizeof(lmc1992));
1.1 root 282: }
283:
284:
1.1.1.8 root 285: /*-----------------------------------------------------------------------*/
286: /**
287: * This function is called on every HBL to ensure the DMA Audio's FIFO
288: * is kept full.
289: * In Hatari, the FIFO is handled like a ring buffer (to avoid memcopying bytes
290: * inside the FIFO when a byte is pushed/pulled).
291: * Note that the DMA fetches words, not bytes, so we read new data only
292: * when 2 bytes or more are missing.
293: * When end of frame is reached, we continue with a new frame if loop mode
294: * is on, else we stop DMA Audio.
295: */
296: static void DmaSnd_FIFO_Refill(void)
297: {
298: /* If DMA sound is OFF, don't update the FIFO */
299: if ( ( nDmaSoundControl & DMASNDCTRL_PLAY ) == 0)
300: return;
301:
302: /* If End Address == Start Address, don't update the FIFO */
303: if (dma.frameEndAddr == dma.frameStartAddr)
304: {
305: DmaSnd_EndOfFrameReached(); /* Stop dma audio if loop mode is off */
306: return;
307: }
308:
309: /* Refill the whole FIFO */
310: while ( DMASND_FIFO_SIZE - dma.FIFO_NbBytes >= 2 )
311: {
312: /* Add one word to the FIFO */
313: LOG_TRACE(TRACE_DMASND, "DMA snd fifo refill adr=%x pos %d nb %d %x %x\n", dma.frameCounterAddr , dma.FIFO_Pos , dma.FIFO_NbBytes ,
314: STRam[ dma.frameCounterAddr ] , STRam[ dma.frameCounterAddr+1 ] );
315:
316: dma.FIFO[ ( dma.FIFO_Pos+dma.FIFO_NbBytes+0 ) & DMASND_FIFO_SIZE_MASK ] = (Sint8)STRam[ dma.frameCounterAddr ]; /* add upper byte of the word */
317: dma.FIFO[ ( dma.FIFO_Pos+dma.FIFO_NbBytes+1 ) & DMASND_FIFO_SIZE_MASK ] = (Sint8)STRam[ dma.frameCounterAddr+1 ]; /* add lower byte of the word */
318:
319: dma.FIFO_NbBytes += 2; /* One word more in the FIFO */
320:
321: /* Increase current frame address and check if we reached frame's end */
322: dma.frameCounterAddr += 2;
323: if ( dma.frameCounterAddr == dma.frameEndAddr ) /* end of frame reached, should we loop or stop dma ? */
324: {
325: if ( DmaSnd_EndOfFrameReached() )
326: break; /* Loop mode off, dma audio is now turned off */
327: }
328: }
329: }
330:
331:
332: /*-----------------------------------------------------------------------*/
333: /**
334: * Pull one sample/byte from the DMA Audio's FIFO and decrease the number of
335: * remaining bytes.
336: * If the FIFO is empty, return 0 (empty sample)
337: * Note : on a real STE, the 8 bytes FIFO is refilled on each HBL, which gives
1.1.1.11! root 338: * a total of 313*8*VBL_PER_SEC=125326 bytes per sec read by the DMA. As the max freq
1.1.1.8 root 339: * is 50066 Hz, the STE can play 100132 bytes per sec in stereo ; so on a real STE
340: * the FIFO can never be empty while DMA is ON.
341: * But on Hatari, if the user chooses an audio's output frequency that is much
342: * lower than the current DMA freq, audio will be updated less frequently than
343: * on each HBL and it could require to process more than DMASND_FIFO_SIZE in one
344: * call to DmaSnd_GenerateSamples(). This is why we allow DmaSnd_FIFO_Refill()
345: * to be called if FIFO is empty but DMA sound is still ON.
346: * This way, sound remains correct even if the user uses very low output freq.
347: */
348: static Sint8 DmaSnd_FIFO_PullByte(void)
349: {
350: Sint8 sample;
351:
352: if ( dma.FIFO_NbBytes == 0 )
353: {
354: DmaSnd_FIFO_Refill();
355: if ( dma.FIFO_NbBytes == 0 ) /* Refill didn't add any new bytes */
356: {
357: LOG_TRACE(TRACE_DMASND, "DMA snd fifo empty for pull\n" );
358: return 0;
359: }
360: }
361:
362:
363: LOG_TRACE(TRACE_DMASND, "DMA snd fifo pull pos %d nb %d %02x\n", dma.FIFO_Pos , dma.FIFO_NbBytes , (Uint8)dma.FIFO[ dma.FIFO_Pos ] );
364:
365: sample = dma.FIFO[ dma.FIFO_Pos ]; /* Get oldest byte from the FIFO */
366: dma.FIFO_Pos = (dma.FIFO_Pos+1) & DMASND_FIFO_SIZE_MASK;/* Pos to be pulled on next call */
367: dma.FIFO_NbBytes--; /* One byte less in the FIFO */
368:
369: return sample;
370: }
371:
372:
373: /*-----------------------------------------------------------------------*/
374: /**
375: * In case a program switches from mono to stereo, we must ensure that
376: * FIFO_pos is on even boundary to keep Left/Right bytes in the correct
377: * order (Left byte should be on even addresses and Right byte on odd ones).
378: * If this is not the case, we skip one byte.
379: */
380: static void DmaSnd_FIFO_SetStereo(void)
381: {
382: Uint16 NewPos;
383:
384: if ( dma.FIFO_Pos & 1 )
385: {
386: NewPos = (dma.FIFO_Pos+1) & DMASND_FIFO_SIZE_MASK; /* skip the byte on odd address */
387:
388: if ( nDmaSoundControl & DMASNDCTRL_PLAY ) /* print a log if we change while playing */
389: { LOG_TRACE(TRACE_DMASND, "DMA snd switching to stereo mode while playing mono FIFO_pos %d->%d\n", dma.FIFO_Pos , NewPos ); }
390: else
391: { LOG_TRACE(TRACE_DMASND, "DMA snd switching to stereo mode FIFO_pos %d->%d\n", dma.FIFO_Pos , NewPos ); }
392:
393: dma.FIFO_Pos = NewPos;
394:
395: if ( dma.FIFO_NbBytes > 0 )
396: dma.FIFO_NbBytes--; /* remove one byte if FIFO was not already empty */
397: }
398:
399: }
400:
401:
402: /*-----------------------------------------------------------------------*/
403: /**
404: * Returns the frequency corresponding to the 2 lower bits of dma.soundMode
405: */
1.1.1.7 root 406: static int DmaSnd_DetectSampleRate(void)
1.1.1.3 root 407: {
1.1.1.7 root 408: return DmaSndSampleRates[dma.soundMode & 3];
1.1.1.3 root 409: }
410:
411:
1.1 root 412: /*-----------------------------------------------------------------------*/
1.1.1.3 root 413: /**
414: * This function is called when a new sound frame is started.
1.1.1.8 root 415: * It copies the start and end address from the I/O registers and set
416: * the frame counter addr to the start of this new frame.
1.1.1.3 root 417: */
1.1 root 418: static void DmaSnd_StartNewFrame(void)
419: {
1.1.1.7 root 420: dma.frameStartAddr = (IoMem[0xff8903] << 16) | (IoMem[0xff8905] << 8) | (IoMem[0xff8907] & ~1);
421: dma.frameEndAddr = (IoMem[0xff890f] << 16) | (IoMem[0xff8911] << 8) | (IoMem[0xff8913] & ~1);
1.1 root 422:
1.1.1.8 root 423: dma.frameCounterAddr = dma.frameStartAddr;
1.1.1.6 root 424:
1.1.1.8 root 425: LOG_TRACE(TRACE_DMASND, "DMA snd new frame start=%x end=%x\n", dma.frameStartAddr, dma.frameEndAddr);
1.1 root 426: }
427:
428:
429: /*-----------------------------------------------------------------------*/
1.1.1.3 root 430: /**
1.1.1.7 root 431: * End-of-frame has been reached. Raise interrupts if needed.
1.1.1.6 root 432: * Returns true if DMA sound processing should be stopped now and false
1.1.1.7 root 433: * if it continues (DMA PLAYLOOP mode).
1.1.1.3 root 434: */
1.1.1.7 root 435: static inline int DmaSnd_EndOfFrameReached(void)
1.1 root 436: {
1.1.1.8 root 437: LOG_TRACE(TRACE_DMASND, "DMA snd end of frame\n");
438:
1.1.1.7 root 439: /* Raise end-of-frame interrupts (MFP-i7 and Time-A) */
1.1.1.11! root 440: MFP_InputOnChannel ( MFP_INT_GPIP7 , 0 );
1.1.1.7 root 441: if (MFP_TACR == 0x08) /* Is timer A in Event Count mode? */
442: MFP_TimerA_EventCount_Interrupt();
1.1 root 443:
1.1.1.7 root 444: if (nDmaSoundControl & DMASNDCTRL_PLAYLOOP)
445: {
446: DmaSnd_StartNewFrame();
1.1 root 447: }
1.1.1.7 root 448: else
1.1.1.6 root 449: {
1.1.1.7 root 450: nDmaSoundControl &= ~DMASNDCTRL_PLAY;
451: return true;
1.1.1.6 root 452: }
453:
1.1.1.7 root 454: return false;
1.1 root 455: }
456:
457:
458: /*-----------------------------------------------------------------------*/
1.1.1.3 root 459: /**
460: * Mix DMA sound sample with the normal PSG sound samples.
1.1.1.6 root 461: * Note: We adjust the volume level of the 8-bit DMA samples to factor
1.1.1.7 root 462: * 0.75 compared to the PSG sound samples.
1.1.1.8 root 463: *
1.1.1.11! root 464: * The following formula: -((256*3/4)/4)/4
1.1.1.8 root 465: *
466: * Multiply by 256 to convert 8 to 16 bits;
467: * DMA sound is 3/4 level of YM sound;
1.1.1.11! root 468: * Divide by 4 to account for the STe YM volume table level;
! 469: * ( STe sound at 1/2 amplitude to avoid overflow. )
! 470: * ( lmc1992.right_gain and lmc1992.left_gain are )
! 471: * ( doubled to compensate. )
1.1.1.8 root 472: * Divide by 4 to account for DmaSnd_LowPassFilter;
473: * Multiply DMA sound by -1 because the LMC1992 inverts the signal
474: * ( YM sign is +1 :: -1(op-amp) * -1(Lmc1992) ).
1.1.1.3 root 475: */
1.1.1.8 root 476:
477:
1.1 root 478: void DmaSnd_GenerateSamples(int nMixBufIdx, int nSamplesToGenerate)
479: {
1.1.1.8 root 480: int i;
1.1.1.7 root 481: int nBufIdx;
1.1.1.10 root 482: Sint8 MonoByte , LeftByte , RightByte;
1.1.1.7 root 483: unsigned n;
1.1.1.8 root 484: Sint64 FreqRatio;
485:
486:
487: /* DMA Audio OFF and FIFO empty : process YM2149's output */
488: if ( !(nDmaSoundControl & DMASNDCTRL_PLAY) && ( dma.FIFO_NbBytes == 0 ) )
489: {
490: for (i = 0; i < nSamplesToGenerate; i++)
491: {
492: nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.6 root 493:
1.1.1.8 root 494: switch (microwire.mixing) {
495: case 1:
1.1.1.10 root 496: /* DMA and (YM2149 0 dB) mixing */
1.1.1.11! root 497: MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][0] + dma.FrameRight * -((256*3/4)/4)/4;
! 498: MixBuffer[nBufIdx][0] += dma.FrameLeft * -((256*3/4)/4)/4;
1.1.1.8 root 499: break;
1.1.1.10 root 500: case 2:
501: /* DMA only (but DMA is off in that case) */
502: MixBuffer[nBufIdx][0] = MixBuffer[nBufIdx][1] = 0;
1.1.1.8 root 503: default:
1.1.1.10 root 504: /* DMA and (YM2149 -12 dB) mixing */
1.1.1.8 root 505: MixBuffer[nBufIdx][0] /= 4;
1.1.1.11! root 506: MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][0] + dma.FrameRight * -((256*3/4)/4)/4;
! 507: MixBuffer[nBufIdx][0] += dma.FrameLeft * -((256*3/4)/4)/4;
1.1.1.8 root 508: break;
509: }
510: }
511:
512: /* Apply LMC1992 sound modifications (Bass and Treble) */
513: DmaSnd_Apply_LMC ( nMixBufIdx , nSamplesToGenerate );
1.1.1.10 root 514:
1.1 root 515: return;
1.1.1.8 root 516: }
517:
1.1 root 518:
1.1.1.8 root 519: /* DMA Audio ON or FIFO not empty yet */
1.1 root 520:
1.1.1.8 root 521: /* Compute ratio between DMA's sound frequency and host computer's sound frequency, */
522: /* use << 32 to simulate floating point precision */
523: FreqRatio = ( ((Sint64)DmaSnd_DetectSampleRate()) << 32 ) / nAudioFrequency;
1.1.1.7 root 524:
525: if (dma.soundMode & DMASNDMODE_MONO)
1.1.1.3 root 526: {
527: /* Mono 8-bit */
1.1 root 528: for (i = 0; i < nSamplesToGenerate; i++)
529: {
1.1.1.8 root 530: if ( DmaInitSample )
531: {
532: MonoByte = DmaSnd_FIFO_PullByte ();
1.1.1.9 root 533: dma.FrameLeft = DmaSnd_LowPassFilterLeft( (Sint16)MonoByte );
534: dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)MonoByte );
1.1.1.8 root 535: DmaInitSample = false;
1.1.1.7 root 536: }
537:
1.1 root 538: nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.7 root 539:
540: switch (microwire.mixing) {
541: case 1:
1.1.1.10 root 542: /* DMA and (YM2149 0 dB) mixing */
1.1.1.11! root 543: MixBuffer[nBufIdx][0] = MixBuffer[nBufIdx][0] + dma.FrameLeft * -((256*3/4)/4)/4;
1.1.1.7 root 544: break;
545: case 2:
1.1.1.10 root 546: /* DMA only */
1.1.1.11! root 547: MixBuffer[nBufIdx][0] = dma.FrameLeft * -((256*3/4)/4)/4;
1.1.1.7 root 548: break;
549: default:
1.1.1.10 root 550: /* DMA and (YM2149 -12 dB) mixing */
551: /* instead of 16462 (-12 dB), we approximate by 16384 */
1.1.1.11! root 552: MixBuffer[nBufIdx][0] = (dma.FrameLeft * -((256*3/4)/4)/4) +
1.1.1.7 root 553: (((Sint32)MixBuffer[nBufIdx][0] * 16384)/65536);
554: break;
555: }
556:
1.1.1.8 root 557: MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][0]; /* right = left */
558:
559: /* Increase freq counter */
560: frameCounter_float += FreqRatio;
561: n = frameCounter_float >> 32; /* number of samples to skip */
562: while ( n > 0 ) /* pull as many bytes from the FIFO as needed */
563: {
564: MonoByte = DmaSnd_FIFO_PullByte ();
1.1.1.9 root 565: dma.FrameLeft = DmaSnd_LowPassFilterLeft( (Sint16)MonoByte );
566: dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)MonoByte );
1.1.1.8 root 567: n--;
1.1.1.7 root 568: }
1.1.1.8 root 569: frameCounter_float &= 0xffffffff; /* only keep the fractional part */
1.1 root 570: }
571: }
572: else
573: {
1.1.1.3 root 574: /* Stereo 8-bit */
1.1 root 575: for (i = 0; i < nSamplesToGenerate; i++)
576: {
1.1.1.8 root 577: if ( DmaInitSample )
578: {
579: LeftByte = DmaSnd_FIFO_PullByte ();
580: RightByte = DmaSnd_FIFO_PullByte ();
581: dma.FrameLeft = DmaSnd_LowPassFilterLeft( (Sint16)LeftByte );
582: dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)RightByte );
583: DmaInitSample = false;
1.1.1.7 root 584: }
585:
1.1 root 586: nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.8 root 587:
1.1.1.7 root 588: switch (microwire.mixing) {
589: case 1:
1.1.1.10 root 590: /* DMA and (YM2149 0 dB) mixing */
1.1.1.11! root 591: MixBuffer[nBufIdx][0] = MixBuffer[nBufIdx][0] + dma.FrameLeft * -((256*3/4)/4)/4;
! 592: MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][1] + dma.FrameRight * -((256*3/4)/4)/4;
1.1.1.7 root 593: break;
594: case 2:
1.1.1.10 root 595: /* DMA only */
1.1.1.11! root 596: MixBuffer[nBufIdx][0] = dma.FrameLeft * -((256*3/4)/4)/4;
! 597: MixBuffer[nBufIdx][1] = dma.FrameRight * -((256*3/4)/4)/4;
1.1.1.7 root 598: break;
599: default:
1.1.1.10 root 600: /* DMA and (YM2149 -12 dB) mixing */
601: /* instead of 16462 (-12 dB), we approximate by 16384 */
1.1.1.11! root 602: MixBuffer[nBufIdx][0] = (dma.FrameLeft * -((256*3/4)/4)/4) +
1.1.1.7 root 603: (((Sint32)MixBuffer[nBufIdx][0] * 16384)/65536);
1.1.1.11! root 604: MixBuffer[nBufIdx][1] = (dma.FrameRight * -((256*3/4)/4)/4) +
1.1.1.7 root 605: (((Sint32)MixBuffer[nBufIdx][1] * 16384)/65536);
606: break;
607: }
608:
1.1.1.8 root 609: /* Increase freq counter */
610: frameCounter_float += FreqRatio;
611: n = frameCounter_float >> 32; /* number of samples to skip */
612: while ( n > 0 ) /* pull as many bytes from the FIFO as needed */
613: {
614: LeftByte = DmaSnd_FIFO_PullByte ();
615: RightByte = DmaSnd_FIFO_PullByte ();
616: dma.FrameLeft = DmaSnd_LowPassFilterLeft( (Sint16)LeftByte );
617: dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)RightByte );
618: n--;
1.1.1.7 root 619: }
1.1.1.8 root 620: frameCounter_float &= 0xffffffff; /* only keep the fractional part */
1.1 root 621: }
622: }
1.1.1.7 root 623:
1.1.1.8 root 624: /* Apply LMC1992 sound modifications (Bass and Treble) */
625: DmaSnd_Apply_LMC ( nMixBufIdx , nSamplesToGenerate );
626: }
627:
628:
629: /*-----------------------------------------------------------------------*/
630: /**
631: * Apply LMC1992 sound modifications (Bass and Treble)
632: * The Bass and Treble get samples at nAudioFrequency rate.
633: * The tone control's sampling frequency must be at least 22050 Hz to sound good.
634: */
635: static void DmaSnd_Apply_LMC(int nMixBufIdx, int nSamplesToGenerate)
636: {
637: int nBufIdx;
638: int i;
1.1.1.11! root 639: Sint32 sample;
1.1.1.7 root 640:
641: /* Apply LMC1992 sound modifications (Left, Right and Master Volume) */
642: for (i = 0; i < nSamplesToGenerate; i++) {
643: nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.11! root 644:
! 645: sample = DmaSnd_IIRfilterL( Subsonic_IIR_HPF_Left( MixBuffer[nBufIdx][0]));
! 646: if (sample<-32767) /* check for overflow to clip waveform */
! 647: sample = -32767;
! 648: else if (sample>32767)
! 649: sample = 32767;
! 650: MixBuffer[nBufIdx][0] = sample;
! 651:
! 652: sample = DmaSnd_IIRfilterR( Subsonic_IIR_HPF_Right(MixBuffer[nBufIdx][1]));
! 653: if (sample<-32767) /* check for overflow to clip waveform */
! 654: sample = -32767;
! 655: else if (sample>32767)
! 656: sample = 32767;
! 657: MixBuffer[nBufIdx][1] = sample;
1.1.1.8 root 658: }
1.1 root 659: }
660:
661:
662: /*-----------------------------------------------------------------------*/
1.1.1.3 root 663: /**
1.1.1.8 root 664: * STE DMA sound is using an 8 bytes FIFO that is checked and filled on each HBL
665: * (at 50066 Hz 8 bit stereo, the DMA requires approx 6.5 new bytes per HBL)
666: * Calling Sound_Update on each HBL allows to emulate some programs that modify
667: * the data between FrameStart and FrameEnd while DMA sound is ON
668: * (eg the demo 'Mental Hangover' or the game 'Power Up Plus')
669: * We first check if the FIFO needs to be refilled, then we call Sound_Update.
670: * This function should be called from the HBL's handler (in video.c)
1.1.1.3 root 671: */
1.1.1.8 root 672: void DmaSnd_STE_HBL_Update(void)
1.1 root 673: {
1.1.1.8 root 674: if ( ( ConfigureParams.System.nMachineType != MACHINE_STE )
675: && ( ConfigureParams.System.nMachineType != MACHINE_MEGA_STE ) )
676: return;
1.1 root 677:
1.1.1.8 root 678:
679: /* The DMA starts refilling the FIFO when display is OFF (eg cycle 376 in low res 50 Hz) */
680: DmaSnd_FIFO_Refill ();
681:
682: /* If DMA sound is ON or FIFO is not empty, update sound */
683: if ( (nDmaSoundControl & DMASNDCTRL_PLAY) || ( dma.FIFO_NbBytes > 0 ) )
684: Sound_Update(false);
685:
686: /* As long as display is OFF, the DMA will refill the FIFO after playing some samples during the HBL */
687: DmaSnd_FIFO_Refill ();
1.1 root 688: }
689:
690:
691: /*-----------------------------------------------------------------------*/
1.1.1.3 root 692: /**
1.1.1.8 root 693: * Return current frame counter address (value is always even)
1.1.1.3 root 694: */
1.1 root 695: static Uint32 DmaSnd_GetFrameCount(void)
696: {
697: Uint32 nActCount;
698:
1.1.1.8 root 699: /* Update sound to get the current DMA frame address */
700: Sound_Update(false);
701:
1.1 root 702: if (nDmaSoundControl & DMASNDCTRL_PLAY)
1.1.1.8 root 703: nActCount = dma.frameCounterAddr;
1.1 root 704: else
1.1.1.8 root 705: nActCount = (IoMem[0xff8903] << 16) | (IoMem[0xff8905] << 8) | (IoMem[0xff8907] & ~1);
1.1 root 706:
707: return nActCount;
708: }
709:
710:
711: /*-----------------------------------------------------------------------*/
1.1.1.3 root 712: /**
713: * Read word from sound control register (0xff8900).
714: */
1.1 root 715: void DmaSnd_SoundControl_ReadWord(void)
716: {
717: IoMem_WriteWord(0xff8900, nDmaSoundControl);
1.1.1.6 root 718:
1.1.1.11! root 719: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 720: {
! 721: int FrameCycles, HblCounterVideo, LineCycles;
! 722: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 723: LOG_TRACE_PRINT("DMA snd control read: 0x%04x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 724: nDmaSoundControl,
! 725: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 726: }
1.1 root 727: }
728:
729:
730: /*-----------------------------------------------------------------------*/
1.1.1.3 root 731: /**
1.1.1.8 root 732: * Write word to sound control register (0xff8900).
733: */
734: void DmaSnd_SoundControl_WriteWord(void)
735: {
736: Uint16 nNewSndCtrl;
737:
1.1.1.11! root 738: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 739: {
! 740: int FrameCycles, HblCounterVideo, LineCycles;
! 741: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 742: LOG_TRACE_PRINT("DMA snd control write: 0x%04x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 743: IoMem_ReadWord(0xff8900),
! 744: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 745: }
1.1.1.8 root 746:
747: /* Before starting/stopping DMA sound, create samples up until this point with current values */
748: Sound_Update(false);
749:
750: nNewSndCtrl = IoMem_ReadWord(0xff8900) & 3;
751:
752: if (!(nDmaSoundControl & DMASNDCTRL_PLAY) && (nNewSndCtrl & DMASNDCTRL_PLAY))
753: {
754: LOG_TRACE(TRACE_DMASND, "DMA snd control write: starting dma sound output\n");
755: DmaInitSample = true;
756: frameCounter_float = 0;
757: DmaSnd_StartNewFrame();
758: }
759: else if ((nDmaSoundControl & DMASNDCTRL_PLAY) && !(nNewSndCtrl & DMASNDCTRL_PLAY))
760: {
761: LOG_TRACE(TRACE_DMASND, "DMA snd control write: stopping dma sound output\n");
762: }
763:
764: nDmaSoundControl = nNewSndCtrl;
765: }
766:
767:
768: /*-----------------------------------------------------------------------*/
769: /**
1.1.1.3 root 770: * Read word from sound frame count high register (0xff8909).
771: */
1.1 root 772: void DmaSnd_FrameCountHigh_ReadByte(void)
773: {
774: IoMem_WriteByte(0xff8909, DmaSnd_GetFrameCount() >> 16);
775: }
776:
777:
778: /*-----------------------------------------------------------------------*/
1.1.1.3 root 779: /**
780: * Read word from sound frame count medium register (0xff890b).
781: */
1.1 root 782: void DmaSnd_FrameCountMed_ReadByte(void)
783: {
784: IoMem_WriteByte(0xff890b, DmaSnd_GetFrameCount() >> 8);
785: }
786:
787:
788: /*-----------------------------------------------------------------------*/
1.1.1.3 root 789: /**
790: * Read word from sound frame count low register (0xff890d).
791: */
1.1 root 792: void DmaSnd_FrameCountLow_ReadByte(void)
793: {
794: IoMem_WriteByte(0xff890d, DmaSnd_GetFrameCount());
795: }
796:
797:
798: /*-----------------------------------------------------------------------*/
1.1.1.3 root 799: /**
1.1.1.8 root 800: * Write bytes to various registers with no action.
801: */
802: void DmaSnd_FrameStartHigh_WriteByte(void)
803: {
1.1.1.11! root 804: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 805: {
! 806: int FrameCycles, HblCounterVideo, LineCycles;
! 807: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 808: LOG_TRACE_PRINT("DMA snd frame start high: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 809: IoMem_ReadByte(0xff8903) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 810: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 811: }
1.1.1.8 root 812: }
813:
814: void DmaSnd_FrameStartMed_WriteByte(void)
815: {
1.1.1.11! root 816: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 817: {
! 818: int FrameCycles, HblCounterVideo, LineCycles;
! 819: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 820: LOG_TRACE_PRINT("DMA snd frame start med: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 821: IoMem_ReadByte(0xff8905) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 822: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 823: }
1.1.1.8 root 824: }
825:
826: void DmaSnd_FrameStartLow_WriteByte(void)
827: {
1.1.1.11! root 828: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 829: {
! 830: int FrameCycles, HblCounterVideo, LineCycles;
! 831: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 832: LOG_TRACE_PRINT("DMA snd frame start low: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 833: IoMem_ReadByte(0xff8907) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 834: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 835: }
1.1.1.8 root 836: }
837:
838: void DmaSnd_FrameCountHigh_WriteByte(void)
839: {
1.1.1.11! root 840: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 841: {
! 842: int FrameCycles, HblCounterVideo, LineCycles;
! 843: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 844: LOG_TRACE_PRINT("DMA snd frame count high: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 845: IoMem_ReadByte(0xff8909) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 846: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 847: }
1.1.1.8 root 848: }
849:
850: void DmaSnd_FrameCountMed_WriteByte(void)
851: {
1.1.1.11! root 852: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 853: {
! 854: int FrameCycles, HblCounterVideo, LineCycles;
! 855: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 856: LOG_TRACE_PRINT("DMA snd frame count med: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 857: IoMem_ReadByte(0xff890b) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 858: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 859: }
1.1.1.8 root 860: }
861:
862: void DmaSnd_FrameCountLow_WriteByte(void)
863: {
1.1.1.11! root 864: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 865: {
! 866: int FrameCycles, HblCounterVideo, LineCycles;
! 867: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 868: LOG_TRACE_PRINT("DMA snd frame count low: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 869: IoMem_ReadByte(0xff890d) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 870: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 871: }
1.1.1.8 root 872: }
873:
874: void DmaSnd_FrameEndHigh_WriteByte(void)
875: {
1.1.1.11! root 876: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 877: {
! 878: int FrameCycles, HblCounterVideo, LineCycles;
! 879: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 880: LOG_TRACE_PRINT("DMA snd frame end high: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 881: IoMem_ReadByte(0xff890f) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 882: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 883: }
1.1.1.8 root 884: }
885:
886: void DmaSnd_FrameEndMed_WriteByte(void)
887: {
1.1.1.11! root 888: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 889: {
! 890: int FrameCycles, HblCounterVideo, LineCycles;
! 891: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 892: LOG_TRACE_PRINT("DMA snd frame end med: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 893: IoMem_ReadByte(0xff8911) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 894: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 895: }
1.1.1.8 root 896: }
897:
898: void DmaSnd_FrameEndLow_WriteByte(void)
899: {
1.1.1.11! root 900: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 901: {
! 902: int FrameCycles, HblCounterVideo, LineCycles;
! 903: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 904: LOG_TRACE_PRINT("DMA snd frame end low: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 905: IoMem_ReadByte(0xff8913) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr ,
! 906: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 907: }
1.1.1.8 root 908: }
909:
910:
911: /*-----------------------------------------------------------------------*/
912: /**
1.1.1.7 root 913: * Read word from sound mode register (0xff8921).
1.1.1.3 root 914: */
1.1.1.7 root 915: void DmaSnd_SoundModeCtrl_ReadByte(void)
1.1 root 916: {
1.1.1.7 root 917: IoMem_WriteByte(0xff8921, dma.soundMode);
1.1.1.6 root 918:
1.1.1.11! root 919: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 920: {
! 921: int FrameCycles, HblCounterVideo, LineCycles;
! 922: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 923: LOG_TRACE_PRINT("DMA snd mode read: 0x%02x video_cyc=%d %d@%d pc=%x instr_cycle %d\n", dma.soundMode,
! 924: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 925: }
1.1 root 926: }
927:
928:
929: /*-----------------------------------------------------------------------*/
1.1.1.3 root 930: /**
1.1.1.7 root 931: * Write word to sound mode register (0xff8921).
1.1.1.3 root 932: */
1.1.1.7 root 933: void DmaSnd_SoundModeCtrl_WriteByte(void)
1.1 root 934: {
1.1.1.8 root 935: Uint16 SoundModeNew;
936:
937: SoundModeNew = IoMem_ReadByte(0xff8921);
938:
1.1.1.11! root 939: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 940: {
! 941: int FrameCycles, HblCounterVideo, LineCycles;
! 942: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 943: LOG_TRACE_PRINT("DMA snd mode write: 0x%02x mode=%s freq=%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 944: SoundModeNew, SoundModeNew & DMASNDMODE_MONO ? "mono" : "stereo" , DmaSndSampleRates[ SoundModeNew & 3 ],
! 945: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 946: }
1.1.1.8 root 947:
948: /* We maskout to only bits that exist on a real STE */
949: SoundModeNew &= 0x8f;
1.1.1.3 root 950:
1.1.1.8 root 951: /* Are we switching from mono to stereo ? */
952: if ( ( dma.soundMode & DMASNDMODE_MONO ) && ( ( SoundModeNew & DMASNDMODE_MONO ) == 0 ) )
953: DmaSnd_FIFO_SetStereo ();
954:
955: dma.soundMode = SoundModeNew;
956: /* We also write the masked value back into the emulated hw registers so we have a correct value there */
1.1.1.7 root 957: IoMem_WriteByte(0xff8921, dma.soundMode);
1.1 root 958: }
959:
1.1.1.8 root 960:
1.1.1.6 root 961: /* ---------------------- Microwire / LMC 1992 ---------------------- */
962:
1.1.1.3 root 963: /**
1.1.1.4 root 964: * Handle the shifting/rotating of the microwire registers
965: * The microwire regs should be done after 16 usec = 32 NOPs = 128 cycles.
966: * That means we have to shift 16 times with a delay of 8 cycles.
1.1.1.3 root 967: */
1.1.1.4 root 968: void DmaSnd_InterruptHandler_Microwire(void)
1.1 root 969: {
1.1.1.7 root 970: Uint8 i, bit;
971: Uint16 saveData;
1.1.1.10 root 972:
973: /* If emulated computer is the Falcon, let's the crossbar Microwire code do the job. */
974: if (ConfigureParams.System.nMachineType == MACHINE_FALCON) {
975: Crossbar_InterruptHandler_Microwire();
976: return;
977: }
1.1.1.7 root 978:
1.1.1.9 root 979: /* How many cycle was this sound interrupt delayed (>= 0) */
980: microwire.pendingCyclesOver += -INT_CONVERT_FROM_INTERNAL ( PendingInterruptCount , INT_CPU_CYCLE );
1.1.1.4 root 981: /* Remove this interrupt from list and re-order */
1.1.1.7 root 982: CycInt_AcknowledgeInterrupt();
1.1.1.4 root 983:
1.1.1.9 root 984: /* Shift the mask and data according to the number of cycles (8 cycles for a shift) */
985: do
1.1 root 986: {
1.1.1.9 root 987: --microwire.mwTransferSteps;
988: /* Shift data register until it becomes zero. */
1.1.1.7 root 989: IoMem_WriteWord(0xff8922, microwire.data<<(16-microwire.mwTransferSteps));
1.1.1.9 root 990: /* Rotate mask register */
991: IoMem_WriteWord(0xff8924, (microwire.mask<<(16-microwire.mwTransferSteps))
992: |(microwire.mask>>microwire.mwTransferSteps));
993: /* 8 cycles for 1 shift */
994: microwire.pendingCyclesOver -= 8;
1.1 root 995: }
1.1.1.9 root 996: while ((microwire.mwTransferSteps != 0) && (microwire.pendingCyclesOver >= 8) );
1.1.1.4 root 997:
1.1.1.9 root 998: /* Is the transfer finished ? */
1.1.1.7 root 999: if (microwire.mwTransferSteps > 0)
1.1.1.4 root 1000: {
1.1.1.11! root 1001: /* No ==> start a new internal interrupt to continue to transfer the data */
1.1.1.9 root 1002: microwire.pendingCyclesOver = 8 - microwire.pendingCyclesOver;
1003: CycInt_AddRelativeInterrupt(microwire.pendingCyclesOver, INT_CPU_CYCLE, INTERRUPT_DMASOUND_MICROWIRE);
1.1.1.4 root 1004: }
1.1.1.9 root 1005: else
1006: {
1007: /* Yes : decode the address + command word according to the binary mask */
1.1.1.7 root 1008: bit = 0;
1009: saveData = microwire.data;
1010: microwire.data = 0;
1011: for (i=0; i<16; i++) {
1012: if ((microwire.mask >> i) & 1) {
1013: microwire.data += ((saveData >> i) & 1) << bit;
1014: bit ++;
1015: }
1016: }
1.1.1.4 root 1017:
1.1.1.7 root 1018: /* The LMC 1992 address should be 10 xxx xxx xxx */
1019: if ((microwire.data & 0x600) != 0x400)
1020: return;
1021:
1022: /* Update the LMC 1992 commands */
1023: switch ((microwire.data >> 6) & 0x7) {
1024: case 0:
1025: /* Mixing command */
1.1.1.8 root 1026: LOG_TRACE ( TRACE_DMASND, "Microwire new mixing=0x%x\n", microwire.data & 0x3 );
1.1.1.7 root 1027: microwire.mixing = microwire.data & 0x3;
1028: break;
1029: case 1:
1030: /* Bass command */
1.1.1.8 root 1031: LOG_TRACE ( TRACE_DMASND, "Microwire new bass=0x%x\n", microwire.data & 0xf );
1.1.1.7 root 1032: microwire.bass = microwire.data & 0xf;
1033: DmaSnd_Set_Tone_Level(LMC1992_Bass_Treble_Table[microwire.bass],
1034: LMC1992_Bass_Treble_Table[microwire.treble]);
1035: break;
1036: case 2:
1037: /* Treble command */
1.1.1.8 root 1038: LOG_TRACE ( TRACE_DMASND, "Microwire new trebble=0x%x\n", microwire.data & 0xf );
1.1.1.7 root 1039: microwire.treble = microwire.data & 0xf;
1040: DmaSnd_Set_Tone_Level(LMC1992_Bass_Treble_Table[microwire.bass],
1041: LMC1992_Bass_Treble_Table[microwire.treble]);
1042: break;
1043: case 3:
1044: /* Master volume command */
1.1.1.8 root 1045: LOG_TRACE ( TRACE_DMASND, "Microwire new master volume=0x%x\n", microwire.data & 0x3f );
1.1.1.7 root 1046: microwire.masterVolume = LMC1992_Master_Volume_Table[microwire.data & 0x3f];
1.1.1.11! root 1047: lmc1992.left_gain = (microwire.leftVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
! 1048: lmc1992.right_gain = (microwire.rightVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.7 root 1049: break;
1050: case 4:
1051: /* Right channel volume */
1.1.1.8 root 1052: LOG_TRACE ( TRACE_DMASND, "Microwire new right volume=0x%x\n", microwire.data & 0x1f );
1.1.1.7 root 1053: microwire.rightVolume = LMC1992_LeftRight_Volume_Table[microwire.data & 0x1f];
1.1.1.11! root 1054: lmc1992.right_gain = (microwire.rightVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.7 root 1055: break;
1056: case 5:
1057: /* Left channel volume */
1.1.1.8 root 1058: LOG_TRACE ( TRACE_DMASND, "Microwire new left volume=0x%x\n", microwire.data & 0x1f );
1.1.1.7 root 1059: microwire.leftVolume = LMC1992_LeftRight_Volume_Table[microwire.data & 0x1f];
1.1.1.11! root 1060: lmc1992.left_gain = (microwire.leftVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.7 root 1061: break;
1062: default:
1063: /* Do nothing */
1064: break;
1065: }
1066: }
1067: }
1.1.1.4 root 1068:
1069: /**
1070: * Read word from microwire data register (0xff8922).
1071: */
1072: void DmaSnd_MicrowireData_ReadWord(void)
1073: {
1074: /* Shifting is done in DmaSnd_InterruptHandler_Microwire! */
1.1.1.11! root 1075: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 1076: {
! 1077: int FrameCycles, HblCounterVideo, LineCycles;
! 1078: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 1079: LOG_TRACE_PRINT("Microwire data read: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 1080: IoMem_ReadWord(0xff8922),
! 1081: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 1082: }
1.1 root 1083: }
1084:
1085:
1.1.1.3 root 1086: /**
1087: * Write word to microwire data register (0xff8922).
1088: */
1.1 root 1089: void DmaSnd_MicrowireData_WriteWord(void)
1090: {
1.1.1.4 root 1091: /* Only update, if no shift is in progress */
1.1.1.7 root 1092: if (!microwire.mwTransferSteps)
1.1.1.4 root 1093: {
1.1.1.7 root 1094: microwire.data = IoMem_ReadWord(0xff8922);
1.1.1.4 root 1095: /* Start shifting events to simulate a microwire transfer */
1.1.1.7 root 1096: microwire.mwTransferSteps = 16;
1.1.1.9 root 1097: microwire.pendingCyclesOver = 8;
1098: CycInt_AddRelativeInterrupt(microwire.pendingCyclesOver, INT_CPU_CYCLE, INTERRUPT_DMASOUND_MICROWIRE);
1.1.1.4 root 1099: }
1100:
1.1.1.11! root 1101: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 1102: {
! 1103: int FrameCycles, HblCounterVideo, LineCycles;
! 1104: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 1105: LOG_TRACE_PRINT("Microwire data write: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 1106: IoMem_ReadWord(0xff8922),
! 1107: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 1108: }
1.1 root 1109: }
1110:
1111:
1.1.1.3 root 1112: /**
1113: * Read word from microwire mask register (0xff8924).
1114: */
1.1 root 1115: void DmaSnd_MicrowireMask_ReadWord(void)
1116: {
1.1.1.4 root 1117: /* Same as with data register, but mask is rotated, not shifted. */
1.1.1.11! root 1118: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 1119: {
! 1120: int FrameCycles, HblCounterVideo, LineCycles;
! 1121: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 1122: LOG_TRACE_PRINT("Microwire mask read: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 1123: IoMem_ReadWord(0xff8924),
! 1124: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 1125: }
1.1 root 1126: }
1127:
1128:
1.1.1.3 root 1129: /**
1130: * Write word to microwire mask register (0xff8924).
1131: */
1.1 root 1132: void DmaSnd_MicrowireMask_WriteWord(void)
1133: {
1.1.1.4 root 1134: /* Only update, if no shift is in progress */
1.1.1.7 root 1135: if (!microwire.mwTransferSteps)
1.1.1.4 root 1136: {
1.1.1.7 root 1137: microwire.mask = IoMem_ReadWord(0xff8924);
1.1.1.4 root 1138: }
1139:
1.1.1.11! root 1140: if(LOG_TRACE_LEVEL(TRACE_DMASND))
! 1141: {
! 1142: int FrameCycles, HblCounterVideo, LineCycles;
! 1143: Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
! 1144: LOG_TRACE_PRINT("Microwire mask write: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
! 1145: IoMem_ReadWord(0xff8924),
! 1146: FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
! 1147: }
1.1.1.6 root 1148: }
1149:
1150:
1.1.1.7 root 1151: /*-------------------Bass / Treble filter ---------------------------*/
1.1.1.6 root 1152:
1.1.1.7 root 1153: /**
1154: * Left voice Filter for Bass/Treble.
1155: */
1156: static float DmaSnd_IIRfilterL(float xn)
1157: {
1158: static float data[2] = { 0.0, 0.0 };
1159: float a, yn;
1.1.1.6 root 1160:
1.1.1.7 root 1161: /* Input coefficients */
1162: /* biquad1 Note: 'a' coefficients are subtracted */
1.1.1.8 root 1163: a = lmc1992.left_gain * xn; /* a=g*xn; */
1.1.1.7 root 1164: a -= lmc1992.coef[0] * data[0]; /* a1; wn-1 */
1165: a -= lmc1992.coef[1] * data[1]; /* a2; wn-2 */
1166: /* If coefficient scale */
1167: /* factor = 0.5 then */
1168: /* multiply by 2 */
1169: /* Output coefficients */
1170: yn = lmc1992.coef[2] * a; /* b0; */
1171: yn += lmc1992.coef[3] * data[0]; /* b1; */
1172: yn += lmc1992.coef[4] * data[1]; /* b2; */
1.1.1.6 root 1173:
1.1.1.7 root 1174: data[1] = data[0]; /* wn-1 -> wn-2; */
1175: data[0] = a; /* wn -> wn-1 */
1176: return yn;
1.1.1.6 root 1177: }
1178:
1179:
1180: /**
1.1.1.7 root 1181: * Right voice Filter for Bass/Treble.
1.1.1.6 root 1182: */
1.1.1.7 root 1183: static float DmaSnd_IIRfilterR(float xn)
1.1.1.6 root 1184: {
1.1.1.7 root 1185: static float data[2] = { 0.0, 0.0 };
1186: float a, yn;
1187:
1188: /* Input coefficients */
1189: /* biquad1 Note: 'a' coefficients are subtracted */
1.1.1.8 root 1190: a = lmc1992.right_gain * xn; /* a=g*xn; */
1.1.1.7 root 1191: a -= lmc1992.coef[0]*data[0]; /* a1; wn-1 */
1192: a -= lmc1992.coef[1]*data[1]; /* a2; wn-2 */
1193: /* If coefficient scale */
1194: /* factor = 0.5 then */
1195: /* multiply by 2 */
1196: /* Output coefficients */
1197: yn = lmc1992.coef[2]*a; /* b0; */
1198: yn += lmc1992.coef[3]*data[0]; /* b1; */
1199: yn += lmc1992.coef[4]*data[1]; /* b2; */
1200:
1201: data[1] = data[0]; /* wn-1 -> wn-2; */
1202: data[0] = a; /* wn -> wn-1 */
1203: return yn;
1.1.1.6 root 1204: }
1205:
1206: /**
1.1.1.7 root 1207: * LowPass Filter Left
1.1.1.6 root 1208: */
1.1.1.8 root 1209: static Sint16 DmaSnd_LowPassFilterLeft(Sint16 in)
1.1.1.6 root 1210: {
1.1.1.8 root 1211: static Sint16 lowPassFilter[2] = { 0, 0 };
1212: static Sint16 out = 0;
1.1.1.6 root 1213:
1.1.1.8 root 1214: if (DmaSnd_LowPass)
1215: {
1216: out = lowPassFilter[0] + (lowPassFilter[1]<<1) + in;
1217: lowPassFilter[0] = lowPassFilter[1];
1218: lowPassFilter[1] = in;
1.1.1.6 root 1219:
1.1.1.8 root 1220: return out; /* Filter Gain = 4 */
1221: }else
1222: {
1223: return in << 2;
1224: }
1.1.1.6 root 1225: }
1226:
1227: /**
1.1.1.7 root 1228: * LowPass Filter Right
1.1.1.6 root 1229: */
1.1.1.8 root 1230: static Sint16 DmaSnd_LowPassFilterRight(Sint16 in)
1.1.1.6 root 1231: {
1.1.1.8 root 1232: static Sint16 lowPassFilter[2] = { 0, 0 };
1233: static Sint16 out = 0;
1.1.1.6 root 1234:
1.1.1.8 root 1235: if (DmaSnd_LowPass)
1236: {
1237: out = lowPassFilter[0] + (lowPassFilter[1]<<1) + in;
1238: lowPassFilter[0] = lowPassFilter[1];
1239: lowPassFilter[1] = in;
1.1.1.6 root 1240:
1.1.1.8 root 1241: return out; /* Filter Gain = 4 */
1242: }else
1243: {
1244: return in << 2;
1245: }
1.1.1.6 root 1246: }
1247:
1248: /**
1.1.1.7 root 1249: * Set Bass and Treble tone level
1.1.1.6 root 1250: */
1.1.1.7 root 1251: static void DmaSnd_Set_Tone_Level(int set_bass, int set_treb)
1252: {
1253: /* 13 levels; 0 through 12 correspond with -12dB to 12dB in 2dB steps */
1254: lmc1992.coef[0] = lmc1992.treb_table[set_treb].a1 + lmc1992.bass_table[set_bass].a1;
1255: lmc1992.coef[1] = lmc1992.treb_table[set_treb].a1 * lmc1992.bass_table[set_bass].a1;
1256: lmc1992.coef[2] = lmc1992.treb_table[set_treb].b0 * lmc1992.bass_table[set_bass].b0;
1257: lmc1992.coef[3] = lmc1992.treb_table[set_treb].b0 * lmc1992.bass_table[set_bass].b1 +
1258: lmc1992.treb_table[set_treb].b1 * lmc1992.bass_table[set_bass].b0;
1259: lmc1992.coef[4] = lmc1992.treb_table[set_treb].b1 * lmc1992.bass_table[set_bass].b1;
1.1.1.6 root 1260: }
1261:
1262:
1263: /**
1.1.1.7 root 1264: * Compute the first order bass shelf
1.1.1.6 root 1265: */
1.1.1.7 root 1266: static struct first_order_s *DmaSnd_Bass_Shelf(float g, float fc, float Fs)
1.1.1.6 root 1267: {
1.1.1.7 root 1268: static struct first_order_s bass;
1269: float a1;
1.1.1.6 root 1270:
1.1.1.7 root 1271: /* g, fc, Fs must be positve real numbers > 0.0 */
1272: if (g < 1.0)
1273: bass.a1 = a1 = (tanf(M_PI*fc/Fs) - g ) / (tanf(M_PI*fc/Fs) + g );
1274: else
1275: bass.a1 = a1 = (tanf(M_PI*fc/Fs) - 1.0) / (tanf(M_PI*fc/Fs) + 1.0);
1.1.1.6 root 1276:
1.1.1.7 root 1277: bass.b0 = (1.0 + a1) * (g - 1.0) / 2.0 + 1.0;
1278: bass.b1 = (1.0 + a1) * (g - 1.0) / 2.0 + a1;
1.1.1.6 root 1279:
1.1.1.7 root 1280: return &bass;
1.1.1.6 root 1281: }
1282:
1283:
1284: /**
1.1.1.7 root 1285: * Compute the first order treble shelf
1.1.1.6 root 1286: */
1.1.1.7 root 1287: static struct first_order_s *DmaSnd_Treble_Shelf(float g, float fc, float Fs)
1.1.1.6 root 1288: {
1.1.1.7 root 1289: static struct first_order_s treb;
1290: float a1;
1.1.1.6 root 1291:
1.1.1.7 root 1292: /* g, fc, Fs must be positve real numbers > 0.0 */
1293: if (g < 1.0)
1294: treb.a1 = a1 = (g*tanf(M_PI*fc/Fs) - 1.0) / (g*tanf(M_PI*fc/Fs) + 1.0);
1295: else
1296: treb.a1 = a1 = (tanf(M_PI*fc/Fs) - 1.0) / (tanf(M_PI*fc/Fs) + 1.0);
1.1.1.6 root 1297:
1.1.1.7 root 1298: treb.b0 = 1.0 + (1.0 - a1) * (g - 1.0) / 2.0;
1299: treb.b1 = a1 + (a1 - 1.0) * (g - 1.0) / 2.0;
1300:
1301: return &treb;
1.1.1.6 root 1302: }
1303:
1.1.1.7 root 1304:
1.1.1.6 root 1305: /**
1.1.1.7 root 1306: * Compute the bass and treble tables (nAudioFrequency)
1.1.1.6 root 1307: */
1.1.1.7 root 1308: void DmaSnd_Init_Bass_and_Treble_Tables(void)
1.1.1.6 root 1309: {
1.1.1.7 root 1310: struct first_order_s *bass;
1311: struct first_order_s *treb;
1312:
1.1.1.8 root 1313: float dB_adjusted, dB, g, fc_bt, fc_tt, Fs;
1.1.1.7 root 1314: int n;
1315:
1316: fc_bt = 118.2763;
1317: fc_tt = 8438.756;
1318: Fs = (float)nAudioFrequency;
1319:
1320: if ((Fs < 8000.0) || (Fs > 96000.0))
1321: Fs = 44100.0;
1322:
1.1.1.8 root 1323: if (fc_tt > 0.5*0.8*Fs)
1324: {
1325: fc_tt = 0.5*0.8*Fs;
1326: dB_adjusted = 2.0 * 0.5*0.8*Fs/fc_tt;
1327: }else
1328: {
1329: dB_adjusted = 2.0;
1330: }
1331:
1332: for (dB = dB_adjusted*(TONE_STEPS-1)/2, n = TONE_STEPS; n--; dB -= dB_adjusted)
1333: {
1334: g = powf(10.0, dB/20.0); /* 12dB to -12dB */
1335:
1336: treb = DmaSnd_Treble_Shelf(g, fc_tt, Fs);
1337:
1338: lmc1992.treb_table[n].a1 = treb->a1;
1339: lmc1992.treb_table[n].b0 = treb->b0;
1340: lmc1992.treb_table[n].b1 = treb->b1;
1341: }
1342:
1.1.1.7 root 1343: for (dB = 12.0, n = TONE_STEPS; n--; dB -= 2.0)
1344: {
1345: g = powf(10.0, dB/20.0); /* 12dB to -12dB */
1346:
1347: bass = DmaSnd_Bass_Shelf(g, fc_bt, Fs);
1348:
1349: lmc1992.bass_table[n].a1 = bass->a1;
1350: lmc1992.bass_table[n].b0 = bass->b0;
1351: lmc1992.bass_table[n].b1 = bass->b1;
1352: }
1353:
1354: DmaSnd_Set_Tone_Level(LMC1992_Bass_Treble_Table[microwire.bass & 0xf],
1355: LMC1992_Bass_Treble_Table[microwire.treble & 0xf]);
1.1.1.8 root 1356:
1357: /* Initialize IIR Filter Gain and use as a Volume Control */
1.1.1.11! root 1358: lmc1992.left_gain = (microwire.leftVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
! 1359: lmc1992.right_gain = (microwire.rightVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.8 root 1360:
1361: /* Anti-alias filter is not required when nAudioFrequency == 50066 Hz */
1362: if (nAudioFrequency>50000 && nAudioFrequency<50100)
1363: DmaSnd_LowPass = false;
1364: else
1365: DmaSnd_LowPass = true;
1.1 root 1366: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.