; d_polysa_16bit.asm
; this file uses NASM syntax.

%include "asm_nasm.inc"
%include "d_polysa.inc"

; underscore prefix handling
; for C-shared symbols:
%ifmacro _sym_prefix
; C-shared externs:
 _sym_prefix alias_colormap16
 _sym_prefix d_viewbuffer
 _sym_prefix d_scantable
 _sym_prefix r_refdef
 _sym_prefix a_sstepxfrac
 _sym_prefix r_affinetridesc
 _sym_prefix acolormap
 _sym_prefix d_pcolormap
 _sym_prefix d_sfrac
 _sym_prefix d_ptex
 _sym_prefix d_pedgespanpackage
 _sym_prefix d_tfrac
 _sym_prefix d_light
 _sym_prefix d_zi
 _sym_prefix d_pdest
 _sym_prefix d_pz
 _sym_prefix d_aspancount
 _sym_prefix erroradjustup
 _sym_prefix erroradjustdown
 _sym_prefix errorterm
 _sym_prefix d_xdenom
 _sym_prefix r_p0
 _sym_prefix r_p1
 _sym_prefix r_p2
 _sym_prefix a_tstepxfrac
 _sym_prefix a_ststepxwhole
 _sym_prefix zspantable
 _sym_prefix skintable
 _sym_prefix d_countextrastep
 _sym_prefix ubasestep
 _sym_prefix a_spans
 _sym_prefix d_pdestextrastep
 _sym_prefix d_pzextrastep
 _sym_prefix d_sfracextrastep
 _sym_prefix d_ptexextrastep
 _sym_prefix d_tfracextrastep
 _sym_prefix d_lightextrastep
 _sym_prefix d_ziextrastep
 _sym_prefix d_pdestbasestep
 _sym_prefix d_pzbasestep
 _sym_prefix d_sfracbasestep
 _sym_prefix d_ptexbasestep
 _sym_prefix d_tfracbasestep
 _sym_prefix d_lightbasestep
 _sym_prefix d_zibasestep
 _sym_prefix r_lstepx
 _sym_prefix r_lstepy
 _sym_prefix r_sstepx
 _sym_prefix r_sstepy
 _sym_prefix r_tstepx
 _sym_prefix r_tstepy
 _sym_prefix r_zistepx
 _sym_prefix r_zistepy
; C-shared globals:
 _sym_prefix D_PolysetAff8Start_16bit
 _sym_prefix R_PolysetDrawSpans8_Opaque_16bit
 _sym_prefix D_PolysetAff8End_16bit
 _sym_prefix D_Aff8Patch_16bit
%endif	; _sym_prefix

; externs from C code
 extern alias_colormap16
 extern d_viewbuffer
 extern d_scantable
 extern r_refdef
 extern a_sstepxfrac
 extern r_affinetridesc
 extern acolormap
 extern d_pcolormap
 extern d_sfrac
 extern d_ptex
 extern d_pedgespanpackage
 extern d_tfrac
 extern d_light
 extern d_zi
 extern d_pdest
 extern d_pz
 extern d_aspancount
 extern erroradjustup
 extern erroradjustdown
 extern errorterm
 extern d_xdenom
 extern r_p0
 extern r_p1
 extern r_p2
 extern a_tstepxfrac
 extern a_ststepxwhole
 extern zspantable
 extern skintable
 extern d_countextrastep
 extern ubasestep
 extern a_spans
 extern d_pdestextrastep
 extern d_pzextrastep
 extern d_sfracextrastep
 extern d_ptexextrastep
 extern d_tfracextrastep
 extern d_lightextrastep
 extern d_ziextrastep
 extern d_pdestbasestep
 extern d_pzbasestep
 extern d_sfracbasestep
 extern d_ptexbasestep
 extern d_tfracbasestep
 extern d_lightbasestep
 extern d_zibasestep
 extern r_lstepx
 extern r_lstepy
 extern r_sstepx
 extern r_sstepy
 extern r_tstepx
 extern r_tstepy
 extern r_zistepx
 extern r_zistepy

; externs from ASM-only code
 extern float_point5
 extern float_1
 extern float_minus_1
 extern float_0
 extern advancetable
 extern sstep
 extern tstep
 extern ceil_cw
 extern single_cw


SEGMENT .data

 ALIGN 4

aff8entryvec_table dd LDraw8, LDraw7, LDraw6, LDraw5
 dd LDraw4, LDraw3, LDraw2, LDraw1
lzistepx dd 0

SEGMENT .text

;;;;;;;;;;;;;;;;;;;;;;;;
; D_PolysetAff8Start_16bit
;;;;;;;;;;;;;;;;;;;;;;;;
 global D_PolysetAff8Start_16bit
D_PolysetAff8Start_16bit:

;;;;;;;;;;;;;;;;;;;;;;;;
; R_PolysetDrawSpans8_Opaque
;;;;;;;;;;;;;;;;;;;;;;;;

 global R_PolysetDrawSpans8_Opaque_16bit
