	TITLE FVSUBS
DGROUP	GROUP _DATA
_DATA	SEGMENT PUBLIC 'DATA'
;	EXTRN FVLDM:BYTE ,BITS:BYTE ; COMMON ADDRESSES.
	ASSUME DS:DGROUP
FV1     DW	0
FV12	DW	0	;POINTER TO FV1
BITS	DW	0
BITS2	DW	0	;POINTER TO BITS
WK	DW	0	;WORK INTEGER
WK2	DW	0	;IN FVS HOLDS ABS (IVAL)
NFG	DW	0	;IN FVS HOLDS NEG FLG (1=YES)
_DATA	ENDS
FV_CODE	SEGMENT 'CODE'
	ASSUME CS:FV_CODE,SS:DGROUP
PUBLIC FVGO
FVGO	PROC	FAR
	PUSH	BP
	MOV	BP,SP
; CALL FVGO(FV1,IBITS)
; SAVES ADDRESSES FOR LATER
; BP+6 IS IBITS START
; BP+10 IS FV1 START
    LES       BX,DWORD PTR [BP].0AH
	MOV	FV12,ES
	MOV	FV1,BX
	LES	BX,DWORD PTR [BP].06H
	MOV	BITS2,ES
	MOV	BITS,BX	
    MOV       SP,BP
    POP       BP
    RET      0008H
FVGO	ENDP
PUBLIC FVG1,FVG2,FVS
; IVAL=FVG1(ID)
; WHERE FV1,FV2, AND FV4 ARE 2258 - BYTE ARRAYS IN THAT ORDER...
; ID = I*2, ALL OTHER ARGS ARE BYTE ADDRESSES...
FVG1 PROC FAR
    PUSH      BP
    MOV       BP,SP
    push  si
    LES       BX,DWORD PTR [BP].06H
; ID ADDRESS
    MOV       CX,ES:[BX]
;NOW MUST SEE IF ANYTHING IN FV1,FV2, OR FV3 BITS FOR THIS ID ARE NONZERO...
	DEC	CX		;FORM (ID-1)
	MOV	DX,CX		;SAVE IN DX TOO...OFFSET INTO IBIT...
;	DEC	CX		;FORM (ID-1)
	SHR	CX,1
	SAR	CX,1		;DIVIDE BY 8
	SAR	CX,1
;JUST LEAVE AS (ID-1)/8 SINCE NO NEED TO ADJUST
;THIS HAS ARRAY OFFSET ADDRESS
	LES	BX,DWORD PTR BITS		;GET BITS ADDRESS
	AND	DX,7
	MOV	SI,DX
	MOV	DL,ES:[BX][SI]			;GET BITMASK INTO DL
	XOR	DH,DH	;ZERO DH MASK
	MOV	SI,CX	;GET ARRAY OFFSET
	LES	BX,DWORD PTR FV1	;GET FV1
	MOV	AX,ES:[BX][SI]		;THEN GET BYTE WE WANT
	AND	AX,DX			;MASK WITH DX (DH=0 SO ONLY AL MATTERS)
	JNE	L44			;IF NONZERO, RETURN AT ONCE WITH 1
	ADD	SI,08D2H		;EXAMINE FV2
	MOV	AX,ES:[BX][SI]
	AND	AX,DX			;MASK FV2 WITH DX
	JNE	L44			;IF NONZERO RETURN NOW
	ADD	SI,08D2H		;LOOK AT FV4
	MOV	AX,ES:[BX][SI]
	AND	AX,DX			;IF NONZERO RETURN
	JE	L11			;IF ZERO RETURN 0
L44:
	MOV	AX,1
	JMP	L45
L11:
	XOR	AX,AX
L45:
; RETURN WITH RESULT OF MASK IN AX REGISTER... JUST ANDS 2 WORDS
; TOGETHER FOR SPEED...
    pop  si
    MOV       SP,BP
    POP       BP
    RET      0004H
FVG1 ENDP
FVG2	PROC	FAR		; IVAL=FVG2(ID)
; RETURNS CORRECT INTEGER IVAL GIVEN ID OF INPUT...
    PUSH      BP
    MOV       BP,SP
    push  si
    LES       BX,DWORD PTR [BP].06H
; ID ADDRESS
    MOV       CX,ES:[BX]
	MOV	WK,0		;ZERO WORK RESULT
;NOW MUST SEE IF ANYTHING IN FV1,FV2, OR FV3 BITS FOR THIS ID ARE NONZERO...
	DEC	CX		;FORM (ID-1)
	MOV	DX,CX		;SAVE IN DX TOO...OFFSET INTO IBIT...
