;
;		Perlin Production Co. Ltd.	1992,1993 (c).
;		Saint - Petersburg	phone: 588-12-35.
;
;	Procedures to LOAD and PRINT graphical screen
;	from file *.SCR in a binary non-packed format.
;
;		     For use in GRAPH.REL library.


	TITLE 2DSCI

ERROR	EQU	0FFH;	Standart BDOS error flag
RAMDRV	EQU	10H;	Ramdisk port of VECTOR-06c computer
BDOS	EQU	5
SYMOUT	EQU	2;	BDOS char out
STROUT	EQU	9;	BDOS string out	
DSKSRES	EQU	0DH;	BDOS reset of ALL disk drives
FOPEN	EQU	0FH;	BDOS open file if it exists
FCLOSE	EQU	10H;	BDOS close file
FERASE	EQU	13H;	BDOS erase file
READREC	EQU	14H;	BDOS record (128 bytes) read
SAVEREC	EQU	15H;	BDOS save record
FCREAT	EQU	16H;	BDOS create & open file
GETDRV	EQU	19H;	BDOS get current drive
SETDMA	EQU	1AH;	BDOS set DMA addres



SAVESTACK	MACRO
	LXI H,0
	DAD SP
	SHLD STACK
	LXI SP,0FFH
	SPHL
	ENDM

LOADSTACK	MACRO
	LHLD STACK
	SPHL

;MVI C,DSKSRES;	Disks reset resets not only DMA & FCB addresses but
;CALL BDOS;	also byte 4 of RAM, where are current drives and users

	LXI D,80H;	That is why we must use our own machine
	MVI C,SETDMA;	independent SET standart DMA addres function.
	CALL BDOS;	( Standart DMA in all CPM is at 0080 HEX )

	ENDM

PUSHREGS	MACRO
	PUSH H
	PUSH D
	PUSH B
	PUSH PSW
	ENDM

POPREGS		MACRO
	POP PSW
	POP B
	POP D
	POP H
	ENDM


MAIN:	XRA A
	STA EXEFLG;	Only to screen
	LXI H,5DH
	MOV A,M
	CPI 20H
	JNZ VFYINP
	LXI D,USAGE
	CALL PUTS
	RET
VFYINP:	CALL INIFCB
	LXI H,82H
	CALL DEL20H
	LXI D,F1
	MOV A,M
	ANI 7FH
	CPI 'A'
	JM MAIN0
	CPI 'Z'+1
	JM MAIN1
MAIN0:	LXI D,FNERR
	CALL PUTS
	RET
MAIN1:	MVI C,9
MAIN2:	MOV A,M
	CPI '.'
	JZ VFYEXT
	CPI ' '
	JZ NOEXT
	ORA A
	JZ EXECUTE
	STAX D
	INX H
	INX D
	DCR C
	JNZ MAIN2
	MOV A,M
	CPI ' '
	JZ NOEXT
	CPI '.'
	JZ VFYEXT
	LXI D,FNERR
	CALL PUTS
	RET
VFYEXT:	INX H
	LXI D,T1
	XCHG
	MVI C,3
VFYSCR:	LDAX D
	ANI 7FH
	MOV M,A
	INX H
	INX D
	DCR C
	JNZ VFYSCR
	XCHG
	MOV A,M
	CPI 20H
	JZ NOEXT
	ORA A
	JZ EXECUTE
	LXI D,EXTERR
	CALL PUTS
	RET
NOEXT:	CALL DEL20H
	MOV A,M
	ORA A
	JZ EXECUTE
	ANI 5FH
	CPI 'N'
	JZ LDPRT
	CPI 'P'
	JNZ MAIN3
	MVI A,0FH
	STA INV
	JMP LDPRT
MAIN3:	LXI D,PARMER
	CALL PUTS
	RET
LDPRT:	MVI A,0FFH
	STA EXEFLG
	INX H
	MOV A,M
	ORA A
	JNZ MAIN3
EXECUTE: SAVESTACK
	CALL GETSCR
	LDA EXEFLG
	ORA A
	JZ LDONLY
	LXI SP,0FFH
	CALL HRDCPY
LDONLY:	LOADSTACK
	RET

DEL20H:	PUSH PSW
DEL21:	MOV A,M
	CPI ' '
	JNZ DEL2E
	INX H
	JMP DEL21
DEL2E:	POP PSW
	RET

PUTS:	PUSHREGS
	MVI C,STROUT
	CALL BDOS
	POPREGS
	
INIFCB:	LXI H,FCB
	LXI B,36
