;[]=======================================================================[]
;
; fn_load.z80 -- primary loader of Flex Navigator v1.10
;
; Copyright (C) 2000 Anton Enin
;
; This program should be compiled using z80asm
; http://www.zxasm.narod.ru/
;
; z80asm.exe -f bin main.z80 -o a_print.exe
;
;[]=======================================================================[]

		org	0x80EA
		section	.text

EXEhead:	db	"EXE"			; exe header
		db	0			; +3
		dw	EntryExec-EXEhead
		dw	0x0000			; +4
		dw	EXEend-EntryExec	; +8
		dw	0, 0			; +10
		dw	0			; +14
		dw	EntryExec		; +16
		dw	EntryExec
		dw	0x80FF

;[]=======================================================================[]
; input:
;	IX - pointer from DOS

EntryExec:	DI
		LD	A, (IX-0x03)		; current file handle
		LD	(.hFile), A		; save
		LD	HL, .copyright_str
		LD	C, 0x5C			; print text
		RST	0x10
		LD	C, 0x00
		RST	0x10
		LD	A, D
		OR	A
		JR	NZ, .next1
		LD	HL, .incor_dos_str
		LD	C, 0x5C			; print text
		RST	0x10
		LD	A, (.hFile)
		LD	C, 0x12
		RST	0x10			; close current file
		LD	BC, 0xFF41
		RST	0x10			; exit
.next1
		LD	BC, 0x063D
		RST	0x10			; need 6 memory pages
		JR	NC, .next2		; CY - not enough memory
		LD	HL, .no_memory_str
		LD	C, 0x5C			; print text
		RST	0x10
		LD	BC, 0xFF41
		RST	0x10			; exit
.next2
		LD	(.hMem), A		; memory handle
		LD	HL, .Buffer
		LD	C, 0xC5
		RST	0x08			; get memory pages list
		LD	A, (.MPg)		; set pages from 0xC000
		OUT	(0xE2), A
		LD	HL, 0x8500		;FNMAIN
		LD	DE, end1-0x8500
		LD	A, (.hFile)
		LD	C, 0x13
		RST	0x10
		JP	C, .disk_error
		LD	A, (.FPg)
		OUT	(0xE2), A
		LD	HL, 0xC000		;FNFUNCT
		LD	DE, end2-0x4000
		LD	A, (.hFile)
		LD	C, 0x13
		RST	0x10
		JP	C, .disk_error
		LD	A, (.VPg)		;FNVIEW
		OUT	(0xE2), A
		LD	HL, 0xC000
		LD	DE, 0x19A6
		LD	A, (.hFile)
		LD	C, 0x13
		RST	0x10
		JP	C, .disk_error
		LD	A, (.EPg)		;FNEDIT
		OUT	(0xE2), A
		LD	HL, 0xC000
		LD	DE, 0x273B
		LD	A, (.hFile)
		LD	C, 0x13
		RST	0x10
		JP	C, .disk_error
		LD	A, (.HPg)		;FNHELP
		OUT	(0xE2), A
		LD	HL, 0xC000
		LD	DE, end3-0x4000
		LD	A, (.hFile)
		LD	C, 0x13
		RST	0x10
		JP	C, .disk_error
		LD	A, (.FNPg)		;FNFONT
		OUT	(0xE2), A
		LD	HL, 0xC000
		LD	DE, end4-0x4000
		LD	A, (.hFile)
		LD	C, 0x13
		RST	0x10
		JP	C, .disk_error
		LD	A, (.hFile)
		LD	C, 0x12
		RST	0x10			;close current file
		LD	A, (.MPg)
		OUT	(0xE2), A
		LD	A, (.hMem)
		LD	(.FNIndef), A
		LD	HL, .FPg
		LD	DE, .FunctPg
		LD	BC, 0x0005
		LDIR
		JP	EntryPoint
.disk_error:
		LD	A, (.hMem)		; free memory
		LD	C, 0x3E
		RST	0x10
		LD	A, (.hFile)
		LD	C, 0x12
		RST	0x10			; close current file
		LD	HL, .load_err_str
		LD	C, 0x5C			; print text
		RST	0x10
		LD	BC, 0xFF41
		RST	10h			; exit

.hFile		db	0x00
.hMem		db	0x00
.Buffer
.MPg		db	0x00
.FPg		db	0x00
.VPg		db	0x00
.EPg		db	0x00
.HPg		db	0x00
.FNPg		db	0x00
		db	0x00

.copyright_str:	db	0x0D, 0x0A
		db	"The Flex Navigator, ver 1.10, Copyright (C) 1999 by Enin Anton, St-Petersburg.", 0x0D, 0x0A
		db	"All rights reserved.", 0x0D, 0x0A, 0x00
.incor_dos_str:	db	"Incorrect DOS version, need DOS 1.00 or high.", 0x0D, 0x0A, 0x00
.no_memory_str:	db	"Not enough memory to load program.", 0x0D, 0x0A, 0x00
.load_err_str:	db	"Disk loading error.", 0x0D, 0x0A, 0x00

EXEend:
