
;*********************************************************************	
; includes
;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore_ctrl.i"

;*********************************************************************	
; sd header is placed here
;*********************************************************************	

	.include "boot_level_sdHeader.s"

;*********************************************************************
;* start at address 32 here
;*********************************************************************

start	
	cli
	
	;setup irq vector
	
	dexti	>irq
	movei	r0,irq	
	nop
	setssr	r0,2

	;setup video (1440x900)
	
	dexti	>setupVideo
	movei	r0,setupVideo
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	;copy font (rom -> ram)
		
	movei	r0,fontDataROM	;source
	movei	r1,>>fontDataROM
	moveih	r0,>fontDataROM
	moveih	r1,>>>fontDataROM
		
	movei	r2,fontDataRAM	;destination 
	movei	r3,>>fontDataRAM
	moveih	r2,>fontDataRAM
	moveih	r3,>>>fontDataRAM	
	
	dexti	>extCopy8
	movei	r5,extCopy8
	gpci	r7,2	;next 4 + X
	jmpi	r5,0
	movei	r4,$df	;delay slot (copy 0x2f00 words -> $5df+1)	
	nop		;delay slot
	moveih	r4,$05	;delay slot
	nop		;delay slot
		
	;copy mod into sram

	movei	r4,modLength
	movei	r2,>>modData
	movei	r5,iToECopy
	moveih	r4,>modLength
	moveih	r2,>>>modData
	moveih	r5,>iToECopy
	gpci	r7,2	;next 4 + X
	jmpi	r5,0
	movei	r0,modDataIRAM		;delay slot	
	movei	r1,modData		;delay slot
	moveih	r0,>modDataIRAM		;delay slot
	moveih	r1,>modData		;delay slot	
	
	;reset PS2 buffer
	
	gpci	r7,2	;next 4 + X
	br	PS2Reset
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;setup audio
	
	dexti	>initAudio
	movei	r0,initAudio
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;setup audio irq
	
	movei	r0,UcIrqMsk
	movei	r1,$6			;enable audio irq and timer 2
	moveih	r0,>UcIrqMsk
	nop
	st	r0,r1			;
	
	;set audio dac counter = 2 (96/3 = 32KHz)
	
	movei	r6,UcAudioDACBase
	nop
	moveih	r6,>UcAudioDACBase
	movei	r1,$2
	movei	r0,0
	moveih	r1,$2	;address 2
	
	
	stinc	r6,r0	;dac left high = 0
	nop
	stinc	r6,r0	;dac left low = 0
	nop
	stinc	r6,r0	;dac right high = 0
	nop
	stinc	r6,r0	;dac right low = 0	
	nop
	st	r6,r1	;dac address/ctrl = $0201	;dac counter = 2
		
	;init module
	
	dexti	>modChannels
	movei	r4,modChannels
	dexti	>modInit
	movei	r2,modInit
	gpci	r7,2		;next 4 + X
	jmpi	r2,0
	movei	r0,modData	;delay slot
	movei	r1,>>modData	;delay slot
	moveih	r0,>modData	;delay slot
	moveih	r1,>>>modData	;delay slot
	
	;enable irqs
	
	sei
		
	;*********************************************************************
	;* main loop
	;*********************************************************************
	
