
ESNAPE  LD IX,WSnape
        LD DE,#1602
        LD (IX+0),D
        LD A,(MWin+1)
        INC A
        INC A
        LD (IX+1),A
        CALL popup
esnape  CALL GETBLK
esnape_ XOR A
    display "esnape_ ",esnape_
        LD (IX+6),A
        CALL Win_
        CP #FF
        JP Z,main_
        LD BC,TBSnape
        JP TAB_JP

TBSnape DW EPLOT
        DW ELINE
        DW ELINE2
        DW ETR
        DW EREC
        DW ECIRC
        DW EELLIP
        DW ERAY
        DW SEF
        DW SEF
        DW SEF
        DW SEF
        DW EERASE

SNP_MODE DB 0

SEF     LD A,(Numb)
        SUB 8
        LD (SNP_MODE),A
        LD HL,effpl
        LD B,4
        CALL SetBCol
        JR esnape_

ESNAPE_ LD IX,WSnape
        CALL POPUP
        JR esnape

WSnape  DEFB 22,2,4,16,7*8,7,%00000000
; Bit 0 - окно с заголовком/без
; Bit 1 - "перерисовать" окно да/нет
; Bit 2 - тискать окно по экрану да/нет
; Bit 3 - выход из окна-ткнуть только лишь за пределами (CANCEL)
        DEFB 0
        DEFW Nil          ; объект-текст
        DEFW Nil          ; объект-подсветка (выборка)
        DEFW HSnape       ; горячие клавиши
        DEFW BSnape       ; объект-кнопки (выборка)
        DEFW #FFFF

HSnape  DB "J", 0: DW 12
    db "L", 0: dw 1
    db "K", 0: dw 2
    DB "D", 0: DW 0
    DB "T", 0: DW 3
    DB "S", 0: DW 4
        DB "C", 0: DW 5
    DB "E", 0: DW 6
    db "R", 0: dw 7
    DB #FF

BSnape  DEFB 0,0,2,2,7,%11
        DEFW PLOTS
        DEFB 0,2,2,2,7,%11
        DEFW LINES
        DEFB 0,4,2,2,7,%11
        DEFW LINES2
        DEFB 0,6,2,2,7,%11
        DEFW TRS
        DEFB 0,8,2,2,7,%11
        DEFW SQS
        DEFB 0,10,2,2,7,%11
        DEFW CIRCS
        DEFB 0,12,2,2,7,%11
        DEFW ELPS
        DEFB 0,14,2,2,7,%11
        DEFW RAYS
        DEFB 2,0,2,2,5,%11  ;
effpl   EQU $-2
        DEFW ORS
        DEFB 2,2,2,2,7,%11
        DEFW XORS
        DEFB 2,4,2,2,7,%11
        DEFW ANDS
        DEFB 2,6,2,2,7,%11
        DEFW NOPS
        DEFB 2,8,2,2,7,%11
        DEFW ERASES
        DEFB #FF

EERASE  CALL SV_UNDO
        CALL LOADSCR
        LD HL,ArwNew1
        LD DE,ArwNew2
        LD A,ARW_NEW
        CALL GUI

eerase  CALL XOR_PUT
        LD HL,Ret
        CALL ARW_SCN_
        JP C,Main_SV
        LD A,STOP_ARW
        CALL GUI

        CALL AND_PUT
        LD BC,(ArwXN)
        LD HL,SPERASE
        CALL OutPix
        JR eerase

ArwNew1 LD HL,SPERASE
        LD BC,(ARW_X)
        LD (ArwXN),BC
        JP OutPix

ArwNew2 LD HL,SPERASE
        LD BC,0
ArwXN   EQU $-2
        JP OutPix

SPERASE DB %00111100
        DB %01111110
        DB %11111111
        DB %11111111
        DB %11111111
        DB %11111111
        DB %01111110
        DB %00111100

INST_SNAPE
        LD (DRW_LINE+1),HL
        CALL INSTALL_PLOT
        CALL SV_UNDO
        CALL LOADSCR
        LD DE,SnapeCur
        CALL SV_ARW
         ld hl,#0101           ; смещение курсора /DA
         ld (ARW_XDISP),hl
        RET

EX_SNAPE_
        CALL DRW_LINE
EX_SNAPE       
        CALL SAVESCR
        CALL LD_ARW
         call retSWpgn
        JP Main

PL_COL  LD HL,PLOT_C
        LD A,#77
pl_col  LD (PLOT_SNP+1),HL
        LD (elcol),A
        RET
PL_NO   LD HL,PLOT
        XOR A
        JR pl_col

; рисование точкой

EPLOT   LD HL,DRW_LINE1
        CALL INST_SNAPE
        CALL MODE_PL
        CALL PL_COL
eplot   LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        LD HL,(ARW_X)
        CALL RCOD
        EX DE,HL
        CALL PLOT_SNP
        LD A,STOP_ARW
        CALL GUI
        JR eplot

; рисование точкой

EELLIP  LD A,1
       ; CALL LPGN2
         call SWPGN2
        LD HL,DRW_LINE6
        CALL INST_SNAPE
        CALL MODE_PL
eellip  CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        CALL NOKEY_
        CALL eline_
        JP C,EX_SNAPE_
        JR eellip

DRW_LINE6
        LD BC,(PLY1)
        LD A,B
        LD B,C
        LD C,A
        LD DE,(PLY2)
        LD A,D
        LD D,E
        LD E,A
        CALL ELLIPSE
        RET

; рисование линией

ELINE   LD HL,DRW_LINE1
        CALL INST_SNAPE
eline   CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        CALL NOKEY_
        CALL eline_
        JP C,EX_SNAPE_
        JR eline

; ломаная линия

ELINE2  LD HL,DRW_LINE1
        CALL INST_SNAPE
eline2_ CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        CALL NOKEY_
eline2  CALL eline_
        JP C,EX_SNAPE_
        LD A,(FILKA)
        AND A
        JR NZ,eline2_
        JR eline2

; обнулить параметры
NEWSN   LD HL,#FFFF
        LD (OLD_XY),HL
        XOR A
        LD (FINT),A
        LD (FILKA),A
        JP XOR_PL

; тругольник
ETR     CALL INST_SNAPE
etr     LD HL,DRW_LINE1
        LD (DRW_LINE+1),HL
        CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        CALL NOKEY_
        CALL eline_
        JP C,EX_SNAPE_
        LD A,(FILKA)
        AND A
        JR NZ,etr
        CALL NEWSN
        LD HL,DRW_LINE2
        LD (DRW_LINE+1),HL
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY3),HL
        LD HL,IntLine
        CALL ARW_SCN_
        JP C,EX_SNAPE_
        LD A,(FILKA)
        AND A
        JR NZ,etr
        CALL NOKEY_
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY2),HL
        CALL DRW_LINE
        CALL MODE_PL
        CALL PL_COL
        CALL DRW_LINE
        JR etr

; прямоугольник

EREC    CALL INST_SNAPE
eline3  LD HL,DRW_LINE1
        LD (DRW_LINE+1),HL
        CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        CALL NOKEY_
        LD A,(FILKA)
        AND A
        JR NZ,eline3
        LD HL,DRW_LINE3
        LD (DRW_LINE+1),HL
        CALL eline_
        JP C,EX_SNAPE_
        JR eline3

; кружочек
ECIRC   LD HL,DRW_CIRC
        CALL INST_SNAPE
eline4  CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY1),HL
        LD (PLY2),HL
        CALL NOKEY_
        LD A,(FILKA)
        AND A
        JR NZ,eline4
        LD HL,IntLine
        CALL ARW_SCN_
        JP C,EX_SNAPE_
        CALL NOKEY_
        LD A,(FILKA)
        AND A
        JR NZ,eline4
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY2),HL
        CALL PL_COL
        CALL DRW_LINE
        CALL MODE_PL
        CALL PL_COL
        CALL DRW_LINE
        JR eline4

; лучевая линия

ERAY    LD HL,DRW_LINE1
        CALL INST_SNAPE
eray    CALL NEWSN
        LD HL,Ret
        CALL ARW_SCN_
        JP C,EX_SNAPE
        CALL NOKEY_
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY1),HL
eline5  CALL NEWSN
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY2),HL
        LD HL,IntLine
        CALL ARW_SCN_
        JP C,EX_SNAPE_
        LD A,(FILKA)
        AND A
        JR NZ,eray
        CALL NOKEY_
        CALL PL_NO
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY2),HL
        CALL DRW_LINE
        CALL MODE_PL
        CALL PL_COL
        CALL DRW_LINE
        JR eline5

FILKA   DB 0
;--
SZ      POP AF
        POP AF
        CALL DRW_LINE
        XOR A
        LD (23560),A
        INC A
        LD (FILKA),A
        SCF
        CCF
        RET

IntLine
        CALL PL_NO
        LD A,(23560)
        CP #0C
        JR Z,SZ
        XOR A
        LD (FILKA),A
        LD A,(DIRECT)
        AND A
        JR Z,zfint
        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
        CALL DRW_LINE
        RET

zfint   LD A,(FINT)
        AND A
        RET Z
        LD HL,(ARW_X)
        LD (OLD_XY),HL
        CALL RCOD
        LD (PLY2),HL
        CALL DRW_LINE
        XOR A
        LD (FINT),A
        RET

DRW_LINE
        JP 0

DRW_LINE1
        LD DE,(PLY1)
        LD BC,(PLY2)
        CALL LINE
        RET

DRW_LINE2
        CALL DRW_LINE1
        LD DE,(PLY2)
        LD BC,(PLY3)
        CALL LINE
        RET

DRW_LINE3
        LD HL,(PLY2)
        LD DE,(PLY1)
        LD A,D
        CP H
        CALL C,bigx
        LD A,E
        CP L
        CALL C,bigy
        LD A,D
        SUB H
        LD B,A
        LD A,E
        SUB L
        LD C,A
        LD (PLY3),HL
        LD (PLY4),DE
        LD A,B
        AND A
        JR Z,leftb
        LD A,C
        AND A
        JR Z,upb
        CALL upb
        CALL rightb
        CALL downb
        CALL leftb
        RET

downb   LD A,B
        AND A
        RET Z
        LD DE,(PLY3)
        LD A,E
        ADD A,C
        LD E,A
        PUSH BC
djnz4   PUSH BC
        PUSH DE
        CALL PLOT_SNP
        POP DE
        INC D
        POP BC
        DJNZ djnz4
        POP BC
        RET

leftb   LD A,C
        AND A
        RET Z
        LD DE,(PLY3)
djnz5   PUSH DE
        PUSH BC
        CALL PLOT_SNP
        POP BC
        POP DE
        INC E
        DEC C
        JR NZ,djnz5
        RET

upb     LD A,B
        AND A
        RET Z
        LD DE,(PLY3)
        PUSH BC
djnz2   PUSH BC
        PUSH DE
        CALL PLOT_SNP
        POP DE
        INC D
        POP BC
        DJNZ djnz2
        POP BC
        RET

rightb  LD A,C
        AND A
        RET Z
        LD DE,(PLY3)
        LD A,D
        ADD A,B
        LD D,A
        PUSH BC
        INC C
djnz3   PUSH DE
        PUSH BC
        CALL PLOT_SNP
        POP BC
        POP DE
        INC E
        DEC C
        JR NZ,djnz3
        POP BC
        RET

bigx    LD A,D
        LD D,H
        LD H,A
        RET
bigy    LD A,E
        LD E,L
        LD L,A
        RET

eline_  XOR A
        LD (FINT),A
        CALL XOR_PL
        CALL PL_NO
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY1),HL
        LD (PLY2),HL
        LD (PLY3),HL
        LD (PLY4),HL
        LD HL,IntLine
        CALL ARW_SCN_
        RET C
        LD A,(FILKA)
        AND A
        RET NZ
        CALL NOKEY_
        CALL PL_NO
        LD HL,(ARW_X)
        CALL RCOD
        LD (PLY2),HL
        CALL DRW_LINE
        CALL MODE_PL
        CALL PL_COL
        CALL DRW_LINE
        XOR A
        RET

FINT    DB 0 ; счетчик прерываний
PLY1    DB 0
PLX1    DB 0
PLY2    DB 0
PLX2    DB 0
PLY3    DB 0
PLX3    DB 0
PLY4    DB 0
PLX4    DB 0

RCOD    LD A,H
        LD H,L
        LD L,A
        RET

;LINE    PUSH BC             ; заменил процедуру вывода линии на чуть меньшую по размеру /DA
;        LD (COORD),DE
;        CALL PLOT_SNP
;        POP DE
;        LD HL,(COORD)
;        EXX
;        PUSH HL
;        EXX
;        LD (STK_P),SP
;        LD BC,#0101
;        LD A,D
;        SUB H
;        JR NC,C_1
;        LD B,#FF
;        NEG
;C_1     LD D,A
;        LD A,E
;        SUB L
;        JR NC,C_2
;        LD C,#FF
;        NEG
;C_2     LD E,A
;        LD A,D
;        CP E
;        JR NC,C_3
;        LD L,D
;        LD (SIGN),BC
;        XOR A
;        LD B,A
;        JR C_4
;C_3     OR D
;        JR Z,FINISH
;        LD L,E
;        LD E,D
;        LD (SIGN),BC
;        LD C,0
;C_4     LD H,E
;        LD A,E
;        RRA
;REPEAT  ADD A,L
;        JR C,C_5
;        CP H
;        JR C,C_6
;C_5     SUB H
;        LD D,A
;        EXX
;        LD DE,0
;SIGN    EQU $-2
;        JR C_7
;C_6     LD D,A
;        PUSH BC
;        EXX
;        POP DE
;C_7     LD HL,0
;COORD   EQU $-2
;        LD A,E
;        ADD A,L
;        LD E,A
;        LD A,D
;        INC A
;        ADD A,H
;        JR C,C_8
;        JR Z,FINISH
;C_9     DEC A
;        LD D,A
;        LD A,191
;        CP E
;        JR C,FIN
;        LD (COORD),DE
;        CALL PLOT_SNP
;        EXX
;        LD A,D
;        DEC E
;        JR NZ,REPEAT
;        JR FINISH
;C_8     JR Z,C_9
;FINISH  LD SP,0
;STK_P   EQU $-2
;        EXX
;        POP HL
;        EXX
;        RET

;FIN     EXX
;        JR FINISH


LINE
         ; de -first pixel , bc -last pixel
        ld l,c
        ld h,b
        ex de,hl

        ; Bresenham's line algorithm
        ; de - first pixel , hl - last pixel

.lineB  call .plot  ; вывод начальной точки
.lineN
        push hl
        or a
        sbc hl,de         ; проверяем на нулевую длину
        pop hl
       ret z

.noequ        ; de - y0x0 , hl - y1x1

        ld a,l
        ld l,#1C   ; inc e
        sub e      ;dx x1-x0
       jr nc, 1F
        neg
        inc l      ; l=#1D - dec e : dx<0  (.Xstep)
1
        ld b,a       ; b = |x1-x0|   (.dX)

        ld a,h
        ld h,#14   ; inc d
        sub d      ; dy y1-y0
       jr nc, 1F
        neg
        inc h     ; h=#15 - dec d : dy<0   (.Ystep)
1
        ld c,a     ; c =  |y1-y0|    (.dY)

        cp b        ; dx > dy ведём линию по X , dy > dx ведём линию по Y
       jr c,1F
        ld c,b      ; меняем местами X и Y
        ld b,a
        ld a,h
        ld h,l
        ld l,a
        ld a,c
1
        ld (.dERR),a
        ld a,b
        ld (.dM),a
        ld a,l
        ld (.Step1),a
        ld a,h
        ld (.Step2),a


        ld hl,0     ; err- проверка ; de - y0x0
.loopln
        push bc

.Step1   inc e       ;Xstep x=x+1  / x=x-1 / y=y+1 / y=y-1
         ld bc,0

.dERR  EQU $-2        ;dERR   dY or dX
         add hl,bc    ; err=err+dY or err+dX
1
         ld bc,0
.dM EQU $-2           ; dX or dY
        bit 7,h
       jr nz,1F
        push hl
         add hl,hl
         or a
         sbc hl,bc
        pop hl
       jr c,1F      ; if (err*2 < dM) goto 1F
        sbc hl,bc   ; err = err-dM
.Step2  inc d       ;Ystep y=y+1  / y=y-1 / x=x+1 / x=x-1
1
        call .plot
        pop bc
      djnz .loopln
        ret

.plot                      ; вход de: e=x , d=y
        push hl,de,bc
        call PLOT_SNP      ; вывод точки меняется от условий
.plotADR EQU $-2
        pop bc,de,hl
        ret




PLOTT   EQU     #BC00

INSTALL_PLOT
        XOR A
        LD (FLAGOBJ),A
        LD HL,PLOTT
        LD DE,#4000
install_plot
        LD B,E
        LD C,#80
        LD HX,4
L_3     LD LX,8
L_2     LD A,8
L_1     LD (HL),E
        INC H
        LD (HL),D
        INC H
        LD (HL),B
        INC H
        LD (HL),C
        RRC C
        DEC H
        DEC H
        DEC H
        INC HL
        INC D
        DEC A
        JR NZ,L_1
        INC B
        LD A,B
        AND 31
        LD B,A
        LD A,D
        SUB 8
        LD D,A
        LD A,E
        ADD A,#20
        LD E,A
        DEC LX
        JR NZ,L_2
        LD A,D
        ADD A,8
        LD D,A
        DEC HX
        JR NZ,L_3
        RET

SET_SNP LD (PLOT_SNP+1),HL
        RET

PLOT_SNP JP PLOT

PLOT_C  LD A,E
        CP 192
        RET NC
        PUSH DE
        DUP 3
        SRL D
        SRL E
        EDUP
        CALL RASCHA_
        LD A,(HL)
        CALL MIXCOL
        LD (HL),A
        POP DE
PLOT    LD A,E
        CP 192
        RET NC
plott   LD L,E
        LD H,high PLOTT
        LD A,(HL)
        INC H
        LD B,(HL)
        INC H
        LD L,D
        ADD A,(HL)
        LD C,A
        INC H
        LD A,(BC)
modepl  OR (HL)
        NOP
        LD (BC),A
        RET

MODE_PL LD A,(SNP_MODE)
        AND A
        JR Z,OR_PL
        DEC A
        JR Z,XOR_PL
        DEC A
        JR Z,AND_PL
        LD HL,#0000
        JR or_pl_

OR_PL   LD HL,#00B6
        XOR A
or_pl   LD (GMODE),A
or_pl_  LD (modepl),HL
        RET
XOR_PL  LD HL,#AE00
        LD A,1
        JR or_pl
AND_PL  LD HL,#AEB6
        LD A,2
        JR or_pl

;---------------------------------------;
;Algorythm designed by SerzhSoft (c)1996
;рисование окружности
; HL=x, DE=y (-32768..+32767)
; A=радиус (0..255) ;при 0 - точка
;---------------------------------------;
CIRCLE  LD A,(PLY1)
        LD H,#00
        LD L,A
        LD (X_CIRC),HL
        LD A,(PLX1)
        LD D,#00
        LD E,A
        LD (Y_CIRC),DE
        LD A,0
RADIUS  EQU $-1
        LD E,A
        LD C,#00
        LD B,C
        LD D,C
        SRL A
LC1     EX AF,AF'
LC2     CALL PX8
        INC C
        EX AF,AF'
        SUB C
        JP NC,LC1
        DEC E
        ADD A,E
        EX AF,AF'
        LD A,E
        CP C
        JP NC,LC2
        RET
;
PX8     CALL PX4
;
PX4     LD A,C
        LD C,E
        LD E,A
        LD HL,0
X_CIRC  EQU $-2
        PUSH HL
        ADD HL,BC
        CALL PX2
        POP HL
        SBC HL,BC
;
PX2     INC H
        DEC H
        RET NZ
        LD A,L
        LD (X_NEW),A
        LD HL,0
Y_CIRC  EQU $-2
        PUSH HL
        ADD HL,DE
        CALL PX1
        POP HL
        SBC HL,DE
;
PX1     INC H
        DEC H
        RET NZ

        LD A,L
;       CP #C0
;       RET NC

        PUSH DE
        LD D,A
        LD E,0
X_NEW   EQU $-1
        PUSH BC
        CALL PLOT_SNP
        POP BC
        POP DE
        RET

DRW_CIRC
        LD DE,(PLY1)
        LD BC,(PLY2)
        CALL SPLEN
        CALL CIRCLE
        RET

SPLEN
; SQRT((X1-X2)^2+(Y1-Y2)^2)
; DE=X1Y1  ARW
; BC=X2Y2  KOORD
        CALL GET_BIGX
        CALL GET_BIGY
        LD A,0
X2      EQU $-1
        LD B,A
        LD A,0
X1      EQU $-1
        SUB B
        LD H,0
        LD L,A
        CALL Sqr
        PUSH HL
        LD A,0
Y2      EQU $-1
        LD B,A
        LD A,0
Y1      EQU $-1
        SUB B
        LD H,0
        LD L,A
        CALL Sqr
        POP DE
        ADD HL,DE
        CALL Sqrt
        LD A,L
        DEC A
        LD (RADIUS),A
        RET

GET_BIGX:
        LD A,D
        CP B
        JR C,dbigx
        LD A,B
        LD B,D
        LD D,A
dbigx   LD A,B
        LD (X1),A
        LD A,D
        LD (X2),A
        RET

GET_BIGY:
        LD A,E
        CP C
        JR C,dbigy
        LD A,E
        LD E,C
        LD C,A
dbigy   LD A,C
        LD (Y1),A
        LD A,E
        LD (Y2),A
        RET

SQRT    LD A,H       ;HL = SQRT (HL)
        OR L         ;ВЫЧИСЛЯЕМ КВАДРАТНЫЙ КОРЕНЬ,
        RET Z        ;ИСПОЛЬЗУЯ ПРИ ЭТОМ ВСЕГО ОДНО ДЕЛЕНИЕ!
        LD D,H       ;(C) CARDINAL/PGC/BDA
        LD E,L
        LD BC,#E000
        ADD HL,BC
        JR C,SQRT2
        LD H,D
        LD L,E
        ADD HL,HL
        ADD HL,HL
        ADD HL,HL
        LD B,12
SQRTL   ADD HL,HL
        JR C,SQRT1
        DJNZ SQRTL
        LD HL,1
        RET
SQRT2   XOR A
        ADD HL,BC
        JR NC,SQRT3
        INC A
        JP SQRT2+1
SQRT3   LD HL,TABL1
        LD C,A
        JP SQRT1+4
SQRT1   LD HL,TABL
        LD C,B
        LD B,0
        ADD HL,BC
        LD C,(HL)
SQRT_   PUSH BC
        CALL Div
        EX DE,HL
        POP BC
        ADD HL,BC
        SRL H
        RR L
        RET

TABL    DEFB 2,2,2,3,5,7,10,14,19,27,39,55,77
TABL1   DEFB 109,142,169,192,212,231,248

Sqrt    PUSH BC
        PUSH DE
        PUSH AF
        CALL SQRT
        INC L
        POP AF
        POP DE
        POP BC
        RET

Sqr     PUSH DE
        PUSH AF
        LD H,L
        LD A,H
        CALL MULS
        INC L
        POP AF
        POP DE
        RET

;....
MULS    LD L,0 ;HL = A*H
        LD D,L
        LD E,A
        DUP 8
        ADD HL,HL
        JR NC,$+3
        ADD HL,DE
        EDUP
        RET

; HL=B*C
Mult    XOR A
        CP B
        JR Z,ZERO_HL
        CP C
        JR Z,ZERO_HL
        LD E,C
        LD HL,0
        LD D,0
L1      ADD HL,DE
        DJNZ L1
        RET
ZERO_HL LD HL,0
        RET

DIV     LD B,D
        LD C,E
        LD D,H
        LD E,L
        CALL Div
        LD H,D
        LD L,E
        RET

Div     PUSH BC         ;DE = DE/BC С ОКРУГЛЕНИЕМ ОТВЕТА
        LD A,B
        CPL
        LD B,A
        LD A,C
        CPL
        LD C,A
        INC BC
        LD HL,0
        LD A,E
        ADD A,A
        RL D
        DUP 16
        ADC HL,HL
        ADD HL,BC
        JR C,$+4
        SBC HL,BC
        RLA
        RL D
        EDUP
        LD E,A
        POP BC
        SRL B
        RR C
        OR A
        SBC HL,BC
        RET C
        INC DE
        RET

Div3         push bc   ;   a=a/3, set flag Z - if modulo = 2 ; /DA
             ld c,0
1             sub 3
             jr c,2F
              inc c
             jr 1B
2             add a,3
              cp 2
              ld a,c
             pop bc
            ret

; курсорчик
WinCur
SnapeCur DB #1F,#A0,#5F,#00,#1F,#A0,#FF,#00
         DB #FF,#00,#FF,#00,#FF,#00,#FF,#00
         DB #FF,#00,#FF,#00                  ;,#FF,#00,#FF,#00  ; - лишнее /DA
         ; DB #FF,#00,#FF,#00,#FF,#00,#FF,#00

GMODE    DB 1

; DE
; BC

ELLIPSE CALL    CHANGE_SET_MODE
        CALL    PREPARATION
        CALL    ELLIPSE_BODY
        RET

CHANGE_SET_MODE:
 ; СМЕНА КОДА В СООТВЕТ. С OR/XOR РЕЖИМОМ

        LD      A,(GMODE);    SET_MODE
        OR      A
        JR      Z,CH_SM01
        LD      L,#AB        ; XOR E
        LD      A,#AE        ; XOR (HL)
        JR      CH_SM02
CH_SM01 LD      L,#B3        ; OR E
        LD      A,#B6        ; OR (HL)
CH_SM02 LD      (E_SET01),A
        LD      (E_SET02),A
        LD      (E_SET03),A
        LD      A,L
        LD      (E_SET05),A
        RET
; Начало координат теперь внизу (BASIC)

RECOD   LD A,191
        SUB C
        LD C,A
        LD A,191
        SUB E
        LD E,A
        RET



       DISPLAY "Primitive len   :",/a, $-ESNAPE