height EQU 200

[BITS 16]               
[ORG 0x0100]            

[SEGMENT .text]		;Main code segment
  mov al, 13h
  int 10h

    mov cx, 254            ;254 colors to cycle
    mov dx, 03c8h          ;port number
    pal_setter:
      mov al, cl           ;move current color to AL
      out dx, al           ;out to 0c38h to start palette receive data
      inc dx               ;port 0c39h, palette data
      shr al, 2            ;divide by 4
      out dx, al           ;send red
      xor al, al           ;
      out dx, al           ;send green
      out dx, al           ;send blue  (0)
      dec dx               ;back to original port
    loop pal_setter

  call clearbuffer
  mainloop:
    cmp byte [vx], 1
    jne DECVX
    inc word [x]
    jmp DONEVX

    DECVX:
    dec word [x]

    DONEVX:
    cmp byte [vy], 1
    jne DECVY
    inc word [y]
    jmp DONEVY

    DECVY:
    dec word [y]

    DONEVY:
    cmp byte [x], 2      ;if the X is too low,make the vx 1
    jne dontclipbottomX
    mov byte [vx], 1
    dontclipbottomX:

    cmp byte [x], 250   ;if the X is too high, make the vx 0
    jne dontclipmaxX
    mov byte [vx], 0
    dontclipmaxX:

    cmp byte [y], 2     ;too low Y make it increment
    jne dontclipbottomY
    mov byte [vy], 1
    dontclipbottomY:

    cmp byte [y], 180
    jne dontclipmaxY
    mov byte [vy], 0
    dontclipmaxY:

    call blitsprite

    in      al,60h       ;was escape pressed???
    cmp     al,1         ;lets check
  jne mainloop         ;if NOT, keep waiting...else...screw it

exit:
  mov ax, 3h
  int 10h
  ret

clearbuffer:
  mov ax, cs
  mov es, ax
  mov di, buffer
  mov cx, 32000
  xor ax, ax
  rep stosw
  ret
  
blitsprite:
  mov dx, cs

  xor cx, cx
  xor ax, ax
  mov al, byte [y]
  shl ax, 6
  add cx, ax
  mov al, byte [y]
  shl ax, 8
  add cx, ax
  add cx, word [x]

  mov ds, dx
  mov si, sprite

  mov es, dx
  mov di, buffer
  add di, cx
  mov bx, 10
  outloop:
    mov cx, 5
    rep movsw
    add di, 310
    dec bx
    cmp bx, 0
  jne outloop

  mov es, dx
  mov di, buffer+64000-320
  mov al, byte [seed]
  cbw
  add di, ax
  mov al, byte [seed]
  stosb

  mov di, buffer+320
  fireloop:
    mov al, byte [es:di]
    cmp al, 0
    je skipcopyabove    
    dec al
    mov byte [es:di-320], al   ;copy to above
    skipcopyabove:
    mov al, byte [es:di-1]
    shr al, 2
    mov bl, al
    mov al, byte [es:di+1]
    shr al, 2
    add bl, al
    mov al, byte [es:di+320]
    shr al, 2
    add bl, al
    mov al, byte [es:di]
    shr al, 2
    add bl, al
    cmp bl, 0
    je skipdecbl
    dec bl
    skipdecbl:
    mov byte [es:di], bl
    inc di
    cmp di, buffer+64000-1
  jne fireloop

  mov ds, dx
  mov si, buffer
  mov ax, 0xa000
  mov es, ax
  xor di, di
  mov cx, (height*160)
  rep movsw

  xor byte [seed], 150
  sub byte [seed], 5
ret

sprite:  ;this is the hardcore of our program.  it is what makes it tick.
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246
  DB 255, 244, 233, 222, 211, 250, 249, 248, 247, 246


vx DB 1
vy DB 0
x  DB 128
o  DB 0    ;needed for overflow
y  DB 100
seed DB 10

[SEGMENT .bss]		;Uninitialised data segment
buffer: times (height*320) db