mainLoop	

	;switch and clear frames
	
	movei	r0,switchAndClearFrame
	nop
	moveih	r0,>switchAndClearFrame
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;draw header
	
	gpci	r7,2		;next 4 + X
	br	drawTextFormated
	movei	r0,textHeader	;delay slot
	movei	r1,0		;delay slot
	moveih	r0,>textHeader	;delay slot
	movei	r2,0		;delay slot
	
	;draw song name
	
	movei	r0,modData
	movei	r1,>>modData
	moveih	r0,>modData
	moveih	r1,>>>modData
	
	nop
	push	r1
	push	r0
	
	gpci	r7,2		;next 4 + X
	br	drawTextFormated
	movei	r0,modSongName	;delay slot
	movei	r1,0		;delay slot
	moveih	r0,>modSongName	;delay slot
	movei	r2,2		;delay slot
	
	rqpop
	rqpop
	nop
	nop
	
	;print sample info sw (1)
	
	movei	r0,UcRSwichtWLedG
	nop
	moveih	r0,>UcRSwichtWLedG
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0	;sw
	nop
	extri	r0,1	
	dexti	>printSampleInfos
	movei	r1,printSampleInfos
	gpci	r7,2		;next 4 + X
	jmpts	r1
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;print song info sw (2)
	
	movei	r0,UcRSwichtWLedG
	nop
	moveih	r0,>UcRSwichtWLedG
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0	;sw
	nop
	extri	r0,2	
	dexti	>printSongInfos
	movei	r1,printSongInfos
	gpci	r7,2		;next 4 + X
	jmpts	r1
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;print pattern info sw (3)
	
	
	movei	r0,UcRSwichtWLedG
	nop
	moveih	r0,>UcRSwichtWLedG
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0	;sw
	nop
	extri	r0,3	
	dexti	>printPatternInfos
	movei	r1,printPatternInfos
	gpci	r7,2		;next 4 + X
	jmpts	r1
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	
	;play mode (4)
	
	cli
	
	movei	r0,UcRSwichtWLedG
	nop
	moveih	r0,>UcRSwichtWLedG
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0	;sw
	nop
	extri	r0,4
	dexti	>modStart
	movei	r1,modStart
	dexti	>modStop
	movei	r2,modStop
	gpci	r7,3		;next 4 + X
	jmpts	r1		;start
	jmptc	r2		;stop 		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	sei
			
	;process ps2 keys
	
	gpci	r7,2		;next 4 + X
	br	PS2Process
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;draw channel buffer
	
	movei	r2,channel0Buffer
	dexti	>drawChannelBuffer
	movei	r1,drawChannelBuffer
	gpci	r7,2		;next 4 + X
	jmpi	r1,0
	movei	r0,12		;delay slot
	movei	r1,64		;delay slot
	moveih	r2,>channel0Buffer			;delay slot
	movei	r3,95		;delay slot
	
	;draw channel buffer
	
	movei	r2,channel1Buffer
	dexti	>drawChannelBuffer
	movei	r1,drawChannelBuffer
	gpci	r7,2		;next 4 + X
	jmpi	r1,0
	movei	r0,132		;delay slot
	movei	r1,64		;delay slot
	moveih	r2,>channel1Buffer			;delay slot
	movei	r3,95		;delay slot
	
	;draw channel buffer
	
	movei	r2,channel2Buffer
	dexti	>drawChannelBuffer
	movei	r1,drawChannelBuffer
	gpci	r7,2		;next 4 + X
	jmpi	r1,0
	movei	r0,252		;delay slot
	movei	r1,64		;delay slot
	moveih	r2,>channel2Buffer			;delay slot
	movei	r3,95		;delay slot
	
	;draw channel buffer
	
	movei	r0,$74
	movei	r2,channel3Buffer
	dexti	>drawChannelBuffer
	movei	r1,drawChannelBuffer
	gpci	r7,2		;next 4 + X
	jmpi	r1,0
	moveih	r0,1		;delay slot
	movei	r1,64		;delay slot
	moveih	r2,>channel3Buffer			;delay slot
	movei	r3,95		;delay slot
	
	;draw channel buffer
	
	movei	r0,$ec
	dexti	>drawChannelBuffer
	movei	r1,drawChannelBuffer
	movei	r2,channel4Buffer
	gpci	r7,1		;next 4 + X
	jmpi	r1,0
	moveih	r0,1		;delay slot
	movei	r1,64		;delay slot
	moveih	r2,>channel4Buffer			;delay slot
	movei	r3,95		;delay slot
	
	;draw channel buffer
	
	movei	r0,$64
	movei	r2,channel5Buffer
	dexti	>drawChannelBuffer
	movei	r1,drawChannelBuffer
	gpci	r7,1		;next 4 + X
	jmpi	r1,0
	moveih	r0,2		;delay slot
	movei	r1,64		;delay slot
	moveih	r2,>channel5Buffer			;delay slot
	movei	r3,95		;delay slot
	
	;debug output
	
	dexti	>debugOutput
	movei	r1,debugOutput
	gpci	r7,2
	jmpi	r1,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	br	mainLoop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

;*********************************************************************
;* functions
;*********************************************************************

	.include	"components\i2c.s"
	.include	"components\drawText.s"
	.include	"components\drawChar.s"
	.include	"components\audioChannel.s"
	.include	"components\ps2keyboard.s"	
	.include	"components\binSearch.s"

	;mod and audio
	
	.include	"components\modSample.s"
	.include	"components\modPattern.s"
	.include	"components\audioPeriode.s"
	.include	"components\audioPeriodeStrings.s"
	.include	"components\mod.s"
	

;******************************
;*
;* printPatternInfos
;*
;*
;*	
	
printPatternInfos	

	.def patternInfoDrawXOffset	10
	.def patternInfoDrawYOffset	10

	push	r7
	
	;print row and song position info
	
	dexti	>modStruct
	movei	r7,modStruct
	nop
	
	rqldi	r7,modCurrentRowPosition
	rqldi	r7,modCurrentSongPosition
	rqldi	r7,modCurrentPatternStore
	ld	r0	;song pos
	ld	r1	;row
	ld	r2
	nop
	push	r2
	push	r1
	push	r0
	
	
	movei	r1,patternInfoDrawXOffset
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	movei	r0,modPatternInfoText		;delay slot
	addi	r1,80				;x+=80	delay slot
	moveih	r0,>modPatternInfoText		;delay slot
	movei	r2,patternInfoDrawYOffset	;y
	
	rqpop	;cleanup stack
	rqpop
	rqpop
	nop
	nop
	nop
		
	;direct mod access ... 
	
	dexti	>modStruct
	movei	r7,modStruct
	nop
	
	rqldi	r7,modModuleAdrLow
	rqldi	r7,modModuleAdrHigh
	rqldi	r7,modCurrentRowPosition
	ld	r5	;mod adr low
	ld	r6	;mod adr high
	ld	r1	;row position (row start offset)
	
	rqldi	r7,modCurrentPatternStore
	movei	r3,0	;row (cnt)
	movei	r4,0	;channel
	ld	r2	;current pattern 
	
	;
		
