; PAPERWEIGHT
; BY Curt Fox
;
RPAD	= $E000
PAD	= $2800
FS	= $2000
EOFS	= FS+690
MRSC	= FS+$0300
VT	= $2000
KEYS	= 764
CTR	= 19
BLINK	= $D401
WSTRTF	= 580
NMIEN	= 54286
AUDCTL	= $D208
SKCTL	= $D20F
FSIST	= 560
SETOB	= $E45C
PADASE	= 756
START	= 6
SELECT	= 5
DYMUM	= $5000
FSIVEC	= 512
VTNABL	= 53277
VDEL	= 53276
VTBASE	= 54279
CTRLSB	= 20
RED	= $30
GREEN	= $D2
AMBER	= $27
ORANGE	= $22
SPFC0	= 710
SCBAK	= 712
ATTRACT	= 77
CONSL	= 53279
COM	= $0342
BUFAD	= $0344
BUFL	= $0348
CUROSR	= 752
CHLUM	= 709
P0C	= 704
HP0C	= $D012
PUT	= $0B
DMA	= 559
P0X	= 53248
OSP0	= $D200
OSP1	= $D202
OSP2	= $D204
OSP3	= $D206
OSC0	= $D201
OSC1	= $D203
OSC2	= $D205
OSC3	= $D207
CIOV	= $E456
PI	= 31416
ROW	= $54
COL	= $55
XITOBV	= $E462
FSPSP	= $D20A
GRAF	= $D00C
HPF1C	= $D017
HPF2C	= $D018
HCBAK	= $D01A
PACTL	= $D302
SHOE	= $02
BOOT	= $09
SIZE	= $D008
;
	*= $2C00 	;After DOS
;
PW	LDA #$3C
	STA PACTL
	LDA #3 	;Trap
	STA BOOT 	;System Reset
;
	LDA # >SDV1
	STA SHOE+1
	LDA # <SDV1
	STA SHOE
;
SDV1	LDA # >PAD
	STA PADASE
	LDX #0
	STX AUDCTL
CT	LDA RPAD,X 	;Move
	STA PAD,X 	;ROM
	LDA RPAD+256,X 	;vector
	STA PAD+256,X 	;table
	LDA RPAD+512,X 	;to RAM
	STA PAD+512,X 	;to use
	LDA RPAD+768,X 	;as a
	STA PAD+768,X 	;"scratchpad"
	DEX 
	BNE CT
	LDA #GREEN 	;condition
	STA SPFC0 	;green
;
SDV	LDA #62
	STA DMA 	;Set
	LDA #$A1 	;preliminary
	STA OSC0 	;access or
	STA OSC1 	;"handshake"
	LDA #255 	;flags so
	STA OSP0 	;vector is
	LDA #254 	;locatable
	STA OSP1
	LDA #1 	;Kill
	STA CUROSR 	;cursor
;
	LDA FSIST 	;Initalize
	STA FSP+1 	;Flag
	STA FSP2+1 	;Sequence
	LDA FSIST+1 	;Processing
	STA FSP+2 	;Stack
	STA FSP2+2 	;Pointers
	LDA #$82 	;(FSPSP)
	LDX #6 	;and
FSP	STA DYMUM,X 	;their
	INX  	;stack
	CPX #26 	;(F-stack)
	BNE FSP
;
	LDA # >FSI
	STA FSIVEC+1
	LDA # <FSI
	STA FSIVEC 	;Compute
	LDA # >VT 	;main
	STA VTBASE 	;offsets
	LDA #3 	;from
	STA SKCTL 	;scratchpad
	STA ATTRACT
	LDA #0
	STA WSTRTF
;
	LDX # >M1 	;Print
	LDY # <M1 	;Paperweight
	LDA #M2-M1 	;by Curt Cox
	JSR PRINT
;
WAIT	LDA CONSL 	;Wait for
	CMP #6 	;[START]
	BNE WAIT
;
	LDA #20
	STA STI 	;Print
W2	LDX # >M2 	;Locating
	LDY # <M2
	LDA #M3-M2
	JSR PRINT
;
	LDA #240 	;Find next
	JSR FIND 	;flag
;
	LDX # >M3
	LDY # <M3 	;Print
	LDA #M4-M3 	;Locating
	JSR PRINT
