* 	BasiEgaXorz Sega Genesis Assembly Header + Routines
*	v 0.12
*	Coding by: Joseph Norman
*	Copyright 2003-2004
*	If you use any portion of this code in your own project, please give me credit =D

MCOMMH	EQU	$A1200E
MCOMMW	EQu	$A12010
MCOMMR	EQu	$A12020
SCOMMH	EQU	$FF800E
SCOMMR	EQU	$FF8010
SCOMMW	EQU	$FF8020

	if	def(SegaCart)		;	Cartridge Header
	org	$000
	dc.l	$01000000,__start,vector_buserror,vector_addresserror,vector_illegalinstr,vector_dividezero,vector_chk,vector_trapv
	dc.l	vector_privilegeviolation,vector_trace,vector_line1010,vector_line1111,__start,__start,__start,__start
	dc.l	__start,__start,__start,__start,__start,__start,__start,__start
	dc.l	__start,__start,__start,__start,__hblank,__start,__vblank,__start
	dc.l	braekpoin,vector_trap1,vector_trap2,vector_trap3,vector_trap4,vector_trap5,vector_trap6,vector_trap7
	dc.l	vector_trap8,vector_trap9,vector_trap10,vector_trap11,vector_trap12,vector_trap13,vector_trap14,vector_trap15
	dc.l	__start,__start,__start,__start,__start,__start,__start,__start
	dc.l	__start,__start,__start,__start,__start,__start,__start,__start
	dc.b	'SEGA GENESIS    '
	dc.b	'(C)SEGA 2003.DEC'
	dc.b	'                '
	dc.b	'                '
	dc.b	'                '
	dc.b	'                '
	dc.b	'                '
	dc.b	'                '
	dc.b	'GM MK-0000 -00'
	dc.w	$0000
	dc.b	'J               '
	dc.l	$00000000,$003FFFFF
	dc.l	$00E00000,$00FFFFFF
	dc.b	'                '
	dc.b	'                '
	dc.b	'                '
	dc.b	'                '
	dc.b	'JUE             '

__start:
* Disable interrupts
	move.w	#$2700,sr
* Do security check
	move.b	$A10001,d0
	andi.b	#$0F,d0
	beq.s	__bootbios_not_v1
	move.l	#'SEGA',$A14000
__bootbios_not_v1:
	jmp	__init_start
__initialize_reset:
	move.w	#$2700,sr
	jmp	__init_start

	endif

	if	def(SegaCDProg)		;	Sega CD RAM Program Header
	org	$200000
__start_segacd:
	bra.s	__start
	bra	sega_cd_prog_jump_in

sega_cd_prog_jump_in_2:
	bra	sega_cd_prog_jump_in_2

__start:
	move.w	#$2000,sr
	move.l	#$1000000,a7
	bsr	reset_reset_vectors
	jmp	__scd_go
vblank_jmp_label:		jmp	__vblank
hblank_jmp_label:		jmp	__hblank
addresserror_or_illegalinstr_jmp_label:
				jmp	vector_addresserror_or_illegalinstr
dividezero_jmp_label:		jmp	vector_dividezero
chk_jmp_label:			jmp	vector_chk
trapv_jmp_label:		jmp	vector_trapv
privilegeviolation_jmp_label:	jmp	vector_privilegeviolation
trace_jmp_label:		jmp	vector_trace
line1010_jmp_label:		jmp	vector_line1010
line1111_jmp_label:		jmp	vector_line1111
trap0_jmp_label:		jmp	braekpoin
trap1_jmp_label:		jmp	vector_trap1
trap2_jmp_label:		jmp	vector_trap2
trap3_jmp_label:		jmp	vector_trap3
trap4_jmp_label:		jmp	vector_trap4
trap5_jmp_label:		jmp	vector_trap5
trap6_jmp_label:		jmp	vector_trap6
trap7_jmp_label:		jmp	vector_trap7
trap8_jmp_label:		jmp	vector_trap8
trap9_jmp_label:		jmp	vector_trap9
trap10_jmp_label:		jmp	vector_trap10
trap11_jmp_label:		jmp	vector_trap11
trap12_jmp_label:		jmp	vector_trap12
trap13_jmp_label:		jmp	vector_trap13
trap14_jmp_label:		jmp	vector_trap14
trap15_jmp_label:		jmp	vector_trap15
__scd_go:
	jmp	__init_start
__initialize_reset:
	move.w	#$2700,sr
	move.l	#$1000000,a7
	bsr	reset_reset_vectors
	jmp	__init_start

sega_cd_prog_jump_in:
;	bra	sega_cd_prog_jump_in
	bsr	reset_reset_vectors
	move.w	#$2000,sr
	move.l	#$1000000,a7
	bra	segacd_jump_in_program

reset_reset_vectors:
	move.l	(vblank_jmp_label),($FFFD06)
	move.w	(vblank_jmp_label+4),($FFFD06+4)
	move.l	(hblank_jmp_label),($FFFD0C)
	move.w	(hblank_jmp_label+4),($FFFD0C+4)
	move.l	(addresserror_or_illegalinstr_jmp_label),($FFFD7E)
	move.w	(addresserror_or_illegalinstr_jmp_label+4),($FFFD7E+4)
	move.l	(dividezero_jmp_label),($FFFD84)
	move.w	(dividezero_jmp_label+4),($FFFD84+4)
	move.l	(chk_jmp_label),($FFFD78)
	move.w	(chk_jmp_label+4),($FFFD78+4)
	move.l	(trapv_jmp_label),($FFFD8A)
	move.w	(trapv_jmp_label+4),($FFFD8A+4)
	move.l	(privilegeviolation_jmp_label),($FFFD9C)
	move.w	(privilegeviolation_jmp_label+4),($FFFD9C+4)
	move.l	(trace_jmp_label),($FFFDA2)
	move.w	(trace_jmp_label+4),($FFFDA2+4)
	move.l	(line1010_jmp_label),($FFFD90)
	move.w	(line1010_jmp_label+4),($FFFD90+4)
	move.l	(line1111_jmp_label),($FFFD96)
	move.w	(line1111_jmp_label+4),($FFFD96+4)
	move.l	(trap0_jmp_label),($FFFD18+0)
	move.w	(trap0_jmp_label+4),($FFFD18+4+0)
	move.l	(trap1_jmp_label),($FFFD18+6)
	move.w	(trap1_jmp_label+4),($FFFD18+4+6)
	move.l	(trap2_jmp_label),($FFFD18+12)
	move.w	(trap2_jmp_label+4),($FFFD18+4+12)
	move.l	(trap3_jmp_label),($FFFD18+18)
	move.w	(trap3_jmp_label+4),($FFFD18+4+18)
	move.l	(trap4_jmp_label),($FFFD18+24)
	move.w	(trap4_jmp_label+4),($FFFD18+4+24)
	move.l	(trap5_jmp_label),($FFFD18+30)
	move.w	(trap5_jmp_label+4),($FFFD18+4+30)
	move.l	(trap6_jmp_label),($FFFD18+36)
	move.w	(trap6_jmp_label+4),($FFFD18+4+36)
	move.l	(trap7_jmp_label),($FFFD18+42)
	move.w	(trap7_jmp_label+4),($FFFD18+4+42)
	move.l	(trap8_jmp_label),($FFFD18+48)
	move.w	(trap8_jmp_label+4),($FFFD18+4+48)
	move.l	(trap9_jmp_label),($FFFD18+54)
	move.w	(trap9_jmp_label+4),($FFFD18+4+54)
	move.l	(trap10_jmp_label),($FFFD18+60)
	move.w	(trap10_jmp_label+4),($FFFD18+4+60)
	move.l	(trap11_jmp_label),($FFFD18+66)
	move.w	(trap11_jmp_label+4),($FFFD18+4+66)
	move.l	(trap12_jmp_label),($FFFD18+72)
	move.w	(trap12_jmp_label+4),($FFFD18+4+72)
	move.l	(trap13_jmp_label),($FFFD18+78)
	move.w	(trap13_jmp_label+4),($FFFD18+4+78)
	move.l	(trap14_jmp_label),($FFFD18+84)
	move.w	(trap14_jmp_label+4),($FFFD18+4+84)
	move.l	(trap15_jmp_label),($FFFD18+90)
	move.w	(trap15_jmp_label+4),($FFFD18+4+90)
	rts
	endif

	if	def(SegaCD)		;	Sega CD Boot Header
	org	$1FFE00
	dc.b	$53,$45,$47,$41,$44,$49,$53,$43			; 0x00
	dc.b	$53,$59,$53,$54,$45,$4d,$20,$20			; 0x08
	dc.b	$42,$41,$53,$49,$45,$47,$41,$58			; 0x10
	dc.b	$4F,$52,$5A,$00,$01,$00,$00,$01			; 0x18
	dc.b	$43,$49,$4E,$4F,$53,$43,$44,$20			; 0x20
	dc.b	$20,$20,$20,$00,$00,$00,$00,$00			; 0x28
	dc.l	$800
	dc.l	$800
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00			; 0x38
	dc.l	$20000
	dc.l	$7000
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00			; 0x48
	dc.b	$31,$30,$30,$36,$31,$39,$39,$33			; 0x50
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20			; 0x58
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$53,$45,$47,$41,$20,$47,$45,$4e
	dc.b	$45,$53,$49,$53,$20,$20,$20,$20
	dc.b	$28,$43,$29,$53,$45,$47,$41,$20
	dc.b	$31,$39,$39,$33,$2e,$4f,$43,$54
	dc.b	$42,$61,$73,$69,$45,$67,$61,$58
	dc.b	$6f,$72,$7a,$20,$47,$61,$6d,$65
	dc.b	$20,$53,$65,$67,$61,$20,$43,$44
	dc.b	$20,$49,$53,$4f,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$42,$61,$73,$69,$45,$67,$61,$58
	dc.b	$6f,$72,$7a,$20,$47,$61,$6d,$65
	dc.b	$20,$53,$65,$67,$61,$20,$43,$44
	dc.b	$20,$49,$53,$4f,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$47,$4d,$20,$4d,$4b,$2d,$34,$34
	dc.b	$30,$37,$20,$2d,$30,$30,$20,$20
	dc.b	$4a,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$55,$20,$20,$20,$20,$20,$20,$20
	dc.b	$20,$20,$20,$20,$20,$20,$20,$20
	dc.b	$43,$fa,$00,$0a,$4e,$b8,$03,$64
	dc.b	$60,$00,$05,$7a,$60,$0f,$00,$00
	dc.b	$00,$00,$0c,$22,$0e,$44,$0e,$66
	dc.b	$0e,$88,$0e,$ee,$0a,$aa,$08,$88
	dc.b	$04,$44,$08,$ae,$04,$6a,$00,$0e
	dc.b	$00,$08,$00,$04,$0e,$20,$80,$3f
	dc.b	$80,$05,$18,$15,$1b,$26,$39,$36
	dc.b	$38,$45,$17,$54,$08,$64,$09,$72
	dc.b	$00,$83,$05,$16,$86,$03,$03,$16
	dc.b	$3a,$89,$03,$02,$15,$19,$26,$3b
	dc.b	$48,$f8,$58,$fa,$67,$7b,$75,$1a
	dc.b	$8a,$04,$0a,$8f,$07,$78,$17,$79
	dc.b	$37,$7a,$ff,$00,$23,$ae,$3a,$cf
	dc.b	$2f,$37,$7e,$80,$3f,$ed,$a3,$ff
	dc.b	$e0,$0e,$fd,$71,$d6,$79,$79,$db
	dc.b	$db,$f4,$00,$c2,$58,$00,$5f,$ab
	dc.b	$bb,$2b,$bf,$c2,$f8,$00,$c7,$f5
	dc.b	$b0,$fe,$9a,$85,$ff,$f0,$3f,$8d
	dc.b	$bf,$d3,$63,$a0,$bb,$e1,$81,$c3
	dc.b	$e5,$ed,$df,$ab,$bd,$bb,$bc,$78
	dc.b	$5f,$af,$c3,$45,$da,$00,$be,$61
	dc.b	$01,$bd,$7d,$61,$42,$00,$9d,$68
	dc.b	$17,$85,$0d,$10,$80,$2e,$b1,$fd
	dc.b	$f6,$00,$03,$e5,$fe,$17,$cf,$2e
	dc.b	$d5,$15,$77,$69,$ab,$bb,$4d,$5d
	dc.b	$da,$15,$00,$18,$cd,$d6,$d5,$f3
	dc.b	$cf,$9c,$79,$7e,$11,$b7,$c3,$00
	dc.b	$38,$64,$b0,$03,$97,$fd,$f6,$00
	dc.b	$02,$84,$11,$98,$c8,$00,$69,$1d
	dc.b	$88,$46,$43,$5f,$00,$1d,$84,$66
	dc.b	$50,$0b,$f9,$ef,$c1,$2c,$21,$da
	dc.b	$6a,$ee,$db,$f4,$a8,$c5,$a6,$80
	dc.b	$8a,$9b,$62,$96,$d5,$2c,$59,$6d
	dc.b	$58,$c5,$45,$59,$70,$ac,$0a,$17
	dc.b	$ff,$c6,$13,$67,$14,$59,$df,$c7
	dc.b	$e1,$58,$00,$17,$7f,$ef,$01,$a2
	dc.b	$5a,$96,$b8,$7e,$55,$d4,$6b,$7f
	dc.b	$40,$01,$f5,$bd,$42,$10,$4a,$25
	dc.b	$02,$09,$44,$eb,$40,$17,$ec,$f8
	dc.b	$e7,$e0,$00,$58,$44,$a9,$40,$10
	dc.b	$8c,$66,$10,$04,$a7,$fa,$f4,$43
	dc.b	$b4,$d1,$77,$69,$a3,$95,$e5,$72
	dc.b	$a2,$a6,$c8,$bd,$4d,$91,$7a,$9a
	dc.b	$40,$05,$ff,$35,$1f,$93,$ba,$eb
	dc.b	$f5,$e6,$ec,$e2,$82,$ce,$28,$e3
	dc.b	$d6,$3a,$f1,$fb,$6e,$56,$3d,$80
	dc.b	$0a,$75,$a0,$00,$cc,$64,$02,$ea
	dc.b	$16,$15,$f2,$00,$22,$54,$20,$02
	dc.b	$50,$b8,$d7,$3a,$00,$21,$0a,$32
	dc.b	$02,$14,$66,$3b,$bf,$60,$04,$20
	dc.b	$00,$21,$35,$53,$57,$76,$9a,$22
	dc.b	$d3,$44,$2e,$51,$15,$36,$45,$ea
	dc.b	$6c,$88,$b6,$6b,$8b,$6e,$dc,$7e
	dc.b	$e4,$00,$8a,$5e,$78,$a2,$ce,$28
	dc.b	$2c,$e2,$8c,$7b,$df,$b0,$03,$1e
	dc.b	$cd,$00,$1a,$8d,$00,$1b,$d6,$89
	dc.b	$44,$27,$74,$f8,$cb,$df,$7d,$71
	dc.b	$9f,$e7,$80,$d4,$b5,$2f,$31,$d7
	dc.b	$6f,$d7,$66,$11,$2d,$4b,$52,$f3
	dc.b	$1d,$67,$0f,$d7,$ee,$40,$08,$d6
	dc.b	$80,$03,$4a,$d8,$f8,$c5,$97,$08
	dc.b	$ab,$d6,$2c,$b8,$b1,$0b,$9f,$dc
	dc.b	$f3,$ee,$d3,$40,$06,$b1,$fe,$60
	dc.b	$b3,$8a,$2c,$e2,$a3,$30,$80,$14
	dc.b	$80,$03,$01,$14,$04,$25,$0e,$35
	dc.b	$18,$45,$13,$56,$39,$67,$79,$75
	dc.b	$14,$81,$03,$00,$16,$2f,$27,$78
	dc.b	$82,$04,$05,$16,$36,$83,$05,$0c
	dc.b	$16,$37,$28,$f9,$84,$05,$15,$17
	dc.b	$7b,$85,$05,$16,$86,$05,$0d,$16
	dc.b	$3a,$87,$05,$0f,$17,$76,$88,$05
	dc.b	$10,$18,$fa,$89,$06,$2e,$8a,$06
	dc.b	$32,$18,$f8,$8b,$05,$11,$17,$7a
	dc.b	$8c,$06,$38,$8d,$05,$1a,$8e,$06
	dc.b	$33,$17,$77,$8f,$05,$12,$ff,$a5
	dc.b	$28,$9b,$26,$f8,$5a,$f6,$70,$6a
	dc.b	$07,$3c,$de,$e0,$6f,$21,$19,$5b
	dc.b	$56,$d5,$77,$fc,$ff,$ee,$ef,$cf
	dc.b	$a0,$49,$7f,$04,$c8,$60,$10,$ef
	dc.b	$07,$bd,$ff,$d4,$f5,$4f,$99,$f8
	dc.b	$df,$9b,$d9,$3d,$98,$ed,$a6,$37
	dc.b	$9e,$e6,$d5,$b5,$6c,$58,$59,$fb
	dc.b	$14,$6a,$3c,$8c,$55,$18,$43,$10
	dc.b	$79,$dd,$cc,$e5,$7b,$9b,$b4,$6b
	dc.b	$6c,$f5,$6e,$21,$61,$f2,$1e,$8c
	dc.b	$73,$1b,$8e,$d4,$a3,$84,$db,$8e
	dc.b	$cc,$20,$fe,$d5,$ac,$d8,$f8,$3b
	dc.b	$ce,$f4,$13,$0a,$9b,$65,$5d,$93
	dc.b	$6c,$f9,$a9,$cd,$d6,$af,$78,$22
	dc.b	$bf,$5f,$99,$1f,$09,$1c,$bd,$4d
	dc.b	$a3,$9d,$35,$f7,$e3,$f3,$bd,$3b
	dc.b	$d6,$9a,$f4,$89,$e8,$38,$1d,$3b
	dc.b	$ae,$06,$47,$eb,$f5,$95,$9f,$bb
	dc.b	$0b,$8b,$07,$e5,$7f,$33,$f3,$d4
	dc.b	$12,$2c,$b7,$68,$78,$83,$50,$41
	dc.b	$ed,$f3,$98,$8e,$b8,$5d,$28,$8e
	dc.b	$5e,$26,$0e,$72,$05,$4e,$64,$67
	dc.b	$67,$4b,$bd,$b0,$25,$97,$ec,$8e
	dc.b	$ac,$b3,$bf,$98,$af,$c7,$af,$84
	dc.b	$cb,$86,$10,$8f,$39,$85,$20,$88
	dc.b	$77,$0a,$d2,$dc,$dd,$74,$91,$a2
	dc.b	$50,$2a,$e7,$c0,$c2,$ec,$69,$f9
	dc.b	$c7,$f5,$51,$ba,$d1,$bb,$0f,$32
	dc.b	$89,$be,$00,$b0,$be,$17,$03,$b9
	dc.b	$90,$46,$04,$e9,$77,$3b,$04,$63
	dc.b	$95,$2e,$31,$73,$db,$15,$b9,$ca
	dc.b	$8b,$3f,$d1,$52,$94,$a5,$3c,$fe
	dc.b	$75,$ed,$5c,$0c,$86,$5c,$c9,$ae
	dc.b	$d0,$b3,$66,$c6,$b2,$09,$f9,$dd
	dc.b	$86,$49,$7f,$3f,$dc,$5f,$77,$63
	dc.b	$2b,$cc,$1a,$92,$d7,$03,$bb,$d3
	dc.b	$f3,$f9,$89,$5d,$a1,$97,$af,$86
	dc.b	$11,$1f,$e8,$c4,$65,$81,$c3,$db
	dc.b	$fb,$cd,$52,$94,$a6,$a9,$4f,$3a
	dc.b	$f3,$5f,$d2,$fb,$dc,$7c,$87,$a3
	dc.b	$1c,$c6,$e3,$b5,$1c,$29,$36,$e3
	dc.b	$b3,$08,$3f,$b5,$6b,$36,$3e,$0e
	dc.b	$f3,$bd,$04,$c2,$d6,$df,$d2,$4d
	dc.b	$a7,$24,$5a,$63,$3a,$fd,$45,$5b
	dc.b	$fa,$ca,$a6,$49,$ee,$c2,$e9,$5f
	dc.b	$f9,$5f,$cc,$b2,$43,$a8,$24,$6e
	dc.b	$1d,$a1,$79,$c9,$a8,$20,$f0,$5f
	dc.b	$36,$9e,$17,$88,$5c,$d9,$47,$24
	dc.b	$ac,$c4,$b0,$a7,$b8,$9e,$61,$eb
	dc.b	$8c,$e4,$9f,$a2,$22,$3c,$d7,$cf
	dc.b	$d6,$51,$aa,$e9,$70,$f3,$25,$37
	dc.b	$c0,$16,$17,$c2,$e0,$77,$32,$08
	dc.b	$c0,$9d,$2e,$e7,$60,$8c,$72,$a5
	dc.b	$c6,$2e,$00,$00,$06,$03,$00,$00
	dc.b	$00,$01,$05,$10,$12,$c2,$04,$88
	dc.b	$50,$11,$04,$80,$14,$05,$10,$9a
	dc.b	$21,$54,$42,$60,$05,$05,$01,$34
	dc.b	$38,$11,$40,$51,$11,$81,$14,$04
	dc.b	$01,$10,$20,$c5,$05,$01,$5c,$58
	dc.b	$02,$20,$40,$1f,$02,$00,$88,$10
	dc.b	$07,$c3,$80,$c8,$04,$01,$a1,$38
	dc.b	$b4,$94,$01,$00,$ac,$58,$0e,$20
	dc.b	$40,$1a,$13,$93,$b1,$a9,$a8,$1c
	dc.b	$5d,$8d,$42,$e2,$68,$6c,$0f,$f0
	dc.b	$05,$00,$00,$00,$00,$00,$39,$0a
	dc.b	$41,$06,$01,$81,$80,$90,$29,$af
	dc.b	$f8,$50,$52,$4f,$44,$55,$43,$45
	dc.b	$44,$20,$42,$59,$20,$4f,$52,$20
	dc.b	$55,$4e,$44,$45,$52,$20,$4c,$49
	dc.b	$43,$45,$4e,$53,$45,$00,$00,$20
	dc.b	$46,$52,$4f,$4d,$20,$53,$45,$47
	dc.b	$41,$20,$45,$4e,$54,$45,$52,$50
	dc.b	$52,$49,$53,$45,$53,$2c,$20,$4c
	dc.b	$54,$44,$2e,$ff,$14,$11,$08,$0d
	dc.b	$17,$11,$00,$0d,$14,$11,$14,$11
	dc.b	$90,$00,$00,$00,$00,$1c,$14,$11
	dc.b	$80,$80,$80,$1d,$11,$00,$00,$00
	dc.b	$00,$ae,$11,$07,$00,$01,$00,$ad
	dc.b	$11,$00,$00,$3f,$11,$00,$0b,$48
	dc.b	$11,$00,$0b,$66,$11,$00,$0b,$84
	dc.b	$11,$00,$0b,$93,$11,$00,$0b,$a2
	dc.b	$11,$00,$0b,$ef,$01,$e0,$80,$80
	dc.b	$0a,$c9,$0c,$f2,$80,$05,$80,$39
	dc.b	$80,$05,$80,$0c,$ef,$00,$f0,$0a
	dc.b	$01,$02,$08,$a9,$1f,$80,$01,$a5
	dc.b	$40,$e7,$a5,$02,$f7,$00,$08,$5d
	dc.b	$11,$f2,$80,$05,$80,$39,$80,$05
	dc.b	$80,$0c,$ef,$00,$f0,$0a,$01,$02
	dc.b	$08,$b8,$1f,$80,$01,$b5,$40,$e7
	dc.b	$b5,$02,$f7,$00,$08,$7b,$11,$f2
	dc.b	$ef,$01,$e0,$80,$80,$05,$c4,$0c
	dc.b	$e0,$40,$80,$05,$c4,$39,$f2,$ef
	dc.b	$01,$e0,$80,$80,$0a,$80,$07,$e0
	dc.b	$40,$80,$0a,$c9,$34,$f2,$ef,$01
	dc.b	$e0,$80,$c1,$12,$e0,$40,$c1,$3f
	dc.b	$f2,$f2,$32,$01,$01,$01,$01,$3f
	dc.b	$1f,$1f,$1f,$19,$06,$04,$07,$08
	dc.b	$05,$05,$04,$19,$19,$19,$19,$11
	dc.b	$89,$18,$87,$04,$37,$72,$77,$49
	dc.b	$1f,$1f,$1f,$1f,$07,$0a,$07,$0d
	dc.b	$00,$0b,$00,$0b,$1f,$0f,$1f,$0f
	dc.b	$23,$80,$23,$80
	move    #$2700,sr
	move.l	#$1000000,a7

	move.l	#$8F02,($C00004)
	move.l	#$491E0003,($C00004)
	move.w	#'L',($C00000)
	move.w	#'o',($C00000)
	move.w	#'a',($C00000)
	move.w	#'d',($C00000)
	move.w	#'i',($C00000)
	move.w	#'n',($C00000)
	move.w	#'g',($C00000)
	move.w	#'.',($C00000)
	move.w	#'.',($C00000)
	move.w	#'!',($C00000)

	bset	#1, $A12003
	move.b	#'R',(MCOMMW)
