	.6502

	.INCLUDE	"MARIA2.S"

	.INCLUDE	"GAMEQU.S"

	.INCLUDE	"PAGEZERO.S"

	.INCLUDE	"RAMAREA.S"

	.INCLUDE	"ROMAREA.S"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	MAIN PROGRAM	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; START COMMANDO
; place for IRQ and NMI to attach
dummy:
	rti

main:		
	sei
	cld

	ldx	#$FF	;init stack
 	txs

	lda	#$7
	sta	PTCTRL
	
;	LDA	#$7F
;	STA	CTRL

;	DPPLOAD	DLLIST1

	inx
loop:
;move display list list to RAM

	lda	ROMCODE,x
	sta	RAM3,x

	lda	ROMCODE+$100,x
	sta	RAM3+$100,x

	inx
 	bne	loop
main30:
	bit	MSTAT	;Vblank?
	bmi	main30	
main32:
	bit	MSTAT 	;Vblank?
	bpl	main32	

	SCREEN	0,2,0,1,1,0
	
;0=normal color.
;2=Normal DMA.
;0=single byte wide characters.
;1=background colored border.
;0=transparency mode.
;3=320 a or c mode.

; DO CODE INITIALIZATION

	LDA	#MAPMAX
	STA	MAPHI
	LDA	#33
	STA	MAPLO
	LDA	#$0
	STA	HICNT
	STA	?MAPLO
	STA	ADDONE
	STA	VLSBACC
	STA	VMSBACC
	STA	LINEOFF
	STA	FRAME
	STA	CASENUM
	LDA	#$32
	STA	BKGRND		

	LDA	#$06
	STA	P0C1
	LDA	#$00
	STA	P0C2
	LDA	#$D4
	STA 	P0C3

; TRANSFER MAP VALUES FROM ROM TO RAM
	LDX	#MAPMAX
MAPLOOP:
	LDA	RMAPOBJ,X
	STA	MAPOBJ,X
	LDA	RMAPHPOS,X
	STA	MAPHPOS,X
	LDA	RMAPVLSB,X
	STA	MAPVLSB,X
	LDA	RMAPVMSB,X
	STA	MAPVMSB,X
	DEX
	BPL	MAPLOOP	
; REPEAT - WAIT UNTIL VBLANK

MAIN40:
	bit	MSTAT	;Vblank?
	bmi	MAIN40	
MAIN50:
	bit	MSTAT 	;Vblank?
	bpl	MAIN50

	LDA	#$32
	STA	BKGRND
;  DO RESET
	LDA	SWCHB
	AND	#RESET
	BNE	MAIN55

	JMP	main
;  IF CASENUM = 0
MAIN55:	LDA	CASENUM
	CMP	#0
	BNE	MAIN60
;   DLL=DLLIST0
;   ZONEVEC=ZONES0
	DPPLOAD	DLLIST1

	LDA	#ZONES0 & $FF
	STA	ZONEVEC
	LDA	#ZONES0 >> 8
	STA	ZONEVEC+1
	JMP	MAIN70
;  ELSE DLL=DLLIST1
;   ZONEVEC=ZONES1
MAIN60:	DPPLOAD	DLLIST0

	LDA	#ZONES1 & $FF
	STA	ZONEVEC
	LDA	#ZONES1 >> 8
	STA	ZONEVEC+1
;  ENDIF
;  ZONEPTR'S = ZONEBASES
MAIN70:	LDY	#23
ZLOOP:	LDA 	(ZONEVEC),Y
	STA	ZONEPTR,Y
	DEY
	LDA	(ZONEVEC),Y
	STA	ZONEPTR,Y
	DEY
	BPL	ZLOOP
;  DO TERRAIN
;	JSR	MEN
;  DO MEN
	JSR 	TERRAIN
;  INCREMENT FRAME
	INC	FRAME	
;  INCREMENT VLSB/VMSB ACC VALUES
	LDA	FRAME
	AND	#$07
	BNE	MAIN75

;	LDA	SWCHA
;	AND	#$0F
;	CMP	#$0E
;	BNE	SCROLL10

	LDA	#1
	STA	ADDONE

	INC	VLSBACC
	LDA	VLSBACC
	CMP	#192
	BNE	MAIN77
	INC	VMSBACC
	LDA	#0
	STA	VLSBACC
	JMP	MAIN77
MAIN75:
	LDA	#0
	STA	ADDONE
MAIN77:
;  TERMINATE HEADER ZONES
	LDX	#23
