PageGUI  EQU 3
BeginGUI EQU #C800

        ORG #C000,PageGUI
+       INCBIN "gui"

        ORG #6128

        INCLUDE "GuiVar",#40
        INCLUDE "ResGUI",#08

Runner  LD A,PageGUI+#10
        LD (GUI_PAGE),A
        LD A,4+#10
        LD (STORE_PAGE),A
        LD A,#10
        LD (S_PAGE),A
        LD HL,Resident
        LD (ResAdr),HL

        CALL AUTOCONF
        LD A,LOAD_CAT
        CALL GUI

        LD HL,0
        LD (total),HL
        LD (FR_FLUSH),HL
        LD HL,(END_DISK)
        LD (TEK_SEC),HL
        CALL INI_BUFF

        LD A,0
cont    PUSH AF
        CALL SSpr
        POP AF
        INC A
        LD HL,All
        CP (HL)
        JR NZ,cont
        XOR A           ; добавить конец диска
        CALL ADD_BYTE
        CALL Flush      ; сбросили остаток буфера
        CALL ClFile
        RET 
;------------------------
; сборс одного спрайта
SSpr    PUSH AF
        LD DE,name+3
        CALL CONV
        LD HL,name
        LD B,6
        CALL ADD_BYTES
        POP AF

        CALL Search
        LD (GADR+1),DE
        LD A,B
        LD (b0),A
        LD A,C
        LD (c0),A
        LD A,0

        LD C,1
c0      EQU $-1
dnz4
        LD B,0
b0      EQU $-1
dnz3    PUSH BC
        LD HL,dbsym
        LD B,4
        CALL ADD_BYTES
        LD B,7
dnz2    PUSH BC
        CALL GADR
        LD A,(HL)
        LD DE,bytsym1
        PUSH DE
        CALL CONV
        POP HL
        LD B,4
        CALL ADD_BYTES
        POP BC
        DJNZ dnz2
        CALL GADR
        LD A,(HL)
        LD DE,bytsym2
        PUSH DE
        CALL CONV
        POP HL
        LD B,4
        CALL ADD_BYTES
        POP BC
        DJNZ dnz3
        DEC C
        JR NZ,dnz4
        RET 
;-----------------------
ClFile  LD HL,FNAME
        LD DE,23773
        LD BC,11
        LDIR 
        LD HL,#4000
        LD DE,(total)
        PUSH DE
        DEC DE
        INC D
        LD B,D
        POP DE
        LD C,#FF
        LD A,DOS_FUNC
        CALL GUI
        RET 

FNAME   DB "flush3  COD"

GADR    LD HL,#C000
        PUSH HL
        INC HL
        LD (GADR+1),HL
        POP HL
        RET 

ADD_BYTES
dnz1    PUSH BC
        PUSH HL
        LD A,(HL)
        CALL ADD_BYTE
        POP HL
        INC HL
        POP BC
        DJNZ dnz1
        RET 

ADD_BYTE
        LD HL,(badr)
        LD (HL),A
        INC HL
        LD (badr),HL
        LD HL,(total)
        INC HL
        LD A,H
        OR L
        JP Z,BIG_FILE
        LD (total),HL
        LD HL,(bbyte)
        INC HL
        LD (bbyte),HL
        LD DE,lbuff
        AND A
        SBC HL,DE
        CALL Z,FLUSH
        RET 

Flush   LD HL,(bbyte)
        LD A,H
        OR L
        RET Z
        JR FLUSH

FLUSH   LD A,7
        OUT (#FE),A
        XOR A
        OUT (#FE),A

        LD HL,(FR_FLUSH)
        LD B,0
        LD C,'lbuff
        ADD HL,BC
        LD (FR_FLUSH),HL

        LD HL,(FREE_SEC)
        LD DE,(FR_FLUSH)
        AND A
        SBC HL,DE
        JP C,OFF_DISK

        LD HL,buffer
        LD DE,(TEK_SEC)
        LD B,'lbuff
        LD C,#06
        LD A,DOS_FUNC
        CALL GUI
        CALL INI_BUFF
        RET 

FR_FLUSH DW 0

INI_BUFF:
        LD HL,buffer
        LD (badr),HL
        LD HL,0
        LD (bbyte),HL
        RET 

OFF_DISK
        LD IX,WOff
        JR mess

BIG_FILE
        LD IX,WBig
mess    LD A,Window
        CALL GUI
        LD A,NOKEY
        CALL GUI
        LD A,WAITKEY
        CALL GUI
        CALL CLOSE
        JP 23600

WOff    DB 11,8,9,5,2*8+7,7,%1111,0
        DW TOff
        DW #FFFF
TOff    DB " Внимание!",13
        DB "Диск полон_",0

WBig    DB 10,8,12,6,2*8+7,7,%1111,0
        DW TBig
        DW #FFFF
TBig    DB "    Внимание!",13
        DB "Данные не входят",13
        DB "  в один файл_",0


CONV    LD C,0
        EX AF,AF'
        LD A,GET_NUM
        JP GUI

name    DB "SPR"
        DB "000"

total   DW 0             ; сколько всего байтов файл
lbuff   EQU 1024
dbsym   DB #09,"DB "
bytsym1 DB "000,"
bytsym2 DB "000",13,10
bbyte   DW 0
badr    DW buffer
buffer  DS lbuff
;...................................
;. Вход:                           .
;.  A  - номер спрайта (0-255)     .
;. Выход:                          .
;.  HL - adress in TABLICA         .
;.  BC - width, high               .
;.  DE - adress in RAM             .
;.  A  - формат                    .
;...................................

Search: LD HL,TABLICA
        AND A
        JR Z,FirSpSch

        LD B,A
        LD DE,5
sloop   PUSH DE
        PUSH HL
        LD E,(HL)
        INC HL
        LD D,(HL)

        PUSH HL
        EX DE,HL
        LD DE,Sprites
        ADD HL,DE
        EX DE,HL
        POP HL

        LD (adrrsp+1),DE
        POP HL
        POP DE
        ADD HL,DE
        DJNZ sloop
adrrsp  LD DE,0
consrch PUSH HL
        INC HL
        INC HL
        LD B,(HL)
        INC HL
        LD C,(HL)
        INC HL
        LD A,(HL)
        POP HL
        RET 

FirSpSch:
        LD DE,Sprites
        JR consrch

        ORG #C000,1
Head    DS 4
All     DB 0
FloatTab DW TABLICA
Float   DW Sprites
teksp   DB 0
TABLICA DS 1275; next(2),x(1),y(1),flag(1)
Sprites NOP 

        ORG #C000
+       INCBIN "test2.S"

        ORG Runner