init_sync_1:
	cmp.b	#'G',(MCOMMR)
	bne	init_sync_1
	move.b	#'Q',(MCOMMW)
init_sync_2:
	cmp.b	#'Q',(MCOMMR)
	bne	init_sync_2
	jmp	$200000

_master_end_segacd:
	ds.b	131072-(_master_end_segacd-$1FFE00)

;	org	$6000
_start_slave_cd:
	dc.b	'MAINPOOPER  ',0,0,0,0
	dc.l	0
	dc.l	131072
	dc.l	$20
	dc.l	0

	andi.b	#$FA, $FF8003		;set 2Mbit mode
	move.b	#'W',(SCOMMW)
	move.b	#'B',(SCOMMW+1)
cd_sync_1:
	cmp.b	#'R',(SCOMMR)
	bne	cd_sync_1
	move.b	#'G',(SCOMMW)
cd_sync_2:
	cmp.b	#'Q',(SCOMMR)
	bne	cd_sync_2
	move.l	#128,d0
	move.l	#128,d1			; @ 2m = #128
	move.l	#$80000,a0		; @ 2m = #$80000
	bsr	readcd
	bset	#0, $FF8003
	move.b	#'Q',(SCOMMW)

command_wait:
	cmp.b	#'P',(SCOMMR)
	bne	command_wait
	cmp.w	#'CA',(SCOMMR+2)
	beq	command_a
	cmp.w	#'CB',(SCOMMR+2)
	beq	command_b
	cmp.w	#'CC',(SCOMMR+2)
	beq	command_c
	cmp.w	#'CD',(SCOMMR+2)
	beq	command_d
	move.b	#'Q',(SCOMMW)
	bra	command_wait

; Play a CD track once
command_a:
	lea	track(PC),a0
	move.w	(SCOMMR+4),(a0)
	move.b	#'P',(SCOMMW)
	move.l	#$12,d0
	jsr	($5F22)
	bra	wait_main

; Play a CD track continuously
command_b:
	lea	track(PC),a0
	move.w	(SCOMMR+4),(a0)
	move.b	#'P',(SCOMMW)
	move.l	#$13,d0
	jsr	($5F22)
	bra	wait_main

; Stop CD play
command_c:
	move.b	#'P',(SCOMMW)
	move.l	#$10,d0
	jsr	($5F22)
	bra	wait_main

; Load a Sega CD Executable into word RAM
command_d:
	move.l	#$10,d0
	jsr	($5F22)
	move.l	(SCOMMR+4),d0
	lsl.l	#7,d0
	move.l	#128,d1			; @ 2m = #128
	move.l	#$80000,a0		; @ 2m = #$80000
	bsr	readcd
	bset	#0, $FF8003
	move.b	#'P',(SCOMMW)
	bra	wait_main

; Wait Main Acknowledge
wait_main:
	cmp.b	#'Q',(SCOMMR)
	bne	wait_main
	move.b	#'Q',(SCOMMW)
	bra	command_wait

; d0=block, d1=number of blocks, a0=place to copy to
; 1 block = 2048 bytes
readcd:
	lea	Packet(pc),a5
	move.l	d0,(a5)
	move.l	d1,4(a5)
	move.l	a0,8(a5)
	movea.l	a5, a0
	move.w	#$89, d0		;init CD controller
	jsr 	$5f22
	move.w	#$20, d0		;start read operation
	jsr	$5f22
readcd_Check:
	move.w	#$8A, d0		;check for data
	jsr	$5f22
	bcs	readcd_Check
readcd_Check2:
	move.w	#$8B, d0
	jsr	$5f22
	bcc	readcd_Check2
readcd_Check3:
	move.w	#$8C, d0
	movea.l	8(a5), a0
	lea	$C(a5), a1
	jsr	$5f22
	bcc	readcd_Check3
	move.w	#$8D, d0
	jsr	$5f22
	addi.l	#$800, 8(a5)
	addq.l	#1, (a5)
	subq.l	#1, 4(a5)
	bne	readcd_Check
	rts

; d0=block, d1=number of blocks, a0=place to copy to
;readcd:
;	lea	Packet(pc),a5
;	move.l	d0,(a5)
;	move.l	d1,4(a5)
;	move.w	#$89,d0
;	jsr 	$5f22
;	move.w	#$20,d0
;	jsr	$5f22
;readcd_Check:
;	move.w	#$8A,d0
;	jsr	$5f22
;	bcs	readcd_Check
;readcd_Check2:
;	move.w	#$8B,d0
;	jsr	$5f22
;	bcc	readcd_Check2
;readcd_Check3:
;	move.w	#$8C,d0
;	movea.l	(a7)+,a0
;	lea	endfile(pc),a1
;	jsr	$5f22
;	bcc	readcd_Check3
;	move.w	#$8D,d0
;	jsr	$5f22
;	addi.l	#$800,8(a5)
;	addq.l	#1,(a5)
;	subq.l	#1,4(a5)
;	bne	readcd_Check
;	rts