Z2LOOP:	LDA	ZONEPTR,X
	STA	TPTR1+1
	DEX
	LDA	ZONEPTR,X
	STA	TPTR1

	LDA 	#0
	LDY	#0
	STA	(TPTR1),Y
	INY
	STA 	(TPTR1),Y
	DEX
	BPL	Z2LOOP
;   IF CASENUM=0
	LDA	CASENUM
	CMP	#0
	BNE	MAIN80
;    CASENUM=1
	LDA	#1
	STA	CASENUM
	JMP	MAIN90
;   ELSE CASENUM=0
MAIN80:	LDA	#0
	STA	CASENUM
;   ENDIF
MAIN90:	
	LDA	#$22
	STA	BKGRND
;  UNTIL FOREVER	
	JMP	MAIN40
; END COMMANDDO

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; START TERRAIN
;  MAPNUM=#MAPMAX
TERRAIN:
	LDA	MAPHI
	STA	MAPNUM
;  IF ?MAPLP SET
	LDA	?MAPLO
	BEQ	TLOOP
;   DECREMENT MAPLO
;   ?MAPLO=CLEAR
;	LDA	#0
;	STA	BKGRND
	DEC	MAPLO
	LDA	MAPLO
	CMP	#MANMAX
	BNE	TERR20

	JMP	main
TERR20:
	LDA	#0
	STA	?MAPLO	
;  REPEAT
;   IF OBJECT VISIBLE
TLOOP:
	JSR	VISCHK
	BEQ	TERR10
;    DRAW OBJECT
	JSR	DRAW
;   ENDIF
;   DO SCROLL
TERR10:
	JSR	SCROLL
;   DECREMENT MAPNUM
	DEC 	MAPNUM
;  UNTIL MAPNUM < 0
	LDA	MAPLO
	CMP	MAPNUM
	BNE	TLOOP
; END TERRAIN
	RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; START MEN
;  MAPNUM=#MAPMAX
MEN:
	LDA	#MANMAX
	STA	MAPNUM
;  REPEAT
;   IF MAN VISIBLE
MENLOOP:
	JSR	VISCHK
	BEQ	MEN10
;    DRAW MAN
	JSR	DRAW
;   ENDIF
;   DO ANIMATE
MEN10:
	JSR	ANIMATE
;   DECREMENT MAPNUM
	DEC 	MAPNUM
;  UNTIL MAPNUM < 0
	BPL	MENLOOP
; END TERRAIN
	RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; START DRAW
;  VZONE=VPOS/16
;  VOFF=REMAINDER
DRAW:	LDX	MAPNUM
	LDA	MAPVLSB,X
	AND	#$0F
	STA	VOFF
	LDA	MAPVLSB,X
	LSR
	LSR
	LSR
	LSR
	STA	VZONE
;  OBJNUM=MAPOBJ
	LDX	MAPNUM
	LDA	MAPOBJ,X
	STA	OBJNUM
;  OBJADR=OBJPTR
	ASL
	TAY
	LDA	OBJPTR,Y
	STA	OBJADR
	INY
	LDA	OBJPTR,Y
	STA	OBJADR+1
;  TCOLOR=OBJCOLOR*32
	LDX	OBJNUM
	LDA	OBJCOLOR,X
	ASL
	ASL
	ASL
	ASL		
   	LDA	#0
	STA	TCOLOR
;  TWIDTH=$1F-OBJHSIZE
	SEC
	LDA	#$1F
	SBC	OBJHSIZE,X
	STA	TWIDTH
;  COLORWTH=TCOLOR OR TWIDTH
	LDA	TCOLOR
	ORA	TWIDTH
	STA	COLORWTH
;  OBJVCNT=OBJVSIZE-OBJVINIT
	SEC
	LDA	OBJVSIZE,X
	SBC	OBJVINIT
	STA	OBJVCNT
;  OBJVPART=OBJVSIZE-OBJVCNT
	SEC
	LDA	OBJVSIZE,X
	SBC	OBJVCNT
	STA	OBJVPART
;   IF ?OFFBOT SET
	LDA	?OFFBOT
	BEQ	DRAW35
;    INCREMENT OBJVINIT
	LDA	OBJVTMP
	STA	OBJVCNT
;    IF VOFF=0
	LDA	VOFF
	BNE	DRAW35
;     DECREMENT OBJVCNT
	DEC	OBJVCNT
;   ENDIF
;  ENDIF
;  HPOS=MAPHPOS
DRAW35:	LDX	MAPNUM
	LDA	MAPHPOS,X
	STA	HPOS
