        MAIN "",134

PL_REAL EQU #C000-#400
PL_VIRT EQU PL_REAL-#400

MgnPgn
        DISP PgnAdr

BMgn    CALL SV_UNDO
        LD HL,PL_VIRT
        LD DE,#C000
        CALL install_plot
        LD HL,PL_REAL
        LD DE,#4000
        CALL install_plot

rmgn    CALL LOADSCR
        LD HL,Ret
        LD DE,Ret
        CALL SInt1

        LD IX,RMgn4
        XOR A
        CALL Ramka8
        JP C,RES_TSK

        LD H,(IX+1)
        LD L,(IX+0)
inirmgn LD A,H
        ADD A,(IX+3)
        CP 23
        JR C,okl1
        LD A,24
        SUB (IX+3)
        LD H,A
okl1    LD A,L
        ADD A,(IX+2)
        CP 31
        JR C,okl2
        LD A,32
        SUB (IX+2)
        LD L,A
okl2    DUP 3
        SLA H
        SLA L
        EDUP 
        LD (BXY),HL
        CALL NOKEY_
Bmgn    CALL CLS
        XOR A
        LD (MSTP),A
;---------------------------
bmgn    CALL CrMgn
bmgn_   LD IX,WMgn
        LD HL,COOR_M
        LD (COOR_P+1),HL
        LD HL,(MARW_ADR)
        LD (ARW_ADR),HL
        LD A,(REALRGB)
        LD (mrgb),A
        CALL NLine
        LD HL,CEL_P
        LD DE,CEL_G
        CALL SInt1
        XOR A
        LD (DIRECT),A
        LD A,%1000
        LD (IX+6),A
        LD SP,(SPP)
        CALL Winn_
        CP #FF
        JP Z,RES_TSK
        PUSH AF
        CALL EMLine
        POP AF
        LD BC,TBMgn
        JP TAB_JP

bmgnn   CALL NOKEY_
        JP bmgn

;-----------------------------
SDATA0  DB #00,#44,#00,#00
        DB #0F,#4F,#0F,#0F
        DB #F0,#F4,#F0,#F0
        DB #FF,#FF,#FF,#FF

SDATA1  DB #00,#00,#00,#00
        DB #0F,#0F,#0F,#0F
        DB #F0,#F0,#F0,#F0
        DB #FF,#FF,#FF,#FF

SHL0    DB #00,#40,#00,#00
SHR0    DB #00,#04,#00,#00

SHL1    DB #00,#00,#00,#00
SHR1    DB #00,#00,#00,#00
;----------------------------------
TBMgn   DW S_MGN
        DW S_MGN
        DW S_MGN
        DW S_MGN
        DW MSetka
        DW MRgb
        DW MRamka
        DW MArw
        DW MUndo
        DW Rmgn
        DW LSwp
        DW MNoRgb
        DW MChange
        DW MLook
        DW Ret
        DW RES_TSK

        DW MSave
        DW MLoad
;----------------------------------
CM_MODE LD A,0
cmf     EQU $-1
        AND A
        JR Z,chmode2
        LD A,#ED
        LD HL,#30FE
        LD BC,#0611
        LD DE,#18FE
        EXX 
        EX AF,AF'
        LD BC,#0606
        LD DE,ButMgn
        LD HL,#C0FE
        XOR A
chmode0 LD (cmf),A
        LD A,B
        LD (ML1+1),A
        LD (ML2+1),A
        LD A,C
        LD (PrevBox),A
        LD (mbuts),DE
        LD (ML3),HL
        LD (ML4),HL
        EXX 
        EX AF,AF'
        LD (preview),A
        LD A,C
        LD (PREVIEW),A
        LD (ML5),HL
        LD A,B
        LD (ML6+1),A
        LD (celp1),DE
        LD (celp2),DE
        LD IX,RMgn4
        LD (IX+2),B
        CALL NOKEY_
        LD HL,(BXY)
        DUP 3
        SRL H
        SRL L
        EDUP 
        JP inirmgn

chmode2 LD A,#C9
        LD HL,#3700
        LD BC,#08C9
        LD D,H
        LD E,L
        EXX 
        EX AF,AF'
        LD A,1
        LD BC,#08C9
        LD DE,Nil
        LD HL,#3700
        JR chmode0

S_MGN   LD A,(Numb)
        LD (MMet),A
        PUSH AF
        LD BC,TBMe
        CALL TAB_HL
        LD (met_st),HL
        POP AF
        LD B,4
        LD HL,metcol
        CALL SetBCol
        JP bmgn_

TBMe    DW XOR_ST
        DW MSET_R
        DW MRES_R
        DW MNOP_R

LSwp    LD HL,SCREEN
        LD D,H
        LD E,L
        LD BC,6912
LZ4     PUSH BC
        LD A,#17
        LD BC,#7FFD
        OUT (C),A
        LD A,(DE)
        EX AF,AF'
        LD A,#10
        LD BC,#7FFD
        OUT (C),A
        LD A,(HL)
        EX AF,AF'
        LD (HL),A
        LD A,#17
        LD BC,#7FFD
        OUT (C),A
        EX AF,AF'
        LD (DE),A
        INC HL
        INC DE
        POP BC
        DEC BC
        LD A,C
        OR B
        JR NZ,LZ4
        JP bmgn

MSetka  LD HL,setkaf
        CALL SwBut
        JR Z,sdata1
        LD HL,SDATA0
        LD DE,SHL0
Msetka  LD (SDATA),HL
        LD (Shl1),DE
        JP bmgnn
sdata1  LD HL,SDATA1
        LD DE,SHL1
        JR Msetka

MUndo   CALL LD_UNDO
        JP bmgnn
SMUndo  CALL SV_UNDO
        JP bmgnn

MLook   CALL lookmgn
        CALL WAITKEY_
        CALL NOKEY_
        JP Bmgn
LookMgn CALL lookmgn
        JP Bmgn

lookmgn CALL LOADSCR
        LD HL,(BXY)
        DUP 3
        SRL H
        SRL L
        EDUP 
        LD IX,RMgn4
        LD (IX+1),H
        LD (IX+0),L
        CALL BoxCol
        LD B,10
        HALT 
        DJNZ $-1
        CALL BoxCol
        CALL NOKEY_
        RET 

Rmgn    CALL EMLine
        LD HL,(BXY)
        LD (ARW_X),HL
        JP rmgn

MRgb    CALL PRGB2
        CALL NOKEY_
        JP bmgn_

MgnCol  CALL Col
        JP bmgn_
;---------------------------------------------------------------
RMgn4   DB 0,0,6,6

HMgn    DB "1",0
         DW 0
        DB "2",0
         DW 1
        DB "3",0
         DW 2
        DB "4",0
         DW 3
        DB "F",0
         DW 4
        DB "C",0
         DW 5
        DB "T",0
         DW 6
        DB "D",0
         DW 7
        DB "L",0
         DW 10
        DB "W",0
         DW 11
        DB "V",0
         DW 13

        DB SS,"W"
         DW MWash_
        DB "K",0
         DW MgnCol

        DB "J",0
         DW MChange_
        DB "U",0
         DW MUndo
        DB "Y",0
         DW SMUndo
        DB 13,0
         DW LookMgn
        DB "M",0
         DW Rmgn

        DB "K",SS
         DW 16
        DB "L",SS
         DW 17

        DB "E",0
         DW CM_MODE

        DB CS,"5"
         DW MLeft
        DB CS,"O"
         DW MLeft

        DB CS,"6"
         DW MDown
        DB CS,"A"
         DW MDown

        DB CS,"7"
         DW MUp
        DB CS,"Q"
         DW MUp

        DB CS,"8"
         DW MRight
        DB CS,"P"
         DW MRight

        DB "G",0
         DW MGridOn
        DB "H",0
         DW MGridOf
        DB "H",SS
         DW MGridOF
        DB CS,"D"
         DW MSCRM
        DB #FF

MSCRM   CALL mscr
        JP Bmgn

MSave   LD SP,(SPP)
        LD HL,FNameScr
        CALL FNEdit
        JP C,bmgnn
        CALL dsvscr_
        JP bmgnn

MLoad   LD SP,(SPP)
        LD HL,SORTscr
        CALL FilerE
        JP C,bmgnn
        CALL SV_UNDO
        LD A,(SEC_LEN)
        LD B,A
        EXLOAD #8000,SCREEN,START_SEC,ScrPage
        JP C,bmgnn
        LD DE,FNameScr
        CALL ToName
        JP bmgnn
;-----------------------
MGridOn CALL SV_UNDO
        CALL SV_BRT
        CALL SET_GRID
        JP bmgnn
MGridOf CALL SV_UNDO
        CALL LD_BRT
        JP bmgnn
MGridOF CALL SV_UNDO
        CALL groff
        JP bmgnn
; -------------------------
MLeft   LD A,(BX)
        SUB 8
        JP C,bmgn_
        LD (BX),A
        JP bmgn
MDown   LD IX,RMgn4
        LD A,24
        SUB (IX+3)
        Amul8
        LD B,A
        LD A,(BY)
        CP B
        JP NC,bmgn_
        ADD A,8
        LD (BY),A
        JP bmgn
MUp     LD A,(BY)
        SUB 8
        JP C,bmgn_
        LD (BY),A
        JP bmgn
MRight  LD IX,RMgn4
        LD A,32
        SUB (IX+2)
        Amul8
        LD B,A
        LD A,(BX)
        CP B
        JP NC,bmgn_
        ADD A,8
        LD (BX),A
        JP bmgn

WMgn    DEFB 0,0,32,24,0,0,%00000000
        DEFB 0
SMWin   DEFW Nil          ; объект-текст
        DEFW Nil          ; объект-подсветка (выборка)
        DEFW HMgn         ; горячие клавиши
mbuts   DEFW ButMgn       ; объект-кнопки (выборка)
        DEFW Nil          ; объект-флажки
        DEFW Nil          ; объект-радио кнопки
        DEFW Nil          ; ползунок
        DEFW Nil          ; вызов при создании
        DEFW IntMagnify   ; вызов на "прерываниях"
        DEFW #FFFF

redvig  LD A,(ARW_X)
        AND A
        JR Z,mleft
        INC A
        JR Z,mright
        LD A,(ARW_Y)
        AND A
        JR Z,mup
        CP #BF
        JR Z,mdown
        RET 

mdown   LD A,(DIRECT)
        BIT 2,A
        RET Z
        JP MDown

mright  LD A,(DIRECT)
        BIT 0,A
        RET Z
        JP MRight

mup     LD A,(DIRECT)
        BIT 3,A
        RET Z
        JP MUp

mleft   LD A,(DIRECT)
        BIT 1,A
        RET Z
        JP MLeft

;---------------------------------------------------------------
; Обработчки события Interupptut
; непосредственно сам редактор
;---------------------------------------------------------------
;                          Begin ----
;---------------------------------------------------------------
IntMagnify
        LD HL,(MARW_ADR)
        LD A,(ARW_X)
ML3     CP 192
        JP C,imagn_
        LD A,(arwf)
        CP 7
        JP Z,imagn_
        LD HL,ARWSPR
imagn_  LD (ARW_ADR),HL
imagn   JP IntMgn
;---------------------------
Was2_   LD HL,(OLD_XY)
        LD DE,(RXY)
        AND A
        SBC HL,DE
        RET Z
        JR Jp1
;---------------------------------------
IntMgn  LD DE,(ARW_X)
        CALL Mgn_Adr
        LD (dem+1),DE
        LD (bcm+1),BC

        LD A,(DIRECT)
        AND #10
        JR Z,Was3_
        LD HL,(NEW_XY)
        LD DE,(RXY)
        AND A
        SBC HL,DE
        JR Z,Was2_
Was3_   LD HL,#FFFF
        LD (NEW_XY),HL

Jp1     LD A,(DIRECT)
        AND #10
        JP Z,redvig

; проверка на попадание в окно
        LD HL,(RXY)
        LD (OLD_XY),HL
        LD (NEW_XY),HL

; процедура рисования точки
        LD A,(ARW_X)
ML4     CP 6*8*4
        RET NC

        LD A,STOP_ARW
        CALL GUI_
        LD A,ScrPage|#10
        LD BC,#7FFD
        OUT (C),A

bcm     LD BC,0
        LD L,C
        LD H,'PL_VIRT
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,B
        ADD A,(HL)
        LD E,A
        INC H
        LD A,(DE)
        AND (HL)
dem     LD DE,0
MMgn    JP MPoint
;---------------------------------------------------------------
;                              End ----
;---------------------------------------------------------------
MPoint  JP XOR_ST
met_st  EQU $-2

XOR_ST  JP Z,MSET_R
        JP MRES_R

MRES_R  PUSH BC
        CALL MRES
        POP BC
        LD L,C
        LD H,'PL_VIRT
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,B
        ADD A,(HL)
        LD E,A
        INC H
        LD A,(DE)
        OR (HL)
        XOR (HL)
        LD (DE),A
        LD (PRV),A
        CALL SET_COL
        CALL preview
        CALL PREVATR
        LD A,GUIPage|#10
        LD BC,#7FFD
        OUT (C),A
        RET 

MNOP_R  CALL SET_COL
        LD BC,(RXY)
        LD A,C
        ADD A,#C8
        LD C,B
        LD B,A
        LD L,C
        LD H,'PL_REAL
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,B
        ADD A,(HL)
        LD E,A
        CALL prevatr
        CALL PREVATR
        LD A,GUIPage|#10
        LD BC,#7FFD
        OUT (C),A
        RET 

MSET_R  CALL MSET
        LD L,C
        LD H,'PL_VIRT
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,B
        ADD A,(HL)
        LD E,A
        INC H
        LD A,(DE)
        OR (HL)
        LD (DE),A
        LD (PRV),A
        CALL SET_COL
        CALL preview
        CALL PREVATR
        LD A,GUIPage|#10
        LD BC,#7FFD
        OUT (C),A
        RET 

SET_COL LD D,B
        LD E,C
        DUP 3
        SRL D
        SRL E
        EDUP 
        LD A,E
        AND #18
        RRCA 
        RRCA 
        RRCA 
        OR #D8
        LD H,A
        LD A,E
        AND #07
        RRCA 
        RRCA 
        RRCA 
        ADD A,D
        LD L,A
        LD A,(HL)
        CALL MIXCOL
        LD (HL),A
        LD (ATS),A
        RET 

preview LD BC,(RXY)
        LD A,C
        ADD A,#C8
        LD C,B
        LD B,A
        LD L,C
        LD H,'PL_REAL
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,B
        ADD A,(HL)
        LD E,A
        INC H
        LD A,0
PRV     EQU $-1
        LD (DE),A
;----------- перенос на PREVIEW атрибуты
prevatr LD H,D
prevATR LD L,E
        LD A,H
        RRCA 
        RRCA 
        RRCA 
        AND #03
        OR #58
        LD H,A
        LD A,(ATS)
        LD (HL),A
        RET 
;----------------------------
; Выключение изменения атрибутов
MNoRgb  LD HL,rgbf
        CALL SwBut
        JR NZ,srgb1
        LD HL,#506B
        LD BC,#ED00
srgb0   LD A,B
        LD (PREVATR),A
        LD A,C
        LD (set_col),A
        LD A,H
        LD (SET_COL),A
        LD A,L
        LD (prevATR),A
        JP bmgnn
srgb1   LD HL,#C9C9
        LD B,H
        LD C,L
        JR srgb0

; Смена маски для стрелки
MArw    LD HL,arwf
        CALL SwBut
        JR NZ,sarw1
        LD HL,ARWSPR
sarw    LD (ARW_ADR),HL
        LD (MARW_ADR),HL
        JP bmgnn
sarw1   LD HL,ARWSPR+1
        LD DE,MARWSPR+1
        LD B,10
mla     LD A,(HL)
        LD (DE),A
        INC HL
        INC DE
        INC HL
        INC DE
        DJNZ mla
        LD HL,MARWSPR
        JR sarw

MARW_ADR DW ARWSPR

MARWSPR DB #FF,#00  ;маска + спр.
        DB #FF,#40
        DB #FF,#30
        DB #FF,#3C
        DB #FF,#18
        DB #FF,#14
        DB #FF,#02
        DB #FF,#00
        DB #FF,#00
        DB #FF,#00

;       CALL ARWM
;       LD A,(ARW_X)
;       CP 192
;       CALL NC,d
;       LD HL,(af)
;       LD (ARW_ADR),HL
;       RET
;-------------------------
; Атрибутная рамка
MRamka  LD HL,ramkaf
        CALL SwBut
        JR Z,sramka1
        LD A,#3A
sramka0 LD (CEL_P),A
        LD (CEL_G),A
        JP bmgnn
sramka1 LD A,#C9
        JR sramka0

CEL_P   DB #C9
        DW ARW_YC
        AND #FC
        LD (LLY),A
        LD A,(ARW_XC)
        AND #FC
        LD (LLY+1),A
celp1   CP 24
        RET NC
        JR CEL

CEL_G   DB #C9
        DW LLY+1
celp2   CP 24
        RET NC
CEL     LD DE,0
LLY     EQU $-2
        LD A,E
        AND #18
        OR #40
        LD H,A
        LD A,E
        AND #07
        RRCA 
        RRCA 
        RRCA 
        ADD A,D
        LD L,A
        LD BC,#0180
        LD A,(HL)
        XOR #E0
        LD (HL),A
        INC H
        LD A,(HL)
        XOR C
        LD (HL),A
        INC H
        LD A,(HL)
        XOR C
        LD (HL),A
        INC L
        INC L
        INC L
        LD A,(HL)
        XOR B
        LD (HL),A
        DEC H
        LD A,(HL)
        XOR B
        LD (HL),A
        DEC H
        LD A,(HL)
        XOR #07
        LD (HL),A
        INC E
        INC E
        INC E
        LD A,E
        AND #18
        OR #40
        LD H,A
        LD A,E
        AND #07
        RRCA 
        RRCA 
        RRCA 
        ADD A,D
        LD L,A
        DUP 5
        INC H
        EDUP 
        LD A,(HL)
        XOR C
        LD (HL),A
        INC H
        LD A,(HL)
        XOR C
        LD (HL),A
        INC H
        LD A,(HL)
        XOR #E0
        LD (HL),A
        INC L
        INC L
        INC L
        LD A,(HL)
        XOR #07
        LD (HL),A
        DEC H
        LD A,(HL)
        XOR B
        LD (HL),A
        DEC H
        LD A,(HL)
        XOR B
        LD (HL),A
        RET 

; Рисование линией
;----------------------------------------------------
MSTP    DB 0
NLine   LD HL,#FFFF
        LD (OLD_XY),HL
        LD (NEW_XY),HL
        XOR A
        LD (MSTP),A
        LD (FINT),A
        RET 

IntMgn2 CALL intmgn2
        LD A,GUIPage|#10
        LD BC,#7FFD
        OUT (C),A
        RET 

intmgn2 LD A,(MSTP)
        AND A
        JP NZ,ka1
        LD HL,#FFFF
        LD (OLD_XY),HL
        LD A,(DIRECT)
        AND #10
        CALL Z,redvig
        LD A,(DIRECT)
        CP #10
        RET NZ
        LD A,NOKEY
        CALL GUI_
        LD HL,(ARW_X)
        CALL RCOD
        LD (MARR1),HL
        LD A,1
        LD (MSTP),A
        RET 

MARR1   DW 0
MARR2   DW 0

MDrw    LD A,NOKEY
        CALL GUI
        LD A,ScrPage|#10
        LD BC,#7FFD
        OUT (C),A
        CALL NLine
        CALL IniMPl
        LD DE,(MARR1)
        LD A,D
        LD D,E
        LD E,A
        CALL Mgn_Adr
        PUSH BC
        LD DE,(MARR2)
        LD A,D
        LD D,E
        LD E,A
        CALL Mgn_Adr
        POP DE
        CALL LINE
resma   XOR A
        LD (FINT),A
        JP bmgn

IniMPl  LD A,0
MMet    EQU $-1
        LD BC,TBMMet
        CALL TAB_HL
        LD (PLOT_SNP+1),HL
        RET 

TBMMet  DW plxor
        DW plor
        DW pland
        DW set_col

plxor   LD L,E
        LD H,'PL_VIRT
        LD A,(HL)
        INC H
        LD B,(HL)
        INC H
        LD L,D
        ADD A,(HL)
        LD C,A
        INC H
        LD A,(BC)
        AND (HL)
        JP Z,plor
pland   LD L,E
        LD H,'PL_VIRT
        LD A,(HL)
        INC H
        LD B,(HL)
        INC H
        LD L,D
        ADD A,(HL)
        LD C,A
        INC H
        LD A,(BC)
        OR (HL)
        XOR (HL)
        LD (BC),A
set_col NOP 
        DUP 3
        SRL D
        SRL E
        EDUP 
        LD A,E
        AND #18
        RRCA 
        RRCA 
        RRCA 
        OR #D8
        LD H,A
        LD A,E
        AND #07
        RRCA 
        RRCA 
        RRCA 
        ADD A,D
        LD L,A
        LD A,(HL)
        CALL MIXCOL
        LD (HL),A
        RET 

plor    LD L,E
        LD H,'PL_VIRT
        LD A,(HL)
        INC H
        LD B,(HL)
        INC H
        LD L,D
        ADD A,(HL)
        LD C,A
        INC H
        LD A,(BC)
        OR (HL)
        LD (BC),A
        JR set_col
;-----------------

delmi   CALL EMLine
        JP resma

EMLine  LD HL,Ret
        LD DE,Ret
        CALL SInt1
        LD HL,COOR_N
        LD (COOR_P+1),HL
        LD HL,ARWSPR
        LD (ARW_ADR),HL
        LD A,(moderis)
        CP 3
        RET NZ
        LD A,(FINT)
        AND A
        RET NZ
        LD HL,(OLD_XY)
        LD DE,#FFFF
        AND A
        SBC HL,DE
        RET Z
        JP MDRW_LINE

ka1     LD A,(23560)
        CP #0C
        JP Z,delmi
        LD A,(DIRECT)
        AND A
        JR Z,zfi
        LD A,(FINT)
        AND A
        RET NZ
        INC A
        LD (FINT),A
        LD HL,(OLD_XY)
        LD DE,#FFFF
        AND A
        SBC HL,DE
        RET Z
        LD A,ScrPage|#10
        LD BC,#7FFD
        OUT (C),A
        CALL MDRW_LINE
        LD A,(DIRECT)
        CP #10
        JP Z,MDrw    
        RET 

MDRW_LINE
        LD DE,(MARR1)
        LD BC,(MARR2)
        DUP 2
        SRL D
        SRL E
        SRL B
        SRL C
        EDUP 
        CALL MLine
        RET 

zfi     LD A,(FINT)
        AND A
        RET Z
        LD HL,(ARW_X)
        LD (OLD_XY),HL
        CALL RCOD
        LD (MARR2),HL
        CALL MDRW_LINE
        XOR A
        LD (FINT),A
        RET 

;------------------------------
MSET    LD A,(PixOdd)
        AND A
        LD H,#F0
        JR NZ,PixOn1
        LD H,#0F
PixOn1  DUP 3
        LD A,(DE)
        OR H
        LD (DE),A
        INC D
        EDUP 
        LD A,(DE)
        OR H
        LD (DE),A
        RET 

MRES    LD A,0
PixOdd  EQU $-1
        AND A
        LD C,#0F
        LD HL,SHL1
Shl1    EQU $-2
        JR NZ,PixOff2
        LD C,#F0
        DUP 4
        INC HL
        EDUP 
PixOff2 DUP 3
        LD A,(DE)
        AND C
        OR (HL)
        LD (DE),A
        INC D
        INC HL
        EDUP 
        LD A,(DE)
        AND C
        OR (HL)
        LD (DE),A
        RET 

Mgn_Adr LD A,D
        AND #FC
        LD D,A
        SRL A
        SRL A
        LD H,A
        LD A,E
        LD C,E
        SRL A
        SRL A
        LD L,A
        SRL A
        LD E,A
        LD (RXY),HL
        LD A,L
        AND 1
        LD A,0
        JR NZ,dev
        INC A
dev     LD (PixOdd),A
        LD B,C
        LD C,D
        LD L,C
        LD H,'PL_REAL
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,B
        ADD A,(HL)
        LD E,A
        LD BC,0
RXY     EQU $-2
        LD HL,(BXY)
        ADD HL,BC
        LD C,H
        LD B,L
        RET 

CrMgn   LD A,ScrPage|#10
        LD BC,#7FFD
        OUT (C),A
        CALL IniMgn
        CALL PrevBox
        CALL PREVIEW
        CALL SETKA
        LD A,GUIPage|#10
        LD BC,#7FFD
        OUT (C),A
        RET 

IniMgn  LD DE,(BXY)
        DUP 3
        SRL D
        SRL E
        EDUP 
        PUSH DE
        CALL RASCH_
        LD A,#80        ;* Offset (see in plot-installer)
        ADD A,H
        LD H,A
        LD (SPIX),HL
        POP DE
        CALL RASCHA
        LD A,#80       ;*
        ADD A,H
        LD H,A
        LD (SATR),HL
        RET 
;---------------------------------------------------------------
BXY:
BX      DB 0
BY      DB 0

PREVATR LD DE,(ARW_X)
        DUP 3
        SRL D
        SRL E
        EDUP 
        LD A,D
        AND #FC
        LD D,A
        LD A,E
        AND #FC
        LD E,A
        CALL RASCHA
        LD A,0
ATS     EQU $-1
        LD BC,32-3
        DUP 4
        DUP 3
        LD (HL),A
        INC L
        EDUP 
        LD (HL),A
        ADD HL,BC
        EDUP 
        RET 

SETKA   CALL SETKATR
        LD HL,SCREEN
SPIX    EQU $-2
        LD DE,#4000
        LD B,48         ;СКОЛЬКО ПИКСЕЛОВ ПО Y
A3_     PUSH BC
        PUSH HL
        PUSH DE
ML2     LD A,6          ;СКОЛЬКО ЗНАКОМСТВ ПО X
A2_     EX AF,AF'
        LD A,(HL)
        LD C,A
        DUP 4
        PUSH HL
        XOR A
        LD H,A
        RL C
        RLA 
        RL C
        RLA 
        LD L,A
        ADD HL,HL
        ADD HL,HL
        PUSH DE
        LD DE,(SDATA)
        ADD HL,DE
        POP DE
        LD B,D
        DUP 4
        LD A,(HL)
        LD (DE),A
        INC L
        INC D
        EDUP 
        LD D,B
        POP HL
        INC E
        EDUP 
        EX AF,AF'
        INC L
        DEC A
        JP NZ,A2_
        POP DE
        DUP 3
        INC D
        EDUP 
        INC D
        LD A,D
        AND #07
        JR NZ,$+12
        LD A,E
        SUB #E0
        LD E,A
        JR NC,$+6
        LD A,D
        SUB #08
        LD D,A
        POP HL
        INC H
        LD A,H
        AND #07
        JR NZ,$+12
        LD A,L
        SUB #E0
        LD L,A
        JR NC,$+6
        LD A,H
        SUB #08
        LD H,A
        POP BC
        DEC B
        JP NZ,A3_
        RET 

SETKATR LD DE,SCREEN+#1800
SATR    EQU $-2
        LD HL,#5800
        LD B,6
B3      PUSH BC
        PUSH DE
        PUSH HL
ML6     LD B,6
B1      PUSH BC
        PUSH HL
        PUSH DE
        LD BC,#20
        DUP 4
        PUSH HL
        LD A,(DE)
        DUP 4
        LD (HL),A
        INC L
        EDUP 
        POP HL
        ADD HL,BC
        EDUP 
        POP DE
        INC E
        POP HL
        DUP 4
        INC L
        EDUP 
        POP BC
        DJNZ B1
        POP HL
        LD BC,#20*4
        ADD HL,BC
        EX DE,HL
        POP HL
        LD BC,#20
        ADD HL,BC
        EX DE,HL
        POP BC
        DJNZ B3
        RET 

PREVIEW LD DE,#5839-32
        LD HL,(SATR)
ML1     LD B,6
PR3     PUSH BC
        PUSH HL
        DUP 6
        LDI 
        EDUP 
        EX DE,HL
        LD DE,#20-6
        ADD HL,DE
        EX DE,HL
        POP HL
        LD BC,#20
        ADD HL,BC
        POP BC
        DJNZ PR3
        LD DE,#4019
        LD HL,(SPIX)
        LD B,6*8
PR1     PUSH BC
        PUSH HL
        PUSH DE
        DUP 6
        LDI 
        EDUP 
        POP HL
        CALL INC_Y
        EX DE,HL
        POP HL
        CALL INC_Y
        POP BC
        DJNZ PR1
        RET 

PrevBox LD B,6
        LD HL,#4018
        PUSH HL
        LD (HL),1
        LD HL,#4018+7
        LD (HL),#80
        POP HL
        LD B,6*8-1
dj2     PUSH BC
        CALL INC_Y
        LD (HL),1
        POP BC
        DJNZ dj2
        CALL INC_Y
        LD B,6
        LD (HL),1
        LD A,#FF
dj3     INC L
        LD (HL),A
        DJNZ dj3
        INC L
        LD (HL),#80
        LD B,6*8-1
dj4     PUSH BC
        CALL DEC_Y
        LD (HL),#80
        POP BC
        DJNZ dj4
        CALL DEC_Y
        RET 

SDATA   DW SDATA1

ButMgn  DEFB 24,7,2,2,5,%11
metcol  EQU $-2
        DEFW XORS
        DEFB 26,7,2,2,7,%11
        DEFW ORS
        DEFB 28,7,2,2,7,%11
        DEFW ANDS
        DEFB 30,7,2,2,7,%11
        DEFW NOPS
        DEFB 24,9,2,2,7,%11
setkaf  EQU $-2
        DEFW SETKAS
        DEFB 26,9,2,2,7,%11
        DEFW RGBS
        DEFB 28,9,2,2,7,%11
ramkaf  EQU $-2
        DEFW RAMKAS
        DEFB 30,9,2,2,7,%11
arwf    EQU $-2
        DEFW AMASKS
        DEFB 24,11,2,2,7,%11
        DEFW UNDOS
        DEFB 26,11,2,2,7,%11
        DEFW MGFS
        DEFB 28,11,2,2,7,%11
        DEFW EK2S
        DEFB 30,11,2,2,7,%11
rgbf    EQU $-2
        DEFW NORGBS
        DEFB 24,13,2,2,6,%11
mchspr  DEFW PRIMS
        DEFB 26,13,2,2,7,%11
        DEFW OTHERS
        DEFB 28,13,2,2,7,%00
mrgb    EQU $-2
        DEFW DTRS
        DEFB 30,13,2,2,7,%11
        DEFW EXITS 
        DEFB #FF

moderis DB 0

MWash_  CALL EMLine
        JP MWash

MChange_
        CALL EMLine
        LD IX,WChan
        CALL POPUP
        JR mcal

MChange LD IX,WChan
        LD HL,#0D18
        LD (WChan),HL
mcal    LD A,%1000
        LD (IX+6),A
        CALL GETBLK
        CALL Win_
        PUSH AF
        CALL CLOSE
        CALL NOKEY_
        POP AF
        CP #FF
        JP Z,bmgn_
mchange PUSH AF
        LD (moderis),A
        LD HL,IntMgn
        LD (imagn+1),HL
        Amul8
        LD BC,schan
        LD H,0
        LD L,A
        ADD HL,BC
        LD DE,mchspr
        LD BC,2
        LDIR 
        POP AF
        PUSH AF
        LD BC,TBMMgn
        CALL TAB_HL
        LD (MMgn+1),HL
        POP AF
        CP 2
        JP Z,MTFill
        CP 3
        JR Z,poska
        CP 4
        JP Z,MWash
        JP bmgn

poska   LD HL,IntMgn2
        LD (imagn+1),HL
        JP bmgn_

TBMMgn  DW MPoint
        DW MSolid
        DW MTFill_
        DW Ret
        DW Ret

TMBuff  DS 32

WChan   DEFB 24,13,2,10,7*8,7,%00001000
; Bit 0 - окно с заголовком/без
; Bit 1 - "перерисовать" окно да/нет
; Bit 2 - тискать окно по экрану да/нет
; Bit 3 - выход из окна-ткнуть только лишь за пределами (CANCEL)
        DEFB 0
        DEFW Nil          ; объект-текст
        DEFW Nil          ; объект-подсветка (выборка)
        DEFW HChan        ; горячие клавиши
        DEFW BChan        ; объект-кнопки (выборка)
        DEFW #FFFF

HChan   DB "J",0
         DW 0
        DB "N",0
         DW 1
        DB "M",0
         DW 3
        DB #FF

BChan   DEFB 0,0,2,2,7,%11
schan   DEFW PRIMS
        DEFB 0,2,2,2,7,%11
        DEFW FILLS
        DEFB 0,4,2,2,7,%11
        DEFW FILLS2
        DEFB 0,6,2,2,7,%11
        DEFW LINES
        DEFB 0,8,2,2,7,%11
        DEFW WASHS1
        DEFB #FF

MLine   LD HL,MPlot
        LD (PLOT_SNP+1),HL
        CALL LINE
        RET 

; DE - XY (кратные четырем)

MPlot   LD A,D
ML5     CP 24*2
        RET NC
        PUSH DE
        PUSH DE
        SRL E
        SRL D
        LD A,E
        AND #18
        OR #40
        LD H,A
        LD A,E
        AND #07
        RRCA 
        RRCA 
        RRCA 
        ADD A,D
        LD L,A
        POP DE
        LD A,E
        AND 1
        JR Z,NOSL
        DUP 4
        INC H
        EDUP 
NOSL    LD A,D
        AND 1
        LD C,#0F
        JR NZ,OFL
        LD C,#F0
OFL     DUP 4
        LD A,(HL)
        XOR C
        LD (HL),A
        INC H
        EDUP 
        POP DE
        RET 

LMgn    EQU $-BMgn

        ENT 

