uses crt, screen, resource, memory;

var
Aantal, T1: longint;
Timer                : Longint ABSOLUTE $0040:$006C;


procedure draw_chunk(x_y:word; col, chunk, size:byte; where:word);assembler;
asm

mov es, where

mov di, x_y

mov al, col
mov ah, al
mov bx, ax
db $66; shl ax, 16
mov ax, bx

mov dh, size
mov dl, dh

xor bx, bx
mov bl, dl
shl bx, 2
neg bx
add bx, 320*4

mov cl, chunk
cmp cl, 0;  je @n0
cmp cl, 1;  je @n1
cmp cl, 2;  je @n2
cmp cl, 3;  je @n3
cmp cl, 4;  je @n4
cmp cl, 5;  je @n5
cmp cl, 6;  je @n6
cmp cl, 7;  je @n7
cmp cl, 8;  je @n8
cmp cl, 9;  je @n9
cmp cl, 10; je @n10
cmp cl, 11; je @n11
cmp cl, 12; je @n12
cmp cl, 13; je @n13
cmp cl, 14; je @n14
cmp cl, 15; je @n15

@n0:
mov ch, dh
@y0:
mov cl, dl
@x0:
db $66; mov es:[di], ax
db $66; mov es:[di+320], ax
db $66; mov es:[di+640], ax
db $66; mov es:[di+960], ax
add di, 4
dec cl
jnz @x0
add di, bx
dec ch
jnz @y0
jmp @next
@n1:
mov ch, dh
@y1:
mov cl, dl
@x1:
mov es:[di+1], ax
mov es:[di+3], al
db $66; mov es:[di+320], ax
db $66; mov es:[di+640], ax
db $66; mov es:[di+960], ax
add di, 4
dec cl
jnz @x1
add di, bx
dec ch
jnz @y1
jmp @next
@n2:
mov ch, dh
@y2:
mov cl, dl
@x2:
mov es:[di+1], ax
mov es:[di+3], al
db $66; mov es:[di+320], ax
mov es:[di+640], ax
mov es:[di+643], al
db $66; mov es:[di+960], ax
add di, 4
dec cl
jnz @x2
add di, bx
dec ch
jnz @y2
jmp @next
@n3:
mov ch, dh
@y3:
mov cl, dl
@x3:
mov es:[di+1], al
mov es:[di+3], al
db $66; mov es:[di+320], ax
mov es:[di+640], ax
mov es:[di+643], al
db $66; mov es:[di+960], ax
add di, 4
dec cl
jnz @x3
add di, bx
dec ch
jnz @y3
jmp @next
@n4:
mov ch, dh
@y4:
mov cl, dl
@x4:
mov es:[di+1], al
mov es:[di+3], al
db $66; mov es:[di+320], ax
mov es:[di+641], al
mov es:[di+643], al
db $66; mov es:[di+960], ax
add di, 4
dec cl
jnz @x4
add di, bx
dec ch
jnz @y4
jmp @next
@n5:
mov ch, dh
@y5:
mov cl, dl
@x5:
mov es:[di+1], al
mov es:[di+3], al
mov es:[di+320], al
mov es:[di+322], ax
mov es:[di+641], al
mov es:[di+643], al
db $66; mov es:[di+960], ax
add di, 4
dec cl
jnz @x5
add di, bx
dec ch
jnz @y5
jmp @next
@n6:
mov ch, dh
@y6:
mov cl, dl
@x6:
mov es:[di+1], al
mov es:[di+3], al
mov es:[di+320], al
mov es:[di+322], ax
mov es:[di+641], al
mov es:[di+643], al
mov es:[di+960], ax
mov es:[di+962], al
add di, 4
dec cl
jnz @x6
add di, bx
dec ch
jnz @y6
jmp @next
@n7:
mov ch, dh
@y7:
mov cl, dl
@x7:
mov es:[di+1], al
mov es:[di+3], al
mov es:[di+320], al
mov es:[di+322], al
mov es:[di+641], al
mov es:[di+643], al
mov es:[di+960], ax
mov es:[di+962], al
add di, 4
dec cl
jnz @x7
add di, bx
dec ch
jnz @y7
jmp @next
@n8:
mov ch, dh
@y8:
mov cl, dl
@x8:
mov es:[di+1], al
mov es:[di+3], al
mov es:[di+320], al
mov es:[di+322], al
mov es:[di+641], al
mov es:[di+643], al
mov es:[di+960], al
mov es:[di+962], al
add di, 4
dec cl
jnz @x8
add di, bx
dec ch
jnz @y8
jmp @next
@n9:
mov ch, dh
@y9:
mov cl, dl
@x9:
mov es:[di+3], al
mov es:[di+320], al
mov es:[di+322], al
mov es:[di+641], al
mov es:[di+643], al
mov es:[di+960], al
mov es:[di+962], al
add di, 4
dec cl
jnz @x9
add di, bx
dec ch
jnz @y9
jmp @next
@n10:
mov ch, dh
@y10:
mov cl, dl
@x10:
mov es:[di+3], al
mov es:[di+320], al
mov es:[di+322], al
mov es:[di+641], al
mov es:[di+960], al
mov es:[di+962], al
add di, 4
dec cl
jnz @x10
add di, bx
dec ch
jnz @y10
jmp @next
@n11:
mov ch, dh
@y11:
mov cl, dl
@x11:
mov es:[di+320], al
mov es:[di+322], al
mov es:[di+641], al
mov es:[di+960], al
mov es:[di+962], al
add di, 4
dec cl
jnz @x11
add di, bx
dec ch
jnz @y11
jmp @next
@n12:
mov ch, dh
@y12:
mov cl, dl
@x12:
mov es:[di+320], al
mov es:[di+322], al
mov es:[di+960], al
mov es:[di+962], al
add di, 4
dec cl
jnz @x12
add di, bx
dec ch
jnz @y12
jmp @next
@n13:
mov ch, dh
@y13:
mov cl, dl
@x13:
mov es:[di+322], al
mov es:[di+960], al
mov es:[di+962], al
add di, 4
dec cl
jnz @x13
add di, bx
dec ch
jnz @y13
jmp @next
@n14:
mov ch, dh
@y14:
mov cl, dl
@x14:
mov es:[di+322], al
mov es:[di+960], al
add di, 4
dec cl
jnz @x14
add di, bx
dec ch
jnz @y14
jmp @next
@n15:
mov ch, dh
@y15:
mov cl, dl
@x15:
mov es:[di+960], al
add di, 4
dec cl
jnz @x15
add di, bx
dec ch
jnz @y15
@next:
end;

