; This is a simple sample program to play a SHORT tune
; on the synth code.

	.include	'notes.inc'
	.include	'jaguar.inc'
	.include	'allsynt.def'

	.extern		GPUSTART
	.extern		GPUEND
	.extern		DSPORG

	HIBIT		equ		$80000000

	move.w	#$8005,$f10014

	move.l	#$00070007,D_END	; Set GOOD mode

	move.l	#0,D_CTRL		; Turn DSP off

	move.l	#0,D_FLAGS		; Set known state

; First we copy the Jerry code into Jerry
; The label GPUSTART will be created by LTXCONV/ALN
; at the start of the block.

; The code gets loaded into the DSP at DSPORG

	move.l	#DSPORG,a1
	move.l	#GPUSTART,a0
	move.l	a0,d0
	move.l	#GPUEND,d1
	sub.l	d0,d1
	asr.l	#2,d1
translop:
	move.l	(a0)+,(a1)+
	dbra	d1,translop

; Now we need to clear the first long word of each "Voice table"
; Initialize 10 of these of length TABOFF starting at 
; tablestart. Then set number 11 to 0

	move.l	#tablestart,a0
	move.l	#-1,d0

	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	d0,(a0)

	add.l	#taboff,a0
	move.l	#0,d0
	move.l	d0,(a0)

; Now we need to set the first long word of the "Effect table" to -1
; EFFECTPOS

	move.l	#EFFECTPOS,a0
	move.l	#-1,d0
	move.l	d0,(a0)


; Set up the I2S interface

	move.l	#19,SCLK
	move.l	#$15,SMODE

; Now it should be safe to turn on the DSP

	move.l	#DSPORG,D_PC	; Set up D_PC

	move.l	#$1,D_CTRL	; LET 'R RIP

; Now we set up an FM2 module, one voice

	move.l	#tablestart,a0

vlop:
	jsr	makevoice

; At this point the voice is playing @ pitch $0

; Now play the pitches and durations in songtab

	move.l	#songtab,a1
	move.w	(a1)+,d2
	subq	#1,d2

	move.l	#tablestart+16,a0

notelop:
	move.l	(a1)+,d1
	move.l	d1,(a0)		; set the pitch

	move.l	(a1)+,d0	; read and do delay
	jsr	dlop

	move.l	#0,(a0)	; kill the pitch 
	move.l	#1,d0		; to turn note off
	jsr	dlop		; wait a bit

	dbra	d2,notelop

	illegal

makevoice:
	move.l	#ROM_SINE,d0	; Carrier wave
	move.l	d0,(1*4,a0)	; offset 1 long 

	move.l	#ROM_SINE,d0	; Modulator
	move.l	d0,(2*4,a0)	; offset 2 long 

	move.l	#0,d0		; Reset
	move.l	d0,(3*4,a0)	; offset 3 long

	move.l	#$0,d0		; Pitch of Carrier (Note is off)
	move.l	d0,(4*4,a0)	; offset 4 long

	move.l	#0,d0		; Reset
	move.l	d0,(5*4,a0)	; offset 5 long
 
	move.l	#$4000,d0	; Volume
	move.l	d0,(6*4,a0)	; offset 6 long

	move.l	#0,d0		; Reset
	move.l	d0,(7*4,a0)	; offset 7 long

	move.l	#$0,d0		; Frequency of Modulation
	move.l	d0,(8*4,a0)	; offset 8 long

	move.l	#$0,d0		; Amplitude of Modulation
	move.l	d0,(9*4,a0)	; offset 9 long

	move.l	#$0,d0		; Reset
	move.l	d0,(10*4,a0)	; offset 10 long

	move.l	#$0,d0		; Reset
	move.l	d0,(11*4,a0)	; offset 11 long

	move.l	#$0,d0		; Reset
	move.l	d0,(12*4,a0)	; offset 12 long

	move.l	#$100,d0	; Frequency of modulator of depth
	move.l	d0,(13*4,a0)	; offset 13 long

	move.l	#$0,d0		; depth of modulation of depth
	move.l	d0,(14*4,a0)	; offset 14 long

	move.l	#0,d0		; Reset
	move.l	d0,(15*4,a0)	; offset 15 long

	move.l	#$100,d0	; Frequency of modulator of frequency
	move.l	d0,(16*4,a0)	; offset 16 long

	move.l	#$0,d0		; Depth of modulator of frequency
	move.l	d0,(17*4,a0)	; offset 17 long

	move.l	#$3fff,d0	; PAN value
	move.l	d0,(19*4,a0)	; offset 19 long

	move.l	#$7000,d0	; TURN UP THE VOLUME
	move.l	d0,MIDIvol	; Store it away

	move.l	#4*4,d0		; FM2 voice
	move.l	d0,(a0)		; TURN IT ON

	rts

dlop:
	move.w	#$8000,d7
ilop:
	dbra	d7,ilop
	dbra	d0,dlop

	rts

.data
.even

songtab:
	dc.w	8	; number of notes
	dc.l	NOTE_D4
	dc.l	4
	dc.l	NOTE_D4
	dc.l	4
	dc.l	NOTE_D4
	dc.l	4
	dc.l	NOTE_D4
	dc.l	4
	dc.l	NOTE_F4
	dc.l	4
	dc.l	NOTE_E4
	dc.l	4
	dc.l	NOTE_D4
	dc.l	4
	dc.l	NOTE_C4
	dc.l	7

