.286
.model small
.code
ORG 100h
start:
		mov ax, 13h
		int 10h
		call MakePal
		call WriteLines
lu:
        mov ah, 06h
        mov dl, 0ffh
        int 21h
        jnz  GAMEOVER
        call AdvPal
        PUSH 5
        call Coolwait
        jmp lu
GAMEOVER:
		mov ax,3
		int 10h
		int 20h
; End of main proc
;-------------------------------
	MakePal proc near
		pusha
		mov cx, 128
colr:
		mov dx, 3c6h		;Mask
		mov al, 0FFh
		out dx, al  

		mov dx, 3c8h		; Write index
		mov al, Col
		out dx, al

		mov dx, 3c9h		; Out data
		mov al, RCol		; R
		out dx, al

		mov al, GCol       	; G
		out dx, al

		mov al, BCol        ; B
		out dx, al	

		cmp byte ptr Col, 63
   		ja	GIn1
   		je lp1end
		inc RCol    	
	    	jmp lp1End
GIn1:   	dec RCol
		inc GCol
lp1End:		inc Col
		loop colr

        	inc RCol
		dec Gcol
		mov cx, 128
colB:
		mov dx, 3c6h		;Mask
		mov al, 0FFh
		out dx, al

		mov dx, 3c8h		; Write index
		mov al, Col
		out dx, al

		mov dx, 3c9h
		mov al, RCol
		out dx, al
		mov al, GCol
		out dx, al
		mov al, BCol
		out dx, al
		cmp byte ptr Col, 0BFh
   		ja	GIn2
   		je lp2end
		inc BCol
	   	dec GCol
    	jmp lp2End
GIn2:   dec BCol
lp2End:	inc Col
	loop colB	
	popa
	ret
	Col	 db 0
	RCol db 0
	GCol db 0
	Bcol db 0
	endp
;{--------------------------------------}
	WriteLines proc near
		pusha
        mov ax, 0A000h
        mov es, ax
        mov di, 0
        mov ax, 0000h
        mov cx, 200
wrlp1:  push cx
		mov cx, 160
		repe stosw
		pop cx
		inc ah
		inc al
		loop wrlp1

		popa	
	ret
	endp
;/---------------
;|
;\---------------
AdvPal proc near
	pusha
		mov dx, 3c7h		; Read index
		mov al, 0FEh
		out dx, al
		mov dx, 3c9h
		in al,	dx
		mov Rlast, al
		in al,	dx
		mov Glast, al
		in al,	dx
		mov Blast, al
; Save last values
		mov cx, 0FEh
; Main cirlce
adV:	mov dx, 3c7h		; Read index
		mov al, cl
		dec al
		out dx, al
		mov dx, 3c9h
		in al,	dx
		mov curR, al
		in al,	dx
		mov curG, al
		in al,	dx
		mov curB, al
; Read last value, and write it to next
		mov dx, 3c8h		; Write index
		mov al, cl
		out dx, al
		mov dx, 3c9h
		mov al, curR
		out dx, al
		mov al, curG
		out dx, al
		mov al, curB
		out dx, al
		dec cx
		cmp cx, 1
		jae  adv

		mov dx, 3c8h		; Write index
		mov al, 1h
		out dx, al
		mov dx, 3c9h
		mov al, Rlast
		out dx, al
		mov al, Glast
		out dx, al
		mov al, Blast
		out dx, al
		popa
	ret
	curR	db 0
	curG	db 0
	curB	db 0 
	Rlast 	db 0
	Glast 	db 0
    Blast 	db 0
endp

CoolWait        proc    near	; CoolWait(word delay);
        push bp
        mov bp, sp
        push cx
        mov cx, word ptr [bp+4]
ggg:    push cx
        mov cx, 0
        loop $
        pop cx
        loop ggg
        pop cx
        pop bp
ret 2
endp
end start