INF1:	MOV M,B
	INX H
	DCR C
	JNZ INF1
	LXI H,FSCR
	LXI D,F1
	MVI C,11;	Length of filename and extension
INF2:	MOV A,M
	STAX D
	INX H
	INX D
	DCR C
	JNZ INF2
	RET


GETSCR:	LXI D,FCB
	MVI C,FOPEN
	CALL BDOS
	CPI ERROR
	JNZ LDS1
	LXI D,NOFILE
	CALL PUTS
	LOADSTACK
	RET
LDS1:	MVI C,SYMOUT
	LXI D,0CH
	CALL BDOS
	MVI C,SETDMA
	LXI D,DMA
	CALL BDOS
	LXI H,0A000H
LDS2:	PUSH H
	LXI D,FCB
	MVI C,READREC
	CALL BDOS
	POP H
	ORA A
	JNZ ENDFILE
	DI
	XRA A
	OUT RAMDRV
	MVI C,128
	LXI D,DMA
LDS3:	LDAX D
	MOV M,A
	INX D
	INX H
	MOV A,H
	CPI 0E0H
	JP ENDFILE	
	DCR C
	JNZ LDS3
	MVI A,23H
	OUT RAMDRV
	EI
	JMP LDS2
ENDFILE: MVI A,23H
	OUT RAMDRV
	EI
	RET




;	This procedure prints graphical screen


HRDCPY:	DI
	XRA A
	OUT RAMDRV
	MVI	A,83H	
	OUT	4	
	CALL	PRINT	
	DB	0,0,10,13,0FFH	
	LXI	H,080FFH
	MVI	C,64	
HRDC1:	CALL	LINE
	DCR	L	
	DCR	L	
	DCR	L	
	DCR	L
	PUSHREGS
	MVI A,23H
	OUT RAMDRV
	LXI D,0FFH
	MVI C,6
	CALL BDOS
	CPI 11H;	Exit if ^Q
	JZ HRDEX
	XRA A
	OUT RAMDRV
	POPREGS		
	DCR	C	
	JNZ	HRDC1
	MVI A,23H
	OUT RAMDRV
HRDEX:	EI
	JMP 0

LINE:	PUSH 	H	
	PUSH	B	
	CALL	LIN	
	LDA	R	
	ANI	1	
	CNZ	LIN;		If double print
LE1:	CALL	PRINT	
	DB	1BH,4AH,24,0FFH		;Carriage return
	POP	B	
	POP	H	
	RET

LIN:	PUSH	H	
	CALL	PRINT	
	DB	1BH,2AH,0,0,2,0FFH	
	MVI	C,32	
	MVI	B,80H	
LN2:	CALL	SLO	
	MOV	A,B	
	RRC		
	MOV	B,A	
	JNC	LN2		
	INR	H	
	DCR	C	
	JNZ	LN2	
	CALL	PRINT	
	DB	0,13,0FFH	
	POP	H	
	RET

INE:	PUSH	B	
	MVI	C,0	
	LXI	D,2000H	;	INPUT:	<B> - mask
	MOV	A,M	;	<HL>-graph. address from 8000H to 9FFFH	
	ANA	B	;
	JZ	I1	;	OUTP:	E-logical pixel color from 0 to 15
	MVI	C,8
I1:	DAD	D	
	MOV	A,M	
	ANA	B	
	JZ	I2	
	MVI	A,4	
	ADD	C	
	MOV	C,A	
I2:	DAD	D	
	MOV	A,M	
	ANA	B	
	JZ	I3	
	INR	C	
	INR	C	
I3:	DAD	D	
	MOV	A,M	
	ANA	B	
	JZ	I4	
	INR	C	
I4:	LDA	INV	
	XRA	C
	MOV	C,A	
	MVI A,6;	Two video-panels 0110
	ANA	C	
	ANI	0FH	
	MOV	E,A	
	CALL	DTO	
	POP	B	
	RET		
DTO:	;----------------- Double pixel dot ----------------------
	LDA	R	
	ANI	2	
	RZ			
	MOV	A,E	
	RRC	
	RRC	
	ORA	E	
	MOV	E,A	
	RLC		
	RLC		
	ORA	E	
	ANI	0FFH	
	MOV	E,A	
	RET		
