*************************************
*  " NDEVICE "  (UKAZKA REZIDENTU)	*
*************************************
* Po prekladu spustit od $A800, az
* potom ulozit (adresy viz. konec)

TARGET	EQU	$6000
	OUT	N

	ORG	TARGET-3,$A800
O	EQU	$A800-TARGET+3
	JMP	UTAJ+O

*Promenne - univerzalni cast
SRCHADR	EPZ	128
RELZP	EPZ	130

*Promenne - specificka cast
CLOSIX	EPZ	132
CLOSTMP	EPZ	133
CLOSAD	EPZ	134

*Makroinstrukce pro znak EOL
EOL	MACRO
	DFB	$9B
	MEND

*Vstupni dekodovaci rutina

ODT0	LDA	$E474	;Meni se!
	LDX	#0
	LDY	#$10

ODTAJ	LDA	PRINT,X
	EOR	#$55
ODT2	STA	PRINT,X

	INX
	BNE	ODTAJ

	INC	ODTAJ+2
	INC	ODT2+2
	DEY
	BNE	ODTAJ

	LDA	#$4C
	STA	ODT0

	JMP	START

*Podprogram pro vystup na obrazovku

PRINT	PLA
	STA	PR4+1
	PLA
	STA	PR4+2

PR2	INC	PR4+1
	BNE	PR3
	INC	PR4+2

PR3	LDA	#0
	TAX
	STA	$348,X	;ICBLL/H
	STA	$349,X

	LDA	#11
	STA	$342,X	;ICCMD

PR4	LDA	$FFFF	;Meni se!
	BEQ	PR5

	JSR	$E456	;CIO
	JMP	PR2

PR5	LDA	PR4+2
	PHA
	LDA	PR4+1
	PHA

	RTS

*Pomocny skok pro prevzeti
*parametru z CP
GETNAME	JMP	$E474	;Meni se!

*Zacatek programu - test DOSu

START	LDA	$700
	CMP	#'S'
	BEQ	START2

	JSR	PRINT
	EOL
	ASC "Bad DOS version !"
	EOL
	DFB 0

	JMP	(10)

*Prevzeti parametru

START2	LDA	10
	CLC
	ADC	#3
	STA	GETNAME+1
	LDA	11
	ADC	#0
	STA	GETNAME+2

	JSR	GETNAME

*Je parametr 'OFF' ?

	LDY	#36
	LDA	(10),Y
	CMP	#'O'
	BNE	START3

	INY
	LDA	(10),Y
	CMP	#'F'
	BNE	SYNTAX
	INY
	CMP	(10),Y
	BNE	SYNTAX
	INY
	LDA	(10),Y
	CMP	#$9B
	BNE	SYNTAX

	JMP	DISABLE

*Specificka cast:
*Zpracovani parametru

START3	CMP	#$9B
	BEQ	START4

*Chyba syntaxe -> napoveda

SYNTAX	JSR	PRINT
	EOL
	ASC "Syntax: NDEVICE"
	EOL
	ASC "        NDEVICE OFF"
	EOL
	DFB 0

	JMP	(10)

*Je jiz rezident instalovan?

START4	JSR	SRCHRUT
	BCC	INSTAL

*Specificka cast:
*Zmena konfigurace

*V tomto pripade neprichazi zmena
*konfigurace v uvahu, proto chybove
*hlaseni
	JSR	PRINT
	EOL
	ASC "NDEVICE already "
	ASC "installed !"
	EOL
	DFB 0

	JMP	(10)

*Instalace rezidentu:
*Vypocet nove adresy MEMLO

INSTAL	LDA	743	;MEMLO
	STA	SRCHADR
	CLC
	ADC	#RESLEN
	STA	743
	STA	LMEM+1

	LDA	744
	STA	SRCHADR+1
	ADC	#0
	STA	744
	STA	HMEM+1

*Relokovani rezidentu

RLK0	LDX	#0

RLK1	LDA	RELOKT,X
	STA	RELZP
	LDA	RELOKT+1,X
	STA	RELZP+1

	ORA	RELZP
	BEQ	INSTAL2

	LDY	#1
	LDA	(RELZP),Y
	SEC
	SBC	#RESID:L
	PHP

	CLC
	ADC	SRCHADR
	STA	(RELZP),Y

	INY
	LDA	(RELZP),Y
	ADC	SRCHADR+1

	PLP
	SBC	#RESID:H
	STA	(RELZP),Y

	INX
	INX
	BNE	RLK1

*Specificka cast:
*Priprava rezidentu pred instalaci

INSTAL2	JSR	CLOSDEV

	JSR	SRCHHATB
	BCC	INSTAL2B

	LDA	$31B,X
	STA	OLDADR
	LDA	$31C,X
	STA	OLDADR+1

