;         EXEMPLE LIVRE SANS GARANTIES AVEC LE TEXTE SUR L'ANIMATION
;                            - Version 320x400 -
;         NB :     Ce source n'est ‚videment pas un exemple...
.386
%out Test video en cours d'assemblage...
SpriHaut=44
SpriLong=25
SpriTaille=25*44
TailleProg=1000h 	; … tout casser...
data	segment use16
palette	db "For RixedPatcher d7.cel",0
	dd 32
	dw 768
	db 768-($-offset palette) dup (?)
sprite	db "For RixedPatcher d7.cel",0
	dd 800
	dw SpriTaille
	db SpriTaille-($-offset sprite) dup (?)
Mess0	db "Pas assez de RAM libre !?!",0
data	ends

code	segment use16
	assume cs:code,ss:code,ds:data
	locals
	; quelques variables...
bxparam	dw ?
accx	dw 0
accy	dw 0
x	dw 70
y	dw 90*80
old_adr	dw ?
	
start	proc far
	mov ax,cs
	mov ss,ax
	mov sp,offset SommetPile
	mov bx,TailleProg
	mov ah,4ah
	int 21h
	xor ax,ax		; pas de 2nd fenetre
	mov bl,80		; et offset normal
	call Init320x400
	mov dx,3c8h
	xor al,al
	out dx,al
	inc dx
	mov ax,data
	mov ds,ax
	mov si,offset palette
	mov cx,768
	rep outsb
	mov ax,0a000h
	mov es,ax
	mov si,offset sprite
	mov LongSpri,SpriLong
	mov HautSpri,SpriHaut
	call Unplan&Swap
	jnc @@1
	mov ax,3
	int 10h
	mov dx,offset Mess0
	mov ah,9
	int 21h
	jmp EXITERR
@@1:	mov bxparam,bx
	mov HautSpri,SpriHaut
	mov OffsetLine,80

	; Bon, voila qui est fait. Et maintenant, on improvise un fond...
TraceFond:
	xor ax,ax
	mov di,ax
	cld
	mov dx,3ceh		; mode lecture 0 et ecriture 0
	mov ax,4005h
	out dx,ax
	mov dl,0c4h		; MapMask=1111b (tous les plans)
	mov ax,0f02h
	out dx,ax
	
	mov bx,400
@@0:	mov cx,80			; Trace des lignes de couleur
	rep stosb
	inc al
	and al,11111b
	dec bx
	jnz @@0

	mov ax,es			; on recopit sur la page 2
	mov ds,ax
	mov ax,0a7d0h
	mov es,ax
	xor di,di
	mov si,di
	mov dx,3ceh		; mode lecture 0 et ecriture 1
	mov ax,4105h
	out dx,ax
	mov cx,32000
	rep movsb
	
	; Pas besoin du bios pour gerer le clavier :
	
	;cli

	; Et c'est partit pour le fun !!!
Boucle:
	; Capture du fond sur l'autre ecran :
	push ds es
	mov ax,0afa0h	; es pointe sur le rab de vram
	mov es,ax
	pop ds		; ds pointe sur l'autre ecran
	xor di,di
	mov si,y		; si=adresse du sprite en vram
	mov ax,x
	shr ax,2
	add si,ax
	mov old_adr,si
	mov cx,7
	mov bx,44
	call SaveVram2Vram
	push ds
	pop es ds
	
	; Modification des coordonnees :
	cmp x,160
	ja @@2
	add accx,2
@@2:	dec accx
	cmp y,200*80
	ja @@3
	add accy,2*80