track:	ds.l	1
Packet:	dc.l	0,0,0,endfile,0
endfile:

_slave_end_segacd:
	ds.b	131072-(_slave_end_segacd-_start_slave_cd)
;	addq	#321654326,d0
	org	$200000
__initialize_reset_start
	bra.s	__initialize_reset
	bra	sega_cd_prog_jump_in

sega_cd_prog_jump_in:
	bsr	reset_reset_vectors
	move.w	#$2700,sr
	move.l	#$1000000,a7
	bra	segacd_jump_in_program

__initialize_reset:
	move    #$2700,sr
	move.l	#$1000000,a7
	move.b	#'Q',(MCOMMW)
	move.l	(vblank_jmp_label),($FFFD06)
	move.w	(vblank_jmp_label+4),($FFFD06+4)
	move.l	(hblank_jmp_label),($FFFD0C)
	move.w	(hblank_jmp_label+4),($FFFD0C+4)
	move.l	(addresserror_or_illegalinstr_jmp_label),($FFFD7E)
	move.w	(addresserror_or_illegalinstr_jmp_label+4),($FFFD7E+4)
	move.l	(dividezero_jmp_label),($FFFD84)
	move.w	(dividezero_jmp_label+4),($FFFD84+4)
	move.l	(chk_jmp_label),($FFFD78)
	move.w	(chk_jmp_label+4),($FFFD78+4)
	move.l	(trapv_jmp_label),($FFFD8A)
	move.w	(trapv_jmp_label+4),($FFFD8A+4)
	move.l	(privilegeviolation_jmp_label),($FFFD9C)
	move.w	(privilegeviolation_jmp_label+4),($FFFD9C+4)
	move.l	(trace_jmp_label),($FFFDA2)
	move.w	(trace_jmp_label+4),($FFFDA2+4)
	move.l	(line1010_jmp_label),($FFFD90)
	move.w	(line1010_jmp_label+4),($FFFD90+4)
	move.l	(line1111_jmp_label),($FFFD96)
	move.w	(line1111_jmp_label+4),($FFFD96+4)
	move.l	(trap0_jmp_label),($FFFD18+0)
	move.w	(trap0_jmp_label+4),($FFFD18+4+0)
	move.l	(trap1_jmp_label),($FFFD18+6)
	move.w	(trap1_jmp_label+4),($FFFD18+4+6)
	move.l	(trap2_jmp_label),($FFFD18+12)
	move.w	(trap2_jmp_label+4),($FFFD18+4+12)
	move.l	(trap3_jmp_label),($FFFD18+18)
	move.w	(trap3_jmp_label+4),($FFFD18+4+18)
	move.l	(trap4_jmp_label),($FFFD18+24)
	move.w	(trap4_jmp_label+4),($FFFD18+4+24)
	move.l	(trap5_jmp_label),($FFFD18+30)
	move.w	(trap5_jmp_label+4),($FFFD18+4+30)
	move.l	(trap6_jmp_label),($FFFD18+36)
	move.w	(trap6_jmp_label+4),($FFFD18+4+36)
	move.l	(trap7_jmp_label),($FFFD18+42)
	move.w	(trap7_jmp_label+4),($FFFD18+4+42)
	move.l	(trap8_jmp_label),($FFFD18+48)
	move.w	(trap8_jmp_label+4),($FFFD18+4+48)
	move.l	(trap9_jmp_label),($FFFD18+54)
	move.w	(trap9_jmp_label+4),($FFFD18+4+54)
	move.l	(trap10_jmp_label),($FFFD18+60)
	move.w	(trap10_jmp_label+4),($FFFD18+4+60)
	move.l	(trap11_jmp_label),($FFFD18+66)
	move.w	(trap11_jmp_label+4),($FFFD18+4+66)
	move.l	(trap12_jmp_label),($FFFD18+72)
	move.w	(trap12_jmp_label+4),($FFFD18+4+72)
	move.l	(trap13_jmp_label),($FFFD18+78)
	move.w	(trap13_jmp_label+4),($FFFD18+4+78)
	move.l	(trap14_jmp_label),($FFFD18+84)
	move.w	(trap14_jmp_label+4),($FFFD18+4+84)
	move.l	(trap15_jmp_label),($FFFD18+90)
	move.w	(trap15_jmp_label+4),($FFFD18+4+90)
	jmp	__init_start

vblank_jmp_label:		jmp	__vblank
hblank_jmp_label:		jmp	__hblank
addresserror_or_illegalinstr_jmp_label:
				jmp	vector_addresserror_or_illegalinstr
dividezero_jmp_label:		jmp	vector_dividezero
chk_jmp_label:			jmp	vector_chk
trapv_jmp_label:		jmp	vector_trapv
privilegeviolation_jmp_label:	jmp	vector_privilegeviolation
trace_jmp_label:		jmp	vector_trace
line1010_jmp_label:		jmp	vector_line1010
line1111_jmp_label:		jmp	vector_line1111
trap0_jmp_label:		jmp	braekpoin
trap1_jmp_label:		jmp	vector_trap1
trap2_jmp_label:		jmp	vector_trap2
trap3_jmp_label:		jmp	vector_trap3
trap4_jmp_label:		jmp	vector_trap4
trap5_jmp_label:		jmp	vector_trap5
trap6_jmp_label:		jmp	vector_trap6
trap7_jmp_label:		jmp	vector_trap7
trap8_jmp_label:		jmp	vector_trap8
trap9_jmp_label:		jmp	vector_trap9
trap10_jmp_label:		jmp	vector_trap10
trap11_jmp_label:		jmp	vector_trap11
trap12_jmp_label:		jmp	vector_trap12
trap13_jmp_label:		jmp	vector_trap13
trap14_jmp_label:		jmp	vector_trap14
trap15_jmp_label:		jmp	vector_trap15

reset_reset_vectors:
	move.l	(vblank_jmp_label),($FFFD06)
	move.w	(vblank_jmp_label+4),($FFFD06+4)
	move.l	(hblank_jmp_label),($FFFD0C)
	move.w	(hblank_jmp_label+4),($FFFD0C+4)
	move.l	(addresserror_or_illegalinstr_jmp_label),($FFFD7E)
	move.w	(addresserror_or_illegalinstr_jmp_label+4),($FFFD7E+4)
	move.l	(dividezero_jmp_label),($FFFD84)
	move.w	(dividezero_jmp_label+4),($FFFD84+4)
	move.l	(chk_jmp_label),($FFFD78)
	move.w	(chk_jmp_label+4),($FFFD78+4)
	move.l	(trapv_jmp_label),($FFFD8A)
	move.w	(trapv_jmp_label+4),($FFFD8A+4)
	move.l	(privilegeviolation_jmp_label),($FFFD9C)
	move.w	(privilegeviolation_jmp_label+4),($FFFD9C+4)
	move.l	(trace_jmp_label),($FFFDA2)
	move.w	(trace_jmp_label+4),($FFFDA2+4)
	move.l	(line1010_jmp_label),($FFFD90)
	move.w	(line1010_jmp_label+4),($FFFD90+4)
	move.l	(line1111_jmp_label),($FFFD96)
	move.w	(line1111_jmp_label+4),($FFFD96+4)
	move.l	(trap0_jmp_label),($FFFD18+0)
	move.w	(trap0_jmp_label+4),($FFFD18+4+0)
	move.l	(trap1_jmp_label),($FFFD18+6)
	move.w	(trap1_jmp_label+4),($FFFD18+4+6)
	move.l	(trap2_jmp_label),($FFFD18+12)
	move.w	(trap2_jmp_label+4),($FFFD18+4+12)
	move.l	(trap3_jmp_label),($FFFD18+18)
	move.w	(trap3_jmp_label+4),($FFFD18+4+18)
	move.l	(trap4_jmp_label),($FFFD18+24)
	move.w	(trap4_jmp_label+4),($FFFD18+4+24)
	move.l	(trap5_jmp_label),($FFFD18+30)
	move.w	(trap5_jmp_label+4),($FFFD18+4+30)
	move.l	(trap6_jmp_label),($FFFD18+36)
	move.w	(trap6_jmp_label+4),($FFFD18+4+36)
	move.l	(trap7_jmp_label),($FFFD18+42)
	move.w	(trap7_jmp_label+4),($FFFD18+4+42)
	move.l	(trap8_jmp_label),($FFFD18+48)
	move.w	(trap8_jmp_label+4),($FFFD18+4+48)
	move.l	(trap9_jmp_label),($FFFD18+54)
	move.w	(trap9_jmp_label+4),($FFFD18+4+54)
	move.l	(trap10_jmp_label),($FFFD18+60)
	move.w	(trap10_jmp_label+4),($FFFD18+4+60)
	move.l	(trap11_jmp_label),($FFFD18+66)
	move.w	(trap11_jmp_label+4),($FFFD18+4+66)
	move.l	(trap12_jmp_label),($FFFD18+72)
	move.w	(trap12_jmp_label+4),($FFFD18+4+72)
	move.l	(trap13_jmp_label),($FFFD18+78)
	move.w	(trap13_jmp_label+4),($FFFD18+4+78)
	move.l	(trap14_jmp_label),($FFFD18+84)
	move.w	(trap14_jmp_label+4),($FFFD18+4+84)
	move.l	(trap15_jmp_label),($FFFD18+90)
	move.w	(trap15_jmp_label+4),($FFFD18+4+90)
	rts

	endif

	; Routine Start
VBLANK_JUMP	EQU	$FF0006
rnd_seed	EQU	$FF000A
HBLANK_JUMP	EQU	$FF0012
VDP_MODE1	EQU	$FF0016
VDP_MODE2	EQU	$FF0017
VDP_MODE3	EQU	$FF0018
VDP_MODE4	EQU	$FF0019
TEXT_PLANE	EQU	$FF001C
GFX_PLANE	EQU	$FF0020
SCROLL_PLANE	EQU	$FF0024

__init_start:
* Stop Z80 reset and get bus
	move.w	#$0100,$A11200
	move.w	#$0100,$A11100
__bootbios_get_bus:
	btst	#8,$A11100
	bne.s	__bootbios_get_bus
* Clear RAM
	lea	$FF0000,a1
	move.l	#$37FF,d0
__bootbios_clear_ram:
	clr.l	(a1)+
	dbra	d0,__bootbios_clear_ram
* Clear Z80 RAM
	lea	$A00000,a1
	lea	_z80_driver,a2
	move.l	#$0F,d0
__bootbios_clear_zram:
	move.b	(a2)+,(a1)+
	dbra	d0,__bootbios_clear_zram
* Clear address and data registers
	movem.l $FF0000,d0-d7/a0-a6
	move.l	($000000),a7
	jsr	_vdp_init
	jmp	__proggy_start

__vblank:
	tst.l	(VBLANK_JUMP)
	bne	__vblank_do
	rte
__vblank_do:
	movem.l	d0-d7/a0-a6,-(a7)
	add.l	#2048,a5
	clr.b	(a5)
	move.w	(CURSORX),-(a7)
	move.l	(VBLANK_JUMP),a0
	jsr	(a0)
	move.w	(a7)+,(CURSORX)
	movem.l	(a7)+,d0-d7/a0-a6
	rte

__hblank:
	tst.l	(HBLANK_JUMP)
	bne	__hblank_do
	rte
__hblank_do:
	movem.l	d0-d7/a0-a6,-(a7)
	move.l	(HBLANK_JUMP),a0
	jsr	(a0)
	movem.l	(a7)+,d0-d7/a0-a6
	rte

_z80_driver:
	dc.b	$F3,$ED,$56,$31,$00,$20,$C3,$80,$00,$00,$00,$00,$00,$00,$00,$00
	EVEN

_vdp_init:
	lea	$C00000,a0
	lea	$C00004,a1
	lea	$C00008,a2
_l0:
	move.b	(a2),d0
	cmpi.b	#$00,d0
	bne.s	_l0
_l1:
	move.b	(a2),d0
	cmpi.b	#$FF,d0
	bne.s	_l1
	move.w  (a1),d0
	move.w	#$8004,(a1)
	move.w	#$8104,(a1)
	move.w	#$8210,(a1)
	move.w	#$8314,(a1)
	move.w	#$8403,(a1)
	move.w	#$853C,(a1)
	move.w	#$8600,(a1)
	move.w	#$8700,(a1)
	move.w	#$8800,(a1)
	move.w	#$8900,(a1)
	move.w	#$8AFF,(a1)
	move.w	#$8B00,(a1)
	move.w	#$8C81,(a1)
	move.w	#$8D1C,(a1)
	move.w	#$8E00,(a1)
	move.w	#$8F02,(a1)
	move.w	#$9001,(a1)
	move.w	#$9100,(a1)
	move.w	#$9200,(a1)
	move.w	#$9300,(a1)
	move.w	#$9400,(a1)
	move.w	#$9500,(a1)
	move.w	#$9600,(a1)
	move.l	#$40000000,(a1)
	move.l	#$00003FFF,d0
clear_vram:
	move.l	#$00000000,(a0)
	dbra	d0,clear_vram
	move.l	#$C0000000,(a1)
	move.l	#$0000001F,d0
clear_cram:
	move.l	#$00000000,(a0)
	dbra	d0,clear_cram
	move.l	#$40000010,(a1)
	move.l	#$0000001F,d0
clear_vsram:
	move.l	#$00000000,(a0)
	dbra	d0,clear_vsram
	rts

color_chart:
	dc.w	$0000,$0777,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000

	dc.w	$0000,$0770,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000

	dc.w	$0000,$0070,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000

	dc.w	$0000,$0707,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000

