	opt 	l-,c+,d-
*** Sound Player auf DEVPAC Assembler 
*** (C) 1988 HAGENAU COMPUTER
*** Written 1/88 by Markus Breyer
	
	incdir	"DevPacAm:include/"
	include	"exec/types.i"
	include	"exec/memory.i"
	include	"hardware/custom.i"
	include	"hardware/intbits.i"
	include	"hardware/dmabits.i"
	include	"libraries/dos.i"

_ciab	equ	$bfd0fe
_custom	equ	$dff000
_AbsExecBase	equ	4

_LVOAllocMem	equ	-198
_LVOFreeMem	equ	-210
_LVOOpenLibrary	equ	-552
_LVOCloseLibrary	equ	-414

_LVOOpen	equ	-30
_LVOClose	equ	-36
_LVORead	equ	-42
_LVOWrite	equ	-48
_LVOOutput	equ	-60

_LVOAllocRemember	equ	-396
_LVOFreeRemember	equ	-408

*** Erstmal ein paar Macros definieren ***

call	macro
	jsr	_LVO\1(a6)
	endm

callexe	macro
	move.l	(_AbsExecBase).w,a6
	jsr	_LVO\1(a6)
	endm

calldos	macro
	move.l	DosBase(a5),a6
	jsr	_LVO\1(a6)
	endm

callint	macro
	move.l	IntuitionBase(a5),a6
	jsr	_LVO\1(a6)
	endm

OpenLib	macro
	lea	\1Name(pc),a1
	clr.l	d0
	callexe	OpenLibrary
	move.l	d0,\1Base(a5)
	beq	\1Fail
	endm

CloseLib	macro
	move.l	\1Base(a5),a1
	callexe	CloseLibrary
\1Fail	
	endm

lalign	macro	;auf LONGWORD-BOUNDARY aufruecken
	ifne	(*-Start)&3
	ds.b	4-((*-Start)&3)
	endc
	endm

*** Geht los ***

ALLOCSTEP	equ	$4000
INTF_AUDIO	equ	INTF_AUD0!INTF_AUD1!INTF_AUD2!INTF_AUD3

*** Start ***

Start	move.l	#100,-(sp)
	clr.b	-1(a0,d0)
	move.l	a0,-(sp)
	move.l	#VARSIZE,d0
	move.l	#MEMF_CLEAR!MEMF_CHIP,d1
	callexe	AllocMem
	move.l	(sp)+,a0
	tst.l	d0
	beq	FastOut
	move.l	d0,a5
	move.l	a0,SName(a5)

	OpenLib	Dos
	OpenLib	Intuition

	move.l	SName(a5),a0
	tst.b	(a0)
	beq	SError
	cmp.b	#'*',(a0)
	bne.s	NoAsterisk
	addq.l	#1,a0
	addq.w	#1,OnceFlag(a5)
NoAsterisk	
	move.l	a0,d1
	move.l	#MODE_OLDFILE,d2
	calldos	Open
	pea	TSNFError(pc)
	move.l	(sp)+,d2
	moveq	#LSNFError,d3
	move.l	d0,FileHandle(a5)
	beq	Error
	move.l	FileHandle(a5),d1
	pea	SLen(a5)
	move.l	(sp)+,d2
	moveq	#6,d3
	call	Read
	cmp.w	#6,d0
	bne	FError
	clr.l	NumBlocks(a5)
ReadLoop	lea	MemKey(a5),a0
	move.l	#ALLOCSTEP,d0
	moveq	#MEMF_CHIP,d1
	callint	AllocRemember
	move.l	d0,d2
	beq	OOMError
	lea	NumBlocks(a5),a0
	lea	BlockTab(a5),a1
	move.l	(a0),d0
	lsl.l	#2,d0
	move.l	d2,0(a1,d0)
	addq.l	#1,(a0)

	move.l	FileHandle(a5),d1
	move.l	#ALLOCSTEP,d3
	calldos	Read
	move.l	d0,Size(a5)
	cmp.w	#ALLOCSTEP,d0
	beq	ReadLoop
	move.l	FileHandle(a5),d1
	call	Close
	clr.l	FileHandle(a5)

	move.w	SFreq(a5),d0
	beq.s	NoDiv
	move.l	#3579545,d1
	divu	d0,d1
	move.w	d1,SFreq(a5)
NoDiv	

* Hier geht's los

	lea	_ciab,a0
	lea	_custom,a1
WaitRelease	btst	#6,$1001(a0)
	beq	WaitRelease
WaitNoSound	btst	#6,$1001(a0)
	beq	NSEnd
	tst.l	SName(a5)
	beq	WaitNoSound

	move.w	intenar(a1),-(sp)
	move.w	#INTF_AUDIO,intena(a1)
	move.w	#DMAF_AUDIO,dmacon(a1)
	move.w	#aud0,d0
	moveq	#3,d1