;  IF VOFF=0
	LDA	VOFF
	BNE	DRAW30
;   IF ?OFFTOP SET
	LDA	?OFFTOP
	BEQ	DRAW30
;   DECREMENT OBJVCNT
	INC	VNDXCNT
;  ENDIF
;  VNDX=VZONE+OBJVPART
DRAW30:	CLC
	LDA	VZONE
	ADC	VNDXCNT
	STA	VNDX
;  REPEAT
;   IF VOFF=0
DLOOP:	LDA	VOFF
	BNE	DRAW10
;    LOADR=OBJDEF LO
;    HIADR=OBJDEF HI
	LDA	OBJVPART
	ASL
	TAY
	LDA	(OBJADR),Y
	STA	LOADR
	INY
	LDA	(OBJADR),Y
	STA	HIADR
;    DO HEADER
;    TRANSFER HEADER
	LDA	VNDX
	ASL
	TAX
	LDA	ZONEPTR,X
	STA	ZONETMP
	INX
	LDA	ZONEPTR,X
	STA	ZONETMP+1
	LDY 	#3
HEAD30:	LDA	LOADR,Y
	STA	(ZONETMP),Y
	DEY
	BPL	HEAD30
;    UPDATE ZONEPTR
	DEX

	CLC
	LDA	ZONEPTR,X
	ADC	#04
	STA	ZONEPTR,X
;    DO COPY
;	JSR	COPY
;    INCREMENT VNDXCNT
	INC	VNDX 
	JMP	DRAW20
;   ELSE CALCADR
DRAW10:	JMP	CALCADR
;   ENDIF
;   INCREMENT OBJVPART
DRAW20:	INC	OBJVPART
;   DECREMENT OBJVCNT
	DEC	OBJVCNT
;  UNTIL OBJVCNT < 0
	BPL	DLOOP
;  RESTORE MAPVLSB
	LDA	SAVEVLSB
	LDY	MAPNUM
	STA	MAPVLSB,Y
; END DRAW
	RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; START CALCADR
;  OBJBASE=OBJDEF
CALCADR:
	LDA	OBJVPART
	ASL
	TAY
	LDA	(OBJADR),Y
	STA	LOADR
	INY
	LDA	(OBJADR),Y
	STA	OBJBASE+1
;  IF ?OFFTOP CLEAR
	LDA	?OFFTOP
	BNE	CALC10
;  LINEOFF=VOFF*256
	LDA	VOFF
	STA 	LINEOFF+1
;  LOADR=(OBJBASE+LINEOFF) LO
;  HIADR=(OBJBASE+LINEOFF) HI
	CLC
	LDA	OBJBASE+1
	ADC	LINEOFF+1
	STA	HIADR
;  DO HEADER
;  TRANSFER HEADER
	LDA	VNDX
	ASL
	TAX
	LDA	ZONEPTR,X
	STA	ZONETMP
	INX
	LDA	ZONEPTR,X
	STA	ZONETMP+1
	LDY 	#3
HEAD20:	LDA	LOADR,Y
	STA	(ZONETMP),Y
	DEY
	BPL	HEAD20
;  UPDATE ZONEPTR
	DEX

	CLC
	LDA	ZONEPTR,X
	ADC	#04
	STA	ZONEPTR,X
;  DO COPY
;	JSR 	COPY 
	JMP	CALC20
;  ELSE CLEAR ?OFFTOP
CALC10:	LDA	#0
	STA	?OFFTOP
;  ENDIF
;  VNDXCNT=VNDXCNT+1
CALC20:	INC	VNDX
;  IF OBJVCNT=0
	LDA	OBJVCNT
	BNE	CALC25
;  IF ?OFFBOT CLEAR
	LDA	?OFFBOT
	BNE	CALC30	
;  LINEOFF=(16-VOFF)*256
CALC25:	SEC
	LDA	#16
	SBC	VOFF
	STA	LINEOFF+1
;  LOADR=(OBJBASE-LINEOFF) LO 
;  HIADR=(OBJBASE-LINEOFF) HI
	SEC
	LDA	OBJBASE+1
	SBC	LINEOFF+1
	STA	HIADR
;  DO HEADER
;  TRANSFER HEADER
	LDA	VNDX
	ASL
	TAX
	LDA	ZONEPTR,X
	STA	ZONETMP
	INX
	LDA	ZONEPTR,X
	STA	ZONETMP+1
	LDY 	#3
HEAD10:	LDA	LOADR,Y
	STA	(ZONETMP),Y
	DEY
	BPL	HEAD10