font:
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00
	dc.b	$7e,$81,$a5,$81,$bd,$99,$81,$7e
	dc.b	$7e,$ff,$db,$ff,$c3,$e7,$ff,$7e
	dc.b	$6c,$fe,$fe,$fe,$7c,$38,$10,$00
	dc.b	$10,$38,$7c,$fe,$7c,$38,$10,$00
	dc.b	$38,$7c,$38,$fe,$fe,$7c,$38,$7c
	dc.b	$10,$10,$38,$7c,$fe,$7c,$38,$7c
	dc.b	$00,$00,$18,$3c,$3c,$18,$00,$00
	dc.b	$ff,$ff,$e7,$c3,$c3,$e7,$ff,$ff
	dc.b	$00,$3c,$66,$42,$42,$66,$3c,$00
	dc.b	$ff,$c3,$99,$bd,$bd,$99,$c3,$ff
	dc.b	$0f,$07,$0f,$7d,$cc,$cc,$cc,$78
	dc.b	$3c,$66,$66,$66,$3c,$18,$7e,$18
	dc.b	$3f,$33,$3f,$30,$30,$70,$f0,$e0
	dc.b	$7f,$63,$7f,$63,$63,$67,$e6,$c0
	dc.b	$99,$5a,$3c,$e7,$e7,$3c,$5a,$99
	dc.b	$80,$e0,$f8,$fe,$f8,$e0,$80,$00
	dc.b	$02,$0e,$3e,$fe,$3e,$0e,$02,$00
	dc.b	$18,$3c,$7e,$18,$18,$7e,$3c,$18
	dc.b	$66,$66,$66,$66,$66,$00,$66,$00
	dc.b	$7f,$db,$db,$7b,$1b,$1b,$1b,$00
	dc.b	$3e,$63,$38,$6c,$6c,$38,$cc,$78
	dc.b	$00,$00,$00,$00,$7e,$7e,$7e,$00
	dc.b	$18,$3c,$7e,$18,$7e,$3c,$18,$ff
	dc.b	$18,$3c,$7e,$18,$18,$18,$18,$00
	dc.b	$18,$18,$18,$18,$7e,$3c,$18,$00
	dc.b	$00,$18,$0c,$fe,$0c,$18,$00,$00
	dc.b	$00,$30,$60,$fe,$60,$30,$00,$00
	dc.b	$00,$00,$c0,$c0,$c0,$fe,$00,$00
	dc.b	$00,$24,$66,$ff,$66,$24,$00,$00
	dc.b	$00,$18,$3c,$7e,$ff,$ff,$00,$00
	dc.b	$00,$ff,$ff,$7e,$3c,$18,$00,$00
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00
	dc.b	$30,$30,$30,$30,$30,$00,$30,$00
	dc.b	$6c,$6c,$6c,$00,$00,$00,$00,$00
	dc.b	$6c,$6c,$fe,$6c,$fe,$6c,$6c,$00
	dc.b	$30,$7c,$c0,$78,$0c,$f8,$30,$00
	dc.b	$00,$c6,$cc,$18,$30,$66,$c6,$00
	dc.b	$38,$6c,$38,$76,$dc,$cc,$76,$00
	dc.b	$60,$60,$c0,$00,$00,$00,$00,$00
	dc.b	$18,$30,$60,$60,$60,$30,$18,$00
	dc.b	$60,$30,$18,$18,$18,$30,$60,$00
	dc.b	$00,$66,$3c,$ff,$3c,$66,$00,$00
	dc.b	$00,$30,$30,$fc,$30,$30,$00,$00
	dc.b	$00,$00,$00,$00,$00,$30,$30,$60
	dc.b	$00,$00,$00,$fc,$00,$00,$00,$00
	dc.b	$00,$00,$00,$00,$00,$30,$30,$00
	dc.b	$06,$0c,$18,$30,$60,$c0,$80,$00
	dc.b	$7c,$c6,$ce,$de,$f6,$e6,$7c,$00
	dc.b	$30,$70,$30,$30,$30,$30,$fc,$00
	dc.b	$78,$cc,$0c,$38,$60,$cc,$fc,$00
	dc.b	$78,$cc,$0c,$38,$0c,$cc,$78,$00
	dc.b	$1c,$3c,$6c,$cc,$fe,$0c,$1e,$00
	dc.b	$fc,$c0,$f8,$0c,$0c,$cc,$78,$00
	dc.b	$38,$60,$c0,$f8,$cc,$cc,$78,$00
	dc.b	$fc,$cc,$0c,$18,$30,$30,$30,$00
	dc.b	$78,$cc,$cc,$78,$cc,$cc,$78,$00
	dc.b	$78,$cc,$cc,$7c,$0c,$18,$70,$00
	dc.b	$00,$30,$30,$00,$00,$30,$30,$00
	dc.b	$00,$30,$30,$00,$00,$30,$30,$60
	dc.b	$18,$30,$60,$c0,$60,$30,$18,$00
	dc.b	$00,$00,$fc,$00,$00,$fc,$00,$00
	dc.b	$60,$30,$18,$0c,$18,$30,$60,$00
	dc.b	$78,$cc,$0c,$18,$30,$00,$30,$00
	dc.b	$7c,$c6,$de,$de,$de,$c0,$78,$00
	dc.b	$30,$78,$cc,$cc,$fc,$cc,$cc,$00
	dc.b	$fc,$66,$66,$7c,$66,$66,$fc,$00
	dc.b	$3c,$66,$c0,$c0,$c0,$66,$3c,$00
	dc.b	$f8,$6c,$66,$66,$66,$6c,$f8,$00
	dc.b	$7e,$60,$60,$78,$60,$60,$7e,$00
	dc.b	$7e,$60,$60,$78,$60,$60,$60,$00
	dc.b	$3c,$66,$c0,$c0,$ce,$66,$3e,$00
	dc.b	$cc,$cc,$cc,$fc,$cc,$cc,$cc,$00
	dc.b	$78,$30,$30,$30,$30,$30,$78,$00
	dc.b	$1e,$0c,$0c,$0c,$cc,$cc,$78,$00
	dc.b	$e6,$66,$6c,$78,$6c,$66,$e6,$00
	dc.b	$60,$60,$60,$60,$60,$60,$7e,$00
	dc.b	$c6,$ee,$fe,$fe,$d6,$c6,$c6,$00
	dc.b	$c6,$e6,$f6,$de,$ce,$c6,$c6,$00
	dc.b	$38,$6c,$c6,$c6,$c6,$6c,$38,$00
	dc.b	$fc,$66,$66,$7c,$60,$60,$f0,$00
	dc.b	$78,$cc,$cc,$cc,$dc,$78,$1c,$00
	dc.b	$fc,$66,$66,$7c,$6c,$66,$e6,$00
	dc.b	$78,$cc,$e0,$78,$1c,$cc,$78,$00
	dc.b	$fc,$30,$30,$30,$30,$30,$30,$00
	dc.b	$cc,$cc,$cc,$cc,$cc,$cc,$fc,$00
	dc.b	$cc,$cc,$cc,$cc,$cc,$78,$30,$00
	dc.b	$c6,$c6,$c6,$d6,$fe,$ee,$c6,$00
	dc.b	$c6,$c6,$6c,$38,$38,$6c,$c6,$00
	dc.b	$cc,$cc,$cc,$78,$30,$30,$78,$00
	dc.b	$fe,$06,$0c,$18,$30,$60,$fe,$00
	dc.b	$78,$60,$60,$60,$60,$60,$78,$00
	dc.b	$c0,$60,$30,$18,$0c,$06,$02,$00
	dc.b	$78,$18,$18,$18,$18,$18,$78,$00
	dc.b	$10,$38,$6c,$c6,$00,$00,$00,$00
	dc.b	$00,$00,$00,$00,$00,$00,$00,$ff
	dc.b	$30,$30,$18,$00,$00,$00,$00,$00
	dc.b	$00,$00,$78,$0c,$7c,$cc,$76,$00
	dc.b	$e0,$60,$60,$7c,$66,$66,$dc,$00
	dc.b	$00,$00,$78,$cc,$c0,$cc,$78,$00
	dc.b	$1c,$0c,$0c,$7c,$cc,$cc,$76,$00
	dc.b	$00,$00,$78,$cc,$fc,$c0,$78,$00
	dc.b	$38,$6c,$60,$f0,$60,$60,$f0,$00
	dc.b	$00,$00,$76,$cc,$cc,$7c,$0c,$f8
	dc.b	$e0,$60,$6c,$76,$66,$66,$e6,$00
	dc.b	$30,$00,$70,$30,$30,$30,$78,$00
	dc.b	$0c,$00,$0c,$0c,$0c,$cc,$cc,$78
	dc.b	$e0,$60,$66,$6c,$78,$6c,$e6,$00
	dc.b	$70,$30,$30,$30,$30,$30,$78,$00
	dc.b	$00,$00,$cc,$fe,$fe,$d6,$c6,$00
	dc.b	$00,$00,$f8,$cc,$cc,$cc,$cc,$00
	dc.b	$00,$00,$78,$cc,$cc,$cc,$78,$00
	dc.b	$00,$00,$dc,$66,$66,$7c,$60,$f0
	dc.b	$00,$00,$76,$cc,$cc,$7c,$0c,$1e
	dc.b	$00,$00,$dc,$76,$66,$60,$f0,$00
	dc.b	$00,$00,$7c,$c0,$78,$0c,$f8,$00
	dc.b	$10,$30,$7c,$30,$30,$34,$18,$00
	dc.b	$00,$00,$cc,$cc,$cc,$cc,$76,$00
	dc.b	$00,$00,$cc,$cc,$cc,$78,$30,$00
	dc.b	$00,$00,$c6,$d6,$fe,$fe,$6c,$00
	dc.b	$00,$00,$c6,$6c,$38,$6c,$c6,$00
	dc.b	$00,$00,$cc,$cc,$cc,$7c,$0c,$f8
	dc.b	$00,$00,$fc,$98,$30,$64,$fc,$00
	dc.b	$1c,$30,$30,$e0,$30,$30,$1c,$00
	dc.b	$18,$18,$18,$00,$18,$18,$18,$00
	dc.b	$e0,$30,$30,$1c,$30,$30,$e0,$00
	dc.b	$76,$dc,$00,$00,$00,$00,$00,$00
	dc.b	$00,$10,$38,$6c,$c6,$fe,$00,$00
	dc.b	$3c,$66,$c0,$c0,$66,$3c,$08,$18
	dc.b	$28,$00,$cc,$cc,$cc,$cc,$76,$00
	dc.b	$08,$10,$78,$cc,$fc,$c0,$78,$00
	dc.b	$10,$28,$78,$0c,$7c,$cc,$76,$00
	dc.b	$28,$00,$78,$0c,$7c,$cc,$76,$00
	dc.b	$20,$10,$78,$0c,$7c,$cc,$76,$00
	dc.b	$18,$18,$78,$0c,$7c,$cc,$76,$00
	dc.b	$00,$78,$cc,$c0,$cc,$78,$10,$30
	dc.b	$10,$28,$78,$cc,$fc,$c0,$78,$00
	dc.b	$28,$00,$78,$cc,$fc,$c0,$78,$00
	dc.b	$20,$10,$78,$cc,$fc,$c0,$78,$00
	dc.b	$28,$00,$70,$30,$30,$30,$78,$00
	dc.b	$10,$28,$70,$30,$30,$30,$78,$00
	dc.b	$10,$08,$70,$30,$30,$30,$78,$00
	dc.b	$28,$30,$78,$cc,$fc,$cc,$cc,$00
	dc.b	$30,$48,$30,$cc,$fc,$cc,$cc,$00
	dc.b	$08,$10,$7e,$60,$78,$60,$7e,$00
	dc.b	$00,$00,$6c,$12,$7e,$90,$7e,$00
	dc.b	$3e,$50,$90,$9c,$f0,$90,$9e,$00
	dc.b	$10,$28,$78,$cc,$cc,$cc,$78,$00
	dc.b	$28,$00,$78,$cc,$cc,$cc,$78,$00
	dc.b	$20,$10,$78,$cc,$cc,$cc,$78,$00
	dc.b	$10,$28,$cc,$cc,$cc,$cc,$76,$00
	dc.b	$20,$10,$cc,$cc,$cc,$cc,$76,$00
	dc.b	$28,$00,$cc,$cc,$cc,$7c,$0c,$f8
	dc.b	$28,$7c,$c6,$c6,$c6,$c6,$7c,$00
	dc.b	$28,$c6,$c6,$c6,$c6,$c6,$7c,$00
	dc.b	$00,$10,$78,$cc,$c0,$cc,$78,$10
	dc.b	$38,$44,$40,$f0,$40,$40,$fe,$00
	dc.b	$c3,$66,$3c,$7e,$18,$7e,$18,$00
	dc.b	$fc,$66,$66,$7c,$60,$60,$f0,$00
	dc.b	$1c,$30,$fc,$30,$30,$30,$30,$e0
	dc.b	$08,$10,$78,$0c,$7c,$cc,$76,$00
	dc.b	$10,$20,$70,$30,$30,$30,$78,$00
	dc.b	$10,$20,$78,$cc,$cc,$cc,$78,$00
	dc.b	$10,$20,$cc,$cc,$cc,$cc,$76,$00
	dc.b	$32,$4c,$f8,$cc,$cc,$cc,$cc,$00
	dc.b	$32,$4c,$c6,$e6,$d6,$ce,$c6,$00
	dc.b	$00,$38,$0c,$3c,$6c,$36,$00,$00
	dc.b	$00,$38,$44,$44,$38,$00,$00,$00
	dc.b	$18,$00,$18,$30,$60,$66,$3c,$00
	dc.b	$00,$00,$fe,$80,$80,$00,$00,$00
	dc.b	$00,$00,$fe,$02,$02,$00,$00,$00
	dc.b	$42,$44,$48,$56,$29,$46,$88,$1f
	dc.b	$42,$44,$48,$56,$2a,$5f,$82,$07
	dc.b	$30,$00,$30,$30,$30,$30,$30,$00
	dc.b	$00,$24,$48,$90,$48,$24,$00,$00
	dc.b	$00,$48,$24,$12,$24,$48,$00,$00
	dc.b	$88,$22,$88,$22,$88,$00,$88,$22
	dc.b	$aa,$55,$aa,$55,$aa,$00,$aa,$55
	dc.b	$77,$dd,$77,$dd,$77,$ff,$77,$dd
	dc.b	$10,$10,$10,$10,$10,$10,$10,$10
	dc.b	$10,$10,$10,$f0,$10,$10,$10,$10
	dc.b	$10,$10,$f0,$10,$f0,$10,$10,$10
	dc.b	$28,$28,$28,$e8,$28,$28,$28,$28
	dc.b	$00,$00,$00,$f8,$28,$28,$28,$28
	dc.b	$00,$00,$f0,$10,$f0,$10,$10,$10
	dc.b	$28,$28,$e8,$08,$e8,$28,$28,$28
	dc.b	$28,$28,$28,$28,$28,$28,$28,$28
	dc.b	$00,$00,$f8,$08,$e8,$28,$28,$28
	dc.b	$28,$28,$e8,$08,$f8,$00,$00,$00
	dc.b	$28,$28,$28,$f8,$00,$00,$00,$00
	dc.b	$10,$10,$f0,$10,$f0,$00,$00,$00
	dc.b	$00,$00,$00,$f0,$10,$10,$10,$10
	dc.b	$10,$10,$10,$1f,$00,$00,$00,$00
	dc.b	$10,$10,$10,$ff,$00,$00,$00,$00
	dc.b	$00,$00,$00,$ff,$10,$10,$10,$10
	dc.b	$10,$10,$10,$1f,$10,$10,$10,$10
	dc.b	$00,$00,$00,$ff,$00,$00,$00,$00
	dc.b	$10,$10,$10,$ff,$10,$10,$10,$10
	dc.b	$10,$10,$1f,$10,$1f,$10,$10,$10
	dc.b	$28,$28,$28,$2f,$28,$28,$28,$28
	dc.b	$28,$28,$2f,$20,$3f,$00,$00,$00
	dc.b	$00,$00,$3f,$20,$2f,$28,$28,$28
	dc.b	$28,$28,$ef,$00,$ff,$00,$00,$00
	dc.b	$00,$00,$ff,$00,$ef,$28,$28,$28
	dc.b	$28,$28,$2f,$20,$2f,$28,$28,$28
	dc.b	$00,$00,$ff,$00,$ff,$00,$00,$00
	dc.b	$28,$28,$ef,$00,$ef,$28,$28,$28
	dc.b	$10,$10,$ff,$00,$ff,$00,$00,$00
	dc.b	$28,$28,$28,$ff,$00,$00,$00,$00
	dc.b	$00,$00,$ff,$00,$ff,$10,$10,$10
	dc.b	$00,$00,$00,$ff,$28,$28,$28,$28
	dc.b	$28,$28,$28,$3f,$00,$00,$00,$00
	dc.b	$10,$10,$1f,$10,$1f,$00,$00,$00
	dc.b	$00,$00,$1f,$10,$1f,$10,$10,$10
	dc.b	$00,$00,$00,$3f,$28,$28,$28,$28
	dc.b	$28,$28,$28,$ff,$28,$28,$28,$28
	dc.b	$10,$10,$ff,$10,$ff,$10,$10,$10
	dc.b	$10,$10,$10,$f0,$00,$00,$00,$00
	dc.b	$00,$00,$00,$1f,$10,$10,$10,$10
	dc.b	$ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
	dc.b	$00,$00,$00,$00,$ff,$ff,$ff,$ff
	dc.b	$f0,$f0,$f0,$f0,$f0,$f0,$f0,$f0
	dc.b	$0f,$0f,$0f,$0f,$0f,$0f,$0f,$0f
	dc.b	$ff,$ff,$ff,$ff,$00,$00,$00,$00
	dc.b	$00,$00,$00,$72,$8c,$88,$3a,$00
	dc.b	$30,$48,$48,$7c,$42,$42,$dc,$00
	dc.b	$00,$00,$fe,$42,$40,$40,$e0,$00
	dc.b	$00,$00,$fe,$44,$44,$44,$ee,$00
	dc.b	$fe,$42,$20,$10,$20,$42,$fe,$00
	dc.b	$00,$00,$00,$3e,$44,$44,$38,$00
	dc.b	$00,$00,$cc,$44,$44,$44,$7a,$40
	dc.b	$00,$00,$7c,$10,$10,$10,$1c,$00
	dc.b	$00,$10,$7c,$92,$92,$7c,$10,$00
	dc.b	$00,$00,$7c,$82,$ba,$82,$7c,$00
	dc.b	$00,$7c,$82,$82,$6c,$28,$ee,$00
	dc.b	$00,$7c,$20,$38,$44,$44,$38,$00
	dc.b	$00,$00,$6c,$92,$92,$6c,$00,$00
	dc.b	$00,$00,$4c,$92,$92,$7c,$10,$00
	dc.b	$00,$00,$3c,$40,$30,$40,$3c,$00
	dc.b	$00,$00,$3c,$42,$42,$42,$42,$00
	dc.b	$00,$fe,$00,$fe,$00,$fe,$00,$00
	dc.b	$10,$10,$fe,$10,$10,$fe,$00,$00
	dc.b	$40,$10,$04,$10,$40,$fe,$00,$00
	dc.b	$04,$10,$40,$10,$04,$fe,$00,$00
	dc.b	$0c,$10,$10,$10,$10,$10,$10,$10
	dc.b	$10,$10,$10,$10,$10,$10,$10,$60
	dc.b	$00,$00,$10,$00,$fe,$00,$10,$00
	dc.b	$00,$62,$9c,$00,$62,$9c,$00,$00
	dc.b	$30,$48,$48,$30,$00,$00,$00,$00
	dc.b	$00,$00,$00,$30,$30,$00,$00,$00
	dc.b	$00,$00,$00,$00,$08,$00,$00,$00
	dc.b	$01,$02,$e2,$24,$14,$18,$08,$00
	dc.b	$00,$00,$58,$24,$24,$00,$00,$00
	dc.b	$30,$48,$10,$20,$78,$00,$00,$00
	dc.b	$00,$00,$ff,$ff,$ff,$ff,$00,$00
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00
	EVEN

