XWIDTH  EQU     436
YDEPTH  EQU     332
YDEPN   EQU     YDEPTH-1
XPOS    EQU     50
YPOS    EQU     -92

MASK    EQU     31

GRPH_LP BSR     CORD_DE
        LEA     GCUR1,A0
        MOVEM.L D0-D6,-(A7)
        MOVE.W  (A0)+,D2
        MOVE.W  (A0)+,D3
        MOVE.W  (A0)+,D0
        MOVE.W  (A0),D1
        LEA     WKSPAC+60,A1
        CMP.W   #1,(A1)
        BEQ     SK_LINE
        BSR     DOLINE
SK_LINE MOVEM.L (A7)+,D0-D6
        LEA     GCUR1,A0
        LEA     GCUR2,A1
        MOVE.L  (A1),(A0)
RETURN2 BRA     GRPH_LP

CORD_DE LEA     STAT_BY,A0
        MOVE.B  #0,(A0)
GR_LOOP BSR     RD_SER
        BSR     GET_BYT
        MOVE.B  D6,D1
        MOVEQ   #0,D6
        MOVE.B  D1,D6
        CMP.B   #32,D6
        BLT     CTR_DEC
        BTST    #6,D6           ; Is it a LOW byte?
        BEQ     HI_DEC          ; if yes then decode it
        BTST    #5,D6
        BNE     LY_DEC
        
LOW_X   AND.B   #MASK,D6
        LEA     LOWX,A0
        MOVE.B  D6,(A0)
        BSR     INTEGRA
        RTS
        
HI_DEC  AND.B   #MASK,D6
        LEA     STAT_BY,A0
        BTST    #2,STAT_BY
        BNE     HX
        BSET    #3,(A0)
        LEA     HIGHY,A1
        MOVE.B  D6,(A1) 
        BRA     GR_LOOP
HX      BSET    #1,(A0)
        LEA     HIGHX,A1
        MOVE.B  D6,(A1)
        BRA     GR_LOOP
        
LY_DEC  AND.B   #MASK,D6
        LEA     LOWY,A0
        MOVE.B  D6,(A0)
        LEA     STAT_BY,A0
        BSET    #2,(A0)
        BRA     GR_LOOP
        
INTEGRA MOVEM.L D0-D4/A0-A2,-(A7)
        LEA     GCUR2,A0
        MOVE.L  (A0),D1
        MOVEQ   #0,D0
        MOVE.B  STAT_BY,D0
        TST.B   D0
        BEQ     LOX
        CMP.B   #6,D0
        BEQ     HIX
        CMP.B   #4,D0
        BEQ     LOY
        CMP.B   #8,D0
        BEQ     HIY
        
        MOVEQ   #0,D1
        MOVE.B  HIGHX,D1
        LSL.L   #5,D1
        MOVEQ   #0,D2
        MOVE.B  LOWX,D2
        ADD.L   D2,D1
        SWAP    D1
        MOVE.B  HIGHY,D2
        ADD.L   D2,D1
        LSL.W   #5,D1
        MOVE.B  LOWY,D2
        ADD.L   D2,D1
        
END_INT MOVE.L  D1,(A0)
        MOVEM.L (A7)+,D0-D4/A0-A2
        RTS
        
LOY     MOVEQ   #0,D2
        MOVE.B  LOWY,D2
        AND.L   #$FFFFFFE0,D1
        ADD.L   D2,D1
        BRA     LOX
        
LOX     MOVEQ   #0,D2
        MOVE.B  LOWX,D2
        SWAP    D2
        AND.L   #$FFE0FFFF,D1
        ADD.L   D2,D1
        BRA     END_INT
        
HIY     MOVEQ   #0,D2
        MOVE.B  HIGHY,D2
        LSL.L   #5,D2
        AND.L   #$FFFFFC1F,D1
        ADD.L   D2,D1
        BRA     LOX
        
HIX     MOVEQ   #0,D2
        MOVE.B  HIGHX,D2
        SWAP    D2
        LSL.L   #5,D2
        AND.L   #$FC1FFFFF,D1
        ADD.L   D2,D1
        BRA     LOY
        
DOLINE  BSR     DOSCALE
        EXG     D0,D2
        EXG     D1,D3
DOLIN2  BSR     DOSCALE
        MOVE.L  #$20000+[YPOS*$80]+[XPOS/8*2],A0
        MOVEQ   #1,D4
        MOVE.L  #$10000,D5
        SUB.W   D0,D2
        BPL.S   X2GEX1
        MOVE.L  #$0FFF0000,D5
        NEG.W   D2
X2GEX1  SUB.W   D1,D3
        BPL.S   Y2GEY1
        MOVE.W  #$0FFF,D4
        NEG.W   D3
Y2GEY1  CMP.W   D2,D3
        BLE.S   NOSWAP
        EXG     D2,D3
        EXG     D4,D5
NOSWAP  SWAP    D0
        MOVE.W  D1,D0
        MOVE.W  D2,D7
        MOVE.W  D2,D6
        LSR.W   #1,D6
NXTPT   AND.L   #$0FFF0FFF,D0
        MOVEM.L D0/D4,-(A7)
        MOVEQ   #0,D1
        MOVE.W  D0,D1
        CLR.W   D0
        SWAP    D0
        BSR.S   DOPT2
        MOVEM.L (A7)+,D0/D4
        ADD.L   D5,D0
        ADD.L   D3,D6
        CMP.W   D6,D2
        BGE.S   NORED
        SUB.W   D2,D6
        ADD.L   D4,D0
NORED   DBRA    D7,NXTPT
        RTS
DOPT2   MOVEQ   #7,D4
        LSL.L   D4,D1
        SUB.B   D0,D4
        LSR.W   #3,D0
        LSL.W   #1,D0
        ADD.L   D0,D1
        MOVE.L  D7,-(A7)
        MOVE.L  D1,D7
        ADD.L   A0,D7
        CMP.L   #$20180,D7
        BLT.S   NO_PLOT
        BSET    D4,1(A0,D1.L)
        BSET    D4,0(A0,D1.L)
NO_PLOT MOVE.L  (A7)+,D7
        RTS

DOSCALE MOVEQ   #10,D4
        MULU    #XWIDTH,D2
        LSR.L   D4,D2
        MULU    #YDEPTH,D3
        LSR.L   D4,D3
        SUB.W   #YDEPN,D3
        NEG.W   D3
        RTS

HIGHY   DCB.W   1,0
LOWY    DCB.W   1,0
HIGHX   DCB.W   1,0
LOWX    DCB.W   1,0
STAT_BY DCB.B   1,0
        ALIGN