; TITANIC CRUNCHER
	opt	c-
I_setclr=$8000
I_inten=$4000
I_ciab=$2000
I_aud=$3c0
I_Blit=$40
I_Vbl=$20
I_Cop=$10
I_Kb=8
I_Soft=4
I_Disk=2

D_setclr=$8000
D_init=$200
D_priB=$400
D_Btpl=$100
D_Copp=$80
D_Blit=$40
D_sprt=$20
D_disk=$10

I_SET=I_setclr!I_inten
D_CON=D_setclr!D_init!D_copp!D_Btpl!$3

	section	titanic,code_c

start:
	bra	start2

	dc.l $ABCD		; magic
file:	ds.l 4

DosName	dc.b 'dos.library',0

ptBufLoad	
	dc.l 0
ptNextBufLoad
	dc.l 0
wbport	dc.l 0

hunktab	dc.l $10374

;TitanicsMessage:
;	dc.b 'TEAM17 present FULL CONTACT'
;	even

MsgPort	dc.l 0
; ---------------------------------------------------------------------------

start2:
	movem.l	d0-a3/a5-a6,-(sp)	; save all registers but A4 (jump program)

	suba.l	a1,a1		; own task
	movea.l	4.w,a6
	jsr	-$126(a6)	; FindTask(name) (a1)
	movea.l	d0,a4
	tst.l	$AC(a4)		; pr_cli
	bne.s	DosCLI
	lea	$5C(a4),a0
	lea	wbport(pc),a1
WB:
	move.l	a0,(a1)
	movea.l	4.w,a6
	jsr	-$180(a6)	; WaitPort(port) (a0)
	jsr	-$174(a6)	; GetMsg(port) (a0)
	lea	MsgPort(pc),a1
	move.l	d0,(a1)

DosCLI:
	lea	DosName(pc),a1	; "dos.library"
	moveq	#0,d0
	movea.l	4.w,a6
	jsr	-$228(a6)	; OpenLib
	movea.l	d0,a5		; DosBase
	bne.s	okDOSLIB	; OutPut()
	movem.l	(sp)+,d0-a3/a5-a6
	moveq	#0,d0
	rts	
; --------------------------------- MESSAGE

okDOSLIB:
;	jsr	-$3C(a5)	; OutPut()
;	move.l	d0,d1
;	beq.s	noOutputCli
;	lea	TitanicsMessage(pc),a0 ; "TITANICS-Cruncher	decrunches while load"...
;	move.l	a0,d2
;	tst.b	(a0)
;	beq.s	noOutputCli
;.lenmess
;	tst.b	(a0)+
;	bne.s	.lenmess
;	move.b	#$A,-1(a0)
;	move.l	a0,d3
;	sub.l	d2,d3
;	jsr	-$30(a5)	; Write(file,buffer,len) (d1,d2,d3)

noOutputCli:
	bset    #1,$BFE001		; Filter PassBass - Power Led OFF

	lea	CopperListBoot,a0
	move.l	a0,$dff080
	move.w	#0,$dff088
; pic
	move.l	#bootpic,d0	; 2 plans 40*48
	lea	CLbpl,a0
	moveq	#2-1,d1
	move.l	#40*48,d2
	bsr	Inst_plans

	move.l	#blanksnd,d0	; sprites off
	lea	CLspr,a0
	moveq	#8-1,d1
	move.l	#0,d2
	bsr	Inst_plans

	move.w	#$20,$dff096	; sprites off
	move.w	#D_CON,$dff096
	move.w	#$c080,$dff09a		; start interrupt audio
; sound	channel 0
	move.l	#bootsnd,$dff0A0	; address sound
	move.w	#61000/2,$dff0A4	; len in word $7724=30500.w=61000 .b (ee48)
	move.w	#428,$dff0A6		; period
	move.w	#64,$dff0A8		; volume
 ; sound channel 1
	move.l	#bootsnd,$dff0B0	;
	move.w	#61000/2,$dff0B4
	move.w	#428,$dff0B6
	move.w	#64,$dff0B8
 
	move.w	#$8003,$dff096		; start dma audio on

;-------------------------------- START READ/DEPACK DATAS
Load	move.l	file(pc),d1
	moveq	#0,d2
	move.l	hunktab(pc),d2	; position
	moveq	#-1,d3		; offset : end
	jsr	-$42(a5)	; Seek(file,position,offset) (d1,d2,d3)
; 
	move.l	#$1400,d0	; bytesize of BUFLOAD read/unpack
	moveq	#0,d1
	jsr	-$C6(a6)	; AllocMem(bytesize,req) (d0,d1)
