; Dumper.asm -- desperation debugger invoker -- James H. Morris Jr.
; Copyright Xerox Corporation 1979
; Last modified September 13, 1979  1:42 AM by Boggs

.BEXT DUMPER, SWATEEP2, SWATDA

	.SREL
DUMPER:	START
SWATEEP2: LABEL		; patched by InitDisk
SWATDA: SWTDA		; patched by InitDisk

HSPA = 176400		; HIGHEST SWAPPED PAGE'S ADDRESS

	.NREL

; THE PAGE 0 DUMPER

STRTL:	; THIS CODE  EXECUTES IN PAGE 0.
	; IT STARTS IN LOCATION 1, BROUGHT IN BY BOOT BUTTON
	; 402 = FILE ID FOR NEXT PAGE OF SAVE FILE
; WRITE BULK OF SAVED IMAGE
START:	JMP .+2
	0		; CLOBBERED BY BOOTING
	STA 0,@AC0	; SAVE REGISTERS
	STA 1,@AC1
	STA 2,@AC2
	STA 3,@AC3
	SUBCL 0,0
	STA 0,@CARRY
	LDA 0,0,0	; LEFT BY BOOTING
	STA 0,@PC

	SUB 0,0
	COM 0,0		; SIGNAL THAT SWATEE IS
	STA 0,@BOOTFLAG	; INCOMPLETE

	LDA 0,WRITE
	JSR L1
LABEL:	.BLK 10
L1:	MOV 3,1		; GET LABEL IN AC1
	JSR SETUPC
	JSR BULKC
; BULK OF FILE WRITTEN, CUR HOLDS NEXT PAGE INFO.
	LDA 0,N400
	JSR PAGEC	; WRITE PAGE 1
; DUMPING COMPLETE, SIMULATE A BOOT
;
;CB:	0
;	0
;	44000		; READ WITHOUT CHECK
;	402		; HEADER POINTER
;	402		; LABEL POINTER
;	1		; DATA POINTER
;	0
;	0
;	0
;	0
	LDA 2,N1000	; THE ABOVE COMMAND BLOCK GOES IN 1000...
	SUB 0,0
	STA 0,0,2	; NEXT COMMAND
	STA 0,1,2	; STATUS
	LDA 1,SREAD	; SIMPLE READ
	STA 1,2,2	; COMMAND
	LDA 1,N402
	STA 1,3,2	; HEADER PTR
	STA 1,4,2	; LABEL PTR
	SUBZL 1,1	; 1
	STA 1,5,2	; DATA PTR
	STA 0,6,2	; INTERRUPT - OK
	STA 0,7,2	; INTERRUPT - ERROR
	LDA 1,SWTDA
	STA 1,11,2	; SWAT'S DISK ADDRESS
; PUT INSTRUCTIONS TO READ IN LOCS 0,1,2
	LDA 0,STIN
	STA 0,0
	LDA 0,N400	; A JMP . INSTRUCTION
	STA 0,1
	LDA 0,KBLK
	STA 0,2
	JMP 0
STIN:	STA 2,@2,0	; START DISK INST. (STORED IN 1)
;
; CONSTANTS
SWTDA:	0
N3:	3
N10:	10
N400:	400
N402:	402
PC:	700
AC0:	701
AC1:	702
AC2:	703
AC3:	704
CARRY:	705
BOOTFLAG: 706
N1000:	1000
HDRDS:	10	; HEADER DISPLACEMENT IN COMMAND BLOCK
SREAD:	44000
WRITE:	44130
LBLDS:	12
MAXAD:	HSPA	; ADDRESS OF LAST PAGE, 64K MEMORY
;
; SUBROUTINES;
; SETUP, CONT, PAGE, AND WAIT, ARE A SUBSYSTEM TO
; COMMUNICATE WITH THE DISK CONTROLLER. AFTER SETUP, CUR AND ALT
; ARE TWO DISK COMMAND BLOCKS SUCH THAT CUR IS THE LAST ONE
; PLACED IN KBLK -- BY US OR THE DISK CONTROLLER.

