1 REM Ins{nd av Bengt Holgersson <559>    1986-03-13 22.58.30
        ORG 0F800H

        JP      FGCLR    ;RADERA OCH S[TT SK[RM I GRAFMODE
        JP      FGCTL    ;1=SETDOT 0=CLRDOT
        JP      FGPNT    ;RITA PUNKT
        JP      FGLINE   ;RITA LINJE
        JP      FGINV    ;INVERTERA GRAFIK
        JP      SETRAD   ;ANGE RADIE F\R CIRKEL
        JP      CIRCLE   ;RITA CIRKEL

FGCLR:  PUSH    IX
        LD      IX,884
        LD      B,24
GRLOP:  PUSH    BC
        LD      L,(IX+0)
        LD      H,(IX+1)
        LD      (HL),23  ;START GRAFIK
        INC     HL
        LD      (HL),32
        LD      D,H
        LD      E,L
        INC     DE
        LD      A,(590)
        DEC     A
        LD      B,0
        LD      C,A
        LDIR
        INC     IX
        INC     IX
        POP     BC
        DJNZ    GRLOP
        LD      HL,0     ;ALLTID OK
        POP     IX
        RET

FGCTL:  LD      A,D
        OR      E
        LD      HL,0B600H  ;SETDOT  NOP+OR (HL)
        JR      NZ,CHNGE
        LD      HL,0A62FH  ;CLRDOT  CPL+AND (HL)
CHNGE:  LD      (SETCLR),HL
        LD      HL,0       ;ALLTID OK
        RET

FGPNT:  LD      (OLDCOR),DE
FGPONT: PUSH    DE       ;INHOPP FR]N FGLINE D=RAD E=KOLUMN
        LD      A,E
        CP      2
        JR      C,NODOT  ;UTANF\R SK[RMEN
        LD      B,0
        LD      H,B
        LD      C,D      ;RAD I C-REG
        LD      L,E      ;KOLUMN I L-REG
        LD      A,(022EEH)
        CP      124
        LD      A,E      ;KOLUMN I A-REG
        JR      Z,OLD
        CALL    022ECH   ;NYA CHECKSUMMOR
        JR      SETIT
OLD:    CALL    022EEH   ;GAMMAL CHECKSUMMA
SETIT:  JR      C,NODOT
SETCLR: NOP              ;<= SETDOT  CLRDOT= CPL
        OR      (HL)     ;<= SETDOT  CLRDOT= AND (HL)
        OR      32
        LD      (HL),A
        LD      HL,0
        JR      OKEY

NODOT:  LD      HL,0FFFFH
OKEY:   POP     DE
        RET

FGLINE: CALL    FGPONT          ;S[TT SISTA PUNKTEN
        RET     C               ;RETUR OM UTANF\R SK[RMEN
        LD      HL,(OLDCOR)     ;H[MTA GAMMAL KOORDINAT
        EX      DE,HL           ;HL=NY  DE=GAMMAL
        LD      (OLDCOR),HL     ;LAGRA NY KOORDINAT
        LD      A,1             ;S[TT COLSTP OCH ROWSTP TILL 1
        LD      (COLSTP),A
        LD      (ROWSTP),A
        LD      A,H             ;BER[KNA ROWDIF  NY-GAMMAL
        SUB     D
        LD      (ROWDIF),A
        JR      NC,COLUMN
        NEG                     ;BYT TECKEN
        LD      (ROWDIF),A
        LD      A,-1            ;S[TT ROWSTP TILL -1
        LD      (ROWSTP),A
COLUMN: LD      A,L             ;BER[KNA COLDIF  NY-GAMMAL
        SUB     E
        LD      (COLDIF),A
        JR      NC,ZCHECK
        NEG                     ;BYT TECKEN
        LD      (COLDIF),A
        LD      A,-1            ;S[TT COLSTP TILL -1
        LD      (COLSTP),A
ZCHECK: LD      A,(COLDIF)      ;KOLLA OM COLDIF = 0
        CP      0
        JR      NZ,SIZCHK
        LD      A,(ROWDIF)      ;KOLLA OM ROWDIF = 0
        CP      0
        RET     Z               ;RETUR OM B]DA = 0
SIZCHK: LD      A,(COLDIF)      ;KOLLA OM COLDIF ELLER ROWDIF [R ST\RST
        LD      B,A
        LD      A,(ROWDIF)
        SUB     B
        JR      NC,LINE2
LINE1:  LD      A,(COLDIF)      ;COLDIF ST\RST
        SRL     A               ;/2
        LD      B,A
        LD      A,(ROWDIF)
        SUB     B
        LD      (TEST),A        ;TEST=ROWDIF-COLDIF/2
        LD      A,0
        SBC     A,0
        LD      (FLAG),A        ;FLAG=TECKEN
        LD      A,(COLDIF)
        LD      B,A             ;TOTALT ANTAL PUNKTER
NEXT1:  PUSH    BC
        CALL    FGPONT
        LD      A,(FLAG)        ;KOLLA TECKEN P] TEST
        AND     80H
        JR      NZ,SINGL1
DOUBL1: LD      A,(ROWDIF)      ;TEST POSITIV
        LD      B,A
        LD      A,(COLDIF)
        SUB     B               ;SINGLE
        LD      B,A
        LD      A,(TEST)
        SUB     B
        LD      (TEST),A        ;TEST=TEST-SINGLE
        LD      A,(FLAG)
        SBC     A,0
        LD      (FLAG),A        ;FLAG=TECKEN
        LD      A,(ROWSTP)
        ADD     A,D
        LD      D,A
        JR      CONT1

SINGL1: LD      A,(ROWDIF)      ;TEST NEGATIV
        LD      B,A
        LD      A,(TEST)
        ADD     A,B
        LD      (TEST),A        ;TEST=TEST+DOUBLE
        LD      A,(FLAG)
        ADC     A,0
        LD      (FLAG),A        ;FLAG=TECKEN
CONT1:  LD      A,(COLSTP)
        ADD     A,E
        LD      E,A
        POP     BC
        DJNZ    NEXT1
        RET

LINE2:  LD      A,(ROWDIF)      ;ROWDIF ST\RST
        SRL     A
        LD      B,A
        LD      A,(COLDIF)
        SUB     B
        LD      (TEST),A        ;TEST=COLDIF-ROWDIF/2
        LD      A,0
        SBC     A,0
        LD      (FLAG),A        ;FLAG=TECKEN
        LD      A,(ROWDIF)
        LD      B,A             ;TOTALT ANTAL PUNKTER
NEXT2:  PUSH    BC
        CALL    FGPONT
        LD      A,(FLAG)        ;KOLLA TECKEN P] TEST
        AND     80H
        JR      NZ,SINGL2
DOUBL2: LD      A,(COLDIF)      ;TEST POSITIV
        LD      B,A
        LD      A,(ROWDIF)
        SUB     B               ;SINGLE
        LD      B,A
        LD      A,(TEST)
        SUB     B
        LD      (TEST),A        ;TEST=TEST-SINGLE
        LD      A,(FLAG)
        SBC     A,0
        LD      (FLAG),A        ;FLAG=TECKEN
        LD      A,(COLSTP)
        ADD     A,E
        LD      E,A
        JR      CONT2

SINGL2: LD      A,(COLDIF)      ;TEST NEGATIV
        LD      B,A
        LD      A,(TEST)
        ADD     A,B
        LD      (TEST),A        ;TEST=TEST+DOUBLE
        LD      A,(FLAG)
        ADC     A,0
        LD      (FLAG),A        ;FLAG=TECKEN
CONT2   LD      A,(ROWSTP)
        ADD     A,D
        LD      D,A
        POP     BC
        DJNZ    NEXT2
        RET

FGINV:  PUSH    IX
        LD      IX,884
        LD      B,24
INVLOP: PUSH    BC
        LD      L,(IX+0)
        LD      H,(IX+1)
        LD      A,(590)
        LD      B,A
INLOP1: LD      A,(HL)
        BIT     5,A
        JR      Z,NOGRAF
        XOR     95
        LD      (HL),A
NOGRAF: INC     HL
        DJNZ    INLOP1
        INC     IX
        INC     IX
        POP     BC
        DJNZ    INVLOP
        LD      HL,0
        POP     IX
        RET

OLDCOR: DEFW    0
COLDIF: DEFB    0
ROWDIF: DEFB    0
COLSTP: DEFB    0
ROWSTP: DEFB    0
TEST:   DEFB    0
FLAG:   DEFB    0

SETRAD: LD A,E
        LD (RADIE),A
        LD HL,0
        RET

CIRCLE: LD (XP),DE
        LD A,(RADIE)
        LD (XDOT1),A     ;XDOT1 = radie
        XOR A
        LD (YDOT1),A     ;YDOT1 = 0
        LD L,A
        LD H,A
        LD (PHI),HL      ;PHI = 0

CIRC1:  LD A,(YDOT1)
        LD B,0
        LD C,A
        LD D,A
        LD HL,(PHI)
        ADD HL,BC
        ADD HL,BC
        INC HL
        LD (PHIY),HL     ;PHIY = PHI + 2*YDOT1 + 1
        LD A,(XDOT1)
        LD C,A
        AND A
        SBC HL,BC
        AND A
        SBC HL,BC
        INC HL
        LD (PHIXY),HL    ;PHIXY = PHIY - 2*XDOT1 + 1
        LD B,2
CIRC2:  CALL XADD
        CALL YADD
        CALL XSUB
        CALL YADD
        CALL XADD
        CALL YSUB
        CALL XSUB
        CALL YSUB
        LD A,D
        LD D,C
        LD C,A
        DJNZ CIRC2

        INC D
        LD A,D
        LD (YDOT1),A     ;\ka YDOT1

        LD HL,(PHIY)
        LD (PHI),HL      ;PHI = PHIY
        CALL ABSHL
        LD (T2),HL       ;T2 = ABS( PHIY )
        LD HL,(PHIXY)
        CALL ABSHL
        LD (T1),HL       ;T1 = ABS( PHIXY )

        LD DE,(T2)       ;J{mf|r T1 och T2
        LD A,H
        CP D
        JR C,CIRC5
        LD A,L
        CP E
        JR NC,CIRC6

CIRC5:  LD HL,(PHIXY)    ;Om T1 < T2 minska XDOT1
        LD (PHI),HL
        LD HL,XDOT1
        DEC (HL)

CIRC6:  LD HL,XDOT1      ;J{mf|r XDOT1 och YDOT1
        LD A,(HL)
        INC HL
        CP (HL)

        JP NC,CIRC1      ;Forts{tt om YDOT1 < XDOT1
        RET

ABSHL:  BIT 7,H          ;Om HL {r negativt byt tecken
        RET Z
        LD A,H
        CPL
        LD H,A
        LD A,L
        CPL
        LD L,A
        INC HL
        RET

XADD:   LD A,(XP)
        ADD A,C
        LD (XDOT),A
        RET

XSUB:   LD A,(XP)
        SUB C
        LD (XDOT),A
        RET

YADD:   LD A,(YP)
        ADD A,D
        LD (YDOT),A
        JR CIRDOT

YSUB:   LD A,(YP)
        SUB D
        LD (YDOT),A
CIRDOT: PUSH DE
        PUSH BC
        LD DE,(XDOT)
        CALL FGPONT
        POP BC
        POP DE
        RET

RADIE   DEFB 0           ;Inparameter radie
XP      DEFB 0           ;Inparameter centrum i X-led
YP      DEFB 0           ;Inparameter centrum i Y-led

XDOT    DEFB 0
YDOT    DEFB 0
XDOT1   DEFB 0
YDOT1   DEFB 0
PHI     DEFW 0
PHIY    DEFW 0
PHIXY   DEFW 0
T1      DEFW 0
T2      DEFW 0

        END
