		.386p
		ASSUME	CS:CODE
		INCLUDE	YGHASM.H

		PUBLIC	YGH_filledBox

;ESI:Nrepeat  EDI:VRAMlineTop  EAX:color  EBX:X1  EDX:X2  EBP:Offset of EDI
;All registers will be destroyed
ONELINE		MACRO
		MOV	ES:[EDI+EBX*2],AX
		MOV	ES:[EDI+EDX*2],AX
		MOV	ECX,ESI
		REP	STOSD
		ADD	EDI,EBP
		ENDM


CODE		SEGMENT
		ALIGN	4
YGH_filledBox	PROC
#Y2		EQU	ESP+48
#X2		EQU	ESP+44
#Y1		EQU	ESP+40
#X1		EQU	ESP+36
#PAGE		EQU	ESP+32
#RETADR		EQU	ESP+28
#ESI		EQU	ESP+24
#EDI		EQU	ESP+20
#EBX		EQU	ESP+16
#EBP		EQU	ESP+12
#ES		EQU	ESP+8
#ESPOFS		EQU	8
#LCOUNT		EQU	ESP+4
#NREPEAT	EQU	ESP

		PUSH	ESI
		PUSH	EDI
		PUSH	EBX
		PUSH	EBP
		PUSH	ES

		SUB	ESP,#ESPOFS

		MOV	EDI,[#PAGE]

		MOV	EAX,[#X1]
		MOV	EBX,[#Y1]
		MOV	ECX,[#X2]
		MOV	EDX,[#Y2]
		CMP	EAX,ECX
		JLE	#X2isLarger
		XCHG	EAX,ECX
#X2isLarger:
		CMP	EBX,EDX
		JLE	#Y2isLarger
		XCHG	EBX,EDX
#Y2isLarger:

		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,[EDI+YGH_VX1]
		JG	#X1isOK
		MOV	EAX,[EDI+YGH_VX1]
#X1isOK:
		CMP	ECX,[EDI+YGH_VX2]
		JL	#X2isOK
		MOV	ECX,[EDI+YGH_VX2]
#X2isOK:
		CMP	EBX,[EDI+YGH_VY1]
		JG	#Y1isOK
		MOV	EBX,[EDI+YGH_VY1]
#Y1isOK:
		CMP	EDX,[EDI+YGH_VY2]
		JL	#Y2isOK
		MOV	EDX,[EDI+YGH_VY2]
#Y2isOK:

		SUB	EDX,EBX
		INC	EDX
		MOV	[#LCOUNT],EDX		;必ず1はあるはずである。

		IMUL	EBX,[EDI+YGH_ODW]
		ADD	EBX,[EDI+YGH_ADR]
		MOV	ESI,EBX			;ESI will be the VRAM pointer.

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

		MOV	EBX,EAX			;EBX will be relative x1
		MOV	EDX,ECX			;EDX will be relative x2 

		INC	EAX
		AND	AL,0FEH
		SUB	ECX,EAX
		INC	ECX
		SHR	ECX,1
		MOV	[#NREPEAT],ECX
		MOV	EBP,[EDI+YGH_ODW]
		NEG	ECX
		LEA	EBP,[EBP+ECX*4]		;EBP:Offset of EDI
		SUB	EBX,EAX			;EBX:relative x1
		SUB	EDX,EAX			;EDX:relative x2
		LEA	ESI,[ESI+EAX*2]

		MOV	ECX,[EDI+YGH_COL]
		MOV	EAX,ECX
		SHL	EAX,16
		MOV	AX,CX

		MOV	EDI,ESI			;EDI:Vram top
		MOV	ESI,[#NREPEAT]		;ESI:X counter

		MOV	ECX,[#LCOUNT]
		AND	ECX,15
		JMP	CS:[#JTBL+ECX*4]

		ALIGN	4
#JTBL		DD  OFFSET #L00,OFFSET #L01,OFFSET #L02,OFFSET #L03
		DD  OFFSET #L04,OFFSET #L05,OFFSET #L06,OFFSET #L07
		DD  OFFSET #L08,OFFSET #L09,OFFSET #L10,OFFSET #L11
		DD  OFFSET #L12,OFFSET #L13,OFFSET #L14,OFFSET #L15
#L00:		ONELINE
#L15:		ONELINE
#L14:		ONELINE
#L13:		ONELINE
#L12:		ONELINE
#L11:		ONELINE
#L10:		ONELINE
#L09:		ONELINE
#L08:		ONELINE
#L07:		ONELINE
#L06:		ONELINE
#L05:		ONELINE
#L04:		ONELINE
#L03:		ONELINE
#L02:		ONELINE
#L01:		ONELINE
		SUB	[#LCOUNT],DWORD PTR 16
		JG	#L00

#EXIT:		ADD	ESP,#ESPOFS
		POP	ES
		POP	EBP
		POP	EBX
		POP	EDI
		POP	ESI
		MOV	EAX,0
		RET
YGH_filledBox	ENDP

CODE		ENDS
		END