;
	LDA #240 	;Find next
	JSR FIND 	;vector
	DEC STI 	;Find more
	BNE W2 	;if needed
;
	LDX # >M4
	LDY # <M4 	;Print
	LDA #M5-M4 	;found
	JSR PRINT 	;and
W4	LDA CONSL 	;wait
	CMP #6 	;for
	BNE W4 	;[START]
;
	LDX # >M5
	LDY # <M5 	;Sure?
	LDA #M6-M5
	JSR PRINT
;
	LDA #0
	STA KEYS 	;Sure.
W3	LDA KEYS
	CMP #43
	BNE W3
;
	LDA #AMBER
	STA SPFC0 	;Print
	LDX # >M6 	;ten seconds
	LDY # <M6 	;to safely
	LDA #M7-M6 	;abort
	JSR PRINT
;
	LDA # >ABORT 	;Abort on
	STA SHOE+1 	;System
	LDA # <ABORT 	;Reset
	STA SHOE
;
	LDA #11 	;Print 10
	STA STI 	; 9
CDW	LDA #190 	; 8
	JSR FIND 	; 7
	LDX # >M7 	; 6
	LDY # <M7 	; 5
	LDA #M8-M7 	; 4
	JSR PRINT 	; 3
	DEC STI 	; 2
	BNE CDW 	; 1 + 0
;
	LDA #1
	STA DAMAGED
	LDX # >M8 	;Destruct
	LDY # <M8 	;Proceeding
	LDA #A1-M8
	JSR PRINT
;
	LDA #192
	STA NMIEN 	;Set
	LDA #$0F 	;final
	STA SBI 	;flags
SB	JSR STAT 	;from
	DEC SBI 	;f-stack
	LDA SBI
	STA SLVL+1
	CMP #4
	BNE SB
;
	LDA #RED
	STA SPFC0
	LDX # >OBI 	;Set
	LDY # <OBI 	;A,X,and y
	LDA #6 	;and jump
	JSR SETOB 	;to SDV
;
	LDA #$0F 	;Successful
	STA SLVL+1 	;Jump?
;
REP	JSR STAT 	;If not
	LDA DEL 	;try
	BMI QE2 	;reset
	JMP REP 	;flags
;
QE2	LDX #$E4 	;and try
	LDY #$5F 	;secondary
	LDA #6 	;vector
	JSR SETOB
;
	LDX #$1C 	;The rest of
FL	JSR FOB 	;the routines
	DEX  	;are used
	BNE FL 	;in flag
	LDA # <RQE2 	;and vector
	STA SHOE 	;locating and
	LDA # >RQE2 	;determining
	STA SHOE+1 	;proper
RQE2	LDA #0 	;flag
	LDX #5 	;values
CPL	STA GRAF,X
	DEX  	;rough
	BNE CPL 	;explanations
	BNE FL 	;will be
	LDA #63 	;given but
	STA DMA 	;generally
	LDX #0 	;a detailed
	STX VTNABL 	;explanation
POOL	LDA #$4F 	;would
	STA FS,X 	;require
	STA FS+255,X 	;comments
	STA FS+510,X 	;of an
	INX  	;obscene
	INX  	;length.
	LDA # >MRSC
	STA FS,X 	;For
	STA FS+255,X 	;further
	STA FS+510,X 	;information
	INX  	;see the revised
	CPX #$FF 	;1986 ATARI
	BNE POOL 	;technical
	LDA #$41 	;users notes
	STA EOFS 	;destruct
	LDA #0 	;appendix
	STA OSP3 	;(ATUNDA)
	STA EOFS+1 	;section
	STA FSIST 	;given
	STA SPFC0
	LDA # >FS
	STA EOFS+2
	STA FSIST+1
;
	LDA #3
	STA OSP0 	;Put
	LDA #2 	;preliminary
	STA OSP1 	;values in
	LDA #1 	;acess
	STA OSP2 	;flags
	LDA #$8F 	;to verify
	STA OSC0 	;vector
	LDA #$88 	;location
	STA OSC1
	LDA #$86
	STA OSC2
	LDA #$83
	STA OSC3
