
; 	This is the synth setup and loading program
;
;
;----------------------------------------------------------------------------

	.include	'jaguar.inc'
	.include	'syn.inc'
;----------------------------------------------------------------------------
; 	DEFINES
;----------------------------------------------------------------------------
	TIMER	equ	$F10000

;----------------------------------------------------------------------------
; 	GLOBALS
;----------------------------------------------------------------------------
	.globl	_init_synth
	.globl	_play_midi
	.globl	_c_play_midi
	.globl	midi_score
	.globl	_midi_score
	.globl	_midi_score2
	.globl	play_flag
	.globl	_play_flag
	.globl	_patchtab

	.globl	_sclock

	.globl	__synth_copy
	.globl	__dsp_start	
	.globl	__scorep1
	.globl	__scorep2
	.globl	__plistp1
	.globl	__mtimep1
	.globl	__mtimep2
	.globl	__shutdown
	.globl	__def_patches
	.globl	__xvalue
	.globl	_stop_midi
	.globl	_sound_off
	.globl	__effect_mode
	.globl	__pr_table
		
;----------------------------------------------------------------------------
; 	EXTERNALS
;----------------------------------------------------------------------------

	.extern	SYNTH_COPY
	.extern	DSP_START	
	.extern	SCOREP1
	.extern	SCOREP2
	.extern	PLISTP1
	.extern	MTIMEP1
	.extern	MTIMEP2
	.extern	SHUTDOWN

	.extern	MDTAB_PWAVE
	.extern	MDTAB_PWAVE2
	.extern	MDTAB_SAMP
	.extern	MDTAB_FSAMP
	.extern	MDTAB_FM
	.extern	MDTAB_FWAVE
	.extern	MDTAB_PULSE
	.extern	ENV_DEF_1
	.extern	ENV_DEF_2
	.extern	FILTER_DEF_1
	.extern	FILTER_DEF_2
	.extern	EFFECT_MODE

	.extern	_patchtab
	.extern	_reverb_buf
	.extern	event			; MIDI Event
	.extern	_event			; MIDI Event
	.extern	_syn_sym
	.extern	priority
	.text

; Init the DSP code
_init_synth:	
	movem.l	d1-d7/a0-a6,-(sp)
	jsr	_syn_sym		; get symbol data

; //new: (6/15/95) - clear midi buffer
	move.l	#$0ffffff0, midi_score+8
	move.l	#$00000000, midi_score+12
	move.l	#$0ffffff0, midi_score+16

	move.l	#$0ffffff0, midi_score2+8
	move.l	#$00000000, midi_score2+12
	move.l	#$0ffffff0, midi_score2+16
; \\

	move.l	#$00070007,D_END	; Set GOOD mode
	move.l	#0,D_CTRL		; Turn DSP off
	move.l	#0,D_FLAGS		; Set known state

	move.l	#$1e4000,a0		; Clear out reverb buffer
	move.l	#$0,d0			; reverb_buf always at $1e4000 !!!
	move.l	#$2000,d1
slop:
	move.l	d0,(a0)+
	dbra	d1,slop

	
; At this point we assume that the DSP is turned off!

; 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	__synth_copy,a0
;;	move.l	#SYNTH_COPY,a0
	move.l	(a0)+,a1
	move.l	(a0)+,d1
	asr.l	#2,d1
translop:
	move.l	(a0)+,(a1)+
	dbra	d1,translop


; Copy midi locations

	move.l	__scorep1, a0
	move.l	#midi_score+8, (a0)
	move.l	__scorep2, a0
	move.l	#midi_score2+8, (a0)
	move.l	__plistp1, a0
	move.l	#_patchtab, (a0)

	move.b	#0, midi_switch

; Make sure the DACs are silent!
	move.l	#0, R_DAC
	move.l	#0, L_DAC

; Set up the I2S mode and clock
	
	move.l	#0,D_FLAGS		; Set known state

	move.w	#0, TIMER
	move.w	#0, TIMER+2

	move.l	_sclock,SCLK
	move.l	#$15,SMODE		; start interrupt

; Setup TIMER 1
	move.w	#$69,  TIMER+2
	move.w	#$100, TIMER

; Enable sound (mute bit)

	move.w	#$100,JOYSTICK

;*******
;*******	FOR DEBUG1GING ONLY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;*******
	move.w	#0, dbufix
;;	move.l	#dbuf2, __dbuf2p
if 0
	move.w	#32000-1, d1
	move.l	#dbuf, a0
	move.l	#dbuf2, a1