printPatternInfoRowLoop	
	
	push	r1
	push	r5
	push	r6
	push	r2
	push	r3
	push	r4	
	
	add	r1,r3,r1	;row + start offset
	nop
	push	r1		;print row
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	movei	r2,patternInfoDrawYOffset
	gpci	r7,2		;next 4 + X
	jmpi	r0,0	
	movei	r0,modePatternRowText			;delay slot
	movei	r1,patternInfoDrawXOffset		;x	delay slot
	moveih	r0,>modePatternRowText			;delay slot
	add	r2,r2,r3				;y
	
	rqpop	;remove row
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r1
	nop
	
	
	
	;draw 4 channels
	
printPatternInfoCLoop
	
	push	r1
	push	r5
	push	r6
	push	r2
	push	r3
	push	r4
	
	add	r3,r3,r1	;+row offset
	
	gpci	r7,2
	br	patternGetCellValue
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	push	r4	;effect
	push	r2	;sample num
	

	gpci	r7,2		;next 4 + X
	br	periodeSearch
	or	r0,r3,r3	;periode	delay slot
	movei	r1,0		;no finetune	delay slot
	nop			;delay slot
	nop			;delay slot
		
	movei	r0,periodeASString
	muli	r2,2
	moveih	r0,>periodeASString
	nop
	add	r0,r0,r2
	nop
	push	r0		;push periode as string
		
	;draw info
	
	getsp	r4,3		;get r3 (get data from stack)
	nop
	rqldi	r4,0		;channel
	rqldi	r4,1		;row
	nop
	ld	r4	
	ld	r2
	muli	r4,16
	addi	r2,patternInfoDrawYOffset		;y = row + yoffset
	addqi	r1,r4,6					;x = channel * 16 + 6
	nop
	addi	r1,patternInfoDrawXOffset	;+xoffset
	
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	gpci	r7,2		;next 4 + X
	jmpi	r0,0
	movei	r0,modPatternCellText	;delay slot
	nop	;movei	r1,0		;x	delay slot
	moveih	r0,>modPatternCellText	;delay slot
	nop	;movei	r2,3		;y
	
	rqpop	;cleanup stack after print
	rqpop
	rqpop

	;
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r1
	
	addi	r4,1	;next channel
	nop
	cmpeqi	r4,4
	brtc	printPatternInfoCLoop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;row
	add	r0,r1,r3
	nop
	cmploi	r0,63
	brtc	printPatternInfoRowLoopStop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	addi	r3,1
	movei	r4,0	;channel 0
	cmpeqi	r3,32
	brtc	printPatternInfoRowLoop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
printPatternInfoRowLoopStop
	
	;now lets print channel info
	

	dexti	>channel1
	movei	r6,channel1
	movei	r4,3
channelPLoop	
	

	rqldi	r6,channelStSampleNumber
	rqldi	r6,channelStVolume
	nop
	ld	r0
	ld	r1
	push	r0
	push	r1

	
	rqldi	r6,channelStPeriode
	rqldi	r6,channelStNote
	rqldi	r6,channelStCmd
	ld	r0
	ld	r1
	ld	r2
	push	r0
	push	r1
	push	r2	
	
	
	
	rqldi	r6,channelSampleAdrLow
	rqldi	r6,channelSampleAdrHigh
	rqldi	r6,channelCurrentPositionHigh
	ld	r0
	ld	r1
	ld	r2
	push	r0
	push	r1
	push	r2
	rqldi	r6,channelCurrentPositionLow
	rqldi	r6,channelLength
	rqldi	r6,channelVolume
	ld	r0
	ld	r1
	ld	r2
	push	r0
	push	r1
	push	r2
	rqldi	r6,channelIncrement
	rqldi	r6,channelRepeatLength
	rqldi	r6,channelRepeatStart
	ld	r0
	ld	r1
	ld	r2
	push	r0
	push	r1
	push	r2
		
	movei	r2,patternInfoDrawYOffset
	dexti	>drawTextFormated
	movei	r0,drawTextFormated
	addi	r2,40
	gpci	r7,2			;next 4 + X
	jmpi	r0,0
	movei	r0,modChannelInfoText		;delay slot
	movei	r1,patternInfoDrawXOffset				;delay slot
	moveih	r0,>modChannelInfoText		;delay slot
	sub	r2,r2,r4			;delay slot
	
	;

	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	
	subi	r4,1
	brts	channelPLoop
	addi	r6,channelStructSize		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
	;
	
	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* printSongInfos
;*
;*
;*

printSongInfos

	push	r7
	
	movei	r5,modData
	movei	r6,>>modData
	moveih	r5,>modData
	moveih	r6,>>>modData
	
	;length
	
	gpci	r7,2
	br	patternGetSongLength
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	push	r0
	
	
	;draw info
	
	dexti	>drawTextFormated
	movei	r1,drawTextFormated
	gpci	r7,2		;next 4 + X
	jmpi	r1,0
	movei	r0,modPatternSongLengthText	;delay slot
	movei	r1,0		;delay slot
	moveih	r0,>modPatternSongLengthText	;delay slot
	movei	r2,3	
	
	rqpop
	nop
	nop
	pop	r0
	nop
	
	subqi	r3,r0,1
	movei	r4,0
	
	;draw song positions
	