; 
	lea	start(pc),a4	; Prog Base
	move.l	d0,ptBufLoad-start(a4)
	bne.s	continue
	movea.l	a5,a1
	jsr	-$19E(a6)	; no BufLoad : Close Dos Lib
	movem.l	(sp)+,d0-a3/a5-a6
	moveq	#0,d0
	rts	
; 
continue:
	addi.l	#$1400,d0
	move.l	d0,ptNextBufLoad-start(a4)

	bsr	DosRead

	move.l	(a0)+,d7
	move.l	d7,d6
	movea.l	$FFFFFFFC,a1
	adda.l	a1,a1
	adda.l	a1,a1
	move.l	a1,-(sp)

_loop0:
	move.l	(a0)+,d0
	cmpa.l	ptNextBufLoad(pc),a0
	bne.s	ok
	bsr	DosRead

ok:
	move.l	d0,d1
	andi.l	#$FFFFFF,d0
	addq.l	#8,d0
	move.l	d0,d5
	rol.l	#8,d1
	andi.l	#7,d1
	addi.l	#$10000,d1
	movem.l	d5-a1,-(sp)
	jsr	-$C6(a6)
	movem.l	(sp)+,d5-a1
	tst.l	d0
	beq	EXITNOMEM

	addq.l	#4,d0
	move.l	d0,d1
	lsr.l	#2,d0
	move.l	d0,(a1)
	movea.l	d1,a1
	move.l	d5,-4(a1)
	dbf	d6,_loop0
	clr.l	(a1)
	movea.l	(sp)+,a4

LOOP:
	movea.l	(a4),a4
	adda.l	a4,a4
	adda.l	a4,a4
	move.l	(a0)+,d0
	cmpa.l	ptNextBufLoad(pc),a0
	bne.s	noread
	bsr	DosRead

noread:
	tst.l	d0
	bne.s	dpack
	dbf	d7,LOOP
	bra.s	EndRead
; 

dpack:
	lea	4(a4),a1
	move.l	-8(a1),d0
	subq.l	#8,d0
	tst.l	d0
	beq.s	unpacked
	bsr	Depack

unpacked:
	move.l	(a0)+,d0
	cmpa.l	ptNextBufLoad(pc),a0
	bne.s	_skipread0
	bsr	DosRead

_skipread0:
	tst.l	d0
	beq.s	_next
	movea.l	$FFFFFFFC,a3
	adda.l	a3,a3
	adda.l	a3,a3
	move.l	(a0)+,d1
	cmpa.l	ptNextBufLoad(pc),a0
	bne.s	_skipread1
	bsr	DosRead

_skipread1:
	movea.l	(a3),a3
	adda.l	a3,a3
	adda.l	a3,a3
	dbf	d1,_skipread1
	move.l	a3,d2
	addq.l	#4,d2

looptest:
	move.l	(a0)+,d1
	cmpa.l	ptNextBufLoad(pc),a0
	bne.s	_skipread
	bsr	DosRead

_skipread:
	add.l	d2,4(a4,d1.l)
	subq.l	#1,d0
	bne.s	looptest
	bra.s	unpacked
; 

_next:
	dbf	d7,LOOP
;-------------------------------------------------
EndRead:
	movea.l	ptBufLoad(pc),a1
	move.l	#$1400,d0
	jsr	-$D2(a6)	; FreeMem
	movea.l	a5,a1
	jsr	-$19E(a6)	; Close	DosLib
	movea.l	$FFFFFFFC,a0
	adda.l	a0,a0
	adda.l	a0,a0
	movea.l	(a0),a4		; get unpacked offset
	adda.l	a4,a4
	adda.l	a4,a4
	addq.l	#4,a4
	movea.l	wbport(pc),a0
	lea	$14(a0),a0	; memory list
	movea.l	MsgPort(pc),a1
	move.l	a1,d0
	beq.s	startprog
	movea.l	4.w,a6
	jsr	-$F0(a6)	; AddHead(list,node) (a0,a1)
;---------------------------------------------------
startprog:
.wait	andi.w	#$80,$dff01e		; interrupt channel 0 ?
	bne.s	.wait

	move.l	#blanksnd,$dff0A0
	move.l	#blanksnd,$dff0B0
	move.w	#2,$dff0A4
	move.w	#2,$dff0B4

	move.w	#$3,$dff096		; start dma audio off
	move.w	#$80,$dff09a		; stop interrupt audio

	movem.l	(sp)+,d0-a3/a5-a6

	jmp	(a4)			; START UNPACKED PROGRAM
	
; 

EXITNOMEM:
	addq.l	#4,sp
	clr.l	(a1)
	movem.l	(sp)+,d0-a3/a5-a6
	rts

