;*
;* mario-brothers
;*
;* (c) Thomas Jensen 1991-94
;*
;* Phase Alternated Linescan
;* Version 2.95 (DOS 3.00)
;*

;* features missing:
;* some sfx/music
;* levels 21->


;* hardware macros *

dmaconw=$dff096
intena=$dff09a
intenar=$dff01c
intreqr=$dff01e
intreq=$dff09c
vpos=$dff004
vhpos=$dff006
coplch1=$dff080
copjmp1=$dff088
coplch2=$dff084
copjmp2=$dff08a
irqvec2=$68
irqvec3=$6c
irqvec4=$70
ciaapra=$bfe001
bltcon0=$dff040
bltcon1=$dff042
bltapth=$dff050
bltbpth=$dff04c
bltcpth=$dff048
bltdpth=$dff054
bltsize=$dff058
bltamod=$dff064
bltbmod=$dff062
bltcmod=$dff060
bltdmod=$dff066
bltafwm=$dff044
bltalwm=$dff046
bpl1mod=$dff108
bpl2mod=$dff10a
bplcon0=$dff100
bplcon1=$dff102
bplcon2=$dff104

;* audio macros *

aud0lch=$dff0a0
aud0len=$dff0a4
aud0per=$dff0a6
aud0vol=$dff0a8
aud1lch=$dff0b0
aud1len=$dff0b4
aud1per=$dff0b6
aud1vol=$dff0b8
aud2lch=$dff0c0
aud2len=$dff0c4
aud2per=$dff0c6
aud2vol=$dff0c8
aud3lch=$dff0d0
aud3len=$dff0d4
aud3per=$dff0d6
aud3vol=$dff0d8


;* ROM macros *

execbase = 4
openlib = -408
closelibrary = -414
open = -30
close = -36
read = -42
allocmem = -198
forbid = -132
permit = -138
freemem = -210
ownblitter = -456
disownblitter = -462


;---------------------------------------------------------

		movem.l	d0-d7/a0-a6,-(sp)	;save regs.




;*
;* main program mario brothers
;*

start:

;*
;* initializations:
;*
		jsr	resmem
		jsr	waitdisk	;wait for disk to finish
		jsr	setupplanes
		jsr	setupaudio
		jsr	setupdma
		jsr	calclevadr
		;bra	mainloop	;skip intro


;*
;* intro-loop:
;*

;* initialize game *
introloop:	jsr	initgame

introloopa:	jsr	mvtitlescreen
		move.w	#$0f00,gamecols	;red background
		jsr	fadeinscr

;*** scroll up title text ***
intro1:		move.l	#9600,introoffset
introtlp1:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printtitletxt
		cmp.l	#0,introoffset
		beq.s	intro11
		sub.l	#80,introoffset
		bra	introtlp1


;* wait 5 secs. *
intro11:	move.l	#250,d7
introtlp11:	cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		dbf	d7,introtlp11


;*** scroll down title text ***
intro2:		jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printtitletxt
		cmp.l	#9600,introoffset
		beq.s	intro3
		add.l	#320,introoffset
		bra	intro2
intro3:


;*** scroll up hi-scores ***
		move.l	#9600,introoffset
introtlp3:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printhiscores
		cmp.l	#0,introoffset
		beq.s	intro31
		sub.l	#80,introoffset
		bra	introtlp3

intro31:

;* wait 5 secs. *
		move.l	#250,d7
introtlp31:	cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		dbf	d7,introtlp31


;*** scroll down hi-scores ***
intro4:		jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printhiscores
		cmp.l	#9600,introoffset
		beq.s	intro5
		add.l	#320,introoffset
		bra	intro4


;* select level *
selectlevel:	move.l	#80,introoffset
		bra.s	introtlp5

;*** scroll up title text2 ***
intro5:		move.l	#9600,introoffset
introtlp5:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printtitletxt2
		cmp.l	#0,introoffset
		beq.s	intro6
		sub.l	#80,introoffset
		bra	introtlp5

;* select level *
intro6:

;wait 10 secs.
		move.l	#500,d7
introtlp6:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	start1plr
		cmp.b	#1,joy1fire
		beq.L	start2plr
		move.l	#0,d0
		jsr	keytest
;is key f1?
		cmp.b	#$5f,newkey
		bne.s	intro6nk
;was key in last frame also f1?
		cmp.b	#$5f,oldkey
		beq.s	intro6nk
;set level to next
		jsr	setlevel
intro6nk:	jsr	cheatmode	;D.A.D.'s cheatmode!!!
		jsr	waitvbl
		jsr	waitvbl2
		jsr	printlevel

		dbf	d7,introtlp6


;*** scroll down title text2 ***
		move.l	#0,introoffset
introlp7:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printtitletxt2
		cmp.l	#9600,introoffset
		beq.s	intro8
		add.l	#320,introoffset
		bra	introlp7

intro8:
;* are we in cheatmode? *
		cmp.w	#1,cheatmodeon
		beq.s	cheatact

;* restart intro *
nocheat:	bra	introloopa


;* start cheatmode *
cheatact:

;*** scroll up cheattext ***
		move.l	#9600,introoffset
cheattlp1:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printcheattxt
		cmp.l	#0,introoffset
		beq.s	cheat2
		sub.l	#160,introoffset
		bra	cheattlp1


;* select start level *
cheat2:
		move.w	#5,getitimer
		move.w	#5,getitimel

;* loop *
cheattest1:	jsr	joyread

;* test fire *
		cmp.b	#1,joy2fire
		beq.L	cheat3		;fire => select #lives
		cmp.b	#1,joy1fire
		beq.L	cheat3

;* test joy right *
c1inf:		cmp.b	#7,joy2dir
		beq.s	c1ir
		cmp.b	#7,joy1dir
		beq.s	c1ir
		bra.s	c1inr

;* joy right! *
c1ir:		sub.w	#1,getitimer

;* time to change level? *
		cmp.w	#0,getitimer
		bne.L	c1ivbl
;* reset timer *
		move.w	#5,getitimer

;* set to next level *
		add.w	#1,levelnumber
		add.w	#1,levelstart
		cmp.w	#10,levelnumber
		bne.s	c1itnz
		move.w	#0,levelnumber
		add.w	#1,levelnumten
		bra	c1ivbl

;* level 50 already reached? *
c1itnz:		cmp.w	#5,levelnumten
		bne.s	c1itn50

;* set to level1 *
		move.w	#1,levelnumber
		move.w	#0,levelnumten
		move.w	#1,levelstart
c1itn50:	bra	c1ivbl


;* test joyleft *
c1inr:
;* reset right timer *
		move.w	#5,getitimer
		cmp.b	#1,joy2dir
		beq.s	c1il
		cmp.b	#1,joy1dir
		beq.s	c1il
		bra.s	c1inl

;* joy left! *
c1il:		sub.w	#1,getitimel

;* time to change level? *
		cmp.w	#0,getitimel
		bne.s	c1ivbl
;* reset timer *
		move.w	#5,getitimel

;* get prev level *
		sub.w	#1,levelnumber
		sub.w	#1,levelstart
		cmp.w	#-1,levelnumber
		bne.s	c1itnneg
		move.w	#9,levelnumber
		sub.w	#1,levelnumten
		bra	c1ivbl

;* level 1 already reached? *
c1itnneg:	cmp.w	#0,levelnumten
		bne.s	c1itn1
		cmp.w	#0,levelnumber
		bne.s	c1itn1

;* set to level50 *
		move.w	#0,levelnumber
		move.w	#5,levelnumten
		move.w	#50,levelstart
c1itn1:		bra	c1ivbl
	

c1inl:		move.w	#10,getitimel

;* wait for vbl *
c1ivbl:		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printcheattxt
		bra	cheattest1


cheat3:		jsr	docheatmode	;update startlevel etc.

;* select lives mario *
		move.w	#10,getitimer
		move.w	#10,getitimel

;* loop *
cheattest2:	jsr	joyread

;* test fire *
		cmp.b	#1,joy2fire
		beq.L	cheat4		;fire => select #lives
		cmp.b	#1,joy1fire
		beq.L	cheat4

;* test joy right *
c2inf:		cmp.b	#7,joy2dir
		beq.s	c2ir
		cmp.b	#7,joy1dir
		beq.s	c2ir
		bra.s	c2inr

;* joy right! *
c2ir:		sub.w	#1,getitimer

;* time to change letter? *
		cmp.w	#0,getitimer
		bne.L	c2ivbl
;* reset timer *
		move.w	#10,getitimer

;* add 1 life *
		add.w	#1,lives
		cmp.w	#10,lives
		bne.s	c2ivbl
		move.w	#1,lives
		bra	c2ivbl


;* test joyleft *
c2inr:
;* reset right timer *
		move.w	#10,getitimer
		cmp.b	#1,joy2dir
		beq.s	c2il
		cmp.b	#1,joy1dir
		beq.s	c2il
		bra.s	c2inl

;* joy left! *
c2il:		sub.w	#1,getitimel

;* time to change letter? *
		cmp.w	#0,getitimel
		bne.s	c2ivbl
;* reset timer *
		move.w	#10,getitimel

;* sub 1 life *
		sub.w	#1,lives
		cmp.w	#0,lives
		bne.s	c2ivbl
		move.w	#9,lives
		bra	c2ivbl
	

c2inl:		move.w	#10,getitimel

;* wait for vbl *
c2ivbl:		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printcheattxt
		bra	cheattest2



;* select lives luigi *
cheat4:
		move.w	#10,getitimer
		move.w	#10,getitimel

;* loop *
cheattest3:	jsr	joyread

;* test fire *
		cmp.b	#1,joy2fire
		beq.L	cheat5		;fire => select #lives
		cmp.b	#1,joy1fire
		beq.L	cheat5

;* test joy right *
c3inf:		cmp.b	#7,joy2dir
		beq.s	c3ir
		cmp.b	#7,joy1dir
		beq.s	c3ir
		bra.s	c3inr

;* joy right! *
c3ir:		sub.w	#1,getitimer

;* time to change letter? *
		cmp.w	#0,getitimer
		bne.L	c3ivbl
;* reset timer *
		move.w	#10,getitimer

;* add 1 life *
		add.w	#1,livesluigi
		cmp.w	#10,livesluigi
		bne.s	c3ivbl
		move.w	#1,livesluigi
		bra	c3ivbl


;* test joyleft *
c3inr:
;* reset right timer *
		move.w	#10,getitimer
		cmp.b	#1,joy2dir
		beq.s	c3il
		cmp.b	#1,joy1dir
		beq.s	c3il
		bra.s	c3inl

;* joy left! *
c3il:		sub.w	#1,getitimel

;* time to change letter? *
		cmp.w	#0,getitimel
		bne.s	c3ivbl
;* reset timer *
		move.w	#10,getitimel

;* sub 1 life *
		sub.w	#1,livesluigi
		cmp.w	#0,livesluigi
		bne.s	c3ivbl
		move.w	#9,livesluigi
		bra	c3ivbl
	

c3inl:		move.w	#10,getitimel

;* wait for vbl *
c3ivbl:		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printcheattxt
		bra	cheattest3



;*** scroll down cheattext ***
cheat5:
		move.l	#0,introoffset
cheattlp3:	jsr	joyread
		cmp.b	#1,joy2fire
		beq.L	selectlevel
		cmp.b	#1,joy1fire
		beq.L	selectlevel
		jsr	waitvbl
		jsr	waitvbl2
		jsr	shiftscr
		jsr	mvtitle1
		jsr	printcheattxt
		cmp.l	#9600,introoffset
		beq.s	cheat6
		add.l	#320,introoffset
		bra	cheattlp3

;* return to intro *

cheat6:		bra	introloopa


;-------------------------------------------------------


;*
;* start 1-player game:
;*

start1plr:	move.l	#50,d7
		move.l	#8,number
		move.l	#0,compare
		move.w	#$0,gamecols	;black backgr.
st1pfadeout:	lea	destcols(pc),a0
		move.l	a0,destcol
		lea	sourcecols(pc),a0
		move.l	a0,sourcecol
		jsr	fader		;fade out titlescreen
		jsr	waitvbl
		dbne	d7,st1pfadeout

;* 1 player *
		move.w	#0,luigiactive
		move.w	#1,marioactive

		jsr	movebackgr
		jsr	printtext1	;print player1...
		move.l	#8,number
		move.l	#0,compare
		bra	mainloop


;----------------------------------------------------

;*
;* start 2-player game:
;*

start2plr:	move.l	#50,d7
		move.l	#8,number
		move.l	#0,compare
		move.w	#0,gamecols
st2pfadeout:	lea	destcols(pc),a0
		move.l	a0,destcol
		lea	sourcecols(pc),a0
		move.l	a0,sourcecol
		jsr	fader		;fade out titlescreen
		jsr	waitvbl
		dbne	d7,st2pfadeout

;* 2 players *
		move.w	#1,luigiactive
		move.w	#1,marioactive

		jsr	movebackgr
		jsr	printtext1	;print player1...
		jsr	printtext2	;print player2
		move.l	#8,number
		move.l	#0,compare
		bra	mainloop


;-----------------------------------------------------

;*
;* check for cheatmode found:
;*
;* cheat=r,l,u,d,r
;*

cheatmode:	move.w	cheatcnt,d0
		cmp.w	#0,d0
		beq.s	cmode0
		cmp.w	#2,d0
		beq.s	cmode1
		cmp.w	#4,d0
		beq.s	cmode2
		cmp.w	#6,d0
		beq.s	cmode3
		cmp.w	#8,d0
		beq.s	cmode4
		bra.s	cmodeneut
		rts

cmode0:		cmp.b	#JOYRIGHT,joy2dir
		bne.s	cmode0ex
		add.w	#1,cheatcnt
cmode0ex:	rts

cmode1:		cmp.b	#JOYLEFT,joy2dir
		bne.s	cmode1ex
		add.w	#1,cheatcnt
cmode1ex:	rts

cmode2:		cmp.b	#JOYUP,joy2dir
		bne.s	cmode2ex
		add.w	#1,cheatcnt
cmode2ex:	rts

cmode3:		cmp.b	#JOYDOWN,joy2dir
		bne.s	cmode3ex
		add.w	#1,cheatcnt
cmode3ex:	rts

cmode4:		cmp.b	#JOYRIGHT,joy2dir
		bne.s	cmode4ex
		move.w	#1,cheatmodeon	;we are in cheatmode!!!
cmode4ex:	rts

;* joy=0? *
cmodeneut:	cmp.b	#JOYNONE,joy2dir
		bne.s	cmodeneutex
		add.w	#1,cheatcnt
cmodeneutex:	rts



cheatcnt:	dc.w	0	;how far in cheatmode?
cheatmodeon:	dc.w	0	;0=no cheatmode


;*
;* do cheatmode:
;*

docheatmode:	move.w	#0,cheatcnt	;reset cheatmode
		move.w	#0,cheatmodeon

		lea	levaddrs,a0
		move.l	#0,d0
		move.w	levelstart,d0
		sub.w	#1,d0
		muls	#4,d0
		add.l	d0,a0

		move.l	(a0),levelno
		rts



;-----------------------------------------------------



;***** main-loop in-game *****

mainloop:	
		jsr	joyread		;read joy1/2
		jsr	keytest		;read keyboard
		jsr	pausekey	;check pausekey pressed
		jsr	quitkey		;check quitkey pressed
		jsr	exittodoskey	;check exittodoskey pressed
		jsr	colcheck2	;collisioncheck mario/backgr.
		jsr	colcheckl2	;ditto luigi
		jsr	nmecol2		;ditto nmes
		jsr	contmario	;control mario
		jsr	contluigi	;control luigi
		jsr	contnme		;control enemies
		jsr	correct		;correct x,y pos. m/l
		jsr	nmecorr		;ditto nme
		jsr	checksafe	;check if no coll. can occur
		jsr	contaudio	;control sfx
		jsr	bobcollision	;collision between sprites
		jsr	powcontrol	;control pow
		jsr	levelcontrol	;control levels
		jsr	pausecont	;pause control
		jsr	gamecontrol	;gamelogic control
		jsr	calcsprite	;calculate sprite for mario
		jsr	calcspriteluigi	;ditto luigi
		jsr	waitvbl		;wait for vb
		jsr	shiftscr	;switch screen
		jsr	jumpscreen	;jump screen when pow hit
		jsr	restore		;restore backgroundgfx
		jsr	printtime	;print time rem. in bonuslevel
		jsr	printlevig	;print level in-game
		jsr	printbob	;print sprites
		jsr	printplatform	;print platforms when needed
		jsr	fadein		;fadein for 1 sec.
		jsr	fadeout		;fadeout for 1 sec.
		jsr	calcscores	;calculate scores
		jsr	printscores	;print points scored
		jsr	printlives	;print # of lives left
		jsr	printgameover	;print game over if needed

eogtest:	;cmp.b	#4,joy2dir	;joyup?
		;beq.s	exit		;exit to shell
		cmp.w	#1,extodos
		beq.s	exit		;exit to dos!
		cmp.w	#1,quit
		beq.L	introloop
		cmp.w	#1,gameover	;game over?
		bne.L	mainloop

;game is finished!
		;jsr	cleanupgame
		jsr	sethitable	;new hiscores?
		jsr	getinitials	;get initials
		bra	introloop
; copperlist is still running in introloop


;* exit to shell *

exit:		jsr	freememory	;free reserved memory
		jsr	pulldowndma	;restore system
		movem.l	(sp)+,d0-d7/a0-a6	;restore regs.
		rts



;------------------------------------------------------

;*
;* reservation and loading into memory v1.1:
;* use a nulladr. if no loading is req.
;*


size1 = $a000	;size of memory to reserve (320*256*4 screen)
size2 = $2800	;(320*256*1 screen)
size3 = 15360	;platformscreen
sizes1 = 702	;702 bytes for sample1 (run1)
sizes2 = 2088	;death1
sizes3 = 9536	;deathfall
sizes4 = 1688	;jump
sizes5 = 1128	;nmelaunch

	

;*
;* reservation code:
;*

resmem:
;* open dos library *
		move.l	execbase,a6
		lea	dosname,a1	;library name
		moveq	#0,d0		;any version
		jsr	openlib(a6)

;* save base addr. *
		move.l	d0,dosbase	

		move.l	#0,curname
		move.l	#size1,cursize
		bsr	reserve		;reserve memory
		move.l	curadr,screen1	;1. gamescreen

		move.l	#0,curname
		move.l	#size1,cursize
		bsr	reserve
		move.l	curadr,screen2	;2. gamescreen

		lea	file1(pc),a0
		move.l	a0,curname
		move.l	#size1,cursize
		bsr	reserve
		move.l	curadr,backgr	;backgr. pic.

		lea	file2(pc),a0
		move.l	a0,curname
		move.l	#size1,cursize
		bsr	reserve
		move.l	curadr,title	;titlescreen

		lea	file15(pc),a0
		move.l	a0,curname
		move.l	#size3,cursize
		bsr	reserve
		move.l	curadr,platform	;platform gfx

		lea	file3(pc),a0
		move.l	a0,curname
		move.l	#size1,cursize
		bsr	reserve
		move.l	curadr,gfx1	;just gfx

		lea	file4(pc),a0
		move.l	a0,curname
		move.l	#size1,cursize
		bsr	reserve
		move.l	curadr,gfx2

		lea	file5(pc),a0
		move.l	a0,curname
		move.l	#size1,cursize
		bsr	reserve
		move.l	curadr,gfx3

		lea	file6(pc),a0
		move.l	a0,curname
		move.l	#size2,cursize
		bsr	reserve
		move.l	curadr,mask1	;mask data

		lea	file7(pc),a0
		move.l	a0,curname
		move.l	#size2,cursize
		bsr	reserve
		move.l	curadr,mask2

		lea	file8(pc),a0
		move.l	a0,curname
		move.l	#size2,cursize
		bsr	reserve
		move.l	curadr,mask3

		lea	file10(pc),a0
		move.l	a0,curname
		move.l	#sizes1,cursize
		bsr	reserve
		move.l	curadr,sample1	;sound-samples

		lea	file11(pc),a0
		move.l	a0,curname
		move.l	#sizes2,cursize
		bsr	reserve
		move.l	curadr,sample2

		lea	file12(pc),a0
		move.l	a0,curname
		move.l	#sizes3,cursize
		bsr	reserve
		move.l	curadr,sample3

		lea	file13(pc),a0
		move.l	a0,curname
		move.l	#sizes4,cursize
		bsr	reserve
		move.l	curadr,sample4

		lea	file14(pc),a0
		move.l	a0,curname
		move.l	#sizes5,cursize
		bsr	reserve
		move.l	curadr,sample5

;* close dos library *
		move.l	dosbase,a1
		move.l	execbase,a6
		jsr	closelibrary(a6)
		rts



;* main reservation routine *

;* reserve space *
reserve:	move.l	execbase,a6
		move.l	cursize,d0	;size of memory to reserve
		moveq	#2,d1		;lower 512K chipmem
		jsr	allocmem(a6)	;reserve
		move.l	d0,curadr	;save adr to caller
		move.l	d0,d6		;save adr.
		beq.s	error		;return on error
		cmp.l	#0,curname
		bne.s	load		;load
		rts			;do not load

;* load file *
load:		move.l	dosbase,a6	;get dos base
		move.l	curname,a0	;filename now in a0
		move.l	a0,d1
		move.l	#1005,d2	;mode = old (file exists)
		jsr	open(a6)
		move.l	d0,d7		;save filevalue
		beq.s	error		;return on error
		move.l	d7,d1
		move.l	d6,d2
		move.l	cursize,d3	;size of file
		jsr	read(a6)
		tst.l	d0
		bmi.s	error		;d0=-1 <= error
		move.l	d7,d1
		jsr	close(a6)
		move.l	#0,d0
		rts			;return to caller


error:		move.w	#$0f00,$dff180
		rts
dosname:	dc.b	"dos.library",0
		even
cursize:	dc.l	0		;size of current file
curname:	dc.l	0		;addr. of current name
curadr:		dc.l	0		;addr. of res. mem. for cur. file


;*
;* filenames:
;*

file1:		dc.b	"mariopic2",0
		even
file2:		dc.b	"mariotitle",0
		even
file3:		dc.b	"mariosuse",0
		even
file4:		dc.b	"mariosuse2",0
		even
file5:		dc.b	"mariosuse3",0
		even
file6:		dc.b	"mariosmask",0
		even
file7:		dc.b	"mariosmask2",0
		even
file8:		dc.b	"mariosmask3",0
		even
file10:		dc.b	"ms_run1_510_raw",0
		even
file11:		dc.b	"ms_death1_510_raw",0
		even
file12:		dc.b	"ms_deathfall_510_raw",0
		even
file13:		dc.b	"ms_jump_510_raw",0
		even
file14:		dc.b	"ms_nmelaunch1_510_raw",0
		even
file15:		dc.b	"marioplat1",0
		even


;* addrs. of reserved/loaded files *

screen1:	dc.l	0
screen2:	dc.l	0
title:		dc.l	0
backgr:		dc.l	0
platform:	dc.l	0
gfx1:		dc.l	0
gfx2:		dc.l	0
gfx3:		dc.l	0
mask1:		dc.l	0
mask2:		dc.l	0
mask3:		dc.l	0
sample1:	dc.l	0
sample2:	dc.l	0
sample3:	dc.l	0
sample4:	dc.l	0
sample5:	dc.l	0
		


;-------------------------------------------------

;*
;* release reserved memory:
;*
;* release upon exiting
;* to shell.
;*

freememory:	move.l	title,a1
		move.l	#size1,d0
		move.l	execbase,a6
		jsr	freemem(a6)

		move.l	screen1,a1
		move.l	#size1,d0
		jsr	freemem(a6)

		move.l	screen2,a1
		move.l	#size1,d0
		jsr	freemem(a6)

		move.l	backgr,a1
		move.l	#size1,d0
		jsr	freemem(a6)

		move.l	platform,a1
		move.l	#size3,d0
		jsr	freemem(a6)

		move.l	gfx1,a1
		move.l	#size1,d0
		jsr	freemem(a6)

		move.l	gfx2,a1
		move.l	#size1,d0
		jsr	freemem(a6)

		move.l	gfx3,a1
		move.l	#size1,d0
		jsr	freemem(a6)

		move.l	mask1,a1
		move.l	#size2,d0
		jsr	freemem(a6)

		move.l	mask2,a1
		move.l	#size2,d0
		jsr	freemem(a6)

		move.l	mask3,a1
		move.l	#size2,d0
		jsr	freemem(a6)

		move.l	sample1,a1
		move.l	#sizes1,d0
		jsr	freemem(a6)

		move.l	sample2,a1
		move.l	#sizes2,d0
		jsr	freemem(a6)

		move.l	sample3,a1
		move.l	#sizes3,d0
		jsr	freemem(a6)

		move.l	sample4,a1
		move.l	#sizes4,d0
		jsr	freemem(a6)

		move.l	sample5,a1
		move.l	#sizes5,d0
		jsr	freemem(a6)

		rts


;-------------------------------------------------------

;*
;* RAW key test v1.2:
;* (c) Thomas Jensen 1993
;*

ciaasdr = $bfec01

keytest:	move.b	newkey,oldkey	;save old code
		move.b	ciaasdr,newkey	;RAW-code in newkey
		rts

oldkey:		dc.b	0
even
newkey:		dc.b	0
even


;-----------------------------------------------------

;*
;* calculate addresses of levels:
;*

calclevadr:
		lea	levaddrs,a1

		lea	level1,a0
		move.l	a0,(a1)+
		lea	level2,a0
		move.l	a0,(a1)+
		lea	level3,a0
		move.l	a0,(a1)+
		lea	level4,a0
		move.l	a0,(a1)+
		lea	level5,a0
		move.l	a0,(a1)+
		lea	level6,a0
		move.l	a0,(a1)+
		lea	level7,a0
		move.l	a0,(a1)+
		lea	level8,a0
		move.l	a0,(a1)+
		lea	level9,a0
		move.l	a0,(a1)+
		lea	level10,a0
		move.l	a0,(a1)+
		lea	level11,a0
		move.l	a0,(a1)+
		lea	level12,a0
		move.l	a0,(a1)+
		lea	level13,a0
		move.l	a0,(a1)+
		lea	level14,a0
		move.l	a0,(a1)+
		lea	level15,a0
		move.l	a0,(a1)+
		lea	level16,a0
		move.l	a0,(a1)+
		lea	level17,a0
		move.l	a0,(a1)+
		lea	level18,a0
		move.l	a0,(a1)+
		lea	level19,a0
		move.l	a0,(a1)+
		lea	level20,a0
		move.l	a0,(a1)+
		lea	level21,a0
		move.l	a0,(a1)+
		lea	level22,a0
		move.l	a0,(a1)+
		lea	level23,a0
		move.l	a0,(a1)+
		lea	level24,a0
		move.l	a0,(a1)+
		lea	level25,a0
		move.l	a0,(a1)+
		lea	level26,a0
		move.l	a0,(a1)+
		lea	level27,a0
		move.l	a0,(a1)+
		lea	level28,a0
		move.l	a0,(a1)+
		lea	level29,a0
		move.l	a0,(a1)+
		lea	level30,a0
		move.l	a0,(a1)+
		lea	level31,a0
		move.l	a0,(a1)+
		lea	level32,a0
		move.l	a0,(a1)+
		lea	level33,a0
		move.l	a0,(a1)+
		lea	level34,a0
		move.l	a0,(a1)+
		lea	level35,a0
		move.l	a0,(a1)+
		lea	level36,a0
		move.l	a0,(a1)+
		lea	level37,a0
		move.l	a0,(a1)+
		lea	level38,a0
		move.l	a0,(a1)+
		lea	level39,a0
		move.l	a0,(a1)+
		lea	level40,a0
		move.l	a0,(a1)+
		lea	level41,a0
		move.l	a0,(a1)+
		lea	level42,a0
		move.l	a0,(a1)+
		lea	level43,a0
		move.l	a0,(a1)+
		lea	level44,a0
		move.l	a0,(a1)+
		lea	level45,a0
		move.l	a0,(a1)+
		lea	level46,a0
		move.l	a0,(a1)+
		lea	level47,a0
		move.l	a0,(a1)+
		lea	level48,a0
		move.l	a0,(a1)+
		lea	level49,a0
		move.l	a0,(a1)+
		lea	level50,a0
		move.l	a0,(a1)+
		rts


;* store addresses here *
levaddrs:	blk.l	51,0

;-----------------------------------------------------

;*
;* set level and update adr.:
;*

setlevel:	bsr	setlevelnum
		bsr	setleveladr
		rts

;* what is current level? *
setlevelnum:	cmp.w	#1,levelnumber
		beq.s	setlevnum4
		cmp.w	#4,levelnumber
		beq.s	setlevnum6
		cmp.w	#9,levelnumber
		beq.s	setlevnum16
		cmp.w	#1,levelnumten
		beq.s	setlevnum1

;* level is 6; set to 9 *
setlevnum9:	move.w	#9,levelnumber
		rts
setlevnum1:	move.w	#0,levelnumten
		move.w	#1,levelnumber
		rts
setlevnum4:	move.w	#4,levelnumber
		rts
setlevnum6:	move.w	#6,levelnumber
		rts
setlevnum16:	move.w	#6,levelnumber
		move.w	#1,levelnumten
		rts


;* set adr. of startlevel *
setleveladr:	cmp.w	#6,levelnumber
		beq.s	setleva616
		cmp.w	#1,levelnumber
		beq.s	setleva1
		cmp.w	#4,levelnumber
		beq.L	setleva4

setleva9:	lea	level9,a0	;set to iceman-level
		move.l	a0,levelno
		move.l	gfx2,a1
		add.l	#7968,a1
		move.l	a1,levelgfx
		rts
setleva616:	cmp.w	#0,levelnumten
		beq.L	setleva6
setleva16:	lea	level16,a0	;set to $-level
		move.l	a0,levelno
		move.l	gfx1,a1
		add.l	#5316,a1
		move.l	a1,levelgfx
		rts
setleva1:	lea	level1,a0	;set to turtle-level
		move.l	a0,levelno
		move.l	gfx2,a1
		add.l	#40,a1
		move.l	a1,levelgfx
		rts
setleva4:	lea	level4,a0	;set to lobster-level
		move.l	a0,levelno
		move.l	gfx3,a1
		add.l	#40,a1
		move.l	a1,levelgfx
		rts
setleva6:	lea	level6,a0	;set to fly-level
		move.l	a0,levelno
		move.l	gfx3,a1
		add.l	#5320,a1
		move.l	a1,levelgfx
		rts


;---------------------------------------------------


;*
;* print current level on-screen:
;*

printlevel:	move.l	#8018,a3	;line 200
		move.l	#8018,a4	;pos 9
		move.w	#36,d1		;amod
		move.w	#36,d3		;dmod
		move.w	#$542,d5	;bltsize (21*2)
		move.l	levelgfx,a1
		bsr	printtxt2	;print current level
		rts

levelgfx:	dc.l	0		;gfx for current level


;-------------------------------------------------------------

;*
;* move titlescreen to gamescreens:
;*

mvtitlescreen:	move.l	title,a0
		move.l	screen1,a1
		move.l	screen2,a2
		move.l	#$27ff,d0	;4 bitplanes (4*$2800 bytes)
mvtitlesc:	move.l	(a0),(a1)+
		move.l	(a0)+,(a2)+
		dbf	d0,mvtitlesc
		rts

;-------------------------------------------------------

;*
;* copy background to game-screens:
;*

movebackgr:	move.l	backgr,a0
		move.l	screen1,a1
		move.l	screen2,a2
		move.l	#$27ff,d0
movebackg:	move.l	(a0),(a1)+
		move.l	(a0)+,(a2)+
		dbf	d0,movebackg
		rts

;----------------------------------------------------

;*
;* copy titlepic to ~game-screen:
;*

mvtitle1:	move.l	title,a0	;src
		move.l	screen,a3	;dest
		move.w	#0,d2		;amod
		move.w	#0,d4		;dmod
		move.w	#$4014,d5	;blitsize 320*20 words

		bsr	rest		;copy!
		rts



;---------------------------------------------------------

;*
;* clear screen:
;*

clearscreen:	move.l	screen1,a1
		move.l	screen2,a2
		move.l	#$27ff,d0
clearsc:	move.l	#$0,(a1)+
		move.l	#$0,(a2)+
		dbf	d0,clearsc
		rts



;-------------------------------------------------------------

;*
;* timer example CIA-B timerA
;*
;* (c) Thomas Jensen 1993
;*

ciabpra = $bfd000
ciabtalo = $bfd400
ciabtahi = $bfd500
ciabicr = $bfdd00
ciabcra = $bfde00


waittime = $ffff	;wait = ca. 0.0923834 secs.

waitdisk:
timer:		move.b	ciabcra,d0		;cont. reg. A
		and.b	#%11000000,d0
		or.b	#%00001000,d0		;oneshot-mode
		move.b	d0,ciabcra
		move.b	#%01111111,ciabicr	;clear all 8520 ints.
		move.b	#waittime&$ff,ciabtalo	;timerlow
		move.b	#waittime/256,ciabtahi	;timerhigh
bwait:		move.l	#55,d0			;ca. 5 sec.
busywait:	btst	#0,ciabicr		;timer expired?
		beq.s	busywait		;no, wait
		bset	#0,ciabcra		;restart timer
		dbf	d0,busywait
		bclr	#0,ciabcra		;stop timer
		rts


;---------------------------------------------------------

;*
;* set planepointers to titlescreen (moved into screen1):
;*

setupplanes:	move.l	screen1,d0	;adr. of screen1
		move.l	screen1,screen
		move.w	d0,plane1+6	;set lower word
		swap	d0
		move.w	d0,plane1+2	;set upper word
		swap	d0
		add.l	#$2800,d0	;next plane (PAL)
		move.w	d0,plane2+6
		swap	d0
		move.w	d0,plane2+2
		swap	d0
		add.l	#$2800,d0
		move.w	d0,plane3+6
		swap	d0
		move.w	d0,plane3+2
		swap	d0
		add.l	#$2800,d0
		move.w	d0,plane4+6
		swap	d0
		move.w	d0,plane4+2
		rts

;-----------------------------------------------------

;*
;* initialize dma-channels, irq, start copper:
;*

setupdma:
;* open graphics library *
		move.l	execbase,a6
		lea	gfxname,a1	;library name
		moveq	#0,d0		;any version
		jsr	openlib(a6)

;* save base addr. *
		move.l	d0,gfxbase

;* get blitter *
		move.l	gfxbase,a6
		jsr	ownblitter(a6)

;* save systemcopper *
		move.l	execbase,a6
		move.l	156(a6),a0	;gfxbase
		add.l	#$32,a0		;system copper
		move.l	(a0),oldcopper	;save adr. of systemcopperlist
		move.w	#%0000000000101111,dmaconw
		move.l	#copper,(a0)	;insert my copperlist
		;move.w	d0,copjmp1	;force jump to my copperlist
		move.w	#%1000000111010000,dmaconw
		move.l	a0,copperaddr	;save copperaddr

;* stop multitasking *
		move.l	execbase,a6
		jsr	forbid(a6)

;* save ints. *
		move.w	intenar,oldintena

;* start my level 3 *
		;move.l	irqvec3,oldirq3	;save old interrupt
		;move.l	#myirq3,irqvec3	;start my int.

;* start my level 4 *
		move.w	#%0000011110000000,intena ;disallow aud. int.
		move.l	irqvec4,oldirq4	;save old vector
		move.l	#myirq4,irqvec4	;start my level4 int.

;* start aud. ints. and dma *
		move.w	#%1000011110000000,intena
		move.w	#%1000000000001111,dmaconw

;* set blitter priority over cpu *
		move.w	#%1000010000000000,dmaconw

;* stop timer ints. *
		;move.w	#%0000000000001000,intena
		rts




;---------------------------------------------------

;*
;* pull down dma,irq etc.:
;*

pulldowndma:
;* start multitask *
		move.l	execbase,a6
		jsr	permit(a6)	;start multitasking
		move.w	#$8030,dmaconw

;* stop audio ints. and dma *
		move.w	#$f,dmaconw
		move.w	#%0000011110000000,intena ;disallow aud. int.

;* restore systemcopper *
		move.l	copperaddr,a0
		move.l	oldcopper,(a0) ;restore systemcopper

;* restore int. vectors *
		;move.l	oldirq3,irqvec3
		move.l	oldirq4,irqvec4

;* set cpu priority over blitter *
		move.w	#%0000010000000000,dmaconw

;* start timer ints. *
		;move.w	#%1000000000001000,intena

;* release blitter *
		move.l	gfxbase,a6
		jsr	disownblitter(a6)

;* close graphics library *
		move.l	gfxbase,a1
		move.l	execbase,a6
		jsr	closelibrary(a6)
		rts


oldirq3:	dc.l	0
oldirq4:	dc.l	0
oldintena:	dc.w	0
oldcopper:	dc.l	0
copperaddr:	dc.l	0
gfxbase:	dc.l	0
dosbase:	dc.l	0
gfxname:	dc.b	"graphics.library",0,0
even

;-----------------------------------------------------

;*
;* set up audio for channel 0-3:
;*
;* volume = 0 (no sound yet!)
;* period = 510 (7 kHz)
;* length = 100 (dummy value)
;* addr.  = sample1 (1. samplefile)
;*

setupaudio:

;* channel0 *
		move.l	sample1,a0
		move.l	a0,aud0lch
		move.w	#100,aud0len
		move.w	#510,aud0per
		move.w	#0,aud0vol

;* channel1 *
		move.l	sample1,a0
		move.l	a0,aud1lch
		move.w	#100,aud1len
		move.w	#510,aud1per
		move.w	#0,aud1vol
;* channel2 *
		move.l	sample1,a0
		move.l	a0,aud2lch
		move.w	#100,aud2len
		move.w	#510,aud2per
		move.w	#0,aud2vol
;* channel3 *
		move.l	sample1,a0
		move.l	a0,aud3lch
		move.w	#100,aud3len
		move.w	#510,aud3per
		move.w	#0,aud3vol
		rts



;------------------------------------------------------


;*
;* waiting for vertical blank:
;*

waitvbl:	cmp.b	#$0,vhpos
		bne.s	waitvbl
		rts

waitvbl2:	cmp.b	#$1,vhpos
		bne.s	waitvbl2
		rts


;-------------------------------------------------------


;*
;* use screen1/screen2 as screen:
;*
;* screen contains the adress of the
;* screen which the printroutine
;* should write to. the adr. of the
;* gamescreen is located in the
;* copperlists planepointers.
;*

shiftscr:	eor.w	#1,screenf	;switch screen
		beq.s	unevenf2
		move.l	screen2,screen
		move.l	screen1,d0
		move.w	d0,plane1+6	;set lower word
		swap	d0
		move.w	d0,plane1+2	;set upper word
		swap	d0
		add.l	#$2800,d0	;next plane
		move.w	d0,plane2+6
		swap	d0
		move.w	d0,plane2+2
		swap	d0
		add.l	#$2800,d0
		move.w	d0,plane3+6
		swap	d0
		move.w	d0,plane3+2
		swap	d0
		add.l	#$2800,d0
		move.w	d0,plane4+6
		swap	d0
		move.w	d0,plane4+2
		rts
unevenf2:	move.l	screen1,screen
		move.l	screen2,d0
		move.w	d0,plane1+6	;set lower word
		swap	d0
		move.w	d0,plane1+2	;set upper word
		swap	d0
		add.l	#$2800,d0	;next plane
		move.w	d0,plane2+6
		swap	d0
		move.w	d0,plane2+2
		swap	d0
		add.l	#$2800,d0
		move.w	d0,plane3+6
		swap	d0
		move.w	d0,plane3+2
		swap	d0
		add.l	#$2800,d0
		move.w	d0,plane4+6
		swap	d0
		move.w	d0,plane4+2
		rts


;-------------------------------------------------------

;*
;* screen-fader v1.0:
;* fade source until
;* equal to dest
;*

fader:		move.l	number,d0
fadelp1:	move.l	#15,d1
		move.l	#0,d5
		move.l	sourcecol,a0
fadelp3:	move.l	#$f,d2
		rol.w	d0,d2
		lea	compare(pc),a1
		moveq	#1,d3
fadelp2:	move.w	(a0,d5),d4
		and.w	d2,d4		;isolate act. color
		ror.w	d0,d4
		move.l	destcol,a0
		move.w	d4,(a1)+
		dbra	d3,fadelp2

		move.l	sourcecol,a0
		subq.l	#4,a1
		move.w	(a1),d3
		cmp.w	2(a1),d3	;compare the 2 colors
		beq.s	fadeend
		blt.s	fadenext
		subq.w	#1,d3
		bra	fadeend
fadenext:	addq.w	#1,d3

fadeend:	rol.w	d0,d3
		eor.w	#$ffff,d2
		and.w	d2,(a0,d5)
		or.w	d3,(a0,d5)
		addq.l	#2,d5		;next color
		dbra	d1,fadelp3
		subq.l	#4,d0
		move.l	d0,number		
		cmp.l	#-4,d0
		bne.s	fadenorre
		move.l	#8,number	;fade again

;* put colors in copperlist *
		move.l	#15,d0
		move.l	sourcecol,a0
		lea	colors(pc),a1
fademvlp:	move.w	(a0)+,2(a1)
		addq.w	#4,a1
		dbra	d0,fademvlp
fadenorre:	rts

number:		dc.l	8

compare:	dc.w	0,0

sourcecol:	dc.l	0
destcol:	dc.l	0

;* fade source *
sourcecols:	dc.w	$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000

;* fade dest *
destcols:	dc.w	$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000
		dc.w	$0000,$0000,$0000,$0000

;* colors in-game *
gamecols:	dc.w	$0000,$0fff,$0f00,$0c00
		dc.w	$0900,$088f,$033d,$0009
		dc.w	$0ff0,$0e91,$0000,$0fca
		dc.w	$0a52,$00d0,$03a0,$0d0f

;----------------------------------------------------


;*
;* fade-in screen:
;*

fadeinscr:	move.l	#50,d7
		move.l	#8,number
		move.l	#0,compare
fadeinsc:	lea	gamecols(pc),a0
		move.l	a0,destcol
		lea	sourcecols(pc),a0
		move.l	a0,sourcecol
		jsr	fader		;fade in screen
		jsr	waitvbl
		dbne	d7,fadeinsc
		rts


;-----------------------------------------------------

;*
;* fade-out screen:
;*

fadeoutscr:	move.l	#50,d7
		move.l	#8,number
		move.l	#0,compare
fadeoutsc:	lea	destcols(pc),a0
		move.l	a0,destcol
		lea	sourcecols(pc),a0
		move.l	a0,sourcecol
		jsr	fader		;fade out screen
		jsr	waitvbl
		dbne	d7,fadeoutsc
		move.l	#8,number
		move.l	#0,compare
		rts

;-----------------------------------------------------

;*
;* fade in gamescreen:
;*
;* fade for 1 sec. 50 frames
;*

fadein:		cmp.w	#0,fadeincnt
		bne.s	fadeings
		rts
fadeings:	lea	gamecols(pc),a0
		move.l	a0,destcol
		lea	sourcecols(pc),a0
		move.l	a0,sourcecol
		bsr	fader		;fade in gamescreen
		sub.w	#1,fadeincnt
fadeinex:	rts

fadeincnt:	dc.w	50


;----------------------------------------------------


;*
;* fade out gamescreen:
;*
;* fade for 1 sec. 50 frames
;*

fadeout:	cmp.w	#0,fadeoutcnt
		bne.s	fadeoutgs
		rts
fadeoutgs:	move.w	#0,gameover
		lea	destcols(pc),a0
		move.l	a0,destcol
		lea	sourcecols(pc),a0
		move.l	a0,sourcecol
		bsr	fader		;fade in gamescreen
		sub.w	#1,fadeoutcnt
		cmp.w	#0,fadeoutcnt
		bne.s	fadeoutex
		move.w	#1,gameover	;gameover
fadeoutex:	rts

fadeoutcnt:	dc.w	0



;--------------------------------------------

;*
;* level-3 interrupt-routine:
;*

myirq3:
;* save regs. *
		;movem.l	d0-d7/a0-a6,-(sp)
		move.l	d0,-(sp)

		move.w	intreqr,d0

;* is irq vert. blank? *
		btst	#5,d0
		bne.s	vblirq3

;* clear remaining irqs *
		move.w	#%0000000001010000,intreq
		bra.s	outirq3
	
;* vertical blank clear irq. *
vblirq3:	move.w	#%0000000000100000,intreq
		move.l	#copper,coplch2	;reload copper addr.
		move.w	#0,copjmp2	;force jump

;* restore regs. *
outirq3:	;movem.l	(sp)+,d0-d7/a0-a6
		move.l	(sp)+,d0
		rte

;--------------------------------------------

;*
;* level-4 interrupt-routine:
;*

myirq4:
;* save regs. *
		;movem.l	d0-d7/a0-a6,-(sp)
		move.l	d0,-(sp)
		move.w	intreqr,d0

		bsr	testch0
		bsr	testch1
		bsr	testch2
		bsr	testch3
		bra	mainirq4

;* channel 0 aud. block done? *
testch0:	btst	#7,d0
		bne.s	abd0
		rts

;* channel 1 aud. block done? *
testch1:	btst	#8,d0
		bne.L	abd1
		rts

;* channel 2 aud. block done? *
testch2:	btst	#9,d0
		bne.L	abd2
		rts

;* channel 3 aud. block done? *
testch3:	btst	#10,d0
		bne.L	abd3
		rts


;* channel 0 audio block done *
abd0:		move.w	#1,abd0flag	;ch.0 audio block done!
		cmp.w	#0,audch0w
		beq.s	abd0offw
		cmp.w	#1,audch0w
		beq.s	abd0runw
		cmp.w	#2,audch0w
		beq.s	abd0jumpw
		cmp.w	#3,audch0w
		beq.s	abd0dead1w

		cmp.w	#4,audch0w
		beq.s	abd0deadfw
		rts
;stop int.
abd0offw:	move.w	#0,aud0vol
		move.w	#100,aud0len
		rts

abd0runw:	move.w	#64,aud0vol
		rts

abd0jumpw:	move.w	#0,audch0w	;turn off next time
		move.w	#64,aud0vol
		rts

abd0dead1w:	move.w	#0,audch0w	;turn off next time
		move.w	#64,aud0vol
		rts

abd0deadfw:	move.w	#0,audch0w	;turn off next time
		move.w	#64,aud0vol
		rts

abd0c:		rts


;* channel 1 audio block done *
abd1:		move.w	#1,abd1flag	;ch.1 audio block done!
		cmp.w	#0,audch1w
		beq.s	abd1offw
		cmp.w	#1,audch1w
		beq.s	abd1runw
		cmp.w	#2,audch1w
		beq.s	abd1jumpw
		cmp.w	#3,audch1w
		beq.s	abd1dead1w

		cmp.w	#4,audch1w
		beq.s	abd1deadfw
		rts
;stop sound
abd1offw:	move.w	#0,aud1vol
		move.w	#100,aud1len
		rts

abd1runw:	move.w	#64,aud1vol
		rts

abd1jumpw:	move.w	#0,audch1w	;turn off next time
		move.w	#64,aud1vol
		rts

abd1dead1w:	move.w	#0,audch1w	;turn off next time
		move.w	#64,aud1vol
		rts

abd1deadfw:	move.w	#0,audch1w	;turn off next time
		move.w	#64,aud1vol
		rts



;* channel 2 audio block done *
abd2:		cmp.w	#0,audch2w
		beq.s	abd2offw
		cmp.w	#1,audch2w
		beq.s	abd2launchw
		rts
;stop int.
abd2offw:	move.w	#0,aud2vol
		move.w	#1000,aud2len
		rts

abd2launchw:	move.w	#0,audch2w	;turn off next time
		move.w	#64,aud2vol
		rts

;* channel 3 audio block done *
abd3:		cmp.w	#0,audch3w
		beq.s	abd3offw
		cmp.w	#1,audch3w
		beq.s	abd3launchw
		rts
;stop sound
abd3offw:	move.w	#0,aud3vol
		move.w	#1000,aud3len
		rts

abd3launchw:	move.w	#0,audch3w	;turn off next time
		move.w	#64,aud3vol
		rts



;* main level 4 *

mainirq4:
;* clear AUD0-3 *
		move.w	#%0000011110000000,intreq
		
outirq4:
;* restore regs. *
		;movem.l	(sp)+,d0-d7/a0-a6
		move.l	(sp)+,d0
		rte



audch0on:	dc.w	0	;channel on?
audch1on:	dc.w	0
audch2on:	dc.w	0
audch3on:	dc.w	0

audch0w:	dc.w	0	;what effect is wanted next?
audch1w:	dc.w	0
audch2w:	dc.w	0
audch3w:	dc.w	0

abd0flag:	dc.w	0	;audio block done?
abd1flag:	dc.w	0

maudrun:	dc.w	0
laudrun:	dc.w	0

;----------------------------------------------------

;*
;* copper-list:
;*

copper:	
colors:		dc.w	$0180,$0,$0182,$0,$0184,$0,$0186,$0
		dc.w	$0188,$0,$018a,$0,$018c,$0,$018e,$0
		dc.w	$0190,$0,$0192,$0,$0194,$0,$0196,$0
		dc.w	$0198,$0,$019a,$0,$019c,$0,$019e,$0
;		dc.w	$01a0,0,$01a2,0,$01a4,0,$01a6,0
;		dc.w	$01a8,0,$01aa,0,$01ac,0,$01ae,0
;		dc.w	$01b0,0,$01b2,0,$01b4,0,$01b6,0
;		dc.w	$01b8,0,$01ba,0,$01bc,0,$01be,0

plane1:		dc.w	$00e0,$0000,$00e2,$0000
plane2:		dc.w	$00e4,$0000,$00e6,$0000
plane3:		dc.w	$00e8,$0000,$00ea,$0000
plane4:		dc.w	$00ec,$0000,$00ee,$0000
;plane5:	dc.w	$00f0,$0000,$00f2,$0000

		dc.w	$0100,$4200	;bplcon0 4 bitplanes
		dc.w	$0102,$0000	;bplcon1
		dc.w	$0108,$0000	;bpl1mod
		dc.w	$010a,$0000	;bpl2mod
		dc.w	$008e,$2c81	;diwstrt (PAL)
		dc.w	$0090,$2cc1	;diwstop (PAL)
		dc.w	$0092,$0038	;ddfstrt
		dc.w	$0094,$00d0	;ddfstop
		dc.w	$ffff,$fffe	;wait forever


;----------------------------------------------------

;*
;* read jostick port 1/2:
;*

;0 = no dir.
;1 = left		$0300
;2 = left,up		$0200
;3 = left,down		$0301
;4 = up			$0400
;5 = down		$0001
;6 = right,up		$0103
;7 = right		$0003
;8 = right,down		$0002
;port 2 = $dff00c
;port 1 = $dff00a

joynone = 0
joyleft = 1
joyleftup = 2
joyleftdown = 3
joyup = 4
joydown = 5
joyrightup = 6
joyright = 7
joyrightdown = 8


joyread:
;check firebuttons
		btst	#7,ciaapra	;firebutton2?
		bne.s	joyrnff2

;check for button held
		cmp.b	#1,joy2lastf
		beq.s	joyrnf2
		
		move.b	#1,joy2fire
		move.b	#1,joy2lastf
		bra	joyrcf1

joyrnff2:	move.b	#0,joy2lastf
joyrnf2:	move.b	#0,joy2fire

joyrcf1:	btst	#6,ciaapra	;firebutton1?
		bne.s	joyrnff1

		cmp.b	#1,joy1lastf
		beq.s	joyrm

		move.b	#1,joy1fire
		move.b	#1,joy1lastf
		bra	joyrmain

joyrnff1:	move.b	#0,joy1lastf
joyrm:		move.b	#0,joy1fire


;* check dirs *
joyrmain:	clr.w	$dff036
		sub.l	d0,d0
		move.w	$dff00c,d0
		move.w	$dff00a,d2
		move.b	#0,joy2dir
		move.b	#0,d1
		bsr	joyl		;read port2
		move.b	d1,joy2dir

		cmp.w	#1,luigiactive
		beq.s	joyread2
		move.b	#0,joy1dir
		rts

joyread2:	move.w	d2,d0
		move.b	#0,joy1dir
		move.b	#0,d1
		bsr	joyl		;read port1
		move.b	d1,joy1dir
		rts

;* read joystick *
joyl:		cmp.w	#$0300,d0
		bne.s	joylu
		move.b	#1,d1
		bra	joyback
joylu:		cmp.w	#$0200,d0
		bne.s	joyld
		move.b	#2,d1
		bra	joyback
joyld:		cmp.w	#$0301,d0
		bne.s	joyu
		move.b	#3,d1
		bra	joyback
joyu:		cmp.w	#$0100,d0
		bne.s	joyd
		move.b	#4,d1
		bra.s	joyback
joyd:		cmp.w	#$0001,d0
		bne.s	joyru
		move.b	#5,d1
		bra.s	joyback
joyru:		cmp.w	#$0103,d0
		bne.s	joyr
		move.b	#6,d1
		bra.s	joyback
joyr:		cmp.w	#$0003,d0
		bne.s	joyrd
		move.b	#7,d1
		bra.s	joyback
joyrd:		cmp.w	#$0002,d0
		bne.s	joyback
		move.b	#8,d1
joyback:	rts

joy2dir:	dc.b	0
even
joy1dir:	dc.b	0
even
joy2fire:	dc.b	0
even
joy1fire:	dc.b	0
even
joy2lastf:	dc.b	0
even
joy1lastf:	dc.b	0
even


;-----------------------------------------------------


;*
;* print gameover:
;* when printgoflag=1
;*

printgameover:	cmp.w	#1,printgoflag
		beq.s	pgameoverok
		rts
pgameoverok:	move.l	#2652,a6
		move.w	#0,textpntr
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		lea	textgo(pc),a0
		move.l	#0,d0
		bsr	ptext1
		rts

;* 'game over' *
textgo:		dc.w	12,0,24,8,$80,28,642,8,34,$8000
printgoflag:	dc.w	0



;----------------------------------------------

;*
;* print title text:
;*

printtitletxt:	move.l	#208,a6		;line5,pos 5
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	#0,d0

		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt1n
		lea	ttext1(pc),a0
		bsr	ptext1

ptt1n:		move.l	#1604,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt2n
		lea	ttext2(pc),a0
		bsr	ptext1

ptt2n:		move.l	#2406,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt3n
		lea	ttext3(pc),a0
		bsr	ptext1

ptt3n:		move.l	#3606,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt4n
		lea	ttext4(pc),a0
		bsr	ptext1

ptt4n:		move.l	#4406,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt5n
		lea	ttext3(pc),a0
		bsr	ptext1

ptt5n:		move.l	#5606,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt6n
		lea	ttext5(pc),a0
		bsr	ptext1

ptt6n:		move.l	#6406,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	ptt7n
		lea	ttext3(pc),a0
		bsr	ptext1

ptt7n:		rts


;'mario bros'
ttext1:		dc.w	24,0,34,16,28,$80,2,34,28,36,$8000
;'programming by:'
ttext2:		dc.w	30,34,28,12,34,0,24,24,16,26,12,$80
		dc.w	2,648,672,$8000
;'thomas jensen'
ttext3:		dc.w	38,14,28,24,0,36,$80
		dc.w	18,8,26,36,8,26,$8000
;'graphics  by:'
ttext4:		dc.w	12,34,0,30,14,16,4,36,$80,$80
		dc.w	2,648,672,$8000
;'sound fx  by:'
ttext5:		dc.w	36,28,640,26,6,$80,10,646,$80,$80
		dc.w	2,648,672,$8000


introoffset:	dc.l	0	;scroll offset

;-----------------------------------------------------

;*
;* print title text2:
;*

printtitletxt2:	
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	#0,d0

		move.l	#1600,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pt2t1n
		lea	t2text1(pc),a0
		bsr	ptext1

pt2t1n:		move.l	#2400,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pt2t2n
		lea	t2text2(pc),a0
		bsr	ptext1

pt2t2n:		move.l	#3600,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pt2t3n
		lea	t2text3(pc),a0
		bsr	ptext1

pt2t3n:		move.l	#4400,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pt2t4n
		lea	t2text4(pc),a0
		bsr	ptext1

pt2t4n:		move.l	#5600,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pt2t5n
		lea	t2text5(pc),a0
		bsr	ptext1

pt2t5n:		move.l	#6400,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pt2t6n
		lea	t2text6(pc),a0
		bsr	ptext1

pt2t6n:		rts


;'press fire in port2'
t2text1:	dc.w	30,34,8,36,36,$80,10,16,34,8,$80
		dc.w	16,26,$80,30,28,34,38,656,$8000
;'for 1 player game'
t2text2:	dc.w	10,28,34,$80,654,$80,30,22,0,648,8,34
		dc.w	$80,12,0,24,8,$8000
;'press fire in port2'
t2text3:	dc.w	30,34,8,36,36,$80,10,16,34,8,$80
		dc.w	16,26,$80,30,28,34,38,654,$8000
;'for 1 player game'
t2text4:	dc.w	10,28,34,$80,656,$80,30,22,0,648,8,34
		dc.w	$80,12,0,24,8,$8000
;'press f1'
t2text5:	dc.w	30,34,8,36,36,$80,10,654,$8000
;'to change level'
t2text6:	dc.w	38,28,$80,4,14,0,26,12,8,$80
		dc.w	22,8,642,8,22,$8000


;----------------------------------------------------

;*
;* print cheatmode text:
;*

printcheattxt:	
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	#0,d0

		move.l	#1602,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pct1n
		lea	tcext1(pc),a0
		bsr	ptext1


;* print level *
pct1n:		move.l	#2418,a3
		add.l	introoffset,a3
		cmp.l	#9400,a3
		bgt.s	pct11n
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.l	#0,d2
		move.w	levelnumten,d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt

		move.l	#2420,a3
		add.l	introoffset,a3
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.l	#0,d2
		move.w	levelnumber,d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt


pct11n:		move.l	#4002,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pct2n
		lea	tcext2(pc),a0
		bsr	ptext1

;* print lives mario *
pct2n:		move.l	#4820,a3
		add.l	introoffset,a3
		cmp.l	#9400,a3
		bgt.s	pct21n
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.l	#0,d2
		move.w	lives,d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt


pct21n:		move.l	#6402,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	pct3n
		lea	tcext3(pc),a0
		bsr	ptext1

;* print lives luigi *
pct3n:		move.l	#7220,a3
		add.l	introoffset,a3
		cmp.l	#9400,a3
		bgt.s	pct31n
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.l	#0,d2
		move.w	livesluigi,d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt

pct31n:		rts


;'select start level'
tcext1:		dc.w	36,8,22,8,4,38,$80,36,38,0,34,38,$80
		dc.w	22,8,642,8,22,$8000
;'select lives mario'
tcext2:		dc.w	36,8,22,8,4,38,$80,22,16,642,8,36,$80
		dc.w	24,0,34,16,28,$8000
;'select lives luigi'
tcext3:		dc.w	36,8,22,8,4,38,$80,22,16,642,8,36,$80
		dc.w	22,640,16,12,16,$8000


;------------------------------------------------

;*
;* print message when plr. has a new hi-score:
;*

printhimess:	move.w	#0,textpntr
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)

		move.l	#1604,a6
		move.w	#0,textpntr
		lea	hitext1(pc),a0
		bsr	ptext2

		move.l	#3214,a6
		move.w	#0,textpntr
		cmp.w	#1,hiwhich
		beq.s	hipmessm
		lea	hitext3(pc),a0
		bra	hipmessdo
hipmessm:	lea	hitext2(pc),a0
hipmessdo:	bsr	ptext2

		move.l	#4802,a6
		move.w	#0,textpntr
		lea	hitext4(pc),a0
		bsr	ptext2
		rts

hiwhich:	dc.w	0

;'congratulations'
hitext1:	dc.w	4,28,26,12,34,0,38,640,22
		dc.w	0,38,16,28,26,36,$8000

;'mario'
hitext2:	dc.w	24,0,34,16,28,$8000

;'luigi'
hitext3:	dc.w	22,640,16,12,16,$8000

;'has a new hi score'
hitext4:	dc.w	14,0,36,$80,0,$80,26,8,640+4,$80
		dc.w	14,16,$80,36,4,28,34,8,$8000



;-----------------------------------------------------

;*
;* print hi-scores:
;*

printhiscores:	
		move.l	#208,a6		;line 5,pos 5
		move.w	#0,textpntr
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		lea	htext1(pc),a0
		move.l	#0,d0
		bsr	ptext1

;calc. numbers to be printed
		move.l	#1008,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit2n
		lea	hisc1(pc),a0
		bsr	printscs


phit2n:		move.l	#1808,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit3n
		lea	hisc2(pc),a0
		bsr	printscs

phit3n:		move.l	#2608,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit4n
		lea	hisc3(pc),a0
		bsr	printscs

phit4n:		move.l	#3408,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit5n
		lea	hisc4(pc),a0
		bsr	printscs

phit5n:		move.l	#4208,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit6n
		lea	hisc5(pc),a0
		bsr	printscs

phit6n:		move.l	#5008,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit7n
		lea	hisc6(pc),a0
		bsr	printscs

phit7n:		move.l	#5808,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit8n
		lea	hisc7(pc),a0
		bsr	printscs

phit8n:		move.l	#6608,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit9n
		lea	hisc8(pc),a0
		bsr	printscs

phit9n:		move.l	#7408,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit10n
		lea	hisc9(pc),a0
		bsr	printscs

phit10n:	move.l	#8208,a6
		add.l	introoffset,a6
		cmp.l	#9400,a6
		bgt.s	phit11n
		lea	hisc10(pc),a0
		bsr	printscs

phit11n:	rts

;---------------------------------------------------

;* hi-scores:
;* 5th figure is total score.
;* last 3 are initials.

hisc1:		dc.w	0,5,0,0,500,38,0,18	;taj
hisc2:		dc.w	0,4,0,0,400,6,0,6	;dad
hisc3:		dc.w	0,3,0,0,300,18,20,18	;jkj
hisc4:		dc.w	0,2,0,0,200,20,20,14	;kkh
hisc5:		dc.w	0,1,0,0,100,2,678,24	;b m
hisc6:		dc.w	0,0,8,0,80,24,0,18	;maj
hisc7:		dc.w	0,0,6,0,60,24,678,36	;m s
hisc8:		dc.w	0,0,4,0,40,0,0,0	;aaa
hisc9:		dc.w	0,0,2,0,20,0,0,0	;aaa
hisc10:		dc.w	0,0,1,0,10,0,0,0	;aaa

;----------------------------------------------------


;*
;* make new hiscore-table when gameover:
;*

sethitable:	bsr	sethtmario
		bsr	sethtluigi
		rts

;* mario *
sethtmario:	move.w	marioscore,d0

;* check if hiscore and get entry# *
		bsr	sethtall

;* was score a new hi-score? *
		cmp.w	#1,newhiflag
		beq.s	sethtmins
		rts

;* insert entry in table *
sethtmins:	move.w	pshunthoum,(a0)
		move.w	pstenthoum,2(a0)
		move.w	psthoum,4(a0)
		move.w	pshunm,6(a0)
		move.w	marioscore,8(a0)
		move.w	#0,10(a0)		;'a'
		move.w	#0,12(a0)
		move.w	#0,14(a0)
		move.w	#0,newhiflag
		move.w	#1,newhiflagw	;mario has a new hiscore
		move.l	a0,newhtentrym
		rts

;* luigi *
sethtluigi:	move.w	luigiscore,d0
		bsr	sethtall

		cmp.w	#1,newhiflag
		beq.s	sethtlins
		rts
sethtlins:	move.w	pshunthoul,(a0)
		move.w	pstenthoul,2(a0)
		move.w	psthoul,4(a0)
		move.w	pshunl,6(a0)
		move.w	luigiscore,8(a0)
		move.w	#0,10(a0)
		move.w	#0,12(a0)
		move.w	#0,14(a0)
		move.w	#0,newhiflag
		move.l	a0,newhtentryl
		cmp.w	#1,newhiflagw
		beq.s	sethtlb
		move.w	#2,newhiflagw
		rts
sethtlb:	move.w	#3,newhiflagw	;both have a hiscore
		rts



;*** insert in hiscoretable (return pointer to entry in a0) ***

sethtall:	lea	hisc1(pc),a0
		moveq	#1,d1		;#1
sethtlp:	move.w	8(a0),d2
		cmp.w	d0,d2
		ble.s	sethtins	;marioscore>=cur.entry?
		add.w	#1,d1		;next entry
		add.l	#16,a0
		cmp.w	#11,d1
		bne.s	sethtlp
		rts

;plr. has a new entry in hi-score-table!
;d1=# of entry to replace
;a0=adr. of entry
sethtins:	move.l	#10,d2
		sub.w	d1,d2		;d2=10-#entry

;move #entry and the lower scores one level down
;don't move lowest score.

		lea	hisc9(pc),a1
sethtinslp:	cmp.w	#0,d2		;time to insert new score?
		beq.s	sethtinsne
		move.l	(a1),16(a1)
		move.l	4(a1),20(a1)
		move.l	8(a1),24(a1)
		move.l	12(a1),28(a1)
		sub.w	#1,d2		;next
		sub.l	#16,a1
		bra	sethtinslp
;insert entry
sethtinsne:	move.w	#1,newhiflag
		rts


newhiflag:	dc.w	0	;new hiscore? (0=no)
newhiflagw:	dc.w	0	;who? (mario=1, luigi=2, none=0)
				;(both=3)
newhtentrym:	dc.l	0	;adr. of entry to change
newhtentryl:	dc.l	0
newhtentry:	dc.l	0


;---------------------------------------------------

;*
;* get initials for hiscore:
;*

getinitials:	bsr	getinitm
		bsr	getinitl
		rts

;* mario *
getinitm:	cmp.w	#1,newhiflagw
		beq.s	getinitmpm
		cmp.w	#3,newhiflagw
		beq.s	getinitmpm
		rts

getinitmpm:	jsr	clearscreen
		move.w	#1,hiwhich
		jsr	printhimess
		jsr	fadeinscr
		move.l	newhtentrym,newhtentry
		bsr	getinitmain
		rts

;* luigi *
getinitl:	cmp.w	#2,newhiflagw
		beq.s	getinitmpl
		cmp.w	#3,newhiflagw
		beq.s	getinitmpl
		rts

getinitmpl:	jsr	clearscreen
		move.w	#2,hiwhich
		jsr	printhimess
		jsr	fadeinscr
		move.w	#150,d0
getilpmlp:	jsr	waitvbl
		jsr	waitvbl2
		dbf	d0,getilpmlp
		move.l	newhtentryl,newhtentry
		bsr	getinitmain
		rts



;*** main (pointer to tableentry in a0) ***
getinitmain:

;* print new entry in hi-score list *
		move.l	newhtentry,a0	;addr. of entry
		move.l	#8208,a6	;pos. to print at
		jsr	printscs2	;print it!

		move.l	newhtentry,a0
		add.l	#10,a0
		move.l	a0,getiadr
		move.w	#0,getinum
		move.w	#5,getitimer
		move.w	#5,getitimel

;* loop *
gettest:	jsr	joyread

;* test fire *
		cmp.b	#1,joy2fire
		beq.s	getinlfire
		cmp.b	#1,joy1fire
		beq.s	getinlfire
		bra	getinf

;* fire!! *
getinlfire:	add.l	#2,getiadr	;next initial
		add.w	#1,getinum
		bra	getivbl

;* test joy right *
getinf:		cmp.b	#7,joy2dir
		beq.s	getir
		cmp.b	#7,joy1dir
		beq.s	getir
		bra.s	getinr

getir:		sub.w	#1,getitimer

;* time to change letter? *
		cmp.w	#0,getitimer
		bne.L	getivbl
;* reset timer *
		move.w	#5,getitimer

;* get next letter *
		move.l	getiadr,a0
		move.w	(a0),d2
		cmp.w	#38,d2		;'t'?
		bne.s	getirnt
		move.w	#640,(a0)
		bra	getirex

getirnt:	cmp.w	#678,d2		;' '?
		bne.s	getirnz
		move.w	#0,(a0)
		bra	getirex

getirnz:	add.w	#2,(a0)		;next letter
getirex:	bra	getivbl

;* test joyleft *
getinr:
;* reset right timer *
		move.w	#5,getitimer
		cmp.b	#1,joy2dir
		beq.s	getil
		cmp.b	#1,joy1dir
		beq.s	getil
		bra.s	getinl

getil:		sub.w	#1,getitimel

;* time to change letter? *
		cmp.w	#0,getitimel
		bne.L	getivbl
;* reset timer *
		move.w	#5,getitimel
;* get next letter *
		move.l	getiadr,a0
		move.w	(a0),d2
		cmp.w	#640,d2		;'u'?
		bne.s	getilnu
		move.w	#38,(a0)
		bra	getilex

getilnu:	cmp.w	#0,d2		;'a'?
		bne.s	getilna
		move.w	#678,(a0)
		bra	getilex

getilna:	sub.w	#2,(a0)		;next letter
getilex:	bra	getivbl

getinl:		move.w	#5,getitimel

;* wait for vbl *
getivbl:	jsr	waitvbl
		jsr	waitvbl2

;* print score & initials *
		move.l	#8208,a6	;pos. to print at
		move.l	newhtentry,a0	;adr. of entry
		jsr	printscs2	;print it!

		cmp.w	#3,getinum	;3 initials ok?
		bne.L	gettest

		jsr	fadeoutscr	;fadeout hi-scores
		rts


getinum:	dc.w	0
getitimer:	dc.w	20
getitimel:	dc.w	20
getiadr:	dc.l	0


;-------------------------------------------------------

;*
;* print hi-score:
;*
;* cookie-cut to one screen.
;* in: a0 = adr. of hunthou
;* in: a6=offset.
;* hunthou,tenthou,thou,hun
;* on consecutive addrs.
;*

printscs:	move.l	a6,a3		;screen
		move.l	#0,d2
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.w	(a0),d2		;hunthous in d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		move.w	#$401,d5	;blitsize
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		bsr	printtxt	;print 100000's

		move.l	a6,a3
		add.l	#2,a3		;next pos.
		move.l	gfx1,a1
		add.l	#9452,a1
		move.l	mask1,a2
		add.l	#9452,a2
		add.l	#2,a0		;tenthous
		move.w	(a0),d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt	;print 10000's

		move.l	a6,a3
		add.l	#4,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.l	mask1,a2
		add.l	#9452,a2
		add.l	#2,a0
		move.w	(a0),d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt	;print 1000's

		move.l	a6,a3
		add.l	#6,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.l	mask1,a2
		add.l	#9452,a2
		add.l	#2,a0
		move.w	(a0),d2
		asl.w	#1,d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt	;print 100's

		move.l	a6,a3
		add.l	#8,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.l	mask1,a2
		add.l	#9452,a2
		bsr	printtxt	;tens=0

		move.l	a6,a3
		add.l	#10,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.l	mask1,a2
		add.l	#9452,a2
		bsr	printtxt	;ones=0

		move.l	a6,a3
		add.l	#14,a3
		move.l	gfx1,a1
		add.l	#8800,a1
		move.l	mask1,a2
		add.l	#8800,a2
		add.l	#4,a0
		move.w	(a0),d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt	;print 1. initial

		move.l	a6,a3
		add.l	#16,a3
		move.l	gfx1,a1
		add.l	#8800,a1
		move.l	mask1,a2
		add.l	#8800,a2
		add.l	#2,a0
		move.w	(a0),d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt	;print 2. initial

		move.l	a6,a3
		add.l	#18,a3
		move.l	gfx1,a1
		add.l	#8800,a1
		move.l	mask1,a2
		add.l	#8800,a2
		add.l	#2,a0
		move.w	(a0),d2
		add.l	d2,a1
		add.l	d2,a2
		bsr	printtxt	;print 3. initial

		rts

;-------------------------------------------------------

;*
;* print hi-score2:
;*
;* straight copy to two screens.
;* in: a0 = adr. of hunthou
;* hunthou,tenthou,thou,hun
;* on consecutive addrs.
;*

printscs2:	move.l	a6,a4		;screen1
		move.l	a6,a5
		move.l	a5,a3		;screen2
		move.l	#0,d2
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.w	(a0),d2		;hunthous in d2
		asl.w	#1,d2
		add.l	d2,a1
		move.w	#$401,d5	;blitsize
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		bsr	printtxt2	;print 100000's

		move.l	a6,a4
		move.l	a5,a3
		add.l	#2,a3		;next pos.
		add.l	#2,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		add.l	#2,a0		;tenthous
		move.w	(a0),d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 10000's

		move.l	a5,a3
		move.l	a6,a4
		add.l	#4,a3
		add.l	#4,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		add.l	#2,a0
		move.w	(a0),d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 1000's

		move.l	a5,a3
		move.l	a6,a4
		add.l	#6,a3
		add.l	#6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		add.l	#2,a0
		move.w	(a0),d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100's

		move.l	a5,a3
		move.l	a6,a4
		add.l	#8,a3
		add.l	#8,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		bsr	printtxt2	;tens=0

		move.l	a5,a3
		move.l	a6,a4
		add.l	#10,a3
		add.l	#10,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		bsr	printtxt2	;ones=0

		move.l	a5,a3
		move.l	a6,a4
		add.l	#14,a3
		add.l	#14,a4
		move.l	gfx1,a1
		add.l	#8800,a1
		add.l	#4,a0
		move.w	(a0),d2
		add.l	d2,a1
		bsr	printtxt2	;print 1. initial

		move.l	a5,a3
		move.l	a6,a4
		add.l	#16,a3
		add.l	#16,a4
		move.l	gfx1,a1
		add.l	#8800,a1
		add.l	#2,a0
		move.w	(a0),d2
		add.l	d2,a1
		bsr	printtxt2	;print 2. initial

		move.l	a5,a3
		move.l	a6,a4
		add.l	#18,a3
		add.l	#18,a4
		move.l	gfx1,a1
		add.l	#8800,a1
		add.l	#2,a0
		move.w	(a0),d2
		add.l	d2,a1
		bsr	printtxt2	;print 3. initial

		rts


;'hi scores:'
htext1:		dc.w	14,16,$80,36,4,28,34,8,36,672,$8000



;--------------------------------------------------

;*
;* print text which only needs to be printed once.
;* NB. score (00000)  will be modified by printscores
;*

printtext1:	move.l	#8040,a6	;line 201,pos. 1
		move.w	#0,textpntr
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		lea	text1(pc),a0
		move.l	#0,d0
		bsr	ptext2

		move.l	#8680,a6
		move.w	#0,textpntr
		lea	text2(pc),a0
		bsr	ptext2
		rts


;--------------------------------------------------

;*
;* print text which only needs to be printed once.
;* this is only for player 2.
;* NB. score (00000)  will be modified by printscores
;*


printtext2:	move.l	#8040+26,a6	;line 201,pos. 14
		move.w	#0,textpntr
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		lea	text3(pc),a0
		move.l	#0,d0
		bsr	ptext2

		move.l	#8680+28,a6	;pos. 15
		move.w	#0,textpntr
		lea	text4(pc),a0
		bsr	ptext2
		rts


;* 'player2' *
text3:		dc.w	30,22,0,640+8,8,34,656,$8000

;* '000000' *
text4:		dc.w	652,652,652,652,652,652,$8000


;--------------------------------------------------

;*
;* print text string:
;*

ptext1:		move.w	#0,textpntr
		move.w	textpntr,d0
		move.l	#0,d7
		move.l	a6,a5
		
ptext1loop:	move.w	(a0,d0),d7	;new letter in d7
		cmp.w	#$8000,d7	;end of text?
		beq.s	ptext1ex
		cmp.w	#$80,d7		;space?
		beq.s	ptext1nl	;yes, new letter
		move.l	gfx1,a1
		add.l	#8800,a1	;'a'
		add.l	d7,a1		;next letters position
		move.l	mask1,a2
		add.l	#8800,a2
		add.l	d7,a2
		move.l	a5,a3
		add.l	d0,a3		;next position in bitplane
		bsr	printtxt
ptext1nl:	add.w	#2,d0
		bra	ptext1loop	;get next letter
ptext1ex:	rts

;-----------------------------------------------

;*
;* print text string to both screens:
;*

ptext2:		move.w	#0,textpntr
		move.w	textpntr,d0
		move.l	#0,d7
		move.l	a6,a5
		
ptext2loop:	move.w	(a0,d0),d7	;new letter in d7
		cmp.w	#$8000,d7	;end of text?
		beq.s	ptext2ex
		cmp.w	#$80,d7		;space?
		beq.s	ptext2nl	;yes, new letter
		move.l	gfx1,a1
		add.l	#8800,a1	;'a'
		add.l	d7,a1		;next letters position
		add.l	d7,a2
		move.l	a5,a3
		move.l	a6,a4
		add.l	d0,a3		;next position in bitplane
		add.l	d0,a4
		bsr	printtxt2
ptext2nl:	add.w	#2,d0
		bra	ptext2loop	;get next letter
ptext2ex:	rts


;* 'player1 high' *
text1:		dc.w	30,22,0,640+8,8,34,654,$80
		dc.w	14,16,12,14,$8000
;* '000000 000000' *
text2:		dc.w	652,652,652,652,652,652
		dc.w	$80,652,652,652,652,652,652,$8000
textpntr:	dc.w	0



;-------------------------------------------------

;*
;* print text to ~current gamescreen:
;*
;* also used for printing levelgfx in intro.
;* in: a1=srcadr.,a2=maskadr., a3=destoffset.
;* d1=amod, d3=dmod, d5=bltsize.
;*

printtxt:	add.l	screen,a3	;add screenbase to offset
		move.w	#$ffff,bltafwm
		move.w	#$ffff,bltalwm
		move.l	a1,bltbpth	;a1=char addr.
		move.l	a1,ptxtsave
		move.l	a2,bltapth	;a2=mask
		move.l	a3,bltdpth	;a3=gamescr.
		move.l	a3,bltcpth	;backgr.
		move.w	d1,bltamod
		move.w	d1,bltbmod
		move.w	d3,bltcmod
		move.w	d3,bltdmod
		move.w	#%0000111111001010,bltcon0	;cookie-cut
		move.w	#0,bltcon1
		move.l	#4,d4

ptxtloop:	move.w	d5,bltsize
		bsr	waitblit
		add.l	#$2800,a3	;next plane (PAL)
		move.l	a3,bltdpth
		move.l	a3,bltcpth
		add.l	#$2800,a1	;next plane
		move.l	a1,bltbpth	;text
		move.l	a2,bltapth
		subq	#1,d4		;next plane
		bne.s	ptxtloop
		rts


ptxtsave:	dc.l	0


;----------------------------------------------------

;*
;* print text to both gamescreens (no mask):
;* used in-game.
;* in: a1=textadr., a3=gamescr1.adr., a4=scr2.adr.
;* d1=amod, d3=dmod, d5=blitsize.
;* used by: printlevel, bonusprint & printscs(hiscores)
;*


printtxt2:	add.l	screen1,a4
		add.l	screen2,a3
		move.w	#$ffff,bltafwm
		move.w	#$ffff,bltalwm
		move.l	a1,bltapth	;a1=text
		move.l	a1,ptxtsave2
		move.l	a3,bltdpth	;a3=gamescr.
		move.w	d1,bltamod
		move.w	d3,bltdmod
		move.w	#%0000100111110000,bltcon0	;straight copy
		move.w	#0,bltcon1
		move.l	#4,d4

ptxt2loop:	move.w	d5,bltsize
		bsr	waitblit
		add.l	#$2800,a3	;next plane (PAL)
		move.l	a3,bltdpth
		add.l	#$2800,a1	;next plane
		move.l	a1,bltapth	;text
		subq	#1,d4		;next plane
		bne.s	ptxt2loop
;print to screen2
		move.l	a4,bltdpth
		move.l	ptxtsave2,a1
		move.l	a1,bltapth
		move.l	#4,d4
ptxt2loop2:	move.w	d5,bltsize
		bsr	waitblit
		add.l	#$2800,a4
		move.l	a4,bltdpth
		add.l	#$2800,a1
		move.l	a1,bltapth
		subq	#1,d4
		bne.s	ptxt2loop2
		rts

ptxtsave2:	dc.l	0


;-----------------------------------------------------

;*
;* calculate scores:
;*
;* give extra man at 20000 points.
;*

calcscores:	bsr	cscoresmario
		bsr	cscoresluigi
		rts

cscoresmario:	cmp.w	#1,marioactive
		beq.s	cscmact
		rts

cscmact:	move.w	pshunm,d0
		cmp.w	#9,d0
		ble.s	cscmhunok
		sub.w	#10,pshunm
		add.w	#1,psthoum

cscmhunok:	move.w	psthoum,d0
		cmp.w	#9,d0
		ble.s	cscmthouok
		sub.w	#10,psthoum
		add.w	#1,pstenthoum

cscmthouok:	move.w	pstenthoum,d0
		cmp.w	#9,d0
		ble.s	cscmtenthouok
		sub.w	#10,pstenthoum
		add.w	#1,pshunthoum

cscmtenthouok:	move.w	pshunthoum,d0
		cmp.w	#9,d0		;score>=100000?
		ble.s	cscmhunthouok
		move.w	#0,pshunthoum	;reset scores
		move.w	#0,pstenthoum
		move.w	#0,psthoum
		move.w	#0,pshunm
cscmhunthouok:	rts


;* luigi
cscoresluigi:	cmp.w	#1,luigiactive
		beq.s	csclact
		rts

csclact:	move.w	pshunl,d0
		cmp.w	#9,d0
		ble.s	csclhunok
		sub.w	#10,pshunl
		add.w	#1,psthoul

csclhunok:	move.w	psthoul,d0
		cmp.w	#9,d0
		ble.s	csclthouok
		sub.w	#10,psthoul
		add.w	#1,pstenthoum

csclthouok:	move.w	pstenthoul,d0
		cmp.w	#9,d0
		ble.s	cscltenthouok
		sub.w	#10,pstenthoul
		add.w	#1,pshunthoul

cscltenthouok:	move.w	pshunthoul,d0
		cmp.w	#9,d0		;score>=100000?
		ble.s	csclhunthouok
		move.w	#0,pshunthoul	;reset scores
		move.w	#0,pstenthoul
		move.w	#0,psthoul
		move.w	#0,pshunl
csclhunthouok:	rts



;-------------------------------------------------

;*
;* print scores on screen every 5 frames:
;*
;* uses printtxt2 above
;* score is stored in 4 words (100's to 100000's)
;* calcscore is monitoring changes
;* and making the necessary upgrades.
;* an additional var. is used to hold the entire score
;* for detecting change since last time.
;*
;* 1's and 10's are not printed.
;*
;* hi-score is only printed once.
;*
;* also checks for extra lives at 20000p!!!
;*

printscores:	bsr	pscoremario
		bsr	pscoreluigi
		bsr	phiscore
		rts

pscoremario:	cmp.w	#1,marioactive
		beq.s	pscmact
		rts
pscmact:	add.w	#1,pscorecntm
		cmp.w	#5,pscorecntm  ;time to print?
		beq.s	psmok
psmex:		rts
psmok:		move.w	#0,pscorecntm

;has mario scored more points?
		move.w	marioscore,d0
		cmp.w	mariolastscore,d0
		beq.s	psmex		;no, exit
;extra?
		cmp.w	#200,marioscore		;20000 points?
		bls.s	psmnextra		
		cmp.w	#200,mariolastscore
		bge.s	psmnextra
;extra!!!
		add.w	#1,lives	;extra!!!

psmnextra:	move.w	d0,mariolastscore  ;save score

;calc. numbers to be printed
		move.l	#8680,a3	;line 217
		move.l	#8680,a4	;pos 1
		move.l	a3,a5
		move.l	a4,a6
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	#0,d2
		move.w	pshunthoum,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100000's

		add.l	#2,a5		;next pos.
		add.l	#2,a6
		move.l	a6,a4
		move.l	a5,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	pstenthoum,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 10000's

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	psthoum,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 1000's

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	pshunm,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100's
		rts

;* luigi
pscoreluigi:	cmp.w	#1,luigiactive
		beq.s	psclact
		rts
psclact:	add.w	#1,pscorecntl
		cmp.w	#5,pscorecntl  ;time to print?
		beq.s	pslok
pslex:		rts
pslok:		move.w	#0,pscorecntl
		move.w	luigiscore,d0
		cmp.w	luigilastscore,d0
				;has mario scored more points?
		beq.s	pslex		;no, exit
;extra?
		cmp.w	#200,luigiscore
		bls.s	pslnextra
		cmp.w	#200,luigilastscore
		bge.s	pslnextra
;extra!!!
		add.w	#1,livesluigi	;extra!!!

pslnextra:	move.w	d0,luigilastscore  ;save score

;calc. numbers to be printed
		move.l	#8680+28,a3	;line 217
		move.l	#8680+28,a4	;pos 15
		move.l	a3,a5
		move.l	a4,a6
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	#0,d2
		move.w	pshunthoul,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100000's

		add.l	#2,a5		;next pos.
		add.l	#2,a6
		move.l	a6,a4
		move.l	a5,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	pstenthoul,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 10000's

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	psthoul,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 1000's

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	pshunl,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100's
		rts

;* hi-score
phiscore:	cmp.w	#0,phsflag
		beq.s	phsok
		rts
phsok:		move.w	#1,phsflag

;calc. numbers to be printed
		move.l	#8680+14,a3	;line 217
		move.l	#8680+14,a4	;pos 8
		move.l	a3,a5
		move.l	a4,a6
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	#0,d2
		move.w	hischunthou,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100000's

		add.l	#2,a5		;next pos.
		add.l	#2,a6
		move.l	a6,a4
		move.l	a5,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	hisctenthou,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 10000's

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	hiscthou,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 1000's

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	hischun,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print 100's
		rts

phsflag:	dc.w	0	;print hi-score (0=yes,other=no)


;-----------------------------------------------

;*
;* print time remaining in bonus level:
;* bonusprint: #1 = print time
;*	       #2 = delete time
;*


printtime:	cmp.w	#1,bonusprint	;time to print?
		beq.s	prtimeok
		cmp.w	#2,bonusprint	;delete time?
		beq.L	prtimedel
		rts

prtimeok:
;calc. numbers to be printed
		move.l	#818,a3	;line 20
		move.l	#818,a4	;pos 20
		move.l	a3,a5
		move.l	a4,a6
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	#0,d2
		move.w	tensecs,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print tensecs

		add.l	#2,a5		;next pos.
		add.l	#2,a6
		move.l	a6,a4
		move.l	a5,a3
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	secs,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print secs

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9474,a1
		bsr	printtxt2	;print '.'

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9452,a1
		move.w	tenthofsec,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print tenth of secs
		move.w	#0,bonusprint
		rts


;------------------------------------------------

;*
;* delete time:
;*

prtimedel:	move.l	#818,a3		;line 1
		move.l	#818,a4		;pos 20
		move.l	a3,a5
		move.l	a4,a6
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9478,a1	;' '
		bsr	printtxt2

		add.l	#2,a5		;next pos.
		add.l	#2,a6
		move.l	a6,a4
		move.l	a5,a3
		move.l	gfx1,a1
		add.l	#9478,a1
		bsr	printtxt2	;print ' '

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9478,a1
		bsr	printtxt2	;print ' '

		add.l	#2,a5
		add.l	#2,a6
		move.l	a5,a3
		move.l	a6,a4
		move.l	gfx1,a1
		add.l	#9478,a1
		bsr	printtxt2	;print ' '
		move.w	#0,bonusprint
		rts



;--------------------------------------------------------

;*
;* print level in-game:
;*

printlevig:
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)

		move.l	#58,a3
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.l	#0,d2
		move.w	levelnumten,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt

		move.l	#60,a3
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	mask1,a2
		add.l	#9452,a2
		move.l	#0,d2
		move.w	levelnumber,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt
		rts


;-------------------------------------------------

;*
;* print number of lives remaining:
;*

printlives:	bsr	plivesmario
		bsr	plivesluigi
		rts

plivesmario:	cmp.w	#1,marioactive
		beq.s	plmact
		rts
plmact:		move.l	#40,a3	;line 1
		move.l	#40,a4	;pos 1
		move.w	#38,d1		;amod
		move.w	#38,d3		;dmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	#0,d2
		move.w	lives,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print # of lives left
		rts


plivesluigi:	cmp.w	#1,luigiactive
		beq.s	pllact
		rts
pllact:		move.l	#40+78,a3	;line 1
		move.l	#40+78,a4	;pos 40
		move.w	#38,d1		;a/bmod
		move.w	#38,d3		;d/cmod
		move.w	#$401,d5	;bltsize (16*1)
		move.l	gfx1,a1
		add.l	#9452,a1	;'0'
		move.l	#0,d2
		move.w	livesluigi,d2
		asl.w	#1,d2
		add.l	d2,a1
		bsr	printtxt2	;print # of lives left
		rts



;----------------------------------------------------------


;*
;* initialize game variables:
;*

initgame:	bsr	initmario
		bsr	initluigi

		lea	level1,a0
		move.l	a0,levelno	;adr. of lev. init. to level1
		move.w	#0,levelpnt	;first nme
		move.w	#0,levelcnt	;0 frames

;* startlevel *
		move.w	#1,levelnumber
		move.w	#0,levelnumten
		move.w	#1,levelstart
		move.l	gfx2,a1
		move.l	a1,levelgfx

		move.w	#3,lives
		move.w	#3,livesluigi

		move.w	#0,gameover
		move.w	#0,printgoflag	;don't print game-over
		move.w	#50,fadeincnt
		move.w	#0,fadeoutcnt
		move.w	#1,maxnooffb	;1 fireball max. to begin with
		move.w	#0,firelaunched	;no fireballs on-screen
		move.w	#0,pause
		move.w	#0,resume
		move.w	#0,icetimer
		move.w	#0,icetimer2

		move.w	#2,frame
		move.w	#0,screenf
		move.w	#0,pscorecntm
		move.w	#0,marioscore	;0 points
		move.w	#0,luigiscore
		move.w	#0,mariolastscore
		move.w	#0,luigilastscore
		move.w	#0,pshunm	;0 100's
		move.w	#0,psthoum
		move.w	#0,pstenthoum
		move.w	#0,pshunthoum
		move.w	#0,pscorecntl
		move.w	#0,pshunl
		move.w	#0,psthoul
		move.w	#0,pstenthoul
		move.w	#0,pshunthoul
;copy hi-score
		lea	hisc1(pc),a0
		move.w	(a0),d0
		move.w	d0,hischunthou
		add.l	#2,a0
		move.w	(a0),d0
		move.w	d0,hisctenthou
		add.l	#2,a0
		move.w	(a0),d0
		move.w	d0,hiscthou
		add.l	#2,a0
		move.w	(a0),d0
		move.w	d0,hischun
		add.l	#2,a0
		move.w	(a0),d0
		move.w	d0,hiscore
		move.w	#0,newhiflagw
		move.w	#0,newhiflag

		move.w	#0,phsflag

		move.w	#0,bonustimer
		move.w	#0,bonusprint
		move.w	#0,bonusflag
		move.w	#1,tensecs
		move.w	#9,secs
		move.w	#9,tenthofsec

		move.w	#3,powlevel	;3 pows to start with
		move.w	#0,powhitmario
		move.w	#0,powhitluigi
		move.w	#0,pushcnt
		move.w	#0,mlcoll	;no collision

		move.l	title,a2	;save to screen in first
					;2 frames.

;disable all nmes:
		lea	nmeactive,a0
		lea	nmesource,a1
		lea	nmesource2,a3
		move.l	#0,d0
		move.l	#0,d7
initnmeloop:	move.w	#0,(a0,d0)	;nme not active
		move.l	a2,(a1,d7)
		move.l	a2,(a3,d7)
		add.w	#2,d0
		add.l	#4,d7
		cmp.w	#20,d0
		bne.s	initnmeloop

		move.l	a2,mariosource
		move.l	a2,mariosource2
		move.l	a2,luigisource
		move.l	a2,luigisource2
		rts

;*** init. mario vars. ***

initmario:	move.w	#0,animbyte
		move.w	#0,colhflag
		move.w	#0,chflag
		move.w	#80,marioxpos
		move.w	#640,marioxposition
		move.w	#167,marioypos
		move.w	#-2,tabcnt
		move.w	#-2,skidcnt
		move.w	#0,runcnt
		move.w	#0,deadcnt
		move.w	#0,deadcnt2
		move.w	#0,deadflag
		move.w	#0,xoffset
		move.w	#0,hitflag
		move.w	#0,powhitmario
		move.w	#0,jumpbyte
		move.w	#5,maxdown
		move.w	#1,facing
		move.w	#0,fbtimerm
		rts

;*** init. luigi vars. ***

initluigi:	move.w	#0,animbyteluigi
		move.w	#0,colhflagluigi
		move.w	#0,chflagluigi
		move.w	#220,luigixpos
		move.w	#1760,luigixposition
		move.w	#167,luigiypos
		move.w	#-2,tabcntluigi
		move.w	#-2,skidcntluigi
		move.w	#0,runcntluigi
		move.w	#0,deadcntluigi
		move.w	#0,deadcnt2luigi
		move.w	#0,deadflagluigi
		move.w	#0,xoffsetluigi
		move.w	#0,hitflagluigi
		move.w	#0,powhitluigi
		move.w	#0,jumpbyteluigi
		move.w	#5,maxdownluigi
		move.w	#0,facingluigi
		move.w	#0,fbtimerl
		rts


;-------------------------------------------------------

;*
;* control soundfx:
;*
;* channel 0 : mario
;* channel 1 : luigi
;* channel 2 : left pipe+misc.
;* channel 3 : right pipe+misc.
;*


contaudio:
		bsr	capause		;check for pause
		bsr	camario		;channel 0
		bsr	caluigi		;channel 1
		bsr	canmeleft	;channel 2
		bsr	canmeright	;channel 3
		rts


capause:	cmp.w	#1,pause
		bne.s	capnp
		;move.w	#%0000000000001111,dmaconw ;kill all sound
capnp:		rts


;* mario *

camario:	cmp.w	#1,marioactive
		beq.s	camarioact
		rts

camarioact:	cmp.w	#0,animbyte
		beq.s	camstand
		cmp.w	#1,animbyte
		beq.L	camjump
		cmp.w	#2,animbyte
		beq.L	camrun
		cmp.w	#3,animbyte
		beq.L	camrun
		cmp.w	#4,animbyte
		beq.L	camskid
		cmp.w	#5,animbyte
		beq.L	camskid
		cmp.w	#6,animbyte
		beq.L	camdead
		cmp.w	#7,animbyte
		beq.L	camskid
		cmp.w	#8,animbyte
		beq.L	camskid
		cmp.w	#9,animbyte
		beq.L	camhit
		cmp.w	#10,animbyte
		beq.s	camwait
		rts


;* mario standing/waiting *

camstand:
camwait:	move.w	#0,aud0vol	;no sound
		move.w	#0,audch0on
		move.w	#0,maudrun
		move.w	#0,audch0w
		move.w	#100,aud0len
		rts

;* mario running *

camrun:		cmp.w	#1,maudrun
		beq.s	camrunex
		move.w	#1,maudrun
		move.l	sample1,aud0lch
		move.w	#sizes1/2,aud0len
		;move.w	#0,aud0vol
		move.w	#1,audch0w	;run fx wanted
camrunex:	rts


;* mario skidding *

camskid:	move.w	#0,aud0vol
		move.w	#0,audch0on
		move.w	#0,audch0w
		rts

;* mario jumping *

camjump:	cmp.w	#-2,tabcnt
		bne.s	camjumpni
		move.l	sample4,aud0lch
		move.w	#sizes4/2,aud0len
		move.w	#2,audch0w	;jump fx wanted after this
		rts
camjumpni:	cmp.w	#48,tabcnt	;top of jump/start fall?
		bne.s	camjumpex
		move.w	#0,audch0w	;turn off next time
camjumpex:	rts


;* mario dead *

camdead:
		cmp.w	#1,deadcnt
		beq.s	camdead1i
		cmp.w	#50,deadcnt
		beq.s	camdead2i
		rts
;1. fx
camdead1i:	move.l	sample2,aud0lch
		move.w	#sizes2/2,aud0len
		move.w	#0,aud0vol
		move.w	#3,audch0w	;dead1 wanted
		rts

;2. fx
camdead2i:	move.l	sample3,aud0lch
		move.w	#sizes3/2,aud0len
		move.w	#0,aud0vol
		move.w	#4,audch0w	;deadfall wanted
		rts


;* mario hit *

camhit:		move.w	#0,aud0vol
		move.w	#0,audch0on
		rts


;*** luigi ***

caluigi:	cmp.w	#1,luigiactive
		beq.s	caluigiact
		rts

caluigiact:	cmp.w	#0,animbyteluigi
		beq.s	calstand
		cmp.w	#1,animbyteluigi
		beq.L	caljump
		cmp.w	#2,animbyteluigi
		beq.L	calrun
		cmp.w	#3,animbyteluigi
		beq.L	calrun
		cmp.w	#4,animbyteluigi
		beq.L	calskid
		cmp.w	#5,animbyteluigi
		beq.L	calskid
		cmp.w	#6,animbyteluigi
		beq.L	caldead
		cmp.w	#7,animbyteluigi
		beq.L	calskid
		cmp.w	#8,animbyteluigi
		beq.L	calskid
		cmp.w	#9,animbyteluigi
		beq.L	calhit
		cmp.w	#10,animbyteluigi
		beq.s	calwait
		rts


;* luigi standing/waiting *

calstand:
calwait:	move.w	#0,aud1vol	;no sound
		move.w	#0,audch1on
		move.w	#0,laudrun
		move.w	#0,audch1w
		move.w	#100,aud1len
		rts

;* luigi running *

calrun:		cmp.w	#1,laudrun
		beq.s	calrunex
		move.w	#1,laudrun
		move.l	sample1,aud1lch
		move.w	#sizes1/2,aud1len
		;move.w	#0,aud1vol
		move.w	#1,audch1w	;run fx wanted
calrunex:	rts


;* luigi skidding *

calskid:	move.w	#0,aud1vol
		move.w	#0,audch1on
		move.w	#0,audch1w
		rts

;* luigi jumping *

caljump:	cmp.w	#-2,tabcntluigi
		bne.s	caljumpni
		move.l	sample4,aud1lch
		move.w	#sizes4/2,aud1len
		move.w	#2,audch1w	;jump fx wanted after this
		rts
caljumpni:	;move.w	#0,audch1w
		rts


;* luigi dead *

caldead:
		cmp.w	#1,deadcntluigi
		beq.s	caldead1i
		cmp.w	#50,deadcntluigi
		beq.s	caldead2i
		rts
;1. fx
caldead1i:	move.l	sample2,aud1lch
		move.w	#sizes2/2,aud1len
		move.w	#0,aud1vol
		move.w	#3,audch1w	;dead1 wanted
		rts

;2. fx
caldead2i:	move.l	sample3,aud1lch
		move.w	#sizes3/2,aud1len
		move.w	#0,aud1vol
		move.w	#4,audch1w	;deadfall wanted
		rts


;* luigi hit *

calhit:		move.w	#0,aud1vol
		move.w	#0,audch1on
		rts



;*** left channel nmes ***

canmeleft:	cmp.w	#1,nmeleftlaunch	;nme launched?
		bne.s	canlex
		move.w	#0,nmeleftlaunch	;clear it
		move.w	#0,nmellcnt

		move.l	sample5,aud2lch
		move.w	#sizes5/2,aud2len
		move.w	#0,aud2vol
		move.w	#1,audch2w
canlex:		rts


nmeleftlaunch:	dc.w	0
nmellcnt:	dc.w	0


;*** right channel nmes ***

canmeright:	cmp.w	#1,nmerightlaunch	;nme launched?
		bne.s	canrex
		move.w	#0,nmerightlaunch	;clear it
		move.w	#0,nmerlcnt

		move.l	sample5,aud3lch
		move.w	#sizes5/2,aud3len
		move.w	#0,aud3vol
		move.w	#1,audch3w
canrex:		rts


nmerightlaunch:	dc.w	0
nmerlcnt:	dc.w	0



;--------------------------------------------------------

;*
;* check if nmes are inside pipes:
;*
;* in that case no collision can occur
;* and the pow has no effect on nmes.
;* should be called before powcontrol
;* and bobcollision
;*

checksafe:	bsr	cnmesafe
		bsr	cmariosafe
		bsr	cluigisafe
		rts

cnmesafe:	move.l	#0,d0
		lea	nmeactive,a0
		lea	nmesafe,a1
		lea	nmeyposition,a2
		lea	nmexposition,a3
		lea	nmeanimbyte,a4
csafeloop:	bsr	csafe
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	csafeloop
		rts

csafe:		cmp.w	#1,(a0,d0)	;active?
		beq.s	csafeact
		cmp.w	#4,(a0,d0)
		beq.s	csafeact
		cmp.w	#5,(a0,d0)
		beq.s	csafeact
		rts
csafeact:	cmp.w	#10,(a2,d0)	;ypos<10
		bgt.s	csafelp
;only upper pipe possible
		cmp.w	#3,(a4,d0)	;nme falling?
		beq.s	csafenset	;yes, exit
		cmp.w	#9,(a4,d0)	;nme stat(coins)
		beq.s	csafenset
csafeset:	move.w	#1,(a1,d0)	;nme = safe
		rts
csafelp:	cmp.w	#167,(a2,d0)	;ypos=167?
		bne.s	csafenset
		cmp.w	#290,(a3,d0)
		bge.s	csafeset	;set safe
		cmp.w	#3,(a3,d0)
		ble.s	csafeset	;set safe
csafenset:	move.w	#0,(a1,d0)	;non-safe
		rts

;check if mario is protected by pipes
;exception for fireball

cmariosafe:	cmp.w	#1,marioactive
		beq.s	cmariosafea
		rts
cmariosafea:	cmp.w	#167,marioypos
		bne.s	msafenset
		cmp.w	#290,marioxpos
		bge.s	msafeset
		cmp.w	#5,marioxpos
		ble.s	msafeset
msafenset:	move.w	#0,mariosafe
		rts
msafeset:	move.w	#1,mariosafe
		rts

;check if luigi is protected by pipes
;exception for fireball

cluigisafe:	cmp.w	#1,luigiactive
		beq.s	cluigisafea
		rts
cluigisafea:	cmp.w	#167,luigiypos
		bne.s	lsafenset
		cmp.w	#290,luigixpos
		bge.s	lsafeset
		cmp.w	#5,luigixpos
		ble.s	lsafeset
lsafenset:	move.w	#0,luigisafe
		rts
lsafeset:	move.w	#1,luigisafe
		rts


;-----------------------------------------------------

;*
;* collisioncheck mario/background:
;*
;* uses platformmap, not file.
;*

colcheck2:
		cmp.w	#6,animbyte	;mario dead?
		beq.s	colchk2ex
		cmp.w	#1,marioactive
		bne.s	colchk2ex
		cmp.w	#0,marioypos
		blt.s	colchk2ex	;neg. ypos: no check
		cmp.w	#35,marioypos	;ypos<35 no coll. on upper pipes
		bhi.s	colchk2nd
colchk2ex:	move.w	#5,maxdown
		rts

colchk2nd:	move.l	#0,d0
		bsr	cchk2high
		bsr	cchk2low
		rts


;*** check collision high ***
cchk2high:
;* check if mario falling down => no coll. high *
		move.w	tabcnt,d0
		lea	jumptable(pc),a0
		adda.w	d0,a0
		move.w	(a0),d0
		bpl.s	cchk2highex	;mario going down, exit
;check pow
;do not check coll. when powlevel=0 and in bonuslevels
;otherwise only check lower part of pow.
		cmp.w	#0,powlevel
		beq.s	cchk2highnp
		cmp.w	#1,bonusflag
		bne.s	cchk2highpc

;no pow or bonusround
cchk2highnp:	cmp.w	#120,marioypos
		blo.s	cchk2highr
		cmp.w	#120,marioxpos
		blo.s	cchk2highr
		cmp.w	#180,marioxpos
		bhi.s	cchk2highr
		rts			;exit; mario in pow-area

cchk2highpc:	cmp.w	#130,marioxpos
		blo.s	cchk2highr
		cmp.w	#180,marioxpos
		bhi.s	cchk2highr
		cmp.w	#137,marioypos
		blo.s	cchk2highr
		cmp.w	#150,marioypos
		bhi.s	cchk2highr
cchk2highex:	move.w	#0,colhflag
		rts			;only lower part of pow
					;can be hit from beneath

;* check platforms *
cchk2highr:

;* get mario x/y-pos *
		move.w	marioypos,d1
		sub.w	#6,d1		;ypos-6
		move.w	marioxposition,d2
		lsr.w	#3,d2

		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	col2hset	;yes!
		add.w	#1,d1		;ypos-5
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	col2hset	;yes!
		add.w	#1,d1		;ypos-4
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	col2hset	;yes!
		add.w	#1,d1		;ypos-3
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	col2hset	;yes!
		bra.s	col2hnset


;* collision high *
col2hset:
;* mario doesn't jump as long as when no collision high *
		cmp.w	#1,chflag	;do only once
		beq.s	col2hsetaj
		add.w	#16,tabcnt	;fall a bit quicker

;* set flags *
col2hsetaj:	move.w	#1,colhflag
		move.w	#1,chflag	;only up once pr. jump
		rts

;* no collision *
col2hnset:	move.w	#0,colhflag
		rts


;*** check coll. low ***
cchk2low:
;check pow
;do not check coll. when powlevel=0 and in bonuslevels
;otherwise only check upper part of pow.
		cmp.w	#0,powlevel
		beq.s	cchk2lownp
		cmp.w	#1,bonusflag	;in bonuslevel?
		bne.s	cchk2lowpc

cchk2lownp:	cmp.w	#110,marioypos
		blo.s	cchk2lowr
		cmp.w	#150,marioypos
		bhi.s	cchk2lowr
		cmp.w	#120,marioxpos
		blo.s	cchk2lowr
		cmp.w	#180,marioxpos
		bhi.s	cchk2lowr
		rts			;exit; mario in pow-area

cchk2lowpc:	cmp.w	#130,marioxpos
		blo.s	cchk2lowr
		cmp.w	#180,marioxpos
		bhi.s	cchk2lowr
		cmp.w	#127,marioypos
		blo.s	cchk2lowr
		cmp.w	#140,marioypos
		bhi.s	cchk2lowr
		rts			;mario can stand on upper
					;part of pow only.
cchk2lowr:	move.l	#0,d1
		move.w	marioypos,d1
		move.w	marioxposition,d2
		lsr.w	#3,d2
		add.w	#21,d1		;d1=ypos+21 (pos. of feet)
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		beq.s	nocol2l1
		move.w	#0,maxdown	;yes.
		rts

nocol2l1:	cmp.w	#1,animbyte
		beq	nc2jump
		cmp.w	#9,animbyte
		beq	nc2jump
		move.w	#46,tabcnt	;init. jumptable
		move.w	#0,jumpbyte
		cmp.w	#2,animbyte
		beq.s	nc2r
		cmp.w	#4,animbyte
		beq.s	nc2r
		cmp.w	#1,facing	;a.byte could be 0
		beq.s	nc2r
		move.w	#0,facing
		move.w	#1,animbyte
		cmp.w	#5,runcnt
		bls.s	nc2falls
		move.w	#2,jumpbyte	;fall left
		bra	nc2jump

nc2r:		move.w	#1,animbyte	
		move.w	#1,facing
		cmp.w	#5,runcnt
		bls.s	nc2falls
		move.w	#1,jumpbyte	;fall right
		bra	nc2jump

nc2falls:	move.w	#0,jumpbyte	;fall straight


;* loop to decide value of maxdown for controlroutine *
nc2jump:	move.w	#0,d7
		move.w	marioypos,a6
		adda.w	#21,a6		;init. ypos
nc2jump2:	adda.w	#1,a6		;next ypos
		move.w	a6,d1
		move.w	marioxposition,d2
		lsr.w	#3,d2
		bsr	chk2collh
		add.w	#1,d7		;current value of maxdown
		cmp.w	#7,d7		;fin?
		beq.s	nocol2l2

		cmp.w	#0,d4
		beq.s	nc2jump2
;* collision *
		move.w	d7,maxdown
		rts
;* no collision *
nocol2l2:	move.w	#5,maxdown	;maxdown = maxvalue
		rts

;-----------------------------------------------------

;*
;* collisioncheck luigi/background:
;*
;* uses platformmap, not file.
;*

colcheckl2:
		cmp.w	#6,animbyteluigi	;luigi dead?
		beq.s	colchkl2ex
		cmp.w	#1,luigiactive
		bne.s	colchkl2ex
		cmp.w	#0,luigiypos
		blt.s	colchkl2ex	;neg. ypos: no check
		cmp.w	#35,luigiypos	;ypos<35 no coll. on upper pipes
		bhi.s	colchkl2nd
colchkl2ex:	move.w	#5,maxdownluigi
		rts

colchkl2nd:	move.l	#0,d0
		bsr	cchkl2high
		bsr	cchkl2low
		rts


;*** check collision high ***
cchkl2high:
;* check if luigi falling down => no coll. high *
		move.w	tabcntluigi,d0
		lea	jumptable(pc),a0
		adda.w	d0,a0
		move.w	(a0),d0
		bpl.s	cchkl2highex	;luigi going down, exit
;check pow
;do not check coll. when powlevel=0 and in bonuslevels
;otherwise only check lower part of pow.
		cmp.w	#0,powlevel
		beq.s	cchkl2highnp
		cmp.w	#1,bonusflag
		bne.s	cchkl2highpc

;no pow or bonusround
cchkl2highnp:	cmp.w	#120,luigiypos
		blo.s	cchkl2highr
		cmp.w	#120,luigixpos
		blo.s	cchkl2highr
		cmp.w	#180,luigixpos
		bhi.s	cchkl2highr
		rts			;exit; luigi in pow-area

cchkl2highpc:	cmp.w	#130,luigixpos
		blo.s	cchkl2highr
		cmp.w	#180,luigixpos
		bhi.s	cchkl2highr
		cmp.w	#137,luigiypos
		blo.s	cchkl2highr
		cmp.w	#150,luigiypos
		bhi.s	cchkl2highr
cchkl2highex:	move.w	#0,colhflagluigi
		rts			;only lower part of pow
					;can be hit from beneath

;* check platforms *
cchkl2highr:

;* get luigi x/y-pos *
		move.w	luigiypos,d1
		sub.w	#6,d1		;ypos-6
		move.w	luigixposition,d2
		lsr.w	#3,d2

		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	coll2hset	;yes!
		add.w	#1,d1		;ypos-5
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	coll2hset	;yes!
		add.w	#1,d1		;ypos-4
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	coll2hset	;yes!
		add.w	#1,d1		;ypos-3
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		bne.s	coll2hset	;yes!
		bra.s	coll2hnset


;* collision high *
coll2hset:
;* luigi doesn't jump as long as when no collision high *
		cmp.w	#1,chflagluigi	;do only once
		beq.s	coll2hsetaj
		add.w	#16,tabcntluigi	;fall a bit quicker

;* set flags *
coll2hsetaj:	move.w	#1,colhflagluigi
		move.w	#1,chflagluigi	;only up once pr. jump
		rts

;* no collision *
coll2hnset:	move.w	#0,colhflagluigi
		rts


;*** check coll. low ***
cchkl2low:
;check pow
;do not check coll. when powlevel=0 and in bonuslevels
;otherwise only check upper part of pow.
		cmp.w	#0,powlevel
		beq.s	cchkl2lownp
		cmp.w	#1,bonusflag	;in bonuslevel?
		bne.s	cchkl2lowpc

cchkl2lownp:	cmp.w	#110,luigiypos
		blo.s	cchkl2lowr
		cmp.w	#150,luigiypos
		bhi.s	cchkl2lowr
		cmp.w	#120,luigixpos
		blo.s	cchkl2lowr
		cmp.w	#180,luigixpos
		bhi.s	cchkl2lowr
		rts			;exit; luigi in pow-area

cchkl2lowpc:	cmp.w	#130,luigixpos
		blo.s	cchkl2lowr
		cmp.w	#180,luigixpos
		bhi.s	cchkl2lowr
		cmp.w	#127,luigiypos
		blo.s	cchkl2lowr
		cmp.w	#140,luigiypos
		bhi.s	cchkl2lowr
		rts			;luigi can stand on upper
					;part of pow only.
cchkl2lowr:	move.l	#0,d1
		move.w	luigiypos,d1
		move.w	luigixposition,d2
		lsr.w	#3,d2
		add.w	#21,d1		;d1=ypos+21 (pos. of feet)
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		beq.s	nocoll2l1
		move.w	#0,maxdownluigi	;yes.
		rts

nocoll2l1:	cmp.w	#1,animbyteluigi
		beq	ncl2jump
		cmp.w	#9,animbyteluigi
		beq	ncl2jump
		move.w	#46,tabcntluigi	;init. jumptable
		move.w	#0,jumpbyteluigi
		cmp.w	#2,animbyteluigi
		beq.s	ncl2r
		cmp.w	#4,animbyteluigi
		beq.s	ncl2r
		cmp.w	#1,facingluigi	;a.byte could be 0
		beq.s	ncl2r
		move.w	#0,facingluigi
		move.w	#1,animbyteluigi
		cmp.w	#5,runcntluigi
		bls.s	ncl2falls
		move.w	#2,jumpbyteluigi	;fall left
		bra	ncl2jump

ncl2r:		move.w	#1,animbyteluigi
		move.w	#1,facingluigi
		cmp.w	#5,runcntluigi
		bls.s	ncl2falls
		move.w	#1,jumpbyteluigi	;fall right
		bra	ncl2jump

ncl2falls:	move.w	#0,jumpbyteluigi	;fall straight

;* loop to decide value of maxdown for controlroutine *
ncl2jump:	move.w	#0,d7
		move.w	luigiypos,a6
		adda.w	#21,a6		;init. ypos
ncl2jump2:	adda.w	#1,a6		;next ypos
		move.w	a6,d1
		move.w	luigixposition,d2
		lsr.w	#3,d2
		bsr	chk2collh
		add.w	#1,d7		;current value of maxdown
		cmp.w	#7,d7		;fin?
		beq.s	nocoll2l2

		cmp.w	#0,d4
		beq.s	ncl2jump2
;* collision *
		move.w	d7,maxdownluigi
		rts
;* no collision *
nocoll2l2:	move.w	#5,maxdownluigi	;maxdown = maxvalue
		rts


;--------------------------------------------------------

;*
;* collision check.
;* in: d1=ypos,d2=xpos.
;* out: d4=collision yes/no. (1/0)
;*

chk2collh:	lea	platforms,a0

chk2colloop:	move.w	(a0),d3		;d2=platform ypos
		cmp.w	#$8000,d3	;fin?
		beq.s	chk2collf

;* collision ypos? *
		cmp.w	d3,d1		;plr. below platform?
		blt.s	chk2collnyc

;* plr. below platform *
		;move.w	d1,d4		;d4=plry
		;sub.w	d3,d4		;d1=plrypos-pltfypos
		cmp.w	d3,d1
		bne.s	chk2collnyc	;ypos equal?
		bra.s	chk2collyc

;* no ycoll, goto next platform *
chk2collnyc:	adda.l	#6,a0		;next
		bra.s	chk2colloop


;* ycollision; check xcol; leftedge *
chk2collyc:	adda.l	#2,a0
		move.w	(a0),d3		;d3=platform leftxpos
		cmp.w	d3,d2
		bgt.s	chk2collxr	;plrxpos>leftedgex?
		move.w	d2,d4		;d4=plrx
		sub.w	d4,d3		;d3=leftx-plrx
		cmp.w	#16,d3
		bgt.s	chk2collnxl	;xdist>16?

;* collision; left edge *
		bra.s	chk2collfc

;* no coll. at left edge *
chk2collnxl:	adda.l	#4,a0
		bra.s	chk2colloop

;* check right edge *
chk2collxr:	adda.l	#2,a0
		move.w	(a0),d3		;d3=platform rightxpos
		cmp.w	d3,d2
		bgt.s	chk2collnxr	;plrx>rightx?

;* plrx<=rightx *
chk2collxnc:	move.w	d3,d4		;d4=pltfrightx
		sub.w	d2,d4		;d3=rightx-plrx
		cmp.w	#8,d4
		blt.s	chk2collnxr	;rightx-plrx<8?
;* collision *
		bra.s	chk2collfc


;* no coll at right edge *
chk2collnxr:	adda.l	#2,a0
		bra.s	chk2colloop


;* no collision *
chk2collf:	move.w	#0,d4
		rts

;* collision! *
chk2collfc:	move.w	#1,d4
		rts


;----------------------------------------------------


;----------------------------------------------------

;*
;* collision check (nme/background):
;* no type-test, except for fireball
;* which has no collision test,
;* no collisiontest in bonusrounds!
;* v2.0.
;*

nmecol2:	cmp.w	#1,bonusflag	;bonus round?
		bne.s	nmecol2nb
		rts

nmecol2nb:
		move.l	#0,d0
nmecol2lp:	lea	nmeactive,a6
		bsr	nmecol2j
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	nmecol2lp		
		rts

nmecol2j:	cmp.w	#1,(a6,d0)	;nme active?
		beq.s	ncol2actv
		cmp.w	#4,(a6,d0)
		beq.s	ncol2actv
		cmp.w	#5,(a6,d0)
		beq.s	ncol2actv
		rts
ncol2actv:	lea	nmeanimbyte,a3
		cmp.w	#8,(a3,d0)
		bne.s	ncol2ndead	;not dead
		rts
ncol2ndead:	lea	nmetype,a3
		cmp.w	#6,(a3,d0)	;fireball?
		bne.s	ncol2nfire	;yes exit
		rts
ncol2nfire:	lea	nmeyposition,a3
		lea	nmetabcnt,a5
		lea	nmefacing,a4
		lea	nmemaxdown,a2
		lea	nmexposition,a1
		cmp.w	#110,(a3,d0)
		blo.s	ncol2np
		cmp.w	#150,(a3,d0)
		bhi.s	ncol2np
		cmp.w	#120,(a1,d0)
		blo.s	ncol2np
		cmp.w	#180,(a1,d0)
		bhi.s	ncol2np
		move.w	#5,(a2,d0)	;maxdown=5
		rts			;exit; nme in pow-area

ncol2np:	move.l	#0,d1
		move.w	(a1,d0),d2	;nmex
		move.w	(a3,d0),d1	;nmey
		lea	nmeanimbyte,a3

		add.w	#21,d1
		bsr	chk2collh
		cmp.w	#0,d4		;collision?
		beq.s	nmenocol2l

		move.w	#0,(a2,d0)	;maxdown=0
		rts

nmenocol2l:	cmp.w	#3,(a3,d0)	;falling?
		beq.s	nmenc2jump

		cmp.w	#4,(a3,d0)	;hit?
		blt.s	nmecol2nhit
		bra	nmenc2jump	;calc. maxdown
		rts

nmecol2nhit:	lea	nmetype,a3
		cmp.w	#3,(a3,d0)	;fly?
		beq.s	nmenc2jump	;yes, do not fall
		lea	nmeanimbyte,a3
		move.w	#46,(a5,d0)	;tabcnt=46
		cmp.w	#1,(a3,d0)	;going right?
		beq.s	nmec2gor
		move.w	#2,(a4,d0)	;fall left
		bra	nmenc2j2
nmec2gor:	move.w	#1,(a4,d0)	;fall right
nmenc2j2:	move.w	#3,(a3,d0)	;start fall

;* loop to decide maxdown *
nmenc2jump:	move.w	#0,d5
		lea	nmeyposition,a5
		lea	nmexposition,a4
		move.w	(a5,d0),a6
		adda.w	#21,a6
nmecol2lp3:	adda.w	#1,a6
		move.w	a6,d1
		move.w	(a4,d0),d2
		bsr	chk2collh
		add.w	#1,d5
		cmp.w	#5,d5		;fin?
		bne.s	nmec2noex
		move.w	#5,(a2,d0)	;maxdown=5
		rts
nmec2noex:	cmp.w	#1,d4		;collision?
		bne.s	nmecol2lp3
		move.w	d5,(a2,d0)	;maxdown=d5
		rts



;----------------------------------------------------------

;*
;* jump screen when pow is hit:
;* should be done after shiftscreen
;*

jumpscreen:	cmp.w	#0,pause
		beq.s	jumpscrok
		rts
jumpscrok:	cmp.w	#1,powhitmario
		beq.s	jumpscok
		cmp.w	#1,powhitluigi
		beq.s	jumpscok
		rts

jumpscok:	move.l	jumpsccnt,d0
		lea	jumpsctable(pc),a0
		move.l	(a0,d0),d1
		cmp.l	#$80,d1		;end of jump?
		beq.L	jumpscfin

		move.w	plane1+2,d2
		swap	d2
		move.w	plane1+6,d2
		add.l	d1,d2
		move.w	d2,plane1+6	;jump
		swap	d2
		move.w	d2,plane1+2

		move.w	plane2+2,d2
		swap	d2
		move.w	plane2+6,d2
		add.l	d1,d2
		move.w	d2,plane2+6	;jump
		swap	d2
		move.w	d2,plane2+2

		move.w	plane3+2,d2
		swap	d2
		move.w	plane3+6,d2
		add.l	d1,d2
		move.w	d2,plane3+6	;jump
		swap	d2
		move.w	d2,plane3+2

		move.w	plane4+2,d2
		swap	d2
		move.w	plane4+6,d2
		add.l	d1,d2
		move.w	d2,plane4+6	;jump
		swap	d2
		move.w	d2,plane4+2
		add.l	#4,jumpsccnt
		rts
jumpscfin:	move.l	#0,jumpsccnt
		rts

jumpsctable:	dc.l	40,80,120,120,160,160,200,200,200
		dc.l	240,240,240,240,280,280,280,280
		dc.l	240,240,240,240,200,200,200,160,160,120
		dc.l	120,80,40,0,$80
jumpsccnt:	dc.l	0


;----------------------------------------------------

;*
;* control pow:
;*
;* NB! tests for type.
;*

powcontrol:	bsr	powcontmario
		bsr	powcontluigi
		rts

powcontmario:	cmp.w	#1,marioactive
		beq.s	powcmarioact
		rts
powcmarioact:	cmp.w	#6,animbyte	;dead?
		bne.s	powcmariond
		rts
powcmariond:	cmp.w	#0,powlevel
		beq.s	powcmex		;no more pows
		cmp.w	#1,powhitmario	;pow already hit in this jump?
		beq.s	powcmex
		cmp.w	#1,colhflag
		beq.s	powcmstart
powcmex:	rts
powcmstart:	cmp.w	#120,marioypos
		blo.s	powcmex	
		cmp.w	#120,marioxpos
		blo.s	powcmex
		cmp.w	#180,marioxpos
		bhi.s	powcmex		

		sub.w	#1,powlevel	;one pow less		
		move.w	#1,powhitmario	;indicate that pow has been
					;hit in this jump
;hit all enemies and luigi, if on the ground
		cmp.w	#0,animbyteluigi
		beq.s	powhl
		cmp.w	#2,animbyteluigi
		beq.s	powhl
		cmp.w	#3,animbyteluigi
		beq.s	powhl
		cmp.w	#4,animbyteluigi
		beq.s	powhl
		cmp.w	#5,animbyteluigi
		beq.s	powhl
		move.w	#1,powwhohit	;mario hit pow

		;hit nmes
powhitb:	lea	nmeactive,a0
		lea	nmesafe,a1
		lea	nmeanimbyte,a3
		lea	nmetype,a4
		lea	nmexoffset,a2
		lea	nmehitflag,a5
		move.l	#0,d0
powcmloop:	bsr	powcmnmehit
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	powcmloop
		rts

;* luigi hit *

powhl:		move.w	#9,animbyteluigi
		move.w	#1,powwhohit
		bra	powhitb


powcmnmehit:	cmp.w	#1,(a0,d0)	;nme active?
		beq.s	powcmnmehit1
		rts
powcmnmehit1:	cmp.w	#5,(a3,d0)	;nme dead?
		beq.s	powcmnmehitex
		cmp.w	#8,(a3,d0)	;nme rehit?
		beq.s	powcmnmehitex
		cmp.w	#4,(a3,d0)	;nme hit?
		beq.s	powcmnmehitex
		cmp.w	#1,(a1,d0)	;nme safe?
		beq.s	powcmnmehitex
		bra	powcmnmehitok
powcmnmehitex:	rts

;* type of enemy? *
powcmnmehitok:	cmp.w	#1,(a4,d0)	;turtle?
		beq	powcmtuhit
		cmp.w	#2,(a4,d0)	;lobster?
		beq	powcmlobhit
		cmp.w	#3,(a4,d0)	;fly?
		beq	powcmflyhit
		cmp.w	#4,(a4,d0)	;iceman?
		beq	powcmicehit
		cmp.w	#5,(a4,d0)	;coin?
		beq	powcmcoinhit
		rts


;* turtle hit *
powcmtuhit:	cmp.w	#0,(a5,d0)
		bne.s	powcmtuhita	;hit allowed (=1)
		rts
powcmtuhita:	cmp.w	#3,(a3,d0)	;nme falling?
		beq.s	powcmnmehitex

		move.w	#0,(a5,d0)	;hit not allowed
		cmp.w	#6,(a3,d0)
		beq.s	powcmturehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;turtle=hit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;x-offset is calc. relative to where mario/luigi is hitting the pow
		cmp.w	#1,powwhohit
		beq.s	powcmtumhit
		move.w	luigixpos,d1
		bra	powcmtumhitc
powcmtumhit:	move.w	marioxpos,d1
powcmtumhitc:	lsr.w	#1,d1
		sub.w	#75,d1		;center of pow/2
		move.w	d1,(a2,d0)	;x-offset
		rts

powcmturehit:	move.w	#8,(a3,d0)	;turtle=rehit
		lea	nmetabcnt,a6
		move.w	#10,(a6,d0)
		lea	nmetimer1,a6
		move.w	#0,(a6,d0)
		rts

;* lobster hit *
powcmlobhit:	cmp.w	#3,(a3,d0)	;nme falling?
		beq.s	powcmlobhitex

		cmp.w	#6,(a3,d0)
		beq.s	powcmlobrehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;lobster hit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt
powcmlobhitex:	rts

powcmlobrehit:	move.w	#8,(a3,d0)	;lobster=rehit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts

;* fly hit *
powcmflyhit:	cmp.w	#0,(a5,d0)
		bne.s	powcmflyhita	;hit allowed (=1)
		rts
powcmflyhita:	move.w	#0,(a5,d0)	;hit not allowed
		cmp.w	#6,(a3,d0)
		beq.L	powcmturehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;fly=hit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;x-offset is calc. relative to where mario/luigi is hitting the pow
		cmp.w	#1,powwhohit
		beq.s	powcmflymhit
		move.w	luigixpos,d1
		bra	powcmflymhitc
powcmflymhit:	move.w	marioxpos,d1
powcmflymhitc:	lsr.w	#1,d1
		sub.w	#75,d1		;center of pow/2
		move.w	d1,(a2,d0)	;x-offset
		rts

powcmflyrehit:	move.w	#8,(a3,d0)	;fly=rehit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts


;* ice hit *
powcmicehit:	cmp.w	#0,(a5,d0)
		bne.s	powcmicehita	;hit allowed (=1)
		rts
powcmicehita:	cmp.w	#3,(a3,d0)	;nme falling?
		beq.L	powcmnmehitex

		move.w	#0,(a5,d0)	;hit not allowed
		move.w	#4,(a3,d0)	;ice=hit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;x-offset is calc. relative to where mario/luigi is hitting the pow
		cmp.w	#1,powwhohit
		beq.s	powcmicemhit
		move.w	luigixpos,d1
		bra	powcmicemhitc
powcmicemhit:	move.w	marioxpos,d1
powcmicemhitc:	lsr.w	#1,d1
		sub.w	#75,d1		;center of pow/2
		move.w	d1,(a2,d0)	;x-offset
		rts

;* coin hit *
powcmcoinhit:	cmp.w	#0,(a5,d0)
		bne.s	powcmcoinhita	;hit allowed (=1)
		rts
powcmcoinhita:	cmp.w	#3,(a3,d0)	;nme falling?
		beq.L	powcmnmehitex

		move.w	#0,(a5,d0)	;hit not allowed
		move.w	#4,(a3,d0)	;coin=hit
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;x-offset is calc. relative to where mario/luigi is hitting the pow
		cmp.w	#1,powwhohit
		beq.s	powcmcoinmhit
		move.w	luigixpos,d1
		bra	powcmcoinmhitc
powcmcoinmhit:	move.w	marioxpos,d1
powcmcoinmhitc:	lsr.w	#1,d1
		sub.w	#75,d1		;center of pow/2
		move.w	d1,(a2,d0)	;x-offset
		rts





;*
;* luigi:
;* uses same code for hitting all enemies
;* as mario does.
;*

powcontluigi:	cmp.w	#1,luigiactive
		beq.s	powcluigiact
		rts
powcluigiact:	cmp.w	#6,animbyteluigi	;dead?
		bne.s	powcluigind
		rts
powcluigind:	cmp.w	#0,powlevel
		beq.s	powclex		;no more pows?
		cmp.w	#1,powhitluigi	;pow already hit in this jump?
		beq.s	powclex
		cmp.w	#1,colhflagluigi
		beq.s	powclstart
powclex:	rts
powclstart:	cmp.w	#120,luigiypos
		blo.s	powclex	
		cmp.w	#120,luigixpos
		blo.s	powclex
		cmp.w	#180,luigixpos
		bhi.s	powclex

		sub.w	#1,powlevel	;one pow less		
		move.w	#1,powhitluigi	;indicate that pow has been
					;hit in this jump
;hit all enemies and mario, if on the ground
		cmp.w	#0,animbyte
		beq.s	powhm
		cmp.w	#2,animbyte
		beq.s	powhm
		cmp.w	#3,animbyte
		beq.s	powhm
		cmp.w	#4,animbyte
		beq.s	powhm
		cmp.w	#5,animbyte
		beq.s	powhm
		move.w	#2,powwhohit
		bra	powhitb		;code above

;* mario hit *
powhm:		move.w	#9,animbyte
		move.w	#2,powwhohit
		bra	powhitb


powwhohit:	dc.w	0	;1=mario, other=luigi


		
;----------------------------------------------------

;*
;* control levels v1.1
;*
;* adr. of 1. level will be written by initgame!
;* NB! tests type.
;*

levelcontrol:	cmp.w	#0,pause
		beq.s	levcontok
		rts

levcontok:	add.w	#1,levelcnt
		move.w	levelpnt,d0
		move.l	levelno,a0	;adr. of current leveldata
		add.w	d0,a0
		move.w	(a0),d1		;current entry
		cmp.w	#$80,d1		;end of level?
		beq.L	levcontnext	;set to next level
		cmp.w	#$8000,d1	;bonus level?
		beq	levcontbonus
		cmp.w	levelcnt,d1	;time to launch enemy?
		bne.s	levcontend

;find 1.nme not activated
		lea	nmeactive(pc),a1
		move.l	#0,d0
		move.l	#0,d7
levcontloop:	cmp.w	#0,(a1,d0)	;nme active?
		beq.s	levcontinsert	;insert new nme
		add.w	#2,d0
		add.w	#4,d7
		cmp.w	#20,d0		;fin?
		bne.s	levcontloop
		sub.w	#1,levelcnt	;nme must be activated
levcontend:	rts			;even if gamesystem
					;cannot handle more nmes.

;init. nme vars.
levcontinsert:	move.w	#4,(a1,d0)	;nme=active
;#4 indicates that no restoring should be done the first 2 frames
;#1 will be written by restore routine

		lea	nmetype(pc),a2
		add.w	#2,a0		;which type of nme?
		move.w	(a0),d1
		move.w	d1,(a2,d0)	;d0=available nmeoffset
					;d7 for longword
;set speed depending on type
		lea	nmespeed(pc),a1
		cmp.w	#1,d1		;turtle?
		beq.s	levcontinstu
		cmp.w	#2,d1		;lobster?
		beq.s	levcontinslo
		cmp.w	#3,d1		;fly?
		beq.s	levcontinsfly
		cmp.w	#4,d1		;iceman
		beq.s	levcontinsice
		cmp.w	#5,d1		;coin?
		beq.s	levcontinsco
		rts			;should never be done!


levcontinstu:	move.w	#4,(a1,d0)	;speed=4
		bra	levcontinsa
levcontinslo:	move.w	#6,(a1,d0)	;speed=6
		bra	levcontinsa
levcontinsice:	move.w	#6,(a1,d0)	;speed=6
		bra	levcontinsa
levcontinsco:	move.w	#8,(a1,d0)	;speed=8
		bra	levcontinsa
levcontinsfly:	move.w	#4,(a1,d0)	;speed=4
		bra	levcontinsa


levcontinsa:	add.w	#2,a0
		lea	nmefacing(pc),a1
		move.w	(a0),d1
		move.w	d1,(a1,d0)	;face in dir. from table
		lea	nmeanimbyte(pc),a1
		move.w	d1,(a1,d0)	;walk in dir. from table
;set xpos
		lea	nmexpos(pc),a1
		lea	nmexposition(pc),a2
		cmp.w	#1,d1		;right?
		bne.s	levcontinsrgt
		move.w	#40,(a1,d0)	;xpos=40 (=5)
		move.w	#5,(a2,d0)
		move.w	#1,nmeleftlaunch ;signal to audio
		bra	levcontinsc
levcontinsrgt:	move.w	#2320,(a1,d0)	;xpos=2320 (=290)
		move.w	#290,(a2,d0)
		move.w	#1,nmerightlaunch ;signal to audio
levcontinsc:	lea	nmetabcnt(pc),a1
		move.w	#46,(a1,d0)	;tabcnt=46
		lea	nmeaggr(pc),a1
		move.w	#0,(a1,d0)	;aggr=0
		lea	nmehitflag(pc),a1
		move.w	#1,(a1,d0)	;hitflag=1
		lea	nmeyposition(pc),a1
		move.w	#6,(a1,d0)	;ypos=6
		add.w	#6,levelpnt	;point to next nme
		lea	nmetimer1(pc),a1
		move.w	#0,(a1,d0)	;timer1=0
		lea	nmetimer2(pc),a1
		move.w	#0,(a1,d0)	;timer2=0
		lea	nmesource(pc),a1
		lea	nmesource2(pc),a2
		move.l	screen,d1
		move.w	d0,d7
		asl.w	#1,d7
		move.l	d1,(a1,d7)	;nmesource=ds
		move.l	d1,(a2,d7)	;nmesource2=ds
		lea	nmebobadr(pc),a1
		lea	nmemaskadr(pc),a2
		move.l	gfx1,d1
		add.l	#7932,d1
		move.l	d1,(a1,d7)	;print void at start
		move.l	mask1,d1
		add.l	#7932,d1
		move.l	d1,(a2,d7)
levcontnex:	rts



;*** next level ***

levcontnext:	lea	nmeactive(pc),a1
;check if all nmes are dead
		move.l	#0,d0
levcontnlp:	cmp.w	#1,(a1,d0)
		beq.s	levcontnex	;1 nme still active=>exit
		cmp.w	#2,(a1,d0)
		beq.s	levcontnex	;dead but not off screen!
		cmp.w	#3,(a1,d0)
		beq.s	levcontnex
		cmp.w	#4,(a1,d0)	;just activated
		beq.s	levcontnex
		cmp.w	#5,(a1,d0)
		beq.s	levcontnex
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	levcontnlp

;* pause, if not resuming game! *
		cmp.w	#1,resume	;resuming game!
		beq.s	levcnins

		move.w	#1,pause	;pause game
		move.w	#0,resume 
		rts

;* insert new level *
levcnins:	move.w	#0,resume	;game resumed!
		move.l	levelno,a0
		move.w	levelpnt,d0
		add.w	d0,a0
		add.w	#2,a0
		move.w	(a0),d1
		cmp.w	#$80,d1		;end of game?
		beq.s	levcontgo
		move.l	a0,levelno	;insert new level
		move.w	#0,levelpnt
		move.w	#0,levelcnt
		add.w	#1,levelnumber
;* passed 10n? *
		cmp.w	#10,levelnumber
		bne.s	levcontnii
		move.w	#0,levelnumber
		add.w	#1,levelnumten

;* reset icetimer *
levcontnii:	move.w	#0,icetimer
		move.w	#0,icetimer2

;* initialize mario/luigi variables *
		jsr	initmario
		jsr	initluigi
levcontnend:	rts

;* last level => game over *
levcontgo:	move.w	#1,gameover	;game over!
		rts
	

;** start bonus level **
levcontbonus:
;check if all nmes are dead
		lea	nmeactive(pc),a1
		move.l	#0,d0
levcontblp:	cmp.w	#1,(a1,d0)
		beq.L	levcontnex	;1 nme still active=>exit
		cmp.w	#2,(a1,d0)
		beq.L	levcontnex	;dead but not off screen!
		cmp.w	#3,(a1,d0)
		beq.l	levcontnex	;ditto
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	levcontblp

		add.w	#2,levelpnt	;point to $80
;indicate to gamecontrol that bonuslevel has started
		move.w	#1,bonusflag
		move.w	#0,bonustimer

;launch all coins
		move.l	#0,d0
		move.l	#0,d7
levclacoinlp:	bsr	levcinscoin
		add.w	#2,d0
		add.w	#4,d7
		cmp.w	#20,d0		;insert ten coins
		bne.s	levclacoinlp
		bsr	levccoinsetxy	;set x,ypos
		jsr	initmario
		jsr	initluigi
		rts


;insert variables for coins that are the same for all

levcinscoin:	lea	nmeactive(pc),a1
		move.w	#4,(a1,d0)	;nme=active(no restore)
		lea	nmetype(pc),a2
		move.w	#5,(a2,d0)	;type=coin
		lea	nmespeed(pc),a1
		move.w	#0,(a1,d0)	;speed=0
		lea	nmefacing(pc),a1
		move.w	#1,(a1,d0)
		lea	nmeanimbyte(pc),a1
		move.w	#9,(a1,d0)	;static
		lea	nmetabcnt(pc),a1
		move.w	#46,(a1,d0)	;tabcnt=46
		lea	nmeaggr(pc),a1
		move.w	#0,(a1,d0)	;aggr=0
		lea	nmehitflag(pc),a1
		move.w	#1,(a1,d0)	;hitflag=1
		lea	nmetimer1(pc),a1
		move.w	#0,(a1,d0)	;timer1=0
		lea	nmetimer2(pc),a1
		move.w	#0,(a1,d0)	;timer2=0
		lea	nmehitcnt(pc),a1
		move.w	#0,(a1,d0)	;hitcnt=0
		lea	nmesafe(pc),a1
		move.w	#0,(a1,d0)	;not safe
		lea	nmesource(pc),a1
		lea	nmesource2(pc),a2
		move.l	screen,d1
		move.w	d0,d7
		asl.w	#1,d7
		move.l	d1,(a1,d7)	;nmesource=ds
		move.l	d1,(a2,d7)	;nmesource2=ds
		lea	nmebobadr(pc),a1
		lea	nmemaskadr(pc),a2
		move.l	gfx1,d1
		add.l	#7932,d1
		move.l	d1,(a1,d7)	;print void at start
		move.l	mask1,d1
		add.l	#7932,d1
		move.l	d1,(a2,d7)
		rts


;set xpos
levccoinsetxy:	lea	nmexpos(pc),a1
		lea	nmexposition(pc),a2
		lea	nmeyposition(pc),a3
		move.l	#0,d0
		move.w	#2000,(a1,d0)	;1.
		move.w	#250,(a2,d0)
		move.w	#3,(a3,d0)
		add.w	#2,d0
		move.w	#336,(a1,d0)	;2.
		move.w	#42,(a2,d0)
		move.w	#3,(a3,d0)
		add.w	#2,d0
		move.w	#2320,(a1,d0)	;3.
		move.w	#290,(a2,d0)
		move.w	#62,(a3,d0)
		add.w	#2,d0
		move.w	#240,(a1,d0)	;4.
		move.w	#30,(a2,d0)
		move.w	#62,(a3,d0)
		add.w	#2,d0
		move.w	#1840,(a1,d0)	;5.
		move.w	#230,(a2,d0)
		move.w	#62,(a3,d0)
		add.w	#2,d0
		move.w	#560,(a1,d0)	;6.
		move.w	#70,(a2,d0)
		move.w	#62,(a3,d0)
		add.w	#2,d0
		move.w	#1840,(a1,d0)	;7.
		move.w	#230,(a2,d0)
		move.w	#105,(a3,d0)
		add.w	#2,d0
		move.w	#560,(a1,d0)	;8.
		move.w	#70,(a2,d0)
		move.w	#105,(a3,d0)
		add.w	#2,d0
		move.w	#2000,(a1,d0)	;9.
		move.w	#250,(a2,d0)
		move.w	#147,(a3,d0)
		add.w	#2,d0
		move.w	#320,(a1,d0)	;10.
		move.w	#40,(a2,d0)
		move.w	#147,(a3,d0)
		rts

;----------------------------------------------------

;*
;* check for pausekey pressed:
;*
;* keytest should be called in mainloop.
;*

pausekey:
;is key f1?
		move.b	newkey,d0
		cmp.b	#$5f,d0
		bne.s	pkeysal
;was key in last frame also f1?
		cmp.b	oldkey,d0
		beq.s	pkeysal		;yes, same as last

;f1 pressed, (un)pause game
		cmp.w	#1,pkey		;game paused?
		bne.s	pkeysp		;no, pause game!
		move.w	#0,pkey		;yes, restart game!
		move.w	#0,pause
		move.w	#0,pausecnt
		bra	pkeysal

pkeysp:		move.w	#1,pkey
		move.w	#1,pause
pkeysal:	move.b	d0,oldkey	;save key-code

		cmp.w	#1,pkey		;game paused?
		bne.s	pkeyexit	;exit
		move.w	#0,pausecnt	;do not resume after 2 secs.

pkeyexit:	rts

pkey:		dc.w	0

;---------------------------------------------------

;*
;* check for quitkey pressed:
;*

quitkey:

;is key 'f10'?
		cmp.b	#$4d,newkey
		bne.s	quitkex
		move.w	#1,quit		;quit game!
		rts
quitkex:	move.w	#0,quit		;no quit!
		rts

quit:		dc.w	0

;---------------------------------------------------

;*
;* check for exittodos pressed:
;*

exittodoskey:

;is key 'f9'?
		cmp.b	#$4f,newkey
		bne.s	extodoskex
		move.w	#1,extodos	;exit to dos!
		rts
extodoskex:	move.w	#0,extodos	;no exit to dos!
		rts

extodos:	dc.w	0

;---------------------------------------------------

;*
;* pause game:
;*
;* used when levels are finished,
;* and when 'p' is pressed.
;*


pausecont:	cmp.w	#1,pause	;game paused?
		bne.s	pauseexit
	
		add.w	#1,pausecnt
		cmp.w	#100,pausecnt	;2 secs.?
		bls.s	pauseexit
;pause fin.
		move.w	#1,resume	;resume game
		move.w	#0,pausecnt	;reset timer
		move.w	#0,pause

pauseexit:	rts

pause:		dc.w	0
pausecnt:	dc.w	0
resume:		dc.w	0




;-------------------------------------------------------


;*
;* control gamelogic:
;*
;* note to gcbonuslevel!
;* levelcontrol writes #1 to bonusflag
;* and gamecontrol writes #1 to bonusprint
;* every 1/10 of a second
;* and printbonus prints time
;* and sets bonusprint to #0.
;*

gamecontrol:	cmp.w	#0,pause
		beq.s	gamecontok
		rts
gamecontok:	bsr	gclivesm
		bsr	gclivesl
		bsr	gcgameover
		bsr	gcbonuslevel
		bsr	gcnooffire
		bsr	gcfirelaunch
		bsr	gclastnme
		bsr	gcicelaunch
		rts

;** check mario no lives left **
gclivesm:	cmp.w	#1,marioactive
		beq.s	gclivmact
		rts	
gclivmact:	cmp.w	#0,lives
		bne.s	gclivesex
		move.w	#2,marioactive	;game over
gclivesex:	rts


;** check luigi no lives left **
gclivesl:	cmp.w	#1,luigiactive
		beq.s	gclivlact
		rts	
gclivlact:	cmp.w	#0,livesluigi
		bne.s	gclivesex
		move.w	#2,luigiactive	;game over
		rts


;** check for game-over **
gcgameover:	cmp.w	#1,gameover
		beq.s	gcfrz
		cmp.w	#0,marioactive
		bne.s	gcmnactex
		cmp.w	#0,luigiactive
		bne.s	gcmnactex
		bra	gcfrz		;game-over!!!
gcmnactex:	rts


;* game-over *
;* gameover will be set by fadeout *
gcfrz:		cmp.w	#0,fadeoutcnt
		bne.s	gcnogoex
		move.w	#50,fadeoutcnt	;start fade-out
		move.w	#1,printgoflag	;print game over, once
gcnogoex:	rts


;** bonuslevel **
gcbonuslevel:	cmp.w	#1,bonusflag	;bonus level running?
		beq.s	gcbonusrun
		rts
;running
gcbonusrun:	add.w	#1,bonustimer
		cmp.w	#5,bonustimer
		bne.s	gcnogoex	;1/10 sec?
		move	#0,bonustimer
		sub.w	#1,tenthofsec
		cmp.w	#-1,tenthofsec
		bne.s	gcnogoto
		sub.w	#1,secs
		move.w	#9,tenthofsec
		cmp.w	#-1,secs
		bne.s	gcnogoto
		sub.w	#1,tensecs
		move.w	#9,secs
		cmp.w	#-1,tensecs	;has time run out?
		bne.s	gcnogoto
;disable all coins
;and reset timer
		move.w	#0,bonusprint	;delete time
		move.w	#0,bonustimer
		move.w	#0,bonusflag

		move.l	#0,d0
		lea	nmeactive(pc),a0
gcnogolp:	move.w	#2,(a0,d0)	;disactivate
		add.w	#2,d0
		cmp.w	#20,d0		;10 coins
		bne.s	gcnogolp
		move.w	#2,bonusprint	;delete time
		rts

gcnogoto:	move.w	#1,bonusprint	;print time remaining
		rts


;----------------------------------------------------

;*
;* check if fireball is to be launched:
;*
;* if mmario/luigi stands on same platform for
;* 10 secs then launch fireball.
;* <10 secs in later rounds.
;* NB! not in bonuslevel.
;* only 1 fireball onscreen.
;* more fireballs in later rounds.
;*

gcfirelaunch:	bsr	gcfblm
		bsr	gcfbll
		rts

;*** mario ***
gcfblm:		cmp.w	#1,marioactive
		beq.s	gcfblmact
		rts
gcfblmact:	cmp.w	#1,bonusflag	;bonusround?
		bne.s	gcfblnb
		rts
gcfblnb:	cmp.w	#30,marioypos	;marioypos<30?
		bge.s	gcfblyok
		rts
gcfblyok:	move.w	marioypos,d1
		cmp.w	oldmarioy,d1	;same ypos as last?
		bne.s	gcfblmrestime	;no reset timer
		add.w	#1,fbtimerm

;* 10 secs-16*level# elapsed? *
;* 5 secs=minimum *
		move.w	#500,d0
		move.w	levelnumber,d1
		asl.w	#4,d1
		sub.w	d1,d0		;d0=500-16*lev#
		cmp.w	#250,d0
		bge.s	gcfblmnlim
		move.w	#250,d0

gcfblmnlim:	cmp.w	fbtimerm,d0
		bne.s	gcfblmex
		move.w	marioypos,d1
		move.w	marioxpos,d2

;* max no of fireball already launched? *
		move.w	maxnooffb,d0
		cmp.w	firelaunched,d0
		beq.s	gcfblmrestime
		bsr	gcfblins

;* reset timer *
gcfblmrestime:	move.w	#0,fbtimerm
gcfblmex:	move.w	marioypos,oldmarioy	;save ypos
		rts


;*** luigi ***
gcfbll:		cmp.w	#1,luigiactive
		beq.s	gcfbllact
		rts
gcfbllact:	cmp.w	#1,bonusflag	;bonusround?
		bne.s	gcfbllnb
		rts
gcfbllnb:	cmp.w	#30,luigiypos	;luigiypos<30?
		bge.s	gcfbllyok
		rts
gcfbllyok:	move.w	luigiypos,d1
		cmp.w	oldluigiy,d1	;same ypos as last?
		bne.s	gcfbllrestime	;no reset timer
		add.w	#1,fbtimerl

;* 10 secs-16*level# elapsed? *
;* 5 secs=minimum *
		move.w	#500,d0
		move.w	levelnumber,d1
		asl.w	#4,d1
		sub.w	d1,d0		;d0=500-16*lev#
		cmp.w	#250,d0
		bge.s	gcfbllnlim
		move.w	#250,d0

gcfbllnlim:	cmp.w	fbtimerl,d0
		bne.s	gcfbllex
		move.w	luigiypos,d1
		move.w	luigixpos,d2

;* max. # already launched *
		move.w	maxnooffb,d0
		cmp.w	firelaunched,d0
		beq.s	gcfbllrestime

;* insert it! *
		bsr	gcfblins	;code above!

;* reset timer *
gcfbllrestime:	move.w	#0,fbtimerl
gcfbllex:	move.w	luigiypos,oldluigiy	;save ypos
		rts


;---------------------------------------------------

;* launch fireball
;* in: plr.ypos in d1, xpos in d2
;* both mario and luigi use this

gcfblins:	add.w	#1,firelaunched	;launch 1 more fireball

;find 1.nme not activated
		lea	nmeactive(pc),a1
		move.l	#0,d0
		move.l	#0,d7
gcfblloop:	cmp.w	#0,(a1,d0)	;nme active?
		beq.s	gcfblinsert	;insert fireball
		add.w	#2,d0
		add.w	#4,d7
		cmp.w	#20,d0		;fin?
		bne.s	gcfblloop
		rts


gcfblinsert:	move.w	#4,(a1,d0)	;nme=active(no restore)
		lea	nmetype(pc),a2

;d0=available nmeoffset
;d7 for longword
		move.w	#6,(a2,d0)	;type=fireball
		lea	nmespeed(pc),a1
		move.w	#8,(a1,d0)	;speed=8

;is player left or right relative to center of screen?
		cmp.w	#160,d2
		bgt.s	gcfblinsleft
		move.w	#2,d3		;facing=2
		move.w	#2352,d4	;xpos=294*8
		move.w	#294,d6
		bra	gcfblinsa
gcfblinsleft:	move.w	#1,d3		;facing=1
		move.w	#24,d4		;xpos=3*8
		move.w	#3,d6
gcfblinsa:	lea	nmefacing(pc),a1
		move.w	d3,(a1,d0)	;face in dir. towards plr.
		lea	nmeanimbyte(pc),a1
		move.w	d3,(a1,d0)	;walk in dir. --
;set xpos
		lea	nmexpos(pc),a1
		lea	nmexposition(pc),a2

;launch fireball at opp. side
		move.w	d4,(a1,d0)	;xpos
		move.w	d6,(a2,d0)	;xposition
		lea	nmetabcnt(pc),a1
		move.w	#-2,(a1,d0)	;tabcnt=-2
		lea	nmehitflag(pc),a1
		move.w	#1,(a1,d0)	;hitflag=1
		lea	nmewalkcnt(pc),a1
		move.w	#0,(a1,d0)	;walkcnt=0
		lea	nmeyposition(pc),a1

;calc. ypos for fireball from plr ypos
		cmp.w	#55,d1		;ypos<55?
		bls.s	gcfblinsy	;just insert same ypos(-8)
		cmp.w	#90,d1
		bgt.s	gcfblinsy

;* insert ypos as if player was standing on central platform *
		move.w	#83,d1		;set ypos to 104-21

gcfblinsy:	sub.w	#8,d1		;d1=plrypos-8
		move.w	d1,(a1,d0)	;ypos
		lea	nmetimer1(pc),a1
		move.w	#0,(a1,d0)	;timer1=0
		lea	nmesource(pc),a1
		lea	nmesource2(pc),a2
		move.l	screen,d1
		move.w	d0,d7
		asl.w	#1,d7
		move.l	d1,(a1,d7)	;nmesource=screen
		move.l	d1,(a2,d7)	;nmesource2=screen
		lea	nmebobadr(pc),a1
		lea	nmemaskadr(pc),a2
		move.l	gfx1,d1
		add.l	#6212,d1
		move.l	d1,(a1,d7)	;print start-frame
		move.l	mask1,d1
		add.l	#6212,d1
		move.l	d1,(a2,d7)
		rts


;----------------------------------------------------

;*
;* control max. no of fireballs on-screen:
;*
;* level 1-8:max=1
;* level 9- :max=2
;*

gcnooffire:	cmp.w	#0,levelnumten	;level>=10?
		bne.s	gcnof2fb
		cmp.w	#9,levelnumber	;level=9?
		beq.s	gcnof2fb

;* max 1 fireball *
		move.w	#1,maxnooffb
		rts

;* max 2 fireballs *
gcnof2fb:	move.w	#2,maxnooffb
		rts





;----------------------------------------------------

;*
;* kill all coins+icemen on screen:
;* called by launchcoin when last nme is killed.
;*

gckillcoins:
;kill coins+ice with animation!
		lea	nmeactive(pc),a0
		lea	nmetype(pc),a1
		lea	nmeanimbyte(pc),a2
		lea	nmetabcnt(pc),a3
		move.l	#0,d1
gckillclp:	cmp.w	#1,(a0,d1)	;active?
		bne.s	gckilln		;no, goto next
		cmp.w	#5,(a1,d1)	;coin?
		beq.s	gckill		;no, goto next
		cmp.w	#4,(a1,d1)	;ice?
		bne.s	gckilln		;no, goto next

gckill:		move.w	#4,(a2,d1)	;coin/ice=hit
		move.w	#12,(a3,d1)	;tabcnt=12
gckilln:	add.w	#2,d1
		cmp.w	#20,d1
		bne.s	gckillclp
		rts


;----------------------------------------------------

;*
;* if only one enemy left then make it max aggr.
;* (for turtle/lobster/fly).
;* only if eol.
;*

gclastnme:	move.w	levelpnt,d0
		move.l	levelno,a0	;adr. of current leveldata
		add.w	d0,a0
		move.w	(a0),d1		;current entry
		cmp.w	#$80,d1		;end of level?
		beq.s	gclnmelf
		rts
gclnmelf:	move.l	#0,d0
		move.l	#0,d1
		lea	nmeactive(pc),a0
		lea	nmeaggr(pc),a1
		lea	nmetype(pc),a2
		lea	nmeanimbyte(pc),a3

gclastnmelp:	cmp.w	#0,(a0,d0)	;active?
		beq.s	gclastnmena
		cmp.w	#2,(a0,d0)
		beq.s	gclastnmena
		cmp.w	#3,(a0,d0)
		beq.s	gclastnmena
		cmp.w	#4,(a2,d0)
		bge.s	gclastnmena	;ice/coin/fire
		cmp.w	#5,(a3,d0)	;dead?
		beq.s	gclastnmena
		add.w	#1,d1		;# of active (t/l/f), 0 aggr.
		move.l	d0,d2		;save offset

gclastnmena:	add.w	#2,d0
		cmp.w	#20,d0
		bne.s	gclastnmelp

;only one active?
		cmp.w	#1,d1
		bne.s	gclastnmeex

		cmp.w	#1,(a2,d2)	;turtle?
		beq.s	gclastnmetu
		cmp.w	#2,(a2,d2)	;lobster?
		beq.s	gclastnmelo
		cmp.w	#3,(a2,d2)	;fly?
		bne.s	gclastnmeex
		move.w	#1,(a1,d2)	;aggr:=max
		rts
gclastnmetu:	move.w	#2,(a1,d2)	;aggr:=max
gclastnmeex:	rts
gclastnmelo:	cmp.w	#3,(a1,d2)	;already at max+hit?
		beq.s	gclastnmeex
		move.w	#2,(a1,d2)	;aggr:=max
		rts


;----------------------------------------------------

;*
;* launch ice after 7 secs. when hit:
;*

gcicelaunch:	bsr	gcicel1
		bsr	gcicel2
		rts

;* 1. timer *
gcicel1:	cmp.w	#0,icetimer	;timer running?
		beq.s	gcicelex	;no, exit
;timer running
		sub.w	#1,icetimer
		cmp.w	#0,icetimer	;time to launch?
		bne.s	gcicelex	;no, exit
		bra.s	gcicelaun

;* 2. timer *
gcicel2:	cmp.w	#0,icetimer2	;timer running?
		beq.s	gcicelex	;no, exit
;timer 2 running
		sub.w	#1,icetimer2
		cmp.w	#0,icetimer2	;time to launch?
		bne.s	gcicelex	;no, exit
		bra.s	gcicelaun



;* launch ice *
;* find vacant entry *
gcicelaun:	move.l	#0,d0
		move.l	#0,d7
		lea	nmeactive(pc),a0

gcicellp:	cmp.w	#0,(a0,d0)	;active?
		beq.s	gcicelch	;no, launch!
		add.w	#2,d0
		add.l	#4,d7
		cmp.w	#20,d0
		bne.s	gcicellp
gcicelex:	rts
		
;launching
gcicelch:	move.w	#4,(a0,d0)	;activate!
		lea	nmebobadr(pc),a0
		move.l	gfx1,d1
		add.l	#7932,d1
		move.l	d1,(a0,d7)	;bob=void
		lea	nmemaskadr(pc),a0
		move.l	mask1,d1
		add.l	#7932,d1
		move.l	d1,(a0,d7)	;mask=void
		lea	nmetype(pc),a0
		move.w	#4,(a0,d0)	;type=ice
		lea	nmetabcnt(pc),a0
		move.w	#46,(a0,d0)	;tabcnt=46
		lea	nmehitflag(pc),a0
		move.w	#1,(a0,d0)	;hitflag=1
		lea	nmeyposition(pc),a0
		move.w	#6,(a0,d0)	;ypos=6
		lea	nmetimer1(pc),a0
		move.w	#0,(a0,d0)	;timer1=0
		lea	nmespeed(pc),a0
		move.w	#6,(a0,d0)	;speed=6
		lea	nmexpos(pc),a0
;set xpos
		eor.w	#1,coinflag	;shift to opp. side
		cmp.w	#1,coinflag	;right?
		bne.s	gciceleft
		move.w	#40,(a0,d0)	;xpos=40 (=5)
		lea	nmexposition(pc),a0
		move.w	#5,(a0,d0)
		lea	nmefacing(pc),a0
		move.w	#1,(a0,d0)
		lea	nmeanimbyte(pc),a0
		move.w	#1,(a0,d0)
		rts
gciceleft:	move.w	#2320,(a0,d0)	;xpos=2320 (=290)
		lea	nmexposition(pc),a0
		move.w	#290,(a0,d0)
		lea	nmefacing(pc),a0
		move.w	#2,(a0,d0)
		lea	nmeanimbyte(pc),a0
		move.w	#2,(a0,d0)
		rts




;---------------------------------------------------

;*
;* control mario:
;*

contmario:	cmp.w	#0,pause	;end of level/game?
		beq.s	contmarioc
		rts
contmarioc:	cmp.w	#1,marioactive
		beq.s	contmarioa
		rts
contmarioa:	cmp.w	#1,animbyte	
		beq	jumping
		cmp.w	#2,animbyte
		beq	runright
		cmp.w	#3,animbyte
		beq	runleft
		cmp.w	#4,animbyte
		beq	skidright
		cmp.w	#5,animbyte
		beq	skidleft
		cmp.w	#6,animbyte
		beq	dead1
		cmp.w	#7,animbyte
		beq	pushedright
		cmp.w	#8,animbyte
		beq	pushedleft
		cmp.w	#9,animbyte
		beq	hit
		cmp.w	#10,animbyte
		beq	wait

;* still standing *
;* fire? *
		cmp.b	#1,joy2fire
		bne.s	cmnofire
		move.w	#1,animbyte	;jump!
		rts

;* move right? *
cmnofire:	cmp.b	#7,joy2dir
		bne.s	cmnoright
		move.w	#1,facing	;face right
		move.w	#2,animbyte	;run right
		rts

;* move left? *
cmnoright:	cmp.b	#1,joy2dir
		bne.s	cmnoleft
		move.w	#0,facing	;face left
		move.w	#3,animbyte	;run left
cmnoleft:	rts



;* jumping *

jumping:	add.w	#2,tabcnt
		move.w	tabcnt,d0
		lea	jumptable(pc),a0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.l	jumpfin
		bls.s	jcont		;going down => no block high
		cmp.w	#1,colhflag
		beq.s	jcont2

jcont3:		cmp.w	#1,chflag	;prevent mario going up
		bne.s	jcont4		;twice in same jump
		bra	jcont2
jcont4:		move.w	#0,d1		;convert neg. to pos.
		sub.w	d0,d1
		sub.w	d1,marioypos
		bra	jcont2

jcont:		cmp.w	maxdown,d0
		bls.s	jnod
		move.w	maxdown,d0
		add.w	d0,marioypos
		bra	jumpfin

jnod:		add.w	d0,marioypos
	
jcont2:		cmp.w	#1,jumpbyte
		bne.s	jex
		add.w	#8,marioxposition
		rts
jex:		cmp.w	#2,jumpbyte
		bne.s	jexit
		sub.w	#8,marioxposition
jexit:		rts

jumpfin:	cmp.w	#1,jumpbyte	;check if skid is necessary
		bne.s	nourj
		move.w	#4,animbyte
		bra	jfex
nourj:		cmp.w	#2,jumpbyte
		bne.s	noulj
		move.w	#5,animbyte
		bra	jfex
noulj:		move.w	#0,animbyte
jfex:		move.w	#0,jumpbyte
		move.w	#-2,tabcnt
		move.w	#-2,skidcnt
		move.w	#0,chflag	;mario can jump again
		move.w	#0,powhitmario	;mario can hit pow again

		lea	nmehitflag(pc),a6	;nmes can be hit again
		move.l	#0,d7		;..	
jfinloop:	move.w	#1,(a6,d7)
		add.w	#2,d7
		cmp.w	#20,d7
		bne.s	jfinloop
		rts


;* run right *		

runright:	add.w	#8,marioxposition
		add.w	#1,runcnt
		cmp.b	#7,joy2dir
		beq.s	rrj
		cmp.w	#5,runcnt	;right 5 in a row
		bgt.s	rrskid		;=> skid
		move.w	#0,animbyte
		move.w	#0,runcnt
		rts
rrskid:		move.w	#4,animbyte
		move.w	#0,runcnt
rrj:		cmp.b	#1,joy2fire
		bne.s	rrjex
		move.w	#1,animbyte
		move.w	#1,jumpbyte
rrjex:		rts


;* run left *
runleft:	sub.w	#8,marioxposition
		add.w	#1,runcnt
		cmp.b	#1,joy2dir
		beq.s	rlj
		cmp.w	#5,runcnt
		bgt.s	rlskid
		move.w	#0,animbyte
		move.w	#0,runcnt
		rts
rlskid:		move.w	#5,animbyte
		move.w	#0,runcnt
rlj:		cmp.b	#1,joy2fire
		bne.s	rljex
		move.w	#1,animbyte
		move.w	#2,jumpbyte
rljex:		rts


;* skid right *
skidright:	add.w	#2,skidcnt
		lea	skidtable(pc),a0
		move.w	skidcnt,d0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.s	skidrfin
		add.w	d0,marioxposition
		rts

skidrfin:	move.w	#-2,skidcnt
		move.w	#0,animbyte
		rts


;* skid left *
skidleft:	add.w	#2,skidcnt
		lea	skidtable(pc),a0
		move.w	skidcnt,d0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.s	skidlfin
		sub.w	d0,marioxposition
		rts

skidlfin:	move.w	#-2,skidcnt
		move.w	#0,animbyte
		rts


;* use for all 3 deaths *
dead1:		cmp.w	#50,deadcnt
		bge.s	dead1addy
		move.w	#0,powhitmario	;stop possible jump screen
		rts
dead1addy:	add.w	#2,tabcnt	;luigi=fall/waving
		move.w	tabcnt,d0
		lea	jumptable(pc),a0
		add.w	d0,a0
		move.w	(a0),d0
		add.w	d0,marioypos
		cmp.w	#170,marioypos	;deathfall fin.?
		bhi.s	dead1fin		;yes
		rts
dead1fin:	move.w	#10,animbyte	;should be waiting
		move.w	#0,deadcnt
		move.w	#0,deadcnt2
		move.w	#8,marioypos
		move.w	#1104,marioxposition
		move.w	#46,tabcnt
		move.w	#5,maxdown
		sub.w	#1,lives	;lost a life
		rts


;* waiting after death *

wait:		cmp.b	#1,joy2dir	;joyleft?
		bne.s	waitnl
		add.w	#1,mariowl
		cmp.w	#25,mariowl	;left 25 in a row?
		bge.s	waitfl
		btst	#7,ciaapra
		beq.s	waitfl
		rts
;fall left
waitfl:		move.w	#2,jumpbyte
		move.w	#0,facing
		bra	waitcf
waitnl:		cmp.b	#7,joy2dir	;joyright?
		bne.s	waitnr
		add.w	#1,mariowr
		cmp.w	#25,mariowr
		bge.s	waitfr
		btst	#7,ciaapra
		beq.s	waitfr
		rts
;fall right
waitfr:		move.w	#1,jumpbyte
		move.w	#1,facing
;check fire (jump left/right)
waitcf:		btst	#7,ciaapra	;fire?
		bne.s	waitsf
		move.w	#-2,tabcnt	;jump!
		bra	waitsf
;check fire(jump up)
waitnr:		btst	#7,ciaapra	;fire?
		bne.s	waitnf
		move.w	#-2,tabcnt
		bra	waitsju		;jump up!

;wait 10 secs.
waitnf:		add.w	#1,deadcnt
		cmp.w	#500,deadcnt
		bne.s	waitex
waitsju:	move.w	#0,jumpbyte	;fall straight down
waitsf:		move.w	#0,deadcnt	;start falling/jumping
		move.w	#1,animbyte
		move.w	#0,chflag
		move.w	#0,mariowl
		move.w	#0,mariowr
waitex:		rts

mariowr:	dc.w	0
mariowl:	dc.w	0



;*** mario pushed right by luigi ***

pushedright:	cmp.b	#1,joy2dir	;mario pushing left?
		bne.s	pushrightns
		add.w	#1,pushcnt
		cmp.w	#40,pushcnt	;time for mario to push?
		bls.s	pushrightns
		move.w	#3,animbyte	;mario run left
		move.w	#8,animbyteluigi     ;luigi pushed left
		move.w	#0,pushcnt
		rts
pushrightex:	add.w	#8,marioxposition	;go right
		rts

pushrightns:	cmp.b	#7,joy1dir	;luigi pushing right?
		beq.s	pushrightex
		move.w	#4,animbyte	;mario skid right
		move.w	#0,pushcnt
		rts


;*** mario pushed left by luigi ***

pushedleft:	cmp.b	#7,joy2dir	;mario pushing right?
		bne.s	pushleftns
		add.w	#1,pushcnt
		cmp.w	#40,pushcnt	;time for mario to push?
		bls.s	pushleftns
		move.w	#2,animbyte	;mario run right
		move.w	#7,animbyteluigi     ;luigi pushed right
		move.w	#0,pushcnt
		rts
pushleftex:	sub.w	#8,marioxposition	;go left
		rts

pushleftns:	cmp.b	#1,joy1dir	;luigi pushing left?
		beq.s	pushleftex
		move.w	#5,animbyte	;mario skid left
		move.w	#0,pushcnt
		rts



;*** mario hit from beneath ***

hit:		add.w	#2,tabcnt
		move.w	tabcnt,d0
		lea	jumptable(pc),a0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.l	hitfin
		bls.s	hitcont		;going down => no block high
		cmp.w	#1,colhflag
		beq.s	hitcont2

hitcont3:	cmp.w	#1,chflag	;prevent mario going up
		bne.s	hitcont4	;twice in same jump
		bra	hitcont2
hitcont4:	move.w	#0,d1		;convert neg. to pos.
		sub.w	d0,d1
		sub.w	d1,marioypos
		bra	hitcont2

hitcont:	cmp.w	maxdown,d0
		bls.s	hitnod
		move.w	maxdown,d0
		add.w	d0,marioypos
		bra	hitfin

hitnod:		add.w	d0,marioypos
	
hitcont2:	move.w	xoffset,d7
		add.w	d7,marioxposition
		rts

hitfin:		cmp.w	#1,jumpbyte	;check if skid is necessary
		bne.s	hitnourj
		move.w	#4,animbyte
		bra	hitfex
hitnourj:	cmp.w	#2,jumpbyte
		bne.s	hitnoulj
		move.w	#5,animbyte
		bra	hitfex
hitnoulj:	move.w	#0,animbyte
hitfex:		move.w	#0,jumpbyte
		move.w	#-2,tabcnt
		move.w	#0,chflag	;mario can jump again
		rts



;----------------------------------------------------

;*
;* control luigi:
;*

contluigi:	cmp.w	#1,luigiactive
		beq.s	contlact
		rts

contlact:	cmp.w	#0,pause
		beq.s	contlnp
		rts

contlnp:	cmp.w	#1,animbyteluigi
		beq	jumpinglu
		cmp.w	#2,animbyteluigi
		beq	runrightlu
		cmp.w	#3,animbyteluigi
		beq	runleftlu
		cmp.w	#4,animbyteluigi
		beq	skidrightlu
		cmp.w	#5,animbyteluigi
		beq	skidleftlu
		cmp.w	#6,animbyteluigi
		beq	dead1lu
		cmp.w	#7,animbyteluigi
		beq	pushedrightlu
		cmp.w	#8,animbyteluigi
		beq	pushedleftlu
		cmp.w	#9,animbyteluigi
		beq	hitlu
		cmp.w	#10,animbyteluigi
		beq	waitlu

;* still standing *
;* fire? *
		cmp.b	#1,joy1fire
		bne.s	clnofire
		move.w	#1,animbyteluigi	;jump!
		rts

;* move right? *
clnofire:	cmp.b	#7,joy1dir
		bne.s	clnoright
		move.w	#1,facingluigi	;face right
		move.w	#2,animbyteluigi;run right
		rts

;* move left? *
clnoright:	cmp.b	#1,joy1dir
		bne.s	clnoleft
		move.w	#0,facingluigi	;face left
		move.w	#3,animbyteluigi;run left
clnoleft:	rts



;*** jumping ***

jumpinglu:	add.w	#2,tabcntluigi
		move.w	tabcntluigi,d0
		lea	jumptable(pc),a0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.l	jumpfinlu
		bls.s	jcontlu		;going down => no block high
		cmp.w	#1,colhflagluigi
		beq.s	jcont2lu

jcont3lu:	cmp.w	#1,chflagluigi	;prevent luigi  going up
		bne.s	jcont4lu	;twice in same jump
		bra	jcont2lu
jcont4lu:	move.w	#0,d1		;convert neg. to pos.
		sub.w	d0,d1
		sub.w	d1,luigiypos
		bra	jcont2lu

jcontlu:	cmp.w	maxdownluigi,d0
		bls.s	jnodlu
		move.w	maxdownluigi,d0
		add.w	d0,luigiypos
		bra	jumpfinlu

jnodlu:		add.w	d0,luigiypos

jcont2lu:	cmp.w	#1,jumpbyteluigi
		bne.s	jexlu
		add.w	#8,luigixposition
		rts
jexlu:		cmp.w	#2,jumpbyteluigi
		bne.s	jexitlu
		sub.w	#8,luigixposition
jexitlu:	rts

jumpfinlu:	cmp.w	#1,jumpbyteluigi  ;check if skid is necessary
		bne.s	nourjlu
		move.w	#4,animbyteluigi
		bra	jfexlu
nourjlu:	cmp.w	#2,jumpbyteluigi
		bne.s	nouljlu
		move.w	#5,animbyteluigi
		bra	jfexlu
nouljlu:	move.w	#0,animbyteluigi
jfexlu:		move.w	#0,jumpbyteluigi
		move.w	#-2,tabcntluigi
		move.w	#-2,skidcntluigi
		move.w	#0,chflagluigi	;luigi can jump again
		move.w	#0,powhitluigi	;luigi can hit pow again

		lea	nmehitflag(pc),a6	;nmes can be hit again
		move.w	#0,d7		;..	
jfinlooplu:	move.w	#1,(a6,d7)
		add.w	#2,d7
		cmp.w	#20,d7
		bne.s	jfinlooplu
		rts


;*** run right ***

runrightlu:	add.w	#8,luigixposition
		add.w	#1,runcntluigi
		cmp.b	#7,joy1dir
		beq.s	rrjlu
		cmp.w	#5,runcntluigi	;right 5 in a row
		bgt.s	rrskidlu	;=> skid
		move.w	#0,animbyteluigi
		move.w	#0,runcntluigi
		rts

rrskidlu:	move.w	#4,animbyteluigi
		move.w	#0,runcntluigi
rrjlu:		cmp.b	#1,joy1fire
		bne.s	rrjexlu
		move.w	#1,animbyteluigi
		move.w	#1,jumpbyteluigi
rrjexlu:	rts


;*** run left ***

runleftlu:	sub.w	#8,luigixposition
		add.w	#1,runcntluigi
		cmp.b	#1,joy1dir
		beq.s	rljlu
		cmp.w	#5,runcntluigi
		bgt.s	rlskidlu
		move.w	#0,animbyteluigi
		move.w	#0,runcntluigi
		rts
rlskidlu:	move.w	#5,animbyteluigi
		move.w	#0,runcntluigi
rljlu:		cmp.b	#1,joy1fire
		bne.s	rljexlu
		move.w	#1,animbyteluigi
		move.w	#2,jumpbyteluigi
rljexlu:	rts


;*** skid right ***

skidrightlu:	add.w	#2,skidcntluigi
		lea	skidtable(pc),a0
		move.w	skidcntluigi,d0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.s	skidrfinlu
		add.w	d0,luigixposition
		rts

skidrfinlu:	move.w	#-2,skidcntluigi
		move.w	#0,animbyteluigi
		rts


;*** skid left ***

skidleftlu:	add.w	#2,skidcntluigi
		lea	skidtable(pc),a0
		move.w	skidcntluigi,d0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.s	skidlfinlu
		sub.w	d0,luigixposition
		rts

skidlfinlu:	move.w	#-2,skidcntluigi
		move.w	#0,animbyteluigi
		rts


;*** dead ***

dead1lu:	cmp.w	#50,deadcntluigi
		bge.s	dead1laddy
		move.w	#0,powhitluigi
		rts
dead1laddy:	add.w	#2,tabcntluigi	;luigi=fall/waving
		move.w	tabcntluigi,d0
		lea	jumptable(pc),a0
		add.w	d0,a0
		move.w	(a0),d0
		add.w	d0,luigiypos
		cmp.w	#170,luigiypos	;deathfall fin.?
		bhi.s	dead1lfin		;yes
		rts
dead1lfin:	move.w	#10,animbyteluigi;should be waiting
		move.w	#0,deadcntluigi
		move.w	#0,deadcnt2luigi
		move.w	#8,luigiypos
		move.w	#1280,luigixposition
		move.w	#46,tabcntluigi	;start falling
		move.w	#5,maxdownluigi
		sub.w	#1,livesluigi	;lost a life
		rts


;*** waiting after death ***

waitlu:		cmp.b	#1,joy1dir	;joyleft?
		bne.s	waitlunl
		add.w	#1,luigiwl
		cmp.w	#25,luigiwl	;left 25 in a row?
		bge.s	waitlufl
		btst	#6,ciaapra
		beq.s	waitlufl
		rts
;fall left
waitlufl:	move.w	#2,jumpbyteluigi
		move.w	#0,facingluigi
		bra	waitlucf
waitlunl:	cmp.b	#7,joy1dir	;joyright?
		bne.s	waitlunr
		add.w	#1,luigiwr
		cmp.w	#25,luigiwr
		bge.s	waitlufr
		btst	#6,ciaapra
		beq.s	waitlufr
		rts
;fall right
waitlufr:		move.w	#1,jumpbyteluigi
		move.w	#1,facingluigi
;check fire (jump left/right)
waitlucf:	btst	#6,ciaapra	;fire?
		bne.s	waitlusf
		move.w	#-2,tabcntluigi	;jump!
		bra	waitlusf
;check fire(jump up)
waitlunr:		btst	#6,ciaapra	;fire?
		bne.s	waitlunf
		move.w	#-2,tabcntluigi
		bra	waitlusju		;jump up!

;wait 10 secs.
waitlunf:	add.w	#1,deadcntluigi
		cmp.w	#500,deadcntluigi
		bne.s	waitluex
waitlusju:	move.w	#0,jumpbyteluigi	;fall straight down
waitlusf:	move.w	#0,deadcntluigi	;start falling/jumping
		move.w	#1,animbyteluigi
		move.w	#0,chflagluigi
		move.w	#0,luigiwl
		move.w	#0,luigiwr
waitluex:	rts


luigiwr:	dc.w	0
luigiwl:	dc.w	0



;*** luigi pushed right by mario ***

pushedrightlu:	cmp.b	#1,joy1dir	;luigi pushing left?
		bne.s	pushrightluns
		add.w	#1,pushcnt
		cmp.w	#40,pushcnt	;time for luigi to push?
		bls.s	pushrightluns
		move.w	#3,animbyteluigi	;luigi run left
		move.w	#8,animbyte     ;mario pushed left
		move.w	#0,pushcnt
		rts
pushrightluex:	add.w	#8,luigixposition	;go right
		rts

pushrightluns:	cmp.b	#7,joy2dir	;mario pushing right?
		beq.s	pushrightluex
		move.w	#4,animbyteluigi	;luigi skid right
		move.w	#0,pushcnt
		rts


;*** luigi pushed left by mario ***

pushedleftlu:	cmp.b	#7,joy1dir	;luigi pushing right?
		bne.s	pushleftluns
		add.w	#1,pushcnt
		cmp.w	#40,pushcnt	;time for luigi to push?
		bls.s	pushleftluns
		move.w	#2,animbyteluigi	;luigi run right
		move.w	#7,animbyte     ;mario pushed right
		move.w	#0,pushcnt
		rts
pushleftluex:	sub.w	#8,luigixposition	;go left
		rts

pushleftluns:	cmp.b	#1,joy2dir	;mario pushing left?
		beq.s	pushleftluex
		move.w	#5,animbyteluigi	;luigi skid left
		move.w	#0,pushcnt
		rts


;*** luigi hit from beneath ***

hitlu:		add.w	#2,tabcntluigi
		move.w	tabcntluigi,d0
		lea	jumptable(pc),a0
		adda.w	d0,a0
		move.w	(a0),d0
		cmp.w	#$80,d0
		beq.l	hitfinl
		bls.s	hitcontl	;going down => no block high
		cmp.w	#1,colhflagluigi
		beq.s	hitcont2l

hitcont3l:	cmp.w	#1,chflagluigi	;prevent luigi going up
		bne.s	hitcont4l	;twice in same jump
		bra	hitcont2l
hitcont4l:	move.w	#0,d1		;convert neg. to pos.
		sub.w	d0,d1
		sub.w	d1,luigiypos
		bra	hitcont2l

hitcontl:	cmp.w	maxdownluigi,d0
		bls.s	hitnodl
		move.w	maxdownluigi,d0
		add.w	d0,luigiypos
		bra	hitfinl

hitnodl:	add.w	d0,luigiypos
	
hitcont2l:	move.w	xoffsetluigi,d7
		add.w	d7,luigixposition
		rts

hitfinl:	cmp.w	#1,jumpbyteluigi;check if skid is necessary
		bne.s	hitnourjl
		move.w	#4,animbyteluigi
		bra	hitfexl
hitnourjl:	cmp.w	#2,jumpbyteluigi
		bne.s	hitnouljl
		move.w	#5,animbyteluigi
		bra	hitfexl
hitnouljl:	move.w	#0,animbyteluigi
hitfexl:	move.w	#0,jumpbyteluigi
		move.w	#-2,tabcntluigi
		move.w	#0,chflagluigi	;luigi can jump again
		rts



;----------------------------------------------------

;*
;* correct xpos (ypos when jumping):
;*
;* xpos = xposition/8
;* virtual ypos is calc. from real ypos.
;*

correct:	bsr	correctx
		bsr	correcty
		bsr	correctxl
		bsr	correctyl
		rts

correctx:	move.w	marioxposition,d0
		asr.w	#3,d0
		move.w	d0,marioxpos
		cmp.w	#299,marioxpos
		bne.s	cornr
		move.w	#8,marioxposition
		rts
cornr:		cmp.w	#0,marioxpos
		bne.s	cornl
		move.w	#2384,marioxposition
cornl:		rts

;set to 0 if <0
correcty:	cmp.w	#0,marioypos
		blt.s	cycor
		move.w	marioypos,mariovypos	;copy to virtual
		rts
cycor:		move.w	#0,mariovypos
		rts

correctxl:	move.w	luigixposition,d0
		asr.w	#3,d0
		move.w	d0,luigixpos
		cmp.w	#299,luigixpos
		bne.s	cornrl
		move.w	#8,luigixposition
		rts
cornrl:		cmp.w	#0,luigixpos
		bne.s	cornll
		move.w	#2384,luigixposition
cornll:		rts

correctyl:	cmp.w	#0,luigiypos
		blt.s	cycorl
		move.w	luigiypos,luigivypos
		rts
cycorl:		move.w	#0,luigivypos
		rts




;----------------------------------------------------

;*
;* control screen (bobs):
;*
;* restore background
;* move bob into 4 bitplanes
;*


restore:	cmp.w	#0,frame
		beq.s	rest1
		sub.w	#1,frame
		rts

;* mario, luigi, nmes. *

rest1:		bsr	restmario
		bsr	restluigi
		bsr	restnmes
		rts

;*** mario ***
restmario:	cmp.w	#1,marioactive
		beq.s	restactm
		cmp.w	#2,marioactive
		beq.s	restactmf
		cmp.w	#3,marioactive
		beq.s	restactmf
		cmp.w	#4,marioactive
		beq.s	restactms
		cmp.w	#5,marioactive
		beq.s	restactms
		rts		;not active

;* game start *
restactms:	add.w	#1,marioactive
		cmp.w	#5,marioactive
		beq.s	restactmss
		rts			;don't restore
restactmss:	move.w	#1,marioactive	;save+rest
		rts
;* game over *
restactmf:	cmp.w	#3,marioactive
		beq.s	restactmd
		add.w	#1,marioactive
		bra	restactm

restactmd:	move.w	#0,marioactive	;disactivate
restactm:	cmp.w	#0,screenf
		beq.s	rmeven
		move.l	mariosource2,a3
		move.l	mariosource2,a0
		bra	rmc
rmeven:		move.l	mariosource,a3
		move.l	mariosource,a0
rmc:		add.l	screen,a3
		add.l	backgr,a0	;restore backgr. gfx
		move.w	#34,d4		;dmod
		move.w	#34,d2
		move.w	#$543,d5	;21*3
		bsr	rest
		rts


;*** luigi ***
restluigi:	cmp.w	#1,luigiactive
		beq.s	restactl
		cmp.w	#2,luigiactive
		beq.s	restactlf
		cmp.w	#3,luigiactive
		beq.s	restactlf
		cmp.w	#4,luigiactive
		beq.s	restactls
		cmp.w	#5,luigiactive
		beq.s	restactls
		rts			;not active

;* game start *
restactls:	add.w	#1,luigiactive
		cmp.w	#5,luigiactive
		beq.s	restactlss
		rts			;don't restore
restactlss:	move.w	#1,luigiactive	;save+rest
		rts
;* game over *
restactlf:	cmp.w	#3,luigiactive
		beq.s	restactld
		add.w	#1,luigiactive
		bra	restactl

restactld:	move.w	#0,luigiactive	;disactivate

restactl:	cmp.w	#0,screenf
		beq.s	rmevenl
		move.l	luigisource2,a3
		move.l	luigisource2,a0
		bra	rmcl
rmevenl:	move.l	luigisource,a3
		move.l	luigisource,a0
rmcl:		add.l	screen,a3
		add.l	backgr,a0
		move.w	#34,d4		;dmod
		move.w	#34,d2
		move.w	#$543,d5	;21*3
		bsr	rest
		rts

;*** nmes ***
restnmes:	move.l	#0,d7		;offset for longword
		move.l	#0,d0		;offset for word
rnloop:		bsr	nmerest
		add.l	#4,d7
		add.w	#2,d0
		cmp.w	#20,d0		;more enemies?
		bne.s	rnloop
		rts
nmerest:	lea	nmeactive(pc),a0
		cmp.w	#1,(a0,d0)	;active?
		beq.s	cnract
		cmp.w	#2,(a0,d0)
		beq.s	nmerest2
		cmp.w	#3,(a0,d0)
		beq.s	nmerest2
		cmp.w	#4,(a0,d0)	;just activated?
		beq.s	nmerestja
		cmp.w	#5,(a0,d0)
		beq.s	nmerestra
		rts

;don't restore if active is 4/5
nmerestja:	add.w	#1,(a0,d0)	;set to #5
		rts
;activate restore in next frame
nmerestra:	move.w	#1,(a0,d0)	;really active
		rts

;restore if nmeactive is 1-3

nmerest2:	cmp.w	#3,(a0,d0)
		beq.s	nmerest3
		add.w	#1,(a0,d0)
		bra	cnract

nmerest3:	move.w	#0,(a0,d0)	;disactivate

cnract:		move.w	#$543,d5	;21*3
		move.w	#34,d4
		move.w	#34,d2
		cmp.w	#0,screenf
		beq	rneven
		lea	nmesource2(pc),a3
		lea	nmesource2(pc),a0
		bra	rngoon
rneven:		lea	nmesource(pc),a3
		lea	nmesource(pc),a0
rngoon:		move.l	(a3,d7),a3
		move.l	(a0,d7),a0
		add.l	screen,a3
		add.l	backgr,a0
		bsr	rest
		rts


;----------------------------------------------------

;*
;* restore routine:
;*
;* in: a3=dest.addr. a0=src.addr.
;* in: d5=blitsize, d4=dmod, d2=amod.
;*
;* call restcut if masks other than $ffff are needed!
;*

rest:		move.w	#$ffff,bltafwm
		move.w	#$ffff,bltalwm
restcut:	move.w	#%0000100111110000,bltcon0
		move.l	a3,bltdpth		;dest.adr.	
		move.l	a0,bltapth		;a0=bufferadr
		move.w	#0,bltcon1
		move.w	d4,bltdmod
		move.w	d2,bltamod
		move.w	#4,d3		;4 bitplanes
restloop:	move.w	d5,bltsize
		bsr	waitblit		
		add.l	#$2800,a3	;next plane (PAL)
		add.l	#$2800,a0
		move.l	a3,bltdpth	;dest.addr.
		move.l	a0,bltapth	;src.addr.
		subq	#1,d3
		bne.s	restloop
		rts



;----------------------------------------------------

;*
;* print bob to screen in 4 bitplanes:
;*
;* pow, nmes, pipes, fireballs, mario, luigi *
;*

printbob:	bsr	printpow
		bsr	printnmes	;all nmes except fireballs
		bsr	printpipes
		bsr	printfire	;fireballs
		bsr	printmario
		bsr	printluigi
		rts

;* nmes, except fireballs *

printnmes:	move.l	#0,d7
		move.l	#0,d0
cnprint:	bsr	nmeprint
		add.l	#4,d7
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	cnprint
		rts

;print if active = 1/4/5
nmeprint:	lea	nmeactive(pc),a0
		cmp.w	#1,(a0,d0)
		beq.s	cnpact
		cmp.w	#4,(a0,d0)	;just activated?
		beq.s	cnpact
		cmp.w	#5,(a0,d0)
		beq.s	cnpact
		rts
cnpact:		lea	nmetype(pc),a0
		cmp.w	#6,(a0,d0)	;fireball?
		bne.s	cnpnfire
		rts
cnpnfire:	lea	nmexposition(pc),a4
		move.w	(a4,d0),d3	;xposition
		move.l	#0,d1
		lea	nmeyposition(pc),a4
		move.w	(a4,d0),d1	;yposition
		asr.w	#3,d3
		lsl.w	#3,d1
		move.w	d1,d6
		lsl.w	#2,d1
		add.w	d6,d1
		add.w	d3,d1


		cmp.w	#0,screenf
		beq.s	pneven
		lea	nmesource2(pc),a3
		bra	pngoon
pneven:		lea	nmesource(pc),a3
pngoon:		move.l	d1,(a3,d7)
		add.l	screen,d1
		move.l	d1,a3
		lea	nmemaskadr(pc),a0
		lea	nmebobadr(pc),a1
		move.l	(a0,d7),a0
		move.l	(a1,d7),a1
		move.w	#0,bltalwm
		move.w	#$543,d5
		move.w	#34,d6
		move.w	#34,d4
		move.w	#34,d2
		lea	nmexposition(pc),a4
		move.w	(a4,d0),d3
		bsr	print
		rts

;* fireballs *

printfire:	move.l	#0,d7
		move.l	#0,d0
cnprintf:	bsr	nmeprintf
		add.l	#4,d7
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	cnprintf
		rts

nmeprintf:	lea	nmeactive(pc),a0
		cmp.w	#1,(a0,d0)
		beq.s	cnpactf
		cmp.w	#4,(a0,d0)	;just activated?
		beq.s	cnpactf
		cmp.w	#5,(a0,d0)
		beq.s	cnpactf
		rts
cnpactf:	lea	nmetype(pc),a0
		cmp.w	#6,(a0,d0)	;fireball?
		beq.s	cnpnfiref
		rts
cnpnfiref:	bsr	cnpnfire	;print: use code above
		rts



;* pipes *

;middle pipes part of background

printpipes:	cmp.w	#0,screenf
		beq.s	ppeven
		move.l	screen2,a3
		bra	ppeven2
ppeven:		move.l	screen1,a3
ppeven2:	add.l	#$1a14+80,a3	;lower pipes
		move.l	gfx1,a1
		add.l	#$1bd0,a1
		move.l	mask1,a0
		add.l	#$1bd0,a0
		move.l	#288,d3
		move.w	#32,d6
		move.w	#32,d4
		move.w	#32,d2
		move.w	#$544,d5
		move.w	#$ffff,bltalwm
		bsr	print

		cmp.w	#0,screenf
		beq.s	ppeven3
		move.l	screen2,a3
		bra	ppeven4
ppeven3:	move.l	screen1,a3
ppeven4:	add.l	#$160+80,a3	;upper pipes
		move.l	gfx1,a1
		add.l	#$1bb0,a1
		move.l	mask1,a0
		add.l	#$1bb0,a0
		move.l	#256,d3
		move.w	#24,d2
		move.w	#24,d4
		move.w	#24,d6
		move.w	#$548,d5
		bsr	print
		rts

;* pow *

;use same col.mask for all sizes inc. void
;print void in bonuslevels
printpow:	cmp.w	#0,screenf
		beq.s	ppoweven
		move.l	screen2,a3
		bra	ppoweven2
ppoweven:	move.l	screen1,a3
ppoweven2:	add.l	#5658,a3
		move.l	gfx1,a1
		add.l	#7920,a1	;largest pow
		move.l	mask1,a0
		add.l	#7920,a0
;decide which pow to use
		cmp.w	#1,bonusflag	;in bonuslevel?
		beq.s	ppowvoid
		cmp.w	#3,powlevel
		beq.s	ppowc3		;use large
		cmp.w	#2,powlevel
		beq.s	ppowc2
		cmp.w	#1,powlevel
		beq.s	ppowc1

ppowvoid:	add.l	#12,a1		;print void
		bra	ppowc3
ppowc1:		add.l	#8,a1		;use small
		bra	ppowc3
ppowc2:		add.l	#4,a1		;use medium

ppowc3:		move.l	#145,d3
		move.w	#34,d6
		move.w	#34,d4
		move.w	#34,d2
		move.w	#$543,d5
		move.w	#$0,bltalwm
		bsr	print
		rts

;* mario *

printmario:	cmp.w	#1,marioactive
		beq.s	printmact
		rts

printmact:	move.w	marioxpos,d3
		move.l	#0,d1
		move.w	mariovypos,d1
		asr.w	#3,d3
		lsl.w	#3,d1
		move.w	d1,d6
		lsl.w	#2,d1
		add.w	d6,d1
		add.w	d3,d1

		cmp.w	#0,screenf
		beq.s	pmeven
		move.l	d1,mariosource2
		bra	pmc
pmeven:		move.l	d1,mariosource

pmc:		add.l	screen,d1
		move.l	d1,a3
		move.l	mariomaskadr,a0	;mariomask
		move.w	#34,d6		;c/d mod
		move.w	#34,d4		;bmod
		move.w	#34,d2		;amod
		move.l	mariobobadr,a1	;mariobob

;calc. boboffset
		cmp.w	#0,marioypos
		bge.s	pmcsiz		;>=0?		
		cmp.w	#-20,marioypos
		bge.s	pmpc		;ypos<-20
;do not print, mario off screen
		rts

pmpc:		move.l	#0,d5
		move.l	#0,d3
		sub.w	marioypos,d5	;negate
		asl.w	#3,d5
		move.w	d5,d3
		asl.w	#2,d5
		add.w	d5,d3		;d3=40*d5
		add.l	d3,a1		;a1=bob+offset
		add.l	d3,a0		;a0=mask+offset
;calc. blitsize
		move.w	#0,d3
		sub.w	marioypos,d3
		move.w	#21,d5
		sub.w	d3,d5		;21-(-ypos)
		asl.w	#6,d5		;height
		add.w	#3,d5		;width
		bra	pmcsize

;calc. blitsize
pmcsiz:		move.w	#$543,d5
pmcsize:	move.w	#0,bltalwm	;mask out last word
		move.w	marioxpos,d3
		bsr	print
		rts

;* luigi *

printluigi:	cmp.w	#1,luigiactive
		beq.s	printlact
		rts

printlact:	move.w	luigixpos,d3
		move.l	#0,d1
		move.w	luigivypos,d1
		asr.w	#3,d3
		lsl.w	#3,d1
		move.w	d1,d6
		lsl.w	#2,d1
		add.w	d6,d1
		add.w	d3,d1

		cmp.w	#0,screenf
		beq.s	pmevenl
		move.l	d1,luigisource2
		bra	pmcl
pmevenl:	move.l	d1,luigisource

pmcl:		add.l	screen,d1
		move.l	d1,a3
		move.l	luigimaskadr,a0	;luigimask
		move.w	#34,d6		;c/d mod
		move.w	#34,d4		;bmod
		move.w	#34,d2		;amod
		move.l	luigibobadr,a1	;luigibob

;calc. boboffset
		cmp.w	#0,luigiypos
		bge.s	plcsiz		;>=0?		
		cmp.w	#-20,luigiypos
		bge.s	plpc		;ypos<-20
;do not print, luigi off screen
		rts

plpc:		move.l	#0,d5
		move.l	#0,d3
		sub.w	luigiypos,d5	;negate
		asl.w	#3,d5
		move.w	d5,d3
		asl.w	#2,d5
		add.w	d5,d3		;d3=40*d5
		add.l	d3,a1		;a1=bob+offset
		add.l	d3,a0		;a0=mask+offset
;calc. blitsize
		move.w	#0,d3
		sub.w	luigiypos,d3
		move.w	#21,d5
		sub.w	d3,d5		;21-(-ypos)
		asl.w	#6,d5		;height
		add.w	#3,d5		;width
		bra	plcsize

;calc. blitsize
plcsiz:		move.w	#$543,d5
plcsize:	move.w	#0,bltalwm	;mask out last word
		move.w	luigixpos,d3
		bsr	print
		rts



;----------------------------------------------------

;*
;* print routine:
;*
;* in: a3=dest.ptr. a0=mask.ptr a1=bob.ptr.
;* in: d6=dmod, d4=bmod, d2=amod, d3=xpos, d5=blitsize.
;*

print:		move.l	a3,bltdpth	;d=screen
		move.l	a3,bltcpth	;c=backgr.

		move.l	a0,bltapth	;a=mask
		move.w	d6,bltdmod
		move.w	d6,bltcmod
		move.w	d4,bltbmod
		move.w	d2,bltamod
		move.l	a1,bltbpth		;b=bob
		move.l	#%0000111111001010,d1

		ror.w	#4,d3	;d3=xpos/16
		move.b	#0,d3	;shiftvalue in d3

		or.w	d3,d1
		move.w	d1,bltcon0

		move.l	#0,d1
		or.w	d3,d1
		move.w	d1,bltcon1
		move.l	#4,d3		;d3=# of bitplanes
		move.l	a3,d2		;a3=sourceadr

bobloop:	move.w	d5,bltsize
		bsr	waitblit
		add.l	#$2800,d2
		move.l	a0,bltapth
		move.l	d2,bltdpth
		move.l	d2,bltcpth
		add.l	#$2800,a1	;PAL
		move.l	a1,bltbpth
		subq	#1,d3
		bne.s	bobloop
		rts


;----------------------------------------------------

;*
;* waiting for blitter to finish:
;*

waitblit:	btst	#14,$dff002
		btst	#14,$dff002
		bne.s	waitblit
		rts


;---------------------------------------------------

;*
;* print platforms when needed:
;*
;* print at start of levels.
;* level1:bricks
;* level4:yellow/red
;* level9:green/yellow
;* 2. bonuslevel:disappearing
;* more than one print.
;*
;* one should also print to background,
;* because that is used for restoring.
;*
;* platformlocations:
;* y,x		,length,blitstartbyte,blit#words
;* 1. 60,0	,137	,0	,9
;* 2. 60,182	,138	,22	,9
;* 3. 111,0	,39	,0	,3
;* 4. 104,69	,182	,8	,12
;* 5. 111,281	,39	,34	,3
;* 6. 146,0	,117	,0	,8
;* 7. 146,203	,117	,24	,8
;*
;* height=8
;*

printplatform:

;* which level? *
		cmp.w	#0,levelnumten	;level<10?
		beq.s	pplatlevl9
		cmp.w	#1,levelnumten
		beq.s	pplatlevm9
		rts

;* level 10-19 *
pplatlevm9:	cmp.w	#5,levelnumber	;level 15(b.l.2)?
		beq	pplatlev15
		cmp.w	#6,levelnumber	;level 16?
		beq	pplatlev16
		rts

;* level 1-9 *
pplatlevl9:
;* time=start of level? *
		cmp.w	#1,levelcnt
		beq.s	pplattimeok9
		cmp.w	#2,levelcnt	;print to 2. screen
		beq.s	pplattimeok9
		rts

pplattimeok9:	cmp.w	#1,levelnumber	;level 1?
		beq.s	pplatlev1
		cmp.w	#4,levelnumber
		beq.s	pplatlev4
		cmp.w	#6,levelnumber
		beq.s	pplatlev4	;same gfx as lev.4
		cmp.w	#9,levelnumber

		beq.s	pplatlev9
		rts


;** level 1 **
pplatlev1:	move.l	platform,a0
		bra.s	pplatprint
;** level 4 **
pplatlev4:	move.l	platform,a0
		add.l	#960,a0
		bra.s	pplatprint
;** level 9 **
pplatlev9:	move.l	platform,a0
		add.l	#1920,a0
		bra.s	pplatprint

;** level 16 **
;* time=start of level? *
pplatlev16:	cmp.w	#1,levelcnt
		beq.s	pplattimeok16
		cmp.w	#2,levelcnt	;print to 2. screen
		beq.s	pplattimeok16
		rts
pplattimeok16:	bra.s	pplatlev9	;same gfx as lev.9


;** level 15 **
;* time for disappearence? *
pplatlev15:	cmp.w	#100,levelcnt
		beq.s	pplattimeok151	;blue platform
		cmp.w	#101,levelcnt	;print to 2. screen
		beq.s	pplattimeok151
		cmp.w	#200,levelcnt
		beq.s	pplattimeok152	;no platform
		cmp.w	#201,levelcnt	;print to 2. screen
		beq.s	pplattimeok152
		rts
pplattimeok151:	move.l	platform,a0
		add.l	#2880,a0
		bra.s	pplatprint
pplattimeok152:	move.l	#0,a0
		bra.s	pplatprint



;*** print platforms ***
;*
;* in: a0=platform base addr. *
;* note: use a NULL addr. in a0
;* if platforms should be cleared,
;* (used in b.l.II )
;*

pplatprint:
;* 1. platform *
		move.l	screen,a3
		add.l	#2400,a3	;ypos=60
;* save addrs. *
		move.l	a0,plattemp1
;* do print *
		move.l	#$209,d5	;8*9
		move.l	#22,d4		;dmod
		move.l	#22,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#2400,a3
		move.l	plattemp1,a0
		jsr	printplat	;print to background

;* 2. platform *
		move.l	screen,a3
		add.l	#2400,a3	;ypos=60
		add.l	#22,a3		;xpos=182
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp2c1	;clear!
		add.l	#22,a0
pplatp2c1:	move.l	#$209,d5	;8*9
		move.l	#22,d4		;dmod
		move.l	#22,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#2400,a3
		add.l	#22,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp2c2	;clear!
		add.l	#22,a0
pplatp2c2:	jsr	printplat	;print to background

;* 3. platform *
		move.l	screen,a3
		add.l	#4440,a3	;ypos=111
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp3c1	;clear!
		add.l	#320,a0
pplatp3c1:	move.l	#$203,d5	;8*3
		move.l	#34,d4		;dmod
		move.l	#34,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#4440,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp3c2	;clear!
		add.l	#320,a0
pplatp3c2:	jsr	printplat	;print to background

;* 4. platform *
		move.l	screen,a3
		add.l	#4160,a3	;ypos=104
		add.l	#8,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp4c1	;clear!
		add.l	#320,a0
		add.l	#8,a0
pplatp4c1:	move.l	#$20c,d5	;8*12
		move.l	#16,d4		;dmod
		move.l	#16,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#4160,a3
		add.l	#8,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp4c2	;clear!
		add.l	#320,a0
		add.l	#8,a0
pplatp4c2:	jsr	printplat	;print to background

;* 5. platform *
		move.l	screen,a3
		add.l	#4440,a3	;ypos=111
		add.l	#34,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp5c1	;clear!
		add.l	#320,a0
		add.l	#34,a0
pplatp5c1:	move.l	#$203,d5	;8*3
		move.l	#34,d4		;dmod
		move.l	#34,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#4440,a3
		add.l	#34,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp5c2	;clear!
		add.l	#320,a0
		add.l	#34,a0
pplatp5c2:	jsr	printplat	;print to background

;* 6. platform *
		move.l	screen,a3
		add.l	#5840,a3	;ypos=146
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp6c1	;clear!
		add.l	#640,a0
pplatp6c1:	move.l	#$208,d5	;8*8
		move.l	#24,d4		;dmod
		move.l	#24,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#5840,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp6c2	;clear!
		add.l	#640,a0
pplatp6c2:	jsr	printplat	;print to background

;* 7. platform *
		move.l	screen,a3
		add.l	#5840,a3	;ypos=146
		add.l	#24,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp7c1	;clear!
		add.l	#640,a0
		add.l	#24,a0
pplatp7c1:	move.l	#$208,d5	;8*8
		move.l	#24,d4		;dmod
		move.l	#24,d2		;amod
		jsr	printplat
		move.l	backgr,a3
		add.l	#5840,a3
		add.l	#24,a3
		move.l	plattemp1,a0
		cmp.l	#0,a0
		beq.s	pplatp7c2	;clear!
		add.l	#640,a0
		add.l	#24,a0
pplatp7c2:	jsr	printplat	;print to background

		rts


;*** temp. vars. ***
plattemp1:	dc.l	0


;---------------------------------------------------

;*
;* print platform:
;*

printplat:	move.w	#$ffff,bltafwm
		move.w	#$ffff,bltalwm

;* clear or print? *
		cmp.l	#0,a0
		bne.s	printplatpr

;* clear it! use d-channel only *
		move.w	#%0000000100000000,bltcon0
		bra.s	printplatc

printplatpr:	move.w	#%0000100111110000,bltcon0
printplatc:	move.l	a3,bltdpth		;dest.adr.	
		move.l	a0,bltapth		;a0=bufferadr
		move.w	#0,bltcon1
		move.w	d4,bltdmod
		move.w	d2,bltamod
		move.w	#4,d3		;4 bitplanes
pplatloop:	move.w	d5,bltsize
		jsr	waitblit
		add.l	#$2800,a3	;next plane (PAL)
		add.l	#$f00,a0	;next plane in platformscreen
		move.l	a3,bltdpth	;dest.addr.
		move.l	a0,bltapth	;src.addr.
		subq	#1,d3
		bne.s	pplatloop
		rts



;---------------------------------------------------

;*
;* calculate bob-data to be used for mario:
;*

calcsprite:	cmp.w	#1,marioactive
		beq.s	csmact
		rts
csmact:		cmp.w	#0,pause
		beq.s	calcspritec
		rts

calcspritec:	cmp.w	#1,animbyte
		bne.s	csnoj
		cmp.w	#2,jumpbyte
		beq.s	csnojl
		cmp.w	#0,facing
		beq.s	csnojl
		move.l	gfx1,mariobobadr
		add.l	#$378,mariobobadr	;jump up-right
		move.l	mask1,mariomaskadr
		add.l	#$378,mariomaskadr
		rts
csnojl:		move.l	gfx1,mariobobadr
		add.l	#$01c,mariobobadr	;jump up-left
		move.l	mask1,mariomaskadr
		add.l	#$01c,mariomaskadr
		rts

;* run right*
csnoj:		cmp.w	#2,animbyte		;run right
		bne.L	csnorr
		move.l	gfx1,d0
		add.l	#952,d0
		cmp.l	mariobobadr,d0	;last frame
		bne.s	csnewrun
		move.l	gfx1,mariobobadr
		add.l	#932,mariobobadr	;first frame
		move.l	mask1,mariomaskadr
		add.l	#932,mariomaskadr
		rts
csnewrun:	move.l	gfx1,d0
		add.l	#952,d0
		cmp.l	mariobobadr,d0	;correct if mario
		blt.s	cscorfr			;has just faced
		move.l	gfx1,d0
		add.l	#928,d0
		cmp.l	mariobobadr,d0
		blt.s	csnocor

cscorfr:	move.l	gfx1,mariobobadr
		add.l	#932,mariobobadr	;opp. dir.
		move.l	mask1,mariomaskadr
		add.l	#932,mariomaskadr
		rts
csnocor:	add.l	#4,mariobobadr
		add.l	#4,mariomaskadr
		rts

;* run left *
csnorr:		cmp.w	#3,animbyte		;run left
		bne.L	csnorl
		move.l	gfx1,d0
		add.l	#64,d0
		cmp.l	mariobobadr,d0
		bne.s	csnewrunl
		move.l	gfx1,mariobobadr
		add.l	#44,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#44,mariomaskadr
		rts
csnewrunl:	move.l	gfx1,d0
		add.l	#64,d0
		cmp.l	mariobobadr,d0
		bhi.s	csnocor2
		move.l	gfx1,mariobobadr
		add.l	#44,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#44,mariomaskadr
		rts
csnocor2:	add.l	#4,mariobobadr
		add.l	#4,mariomaskadr
		rts

;* skid right *
csnorl:		cmp.w	#4,animbyte		;skid right
		bne.L	csnosr
		cmp.w	#4,gfxcnt		;time for new frame?
		bls.s	cssrex
		move.w	#0,gfxcnt
		move.l	gfx1,d0
		add.l	#920,d0
		cmp.l	mariobobadr,d0
		beq.s	cssrof			;other frame
		move.l	gfx1,mariobobadr
		add.l	#920,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#920,mariomaskadr
		rts
cssrof:		move.l	gfx1,mariobobadr
		add.l	#924,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#924,mariomaskadr
		rts
cssrex:		add.w	#1,gfxcnt
		rts

;* skid left *
csnosr:		cmp.w	#5,animbyte		;skid left
		bne.s	csnosl
		cmp.w	#4,gfxcnt
		bls.s	cssrex
		move.w	#0,gfxcnt
		move.l	gfx1,d0
		add.l	#76,d0
		cmp.l	mariobobadr,d0
		beq.s	csslof
		move.l	gfx1,mariobobadr
		add.l	#76,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#76,mariomaskadr
		rts
csslof:		move.l	gfx1,mariobobadr
		add.l	#72,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#72,mariomaskadr
		rts

csnosl:		cmp.w	#1,animbyte
		bne.s	csnoju
		cmp.w	#0,facing		;jump up
		beq.s	csjafl
		move.l	gfx1,mariobobadr
		add.l	#$378,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#$378,mariomaskadr
		rts
csjafl:		move.l	gfx1,mariobobadr
		add.l	#$01c,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#$01c,mariomaskadr
		rts

;* dead *
csnoju:		cmp.w	#6,animbyte	;mario dead?
		bne	csnodead1
		add.w	#1,deadcnt	;mario dead!
		cmp.w	#0,facing
		beq.s	csdead1fl
		cmp.w	#50,deadcnt	;faced r
		blo.s	csd1f1
		move.w	#0,facing	;use same waveroutine
		rts

csd1f1:		move.l	gfx1,mariobobadr
		move.l	mask1,mariomaskadr
;type of death?
		cmp.w	#0,deadno
		beq.s	csd1f1ord
		cmp.w	#1,deadno
		beq.s	csd1f1ice
		add.l	#4476,mariobobadr	;burnt right
		add.l	#4476,mariomaskadr
		rts
csd1f1ord:	add.l	#3544,mariobobadr	;dead right!
		add.l	#3544,mariomaskadr
		rts
csd1f1ice:	add.l	#3548,mariobobadr	;iced right
		add.l	#3548,mariomaskadr
		rts

csdead1fl:	cmp.w	#50,deadcnt
		blo	csd1f1l
		add.w	#1,deadcnt2
		cmp.w	#5,deadcnt2	;time for new anim?
		beq.s	cswaven
		rts

cswaven:	eor.w	#1,deadflag
		beq.s	cswave1
		move.l	gfx1,mariobobadr
		move.l	mask1,mariomaskadr
;type of death?
		cmp.w	#0,deadno
		beq.s	cswavenord
		cmp.w	#1,deadno
		beq.s	cswavenice
		add.l	#3596,mariobobadr	;burnt wave
		add.l	#3596,mariomaskadr
		bra	cswavenex
cswavenord:	add.l	#3528,mariobobadr	;dead wave
		add.l	#3528,mariomaskadr
		bra	cswavenex
cswavenice:	add.l	#3536,mariobobadr	;iced wave
		add.l	#3536,mariomaskadr
cswavenex:	move.w	#0,deadcnt2
		rts

cswave1:	move.l	gfx1,mariobobadr
		move.l	mask1,mariomaskadr
;type of death?
		cmp.w	#0,deadno
		beq.s	cswave1ord
		cmp.w	#1,deadno
		beq.s	cswave1ice
		add.l	#3592,mariobobadr	;burnt wave1
		add.l	#3592,mariomaskadr
		bra	cswave1ex
cswave1ord:	add.l	#3532,mariobobadr	;dead wave1
		add.l	#3532,mariomaskadr
		bra	cswave1ex
cswave1ice:	add.l	#3540,mariobobadr	;iced wave1
		add.l	#3540,mariomaskadr
cswave1ex:	move.w	#0,deadcnt2
		rts


csd1f1l:	move.l	gfx1,mariobobadr
		move.l	mask1,mariomaskadr
;type of death?
		cmp.w	#0,deadno
		beq.s	csd1f1lord
		cmp.w	#1,deadno
		beq.s	csd1f1lice
		add.l	#4472,mariobobadr	;burnt left
		add.l	#4472,mariomaskadr
		rts
csd1f1lord:	add.l	#3520,mariobobadr	;dead left
		add.l	#3520,mariomaskadr
		rts
csd1f1lice:	add.l	#3524,mariobobadr	;iced left
		add.l	#3524,mariomaskadr
		rts


;* pushed right *
csnodead1:	cmp.w	#7,animbyte		;pushed right?
		bne.s	csnopushr
		move.l	gfx1,mariobobadr
		add.l	#$398,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#$398,mariomaskadr
		rts				

;* pushed left *	
csnopushr:	cmp.w	#8,animbyte		;pushed left?
		bne.s	csnopushl
		move.l	gfx1,mariobobadr
		add.l	#76,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#76,mariomaskadr
		rts

;* standing *
csnopushl:	cmp.w	#0,facing		;stand left?
		beq.s	cssafl
		move.l	gfx1,mariobobadr
		add.l	#956,mariobobadr	;stand right
		move.l	mask1,mariomaskadr
		add.l	#956,mariomaskadr
		rts
cssafl:		move.l	gfx1,mariobobadr
		add.l	#40,mariobobadr
		move.l	mask1,mariomaskadr
		add.l	#40,mariomaskadr
		rts


;----------------------------------------------------

;*
;* calculate bob-data to be used for luigi:
;*

calcspriteluigi:
		cmp.w	#1,luigiactive
		beq.s	calcslact
		rts

calcslact:	cmp.w	#0,pause
		beq.s	calcslnp
		rts

calcslnp:	cmp.w	#1,animbyteluigi
		bne.s	csnojlu
		cmp.w	#2,jumpbyteluigi
		beq.s	csnojllu
		cmp.w	#0,facingluigi
		beq.s	csnojllu
		move.l	gfx1,luigibobadr
		add.l	#$378+1760,luigibobadr	;jump up-right
		move.l	mask1,luigimaskadr
		add.l	#$378,luigimaskadr
		rts
csnojllu:	move.l	gfx1,luigibobadr
		add.l	#$01c+1760,luigibobadr	;jump up-left
		move.l	mask1,luigimaskadr
		add.l	#$01c,luigimaskadr
		rts

csnojlu:	cmp.w	#2,animbyteluigi	;run right
		bne.L	csnorrlu
		move.l	gfx1,d0
		add.l	#948+1760,d0
		cmp.l	luigibobadr,d0	;last frame
		bne.s	csnewrunlu
		move.l	gfx1,luigibobadr
		add.l	#932+1760,luigibobadr	;first frame
		move.l	mask1,luigimaskadr
		add.l	#932,luigimaskadr
		rts
csnewrunlu:	move.l	gfx1,d0
		add.l	#928+1760,d0
		cmp.l	luigibobadr,d0	;correct if luigi
		blt.s	csnocorlu		;has just faced
cscorfrlu:	move.l	gfx1,luigibobadr
		add.l	#932+1760,luigibobadr	;opp. dir.
		move.l	mask1,luigimaskadr
		add.l	#932,luigimaskadr
		rts
csnocorlu:	move.l	gfx1,d0
		add.l	#952+1760,d0
		cmp.l	luigibobadr,d0
		blt.s	cscorfrlu
		add.l	#$4,luigibobadr		;add only if 
		add.l	#$4,luigimaskadr
		rts

csnorrlu:	cmp.w	#3,animbyteluigi	;run left
		bne.L	csnorllu
		move.l	gfx1,d0
		add.l	#64+1760,d0
		cmp.l	luigibobadr,d0
		bne.s	csnewrunllu
		move.l	gfx1,luigibobadr
		add.l	#44+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#44,luigimaskadr
		rts
csnewrunllu:	move.l	gfx1,d0
		add.l	#$100+1760,d0
		cmp.l	luigibobadr,d0
		bhi.s	csnocor2lu
		move.l	gfx1,luigibobadr
		add.l	#$030+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#$030,luigimaskadr
		rts
csnocor2lu:	add.l	#$4,luigibobadr
		add.l	#$4,luigimaskadr
		rts


csnorllu:	cmp.w	#4,animbyteluigi	;skid right
		bne.L	csnosrlu
		cmp.w	#4,gfxcntluigi		;time for new frame?
		bls.s	cssrexlu
		move.w	#0,gfxcntluigi
		move.l	gfx1,d0
		add.l	#920+1760,d0
		cmp.l	luigibobadr,d0
		beq.s	cssroflu		;other frame
		move.l	gfx1,luigibobadr
		add.l	#920+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#920,luigimaskadr
		rts
cssroflu:	move.l	gfx1,luigibobadr
		add.l	#924+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#924,luigimaskadr
		rts
cssrexlu:	add.w	#1,gfxcntluigi
		rts

csnosrlu:	cmp.w	#5,animbyteluigi	;skid left
		bne.s	csnosllu
		cmp.w	#4,gfxcntluigi
		bls.s	cssrexlu
		move.w	#0,gfxcntluigi
		move.l	gfx1,d0
		add.l	#76+1760,d0
		cmp.l	luigibobadr,d0
		beq.s	cssloflu
		move.l	gfx1,luigibobadr
		add.l	#76+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#76,luigimaskadr
		rts
cssloflu:	move.l	gfx1,luigibobadr
		add.l	#72+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#72,luigimaskadr
		rts

csnosllu:	cmp.w	#1,animbyteluigi
		bne.s	csnojulu
		cmp.w	#0,facingluigi		;jump up
		beq.s	csjafllu
		move.l	gfx1,luigibobadr
		add.l	#$378+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#$378,luigimaskadr
		rts
csjafllu:	move.l	gfx1,luigibobadr
		add.l	#$01c+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#$01c,luigimaskadr
		rts

;* dead *
csnojulu:	cmp.w	#6,animbyteluigi	;luigi dead?
		bne	csnodead1lu
		add.w	#1,deadcntluigi	;luigi dead!
		cmp.w	#0,facingluigi
		beq.s	csdead1fllu
		cmp.w	#50,deadcntluigi	;faced r
		blo.s	csd1f1lu
		move.w	#0,facingluigi	;use same waveroutine
		rts

csd1f1lu:	move.l	gfx1,luigibobadr
		move.l	mask1,luigimaskadr
;type of death?
		cmp.w	#0,deadnoluigi
		beq.s	csd1f1ordlu
		cmp.w	#1,deadnoluigi
		beq.s	csd1f1icelu
		add.l	#4476,luigibobadr	;burnt right
		add.l	#4476,luigimaskadr
		rts
csd1f1ordlu:	add.l	#4464,luigibobadr	;dead right!
		add.l	#4464,luigimaskadr
		rts
csd1f1icelu:	add.l	#4468,luigibobadr	;iced right
		add.l	#4468,luigimaskadr
		rts

csdead1fllu:	cmp.w	#50,deadcntluigi
		blo	csd1f1llu
		add.w	#1,deadcnt2luigi
		cmp.w	#5,deadcnt2luigi	;time for new anim?
		beq.s	cswavenlu
		rts

cswavenlu:	eor.w	#1,deadflagluigi
		beq.s	cswave1lu
		move.l	gfx1,luigibobadr
		move.l	mask1,luigimaskadr
;type of death?
		cmp.w	#0,deadnoluigi
		beq.s	cswavenordlu
		cmp.w	#1,deadnoluigi
		beq.s	cswavenicelu
		add.l	#3596,luigibobadr	;burnt wave
		add.l	#3596,luigimaskadr
		bra	cswavenexlu
cswavenordlu:	add.l	#4448,luigibobadr	;dead wave
		add.l	#4448,luigimaskadr
		bra	cswavenexlu
cswavenicelu:	add.l	#4456,luigibobadr	;iced wave
		add.l	#4456,luigimaskadr
cswavenexlu:	move.w	#0,deadcnt2luigi
		rts

cswave1lu:	move.l	gfx1,luigibobadr
		move.l	mask1,luigimaskadr
;type of death?
		cmp.w	#0,deadnoluigi
		beq.s	cswave1ordlu
		cmp.w	#1,deadnoluigi
		beq.s	cswave1icelu
		add.l	#3592,luigibobadr	;burnt wave1
		add.l	#3592,luigimaskadr
		bra	cswave1exlu
cswave1ordlu:	add.l	#4452,luigibobadr	;dead wave1
		add.l	#4452,luigimaskadr
		bra	cswave1exlu
cswave1icelu:	add.l	#4460,luigibobadr	;iced wave1
		add.l	#4460,luigimaskadr
cswave1exlu:	move.w	#0,deadcnt2luigi
		rts


csd1f1llu:	move.l	gfx1,luigibobadr
		move.l	mask1,luigimaskadr
;type of death?
		cmp.w	#0,deadnoluigi
		beq.s	csd1f1lordlu
		cmp.w	#1,deadnoluigi
		beq.s	csd1f1licelu
		add.l	#4472,luigibobadr	;burnt left
		add.l	#4472,luigimaskadr
		rts
csd1f1lordlu:	add.l	#4440,luigibobadr	;dead left
		add.l	#4440,luigimaskadr
		rts
csd1f1licelu:	add.l	#4444,luigibobadr	;iced left
		add.l	#4444,luigimaskadr
		rts


;* pushed right *
csnodead1lu:	cmp.w	#7,animbyteluigi	;pushed right?
		bne.s	csnopushrlu
		move.l	gfx1,luigibobadr
		add.l	#$398+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#$398,luigimaskadr
		rts				

csnopushrlu:	cmp.w	#8,animbyteluigi
		bne.s	csnopushllu
		move.l	gfx1,luigibobadr
		add.l	#76+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#76,luigimaskadr
		rts

csnopushllu:	cmp.w	#0,facingluigi		;stand left?
		beq.s	cssafllu
		move.l	gfx1,luigibobadr
		add.l	#956+1760,luigibobadr	;stand right
		move.l	mask1,luigimaskadr
		add.l	#956,luigimaskadr
		rts
cssafllu:	move.l	gfx1,luigibobadr
		add.l	#40+1760,luigibobadr
		move.l	mask1,luigimaskadr
		add.l	#40,luigimaskadr
		rts


;-------------------------------------------------------



;*
;* control enemies:
;* 
;* nmetype: #1 = turtle
;*	    #2 = lobster
;*	    #3 = fly
;*	    #4 = iceman
;*	    #5 = coin
;*	    #6 = fireball
;*
;* d0,d7 reserved, do not use
;*

contnme:	cmp.w	#0,pause	;game paused?
		beq.s	contnmeok
		rts
contnmeok:	move.l	#0,d0	;nme1offset=0
		move.l	#0,d7
cnloop:		bsr	nmecont
		add.l	#4,d7
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	cnloop
		rts

nmecont:	lea	nmeactive(pc),a0
		cmp.w	#1,(a0,d0)	;nmeactive?
		beq.s	nmecact
		cmp.w	#4,(a0,d0)
		beq.s	nmecact
		cmp.w	#5,(a0,d0)
		beq.s	nmecact
		rts			;no!
nmecact:	lea	nmetype(pc),a0	;type?
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;turtle?
		beq.s	turtle
		cmp.w	#2,d1		;lobster?
		beq	lobster
		cmp.w	#3,d1		;fly?
		beq	fly
		cmp.w	#4,d1		;iceman?
		beq	iceman
		cmp.w	#5,d1		;coin?
		beq	coin
		cmp.w	#6,d1		;fireball?
		beq	fire
		rts

;----------------------------------------------------

;*
;* control turtle:
;*

; animbyte:
; 1 = going right
; 2 = going left
; 3 = falling
; 4 = hit
; 5 = dead
; 6 = hit2
; 7 = turning
; 8 = rehit

turtle:		lea	nmeanimbyte(pc),a0
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;going right?
		beq	turtlegr
		cmp.w	#2,d1		;going left?
		beq	turtlegl
		cmp.w	#3,d1
		beq	turtlefall	;falling?
		cmp.w	#5,d1		;dead?
		beq	turtledead
		cmp.w	#4,d1
		beq	turtlehit
		cmp.w	#6,d1
		beq	turtlehit2	;hit but not moving
		cmp.w	#8,d1
		beq	turtlerehit
		cmp.w	#7,d1
		beq	turtleturn
		rts


;** turtle going right **

turtlegr:	lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		lea	nmeaggr(pc),a5
		cmp.w	#1,(a5,d0)	;red turtle?
		beq.L	tgraggr1
		cmp.w	#2,(a5,d0)	;blue turtle?
		beq.L	tgraggr2

;** green turtle **

		add.w	#4,(a3,d0)	;xpos:+4 (speed=4)
		move.l	gfx2,d2
		add.l	#928,d2
		cmp.l	(a0,d7),d2
		bgt.s	tgrnew
		move.l	gfx2,d2
		add.l	#940,d2
		cmp.l	(a0,d7),d2
		ble.s	tgrnew
		cmp.w	#5,(a2,d0)
		beq.s	tgrtfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
tgrtfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
tgrnew:		cmp.w	#5,(a2,d0)
		beq.s	tgrtfnff
		add.w	#1,(a2,d0)
		rts
tgrtfnff:	move.l	gfx2,(a0,d7)
		add.l	#928,(a0,d7)  ;time for new 1. frame
		move.l	mask2,(a1,d7)
		add.l	#928,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** red turtle **

tgraggr1:	add.w	#6,(a3,d0)	;xpos:+speed
		move.l	gfx2,d2
		add.l	#3568,d2
		cmp.l	(a0,d7),d2
		bgt.s	tgra1new
		move.l	gfx2,d2
		add.l	#3580,d2
		cmp.l	(a0,d7),d2
		ble.s	tgra1new
		cmp.w	#5,(a2,d0)
		beq.s	tgra1tfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
tgra1tfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
tgra1new:	cmp.w	#5,(a2,d0)
		beq.s	tgrta1fnff
		add.w	#1,(a2,d0)
		rts
tgrta1fnff:	move.l	gfx2,(a0,d7)
		add.l	#3568,(a0,d7)  ;time for new 1. frame
		move.l	mask2,(a1,d7)
		add.l	#3568,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** blue turtle **

tgraggr2:	add.w	#8,(a3,d0)	;xpos:+speed
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		
		move.l	gfx2,d2
		add.l	#6208,d2
		cmp.l	(a0,d7),d2
		bgt.s	tgra2new
		move.l	gfx2,d2
		add.l	#6220,d2
		cmp.l	(a0,d7),d2
		ble.s	tgra2new
		cmp.w	#5,(a2,d0)
		beq.s	tgra2tfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
tgra2tfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
tgra2new:	cmp.w	#5,(a2,d0)
		beq.s	tgrta2fnff
		add.w	#1,(a2,d0)
		rts
tgrta2fnff:	move.l	gfx2,(a0,d7)
		add.l	#6208,(a0,d7)  ;time for new 1. frame
		move.l	mask2,(a1,d7)
		add.l	#6208,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** turtle going left **

turtlegl:	lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		lea	nmeaggr(pc),a5
		cmp.w	#1,(a5,d0)	;red turtle?
		beq.L	tglaggr1
		cmp.w	#2,(a5,d0)	;blue turtle?
		beq.L	tglaggr2

;** green turtle **

		sub.w	#4,(a3,d0)
		move.l	gfx2,d2
		add.l	#40,d2
		cmp.l	(a0,d7),d2
		bgt.s	tglnew
		move.l	gfx2,d2
		add.l	#52,d2
		cmp.l	(a0,d7),d2
		ble.s	tglnew
		cmp.w	#5,(a2,d0)
		beq.s	tgltfnf
		add.w	#1,(a2,d0)
		rts
tgltfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
tglnew:		cmp.w	#5,(a2,d0)
		beq.s	tgltfnff
		add.w	#1,(a2,d0)
		rts
tgltfnff:	move.l	gfx2,(a0,d7)
		add.l	#40,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#40,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** red turtle **

tglaggr1:	sub.w	#6,(a3,d0)
		move.l	gfx2,d2
		add.l	#2680,d2
		cmp.l	(a0,d7),d2
		bgt.s	tgla1new
		move.l	gfx2,d2
		add.l	#2692,d2
		cmp.l	(a0,d7),d2
		ble.s	tgla1new
		cmp.w	#5,(a2,d0)
		beq.s	tgla1tfnf
		add.w	#1,(a2,d0)
		rts
tgla1tfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
tgla1new:	cmp.w	#5,(a2,d0)
		beq.s	tgla1tfnff
		add.w	#1,(a2,d0)
		rts
tgla1tfnff:	move.l	gfx2,(a0,d7)
		add.l	#2680,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#2680,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** blue turtle **

tglaggr2:	sub.w	#8,(a3,d0)
		move.l	gfx2,d2
		add.l	#5320,d2
		cmp.l	(a0,d7),d2
		bgt.s	tgla2new
		move.l	gfx2,d2
		add.l	#5332,d2
		cmp.l	(a0,d7),d2
		ble.s	tgla2new
		cmp.w	#5,(a2,d0)
		beq.s	tgla2tfnf
		add.w	#1,(a2,d0)
		rts
tgla2tfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
tgla2new:	cmp.w	#5,(a2,d0)
		beq.s	tgla2tfnff
		add.w	#1,(a2,d0)
		rts
tgla2tfnff:	move.l	gfx2,(a0,d7)
		add.l	#5320,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#5320,(a1,d7)
		move.w	#0,(a2,d0)
		rts


;** turtle falling **

turtlefall:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexpos(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmefacing(pc),a4
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		add.w	#2,(a1,d0)	;tabcnt+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next from table
		cmp.w	#$80,d1
		beq	tjumpfin
		cmp.w	(a6,d0),d1
		bge.s	tjnod
		add.w	d1,(a3,d0)
		bra	tjcont2

tjnod:		move.w	(a6,d0),d1
		add.w	d1,(a3,d0)	;ypos:+maxdown
		bra.s	tjumpfin	;turtle has landed!
		
tjcont2:	move.w	(a5,d0),d2
		cmp.w	#1,(a4,d0)	;facing right?
		bne.s	tjex
		add.w	d2,(a2,d0)
		rts
tjex:		sub.w	d2,(a2,d0)
		rts

tjumpfin:	lea	nmeanimbyte(pc),a0
		move.w	#46,(a1,d0)
		cmp.w	#1,(a4,d0)
		bne.s	tjnod3
		move.w	#1,(a0,d0)	;animbyte=walk right
		rts
tjnod3:		move.w	#2,(a0,d0)
		rts

;** turtle hit **

turtlehit:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexoffset(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6

		move.w	(a2,d0),d1
		lea	nmexpos(pc),a2
		add.w	d1,(a2,d0)
		
		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next jumpoffset in d1
		cmp.w	#$80,d1
		beq.s	thitfin
		cmp.w	(a6,d0),d1
		bgt.s	thitjnod
		add.w	d1,(a3,d0)	;ypos:+tableoffset
		rts

thitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

thitfin:	lea	nmeanimbyte(pc),a0
		move.w	#6,(a0,d0)
		rts


;** turtle dead **

turtledead:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmeyposition(pc),a2
		lea	nmexpos(pc),a3
		add.w	#2,(a1,d0)
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1
		add.w	d1,(a2,d0)	;ny ypos
;get next anim
		lea	nmebobadr(pc),a4
		lea	nmemaskadr(pc),a5
		lea	nmeaggr(pc),a6
		cmp.w	#2,(a6,d0)
		beq.L	tdeada2
		cmp.w	#1,(a6,d0)
		beq.L	tdeada1

;* green turtle **

		lea	nmewalkcnt(pc),a6
		move.l	gfx2,d2
		add.l	#64,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeadnew	;bobadr<64?
		move.l	gfx2,d2
		add.l	#76,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeadnf		;bobadr<76
		beq.L	tdeadsp	  ;anim at end of row/shift to new
		move.l	gfx2,d2
		add.l	#956,d2
		cmp.l	(a4,d7),d2
		blt.s	tdeadnew	;bobadr>956
		move.l	gfx2,d2
		add.l	#944,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeadnew	;bobadr<944

tdeadnf:	cmp.w	#2,(a6,d0)
		beq.s	tdeadtfnf
		add.w	#1,(a6,d0)	;walkcnt:+1
		bra	tdeadmain

tdeadtfnf:	add.l	#$4,(a4,d7)	;time for new frame
		add.l	#$4,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

tdeadnew:	cmp.w	#2,(a6,d0)	;walkcnt>=2?
		bge.s	tdeadff
		add.w	#1,(a6,d0)
		bra	tdeadmain

tdeadff:	move.l	gfx2,(a4,d7)
		add.l	#64,(a4,d7)  ;time for new 1. frame
		move.l	mask2,(a5,d7)
		add.l	#64,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

tdeadsp:	cmp.w	#2,(a6,d0)
		beq.s	tdeadspi
		add.w	#1,(a6,d0)
		bra	tdeadmain
		
tdeadspi:	move.l	gfx2,(a4,d7)
		add.l	#944,(a4,d7)
		move.l	mask2,(a5,d7)
		add.l	#944,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

;* red turtle *

tdeada1:	lea	nmewalkcnt(pc),a6
		move.l	gfx2,d2
		add.l	#64+2640,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeada1new
		move.l	gfx2,d2
		add.l	#76+2640,d2
		cmp.l	(a4,d7),d2
		bgt.L	tdeada1nf
		move.l	gfx2,d2
		add.l	#76+2640,d2
		cmp.l	(a4,d7),d2
		beq.L	tdeada1sp	;anim at end of row/shift to new
		move.l	gfx2,d2
		add.l	#956+2640,d2
		cmp.l	(a4,d7),d2
		blt.L	tdeada1new
		move.l	gfx2,d2
		add.l	#944+2640,d2
		cmp.l	(a4,d7),d2
		bgt.L	tdeada1new

tdeada1nf:	cmp.w	#2,(a6,d0)
		beq.s	tdeada1tfnf
		add.w	#1,(a6,d0)	;walkcnt:+1
		bra	tdeadmain

tdeada1tfnf:	add.l	#$4,(a4,d7)	;time for new frame
		add.l	#$4,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

tdeada1new:	cmp.w	#2,(a6,d0)
		bge.s	tdeada1ff
		add.w	#1,(a6,d0)
		bra	tdeadmain

tdeada1ff:	move.l	gfx2,(a4,d7)
		add.l	#64+2640,(a4,d7)  ;time for new 1. frame
		move.l	mask2,(a5,d7)
		add.l	#64+2640,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

tdeada1sp:	cmp.w	#2,(a6,d0)
		beq.s	tdeada1spi
		add.w	#1,(a6,d0)
		bra	tdeadmain
		
tdeada1spi:	move.l	gfx2,(a4,d7)
		add.l	#944+2640,(a4,d7)
		move.l	mask2,(a5,d7)
		add.l	#944+2640,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

;* blue turtle *

tdeada2:	lea	nmewalkcnt(pc),a6
		move.l	gfx2,d2
		add.l	#64+5280,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeada2new
		move.l	gfx2,d2
		add.l	#76+5280,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeada2nf
		move.l	gfx2,d2
		add.l	#76+5280,d2
		cmp.l	(a4,d7),d2
		beq.L	tdeada2sp	;anim at end of row/shift to new
		move.l	gfx2,d2
		add.l	#956+5280,d2
		cmp.l	(a4,d7),d2
		blt.s	tdeada2new
		move.l	gfx2,d2
		add.l	#944+5280,d2
		cmp.l	(a4,d7),d2
		bgt.s	tdeada2new

tdeada2nf:	cmp.w	#2,(a6,d0)
		beq.s	tdeada2tfnf
		add.w	#1,(a6,d0)	;walkcnt:+1
		bra	tdeadmain

tdeada2tfnf:	add.l	#$4,(a4,d7)	;time for new frame
		add.l	#$4,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

tdeada2new:	cmp.w	#2,(a6,d0)
		bge.s	tdeada2ff
		add.w	#1,(a6,d0)
		bra	tdeadmain

tdeada2ff:	move.l	gfx2,(a4,d7)
		add.l	#64+5280,(a4,d7)  ;time for new 1. frame
		move.l	mask2,(a5,d7)
		add.l	#64+5280,(a5,d7)
		move.w	#0,(a6,d0)
		bra	tdeadmain

tdeada2sp:	cmp.w	#2,(a6,d0)
		beq.s	tdeada2spi
		add.w	#1,(a6,d0)
		bra	tdeadmain
		
tdeada2spi:	move.l	gfx2,(a4,d7)
		add.l	#944+5280,(a4,d7)
		move.l	mask2,(a5,d7)
		add.l	#944+5280,(a5,d7)
		move.w	#0,(a6,d0)


;main shared by all turtles
tdeadmain:	cmp.w	#168,(a2,d0)
		bgt.s	tdeadfin
		lea	nmexposition(pc),a1
		lea	nmexpos(pc),a2
		move.w	(a1,d0),d1
		cmp.w	marioxpos,d1
		blt.s	tdeadfl

;turtle falling right
		add.w	#8,(a2,d0)	;ny xpos
		rts
tdeadfl:	sub.w	#8,(a2,d0)
		rts

;launch coin
tdeadfin:	bsr	launchcoin	;d0/d7 = offset for nme
		rts



;** turtle hit again **

turtlerehit:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		lea	nmeanimbyte(pc),a2
		add.w	#2,(a1,d0)
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1
		cmp.w	#$80,d1
		beq.s	trehitfin
		cmp.w	(a6,d0),d1
		bgt.s	trehitjnod
		add.w	d1,(a3,d0)
		rts
trehitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

trehitfin:	lea	nmefacing(pc),a4
		cmp.w	#1,(a4,d0)
		beq.s	trehitgr
		move.w	#2,(a2,d0)
		rts
trehitgr:	move.w	#1,(a2,d0)
		rts


;*** turtle hit once/has landed. ***

;turtle should move on
;animbyte = getting up
;inc(aggr), animbyte = walk

turtlehit2:	lea	nmetimer1(pc),a4
		lea	nmeaggr(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		add.w	#1,(a4,d0)
		cmp.w	#500,(a4,d0)	;wait 10 sec.
		bls	thit2ok

;fin. waiting
		lea	nmetimer2(pc),a5
		add.w	#1,(a5,d0)
		cmp.w	#10,(a5,d0)	;time for new frame?
		beq.s	thit2tfnf
		rts
thit2tfnf:	move.w	#0,(a5,d0)	;timer2=0
		cmp.w	#2,(a3,d0)	;aggr?
		beq.s	thit2tfnf2
		cmp.w	#1,(a3,d0)
		beq.s	thit2tfnf1
;aggr=0
		move.l	gfx2,d2
		add.l	#1800+32,d2
		cmp.l	(a0,d7),d2	;fin?
		beq.s	thit2nai
		add.l	#4,(a0,d7)	;next frame
		add.l	#4,(a1,d7)	;next mask
		rts
thit2tfnf1:	move.l	gfx2,d2
		add.l	#4440+32,d2
		cmp.l	(a0,d7),d2
		beq.s	thit2nai
		add.l	#4,(a0,d7)
		add.l	#4,(a1,d7)
		rts
thit2tfnf2:	move.l	gfx2,d2
		add.l	#7080+32,d2
		cmp.l	(a0,d7),d2
		beq.s	thit2nai
		add.l	#4,(a0,d7)
		add.l	#4,(a1,d7)
		rts

;start walking again
thit2nai:	move.w	#0,(a4,d0)	;timer1=0
		move.w	#0,(a5,d0)	;timer2=0
		cmp.w	#2,(a3,d0)	;aggr?
		beq.s	thit2naic
		add.w	#1,(a3,d0)
thit2naic:	lea	nmeanimbyte(pc),a3
		lea	nmefacing(pc),a4
		cmp.w	#1,(a4,d0)
		beq.s	thit2gr
		move.w	#2,(a3,d0)
		rts
thit2gr:	move.w	#1,(a3,d0)	;walk r.
		rts

;waiting to get up
thit2ok:	cmp.w	#2,(a3,d0)	;aggr
		beq.s	thit2a3
		cmp.w	#1,(a3,d0)
		beq.s	thit2a2

thit2a1:	move.l	gfx2,(a0,d7)
		add.l	#1800,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#1800,(a1,d7)
		rts
thit2a2:	move.l	gfx2,(a0,d7)
		add.l	#4440,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#4440,(a1,d7)
		rts
thit2a3:	move.l	gfx2,(a0,d7)
		add.l	#7080,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#7080,(a1,d7)
		rts


;*** turtle turning ***

turtleturn:	lea	nmeanimbyte(pc),a0
		lea	nmefacing(pc),a1
		lea	nmetimer1(pc),a2
		add.w	#1,(a2,d0)
		cmp.w	#10,(a2,d0)
		blt.L	tturnex
		lea	nmebobadr(pc),a3
		lea	nmemaskadr(pc),a4
		lea	nmeaggr(pc),a5

		cmp.w	#1,(a1,d0)
		beq.L	tturnl

tturnr:		cmp.w	#10,(a2,d0)	;time=1.frame?
		bne.s	tturnrnff

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	tturnra21
		cmp.w	#1,(a5,d0)	;agg=1?
		beq.s	tturnra11
tturnra01:	move.l	gfx2,(a3,d7)
		add.l	#56,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#56,(a4,d7)
		rts
tturnra11:	move.l	gfx2,(a3,d7)
		add.l	#56+2640,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#56+2640,(a4,d7)
		rts
tturnra21:	move.l	gfx2,(a3,d7)
		add.l	#56+5280,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#56+5280,(a4,d7)
		rts


tturnrnff:	cmp.w	#20,(a2,d0)	;time=2.frame?
		bne.s	tturnrnsf

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	tturnra22
		cmp.w	#1,(a5,d0)
		beq.s	tturnra12
tturna02:	move.l	gfx2,(a3,d7)
		add.l	#60,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#60,(a4,d7)
		rts
tturnra12:	move.l	gfx2,(a3,d7)
		add.l	#60+2640,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#60+2640,(a4,d7)
		rts
tturnra22:	move.l	gfx2,(a3,d7)
		add.l	#60+5280,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#60+5280,(a4,d7)
		rts

tturnrnsf:	cmp.w	#30,(a2,d0)
		beq.s	tturnrdot
		rts

tturnrdot:	move.w	#1,(a0,d0)	;go right
		move.w	#1,(a1,d0)	;face right
		move.w	#0,(a2,d0)	;timer1=0
tturnex:	rts


;*** turning left ***

tturnl:		cmp.w	#10,(a2,d0)	;time=1.frame?
		bne.s	tturnlnff
		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	tturnla21
		cmp.w	#1,(a5,d0)
		beq.s	tturnla11
tturnla01:	move.l	gfx2,(a3,d7)
		add.l	#924,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#924,(a4,d7)
		rts
tturnla11:	move.l	gfx2,(a3,d7)
		add.l	#924+2640,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#924+2640,(a4,d7)
		rts
tturnla21:	move.l	gfx2,(a3,d7)
		add.l	#924+5280,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#924+5280,(a4,d7)
		rts

tturnlnff:	cmp.w	#20,(a2,d0)	;time=2.frame?
		bne.s	tturnlnsf
		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	tturnla22
		cmp.w	#1,(a5,d0)
		beq.s	tturnla12
tturnla02:	move.l	gfx2,(a3,d7)
		add.l	#920,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#920,(a4,d7)
		rts
tturnla12:	move.l	gfx2,(a3,d7)
		add.l	#920+2640,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#920+2640,(a4,d7)
		rts
tturnla22:	move.l	gfx2,(a3,d7)
		add.l	#920+5280,(a3,d7)
		move.l	mask2,(a4,d7)
		add.l	#920+5280,(a4,d7)
		rts

tturnlnsf:	cmp.w	#30,(a2,d0)
		beq.s	tturnldot
		rts

tturnldot:	move.w	#2,(a0,d0)	;do turn
		move.w	#2,(a1,d0)
		move.w	#0,(a2,d0)	;timer1=0
tturnlex:	rts



;----------------------------------------------------

;*
;* control lobster:
;*

; animbyte:
; 1 = going right
; 2 = going left
; 3 = falling
; 4 = hit
; 5 = dead
; 6 = hit2
; 7 = turning
; 8 = rehit

; aggr:
; 0 = red not hit (different below!)
; 1 = red +hit
; 2 = blue not hit
; 3 = blue +hit


lobster:	lea	nmeanimbyte(pc),a0
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;going right?
		beq	lobstergr
		cmp.w	#2,d1		;going left?
		beq	lobstergl
		cmp.w	#3,d1
		beq	lobsterfall	;falling?
		cmp.w	#5,d1		;dead?
		beq	lobsterdead
		cmp.w	#4,d1
		beq	lobsterhit
		cmp.w	#6,d1
		beq	lobsterhit2	;hit but not moving
		cmp.w	#8,d1
		beq	lobsterrehit
		cmp.w	#7,d1
		beq	lobsterturn

		rts


;*** lobster going right ***

lobstergr:	lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		lea	nmeaggr(pc),a5
		cmp.w	#1,(a5,d0)	;red +hit
		beq.L	lgraggr1
		cmp.w	#2,(a5,d0)	;blue not hit
		beq.L	lgraggr2
		cmp.w	#3,(a5,d0)	;red +hit
		beq.L	lgraggr3


;** red lobster not hit aggr=0 **

		add.w	#6,(a3,d0)	;xpos:+6 (speed=6)
		move.l	gfx3,d3
		add.l	#40,d3
		cmp.l	(a0,d7),d3	;bobadr<40
		bgt.s	lgrnew
		move.l	gfx3,d3
		add.l	#44,d3
		cmp.l	(a0,d7),d3	;bobadr>=44
		ble.s	lgrnew
		cmp.w	#5,(a2,d0)
		beq.s	lgrtfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
lgrtfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgrnew:		cmp.w	#5,(a2,d0)
		beq.s	lgrtfnff
		add.w	#1,(a2,d0)
		rts
lgrtfnff:	move.l	gfx3,(a0,d7)
		add.l	#40,(a0,d7)  ;time for new 1. frame
		move.l	mask3,(a1,d7)
		add.l	#40,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** red lobster +hit **

lgraggr1:	add.w	#8,(a3,d0)	;xpos:+speed
		move.l	gfx3,d3
		add.l	#40+8,d3
		cmp.l	(a0,d7),d3
		bgt.s	lgra1new
		move.l	gfx3,d3
		add.l	#40+12,d3
		cmp.l	(a0,d7),d3
		ble.s	lgra1new
		cmp.w	#5,(a2,d0)
		beq.s	lgra1tfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
lgra1tfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgra1new:	cmp.w	#5,(a2,d0)
		beq.s	lgrta1fnff
		add.w	#1,(a2,d0)
		rts
lgrta1fnff:	move.l	gfx3,(a0,d7)
		add.l	#48,(a0,d7)  ;time for new 1. frame
		move.l	mask3,(a1,d7)
		add.l	#48,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** blue lobster not hit **

lgraggr2:	add.w	#10,(a3,d0)	;xpos:+speed
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		
		move.l	gfx3,d3
		add.l	#1800,d3
		cmp.l	(a0,d7),d3
		bgt.s	lgra2new
		move.l	gfx3,d3
		add.l	#1804,d3
		cmp.l	(a0,d7),d3
		ble.s	lgra2new
		cmp.w	#5,(a2,d0)
		beq.s	lgra2tfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
lgra2tfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgra2new:	cmp.w	#5,(a2,d0)
		beq.s	lgrta2fnff
		add.w	#1,(a2,d0)
		rts
lgrta2fnff:	move.l	gfx3,(a0,d7)
		add.l	#1800,(a0,d7)  ;time for new 1. frame
		move.l	mask3,(a1,d7)
		add.l	#1800,(a1,d7)
		move.w	#0,(a2,d0)
		rts


;** blue lobster + hit **

lgraggr3:	add.w	#12,(a3,d0)	;xpos:+speed
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		
		move.l	gfx3,d3
		add.l	#1808,d3
		cmp.l	(a0,d7),d3
		bgt.s	lgra3new
		move.l	gfx3,d3
		add.l	#1812,d3
		cmp.l	(a0,d7),d3
		ble.s	lgra3new
		cmp.w	#5,(a2,d0)
		beq.s	lgra3tfnf
		add.w	#1,(a2,d0)	;walkcnt:+1
		rts
lgra3tfnf:	add.l	#$4,(a0,d7)	;time for new frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgra3new:	cmp.w	#5,(a2,d0)
		beq.s	lgrta3fnff
		add.w	#1,(a2,d0)
		rts
lgrta3fnff:	move.l	gfx3,(a0,d7)
		add.l	#1808,(a0,d7)  ;time for new 1. frame
		move.l	mask3,(a1,d7)
		add.l	#1808,(a1,d7)
		move.w	#0,(a2,d0)
		rts



;*** lobster going left ***

lobstergl:	lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		lea	nmeaggr(pc),a5
		cmp.w	#1,(a5,d0)	;red 
		beq.L	lglaggr1
		cmp.w	#2,(a5,d0)	;blue
		beq.L	lglaggr2
		cmp.w	#3,(a5,d0)	;blue
		beq.L	lglaggr3


;** red lobster not hit **

		sub.w	#6,(a3,d0)
		move.l	gfx3,d3
		add.l	#40,d3
		cmp.l	(a0,d7),d3
		bgt.s	lglnew		;bobadr<40?
		move.l	gfx3,d3
		add.l	#44,d3
		cmp.l	(a0,d7),d3
		ble.s	lglnew		;bobadr>=44?
		cmp.w	#5,(a2,d0)
		beq.s	lgltfnf
		add.w	#1,(a2,d0)
		rts
lgltfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lglnew:		cmp.w	#5,(a2,d0)
		beq.s	lgltfnff
		add.w	#1,(a2,d0)
		rts
lgltfnff:	move.l	gfx3,(a0,d7)
		add.l	#40,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#40,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** red lobster +hit **

lglaggr1:	sub.w	#8,(a3,d0)
		move.l	gfx3,d3
		add.l	#48,d3
		cmp.l	(a0,d7),d3
		bgt.s	lgla1new
		move.l	gfx3,d3
		add.l	#52,d3
		cmp.l	(a0,d7),d3
		ble.s	lgla1new
		cmp.w	#5,(a2,d0)
		beq.s	lgla1tfnf
		add.w	#1,(a2,d0)
		rts
lgla1tfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgla1new:	cmp.w	#5,(a2,d0)
		beq.s	lgla1tfnff
		add.w	#1,(a2,d0)
		rts
lgla1tfnff:	move.l	gfx3,(a0,d7)
		add.l	#48,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#48,(a1,d7)
		move.w	#0,(a2,d0)
		rts

;** blue lobster not hit **

lglaggr2:	sub.w	#10,(a3,d0)
		move.l	gfx3,d3
		add.l	#1800,d3
		cmp.l	(a0,d7),d3
		bgt.s	lgla2new
		move.l	gfx3,d3
		add.l	#1804,d3
		cmp.l	(a0,d7),d3
		ble.s	lgla2new
		cmp.w	#5,(a2,d0)
		beq.s	lgla2tfnf
		add.w	#1,(a2,d0)
		rts
lgla2tfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgla2new:	cmp.w	#5,(a2,d0)
		beq.s	lgla2tfnff
		add.w	#1,(a2,d0)
		rts
lgla2tfnff:	move.l	gfx3,(a0,d7)
		add.l	#1800,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#1800,(a1,d7)
		move.w	#0,(a2,d0)
		rts


;** blue lobster +hit **

lglaggr3:	sub.w	#12,(a3,d0)
		move.l	gfx3,d3
		add.l	#1808,d3
		cmp.l	(a0,d7),d3
		bgt.s	lgla3new
		move.l	gfx3,d3
		add.l	#1812,d3
		cmp.l	(a0,d7),d3
		ble.s	lgla3new
		cmp.w	#5,(a2,d0)
		beq.s	lgla3tfnf
		add.w	#1,(a2,d0)
		rts
lgla3tfnf:	add.l	#$4,(a0,d7)
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
lgla3new:	cmp.w	#5,(a2,d0)
		beq.s	lgla3tfnff
		add.w	#1,(a2,d0)
		rts
lgla3tfnff:	move.l	gfx3,(a0,d7)
		add.l	#1808,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#1808,(a1,d7)
		move.w	#0,(a2,d0)
		rts



;*** lobster falling ***

lobsterfall:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexpos(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmefacing(pc),a4
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		add.w	#2,(a1,d0)	;tabcnt+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next from table
		cmp.w	#$80,d1
		beq	ljumpfin
		cmp.w	(a6,d0),d1
		bge.s	ljnod
		add.w	d1,(a3,d0)
		bra	ljcont2

ljnod:		move.w	(a6,d0),d1
		add.w	d1,(a3,d0)	;ypos:+maxdown
		bra.s	ljumpfin	;lobster has landed!
		
ljcont2:	move.w	(a5,d0),d2
		cmp.w	#1,(a4,d0)	;facing right?
		bne.s	ljex
		add.w	d2,(a2,d0)	;new xpos
		rts
ljex:		sub.w	d2,(a2,d0)
		rts

ljumpfin:	lea	nmeanimbyte(pc),a0
		move.w	#46,(a1,d0)
		cmp.w	#1,(a4,d0)
		bne.s	ljnod3
		move.w	#1,(a0,d0)	;animbyte=walk right
		rts
ljnod3:		move.w	#2,(a0,d0)
		rts


;*** lobster hit ***

lobsterhit:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexoffset(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmeaggr(pc),a4
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6


		move.w	(a2,d0),d1
		lea	nmexpos(pc),a2
		add.w	d1,(a2,d0)
		
		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next jumpoffset in d1
		cmp.w	#$80,d1
		beq.s	lhitfin
		cmp.w	(a6,d0),d1
		bgt.s	lhitjnod
		add.w	d1,(a3,d0)	;ypos:+tableoffset
		rts

lhitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

;when hitground: if lobster hit once => aggr:+1 else lobster=hit2

lhitfin:	cmp.w	#0,(a4,d0)	;aggr=0?
		beq.s	lhitfinma
		cmp.w	#2,(a4,d0)
		beq.s	lhitfinma
		lea	nmeanimbyte(pc),a0
		move.w	#6,(a0,d0)
		rts
lhitfinma:	add.w	#1,(a4,d0)	;more aggr
		add.w	#2,(a5,d0)	;more speed
		lea	nmefacing(pc),a1
		lea	nmeanimbyte(pc),a2
		cmp.w	#1,(a1,d0)
		beq.s	lhitfinnfr
		move.w	#2,(a2,d0)
		rts
lhitfinnfr:	move.w	#1,(a2,d0)
		rts



;*** lobster dead ***

lobsterdead:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmeyposition(pc),a2
		lea	nmexpos(pc),a3
		add.w	#2,(a1,d0)
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1
		add.w	d1,(a2,d0)	;new ypos
;get next anim
		lea	nmebobadr(pc),a4
		lea	nmemaskadr(pc),a5
		lea	nmeaggr(pc),a6
		cmp.w	#2,(a6,d0)	;red
		beq.L	ldeada23
		cmp.w	#3,(a5,d0)	;blue
		beq.L	ldeada23

;** red lobster not hit (and +hit)  **

		lea	nmewalkcnt(pc),a6
		move.l	gfx3,d3
		add.l	#920,d3
		cmp.l	(a4,d7),d3
		bgt.s	ldeadnew	;bobadr<920?
		move.l	gfx3,d3
		add.l	#948,d3
		cmp.l	(a4,d7),d3
		ble.s	ldeadnew	;bobadr>=948


ldeadnf:	cmp.w	#2,(a6,d0)
		bge.s	ldeadtfnf
		add.w	#1,(a6,d0)	;walkcnt:+1
		bra	ldeadmain

ldeadtfnf:	add.l	#$4,(a4,d7)	;time for next frame
		add.l	#$4,(a5,d7)
		move.w	#0,(a6,d0)	;reset walkcnt
		bra	ldeadmain

ldeadnew:	cmp.w	#2,(a6,d0)
		bge.s	ldeadff
		add.w	#1,(a6,d0)
		bra	ldeadmain

ldeadff:	move.l	gfx3,(a4,d7)
		add.l	#920,(a4,d7)  ;time for new 1. frame
		move.l	mask3,(a5,d7)
		add.l	#920,(a5,d7)
		move.w	#0,(a6,d0)	;reset walkcnt
		bra	ldeadmain




;* blue lobster not hit (and +hit)  *

ldeada23:	lea	nmewalkcnt(pc),a6
		move.l	gfx3,d3
		add.l	#1800,d3
		cmp.l	(a4,d7),d3
		bgt.s	ldeada2new
		move.l	gfx3,d3
		add.l	#1828,d3
		cmp.l	(a4,d7),d3
		ble.s	ldeada2new


ldeada2nf:	cmp.w	#2,(a6,d0)
		bge.s	ldeada2tfnf
		add.w	#1,(a6,d0)	;walkcnt:+1
		bra	ldeadmain

ldeada2tfnf:	add.l	#$4,(a4,d7)	;time for new frame
		add.l	#$4,(a5,d7)
		move.w	#0,(a6,d0)
		bra	ldeadmain

ldeada2new:	cmp.w	#2,(a6,d0)
		bge.s	ldeada2ff
		add.w	#1,(a6,d0)
		bra	ldeadmain

ldeada2ff:	move.l	gfx3,(a4,d7)
		add.l	#1800,(a4,d7)  ;time for new 1. frame
		move.l	mask3,(a5,d7)
		add.l	#1800,(a5,d7)
		move.w	#0,(a6,d0)
		bra	ldeadmain


;main
ldeadmain:	cmp.w	#180,(a2,d0)
		bgt.s	ldeadfin
		lea	nmexposition(pc),a1
		lea	nmexpos(pc),a2
		move.w	(a1,d0),d1
		cmp.w	marioxpos,d1	;fall to which side when dead
		blt.s	ldeadfl

;lobster falling left
		add.w	#8,(a2,d0)	;ny xpos
		rts
ldeadfl:	sub.w	#8,(a2,d0)
		rts

ldeadfin:	jsr	launchcoin
		rts


;*** lobster hit again ***

lobsterrehit:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		lea	nmeanimbyte(pc),a2
		add.w	#2,(a1,d0)
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1
		cmp.w	#$80,d1
		beq.s	lrehitfin
		cmp.w	(a6,d0),d1
		bgt.s	lrehitjnod
		add.w	d1,(a3,d0)
		rts
lrehitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

lrehitfin:	lea	nmeanimbyte(pc),a6
		lea	nmefacing(pc),a4
		cmp.w	#1,(a4,d0)
		beq.s	lrehitgr
		move.w	#2,(a6,d0)
		rts
lrehitgr:	move.w	#1,(a6,d0)
		rts


;*** lobster hit once/has landed. ***
;only for aggr=1/3
;lobster should move on
;animbyte = blink eyes
;inc(aggr), animbyte = walk

lobsterhit2:	lea	nmetimer1(pc),a4
		lea	nmeaggr(pc),a3
		add.w	#1,(a4,d0)
		cmp.w	#500,(a4,d0)	;wait 10 sec.
		bne.s	lhit2ok

		cmp.w	#3,(a3,d0)	;aggr=max?
		beq.s	lhit2nai
		add.w	#1,(a3,d0)	;more aggr.

lhit2nai:	move.w	#0,(a4,d0)	;timer1=0
		lea	nmeanimbyte(pc),a3
		lea	nmefacing(pc),a4
		cmp.w	#1,(a4,d0)
		beq.s	lhit2gr
		move.w	#2,(a3,d0)
		rts
lhit2gr:	move.w	#1,(a3,d0)	;walk r.
		rts
;waiting
lhit2ok:	cmp.w	#4,(a4,d0)	;timer1=4?
		blo.s	lhit2w1

		cmp.w	#400,(a4,d0)	;timer1>400?
		bge.L	lhit2wb		;yes, blink eyes

;close eyes!
lhit2wb1:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		cmp.w	#3,(a3,d0)	;aggr=blue?
		beq.s	lhit2a3

lhit2a1:	move.l	gfx3,(a0,d7)
		add.l	#60,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#60,(a1,d7)
		rts
lhit2a3:	move.l	gfx3,(a0,d7)
		add.l	#1760+60,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#1760+60,(a1,d7)
		rts

;1. frame when hit (lying down)
lhit2w1:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		cmp.w	#3,(a3,d0)	;aggr
		beq.s	lhit2w1a3

lhit2w1a1:	move.l	gfx3,(a0,d7)
		add.l	#56,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#56,(a1,d7)
		rts
lhit2w1a3:	move.l	gfx3,(a0,d7)
		add.l	#1760+56,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#1760+56,(a1,d7)
		rts


;blinking eyes (timer1>400)
lhit2wb:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1

;should eyes be closed or opened?
		cmp.w	#420,(a4,d0)
		beq.s	lhit2wb2
		cmp.w	#454,(a4,d0)
		beq.s	lhit2wb2
		cmp.w	#480,(a4,d0)
		beq.s	lhit2wb2
		cmp.w	#495,(a4,d0)
		beq.s	lhit2wb2

		cmp.w	#438,(a4,d0)
		beq.L	lhit2wb1
		cmp.w	#468,(a4,d0)
		beq.L	lhit2wb1
		cmp.w	#490,(a4,d0)
		beq.L	lhit2wb1
		rts


;open eyes!
lhit2wb2:	cmp.w	#3,(a3,d0)	;aggr
		beq.s	lhit2wba3

lhit2wba1:	move.l	gfx3,(a0,d7)
		add.l	#64,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#64,(a1,d7)
		rts
lhit2wba3:	move.l	gfx3,(a0,d7)
		add.l	#1760+64,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#1760+64,(a1,d7)
		rts



;*** lobster turning ***

lobsterturn:	lea	nmeanimbyte(pc),a0
		lea	nmefacing(pc),a1
		lea	nmetimer1(pc),a2
		add.w	#1,(a2,d0)
		cmp.w	#10,(a2,d0)
		blt.L	lturnex
		lea	nmebobadr(pc),a3
		lea	nmemaskadr(pc),a4
		lea	nmeaggr(pc),a5

		cmp.w	#1,(a1,d0)	;turn which way?
		beq.L	lturnl

lturnr:		cmp.w	#10,(a2,d0)	;time=1.frame?
		bne.L	lturnrn1f

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	lturnra21
		cmp.w	#3,(a5,d0)	;blue?
		beq.s	lturnra21


lturnra01:	move.l	gfx3,(a3,d7)
		add.l	#76,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#76,(a4,d7)
		rts

lturnra21:	move.l	gfx3,(a3,d7)
		add.l	#76+1760,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#76+1760,(a4,d7)
		rts


lturnrn1f:	cmp.w	#20,(a2,d0)	;time=2.frame?
		bne.L	lturnrn2f

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	lturnra22
		cmp.w	#3,(a5,d0)
		beq.s	lturnra22

lturna02:	move.l	gfx3,(a3,d7)
		add.l	#72,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#72,(a4,d7)
		rts

lturnra22:	move.l	gfx3,(a3,d7)
		add.l	#72+1760,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#72+1760,(a4,d7)
		rts


lturnrn2f:	cmp.w	#30,(a2,d0)	;time=3.frame?
		bne.L	lturnrn3f

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	lturnra23
		cmp.w	#3,(a5,d0)
		beq.s	lturnra23

lturna03:	move.l	gfx3,(a3,d7)
		add.l	#68,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#68,(a4,d7)
		rts

lturnra23:	move.l	gfx3,(a3,d7)
		add.l	#68+1760,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#68+1760,(a4,d7)
		rts


lturnrn3f:	cmp.w	#40,(a2,d0)	;time to end?
		beq.s	lturnrdot	;do turn
		rts

lturnrdot:	move.w	#1,(a0,d0)	;go right
		move.w	#1,(a1,d0)	;face right
		move.w	#0,(a2,d0)	;timer1=0
lturnex:	rts


;* turning left *

lturnl:		cmp.w	#10,(a2,d0)	;time=1.frame?
		bne.L	lturnln1f

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	lturnla21
		cmp.w	#3,(a5,d0)
		beq.s	lturnla21


lturnla01:	move.l	gfx3,(a3,d7)
		add.l	#68,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#68,(a4,d7)
		rts

lturnla21:	move.l	gfx3,(a3,d7)
		add.l	#68+1760,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#68+1760,(a4,d7)
		rts


lturnln1f:	cmp.w	#20,(a2,d0)	;time=2.frame?
		bne.L	lturnln2f

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	lturnla22
		cmp.w	#3,(a5,d0)
		beq.s	lturnla22

lturnla02:	move.l	gfx3,(a3,d7)
		add.l	#72,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#72,(a4,d7)
		rts

lturnla22:	move.l	gfx3,(a3,d7)
		add.l	#72+1760,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#72+1760,(a4,d7)
		rts


lturnln2f:	cmp.w	#30,(a2,d0)	;time=3.frame?
		bne.L	lturnln3f

		cmp.w	#2,(a5,d0)	;agg=2?
		beq.s	lturnla23
		cmp.w	#3,(a5,d0)
		beq.s	lturnla23

lturnla03:	move.l	gfx3,(a3,d7)
		add.l	#76,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#76,(a4,d7)
		rts

lturnla23:	move.l	gfx3,(a3,d7)
		add.l	#76+1760,(a3,d7)
		move.l	mask3,(a4,d7)
		add.l	#76+1760,(a4,d7)
		rts


lturnln3f:	cmp.w	#40,(a2,d0)	;time to end?
		beq.s	lturnldot	;do turn
		rts

lturnldot:	move.w	#2,(a0,d0)	;go left
		move.w	#0,(a1,d0)	;face left
		move.w	#0,(a2,d0)	;timer1=0
lturnlex:	rts




;----------------------------------------------------

;*
;* control fly:
;*

; animbyte:
; 1 = going right
; 2 = going left
; 3 = waiting to move on
; 4 = hit
; 5 = dead
; 6 = hit2
; 7 = turning
; 8 = rehit

;aggr=0/1


fly:		lea	nmeanimbyte(pc),a0
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;moving right?
		beq	flymr
		cmp.w	#2,d1		;moving left?
		beq	flyml
		cmp.w	#3,d1		;lying still?
		beq.L	flymwait
		cmp.w	#5,d1		;dead?
		beq	flydead
		cmp.w	#4,d1
		beq	flyhit
		cmp.w	#6,d1		;hit but not moving
		beq	flyhit2
		cmp.w	#8,d1
		beq	flyrehit
		cmp.w	#7,d1
		beq	flyturn
		rts


;*** fly moving right ***

flymr:		lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexpos(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmemaxdown(pc),a6


		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		adda.w	d1,a0
		move.w	(a0),d1

		cmp.w	#$80,d1
		beq.l	flymrfin
		bls.s	flymraddy

		move.w	#0,d2		;convert neg. to pos.
		sub.w	d1,d2
		sub.w	d2,(a3,d0)
		bra	flymraddx

flymraddy:	cmp.w	(a6,d0),d1
		bls.s	flymrnod	;maxdown>tableentry?
		move.w	(a6,d0),d1
		add.w	d1,(a3,d0)	;ypos:+maxdown
		bra	flymrfin	;touch down!

flymrnod:	add.w	d1,(a3,d0)	;new ypos
flymraddx:	lea	nmeaggr(pc),a5
		cmp.w	#1,(a5,d0)
		beq.s	flymraggr1
		add.w	#6,(a2,d0)	;new xpos aggr=0
		bra	flymrcgfx
flymraggr1:	add.w	#8,(a2,d0)	;new xpos aggr=1
flymrexit:	bra	flymrcgfx

flymrfin:	lea	nmeanimbyte(pc),a2
		move.w	#3,(a2,d0)	;wait!
		rts


flymrcgfx:	lea	nmewalkcnt(pc),a2
		add.w	#1,(a2,d0)
		cmp.w	#3,(a2,d0)
		bge.s	flymrtfnf	;walkcnt>=3?
		rts

flymrtfnf:	move.w	#0,(a2,d0)	;reset walkcnt
		lea	nmebobadr(pc),a4
		lea	nmemaskadr(pc),a5
		move.l	gfx3,d5
		add.l	#5320,d5
		cmp.l	(a4,d7),d5	;frame=1.?
		beq.s	flymrtfnsf	;yes, insert 2.

flymrtfnff:	move.l	gfx3,d5
		add.l	#5320,d5
		move.l	d5,(a4,d7)
		move.l	mask3,d5
		add.l	#5320,d5
		move.l	d5,(a5,d7)
		rts

flymrtfnsf:	move.l	gfx3,d5
		add.l	#5324,d5
		move.l	d5,(a4,d7)
		move.l	mask3,d5
		add.l	#5324,d5
		move.l	d5,(a5,d7)
		rts



;*** fly moving left ***

flyml:		lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexpos(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmemaxdown(pc),a6

		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		adda.w	d1,a0
		move.w	(a0),d1

		cmp.w	#$80,d1
		beq.l	flymlfin
		bls.s	flymladdy

		move.w	#0,d2		;convert neg. to pos.
		sub.w	d1,d2
		sub.w	d2,(a3,d0)
		bra	flymladdx

flymladdy:	cmp.w	(a6,d0),d1
		bls.s	flymlnod	;maxdown>tableentry?
		move.w	(a6,d0),d1
		add.w	d1,(a3,d0)	;ypos:+maxdown
		bra	flymlfin	;touch down!

flymlnod:	add.w	d1,(a3,d0)	;new ypos
flymladdx:	lea	nmeaggr(pc),a5
		cmp.w	#1,(a5,d0)
		beq.s	flymlaggr1
		sub.w	#6,(a2,d0)	;new xpos aggr=0
		bra	flymlcgfx
flymlaggr1:	sub.w	#8,(a2,d0)	;new xpos aggr=1
flymlexit:	bra	flymlcgfx


flymlfin:	lea	nmeanimbyte(pc),a2
		move.w	#3,(a2,d0)	;wait!
		rts

;calc. gfx
flymlcgfx:	lea	nmewalkcnt(pc),a2
		add.w	#1,(a2,d0)
		cmp.w	#3,(a2,d0)
		bge.s	flymltfnf
		rts

flymltfnf:	move.w	#0,(a2,d0)	;reset walkcnt
		lea	nmebobadr(pc),a4
		lea	nmemaskadr(pc),a5
		move.l	gfx3,d5
		add.l	#5328,d5
		cmp.l	(a4,d7),d5	;frame=1.?
		beq.s	flymltfnsf	;yes, insert 2.

flymltfnff:	move.l	gfx3,d5
		add.l	#5328,d5
		move.l	d5,(a4,d7)
		move.l	mask3,d5
		add.l	#5328,d5
		move.l	d5,(a5,d7)
		rts

flymltfnsf:	move.l	gfx3,d5
		add.l	#5324,d5
		move.l	d5,(a4,d7)
		move.l	mask3,d5
		add.l	#5324,d5
		move.l	d5,(a5,d7)
		rts





;*** fly waiting to move on ***

flymwait:	lea	nmetimer1(pc),a0
		lea	nmefacing(pc),a1
		lea	nmetabcnt(pc),a2
		lea	nmeanimbyte(pc),a3
		lea	nmebobadr(pc),a4
		lea	nmemaskadr(pc),a5

		add.w	#1,(a0,d0)
		cmp.w	#50,(a0,d0)	;time to move on?
		beq.s	flymwaitmo
;calc. gfx
		cmp.w	#4,(a0,d0)	;timer1=4?
		bgt.s	flywaitnfa
		move.l	gfx3,d5
		add.l	#5336,d5
		move.l	d5,(a4,d7)
		move.l	mask3,d5
		add.l	#5336,d5
		move.l	d5,(a5,d7)
		rts

flywaitnfa:	move.l	gfx3,d5
		add.l	#5340,d5
		move.l	d5,(a4,d7)
		move.l	mask3,d5
		add.l	#5340,d5
		move.l	d5,(a5,d7)
		rts

flymwaitmo:	move.w	#20,(a2,d0)	;tabcnt=20
		move.w	#0,(a0,d0)	;reset timer1
;move which way?
		cmp.w	#2,(a1,d0)	;face left?
		beq.s	flymwml
		move.w	#1,(a3,d0)	;move right
		rts
flymwml:	move.w	#2,(a3,d0)	;move left
		rts



;*** fly turning ***

flyturn:	lea	nmefacing(pc),a0
		lea	nmeanimbyte(pc),a1

		cmp.w	#2,(a0,d0)	;facing left?
		beq.s	flyturnl

		move.w	#2,(a1,d0)
		move.w	#2,(a0,d0)
		rts

flyturnl:	move.w	#1,(a1,d0)
		move.w	#1,(a0,d0)	;face right
		rts





;** fly hit **

flyhit:		lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexoffset(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6

		move.w	(a2,d0),d1
		lea	nmexpos(pc),a2
		add.w	d1,(a2,d0)
		
		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next jumpoffset in d1
		cmp.w	#$80,d1
		beq.s	fhitfin
		cmp.w	(a6,d0),d1
		bgt.s	fhitjnod
		add.w	d1,(a3,d0)	;ypos:+tableoffset
		rts

fhitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

fhitfin:	lea	nmeanimbyte(pc),a0
		move.w	#6,(a0,d0)
		rts


;*** fly dead ***

flydead:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmeyposition(pc),a2
		lea	nmexpos(pc),a3

		add.w	#2,(a1,d0)
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1
		add.w	d1,(a2,d0)	;new ypos
;get next anim
		lea	nmebobadr(pc),a4
		lea	nmemaskadr(pc),a5
		lea	nmewalkcnt(pc),a6
		move.l	gfx3,d2
		add.l	#6200,d2
		cmp.l	(a4,d7),d2
		bgt.s	fdeadnew
		move.l	gfx3,d2
		add.l	#6228,d2
		cmp.l	(a4,d7),d2	;bobadr>6228?
		blt.s	fdeadnew


fdeadnf:	cmp.w	#2,(a6,d0)
		bge.s	fdeadtfnf
		add.w	#1,(a6,d0)	;walkcnt:+1
		bra	fdeadmain

fdeadtfnf:	add.l	#$4,(a4,d7)	;time for new frame
		add.l	#$4,(a5,d7)
		move.w	#0,(a6,d0)
		bra	fdeadmain

fdeadnew:	cmp.w	#2,(a6,d0)	;walkcnt>=2?
		bge.s	fdeadff
		add.w	#1,(a6,d0)
		bra	fdeadmain

fdeadff:	move.l	gfx3,(a4,d7)
		add.l	#6200,(a4,d7)  ;time for new 1. frame
		move.l	mask3,(a5,d7)
		add.l	#6200,(a5,d7)
		move.w	#0,(a6,d0)
		bra	fdeadmain


fdeadmain:	cmp.w	#168,(a2,d0)
		bgt.s	fdeadfin
		lea	nmexposition(pc),a1
		lea	nmexpos(pc),a2
		move.w	(a1,d0),d1
		cmp.w	marioxpos,d1
		blt.s	fdeadfl

;fly falling right
		add.w	#8,(a2,d0)	;ny xpos
		rts
fdeadfl:	sub.w	#8,(a2,d0)
		rts

;launch coin
fdeadfin:	jsr	launchcoin	;d0 = offset for nme		
		rts



;*** fly hit again ***

flyrehit:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		lea	nmeanimbyte(pc),a2

		add.w	#2,(a1,d0)
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1
		cmp.w	#$80,d1
		beq.s	frehitfin
		cmp.w	(a6,d0),d1
		bgt.s	frehitjnod
		add.w	d1,(a3,d0)
		rts
frehitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

frehitfin:	lea	nmefacing(pc),a4
		cmp.w	#1,(a4,d0)
		beq.s	frehitgr
		move.w	#2,(a2,d0)
		rts
frehitgr:	move.w	#1,(a2,d0)
		rts


;*** fly hit once/has landed. ***

;fly should move on
;anim = wave antennas
;inc(aggr), animbyte = flying

flyhit2:	lea	nmetimer1(pc),a4
		lea	nmeaggr(pc),a3

		add.w	#1,(a4,d0)
		cmp.w	#500,(a4,d0)	;wait 10 sec.
		bge.s	fhit2f
		bra	fhit2ok

;fin. waiting
fhit2f:		lea	nmetimer2(pc),a5
		add.w	#1,(a5,d0)
		cmp.w	#10,(a5,d0)	;time for new frame?
		beq.s	fhit2tfnf
		rts
fhit2tfnf:	move.w	#0,(a5,d0)	;timer2=0
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		move.l	gfx3,d2
		add.l	#5332,d2
		cmp.l	(a0,d7),d2	;fin?
		beq.s	fhit2nai
		sub.l	#4,(a0,d7)	;next frame
		sub.l	#4,(a1,d7)	;next mask
		rts


;start flying again
fhit2nai:	move.w	#0,(a4,d0)	;timer1=0
		move.w	#0,(a5,d0)	;timer2=0
		cmp.w	#1,(a3,d0)	;aggr?
		beq.s	fhit2naic
		add.w	#1,(a3,d0)
fhit2naic:	lea	nmeanimbyte(pc),a3
		lea	nmefacing(pc),a4
		cmp.w	#1,(a4,d0)
		beq.s	fhit2gr
		move.w	#2,(a3,d0)
		rts
fhit2gr:	move.w	#1,(a3,d0)	;walk r.
		rts


;sit down (anim)
;waiting to get up
fhit2ok:	cmp.w	#4,(a4,d0)	;timer1=4?
		blo.s	fhit2w1

		cmp.w	#400,(a4,d0)	;timer1>400?
		bge.L	fhit2wb		;yes, wave antennas

;move antennas apart
fhit2wb1:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1

		move.l	gfx3,(a0,d7)
		add.l	#5320+20,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#5320+20,(a1,d7)
		rts

;1. frame when hit (lying down)
fhit2w1:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1

fhit2w1a1:	move.l	gfx3,(a0,d7)
		add.l	#5320+16,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#5320+16,(a1,d7)
		rts

;waving antennas (timer1>400)
fhit2wb:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1

;should antennas be together or apart?
		cmp.w	#420,(a4,d0)
		beq.s	fhit2wb2
		cmp.w	#454,(a4,d0)
		beq.s	fhit2wb2
		cmp.w	#480,(a4,d0)
		beq.s	fhit2wb2
		cmp.w	#495,(a4,d0)
		beq.s	fhit2wb2

		cmp.w	#438,(a4,d0)
		beq.L	fhit2wb1
		cmp.w	#468,(a4,d0)
		beq.L	fhit2wb1
		cmp.w	#490,(a4,d0)
		beq.L	fhit2wb1
		rts


;move antennas together!
fhit2wb2:	move.l	gfx3,(a0,d7)
		add.l	#5320+24,(a0,d7)
		move.l	mask3,(a1,d7)
		add.l	#5320+24,(a1,d7)
		rts



;----------------------------------------------------

;*
;* control iceman:
;*

;* animbyte:
;* 1 = moving right
;* 2 = moving left
;* 3 = falling
;* 4 = hit
;* 7 = turning
;*
;* note: ice will reappear
;* after 7 secs. when hit.
;*

iceman:		lea	nmeanimbyte(pc),a0
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;moving right?
		beq	icemr
		cmp.w	#2,d1		;moving left?
		beq	iceml
		cmp.w	#3,d1		;falling?
		beq	icefall
		cmp.w	#4,d1		;hit?
		beq	icehit
		cmp.w	#7,d1		;turning?
		beq	iceturn
		rts

;** ice moving right **

icemr:		lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2

		add.w	#6,(a3,d0)	;xpos:+8 (speed=8)
		bra	icemcalcgfx


;** ice moving left **

iceml:		lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2

		sub.w	#6,(a3,d0)	;new xpos

icemcalcgfx:	move.l	gfx2,d2
		add.l	#7960,d2
		cmp.l	(a0,d7),d2
		bgt.s	imlnew		;bobadr<7960
		move.l	gfx2,d2
		add.l	#7964,d2
		cmp.l	(a0,d7),d2
		ble.s	imlnew		;bobadr>=7964
		cmp.w	#5,(a2,d0)	;time for new frame
		beq.s	imltfnf
		add.w	#1,(a2,d0)
		rts
imltfnf:	add.l	#$4,(a0,d7)	;next frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
imlnew:		cmp.w	#5,(a2,d0)
		beq.s	imltfnff
		add.w	#1,(a2,d0)
		rts
imltfnff:	move.l	gfx2,(a0,d7)
		add.l	#7960,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#7960,(a1,d7)
		move.w	#0,(a2,d0)
		rts


;** ice falling **

icefall:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexpos(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmefacing(pc),a4
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		add.w	#2,(a1,d0)	;tabcnt+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next from table
		cmp.w	#$80,d1
		beq	ijumpfin
		cmp.w	(a6,d0),d1
		bge.s	ijnod
		add.w	d1,(a3,d0)
		bra	ijcont2

ijnod:		move.w	(a6,d0),d1
		add.w	d1,(a3,d0)	;ypos:+maxdown
		bra.s	ijumpfin	;ice has landed!
		
ijcont2:	move.w	(a5,d0),d2
		cmp.w	#1,(a4,d0)	;facing right?
		bne.s	ijex
		add.w	d2,(a2,d0)
		rts
ijex:		sub.w	d2,(a2,d0)
		rts

ijumpfin:	lea	nmeanimbyte(pc),a0
		move.w	#46,(a1,d0)
		cmp.w	#1,(a4,d0)
		bne.s	ijnod3
		move.w	#1,(a0,d0)	;animbyte=move right
		rts
ijnod3:		move.w	#2,(a0,d0)
		rts


;** ice hit **

icehit:		lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexoffset(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6

		move.w	(a2,d0),d1
		lea	nmexpos(pc),a2
		add.w	d1,(a2,d0)
		
		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next jumpoffset in d1

;has ice reached point of disactivation?
		cmp.w	#1,d1		;top of hit?
		beq.s	ihitfin
		cmp.w	(a6,d0),d1
		bgt.s	ihitjnod
		add.w	d1,(a3,d0)	;ypos:+tableoffset
		bra	ihitgfx

ihitjnod:	move.w	(a6,d0),d1
		add.w	d1,(a3,d0)

ihitfin:	jsr	launchcoin
;start timer to reappear
		cmp.w	#0,icetimer
		beq.s	ihitfint1
;* use timer 2 *
		move.w	#350,icetimer2
		rts
;* use timer 1 *
ihitfint1:	move.w	#350,icetimer	;come back after 7 secs.
		rts

;calc. gfx
ihitgfx:	lea	nmehitcnt(pc),a6
		add.w	#1,(a6,d0)
		cmp.w	#4,(a6,d0)	;time for new frame?
		bne.s	ihitgfxex
		move.w	#0,(a6,d0)
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		move.l	gfx2,d1
		add.l	#7968,d1
		cmp.l	(a0,d7),d1
		ble.s	ihitgfxnext	;bobadr>7968
		move.l	d1,(a0,d7)	;1. frame
		move.l	mask2,d1
		add.l	#7968,d1
		move.l	d1,(a1,d7)
		rts
ihitgfxnext:	add.l	#4,(a0,d7)
		add.l	#4,(a1,d7)
ihitgfxex:	rts



;*** ice turning ***

iceturn:	lea	nmeanimbyte(pc),a0
		lea	nmefacing(pc),a1
		lea	nmewalkcnt(pc),a2

;wait a moment
		cmp.w	#15,(a2,d0)
		beq.s	icedoturn
		add.w	#1,(a2,d0)
		rts

icedoturn:	move.w	#0,(a2,d0)
		cmp.w	#1,(a1,d0)	;facing?
		beq.s	iturnl

;*** ice turning right ***

iturnr:		move.w	#1,(a0,d0)	;go right
		move.w	#1,(a1,d0)	;face right
		rts


;*** ice turning left ***

iturnl:		move.w	#2,(a0,d0)	;do turn
		move.w	#2,(a1,d0)
		rts


;----------------------------------------------------

;*
;* control coins:
;*
;* animbyte:
;* 1 = moving right
;* 2 = moving left
;* 3 = falling
;* 4 = hit
;* 5 = dead
;* 7 = turning
;* 9 = stationary	;in bonus level
;*

coin:		lea	nmeanimbyte(pc),a0
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;moving right?
		beq	coinmr
		cmp.w	#2,d1		;moving left?
		beq	coinml
		cmp.w	#3,d1		;falling?
		beq	coinfall
		cmp.w	#4,d1		;hit?
		beq	coinhit
		cmp.w	#5,d1		;dead?
		beq	coindead
		cmp.w	#7,d1		;turning?
		beq	cointurn
		cmp.w	#9,d1		;stationary?
		beq	coinstat
		rts

;** coin moving right **

coinmr:		lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2

		add.w	#6,(a3,d0)	;xpos:+6 (speed=6)
		bra	coinmcalcgfx


;** coin moving left **

coinml:		lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2

		sub.w	#6,(a3,d0)	;new xpos

coinmcalcgfx:	move.l	gfx1,d2
		add.l	#5320,d2
		cmp.l	(a0,d7),d2
		bgt.s	cmlnew		;bobadr<5320
		move.l	gfx1,d2
		add.l	#5320+16,d2
		cmp.l	(a0,d7),d2
		ble.s	cmlnew		;bobadr>=5336
		cmp.w	#5,(a2,d0)	;time for new frame
		beq.s	cmltfnf
		add.w	#1,(a2,d0)
		rts
cmltfnf:	add.l	#$4,(a0,d7)	;next frame
		add.l	#$4,(a1,d7)
		move.w	#0,(a2,d0)
		rts
cmlnew:		cmp.w	#5,(a2,d0)
		beq.s	cmltfnff
		add.w	#1,(a2,d0)
		rts
cmltfnff:	move.l	gfx1,(a0,d7)
		add.l	#5320,(a0,d7)
		move.l	mask1,(a1,d7)
		add.l	#5320,(a1,d7)
		move.w	#0,(a2,d0)
		rts


;** coin falling **

coinfall:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexpos(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmefacing(pc),a4
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6
		add.w	#2,(a1,d0)	;tabcnt+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next from table
		cmp.w	#$80,d1
		beq	cjumpfin
		cmp.w	(a6,d0),d1
		bge.s	cjnod
		add.w	d1,(a3,d0)
		bra	cjcont2

cjnod:		move.w	(a6,d0),d1
		add.w	d1,(a3,d0)	;ypos:+maxdown
		bra.s	cjumpfin	;coin has landed!
		
cjcont2:	move.w	(a5,d0),d2
		cmp.w	#1,(a4,d0)	;facing right?
		bne.s	cjex
		add.w	d2,(a2,d0)
		rts
cjex:		sub.w	d2,(a2,d0)
		rts

cjumpfin:	lea	nmeanimbyte(pc),a0
		move.w	#46,(a1,d0)
		cmp.w	#1,(a4,d0)
		bne.s	cjnod3
		move.w	#1,(a0,d0)	;animbyte=move right
		rts
cjnod3:		move.w	#2,(a0,d0)
		rts



;** coin hit **

coinhit:	lea	nmeanimbyte(pc),a0
		move.w	#5,(a0,d0)		;coin=dead
		rts


;** coin dead **

coindead:	lea	jumptable(pc),a0
		lea	nmetabcnt(pc),a1
		lea	nmexoffset(pc),a2
		lea	nmeyposition(pc),a3
		lea	nmespeed(pc),a5
		lea	nmemaxdown(pc),a6

		cmp.w	#1,bonusflag	;bonusround?
		beq.s	chitgfx

		move.w	(a2,d0),d1
		lea	nmexpos(pc),a2
		add.w	d1,(a2,d0)
		
		add.w	#2,(a1,d0)	;tabcnt:+2
		move.w	(a1,d0),d1
		add.w	d1,a0
		move.w	(a0),d1		;next jumpoffset in d1
		add.w	d1,(a3,d0)	;new ypos


;calc. gfx
chitgfx:	lea	nmehitcnt(pc),a6
		add.w	#1,(a6,d0)
		cmp.w	#4,(a6,d0)	;time for new frame?
		bne.s	chitgfxex
		move.w	#0,(a6,d0)
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		move.l	gfx1,d1
		add.l	#5356,d1
		cmp.l	(a0,d7),d1	;end of anims?
		beq.s	chitfin

		move.l	gfx1,d1
		add.l	#5340,d1
		cmp.l	(a0,d7),d1
		ble.s	chitgfxnext	;bobadr>5340
		move.l	d1,(a0,d7)	;1. frame
		move.l	mask1,d1
		add.l	#5340,d1
		move.l	d1,(a1,d7)
		rts
chitgfxnext:	add.l	#4,(a0,d7)
		add.l	#4,(a1,d7)
chitgfxex:	rts

chitfin:	lea	nmeactive(pc),a0
		move.w	#2,(a0,d0)	;disactivate
		rts



;*** coin turning ***

cointurn:	lea	nmeanimbyte(pc),a0
		lea	nmefacing(pc),a1

		cmp.w	#1,(a1,d0)	;facing?
		beq.s	cturnl

;*** coin turning right ***

cturnr:		move.w	#1,(a0,d0)	;go right
		move.w	#1,(a1,d0)	;face right
		rts


;*** coin turning left ***

cturnl:		move.w	#2,(a0,d0)	;do turn
		move.w	#2,(a1,d0)
		rts


;*** coin stationary (bonus levels) ***

coinstat:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmewalkcnt(pc),a2
		bra	coinmcalcgfx	;calc. gfx frame



;----------------------------------------------------

;*
;* control fireball:
;*
;* animbyte:
;* 1 = moving right
;* 2 = moving left
;* 4 = hit
;* 5 = dead 
;*

fire:		lea	nmeanimbyte(pc),a0
		move.w	(a0,d0),d1
		cmp.w	#1,d1		;moving right?
		beq	firemr
		cmp.w	#2,d1		;moving left?
		beq	fireml
		cmp.w	#4,d1		;hit?
		beq	firehit
		cmp.w	#5,d1		;dead?
		beq	firedead
		rts

;** fireball moving right **

firemr:		lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmetimer1(pc),a4
		lea	nmewalkcnt(pc),a5
		lea	nmeyposition(pc),a6

		add.w	#1,(a4,d0)	;timer1:+1
		cmp.w	#100,(a4,d0)
		bgt.s	firemover	;timer>2 secs?
;launching!
fireclgfx:	move.l	gfx1,d1
		add.l	#6212,d1	;first frame
		cmp.l	(a0,d7),d1
		bgt.s	firemrlf1	;bobadr<6212
		add.l	#12,d1
		cmp.l	(a0,d7),d1
		ble.s	firemrlf1	;bobadr>=6224

		add.w	#1,(a5,d0)	;walkcnt:+1
		cmp.w	#5,(a5,d0)
		beq.s	firetfnf
		rts
firetfnf:	move.w	#0,(a5,d0)
		add.l	#$4,(a0,d7)	;next frame
		add.l	#$4,(a1,d7)
		rts

;first frame when launching
firemrlf1:	move.l	gfx1,(a0,d7)
		add.l	#6212,(a0,d7)
		move.l	mask1,(a1,d7)
		add.l	#6212,(a1,d7)
		rts


firemover:	add.w	#8,(a3,d0)	;xpos:+8 (speed=8)
;get yoffset
fireyoff:	lea	nmetabcnt(pc),a3
		lea	firetable(pc),a2
		add.w	#2,(a3,d0)
		move.w	(a3,d0),d1	;get value from table
		add.w	d1,a2
		move.w	(a2),d1
		cmp.w	#$80,d1
		beq.s	firemrfin
		bls.s	firemraddy
		move.w	#0,d2
		sub.w	d1,d2
		sub.w	d2,(a6,d0)
		bra	firecmgfx
firemraddy:	add.w	d1,(a6,d0)	;new ypos
		bra	firecmgfx
firemrfin:	move.w	#-2,(a3,d0)

firecmgfx:	move.l	gfx1,d1
		add.l	#6228,d1	;first frame
		cmp.l	(a0,d7),d1
		bgt.s	firemrmf1	;bobadr<6228
		add.l	#8,d1
		cmp.l	(a0,d7),d1		
		ble.s	firemrmf1

		add.w	#1,(a5,d0)	;walkcnt:+1
		cmp.w	#5,(a5,d0)
		beq.s	firemtfnf
		rts
firemtfnf:	move.w	#0,(a5,d0)
		add.l	#$4,(a0,d7)	;next frame
		add.l	#$4,(a1,d7)
		rts

;first frame when moving
firemrmf1:	move.l	gfx1,(a0,d7)
		add.l	#6228,(a0,d7)
		move.l	mask1,(a1,d7)
		add.l	#6228,(a1,d7)
		rts



;** fireball moving left **

fireml:		lea	nmexpos(pc),a3
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		lea	nmetimer1(pc),a4
		lea	nmewalkcnt(pc),a5
		lea	nmeyposition(pc),a6

		add.w	#1,(a4,d0)	;timer1:+1
		cmp.w	#100,(a4,d0)
		bgt.s	firemovel	;timer>2 secs?
		bra	fireclgfx

firemovel:	sub.w	#8,(a3,d0)	;new xpos
		bra	fireyoff



;** fire hit **

firehit:	lea	nmeanimbyte(pc),a0
		move.w	#5,(a0,d0)		;fire:=dead
		rts


;** fire dead **

firedead:

;calc. gfx
fideadgfx:	lea	nmehitcnt(pc),a6
		add.w	#1,(a6,d0)
		cmp.w	#4,(a6,d0)	;time for new frame?
		bls.s	fideadgfxex
		move.w	#0,(a6,d0)
		lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		move.l	gfx1,d1
		add.l	#6212,d1
		cmp.l	(a0,d7),d1	;end of anims?
		beq.s	fideadfin
		bgt.s	fideadgfxnext	;bobadr<6212

		move.l	gfx1,d1
		add.l	#6200,d1
		move.l	d1,(a0,d7)	;1. frame
		move.l	mask1,d1
		add.l	#6200,d1
		move.l	d1,(a1,d7)
		rts
fideadgfxnext:	add.l	#4,(a0,d7)
		add.l	#4,(a1,d7)
fideadgfxex:	rts

fideadfin:	lea	nmeactive(pc),a0
		move.w	#2,(a0,d0)	;disactivate
		sub.w	#1,firelaunched	;1 fireball less
		rts


;* yoffset when moving l/r *
firetable:	dc.w	1,1,2,1,1,0,-1,-1,-2,-1,-1,0
		dc.w	1,1,2,1,1,0,-1,-1,-2,-1,-1,0
		dc.w	1,1,2,2,2,1,1,0,0
		dc.w	-1,-1,-2,-2,-2,-1,-1,0,0
		dc.w	$80



;-----------------------------------------------------


;*
;* launch coin when nme dead:
;* in: d0,d7 = offset for nme
;* called by deadfin
;* launchcoin uses the offset
;* of the caller to substitute
;* the nme with a coin.
;* the coin is deactivated when hit
;* by writing #2 to nmeactive
;* this should not be done in
;* deadfin!
;*

launchcoin:	lea	nmeactive(pc),a0
		lea	nmetype(pc),a1
		lea	nmeanimbyte(pc),a2

		move.w	levelpnt,d1
		move.l	levelno,a3	;adr. of current leveldata
		add.w	d1,a3
		move.w	(a3),d2		;current entry
		cmp.w	#$80,d2		;end of level?
		bne.s	lcoinok

;only 1 nme left (just killed)?
		move.l	#0,d1
		move.l	#0,d2
lcoinlp:	cmp.w	#1,(a0,d1)	;active?
		bne.s	lcoinlpn	;no, goto next
		cmp.w	#5,(a2,d1)	;dead?
		beq.s	lcoinlpn	;yes, goto next
		cmp.w	#4,(a1,d1)	;ice/coin/fire?
		bge.s	lcoinlpn	;yes, goto next
		add.w	#1,d2		;1 nme alive
lcoinlpn:	add.w	#2,d1
		cmp.w	#20,d1
		bne.s	lcoinlp

		cmp.w	#0,d2
		bne.s	lcoinok
		move.w	#2,(a0,d0)	;disactivate
		bsr	gckillcoins	;kill rest of coins+ice
		rts

;insert coin instead of nme
lcoinok:	lea	nmebobadr(pc),a0
		move.l	gfx1,d1
		add.l	#7932,d1
		move.l	d1,(a0,d7)	;bob=void
		lea	nmemaskadr(pc),a0
		move.l	mask1,d1
		add.l	#7932,d1
		move.l	d1,(a0,d7)	;mask=void
		lea	nmetype(pc),a0
		move.w	#5,(a0,d0)	;type=coin
		lea	nmetabcnt(pc),a0
		move.w	#46,(a0,d0)	;tabcnt=46
		lea	nmehitflag(pc),a0
		move.w	#1,(a0,d0)	;hitflag=1
		lea	nmeyposition(pc),a0
		move.w	#6,(a0,d0)	;ypos=6
		lea	nmetimer1(pc),a0
		move.w	#0,(a0,d0)	;timer1=0
		lea	nmespeed(pc),a0
		move.w	#6,(a0,d0)	;speed=6
		lea	nmexpos(pc),a0
;set xpos
		eor.w	#1,coinflag	;shift to opp. side
		cmp.w	#1,coinflag	;right?
		bne.s	lcoinleft
		move.w	#40,(a0,d0)	;xpos=40 (=5)
		lea	nmexposition(pc),a0
		move.w	#5,(a0,d0)
		lea	nmefacing(pc),a0
		move.w	#1,(a0,d0)
		lea	nmeanimbyte(pc),a0
		move.w	#1,(a0,d0)
		rts
lcoinleft:	move.w	#2320,(a0,d0)	;xpos=2320 (=290)
		lea	nmexposition(pc),a0
		move.w	#290,(a0,d0)
		lea	nmefacing(pc),a0
		move.w	#2,(a0,d0)
		lea	nmeanimbyte(pc),a0
		move.w	#2,(a0,d0)
		rts

coinflag:	dc.w	0

		

;---------------------------------------------------------


;*
;* correct nmex- and nmeypositions:
;*
;* calculate xposition for bobr.
;* and disable coins when entering pipes
;* and kill fireballs when reaching eos.
;* 

nmecorr:	lea	nmeyposition,a3
		lea	nmeanimbyte,a4
		lea	nmefacing,a5
		lea	nmetype,a6
		move.l	#0,d0
		move.l	#0,d7
ncloop:		bsr	nmecorrect
		bsr	nmecorpipe
		add.w	#2,d0
		add.w	#4,d7
		cmp.w	#20,d0
		bne.s	ncloop
		rts
nmecorrect:	lea	nmexpos,a0
		lea	nmexposition,a1
		lea	nmeactive,a2
		cmp.w	#1,(a2,d0)	;active?
		beq.s	nmecoract
		cmp.w	#4,(a2,d0)
		beq.s	nmecoract
		cmp.w	#5,(a2,d0)
		beq.s	nmecoract
		rts

nmecoract:	lea	nmexpos(pc),a2
		move.w	(a2,d0),d1	;use xpos to
		asr.w	#3,d1		;calc.
		move.w	d1,(a1,d0)	;xposition

;gone through the screen?
		cmp.w	#6,(a6,d0)	;fireball?
		beq.s	nmecorfire
		
		cmp.w	#299,(a1,d0)	
		blt.s	nmenocx1
		move.w	#8,(a0,d0)
		move.w	#1,(a1,d0)
		rts
nmenocx1:	move.w	(a1,d0),d2
		bpl.s	nmenocx2
		move.w	#2384,(a0,d0)
		move.w	#298,(a1,d0)
nmenocx2:	rts

;* fireball=dead if limit of screen is reached *
nmecorfire:	cmp.w	#296,(a1,d0)	;xpos
		bgt.s	nmefirecx
		cmp.w	#1,(a1,d0)
		blt.s	nmefirecx
		rts
nmefirecx:	lea	nmeanimbyte(pc),a2
		move.w	#5,(a2,d0)	;dead
		rts


;* check pipes *
nmecorpipe:	lea	nmeactive(pc),a2
		cmp.w	#1,(a2,d0)	;active?
		beq.s	nmecoract2
		rts
nmecoract2:	cmp.w	#5,(a4,d0)	;dead?
		bne.s	nmecoract3
		rts
nmecoract3:	cmp.w	#6,(a6,d0)	;fireball?
		bne.s	nmecoract4
		rts
nmecoract4:	cmp.w	#4,(a4,d0)	;hit?
		bne.s	nmecoract5
		rts
nmecoract5:	cmp.w	#6,(a4,d0)	;hit2?
		bne.s	nmecoract6
		rts
nmecoract6:	cmp.w	#8,(a4,d0)	;rehit?
		bne.s	nmecoract7
		rts
nmecoract7:	cmp.w	#167,(a3,d0)	;walking on bricks?
		beq.s	nmecbot
		cmp.w	#3,(a6,d0)	;fly?
		bne.s	nmecornf
		cmp.w	#150,(a3,d0)	;on y-level with lower pipes?
		bgt.s	nmecbot

;set ypos=0 if <0
nmecornf:	cmp.w	#0,(a3,d0)
		bge.s	nmecoryex
		move.w	#0,(a3,d0)	;set ypos=0
nmecoryex:	rts

nmecbot:	cmp.w	#3,(a1,d0)	;left pipe entered
		bge.s	nmen1cory
		lea	nmeanimbyte(pc),a4
		move.w	#1,(a4,d0)
		move.w	#1,(a5,d0)
		bra	nmecory
nmen1cory:	cmp.w	#290,(a1,d0)	;right pipe entered
		bge.s	nmen2cory
		rts
nmen2cory:	move.w	#2,(a4,d0)
		move.w	#2,(a5,d0)

;* set new ypos, keep xpos *
nmecory:	move.w	#6,(a3,d0)	;new ypos
		move.w	(a6,d0),d1
		cmp.w	#1,d1		;turtle?
		beq.s	ncortpe
		cmp.w	#2,d1		;lobster?
		beq.s	ncortpe
		cmp.w	#3,d1		;fly?
		beq.s	ncortpe
		cmp.w	#4,d1		;iceman?
		beq.s	ncortpe
		cmp.w	#5,d1		;coin?
		beq.s	ncorcpe

		rts

;* should print void when in pipe
ncortpe:	cmp.w	#3,(a1,d0)
		bge.s	ncortper
;left pipe entered
		move.w	#1,nmeleftlaunch ;signal to audio
		bra	ncortpec
ncortper:	move.w	#1,nmerightlaunch ;signal to audio

ncortpec:	lea	nmebobadr(pc),a0
		lea	nmemaskadr(pc),a1
		cmp.w	#1,(a5,d0)
		bne.s	ncorpnr
		move.l	gfx2,(a0,d7)
		add.l	#$378,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#$378,(a1,d7)
		rts

ncorpnr:	move.l	gfx2,(a0,d7)
		add.l	#40,(a0,d7)
		move.l	mask2,(a1,d7)
		add.l	#40,(a1,d7)
		rts

;* disable coin when entering pipe *

ncorcpe:	lea	nmeactive(pc),a0
		move.w	#2,(a0,d0)
		rts




;----------------------------------------------------

;*
;* collision between objects :
;*
;* mario/nme
;* luigi/nme
;* nme/nme
;* mario/luigi
;*


bobcollision:	bsr	bcmarionme
		bsr	bcluiginme
		bsr	bcnmenme
		bsr	bluigim
		rts


;*** check mario/nme collision ***

bcmarionme:	cmp.w	#1,marioactive
		beq.s	bcmnmact
		rts

bcmnmact:	lea	nmeactive(pc),a0
		lea	nmexposition(pc),a1
		lea	nmeyposition(pc),a2
		lea	nmeanimbyte(pc),a3
		lea	nmetype(pc),a4

		move.l	#0,d0
		move.l	#0,d7
bcmloop:	bsr	bcmchk
		add.l	#4,d7
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	bcmloop
		rts

;do not change a0,a1,a2,a3,a4

bcmchk:		cmp.w	#1,(a0,d0)	;active?
		beq.s	bcmact
		rts
bcmact:		cmp.w	#6,animbyte	;mario dead?
		bne.s	bcmndead
		rts
bcmndead:	cmp.w	#5,(a3,d0)	;nme dead?
		bne.s	bcmnndead
		rts
bcmnndead:	lea	nmesafe(pc),a6
		cmp.w	#1,(a6,d0)	;nme safe?
		bne.s	bcmnnsafe
		rts
bcmnnsafe:	cmp.w	#1,mariosafe	;mario safe?
		bne.s	bcmmnsafe
		rts
bcmmnsafe:	move.w	marioypos,d2
		move.w	marioxpos,d1
		sub.w	(a1,d0),d1	;d1=mariox-nmex
		cmp.w	#14,d1
		blo.s	bcmxcol
		cmp.w	#-14,d1
		bhi.s	bcmxcol
		rts
bcmxcol:	sub.w	(a2,d0),d2
		cmp.w	#28,d2		;possible jump->hit
		blo.s	bcmycol
		cmp.w	#-28,d2
		bhi.s	bcmycol		
		rts

bcmycol:	move.w	marioypos,d2
		sub.w	(a2,d0),d2
		cmp.w	#14,d2
		blo	bcmycol2	;real collision
		cmp.w	#-14,d2
		bhi	bcmycol2

;* possible jump/hit *
		
		cmp.w	#1,colhflag	;mario touching platform?
		beq.s	bcmyc1ch
		rts
bcmyc1ch:	cmp.w	#3,(a4,d0)	;fly?
		beq.s	bcmnf
		cmp.w	#3,(a3,d0)	;nme=fall?
		beq.s	bcmdex		;yes->exit(if not fly)
bcmnf:		cmp.w	#4,(a3,d0)	;nme already hit?
		beq.s	bcmdex
		cmp.w	#8,(a3,d0)	;nme rehit?
		beq.s	bcmdex

bcmposjh:	move.w	marioypos,d1
		cmp.w	(a2,d0),d1
		blt.s	bcmdex		;nmey<marioy->exit


;*** type of enemy? (for hit only) ***

		lea	nmeaggr(pc),a6
		cmp.w	#1,(a4,d0)	;turtle?
		beq	bcmtuhit
		cmp.w	#2,(a4,d0)	;lobster?
		beq	bcmlobhit
		cmp.w	#3,(a4,d0)	;fly?
		beq	bcmflyhit
		cmp.w	#4,(a4,d0)	;iceman?
		beq	bcmicehit
		cmp.w	#5,(a4,d0)	;coin?
		beq	bcmcoinhit
		cmp.w	#6,(a4,d0)
		beq	bcmfirehit	;fire?

bcmdex:		rts


;* turtle hit *
bcmtuhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcmtuhita
		rts

bcmtuhita:	move.w	#0,(a5,d0)	;no more hits this jump
		cmp.w	#6,(a3,d0)
		beq.s	bcmturehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;turtle=hit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	marioxpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bcmthitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bcmthitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

bcmturehit:	move.w	#8,(a3,d0)	;turtle=rehit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts

;* lobster hit *
bcmlobhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcmlobhita
		rts

bcmlobhita:	move.w	#0,(a5,d0)	;no more hits this jump
		cmp.w	#6,(a3,d0)
		beq.s	bcmlobrehit	;nme=hit? (lying still)

		move.w	#4,(a3,d0)	;lobster hit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	marioxpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bcmlhitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bcmlhitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

bcmlobrehit:	move.w	#8,(a3,d0)	;lobster=rehit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts

;* fly hit *
bcmflyhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcmflyhita
		rts

bcmflyhita:	move.w	#0,(a5,d0)	;no more hits this jump
		cmp.w	#6,(a3,d0)
		beq.s	bcmflyrehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;fly=hit
		lea	nmetabcnt(pc),a6
		move.w	#20,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	marioxpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bcmfhitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bcmfhitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

bcmflyrehit:	move.w	#8,(a3,d0)	;fly=rehit
		lea	nmetabcnt(pc),a6
		move.w	#20,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts




;* coin hit *
bcmcoinhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcmcoinhita
		rts

bcmcoinhita:	move.w	#0,(a5,d0)	;no more hits this jump
		move.w	#4,(a3,d0)	;coin=hit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt
		add.w	#8,pshunm	;800 points
		add.w	#8,marioscore

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	marioxpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bcmcoinhitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bcmcoinhitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

;* iceman hit *
bcmicehit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcmicehita
		rts

bcmicehita:	move.w	#0,(a5,d0)	;no more hits this jump
		move.w	#4,(a3,d0)	;coin=hit
		add.w	#5,pshunm	;add 500 points
		add.w	#5,marioscore
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	marioxpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bcmicehitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bcmicehitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts


;* fire hit *
bcmfirehit:	move.w	#4,(a3,d0)	;fire=hit
		rts


;** real collision **

bcmycol2:	cmp.w	#1,(a4,d0)	;turtle?
		beq	bcmyc2tu
		cmp.w	#2,(a4,d0)	;lobster?
		beq	bcmyc2lo
		cmp.w	#3,(a4,d0)	;fly?
		beq	bcmyc2fly
		cmp.w	#4,(a4,d0)	;iceman?
		beq	bcmyc2ice
		cmp.w	#5,(a4,d0)	;coin?
		beq	bcmyc2coin
		cmp.w	#6,(a4,d0)	;fireball?
		beq	bcmyc2fire
		rts

;* turtle *
bcmyc2tu:	cmp.w	#6,(a3,d0)	;turtle=hit2?
		beq.L	bcmtudie
		cmp.w	#4,(a3,d0)	;turtle=hit?
		bne.s	bcmtunhit
bcmtudie:	lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		move.w	#5,(a3,d0)	;turtle=dead
		lea	nmeaggr(pc),a6
		move.w	(a6,d0),d1	;add 800/800/800 points
		add.w	#1,d1		;depending on aggr. level
		add.w	#8,pshunm	;add d1 for diff. points
		add.w	#8,marioscore
		rts

;mario dead, turn turtle
bcmtunhit:	move.w	#7,(a3,d0)	;turn turtle
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyte	;mario=dead
		move.w	#0,deadno	;hit by turtle
		move.w	#36,tabcnt	;not 46, wait a moment
		rts

;* lobster *
bcmyc2lo:	cmp.w	#6,(a3,d0)	;lobster=hit2?
		beq.L	bcmlodie
		cmp.w	#4,(a3,d0)	;lobster=hit?
		bne.s	bcmlonhit
bcmlodie:	lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		move.w	#5,(a3,d0)	;lobster=dead
		lea	nmeaggr(pc),a6
		move.w	(a6,d0),d1	;add 800 points
		add.w	#2,d1		;depending on aggr.level
		add.w	#8,pshunm
		add.w	#8,marioscore
		rts

;mario dead, turn lobster
bcmlonhit:	move.w	#7,(a3,d0)	;turn nme
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyte	;mario=dead
		move.w	#0,deadno	;hit by lobster
		move.w	#36,tabcnt	;not 46, wait a moment
		rts

;* fly *
bcmyc2fly:	cmp.w	#6,(a3,d0)	;fly=hit2?
		beq.L	bcmflydie
		cmp.w	#4,(a3,d0)	;fly=hit?
		bne.s	bcmflynhit
bcmflydie:	lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		move.w	#5,(a3,d0)	;fly=dead
		lea	nmeaggr(pc),a6
		move.w	(a6,d0),d1	;add 800 points
		add.w	#3,d1		;depending on aggr.level
		add.w	#8,pshunm
		add.w	#8,marioscore
		rts

bcmflynhit:	move.w	#7,(a3,d0)	;turn fly
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyte	;mario=dead
		move.w	#0,deadno	;hit by fly
		move.w	#36,tabcnt	;not 46, wait a moment
		rts


;* coin *
bcmyc2coin:	move.w	#4,(a3,d0)	;coin=hit(dead)
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;tabcnt=10
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;hitcnt=0
		add.w	#8,pshunm	;add 800 points
		add.w	#8,marioscore
;calc. xoffset
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	marioxpos,d1	;nmex<mariox?
		cmp.w	(a6,d0),d1
		bls.s	bcmcoinx1
		move.w	#-8,(a5,d0)
		rts
bcmcoinx1:	move.w	#8,(a5,d0)
		rts

;* ice *
;* mario always dies, turn ice
bcmyc2ice:	move.w	#7,(a3,d0)	;turn ice
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyte	;mario=dead
		move.w	#1,deadno	;hit by iceman
		move.w	#36,tabcnt	;not 46, wait a moment
		rts


;* fire *
;* mario always dies, except for the first 3.5 secs.
bcmyc2fire:	lea	nmetimer1(pc),a5
		cmp.w	#175,(a5,d0)
		blt.s	bcmyc2fex
		move.w	#6,animbyte	;mario dead
		move.w	#2,deadno	;hit by fire
		move.w	#36,tabcnt
bcmyc2fex:	rts




;----------------------------------------------------

;*
;* check luigi/nme collision:
;*

bcluiginme:	cmp.w	#1,luigiactive
		beq.s	bclnact
		rts

bclnact:	lea	nmeactive(pc),a0
		lea	nmexposition(pc),a1
		lea	nmeyposition(pc),a2
		lea	nmeanimbyte(pc),a3
		lea	nmetype(pc),a4
		lea	nmehitflag(pc),a5

		move.l	#0,d0
		move.l	#0,d7
bclloop:	bsr	bclchk
		add.l	#4,d7
		add.w	#2,d0
		cmp.w	#20,d0
		bne.s	bclloop
		rts

bclchk:		cmp.w	#1,(a0,d0)	;active?
		beq.s	bclact
		rts
bclact:		cmp.w	#6,animbyteluigi  ;luigi dead?
		bne.s	bclndead
		rts
bclndead:	cmp.w	#5,(a3,d0)	;nme dead?
		bne.s	bclnndead
		rts
;do not change a0,a1,a2,a3,a4

bclnndead:	lea	nmesafe(pc),a6
		cmp.w	#1,(a6,d0)	;nme safe?
		bne.s	bclnnsafe
		rts
bclnnsafe:	cmp.w	#1,luigisafe	;luigi safe?
		bne.s	bcllnsafe
		rts

bcllnsafe:	move.w	luigiypos,d2
		move.w	luigixpos,d1
		sub.w	(a1,d0),d1	;d1=luigix-nmex
		cmp.w	#14,d1
		blo.s	bclxcol
		cmp.w	#-14,d1
		bhi.s	bclxcol
		rts
bclxcol:	sub.w	(a2,d0),d2
		cmp.w	#28,d2		;possible jump->hit
		blo.s	bclycol
		cmp.w	#-28,d2
		bhi.s	bclycol		
		rts

bclycol:	move.w	luigiypos,d2
		sub.w	(a2,d0),d2
		cmp.w	#14,d2
		blo	bclycol2	;real collision
		cmp.w	#-14,d2
		bhi	bclycol2

;* possible jump/hit
		cmp.w	#1,colhflagluigi	;platform hit?
		beq.s	bclyc1ch
		rts
bclyc1ch:	cmp.w	#3,(a4,d0)
		beq.s	bclytfly
		cmp.w	#3,(a3,d0)	;nme=fall(not fly)?
		beq.s	bcldex		;yes->exit
bclytfly:	cmp.w	#4,(a3,d0)	;nme already hit
		beq.s	bcldex
		cmp.w	#8,(a3,d0)
		beq.s	bcldex		;nme rehit?

bclposjh:	move.w	luigiypos,d1
		cmp.w	(a2,d0),d1
		blt.s	bcldex		;nmey<luigiy->exit

;*** type of enemy?

		cmp.w	#1,(a4,d0)	;turtle?
		beq	bcltuhit
		cmp.w	#2,(a4,d0)	;lobster?
		beq	bcllobhit
		cmp.w	#3,(a4,d0)	;fly?
		beq	bclflyhit
		cmp.w	#4,(a4,d0)	;iceman?
		beq	bclicehit
		cmp.w	#5,(a4,d0)	;coin?
		beq	bclcoinhit
		cmp.w	#6,(a4,d0)	;fire?
		beq	bclfirehit

bcldex:		rts

;* turtle hit *
bcltuhit:	cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcltuhita
		rts

bcltuhita:	move.w	#0,(a5,d0)	;no more hits this jump
		cmp.w	#6,(a3,d0)
		beq.s	bclturehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;turtle=hit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	luigixpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bclthitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bclthitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

bclturehit:	move.w	#8,(a3,d0)	;turtle=rehit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts

;* lobster hit *
bcllobhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bcllobhita
		rts

bcllobhita:	move.w	#0,(a5,d0)	;no more hits this jump
		cmp.w	#6,(a3,d0)
		beq.s	bcllobrehit	;nme=hit? (lying still)

		move.w	#4,(a3,d0)	;lobster hit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	luigixpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bcllhitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bcllhitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

bcllobrehit:	move.w	#8,(a3,d0)	;lobster=rehit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts

;* fly hit *
bclflyhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bclflyhita
		rts

bclflyhita:	move.w	#0,(a5,d0)	;no more hits this jump
		cmp.w	#6,(a3,d0)
		beq.s	bclflyrehit	;nme=hit? (lying still)
		move.w	#4,(a3,d0)	;fly=hit
		lea	nmetabcnt(pc),a6
		move.w	#20,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	luigixpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bclfhitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bclfhitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

bclflyrehit:	move.w	#8,(a3,d0)	;fly=rehit
		lea	nmetabcnt(pc),a6
		move.w	#20,(a6,d0)
		lea	nmetimer1(pc),a6
		move.w	#0,(a6,d0)
		rts


;* coin hit *
bclcoinhit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bclcoinhita
		rts

bclcoinhita:	move.w	#0,(a5,d0)	;no more hits this jump
		move.w	#4,(a3,d0)	;coin=hit
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt
		add.w	#8,pshunl	;800 points
		add.w	#8,luigiscore

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	luigixpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bclcoinhitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bclcoinhitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

;* iceman hit *
bclicehit:	lea	nmehitflag(pc),a5
		cmp.w	#0,(a5,d0)	;nme already hit in this jump?
		bne.s	bclicehita
		rts

bclicehita:	move.w	#0,(a5,d0)	;no more hits this jump
		move.w	#4,(a3,d0)	;coin=hit
		add.w	#5,pshunl	;add 500 points
		add.w	#5,luigiscore
		lea	nmetabcnt(pc),a6
		move.w	#12,(a6,d0)	;offset for jumptable
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;init. hitcnt

;calculate offset for x-dir
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	luigixpos,d1
		move.w	(a6,d0),d2
		cmp.w	d2,d1
		bls.s	bclicehitoff
		lsr.w	#1,d2
		lsr.w	#1,d1
		sub.w	d1,d2
		move.w	d2,(a5,d0)	;hit left
		rts
bclicehitoff:	sub.w	d1,d2
		lsr.w	#1,d2
		move.w	d2,(a5,d0)	;hit right
		rts

;* fire hit *

bclfirehit:	move.w	#4,(a3,d0)	;fire=hit
		rts



;** real collision **

bclycol2:	cmp.w	#1,(a4,d0)	;turtle?
		beq	bclyc2tu
		cmp.w	#2,(a4,d0)	;lobster?
		beq	bclyc2lo
		cmp.w	#3,(a4,d0)	;fly?
		beq	bclyc2fly
		cmp.w	#4,(a4,d0)	;iceman?
		beq	bclyc2ice
		cmp.w	#5,(a4,d0)	;coin?
		beq	bclyc2coin
		cmp.w	#6,(a4,d0)	;fire?
		beq.L	bclyc2fire
		rts
;* turtle *
bclyc2tu:	cmp.w	#6,(a3,d0)	;turtle=hit2?
		beq.L	bcltudie
		cmp.w	#4,(a3,d0)	;turtle=hit?
		bne.s	bcltunhit
bcltudie:	lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		move.w	#5,(a3,d0)	;turtle=dead
		lea	nmeaggr(pc),a6
		move.w	(a6,d0),d1	;add 800/800/800 points
		add.w	#1,d1		;depending on aggr.level
		add.w	#8,pshunl
		add.w	#8,luigiscore
		rts

bcltunhit:	move.w	#7,(a3,d0)	;turn turtle
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyteluigi;luigi=dead
		move.w	#0,deadnoluigi
		move.w	#36,tabcntluigi	;not 46, wait a moment
		rts

;* lobster *
bclyc2lo:	cmp.w	#6,(a3,d0)	;lobster=hit2?
		beq.L	bcllodie
		cmp.w	#4,(a3,d0)	;lobster=hit?
		bne.s	bcllonhit
bcllodie:	lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		move.w	#5,(a3,d0)	;lobster=dead
		lea	nmeaggr(pc),a6
		move.w	(a6,d0),d1	;add 800 points
		add.w	#2,d1		;depending on aggr.level
		add.w	#8,pshunl
		add.w	#8,luigiscore
		rts

bcllonhit:	move.w	#7,(a3,d0)	;turn nme
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyteluigi;luigi=dead
		move.w	#0,deadnoluigi
		move.w	#36,tabcntluigi	;not 46, wait a moment
		rts

;* fly *
bclyc2fly:	cmp.w	#6,(a3,d0)	;fly=hit2?
		beq.L	bclflydie
		cmp.w	#4,(a3,d0)	;fly=hit?
		bne.s	bclflynhit
bclflydie:	lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)
		move.w	#5,(a3,d0)	;fly=dead
		lea	nmeaggr(pc),a6
		move.w	(a6,d0),d1	;add 800 points
		add.w	#3,d1		;depending on aggr.level
		add.w	#8,pshunl
		add.w	#8,luigiscore
		rts

bclflynhit:	move.w	#7,(a3,d0)	;turn fly
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyteluigi;luigi=dead
		move.w	#0,deadnoluigi	;hit by fly
		move.w	#36,tabcntluigi	;not 46, wait a moment
		rts

;* coin *
bclyc2coin:	move.w	#4,(a3,d0)	;coin=hit(dead)
		lea	nmetabcnt(pc),a6
		move.w	#10,(a6,d0)	;tabcnt=10
		lea	nmehitcnt(pc),a6
		move.w	#0,(a6,d0)	;hitcnt=0
		add.w	#8,pshunl	;add 800 points
		add.w	#8,luigiscore
;calc. xoffset
		lea	nmexposition(pc),a6
		lea	nmexoffset(pc),a5
		move.w	luigixpos,d1	;nmex<luigix?
		cmp.w	(a6,d0),d1
		bls.s	bclcoinx1
		move.w	#-8,(a5,d0)
		rts
bclcoinx1:	move.w	#8,(a5,d0)
		rts

;* ice *
;* luigi always dies, turn ice
bclyc2ice:	move.w	#7,(a3,d0)	;turn ice
		lea	nmetimer1(pc),a5
		move.w	#0,(a5,d0)	;timer1=0
		move.w	#6,animbyteluigi;luigi=dead
		move.w	#1,deadnoluigi	;hit by ice
		move.w	#36,tabcntluigi	;not 46, wait a moment
		rts


;* fire *
;* luigi always dies, except in the first 3.5 secs.
bclyc2fire:	lea	nmetimer1(pc),a5
		cmp.w	#175,(a5,d0)
		blt.s	bclyc2fex	;timer1<175?
		move.w	#6,animbyteluigi;luigi dead
		move.w	#2,deadnoluigi	;hit by fire
		move.w	#36,tabcntluigi
bclyc2fex:	rts




;----------------------------------------------------

;*
;* nme/nme collision:
;*
;* d0 used for nmesource(nme1)
;* d6  -    -  nmedest(nme2)
;*


bcnmenme:	lea	nmeactive(pc),a0
		lea	nmexposition(pc),a1
		lea	nmeyposition(pc),a2
		lea	nmeanimbyte(pc),a3
		lea	nmetype(pc),a4

		move.l	#0,d0		;offset for nmesource
bcnloop:	bsr	bcnchk
		add.w	#2,d0
		cmp.w	#18,d0		;fin?
		bne.s	bcnloop
		rts

bcnchk:		cmp.w	#1,(a0,d0)	;active?
		beq.s	bcnact
		rts
bcnact:		cmp.w	#6,(a4,d0)	;fireball?
		bne.s	bcnnfire
		rts
bcnnfire:	move.l	d0,d6
		add.w	#2,d6		;offset for nmedest
bcnloop2:	bsr	bcnchk2
		add.w	#2,d6
		cmp.w	#20,d6		;fin?
		bne.s	bcnloop2
		rts

bcnchk2:	cmp.w	#1,(a0,d6)	;nme2 active?
		beq.s	bcnact2
		rts
bcnact2:	cmp.w	#6,(a4,d6)	;nme2 fireball?
		bne.s	bcnnfire2
		rts
;ycol?
bcnnfire2:	move.w	(a2,d0),d4	;nme1 ypos
		move.w	(a2,d6),d5	;nme2 ypos
		sub.w	d4,d5
		cmp.w	#15,d5
		blo.s	bcnycol
		cmp.w	#-15,d5
		bhi.s	bcnycol
		rts
;xcol?
bcnycol:	move.w	(a1,d0),d2	;nme1 xpos
		move.w	(a1,d6),d3	;nme2 xpos
		sub.w	d2,d3
		cmp.w	#20,d3
		blo.L	bcnxcol2	;x2-x1<20?
		cmp.w	#-20,d3
		bhi.s	bcnxcol1	;x1-x2<20?
		rts


;* nme1 to the right *

bcnxcol1:	move.w	(a3,d0),d2	;animbyte nme1
		move.w	(a3,d6),d3	;nme2
		cmp.w	#1,d2		;nme1 going right?
		beq.s	bcn1gr
		cmp.w	#2,d2		;nme1 going left?
		beq.s	bcn1gl
		cmp.w	#6,d2		;nme1 hit2?
		beq.s	bcn1hit
		cmp.w	#3,(a4,d0)	;fly?		
		bne.s	bcnxcol1nf
		cmp.w	#3,(a3,d0)	;lying still?
		beq.s	bcn1hit		;turn 2 if 2gr
bcnxcol1nf:	rts

bcn1gr:		cmp.w	#1,d3		;nme2 going right?
		bne.s	bcn1grex
		move.w	#7,(a3,d6)	;turn nme2
bcn1grex:	rts

bcn1gl:		cmp.w	#1,d3		;nme2 gr?
		bne.s	bcn1gl2ngr
		move.w	#7,(a3,d0)	;turn 1
		move.w	#7,(a3,d6)	;turn 2
		rts
bcn1gl2ngr:	cmp.w	#2,d3		;nme2 gl?
		bne.s	bcn1gl2ngl		
		move.w	#7,(a3,d0)	;turn1
		rts
bcn1gl2ngl:	cmp.w	#6,d3		;nme2 hit2?
		bne.s	bcn1gl2nh
		move.w	#7,(a3,d0)	;turn1
		rts
bcn1gl2nh:	cmp.w	#3,(a4,d6)	;fly?		
		bne.s	bcn1gl2nf
		cmp.w	#3,(a3,d6)	;lying still?
		bne.s	bcn1gl2nf
		move.w	#7,(a3,d0)	;turn1
bcn1gl2nf:	rts


bcn1hit:	cmp.w	#1,d3		;nme2 going right?
		bne.s	bcn1hitex
		move.w	#7,(a3,d6)	;turn 2
bcn1hitex:	rts



;* nme2 to the right

bcnxcol2:	move.w	(a3,d0),d2	;animbyte nme1
		move.w	(a3,d6),d3	;nme2
		cmp.w	#1,d3		;nme2 going right?
		beq.s	bcn2gr
		cmp.w	#2,d3		;nme2 going left?
		beq.s	bcn2gl
		cmp.w	#6,d3		;nme2 hit2?
		beq.s	bcn2hit
		cmp.w	#3,(a4,d6)	;fly?		
		bne.s	bcnxcol2nf
		cmp.w	#3,(a3,d6)	;lying still?
		beq.s	bcn2hit		;turn 1 if 1gr
bcnxcol2nf:	rts


bcn2gr:		cmp.w	#1,d2		;nme1 going right?
		bne.s	bcn2grex
		move.w	#7,(a3,d0)	;turn nme1
bcn2grex:	rts

bcn2gl:		cmp.w	#1,d2		;nme1 gr?
		bne.s	bcn2gl1ngr
		move.w	#7,(a3,d0)	;turn 1
		move.w	#7,(a3,d6)	;turn 2
		rts
bcn2gl1ngr:	cmp.w	#2,d2		;nme1 gl?
		bne.s	bcn2gl1ngl		
		move.w	#7,(a3,d6)	;turn 2
		rts
bcn2gl1ngl:	cmp.w	#6,d2		;nme1 hit2?
		bne.s	bcn2gl1nh
		move.w	#7,(a3,d6)	;turn2
		rts
bcn2gl1nh:	cmp.w	#3,(a4,d0)	;fly?		
		bne.s	bcn2gl1nf
		cmp.w	#3,(a3,d0)	;lying still?
		bne.s	bcn2gl1nf
		move.w	#7,(a3,d6)	;turn2
bcn2gl1nf:	rts


bcn2hit:	cmp.w	#1,d2		;nme1 going right?
		bne.s	bcn2hitex
		move.w	#7,(a3,d0)	;turn1
bcn2hitex:	rts



;----------------------------------------------------

;*
;* mario/luigi collision:
;*
;* 4 cases
;* no-one jumping
;* mario jumping
;* luigi jumping
;* both jumping (incl. one waiting after death)
;*
;* jumping equals hit
;*

bluigim:	cmp.w	#1,luigiactive
		beq.s	bluact
		rts
bluact:		cmp.w	#1,marioactive
		beq.s	bluactm
		rts
bluactm:	cmp.w	#6,animbyte	;mario dead?
		bne.s	blumndead
		rts
blumndead:	cmp.w	#6,animbyteluigi	;luigi dead?
		bne.s	blulndead
		rts

;* chk collision *

blulndead:	move.w	marioxpos,d0
		move.w	luigixpos,d1
		sub.w	d1,d0
		cmp.w	#20,d0		;deltax<20?
		blo.s	bluxcol
		cmp.w	#-20,d0
		bhi.s	bluxcol
		rts

bluxcol:	move.w	marioypos,d0
		move.w	luigiypos,d1
		cmp.w	d0,d1		;marioy<luigiy?
		bhi.s	bluxcol1

		sub.w	d1,d0		;luigiy<marioy
		cmp.w	#22,d0		;ycol
		blo.L	bluycol
		move.w	#0,mlcoll	;no coll.
		cmp.w	#30,d0		;possible hit from beneath
		blo.s	bluycolpl
		rts

bluxcol1:	sub.w	d0,d1		;marioy<luigiy
		cmp.w	#22,d1		;in-air
		blo.L	bluycol
;no collision (in-air)
		move.w	#0,mlcoll	;no collision
		cmp.w	#30,d1		;platform
		blo.s	bluycolpl
		rts


;* possible hit on platform

;* who is on top? *

bluycolpl:	move.w	marioypos,d0
		cmp.w	luigiypos,d0
		bgt.s	bluycpllt	;luigi on top=>branch


;mario on top
		cmp.w	#1,animbyte	;mario jump/falling?
		beq.s	bludex		;yes->exit
		cmp.w	#9,animbyte	;mario already hit?
		beq.s	bludex
		cmp.w	#10,animbyte	;mario waiting?
		beq.s	bludex
		bra	blumhit
bludex:		rts

blumhit:	;cmp.w	#0,hitflag	;mario already hit in this jump?
		;bne.s	blumhita
		;rts

blumhita:	;move.w	#0,hitflag	;no more hits this jump
		move.w	#9,animbyte
		move.w	#12,tabcnt

;calculate offset for x-dir
		move.w	luigixpos,d1
		move.w	marioxpos,d2
		bsr	calcxoffset
		move.w	d0,xoffset
		move.w	d1,jumpbyte
		move.w	d2,facing
		rts

;* luigi on top *

bluycpllt:	cmp.w	#1,animbyteluigi	;luigi jump/falling?
		beq.s	bluldex			;yes->exit
		cmp.w	#9,animbyteluigi	;luigi already hit?
		beq.s	bluldex
		cmp.w	#10,animbyteluigi	;luigi waiting?
		beq.s	bluldex
		bra	blulhit
bluldex:	rts

blulhit:	move.w	#9,animbyteluigi
		move.w	#12,tabcntluigi

;calculate offset for x-dir
		move.w	marioxpos,d1
		move.w	luigixpos,d2
		bsr	calcxoffset
		move.w	d0,xoffsetluigi
		move.w	d1,jumpbyteluigi
		move.w	d2,facingluigi
		rts




;*** real y-collision ***

bluycol:	cmp.w	#1,animbyte	;mario jumping?
		beq.L	blumjump
		cmp.w	#1,animbyteluigi;luigi jumping?
		beq.L	bluljump
		cmp.w	#9,animbyte	;mario hit?
		beq.l	blumjump
		cmp.w	#9,animbyteluigi;luigi hit?
		beq.l	bluljump


;*** mario and luigi not jumping ***

		move.w	marioxpos,d0
		move.w	luigixpos,d1
		cmp.w	d0,d1
		blo.L	blunjmr		;mario to the right

;**** luigi to the right ****

blunjlr:	cmp.w	#2,animbyteluigi	;luigi g.r?
		beq.s	blunjlrgr	;yes exit
		cmp.w	#3,animbyte	;mario going left?
		beq.s	blunjlrgr	;yes exit

;* what is luigi doing *

		cmp.w	#3,animbyteluigi	;luigi g.l?
		beq.s	blunjlrgl
		cmp.w	#5,animbyteluigi	;luigi skidding left?
		beq.L	blunjlrsl
		cmp.w	#4,animbyteluigi	;luigi skidding right
		beq.L	blunjlrs
		cmp.w	#0,animbyteluigi	;luigi standing?
		beq.s	blunjlrs

blunjlrgr:	rts


;*** luigi go left ***
;* what is mario doing		

blunjlrgl:	cmp.w	#2,animbyte	;run right?
		beq.s	blunjlrturn
		cmp.w	#4,animbyte	;skid right?
		beq.s	blunjlrturn
		cmp.w	#8,animbyte	;pushed left?
		beq.s	blunjlrgr	;exit
		cmp.w	#7,animbyte	;pushed right?
		beq.s	blunjlrgr	;exit

;mario standing or skidding left => start push
		move.w	#8,animbyte	;pushed left
		move.w	#0,facing
		move.w	#0,pushcnt
		rts
	
;* turn both *

blunjlrturn:	move.w	#5,animbyte		;skid left
		move.w	#4,animbyteluigi	;skid right
		move.w	#0,facing
		move.w	#1,facingluigi
		rts

;*** luigi standing ***

blunjlrs:	cmp.w	#2,animbyte	;run right
		beq.s	blunjlrssp
		cmp.w	#4,animbyte	;skid right
		beq.s	blunjlrssm
		rts

;* mario skidding into luigi *

blunjlrssm:	move.w	#-2,skidcntluigi
		move.w	#4,animbyteluigi
		move.w	#1,facingluigi
		rts

;* start push *

blunjlrssp:	move.w	#7,animbyteluigi	;luigi pushed r.
		move.w	#0,pushcnt
		rts


;*** luigi skid left ***

blunjlrsl:	cmp.w	#2,animbyte	;run r
		beq.s	blunjlrturn
		cmp.w	#4,animbyte	;skid r
		beq.s	blunjlrturn
		cmp.w	#0,animbyte	;standing
		beq.s	blunjlrsll
		rts

blunjlrsll:	move.w	#5,animbyte	;skid left
		move.w	#0,facing
		rts

blunjlrsr:	cmp.w	#2,animbyte
		beq.s	blunjlrsrmrr
		rts
blunjlrsrmrr:	move.w	#-2,skidcntluigi
		rts



;**** mario to the right ****

blunjmr:	cmp.w	#2,animbyte	;mario g.r?
		beq.s	blunjmrgr	;yes exit
		cmp.w	#3,animbyteluigi	;luigi going left?
		beq.s	blunjmrgr	;yes exit

;* what is mario doing *

		cmp.w	#3,animbyte	;mario g.l?
		beq.s	blunjmrgl
		cmp.w	#5,animbyte	;mario skidding left?
		beq.L	blunjmrsl
		cmp.w	#4,animbyte	;mario skidding right
		beq.L	blunjmrs
		cmp.w	#0,animbyte	;mario standing?
		beq.s	blunjmrs

blunjmrgr:	rts


;*** mario go left
;* what is luigi doing		

blunjmrgl:	cmp.w	#2,animbyteluigi	;run right?
		beq.s	blunjmrturn
		cmp.w	#4,animbyteluigi	;skid right?
		beq.s	blunjmrturn
		cmp.w	#7,animbyteluigi	;pushed right?
		beq.s	blunjmrgr		;exit
		cmp.w	#8,animbyteluigi	;pushed left?
		beq.s	blunjmrgr		;exit

;luigi standing or skidding left => start push
		move.w	#8,animbyteluigi	;pushed left
		move.w	#0,facingluigi
		move.w	#0,pushcnt
		rts
	
;* turn both *

blunjmrturn:	move.w	#4,animbyte		;skid right
		move.w	#5,animbyteluigi	;skid left
		move.w	#1,facing
		move.w	#0,facingluigi
		rts

;*** mario standing ***

blunjmrs:	cmp.w	#2,animbyteluigi	;run right
		beq.s	blunjmrssp
		cmp.w	#4,animbyteluigi	;skid right
		beq.s	blunjmrssm
		rts

;* luigi skidding into mario *

blunjmrssm:	move.w	#-2,skidcnt
		move.w	#4,animbyte
		move.w	#1,facing
		rts

;* start push *

blunjmrssp:	move.w	#7,animbyte	;mario pushed r.
		move.w	#0,pushcnt
		rts


;*** mario skid left ***

blunjmrsl:	cmp.w	#2,animbyteluigi	;run r
		beq.s	blunjmrturn
		cmp.w	#4,animbyteluigi	;skid r
		beq.s	blunjmrturn
		cmp.w	#0,animbyteluigi	;standing
		beq.s	blunjmrsll
		rts

blunjmrsll:	move.w	#5,animbyteluigi	;skid left
		move.w	#0,facingluigi
		rts

blunjmrsr:	cmp.w	#2,animbyteluigi
		beq.s	blunjmrsrlrr
		rts
blunjmrsrlrr:	move.w	#-2,skidcnt
		rts




;*** mario jumping ***

blumjump:	cmp.w	#1,animbyteluigi	;both jumping?
		beq.L	blumljump
		cmp.w	#10,animbyteluigi	;luigi waiting?
		beq.l	blumljump
		cmp.w	#9,animbyteluigi	;luigi hit?
		beq.l	blumljump



;**** only mario jumping ****
;* 2 cases dx>dy and v.v.

		move.w	marioxpos,d0
		move.w	luigixpos,d1
		move.w	marioypos,d2
		move.w	luigiypos,d3
		cmp.w	d0,d1
		bhi.s	blumj1
		sub.w	d1,d0		;d0>0  dx
		add.w	#2,d0		;because dx tested as +/-20
		bra	blumjxc

blumj1:		sub.w	d0,d1
		move.w	d1,d0		;d0>0				
		add.w	#2,d0		;same

blumjxc:	cmp.w	d2,d3
		bhi.s	blumj2
		sub.w	d3,d2		;d2>0  dy
		bra	blumjyc

blumj2:		sub.w	d2,d3
		move.w	d3,d2		;d2>0

blumjyc:	cmp.w	d0,d2
		blo.L	blumjx

;* deltax<deltay (hit top-bottom, luigi standing)


;mario on top => mario=start new jump

		move.w	#10,tabcnt
		move.w	#0,chflag
;calc. dir. if jumpbyte=0
		cmp.w	#0,jumpbyte
		bne.s	blumjxx
		cmp.w	#0,facing
		beq.s	blumjxo1
		move.w	#1,jumpbyte
		rts
blumjxo1:	move.w	#2,jumpbyte
blumjxx:	rts



;* deltax>deltay (hit side-on, luigi standing)

blumjx:		move.w	marioxpos,d0
		cmp.w	luigixpos,d0
		bhi.s	blummr		;mario right
;* luigi right
		move.w	#4,animbyteluigi	;luigi skid right
		move.w	#1,facingluigi
		move.w	#2,jumpbyte		;mario jump left
		move.w	#0,facing
		rts


;* mario right
blummr:		move.w	#5,animbyteluigi
		move.w	#0,facingluigi
		move.w	#1,jumpbyte
		move.w	#1,facing
		rts





;*** only luigi jumping ****
;* 2 cases

bluljump:	cmp.w	#10,animbyte	;mario waiting?
		beq	blumljump
		cmp.w	#9,animbyte	;mario hit?
		beq	blumljump

		move.w	marioxpos,d0
		move.w	luigixpos,d1
		move.w	marioypos,d2
		move.w	luigiypos,d3
		cmp.w	d0,d1
		bhi.s	blulj1
		sub.w	d1,d0		;d0>0  dx
		add.w	#2,d0
		bra	bluljxc

blulj1:		sub.w	d0,d1
		move.w	d1,d0		;d0>0
		add.w	#2,d0

bluljxc:	cmp.w	d2,d3
		bhi.s	blulj2
		sub.w	d3,d2		;d2>0  dy
		bra	bluljyc
blulj2:		sub.w	d2,d3
		move.w	d3,d2		;d2>0

bluljyc:	cmp.w	d0,d2
		blo.L	bluljx

;* deltax<deltay (hit top-bottom, mario standing)


;luigi on top => luigi=start new jump

		move.w	#10,tabcntluigi
		move.w	#0,chflagluigi
;calc. dir. if jumpbyte=0
		cmp.w	#0,jumpbyteluigi
		bne.s	bluljex
		cmp.w	#0,facingluigi
		beq.s	bluljxo1
		move.w	#1,jumpbyteluigi
		rts
bluljxo1:	move.w	#2,jumpbyteluigi
bluljex:	rts


;* deltax>deltay (hit side-on, mario standing)

bluljx:		move.w	luigixpos,d0
		cmp.w	marioxpos,d0
		bhi.s	blullr		;luigi right
;* mario right
		move.w	#4,animbyte
		move.w	#1,facing
		move.w	#2,jumpbyteluigi
		move.w	#0,facingluigi
		rts

;* luigi right
blullr:		move.w	#5,animbyte
		move.w	#0,facing
		move.w	#1,jumpbyteluigi
		move.w	#1,facingluigi
		rts






;*** both mario and luigi jumping ***
;*
;* NB! one could be waiting or hit.
;*

blumljump:	cmp.w	#0,mlcoll
		beq.s	blumljok
		rts

;no coll. last frame!
blumljok:	move.w	#1,mlcoll	;collision in-air
		move.w	marioxpos,d0
		move.w	luigixpos,d1
		move.w	marioypos,d2
		move.w	luigiypos,d3
		cmp.w	d0,d1
		bhi.s	blumlj1
		sub.w	d1,d0		;d0>0  dx
		bra	blumljxc
blumlj1:	sub.w	d0,d1
		move.w	d1,d0		;d0>0				

blumljxc:	cmp.w	d2,d3
		bhi.s	blumlj2
		sub.w	d3,d2		;d2>0  dy
		bra	blumljyc
blumlj2:	sub.w	d2,d3
		move.w	d3,d2		;d2>0

blumljyc:	cmp.w	d0,d2
		blo.L	blumljx

;* deltax<deltay (hit top-bottom)
		cmp.w	#10,animbyte	;waiting?
		beq.L	blumljwm
		cmp.w	#10,animbyteluigi
		beq.L	blumljwl
		move.w	marioypos,d0
		cmp.w	luigiypos,d0
		blo.s	blumtop

;luigi on top => luigi=hit

 		move.w	#9,animbyteluigi	;luigi hit
		move.w	#-2,tabcntluigi
		move.w	#60,tabcnt		;start fall
		move.w	#0,chflagluigi		;luigi can go up

		move.w	marioxpos,d1
		move.w	luigixpos,d2
		bsr	calcxoffset

		cmp.w	#0,d0
		bne.s	blultopxok
		add.w	#1,d0		;make xoff !=0

blultopxok:	move.w	d0,xoffsetluigi
		move.w	d1,jumpbyteluigi
		move.w	d2,facingluigi
		rts



;mario on top => mario=hit

blumtop:	move.w	#9,animbyte	;hit
		move.w	#-2,tabcnt
		move.w	#60,tabcntluigi	;start fall
		move.w	#0,chflag	;mario can go up

;pass parm. to calcxoff
		move.w	luigixpos,d1
		move.w	marioxpos,d2
		bsr	calcxoffset	;calc xoff for mario
;get return values from calcxoff
;and prevent xoffset=0
		cmp.w	#0,d0
		bne.s	blumtopxok
		add.w	#1,d0		;make xoff !=0

blumtopxok:	move.w	d0,xoffset
		move.w	d1,jumpbyte
		move.w	d2,facing
		rts

;* deltax>deltay (hit side-on)

blumljx:	cmp.w	#10,animbyte	;mario waiting?
		beq	blumljwm
		cmp.w	#10,animbyteluigi	;luigi waiting?
		beq	blumljwl

		cmp.w	#0,jumpbyte	;jumping up?
		beq.s	blumljturnl
		cmp.w	#2,jumpbyte	;turn mario
		bne.s	blumljturnm
		move.w	#1,jumpbyte	;turn mario
		move.w	#1,facing
		bra	blumljturnl
blumljturnm:	move.w	#2,jumpbyte
		move.w	#0,facing

blumljturnl:	cmp.w	#0,jumpbyteluigi	;jump up?
		beq.s	blumljturnex
		cmp.w	#2,jumpbyteluigi
		bne.s	blumljturn
		move.w	#1,jumpbyteluigi
		move.w	#1,facingluigi
		rts

blumljturn:	move.w	#2,jumpbyteluigi
		move.w	#0,facingluigi
blumljturnex:	rts


;* mario waiting, hit by luigi side-on *
blumljwm:	move.w	#1,animbyte	;start falling
		move.w	#46,tabcnt
;turn luigi
		cmp.w	#2,jumpbyteluigi
		bne.s	blumljwmturn
		move.w	#1,jumpbyteluigi
		move.w	#1,facingluigi
		move.w	#2,jumpbyte	;mario fall left 
		move.w	#0,facing
		rts
blumljwmturn:	move.w	#2,jumpbyteluigi
		move.w	#0,facingluigi
		move.w	#1,jumpbyte	;fall right
		move.w	#1,facing
		rts


;* luigi waiting, hit by mario side-on *
blumljwl:	move.w	#1,animbyteluigi	;start falling
		move.w	#46,tabcntluigi
;turn mario
		cmp.w	#2,jumpbyte
		bne.s	blumljwlturn
		move.w	#1,jumpbyte
		move.w	#1,facing
		move.w	#2,jumpbyteluigi
		move.w	#0,facingluigi
		rts
blumljwlturn:	move.w	#2,jumpbyte
		move.w	#0,facing
		move.w	#1,jumpbyteluigi
		move.w	#1,facingluigi
		rts




;----------------------------------------------------

;*
;* calculate xoffset:
;*
;* used by mario/luigi
;* in:      x1 in d1, x2 in d2
;* out: xoffset (for x2) in d0
;* jumpbyte in d1,facing in d2
;*

calcxoffset:	cmp.w	d2,d1
		bls.s	cxoff2		;neg. offset
		sub.w	d2,d1
		lsr.w	#1,d1
		move.w	#0,d2
		sub.w	d1,d2
		move.w	d2,d0		;hit left
;xoffset=(x2/2-x1/2)
		move.w	#2,d1		;jump left
		move.w	#0,d2		;face left
		rts

cxoff2:		sub.w	d1,d2
		lsr.w	#1,d2
;xoffset=(x2-x1)/2
		move.w	d2,d0		;hit right
		move.w	#1,d1
		move.w	#1,d2
		rts



;-------------------------------------------------


;*
;* variables:
;*


;*** mario variables ***

;animbyte: #1 = jumping
;	   #2 = running right
;	   #3 = running left
;	   #4 = skid right
;	   #5 = skid left
;	   #6 = dead1

marioactive:	dc.w	0	;used when only luigi is onscreen
				;and to allow delayed printing
				;when starting game
lives:		dc.w	0	;# of lives left
animbyte:	dc.w	0

colhflag:	dc.w	0
chflag:		dc.w	0
		
marioxpos:	dc.w	0
marioxposition: dc.w	0
marioypos:	dc.w	0	;real ypos (<0 => rypos!=vypos)
oldmarioy:	dc.w	0	;last ypos
mariovypos:	dc.w	0	;virtual ypos (start of bob on-screen)

tabcnt:		dc.w	0
skidcnt:	dc.w	0
runcnt:		dc.w	0
deadcnt:	dc.w	0
deadcnt2:	dc.w	0
deadflag:	dc.w	0
deadno:		dc.w	0	;0=ord. 1=iced 2=burnt
xoffset:	dc.w	0
hitflag:	dc.w	0
powhitmario:	dc.w	0	;1=pow hit in current jump
mariosafe:	dc.w	0
gfxcnt:		dc.w	0
fbtimerm:	dc.w	0	;# of consecutive frames with
				;same marioypos.

mlcoll:		dc.w	0	;m/l-coll.? (0=no)

;* jumpbyte
;0 = jump up (opt)
;1 = jump right
;2 = jump left

jumpbyte:	dc.w	0
maxdown:	dc.w	0


;* dir. facing 0=left

facing:		dc.w	0


;*** luigi variables ***

luigiactive:	dc.w	0	;0=off
livesluigi:	dc.w	0
animbyteluigi:	dc.w	0

colhflagluigi:	dc.w	0
chflagluigi:	dc.w	0
		
luigixpos:	dc.w	0
luigixposition:	dc.w	0
luigiypos:	dc.w	0
oldluigiy:	dc.w	0
luigivypos:	dc.w	0

tabcntluigi:	dc.w	0
skidcntluigi:	dc.w	0
runcntluigi:	dc.w	0
deadcntluigi:	dc.w	0
deadcnt2luigi:	dc.w	0
deadflagluigi:	dc.w	0
deadnoluigi:	dc.w	0
xoffsetluigi:	dc.w	0
hitflagluigi:	dc.w	0
powhitluigi:	dc.w	0
luigisafe:	dc.w	0
gfxcntluigi:	dc.w	0
fbtimerl:	dc.w	0

jumpbyteluigi:	dc.w	0
maxdownluigi:	dc.w	0

facingluigi:	dc.w	0


;* 54*2 = 108 *
jumptable:	dc.w	-4,-4,-4,-4,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2
		dc.w	-2,-1,-1,-1,-1,-1,-1,-1,0,0,0
		dc.w	0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2
		dc.w	2,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4
		dc.w	4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,$80

skidtable:	dc.w	8,8,8,0,8,8,8,0,8,8,0,8,8,0,8,0,8,0,$80

pushcnt:	dc.w	0

;* scores *

;* hi-score
hiscore:	dc.w	0
hischunthou:	dc.w	0
hisctenthou:	dc.w	0
hiscthou:	dc.w	0
hischun:	dc.w	0

;* mario/luigi-score
pscorecntm:	dc.w	0
pscorecntl:	dc.w	0
marioscore:	dc.w	0
luigiscore:	dc.w	0
mariolastscore:	dc.w	0
luigilastscore:	dc.w	0
pshunm:		dc.w	0
psthoum:	dc.w	0
pstenthoum:	dc.w	0
pshunthoum:	dc.w	0
pshunl:		dc.w	0
psthoul:	dc.w	0
pstenthoul:	dc.w	0
pshunthoul:	dc.w	0

screen:		dc.l	0	;location of screen to print to
				;not the current gamescreen!
screenf:	dc.w	0
frame:		dc.w	2

maxnooffb:	dc.w	0	;max. # of fireballs on-screen
firelaunched:	dc.w	0	;no of fireballs launched

mariosource:	dc.l	0
mariosource2:	dc.l	0
mariomaskadr:	dc.l	0
mariobobadr:	dc.l	0

luigisource:	dc.l	0
luigisource2:	dc.l	0
luigimaskadr:	dc.l	0
luigibobadr:	dc.l	0

;*
;* levels:
;*
;* leveldata must end with a $80
;* the final leveldata must end with $80,$80
;* leveldata must be located on sequential adresses
;* because the levelcontroller will just add 2 to
;* levelpnt+levelno when switching level.
;* use $8000 for bonus level.
;*

levelpnt:	dc.w	0	;offset in current leveldata
levelno:	dc.l	0	;adr. of current leveldata
levelcnt:	dc.w	0	;time since levelstart
levelnumber:	dc.w	1	;level#	ones
levelnumten:	dc.w	0	;level# tens
levelstart:	dc.w	1	;level# in one word

level1:		dc.w	200,1,1,300,1,2,500,1,2,$80
level2:		dc.w	200,1,2,300,1,1,500,1,1,600,1,2
		dc.w	800,1,1,900,1,2,$80

level3:		dc.w	$8000,$80	;bonus level

level4:		dc.w	200,2,1,300,2,2,500,2,2,600,2,2,$80
level5:		dc.w	200,2,2,300,2,1,500,1,2,600,1,2
		dc.w	800,2,1,900,2,2,$80
level6:		dc.w	200,3,1,300,3,2,500,3,1,600,3,2,$80
level7:		dc.w	200,3,2,300,3,1,500,2,2,600,2,1
		dc.w	800,3,2,900,3,1,$80

level8:		dc.w	$8000,$80

level9:		dc.w	200,1,1,300,1,2,500,4,1,600,3,2
		dc.w	800,1,1,900,1,2,$80
level10:	dc.w	200,2,2,300,2,1,500,4,2,600,3,1
		dc.w	800,1,2,900,2,2,$80
level11:	dc.w	200,3,1,300,2,2,500,4,1,600,2,2
		dc.w	800,2,1,900,3,1,950,2,2,$80
level12:	dc.w	200,1,2,300,1,1,500,4,1,600,3,1
		dc.w	800,1,1,900,1,1,$80
level13:	dc.w	200,2,1,300,2,2,500,4,2,600,3,1
		dc.w	800,2,1,900,2,2,950,2,2,$80
level14:	dc.w	200,3,2,300,2,1,500,4,1,600,2,2
		dc.w	800,2,2,900,3,2,950,2,2,$80
level15:	dc.w	$8000,$80	;bonus level II

level16:	dc.w	200,1,1,300,1,2,500,4,1,600,3,2
		dc.w	800,1,1,900,1,2,$80
level17:	dc.w	200,2,2,300,2,1,500,4,1,600,3,2
		dc.w	800,4,2,900,2,1,950,2,1,$80
level18:	dc.w	200,3,1,300,2,2,500,4,1,600,2,1
		dc.w	800,2,1,900,3,2,950,2,1,$80
level19:	dc.w	200,1,2,300,1,1,500,4,1,600,3,2
		dc.w	800,1,1,900,1,2,$80
level20:	dc.w	200,2,1,300,2,2,500,4,2,600,3,1
		dc.w	800,2,2,900,2,1,950,2,2,$80

level21:	dc.w	200,1,2,300,2,1,500,3,2,600,4,1
		dc.w	800,3,2,900,2,1,950,1,2,$80
level22:	dc.w	200,2,1,300,2,2,500,2,2,600,2,1
		dc.w	800,3,2,900,3,1,$80
level23:	dc.w	200,3,1,300,2,2,500,4,1,600,2,1
		dc.w	800,2,1,900,3,2,950,2,1,$80
level24:	dc.w	200,3,2,300,2,1,500,4,1,600,2,2
		dc.w	800,2,2,900,3,2,950,2,2,$80
level25:	dc.w	$8000,$80	;bonus level

level26:	dc.w	200,1,1,300,1,2,500,2,1,600,3,2
		dc.w	800,1,1,900,4,2,1100,3,2,1200,1,1,$80
level27:	dc.w	200,2,1,300,2,2,500,2,2,600,2,1
		dc.w	800,3,2,900,3,1,$80
level28:	dc.w	200,3,1,300,2,2,500,4,1,600,2,1
		dc.w	800,2,1,900,3,2,950,2,1,$80
level29:	dc.w	200,3,2,300,2,1,500,4,1,600,2,2
		dc.w	800,2,2,900,3,2,950,2,2,$80
level30:	dc.w	200,2,1,300,3,2,500,1,2,600,1,2
		dc.w	800,3,1,900,4,2,$80
level31:	dc.w	200,2,2,300,3,2,500,2,1,600,1,2
		dc.w	800,4,1,900,1,2,1100,1,1,1200,2,2,$80
level32:	dc.w	200,2,2,300,3,2,500,2,1,600,3,2
		dc.w	800,4,1,900,2,2,1100,3,1,1200,2,2,$80
level33:	dc.w	200,2,2,300,3,2,500,2,1,600,1,2
		dc.w	800,4,1,900,1,2,1100,1,1,1200,2,2,$80
level34:	dc.w	200,2,2,300,3,2,500,2,1,600,3,2
		dc.w	800,4,1,900,2,2,1100,3,1,1200,2,2,$80
level35:	dc.w	200,2,2,300,3,2,500,2,1,600,3,2
		dc.w	800,4,1,900,2,2,1100,3,1,1200,2,2,$80
level36:	dc.w	$8000,$80	;bonus level

level37:	dc.w	200,1,1,300,1,2,500,1,1,600,1,1
		dc.w	800,4,1,900,4,2,$80
level38:	dc.w	200,2,2,300,2,1,500,2,2,600,2,1
		dc.w	800,4,1,900,1,1,1100,4,2,$80
level39:	dc.w	200,1,1,300,1,1,500,1,2,600,1,2
		dc.w	800,1,1,900,1,2,1100,4,1,1200,4,2,$80
level40:	dc.w	200,2,1,300,3,2,500,1,2,600,1,2
		dc.w	800,3,1,900,4,2,$80
level41:	dc.w	200,1,1,300,1,2,500,1,1,600,1,1
		dc.w	800,4,1,900,4,2,$80
level42:	dc.w	200,2,2,300,2,1,500,2,2,600,2,1
		dc.w	800,4,1,900,1,1,1100,4,2,$80
level43:	dc.w	200,1,1,300,1,1,500,1,2,600,1,2
		dc.w	800,1,1,900,1,2,1100,4,1,1200,4,2,$80
level44:	dc.w	200,2,1,300,3,2,500,1,2,600,1,2
		dc.w	800,3,1,900,4,2,$80
level45:	dc.w	200,1,1,300,1,1,500,1,2,600,1,2
		dc.w	800,1,1,900,1,2,1100,4,1,1200,4,2,$80
level46:	dc.w	200,2,1,300,3,2,500,1,2,600,1,2
		dc.w	800,3,1,900,4,2,$80
level47:	dc.w	200,1,1,300,1,2,500,1,1,600,1,1
		dc.w	800,4,1,900,4,2,$80
level48:	dc.w	200,2,2,300,2,1,500,2,2,600,2,1
		dc.w	800,4,1,900,1,1,1100,4,2,$80
level49:	dc.w	$8000,$80

level50:	dc.w	200,1,1,300,3,1,500,4,2,600,3,2
		dc.w	800,2,2,900,2,1,1100,4,2,1200,2,1,$80,$80

;* end of levels *

;NB! levels 1-20 taken from 1983 mario


gameover:	dc.w	0	;gameover? (0=no)
powlevel:	dc.w	0	;# of pows left
bonusprint:	dc.w	0
bonusflag:	dc.w	0	;bonus round running? (0=no)
bonustimer:	dc.w	0
tensecs:	dc.w	2	;time in bonus level
secs:		dc.w	9
tenthofsec:	dc.w	9
icetimer:	dc.w	0	;time to reapperance of ice
icetimer2:	dc.w	0	;2. timer


;*** enemy variables ***

nmesource:	dc.l	0,0,0,0,0,0,0,0,0,0
nmesource2:	dc.l	0,0,0,0,0,0,0,0,0,0
nmesavemem:	dc.l	0,0,0,0,0,0,0,0,0,0
nmesavemem2:	dc.l	0,0,0,0,0,0,0,0,0,0
nmebobadr:	dc.l	0,0,0,0,0,0,0,0,0,0
nmemaskadr:	dc.l	0,0,0,0,0,0,0,0,0,0
nmetimer1:	dc.w	0,0,0,0,0,0,0,0,0,0
nmetimer2:	dc.w	0,0,0,0,0,0,0,0,0,0
nmetype:	dc.w	0,0,0,0,0,0,0,0,0,0
nmeanimbyte:	dc.w	0,0,0,0,0,0,0,0,0,0
nmexposition:	dc.w	0,0,0,0,0,0,0,0,0,0
nmeyposition:	dc.w	0,0,0,0,0,0,0,0,0,0
nmexpos:	dc.w	0,0,0,0,0,0,0,0,0,0
nmespeed:	dc.w	0,0,0,0,0,0,0,0,0,0
nmefacing:	dc.w	0,0,0,0,0,0,0,0,0,0
nmetabcnt:	dc.w	0,0,0,0,0,0,0,0,0,0
nmemaxdown:	dc.w	0,0,0,0,0,0,0,0,0,0
nmeactive:	dc.w	0,0,0,0,0,0,0,0,0,0
nmeaggr:	dc.w	0,0,0,0,0,0,0,0,0,0
nmewalkcnt:	dc.w	0,0,0,0,0,0,0,0,0,0
nmehitcnt:	dc.w	0,0,0,0,0,0,0,0,0,0
nmehitflag:	dc.w	0,0,0,0,0,0,0,0,0,0
nmexoffset:	dc.w	0,0,0,0,0,0,0,0,0,0
nmesafe:	dc.w	0,0,0,0,0,0,0,0,0,0
even

;*
;* notes to nme variables
;*
;* nmeactive will be set to 2/3 at the end of spinning(dead)
;* to allow the restoreroutine to restore both frames when dead.
;* so when checking if an nme is active check both 1,2 and 3.
;*
;* xposition is calculated from xpos (/8). (opp. to mario/luigi)
;*
;* nmeactive:	#0 = not active
;*		#1 = active real (restore and save)
;*		#2 = active just dead (only restore 1.frame)
;*		#3 = active just dead (only restore 2.frame)
;*		#4 = active just launched (only save 1.frame)
;*		#5 = active just launched (only save 2.frame)
;*
;* nmefacing:	#1 = face right, #2 = face left.
;*


;*
;* collision map; platforms:
;*

;* info: ypos,xpostart,xposend.

platforms:
		dc.w	27,0,38		;left pipe
		dc.w	27,281,319	;right pipe
		dc.w	60,0,136
		dc.w	60,182,319
		dc.w	111,0,38
		dc.w	104,69,250
		dc.w	111,281,319
		dc.w	146,0,116
		dc.w	146,203,319
		dc.w	142,145,175	;upper pow
		dc.w	150,145,175	;lower pow
		dc.w	188,0,319	;red bricks
		dc.w	$8000		;no more platforms

