/***************************************/
/*  Use MADS http://mads.atari8.info/  */
/*  Mode: VBXE                         */
/***************************************/

	icl "sergeantseymour-robotcop.h"

	org $f0

fcnt	.ds 2
fadr	.ds 2
fhlp	.ds 2
cloc	.ds 1
regA	.ds 1
regX	.ds 1
regY	.ds 1

WIDTH	= 40
HEIGHT	= 30

; ---	BASIC switch OFF
	icl "sergeantseymour-robotcop.vbx"

	org $2000

	.zpvar	= $f0

detect	DetectCore
	bcs ok
	jsr printf
	dta $9b
	dta c'Core revision < 1.20',$9b,0

	pla
	pla
	rts

ok	rts

	.link 'printf.obx'

	ini detect

; ---	VBXE XDLIST, COLORS MAP

	org $0600
vbxe_bank_on
	fxs vbxe_bank FX_MEMB		;enable VBXE BANK #0 MEM = $4000..$7FFF
	inc vbxe_bank
	rts
vbxe_bank	dta $80

vbxe_bank_off
	fxs #$00 FX_MEMB		;disable VBXE BANK #0 MEM = $4000..$7FFF
	rts

	ini vbxe_bank_on

	org MEMAC_B_WINDOW
xdlist	equ *

	;XDLC
	;włączam mapę koloru pokrywającą 240 linii
	;jednocześnie jest to koniec XDL (XDLC_END)
	.word	XDLC_END|XDLC_RPTL|XDLC_MAPON|XDLC_MAPADR|XDLC_OVADR|XDLC_CHBASE|XDLC_MAPPAR|XDLC_OVATT

	;XDLC_RPTL
	;razem 240 linii z mapą koloru
	.byte	240-1
	
	.long $1000
	
	.word 320
	
	.byte 2

	;XDLC_MAPADR
	;adres mapy koloru bezpośrednio za XDL w pamięci VBXE
	.long	$4000

	;krok adresu mapy
	.word	40*4

	;XDLC_MAPPAR
	.byte	0 ;hscroll mapy
	.byte	0 ;vscroll mapy
	.byte	8-1 ;szerokość pola
	.byte	8-1 ;wysokość pola

	;XDLC_OVATT
	.byte %00010001	; screen 40 bytes
	.byte $ff

	ini vbxe_bank_on


	?l=.len "D:\!Delphi\graph2font\___examples\vbxe\sergeantseymour-robotcop.cmp"
	.rept [?l/16384]+[[?l%16384]<>0]

	org MEMAC_B_WINDOW
	ift ?l>=16384
	ins "D:\!Delphi\graph2font\___examples\vbxe\sergeantseymour-robotcop.cmp",#*16384,16384
	els
	ins "D:\!Delphi\graph2font\___examples\vbxe\sergeantseymour-robotcop.cmp",#*16384,?l
	eif

	?l-=16384
	ini vbxe_bank_on
	.endr

	ini vbxe_bank_off


; ---	MAIN PROGRAM
	org $2000
ant	dta $44,a(scr)
	dta $04,$04,$04,$84,$04,$04,$84,$04,$04,$84,$04,$04,$84,$04,$04,$84
	dta $04,$04,$84,$04,$04,$84,$04,$04,$04,$04,$04,$04,$04
	dta $41,a(ant)

scr	ins "sergeantseymour-robotcop.scr"

	.ds 0*40

	.ALIGN $0400
fnt	ins "sergeantseymour-robotcop.fnt"

	ift USESPRITES
	.ALIGN $0800
pmg	.ds $0300
	ift FADECHR = 0
	SPRITES
	els
	.ds $500
	eif
	eif

main
; ---	init PMG

	ift USESPRITES
	mva >pmg pmbase		;missiles and players data address
	mva #$03 pmcntl		;enable players and missiles
	eif

	lda:cmp:req $14		;wait 1 frame

	sei			;stop IRQ interrupts
	mva #$00 nmien		;stop NMI interrupts
	sta dmactl
	mva #$fe portb		;switch off ROM to get 16k more ram

	mwa #NMI $fffa		;new NMI handler

	lda #$00
	fxsa FX_XDL_ADR0		; XDLIST PROGRAM ADDRES = $000000
	fxsa FX_XDL_ADR1
	fxsa FX_XDL_ADR2

	lda #VC_XDL_ENABLED|VC_XCOLOR
	fxsa FX_VIDEO_CONTROL

	mva #$c0 nmien		;switch on NMI+DLI again

	jmp *		;CPU is busy here, so no more routines allowed