check_basigeaxorz_cd:
	cmp.b	#'B',(MCOMMR+1)
	beq	check_basigeaxorz_cd_ok
	add.l	#4,a7
	rts
	move.l	#vector_segacd_text,a0
	bra	vector_do
check_basigeaxorz_cd_ok:
	rts
segacd_send_req:
	cmp.b	#'Q',(MCOMMR)
	bne	segacd_send_req
	rts
segacd_send_ack:
	move.b	#'P',(MCOMMW)
segacd_send_ack_1:
	cmp.b	#'P',(MCOMMR)
	bne	segacd_send_ack_1
	move.b	#'Q',(MCOMMW)
	rts

segacd_play_track:
	bsr	check_basigeaxorz_cd
	bsr	segacd_send_req
	move.w	#'CA',(MCOMMW+2)
	move.w	d0,(MCOMMW+4)
	bra	segacd_send_ack
segacd_play_track2:
	bsr	check_basigeaxorz_cd
	bsr	segacd_send_req
	move.w	#'CB',(MCOMMW+2)
	move.w	d0,(MCOMMW+4)
	bra	segacd_send_ack
segacd_stop_play:
	bsr	check_basigeaxorz_cd
	bsr	segacd_send_req
	move.w	#'CC',(MCOMMW+2)
	bra	segacd_send_ack

segacd_load:		; d0=cluster
	bsr	check_basigeaxorz_cd
	move	#$2700,sr
	clr.l	(VBLANK_JUMP)
	clr.l	(HBLANK_JUMP)
	move.l	#segacd_load_driver,a0
	move.l	#HEAPSTART,a1
	move.l	#segacd_load_driver_end-segacd_load_driver+4,d1
segacd_load_loop:
	move.b	(a0)+,(a1)+
	dbra	d1,segacd_load_loop
	jmp	HEAPSTART
segacd_load_driver:
	bset	#1, $A12003
segacd_load_driver_ack_1:
	cmp.b	#'Q',(MCOMMR)
	bne	segacd_load_driver_ack_1
	move.w	#'CD',(MCOMMW+2)
	move.l	d0,(MCOMMW+4)
	move.b	#'P',(MCOMMW)
segacd_load_driver_ack:
	cmp.b	#'P',(MCOMMR)
	bne	segacd_load_driver_ack
	move.b	#'Q',(MCOMMW)
;	move.l	($200000),($FF0000)
segacd_load_driver_end_loop:
;	bra	segacd_load_driver_end_loop
	jmp	$200002
segacd_load_driver_end:

pushdatastring:
	move.l	#HEAPSTART,a5
	move.l	a5,a1
pushdatastring_loop:
	move.b	(a1)+,(a6)+
	bne	pushdatastring_loop
	rts

delay2:
	tst.l	d0
	beq	delay2_exit
	move.l	d1,-(a7)
	move.l	d0,d1
delay2_loop1:
	move.w	($C00004),d0
	btst.l	#2,d0
	beq	delay2_loop1
delay2_loop2:
	move.w	($C00004),d0
	btst.l	#2,d0
	bne	delay2_loop2
	sub.l	#1,d1
	bne	delay2_loop1
	move.l	(a7)+,d1
delay2_exit:
	rts

delay:
	tst.l	d0
	beq	delay_exit
	move.l	d1,-(a7)
	move.l	d0,d1
delay_loop1:
	move.w	($C00004),d0
	btst.l	#3,d0
	beq	delay_loop1
delay_loop2:
	move.w	($C00004),d0
	btst.l	#3,d0
	bne	delay_loop2
	sub.l	#1,d1
	bne	delay_loop1
	move.l	(a7)+,d1
delay_exit:
	rts

wait_hblank:
	move.w	($C00004),d0
	and.b	#$04,d0
	beq	wait_hblank
	rts

wait_vblank:
	move.w	($C00004),d0
	and.b	#$08,d0
	beq	wait_vblank
	rts

wait_activedisplay:
	move.w	($C00004),d0
	and.b	#$0C,d0
	bne	wait_activedisplay
	rts

joypad:
	move	#$2700,sr
	move.w	d1,-(a7)
	move.l	a5,a1
	and.l	#3,d0
	clr.l	d2
	move.l	#$A10003,a0
	lsl.b	#1,d0
	add.l	d0,a0
	move.w	#3,d0
joypad_loop:
	move.b	(a0),(a1)+
	move.b	#$00,(a0)	; Low
	move.w	#10,d1
joypad_loop_1:
	dbra	d1,joypad_loop_1
	move.b	(a0),(a1)+
	move.b	#$40,(a0)	; High
	move.w	#10,d1
joypad_loop_2:
	dbra	d1,joypad_loop_2
	dbra	d0,joypad_loop
	move.b	2(a5),d0
	and.w	#$3F,d0
	move.b	3(a5),d1
	and.b	#$30,d1
	lsl.b	#2,d1
	add.b	d1,d0
	move.b	6(a5),d2
	and.w	#$000F,d2
	lsl.w	#8,d2
	add.w	d0,d2
	not.w	d2
	and.w	#$0FFF,d2
	move.w	(a7)+,d1
	move	#$2000,sr
	rts

str_cmp:
	move.l	#HEAPSTART,a5
	move.l	a5,a0
str_cmp_loop:
	move.b	(a0)+,d1
	cmp.b	(a1)+,d1
	bne	str_cmp_end
	tst.b	d1
	bne	str_cmp_loop
	moveq	#-127,d0
	rts
str_cmp_end:
	clr.b	d0
	rts

push_string:
	move.b	(a0)+,(a5)+
	bne	push_string
	sub.l	#1,a5
	rts

popstring:
	move.l	#HEAPSTART,a5
	move.l	a5,a1
popstring_loop:
	move.b	(a1)+,(a0)+
	bne	popstring_loop
	rts

* Generates the random seed
* In: D0.l = seed
* Returns: D0.l = garbage
RandomSeed:
	add.l	d0,d1
	movem.l	d0/d1,(rnd_seed)
longrnd:
	movem.l	d2-d3,-(a7)
	movem.l	(rnd_seed),d0/d1
	andi.b	#$0e,d0
	ori.b	#$20,d0
	move.l	d0,d2
	move.l	d1,d3
	add.l	d2,d2
	addx.l	d3,d3
	add.l	d2,d0
	addx.l	d3,d1
	swap	d3
	swap	d2
	move.w	d2,d3
	clr.w	d2
	add.l	d2,d0
	addx.l	d3,d1
	movem.l	d0/d1,(rnd_seed)
	move.l	d1,d0
	movem.l	(a7)+,d2-d3
	rts


strings_length:
	clr.l	d2
strings_length_loop:
	addq.l	#1,d2
	tst.b	(a2)+
	bne	strings_length_loop
	subq.l	#1,d2
	rts

* HEAP=string, d0=size
strings_left:
	move.l	a2,a5
strings_left_1:
	tst.b	(a5)+
	beq	strings_left_2
	dbra	d0,strings_left_1
strings_left_2:
	clr.b	-(a5)
	rts

* HEAP=string, d0=size
strings_right:
	move.l	a2,a0
	move.l	a5,a1
	sub.l	d0,a1
	cmp.l	a0,a1
	bhi	strings_right_1
	rts
strings_right_1:
	move.b	(a1)+,(a0)+
	bne	strings_right_1
	move.l	a0,a5
	subq.l	#1,a5
	rts

* HEAP=string, d0=size, d7=start
strings_mid_1:
	move.l	a2,a0
	move.l	a0,a1
	tst.l	d0
	beq	strings_mid_5
	add.l	d7,a0
	cmp.l	a0,a5
	bhi	strings_mid_2
strings_mid_5:
	clr.b	(a1)
	move.l	a1,a5
	rts
strings_mid_2:
	subq.l	#1,d0
strings_mid_3:
	move.b	(a0)+,(a1)+
	beq	strings_mid_4
	dbra	d0,strings_mid_3
	clr.b	(a1)
strings_mid_4:
	move.l	a1,a5
	rts

* HEAP=string, d0=start
strings_mid_dsg1:
	move.l	a2,a0
	move.l	a0,a1
	add.l	d0,a0
	cmp.l	a0,a5
	bhi	strings_mid_fgh3
	clr.b	(a1)
	move.l	a1,a5
	rts
strings_mid_fgh3:
	move.b	(a0)+,(a1)+
	bne	strings_mid_fgh3
	move.l	a1,a5
	rts

* Generates a random word
* In: D0.w = Range 1-65535
* Returns: D0.w = 0-65534
GenRandom:
	move.w	d2,-(a7)
	move.w	d0,d2
	beq.s	_random_loop
	bsr	longrnd
	clr.w	d0
	swap	d0
	divu.w	d2,d0
	clr.w	d0
	swap	d0
_random_loop:
	move.w	(a7)+,d2
	rts

pushinteger:
	move.l	d1,-(a7)
	move.l	d2,-(a7)
	move.l	d3,-(a7)
	move.l	d4,-(a7)
	move.l	d5,-(a7)
	and.l	#$FFFF,d0
	move.l	d0,d2
	clr.l	d1
	move.l	#10000,d3
	move.l	#4,d4
	clr.l	d5
pushinteger_loop:
	divu	d3,d2
	move.w	d2,d1
	tst.w	d1
	beq	pushinteger_zero
pushinteger_notzero:
	move.l	#1,d5
	add.b	#'0',d1
	move.b	d1,(a5)+
pushinteger_loop_ret:
	divu	#10,d3
	clr.w	d2
	swap	d2
	dbra	d4,pushinteger_loop
	move.l	(a7)+,d5
	move.l	(a7)+,d4
	move.l	(a7)+,d3
	move.l	(a7)+,d2
	move.l	(a7)+,d1
	rts
pushinteger_zero:
	tst.l	d5
	bne	pushinteger_notzero
	tst.l	d4
	beq	pushinteger_notzero
	bra	pushinteger_loop_ret

string_input:
	lea	string_input_chars+1(PC),a0
	move.l	a5,a2
string_input_loop:
	clr.w	d1
	move.b	(a0),d1
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	add.w	(COLOR),d1
	move.w	d1,($C00000)
	move	#$2000,sr
string_input_key:
	move.l	a0,-(a7)
	clr.l	d0
	bsr	joypad
	move.l	(a7)+,a0
	btst.l	#0,d2	; Up
	bne	string_input_up
	btst.l	#1,d2	; Down
	bne	string_input_down
	btst.l	#6,d2	; A
	bne	string_input_a
	btst.l	#4,d2	; B
	bne	string_input_b
	btst.l	#7,d2	; Start
	bne	string_input_start
	bra	string_input_key
string_input_up:
	addq.l	#1,a0
	tst.b	(a0)
	bne	string_input_delay
	move.l	#string_input_chars+1,a0
;	bra	string_input_delay
string_input_delay:
	clr.w	d1
	move.b	(a0),d1
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	add.w	(COLOR),d1
	move.w	d1,($C00000)
	move	#$2000,sr
	move.l	#10,d0
	bsr	delay
	bra	string_input_key
string_input_down:
	subq.l	#1,a0
	tst.b	(a0)
	bne	string_input_delay
	move.l	#string_input_chars_end-2,a0
	bra	string_input_delay
string_input_a:
	cmp.b	#'_',(a0)
	beq	string_input_a_space
	move.b	(a0),(a5)+
string_input_a_ret:
	add.b	#1,(CURSORX)
	cmp.b	#38,(CURSORX)
	bne	string_input_delay
	bsr	print_cr
	bra	string_input_delay
string_input_a_space:
	move.b	#' ',(a5)+
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	move.w	(COLOR),($C00000)
	move	#$2000,sr
	bra	string_input_a_ret
