;
;	ARSOME - by JAC!
;
;	(c) 2014-02-13 for SillyPack2k13
;
;	Code by JAC!, conversion using RasterConverterBeta7 by ilmenit.
;	Graphics "ARSOME.GIF" by Pafnucy & Gerwazy of Lamers in 2013
;	Music "Lamaliza" by Slawek Sledz (Slaves) of Taquart in 1998
;	Pack 2nd segment with superpacker (exomizer: depack=$b000, buffer=$b100) before adding it to the disk as LMSARSOM.xex.
;	@com.wudsn.ide.asm.outputfile=LMSARSOM-Unpacked.xex

	icl "no_name.h"

basicf	= $3f8

	opt f-

;===============================================================

	org $80	;Fixed, was $00 before

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

zc	.ds ZCOLORS

;===============================================================
	org $2000		;Loader
	
	.proc loader_init
	mva #$ff portb		;BASIC off
	mva #$01 basicf

	lda #0
	sta 559
	sta $d400		;Screen off 
	rts
	.endp

	ini loader_init

;===============================================================


	org $2000		;Demo

	opt f+			;Single chunk to optimize for Supercuncher

	.proc demo

cnt	= $14

;===============================================================

	.proc init

	.proc copy_player	;Copy player to low-mem
	ldy #>[.len player+$0ff]
	ldx #0
loop
src	lda player,x
dst	sta player.init,x
	inx
	bne loop
	inc src+2
	inc dst+2
	dey
	bne loop
	.endp

	mva #1 580		;Force coldstart because player kills DOS

	.proc clear_missles
	ldx #8
	lda #0
loop	sta pmg+$3c0,x
	dex
	bpl loop
	.endp

	jsr player.init

	rts
	.endp			;End of init

;===============================================================
	.proc vbi
	mva #>text_chr $d409
	mva #$3f dmactl
	lda #14
	sta $d017
	lda cnt
	and #3
	eor #3
	sta $d404
	cmp #3
	bne not_next_char
	inw text_lms+1
not_next_char
	cpw text_lms+1 #[text+.len text]
	bne not_last_char
	mwa #text text_lms+1
not_last_char
	inc cnt
	jsr player.play
	rts
	.endp			;End of vbi

	.align 256
	.local text
:48	.byte ' '
	.byte '   This is the 4th bonus part. It is an Atari XL/XE conversion of a wild entry by our beloved Lamers. '
	.byte 'When I saw it at the party, I was banging my head on the table shouting "will it never end?" because '
	.byte 'it was about the 100th (!!!) compo entry in a row for the evening, it was 3:15 AM and... ridiculous :-) '
	.byte 'So i decided to make a demo about it. '
	.byte 'Code by JAC!, conversion using RasterConverterBeta7 by ilmenit. '
	.byte 'Graphics "ARSOME.GIF'' by Pafnucy & Gerwazy of Lamers in 2013. '
	.byte 'Music "Lamaliza" by Slawek Sledz (Slaves) of Taquart in 1998. '
	.byte 'Now enjoy the rest of SillyPack2k13!'
	.endl
:48	.byte ' '

	.endp			;End of demo

;===============================================================

	org $2400
	.local text_chr
	ins 'Arsome-AscII.chr'
	.endl

	org $2800		;Sound module

	.local module
	ins 'Arsome-Lamaliza.sap',+$7d,$abc
	.endl

	.local player
init	= $04f5
play	= $0503
	ins 'Arsome-Lamaliza.sap',+$15b5,$0a74
	.endl

;===============================================================


	org $5010		;Rasterprogram Mode: GED- (bitmap mode)

	IFT PIC_HEIGHT>=204
scr	ins "output.png.mic", 0, 8160
	:16 .byte 0
	ins "output.png.mic" , +8160
	ELS
scr	ins "output.png.mic"
	EIF

	.ifdef nil_used
nil	:8*40 brk
	eif

	.ALIGN $0400
ant	ANTIC_PROGRAM scr,ant
	.byte $70
text_lms
	.byte $52,a(demo.text)
	dta $41,a(ant)

fnt

	ift USESPRITES
	.ALIGN $0800
	.ds $0300
pmg	SPRITES
	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 interrups
	mva #$00 nmien		;stop all interrupts
;	mva #$fe portb		;switch off ROM to get 16k more ram

	ZPINIT