patternPosLoop

	
	gpci	r7,2
	br	patternGetPatternNumberAtSongPosition
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	push	r0
	push	r4
	
	;
	
	dexti	>drawTextFormated
	movei	r1,drawTextFormated
	gpci	r7,2		;next 4 + X
	jmpi	r1,0
	movei	r0,modPatternSongPosText	;delay slot
	movei	r1,0		;delay slot
	moveih	r0,>modPatternSongPosText	;delay slot
	addqi	r2,r4,4	;delay slot
	
	;
	
	rqpop
	rqpop
	nop
	pop	r4
	pop	r0
	

	cmpeq	r3,r4
	brtc	patternPosLoop
	addi	r4,1		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* printSampleInfos
;*
;*
;*

printSampleInfos
	
	push	r7
	
	movei	r4,0			;sample num
	
	movei	r5,modData
	movei	r6,>>modData
	moveih	r5,>modData
	moveih	r6,>>>modData

printSampleInfoLoop	

	push	r5
	push	r6
	
	;
	movei	r0,sampleGetFineTune
	nop
	moveih	r0,>sampleGetFineTune
	gpci	r7,2
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	push	r0
	
	;
	movei	r0,sampleGetVolume
	nop
	moveih	r0,>sampleGetVolume
	gpci	r7,2
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	push	r0
	
	;
	movei	r0,sampleGetRepeatLength
	nop
	moveih	r0,>sampleGetRepeatLength
	gpci	r7,2
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	push	r0
	
	;
	movei	r0,sampleGetRepeatStart
	nop
	moveih	r0,>sampleGetRepeatStart
	gpci	r7,2
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	push	r0
	
	;
	movei	r0,sampleGetLength
	nop
	moveih	r0,>sampleGetLength
	gpci	r7,2
	jmpi	r0,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	push	r0
	
	;name
	
	or	r0,r4,r4
	nop
	muli	r0,15		;sample struct in mod is 30 bytes long
	nop
	addi	r0,10		;to sample name
	nop
	
	add	r5,r5,r0
	addtqi	r6,r6,0
	
	nop
	push	r6
	push	r5
	
	push	r4	;sample number
	
	;draw info
	
	or	r2,r4,r4		;y = sample num
	dexti	>drawTextFormated
	movei	r1,drawTextFormated
	gpci	r7,2			;next 4 + X
	jmpi	r1,0
	movei	r0,modSampleText	;delay slot
	movei	r1,0			;delay slot
	moveih	r0,>modSampleText	;delay slot
	addi	r2,4			;y+=4 delay slot
	
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop
	
	rqpop
	rqpop
	nop
	pop	r6
	pop	r5
	
	cmploi	r4,30		;0..29
	brts	printSampleInfoLoop
	addi	r4,1		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* drawChannelBuffer
;*
;* return
;*
;* r0 = x
;* r1 = y
;* r2 = channelBuffer
;* r3 = channelBuffer length -1
;*

drawChannelBuffer
	
	nop
	addi	r1,32
drCLoop
	rqldi	r2,0
	addi	r2,1		;next * 2 value
	nop
	ld	r4
	movei	r6,$a0
	asri	r4,8
	moveih	r6,$5		;r6 = 1440
	sub	r4,r1,r4	;r4 = y	
	nop
	mul	r5,r4,r6	;y*width
	gmulhi	r6
	add	r5,r5,r0	;+x
	addtqi	r6,r6,0		;
	
	push	r1
	push	r2
	push	r3
	
	movei	r1,drawFrame
	nop
	moveih	r1,>drawFrame
	nop
	rqldi	r1,1	;low
	rqldi	r1,0	;high
	nop
	ld	r2
	ld	r3
	
	add	r2,r2,r5
	addt	r3,r3,r6
	
	movei	r1,$ff
	esadr	r3,r2
	moveih	r1,$ff
	nop
	est	r1,0	
	
	rqpop	
	rqpop
	rqpop
	pop	r3
	pop	r2
	pop	r1
	
	subi	r3,1
	brts	drCLoop
	addi	r0,1		;x++ delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	

;******************************
;* switchAndClearFrame
;*	
;*
;*

switchAndClearFrame	
	
	push	r7
	
	;wait vpos (930)

	gpci	r7,2	;next 4 + X
	br	waitVPos
	movei	r0,$a1	;delay slot
	nop		;delay slot
	moveih	r0,$3	;delay slot
	nop		;delay slot
	
	;switch frames
	
	gpci	r7,2	;next 4 + X
	br	switchFrames
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;set display address to video
	
	movei	r0,displayFrame	
	movei	r1,UcVideoMEMStartAdrLow
	movei	r2,UcVideoMEMLatch
	moveih	r0,>displayFrame
	moveih	r1,>UcVideoMEMStartAdrLow	;low memstart
	moveih	r2,>UcVideoMEMLatch		;latch out
	
	rqldi	r0,1
	rqldi	r0,0
	nop
	ld	r3
	ld	r4
	
	st	r1,r3
	addi	r1,1
	nop
	st	r1,r4
	st	r2,r7		;latch out
	
	;clear draw frame
	
	movei	r0,drawFrame	
	movei	r1,$33		;clear 
	moveih	r0,>drawFrame
	moveih	r1,$9e
	
	rqldi	r0,1
	rqldi	r0,0
	nop
	ld	r2
	ld	r3
	
	gpci	r7,2
	br	memclr32
	movei	r0,0	;clear value delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;* switchFrames
;*