@@3:	sub accy,1*80

	mov ax,accx
	add x,ax
	mov ax,accy
	add y,ax

	; Affiche le sprite dans l'autre ecran :
	mov bx,bxparam
	mov ax,data
	push ds
	mov ds,ax
	mov ax,x
	mov di,y
	mov si,offset sprite
	call AffRam2Vram

	; Change d'ecrant
	mov dx,3dah		; tit' synchro...
	in al,dx
	test al,8
	jnz $-3

	mov dl,0d4h		; calcul de la nouvelle adresse...
	mov ax,es			; a partir de es
	mov ds,ax
	shl ax,4
	mov al,0ch
	out dx,ax

	mov dl,0dah
	in al,dx
	test al,8
	jz $-3


	pop es	; et hop ! les registres de segment sont echanges !

	; Efface l'ancien sprite de l'autre ecrant :
	push ds
	mov ax,0afa0h	; ds:si pointe sur le rab de vram
	mov ds,ax
	xor si,si
	mov di,old_adr	; es:di=adresse du sprite en vram
	mov cx,7
	mov bx,44
	call RestoreVram2Vram
	pop ds

	; Boucle :
	in al,60h
	dec al
	jz EXIT
	jmp Boucle

start	endp


;ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
;º ROUTINES D'AFFICHAGES EN MODES 256 COULEURS REUNIES POUR ACCES AU DEBUT 94 º
;ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¹
;º                   ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿                     º
;ÓÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ Separation des plans d'un sprite ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ½
;                    ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
UnPlan&Swap Proc near
	; entree : ds:si -> sprite
	;          LongSpri, HautSpri...
	; sortie : bx=parametre du sprite
	;          si Cf=1 ou bx=-1 => ERROR, pas assez de mem... :-(
	push si di es ax cx dx
	mov ax,LongSpri
	mov dl,HautSpri
	xor dh,dh
	mul dx
	push ax
	mov bx,ax
	shr bx,4
	add bx,3
	mov ah,48h
	int 21h
	jnc @@0
	mov bx,-1
	pop ax
	pop dx cx ax es di si
	ret
@@0:	mov es,ax
	xor di,di
	push si
	call UnPlan
	push es
	push ds
	pop es
	pop ds
	xor si,si
	pop di
	pop cx
	rep movsb
	push ds
	push es
	pop ds
	pop es
	mov ah,49h
	int 21h
	clc
	pop dx cx ax es di si
	ret
UnPlan&Swap endp	

UnPlan	Proc near
	; entree : ds:si -> avant ds:di -> apres
	;          LongSpri, HautSpri...
	; sortie : bx=parametre a envoyer a AffRam2Vram
	push ax cx dx si di
	mov @@DebutSpri,si
	mov @@Plan,0
	cld
@@0:
	mov cl,HautSpri
	mov ax,@@DebutSpri
@@1:
	add ax,LongSpri
@@2:
	movsb
	add si,3
	cmp si,ax
	jb @@2
	mov si,ax
	add si,@@Plan
	dec cl
	jnz @@1
	inc @@Plan
	mov si,@@Plan
	add si,@@DebutSpri
	cmp @@Plan,4
	jnz @@0

	mov ax,LongSpri	; Calcul de Bl et Bh
	mov dl,4
	div dl
	xchg ah,al
	or al,0
	jnz @@3
	dec ah
	mov al,4
@@3:	inc ah
	mov bx,ax
	pop dx cx ax di si
	ret
LongSpri	dw ?
HautSpri	db ?
@@DebutSpri dw ?
@@Plan	dw ?
Unplan	endp

;                          ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ Affichage du sprite ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;                          ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

AffRam2Vram	proc near
	; entree : HautSpri, bl=n 1ers plans de longueur bh, ax=coord x...
	;          ...en points, di=coord y en points*offset ligne
	;          ds:si -> le sprite en ram et es->video
	push ax
	mov dx,3ceh		; mode lecture 0 et ecriture 0
	mov ax,4005h
	out dx,ax
	pop ax
	mov @@plan,4		; variable memoire pour compter les plans
	mov cx,ax			; calcul du masque dans cl
	and cl,11b
	shr ax,2			; @@adr_dep_spri <- adresse finale
	add di,ax
	mov @@adr_dep_spri,di
	mov dx,3c4h		; selection du MapMask
	mov al,2
	out dx,al
	inc dx
	mov ah,1			; preparation du masque
	shl ah,cl
	mov al,ah			; masque dans ah (doubl‚)
	shl al,4
	add ah,al
@@nxtpln:	mov al,ah			; on envoit le masque
	out dx,al
	mov ch,HautSpri
	EVEN
@@nxtln:	mov cl,bh
	mov bp,di			; bp sauve l'adresse du debut de ligne
	EVEN
@@nxtpt:	lodsb
	or al,al
	jz @@0
	mov es:[di],al
@@0:	inc di
	dec cl			; cl compte la longueur de ce plan
	jnz @@nxtpt
	mov di,bp			; si fin de ligne -> retours au debut...
	add di,OffsetLine			; ...de la ligne suivante
	dec ch			; ch compte la hauteur du sprite
	jnz @@nxtln
	dec @@plan		; et on boucle sur les 4 plans
	jz @@exit
	rol ah,1			; si fin du plan, calcul du nouveau masque
	adc @@adr_dep_spri,0	; et repositionnement si d‚passement
	mov di,@@adr_dep_spri
	dec bl			; passe au plan suivant
	jnz @@1
	dec bh			; si les n premiers sont pass‚s, dec...
@@1:	jmp @@nxtpln		; ...la longueur des plans
@@exit:	ret
@@adr_dep_spri	dw ?
OffsetLine	dw 80
@@plan		db ?
AffRam2Vram	endp

;                     ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ Initialisation des modes video ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;                     ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

Init320x200	proc near
	push dx di cx es ax
	mov byte ptr OffsetLine,bl	; initialise OffsetLine
	push ax
	mov ax,13h
	int 10h
	mov dx,3ceh
	mov ax,4005h
	out dx,ax
	mov ax,0506h
	out dx,ax	
	mov dl,0c4h
	mov ax,0604h
	out dx,ax
	mov ax,0a000h
	mov es,ax
	xor di,di
	mov ax,di
	mov cx,8000h
	rep stosw
	mov dl,0d4h
	mov ax,0014h
	out dx,ax
	mov ax,0E317h
	out dx,ax
	mov ah,bl
	shr ah,1
	mov al,13h
	out dx,ax
	pop cx
	or cx,cx
	jz @@0
	mov al,cl		; 8 1er bits d'offset
	mov ah,18h
	out dx,ax
	mov al,11h	; deprotege reg 7
	out dx,al
	inc dx
	in al,dx
	mov cl,al
	and al,1111111b
	out dx,al
	dec dx
	mov al,7		; ecriture du bit 8
	out dx,al
	inc dx
	in al,dx
	test ch,1
	jz @@1
	or al,10000b
	jmp @@2
@@1:	and al,11101111b
@@2:	out dx,al
	dec dx
	mov al,11h	; reprotege les registres
	mov ah,al
	out dx,ax
	mov al,9		; ecriture du bit 9 (0 car cache au debut)
	out dx,al
	inc dx
	in al,dx
	or al,01000000b
	out dx,al
@@0:	pop ax es cx di dx
	ret
Init320x200	endp

Init320x400	proc
	call Init320x200
	push ax dx
	mov dx,3d4h
	mov ax,4009h
	out dx,ax
	pop dx ax
	ret
Init320x400	endp

;                    ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ Sauvegarde / Restauration du fond ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;                    ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

SaveVram2Vram	proc near
	mov dx,3ceh
	mov ax,4105h	; r0w1
	out dx,ax
	mov dl,0c4h
	mov ax,0f02h
	out dx,ax
	mov dx,cx
@@0:	mov cx,dx
	rep movsb
	add si,OffsetLine
	sub si,dx
	dec bx
	jnz @@0
	ret
SaveVram2Vram	endp

RestoreVram2Vram	proc near
	mov dx,3ceh
	mov ax,4105h	; r0w1
	out dx,ax
	mov dl,0c4h
	mov ax,0f02h
	out dx,ax
	mov dx,cx
@@0:	mov cx,dx
	rep movsb
	add di,OffsetLine
	sub di,dx
	dec bx
	jnz @@0
	ret
RestoreVram2Vram	endp

EXIT:	mov ax,3
	int 10h
EXITERR:	mov ax,4c00h
	int 21h
SommetPile=$+200
code	ends
end	start
