;GSfhsput
;for comments of most of the code, look at GSsprite.asm

.model medium, basic
.stack 20h
.486

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

.code

;SUB gsfhsput (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  es
;bp-16  num of leftover bytes

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

public gsfhsput
gsfhsput proc

push bp
mov bp, sp
xor bx,bx
push bx

mov cx, gsclipx1
mov dx, gsclipy1
push cx
push dx
mov cx, gsclipx2
mov dx, gsclipy2
push cx
push dx

push ds
push es

mov es, [bp+12]
mov di, [bp+10]
mov ds, [bp+8]
mov si, [bp+6]

mov dx, [si]
shr dx, 3
add si, 2

mov bx, [si]
add si, 2

mov cx, [bp+14]
cmp cx, [bp-10]
jg endofsolid
cmp cx, [bp-6]
jl toohighsolid
aftertoohighsolid:

mov ax, cx
shl ax, 2
add ax, cx
shl ax, 6
add di, ax

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

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

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

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

toohighsolid:
neg cx
add cx,[bp-6]
cmp cx, bx
jge endofsolid
toohighsolidloop:
add si, dx
dec bx
loop toohighsolidloop
mov cx,[bp-6]
jmp aftertoohighsolid

toolowsolid:
sub ax, [bp-10]
dec ax
sub bx, 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]
dec dx
add si, dx
add [bp-2], dx
mov dx, [bp-8]
inc dx
sub dx, ax
jmp aftertoorightsolid

solidloop:
mov eax, [si]
add si, 4

bswap eax

mov es:[di], eax
sub di, 4

dec cx
jnz solidloop

add di, 320
shl dx, 2
add si, [bp-2]
add 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:
mov eax, [si]
add si, 4

bswap eax

mov es:[di], eax
sub di, 4

dec cx
jnz solidwloop

mov cx, [bp-16]
add di, 3
solidwloop1:
mov al,[si]
inc si
mov es:[di],al
dec di
loop solidwloop1

shl dx, 2
add di, 317
add si, [bp-2]
add di, dx
shr dx, 2
add di, [bp-16]
mov cx, dx
dec bx
jnz solidwloop
pop cx
jmp endofsolid

smallstuff:
mov dx, cx
add di,3
solidsloop:
mov al, [si]
inc si
mov es:[di], al
dec di
loop solidsloop
mov cx, dx
add di, 320
add si, [bp-2]
add di, dx
dec bx
jnz solidsloop
pop cx

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