XNEU	= $E0
YNEU	= $E1
XALT	= $E2
YALT	= $E3
ZAEHLER	= $E4
DELTAX	= $E5
DELTAY	= $E6
XFLAG	= $E7
YFLAG	= $E8
HILFA	= $E9
HILFB	= $EB
HILFT	= $ED

MODE	= $EF
XA	= $F0
YA	= $F1
XE	= $F2
YE	= $F3
XAP	= $F4
YAP	= $F5
XEP	= $F6
YEP	= $F7
COL	= $F8
LUM	= $F9
X1	= $FA
Y1	= $FB
X2	= $FC
Y2	= $FD
ADR1	= $80

	.MACRO ADD	; A,B : B=A+B
	CLC
	LDA %2
	ADC %1
	STA %2
	LDA %2+1
	ADC %1+1
	STA %2+1
	.ENDM

	.MACRO SUB	; A,B,C: C=A-B
	SEC
	LDA %1
	SBC %2
	STA %3
	LDA %1+1
	SBC #0
	STA %3+1
	.ENDM


	.MACRO NEG	; negate A
	EOR #$FF
	CLC
	ADC #1
	.ENDM

.BANK

	* = $7000

	JMP MAIN

DRAWTO	STX XNEU
	STY YNEU
	LDX XALT
	LDY YALT
	JSR PLOT
	LDA #0
	STA 77
	STA XFLAG
	STA YFLAG
	LDX XNEU
	LDY YNEU
	CPX XALT
	BNE DR1
	CPY YALT
	BNE DR1
	RTS

DR1	CPX XALT
	BCC DRX
	INC XFLAG
	TXA
	SBC XALT
	JMP DR2

DRX	DEC XFLAG
	SEC
	LDA XALT
	SBC XNEU

DR2	STA DELTAX
	CPY YALT
	BCC DRY
	INC YFLAG
	TYA
	SBC YALT
	JMP DR3

DRY	DEC YFLAG
	SEC
	LDA YALT
	SBC YNEU

DR3	STA DELTAY
	LDA XALT
	STA XNEU
	LDA YALT
	STA YNEU
	LDA DELTAX
	CMP DELTAY
	BCC DRYSTEP

	LDA DELTAY
	ASL
	STA HILFA
	LDA #0
	ROL
	STA HILFA+1

	SUB HILFA,DELTAX,HILFT
	SUB HILFT,DELTAX,HILFB
	LDA DELTAX
	STA ZAEHLER

DXSCHL	CLC
	LDA XNEU
	ADC XFLAG
	STA XNEU
	LDA HILFT+1
	BPL DRX5
	ADD HILFA,HILFT
	JMP DRX4

DRX5	ADD HILFB,HILFT
	CLC
	LDA YNEU
	ADC YFLAG
	STA YNEU

DRX4	LDX XNEU
	LDY YNEU
	JSR PLOT
	DEC ZAEHLER
	BNE DXSCHL
	JMP DREND

DRYSTEP	LDA DELTAX
	ASL
	STA HILFA
	LDA #0
	ROL
	STA HILFA+1

	SUB HILFA,DELTAY,HILFT
	SUB HILFT,DELTAY,HILFB

	LDA DELTAY
	STA ZAEHLER

DYSCHL	CLC
	LDA YNEU
	ADC YFLAG
	STA YNEU
	LDA HILFT+1
	BPL DRY5
	ADD HILFA,HILFT
	JMP DRY4

DRY5	ADD HILFB,HILFT
	CLC
	LDA XNEU
	ADC XFLAG
	STA XNEU
DRY4	LDX XNEU
	LDY YNEU
	JSR PLOT
	DEC ZAEHLER
	BNE DYSCHL

DREND	LDX XNEU
	LDY YNEU
	STX XALT
	STY YALT
	RTS

PLOT	LDA LTAB,Y
	STA ADR1
	LDA HTAB,Y
	STA ADR1+1
	TXA
	LSR
	TAY
	BCS PLOT2
	LDX COL
	LDA (ADR1),Y
	AND #$0F
	ORA COLTB,X
	STA (ADR1),Y
	CLC
	LDA ADR1
	ADC #40
	STA ADR1
	BCC PLW1
	INC ADR1+1
PLW1	LDX LUM
	LDA (ADR1),Y
	AND #$0F
	ORA COLTB,X
	STA (ADR1),Y
	RTS

COLTB	.BYTE $00,$10,$20,$30
	.BYTE $40,$50,$60,$70
	.BYTE $80,$90,$A0,$B0
	.BYTE $C0,$D0,$E0,$F0

PLOT2	LDA (ADR1),Y
	AND #$F0
	ORA COL
	STA (ADR1),Y
	CLC
	LDA ADR1
	ADC #40
	STA ADR1
	BCC PLW2
	INC ADR1+1
PLW2	LDA (ADR1),Y
	AND #$F0
	ORA LUM
	STA (ADR1),Y
	RTS

TABGEN	LDA #$F0
	STA LTAB
	LDA #$86
	STA HTAB
	LDY #0
LP	CLC
	LDA LTAB,Y
	ADC #80
	STA LTAB+1,Y
	LDA HTAB,Y
	ADC #0
	STA HTAB+1,Y
	INY
	CPY #79
	BNE LP
	RTS

INTINI	LDA #6
	LDX #>VBI
	LDY #<VBI
	JSR $E45C
	LDA #7
	STA XFINE
	LDA #<TXT
	STA LAUF
	LDA #>TXT
	STA LAUF+1
	LDA #<DLI
	STA 512
	LDA #>DLI
	STA 513
	LDA #$C0
	STA 54286
	RTS