;  UPDATE ZONEPTR
	DEX

	CLC
	LDA	ZONEPTR,X
	ADC	#04
	STA	ZONEPTR,X
;  DO COPY
;	JSR	COPY
;  ENDIF
; END CALCADR
CALC30:	JMP DRAW20
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

VIS10T:	JMP	VIS10
; START
;  IF MAPVMSB=$FF
VISCHK:	LDX	MAPNUM
	LDA	MAPVMSB,X
	CMP	#$FF
	BNE	VIS10T
;   VLINES=OBJVSIZE*16
;   VLINES=VLINES-1 
	LDY	MAPOBJ,X
	STY	OBJNUM

	CLC
	LDA	OBJVSIZE,Y
	ADC	#1

	ASL
	ASL
	ASL
	ASL
	STA	VLINES
;	DEC	VLINES
;   INVISVAL=191-MAPVLSB
	SEC
	LDA	#192
	SBC	MAPVLSB,X
	STA	INVISVAL
;   IF INVISVAL < VLIINES (OFF TOP)
	CMP	VLINES
	BCS	VIS40

;	LDA	#0
;	STA	BKGRND
;    CHECK MAPLO
	INC	MAPLO
	CPX	MAPLO
	BNE	VIS41

;	LDA	#0
;	STA	BKGRND
	
	LDA	#$FF
	STA	?MAPLO
;	BNE	VIS43
;	LDA	#28
;	STA	MAPLO
VIS43:
	DEX
	CLC
	LDA	VLSBACC
	ADC	MAPVLSB,X
	BCS	VIS60
	CMP	#192
	BCS	VIS60

	STA	MAPVLSB,X
;	LDA	#0
;	STA	BKGRND
	JMP	VIS70
VIS60:
;	LDA	#0
;	STA	BKGRND
	SEC
	SBC	#192
	STA	MAPVLSB,X
	INC	MAPVMSB,X
VIS70:
	CLC	
	LDA	VMSBACC
	ADC	MAPVMSB,X
	STA	MAPVMSB,X
	INX
VIS41:	DEC	MAPLO
;    OBJVINIT=INVISVAL/16
;    SAVEVLSB=MAPVLSB    
;    MAPVLSB=REMAINDER
;    SET ?OFFTOP
;    CLEAR ?OFFBOT	
;    VNDXCNT=FF	
;    ACC=TRUE
	LDA	MAPVLSB,X
	STA	SAVEVLSB
	AND	#$0F
	STA	MAPVLSB,X

	LDA	INVISVAL
	LSR
	LSR
	LSR
	LSR
	STA	OBJVINIT

	LDA	#0
	STA	?OFFBOT
	LDA	#$FF
	STA	?OFFTOP
	STA	VNDXCNT
	JMP	VIS20
;   ELSE
VIS40:
;	LDA	MAPLO
;	CMP	#26
;	BNE 	VIS46
;	LDA	#0
;	STA	BKGRND
VIS46:
	LDA	#0
;   ENDIF
	JMP	VIS20
;  ELSEIF MAPVMSB=0
VIS10:	LDA	MAPVMSB,X
	CMP	#0
	BNE	VIS30

;	CPX	#30
;	BNE	VIS03
;	LDA	#0
;	STA	BKGRND
;   VLINES=OBJVSIZE*16
;   VLINES=VLINES-1 
VIS03:	LDY	MAPOBJ,X
	STY	OBJNUM

	CLC
	LDA	OBJVSIZE,Y
	ADC	#1

	ASL
	ASL
	ASL
	ASL
	STA	VLINES
;	DEC	VLINES
;   INVISVAL=191-MAPVLSB
	SEC
	LDA	#192
	SBC	MAPVLSB,X
	STA	INVISVAL
;   IF INVISVAL < VLIINES (OFF BOTTOM)
	CMP	VLINES
	BCS	VIS42
;    OBJVINIT=OBJVSIZE-(INVISVAL/16)
;    SAVEVLSB=MAPVLSB    
;    SET ?OFFTBOT
;    CLEAR ?OFFTOP
;    VNDXCNT=0	
;    ACC=TRUE
	LDA	MAPVLSB,X
	STA	SAVEVLSB

	LDA	INVISVAL
	LSR
	LSR
	LSR
	LSR
	STA	OBJVTMP

;	SEC
;	LDA	OBJVSIZE,Y
;	SBC	OBJVINIT
;	STA	OBJVINIT

	LDA	#0
	STA	OBJVINIT
	STA	?OFFTOP
	STA	VNDXCNT
	LDA	#$FF
	STA	?OFFBOT
	JMP 	VIS20