;
OL	LDX #0
L2	LDA FSPSP 	;Resort
	INX  	;vector
	STA FS,X 	;table
	STA FS+255,X 	;according
	STA FS+510,X 	;to flag
	INX  	;value
	INX 
	CPX #$FF
	BNE L2
;
	LDX #$FF
L3	LDA FSPSP 	;Screen
	STA MRSC,X 	;out
	LDA FSPSP 	;unnecessary
	STA MRSC+256,X 	;scratchpad
	DEX  	;and
	BNE L3 	;f-stack
	STX ATTRACT 	;entries
	JMP OL 	;to save
FSI	PHA  	;time.
	TXA 
	PHA  	;Pass
	LDX PN 	;results
	LDA FSPSP 	;to 6502
	STA P0X-1,X 	;stack
	DEX  	;to FILO
	BNE XITD 	;reverse
	LDX #8
XITD	STX PN
	PLA 
	TAX 
	PLA 
	RTI 
;
OBI	LDA #RED
	STA HPF2C 	;Load
	LDA #10 	;flag
	STA HPF1C 	;location
	LDA #0 	;and
	STA HCBAK 	;scramble
	INC STI 	;it to
	LDA STI 	;determine
	LSR A 	;its
	LSR A 	;proper
	LSR A 	;value
	AND #2
	STA BLINK
	LDA SLVL+1 	;Scramble
	LSR A 	;it using
	TAY  	;the vector
	LDA #0 	;high byte
	STA OSC3
	LDA FSPSP
CSL	ORA FSPSP
	DEY  	;If the
	BNE CSL 	;difference
	EOR CONSL 	;is even
	STA CONSL 	;then
	LDX #4 	;go on.
RL	LDA FSPSP
	STA HP0C,X
	LDA FSPSP 	;Sort
	STA SIZE,X 	;needed
	DEX  	;vectors
	BNE RL 	;out of
	LDX CD 	;scratchpad
	DEX  	;and
	BNE XITV 	;transpose
	LDA FSPSP 	;them
	AND #$70 	;as
FSP2	STA DYMUM 	;specified
	LDA #14 	;by bits
	STA HPF2C 	;2,3,4, and
	STA HCBAK 	;7 of their
	LDA #0 	;f-stack
	STA HPF1C 	;entries
	DEC DEL
	LDA DEL
	LSR A
	LSR A
	LSR A
	LSR A 	;Compute
	AND #$0F 	;the
	ADC #4 	;ambidorsal
	STA SLVL+1 	;parallels
	LDA #$8F 	;by sub-
	STA OSC3 	;sequencing
	LDY #PAD/256+3 	;the
DCHA	STY CP1+2 	;returciary
	STY SL+2 	;indices
	JSR DCH 	;and
	DEY  	;performing
	CPY #PAD/256-1 	;Goadian
	BNE DCHA 	;transduction
	LDA DEL
	AND FSPSP
	ORA #1
	TAX 
XITV	STX CD
	JMP XITOBV
;
DCH	LDX FSPSP
	LDA FSPSP 	;Scramble
	AND #1 	;the
	BNE SL 	;FSPSP
CP1	LSR PAD,X
	RTS 
SL	ASL PAD,X
	RTS 
;
STAT	LDX #4
PC	LDA FSPSP 	;Use
	AND #$0F 	;scratchpad
	STA P0C,X 	;to
	LDA #3 	;determine
	STA VTNABL 	;if all
	LDA #0 	;flags
	STA VDEL 	;have been
	DEX  	;found
	BNE PC
;
	LDX #$FF
LOOP	JSR SORT
	STA VT+$0300,X 	;Find
	JSR SORT 	;the
	STA VT+$0400,X 	;variance
	JSR SORT 	;factor
	STA VT+$0500,X 	;between
	JSR SORT 	;FSPSP
	STA VT+$0600,X 	;and the
	JSR SORT 	;scratchpad
	STA VT+$0700,X 	;and
	DEX  	;restructure
	BNE LOOP 	;if
	RTS  	;necessary
;
SORT	LDA FSPSP
SLVL	LDY #$0F
L9	AND FSPSP 	;Check
	DEY  	;flag
	BNE L9 	;to make
	TAY  	;sure
	AND #$1F 	;proper
	ORA #$10 	;value has
	STA OSC2 	;been found
	TYA 
	RTS 
