	page 55
	title 'SRCTOBIN.MAC'

;
;	Perlin Production Co. Ltd. July 1993 (c)
;	Convertor of packed SCR files into 32K VRAM bitmap file.
;

MACLIB	CPMLIB.LIB


DTA	EQU BIOSNEST+2000H

	lxi sp,stack
	lxi h,0
	push h
	lda SFCBN
	cpi 20h
	jnz s1
	puts usage
	ret
s1:	mvi c,_OPEN
	lxi d,SFCB
	call BDOS
	cpi BDOSERR
	jnz s3
	puts cantop
	lxi h,SFCBN
	mvi b,11
	call putbytes
	puts opnerr
	ret

;	Fill output FCB
s3:	movmem wfcb,SFCB,9

;	Move BIOS to its temporary nest
	movmem BIOSNEST,0E000H,2000H

;	Load picture in memory after end of BIOSNEST
	lxi d,DTA
s4:	push d
	mvi c,_SETDMA
	call BDOS
	lxi d,SFCB
	mvi c,_READ
	call BDOS
	pop d
	lxi h,RECSIZ
	dad d
	xchg
	ora a
	jz s4
	DI
;	Save System Page Area in Memory
	movmem SYSPAGE,0,0ffh

	LXI H,PRER
	SHLD 39H
	xra a;	Disable DOS page
	out 10h
	EI
	lxi h,DTA
	CALL REKOMP
	di

;	Move picture to DTA
	movmem DTA,8000H,8000H

;	Restore System Page Area
	movmem 0,SYSPAGE,0ffh

;	Replace BIOS where it lives
	movmem 0E000H,BIOSNEST,1FFFH


	mvi a,23h;	Enable DOS page
	out 10h
	ei

;	And save usqueezed binary file .BIN

	lxi d,wfcb
	mvi c,_DELETE
	call BDOS
	lxi d,wfcb
	mvi c,_CREAT
	call BDOS
	cpi BDOSERR
	jnz s11
	puts noplace
	jmp 0
s11:	call whalf
	di
	xra a
	out 10h
	movmem DTA,DTA+4000H,4000H
	mvi a,23h
	out 10h
	ei
	call whalf
	puts done
	lxi h,wfcb+1
	mvi b,11
	call putbytes
	ret
whalf:	lxi d,DTA
	lxi b,80h;	4000h/RECSIZ
s12:	push b
	push d
	mvi c,_SETDMA
	call BDOS
	lxi d,wfcb
	mvi c,_WRITE
	call BDOS
	ora a
	jz s13
	puts nospace
	jmp s20
s13:	pop d
	pop b
	lxi h,RECSIZ
	dad d
	xchg
	dcx b
	mov a,b
	ora c
	jnz s12
s20:	lxi d,wfcb
	mvi c,_CLOSE
	call BDOS
	cpi BDOSERR
	rnz
	puts clserr
s21:	jmp 0


;
;   This procedure puts <B> bytes on screen. <HL> is a pointer to 1-st byte.

putbytes:
	pusha
	mov c,m
	bios CONOUT
	popa
	inx h
	dcr b
	jnz putbytes
	ret


REKOMP:	LXI D,D830
	MVI C,40H
M831:	MOV A,M
	STAX D
	INX D
	INX H
	DCR C
	JNZ M831
	SHLD D833
	LXI H,8000H
	XCHG
	LHLD D830
	DAD D
	XCHG
	LHLD D833
	MOV B,H
	MOV C,L
	LHLD D831
	DAD B
	MVI C,7FH
M832:	MOV A,C
	CMP M
	JNC M833
	LDA DT83A
	CMP M
	JZ M839
	LDA DT83B
	CMP M
	JZ M8310
	LDA DT83C
	CMP M
	JZ M8311
M833:	MOV A,M
M834:	STAX D
	DCX D
M835:	DCX H
	MVI A,7FH
	CMP D
	JNZ M832
	MVI A,0FFH
	CMP E
	JNZ M832
	LXI H,DT83A
	MVI B,3
M836:	PUSH H
	MOV A,M
	INX H
	MOV C,A
	XRA A
	CMP M
	JZ M838
	MOV A,C
	MOV C,M
M837:	INX H
	MOV E,M
	INX H
	MOV D,M
	PUSH H
	LXI H,8000H
	DAD D
	MOV M,A
	POP H
	DCR C
	JNZ M837
M838:	POP H
	LXI D,10H
	DAD D
	DCR B
	JNZ M836
	RET
M839:	LDA D832
	STAX D
	DCX D
	JMP M834
M8310:	DCX H
	MOV A,M
	STAX D
	DCX D
	STAX D
	DCX D
	JMP M834
M8311:	DCX H
	PUSH B
	MOV C,M
	DCX H
	MOV A,M
M8312:	STAX D
	DCX D
	DCR C
	JNZ M8312
	POP B
	JMP M835
;
D830:	DS 2
D831:	DS 2
D832:	DS 1
D833:	DS 0BH
DT83A:	DS 10H
DT83B:	DS 10H
DT83C:	DS 10H
;
PRER:	PUSH PSW
	PUSH B
	PUSH D
	PUSH H
	MVI A,88H
	OUT 0
	LXI H,SCOL
	LXI D,1000H
USTCOL:	MOV A,E
	OUT 2
	MOV A,M
	OUT 0CH
	INX H
	OUT 0CH
	INR E
	OUT 0CH
	DCR D
	OUT 0CH
	JNZ USTCOL
	MVI A,0F0H
	OUT 3
	XRA A
	OUT 2
	OUT 1
	POP H
	POP D
	POP B
	POP PSW
	EI
	RET

SCOL:	DB 64,128,16,208,6,134,22,54,0,197,34,192,2,152,82,173

usage:	db 'Perlin Production Co.Ltd. SCRTOBIN v.0.1 1993 (c).',0dh,0ah
	db 'This program converts SCR files to VRAM bitmap.',0dh,0ah
	db 9,'Usage: SCRTOBIN filename.SCR$'
	ds 40
stack:
cantop:	db 'Can''t open $'
opnerr:	db ' BDOS error on open.$'
noplace: db 0CH,'Can''t create binary unpacked file. Terminate program.$'
nospace: db 0CH,'Disk full! Writing aborted$'
clserr:	db 0CH,'BDOS error on close unpacked binary file.$'
done:	db 0CH,'Source file converted into VRAM bitmap format and written to $'
wfcb:
	REPT 9
		db 0
	ENDM
	db 'BIN'
	REPT 25
		db 0
	ENDM

SYSPAGE: DS 100H

;
; Place for BIOS
;
	
BIOSNEST:

;
;	Data transfer area for .SCR file will sit after BIOSNEST+2000H

	END
