//-------------------------------------------------------------------
//
//	TIPANM ENGINE v2.3 by Tebe/Madteam (24.08.2008)
//
//-------------------------------------------------------------------

___newDly	= $fc
___newFrm	= $fd
___newPos	= $fe
___newBnk	= $ff

___first	= ___newDly


.nowarn	.zpvar ad, b0, b1, b2, tmp .word	= $80
.nowarn	.zpvar rleA, rleB, rleC, bank, height, px, regA, regX, regY, time .byte


start	lda:cmp:req 20

	sei
	mva #0 $d40e
	sta $d400

	sta time

	mva #$fe $d301

	mwa #nmi $fffa

	mwa #dl2 $d402

	mva #$c0 $d40e


	mwa #anm ad

	jmp TIPANM.MAIN


//-------------------------------------------------------------------


.local	TIPANM


* ---	NEW DELAY

NEWDLY	iny
	lda (ad),y
	sta _delay+1

	jmp plus2


* ---	INC POS

INCPOS	inc px

	bne skp3


* ---	NEW FRAME

NEWFRM	lda time
_delay	cmp #delay
	bcc NEWFRM

	mva #0 time

skp3	inw ad

	jmp MAIN


* ---	NEW POSITION

NEWPOS	iny
	mva (ad),y px

	jmp plus2


* ---	NEW BANK

NEWBNK	iny
	lda (ad),y
	cmp #$ff
	bne skp

	mwa #anm-2 ad

	lda #0

skp	sta bank

plus2	adw ad #2


* ---	MAIN

MAIN	ldy #0

loop	mva #$fe $d301

	lda (ad),y
	beq INCPOS
	bpl STORE

	cmp #101|$80
	bcc RLE

	sbc #___first
	tax

	mva tbra,x branch+1

branch	bne *


* ---	Run Length Encode

RLE	and #$7f
	tax			; pozycja pionowa +1	(vertical position +1)

	iny
	lda (ad),y
	bpl newRle

oldRle	and #$7f		; repeat last RLE data
	sta height		; height -1

	bpl skpRLE


newRle	sta height		; new RLE data	(height -1)

	iny
	mva (ad),y rleA

	iny
	mva (ad),y rleB

	iny
	mva (ad),y rleC

	jmp skpRLE


* ---	STORE N BYTES

STORE	tax			; pozycja pionowa +1	(vertical position +1)

	iny
	lda (ad),y
	bmi Store1

Store0	sta height		; wysokosc -1		(height -1)

	iny
	mva (ad),y b0
	sec
	adc height
	sta b1

	iny
	mva (ad),y b0+1
	adc #0
	sta b1+1
	sta b2+1

	lda b1
	sec
	adc height
	sta b2
	scc
	inc b2+1	

	jmp skpSTORE


Store1	and #$7f		; wysokosc -1		(height -1)
	sta height

	iny
	mwa (ad),y b0

	iny
	mwa (ad),y b1

	iny
	mwa (ad),y b2


skpSTORE
	mva lcol,x _jsr+1
	mva hcol,x _jsr+2

	txa
	add height
	tax
	mva lcol+1,x tmp
	mva hcol+1,x tmp+1

	bne skp2


skpRLE
	mva lrle,x _jsr+1
	mva hrle,x _jsr+2

	txa
	add height
	tax
	mva lrle+1,x tmp
	mva hrle+1,x tmp+1

	mva rleC _y+1

skp2
;	iny
	tya
	sec			; +1
	adc ad
	sta ad
	scc
	inc ad+1
;	lda #0
;	adc ad+1
;	sta ad+1


	ift !onedic

	ldx bank
	lda @TAB_MEM_BANKS+1,x
	and #$fe
	sta $d301

	eif


	ldy	#0
	mva	(tmp),y	backup+1

	mva	#{rts}	(tmp),y


	ldx	px		; regX = PX
_y	ldy	#0		; (regY = #0) if STORE, (regY = rleC) if RLE

_jsr	jsr	$ffff

	ldy	#0
backup	mva	#0	(tmp),y

	sty	_y+1		; !!! _Y = #0

	jmp	loop		; !!! regY = #0


column	.rept 100
	mva	(b0),y	gr9+#*40,x
	mva	(b1),y	gra+#*40,x
	mva	(b2),y	grb+#*40,x
	iny
	.endr


colRle	.rept 100
	mva	rleA	gr9+#*40,x
	mva	rleB	gra+#*40,x
	tya
	sta	grb+#*40,x
	.endr


.array	TBRA [3] .byte
	[___newDly-___first] = [branch-NEWDLY+1]^$ff
	[___newFrm-___first] = [branch-NEWFRM+1]^$ff
	[___newPos-___first] = [branch-NEWPOS+1]^$ff
	[___newBnk-___first] = [branch-NEWBNK+1]^$ff
.enda

.end


//-------------------------------------------------------------------

nmi	bit $d40f
	bpl vbl

dli	sta regA
	stx regX
	sty regY

	ldx #$c0

	ldy #100

dli1	lda #$80
dli2	sta $d40a
	stx $d01b
	sta $d40a
	sta $d01b
	eor #$c0
	dey
	bne dli2

;	lda dli1+1
	eor #$c0
	sta dli1+1

	lda hdlptr+1
	eor #(>dl1)^(>dl2)
	sta hdlptr+1

	lda regA
	ldx regX
	ldy regY
	rti


vbl	phr
	sta $d40f

	inc time

ldlptr	mva <dl2 $d402
hdlptr	mva >dl2 $d403

	mva #$22 $d400

	.put = 0, 2, 4, 6, 8, $a, $c, $e, 0

	:9 mva #.get[#] $d012+#

	plr
	rti
