Annotation of hatari/src/dmaSnd.c, revision 1.1.1.12

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
1.1.1.12! root       48:                00 : DMA sound only
        !            49:                01 : DMA sound + input 1 (YM2149 + AUDIOI, full frequency range)
        !            50:                10 : DMA sound + input 2 (YM2149 + AUDIOI, Low Pass Filter) -> DMA sound only
        !            51:                11 : DMA sound + input 3 (not connected) -> DMA sound only
1.1.1.7   root       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(&microwire, 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.12! root      496:                                        /* DMA and YM2149 mixing */
        !           497:                                        MixBuffer[nBufIdx][0] = MixBuffer[nBufIdx][0] + dma.FrameLeft * -((256*3/4)/4)/4;
        !           498:                                        MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][1] + dma.FrameRight * -((256*3/4)/4)/4;
1.1.1.8   root      499:                                        break;
                    500:                                default:
1.1.1.12! root      501:                                        /* mixing=0 DMA only */
        !           502:                                        /* mixing=2 DMA and input 2 (YM2149 LPF) -> DMA */
        !           503:                                        /* mixing=3 DMA and input 3 -> DMA */
        !           504:                                        MixBuffer[nBufIdx][0] = dma.FrameLeft * -((256*3/4)/4)/4;
        !           505:                                        MixBuffer[nBufIdx][1] = dma.FrameRight * -((256*3/4)/4)/4;
1.1.1.8   root      506:                                        break;
                    507:                        }
                    508:                }
                    509: 
                    510:                /* Apply LMC1992 sound modifications (Bass and Treble) */
                    511:                DmaSnd_Apply_LMC ( nMixBufIdx , nSamplesToGenerate );
1.1.1.10  root      512: 
1.1       root      513:                return;
1.1.1.8   root      514:        }
                    515: 
1.1       root      516: 
1.1.1.8   root      517:        /* DMA Audio ON or FIFO not empty yet */
1.1       root      518: 
1.1.1.8   root      519:        /* Compute ratio between DMA's sound frequency and host computer's sound frequency, */
                    520:        /* use << 32 to simulate floating point precision */
                    521:        FreqRatio = ( ((Sint64)DmaSnd_DetectSampleRate()) << 32 ) / nAudioFrequency;
1.1.1.7   root      522: 
                    523:        if (dma.soundMode & DMASNDMODE_MONO)
1.1.1.3   root      524:        {
                    525:                /* Mono 8-bit */
1.1       root      526:                for (i = 0; i < nSamplesToGenerate; i++)
                    527:                {
1.1.1.8   root      528:                        if ( DmaInitSample )
                    529:                        {
                    530:                                MonoByte = DmaSnd_FIFO_PullByte ();
1.1.1.9   root      531:                                dma.FrameLeft  = DmaSnd_LowPassFilterLeft( (Sint16)MonoByte );
                    532:                                dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)MonoByte );
1.1.1.8   root      533:                                DmaInitSample = false;
1.1.1.7   root      534:                        }
                    535: 
1.1       root      536:                        nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.7   root      537: 
                    538:                        switch (microwire.mixing) {
                    539:                                case 1:
1.1.1.12! root      540:                                        /* DMA and YM2149 mixing */
1.1.1.11  root      541:                                        MixBuffer[nBufIdx][0] = MixBuffer[nBufIdx][0] + dma.FrameLeft * -((256*3/4)/4)/4;
1.1.1.7   root      542:                                        break;
                    543:                                default:
1.1.1.12! root      544:                                        /* mixing=0 DMA only */
        !           545:                                        /* mixing=2 DMA and input 2 (YM2149 LPF) -> DMA */
        !           546:                                        /* mixing=3 DMA and input 3 -> DMA */
        !           547:                                        MixBuffer[nBufIdx][0] = dma.FrameLeft * -((256*3/4)/4)/4;
1.1.1.7   root      548:                                        break;
                    549:                        }
                    550: 
1.1.1.8   root      551:                        MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][0];          /* right = left */
                    552: 
                    553:                        /* Increase freq counter */
                    554:                        frameCounter_float += FreqRatio;
                    555:                        n = frameCounter_float >> 32;                           /* number of samples to skip */
                    556:                        while ( n > 0 )                                         /* pull as many bytes from the FIFO as needed */
                    557:                        {
                    558:                                MonoByte = DmaSnd_FIFO_PullByte ();
1.1.1.9   root      559:                                dma.FrameLeft  = DmaSnd_LowPassFilterLeft( (Sint16)MonoByte );
                    560:                                dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)MonoByte );
1.1.1.8   root      561:                                n--;
1.1.1.7   root      562:                        }
1.1.1.8   root      563:                        frameCounter_float &= 0xffffffff;                       /* only keep the fractional part */
1.1       root      564:                }
                    565:        }
                    566:        else
                    567:        {
1.1.1.3   root      568:                /* Stereo 8-bit */
1.1       root      569:                for (i = 0; i < nSamplesToGenerate; i++)
                    570:                {
1.1.1.8   root      571:                        if ( DmaInitSample )
                    572:                        {
                    573:                                LeftByte = DmaSnd_FIFO_PullByte ();
                    574:                                RightByte = DmaSnd_FIFO_PullByte ();
                    575:                                dma.FrameLeft  = DmaSnd_LowPassFilterLeft( (Sint16)LeftByte );
                    576:                                dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)RightByte );
                    577:                                DmaInitSample = false;
1.1.1.7   root      578:                        }
                    579: 