*Doplneni "pulene" adresy do rezidentu

INSTAL2B	LDA	TABADR
	STA	TA1+1
	LDA	TABADR+1
	STA	TA2+1

*Kopirovani rezidentu na misto urceni

	LDY	#RESLEN

COP1	DEY
	LDA	RESID,Y
	STA	(SRCHADR),Y

	TYA
	BNE	COP1

*Pripojeni do sekvence <RESET>

	LDY	#2
INSTAL3	LDA	11,Y
	STA	(SRCHADR),Y

	LDA	SRCHADR-1,Y
	STA	11,Y

	DEY
	BNE	INSTAL3

*Pripojeni instalovaneho rezidentu
*k operacnimu systemu

	JSR	INIT

*Zakonceni

	JSR	PRINT
	EOL
	ASC "NDEVICE installed."
	EOL
	DFB 0

	JMP	(10)

*Odstraneni rezidentu:
*Je rezident pritomen a je posledni?

DISABLE	JSR	SRCHRUT
	BCS	DISABLE2

	JSR	PRINT
	EOL
	ASC "NDEVICE not Installed !"
	EOL
	DFB 0

	JMP	DISERR2

DISCANT	JSR	PRINT
	EOL
	ASC "NDEVICE is not the last"
	EOL
	ASC "installed handler!"
	EOL
	DFB 0

DISERR2	JSR	PRINT
	ASC "Can't remove."
	EOL
	DFB 0

	JMP	(10)

DISABLE2	LDA	743
	SEC
	SBC	SRCHADR
	TAY
	LDA	744
	SBC	SRCHADR+1
	BNE	DISCANT

	CPY	#RESLEN
	BNE	DISCANT

*Specificka cast:
*Odpojeni rezidentu od OS

	JSR	CLOSDEV

	JSR	SRCHHATB
	BCC	DISBL1

	LDY	#OLDADR-RESID
	LDA	(SRCHADR),Y
	STA	CLOSAD
	INY
	LDA	(SRCHADR),Y
	STA	CLOSAD+1

	ORA	CLOSAD
	BNE	DIS2A

	STA	$31A,X

DIS2A	LDA	CLOSAD
	STA	$31B,X
	LDA	CLOSAD+1
	STA	$31C,X

*Odpojeni od <RESET> a vraceni
*puvodni hodnoty MEMLO

DISBL1	LDY	#2

DISBL2	LDA	(SRCHADR),Y
	STA	11,Y

	LDA	SRCHADR-1,Y
	STA	743-1,Y

	DEY
	BNE	DISBL2

*Zakonceni

	JSR	PRINT
	EOL
	ASC "NDEVICE removed."
	EOL
	DFB	0

	JMP	(10)

*Podprogram - vyhledani rezidentniho
*kodu v pameti pod MEMLO:

*Makroinstrukce pro jeden krok
*vyhledavani

SRCHIT	MACRO	LABEL1,LABEL2

	LDY	#LABEL1-RESID

SRIT@	LDA	(SRCHADR),Y
	CMP	RESID,Y
	BNE	SRCHNXT

	INY
	CPY	#LABEL2-RESID
	BNE	SRIT@

	MEND

*Priprava pocatecni adresy

SRCHRUT	LDA	743
	SEC
	SBC	#RESLEN
	STA	SRCHADR
	LDA	744
	SBC	#0
	STA	SRCHADR+1

*Specificka cast:
*Porovnani se vzorem rezidentu

SRCHR1	SRCHIT	T1,TA1
	SRCHIT	OK,R6
	SRCHIT	T2,T3

	SEC
	RTS

*Nesouhlasi -> dalsi adresa

SRCHNXT	LDA	SRCHADR
	BNE	SNXT2
	DEC	SRCHADR+1
SNXT2	DEC	SRCHADR

*Adresa<$1000 -> nenasel

	LDA	SRCHADR+1
	CMP	#$10
	BCS	SRCHR1

CLOSX	RTS

*Specificka cast:
*Podprogramy instalacni procedury

*Uzavreni vsech IOCB, ktere jsou
*otevreny na zarizeni "N:", vcetne
*davkoveho souboru a HardCopy.

CLOSDEV	JSR	SRCHHATB	;Hledej
	BCC	CLOSX	;v HATABS

	STX	CLOSIX
	LDA	#$70

CLO4	STA	CLOSTMP	;Je IOCB
	TAX		;otevren
	LDA	$340,X	;pro "N:"?
	CMP	CLOSIX
	BNE	CLO5

	LDA	#12	;CLOSE
	STA	$342,X
	JSR	$E456