SLO:	;--------------------- Word ----------------------
	PUSH	B	
	PUSH	H	
	MVI	A,0	
	STA	PI	
	STA	PI+1	
	CALL	PIB	
	CALL	PIB	
	CALL	PIB	
	CALL	PIB	
	LDA	PI+1	
	RRC		
	MOV	C,A	
	CALL	PRN	
	LDA	PI	
	RRC		
	MOV	C,A	
	CALL	SLO1	
	CALL	PRN	
	POP	H	
	POP	B	
	RET		
SLO1:	ANI	55H	
	RLC		
	MOV	B,A	
	MOV	A,C	
	ANI	0AAH	
	RRC		
	ORA	B	
	MOV	C,A	
	RET

PIB:	PUSH	H	
	CALL	INE	
	PUSH	B	
	LDA	PI	
	MOV	C,A	
	LDA	PI+1	
	MOV	B,A	
	MOV	A,E	
 	ANI	1	
	ORA	C	
	RLC		
	MOV	C,A	;1
	MOV	A,E	
	RRC		
	MOV	E,A	
	ANI	1	
	ORA	B	
	RLC		
	MOV	B,A	;2
	MOV	A,E	
	RRC		
	MOV	E,A	
	ANI	1	
	ORA	C	
	RLC		
	STA	PI	;3
	MOV	A,E	
	RRC		
	ANI	1	
	ORA	B	
	RLC		
	STA	PI+1	;4
	POP	B	
	POP	H	
	DCR	L	
	RET

PRINT:	XTHL	
	PUSH	B	
	PUSH	PSW	;	P R I N T S bytes
PR5:	MOV	A,M	;	from CALL PRINT to 0FFH	
	MOV	C,A	
	CPI	0FFH	
	JZ	PR6	
	CALL	PRN	
	INX	H	
	JMP	PR5	
PR6:	INX	H	
	POP	PSW	
	POP	B	
	XTHL		
	RET

PRN:	PUSH	PSW	;	INP:	<C>
	MOV	A,C	
	OUT	7	
P1:	IN	5	
	RRC		
	JC	P1	
	MVI	A,8	
	OUT	4	
	NOP		
	NOP		
	MVI	A,9	
	OUT	4	
	POP	PSW	
	RET		

R:	DB	3	;	Mode : double-1,double pixel-2
INV:	DB	0	;	Negative - 0   Positive - 0FH
PI:	DW	0	;	Pixel
	
;	File control block
FCB:
DR:	DB 0;	current drive initiates
F1:	REPT 8
	 DB 20H
	ENDM
T1:	REPT 3
	 DB 20H
	ENDM
EX:	DB 0;	number fo extent (byte fills by system)
S1:	DB 0;	level of using of current
S2:	DB 0;	extent by a file
RC:	DB 0
D0:	REPT 16
	 DB 0
	ENDM
CR:	DB 0
R0:	DB 0
R1:	DB 0
R2:	DB 0

;	End of --- FCB ---

FSCR:	DB '        SCR'
TMPVAR:	DW 0
STACK:	DW 0
NOFILE:	DB 7,1BH,5BH,0DH,0AH,'Can''t find file on a current disk.$'
FNERR:	DB 7,1BH,5BH,0DH,0AH,'ERROR in input filename.$'
USAGE:	DB 1BH,5BH,'Perlin Production Co.Ltd. 2DSCI v.2.0 1992,1993 (c).'
	DB 0DH,0AH,'This utility shows & prints binary non-packed files.'
	DB 0DH,0AH,'     ^Q  -  TERMINATES PRINTING SCREEN.'
	DB 0DH,0AH,'  File can have any extension ( default .SCR ).'
	DB 0DH,0AH
	DB 0DH,0AH,'     Usage :',1BH,62H,' 2DSCI filename[.SCR] [KEY] '
	DB 1BH,61H,0DH,0AH
	DB '[KEY] mean:',0DH,0AH
	DB 1BH,62H,' N-make NEGATIVE HARD COPY    P-make POSITIVE HARD COPY. '
	DB 1BH,61H,0DH,0AH
	DB 0DH,0AH,'<<< MaChiNE dEPeNdeNt >>> for VECTOR-06c COMPUTER.'
	DB 0DH,0AH,'PPC touch phone in Saint Petersburg : 588-12-35.$'
EXTERR:	DB 7,1BH,5BH,0DH,0AH,'ERROR in extension.$'
PARMER:	DB 7,1BH,5BH,0DH,0AH,'ERROR in input parameter.$'
EXEFLG:	DB 0;	0 - Load picture only to screen , NON-ZERO - and print.
;	--- DMA ---
DMA:
	REPT 128
	 DB 0
	ENDM

; End   --- DMA ---

	END
