;gouldsoft flip-vertical-solid-put
.model medium, basic
.stack 20h
.386

EXTRN gsclipx1:WORD
EXTRN gsclipx2:WORD
EXTRN gsclipy1:WORD
EXTRN gsclipy2:WORD

.code

;SUB gsfvsput (x,y,varseg(screen),varptr(screen),varseg(array),varptr(array)) [all byvals]
;stack:
;bp+16	x
;bp+14	y
;bp+12	screenseg
;bp+10	screenofs
;bp+8   aryseg
;bp+6   aryofs
;bp+4 	returnseg
;bp+2   returnofs
;bp     bp
;bp-2   mysterythingy
;bp-4   clipx1
;bp-6   clipy1
;bp-8   clipx2
;bp-10  clipy2
;bp-12  ds
;bp-14  num of leftover bytes

;es:di = screenseg:screenofset
;ds:si = aryseg:aryofs

public gsfvsput
gsfvsput proc

push bp

mov bp, sp
xor bx,bx

mov cx, gsclipx1
push bx

mov dx, gsclipy1
push cx

mov cx, gsclipx2
push dx

mov dx, gsclipy2
push cx

push dx
push es

mov es, [bp+12]
push ds

mov di, [bp+10]
mov ds, [bp+8]

mov si, [bp+6]
mov dx, [si]

shr dx, 3           ;dx = width
add si, 2

mov bx, [si]        ;bx = height
add si, 2

mov cx, [bp+14]     ;cx = y

cmp cx, [bp-10]
jg endofsolid

cmp cx, [bp-6]
jl toohighsolid
aftertoohighsolid:


mov ax, cx
add ax, bx
cmp ax, [bp-10]
jg toolowsolid
aftertoolowsolid:

dec ax
mov cx, ax
shl ax, 2
add ax, cx
shl ax, 6
add di, ax
;add di, 192

mov ax, [bp+16]
cmp ax, [bp-8]
jg endofsolid
cmp ax, [bp-4]
jl tooleftsolid
aftertooleftsolid:
add di, ax

add dx, ax
cmp dx, [bp-8]
jg toorightsolid
sub dx,ax
aftertoorightsolid:
mov cx,dx

;;;;;; ax = X, bx = height, cx = width, dx = width
and cx, 0000000000000011b
jnz solidwleftovers 
shr dx, 2
mov cx, dx
jmp solidloop

toohighsolid:
neg cx            ;cx = -Y
add cx, [bp-6]
cmp cx, bx
jge endofsolid  ;if it is waaay too high, end
sub bx, cx
mov cx, [bp-6]
jmp aftertoohighsolid  ;now cx = 0, 

toolowsolid:
sub ax, [bp-10]        ;ax = Y2
dec ax    
sub bx, ax        ;bx = height... this "crops" the height...
push ax
toolowloop:
add si, dx
dec ax
jnz toolowloop
pop ax
jmp aftertoolowsolid

tooleftsolid:
neg ax
add ax,[bp-4]
cmp ax, dx
jge endofsolid
add si, ax
sub dx, ax
mov [bp-2], ax
mov ax,[bp-4]
jmp aftertooleftsolid

toorightsolid:
sub dx, [bp-8]
sub dx,1
add [bp-2], dx
mov dx, [bp-8]
add dx,1
sub dx, ax
jmp aftertoorightsolid

solidloop:
rep movsd
sub di, 320
shl dx, 2
add si, [bp-2]
sub di, dx
shr dx, 2
dec bx
mov cx, dx
jnz solidloop
jmp endofsolid

solidwleftovers:
push cx
shr dx, 2
jz smallstuff
mov ax, [bp-16]
mov cx, dx

solidwloop:
rep movsd
mov cx, ax
rep movsb
sub di, 320
shl dx, 2
add si, [bp-2]
sub di, dx
shr dx, 2
sub di, ax
mov cx, dx
dec bx
jnz solidwloop
pop cx
jmp endofsolid

smallstuff:
mov dx, cx
solidsloop:
rep movsb
mov cx, dx
sub di, 320
add si, [bp-2]
sub di, dx
dec bx
jnz solidsloop
pop cx

endofsolid:
pop ds
pop es
pop eax
pop ecx
pop bx
pop bp
ret 12
gsfvsput endp

end