;
FIND	STA CTRLSB 	;Wait
PAW	LDA CTRLSB 	;for Destruct
	BNE PAW 	;line to
	RTS  	;be low.
;
FOB	LDA CTRLSB
FOBW	CMP CTRLSB
	BEQ FOBW 	;Check
	LDA #$8F 	;flag,
	STA OSC3 	;flag value
	LDA #14 	;vector
	STA SPFC0 	;and
	STA SCBAK 	;sequence
	LDA #0 	;code and
	STA CHLUM 	;recompute
	LDA CTRLSB 	;as necessary
VW	CMP CTRLSB
	BEQ VW
	LDA #RED
	STA SPFC0
	LDA #0
	STA ATTRACT
	STA SCBAK
	LDA #10
	STA CHLUM
	LDA #0
	STA OSC3
	RTS 
;
PRINT	STY BUFAD
	STX BUFAD+1
	STA BUFL 	;Print
	LDX #0 	;routine
	STX BUFL+1
	LDA #$0B
	STA COM
	JSR CIOV
	RTS 
;
ABORT	LDA # >PAD 	;Clear
	STA PADASE 	;destruct
	LDA #1 	;request
	STA CUROSR
	LDA #GREEN
	STA 710
;
	LDX # >A1 	;Print
	LDY # <A1 	;aborted
	LDA #A2-A1
	JSR PRINT
;
	LDA DAMAGED
	BEQ NOD 	;determine
	LDA #ORANGE 	;damage
	STA 710
	LDX # >A3
	LDY # <A3
	LDA #A4-A3
	JSR PRINT
;
NOD	LDX # >A2 	;Print
	LDY # <A2 	;how
	LDA #A3-A2 	;much
	JSR PRINT
;
CHECK	LDA CONSL
	CMP #START 	;Exit
	BEQ JSDV 	;or
	CMP #SELECT 	;restart
	BEQ JDOS 	;as
	JMP CHECK 	;requested
JSDV	JMP SDV
JDOS	JMP ($0A)
;
CD	.BYTE 0
DEL	.BYTE 120
PN	.BYTE 0
DAMAGED	.BYTE 0
SBI	.BYTE 0
STI	.BYTE 0
M1	.BYTE "}          "
	.BYTE "PAPERWEIGHT 3.1",$9B	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE "          by Curt Cox"
	.BYTE $9B,$9B
	.BYTE "Press [START] to begin"	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE " locating SDV",$9B,$9B
M2	.BYTE "Locating ...",$9B
M3	.BYTE "!Locating ...",$9B,"!"	;!!!!!!!! INVERSE CHARS SPECIAL CHARS !!!!!!!!
M4	.BYTE $9B,$9B,$9B
	.BYTE "Self-Destruct Vector"
	.BYTE " found",$9B,$9B
	.BYTE "Press [START] to "	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE "execute or",$9B
	.BYTE "[SYSTEM RESET] to "	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE "abort.",$9B,$9B
M5	.BYTE "}Are you sure?"	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE " (press 'Y' for yes)"
	.BYTE $9B,$9B
M6	.BYTE "}",$9B,$9B,$9B
	.BYTE "You can abort without"
	.BYTE " damage for",$9B
	.BYTE "ten seconds.",$9B,$9B
	.BYTE $9B
	.BYTE " 10  9  8  7  6  5  4"	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE "  3  2  1  0",$9B,"!"	;!!!!!!!! INVERSE CHARS SPECIAL CHARS !!!!!!!!
M7	.BYTE "   "
M8	.BYTE $9B
	.BYTE "DESTRUCTION PROCEEDING"	;!!!!!!!! INVERSE CHARS !!!!!!!!
A1	.BYTE "}",$9B,$9B
	.BYTE "Destruct aborted - ",$9B
A2	.BYTE $9B,"press [START] to"	;!!!!!!!! INVERSE CHARS !!!!!!!!
	.BYTE $9B,"re-locate SDV or "
	.BYTE "[SELECT] to exit."	;!!!!!!!! INVERSE CHARS !!!!!!!!
A3	.BYTE $9B,"Damage incalcuable "
	.BYTE "because of",$9B
	.BYTE "damaged memory",$9B
A4
;
	*= $02E0 	;load run address
	.WORD PW
	.END 