string_input_b:
	cmp.l	a2,a5
	beq	string_input_key
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	move.w	(COLOR),($C00000)
	move	#$2000,sr
	move.l	#string_input_chars+1,a0
	move.b	-(a5),d0
	cmp.b	#' ',d0
	beq	string_input_b_skip
	addq.l	#1,a0
string_input_b_loop:
	cmp.b	(a0)+,d0
	bne	string_input_b_loop
	subq.l	#1,a0
string_input_b_skip:
	tst.b	(CURSORX)
	beq	string_input_b_back
	sub.b	#1,(CURSORX)
	bra	string_input_delay
string_input_b_back:
	move.b	#37,(CURSORX)
	sub.b	#1,(CURSORY)
	bra	string_input_delay
string_input_start:
	clr.b	(a5)
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	move.w	(COLOR),($C00000)
	move	#$2000,sr
	rts
string_input_chars:
	dc.b	0,'_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',0
string_input_chars_end:
	EVEN

string_input2:
	lea	string_input2_chars+1(PC),a0
	move.l	a5,a2
string_input2_loop:
	clr.w	d1
	move.b	(a0),d1
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	add.w	(COLOR),d1
	move.w	d1,($C00000)
	move	#$2000,sr
string_input2_key:
	move.l	a0,-(a7)
	clr.l	d0
	bsr	joypad
	move.l	(a7)+,a0
	btst.l	#0,d2	; Up
	bne	string_input2_up
	btst.l	#1,d2	; Down
	bne	string_input2_down
	btst.l	#6,d2	; A
	bne	string_input2_a
	btst.l	#4,d2	; B
	bne	string_input2_b
	btst.l	#7,d2	; Start
	bne	string_input2_start
	bra	string_input2_key
string_input2_up:
	addq.l	#1,a0
	tst.b	(a0)
	bne	string_input2_delay
	move.l	#string_input2_chars+1,a0
;	bra	string_input2_delay
string_input2_delay:
	clr.w	d1
	move.b	(a0),d1
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	add.w	(COLOR),d1
	move.w	d1,($C00000)
	move	#$2000,sr
	move.l	#10,d0
	bsr	delay
	bra	string_input2_key
string_input2_down:
	subq.l	#1,a0
	tst.b	(a0)
	bne	string_input2_delay
	move.l	#string_input2_chars_end-2,a0
	bra	string_input2_delay
string_input2_a:
	cmp.b	#'_',(a0)
	beq	string_input2_a_space
	move.b	(a0),(a5)+
string_input2_a_ret:
	add.b	#1,(CURSORX)
	cmp.b	#38,(CURSORX)
	bne	string_input2_delay
	bsr	print_cr
	bra	string_input2_delay
string_input2_a_space:
	move.b	#' ',(a5)+
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	move.w	(COLOR),($C00000)
	move	#$2000,sr
	bra	string_input2_a_ret
string_input2_b:
	cmp.l	a2,a5
	beq	string_input2_key
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	move.w	(COLOR),($C00000)
	move	#$2000,sr
	move.l	#string_input2_chars+1,a0
	move.b	-(a5),d0
	cmp.b	#' ',d0
	beq	string_input2_b_skip
	addq.l	#1,a0
string_input2_b_loop:
	cmp.b	(a0)+,d0
	bne	string_input2_b_loop
	subq.l	#1,a0
string_input2_b_skip:
	tst.b	(CURSORX)
	beq	string_input2_b_back
	sub.b	#1,(CURSORX)
	bra	string_input2_delay
string_input2_b_back:
	move.b	#37,(CURSORX)
	sub.b	#1,(CURSORY)
	bra	string_input2_delay
string_input2_start:
	clr.b	(a5)
	bsr	calc_cursor
	move	#$2700,sr
	move.l	d0,($C00004)
	move.w	(COLOR),($C00000)
	move	#$2000,sr
	rts
string_input2_chars:
	dc.b	0,'0123456789',0
string_input2_chars_end:
	EVEN

calc_cursor:
	clr.l	d0
	move.b	(CURSORY),d0
	add.l	#1,d0
	lsl.w	#6,d0
	add.b	(CURSORX),d0
	add.l	#1,d0
	lsl.l	#3,d0
	lsr.w	#2,d0
	swap	d0
	add.l	(TEXT_PLANE),d0
	rts

print_w:
	move	#$2700,sr
	and.l	#$FFFF,d0
	move.l	d0,d2
	move.w	#$8F02,($C00004)
	bsr	calc_cursor
	move.l	d0,($C00004)
	clr.l	d1
	move.l	#10000,d3
	move.l	#4,d4
	clr.l	d5
print_w_loop:
	divu	d3,d2
	move.w	d2,d1
	tst.w	d1
	beq	print_w_zero
print_w_notzero:
	move.l	#1,d5
	add.b	#'0',d1
	add.w	(COLOR),d1
	move.w	d1,($C00000)
	add.b	#1,(CURSORX)
	cmp.b	#38,(CURSORX)
	beq	print_w_cr
print_w_loop_ret:
	divu	#10,d3
	clr.w	d2
	swap	d2
	dbra	d4,print_w_loop
	move	#$2000,sr
	rts
print_w_cr:
	bsr	print_cr
*	move.w	#$8F02,($C00004)
	bsr	calc_cursor
	move.l	d0,($C00004)
	clr.l	d1
	bra	print_w_loop_ret
print_w_zero:
	tst.l	d5
	bne	print_w_notzero
	tst.l	d4
	beq	print_w_notzero
	bra	print_w_loop_ret

puts_print_w:
	move	#$2700,sr
	and.l	#$FFFF,d0
	move.l	d0,d2
;	move.w	#$8F02,($C00004)
;	bsr	calc_cursor
;	move.l	d0,($C00004)
	clr.l	d1
	move.l	#10000,d3
	move.l	#4,d4
	clr.l	d5
puts_print_w_loop:
	divu	d3,d2
	move.w	d2,d1
	tst.w	d1
	beq	puts_print_w_zero
puts_print_w_notzero:
	move.l	#1,d5
	add.b	#'0',d1
	add.w	(COLOR),d1
	move.w	d1,($C00000)
;	add.b	#1,(CURSORX)
;	cmp.b	#38,(CURSORX)
;	beq	puts_print_w_cr
puts_print_w_loop_ret:
	divu	#10,d3
	clr.w	d2
	swap	d2
	dbra	d4,puts_print_w_loop
	move	#$2000,sr
	rts
puts_print_w_zero:
	tst.l	d5
	bne	puts_print_w_notzero
	tst.l	d4
	beq	puts_print_w_notzero
	bra	puts_print_w_loop_ret

popdisplay:
	move	#$2700,sr
	move.l	#HEAPSTART,a5
	move.l	a5,a1
popdisplay_loop:
	move.b	(a1)+,d0
	tst.b	d0
	beq	popdisplay_end
	bsr	print_c
	bra	popdisplay_loop
popdisplay_end:
	move	#$2000,sr
	rts

puts_popdisplay:
	move	#$2700,sr
	move.l	#HEAPSTART,a5
	move.l	a5,a1
	move.w	(COLOR),d0
puts_popdisplay_loop:
	move.b	(a1)+,d0
;	tst.b	d0
	beq	puts_popdisplay_end
	move.w	d0,($C00000)
	bra	puts_popdisplay_loop
puts_popdisplay_end:
	move	#$2000,sr
	rts

print_z:
	move	#$2700,sr
print_z_loop:
	move.b	(a0)+,d0
	tst.b	d0
	beq	print_z_end
	bsr	print_c
	bra	print_z_loop
print_z_end:
	move	#$2000,sr
	rts

popdatastring:
	move.l	($FF0006),a1
popdatastring_loop:
	move.b	(a6)+,(a0)+
	bne	popdatastring_loop
	rts

CURSORX		EQU	$FF0000
CURSORY		EQU	$FF0001
COLOR		EQU	$FF0002
CURSORX2	EQU	$FF0004
CURSORY2	EQU	$FF0005

screen_init_scroll:
	move.w	#$8F01,($C00004)
	move.w	#$9300,($C00004)
	move.w	#$9404,($C00004)
	move.w	#$9500,($C00004)
	move.w	#$9600,($C00004)
	move.w	#$9780,($C00004)
	move.l	#$68000082,($C00004)
	move.w	#$0000,($C00000)
	bsr	wait_dma
	move.w	#$8F02,($C00004)
	move.l	#$40000010,($C00004)
	move.l	#39,d0
screen_init_scroll_loop:
	move.w	#$0000,($C00000)
	dbra	d0,screen_init_scroll_loop
	rts

wait_dma:
	move.w	d0,-(a7)
wait_dma_loop:
	move.w	($C00004),d0
	btst.l	#1,d0
	bne	wait_dma_loop
	move.w	(a7)+,d0
	rts

strings_hex:
	move.w	#7,d2
strings_hex_loop:
	move.l	#strings_hex_chars,a0
	rol.l	#4,d0
	clr.l	d1
	move.b	d0,d1
	and.b	#$F,d1
	move.b	0(d1,a0),(a5)+
	dbra	d2,strings_hex_loop
	clr.b	(a5)
	rts
strings_hex_chars:
	dc.b	'0123456789ABCDEF'
	EVEN

strings_bin:
	move.w	#31,d2
strings_bin_loop:
	move.b	#'0',d1
	lsl.l	#1,d0
	bcc	strings_bin_zero
	move.b	#'1',d1
strings_bin_zero:
	move.b	d1,(a5)+
	dbra	d2,strings_bin_loop
	clr.b	(a5)
	rts

screen_cls:
	move	#$2700,sr
	move.l	#$C00004,a0
	move.w	#$8F02,(a0)
	move.w	#$93FF,(a0)		; Num bytes low
	move.w	#$9406,(a0)		; Num bytes high
	move.w	#$9500,(a0)		; Zeroed
	move.w	#$9600,(a0)		; Zeroed
	move.w	#$9780,(a0)		; Set to $80
	move.l	#$00000080,d0
	add.l	(TEXT_PLANE),d0
	move.l	d0,(a0)			; Address
	move.w	#$2000,-4(a0)
	bsr	wait_dma
	move.w	#$8F02,(a0)
	move.w	#$93FF,(a0)
	move.w	#$9406,(a0)
	move.w	#$9500,(a0)
	move.w	#$9600,(a0)
	move.w	#$9780,(a0)
	move.l	#$00010080,d0
	add.l	(TEXT_PLANE),d0
	move.l	d0,(a0)
	move.w	(COLOR),-4(a0)
	bsr	wait_dma
	clr.w	(CURSORX)
	move	#$2000,sr
	rts

;4:
;	clr.l	d0
;	move.b	(CURSORY),d0
;	add.l	#1,d0
;	lsl.w	#6,d0
;	add.b	(CURSORX),d0
;	add.l	#1,d0
;	lsl.l	#3,d0
;	lsr.w	#2,d0
;	swap	d0
;	add.l	#$40000003,d0
;	rts

sprites_init:
;	move.l	#SPRITES_TABLE,a0
	move.l	#$8001,d0
	move.w	#$8F02,($C00004)
	move.l	#$6C000002,($C00004)
sprites_init_loop_1:
;	clr.b	(a0)+
;	nop
	move.w	#0,($C00000)
	move.w	d0,($C00000)
	move.w	#0,($C00000)
	move.w	#0,($C00000)
	addq.l	#1,d0
	cmp.w	#$8000+80,d0
	bne	sprites_init_loop_1
;	clr.b	(a0)+
	move.w	#0,($C00000)
	move.w	#$8000,($C00000)
	move.w	#0,($C00000)
	move.w	#0,($C00000)
	rts

* Index + 0  :   ------yy yyyyyyyy
* Index + 2  :   ----hhvv
* Index + 3  :   -lllllll
* Index + 4  :   pccvhnnn nnnnnnnn
* Index + 6  :   ------xx xxxxxxxx
* heap: ,,
sprites_add:
	move	#$2700,sr
	sub.b	#1,d0
	and.w	#3,d0
	lsl.b	#2,d0
	add.w	-(a5),d0
	lsl.w	#8,d0
	clr.l	d2
	move.w	#$8F08,($C00004)
	move.l	#$2C020002,($C00004)
sprites_add_loop_1:
	move.w	($C00000),d1
	btst.l	#15,d1
	bne	sprites_add_loop_2
	addq.l	#1,d2
	cmp.b	#80,d2
	bne	sprites_add_loop_1
	move.l	#vector_sprite_text,a0
	bra	vector_do
sprites_add_loop_2:
	bclr.l	#15,d1
	add.w	d1,d0
	move.l	d2,d1
	lsl.w	#3,d1
	swap	d1
	move.w	#$8F02,($C00004)
	add.l	#$6C020002,d1
	move.l	d1,($C00004)
	move.w	d0,($C00000)
	move	#$2000,sr
	rts

;	add.w	($C00000),d0
;	add.l	#$3FFE0000,d1
;
;	move.w	#0,($C00000)
;	move.w	#$8F02,($C00004)
;	move.l	d1,($C00004)
;
;
;	move	#$2700,sr
;	sub.b	#1,d0
;	and.w	#3,d0
;	lsl.b	#2,d0
;	add.w	-(a5),d0
;	lsl.w	#8,d0
;	move.l	#SPRITES_TABLE,a0
;	move.l	a0,a1
;	clr.l	d3
;	move.l	#$FF,d3
;sprites_add_loop:
;	add.b	#1,d3
;	move.b	(a0)+,d2
;	bpl	sprites_add_loop
;	clr.w	d5
;	move.b	(a1),d5
;	and.b	#$7F,d5
;	move.b	d5,-(a0)
;	move.b	d3,(a1)
;	move.w	d3,(a5)+
;	move.w	#$8F02,($C00004)
;	move.l	#$2C020002,($C00004)
;	nop
;	move.w	($C00000),d6
;	clr.b	d6
;	move.b	d3,d6
;	nop
;	move.w	#$8F02,($C00004)
;	move.l	#$6C020002,($C00004)
;	nop
;	move.w	d6,($C00000)
;	move.l	d3,d6
;	lsl.w	#3,d6
;	swap	d6
;	move.w	#$8F02,($C00004)
;	add.l	#$6C000002,d6
;	move.l	d6,($C00004)
;	nop
;	move.w	#0,($C00000)
;	add.w	d5,d0
;	move.w	d0,($C00000)
;	nop
;	move.w	#0,($C00000)
;	nop
;	move.w	#0,($C00000)
;	move	#$2000,sr
;	rts

* heap: d0.w=sprite
sprites_kill:
	move	#$2700,sr
	lsl.w	#3,d0
	swap	d0
	move.w	#$8F02,($C00004)
	add.l	#$2C020002,d0
	move.l	d0,($C00004)
	move.w	#$8000,d1
	move.w	($C00000),d2
	add.b	d2,d1
	move.w	#$8F02,($C00004)
	add.l	#$3FFE0000,d0
	move.l	d0,($C00004)
	move.w	#0,($C00000)
	move.w	d1,($C00000)
	move.w	#0,($C00000)
	move.w	#0,($C00000)
	move	#$2000,sr
	rts