////////////////////
// RASTER PROGRAM //
////////////////////

;	jmp line239
	jsr demo.init
	jmp raster_program_end

LOOP	lda vcount		;synchronization for the first screen line
	cmp #$02
	bne LOOP

	mva #%00111110 dmactl	;set new screen width
	mva <ant dlptr
	mva >ant dlptr+1

  icl "output.png.rp.ini"

;--- wait 18 cycles
	jsr _rts
	inc byt3

;--- set global offset (23 cycles)
	jsr _rts
	cmp byt3\ pha:pla

;--- empty line
	jsr wait54cycle
	inc byt2

  icl "output.png.rp"

raster_program_end

	lda >fnt+$400*$00
	sta chbase
c0	lda #$00
	sta colbak
c1	lda #$00
	sta color0
c2	lda #$00
	sta color1
c3	lda #$00
	sta color2
c4	lda #$00
	sta color3
s0	lda #$03
	sta sizep0
	sta sizep1
	sta sizep2
	sta sizep3
	mva #$ff sizem
	sta grafm
	mva #$20 hposm0
	mva #$28 hposm1
	mva #$d0 hposm2
	mva #$d8 hposm3
	mva #$02 pmcntl
	lda #$14
	sta gtictl
	
	lda #14
	sta $d40a
	sta $d01a
	sta $d019
	lda #0
	sta $d40a
	sta $d01a
	sta $d019

//--------------------
//	EXIT
//--------------------

	lda trig0		; FIRE #0
	beq stop

	lda trig1		; FIRE #1
	beq stop

	lda consol		; START
	and #1
	beq stop

	lda skctl		; ANY KEY
	and #$04
	bne skp

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
skp

//--------------------
	jsr demo.vbi
	jmp LOOP

;---

wait54cycle
	:2 inc byt2
wait44cycle
	inc byt3
	nop
wait36cycle
	inc byt3
	jsr _rts
wait18cycle
	inc byt3
_rts	rts

byt3	brk


;---

.MACRO	ANTIC_PROGRAM
	:+8 dta $4e,a(:1+$0000+#*40)
	:+8 dta $4e,a(:1+$0140+#*40)
	:+8 dta $4e,a(:1+$0280+#*40)
	:+8 dta $4e,a(:1+$03C0+#*40)
	:+8 dta $4e,a(:1+$0500+#*40)
	:+8 dta $4e,a(:1+$0640+#*40)
	:+8 dta $4e,a(:1+$0780+#*40)
	:+8 dta $4e,a(:1+$08C0+#*40)
	:+8 dta $4e,a(:1+$0A00+#*40)
	:+8 dta $4e,a(:1+$0B40+#*40)
	:+8 dta $4e,a(:1+$0C80+#*40)
	:+8 dta $4e,a(:1+$0DC0+#*40)
	:+8 dta $4e,a(:1+$0F00+#*40)
	:+8 dta $4e,a(:1+$1040+#*40)
	:+8 dta $4e,a(:1+$1180+#*40)
	:+8 dta $4e,a(:1+$12C0+#*40)
	:+8 dta $4e,a(:1+$1400+#*40)
	:+8 dta $4e,a(:1+$1540+#*40)
	:+8 dta $4e,a(:1+$1680+#*40)
	:+8 dta $4e,a(:1+$17C0+#*40)
	:+8 dta $4e,a(:1+$1900+#*40)
	:+8 dta $4e,a(:1+$1A40+#*40)
	:+8 dta $4e,a(:1+$1B80+#*40)
	:+8 dta $4e,a(:1+$1CC0+#*40)
	:+8 dta $4e,a(:1+$1E00+#*40)
;	:+4 dta $4e,a(:1+$1F40+#*40)
;	:+4 dta $4e,a(:1+$1FF0+#*40)
;	:+8 dta $4e,a(:1+$2090+#*40)
;	:+8 dta $4e,a(:1+$21D0+#*40)
;	:+8 dta $4e,a(:1+$2310+#*40)
;	:+8 dta $4e,a(:1+$2450+#*40)
.ENDM

CL

.MACRO	ZPINIT
.ENDM

ZCOLORS	= 0

;---
	run main
;---

	opt l-

.MACRO	SPRITES
	icl "output.png.pmg"
.ENDM

USESPRITES = 1