stop
	mva #$00 pmcntl		;PMG disabled
	tax
	sta:rne hposp0,x+

	mva #$ff portb		;ROM switch on
	mva #$40 nmien		;only NMI interrupts, DLI disabled
	cli			;IRQ enabled

	rts			;return to ... DOS

; ---	DLI PROGRAM

.local	DLI

	?old_dli = *

	ift !CHANGES

dli1	lda trig0		; FIRE #0
	beq stop

	lda trig1		; FIRE #1
	beq stop

	lda consol		; START
	and #1
	beq stop

	lda skctl
	and #$04
	beq stop

	lda vcount
	cmp #$02
	bne dli1

	:3 sta wsync

	DLINEW DLI.dli2
	eif


dli_start

dli2
	sta regA
	lda >fnt+$400*$01
	sta wsync		;line=40
	sta chbase
	DLINEW dli3 1 0 0

dli3
	sta regA
	lda >fnt+$400*$02
	sta wsync		;line=64
	sta chbase
	DLINEW dli4 1 0 0

dli4
	sta regA
	lda >fnt+$400*$03
	sta wsync		;line=88
	sta chbase
	DLINEW dli5 1 0 0

dli5
	sta regA
	lda >fnt+$400*$04
	sta wsync		;line=112
	sta chbase
	DLINEW dli6 1 0 0

dli6
	sta regA
	lda >fnt+$400*$05
	sta wsync		;line=136
	sta chbase
	DLINEW dli7 1 0 0

dli7
	sta regA
	lda >fnt+$400*$06
	sta wsync		;line=160
	sta chbase
	DLINEW dli8 1 0 0

dli8
	sta regA
	lda >fnt+$400*$07
	sta wsync		;line=184
	sta chbase

	lda regA
	rti

.endl

; ---

CHANGES = 0
FADECHR	= 0

SCHR	= 127

; ---

.proc	NMI

	bit nmist
	bpl VBL

	jmp DLI.dli_start
dliv	equ *-2

VBL
	sta regA
	stx regX
	sty regY

	sta nmist		;reset NMI flag

	mwa #ant dlptr		;ANTIC address program

	mva #@dmactl(standard|dma|lineX1|players|missiles) dmactl	;set new screen width

	inc cloc		;little timer

; Initial values

	lda >fnt+$400*$00
	sta chbase
c0	lda #$00
	sta colbak
	sta color0
c1	lda #$0E
	sta color1
c2	lda #$00
	sta color2
	sta color3
	lda #$02
	sta chrctl
	lda #$04
	sta gtictl
x0	lda #$00
	sta hposp0
	sta hposp1
	sta hposp2
	sta hposp3
	sta hposm0
	sta hposm1
	sta hposm2
	sta hposm3
	sta sizep0
	sta sizep1
	sta sizep2
	sta sizep3
	sta sizem
	sta colpm0
	sta colpm1
	sta colpm2
	sta colpm3

	mwa #DLI.dli_start dliv	;set the first address of DLI interrupt

;this area is for yours routines

quit
	lda regA
	ldx regX
	ldy regY
	rti

.endp

; ---
	run main
; ---

	opt l-

.MACRO	SPRITES
missiles
	.ds $100
player0
	.ds $100
player1
	.ds $100
player2
	.ds $100
player3
	.ds $100
.ENDM

USESPRITES = 0

.MACRO	DLINEW
	mva <:1 NMI.dliv
	ift [>?old_dli]<>[>:1]
	mva >:1 NMI.dliv+1
	eif

	ift :2
	lda regA
	eif

	ift :3
	ldx regX
	eif

	ift :4
	ldy regY
	eif

	rti

	.def ?old_dli = *
.ENDM