;
; DISPLACEMENTS IN DISK CONTROL BLOCK.
NXTCM=0
STATU=1
CMMD=2
HDRPT=3
LBLPT=4
CMADD=5
OKINT=6
ERRIN=7
HDR1=10
CURRE=11
NEXTP=12
BACKP=13
UNUSE=14
NUMCH=15
PN=16
VN=17
SN1=20
SN2=21
;
BULKC:	; READS OR WRITES PAGES 2 THRU MAXAD/400
	; D.C. DORMANT
	; CUR AND ALT HAVE BEEN SET UP
	; THIS CODE IS ASYNCHRONOUS AND TOOK SEVERAL TRYS
	; TO GET RIGHT!
	; IT'S SUPPOSED TO BE TIMING INDEPENDENT.
	STA 3,BLRET
	LDA 0,N1000
	JSR CONTC	; BEGIN ON PAGE 2
SWTCH:	LDA 0,CUR
	LDA 1,ALT
	STA 1,CUR
	STA 0,ALT

WAITL:	; ALL PAGES BEFORE CUR ARE PROCESSED AND CHECKED.
	; LINK OF CUR IS 0.
	; EITHER D.C. IS ACTIVE, WORKING ON CUR
	; OR D.C. IS DORMANT, NEVER STARTED CUR
	; OR D.C. IS DORMANT, FINISHED CUR
	LDA 2,CUR
	LDA 0,NEXTP,2
	MOV 0,0,SZR
	JMP LBLIN	; D.C. STARTED CUR, WORKING OR FINISHED
	LDA 0,@KBLK
	MOV 0,0,SZR
	JMP WAITL	; D.C. ACTIVE, STARTED CUR
	LDA 0,STATU,2
	MOV 0,0,SZR
	JMP FUNNY	; D.C. FINISHED, PROBABLY AN ERROR
	STA 2,@KBLK	; D.C. NEVER STARTED CUR
	JMP WAITL
;
FUNNY:	JSR WAITC	; HAVE WAIT CHECK FOR ERRORS
			; IT MAY RETRY.
			; LABEL AS WELL AS DATA IN

LBLIN:	; NOW WE KNOW ADDRESS OF NEXT PAGE. SET UP ALT
	LDA 0,CMADD,2	; AC2 = CUR
	LDA 1,MAXAD
	SUB# 0,1,SNR
	JMP LAST
	LDA 1,N400
	ADD 1,0
	JSR CONTC
	JSR WAITC	; FOR CUR'S DATA
	JMP SWTCH
LAST:	JSR WAITC
	JMP @BLRET
	; PAGES 0...END DONE, D.C. DORMANT
BLRET:	.BLK 1
;END OF BULK

KBLK:	521	; MAIL BOX FOR DISK CONTROLLER
;
CONTC:	; SETS UP ALTERNATE COMMAND BLOCK
	; D.C. MAY BE ACTIVE.
	; AC0 = ADDRESS OF CORE PAGE.
	STA 3,CRA
	LDA 2,CUR
	LDA 3,ALT
	STA 0,CMADD,3
	SUB 0,0
	STA 0,NXTCM,3
	STA 0,STATU,3
	LDA 1,NEXTP,2
	STA 1,CURRE,3
	LDA 1,CURRE,2
	STA 1,BACKP,3
	STA 0,NEXTP,3
	STA 0,PN,3
	STA 3,NXTCM,2	; PUT LINK IN ACTIVE COMMAND
	JMP @CRA
	; ALT IS SET UP, HOPEFULLY BEFORE D.C. LOOKED FOR
	; LINK IN CUR.
CRA:	0
; END OF CONT
;
CUR:	.BLK 1		; CURRENT COMMAND
ALT:	.BLK 1		; ALTERNATE
;
PAGEC:	; AC0 = MAIN MEM ADDRESS TO SAVE OR RESTORE
	; PROCESS AND CHECK PAGE
	STA 3,RPAGE
	JSR CONTC
	LDA 0,CUR
	LDA 1,ALT
	STA 1,CUR
	STA 0,ALT
	STA 1,@KBLK
	JSR WAITC
	JMP @RPAGE
