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

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

unix.superglobalmegacorp.com

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