		.386p
		ASSUME	CS:CODE
		INCLUDE YGHASM.H

		PUBLIC	YGH_put

CODE		SEGMENT
		ALIGN	4
YGH_put		PROC
#Y2		EQU	ESP+40
#X2		EQU	ESP+36
#Y1		EQU	ESP+32
#X1		EQU	ESP+28
#DAT		EQU	ESP+24
#PAGE		EQU	ESP+20
#RETADR		EQU	ESP+16
#EBP		EQU	ESP+12
#ESI		EQU	ESP+8
#EDI		EQU	ESP+4
#EBX		EQU	ESP

		PUSH	EBP
		PUSH	ESI
		PUSH	EDI
		PUSH	EBX

		MOV	EDI,[#PAGE]
		MOV	ESI,[#DAT]

		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	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

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

		CMP	EBX,[EDI+YGH_VY1]
		JGE	SHORT #Y1fixed

		SUB	ECX,EAX
		LEA	ECX,[ECX*2+2]		;ECX:Byte per line of DATA
		MOV	EAX,[EDI+YGH_VY1]
		MOV	[#Y1],EAX
		SUB	EAX,EBX
		IMUL	EAX,ECX
		ADD	ESI,EAX			;ESI:Effective Data Top
#Y1fixed:
		CMP	EDX,[EDI+YGH_VY2]
		JLE	#Y2fixed
		MOV	EDX,[EDI+YGH_VY2]
#Y2fixed:

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

		CALL	PUTCOLOR

#EXIT:		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		RET
YGH_put		ENDP


ONESTEP		MACRO	N
		LOCAL	L00
		MOV	AX,[ESI+N*2]
		AND	AX,AX		;なんでJCXZを使う方が遅いんじゃ(^_^;)
		JE	SHORT L00
		MOV	ES:[EDI+N*2],AX
L00:
		ENDM


;ESI:DATA    EDI:PAGE    EAX,EBX,ECX,EDX:X1,Y1,X2,Y2
		ALIGN	4
PUTCOLOR	PROC

#ESPOFS		EQU	16
#DODOT		EQU	ESP+12
#ODWVRAM	EQU	ESP+8
#ODWDATA	EQU	ESP+4
#YCOUNT		EQU	ESP

		PUSH	ES
		SUB	ESP,#ESPOFS

		SUB	EDX,EBX
		INC	EDX
		MOV	[#YCOUNT],EDX		;EDX:free

		IMUL	EBX,[EDI+YGH_ODW]
		LEA	EBX,[EBX+EAX*2]
		ADD	EBX,[EDI+YGH_ADR]	;EBX:VRAM Top (mustn't change)

		MOV	EDX,ECX
		SUB	EDX,EAX
		LEA	EDX,[EDX*2+2]
		MOV	[#ODWDATA],EDX

		MOV	EDX,[EDI+YGH_ODW]
		MOV	[#ODWVRAM],EDX

		CMP	ECX,[EDI+YGH_VX2]
		JL	#X2fixed
		MOV	ECX,[EDI+YGH_VX2]
#X2fixed:

		CMP	EAX,[EDI+YGH_VX1]
		JGE	#X1fixed
		MOV	EDX,[EDI+YGH_VX1]
		SUB	EDX,EAX
		LEA	ESI,[ESI+EDX*2]		;ESI:Effective Data Top
		LEA	EBX,[EBX+EDX*2]
		MOV	EAX,[EDI+YGH_VX1]
#X1fixed:

		SUB	ECX,EAX
		INC	ECX
		MOV	[#DODOT],ECX

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

		MOV	EDX,ESI			;EDX:Data
						;EBX:Vram
		MOV	EBP,[#YCOUNT]		;EBP:Y counter

		ALIGN	4
#LOOP0:		MOV	ESI,EDX
		MOV	EDI,EBX

		ADD	EDX,[#ODWDATA]
		ADD	EBX,[#ODWVRAM]

		MOV	ECX,[#DODOT]
		SUB	ECX,8
		JL	#LOOP1
		ALIGN	4
#L00:		ONESTEP	7
		ONESTEP	6
		ONESTEP	5
		ONESTEP	4
		ONESTEP	3
		ONESTEP	2
		ONESTEP	1
		ONESTEP	0
		ADD	ESI,16
		ADD	EDI,16
		SUB	ECX,8
		JGE	#L00

#LOOP1:		JMP	CS:[#JTBL1 + 8*4 + ECX*4]
		ALIGN	4
#JTBL1		DD	OFFSET #L10,OFFSET #L11,OFFSET #L12,OFFSET #L13
		DD	OFFSET #L14,OFFSET #L15,OFFSET #L16,OFFSET #L17
#L17:		ONESTEP	6
#L16:		ONESTEP	5
#L15:		ONESTEP	4
#L14:		ONESTEP	3
#L13:		ONESTEP	2
#L12:		ONESTEP	1
#L11:		ONESTEP	0
#L10:

		DEC	EBP
		JNE	#LOOP0

		ADD	ESP,#ESPOFS
		POP	ES
		RET
PUTCOLOR	ENDP

		ALIGN	4
CODE		ENDS
		END