;	move	#$2700,sr
;	tst.b	d0
;	beq	sprites_kill_skip
;	move.l	#SPRITES_TABLE,a0
;	move.l	d0,d1
;	lsl.w	#3,d1
;	swap	d1
;	add.l	#$6C020002,d1
;	move.l	d1,($C00004)
;	nop
;	move.w	#0,($C00000)
;	clr.l	d1
;	move.b	0(d0,a0),d1
;	move.b	#$80,0(d0,a0)
;	clr.l	d2
;	sub.l	#1,d2
;sprites_kill_look:
;	add.l	#1,d2
;	cmp.b	(a0)+,d0
;	bne	sprites_kill_look
;	move.b	d1,-(a0)
;	lsl.w	#3,d2
;	swap	d2
;	add.l	#$2C020002,d2
;	move.l	d2,($C00004)
;	nop
;	move.w	($C00000),d0
;	and.w	#$FF00,d0
;	add.b	d1,d0
;	add.l	#$40000000,d2
;	move.l	d2,($C00004)
;	nop
;	move.w	d1,($C00000)
;	move	#$2000,sr
;	rts
;sprites_kill_skip:
;	move.w	#$8F04,($C00004)
;	move.l	#$6C000002,($C00004)
;	nop
;	move.w	#0,($C00000)
;	nop
;	move.w	#0,($C00000)
;	move	#$2000,sr
;	rts

strings_string:
	subq.l	#1,d0
	bcs	strings_string_ret
strings_string_loop:
	move.b	d7,(a5)+
	dbra	d0,strings_string_loop
strings_string_ret:
	clr.b	(a5)
	rts

; tones: d7=channel, d0=frequency
; noise: d7=3, d0=feedback/clock
psg_sound:
	and.b	#$03,d7
	cmp.b	#3,d7
	beq	psg_sound_noise
	not.w	d0
	or.b	#$04,d7
	lsl.b	#5,d7
	move.b	d0,d1
	and.b	#$0F,d1
	or.b	d7,d1
	move.b	d1,($C00011)
	lsr.w	#4,d0
	and.b	#$3F,d0
	move.b	d0,($C00011)
	rts
psg_sound_noise:
	and.b	#$07,d0
	or.b	#$E0,d0
	move.b	d0,($C00011)
	rts

; d7=channel, d0=volume
psg_volume:
	and.b	#$03,d7
	lsl.b	#5,d7
	or.b	#$90,d7
	not.b	d0
	and.b	#$0F,d0
	or.b	d7,d0
	move.b	d0,($C00011)
	rts

sprites_collision:
	move	#$2700,sr
	move.w	-(a5),d2
sprites_collission_check_h:
	move.w	($C00008),d1
	lsr.w	#8,d1
	cmp.w	d1,d2
	bne	sprites_collission_check_h
sprites_collision_check_h2:
	move.w	($C00004),d2
	btst.l	#5,d2
	bne	sprites_collidde_collidede
	move.w	($C00008),d1
	lsr.w	#8,d1
	cmp.w	d1,d0
	bne	sprites_collision_check_h2
	clr.l	d2
	move	#$2000,sr
	rts
sprites_collidde_collidede:
	move.w	($C00008),d2
	and.l	#$FF00,d2
	lsr.w	#8,d2
	move	#$2000,sr
	rts

* heap: d0.w=sprite
hide_spritess:
	move	#$2700,sr
	move.w	#$8F06,($C00004)
	lsl.w	#3,d0
	add.l	#$6C000002,d0
	move.l	d0,($C00004)
;	nop
	move.w	#0,($C00000)
;	nop
	move.w	#0,($C00000)
	move	#$2000,sr
	rts

* heap: sprite.w, tile.w; d0.w=pallette
propsprite:
	move	#$2700,sr
	and.w	#3,d0
	ror.w	#3,d0
	add.w	-(a5),d0
	clr.l	d1
	move.w	-(a5),d1
	lsl.w	#3,d1
	swap	d1
	add.l	#$6C040002,d1
	move.l	d1,($C00004)
	nop
	move.w	d0,($C00000)
	move	#$2000,sr
	rts

* heap: sprite.w, dx.w; d0.w=dy
shiftsprite:
	move	#$2700,sr
	move.w	-(a5),d1
	clr.l	d2
	move.w	-(a5),d2
	lsl.w	#3,d2
	swap	d2
	move.w	#$8F06,($C00004)
	add.l	#$2C000002,d2
	move.l	d2,($C00004)
	nop
	add.w	($C00000),d0
	and.w	#$3FF,d0
	nop
	add.w	($C00000),d1
	and.w	#$3FF,d1
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	nop
	move.w	d0,($C00000)
	nop
	move.w	d1,($C00000)
	move	#$2000,sr
	rts

* d0.w=sprite.w
getspriteposy:
	move	#$2700,sr
	lsl.w	#3,d0
	swap	d0
	add.l	#$2C000002,d0
	move.l	d0,($C00004)
	nop
	clr.l	d2
	move.w	($C00000),d2
	move	#$2000,sr
	rts

* d0.w=sprite.w
getspriteposx:
	move	#$2700,sr
	lsl.w	#3,d0
	swap	d0
	add.l	#$2C060002,d0
	move.l	d0,($C00004)
	nop
	clr.l	d2
	move.w	($C00000),d2
	move	#$2000,sr
	rts

* HEAP: pallete number.w,entry.w,rgb increment.w,delay.w; d0.w=count
brightenpallette:
	move	#$2700,sr
	move.w	#$8F00,($C00004)
	clr.l	d4
	move.w	-(a5),d4
	move.w	-(a5),d1
	clr.l	d2
	move.w	-(a5),d2
	lsl.b	#1,d2
	move.w	-(a5),d5
	ror.b	#3,d5
	add.b	d5,d2
	swap	d2
	move.l	d2,d3
	add.b	#$20,d3
	add.l	#$C0000000,d2
	move.w	d0,d5
brightenpallette_loop:
	move.l	d4,d0
	bsr	delay
	move.l	d3,($C00004)
	nop
	move.w	($C00000),d0
	add.w	d1,d0
	move.l	d2,($C00004)
	nop
	move.w	d0,($C00000)
	sub.w	#1,d5
	bne	brightenpallette_loop
	move	#$2000,sr
	clr.b	(a5)
	rts

allign_heap:
	addq.l	#1,a5
	clr.b	(a5)+
	move.l	a5,d0
	and.l	#$FFFFFFFE,d0
	move.l	d0,a5
	rts

* HEAP: pallete number.w,entry.w,rgb increment.w,delay.w; d0.w=count
darkenpallette:
	move	#$2700,sr
	move.w	#$8F00,($C00004)
	clr.l	d4
	move.w	-(a5),d4
	move.w	-(a5),d1
	clr.l	d2
	move.w	-(a5),d2
	lsl.b	#1,d2
	move.w	-(a5),d5
	ror.b	#3,d5
	add.b	d5,d2
	swap	d2
	move.l	d2,d3
	add.b	#$20,d3
	add.l	#$C0000000,d2
	move.w	d0,d5
darkenpallette_loop:
	move.l	d4,d0
	bsr	delay
	move.l	d3,($C00004)
	nop
	move.w	($C00000),d0
	sub.w	d1,d0
	move.l	d2,($C00004)
	nop
	move.w	d0,($C00000)
	sub.w	#1,d5
	bne	darkenpallette_loop
	move	#$2000,sr
	clr.b	(a5)
	rts

string_to_long:
	move.l	#string_to_long_conv,a0
	clr.l	d2
	clr.l	d3
	move.l	a2,a1
string_to_long_loop:
	cmp.l	a1,a5
	beq	string_to_long_end
	move.b	-(a5),d3
	cmp.b	#'0',d3
	bcs	string_to_long_loop
	cmp.b	#'9',d3
	bhi	string_to_long_loop
	sub.b	#'0',d3
	lsl.b	#2,d3
	add.l	0(d3,a0),d2
	add.l	#40,a0
	bra	string_to_long_loop
string_to_long_end:
	rts
string_to_long_conv:
	dc.l	0,1,2,3,4,5,6,7,8,9
	dc.l	0,10,20,30,40,50,60,70,80,90
	dc.l	0,100,200,300,400,500,600,700,800,900
	dc.l	0,1000,2000,3000,4000,5000,6000,7000,8000,9000
	dc.l	0,10000,20000,30000,40000,50000,60000,70000,80000,90000
	dc.l	0,100000,200000,300000,400000,500000,600000,700000,800000,900000
	dc.l	0,1000000,2000000,3000000,4000000,5000000,6000000,7000000,8000000,9000000
	dc.l	0,10000000,20000000,30000000,40000000,50000000,60000000,70000000,80000000,90000000
	dc.l	0,100000000,200000000,300000000,400000000,500000000,600000000,700000000,800000000,900000000
	dc.l	0,1000000000,2000000000,3000000000,4000000000

* d0.l=number
l_to_string:
	lea	l_to_string_data(PC),a0
	move.b	#'0',d2
	move.l	a5,a2
l_to_string_loop:
	tst.l	(a0)
	beq	l_to_string_ret
	move.b	#'0',d1
l_to_string_div:
	sub.l	(a0),d0
	bcs	l_to_string_char
	addq.l	#1,d1
	bra	l_to_string_div
l_to_string_char:
	add.l	(a0)+,d0
	cmp.b	d2,d1
	beq	l_to_string_loop
	clr.b	d2
	move.b	d1,(a5)+
	bra	l_to_string_loop
l_to_string_ret:
	cmp.l	a2,a5
	bne	l_to_string_ret_2
	move.b	#'0',(a5)+
l_to_string_ret_2
	clr.b	(a5)
	rts
l_to_string_data:
	dc.l	1000000000
	dc.l	100000000
	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1
	dc.l	0

* Prints a long-word in decimal format onto the screen at cursorx, cursory
* d0.l=number
print_l:
	lea	l_to_string_data(PC),a0
	move.b	(CURSORX),d2
print_l_loop:
	tst.l	(a0)
	beq	print_l_ret
	clr.b	d1
print_l_div:
	sub.l	(a0),d0
	bcs	print_l_disp_char
	add.b	#1,d1
	bra	print_l_div
print_l_disp_char:
	add.l	(a0),d0
	add.l	#4,a0
	tst.b	d1
	bne	print_l_loop2
	tst.l	(a0)
	beq	print_l_loop2
	cmp.b	(CURSORX),d2
	beq	print_l_loop
print_l_loop2:
	move.l	d0,-(a7)
	clr.l	d0
	move.b	d1,d0
	add.l	#'0',d0
	bsr	print_c
	move.l	(a7)+,d0
	bra	print_l_loop
print_l_ret:
	rts

* Prints a long-word in decimal format onto the screen at cursorx, cursory
* d0.l=number
puts_print_l:
	lea	l_to_string_data(PC),a0
	clr.b	d2
puts_print_l_loop:
	tst.l	(a0)
	beq	puts_print_l_ret
	clr.b	d1
puts_print_l_div:
	sub.l	(a0),d0
	bcs	puts_print_l_disp_char
	add.b	#1,d1
	bra	puts_print_l_div
puts_print_l_disp_char:
	add.l	(a0),d0
	add.l	#4,a0
	tst.b	d1
	bne	puts_print_l_loop2
	tst.l	(a0)
	beq	puts_print_l_loop2
	tst.b	d2
	beq	puts_print_l_loop
puts_print_l_loop2:
	move.b	#1,d2
	move.l	d0,-(a7)
	move.w	(COLOR),d0
	move.b	d1,d0
	add.l	#'0',d0
	move.w	d0,($C00000)
	move.l	(a7)+,d0
	bra	puts_print_l_loop
puts_print_l_ret:
	rts

* heap: sprite.w, x.w; d0.w=y
movesprite:
	move	#$2700,sr
	move.w	-(a5),d1
	clr.l	d2
	move.w	-(a5),d2
	lsl.w	#3,d2
	swap	d2
	move.w	#$8F06,($C00004)
	add.l	#$6C000002,d2
	move.l	d2,($C00004)
	nop
	move.w	d0,($C00000)
	nop
	move.w	d1,($C00000)
	move	#$2000,sr
	rts
	
* heap: label.l, tile vram offset.w, x.w, y.w, width.w; d0.w=height
load_map:
	move	#$2700,sr
	move.w	#$8F02,($C00004)
	clr.l	d2
	clr.l	d3
	clr.l	d4
	clr.l	d5
        move.w	d0,d4
	sub.w	#1,d4
	move.w	-(a5),d2
	sub.w	#1,d2
	move.w	-(a5),d5
	move.w	-(a5),d3
	move.w	-(a5),d1
	move.l	-(a5),a0
	lsl.w	#5,d1
	move.l	d5,d0
	lsl.l	#6,d0
	add.l	d3,d0
	lsl.l	#1,d0
	add.l	d0,d1
	lsl.l	#2,d1
	lsr.w	#2,d1
	swap	d1
	add.l	(GFX_PLANE),d1
	clr.l	d0
	move.w	(COLOR),d0
load_map_loop1:
	move.l	d1,($C00004)
	add.l   #$800000,d1
	move.l  d2,d3
load_map_loop2:
	move.b	(a0)+,d0
	move.w	d0,($C00000)
	dbra	d3,load_map_loop2
	dbra	d4,load_map_loop1
	move	#$2000,sr
	rts

* heap: tile.w, y.w; d0.w=x
drawtile:
	move	#$2700,sr
	move.w	#$8F02,($C00004)
	move.w	-(a5),d1
	exg	d0,d1
	lsl.w	#6,d1
	add.w	d1,d0
	lsl.l	#3,d0
	lsr.w	#2,d0
	swap	d0
	add.l	(GFX_PLANE),d0
	move.l	d0,($C00004)
	clr.l	d0
	move.w	-(a5),d0
	add.w	(COLOR),d0
	move.w	d0,($C00000)
	move	#$2000,sr
	rts

vscroll_up:
;	add.w	(SCROLL_PLANE),d2
	swap	d2
	add.l	#$00000010,d2
	move.l	d2,($C00004)
	nop
	move.w	($C00000),d1
	add.w	d0,d1
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	nop
	move.w	d1,($C00000)
	rts

vscroll_down:
;	add.w	(SCROLL_PLANE),d2
	swap	d2
	add.l	#$00000010,d2
	move.l	d2,($C00004)
	nop
	move.w	($C00000),d1
	sub.w	d0,d1
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	nop
	move.w	d1,($C00000)
	rts

hscroll_left:
	add.w	(SCROLL_PLANE),d2
	swap	d2
	add.l	#$28000002,d2
	move.l	d2,($C00004)
	nop
	move.w	($C00000),d1
	nop
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	sub.w	d0,d1
	move.w	d1,($C00000)
	rts

hscroll_right:
	add.w	(SCROLL_PLANE),d2
	swap	d2
	add.l	#$28000002,d2
	move.l	d2,($C00004)
	nop
	move.w	($C00000),d1
	nop
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	add.w	d0,d1
	move.w	d1,($C00000)
	rts

vscroll_up_abs:
;	add.w	(SCROLL_PLANE),d2
	swap	d2
	add.l	#$00000010,d2
	move.l	d2,($C00004)
	nop
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	nop
	move.w	d0,($C00000)
	rts

hscroll_right_abs:
	add.w	(SCROLL_PLANE),d2
	swap	d2
	add.l	#$28000002,d2
	move.l	d2,($C00004)
	nop
	add.l	#$40000000,d2
	move.l	d2,($C00004)
	move.w	d0,($C00000)
	rts

