/***************************************\
** Examplefileformat:                  **
** $0$0009FF20$AE6A$0000$0002$0215$40  **
**  |                                = Channel 0, always (for stereo) **
**    ||||||||                       = start adress                   **
**             ||||                  = length of sample               **
**                  ||||             = rep, set to 0000 usally        **
**                       ||||        = replen, set 0002 always        **
**                            ||||   = sampleperiod (in hex)          **
**                                 ||= vol $40=64, MAX VOL            ** 
**
**
** Thi-hi.. stereo added by stein :) 11/1-97  **
\***************************************/


OPT PREPROCESS

#define NAME 'RAM:T/esparams'
     ->> Filename of param file <<-
->#define LIB    ->> comment this to create executable instead of library <<-

#ifdef LIB
  LIBRARY 'easysound.library',1,0,'Easysound 1.0 (28.12.96)' IS sound,waitdma
#endif

MODULE 'hardware/intbits', 'hardware/dmabits', 'hardware/custom'

#ifndef LIB
PROC main()
DEF fhl,ch,p,l,re,rl,hz,v,buf[100]:STRING,waste[100]:STRING

	IF fhl:=Open(NAME,OLDFILE)
		Read(fhl,buf,34)   ->> Fileformat: 0 11111111 2222 3333 4444 5555 66
->		WriteF('Buffer=\s\n',buf)
		MidStr(waste,buf,0,2) ;	ch:=Val(waste)
		MidStr(waste,buf,2,10);	 p:=Val(waste)
		MidStr(waste,buf,11,5);	 l:=Val(waste)
		MidStr(waste,buf,16,5); re:=Val(waste)
		MidStr(waste,buf,21,5);	rl:=Val(waste)
		MidStr(waste,buf,26,5); hz:=Val(waste)
		MidStr(waste,buf,31,3); v:=Val(waste)
/**** DEBUG LINES ****
		WriteF('Ch =\z\h[1]\n',ch)
		WriteF('Mem=\z\h[8]\n',p)
		WriteF('Len=\z\h[4]\n',l)
		WriteF('Rep=\z\h[4]\n',re)
		WriteF('Rel=\z\h[4]\n',rl)
		WriteF('Per=\z\h[4]\n',hz)
		WriteF('Vol=\z\h[4]\n',v)
*********************/
		sound(ch,p,l,re,rl,hz,v)
		Close(fhl)
	ENDIF
ENDPROC
#endif
#ifdef LIB
PROC main() ; ENDPROC
#endif

->          A2   A1  A0  D3  D2   D1   D0
PROC sound(voice,mem,len,rep,repl,rate,vol)
	-> rep:=0 ; repl:=2  -> This stuff are buggy!! WHY?!?!
	
	IF mem>NIL
		PutLong(mem,0) 							->Clear fist 2 words in a sample
	ENDIF
	IF voice=0
		IF (mem>NIL) AND (len>NIL)
			PutInt(CUSTOMADDR+DMACON,DMAF_AUD0)
      PutInt(CUSTOMADDR+DMACON,DMAF_AUD1)
			waitdma()
			PutLong(CUSTOMADDR+AUD0+0,mem+rep)
      PutLong(CUSTOMADDR+AUD1+0,mem+rep)
			IF rep>0 
				PutInt(CUSTOMADDR+AUD0+4,repl/2)
        PutInt(CUSTOMADDR+AUD1+4,repl/2)
			ELSE
				PutInt(CUSTOMADDR+AUD0+4,len/2)
        PutInt(CUSTOMADDR+AUD1+4,len/2)
			ENDIF
		ENDIF
		IF rate>NIL
			PutInt(CUSTOMADDR+AUD0+6,rate)
      PutInt(CUSTOMADDR+AUD1+6,rate)
		ENDIF
		IF vol>-1
      PutInt(CUSTOMADDR+AUD1+8,vol)
		  PutInt(CUSTOMADDR+AUD0+8,vol)
		ENDIF
		IF (mem>NIL) AND (len>NIL)
			PutInt(CUSTOMADDR+DMACON,DMAF_SETCLR OR DMAF_AUD0 OR DMAF_MASTER)
      PutInt(CUSTOMADDR+DMACON,DMAF_SETCLR OR DMAF_AUD1 OR DMAF_MASTER)
			PutInt(CUSTOMADDR+AUD0+4,repl/2)
  		PutInt(CUSTOMADDR+AUD1+4,repl/2)
		ENDIF
	ENDIF

ENDPROC

PROC waitdma()
DEF x
	->  FOR x:=0 TO 3100 DO NOP  ->>> This is the old shitty way doing it <<<-

	-> Personal coded 100% working state-of-the-art DMA wait who is 
	-> guaranteed to wait for a minimum of 8 rasterlines!

	Disable()							-> Prevent other interrupts from interrupting us!
			LEA.L	$DFF006,A0
			CLR.L	D3
			MOVE.B	(A0),D1
bah:	MOVE.B	(A0),D0
			CMP.B	D0,D1
			BEQ.S	bah
			MOVE.B	D0,D1
			ADD.B	#1,D3
			CMP.B	#8,D3						-> Wait for around 8 raster lines
			BNE.S	bah
	Enable()							-> Enable interrupt handling again

ENDPROC
