;Modified:  JJKDMO   71284  Add Gpam.  Make semireadable.
;           PKDDMO  121884  Add PUBLIC pProcRoutine.
;           DC       030685  Make SetImageMode generic.
;	    DR       030685  Add fSameLooks to BsVid
;	    PBC      123085  Add MF 8251 tags

*DEFINE (Init) (
	PUBLIC OpenTable,sTagProcs,tagTable,tagLast,deviceLast
	PUBLIC fDiskBsDef,DiskBsOpen,fSplBsDef,SplBsOpen,fGpamBsDef,GpamBsOpen
	DGROUP GROUP DATA
	SET(DevNum,0)
	SET(tag,0)
	SET(segFlag,0)
	SET(DiskFlag,0)
	SET(SplFlag,0)
	SET(GpamFlag,0)
	SET(DevDepFlag,0)
	SET(tagVideoWriteVal,0FFH)
	SET(tagKbdReadVal,0FFH)
	DEFINE (OpenProcDisk) (0)
	DEFINE (OpenProcSpl) (0)
	DEFINE (OpenProcGpam) (0)
	DATA	SEGMENT PUBLIC 'DATA'
	xDisk	LABEL	BYTE
	ySpl	LABEL	BYTE
	zGpam LABEL BYTE
	)

*DEFINE (DeviceOpen(DeviceName,OpenProc)) (

	IF (NOT(ISDEF(OpenProc))) THEN (
		??SEG	SEGMENT
		EXTRN	OpenProc:FAR
		??SEG	ENDS
		) FI

	DEFINE(DevName_Gpam)(zSUBSTR(DeviceName,2,LEN(DeviceName)2))
	DEFINE(DevName_Disk)(xSUBSTR(DeviceName,2,LEN(DeviceName)2))
	DEFINE(DevName_Spl) (ySUBSTR(DeviceName,2,LEN(DeviceName)2))

	IF (NOT(ISDEF(DevName_Disk))) THEN (

		IF (NOT(ISDEF(DevName_Spl))) THEN (

			IF (NOT(ISDEF(DevName_Gpam))) THEN (
				IF (DevNum EQ 0) THEN (OpenTable LABEL BYTE) FI
				SET(DevNum, DevNum+1)
				DB	LEN(DeviceName)
				DB	'DeviceName'
				DD	OpenProc
				)
			ELSE (
				DEFINE (OpenProcGpam) (OpenProc)
				SET(GpamFlag,0FFh)
				) FI
			)

		ELSE (	
			DEFINE (OpenProcSpl) (OpenProc)
			SET(SplFlag,0FFH)
			) FI

		)
	ELSE (	
		DEFINE (OpenProcDisk) (OpenProc)
		SET(DiskFlag,0FFH)
		) FI
	)

