height EQU 80
maxcolors EQU 255
imageY EQU 120
textline EQU 2

[BITS 16]
[ORG 0x0100]            

  mov al, 13h
  int 10h

    mov dx, 03c8h          ;port number
    xor cl, cl
    palloop:
      mov al, cl
      out dx, al
      inc dx
      shr al, 2
      out dx, al       ;/4 for red
      xor al, al
      cmp cl, 128
      jb skipaddgreen
        mov al, cl
        sub al, 128
        sar al, 2
      skipaddgreen:
        out dx, al      ;green
      xor al, al
      out dx, al      ;blue
      dec dx
      inc cl
      cmp cl, 254
    jne palloop

  mov word [greetoffset], greets

  mov ax, cs       ;clear the buffer
  mov es, ax
  mov di, buffer
  mov cx, (height*160)
  xor ax, ax
  rep stosw

  mainloop:

  cmp byte [delay], 0
  jne skipdraw

    call drawnextchar

  skipdraw:
  dec byte [delay]

  mov al, byte [imagedir]
  add byte [imageoffset], al

  mov dx, 0xa000
  mov es, dx
;  xor ah, ah
;  mov al, byte [imageoffset]  ; same size, possibly fewer cycles, nonpairable
  movzx ax, byte [imageoffset]
  shl ax, 1   ;multiply by 2
  add ax, 320*imageY
  mov di, ax

      mov bx, cs
      mov ds, bx
      mov si, tpuimage
      mov dl, 20 ;Y height
      outter:
        mov cx, 10
        rep movsd
        add di, 320-40
        dec dl
     jne outter

  mov es, bx
  xor ah, ah

  ror byte [seed], 5
  xor byte [seed], 169
  inc byte [seed]
  mov al, byte [seed]
;  in al, 40h
;  xor al, 210

  mov di, buffer+(height*320)-320+20
  add di, ax
  mov eax, dword [colorint]
  mov dword [es:di-2], eax
;  mov word  [es:di+1], ax
;  mov dword [es:di-318], eax
;  mov word  [es:di-322], ax
  xor eax, eax
  mov di, buffer+320
  fireloop:
    movzx cx, byte [es:di]             ; 3 cycles Non-Pairable   1-3  
    cmp cl, 2                          ; 1 cycle  UV             2
    jb skipdeccurrent                  ; 1 cycle  P-V            3
    sub byte [es:di], 2
    skipdeccurrent:

    xor bh, bh                         ; 1 cycle  UV             5-U
    xor ah, ah                         ; 1 cycle  UV             5-V
    mov bl, byte [es:di-321]           ; 1 cycle  UV             6-U
    mov al, byte [es:di-319]           ; 1 cycle  UV             7-U
;    movzx bx, byte [es:di-321]         ; 3 cycles Non-Pairable   5-8
;    movzx ax, byte [es:di-319]         ; 3 cycles Non-Pairable   9-12
    add bx, ax                         ; 1 cycle  UV             8 
    mov al, byte [es:di-320]           ; 1 cycle  UV             9 
    add bx, ax                         ; 1 cycle  UV             10
    add bx, cx                         ; 1 cycle  UV             11
    sar bx, 2                          ; 1 cycle  P-U            12

    mov byte [es:di-320], bl           ; 1 cycle  UV             13
    inc di                             ; 1 cycle  UV             14
    cmp di, buffer+(height*320)-1      ; 1 cycle  UV             15
  jne fireloop                         ; 1 cycle  P-V            16 
                                                                 ;orig=21
   mov     dx,3dah
   Vrt:
   in      al,dx
   test    al,8
   jnz     Vrt
   NoVrt:
   in      al,dx
   test    al,8
   jz      NoVrt

    mov dx, cs
    mov ds, dx
    mov si, buffer
    mov ax, 0xa000
    mov es, ax
    mov di, (320*(200-height))
    mov cx, ((height-1)*80)
    rep movsd

    cmp byte [imageoffset], 1
    jne skipleftadjust
    mov byte [imagedir], 1
    skipleftadjust:
    cmp byte [imageoffset], 135
    jne skiprightadjust
    mov byte [imagedir], -1
    skiprightadjust:
    skipz:
    mov ah, 1             ;check for kbhit
    int 16h
    jz skipfadeoutinit
    sub dword [colorint], 0x01010101

    skipfadeoutinit:
      cmp byte [colorint], 255
    je shitfuck
      dec byte [colorint]
      cmp byte [colorint], 5
    jb exit
    shitfuck:
  jmp mainloop         ;if NOT, keep waiting...else...screw it
  exit:
     mov ax, 3           ;return to console
     int 10h
  ret
  
drawchar: ;requires al as character
  push ax
  ;change cursor
  mov ah, 02
  mov bh, 0
  mov dh, byte [cursory]
  mov dl, byte [cursorx]
  int 10h

  mov bl, byte [seed]
  shr bl, 2
  add bl, 180

  pop ax
  mov ah, 0ah
  mov bh, 1
;  mov bl, 200
  mov cx, 1
  int 10h
  inc byte [cursorx]
  ret
ret

drawnextchar:
  mov ax, cs
  mov es, ax
  mov di, word [greetoffset]
  inc word [greetoffset]
  mov al, byte [es:di]

  cmp al, 0
  jne skipresetgreet
  mov al, byte [es:di]

  mov word [greetoffset], greets
  ret
  skipresetgreet:

  cmp al, '~'
  jne skipclear
  mov byte [cursorx], 0
  mov byte [delay], 10
  ret
  skipclear:
  call drawchar
  mov byte [delay], 5
  ret

seed        DB 53
imageoffset DB 0
imagedir    DB 1
colorint    DB 255, 255, 255, 255

delay       DB 5
cursorx     DB 0
cursory     DB 0
greetoffset DW 0

greets:
  DB "GASM Assembler Example -=- FIRE4.ASM~"
  DB " Cool, Excellent Fire demo for TPU  ~"
  DB "      Coded by the INTSHREDR!       ~"
  DB "    Greets from Gareth Owen, to     ~"
  DB "  INTSHREDR!, Ramon, xekul, sune    ~"
  DB "    Flames to... Psion, Mike_L      ~"
  DB "======> http://gasm.cjb.net <====== ~"
  DB " GASM -=- The Ultimate Assembler !  ~"
  DB "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ~"
  DB 0 ;signals end

%include "tpu.inc"

buffer: ;un-initialised data