CLO5	LDA	CLOSTMP	;Dalsi IOCB
	SEC
	SBC	#$10
	BCS	CLO4

	LDA	10	;Priprava
	SEC		;adresy
	SBC	#8	;ECHOFLG
	STA	CLOSAD
	LDA	11
	SBC	#0
	STA	CLOSAD+1

	LDA	10	;Priprava
	CLC		;adresy
	ADC	#8	;XDIVIO
	STA	XDIVJ+1
	LDA	11
	ADC	#0
	STA	XDIVJ+2

	LDY	#0	;HardCopy
	JSR	CLO6

	LDY	#1	;Batch file

CLO6	LDA	(CLOSAD),Y
	CMP	CLOSIX	;Je to N:?
	BNE	CLO7

XDIVJ	JMP	($FFFF)	;Meni se!

CLO7	RTS

*Vyhledani zarizeni "N:" v HATABS
*(neni-li, nastavi C=0)

SRCHHATB	LDX	#0

SRCHH2	LDA	$31A,X
	CMP	#'N'
	BEQ	SRCHH3

	INX
	INX
	INX
	CPX	#33
	BCC	SRCHH2

	CLC
SRCHH3	RTS

*Inicializace rezidentu

INIT	LDA	SRCHADR
	CLC
	ADC	#3
	STA	INIT2+1
	LDA	SRCHADR+1
	ADC	#0
	STA	INIT2+2

INIT2	JMP	$E474	;Meni se!

*Vlastni rezident:
*Inicializacni sekvence

RESID	JSR	$FFFF

LMEM	LDA	#$22
	STA	743
HMEM	LDA	#$22
T1	STA	744

*Specificka cast:
*Inicializace rezidentu

	LDY	#30
	LDX	#$FF

RESINI1	LDA	$31A,Y
	BNE	RESINI2

	TYA
	TAX
*(A<=30 - nikdy se neshodne s rozumnym
* nazvem zarizeni)

RESINI2	CMP	#'N'
	BEQ	RESINI4

	DEY
	DEY
	DEY
	BPL	RESINI1

	TXA
	TAY
	BMI	HNDINI

RESINI4	LDA	#'N'
	STA	$31A,Y
TA1	LDA	#TABLE:L
	STA	$31B,Y
TA2	LDA	#TABLE:H
	STA	$31C,Y
	BNE	HNDINI

*Misto pro uschovani puvodni adresy
*z HATABS

OLDADR	DFW	0

*Specificka cast:
*Hlavni program rezidentu

TABLE	DFW	OPEN-1
R1	DFW	OK-1	;CLOSE
R2	DFW	GET-1
R3	DFW	PUT-1
R4	DFW	OK-1	;STATUS
R5	DFW	SPEC-1

HNDINI	RTS

OPEN	LSR	EOFFLG+1

OK	LDY	#1
	RTS

SPEC	LDY	#146	;Not imple-
	RTS		;mented

GET	LDA	$D20A
	BNE	EOFFLG

	SEC
R6	ROR	EOFFLG+1

EOFFLG	LDY	#0
T2	BPL	OK

	LDY	#136	;EOF
	RTS

PUT	STA	$D01A

	LDY	#1
T3	RTS

*Konec rezidentu

*Pomocna hodnota - delka rezidentu
RESLEN	EPZ	*-RESID

TABADR	DFW	TABLE

*Tabulka absolutnich adres v
*rezidentu - pro relokaci

RELOKT	DFW	TABLE-1,R1-1,R2-1
	DFW	R3-1,R4-1,R5-1,OPEN
	DFW	R6,TABADR-1,0

*************************************

*Konec vlastniho programu

*Nasleduje rutina pro zakodovani
*programu po prekladu

UTZP	EPZ	$20

UTAJ	LDA	#PRINT+O:L
	STA	UTZP
	LDA	#PRINT+O:H
	STA	UTZP+1

UTAJ2	LDY	#0
	LDA	(UTZP),Y
	EOR	#$55
	STA	(UTZP),Y

	INC	UTZP
	BNE	UTAJ3
	INC	UTZP+1

UTAJ3	LDA	UTZP
	CMP	#UTAJ+O:L
	BNE	UTAJ2
	LDA	UTZP+1
	CMP	#UTAJ+O:H
	BNE	UTAJ2

	RTS

*Opakovani dulezitych hodnot - kvuli
*snadnemu precteni behem prekladu

*Delka rezidentniho kodu
DELKARES	EQU	RESLEN

*Upozorneni na startovaci adresu
*pro zakodovani po prekladu
SPUSTOD	EQU	$A800

*Adresy pro ulozeni vysledneho prog.
SAVEFROM	EQU	ODT0+O
SAVETO	EQU	UTAJ+O
SAVEINTO	EQU	TARGET

************** KONEC ***************

����