;   ELSE DO FULL OBJECT
;    OBJVINIT=0
;    CLEAR ?OFFTOP
;    CLEAR ?OFFBOT	
;    VNDXCNT=0
;    SAVEVLSB=MAPVLSB	
;    ACC=TRUE
VIS42:	LDA	#0
	STA	?OFFTOP
	STA	?OFFBOT
	STA	VNDXCNT
	STA	OBJVINIT

	LDA	MAPVLSB,X
	STA	SAVEVLSB

	LDA	#$FF
	JMP	VIS20
;  ELSE ACC=FALSE
VIS30:
	LDA	MAPVMSB,X
	CMP	#1
	BNE	VIS32
	
	INC	HICNT
	LDA	HICNT
	CMP	#3
	BNE	VIS32

	LDA	#0
	STA	HICNT
;	DEC	MAPHI
 
;	LDA	MAPHI
;	CMP	#1
;	BNE	VIS32
;	LDA	#MAPMAX
;	STA	MAPHI
VIS32:	
	LDA	#0
;  ENDIF
; END VISCHK
VIS20:	RTS
	 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; START SCROLL
SCROLL:
	LDX	MAPNUM

	CLC
	LDA	ADDONE
	ADC	MAPVLSB,X
	STA	MAPVLSB,X
	CMP	#192
	BNE	SCROLL10
	INC	MAPVMSB,X
	LDA	#0
	STA	MAPVLSB,X
SCROLL10:
	RTS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; START ANIMATE
ANIMATE:
	LDX	MAPNUM
	LDA	FRAME
	AND	SPEED,X
	BNE	AN10
; UPDATE POSITION
	LDA	DIRECTION,X
	TAY
	CLC
	LDA	DIRVLSB,Y
	ADC	MAPVLSB,X
	STA	MAPVLSB,X
	CLC
	LDA	DIRHPOS,Y
	ADC	MAPHPOS,X
	STA	MAPHPOS,X
; UPDATE OBJECT
	LDA	CASEBIT,X
	BNE	AN20

	LDA	#1
	STA	CASEBIT,X
	LDA	#UJOB
	STA	MAPOBJ,X
	JMP	AN30
AN20:
	LDA	#0
	STA	CASEBIT,X
	LDA	#UJOC
	STA	MAPOBJ,X
AN30:
; RANGE CHECK
	LDA	MAPVLSB,X
	BNE	AN40
	
	LDA	#149
	STA	MAPVLSB,X
AN40:
	LDA	MAPVLSB,X
	CMP	#150
	BNE	AN50

	LDA	#1
	STA	MAPVLSB,X
AN50:	
	LDA	MAPHPOS,X
	BNE	AN60
	
	LDA	#149
	STA	MAPHPOS,X
AN60:
	LDA	MAPHPOS,X
	CMP	#150
	BNE	AN70

	LDA	#1
	STA	MAPHPOS,X
AN70:
AN10:	RTS	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; START COPY
;  IF A OBJECT COPY
COPY:	
	LDX	OBJNUM
	LDA	COPYS,X
	BMI	COPY10
;   LOOPCNT=3 OF COPIES
	STA	LOOPCNT
;   HPOS POINTER
	TXA
	ASL
	TAX
	LDA	HPOSVEC,X
	STA	TPTR1
	INX
	LDA	HPOSVEC,X
	STA	TPTR1+1
;   REPEAT
;    HPOS VALUE
COPY20:
	LDY	LOOPCNT
	LDA	(TPTR1),Y
	STA	HPOS
;    TRANSFER HEADER
	LDA	VNDX
	ASL
	TAX
	LDA	ZONEPTR,X
	STA	ZONETMP
	INX
	LDA	ZONEPTR,X
	STA	ZONETMP+1
	LDY	#3
COPY30:
	LDA	LOADR,Y
	STA	(ZONETMP),Y
	DEY
	BPL	COPY30
;    UPDATE ZONEPTR
	DEX

	CLC
	LDA	ZONEPTR,X
	ADC	#04
	STA	ZONEPTR,X
;    DECREMENT LOOPCNT
	DEC	LOOPCNT
;   UNTIL LOOPCNT < 0
	BPL	COPY20
;  ENDIF
; END COPY OBJECT
COPY10:	RTS 
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; SET INTERRUPT VECTORS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	.org	$FFF8

	.dc.w	$F7FF
	.dc.w	dummy	;NMI
	.dc.w	main	;RESET
	.dc.w	dummy	;IRQ
