	; ##########################
	; # PLASMA  T.Landspurg 91 #
	; ##########################
	; A assembler sous devpac
	opt c+,o+,ow-

prof=5
CUSTOM = $dff000	; Registres hardware
NB_LIGNE=256
NB_LIGNE_SPR=10
NB_BYTE_LIGNE=40
tailleplan=NB_LIGNE*NB_BYTE_LIGNE
taillescr=tailleplan*prof

	opt	d+

;	incdir	"include:"
	incdir	"work:prog/devpac/include/"
	include	exec/exec_lib.i
	include	exec/types.i
	include	exec/memory.i
	include	graphics/graphics_lib.i
	include	intuition/intuition_lib.i
	include	graphics/gfxbase.i
	include	hardware/custom.i
	include libraries/dos.i
	include	struct.i

	section	plasma,CODE_C
	; DEBUT DU PROGRAMME
_dark:
	movem.l	d0-d7/a0-a6,-(a7)


	lea	b(pc),a4	; a4 POINTERA TOUJOURS SUR LA BASE!
	lea	graphname(pc),a1; Ouverture de la graphics
	clr.l	d0
	CALLEXEC	OpenLibrary
	move.l	d0,_GfxBase-b(a4)
	lea	intuiname(pc),a1
	clr.l	d0
	CALLEXEC	OpenLibrary
	move.l	d0,_IntuitionBase-b(a4)

	bsr	allocscr
	tst.l	d0
	beq	error_scr
	lea	CUSTOM,a5	; Base des coprocesseurs
;	move.w	intenar(a5),save_intena-b(a4)
;	or.w	#$c000,save_intena-b(a4)
;	move.w	#$7fff,intena(a5)
;	move.w	#$0020,dmacon(a5); controle DMA
;	move.w	#$8400,dmacon(a5) ; Bltpri
;	move.b	#%10000111,$bfd100 ; ARRET DU DRIVE (BEURK!)
	CALLGRAF	OwnBlitter
	CALLGRAF	WaitBlit
	bsr	met_cop
waitbout:			; BOUCLE PRINCIPALE
	sub.w	#1,tempo
	tst.w	tempo
	bne.s	pas_end_temp
pas_end_temp:

sync:
	btst	#0,vposr+1(a5)
	bne.s	sync
sync1:
	cmp.b	#60,vhposr(a5)	; Syncronisation
	bne.s	sync1
	btst	#6,$bfe001	; Test le bouton de la souris
	beq.s	out


	lea	CUSTOM,a5
	move.w	joy0dat(a5),d0	; recupere coord de la souris
	move.w	d0,d1
	and.w	#$ff,d0
	lsr.w	#8,d1

	bsr	aff_sprite

	moveq	#0,d0
	moveq	#0,d1
	movem.l	d1-d7/a0-a6,-(a7)
	CALLEXEC	SetSignal
	and.l	#SIGBREAKF_CTRL_C,d0
	movem.l	(a7)+,d1-d7/a0-a6
	tst.l	d0
	beq.s	waitbout
out:
	lea	CUSTOM,a5	; a5 pointera sur les registres hardware
;	move.w	save_intena-b(a4),intena(a5)	; interuption
	move.l	_GfxBase(pc),a0 ; On restore l'ancienne copperlist
	move.l	$32(a0),d0
	move.l	$26(a0),cop1lc(a5)

;	CALLINT	RethinkDisplay

	CALLGRAF	DisownBlitter
	bsr	freescr
error_scr:
	move.l	_GfxBase(pc),a1 ; On referme la libraire
	CALLEXEC CloseLibrary
	move.l	_IntuitionBase(pc),a1 ; On referme la libraire
	CALLEXEC CloseLibrary
;	move.w	#$0400,dmaconr(a5)
;	move.w	#$8020,dmaconr(a5)
	movem.l	(a7)+,d0-d7/a0-a6
	moveq	#0,d0
	rts


	; #########################
	; ROUTINES POUR LES SPRITES
	; #########################
	; d4:numsprite
