	dc.l $00000000,	EntryPoint,	ErrorTrap,	ErrorTrap	;00
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;10
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;20
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;30
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;40
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;50
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;60
	dc.l HInt,	ErrorTrap,	VInt,		ErrorTrap	;70
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;80
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;90
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;A0
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;B0
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;C0
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;D0
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;E0
	dc.l ErrorTrap,	ErrorTrap,	ErrorTrap,	ErrorTrap	;F0

check_sum equ	$0000
;------------------------------------------------------------------------
	dc.b	'SEGA GENESIS    '	;100
	dc.b	'(C)T-xx 2008.06 '	;110 release year.month
	dc.b	'YM2612 CSM Mode '	;120 Japan title
	dc.b	'                '	;130
	dc.b	'                '	;140
	dc.b	'YM2612 CSM Mode '	;150 US title
	dc.b	'                '	;160
	dc.b	'                '	;170
	dc.b	'GM T-XXXXXX XX'  	;180 product #, version
	dc.w	check_sum         	;18E check sum
	dc.b	'J               '	;190 controller
	dc.l	$00000000,$0007ffff,$00ff0000,$00ffffff		;1A0
	dc.b	'                '	;1B0
	dc.b	'                '	;1C0
	dc.b	'                '	;1D0
	dc.b	'                '	;1E0
	dc.b	'JUE             '	;1F0


ErrorTrap:
	jmp	ErrorTrap

HInt:
VInt:
	rte

EntryPoint:
	move.w #$FFFF,D0
	EntryWaitLoop:
	dbf D0,EntryWaitLoop

	Z80WaitLoop:
	move.b #1,$A11100	;BUSREQ
	move.b #1,$A11200	;RESET
	btst.b #0,$A11100
	bne Z80WaitLoop

	;Set channel 3 global settings
	move.b #$B2,D0
	move.b #$07,D1	;Set algorithm to 7 (4 channel parallel)
	bsr WriteYm2612_Part1
	move.b #$B6,D0
	move.b #$C0,D1	;Set L/R,AMS/PMS
	bsr WriteYm2612_Part1

	;Initialize channel 3 op 4 with a decaying envelope
	move.b #$4E,D0
	move.b #$00,D1	;Set TL
	bsr WriteYm2612_Part1
	move.b #$5E,D0
	move.b #$1F,D1	;Set KS/AR
	bsr WriteYm2612_Part1
	move.b #$6E,D0
	move.b #$1F,D1	;Set AM/DR
	bsr WriteYm2612_Part1
	move.b #$7E,D0
	move.b #$00,D1	;Set SR
	bsr WriteYm2612_Part1
	move.b #$8E,D0
	move.b #$0B,D1	;Set SL/RR
	bsr WriteYm2612_Part1
	move.b #$9E,D0
	move.b #$00,D1	;Set SSG-EG
	bsr WriteYm2612_Part1

	;Set DT/MULTI
	move.b #$3E,D0
	move.b #$00,D1
	bsr WriteYm2612_Part1

	;Set channel 3 frequency to A6
	move.b #$A6,D0
	move.b #$34,D1
	bsr WriteYm2612_Part1
	move.b #$A2,D0
	move.b #$43,D1
	bsr WriteYm2612_Part1

	;Set timer A to the longest period
	move.b #$24,D0
	move.b #$00,D1
	bsr WriteYm2612_Part1
	move.b #$25,D0
	move.b #$00,D1
	bsr WriteYm2612_Part1

	;Enable timer A and channel 3 CSM mode
	move.b #$27,D0
	move.b #$81,D1
	bsr WriteYm2612_Part1

	jmp ErrorTrap

;----------------------------------------------------------------------------
;YM2612 Interface
;----------------------------------------------------------------------------
WriteYM2612_Part1:
	btst #7,$A04000
	bne WriteYM2612_Part1
	move.b D0,$A04000
WriteYM2612Data_Part1:
	btst #7,$A04000
	bne WriteYM2612Data_Part1
	move.b D1,$A04001
	rts
WriteYM2612Address_Part1:
	btst #7,$A04000
	bne WriteYM2612Address_Part1
	move.b D0,$A04000
	rts

WriteYM2612_Part2:
	btst #7,$A04000
	bne WriteYM2612_Part2
	move.b D0,$A04002
WriteYM2612Data_Part2:
	btst #7,$A04000
	bne WriteYM2612Data_Part2
	move.b D1,$A04003
	rts
WriteYM2612Address_Part2:
	btst #7,$A04000
	bne WriteYM2612Address_Part2
	move.b D0,$A04002
	rts

ReadYM2612_0:
	btst #7,$A04000
	bne ReadYM2612_0
	move.b $A04000,D1
	rts
ReadYM2612_1:
	btst #7,$A04000
	bne ReadYM2612_1
	move.b $A04001,D1
	rts
ReadYM2612_2:
	btst #7,$A04000
	bne ReadYM2612_2
	move.b $A04002,D1
	rts
ReadYM2612_3:
	btst #7,$A04000
	bne ReadYM2612_3
	move.b $A04003,D1
	rts
