		.386p
		ASSUME	CS:CODE
		INCLUDE YGHASM.H

		PUBLIC	YGH_areaPaint


;void paint(int *plg)
;{
;    int y,y1,y2;
;    y1=mny;
;    y2=may;
;
;    if(y2<vy1 || y1>vy2)return;
;
;    if(y1<vy1)y1=vy1;
;    if(y2>vy2)y2=vy2;
;
;    plg+=y1*XBUF;
;
;    for(y=y1; y<=y2; y++)
;    {
;        int i,x1,x2;
;        if(*plg&1)YGB_color(EGB_work,31*1024);
;             else YGB_color(EGB_work,31*32);
;
;        for(i=0; i<*plg; i+=2)
;        {
;            x1=plg[1+i];
;            x2=plg[2+i];
;            if(x2<=vx1 || vx2<=x1)continue;
;            YGB_line(EGB_work,x1,y,x2,y);
;        }
;
;        plg+=XBUF;
;    }
;}


CODE		SEGMENT

VY1CUT		MACRO	Y
		LOCAL	L0
		CMP	Y,[EDI+YGH_VY1]
		JGE	L0
		MOV	Y,[EDI+YGH_VY1]
L0:
		ENDM

VY2CUT		MACRO	Y
		LOCAL	L0
		CMP	Y,[EDI+YGH_VY2]
		JLE	L0
		MOV	Y,[EDI+YGH_VY2]
L0:
		ENDM