; --------------------------------------------------------------------------- READ PACKED DATA
DosRead:
	movem.l	d0-d7/a1-a6,-(sp)

	move.l	file(pc),d1
	move.l	ptBufLoad(pc),d2
	move.l	#$1400,d3
	jsr	-$2A(a5)	; read (file,buf,len) (d1,d2,d3)

	movem.l	(sp)+,d0-d7/a1-a6
	movea.l	ptBufLoad(pc),a0
	rts
; ----------------------------------------------------------------------------- DEPACKER
Depack:
		lea	(a1,d0.l),a2
		moveq	#0,d0

loc_0_260:
		lsl.l	#1,d0
		bne.s	loc_0_268
		bsr.w	pread

loc_0_268:
		bcs.s	loc_0_2A2
		lsl.l	#1,d0
		bne.s	loc_0_272
		bsr.w	pread

loc_0_272:
		bcs.s	loc_0_28C
		moveq	#2,d1
		bsr.s	sub_0_2E8
		move.w	d2,d3

loc_0_27A:
		moveq	#7,d1
		bsr.s	sub_0_2E8
		nop	
		move.b	d2,(a1)+
		dbf	d3,loc_0_27A
		cmpa.l	a1,a2
		bgt.s	loc_0_260
		rts	

loc_0_28C:
		moveq	#7,d1
		bsr.s	sub_0_2E8
		addq.w	#2,d2
		neg.w	d2
		move.b	(a1,d2.w),(a1)+
		move.b	(a1,d2.w),(a1)+
		cmpa.l	a1,a2
		bgt.s	loc_0_260
		rts	

loc_0_2A2:
		moveq	#1,d1
		bsr.s	sub_0_2E8
		cmp.w	#2,d2
		bgt.s	loc_0_2DE
		beq.s	loc_0_2CC
		moveq	#8,d1
		add.w	d2,d1
		moveq	#2,d3
		add.w	d2,d3
		bsr.s	sub_0_2E8
		add.w	d3,d2
		addq.w	#1,d2
		neg.w	d2

loc_0_2BE:
		move.b	(a1,d2.w),(a1)+
		dbf	d3,loc_0_2BE
		cmpa.l	a1,a2
		bgt.s	loc_0_260
		rts	

loc_0_2CC:
		moveq	#7,d1
		bsr.s	sub_0_2E8
		move.w	d2,d3
		addq.w	#2,d3
		moveq	#$B,d1
		bsr.s	sub_0_2E8
		addq.w	#3,d2
		neg.w	d2
		bra.s	loc_0_2BE

loc_0_2DE:
		moveq	#7,d1
		bsr.s	sub_0_2E8
		move.w	d2,d3
		addq.w	#8,d3
		bra.s	loc_0_27A

; 


sub_0_2E8:
		moveq	#0,d2

loc_0_2EA:
		lsl.l	#1,d0
		bne.s	loc_0_2F0
		bsr.s	pread

loc_0_2F0:
		roxl.w	#1,d2
		dbf	d1,loc_0_2EA
		rts	
; 

pread:
		move.l	(a0)+,d0
		cmpa.l	ptNextBufLoad(pc),a0
		bne.s	_noread
		bsr	DosRead

_noread:
		move	#$10,ccr
		roxl.l	#1,d0
		rts	
; end of 'CODE'

;-------------------------------------------------------------------
Inst_plans:		
	move.w	d0,6(a0)
	swap	d0			
	move.w 	d0,2(a0)
	swap	d0
	tst.w	d1
	beq.s	.fin
	add.l	d2,d0
	addq.l	#8,a0
	subq.w	#1,d1
	bra.s	inst_plans
.fin	rts


CopperListBoot
	dc.w	$96,$20
	dc.w	$96,D_CON

	dc.w	$8e,$9381,$90,$c3c1
	dc.w	$92,$38,$94,$d0
	dc.w	$100,$2200
	dc.w	$108,0,$10a,0
	dc.w	$102,0,$104,0

CLspr	dc.l	$1200000,$1220000
	dc.l	$1240000,$1260000
	dc.l	$1280000,$12a0000
	dc.l	$12c0000,$12e0000
	dc.l	$1300000,$1320000
	dc.l	$1340000,$1360000
	dc.l	$1380000,$13a0000
	dc.l	$13c0000,$13e0000

CLbpl	dc.l	$E00000,$E20000	; 1
	dc.l	$E40000,$E60000	; 2
	
	dc.l	$1800000,$1820CCC
	dc.l	$1840888,$1860333

	dc.l -2

;---- 20k
bootpic	incbin	"pics/fcload.rw"	; 40*48*2
bootsnd	incbin	"fcsound.rw"	; "team 7 presents" 44*512=22528
blanksnd ds.l	4