1.1       root      580:                        nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.8   root      581: 
1.1.1.7   root      582:                        switch (microwire.mixing) {
                    583:                                case 1:
1.1.1.12! root      584:                                        /* DMA and YM2149 mixing */
1.1.1.11  root      585:                                        MixBuffer[nBufIdx][0] = MixBuffer[nBufIdx][0] + dma.FrameLeft * -((256*3/4)/4)/4;
                    586:                                        MixBuffer[nBufIdx][1] = MixBuffer[nBufIdx][1] + dma.FrameRight * -((256*3/4)/4)/4;
1.1.1.7   root      587:                                        break;
1.1.1.12! root      588:                                default:
        !           589:                                        /* mixing=0 DMA only */
        !           590:                                        /* mixing=2 DMA and input 2 (YM2149 LPF) -> DMA */
        !           591:                                        /* mixing=3 DMA and input 3 -> DMA */
1.1.1.11  root      592:                                        MixBuffer[nBufIdx][0] = dma.FrameLeft * -((256*3/4)/4)/4;
                    593:                                        MixBuffer[nBufIdx][1] = dma.FrameRight * -((256*3/4)/4)/4;
1.1.1.7   root      594:                                        break;
                    595:                        }
                    596: 
1.1.1.8   root      597:                        /* Increase freq counter */
                    598:                        frameCounter_float += FreqRatio;
                    599:                        n = frameCounter_float >> 32;                           /* number of samples to skip */
                    600:                        while ( n > 0 )                                         /* pull as many bytes from the FIFO as needed */
                    601:                        {
                    602:                                LeftByte = DmaSnd_FIFO_PullByte ();
                    603:                                RightByte = DmaSnd_FIFO_PullByte ();
                    604:                                dma.FrameLeft  = DmaSnd_LowPassFilterLeft( (Sint16)LeftByte );
                    605:                                dma.FrameRight = DmaSnd_LowPassFilterRight( (Sint16)RightByte );
                    606:                                n--;
1.1.1.7   root      607:                        }
1.1.1.8   root      608:                        frameCounter_float &= 0xffffffff;                       /* only keep the fractional part */
1.1       root      609:                }
                    610:        }
1.1.1.7   root      611: 
1.1.1.8   root      612:        /* Apply LMC1992 sound modifications (Bass and Treble) */
                    613:        DmaSnd_Apply_LMC ( nMixBufIdx , nSamplesToGenerate );
                    614: }
                    615: 
                    616: 
                    617: /*-----------------------------------------------------------------------*/
                    618: /**
                    619:  * Apply LMC1992 sound modifications (Bass and Treble)
                    620:  * The Bass and Treble get samples at nAudioFrequency rate.
                    621:  * The tone control's sampling frequency must be at least 22050 Hz to sound good.
                    622:  */
                    623: static void DmaSnd_Apply_LMC(int nMixBufIdx, int nSamplesToGenerate)
                    624: {
                    625:        int nBufIdx;
                    626:        int i;
1.1.1.11  root      627:        Sint32 sample;
1.1.1.7   root      628: 
                    629:        /* Apply LMC1992 sound modifications (Left, Right and Master Volume) */
                    630:        for (i = 0; i < nSamplesToGenerate; i++) {
                    631:                nBufIdx = (nMixBufIdx + i) % MIXBUFFER_SIZE;
1.1.1.11  root      632: 
                    633:                sample = DmaSnd_IIRfilterL( Subsonic_IIR_HPF_Left( MixBuffer[nBufIdx][0]));
                    634:                if (sample<-32767)                                              /* check for overflow to clip waveform */
                    635:                        sample = -32767;
                    636:                else if (sample>32767)
                    637:                        sample = 32767;
                    638:                MixBuffer[nBufIdx][0] = sample;
                    639: 
                    640:                sample = DmaSnd_IIRfilterR( Subsonic_IIR_HPF_Right(MixBuffer[nBufIdx][1]));
                    641:                if (sample<-32767)                                              /* check for overflow to clip waveform */
                    642:                        sample = -32767;
                    643:                else if (sample>32767)
                    644:                        sample = 32767;
                    645:                MixBuffer[nBufIdx][1] = sample;
1.1.1.8   root      646:        }
1.1       root      647: }
                    648: 
                    649: 
                    650: /*-----------------------------------------------------------------------*/
1.1.1.3   root      651: /**
1.1.1.8   root      652:  * STE DMA sound is using an 8 bytes FIFO that is checked and filled on each HBL
                    653:  * (at 50066 Hz 8 bit stereo, the DMA requires approx 6.5 new bytes per HBL)
                    654:  * Calling Sound_Update on each HBL allows to emulate some programs that modify
                    655:  * the data between FrameStart and FrameEnd while DMA sound is ON
                    656:  * (eg the demo 'Mental Hangover' or the game 'Power Up Plus')
                    657:  * We first check if the FIFO needs to be refilled, then we call Sound_Update.
                    658:  * This function should be called from the HBL's handler (in video.c)
1.1.1.3   root      659:  */
1.1.1.8   root      660: void DmaSnd_STE_HBL_Update(void)
1.1       root      661: {
1.1.1.8   root      662:        if ( ( ConfigureParams.System.nMachineType != MACHINE_STE )
                    663:          && ( ConfigureParams.System.nMachineType != MACHINE_MEGA_STE ) )
                    664:                return;
1.1       root      665: 
1.1.1.8   root      666: 
                    667:        /* The DMA starts refilling the FIFO when display is OFF (eg cycle 376 in low res 50 Hz) */
                    668:        DmaSnd_FIFO_Refill ();
                    669: 
                    670:        /* If DMA sound is ON or FIFO is not empty, update sound */
                    671:        if  ( (nDmaSoundControl & DMASNDCTRL_PLAY) || ( dma.FIFO_NbBytes > 0 ) )
                    672:                Sound_Update(false);
                    673: 
                    674:        /* As long as display is OFF, the DMA will refill the FIFO after playing some samples during the HBL */
                    675:        DmaSnd_FIFO_Refill ();
1.1       root      676: }
                    677: 
                    678: 
                    679: /*-----------------------------------------------------------------------*/
