		.386p
		ASSUME	CS:CODE,DS:DATA
		INCLUDE	YGHASM.H

		PUBLIC	YGH_print8font

DATA		SEGMENT
		EXTRN	YGH_8font:BYTE
DATA		ENDS

CODE		SEGMENT
qSTRING		EQU	EBP+20
qY		EQU	EBP+16
qX		EQU	EBP+12
qPAGE		EQU	EBP+8
qRETADR		EQU	EBP+4
qEBP		EQU	EBP
qVX1		EQU	EBP-4
qVX2		EQU	EBP-8
qODW		EQU	EBP-12
qADR		EQU	EBP-16
qVRAM		EQU	EBP-20
qY1		EQU	EBP-24
qDY		EQU	EBP-28
qCOL		EQU	EBP-32
qSKIPY		EQU	EBP-36
qDRAWY		EQU	EBP-40
qESPOFS		EQU	40

		ALIGN	4
YGH_print8font	PROC
		PUSH	EBP
		MOV	EBP,ESP
		SUB	ESP,qESPOFS
		PUSH	ESI
		PUSH	EDI
		PUSH	EBX
		PUSH	ES

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

		MOV	EAX,[EDI+YGH_ODW]
		MOV	[qODW],EAX
		MOV	EAX,[EDI+YGH_ADR]
		MOV	[qADR],EAX
		MOV	EAX,[EDI+YGH_VX1]
		MOV	EBX,[EDI+YGH_VX2]
		MOV	[qVX1],EAX
		MOV	[qVX2],EBX
		MOV	EAX,[EDI+YGH_COL]
		MOV	[qCOL],EAX

		MOV	EAX,[qY]
		CMP	EAX,[EDI+YGH_VY1]
		JL	#EXIT
		SUB	EAX,7
		CMP	EAX,[EDI+YGH_VY2]
		JG	#EXIT

		CMP	EAX,[EDI+YGH_VY1]
		MOV	EBX,0
		JGE	#Y1isFixed
		MOV	EBX,[EDI+YGH_VY1]
		SUB	EBX,EAX
		MOV	EAX,[EDI+YGH_VY1]
#Y1isFixed:	MOV	[qSKIPY],EBX
		MOV	[qY1],EAX

		MOV	EAX,[qY]
		CMP	EAX,[EDI+YGH_VY2]
		JLE	#Y2isFixed
		MOV	EAX,[EDI+YGH_VY2]
#Y2isFixed:	SUB	EAX,[qY1]
		INC	EAX
		MOV	[qDRAWY],EAX



		MOV	EDX,[qCOL]		;EDX:color (must not change)


		MOV	EAX,[qX]		;EDI:PAGEはもう使わんはず
		CMP	EAX,[qVX1]
		JGE	#LOOP0OUT
		MOV	ESI,[qSTRING]
#LOOP0:		CMP	BYTE PTR [ESI],0
		JE	#EXIT
		INC	ESI
		ADD	EAX,8
		CMP	EAX,[qVX1]
		JL	#LOOP0
		MOV	[qSTRING],ESI
		JE	#LOOP0OUT

		MOV	EDI,[qY1]
		IMUL	EDI,[qODW]
		ADD	EDI,[qADR]
		ADD	EDI,[qVX1]
		ADD	EDI,[qVX1]

		MOV	EBX,EAX
		SUB	EBX,[qVX1]
		MOV	ECX,8
		SUB	ECX,EBX
		ADD	EAX,8
		CMP	EAX,[qVX2]
		JG	#LOOP1OUT
		SUB	EAX,8

		PUSH	EAX
		MOVZX	ESI,BYTE PTR [ESI-1]
		LEA	ESI,[YGH_8font+ESI*8]
		ADD	ESI,[qSKIPY]
		MOV	EAX,[qDRAWY]
		CALL	ONELETTER
		POP	EAX
#LOOP0OUT:

		MOV	EDI,[qY1]
		IMUL	EDI,[qODW]
		ADD	EDI,[qADR]
		LEA	EDI,[EDI+EAX*2]		;EDI:VRAM
		MOV	ECX,0
		ADD	EAX,8
		CMP	EAX,[qVX2]
		JG	#LOOP1OUT

		MOV	EBX,EAX			;EBX:X+8(次の文字のX)

		ALIGN	4
#LOOP1:		MOV	ESI,[qSTRING]
		MOVZX	ESI,BYTE PTR [ESI]
		AND	ESI,ESI
		JE	#EXIT
		LEA	ESI,[YGH_8font+ESI*8]
		ADD	ESI,[qSKIPY]

		MOV	ECX,[qDRAWY]

		CALL	FLYINGONELETTER

		ADD	EDI,16
		INC	DWORD PTR [qSTRING]
		ADD	EBX,8
		CMP	EBX,[qVX2]
		JLE	#LOOP1

		MOV	EAX,EBX
		MOV	ECX,0

;EAX:must be Next X  ECX:must be skipX  EDX:must be color  EDI:must be VRAM
#LOOP1OUT:	SUB	EAX,8

		MOV	ESI,[qSTRING]
		MOVZX	ESI,BYTE PTR [ESI]
		AND	ESI,ESI
		JE	#EXIT
		LEA	ESI,[ESI*8 + YGH_8font]
		ADD	ESI,[qSKIPY]

		MOV	EBX,[qVX2]
		INC	EBX
		SUB	EBX,EAX
		JLE	#EXIT

		MOV	EAX,[qDRAWY]

		CALL	ONELETTER

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



ONEDOT		MACRO	N
		LOCAL	NODOT
		BT	EAX,7-N
		JNC	SHORT NODOT
		MOV	ES:[EDI+N*2],DX
NODOT:
		ENDM




;ESI:Data Top  EDI:Vram Top  EAX:drawY  EBX:drawx  ECX:skipx  EDX:color
;EBP:Local Variable Pointer
;ESI,EAX will be destroyed
ONELETTER	PROC
		PUSH	EDI
		MOV	AH,AL		;AH:drawY

		ALIGN	4
#LOOP0:		LODSB
		SHL	AL,CL
		JMP	CS:[#JTBL+EBX*4]
		ALIGN	4
#JTBL		DD	OFFSET #L08,OFFSET #L01,OFFSET #L02,OFFSET #L03
		DD	OFFSET #L04,OFFSET #L05,OFFSET #L06,OFFSET #L07
		DD	OFFSET #L08
#L08:		ONEDOT	7		
#L07:		ONEDOT	6
#L06:		ONEDOT	5		
#L05:		ONEDOT	4		
#L04:		ONEDOT	3		
#L03:		ONEDOT	2		
#L02:		ONEDOT	1		
#L01:		ONEDOT	0

		ADD	EDI,[qODW]
		DEC	AH
		JNE	#LOOP0

		POP	EDI
		RET
ONELETTER	ENDP




;ESI:Data Top  EDI:Vram Top  ECX:drawY  EDX:color
;EBP:Local Variable Pointer
;ESI,EAX,ECX will be destroyed
FLYINGONELETTER	PROC
		PUSH	EDI

		ALIGN	4
#LOOP0:		LODSB
		ONEDOT	7		
		ONEDOT	6
		ONEDOT	5		
		ONEDOT	4		
		ONEDOT	3		
		ONEDOT	2		
		ONEDOT	1		
		ONEDOT	0

		ADD	EDI,[qODW]
		LOOP	#LOOP0

		POP	EDI
		RET
FLYINGONELETTER	ENDP

CODE		ENDS
		END