switchFrames

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5

	movei	r0,displayFrame
	movei	r1,drawFrame
	moveih	r0,>displayFrame	
	moveih	r1,>drawFrame
	
	rqldi	r0,0
	rqldi	r0,1
	nop
	ld	r2
	ld	r3
	
	rqldi	r1,0
	rqldi	r1,1
	nop
	ld	r4
	ld	r5

	st	r0,r4
	addi	r0,1
	nop
	st	r0,r5
	
	st	r1,r2
	addi	r1,1
	nop
	st	r1,r3
	
	rqpop
	rqpop
	rqpop
	pop	r5
	pop	r4
	pop	r3
	
	rqpop
	rqpop
	rqpop
	pop	r2
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop		;delay slot
	nop	     	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*	
;* debugOutput
;*

debugOutput
	
	push	r7

	dexti	>UcRSwichtWLedG
	movei	r0,UcRSwichtWLedG

	nop
	rqldi	r0,0
	nop
	nop
	ld	r0	;sw
	nop
	extri	r0,0
	brtc	debugModeDisabled
	movei	r0,UcVideoRVCountWHSYNC		;		delay slot
	nop					;		delay slot
	moveih	r0,>UcVideoRVCountWHSYNC	;vblank address	delay slot
	nop					;delay slot
	
	rqldi	r0,0	;vcount
	nop
	nop
	ld	r0
	nop
	subi	r0,30
	nop
	lsri	r0,2	;/4
	
	dexti	5
	movei	r1,$a0
	
	movei	r3,drawFrame
	mul	r0,r1,r0	;r0 = low
	moveih	r3,>drawFrame
	gmulhi	r1		;r1 = high 
	
	rqldi	r3,0	;high
	rqldi	r3,1	;low
	nop
	ld	r3	;high
	ld	r2	;low
	
	nop
	add	r2,r2,r0	;low
	addt	r3,r3,r1	;high
	dexti	$ff
	movei	r0,$e0
	esadr	r3,r2
	est	r0,0
	est	r0,1
	est	r0,2
	est	r0,3
	est	r0,4
	est	r0,5
	est	r0,6
	est	r0,7
	
	addi	r2,8
	addtqi	r3,r3,0
	nop
	esadr	r3,r2
	est	r0,0
	est	r0,1
	est	r0,2
	est	r0,3
	est	r0,4
	est	r0,5
	est	r0,6
	est	r0,7
	
debugModeDisabled	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;* waitVPos
;*
;* r0 pos
;* 

waitVPos	
	
	push	r1
	push	r2
	
	;check emulator to prevent face freez .....
	
	dexti	>UuLedR
	movei	r1,UuLedR	;bit 1 is emulator check
	nop
	rqldi	r1,0
	nop
	nop
	ld	r2
	nop
	cmpeqi	r2,1
	brts	waitVEmulator
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
waitVRealHardWare
	
	dexti	>UcVideoRVCountWHSYNC
	movei	r1,UcVideoRVCountWHSYNC	;vcount
	nop
		
waitV	
	rqldi	r1,0
	nop
	nop
	ld	r2	;get vcount
	nop
	cmpeq	r0,r2
	brtc	waitV
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
waitVEmulator
	
	dexti	>UcVideoRVCountWHSYNC
	movei	r1,UcVideoRVCountWHSYNC	;vcount
	nop
		
waitVE	
	rqldi	r1,0
	nop
	nop
	ld	r2	;get vcount
	nop
	cmplo	r0,r2
	brts	waitVE
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1     	;delay slot
	nop		;delay slot
	nop		;delay slot		

	
;***********************************************************************
;* irq code start
;***********************************************************************
	
irq
	cli
	
	;save all register
	
	epushsadrl
	epushsadrh
	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;check type of irq
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0		;memory
	nop
	extri	r0,1		;timer 2
	gpci	r7,2		
	brts	irqTimer2	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0		;memory
	nop
	extri	r0,2		;dac
	gpci	r7,2		
	brts	irqDAC	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;restore all used register and go back
	
	rqpop		
	rqpop	
	rqpop	
	pop	r7
	pop	r6
	pop	r5
	
	rqpop		
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop		
	rqpop
	rqpop
	pop	r1
        pop	r0
	epopsadrh
	
	rqpop
	rti
	nop		;delay slot
	epopsadrl	;delay slot
	nop		;delay slot
	sei		;delay slot
	
;***********************************************************************
;* irq timer 2
;***********************************************************************
		
irqTimer2

	push	r0
	push	r7
	
	;mod tick
	
	
	dexti	>modTick
	movei	r0,modTick
	gpci	r7,2
	jmpi	r0,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;clear irq 2 timer
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	movei	r1,2		;timer 2
	ld	r2		;memory
	nop
	bic	r2,r2,r1
	nop
	st	r0,r2		;
	
	;back
	
	rqpop
	nop
	nop
	pop	r7
	
	rqpop
	jmpi	r7,0
	nop		;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;***********************************************************************
;* irq audio
;***********************************************************************
	