1.1.1.3   root      680: /**
1.1.1.8   root      681:  * Return current frame counter address (value is always even)
1.1.1.3   root      682:  */
1.1       root      683: static Uint32 DmaSnd_GetFrameCount(void)
                    684: {
                    685:        Uint32 nActCount;
                    686: 
1.1.1.8   root      687:        /* Update sound to get the current DMA frame address */
                    688:        Sound_Update(false);
                    689: 
1.1       root      690:        if (nDmaSoundControl & DMASNDCTRL_PLAY)
1.1.1.8   root      691:                nActCount = dma.frameCounterAddr;
1.1       root      692:        else
1.1.1.8   root      693:                nActCount = (IoMem[0xff8903] << 16) | (IoMem[0xff8905] << 8) | (IoMem[0xff8907] & ~1);
1.1       root      694: 
                    695:        return nActCount;
                    696: }
                    697: 
                    698: 
                    699: /*-----------------------------------------------------------------------*/
1.1.1.3   root      700: /**
                    701:  * Read word from sound control register (0xff8900).
                    702:  */
1.1       root      703: void DmaSnd_SoundControl_ReadWord(void)
                    704: {
                    705:        IoMem_WriteWord(0xff8900, nDmaSoundControl);
1.1.1.6   root      706: 
1.1.1.11  root      707:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                    708:        {
                    709:                 int FrameCycles, HblCounterVideo, LineCycles;
                    710:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                    711:                LOG_TRACE_PRINT("DMA snd control read: 0x%04x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    712:                        nDmaSoundControl,
                    713:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                    714:        }
1.1       root      715: }
                    716: 
                    717: 
                    718: /*-----------------------------------------------------------------------*/
1.1.1.3   root      719: /**
1.1.1.8   root      720:  * Write word to sound control register (0xff8900).
                    721:  */
                    722: void DmaSnd_SoundControl_WriteWord(void)
                    723: {
                    724:        Uint16 nNewSndCtrl;
                    725: 
1.1.1.11  root      726:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                    727:        {
                    728:                 int FrameCycles, HblCounterVideo, LineCycles;
                    729:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                    730:                LOG_TRACE_PRINT("DMA snd control write: 0x%04x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    731:                        IoMem_ReadWord(0xff8900),
                    732:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                    733:        }
1.1.1.8   root      734: 
                    735:         /* Before starting/stopping DMA sound, create samples up until this point with current values */
                    736:        Sound_Update(false);
                    737: 
                    738:        nNewSndCtrl = IoMem_ReadWord(0xff8900) & 3;
                    739: 
                    740:        if (!(nDmaSoundControl & DMASNDCTRL_PLAY) && (nNewSndCtrl & DMASNDCTRL_PLAY))
                    741:        {
                    742:                LOG_TRACE(TRACE_DMASND, "DMA snd control write: starting dma sound output\n");
                    743:                DmaInitSample = true;
                    744:                frameCounter_float = 0;
                    745:                DmaSnd_StartNewFrame();
                    746:        }
                    747:        else if ((nDmaSoundControl & DMASNDCTRL_PLAY) && !(nNewSndCtrl & DMASNDCTRL_PLAY))
                    748:        {
                    749:                LOG_TRACE(TRACE_DMASND, "DMA snd control write: stopping dma sound output\n");
                    750:        }
                    751: 
                    752:        nDmaSoundControl = nNewSndCtrl;
                    753: }
                    754: 
                    755: 
                    756: /*-----------------------------------------------------------------------*/
                    757: /**
1.1.1.3   root      758:  * Read word from sound frame count high register (0xff8909).
                    759:  */
1.1       root      760: void DmaSnd_FrameCountHigh_ReadByte(void)
                    761: {
                    762:        IoMem_WriteByte(0xff8909, DmaSnd_GetFrameCount() >> 16);
                    763: }
                    764: 
                    765: 
                    766: /*-----------------------------------------------------------------------*/
1.1.1.3   root      767: /**
                    768:  * Read word from sound frame count medium register (0xff890b).
                    769:  */
1.1       root      770: void DmaSnd_FrameCountMed_ReadByte(void)
                    771: {
                    772:        IoMem_WriteByte(0xff890b, DmaSnd_GetFrameCount() >> 8);
                    773: }
                    774: 
                    775: 
                    776: /*-----------------------------------------------------------------------*/
1.1.1.3   root      777: /**
                    778:  * Read word from sound frame count low register (0xff890d).
                    779:  */
1.1       root      780: void DmaSnd_FrameCountLow_ReadByte(void)
                    781: {
                    782:        IoMem_WriteByte(0xff890d, DmaSnd_GetFrameCount());
                    783: }
                    784: 
                    785: 
                    786: /*-----------------------------------------------------------------------*/
1.1.1.3   root      787: /**
1.1.1.8   root      788:  * Write bytes to various registers with no action.
                    789:  */
                    790: void DmaSnd_FrameStartHigh_WriteByte(void)
                    791: {
1.1.1.11  root      792:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                    793:        {
                    794:                 int FrameCycles, HblCounterVideo, LineCycles;
                    795:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                    796:                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",
                    797:                        IoMem_ReadByte(0xff8903) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr  ,
                    798:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                    799:        }
1.1.1.8   root      800: }
                    801: 
                    802: void DmaSnd_FrameStartMed_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 med: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    809:                        IoMem_ReadByte(0xff8905) , 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_FrameStartLow_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 low: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    821:                        IoMem_ReadByte(0xff8907) , 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_FrameCountHigh_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 count high: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    833:                        IoMem_ReadByte(0xff8909) , 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_FrameCountMed_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 med: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    845:                        IoMem_ReadByte(0xff890b) , 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_FrameCountLow_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 low: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    857:                        IoMem_ReadByte(0xff890d) , 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_FrameEndHigh_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 end high: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    869:                        IoMem_ReadByte(0xff890f) , 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_FrameEndMed_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 med: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    881:                        IoMem_ReadByte(0xff8911) , 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_FrameEndLow_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 low: 0x%02x at pos %d/%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    893:                        IoMem_ReadByte(0xff8913) , dma.frameCounterAddr - dma.frameStartAddr , dma.frameEndAddr - dma.frameStartAddr  ,
                    894:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                    895:        }
1.1.1.8   root      896: }
                    897: 
                    898: 
                    899: /*-----------------------------------------------------------------------*/
                    900: /**
1.1.1.7   root      901:  * Read word from sound mode register (0xff8921).
1.1.1.3   root      902:  */
1.1.1.7   root      903: void DmaSnd_SoundModeCtrl_ReadByte(void)
1.1       root      904: {
1.1.1.7   root      905:        IoMem_WriteByte(0xff8921, dma.soundMode);
1.1.1.6   root      906: 
1.1.1.11  root      907:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                    908:        {
                    909:                 int FrameCycles, HblCounterVideo, LineCycles;
                    910:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                    911:                LOG_TRACE_PRINT("DMA snd mode read: 0x%02x video_cyc=%d %d@%d pc=%x instr_cycle %d\n", dma.soundMode,
                    912:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                    913:        }
1.1       root      914: }
                    915: 
                    916: 
                    917: /*-----------------------------------------------------------------------*/
1.1.1.3   root      918: /**
1.1.1.7   root      919:  * Write word to sound mode register (0xff8921).
1.1.1.3   root      920:  */
1.1.1.7   root      921: void DmaSnd_SoundModeCtrl_WriteByte(void)
1.1       root      922: {
1.1.1.8   root      923:        Uint16  SoundModeNew;
                    924: 
                    925:        SoundModeNew = IoMem_ReadByte(0xff8921);
                    926: 
1.1.1.11  root      927:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                    928:        {
                    929:                 int FrameCycles, HblCounterVideo, LineCycles;
                    930:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                    931:                LOG_TRACE_PRINT("DMA snd mode write: 0x%02x mode=%s freq=%d video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                    932:                        SoundModeNew, SoundModeNew & DMASNDMODE_MONO ? "mono" : "stereo" , DmaSndSampleRates[ SoundModeNew & 3 ],
                    933:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                    934:        }
1.1.1.8   root      935: 
                    936:        /* We maskout to only bits that exist on a real STE */
                    937:        SoundModeNew &= 0x8f;
1.1.1.3   root      938: 
1.1.1.8   root      939:        /* Are we switching from mono to stereo ? */
                    940:        if ( ( dma.soundMode & DMASNDMODE_MONO ) && ( ( SoundModeNew & DMASNDMODE_MONO ) == 0 ) )
                    941:                DmaSnd_FIFO_SetStereo ();
                    942: 
                    943:        dma.soundMode = SoundModeNew;
                    944:        /* We also write the masked value back into the emulated hw registers so we have a correct value there */
1.1.1.7   root      945:        IoMem_WriteByte(0xff8921, dma.soundMode);
1.1       root      946: }
                    947: 
1.1.1.8   root      948: 
1.1.1.6   root      949: /* ---------------------- Microwire / LMC 1992  ---------------------- */
                    950: 
1.1.1.3   root      951: /**
1.1.1.4   root      952:  * Handle the shifting/rotating of the microwire registers
                    953:  * The microwire regs should be done after 16 usec = 32 NOPs = 128 cycles.
                    954:  * That means we have to shift 16 times with a delay of 8 cycles.
1.1.1.12! root      955:  * Microwire uses the MWK clock signal at 1 Mhz
1.1.1.3   root      956:  */
1.1.1.4   root      957: void DmaSnd_InterruptHandler_Microwire(void)
1.1       root      958: {
1.1.1.12! root      959:        int     i;
        !           960:        Uint16  cmd;
        !           961:        int     cmd_len;
1.1.1.10  root      962: 
                    963:        /* If emulated computer is the Falcon, let's the crossbar Microwire code do the job. */
                    964:        if (ConfigureParams.System.nMachineType == MACHINE_FALCON) {
                    965:                Crossbar_InterruptHandler_Microwire();
                    966:                return;
                    967:        }
1.1.1.7   root      968:        
1.1.1.9   root      969:        /* How many cycle was this sound interrupt delayed (>= 0) */
                    970:        microwire.pendingCyclesOver += -INT_CONVERT_FROM_INTERNAL ( PendingInterruptCount , INT_CPU_CYCLE );
1.1.1.4   root      971:        /* Remove this interrupt from list and re-order */
1.1.1.7   root      972:        CycInt_AcknowledgeInterrupt();
1.1.1.4   root      973: 
1.1.1.9   root      974:        /* Shift the mask and data according to the number of cycles (8 cycles for a shift) */
                    975:        do
1.1       root      976:        {
1.1.1.9   root      977:                --microwire.mwTransferSteps;
                    978:                        /* Shift data register until it becomes zero. */
1.1.1.7   root      979:                IoMem_WriteWord(0xff8922, microwire.data<<(16-microwire.mwTransferSteps));
1.1.1.9   root      980:                        /* Rotate mask register */
                    981:                IoMem_WriteWord(0xff8924, (microwire.mask<<(16-microwire.mwTransferSteps))
                    982:                                                                |(microwire.mask>>microwire.mwTransferSteps));
                    983:                /* 8 cycles for 1 shift */
                    984:                microwire.pendingCyclesOver -= 8;
1.1       root      985:        }
1.1.1.9   root      986:        while ((microwire.mwTransferSteps != 0) && (microwire.pendingCyclesOver >= 8) );
1.1.1.4   root      987: 
1.1.1.9   root      988:        /* Is the transfer finished ? */
1.1.1.7   root      989:        if (microwire.mwTransferSteps > 0)
1.1.1.4   root      990:        {
1.1.1.11  root      991:                /* No ==> start a new internal interrupt to continue to transfer the data */
1.1.1.9   root      992:                microwire.pendingCyclesOver = 8 - microwire.pendingCyclesOver;
                    993:                CycInt_AddRelativeInterrupt(microwire.pendingCyclesOver, INT_CPU_CYCLE, INTERRUPT_DMASOUND_MICROWIRE);
1.1.1.4   root      994:        }
1.1.1.9   root      995:        else 
                    996:        {
                    997:                /* Yes : decode the address + command word according to the binary mask */
1.1.1.12! root      998:                /* According to LMC1992 doc, command starts with the first '1' bit in the mask */
        !           999:                /* and ends when a '0' bits is received in the mask */
        !          1000:                /* If we get a bad command, we must scan the rest of the mask in case there's a valid */
        !          1001:                /* command in the remaining bits */
        !          1002:                /* TODO [NP] : to be really cycle accurate, we should decode the command at the same */
        !          1003:                /* time as we rotate mask/data, instead of doing it when 16 rotations were made. */
        !          1004:                /* But this would not be noticeable, so leave it like this for now */
        !          1005:                cmd = 0;
        !          1006:                cmd_len = 0;
        !          1007:                for ( i=15 ; i>=0 ; i-- )
        !          1008:                        if ( microwire.mask & ( 1 << i ) )
        !          1009:                        {
        !          1010:                                /* Start of command found, wait for next '0' bit or end of mask */
        !          1011:                                do
        !          1012:                                {
        !          1013:                                        cmd <<= 1;
        !          1014:                                        cmd_len++;
        !          1015:                                        if ( microwire.data & ( 1 << i ) )
        !          1016:                                                cmd |= 1;
        !          1017:                                        i--;
        !          1018:                                }
        !          1019:                                while ( ( i >= 0 ) && ( microwire.mask & ( 1 << i ) ) );
        !          1020: 
        !          1021:                                if ( ( cmd_len >= 11 )
        !          1022:                                  && ( ( cmd >> ( cmd_len-2 ) ) & 0x03 ) == 0x02 )
        !          1023:                                        break;                          /* We found a valid command */
        !          1024: 
        !          1025:                                LOG_TRACE ( TRACE_DMASND, "Microwire bad command=0x%x len=%d ignored mask=0x%x data=0x%x\n", cmd , cmd_len , microwire.mask , microwire.data );
        !          1026:                                if ( i < 0 )
        !          1027:                                        return;                         /* All bits were tested, stop here */
        !          1028: 
        !          1029:                                /* Check remaining bits for a possible command */
        !          1030:                                cmd = 0;
        !          1031:                                cmd_len = 0;
1.1.1.7   root     1032:                        }
1.1.1.12! root     1033: //fprintf ( stderr , "mwire cmd=%x len=%d mask=%x data=%x\n" , cmd , cmd_len , microwire.mask , microwire.data );
1.1.1.4   root     1034: 
1.1.1.12! root     1035:                /* The LMC 1992 address (first 2 bits) should be "10", else we ignore the command */
        !          1036:                /* The address should be followed by at least 9 bits ; if more bits were received, */
        !          1037:                /* then only the latest 9 ones should be kept */
        !          1038:                if ( ( cmd_len < 11 )
        !          1039:                  || ( ( cmd >> ( cmd_len-2 ) ) & 0x03 ) != 0x02 )
        !          1040:                {
        !          1041:                        LOG_TRACE ( TRACE_DMASND, "Microwire bad command=0x%x len=%d ignored mask=0x%x data=0x%x\n", cmd , cmd_len , microwire.mask , microwire.data );
1.1.1.7   root     1042:                        return;
1.1.1.12! root     1043:                }
1.1.1.7   root     1044: 
                   1045:                /* Update the LMC 1992 commands */
1.1.1.12! root     1046:                switch ( ( cmd >> 6 ) & 0x7 ) {
1.1.1.7   root     1047:                        case 0:
                   1048:                                /* Mixing command */
1.1.1.12! root     1049:                                LOG_TRACE ( TRACE_DMASND, "Microwire new mixing=0x%x\n", cmd & 0x3 );
        !          1050:                                microwire.mixing = cmd & 0x3;
1.1.1.7   root     1051:                                break;
                   1052:                        case 1:
                   1053:                                /* Bass command */
1.1.1.12! root     1054:                                LOG_TRACE ( TRACE_DMASND, "Microwire new bass=0x%x\n", cmd & 0xf );
        !          1055:                                microwire.bass = cmd & 0xf;
1.1.1.7   root     1056:                                DmaSnd_Set_Tone_Level(LMC1992_Bass_Treble_Table[microwire.bass], 
                   1057:                                                      LMC1992_Bass_Treble_Table[microwire.treble]);
                   1058:                                break;
                   1059:                        case 2: 
                   1060:                                /* Treble command */
1.1.1.12! root     1061:                                LOG_TRACE ( TRACE_DMASND, "Microwire new trebble=0x%x\n", cmd & 0xf );
        !          1062:                                microwire.treble = cmd & 0xf;
1.1.1.7   root     1063:                                DmaSnd_Set_Tone_Level(LMC1992_Bass_Treble_Table[microwire.bass], 
                   1064:                                                      LMC1992_Bass_Treble_Table[microwire.treble]);
                   1065:                                break;
                   1066:                        case 3:
                   1067:                                /* Master volume command */
1.1.1.12! root     1068:                                LOG_TRACE ( TRACE_DMASND, "Microwire new master volume=0x%x\n", cmd & 0x3f );
        !          1069:                                microwire.masterVolume = LMC1992_Master_Volume_Table[ cmd & 0x3f ];
1.1.1.11  root     1070:                                lmc1992.left_gain = (microwire.leftVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
                   1071:                                lmc1992.right_gain = (microwire.rightVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.7   root     1072:                                break;
                   1073:                        case 4:
                   1074:                                /* Right channel volume */
1.1.1.12! root     1075:                                LOG_TRACE ( TRACE_DMASND, "Microwire new right volume=0x%x\n", cmd & 0x1f );
        !          1076:                                microwire.rightVolume = LMC1992_LeftRight_Volume_Table[ cmd & 0x1f ];
1.1.1.11  root     1077:                                lmc1992.right_gain = (microwire.rightVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.7   root     1078:                                break;
                   1079:                        case 5:
                   1080:                                /* Left channel volume */
1.1.1.12! root     1081:                                LOG_TRACE ( TRACE_DMASND, "Microwire new left volume=0x%x\n", cmd & 0x1f );
        !          1082:                                microwire.leftVolume = LMC1992_LeftRight_Volume_Table[ cmd & 0x1f ];
1.1.1.11  root     1083:                                lmc1992.left_gain = (microwire.leftVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.7   root     1084:                                break;
                   1085:                        default:
                   1086:                                /* Do nothing */
1.1.1.12! root     1087:                                LOG_TRACE ( TRACE_DMASND, "Microwire unknown command=0x%x len=%d ignored mask=0x%x data=0x%x\n", cmd , cmd_len , microwire.mask , microwire.data );
1.1.1.7   root     1088:                                break;
                   1089:                }
                   1090:        }
                   1091: }
1.1.1.4   root     1092: 
                   1093: /**
                   1094:  * Read word from microwire data register (0xff8922).
                   1095:  */
                   1096: void DmaSnd_MicrowireData_ReadWord(void)
                   1097: {
                   1098:        /* Shifting is done in DmaSnd_InterruptHandler_Microwire! */
1.1.1.11  root     1099:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                   1100:        {
                   1101:                 int FrameCycles, HblCounterVideo, LineCycles;
                   1102:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   1103:                LOG_TRACE_PRINT("Microwire data read: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                   1104:                        IoMem_ReadWord(0xff8922),
                   1105:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                   1106:        }
1.1       root     1107: }
                   1108: 
                   1109: 
1.1.1.3   root     1110: /**
                   1111:  * Write word to microwire data register (0xff8922).
                   1112:  */
1.1       root     1113: void DmaSnd_MicrowireData_WriteWord(void)
                   1114: {
1.1.1.4   root     1115:        /* Only update, if no shift is in progress */
1.1.1.7   root     1116:        if (!microwire.mwTransferSteps)
1.1.1.4   root     1117:        {
1.1.1.7   root     1118:                microwire.data = IoMem_ReadWord(0xff8922);
1.1.1.4   root     1119:                /* Start shifting events to simulate a microwire transfer */
1.1.1.7   root     1120:                microwire.mwTransferSteps = 16;
1.1.1.9   root     1121:                microwire.pendingCyclesOver = 8;
                   1122:                CycInt_AddRelativeInterrupt(microwire.pendingCyclesOver, INT_CPU_CYCLE, INTERRUPT_DMASOUND_MICROWIRE);
1.1.1.4   root     1123:        }
                   1124: 
1.1.1.11  root     1125:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                   1126:        {
                   1127:                 int FrameCycles, HblCounterVideo, LineCycles;
                   1128:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   1129:                LOG_TRACE_PRINT("Microwire data write: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                   1130:                        IoMem_ReadWord(0xff8922),
                   1131:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                   1132:        }
1.1       root     1133: }
                   1134: 
                   1135: 
1.1.1.3   root     1136: /**
                   1137:  * Read word from microwire mask register (0xff8924).
                   1138:  */
1.1       root     1139: void DmaSnd_MicrowireMask_ReadWord(void)
                   1140: {
1.1.1.4   root     1141:        /* Same as with data register, but mask is rotated, not shifted. */
1.1.1.11  root     1142:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                   1143:        {
                   1144:                 int FrameCycles, HblCounterVideo, LineCycles;
                   1145:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   1146:                LOG_TRACE_PRINT("Microwire mask read: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                   1147:                        IoMem_ReadWord(0xff8924),
                   1148:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                   1149:        }
1.1       root     1150: }
                   1151: 
                   1152: 
1.1.1.3   root     1153: /**
                   1154:  * Write word to microwire mask register (0xff8924).
                   1155:  */
1.1       root     1156: void DmaSnd_MicrowireMask_WriteWord(void)
                   1157: {
1.1.1.4   root     1158:        /* Only update, if no shift is in progress */
1.1.1.7   root     1159:        if (!microwire.mwTransferSteps)
1.1.1.4   root     1160:        {
1.1.1.7   root     1161:                microwire.mask = IoMem_ReadWord(0xff8924);
1.1.1.4   root     1162:        }
                   1163: 
1.1.1.11  root     1164:        if(LOG_TRACE_LEVEL(TRACE_DMASND))
                   1165:        {
                   1166:                 int FrameCycles, HblCounterVideo, LineCycles;
                   1167:                 Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   1168:                LOG_TRACE_PRINT("Microwire mask write: 0x%x video_cyc=%d %d@%d pc=%x instr_cycle %d\n",
                   1169:                        IoMem_ReadWord(0xff8924),
                   1170:                        FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles);
                   1171:        }
1.1.1.6   root     1172: }
                   1173: 
                   1174: 
1.1.1.7   root     1175: /*-------------------Bass / Treble filter ---------------------------*/
1.1.1.6   root     1176: 
1.1.1.7   root     1177: /**
                   1178:  * Left voice Filter for Bass/Treble.
                   1179:  */
                   1180: static float DmaSnd_IIRfilterL(float xn)
                   1181: {
                   1182:        static float data[2] = { 0.0, 0.0 };
                   1183:        float a, yn;
1.1.1.6   root     1184: 
1.1.1.7   root     1185:        /* Input coefficients */
                   1186:        /* biquad1  Note: 'a' coefficients are subtracted */
1.1.1.8   root     1187:        a  = lmc1992.left_gain * xn;            /* a=g*xn;               */
1.1.1.7   root     1188:        a -= lmc1992.coef[0] * data[0];         /* a1;  wn-1             */
                   1189:        a -= lmc1992.coef[1] * data[1];         /* a2;  wn-2             */
                   1190:                                                /* If coefficient scale  */
                   1191:                                                /* factor = 0.5 then     */
                   1192:                                                /* multiply by 2         */
                   1193:        /* Output coefficients */
                   1194:        yn  = lmc1992.coef[2] * a;              /* b0;                   */
                   1195:        yn += lmc1992.coef[3] * data[0];        /* b1;                   */
                   1196:        yn += lmc1992.coef[4] * data[1];        /* b2;                   */
1.1.1.6   root     1197: 
1.1.1.7   root     1198:        data[1] = data[0];                      /* wn-1 -> wn-2;         */
                   1199:        data[0] = a;                            /* wn -> wn-1            */
                   1200:        return yn;
1.1.1.6   root     1201: }
                   1202: 
                   1203: 
                   1204: /**
1.1.1.7   root     1205:  * Right voice Filter for Bass/Treble.
1.1.1.6   root     1206:  */
1.1.1.7   root     1207: static float DmaSnd_IIRfilterR(float xn)
1.1.1.6   root     1208: {
1.1.1.7   root     1209:        static float data[2] = { 0.0, 0.0 };
                   1210:        float a, yn;
                   1211: 
                   1212:        /* Input coefficients */
                   1213:        /* biquad1  Note: 'a' coefficients are subtracted */
1.1.1.8   root     1214:        a  = lmc1992.right_gain * xn;           /* a=g*xn;               */
1.1.1.7   root     1215:        a -= lmc1992.coef[0]*data[0];           /* a1;  wn-1             */
                   1216:        a -= lmc1992.coef[1]*data[1];           /* a2;  wn-2             */
                   1217:                                                /* If coefficient scale  */
                   1218:                                                /* factor = 0.5 then     */
                   1219:                                                /* multiply by 2         */
                   1220:        /* Output coefficients */
                   1221:        yn  = lmc1992.coef[2]*a;                /* b0;                   */
                   1222:        yn += lmc1992.coef[3]*data[0];          /* b1;                   */
                   1223:        yn += lmc1992.coef[4]*data[1];          /* b2;                   */
                   1224: 
                   1225:        data[1] = data[0];                      /* wn-1 -> wn-2;         */
                   1226:        data[0] = a;                            /* wn -> wn-1            */
                   1227:        return yn;
1.1.1.6   root     1228: }
                   1229: 
                   1230: /**
1.1.1.7   root     1231:  * LowPass Filter Left
1.1.1.6   root     1232:  */
1.1.1.8   root     1233: static Sint16 DmaSnd_LowPassFilterLeft(Sint16 in)
1.1.1.6   root     1234: {
1.1.1.8   root     1235:        static  Sint16  lowPassFilter[2] = { 0, 0 };
                   1236:        static  Sint16  out = 0;
1.1.1.6   root     1237: 
1.1.1.8   root     1238:        if (DmaSnd_LowPass)
                   1239:        {
                   1240:                out = lowPassFilter[0] + (lowPassFilter[1]<<1) + in;
                   1241:                lowPassFilter[0] = lowPassFilter[1];
                   1242:                lowPassFilter[1] = in;
1.1.1.6   root     1243: 
1.1.1.8   root     1244:                return out; /* Filter Gain = 4 */
                   1245:        }else
                   1246:        {
                   1247:                return in << 2;
                   1248:        }
1.1.1.6   root     1249: }
                   1250: 
                   1251: /**
1.1.1.7   root     1252:  * LowPass Filter Right
1.1.1.6   root     1253:  */
1.1.1.8   root     1254: static Sint16 DmaSnd_LowPassFilterRight(Sint16 in)
1.1.1.6   root     1255: {
1.1.1.8   root     1256:        static  Sint16  lowPassFilter[2] = { 0, 0 };
                   1257:        static  Sint16  out = 0;
1.1.1.6   root     1258: 
1.1.1.8   root     1259:        if (DmaSnd_LowPass)
                   1260:        {
                   1261:                out = lowPassFilter[0] + (lowPassFilter[1]<<1) + in;
                   1262:                lowPassFilter[0] = lowPassFilter[1];
                   1263:                lowPassFilter[1] = in;
1.1.1.6   root     1264: 
1.1.1.8   root     1265:                return out; /* Filter Gain = 4 */
                   1266:        }else
                   1267:        {
                   1268:                return in << 2;
                   1269:        }
1.1.1.6   root     1270: }
                   1271: 
                   1272: /**
1.1.1.7   root     1273:  * Set Bass and Treble tone level
1.1.1.6   root     1274:  */
1.1.1.7   root     1275: static void DmaSnd_Set_Tone_Level(int set_bass, int set_treb)
                   1276: { 
                   1277:        /* 13 levels; 0 through 12 correspond with -12dB to 12dB in 2dB steps */
                   1278:        lmc1992.coef[0] = lmc1992.treb_table[set_treb].a1 + lmc1992.bass_table[set_bass].a1;
                   1279:        lmc1992.coef[1] = lmc1992.treb_table[set_treb].a1 * lmc1992.bass_table[set_bass].a1;
                   1280:        lmc1992.coef[2] = lmc1992.treb_table[set_treb].b0 * lmc1992.bass_table[set_bass].b0;
                   1281:        lmc1992.coef[3] = lmc1992.treb_table[set_treb].b0 * lmc1992.bass_table[set_bass].b1 +
                   1282:                          lmc1992.treb_table[set_treb].b1 * lmc1992.bass_table[set_bass].b0;
                   1283:        lmc1992.coef[4] = lmc1992.treb_table[set_treb].b1 * lmc1992.bass_table[set_bass].b1;
1.1.1.6   root     1284: }
                   1285: 
                   1286: 
                   1287: /**
1.1.1.7   root     1288:  * Compute the first order bass shelf
1.1.1.6   root     1289:  */
1.1.1.7   root     1290: static struct first_order_s *DmaSnd_Bass_Shelf(float g, float fc, float Fs)
1.1.1.6   root     1291: {
1.1.1.7   root     1292:        static struct first_order_s bass;
                   1293:        float  a1;
1.1.1.6   root     1294: 
1.1.1.7   root     1295:        /* g, fc, Fs must be positve real numbers > 0.0 */
                   1296:        if (g < 1.0)
                   1297:                bass.a1 = a1 = (tanf(M_PI*fc/Fs) - g  ) / (tanf(M_PI*fc/Fs) + g  );
                   1298:        else
                   1299:                bass.a1 = a1 = (tanf(M_PI*fc/Fs) - 1.0) / (tanf(M_PI*fc/Fs) + 1.0);
1.1.1.6   root     1300: 
1.1.1.7   root     1301:        bass.b0 = (1.0 + a1) * (g - 1.0) / 2.0 + 1.0;
                   1302:        bass.b1 = (1.0 + a1) * (g - 1.0) / 2.0 + a1;
1.1.1.6   root     1303: 
1.1.1.7   root     1304:        return &bass;
1.1.1.6   root     1305: }
                   1306: 
                   1307: 
                   1308: /**
1.1.1.7   root     1309:  * Compute the first order treble shelf
1.1.1.6   root     1310:  */
1.1.1.7   root     1311: static struct first_order_s *DmaSnd_Treble_Shelf(float g, float fc, float Fs)
1.1.1.6   root     1312: {
1.1.1.7   root     1313:        static struct first_order_s treb;
                   1314:        float  a1;
1.1.1.6   root     1315: 
1.1.1.7   root     1316:        /* g, fc, Fs must be positve real numbers > 0.0 */
                   1317:        if (g < 1.0)
                   1318:                treb.a1 = a1 = (g*tanf(M_PI*fc/Fs) - 1.0) / (g*tanf(M_PI*fc/Fs) + 1.0);
                   1319:        else
                   1320:                treb.a1 = a1 =   (tanf(M_PI*fc/Fs) - 1.0) /   (tanf(M_PI*fc/Fs) + 1.0);
1.1.1.6   root     1321: 
1.1.1.7   root     1322:        treb.b0 = 1.0 + (1.0 - a1) * (g - 1.0) / 2.0;
                   1323:        treb.b1 = a1  + (a1 - 1.0) * (g - 1.0) / 2.0;
                   1324: 
                   1325:        return &treb;
1.1.1.6   root     1326: }
                   1327: 
1.1.1.7   root     1328: 
1.1.1.6   root     1329: /**
1.1.1.7   root     1330:  * Compute the bass and treble tables (nAudioFrequency)
1.1.1.6   root     1331:  */
1.1.1.7   root     1332: void DmaSnd_Init_Bass_and_Treble_Tables(void)
1.1.1.6   root     1333: {
1.1.1.7   root     1334:        struct first_order_s *bass;
                   1335:        struct first_order_s *treb;
                   1336: 
1.1.1.8   root     1337:        float  dB_adjusted, dB, g, fc_bt, fc_tt, Fs;
1.1.1.7   root     1338:        int    n;
                   1339: 
                   1340:        fc_bt = 118.2763;
                   1341:        fc_tt = 8438.756;
                   1342:        Fs = (float)nAudioFrequency;
                   1343: 
                   1344:        if ((Fs < 8000.0) || (Fs > 96000.0))
                   1345:                Fs = 44100.0;
                   1346: 
1.1.1.8   root     1347:        if (fc_tt > 0.5*0.8*Fs)
                   1348:        {
                   1349:                fc_tt = 0.5*0.8*Fs;
                   1350:                dB_adjusted = 2.0 * 0.5*0.8*Fs/fc_tt;
                   1351:        }else
                   1352:        {
                   1353:                dB_adjusted = 2.0;
                   1354:        }
                   1355: 
                   1356:        for (dB = dB_adjusted*(TONE_STEPS-1)/2, n = TONE_STEPS; n--; dB -= dB_adjusted)
                   1357:        {
                   1358:                g = powf(10.0, dB/20.0);        /* 12dB to -12dB */
                   1359: 
                   1360:                treb = DmaSnd_Treble_Shelf(g, fc_tt, Fs);
                   1361: 
                   1362:                lmc1992.treb_table[n].a1 = treb->a1;
                   1363:                lmc1992.treb_table[n].b0 = treb->b0;
                   1364:                lmc1992.treb_table[n].b1 = treb->b1;
                   1365:        }
                   1366: 
1.1.1.7   root     1367:        for (dB = 12.0, n = TONE_STEPS; n--; dB -= 2.0)
                   1368:        {
                   1369:                g = powf(10.0, dB/20.0);        /* 12dB to -12dB */
                   1370: 
                   1371:                bass = DmaSnd_Bass_Shelf(g, fc_bt, Fs);
                   1372: 
                   1373:                lmc1992.bass_table[n].a1 = bass->a1;
                   1374:                lmc1992.bass_table[n].b0 = bass->b0;
                   1375:                lmc1992.bass_table[n].b1 = bass->b1;
                   1376:        }
                   1377: 
                   1378:        DmaSnd_Set_Tone_Level(LMC1992_Bass_Treble_Table[microwire.bass & 0xf], 
                   1379:                              LMC1992_Bass_Treble_Table[microwire.treble & 0xf]);
1.1.1.8   root     1380: 
                   1381:        /* Initialize IIR Filter Gain and use as a Volume Control */
1.1.1.11  root     1382:        lmc1992.left_gain = (microwire.leftVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
                   1383:        lmc1992.right_gain = (microwire.rightVolume * (Uint32)microwire.masterVolume) * (2.0/(65536.0*65536.0));
1.1.1.8   root     1384: 
                   1385:        /* Anti-alias filter is not required when nAudioFrequency == 50066 Hz */
                   1386:        if (nAudioFrequency>50000 && nAudioFrequency<50100)
                   1387:                DmaSnd_LowPass = false;
                   1388:        else
                   1389:                DmaSnd_LowPass = true;
1.1       root     1390: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.