.ld1:
	move.l	#$ffffffff, (a0)+
	move.l	#$ffffffff, (a1)+
	dbra	d1,.ld1
	move.w	#0, dbufix

	move.l	__dbuf2p, a0
	move.l	#dbuf2, (a0)
.endif

	move.l	__midi_bak, a1
	move.l	#$ffffffff, (a1)
	move.l	#$ffffffff, old_event

	move.w	#0, play_flag

; Now it should be safe to turn on the DSP
	move.l	__dsp_start,D_PC ; Set up D_PC
;;	move.l	#DSP_START,D_PC ; Set up D_PC
	move.l	#$1,D_CTRL	  ; LET 'R RIP
	movem.l	(sp)+, d1-d7/a0-a6
	rts

; THIS IS NEW MAGIC TO CAUSE THE DSP TO STOP
; AT THE NEXT SAMPLE INTERRUPT

synth_off:
	move.l	__shutdown,d0
	and.l	#$ffff,d0
	or.l	#$981d0000,d0
	move.l	d0,$f1b010

	rts

;;	plays a translated MIDI event

_c_play_midi:
	movem.l	d0-d7/a0-a6, -(a7)
	jsr	_play_midi
	movem.l	(a7)+, d0-d7/a0-a6
	rts

_play_midi:
	cmp.w	#1, _sound_off
	bne	sound_is_on
	rts
sound_is_on:
	move.l	#midi_score, a1
	move.l	a1, d2
	addq.l	#8, d2
	move.l	__scorep1,a0	


	move.l	(a0),d1			;;	move.l	SCOREP1,d1
	cmp.l	d2, d1			;;	cmp.l	#midi_score+8, d1
	beq	.pl_check2
	rts

.pl_check2:
	move.l	__midi_bak, a2		; check if the synth really got our
	move.l	(a2), d2
	move.l	old_event, d1		; last event, if not, we sent it again
	cmp.l	d1, d2
	beq	.pl_go

	move.l	__mtimep1, a2
	move.l	(a2),d1
	move.l	d1, (a1)
	addq.l	#4, a1
	move.l	old_event, d1		; this used to be one instr
	move.l	d1, (a1)		; move.l event, midi_score+4
					; but maybe it screws up
	subq.l	#4, a1
	move.l	a1,(a0)			;;	move.l	#midi_score,SCOREP1
; Set the beginning of time

;;	move.l  #0,(a2)			;;      move.l  #0,MTIMEP1
	rts
	
.pl_go:	
	move.l	__mtimep1, a2

	clr.l	d0
	move.l	#29,d1
	move.w	priority, d0
	lsl.l	d1,d0
	move.l	(a2),d1
	and.l	#$1fffffff,d1
	or.l	d0,d1
	move.l	d1, (a1)
	addq.l	#4, a1
	move.l	event, d1		; this used to be one instr
	move.l	d1, (a1)		; move.l event, midi_score+4
					; but maybe it screws up

	move.l	d1, old_event


	subq.l	#4, a1
	move.l	a1,(a0)			;;	move.l	#midi_score,SCOREP1
; Set the beginning of time

;;	move.l  #0,(a2)			;;      move.l  #0,MTIMEP1



;*******
;*******	FOR DEBUGGING ONLY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;*******
if 0
.rept	20
	nop			;8
.endr
endif
	
if 1


	move.l	#dbuf, a0	;24
	move.l	#0,d1		;8
	move.w	dbufix, d1	;24
	add.l	d1, a0		;10
	addq.w	#4, dbufix	;16
	move.l	old_event, (a0)	;56
	subq.w	#4, dbufix	;24
;                              -----
;                               162
endif



	move.w	#0, play_flag
	rts


; ************************ END OF MAIN CODE ***********************



	
	.data
	.long
midi_thing:
	dc.l $7ffffff0

	.BSS

		.phrase
_sclock:
	.ds.l	1

midi_score:			; midi score buffer
_midi_score:
	ds.l 6

midi_score2:			; midi score buffer 2
_midi_score2:
	ds.l 6
	
midi_switch:
	ds.b 1
	ds.b 1

	.long
old_event:
	.ds.l	1

	.phrase

__midi_bak::
	ds.l	1
__synth_copy:
	ds.l	1
__dsp_start:
	ds.l	1
__scorep1:
	ds.l	1
__scorep2:
	ds.l	1