irqDAC
	;irq begin
	
	push	r7
	
	;push leds on
	
	movei	r0,UcRSwichtWLedG
	movei	r1,$ff
	moveih	r0,>UcRSwichtWLedG
	moveih	r1,$ff
	nop
	st	r0,r1	
		
	
	;copy 96 values from channel 0	

	movei	r7,channel0
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel0Buffer	;out buffer
	moveih	r7,>channel0		;data struct
	moveih	r6,>channel0Buffer
	
	;copy 96 values from channel 1	

	movei	r7,channel1
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel1Buffer	;out buffer
	moveih	r7,>channel1		;data struct
	moveih	r6,>channel1Buffer
	
	;copy 96 values from channel 2	

	movei	r7,channel2
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel2Buffer	;out buffer
	moveih	r7,>channel2		;data struct
	moveih	r6,>channel2Buffer
	
	;copy 96 values from channel 3	

	movei	r7,channel3
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel3Buffer	;out buffer
	moveih	r7,>channel3		;data struct
	moveih	r6,>channel3Buffer
	
	;copy 96 values from channel 4	

	movei	r7,channel4
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel4Buffer	;out buffer
	moveih	r7,>channel4		;data struct
	moveih	r6,>channel4Buffer
	
	;copy 96 values from channel 5	

	movei	r7,channel5
	dexti	>channelBufferdCopy
	movei	r1,channelBufferdCopy
	gpci	r5,2
	jmpi	r1,0
	movei	r0,95			;value count samples
	movei	r6,channel5Buffer	;out buffer
	moveih	r7,>channel5		;data struct
	moveih	r6,>channel5Buffer
	
	;push dac data
	
	movei	r1,channel0Buffer
	movei	r2,channel1Buffer
	movei	r3,channel2Buffer
	movei	r4,channel3Buffer
	movei	r5,channel4Buffer
	movei	r6,channel5Buffer
	moveih	r1,>channel0Buffer
	moveih	r2,>channel1Buffer
	moveih	r3,>channel2Buffer
	moveih	r4,>channel3Buffer
	moveih	r5,>channel4Buffer
	moveih	r6,>channel5Buffer
	
	movei	r0,95			;95 values to DAC
	
fillDACBuffer

	push	r1
	push	r2
	push	r3
	push	r4

	rqldi	r1,0
	rqldi	r2,0
	rqldi	r3,0
	ld	r1
	ld	r2
	ld	r3
	add	r1,r1,r2
	nop
	add	r1,r1,r3
	
	rqldi	r4,0
	rqldi	r5,0
	rqldi	r6,0
	ld	r4
	ld	r2
	ld	r3
	add	r4,r4,r2
	movei	r2,UcAudioDACBase
	add	r4,r4,r3
			
	moveih	r2,>UcAudioDACBase
	movei	r3,0			;zero
	
	
	stinc	r2,r1	;dac left high = value
	movei	r7,$1
	stinc	r2,r3	;dac left low = 0
	moveih	r7,$1
	stinc	r2,r4	;dac right high = value
	nop
	stinc	r2,r3	;dac right low = 0
	nop
	st	r2,r7	;dac address/ctrl = $0101	;ctrl = enable audio
	
	rqpop	
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2
	
	rqpop
	addi	r6,1
	addi	r5,1
	pop	r1

	subi	r0,1
	brts	fillDACBuffer
	addi	r1,1		;delay slot						
	addi	r4,1		;delay slot
	addi	r3,1		;delay slot
	addi	r2,1		;delay slot
	
	;push leds off
	
	movei	r7,UcRSwichtWLedG
	movei	r6,$0
	moveih	r7,>UcRSwichtWLedG
	nop
	st	r7,r6	
	
	;clear stored audio irq
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	movei	r1,4		;dac
	ld	r2		;memory
	nop
	bic	r2,r2,r1
	nop
	st	r0,r2		;
	
	;restore all used register and go back
	
	rqpop		
	nop
	nop	
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* initAudio
;*
;* return
;*
;* r0 = 0 (PASS)
;*

initAudio	

	push	r7
	
	movei	r6,audioDefault	
	movei	r5,2			;y cursor	
	moveih	r6,>audioDefault
	nop
	
initAudioI2CWriteLoop
	
	rqldi	r6,0
	rqldi	r6,1
	nop
	ld	r2		;address
	ld	r3		;data
	
	cmpeqi	r2,$ff		;stop at sentinel ($ff)
	brts	initAudioI2CWriteFinish
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
	gpci	r7,2		;next 4 + X
	br	i2cWM8731WriteWord	;r2 = address;	r3 = data; return r0 = result
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	; ;print i2c 
	; push	r0	;result
	; push	r2	;address
	; push	r3	;value
	
	; gpci	r7,2			;next 4 + X
	; br	drawTextFormated	;r0 = text; r1 = cursor x; r2 = cursor y	
	; movei	r0,i2cSetupString	;delay slot
	; movei	r1,0			;x = 0		delay slot
	; moveih	r0,>i2cSetupString	;delay slot
	; movei	r2,2;or	r2,r5,r5		;y = y + 2	delay slot
	
	; rqpop
	; rqpop
	; rqpop
	; pop	r3
	; pop	r2
	; pop	r0
		
	br	initAudioI2CWriteLoop
	addi	r6,2		;next lut address	delay slot
	addi	r5,1		;next y counter		delay slot
	nop			;delay slot
	nop			;delay slot

	movei	r0,0
	
initAudioI2CWriteFinish	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* i2cWM8731WriteWord
;*
;* r2 = address
;* r3 = data
;* 
;* return:
;*
;* r0 = result
;*