;	DEC	CX		;FORM (ID-1)
	SHR	CX,1	;(USE SHR HERE TO ENSURE HI BIT NOT SIGN EXTENDED.
	SAR	CX,1		;DIVIDE BY 8
	SAR	CX,1
;JUST LEAVE AS (ID-1)/8 SINCE NO NEED TO ADJUST
;THIS HAS ARRAY OFFSET ADDRESS
	LES	BX,DWORD PTR BITS		;GET BITS ADDRESS
	AND	DX,7
	MOV	SI,DX
	MOV	DL,ES:[BX][SI]			;GET BITMASK INTO DL
	XOR	DH,DH	;ZERO DH MASK
	MOV	SI,CX	;GET ARRAY OFFSET
	LES	BX,DWORD PTR FV1	;GET FV1
	MOV	AX,ES:[BX][SI]		;THEN GET BYTE WE WANT
	AND	AX,DX			;MASK WITH DX (DH=0 SO ONLY AL MATTERS)
	JE	M40			;IF 0, LEAVE ALONE
	MOV	WK,1			;IF 1, SET RESULT 1 BIT
M40:
	ADD	SI,08D2H		;EXAMINE FV2
	MOV	AX,ES:[BX][SI]
	AND	AX,DX			;GET BIT THERE
	JE	M41
	ADD	WK,2			;ADD 2 IF FV2 BIT SET
M41:
	ADD	SI,08D2H		;LOOK AT FV4 NEXT
	MOV	AX,ES:[BX][SI]		;GET BYTE THERE
	MOV	BX,WK			;NOW HOLD WORK VALUE IN BX REGISTER
	AND	AX,DX
	JE	M42
	NEG	BX			;IF FV4 BIT IS SET, NEGATE RESULT
M42:
	MOV	AX,BX			;RETURN VALUE...
    pop  si
    MOV       SP,BP
    POP       BP
    RET      0004H
FVG2	ENDP
FVS PROC FAR
    PUSH      BP
    MOV       BP,SP
    push  si
; CALL FVS(ID,IVAL)
; SETS BITMAP BITS...
	LES	BX,DWORD PTR [BP].06H ;IVAL
	MOV	AX,ES:[BX]
	CBW			; SIGN EXTEND AL INTO AX
	MOV	NFG,0
	AND	AX,AX		;SET COND CODES
	JNS	N40		;IF + LEAVE ALONE
	MOV	NFG,1
	NEG	AX
N40:	;STORE SIGN IN NF, MAG IN WK2
	MOV	WK2,AX		;STORE IVAL LOCALLY IN WK2
	LES	BX,DWORD PTR [BP].0AH  ; ID OFFSET (.06 IS IVAL OFFSET...)
; ID ADDRESS
	MOV	CX,ES:[BX]
	MOV	WK,0		;ZERO WORK RESULT
;NOW MUST SEE IF ANYTHING IN FV1,FV2, OR FV3 BITS FOR THIS ID ARE NONZERO...
	DEC	CX		;FORM (ID-1)
	MOV	DX,CX		;SAVE IN DX TOO...OFFSET INTO IBIT...
;	DEC	CX		;FORM (ID-1)
	SHR	CX,1
	SAR	CX,1		;DIVIDE BY 8
	SAR	CX,1
;JUST LEAVE AS (ID-1)/8 SINCE NO NEED TO ADJUST
;THIS HAS ARRAY OFFSET ADDRESS
	LES	BX,DWORD PTR BITS		;GET BITS ADDRESS
	AND	DX,7
	MOV	SI,DX
	MOV	DL,ES:[BX][SI]			;GET BITMASK INTO DL
	XOR	DH,DH	;ZERO DH MASK
	MOV	SI,CX	;GET ARRAY OFFSET
	LES	BX,DWORD PTR FV1	;GET FV1
	OR	ES:[BX][SI],DX		;SET BIT DESIRED
	TEST	WK2,1		;SEE IF 1 BIT SET
	JNE	N41
	XOR	ES:[BX][SI],DX	;IF 1 BIT WAS NOT SET TURN BIT OFF...
N41:
	ADD	SI,08D2H	;LOOK AT FV2 NEXT
	OR	ES:[BX][SI],DX
	TEST	WK2,2		;SEE IF 2 BIT WAS SET
	JNE	N42
	XOR	ES:[BX][SI],DX ; TURN OFF IF NOT
N42:
	ADD	SI,08D2H
	OR	ES:[BX][SI],DX	;TURN FV4 BIT ON
	TEST	NFG,1		;SEE IF SIGN WAS ON
	JNE	N43
	XOR	ES:[BX][SI],DX	;TURN FV4 BIT OFF IF SIGN WAS +
N43:
    pop  si
    MOV       SP,BP
    POP       BP
    RET      0008H
FVS	ENDP
FV_CODE ENDS
	END