print_c:
	move	#$2700,sr
	move.l	d0,-(a7)
	move.l	d1,-(a7)
	and.l	#$FF,d0
	move.l	d0,d1
	move.w	#$8F02,($C00004)
	bsr	calc_cursor
	move.l	d0,($C00004)
	add.w	(COLOR),d1
	move.w	d1,($C00000)
	move.l	(a7)+,d1
	move.l	(a7)+,d0
	add.b	#1,(CURSORX)
	cmp.b	#38,(CURSORX)
	beq	print_cr
	move	#$2000,sr
	rts

print_cr:
	move	#$2700,sr
	clr.b	(CURSORX)
	add.b	#1,(CURSORY)
	cmp.b	#26,(CURSORY)
	beq	print_cr_lf
	move	#$2000,sr
	rts
print_cr_lf:
	move.b	#25,(CURSORY)
	move.l	#$C00004,a0
	move.w	#$8F01,(a0)
	move.w	#$9300,(a0)
	move.w	#$940D,(a0)
	move.l	(TEXT_PLANE),d0
	swap	d0
	lsl.w	#2,d0
	lsr.l	#2,d0
	lsr.w	#8,d0
	add.w	#$9601,d0
	move.w	#$9500,(a0)
	move.w	d0,(a0)
	move.w	#$97C0,(a0)
	move.l	#$C08000C0,d0
	add.l	(TEXT_PLANE),d0
	move.l	d0,(a0)
	bsr	wait_dma
	move	#$2000,sr
	rts

print_tab:
	move.b	(CURSORX),d0
	and.b	#$F8,d0
	add.b	#8,d0
	cmp.b	#38,d0
	bcc	print_cr
	move.b	d0,(CURSORX)
	rts	

load_colors:
	move	#$2700,sr
	move.w	#$8F02,($C00004)
	lsl.l	#1,d1
	swap	d1
	add.l	#$C0000000,d1
	move.l	d1,($C00004)
	clr.l	d1
	sub.l	#1,d0
load_colors_loop1:
	move.w	(a0)+,d1
	lsl.w	#1,d1
	move.w	d1,($C00000)
	dbra	d0,load_colors_loop1
	move	#$2000,sr
	rts

* load a set of tiles 8*8 large, and 32 bytes/tile
* a0=adress of tiles,d0=number of tiles,d1=vram adress
load_tiles:
	move	#$2700,sr
	move.w	#$8F02,($C00004)
	lsl.l	#2,d1
	lsr.w	#2,d1
	swap	d1
	add.l	#$40000000,d1
	move.l	d1,($C00004)
	lsl.l	#4,d0
	sub.l	#1,d0
load_tiles_loop1:
	move.w	(a0)+,($C00000)
	dbra	d0,load_tiles_loop1
	move	#$2000,sr
	rts

TILE_UNSET	equ	0
TILE_SET	equ	1
load_tiles_1bit:
	move	#$2700,sr
	move.w	#$8F02,($C00004)
	lsl.l	#2,d1
	lsr.w	#2,d1
	swap	d1
	add.l	#$40000000,d1
	move.l	d1,($C00004)
	lsl.l	#3,d0
	sub.l	#1,d0
load_tiles_1bit_loop:
	clr.l	d1
	clr.l	d2
	move.l	#1,d3
	move.l	#3,d4
	move.b	(a0)+,d1
load_tiles_1bit_loop1:
	lsl.w	#4,d2
	move.b	d1,d1
	bmi	load_tiles_1bit_set
	add.w	#TILE_UNSET,d2
load_tiles_1bit_return:
	lsl.b	#1,d1
	dbra	d4,load_tiles_1bit_loop1
	move.w	d2,($C00000)
	clr.l	d2
	move.l	#3,d4
	dbra	d3,load_tiles_1bit_loop1
	move.l	#1,d3
	move.b	(a0)+,d1
	dbra	d0,load_tiles_1bit_loop1
	move	#$2000,sr
	rts
load_tiles_1bit_set:
	add.w	#TILE_SET,d2
	bra	load_tiles_1bit_return

braekpoin:
	move	#$2700,sr
	move.w	(CURSORX),-(a7)
	move.w	#$001A,(CURSORX)
	move.l	#braekpoin_text,a0
	bsr	print_z
	move.w	#$8100,d0
	add.b	(VDP_MODE2),d0
	or.b	#$40,d0
	move.w	d0,($C00004)
	move.l	4(a7),a0
	move.l	(a0)+,d0
	move.l	a0,4(a7)
	bsr	print_l
	move.w	#$171A,(CURSORX)
	move.l	#breakpion_text_end,a0
	bsr	print_z
	bra	vector_trap_jump
braekpoin_text:		dc.b	'BREAKPOINT-Line: ',0
breakpion_text_end:	dc.b	'(START:return)',0
	EVEN

vector_buserror:
	move.l	#vector_buserror_text,a0
	bra	vector_do
vector_addresserror:
	move.l	#vector_addresserror_text,a0
	bra	vector_do
vector_illegalinstr:
	move.l	#vector_illegalinstr_text,a0
	bra	vector_do
vector_addresserror_or_illegalinstr:
	move.l	#vector_addresserror_or_illegalinstr_text,a0
	bra	vector_do
vector_dividezero:
	move.l	#vector_dividezero_text,a0
	bra	vector_do
vector_chk:
	move.l	#vector_chk_text,a0
	bra	vector_do
vector_trapv:
	move.l	#vector_trapv_text,a0
	bra	vector_do
vector_privilegeviolation:
	move.l	#vector_privilegeviolation_text,a0
	bra	vector_do
vector_trace:
	move.l	#vector_trace_text,a0
	bra	vector_do
vector_line1010:
	move.l	#vector_line1010_text,a0
	bra	vector_do
vector_line1111:
	move.l	#vector_line1111_text,a0
	bra	vector_do
vector_trap1:
	move.l	#vector_trap1_text,a0
	bra	vector_trap
vector_trap2:
	move.l	#vector_trap2_text,a0
	bra	vector_trap
vector_trap3:
	move.l	#vector_trap3_text,a0
	bra	vector_trap
vector_trap4:
	move.l	#vector_trap4_text,a0
	bra	vector_trap
vector_trap5:
	move.l	#vector_trap5_text,a0
	bra	vector_trap
vector_trap6:
	move.l	#vector_trap6_text,a0
	bra	vector_trap
vector_trap7:
	move.l	#vector_trap7_text,a0
	bra	vector_trap
vector_trap8:
	move.l	#vector_trap8_text,a0
	bra	vector_trap
vector_trap9:
	move.l	#vector_trap9_text,a0
	bra	vector_trap
vector_trap10:
	move.l	#vector_trap10_text,a0
	bra	vector_trap
vector_trap11:
	move.l	#vector_trap11_text,a0
	bra	vector_trap
vector_trap12:
	move.l	#vector_trap12_text,a0
	bra	vector_trap
vector_trap13:
	move.l	#vector_trap13_text,a0
	bra	vector_trap
vector_trap14:
	move.l	#vector_trap14_text,a0
	bra	vector_trap
vector_trap15:
	move.l	#vector_trap15_text,a0
	bra	vector_trap
vector_buserror_text:		dc.b	'68K Bus Error           (START:reset)',0
vector_addresserror_text:	dc.b	'68K Address Error       (START:reset)',0
vector_addresserror_or_illegalinstr_text:
				dc.b	'68K Address/Instr Error (START:reset)',0
vector_illegalinstr_text:	dc.b	'68K Illegal Instruction (START:reset)',0
vector_dividezero_text:		dc.b	'68K Divide by Zero      (START:reset)',0
vector_chk_text:		dc.b	'68K CHK Instruction     (START:reset)',0
vector_trapv_text:		dc.b	'68K TRAPV Instruction   (START:reset)',0
vector_privilegeviolation_text:	dc.b	'68K Privilege Violation (START:reset)',0
vector_trace_text:		dc.b	'68K TRACE               (START:reset)',0
vector_line1010_text:		dc.b	'68K Line 1010 Emulator  (START:reset)',0
vector_line1111_text:		dc.b	'68K Line 1111 Emulator  (START:reset)',0
vector_trap1_text:		dc.b	'68K TRAP #1   (START:return, C:reset)',0
vector_trap2_text:		dc.b	'68K TRAP #2   (START:return, C:reset)',0
vector_trap3_text:		dc.b	'68K TRAP #3   (START:return, C:reset)',0
vector_trap4_text:		dc.b	'68K TRAP #4   (START:return, C:reset)',0
vector_trap5_text:		dc.b	'68K TRAP #5   (START:return, C:reset)',0
vector_trap6_text:		dc.b	'68K TRAP #6   (START:return, C:reset)',0
vector_trap7_text:		dc.b	'68K TRAP #7   (START:return, C:reset)',0
vector_trap8_text:		dc.b	'68K TRAP #8   (START:return, C:reset)',0
vector_trap9_text:		dc.b	'68K TRAP #9   (START:return, C:reset)',0
vector_trap10_text:		dc.b	'68K TRAP #10  (START:return, C:reset)',0
vector_trap11_text:		dc.b	'68K TRAP #11  (START:return, C:reset)',0
vector_trap12_text:		dc.b	'68K TRAP #12  (START:return, C:reset)',0
vector_trap13_text:		dc.b	'68K TRAP #13  (START:return, C:reset)',0
vector_trap14_text:		dc.b	'68K TRAP #14  (START:return, C:reset)',0
vector_trap15_text:		dc.b	'68K TRAP #15  (START:return, C:reset)',0
vector_sprite_text:		dc.b	'SPRITE LIMIT REACHED    (START:reset)',0
vector_segacd_text:		dc.b	'NOT A BASIEGAXORZ CD    (START:reset)',0
	EVEN
vector_do:
	move.w	#$001A,(CURSORX)
	bsr	print_z
	move.w	#$8100,d0
	add.b	(VDP_MODE2),d0
	or.b	#$40,d0
	move.w	d0,($C00004)
	clr.l	d0
	bsr	waitpadup
vector_do_wait:
	clr.l	d0
	bsr	joypad
	tst.b	d2
	bpl	vector_do_wait
	bra	__initialize_reset
vector_trap:
	move	#$2700,sr
	move.w	(CURSORX),-(a7)
	move.w	#$001A,(CURSORX)
	bsr	print_z
	move.w	#$8100,d0
	add.b	(VDP_MODE2),d0
	or.b	#$40,d0
	move.w	d0,($C00004)
vector_trap_jump:
	clr.l	d0
	bsr	waitpadup
vector_trap_wait:
	clr.l	d0
	bsr	joypad
	btst.l	#7,d2
	bne	vector_trap_return
	btst.l	#5,d2
	bra	__initialize_reset
	bra	vector_trap_wait
vector_trap_return:
	move.w	#$001A,(CURSORX)
	move.l	#$8F02,($C00004)
	bsr	calc_cursor
	move.l	d0,($C00004)
	move.w	#36,d1
vector_trap_return_clear:
	move.w	(COLOR),($C00000)
	dbra	d1,vector_trap_return_clear
	move.w	(a7)+,(CURSORX)
	move	#$2000,sr
	rte

waitpadup:
	move.l	d0,-(a7)
	bsr	joypad
	move.l	(a7)+,d0
	tst.w	d2
	bne	waitpadup
	rts

; heap: address.l, pallette.w, entry.w; d0=count
load_colors2:
	move	#$2700,sr
	move.w	#$8F02,($C00004)
	clr.l	d1
	move.w	-(a5),d1
	lsl.w	#1,d1
	move.w	-(a5),d2
	ror.b	#3,d2
	add.w	d2,d1
	swap	d1
	add.l	#$C0000000,d1
	move.l	d1,($C00004)
	clr.l	d1
	subq.l	#1,d0
	move.l	-(a5),a0
load_colors2_loop1:
	move.w	(a0)+,d1
	move.w	d1,($C00000)
	dbra	d0,load_colors2_loop1
	move	#$2000,sr
	rts

__proggy_start:
	move    #$2700,sr
* VRAM Map
* Scroll B = $E000
* Scroll A = $C000
* Window   = $B000
* Sprites  = $AC00
* H Scroll = $A800
* Patterns = $0000

* VRAM Registers
	move.l	#$C00004,a0
	move.b	#$06,(VDP_MODE1)
	move.b	#$54,(VDP_MODE2)
	move.b	#$00,(VDP_MODE3)
	move.b	#$81,(VDP_MODE4)
	move.w	#$8000,d0		; Mode #1
	add.b	(VDP_MODE1),d0
	move.w	d0,(a0)
	move.w	#$8100,d0		; Mode #2
	add.b	(VDP_MODE2),d0
	move.w	d0,(a0)
	move.w	#$8B00,d0		; Mode #3
	add.b	(VDP_MODE3),d0
	move.w	d0,(a0)
	move.w	#$8C00,d0		; Mode #4
	add.b	(VDP_MODE4),d0
	move.w	d0,(a0)
	move.w	#$8700,(a0)		; Background
	move.w	#$8800,(a0)		; Low
	move.w	#$8900,(a0)		; Low
	move.w	#$8230,(a0)		; Scroll A Name Table
	move.w	#$832C,(a0)		; Window
	move.w	#$8407,(a0)		; Scroll B Name Table
	move.w	#$9011,(a0)		; 64*64
	move.w	#$9100,(a0)		; Window X
	move.w	#$9200,(a0)		; Window Y
	move.w	#$8556,(a0)		; Sprites Name Table
	move.w	#$8600,(a0)		; Low
	move.w	#$8D2A,(a0)		; H Scroll
	move.w	#$8E00,(a0)		; Low
	bsr	screen_init_scroll
	clr.l	(CURSORX)

	move.b	#$00,($a10003)
	move.b	#$00,($a10005)
	move.b	#$00,($a10007)
	move.b	#$40,($a10009)
	move.b	#$40,($a1000B)
	move.b	#$40,($a1000D)
	clr.b	($A10013)
	clr.b	($A10019)
	clr.b	($A1001F)

	lea	color_chart(PC),a0
	move.l	#64,d0
	move.l	#0,d1
	bsr	load_colors
	lea	font(PC),a0
	move.l	#256,d0
	move.l	#0,d1
	bsr	load_tiles_1bit

	clr.w	(CURSORX)
	clr.w	(CURSORX2)
	clr.w	(COLOR)
;	move.l	#SPRITES_TABLE,a0
;	move.l	#31,d0
;sprites_init_1:
;	move.l	#$80808080,(a0)+
;	dbra	d0,sprites_init_1
;	move.l	#31,d0
;sprites_init_2:
;	clr.l	(a0)+
;	dbra	d0,sprites_init_2

	bsr	sprites_init

	move.l	#$40000003,(TEXT_PLANE)
	move.l	#$40000003,(GFX_PLANE)
	clr.w	(SCROLL_PLANE)
segacd_jump_in_program:
	move.l	#__DATASTART,a6
	move.l	#HEAPSTART,a5
	clr.l	(VBLANK_JUMP)
	clr.l	(HBLANK_JUMP)
	move.w	#$2000,sr
begin_soft_start: