        MAIN "",134

;30.12.1999
;(C) Predator/Delirium Tremens
;
;**.**.****  :)
;(C) Eraser/Delirium Tremens

DOS_FUNC
        PUSH AF
        PUSH HL
        PUSH DE
        LD HL,TAB_FUNC
        LD DE,3
DOS_FUNC2
        LD A,(HL)
        AND A
        JR Z,DOS_F_NO
        CP C
        JR Z,DOS_FUNC1
        ADD HL,DE
        JR DOS_FUNC2
DOS_FUNC1
        INC HL
        DEFHL
DOS_F_N1 LD (DOS_FUNC3),DE
        POP DE
        POP HL
        POP AF
        JP #0000
DOS_FUNC3 EQU $-2

DOS_F_NO
        LD A,(MODE)
        EX AF,AF'
        XOR A
        LD (MODE),A
        POP DE
        POP HL
        POP AF
        CALL FDOS
        EX AF,AF'
        LD (MODE),A
        RET 

TAB_FUNC                ;Можно продолжить
        DEFB #01
        DEFW SEL_DRV
        DEFB #05
        DEFW LOAD_SEC
        DEFB #06
        DEFW SAVE_SEC
        DEFB #0E
        DEFW LOAD_FILE
        DEFB #0B
        DEFW SAVE_FILE2
        DEFB #0A
        DEFW SEARCH_FILE
        DEFB #FF
        DEFW SAVE_HEAD
        DEFB #FE
        DEFW SEARCH_FILE2
        DEFB #AE
        DEFW LOAD_FILE_CAT
        DEFB #00