aff_sprite:
	movem.l	d0-d3/d5-d7/a0-a5,-(a7)	; !!! PETITE OPTIMISATION SUR LES REGISTRES

	move.l	bitplane0(pc),a1 ; destination
	lea	motif,a0	; source

	mulu	#(NB_BYTE_LIGNE),d1 ; y

	move.l	d0,d3		; x
	and.w	#$0f,d0		; partie basse=decalage raster
	ror.w	#4,d0		; rotation (circulaire)=lsl.w #12,d7

	lsr.l	#3,d3		; en octets
	bclr	#0,d3	; partie haute
	add.w	d3,d1
	lea	(a1,d1.w),a1	; adresse de pour la deuxieme partie
	move.w	d0,d3
;	or.w	#$0fca,d3
	or.w	#$09f0,d3

	move.w	d3,bltcon0(a5)	; controle
	move.w	d0,bltcon1(a5)
	move.w	#NB_BYTE_LIGNE-4,bltdmod(a5)	; modulos pour le masque
	clr.w	bltamod(a5)
	move.l	#$ffff0000,bltafwm(a5)
	bsr	waitblit

	move.l	a0,bltapt(a5)
	move.l	a1,bltdpt(a5)
	move.w	#NB_LIGNE_SPR<<6+2,bltsize(a5)	; depart
	lea	(a0,d0.w),a0
	lea	(a1,d1.w),a1
	movem.l	(a7)+,d0-d3/d5-d7/a0-a5	; !!! FAUT DESEMPILER LES MEMES REGISTRES
	rts




	; Allocation memoire pour les buffers ecran
allocscr:
	move.l	#taillescr,d0	; 2 ecrans car double buffering
	move.l	#MEMF_CLEAR|MEMF_CHIP,d1
	CALLEXEC AllocMem
	move.l	d0,bitplane0
	rts
	; desallocation memoire des buffers
freescr:
	move.l	bitplane0(pc),a1
	move.l	#taillescr,d0
	CALLEXEC FreeMem
	rts

met_cop:
	move.l	bitplane0,a5
	bsr	initbrush		; on change la copperlist
	move.l	_GfxBase(pc),a0		; on reaffiche la copperlist
	move.l	#mycop,$32(a0)
	lea	CUSTOM,a5
	move.l	#mycop,cop1lc(a5)
	clr.w	copjmp1(a5)
	rts

	; MET DANS LA COPPER LIST LES ADRESSES DES BITPLANES

initbrush:			; d0 contient l'adresse du prem plan
	lea	p_bitpl,a0
	move.w	#$00e0,d1	; adresses de bitp0h
	move.w	#prof-1,d2
ibrush:	
	move.l	a5,d0
	bsr	metadr
	bsr	metadr
	add.l	#NB_LIGNE*NB_BYTE_LIGNE,a5
	dbf	d2,ibrush
	rts

metadr:				; cet ici qu'on le met dans la coplist
	swap	d0
	move.w	d1,(a0)+
	move.w	d0,(a0)+
	addq.w	#2,d1
	rts

waitblit:
	btst	#14,dmaconr(a5)
	bne.s	waitblit
	rts
b:
oldcop:	dc.l	0
bitplane0:dcb.l	1
flagcop:dc.b	0

save_intena:dc.w	0
waitblit:macro
waitblit\@:
	btst	#14,dmaconr(a5)
	bne	waitblit\@
	endm
graphname:
	GRAFNAME
intuiname:
	INTNAME
	even
tempo:dc.w	100
_IntuitionBase:dc.l	0
_GfxBase:
	dc.l	0

	section	coplist,DATA_C
mycop:
	dc.w	$1081,$fffe
	dc.w	$180,0,$182,$ff
	dc.w	$008e,$2581
	dc.w	$0090,$40c1
	dc.w	$0092,$0038
	dc.w	$0094,$00cc
	dc.w	$102,0
	dc.w	$108,0
	dc.w	$10a,0
	dc.w	$009c,$8010
	dc.w	$3101,$fffe
	dc.w	$0100,prof<<12
p_bitpl
	dcb.w	prof*4,0
	dc.l	$f0dffffe
	dc.l	$01000000
	dc.l	$fffffffe
fincop4:

motif:
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
	dc.w	$ffff,$ffff