RPAGE:	.BLK 1
;END OF PAGE
;
SETUPC:	; D.C. IS DORMANT
	; AC0 = READ OR WRITE COMMAND
	; AC1 = LABEL BLOCK WHOSE NEXTP IS ADRESS TO BEGIN WITH
	; INITIALIZES COMMAND/LABEL BLOCKS SO THAT
	; NEXTP IN CUR IS THE DISK ADDRESS TO START
	; WITH. A CALL ON PAGE (OR CONT) WILL FOLLOW.
	STA 0,COMM	; SAVE PARAMS
	STA 1,FILID
	STA 3,RSTRT
	; SET CUR,ALT AND BACKGROUND WITH 0'S
	JSR CBA		; AC3 = FIRST WORD OF COMM. BLK. AREA
	MOV 3,1
	STA 1,CUR
	LDA 3,CBSIZE
	ADD 3,1		; AC1 = START OF SECOND CB
	STA 1,ALT
	ADD 3,1		; AC1 = END OF CB2 + 1
	ADD 3,3		; AC3 = SIZE OF AREA
	NEG 3,3		; (EXPLETIVE DELETED)
	SUB 0,0
	BLKS		; ZERO ALL BUT NXTCM OF CUR, WHICH
			; CONT WILL ZERO LATER
	; PUT IN LOCAL POINTERS
	LDA 2,CUR
	LDA 3,ALT
	LDA 0,HDRDS
	ADD 2,0
	STA 0,HDRPT,2
	LDA 0,HDRDS
	ADD 3,0
	STA 0,HDRPT,3
	LDA 0,LBLDS
	ADD 2,0
	STA 0,LBLPT,2
	LDA 0,LBLDS
	ADD 3,0
	STA 0,LBLPT,3
	LDA 0,COMM
	STA 0,CMMD,2
	STA 0,CMMD,3
	; PUT IN FILE ID
	LDA 3,FILID
	LDA 0,NEXTP-12,3
	STA 0,NEXTP,2	; AS IF WE JUST DID PAGE BEFORE FIRST
	LDA 0,SN1-12,3
	LDA 1,SN2-12,3
	LDA 3,VN-12,3
	STA 0,SN1,2
	STA 1,SN2,2
	STA 3,VN,2
	LDA 2,ALT
	STA 0,SN1,2
	STA 1,SN2,2
	STA 3,VN,2
	;
	; COMMAND BLOCKS SET UP.
	JMP @RSTRT
COMM:	.BLK 1
FILID:	.BLK 1
RSTRT:	.BLK 1
CBSIZE:	22
; END OF SETUPC

WAITC:	; D.C. PROBABLY ACTIVE, WORKING ON CUR
	STA 3,RETW
	LDA 3,N10	; TRY 8 TIMES
	STA 3,TRYCT
RETRY:	LDA 2,CUR
	LDA 0,STATU,2
	MOV 0,0,SNR
	JMP RETRY
	LDA 1,N377
	AND 0,1,SNR
	JMP @RETW	; RETURN IF NO ERROR
	DSZ TRYCT
	JMP .+2
	JMP GVUP
	LDA 0,N2
	SUB# 0,1,SNR	; SKIP UNLESS LABEL CHECK
	JMP GVUP	; LABEL CHECK
	SUB 0,0		; STRANGE ERROR
	STA 0,STATU,2	; CLEAR LABEL BLOCK
	STA 0,NEXTP,2
	STA 0,BACKP,2
	STA 0,PN,2
	STA 0,NUMCH,2
	STA 0,UNUSE,2
	STA 0,HDR1,2
	COM 0,0
	STA 0,@KBLK2	; FORCE SEEK
	STA 2,@KBLK
	JMP RETRY
GVUP:	77400		; Break,i.e. try anything
KBLK2:	523
N2:	2
N377:	377
TRYCT:	.BLK 1		; TRY COUNT
RETW:	0
; END OF WAIT
;
CBA:	JSR 0,3
CB1:	.BLK 22
CB2:	.BLK 23
	; EXTRA WORD IS CLOBBERED BY BLKS, WHO CARES?
;END OF SETUP
LASTL:	JMP STRTL+201	; CAUSES ASM ERROR IF PROGRAM
			; IS TOO BIG FOR ONE PAGE
	.END