-> MODULE 'hardware/custom', 'hardware/dmabits', 'dos/dos'

OPT PREPROCESS

#define NAME 'RAM:piss'

MODULE 'dos/dos', 'exec/memory'

CONST CUSTOMADDR  = $DFF000,
      DMACON      =     $96,
      DMAF_AUD0   =      $1,
      DMAF_AUD1   =      $2,
      AUD0        =     $A0,
      AUD1        =     $B0,
      DMAF_SETCLR =   $8000,
      DMAF_MASTER =    $200  

PROC main()
DEF fh,mem

	IF fh:=Open(NAME,OLDFILE)
		IF mem:=AllocMem(45214,MEMF_CHIP)

			Read(fh,mem,45214)

			WriteF('Sound located at: $\z\h[8]\n',mem)

			MOVE.W #$800F,$DFF096

			sound(0,mem,45214,0,45214,360,$40)

			WriteF('Pull the trigger!\n')
			Wait(SIGBREAKF_CTRL_C)
			WriteF('***Joined\n')

			MOVE.W #$800F,$DFF096

			FreeMem(mem,45214)
		ENDIF	
		Close(fh)
	ENDIF

 CleanUp(0)
ENDPROC




->          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
