		.386p
		ASSUME	CS:CODE
		INCLUDE	YGHASM.H


		PUBLIC	YGH_putMono


;putMono(char *dat,int x1,int y1,int x2,int y2)
;{
;    int x,y;
;    int i,j,p;
;
;    i=0;
;    for(y=y1; y<(y2+1); y++)
;    {
;        for(x=x1; x<(x2+1)-8; x+=8)
;        {
;            p=dat[i++];
;            for(j=x; j<x+8; j++)
;            {
;                if(p&128)YGB_pset(egb,j,y);
;                p*=2;
;            }
;        }
;        if(j<x2)
;        {
;            p=dat[i++];
;            for(j=j; j<(x2+1); j++)
;            {
;                if(p&128)YGB_pset(egb,j,y);
;                p*=2;
;            }
;        }
;    }
;}
CODE		SEGMENT

		ALIGN	4
YGH_putMono	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	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

		MOV	ESI,[#DAT]
		CMP	EBX,[EDI+YGH_VY1]
		JGE	#Y1fixed

		SUB	ECX,EAX
		SHR	ECX,3
		INC	ECX		;ECX:bytes per 1 line of data
		MOV	EDX,[EDI+YGH_VY1]
		MOV	[#Y1],EDX
		SUB	EDX,EBX		;EDX:VY1-Y1
		IMUL	EDX,ECX
		ADD	ESI,EDX		;ESI:Effective Data Top
#Y1fixed:
		MOV	EDX,[#Y2]
		CMP	EDX,[EDI+YGH_VY2]
		JLE	#Y2fixed
		MOV	EDX,[EDI+YGH_VY2]
#Y2fixed:
		MOV	EAX,[#X1]
		MOV	EBX,[#Y1]
		MOV	ECX,[#X2]

		CMP	EAX,[EDI+YGH_VX1]
		JL	#DANGER
		CMP	ECX,[EDI+YGH_VX2]
		JG	#DANGER

		CALL	FLYINGPUT1COLOR

		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		MOV	EAX,0
		RET



#DANGER:	CALL	PUT1COLOR

#EXIT:		POP	EBX
		POP	EDI
		POP	ESI
		POP	EBP
		MOV	EAX,0
		RET
YGH_putMono	ENDP





;ESI:data top    EDI:PAGE    EAX,EBX,ECX,EDX:X1,Y1,X2,Y2
;Y1,Y2 must be already cut
PSET		MACRO	N
		LOCAL	SKP
		BT	EBX,N
		JNC	SHORT SKP
		MOV	[EDI+(7-N)*2],AX
SKP:
		ENDM


PUT1COLOR	PROC

#ESPOFS		EQU	44
#RESTBIT	EQU	ESP+40
#DOBIT		EQU	ESP+36
#DOBYTE		EQU	ESP+32
#SKIPBIT	EQU	ESP+28
#SKIPBYTE	EQU	ESP+24
#SKIPDOT	EQU	ESP+20
#ODWVRAM	EQU	ESP+16
#ODWDATA	EQU	ESP+12
#NEXTVRAM	EQU	ESP+8
#NEXTDATA	EQU	ESP+4
#YCOUNT		EQU	ESP

		PUSH	DS
		PUSH	ES
		SUB	ESP,#ESPOFS

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

		MOV	EDX,ECX
		SUB	EDX,EAX
		SHR	EDX,3
		INC	EDX
		MOV	[#ODWDATA],EDX

		MOV	EDX,[EDI+YGH_ODW]
		MOV	[#ODWVRAM],EDX
		IMUL	EBX,EDX
		LEA	EBX,[EBX+EAX*2]
		ADD	EBX,[EDI+YGH_ADR]
		MOV	[#NEXTVRAM],EBX		;EBX:free

		MOV	[#NEXTDATA],ESI

		CMP	ECX,[EDI+YGH_VX2]
		JLE	#X2fixed
		MOV	ECX,[EDI+YGH_VX2]
#X2fixed:
		MOV	EBX,ECX
		SUB	EBX,EAX
		INC	EBX
		MOV	[#RESTBIT],EBX

		MOV	[#SKIPDOT],DWORD PTR 0
		MOV	[#SKIPBIT],DWORD PTR 0
		MOV	[#SKIPBYTE],DWORD PTR 0
		CMP	EAX,[EDI+YGH_VX1]
		JGE	#X1fixed
		MOV	EDX,[EDI+YGH_VX1]
		SUB	EDX,EAX
		MOV	EAX,EDX
		AND	EAX,7
		MOV	[#SKIPBIT],EAX
		MOV	EAX,EDX
		ADD	EAX,7
		AND	AL,0FFH-7
		SUB	[#RESTBIT],EAX
		SHR	EDX,3
		MOV	[#SKIPBYTE],EDX
		SHL	EDX,3
		MOV	[#SKIPDOT],EDX
#X1fixed:

		MOV	EAX,[#RESTBIT]
		MOV	ECX,EAX
		SHR	ECX,3
		MOV	[#DOBYTE],ECX
		AND	EAX,7
		MOV	[#DOBIT],EAX

		PUSH	DS
		POP	ES
		MOV	ECX,[EDI+YGH_COL]
		MOV	EAX,ECX
		SHL	EAX,16
		MOV	AX,CX
		MOV	ECX,[EDI+YGH_SEG]
		MOV	DS,CX			;EDI:free


		MOV	EDX,[#NEXTDATA]		;Seek Pointers to VX1
		ADD	EDX,[#SKIPBYTE]

		MOV	EBP,[#NEXTVRAM]
		ADD	EBP,[#SKIPDOT]
		ADD	EBP,[#SKIPDOT]

#LOOP0:		MOV	ESI,EDX
		MOV	EDI,EBP

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

		MOV	BL,ES:[ESI]
		MOV	ECX,[#SKIPBIT]
		JMP	CS:[#JTBL0+ECX*4]
		ALIGN	4
#JTBL0		DD	OFFSET #L00,OFFSET #L01,OFFSET #L02,OFFSET #L03
		DD	OFFSET #L04,OFFSET #L05,OFFSET #L06,OFFSET #L07
#L01:		PSET	6
#L02:		PSET	5
#L03:		PSET	4
#L04:		PSET	3
#L05:		PSET	2
#L06:		PSET	1
#L07:		PSET	0
		ADD	EDI,8*2
		INC	ESI
#L00:

		MOV	ECX,[#DOBYTE]
		JECXZ	#LOOP1OUT

		ALIGN	4
#LOOP1:		MOV	BL,ES:[ESI]
		INC	ESI
		AND	BL,BL
		JE	#LOOP1UP
		CMP	BL,255
		JE	#FLYING255
		PSET	0
		PSET	1
		PSET	2
		PSET	3
		PSET	4
		PSET	5
		PSET	6
		PSET	7
#LOOP1UP:	ADD	EDI,8*2
		LOOP	#LOOP1
#LOOP1OUT:
		MOV	BL,ES:[ESI]
		MOV	ECX,[#DOBIT]
		JMP	CS:[#JTBL1+ECX*4]
		ALIGN	4
#JTBL1		DD	OFFSET #L10,OFFSET #L11,OFFSET #L12,OFFSET #L13
		DD	OFFSET #L14,OFFSET #L15,OFFSET #L16,OFFSET #L17
#L17:		PSET	1
#L16:		PSET	2
#L15:		PSET	3
#L14:		PSET	4
#L13:		PSET	5
#L12:		PSET	6
#L11:		PSET	7
#L10:
		DEC	DWORD PTR [#YCOUNT]
		JNE	#LOOP0

		ADD	ESP,#ESPOFS
		POP	ES
		POP	DS
		RET

		ALIGN	4
#FLYING255:	MOV	[EDI   ],EAX
		MOV	[EDI+ 4],EAX
		MOV	[EDI+ 8],EAX
		MOV	[EDI+12],EAX
		ADD	EDI,8*2
		DEC	ECX
		JNE	#LOOP1
		JMP	#LOOP1OUT
PUT1COLOR	ENDP





		ALIGN	4
FLYINGPUT1COLOR	PROC

#ESPOFS		EQU	16
#DOBIT		EQU	ESP+12
#DOBYTE		EQU	ESP+8
#ODWVRAM	EQU	ESP+4
#YCOUNT		EQU	ESP

		PUSH	DS
		PUSH	ES
		SUB	ESP,#ESPOFS

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

		MOV	EDX,[EDI+YGH_ODW]
		MOV	[#ODWVRAM],EDX
		IMUL	EBX,EDX
		LEA	EBX,[EBX+EAX*2]
		ADD	EBX,[EDI+YGH_ADR]
		MOV	EBP,EBX				;EBP:NEXT VRAM

		SUB	ECX,EAX
		INC	ECX
		MOV	EAX,ECX
		SHR	ECX,3
		MOV	[#DOBYTE],ECX
		AND	EAX,7
		MOV	[#DOBIT],EAX

		MOV	ECX,[EDI+YGH_COL]
		MOV	EAX,ECX
		SHL	EAX,16
		MOV	AX,CX
		MOV	ECX,[EDI+YGH_SEG]
		PUSH	DS
		POP	ES
		MOV	DS,CX

#LOOP0:		MOV	EDI,EBP

		ADD	EBP,[#ODWVRAM]

		MOV	ECX,[#DOBYTE]
		JECXZ	#LOOP1OUT

		ALIGN	4
#LOOP1:		MOV	BL,ES:[ESI]
		INC	ESI
		AND	BL,BL
		JE	#LOOP1UP
		CMP	BL,255
		JE	#FLYING255
		PSET	0
		PSET	1
		PSET	2
		PSET	3
		PSET	4
		PSET	5
		PSET	6
		PSET	7
#LOOP1UP:	ADD	EDI,8*2
		LOOP	#LOOP1
#LOOP1OUT:
		MOV	BL,ES:[ESI]
		MOV	ECX,[#DOBIT]
		JMP	CS:[#JTBL1+ECX*4]
		ALIGN	4
#JTBL1		DD	OFFSET #L10,OFFSET #L11,OFFSET #L12,OFFSET #L13
		DD	OFFSET #L14,OFFSET #L15,OFFSET #L16,OFFSET #L17
#L17:		PSET	1
#L16:		PSET	2
#L15:		PSET	3
#L14:		PSET	4
#L13:		PSET	5
#L12:		PSET	6
#L11:		PSET	7
		INC	ESI
#L10:
		DEC	DWORD PTR [#YCOUNT]
		JNE	#LOOP0

		ADD	ESP,#ESPOFS
		POP	ES
		POP	DS
		RET

		ALIGN	4
#FLYING255:	MOV	[EDI   ],EAX
		MOV	[EDI+ 4],EAX
		MOV	[EDI+ 8],EAX
		MOV	[EDI+12],EAX
		ADD	EDI,8*2
		DEC	ECX
		JNE	#LOOP1
		JMP	#LOOP1OUT
FLYINGPUT1COLOR	ENDP



CODE		ENDS
		END