AudioLoop	move.w	#1,ac_len(a1,d0.w)
	move.w	SFreq(a5),ac_per(a1,d0.w)
	move.w	#64,ac_vol(a1,d0.w)
	add.w	#ac_SIZEOF,d0
	dbf	d1,AudioLoop

	moveq	#1,d6	;BlockZaehler
	move.l	BlockTab(a5),d0
	bsr	MakePL
	move.w	#INTF_AUDIO,intreq(a1)
	move.w	#DMAF_SETCLR!DMAF_MASTER!DMAF_AUDIO,dmacon(a1)
LPlayLoop	btst	#6,$1001(a0)
	beq.s	PEnd
	move.w	intreqr(a1),d0
	and.w	#INTF_AUDIO,d0
	cmp.w	#INTF_AUDIO,d0
	bne	LPlayLoop
	move.w	d0,intreq(a1)
	cmp.l	NumBlocks(a5),d6
	bcs.s	OkBlocks
	moveq	#0,d6
	tst.w	OnceFlag(a5)
	beq.s	OkBlocks
WaitDMA	move.w	intreqr(a1),d0
	and.w	#INTF_AUDIO,d0
	cmp.w	#INTF_AUDIO,d0
	bne	WaitDMA
	move.w	d0,intreq(a1)
	bra.s	PEnd
OkBlocks	lea	BlockTab(a5),a2
	move.w	d6,d0
	lsl.w	#2,d0
	move.l	0(a2,d0.w),d0
	addq.l	#1,d6
	bsr	MakePL
	bra	LPlayLoop

PEnd	moveq	#0,d0
	bsr	MakeVol
	move.w	#DMAF_AUDIO,dmacon(a1)
	move.w	(sp)+,d0
	and.w	#INTF_AUDIO,d0
	or.w	#INTF_SETCLR,d0
	move.w	d0,intena(a1)

NSEnd	btst	#6,$1001(a0)
	beq	NSEnd

NormalOut	clr.l	(sp)
	bra.s	Abort
SError	pea	TSError(pc)
	move.l	(sp)+,d2
	moveq	#LSError,d3
	bra.s	Error
FError	pea	TFError(pc)
	move.l	(sp)+,d2
	moveq	#LFError,d3
	bra.s	Error
OOMError	pea	TOOMError(pc)
	move.l	(sp)+,d2
	moveq	#LOOMError,d3
Error	movem.l	d2/d3,-(sp)
	calldos	Output
	move.l	d0,d1
	movem.l	(sp)+,d2/d3
	call	Write
Abort	move.l	FileHandle(a5),d1
	beq.s	NoCF
	calldos	Close
NoCF	lea	MemKey(a5),a0
	moveq	#-1,d0
	callint	FreeRemember
	CloseLib	Intuition
	CloseLib	Dos
	move.l	a5,a1
	move.l	#VARSIZE,d0
	callexe	FreeMem
FastOut	move.l	(sp)+,d0
	rts

MakePL	move.l	d0,aud0+ac_ptr(a1)
	move.l	d0,aud1+ac_ptr(a1)
	move.l	d0,aud2+ac_ptr(a1)
	move.l	d0,aud3+ac_ptr(a1)
	move.w	#ALLOCSTEP/2,d0
	cmp.l	NumBlocks(a5),d6
	bne.s	OkLength
	move.l	Size(a5),d0
	lsr.w	#1,d0
	bne.s	OkLength
	moveq	#1,d0
OkLength	move.w	d0,aud0+ac_len(a1)
	move.w	d0,aud1+ac_len(a1)
	move.w	d0,aud2+ac_len(a1)
	move.w	d0,aud3+ac_len(a1)
	rts

MakeVol	;(Volume:d0, Custom:a1)
	move.w	d0,aud0+ac_vol(a1)
	move.w	d0,aud1+ac_vol(a1)
	move.w	d0,aud2+ac_vol(a1)
	move.w	d0,aud3+ac_vol(a1)
	rts

DosName	dc.b	'dos.library',0
IntuitionName	dc.b	'intuition.library',0

TSError	dc.b	'Usage: aplay [*]<soundfile>',10
	dc.b	'The optional asterisk (*) indicates a one shot sound.',10
LSError	equ	*-TSError
TSNFError	dc.b	'Sorry, can''t find sound file!',10
LSNFError	equ	*-TSNFError
TOOMError	dc.b	'Sorry, out of memory!',10
LOOMError	equ	*-TOOMError
TFError	dc.b	'Trouble in the filing system!',10
LFError	equ	*-TFError

*** Variablen

	rsset	0
MemKey	rs.l	1
SName	rs.l	1
FileHandle	rs.l	1
Size	rs.l	1
NumBlocks	rs.l	1
IntuitionBase	rs.l	1
DosBase	rs.l	1
SLen	rs.l	1
SFreq	rs.w	1
OnceFlag	rs.w	1
BlockTab	rs.l	32
VARSIZE	rs.w	0

	end