__plistp1:
	ds.l	1
__mtimep1:
	ds.l	1
__mtimep2:
	ds.l	1
__shutdown:
	ds.l	1
__attn::
	ds.l	1
__delay::
	ds.l	1
__dft_filter::
	ds.l	1
__filter_def_1::
	ds.l	1
__filter_def_2::
	ds.l	1
__mixer::
	ds.l	1
__reverb::
	ds.l	1
__osc_sampler::
	ds.l	1
__osc_sampler_loop::
	ds.l	1
__osc_nisampler::
	ds.l	1
__osc_fixedwave::
	ds.l	1
__osc_fixedwave_loop::
	ds.l	1
__osc_fm::
	ds.l	1
__osc_fm_loop::
	ds.l	1
__osc_polywave::
	ds.l	1
__osc_noise::
	ds.l	1
__osc_fx_pw::
	ds.l	1
__osc_fx_value::
	ds.l	1
__osc_fx_dist::
	ds.l	1
__osc_fx_pan::
	ds.l	1
__env_fx_pw::
	ds.l	1
__osc_fx_pitch::
	ds.l	1
__env_fx_dist::
	ds.l	1
__env_fx_pan::
	ds.l	1
__env_fx_pitch::
	ds.l	1
__env_slot1::
	ds.l	1
__env_slot2::
	ds.l	1
__env_slot3::
	ds.l	1
__loopenv_slot1::
	ds.l	1
__loopenv_slot2::
	ds.l	1
__loopenv_slot3::
	ds.l	1
__loopenv_skip::
	ds.l	1
__lfo_1::
	ds.l	1
__lfo_2::
	ds.l	1
__dist::
	ds.l	1
__mdtab_start::
	ds.l	1
__mdtab_end::
	ds.l	1
__mdtab_reverb::
	ds.l	1
__vt_return_loc::
	ds.l	1
__vt_ret_value::
	ds.l	1
__env_start::
	ds.l	1
__env_end::
	ds.l	1
__env_temp::
	ds.l	1
__env_temp_end::
	ds.l	1

__def_patches:
	ds.l	1
__xvalue:
	ds.l	1
__effect_mode::
	ds.l	1
__pr_table::
	ds.l	1
__reverb_table::
	ds.l	1
__voice_buffer::
	ds.l	1
__voice_table::
	ds.l	1

	
;**************************************************************
;	THE PATCH TABLE 
	.phrase
_patchtab:
	.ds.l	NR_PATCHES * PATCH_SIZE	; 128 patches a 21 longs

;**************************************************************

_play_flag:
play_flag:
	ds.w 	1

play_count:
	ds.w 	1

_stop_midi:
	ds.w	1

_sound_off:
	ds.w	1



;*******
;*******	FOR DEBUGGING ONLY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;*******

	.long
__dbuf2p::
	.ds.l	1
dbufix:
	.ds.w	1

	.long
dbuf:
	.ds.l	3
if 0
	.long
dbuf2:
	.ds.l	32000
endif

	.data
_reverb_voice::
	;VOICE 13
	.dc.l	0		;Synth type		              Offset
	.dc.l	$FFFF8000	;Rflags register			1
	.dc.l	14		;Number of voices			2
	.dc.l	0		;voice_buffer location			3
	.dc.l	0		;Send values for reverb			4
REVERBPTR:
	.dc.l	$001e4000	;Reverb buffer location	@ 1.9MBYTE	5
	.dc.l	$00001400	;Delay Tap#1 location			6
	.dc.l	$00001810	;Delay Tap#1 Volume			7
	.dc.l	$00000300	;Delay Tap#2 location			8
	.dc.l	$00003240	;Delay Tap#2 Volume			9
	.dc.l	$00000600	;Delay Tap#3 location			10
	.dc.l	$00003135	;Delay Tap#3 Volume			11
	.dc.l	$000002CA	;Delay Tap#4 location			12
	.dc.l	$000030F3	;Delay Tap#4 Volume			13
	.dc.l	$00000000	;Env#3 envelope location		14
	.dc.l	$00000000	;Env#3 value 15.15			15
	.dc.l	$00000000	;Env#3 Release slope 15.15		16
	.dc.l	$00000000	;					17
	.dc.l	$00000000	;					18
	.dc.l	$00000000	;					19
	.dc.l	$00007FFF	;Volume	not used yet			20

_roar_raw::
	.incbin "roar.raw"

_roar_cmp::
	.incbin "roar.cmp"
