|
Programming The Sound Blaster 16 Example 1 |
Mission : Obtain Environmental Settings Reset the DSP Read the DSP Version Numbers Display Everything!IntroDownload the Expansion Pack!
SB16::SB16()
{ DSPVersionNum=0;
GetBlasterID();
if(DSP_Reset())
{ cout<<"\n\nSound Blaster Initialization Successful!\n";
}
else
{ cout<<"\n\nSound Initialization FAILED!\n";
}
DisplayEnv();
}
SB16::~SB16()
{
} |
void SB16::GetBlasterID()
{ char *blaster;
if((blaster = getenv("BLASTER")) != 0)
{ while(*blaster)
{ switch(*blaster)
{ // Sound Blaster IO Address
case 'A':{blaster++;BLASTER.IOAddr=Parse(blaster,16);break;}
// Sound Blaster Interrupt
case 'I':{blaster++;BLASTER.SBIntr=Parse(blaster,10);break;}
// DMA Channel
case 'D':{blaster++;BLASTER.DMA=Parse(blaster,10);break;}
// Card Type
case 'T':{blaster++;BLASTER.CardID=Parse(blaster,10);break;}
// Mixer Port
case 'M':{blaster++;BLASTER.Mixer=Parse(blaster,10);break;}
// MIDI Port
case 'P':{blaster++;BLASTER.MIDI=Parse(blaster,16);break;}
// High DMA Channel
case 'H':{blaster++;BLASTER.HDMA=Parse(blaster,10);break;}
}
if(*blaster){blaster++;} //if because it could be last item!
}
}
else
{cout<<"BLASTER environmental variable NOT set!\n";
}
// Check to see if stuff is valid
if(BLASTER.DMA <0 ||BLASTER.DMA>=4)
{cout<<"Low DMA Channel "<<BLASTER.DMA<<" is Invalid!\n";
}
if(BLASTER.HDMA<0 ||BLASTER.HDMA>=8)
{cout<<"High DMA Channel "<<BLASTER.HDMA<<" is Invalid!\n";
}
SetIO();
} |
short SB16::Parse(char *string,int radix)
{ short count=0;
char temp[8],*notused;
while((*string != ' ') && (*string != '\0'))
{ temp[count++]=*(string++);
}
temp[count]='\0';
return (short)strtol(temp,¬used,radix);
}
void SB16::SetIO(void)
{DSPReset = (BLASTER.IOAddr + 0x0006);
DSPRead = (BLASTER.IOAddr + 0x000A);
DSPWrite = (BLASTER.IOAddr + 0x000C);
DSPStatus= (BLASTER.IOAddr + 0x000E);
MixerAddr= (BLASTER.IOAddr + 0x0004);
MixerData= (BLASTER.IOAddr + 0x0005);
DSPIntAck= (BLASTER.IOAddr + 0x000F);
} |
BOOL SB16::DSP_Reset()
{ outp(DSPReset,0x01); //write 1 to the port
delay(1); //wait a milisecond.
outp(DSPReset,0x00); //write 0 to the port
DSPDataAvail();
for(int p=0;p<1000;p++)
{ if((inp(DSPRead))==DSP_READY)
{GetDSPVersion();
return 1; //return to caller (SUCCESS)
}
}
return 0;//this will happen should 1000 tries fail.
}
void SB16::GetDSPVersion(void)
{ unsigned char VersionMaj,VersionMin;
char string[7],temp[4],*notused;
DSPWait();
outp(DSPWrite,DSPVersion); //send instruction
DSPDataAvail(); //wait for dsp to flag u for instruction
ReadDSP(&VersionMaj);
ReadDSP(&VersionMin);
DSPVersionNum=VersionMaj;
itoa(VersionMaj,string,10);
strcat(string,".");
strcat(string,itoa(VersionMin,temp,10));
DSPVersionNum=(float)strtod(string,¬used);
}
void SB16::WriteDSP(unsigned char value)
{ DSPWait();
outportb(DSPWrite,(unsigned char)value);
}
void SB16::ReadDSP(unsigned char *value)
{DSPDataAvail();
*value=inportb(DSPRead);
}
void SB16::DSPDataAvail()
{while((inp(DSPStatus) & 0x80)==0);
}
void SB16::DSPWait(void)
{while((inp(DSPWrite) & 0x80)!=0);
} |
void SB16::DisplayEnv()
{if(BLASTER.CardID < 7 && BLASTER.CardID >=0)
{ char CardType[7][34]={
"-Creative Sound Blaster Series -",
"Sound Blaster",
"Sound Blaster Pro",
"Sound Blaster 2.0",
"Sound Blaster Pro 2.0",
"Sound Blaster Pro MCV",
"Sound Blaster 16"};
cout<<"\n\nThe "<<CardType[BLASTER.CardID]<<" is detected\n";
cout<<"Blaster I/O Port : "<<hex<<BLASTER.IOAddr<<endl;
cout<<"DSP Version : "<<DSPVersionNum<<endl;
cout<<"IRQ : "<<BLASTER.SBIntr<<endl;
cout<<"DMA : "<<BLASTER.DMA<<endl;
if(BLASTER.HDMA)
{ cout<<"HDMA : "<<BLASTER.HDMA<<endl;
}
if(BLASTER.MIDI)
{cout<<"MIDI : "<<hex<<BLASTER.MIDI<<endl;
}
if(BLASTER.Mixer)
{ cout<<"Mixer Addr : "<<hex<<BLASTER.Mixer<<endl;
}
}
else
{cout<<"Unknown Card!\n";
}
}
|