Procedure SetupRndMatrix(segp : word;MaxRnd : byte);
Var i : word;
Begin
Randomize;
For i:=0 to $fff do mem[segp:i]:=Random(MaxRnd);
End;

procedure tflip8 (s1,s2: Word);assembler;
{s2 = background}

Asm
push ds
mov es,s1
mov ds,s2
xor di,di
mov cx,64000

@LoopDi:

mov al,byte ptr [es:di]  {blank}
mov bl,byte ptr [ds:di]  {blackground}

cmp al,0
jne @SkipMix1             {If not black then Skip Mix}
mov byte ptr [es:di],bl  {ELSE Mix}
@SkipMix1:

add di,1
Loop @LoopDi
pop ds
End;


procedure tflip16 (s1,s2: Word);assembler;
{s2 = background}

Asm
push ds
mov es,s1
mov ds,s2
xor di,di
mov cx,32000

@LoopDi:

mov ax,word ptr [es:di]  {blank}
mov bx,word ptr [ds:di]  {blackground}

cmp al,0
jne @SkipMix1             {If not black then Skip Mix}
mov byte ptr [es:di],bl  {ELSE Mix}
@SkipMix1:

cmp ah,0
jne @SkipMix2             {If not black then Skip Mix}
mov byte ptr [es:di+1],bh  {ELSE Mix}
@SkipMix2:

add di,2

Loop @LoopDi
pop ds
End;


{Den doyleyei sosta akoma!!}
procedure tflip32 (s1,s2: Word);assembler;
{s2 = background}

Asm
push ds
mov es,s1
mov ds,s2
xor di,di
mov cx,16000

@LoopDi:

{mov eax,es:[di]}
db $26,$66,$8b,$05

{mov ebx,ds:[di]}
db $3e,$66,$8b,$1d

cmp al,0
jne @SkipMix1             {If not black then Skip Mix}
mov byte ptr [es:di+3],bl {ELSE Mix}
@SkipMix1:

cmp ah,0
jne @SkipMix2              {If not black then Skip Mix}
mov byte ptr [es:di+2],bh  {ELSE Mix}
@SkipMix2:

db $66,$c1,$e8,16 {shr eax,16}
db $66,$c1,$eb,16 {shr ebx,16}

cmp al,0
jne @SkipMix3              {If not black then Skip Mix}
mov byte ptr [es:di+1],bl  {ELSE Mix}
@SkipMix3:

cmp ah,0
jne @SkipMix4              {If not black then Skip Mix}
mov byte ptr [es:di],bh  {ELSE Mix}
@SkipMix4:

add di,4

Loop @LoopDi
pop ds
End;

procedure trndflip16 (s1,s2,rndbuffer,rndstart: Word);
{s2 = background}
begin
asm
push ds
{Random Part}
mov es,s2
mov si,0
mov ds,rndbuffer
mov di,rndstart
mov bx,0           {i}

mov dx,rndstart    {step}

mov cx,64000

@LoopCX:

 mov al,es:[si]    {image source}
 mov bl,ds:[di]    {random matrix}

 add di,dx
 and di,$fff       {4096 bytes}

 add al,bl         {color=color+rnd}
 mov es:[si],al    {store back color}

 inc si
Loop @LoopCX
pop ds
end;


Asm
{Tflip Part}
push ds
mov es,s1
mov ds,s2
xor di,di
mov cx,32000

@LoopDi:

mov ax,word ptr [es:di]  {blank}
mov bx,word ptr [ds:di]  {blackground}

cmp al,0
jne @SkipMix1             {If not black then Skip Mix}
mov byte ptr [es:di],bl  {ELSE Mix}
@SkipMix1:

cmp ah,0
jne @SkipMix2             {If not black then Skip Mix}
mov byte ptr [es:di+1],bh  {ELSE Mix}
@SkipMix2:

add di,2

Loop @LoopDi
pop ds
end;

End;