;ONELINE
;EAX:color    ESI:x1,x2BufferPointer    EDX:VRAMlineTop
;EBX,ECX,EDI will be destroyed   ESI will be saved
ONELINE		MACRO	qERROR2,qNUM
		LOCAL	qNEXT
		MOV	EBX,[ESI+qNUM*8+4]
		CMP	EBX,[#VX2]
		JGE	SHORT qNEXT
		MOV	ECX,[ESI+qNUM*8+8]
		CMP	ECX,[#VX1]
		JLE	SHORT qNEXT

		MOV	ES:[EDX+EBX*2],AX
		MOV	ES:[EDX+ECX*2],AX

		INC	EBX
		AND	BL,0FEH	    ;==AND  EBX,0FFFFFFFFH-1 (省メモリ(^_^;))
		LEA	EDI,[EDX+EBX*2]

		SUB	ECX,EBX
		ADD	ECX,1
		SHR	ECX,1

		REP	STOSD
qNEXT:
		ENDM


;ONECOLUMN
;ESI:x1,x2 BufferPointer    EAX:color    EDX:VRAMlineTop
;EBX,ECX,EDI will be destroyed   ESI,EDX will be incremented
ONECOLUMN	MACRO
		LOCAL	qJTBL,qLOOP0OUT
		LOCAL	qL01,qL02,qL03,qL04,qL05,qL06,qL07

		MOV	ECX,[ESI]
		AND	CL,0FEH		;== AND ECX,0FFFFFFFEH
		JMP	CS:[qJTBL + ECX*2]
		ALIGN	4
qJTBL		DD	OFFSET qLOOP0OUT
		DD	OFFSET qL01,OFFSET qL02
		DD	OFFSET qL03,OFFSET qL04
		DD	OFFSET qL05,OFFSET qL06
		DD	OFFSET qL07,OFFSET qL07
qL07:		ONELINE	#ERROR2,6
qL06:		ONELINE	#ERROR2,5
qL05:		ONELINE	#ERROR2,4
qL04:		ONELINE	#ERROR2,3
qL03:		ONELINE	#ERROR2,2
qL02:		ONELINE	#ERROR2,1
		ALIGN	4
qL01:		ONELINE	#ERROR2,0
qLOOP0OUT:	ADD	ESI,YGH_XBUF*4
		ADD	EDX,[#ODW]
		ENDM


		ALIGN	4
YGH_areaPaint	PROC
#EDGE		EQU	ESP+52
#AREA		EQU	ESP+48
#PAGE		EQU	ESP+44
#RETADR		EQU	ESP+40
#EBP		EQU	ESP+36
#ESI		EQU	ESP+32
#EDI		EQU	ESP+28
#EBX		EQU	ESP+24
#DS		EQU	ESP+20
#ES		EQU	ESP+16
#ESPOFS		EQU	16
#YCOUNT		EQU	ESP+12
#ODW		EQU	ESP+8
#VX1		EQU	ESP+4
#VX2		EQU	ESP

		PUSH	EBP
		PUSH	ESI
		PUSH	EDI
		PUSH	EBX
		PUSH	DS
		PUSH	ES
		SUB	ESP,#ESPOFS

		MOV	EDI,[#EDGE]
		MOV	EAX,[EDI+YGH_EDX1]
		MOV	EBX,[EDI+YGH_EDY1]
		MOV	ECX,[EDI+YGH_EDX2]
		MOV	EDX,[EDI+YGH_EDY2]

		MOV	EDI,[#PAGE]

		CMP	ECX,[EDI+YGH_VX1]
		JL	#EXIT
		CMP	EAX,[EDI+YGH_VX2]
		JG	#EXIT
		CMP	EDX,[EDI+YGH_VY1]
		JL	#EXIT
		CMP	EBX,[EDI+YGH_VY2]
		JG	#EXIT

		CMP	EAX,ECX
		JE	#VERTICAL
		CMP	EBX,EDX
		JE	#HORIZONTAL

		MOV	EAX,[EDI+YGH_SEG]
		MOV	ES,AX

		VY1CUT	EBX
		VY2CUT	EDX

		MOV	EBP,EDX
		SUB	EBP,EBX
		INC	EBP
		MOV	[#YCOUNT],EBP

		SHL	EDX,YGH_XBUFSHIFT
		ADD	EDX,[#AREA]
		MOV	EBP,EDX			;EBP:最後のXBUFへのポインタ

		MOV	EDX,EBX
		IMUL	EDX,[EDI+YGH_ODW]
		ADD	EDX,[EDI+YGH_ADR]	;EDX:ラインの先頭アドレス保持

		SHL	EBX,YGH_XBUFSHIFT
		MOV	ESI,[#AREA]
		ADD	ESI,EBX			;ESI:端点バッファへのポインタ

		MOV	EBX,[EDI+YGH_COL]
		MOV	EAX,EBX
		SHL	EAX,16
		MOV	AX,BX			;EAX:色コード

		MOV	EBX,[EDI+YGH_VX1]
		MOV	[#VX1],EBX
		MOV	EBX,[EDI+YGH_VX2]
		MOV	[#VX2],EBX
		MOV	EBX,[EDI+YGH_ODW]	; <- EDI解放 : REP STOSDで使用
		MOV	[#ODW],EBX

		MOV	ECX,[#YCOUNT]
		AND	ECX,7
		JMP	CS:[#JTBL + ECX*4]
		ALIGN	4
#JTBL		DD	OFFSET #LOOP0,OFFSET #LOOP1
		DD	OFFSET #LOOP2,OFFSET #LOOP3
		DD	OFFSET #LOOP4,OFFSET #LOOP5
		DD	OFFSET #LOOP6,OFFSET #LOOP7
#LOOP0:		ONECOLUMN
#LOOP7:		ONECOLUMN
#LOOP6:		ONECOLUMN
#LOOP5:		ONECOLUMN
#LOOP4:		ONECOLUMN
#LOOP3:		ONECOLUMN
#LOOP2:		ONECOLUMN
#LOOP1:		ONECOLUMN
		CMP	ESI,EBP
		JLE	#LOOP0


#EXIT:		ADD	ESP,#ESPOFS
		POP	ES
		POP	DS
		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		RET



#VERT_JTBL	DD	OFFSET #V0,OFFSET #V1,OFFSET #V2,OFFSET #V3
		DD	OFFSET #V4,OFFSET #V5,OFFSET #V6,OFFSET #V7
#VERTICAL:
		MOV	EDI,[#PAGE]
		PUSH	DS
		POP	ES
		MOV	EAX,ES:[EDI+YGH_SEG]
		MOV	DS,AX

		CMP	EBX,ES:[EDI+YGH_VY2]
		JG	#EXIT
		CMP	EDX,ES:[EDI+YGH_VY1]
		JL	#EXIT

		CMP	EBX,ES:[EDI+YGH_VY1]
		JGE	#NOTOPCLIP
		MOV	EBX,ES:[EDI+YGH_VY1]
#NOTOPCLIP:
		CMP	EDX,ES:[EDI+YGH_VY2]
		JLE	#NOBTMCLIP
		MOV	EDX,ES:[EDI+YGH_VY2]
#NOBTMCLIP:

		MOV	EAX,EBX
		IMUL	EAX,ES:[EDI+YGH_ODW]
		ADD	EAX,ES:[EDI+YGH_ADR]
		LEA	EAX,[EAX+ECX*2]

		SUB	EDX,EBX
		JL	#ERROR1
		MOV	ECX,ES:[EDI+YGH_COL]
		MOV	[EAX],CX
		JE	#EXIT


		MOV	EDI,ES:[EDI+YGH_ODW]
		MOV	ESI,EDX
		AND	ESI,7
		JMP	CS:[#VERT_JTBL + ESI*4]

		ALIGN	4
#V0:		ADD	EAX,EDI
		MOV	[EAX],CX
#V7:		ADD	EAX,EDI
		MOV	[EAX],CX
#V6:		ADD	EAX,EDI
		MOV	[EAX],CX
#V5:		ADD	EAX,EDI
		MOV	[EAX],CX
#V4:		ADD	EAX,EDI
		MOV	[EAX],CX
#V3:		ADD	EAX,EDI
		MOV	[EAX],CX
#V2:		ADD	EAX,EDI
		MOV	[EAX],CX
#V1:		ADD	EAX,EDI
		MOV	[EAX],CX
		SUB	EDX,8
		JG	#V0

		ADD	ESP,#ESPOFS
		POP	ES
		POP	DS
		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		RET



#HORIZONTAL:
		MOV	EDI,[#PAGE]

		CMP	EAX,[EDI+YGH_VX2]
		JGE	#EXIT
		CMP	ECX,[EDI+YGH_VX1]
		JLE	#EXIT

		CMP	EAX,[EDI+YGH_VX1]
		JGE	#NOLFTCLIP
		MOV	EAX,[EDI+YGH_VX1]
#NOLFTCLIP:
		CMP	ECX,[EDI+YGH_VX2]
		JLE	#NORITCLIP
		MOV	ECX,[EDI+YGH_VX2]
#NORITCLIP:

		MOV	EDX,[EDI+YGH_SEG]
		MOV	ES,DX

		MOV	ESI,[EDI+YGH_COL]
		IMUL	EBX,[EDI+YGH_ODW]
		ADD	EBX,[EDI+YGH_ADR]
		LEA	EDI,[EBX+EAX*2]
		LEA	ECX,[EBX+ECX*2]
		CMP	EDI,ECX
		JG	#ERROR1
		MOV	ES:[EDI],SI
		MOV	ES:[ECX],SI
		JE	#EXIT

		ADD	EDI,2
		AND	EDI,0FFFFFFFFH -3

		SUB	ECX,EDI
		ADD	ECX,2
		SHR	ECX,2

		MOVZX	EAX,SI
		SHL	ESI,16
		OR	EAX,ESI

		REP	STOSD

		ADD	ESP,#ESPOFS
		POP	ES
		POP	DS
		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		RET

		ALIGN	4
#ERROR1:	MOV	EAX,1
		JMP	#EXIT
		ALIGN	4
#ERROR2:	MOV	EAX,2
		JMP	#EXIT



YGH_areaPaint	ENDP

CODE		ENDS
		END