SEL_DRV PUSH AF
        LD A,(#5D19)
        LD (LDRV),A
        POP AF
sel_drv PUSH AF
        LD (#5D19),A
        LD (#5CF6),A
        OR #3C
        LD (#5D16),A
        AND #03
        PUSH AF
        LD HL,#5CC8
        ADD A,L
        LD L,A
        LD A,#83
        LD (HL),A
        POP AF
        LD HL,#5CFA
        ADD A,L
        LD L,A
        LD A,#08
        LD (HL),A
        LD A,(MODE)
        LD (mode+1),A
        XOR A
        LD (MODE),A
        POP AF
        CP 4
        JR Z,TESTRD_

        LD C,1
        CALL FDOS
mode    LD A,0
        LD (MODE),A
        CALL L_DRIVE
        JP OK

TESTRD_ CALL TESTRD
        JR Z,mode
        LD A,0
LDRV    EQU $-1
        JR sel_drv

LOAD_SEC
        LD C,5
        CALL FDOS
        JP OK

SAVE_SEC
        LD C,6
        CALL FDOS
        JP OK

LOAD_FILE_CAT
        PUSH HL
        CALL SEARCH_FILE
        JR C,nfnd
        JR lfile

nfnd    POP HL
        SCF 
        RET 

LOAD_FILE
        PUSH HL
        CALL LOAD_CAT
        CALL SEARCH_FILE
        JR C,NO_LFILE
lfile   PUSH IX
        POP HL
        LD DE,FILE_PARAM
        LD BC,#10
        LDIR 
        POP HL
        LD DE,(START_SEC)
        LD A,(SEC_LEN)
        LD B,A
        LD C,#05
        CALL FDOS
        JP OK

NO_LFILE
        POP HL
        LD IX,NoFW
        CALL Window
        CALL WAITKEY
        CALL RestBack
scf     SCF 
        RET 

NoFW    DEFB 10,7,11,5,2*8+7,7,%00001111,0
        DEFW TxtWE        ; объект-текст
        DEFW #FFFF

TxtWE   DB "  Вниамние!",13
        DB "Файл не найден",0

;................;
START_ADR_ DW 0  ;
;................;
SAVE_FILE2
        LD (START_ADR_),HL
        LD (BYTES_LEN),DE
        LD A,E
        AND A
        LD A,D
        JR Z,L3_
        INC A
L3_     LD (SEC_LEN),A

        CALL LOAD_CAT
        CALL SEARCH_FILE
        JR C,SAabs1

        LD A,(OVERF)
        AND A
        JR NZ,YES_

        LD IX,OVERW
        CALL Win
        PUSH AF
        CALL RestBack
        POP AF
        CP #FF
        JP Z,scf
        AND A
        JR NZ,SAabs2

YES_    LD A,13
        CALL Search_File
        JR C,SAabs2

        PUSH IX
        POP HL
        LD DE,FILE_PARAM
        LD BC,16
        LDIR 

        LD HL,(START_ADR_)
        LD DE,(START_SEC)
        LD A,(SEC_LEN)
        LD B,A
        CALL SAVE_SEC
MAZ
        XOR A
        RET 

SAabs2  CALL erase_file
SAabs1  LD HL,(START_ADR_)
        LD DE,(BYTES_LEN)
        LD A,(SEC_LEN)
        LD B,A
        JP SAVE_FILE

OVERW   DB 9,8,12,6,2*8+7,7,%00001111,0
        DEFW TxtOv        ; объект-текст
        DEFW SelOv        ; объект-подсветка (выборка)
        DEFW HotOv        ; горячие клавиши
        DEFW #FFFF

TxtOv   DB "Файл существует",13
        DB "Записать поверх",13
        DB "Перезаписать",0
        DB #FF

SelOv   DB 1,3,10,1,%11
        DB 1,4,10,1,%11
        DB #FF

HotOv   DB "Y"
        DB 0
         DW 0
        DB "N"
        DB 0
         DW 1
        DB #FF
;....................
NOK
        POP IX
NO_FILE
        LD A,1
        RET 

erase_file
        PUSH IX
        JP ef

ERASE_FILE
        PUSH IX
        LD HL,CAT
        LD DE,#0000
        LD BC,#0905
        CALL DOS_FUNC

ef      XOR A
        CALL SEARCH_FILE
        JR C,NOK
        LD B,A
        SBC HL,HL
        AND A
        JR Z,ERG3
        LD DE,#10
ERG1    ADD HL,DE
        DJNZ ERG1
ERG3    EX DE,HL
        LD HL,CAT
        ADD HL,DE
        PUSH HL
        POP IX
        LD C,1
        LD A,(IX+#10)
        AND A
        JR NZ,ERF1
        LD C,0
ERF1
        LD (IX+0),C
        LD H,D
        LD E,D
        LD D,0
        LD L,0
        LD BC,CAT
        ADD HL,BC
        LD BC,#0106
        PUSH BC
        PUSH IX
        CALL DOS_FUNC
        POP IX
        LD A,(count)
        INC A
        LD B,A
        LD HL,CAT+#8E4
        LD A,(HL)
        CP B
        JR NZ,ERG4
        DEC (HL)
        JR ERG5
ERG4    LD HL,CAT+#8F4
        INC (HL)
ERG5    LD A,(IX+0)
        AND A
        JR NZ,ERF2
        LD E,(IX+#0D)
        LD D,0
        LD HL,(CAT+#8E5)
        ADD HL,DE
        LD (CAT+#8E5),HL
        LD L,(IX+#0E)
        LD H,(IX+#0F)
        LD (CAT+#8E1),HL
ERF2
        POP BC
        LD DE,#0008
        LD HL,CAT+#800
        CALL DOS_FUNC
        POP IX
        JP OK

count   DB #FF

Search_File
        LD HL,CAT-#10
        JP srch

SEARCH_FILE2:
        LD HL,NEWCAT-#10
        LD A,9
        JP srch

SEARCH_FILE:
        LD HL,CAT-#10
        LD A,9
srch    LD (lensrch+1),A
        LD A,#FF
        LD (count),A
Cikl_6  LD A,(count)
        INC A
        LD (count),A
        LD BC,#0010
        ADD HL,BC
        LD A,(HL)
        OR A
        JR Z,End_False
        LD IX,#5CDD
        PUSH HL
lensrch LD B,9
Cikl_1  PUSH BC
        LD A,(HL)
        INC HL
        CP (IX+#00)
        INC IX
        JR NZ,False
        POP BC
        DJNZ Cikl_1
        POP HL
        PUSH HL
        POP IX
True    XOR A
        LD A,(count)
        RET 

False   POP BC
        POP HL
        JR Cikl_6

End_False
        SCF 
        RET 

CAT_OVER
NO_SPACE
        LD IX,NO_SPACEW
        CALL Window
        CALL NOKEY
        CALL WAITKEY
        CALL RestBack
        JP FM_EXIT

NO_SPACEW
        DEFB 12,8,9,5,2*8+7,7,%00001111,0
        DEFW SpaceErr     ; объект-текст
        DEFW #FFFF

SpaceErr DB " Внимание!",13
         DB "Диск полон_",#00

CAT1     DEFW #0000
CAT2     DEFW #0000
BUFILE   DS 512

;................. SAVE RULEZ FOREVER ..........................
SAVE_HEAD
        PUSH HL
        LD HL,NOSAVE
        LD (jsv+1),HL
        POP HL
        PUSH BC
        PUSH HL
        PUSH DE
        LD HL,BUFILE
        LD BC,#0105
        LD DE,#0008
        CALL DOS_FUNC
        POP DE
        POP HL
        POP BC
        LD A,B
        JP L3

NOSAVE  LD HL,(BUFILE+#E1)
        LD A,(SEC_LEN)
        LD B,A
        CALL TRAK
        LD (BUFILE+#E1),HL
        RET 

SAVE_FILE
        PUSH HL
        LD HL,SAVEF
        LD (jsv+1),HL
        POP HL
        JP save_file

SAVEF   LD HL,(START_ADR_)
        LD DE,(START_SEC)
        LD A,(SEC_LEN)
        LD B,A
        LD C,#06
        CALL DOS_FUNC
        LD HL,(#5CF4)
        LD (BUFILE+#E1),HL
        RET 

save_file
        PUSH HL
        PUSH DE
        LD HL,BUFILE
        LD BC,#0105
        LD DE,#0008
        CALL DOS_FUNC
        POP DE
        POP HL
        LD A,E
        AND A
        LD A,D
        JR Z,L3
        INC A
L3      LD (SEC_LEN),A
        LD (BYTES_LEN),DE
        LD (START_ADR_),HL
        LD DE,(BUFILE+#E1)
        LD (START_SEC),DE
        LD A,(BUFILE+#E4)
        CP #80
        JP NC,CAT_OVER
        AND #F0
        DUP 4
        RRCA 
        EDUP 
        LD E,A
        LD (SAVE_SCT),A
        LD D,0
        LD HL,BUFILE+#100
        LD BC,#0105
        PUSH HL
        CALL DOS_FUNC
        POP HL
        LD DE,#10
L1
        LD A,(HL)
        AND A
        JR Z,L2
        ADD HL,DE
        JR L1
L2
        LD (CAT1),HL
        LD HL,(BUFILE+#E5)
        XOR A
        OR H
        LD A,(SEC_LEN)
        JR NZ,L4
        INC L
        CP L
        JR C,L4-1
        JP NO_SPACE
        DEC L
L4
        LD E,A
        LD D,0
        AND A
        SBC HL,DE
        LD (BUFILE+#E5),HL
        LD HL,BUFILE+#E4
        INC (HL)
        LD HL,FILE_PARAM
        LD DE,(CAT1)
        LD BC,#10
        LDIR 
        LD HL,BUFILE+#100
        LD D,0
        LD E,0
SAVE_SCT EQU $-1
        LD BC,#0106
        PUSH BC
        CALL DOS_FUNC

jsv     CALL 0

        LD HL,BUFILE
        LD DE,#0008
        POP BC
        CALL DOS_FUNC

        LD A,(SAVE_SCT)
        LD H,A
        LD L,0
        LD BC,CAT
        ADD HL,BC
        EX DE,HL
        LD HL,BUFILE+#100
        LD BC,#100
        PUSH BC
        LDIR 
        LD HL,BUFILE
        LD DE,CAT+#0800
        POP BC
        LDIR 

OK      XOR A
        RET 

; Из байтовой системы в секторную
; In:
; HL - bytes
; Out:
; B  - sectors

CLC_SEC LD B,H
        LD A,L
        AND A
        RET Z
        INC B
        RET 


TRAK    INC L
        LD A,L
        CP #10
        CALL Z,RESL
        DJNZ TRAK
        RET 

RESL    LD L,0
        INC H
        RET 

