# 
# Copyright (C) 1996-1997 Id Software, Inc. 
# 
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   
# 
# See the GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
# 

##
##  Sound mixing routines for Amiga PPC
##

.include        "macrosPPC.i"

# dma_t (sound.h)
.set	dma_gamealive,0
.set	dma_soundalive,4
.set	dma_splitbuffer,8
.set	dma_channels,12
.set	dma_samples,16
.set	dma_submission_chunk,20
.set	dma_samplepos,24
.set	dma_samplebits,28
.set	dma_speed,32
.set	dma_buffer,36


	.text


# external references
	xrefv	shm
        xrefv	paintedtime
        xrefa	paintbuffer
        xrefa	volume


	funcdef	S_TransferPaintBuffer

# The Amiga PPC version only supports 8 bit samples
	stwu	r1,-32(r1)
	lw	r4,shm			# r4 shm (struct dma_t)
	lxa	r5,paintbuffer		# r5 paintbuffer (int left,int right)
	lxa	r6,volume
	lfs	f1,16(r6)		# volume.value * 256
	ls	f2,c256
	fmuls	f1,f1,f2
	lw	r7,paintedtime
	subf.	r8,r7,r3		# r8 count
	beq	exit
	lwz	r9,dma_buffer(r4)	# r9 dma buffer start address
	fctiwz	f1,f1
	stfd	f1,24(r1)
	lwz	r10,28(r1)		# r10 = snd_vol
	lwz	r11,dma_samples(r4)
	add	r12,r9,r11
	subi	r11,r11,1
	and	r7,r7,r11
	add	r3,r7,r9		# r3 out
	lis	r4,0x7fffff@h		# r4 max val
	ori	r4,r4,0x7fffff@l
	not	r6,r4			# r6 min val
loop:
	lwz	r0,0(r5)
	mullw	r0,r0,r10
	addi	r5,r5,8
	cmpw	r0,r4
	ble	.1
	mr	r0,r4
	b	.2
.1:	cmpw	r0,r6
	bge	.2
	mr	r0,r6
.2:	srawi	r0,r0,16
	stb	r0,0(r3)
	addi	r3,r3,1
	cmpw	r3,r12
	bge	.3
	subic.	r8,r8,1
	bne	loop
	b	exit
.3:	mr	r3,r9
	subic.	r8,r8,1
	bne	loop
exit:
	addi	r1,r1,32
	blr

	funcend	S_TransferPaintBuffer



.ifdef	WOS
	.tocd
.else
        .data
.endif
	.align	2
lab c256
	.float	256.0