R_PolysetDrawSpans8_Opaque_16bit:
 push esi
 push ebx
 mov esi, dword [4+8+esp]
 mov ecx, dword [r_zistepx]
 push ebp
 push edi
 ror ecx,16
 mov edx, dword [8+esi]
 mov dword [lzistepx],ecx
LSpanLoop:
 mov eax, dword [d_aspancount]
 sub eax,edx
 mov edx, dword [erroradjustup]
 mov ebx, dword [errorterm]
 add ebx,edx
 js LNoTurnover
 mov edx, dword [erroradjustdown]
 mov edi, dword [d_countextrastep]
 sub ebx,edx
 mov ebp, dword [d_aspancount]
 mov dword [errorterm],ebx
 add ebp,edi
 mov dword [d_aspancount],ebp
 jmp LRightEdgeStepped
LNoTurnover:
 mov edi, dword [d_aspancount]
 mov edx, dword [ubasestep]
 mov dword [errorterm],ebx
 add edi,edx
 mov dword [d_aspancount],edi
LRightEdgeStepped:
 cmp eax,1
 jl near LNextSpan
 jz near LExactlyOneLong
 mov ecx, dword [a_ststepxwhole]
 mov edx, dword [r_affinetridesc+8]
 mov dword [advancetable+4],ecx
 add ecx,edx
 mov dword [advancetable],ecx
 mov ecx, dword [a_tstepxfrac]
 mov cx, word [r_lstepx]
 mov edx,eax
 mov dword [tstep],ecx
 add edx,7
 shr edx,3
 mov ebx, dword [16+esi]
 mov bx,dx
 mov ecx, dword [4+esi]
 neg eax
 mov edi, dword [0+esi]
 and eax,7
 sub edi,eax
 sub edi,eax
 sub ecx,eax
 sub ecx,eax
 mov edx, dword [20+esi]
 mov dx, word [24+esi]
 mov ebp, dword [28+esi]
 ror ebp,16
 push esi
 mov esi, dword [12+esi]
 jmp dword[aff8entryvec_table+eax*4]
LDrawLoop:
LDraw8:
 cmp bp, word [ecx]
 jl Lp1
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch8:
 mov word [edi],ax
Lp1:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw7:
 cmp bp, word [2+ecx]
 jl Lp2
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [2+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch7:
 mov word [2+edi],ax
Lp2:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw6:
 cmp bp, word [4+ecx]
 jl Lp3
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [4+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch6:
 mov word [4+edi],ax
Lp3:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw5:
 cmp bp, word [6+ecx]
 jl Lp4
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [6+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch5:
 mov word [6+edi],ax
Lp4:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw4:
 cmp bp, word [8+ecx]
 jl Lp5
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [8+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch4:
 mov word [8+edi],ax
Lp5:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw3:
 cmp bp, word [10+ecx]
 jl Lp6
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [10+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch3:
 mov word [10+edi],ax
Lp6:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw2:
 cmp bp, word [12+ecx]
 jl Lp7
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [12+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch2:
 mov word [12+edi],ax
Lp7:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw1:
 cmp bp, word [14+ecx]
 jl Lp8
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [14+ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch1:
 mov word [14+edi],ax
Lp8:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
 add edi,16
 add ecx,16
 dec bx
 jnz near LDrawLoop
 pop esi
LNextSpan:
 add esi,32
LNextSpanESISet:
 mov edx, dword [8+esi]
 cmp edx,offset -999999
 jnz near LSpanLoop
 pop edi
 pop ebp
 pop ebx
 pop esi
 ret
LExactlyOneLong:
 mov ecx, dword [4+esi]
 mov ebp, dword [28+esi]
 ror ebp,16
 mov ebx, dword [12+esi]
 cmp bp, word [ecx]
 jl LNextSpan
 xor eax,eax
 mov edi, dword [0+esi]
 mov ah, byte [24+1+esi]
 add esi,32
 mov al, byte [ebx]
 mov word [ecx],bp
 mov ax, word [12345678h+eax*2]
LPatch9:
 mov word [edi],ax
 jmp LNextSpanESISet


;;;;;;;;;;;;;;;;;;;;;;;;
; D_PolysetAff8End_16bit
;;;;;;;;;;;;;;;;;;;;;;;;
 global D_PolysetAff8End_16bit
D_PolysetAff8End_16bit:

;;;;;;;;;;;;;;;;;;;;;;;;
; D_Aff8Patch_16bit
;;;;;;;;;;;;;;;;;;;;;;;;
 global D_Aff8Patch_16bit
D_Aff8Patch_16bit:
 mov eax,[alias_colormap16]
 mov dword [LPatch1-4],eax
 mov dword [LPatch2-4],eax
 mov dword [LPatch3-4],eax
 mov dword [LPatch4-4],eax
 mov dword [LPatch5-4],eax
 mov dword [LPatch6-4],eax
 mov dword [LPatch7-4],eax
 mov dword [LPatch8-4],eax
 mov dword [LPatch9-4],eax
 ret

