home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Intermedia 1998 January
/
inter1_98.iso
/
www
/
rozi
/
RAW1.ZIP
/
MGPSB.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-01-04
|
7KB
|
247 lines
{****************************************************************************}
{* *}
{* MegaPlay v.2.0 *}
{* Sound Blaster Routines *}
{* *}
{****************************************************************************}
Unit MGPSB;
INTERFACE
Uses CRT;
var
sbLocation:byte;
const
Toff:boolean=FALSE;
Stereo:boolean=FALSE;
SBPro:boolean=FALSE;
sbReset=$206; { Write }
sbWriteCmd=$20C; { Write }
sbWriteStat=$20C; { Read }
sbDataAvail=$20E; { Read }
sbReadData=$20A; { Read }
sbpMixerAddr=$204; { Write }
sbpMixerData=$205; { ReadWrite }
sbpmControl=$0E;
sbpmMasterVol=$22;
sbpmFMVol=$26;
procedure StopAll;
procedure Playback(sound : Pointer; xsize : word; tc:byte);
procedure FadeVol(v:byte);
procedure SetVolume(Volume:byte);
procedure Speaker(OnOff:boolean);
procedure WriteDSP(data:byte);
function DetectCard:byte;
procedure SetOutMode(Stereo:boolean);
function ResetDSP:boolean;
procedure WriteDAC(level : byte);
IMPLEMENTATION
{****************************************************************************}
procedure sbOut(Base:word;data:byte);
begin
port[Base+sbLocation]:=data;
end;
{****************************************************************************}
function sbIn(Base:word):byte;
begin
sbIn:=port[Base+sbLocation];
end;
{****************************************************************************}
procedure WaitWrite;assembler;
label LoopWait,EndLoop;
asm
push cx
xor cx,cx { need that for slow SBs ! }
loopWait: dec cx
jz endloop
in al,dx { AL = WRITE COMMAND STATUS }
or al,al
js loopWait { Jump if bit7=1 - writing not allowed }
endloop: pop cx
end;
{****************************************************************************}
procedure StopAll;
begin
Speaker(False);
WriteDSP($D0);
WriteDSP($DA);
WriteDSP($D0);
Toff:=TRUE;
end;
{****************************************************************************}
procedure Playback;
var page, offset : word;
begin
Speaker(TRUE);
xsize := xsize - 1;
{ Set up the DMA chip }
offset := Seg(sound^) Shl 4 + Ofs(sound^);
page := (Seg(sound^) + Ofs(sound^) shr 4) shr 12;
Port[$0A] := 5;
Port[$0C] := 0;
Port[$0B] := $49;
Port[$02] := Lo(offset);
Port[$02] := Hi(offset);
Port[$83] := page;
Port[$03] := Lo(xsize);
Port[$03] := Hi(xsize);
Port[$0A] := 1;
{ Set the playback frequency }
WriteDSP($40);
WriteDSP(tc);
{ Set the playback type (8-bit) }
WriteDSP($14);
WriteDSP(Lo(xsize));
WriteDSP(Hi(xsize));
end;
{****************************************************************************}
procedure FadeVol;
begin
v:=v shr 4 + v shl 4;
repeat
if v and $F>0 then dec(v,1);
if v and $F0>0 then dec(v,$10);
SetVolume(v);
delay(100);
until v=0;
end;
{****************************************************************************}
procedure SetVolume;
begin
sbOut(sbpMixerAddr,sbpmMasterVol);
sbOut(sbpMixerData,Volume);
end;
{****************************************************************************}
procedure Speaker;
begin
if onoff then WriteDSP($D1) else WriteDSP($D3);
end;
{****************************************************************************}
procedure WriteDSP(data:byte);
begin
WaitWrite;
sbOut(sbWriteCmd,data);
end;
{****************************************************************************}
function DetectCard:byte;
{****************************************************************************}
{* Detects a Sound Card *}
{* Returns: *}
{* 0 - None *}
{* 1 - SB *}
{* 2 - SB Pro *}
{****************************************************************************}
var
IOloc,t,Return:byte;
sbOk,EndSearch:boolean;
DSPV:array[1..2] of byte;
begin
Return:=0;
IOloc:=$10;
sbOk:=FALSE;
EndSearch:=FALSE;
{ Search for SB through different base addresses }
repeat
sbLocation:=IOloc;
sbOut(sbReset,1);
asm
mov dx,$388 { Wait >4usec }
in al, dx
in al, dx
in al, dx
in al, dx
in al, dx
in al, dx
in al, dx
in al, dx
in al, dx
end;
sbOut(sbReset,0);
asm
mov dx,$388 { Wait >100usec }
mov cx,100
@@1:
in al,dx
loop @@1
end;
for t:=0 to 100 do
begin
if sbIn(sbDataAvail) and $80>0 then
begin
if sbIn(sbReadData)=$AA then
begin
EndSearch:=TRUE; { SB found }
SBOk:=TRUE;
end;
end;
end;
inc(IOloc,$10);
if IOloc=7 then
begin
EndSearch:=TRUE;
SBOk:=FALSE;
end;
until EndSearch;
if sbOk then Return:=1;
{ Try Mixer Chip : Detect SB Pro }
sbOk:=false;
sbOut(sbpMixerAddr,sbpmFMVol);
sbOut(sbpMixerData,$bb);
t:=sbIn(sbpMixerData);
if t=$bb then
begin
sbOut(sbpMixerData,$ff);
t:=sbIn(sbpMixerData);
if t=$ff then
begin
sbOk:=true;
end;
end;
if sbOk and (Return=1) then Return:=2;
DetectCard:=Return;
end;
{****************************************************************************}
procedure SetOutMode;
begin
if Stereo then
begin
sbOut(sbpMixerAddr,sbpmControl);
sbOut(sbpMixerData,2);
end
else
begin
sbOut(sbpMixerAddr,sbpmControl);
sbOut(sbpMixerData,0);
end;
end;
{****************************************************************************}
function ResetDSP;
begin
sbOut(sbReset,1);
Delay(10);
sbOut(sbReset,0);
Delay(10);
if (sbIn(sbDataAvail) And $80 = $80) And
(sbIn(sbReadData) = $AA) then
ResetDSP := true
else
ResetDSP := false;
end;
{****************************************************************************}
procedure WriteDAC;
begin
WriteDSP($10);
WriteDSP(level);
end;
{****************************************************************************}
END.