'  tagProcs  used only with 10.0 and later SamGens.
'  Earlier SamGens used tagProc, which does not include SetImageMode.
'
*DEFINE (tagProcs(tagName,FillProc,FlushProc,CheckPointProc,ReleaseProc,SetImageModeProc)) (
	IF (tag EQ 0) THEN (
	     DeviceOpen([SysIn],OpenByteStreamSysIn)
	     DeviceOpen([SysOut],OpenByteStreamSysOut)
	     sTagProcs   DW 21
	     tagTable    LABEL DWORD)
	FI
	PUBLIC  tagName
	tagName DB tag
	IF (EQS(tagName,tagVideoWrite)) THEN(
             SET(tagVideoWriteVal,tag))
	FI
	IF (EQS(tagName,tagKbdRead)) THEN(
             SET(tagKbdReadVal,tag))
	FI
	SET(tag, tag+1)
	IF (NOT(ISDEF(FillProc))) THEN(
 	     ??SEG	SEGMENT
             EXTRN	FillProc:FAR
	     ??SEG	ENDS)
	FI
	DD	FillProc
	IF (NOT(ISDEF(FlushProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	FlushProc:FAR
	     ??SEG	ENDS)
	FI
	DD      FlushProc
	IF (NOT(ISDEF(CheckPointProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	CheckPointProc:FAR
	     ??SEG	ENDS)
	FI
	DD	CheckPointProc
	IF (NOT(ISDEF(ReleaseProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	ReleaseProc:FAR
	     ??SEG	ENDS)
	FI
	DD	ReleaseProc
	IF (NOT(ISDEF(SetImageModeProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	SetImageModeProc:FAR
	     ??SEG	ENDS)
	FI
	DD	SetImageModeProc)

'  tagProc  used only with priorto10.0 format Samgens.
'  (Included so such Samgens can still be compiled and used with 10.0
'   CTOS.)
'  10.0 and later SamGens use tagProcs, a tagProc replacement which
'   includes SetImageMode.
'
*DEFINE (tagProc(tagName,FillProc,FlushProc,CheckPointProc,ReleaseProc)) (
	IF (tag EQ 0) THEN (
	     DeviceOpen([SysIn],OpenByteStreamSysIn)
	     DeviceOpen([SysOut],OpenByteStreamSysOut)
	     sTagProcs   DW 17
	     tagTable    LABEL DWORD)
	FI
	PUBLIC  tagName
	tagName DB tag
	IF (EQS(tagName,tagVideoWrite)) THEN(
             SET(tagVideoWriteVal,tag))
	FI
	IF (EQS(tagName,tagKbdRead)) THEN(
             SET(tagKbdReadVal,tag))
	FI
	SET(tag, tag+1)
	IF (NOT(ISDEF(FillProc))) THEN(
 	     ??SEG	SEGMENT
             EXTRN	FillProc:FAR
	     ??SEG	ENDS)
	FI
	DD	FillProc
	IF (NOT(ISDEF(FlushProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	FlushProc:FAR
	     ??SEG	ENDS)
	FI
	DD      FlushProc
	IF (NOT(ISDEF(CheckPointProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	CheckPointProc:FAR
	     ??SEG	ENDS)
	FI
	DD	CheckPointProc
	IF (NOT(ISDEF(ReleaseProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	ReleaseProc:FAR
	     ??SEG	ENDS)
	FI
	DD	ReleaseProc)

*DEFINE (DevDepProc(ProcName,ProcRoutine)) (
	IF (DevDepFlag EQ 0) THEN (
	     tagProc(tagSysIn,FillBufferSysIn,FlushBufIllegal,ChkptNop,ReleaseEasy)
	     tagProc(tagSysOut,FillBufIllegal,FlushBufferSysOut,CheckPointBsSysOut,ReleaseEasy)
	     DATA ENDS
	     SAMGEN SEGMENT PUBLIC 'CODE'
	     xQueryVidBs LABEL FAR
	     yQueryBsVid LABEL FAR
	     SAMGEN ENDS
	     SET(DevDepFlag,1))
	FI

	PUBLIC pProcRoutine
	DATA	SEGMENT PUBLIC 'DATA'
	pProcRoutine DD ProcRoutine
	DATA	ENDS

	SAMGEN SEGMENT PUBLIC 'CODE'
	ASSUME CS:SAMGEN
	ASSUME DS:DGROUP
	IF (ISDEF(xProcName)) THEN (
	     QueryBsVid LABEL FAR
	     PUBLIC QueryBsVid)
	FI
	IF (ISDEF(yProcName)) THEN (
	     QueryVidBs LABEL FAR
 	     PUBLIC QueryVidBs)
	FI
        ??SEG 	SEGMENT
	EXTRN	ProcRoutine:FAR
	??SEG	ENDS
	PUBLIC ProcName
	ProcName PROC FAR
		  PUSH BP
		  MOV  BP,SP
		  POP  BP
		  JMP  DWORD PTR pProcRoutine
		  RET
	ProcName ENDP
	SAMGEN ENDS)

*DEFINE (CheckDef(tagCT)) (
	IF (NOT(ISDEF(tagCT))) THEN(
             tagCT	DB tag
             PUBLIC	tagCT
	IF (EQS(tagCT,tagVideoWrite)) THEN(
             SET(tagVideoWriteVal,tag))
	FI
	IF (EQS(tagCT,tagKbdRead)) THEN(
             SET(tagKbdReadVal,tag))
	FI
             SET(tag,tag+1))
	FI)

*DEFINE (Final) (
	IF (DevDepFlag NE 0) THEN (
	     DATA SEGMENT PUBLIC 'DATA')
	ELSE (tagProc(tagSysIn,FillBufferSysIn,FlushBufIllegal,ChkptNop,ReleaseEasy)
	     tagProc(tagSysOut,FillBufIllegal,FlushBufferSysOut,CheckPointBsSysOut,ReleaseEasy))
        FI
	tagLast    DB tag
	SET(tag,tag+1)
	deviceLast DB DevNum
	fDiskBsDef DB DiskFlag
	DiskBsOpen DD OpenProcDisk
        fSplBsDef  DB SplFlag
	SplBsOpen  DD OpenProcSpl
	fGpamBsDef DB GpamFlag
	GpamBsOpen DD OpenProcGpam
	CheckDef(tagDiskRead)
	CheckDef(tagDiskWrite)
	CheckDef(tagDiskModify)
	CheckDef(tagKbdRead)
	CheckDef(tagVideoWrite)
	CheckDef(tagLptWrite)
	CheckDef(tagPtrWrite)
	CheckDef(tagNul)
	CheckDef(tagCommRead)
	CheckDef(tagCommWrite)
	CheckDef(tagCommModify)
	CheckDef(tag8251Read)
	CheckDef(tag8251Write)
	CheckDef(tag8251Modify)
	CheckDef(tagPtr8251Write)
	CheckDef(tagSplWrite)
	CheckDef(tagGpamWrite)
	PUBLIC  bsVid
	EVEN
	bsVid   LABEL DWORD
	        DD    0
                DW    0
                DW    0
                DW    0
                DB    0
                DB    0
                DB    0
                DB    tagVideoWriteVal
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
                DB    0
	PUBLIC  bsKbd
	EVEN
	bsKbd   LABEL DWORD
	        DD    0
                DW    0
                DW    0
                DW    0
                DB    0FFH
                DB    0
                DB    0
                DB    tagKbdReadVal
                DB    0
                DB    0
                DB    0
PUBLIC pPrepWriteMMD
PUBLIC pPrepReadMMD
pPrepWriteMMD   DD    ?
pPrepReadMMD    DD    ?
DATA	ENDS
                END)
