
;
;  11.1 SamGen.mdf uses new fixed tag logic
;  Compatible with 11.1 Ctos.lib and CTOSVM 2.1
;
;Modified:  JJKDMO   71284  Add Gpam.  Make semireadable.
;           PKDDMO  121884  Add PUBLIC pProcRoutine.
;           DC       030685  Make SetImageMode generic.
;	        		DR       030685  Add fSameLooks to BsVid
;	        		AM       012287  Introduce new tags management
;	        		AM       040687  A new variable is used, LinkWithSam, if set 
;                              to 0FFh SAM Comm and printer not linked with 
;                              application.
;			101787 JM use tagProcs for sysin,sysout instead of tagProc.


NTag_tagDiskRead     equ 0
NTag_tagDiskWrite    equ 1
NTag_tagDiskModify   equ 2
NTag_tagKbdRead      equ 3
NTag_tagVideoWrite   equ 4
NTag_tagLptWrite     equ 5
NTag_tagPtrWrite     equ 6
NTag_tagNul          equ 7
NTag_tagCommRead     equ 8
NTag_tagCommWrite    equ 9
NTag_tagCommModify   equ 10
NTag_tagSplWrite     equ 11
NTag_tagSysIn        equ 12
NTag_tagSysOut       equ 13
NTag_tagGpamWrite    equ 14
NTag_tagCardC        equ 15
NTag_tagCardI        equ 16

NTag_tag8251Read     equ 17
NTag_tag8251Write    equ 18
NTag_tag8251Modify   equ 19
NTag_tagPtr8251Write equ 20
NTag_tagQicRead      equ 21
NTag_tagQicWrite     equ 22
NTag_tagTapeRead     equ 23
NTag_tagTapeWrite    equ 24
NTag_tagLptRead      equ 25
NTag_tagLptModify    equ 26


NTag_taglast         equ 0ffh


*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 NTag_tagName        ;NTag;
	IF (EQS(tagName,tagVideoWrite)) THEN(
             SET(tagVideoWriteVal,NTag_tagName))
	FI
	IF (EQS(tagName,tagKbdRead)) THEN(
             SET(tagKbdReadVal,NTag_tagName))
	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 NTag_tagName       ;NTag;
	IF (EQS(tagName,tagVideoWrite)) THEN(
             SET(tagVideoWriteVal,NTag_tagName))
	FI
	IF (EQS(tagName,tagKbdRead)) THEN(
             SET(tagKbdReadVal,NTag_tagName))
	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 ( tagProcs(tagSysIn,FillBufferSysIn,FlushBufIllegal,ChkptNop,ReleaseEasy,SetImageModeIllegal)
	     tagProcs(tagSysOut,FillBufIllegal,FlushBufferSysOut,CheckPointBsSysOut,ReleaseEasy,SetImageModeIllegal)

	tagLast DB NTag_tagLast    ;NTag;

	     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 Ntag_tagCT		;NTAG;
             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 (tagProcs(tagSysIn,FillBufferSysIn,FlushBufIllegal,ChkptNop,ReleaseEasy,SetImageModeIllegal)
	     tagProcs(tagSysOut,FillBufIllegal,FlushBufferSysOut,CheckPointBsSysOut,ReleaseEasy,SetImageModeIllegal) 

	tagLast DB NTag_tagLast    ;NTag;)
        FI
	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)
	CheckDef(tagLptRead)
	CheckDef(tagLptModify)
	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    ?


*DEFINE (OsSubLab ( n, pName ))
(SSEVAL(N8)   SEGMENT AT (0FFFFh(16*(N8)))
        ORG     (230h+(100h*(N8))+2*N10h*(N8))
PUBLIC  pName
pName  EQU     THIS BYTE
        SSEVAL(N8)   ENDS
)

*DEFINE (Same( original , copy ))(
PUBLIC copy
copy EQU original
)


PUBLIC FLink
IF (*ISDEF(LinkWithSam)) THEN (

Flink	DB	0

OsSubLab(541, IN_OpenByteStreamLpt)	;541
OsSubLab(542, IN_OpenByteStreamSpecialC)	;542
OsSubLab(543, IN_FlushBufferLpt)	;543
OsSubLab(544, IN_FlushBufferC)	;544
OsSubLab(545, IN_CheckPointBsLpt)	;545
OsSubLab(546, IN_CheckpointBsAsyncC)	;546
OsSubLab(547, IN_ReleaseByteStreamLpt)	;547
OsSubLab(548, IN_ReleaseByteStreamC)	;548
OsSubLab(549, IN_SetImageModeLpt)	;549
OsSubLab(550, IN_SetImageModeC)	;550
OsSubLab(551, IN_CheckPointPhy)	;551
OsSubLab(552, IN_QueryPrtStatus)	;552
OsSubLab(553, IN_ReadByteStreamParameterLpt)	;553
OsSubLab(554, IN_WriteByteStreamParameterLpt)	;554
OsSubLab(555, IN_FlushbufferAsyncC)	;555
OsSubLab(556, IN_ReadByteStreamParameterC)	;556
OsSubLab(557, IN_WriteByteStreamParameterC)	;557
OsSubLab(558, IN_CheckForOperatorRestartC)	;558
OsSubLab(559, IN_DiscardOutputBsc)	;559
OsSubLab(560, IN_FillBufferAsyncC)	;560
OsSubLab(561, IN_DiscardinputBsc)	;561
OsSubLab(562, IN_ReadStatusc)	;562
OsSubLab(563, IN_AcquireByteStreamC)	;563
OsSubLab(564, IN_WriteStatusc)	;564
OsSubLab(565, IN_SendBreakC)	;565
OsSubLab(566, IN_CheckPointBsc)	;566
OsSubLab(567, IN_FillBufferLpt)	;567	 

) ELSE (
Flink	DB	0FFh
) FI

DATA	ENDS
                END)
	IF (NOT(ISDEF(ReleaseProc))) THEN(
	     ??SEG	SEGMENT
             EXTRN	ReleaseProc:FAR
	     ??SEG	ENDS)
	FI
	DD	ReleaseProc)

*DEFINE (DevDepProc(ProcName,ProcRoutine)) (
	IF (DevDepFlag EQ 0) THEN ( tagProcs(tagSysIn,FillBufferSysIn,FlushBufIllegal,ChkptNop,ReleaseEas