VBI	LDA #$C0
	STA MODE
	LDA #0
	STA $D01B
	LDA #<DL
	STA $D402
	LDA #>DL
	STA $D403
	LDA #$23
	STA $D400
	LDA #0
	STA $D01A
	LDA #<DLI1
	STA 512
	LDA #>DLI1
	STA 513
	JSR LAUFX
	INC COUNTER
	LDA COUNTER
	ASL
	AND #$F0
	STA CCNT
	JMP $E462

DLI1	PHA
	TXA
	PHA
	LDX #$F
DLI1LP	SEC
	LDA COLTAB2,X
	SBC CCNT
	STA $D40A
	STA $D016
	DEX
	BPL DLI1LP
	LDA #<DLI
	STA 512
	LDA #>DLI
	STA 513
	LDA #$C0
	STA $D01B
	LDA #$22
	STA $D400
	PLA
	TAX
	PLA
	RTI

COLTAB2	.BYTE $00,$02,$14,$16
	.BYTE $28,$2A,$3C,$3E
	.BYTE $4E,$4C,$5A,$58
	.BYTE $66,$64,$72,$70

DLI	PHA
	LDA MODE
	EOR #$80
	STA MODE
	STA $D40A
	STA $D01B
	PLA
	RTI

CLS	LDA #$86
	STA CLSLP+2
	LDA #0
	LDX #0
CLSLP	STA $8600,X
	INX
	BNE CLSLP
	INC CLSLP+2
	LDY CLSLP+2
	CPY #$A0
	BNE CLSLP
	RTS

INIT	LDA #0
	STA XA
	STA YA
	STA XE
	STA YE
	STA COL
	STA LUM
	RTS

SETRND	JSR RND
	STA XAP
	JSR RND
	STA YAP
	JSR RND
	STA XEP
	JSR RND
	STA YEP
	RTS

RND	LDA $D20A
	AND #3
	BEQ RND
	SEC
	SBC #2
	RTS

CALC	CLC
	LDA XA
	ADC XAP
	JSR TEST
	STA XA

	CLC
	LDA YA
	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,$8F
	.BYTE $8F,$8F,$8F,$8F,$8F,$8F
	.BYTE $8F,$8F,$8F,$8F,$8F,$8F
	.BYTE $8F,$8F,$8F,$8F,$F,$41
	.WORD DL

MAIN	JSR TABGEN
	JSR CLS
	JSR INTINI
	JSR INIT
	JSR SETRND
LOOP	JSR LINES1
	JSR LINES2
	LDA $D20A
	AND #$1F
	BNE NNEW
	JSR SETRND
NNEW	LDA 53279
	CMP #3
	BEQ ENDE
	CMP #5
	BEQ MAIN
	LDA $D20A
	BNE GOON
	LDA $D20A
	AND #15
	BEQ MAIN
GOON	INC LUM
	LDA LUM
	AND #15
	STA LUM
	BNE LW1
	INC COL
	LDA COL
	AND #15
	STA COL
LW1	JSR CALC
	LDX #$14
	LDY #0
WAIT	DEY
	BNE WAIT
	DEX
	BNE WAIT
	JMP LOOP

ENDE	LDA #64
	STA 54286
	LDA #6
	LDX #$E4
	LDY #$5F
	JSR $E45C
	RTS

LINES1	LDA XA
	STA X1
	LDA XE
	STA X2
	LDA YA
	STA Y1
	LDA YE
	STA Y2
	JSR LINE

	LDA YA
	NEG
	STA Y1
	LDA YE
	NEG
	STA Y2
	JSR LINE

	LDA XA
	NEG
	STA X1
	LDA XE
	NEG
	STA X2
	JSR LINE

	LDA YA
	STA Y1
	LDA YE
	STA Y2
	JSR LINE
	RTS

LINES2	LDA YA
	STA X1
	LDA YE
	STA X2
	LDA XA
	STA Y1
	LDA XE
	STA Y2
	JSR LINE

	LDA XA
	NEG
	STA Y1
	LDA XE
	NEG
	STA Y2
	JSR LINE

	LDA YA
	NEG
	STA X1
	LDA YE
	NEG
	STA X2
	JSR LINE

	LDA XA
	STA Y1
	LDA XE
	STA Y2
	JSR LINE
	RTS

LAUFX	DEC XFINE
	BMI ADPL
	LDA XFINE
	STA $D404
	RTS
ADPL	LDA #7
	STA XFINE
	STA $D404
	INC LAUF
	BNE ADPL1
	INC LAUF+1
ADPL1	LDA LAUF
	CMP #<ETXT
	BNE NT1
	LDA LAUF+1
	CMP #>ETXT
	BNE NT1
	LDA #<TXT
	STA LAUF
	LDA #>TXT
	STA LAUF+1
NT1	RTS

TXT	.SBYTE "                      "
	.SBYTE "    PSYCHEDELIC DREAMS"
	.SBYTE "                      "
	.SBYTE "   COPYRIGHT (C) 1989 "
	.SBYTE "BY HIASSOFT      "
	.SBYTE "                    "
	.SBYTE " PRESS SELECT TO "
	.SBYTE "RESTART THE DEMO "
	.SBYTE "OR OPTION TO QUIT.    "
	.SBYTE "           "
ETXT	.SBYTE "                    "
	.SBYTE "          "

XFINE	.BYTE 0
COUNTER	.BYTE 0
CCNT	.BYTE 0

LTAB	= *
HTAB	= *+80

	.BANK

	* = $2E0
	.WORD $7000
