        MAIN "",134

; +18
; x, y, за раз эл-ов, всего эл-ов, высота, позиция после "за-раз
Scroll  PUSH IX
        LD HL,(ScrAdr)
        CPHL0
        JP Z,PopIX2

        PUSH HL
        POP IX

        LD DE,(RealScr)

        LD A,(ARW_XC)
        CP D
        JP NZ,PopIX2
        LD A,(ARW_YC)
        INC A
        CP E
        JP C,PopIX2
        LD A,(IX+4)
        ADD A,E
        LD HL,ARW_YC
        CP (HL)
        JP C,PopIX2

        DEC E
        LD A,D
        LD D,E
        LD E,A

        LD HL,(ARW_XC)
        AND A
        SBC HL,DE
        JP Z,ScrUp
        LD A,(IX+4)
        ADD A,D
        INC A
        LD D,A
        LD HL,(ARW_XC)
        AND A
        SBC HL,DE
        JR Z,ScrDn

        XOR A
        LD (IX+5),A

fucker  LD B,0
        LD C,(IX+5)
        LD DE,0
STP     EQU $-2
        CALL MUL16
        CALL DIV256
        LD A,(RealScr)
        Amul8
        ADD A,L
        LD E,A

        LD A,0
HGT     EQU $-1
        SRL A
        ADD A,E
        INC A
        LD E,A

        LD A,(ARW_Y)
        CP E
        JR NC,ZAA

sdirr   POP IX
        HALT 
        CALL DrwScroll
        CALL DIRSCN
        CALL MOVES
        CALL MOVES
sdir_   CALL DIRSCN
        LD A,(DIRECT)
        CP #10
        JR Z,sdir_
        AND #10
        JP Z,Arrow
        JP Scroll

ZAA     LD A,(IX+3)
        SUB (IX+2)
        LD B,A
        LD A,(IX+5)
        CP B
        JR NC,zaa
        INC A
        LD (IX+5),A
        JR fucker

zaa     LD A,(IX+3)
        AND A
        JR Z,nomove_
        SUB (IX+2)
        JR C,nomove_
        LD (IX+5),A
        JR sdirr

PopIX2  POP IX
        RET 

ScrDn   LD A,(IX+3)
        AND A
        JR Z,nomove
        CP (IX+2)
        JR C,nomove
        JR Z,nomove
        SUB (IX+2)
        DEC A
        CP (IX+5)
        JR C,nomove
        INC (IX+5)
        JR scrup

nomove_ XOR A
        LD (IX+5),A
nomove  POP IX
        HALT 
        JR disr

ScrUp   LD A,(IX+3)
        AND A
        JR Z,nomove
        LD A,(IX+5)
        AND A
        JR Z,nomove
        DEC (IX+5)
scrup   POP IX
        HALT 
        CALL DrwScroll
disr    CALL DIRSCN
sdir    LD A,(DIRECT)
        AND #10
        JP Z,Arrow
        JP Scroll

DrwScroll
        PUSH IX
        LD HL,(ScrAdr)
        CPHL0
        JR Z,PopIX2

        PUSH HL
        POP IX

        LD HL,HOTKEYF
        LD (HotAdr),HL
        LD A,(IX+3)
        SUB (IX+2)
        JR C,nks1
        CP (IX+5)
        JR NC,oks1
        JR nks2
nks1    XOR A
nks2    LD (IX+5),A
oks1    LD H,(IX+7)
        LD L,(IX+6)
        CPHL0
        CALL NZ,JPHL

        LD D,(IX+0)
        LD E,(IX+1)
        CALL RealWin
        LD (RealScr),DE

        DEC E
        CALL RASCH
        PUSH DE
        LD DE,SUP
        LD BC,#0108
        CALL Sprite
        POP DE

        LD A,(IX+4)
        ADD A,E
        LD E,A
        INC E
        CALL RASCH

        LD DE,SDN
        LD BC,#0108
        CALL Sprite

        CALL Schet

        PUSH BC
        PUSH DE
        PUSH AF

        LD (SA),A
        AND A
        JR Z,Lp18_
        LD B,A
        CALL PixHL
Lp18    LD (HL),%01000001
        CALL INC_Y
        DJNZ Lp18

Lp18_   POP AF
        POP DE
        POP BC

        ADD A,E
        SUB B
        LD E,A

        CALL PixHL

        EX AF,AF'
        LD B,A
        LD A,(SA)
        ADD A,B
        LD (SA),A

        LD (HL),%01011101
        CALL INC_Y
        DEC B
        JR Z,Lp19_
        DEC B
        JR Z,Lp19__

Lp19    LD (HL),%01010101
        CALL INC_Y
        DJNZ Lp19
Lp19__  LD (HL),%01011101
        CALL INC_Y

Lp19_   LD A,(IX+4)
        Amul8
        SUB 0
SA      EQU $-1
        JR C,Lp20_
        AND A
        JR Z,Lp20_
        LD B,A

Lp20    LD (HL),%01000001
        CALL INC_Y
        DJNZ Lp20

Lp20_   LD H,(IX+9)
        LD L,(IX+8)
        CALL JPHL
PopIX3  POP IX
        RET 

Menshe  HL0
        LD (STP),HL
        XOR A
        LD (HGT),A
        LD A,(IX+4)
        Amul8
        EX AF,AF'
        LD DE,(RealScr)
        EX DE,HL
        CALL HLmul8
        EX DE,HL
        XOR A
        LD B,A
        RET 

Schet   LD A,(IX+3)
        CP (IX+2)
        JR C,Menshe

        XOR A
        LD H,A
        LD D,A
        LD A,(IX+3)
        SUB (IX+2)
        PUSH AF
        PUSH AF
        LD L,A
        CALL MUL256
        LD A,(IX+4)
        LD D,0
        LD E,A
        CALL DIV
        INC H

        EX DE,HL

        LD H,0
        LD A,(IX+4)
        Amul8
        LD L,A
        CALL MUL256
        CALL DIV

        LD A,L
        LD (HGT),A
        EX AF,AF'

; x, y, за раз эл-ов, всего эл-ов, высота, позиция

        XOR A
        LD D,A
        LD H,A
        LD E,L
        LD A,(IX+4)
        Amul8
        LD L,A
        AND A
        SBC HL,DE
        CALL MUL256

        POP AF
        LD D,0
        LD E,A
        CALL DIV
        LD (STP),HL
        EX DE,HL
        LD B,0
        LD C,(IX+5)
        CALL MUL16
        CALL DIV256
        LD A,L

        LD DE,0
RealScr EQU $-2
        EX DE,HL
        CALL HLmul8
        EX DE,HL

        POP AF
        CP (IX+5)
        JR NZ,zak

        EX AF,AF'
        LD B,A
        EX AF,AF'

        LD A,(IX+4)
        Amul8
        RET 

zak     LD A,L
        LD B,0
        RET 

;+-----------------------+
;|      HL = HL/DE       |
;+-----------------------+

DIV     LD      A,D
        OR      E
        JR      Z,ZERO
        PUSH    DE
        PUSH    BC
        LD      A,1
DIV_0   PUSH    HL
        SBC     HL,DE
        JP      C,HL0_
        SBC     HL,DE
        JP      C,DIV_1
DIV_01  INC     A
        SLA     E
        RL      D
        POP     HL
        JP      DIV_0
DIV_1   POP     HL
        LD      BC,0
DIV_2   AND     A
        JP      NZ,DIV_3
        LD      H,B
        LD      L,C
        POP     BC
        POP     DE
        RET 
DIV_3   SBC     HL,DE
        JP      NC,DIV_4
        ADD     HL,DE
DIV_4   CCF 
        RL      C
        RL      B
        SRL     D
        RR      E
        DEC     A
        JP      DIV_2
HL0_    CP      1
        JP      NZ,DIV_01
        POP     HL
        POP     BC
        POP     DE
ZERO    LD      HL,0
        RET 

;+-----------------------+
;|     HL = DE*BC        |
;+-----------------------+

MUL16   LD      HL,0
MUL16_2 LD      A,B
        OR      C
        RET     Z
        SRL     B
        RR      C
        JP      NC,MUL16_0
        ADD     HL,DE
MUL16_0 SLA     E
        RL      D
        JP      MUL16_2

MUL256  LD B,8
mul256  ADD HL,HL
        DJNZ mul256
        RET 

DIV256  LD B,8
div256  RR H
        RR L
        DJNZ div256
        RET 

SUP     DB %00000000
        DB %01111111
        DB %01110111
        DB %01101011
        DB %01011101
        DB %01111111
        DB %01111111
        DB %01000001

SDN     DB %01000001
        DB %01111111
        DB %01111111
        DB %01011101
        DB %01101011
        DB %01110111
        DB %01111111
        DB %00000000

