
org 100h
        mov al,13h
        int 10h

        mov dx,3c8h
        xor ax,ax
        out dx,al
        inc dx

        mov bx,ax
pal1:
        mov al,bl
        shr al,2
        out dx,al
        shr al,1
        out dx,al
        mov al,bl
        out dx,al
        inc bx
        jnz pal1

        mov bl,64
pal2:
        dec bl
        mov al,bl
        out dx,al
        shl al,1
        out dx,al
        shl al,1
        out dx,al
        jnz pal2

; ----------- Sinegen ------------

        XOR     DI,DI
tick:
        push word 09000h
        pop ds
        push word 0a000h
        pop es

Singen:
        MOV     [DI],CH
        ADD     CX,SI
        MOV     AX,40
        IMUL    CX
        SUB     SI,DX
        INC     DI
        JNZ     Singen

main:
        add di,4
        xor si,si

        mov bl,200
yloop:
        mov cx,320
xloop:

        push si
        push di
        mov si,4
        xor bp,bp
nblobs:
        mov ax,si
        shl ax,6
        add ax,bx
;        mov al,bl
;        sub al,100
        sub al,[di]
        imul al
        mov dx,ax

        mov ax,si
        shl ax,7
        add ax,cx

;        mov al,cl
;        sub al,160
        shr di,1
        sub al,[di+64]
        imul al
        add ax,dx

        push bp
        or al,3
        mov bp,ax
        xor dx,dx
        mov ax,65535
        idiv bp
        pop bp


        shl ax,2
        add bp,ax

        dec si
        jnz nblobs

        mov ax,bp

        or ah,ah
        jz olala2

        mov ax,[cs:tick]
        or ah,ah
        jnz afterfirst
        mov al,8
        jmp olala2
afterfirst:
        and ah,3
        jz olala
        dec ah
        jz next1
        dec ah
        jz next2
        dec ah
        jz next3
next1:
        add al,bl
        add al,cl
        shl al,3
        jmp olala2
next2:
        mov di,ax
        add al,bl
        add di,cx
        add al,[di+32]
        xor al,cl
        shr al,2
        jmp olala2
next3:
        add al,[di+bx]
        add di,cx
        add al,[di+64]
        shl al,4
        shr al,3
        add al,32
        jmp olala2
olala:
        xor al,bl
        xor al,cl
olala2:
        pop di
        pop si
        mov [es:si],al

        inc si
        dec cx
        jnz xloop

        dec bx
        jnz yloop

        inc word [cs:tick]

        MOV     DX,3DAh
;        xor ah,ah
Vsync:
        IN      AL,DX
        AND     AL,8
        JZ      Vsync

        in al,60h
        dec ax
        jnz main

        mov al,03h
        int 10h
RET