var
Logo_clear,Logo_fill: array[0..1] of word;
procedure Draw_Logo(var Logo_fill, Logo_clear : word;  mode, cell_size, color, pos, size, step, logo, where:word);
var
vdi,m,n,offset:word;
s, l:integer;
begin
   s:=15;

   if size>0 then
   for n:=0 to size-1 do
   begin
       l:=memw[logo:n*4] shr 4;
       offset:=memw[logo: n*4+2] + pos;
       if (l<16) then draw_chunk(offset, color, l, cell_size, where);
   end;

if step>0 then begin {-----------------}

   if mode=0 then
       if Logo_fill<size then
         begin
           Logo_fill:=Logo_fill+step;
           if Logo_fill>size then Logo_fill:=size;
         end;

   if mode=1 then
       if Logo_clear<size then
         begin
           Logo_clear:=Logo_clear+step;
           if Logo_clear>size then Logo_clear:=size;
         end;


   if mode=0 then begin
      vdi:=0;
      m:=Logo_fill-step;
      if m>0 then
      for n:=1 to m do begin
         l:=memw[logo:vdi*4];
         if l>0 then begin
             l:=l-s*step;
             if l<0 then l:=0;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
      for n:=step downto 1 do begin
         l:=memw[logo:vdi*4];
         if l>0 then begin
             l:=l-s*n;
             if l<0 then l:=0;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
   end;

   if mode=1 then begin
      vdi:=0;
      m:=Logo_clear-step;
      if m>0 then
      for n:=1 to m do begin
         l:=memw[logo:vdi*4];
         if l<256 then begin
             l:=l+s*step;
             if l>256 then l:=256;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
      for n:=step downto 1 do begin
         l:=memw[logo:vdi*4];
         if l<256 then begin
             l:=l+s*n;
             if l>256 then l:=256;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
   end;
end;{-----------------------}
end;

var
logo_mode:byte;
q:word;
start_time, old_time:longint;
elapsed_time:word;

begin
SetUpVirtual(scr,vaddr);
SetUpVirtual(scr2, vaddr2);
SetUpVirtual(scr3, vaddr3);

asm
mov ax, 13h
int 10h
end;
set60hz;

asm
mov al, 34h
out 43h, al
mov al, 19886 and 0ffh {1193180/freq}
out 40h, al
mov al, 19886 shr 8
out 40h, al
end;


{LoadBin('7dump.bin', 236, vaddr2);
LoadBin('7dump2.bin', 1040, vaddr2+16);}
LoadBin('sands.bin', 260, vaddr2);
LoadBin('sands2.bin', 1200, vaddr2+20);

Logo_mode:=0;
Logo_clear[0]:=0; Logo_Fill[0]:=0;
Logo_clear[1]:=0; Logo_Fill[1]:=0;
q:=0;

Aantal:=0; T1:=Timer;
old_time:=timer;
start_time:=timer;

repeat
q:=timer-start_time;
elapsed_time:=(timer-old_time);
old_time:=timer;


clear(0, 64000,vaddr); {Clear framebuffer}

if q>260 then logo_mode:=1;

if q>60 then begin
Draw_Logo(Logo_fill[0], Logo_clear[0],
          logo_mode, 2, 15, 60+80*320,  260 shr 2, elapsed_time,       vaddr2, vaddr);{}

Draw_Logo(Logo_fill[1], Logo_clear[1],
          logo_mode, 1, 9,  56+76*320, 1200 shr 2, elapsed_time shl 1, vaddr2+20, vaddr);{}
end;

{WaitVR;{}
{WaitVR;{}
{WaitVR;{}
WaitVR;{}
FlipScreen(vaddr,VGA);
Inc(Aantal);
until port[$60]=1;

asm
mov ax, 3h
int 10h
end;

asm
mov al, 34h
out 43h, al
xor al, al
out 40h, al
out 40h, al
end;

T1:=timer-T1;
WriteLn(Aantal/(T1/60):2:1,' Frames per second');

readkey;
repeat until keypressed;


end.