i2cWM8731WriteWord

	push	r4
	push	r1
	push	r6
	push	r7
	
	;start
	
	dexti	>i2cSendStart
	movei	r4,i2cSendStart
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	movei	r6,UcI2CSoftPortBCtrl		;delay slot
	nop					;delay slot
	moveih	r6,>UcI2CSoftPortBCtrl		;I2C ports b	;delay slot
	nop					;delay slot
	
	;write id
	
	dexti	>i2cSend8WACK
	movei	r4,i2cSend8WACK
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	movei	r1,$34		;WRITE			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$01		;delay slot
	nop			;delay slot
	
	;write address
	
	add	r1,r2,r2	;adr*2
	extri	r3,8
	addtqi	r1,r1,0		;+ bit8 of data
	
	dexti	>i2cSend8WACK
	movei	r4,i2cSend8WACK
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$02		;delay slot
	nop			;delay slot
		
	;write data low

	dexti	>i2cSend8WACK
	movei	r4,i2cSend8WACK
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	or	r1,r3,r3	;WRITE		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$04		;delay slot
	nop			;delay slot
	
	;finished without fail
	
	movei	r0,0		;all fine
	
	;stop
	
i2cSend8WACKFailsWr

	dexti	>i2cSendStop
	movei	r4,i2cSendStop
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cSendStop
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cSendStop
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cSendStop
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	dexti	>i2cSendStop
	movei	r4,i2cWaitHalfPeriode
	gpci	r7,2	;next 4 + X
	jmpi	r4,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r1

	rqpop
	jmpi	r7,0
	nop			;delay slot
	pop	r4		;delay slot
	nop			;delay slot
	nop			;delay slot	

;******************************
;*
;* r0 	 	value
;* r1		count-1 (32 words per count)
;* r3:r2	dest
;*
;* trash r4
;*

memclr32

	push	r4
	push	r3
	push	r2

clearLoop

	movei	r4,3
clearInnerLoop
	esadr	r3,r2
	est	r0,0
	est	r0,1
	est	r0,2
	est	r0,3
	est	r0,4
	est	r0,5
	est	r0,6
	est	r0,7
	subi	r4,1
	brts	clearInnerLoop
	addi	r2,8	;delay slot
	addtqi	r3,r3,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	subi	r1,1
	brts	clearLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	rqpop
	rqpop
	pop	r2
	pop	r3
	pop	r4
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* setupVideo
;*

setupVideo

	
	movei	r0,videoDefault		;source
	movei	r1,UcTimer2Ctrl		;dest-1
	moveih	r0,>videoDefault
	moveih	r1,>UcTimer2Ctrl			
	movei	r2,26			;27-1
	
setupVideoLoop

	rqldi	r0,0
	addi	r0,1
	addi	r1,1
	ld	r3
	subi	r2,1
	brts	setupVideoLoop
	st	r1,r3	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;* iToECopy
;*
;* r0 source
;* r2:r1	dest
;* r4 count
;* 
;* waste r0 .. r4
;*	

iToECopy

	subi	r4,1
	
iToECopyLoop
	
	rqldi	r0,0
	addi	r0,1
	subi	r4,1
	ld	r3
	brts	iToECopyLoop
	esadr	r2,r1		;delay slot
	est	r3,0		;delay slot
	addi	r1,1		;delay slot
	addtqi	r2,r2,0		;delay slot
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;* extCopy8
;*
;* r1:r0 source
;* r3:r2	dest
;* r4 count - 1 (8 words per count)
;* 
		
extCopy8	

	push	r5
	push	r6
	
extCopy8Loop
	esadr	r1,r0
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	
	esadr	r3,r2
	eld	r5
	eld	r6
	est	r5,0
	est	r6,1
	eld	r5
	eld	r6
	est	r5,2
	est	r6,3
	eld	r5
	eld	r6
	est	r5,4
	est	r6,5
	eld	r5
	eld	r6
	est	r5,6
	est	r6,7
	

	subi	r4,1
	brts	extCopy8Loop
	addi	r0,8		;delay slot
	addtqi	r1,r1,0		;delay slot
	addi	r2,8		;delay slot
	addtqi	r3,r3,0		;delay slot
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r6	;delay slot
	pop	r7     	;delay slot
	nop		;delay slot
	nop		;delay slot	


;*********************************************************************
;* data
;*********************************************************************
	
videoDefault		;AD NAME					DEFAULT
	                                                                
	word	$0001	;10 VIDEO_ON					1 (ON)
	word	$0097	;11 HSYNC (D = 11 .. 0)				152-1
	word	$017f	;12 HSTART (D = 11 .. 0)			384-1
	word	$017d	;13 HMEMSTART (D = 11 .. 0)			382-1
	word	$071f	;14 HSTOP (D = 11 .. 0)				1824-1
	word	$076f	;15 HTOTAL (D = 11 .. 0)			1904-1
	word	$0002	;16 VSYNC (D = 10 .. 0)				3-1
	word	$001e	;17 VSTART (D = 10 .. 0)			31-1
	word	$03a2	;18 VSTOP  (D = 10 .. 0)			931-1
	word	$03a3	;19 VTOTAL  (D = 10 .. 0)			932-1
	word	$0000	;1a LC ADDER LOW				$0000
	word	$0100	;1b LC ADDER HIGH				$0040	
	word	$0000	;1c LC ADDER LATCH				X
	word	$0000	;1d LC START (10 .. 0) Pixeloffset		$0000	
	word	$0000	;1e						X
	word	$0000	;1f						X
	word	$0008	;20 HLOADSTART 					8
	word	$02d8	;21 HLOADSTOP					180+8
	word	$001e	;22 VLOADSTART 					31-1
	word	$03a2	;23 VLOADSTOP 					931-1
	word	$0001	;24 VLOADNEXT					4
	word	$0000	;25 MEM_STARTADR_store low			$0000
	word	$d000	;26 MEM_STARTADR_store high			$f000
	word	$0000	;27 MEM_LINEOFFSET_store low			$0000
	word	$0000	;28 MEM_LINEOFFSET_store high			$0000
	word	$0002	;29 MEM_MODE_store 				2 (X1R5G5B5X1R5G5B5)
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X
	

