?? id_sd.c
字號:
//
// ID Engine
// ID_SD.c - Sound Manager for Wolfenstein 3D
// v1.2
// By Jason Blochowiak
//
//
// This module handles dealing with generating sound on the appropriate
// hardware
//
// Depends on: User Mgr (for parm checking)
//
// Globals:
// For User Mgr:
// SoundSourcePresent - Sound Source thingie present?
// SoundBlasterPresent - SoundBlaster card present?
// AdLibPresent - AdLib card present?
// SoundMode - What device is used for sound effects
// (Use SM_SetSoundMode() to set)
// MusicMode - What device is used for music
// (Use SM_SetMusicMode() to set)
// DigiMode - What device is used for digitized sound effects
// (Use SM_SetDigiDevice() to set)
//
// For Cache Mgr:
// NeedsDigitized - load digitized sounds?
// NeedsMusic - load music?
//
#pragma hdrstop // Wierdo thing with MUSE
#include <dos.h>
#ifdef _MUSE_ // Will be defined in ID_Types.h
#include "ID_SD.h"
#else
#include "ID_HEADS.H"
#endif
#pragma hdrstop
#pragma warn -pia
#ifdef nil
#undef nil
#endif
#define nil 0
#define SDL_SoundFinished() {SoundNumber = SoundPriority = 0;}
// Macros for SoundBlaster stuff
#define sbOut(n,b) outportb((n) + sbLocation,b)
#define sbIn(n) inportb((n) + sbLocation)
#define sbWriteDelay() while (sbIn(sbWriteStat) & 0x80);
#define sbReadDelay() while (sbIn(sbDataAvail) & 0x80);
// Macros for AdLib stuff
#define selreg(n) outportb(alFMAddr,n)
#define writereg(n) outportb(alFMData,n)
#define readstat() inportb(alFMStatus)
// Imports from ID_SD_A.ASM
extern void SDL_SetDS(void),
SDL_IndicatePC(boolean on);
extern void interrupt SDL_t0ExtremeAsmService(void),
SDL_t0FastAsmService(void),
SDL_t0SlowAsmService(void);
// Global variables
boolean SoundSourcePresent,
AdLibPresent,
SoundBlasterPresent,SBProPresent,
NeedsDigitized,NeedsMusic,
SoundPositioned;
SDMode SoundMode;
SMMode MusicMode;
SDSMode DigiMode;
longword TimeCount;
word HackCount;
word *SoundTable; // Really * _seg *SoundTable, but that don't work
boolean ssIsTandy;
word ssPort = 2;
int DigiMap[LASTSOUND];
// Internal variables
static boolean SD_Started;
boolean nextsoundpos;
longword TimerDivisor,TimerCount;
static char *ParmStrings[] =
{
"noal",
"nosb",
"nopro",
"noss",
"sst",
"ss1",
"ss2",
"ss3",
nil
};
static void (*SoundUserHook)(void);
soundnames SoundNumber,DigiNumber;
word SoundPriority,DigiPriority;
int LeftPosition,RightPosition;
void interrupt (*t0OldService)(void);
long LocalTime;
word TimerRate;
word NumDigi,DigiLeft,DigiPage;
word _seg *DigiList;
word DigiLastStart,DigiLastEnd;
boolean DigiPlaying;
static boolean DigiMissed,DigiLastSegment;
static memptr DigiNextAddr;
static word DigiNextLen;
// SoundBlaster variables
static boolean sbNoCheck,sbNoProCheck;
static volatile boolean sbSamplePlaying;
static byte sbOldIntMask = -1;
static volatile byte huge *sbNextSegPtr;
static byte sbDMA = 1,
sbDMAa1 = 0x83,sbDMAa2 = 2,sbDMAa3 = 3,
sba1Vals[] = {0x87,0x83,0,0x82},
sba2Vals[] = {0,2,0,6},
sba3Vals[] = {1,3,0,7};
static int sbLocation = -1,sbInterrupt = 7,sbIntVec = 0xf,
sbIntVectors[] = {-1,-1,0xa,0xb,-1,0xd,-1,0xf,-1,-1,-1};
static volatile longword sbNextSegLen;
static volatile SampledSound huge *sbSamples;
static void interrupt (*sbOldIntHand)(void);
static byte sbpOldFMMix,sbpOldVOCMix;
// SoundSource variables
boolean ssNoCheck;
boolean ssActive;
word ssControl,ssStatus,ssData;
byte ssOn,ssOff;
volatile byte far *ssSample;
volatile longword ssLengthLeft;
// PC Sound variables
volatile byte pcLastSample,far *pcSound;
longword pcLengthLeft;
word pcSoundLookup[255];
// AdLib variables
boolean alNoCheck;
byte far *alSound;
word alBlock;
longword alLengthLeft;
longword alTimeCount;
Instrument alZeroInst;
// This table maps channel numbers to carrier and modulator op cells
static byte carriers[9] = { 3, 4, 5,11,12,13,19,20,21},
modifiers[9] = { 0, 1, 2, 8, 9,10,16,17,18},
// This table maps percussive voice numbers to op cells
pcarriers[5] = {19,0xff,0xff,0xff,0xff},
pmodifiers[5] = {16,17,18,20,21};
// Sequencer variables
boolean sqActive;
static word alFXReg;
static ActiveTrack *tracks[sqMaxTracks],
mytracks[sqMaxTracks];
static word sqMode,sqFadeStep;
word far *sqHack,far *sqHackPtr,sqHackLen,sqHackSeqLen;
long sqHackTime;
// Internal routines
void SDL_DigitizedDone(void);
///////////////////////////////////////////////////////////////////////////
//
// SDL_SetTimer0() - Sets system timer 0 to the specified speed
//
///////////////////////////////////////////////////////////////////////////
#pragma argsused
static void
SDL_SetTimer0(word speed)
{
#ifndef TPROF // If using Borland's profiling, don't screw with the timer
asm pushf
asm cli
outportb(0x43,0x36); // Change timer 0
outportb(0x40,speed);
outportb(0x40,speed >> 8);
// Kludge to handle special case for digitized PC sounds
if (TimerDivisor == (1192030 / (TickBase * 100)))
TimerDivisor = (1192030 / (TickBase * 10));
else
TimerDivisor = speed;
asm popf
#else
TimerDivisor = 0x10000;
#endif
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SetIntsPerSec() - Uses SDL_SetTimer0() to set the number of
// interrupts generated by system timer 0 per second
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_SetIntsPerSec(word ints)
{
TimerRate = ints;
SDL_SetTimer0(1192030 / ints);
}
static void
SDL_SetTimerSpeed(void)
{
word rate;
void interrupt (*isr)(void);
if ((DigiMode == sds_PC) && DigiPlaying)
{
rate = TickBase * 100;
isr = SDL_t0ExtremeAsmService;
}
else if
(
(MusicMode == smm_AdLib)
|| ((DigiMode == sds_SoundSource) && DigiPlaying)
)
{
rate = TickBase * 10;
isr = SDL_t0FastAsmService;
}
else
{
rate = TickBase * 2;
isr = SDL_t0SlowAsmService;
}
if (rate != TimerRate)
{
setvect(8,isr);
SDL_SetIntsPerSec(rate);
TimerRate = rate;
}
}
//
// SoundBlaster code
//
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBStopSample() - Stops any active sampled sound and causes DMA
// requests from the SoundBlaster to cease
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_SBStopSample(void)
{
byte is;
asm pushf
asm cli
if (sbSamplePlaying)
{
sbSamplePlaying = false;
sbWriteDelay();
sbOut(sbWriteCmd,0xd0); // Turn off DSP DMA
is = inportb(0x21); // Restore interrupt mask bit
if (sbOldIntMask & (1 << sbInterrupt))
is |= (1 << sbInterrupt);
else
is &= ~(1 << sbInterrupt);
outportb(0x21,is);
}
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBPlaySeg() - Plays a chunk of sampled sound on the SoundBlaster
// Insures that the chunk doesn't cross a bank boundary, programs the DMA
// controller, and tells the SB to start doing DMA requests for DAC
//
///////////////////////////////////////////////////////////////////////////
static longword
SDL_SBPlaySeg(volatile byte huge *data,longword length)
{
unsigned datapage;
longword dataofs,uselen;
uselen = length;
datapage = FP_SEG(data) >> 12;
dataofs = ((FP_SEG(data) & 0xfff) << 4) + FP_OFF(data);
if (dataofs >= 0x10000)
{
datapage++;
dataofs -= 0x10000;
}
if (dataofs + uselen > 0x10000)
uselen = 0x10000 - dataofs;
uselen--;
// Program the DMA controller
asm pushf
asm cli
outportb(0x0a,sbDMA | 4); // Mask off DMA on channel sbDMA
outportb(0x0c,0); // Clear byte ptr flip-flop to lower byte
outportb(0x0b,0x49); // Set transfer mode for D/A conv
outportb(sbDMAa2,(byte)dataofs); // Give LSB of address
outportb(sbDMAa2,(byte)(dataofs >> 8)); // Give MSB of address
outportb(sbDMAa1,(byte)datapage); // Give page of address
outportb(sbDMAa3,(byte)uselen); // Give LSB of length
outportb(sbDMAa3,(byte)(uselen >> 8)); // Give MSB of length
outportb(0x0a,sbDMA); // Re-enable DMA on channel sbDMA
// Start playing the thing
sbWriteDelay();
sbOut(sbWriteCmd,0x14);
sbWriteDelay();
sbOut(sbWriteData,(byte)uselen);
sbWriteDelay();
sbOut(sbWriteData,(byte)(uselen >> 8));
asm popf
return(uselen + 1);
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBService() - Services the SoundBlaster DMA interrupt
//
///////////////////////////////////////////////////////////////////////////
static void interrupt
SDL_SBService(void)
{
longword used;
sbIn(sbDataAvail); // Ack interrupt to SB
if (sbNextSegPtr)
{
used = SDL_SBPlaySeg(sbNextSegPtr,sbNextSegLen);
if (sbNextSegLen <= used)
sbNextSegPtr = nil;
else
{
sbNextSegPtr += used;
sbNextSegLen -= used;
}
}
else
{
SDL_SBStopSample();
SDL_DigitizedDone();
}
outportb(0x20,0x20); // Ack interrupt
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBPlaySample() - Plays a sampled sound on the SoundBlaster. Sets up
// DMA to play the sound
//
///////////////////////////////////////////////////////////////////////////
#ifdef _MUSE_
void
#else
static void
#endif
SDL_SBPlaySample(byte huge *data,longword len)
{
longword used;
SDL_SBStopSample();
asm pushf
asm cli
used = SDL_SBPlaySeg(data,len);
if (len <= used)
sbNextSegPtr = nil;
else
{
sbNextSegPtr = data + used;
sbNextSegLen = len - used;
}
// Save old interrupt status and unmask ours
sbOldIntMask = inportb(0x21);
outportb(0x21,sbOldIntMask & ~(1 << sbInterrupt));
sbWriteDelay();
sbOut(sbWriteCmd,0xd4); // Make sure DSP DMA is enabled
sbSamplePlaying = true;
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_PositionSBP() - Sets the attenuation levels for the left and right
// channels by using the mixer chip on the SB Pro. This hits a hole in
// the address map for normal SBs.
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_PositionSBP(int leftpos,int rightpos)
{
byte v;
if (!SBProPresent)
return;
leftpos = 15 - leftpos;
rightpos = 15 - rightpos;
v = ((leftpos & 0x0f) << 4) | (rightpos & 0x0f);
asm pushf
asm cli
sbOut(sbpMixerAddr,sbpmVoiceVol);
sbOut(sbpMixerData,v);
asm popf
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_CheckSB() - Checks to see if a SoundBlaster resides at a
// particular I/O location
//
///////////////////////////////////////////////////////////////////////////
static boolean
SDL_CheckSB(int port)
{
int i;
sbLocation = port << 4; // Initialize stuff for later use
sbOut(sbReset,true); // Reset the SoundBlaster DSP
asm mov dx,0x388 // Wait >4usec
asm in al, dx
asm in al, dx
asm in al, dx
asm in al, dx
asm in al, dx
asm in al, dx
asm in al, dx
asm in al, dx
asm in al, dx
sbOut(sbReset,false); // Turn off sb DSP reset
asm mov dx,0x388 // Wait >100usec
asm mov cx,100
usecloop:
asm in al,dx
asm loop usecloop
for (i = 0;i < 100;i++)
{
if (sbIn(sbDataAvail) & 0x80) // If data is available...
{
if (sbIn(sbReadData) == 0xaa) // If it matches correct value
return(true);
else
{
sbLocation = -1; // Otherwise not a SoundBlaster
return(false);
}
}
}
sbLocation = -1; // Retry count exceeded - fail
return(false);
}
///////////////////////////////////////////////////////////////////////////
//
// Checks to see if a SoundBlaster is in the system. If the port passed is
// -1, then it scans through all possible I/O locations. If the port
// passed is 0, then it uses the default (2). If the port is >0, then
// it just passes it directly to SDL_CheckSB()
//
///////////////////////////////////////////////////////////////////////////
static boolean
SDL_DetectSoundBlaster(int port)
{
int i;
if (port == 0) // If user specifies default, use 2
port = 2;
if (port == -1)
{
if (SDL_CheckSB(2)) // Check default before scanning
return(true);
if (SDL_CheckSB(4)) // Check other SB Pro location before scan
return(true);
for (i = 1;i <= 6;i++) // Scan through possible SB locations
{
if ((i == 2) || (i == 4))
continue;
if (SDL_CheckSB(i)) // If found at this address,
return(true); // return success
}
return(false); // All addresses failed, return failure
}
else
return(SDL_CheckSB(port)); // User specified address or default
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_SBSetDMA() - Sets the DMA channel to be used by the SoundBlaster
// code. Sets up sbDMA, and sbDMAa1-sbDMAa3 (used by SDL_SBPlaySeg()).
//
///////////////////////////////////////////////////////////////////////////
void
SDL_SBSetDMA(byte channel)
{
if (channel > 3)
Quit("SDL_SBSetDMA() - invalid SoundBlaster DMA channel");
sbDMA = channel;
sbDMAa1 = sba1Vals[channel];
sbDMAa2 = sba2Vals[channel];
sbDMAa3 = sba3Vals[channel];
}
///////////////////////////////////////////////////////////////////////////
//
// SDL_StartSB() - Turns on the SoundBlaster
//
///////////////////////////////////////////////////////////////////////////
static void
SDL_StartSB(void)
{
byte timevalue,test;
sbIntVec = sbIntVectors[sbInterrupt];
if (sbIntVec < 0)
Quit("SDL_StartSB: Illegal or unsupported interrupt number for SoundBlaster");
sbOldIntHand = getvect(sbIntVec); // Get old interrupt handler
setvect(sbIntVec,SDL_SBService); // Set mine
sbWriteDelay();
sbOut(sbWriteCmd,0xd1); // Turn on DSP speaker
// Set the SoundBlaster DAC time constant for 7KHz
timevalue = 256 - (1000000 / 7000);
sbWriteDelay();
sbOut(sbWriteCmd,0x40);
sbWriteDelay();
sbOut(sbWriteData,timevalue);
SBProPresent = false;
if (sbNoProCheck)
return;
// Check to see if this is a SB Pro
sbOut(sbpMixerAddr,sbpmFMVol);
sbpOldFMMix = sbIn(sbpMixerData);
sbOut(sbpMixerData,0xbb);
test = sbIn(sbpMixerData);
if (test == 0xbb)
{
// Boost FM output levels to be equivilent with digitized output
sbOut(sbpMixerData,0xff);
test = sbIn(sbpMixerData);
if (test == 0xff)
{
SBProPresent = true;
// Save old Voice output levels (SB Pro)
sbOut(sbpMixerAddr,sbpmVoiceVol);
sbpOldVOCMix = sbIn(sbpMixerData);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -