        MAIN "",134

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

        PUSH HL
        POP IX
        LD DE,(RealScr)

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

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

        LD HL,(ARW_XC)
        AND A
        SBC HL,DE
        JR 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  CALL Schet
        EX AF,AF'
        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
        JR sdir

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)
        SUB (IX+2)
        LD (IX+5),A
        JR sdirr

PopIX2  POP IX
        RET 

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

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

DrwScroll
        PUSH IX
        LD L,(IX+18)
        LD H,(IX+19)
        CPHL0
        JR Z,PopIX2

        PUSH HL
        POP IX

        LD H,(IX+7)
        LD L,(IX+6)
        CALL JPHL

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

drwscroll
        PUSH 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

        POP DE
        CALL RASCH

        LD A,(IX+4)
        Amul8
        LD B,A
Lp18    LD (HL),%01000001
        CALL INC_Y
        DJNZ Lp18

        CALL Schet
        CALL PixHL

        EX AF,AF'
        LD B,A

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

        LD H,(IX+9)
        LD L,(IX+8)
        CPHL0
        JR Z,PopIX3
        CALL JPHL
PopIX3  POP IX
        RET 

Schet   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
        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
        EX DE,HL
        LD B,0
        LD C,(IX+5)
        CALL MUL16
        CALL DIV256
        LD A,L

ZA      LD DE,0
RealScr EQU $-2
        DUP 3
        SLA D
        SLA E
        EDUP 

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

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

        LD A,(IX+4)
        Amul8
        ADD A,E
        SUB B
        LD E,A
        RET 

zak     LD A,L
        ADD A,E
        LD E,A
        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 

;+-----------------------+
;|     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 

        DISPLAY "ScrBar:           ",$-Scroll
