		.386p
		ASSUME	CS:CODE
		INCLUDE	YGHASM.H

		PUBLIC	YGH_box

DRAWDOUBLE	MACRO
		MOV	[EDI+EBX*2],AX
		MOV	[EDI+EDX*2],AX
		ADD	EDI,EBP
		ENDM

DSINGLE		MACRO
		MOV	[EDI+EBX*2],AX
		ADD	EDI,EBP
		ENDM



CODE		SEGMENT

		ALIGN	4
YGH_box		PROC
#Y2		EQU	ESP+44
#X2		EQU	ESP+40
#Y1		EQU	ESP+36
#X1		EQU	ESP+32
#PAGE		EQU	ESP+28
#RETADR		EQU	ESP+24
#EBP		EQU	ESP+20
#ESI		EQU	ESP+16
#EDI		EQU	ESP+12
#EBX		EQU	ESP+8
#ES		EQU	ESP+4
#DS		EQU	ESP

		PUSH	EBP
		PUSH	ESI
		PUSH	EDI
		PUSH	EBX
		PUSH	ES
		PUSH	DS

		MOV	ESI,[#PAGE]

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

		MOV	EAX,[#X1]
		MOV	EBX,[#Y1]
		MOV	ECX,[#X2]
		MOV	EDX,[#Y2]

		CMP	EAX,ECX
		JL	#X2isLarger
		XCHG	EAX,ECX
#X2isLarger:
		CMP	EBX,EDX
		JL	#Y2isLarger
		XCHG	EBX,EDX
#Y2isLarger:


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

		MOV	[#X1],EAX
		MOV	[#Y1],EBX
		MOV	[#X2],ECX
		MOV	[#Y2],EDX

		CMP	EAX,[ESI+YGH_VX1]
		JG	#X1isCut
		MOV	EAX,[ESI+YGH_VX1]
#X1isCut:
		CMP	ECX,[ESI+YGH_VX2]
		JL	#X2isCut
		MOV	ECX,[ESI+YGH_VX2]
#X2isCut:

		INC	EAX
		AND	AL,0FEH
		MOV	EBP,EAX			;EBP:repeat start pointer
		SUB	ECX,EAX
		INC	ECX
		SHR	ECX,1			;ECX:repeat counter

		MOV	EDI,[ESI+YGH_COL]
		MOV	EAX,EDI
		SHL	EAX,16
		MOV	AX,DI			;EAX:color shouldn't be changed

		CMP	EBX,[ESI+YGH_VY1]
		JL	#Y1drawn
		IMUL	EBX,[ESI+YGH_ODW]
		ADD	EBX,[ESI+YGH_ADR]
		LEA	EDI,[EBX+EBP*2]
		MOV	EBX,ECX
		REP	STOSD
		MOV	ECX,EBX
#Y1drawn:
		CMP	EDX,[ESI+YGH_VY2]
		JG	#Y2drawn
		IMUL	EDX,[ESI+YGH_ODW]
		ADD	EDX,[ESI+YGH_ADR]
		LEA	EDI,[EDX+EBP*2]
		REP	STOSD
#Y2drawn:

		;EBX,ECX,EDX:free
		MOV	EBX,[#Y1]
		MOV	EDX,[#Y2]

		CMP	EBX,[ESI+YGH_VY1]
		JG	#Y1isCut
		MOV	EBX,[ESI+YGH_VY1]
#Y1isCut:
		CMP	EDX,[ESI+YGH_VY2]
		JL	#Y2isCut
		MOV	EDX,[ESI+YGH_VY2]
#Y2isCut:
		MOV	EBP,[ESI+YGH_ODW]	;EBP:ODW

		MOV	EDI,EBX
		IMUL	EDI,EBP
		ADD	EDI,[ESI+YGH_ADR]	;EDI:VRAM line pointer

		SUB	EDX,EBX
		LEA	ECX,[EDX+1]		;ECX:line counter

		MOV	EBX,[#X1]
		MOV	EDX,[#X2]
		CMP	EBX,[ESI+YGH_VX1]
		JL	#drawX2			;at least No X1  X2:unknown
		CMP	EDX,[ESI+YGH_VX2]
		JG	#drawX1			;No X2 but X1
		PUSH	ES
		POP	DS
#drawX1X2:	MOV	ESI,ECX
		ADD	ECX,7
		SHR	ECX,3
		AND	ESI,7
		JMP	CS:[#JTBL0X+ESI*4]
		ALIGN	4
#JTBL0X		DD OFFSET #L00,OFFSET #L01,OFFSET #L02,OFFSET #L03
		DD OFFSET #L04,OFFSET #L05,OFFSET #L06,OFFSET #L07
#L00:		DRAWDOUBLE
#L07:		DRAWDOUBLE
#L06:		DRAWDOUBLE
#L05:		DRAWDOUBLE
#L04:		DRAWDOUBLE
#L03:		DRAWDOUBLE
#L02:		DRAWDOUBLE
#L01:		DRAWDOUBLE
		DEC	ECX
		JNE	#L00
		JMP	#EXIT


		ALIGN	4
#drawX2:	CMP	EDX,[ESI+YGH_VX2]
		JG	#EXIT
		MOV	EBX,EDX
		ALIGN	4
#drawX1:	PUSH	ES
		POP	DS
		MOV	ESI,ECX
		ADD	ECX,7
		SHR	ECX,3
		AND	ESI,7
		JMP	CS:[#JTBL2X+ESI*4]
		ALIGN	4
#JTBL2X		DD OFFSET #L20,OFFSET #L21,OFFSET #L22,OFFSET #L23
		DD OFFSET #L24,OFFSET #L25,OFFSET #L26,OFFSET #L27
#L20:		DSINGLE
#L27:		DSINGLE
#L26:		DSINGLE
#L25:		DSINGLE
#L24:		DSINGLE
#L23:		DSINGLE
#L22:		DSINGLE
#L21:		DSINGLE
		LOOP	#L20

#EXIT:		POP	DS
		POP	ES
		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		MOV	EAX,0
		RET
YGH_box		ENDP

CODE		ENDS
		END