audioDefault
	
	;
				;A =  8 7654 3210
	.data   $9,$0000	;9 =  0 0000 0000 	(inactivate interface)
	.data	$0,$0097	;0 =  0 1001 0111	;left line in mute
	.data	$1,$0097	;1 =  0 1001 0111	;right line in mute
	.data	$2,$0079	;2 =  0 0111 1001	;+6db left out
	.data	$3,$0079	;3 =  0 0111 1001	;0db right out
	.data	$4,$0012	;4 =  0 0001 0010	;0=0 mic boost off;
							;1=1 enable line input mute
							;2=1 line input to adc
							;3=0 disable bypass
							;4=1 DAC select
							;5=0 side tone disable
							;76=0 -6db sidetone						
	.data	$5,$0000	;5 =  0 0000 0000	;DAC soft Mute off, no Filer
	.data	$6,$0067	;6 =  0 0110 0111 	;DAC Power, Output Power, Device Power on
	.data	$7,$0009	;7 =  0 0000 1001 	;left justified, 24 bit
	.data	$8,$001e	;8 =  0 0001 1110	;0=0 normal mode
							;1=1 bosr (384/192)
							;5432=0111 96khz
							;78=00 no clk dividing	
	.data   $9,$0001	;9 =  0 0000 0001 	(activate interface)
	.data	$ff,$0000	;stop
	
;*********************************************************************
;* channel data
;*********************************************************************

channel0	
	.data	$ffff,$0,0,63,3530,0,0,$e340,$d009	;channel 0
	.space	12
	
channel1	                     
	.data	$ffff,$0,0,63,3530,0,0,$e340,$d009	;channel 1       	
	.space	12
	
channel2		                              
	.data	$ffff,$0,0,63,3530,0,0,$e340,$d009	;channel 2
	.space	12
	
channel3		                              
	.data	$ffff,$0,0,63,3530,0,0,$e340,$d009	;channel 3	
	.space	12
	
channel4	                           
	.data	$ffff,$0,0,63,3530,0,0,$e340,$d009	;channel 4  	
	.space	12
	
channel5	                            
	.data	$ffff,$0,0,63,3530,0,0,$e340,$d009	;channel 5 		
	.space	12
	
	
modChannels

	word	channel1
	word	channel2
	word	channel3
	word	channel4
	
channel0Buffer
	.space	96

channel1Buffer
	.space	96

channel2Buffer
	.space	96
	
channel3Buffer
	.space	96
		
channel4Buffer
	.space	96

channel5Buffer
	.space	96

;*********************************************************************
;* text data
;*********************************************************************	

textHeader	
	.stringz		"Sound Test MOD, Switch (Info): 0 = CPU Load, 1 = Sample, 2 = Song, 3 = Pattern, 4 = Play MOD"
	
;i2cSetupString
;	.stringz		"i2c write value %x to address %x result %x"

modSongName
	.stringz	"Songname '%Z'"
	
modSampleText
	.stringz	"#%x Name '%Z' Len %u Rep (Start %u Len %u) Vol %u Fine %u"

modPatternSongLengthText

	.stringz	"Song length %u"

modPatternSongPosText

	.stringz	"Song position %u -> Pattern %u"

modePatternRowText

	.stringz	"%x"
	
modPatternCellText

	.stringz	"%z %x %x"
	
modPatternInfoText	

	.stringz	"Row %x Songposition %x Pattern %x"	
	
modChannelInfoText

	.stringz	"Repeat Start %x%x Inc %x Vol %x Len %x Pos %x%x SAdr %x%x (Cmd %x Note %x Per %x Vol %x SmplNum %x)" 

;
	
textReaded
	.space	32	
	
displayFrame	

	.data	$d000,$0000

drawFrame	

	.data	$d013,$c680
	
screenWidth
	word	1440
	
fontAddress
	word	$f000
	word	$0000
	
;*********************************************************************
;* data 
;*********************************************************************	
	
	
	.def	fontDataROM	$e0017f68
	.def	fontDataRAM	$f0000000	;length = 24064 byte -> $2f00 words
	.def	modData		$f0002f00
	
;*********************************************************************
;* mod data
;*********************************************************************
	
	.incdir		"F:\git\c_sharp\MODTest_Songs\songs"
	
modDataIRAM
	
	.incbin		"rakad armhole.chip.mod"
	
	.def	modLength	2609		;5218 byte	
	
	
