	.TITLE	ROLLIN - SYSTEM ROLLIN PROGRAM

	.IDENT	/01/

;++
;
; Facility: System ROLLIN secondary bootstrap
;
; Abstract: 
;	ROLLIN is passed control for the system primary
; bootstrap routine VMB to restore the contents of core
; saved during ROLLOUT.
;
;
; Environment:
;	Mode=Kernel ,  Memory Management OFF
;	IS=1 ,  IPL=31
;
; Author: W.C. BLASINGAME , Seattle Software Services 
;         Created under PL90 contract - Date: 4-Sep-79
;
; Modified by:
;
;  , Version : 1
;
;--

;
; Include files:
;
	$DPTDEF				; Define Driver Prologue Definitions
	$IHDDEF				; Define Image Header offsets
	$IODEF				; Define I/O function code values
	$PHDDEF				; Define process header offsets
	$PRDEF				; Define process register numbers
	$PTEDEF				; Define page table entry fields
	$RPBDEF				; Define restart parameter block offsets
	$SECDEF				; Define process section block
	$TPADEF				; Define TPARSE offsets
	$VADEF				; Define fields in virtual address
	$WSLDEF				; Define Working Set lis entry

;
; Macros:
;
	.MACRO	ERROR,STR,DAT		; 
	.NARG TEST
	.IF	EQ	TEST-2
	MOVL	DAT,-(SP)
	.ENDC
	.IF	EQ	TEST-1
	CLRL	-(SP)
	.ENDC
	BSBW	ERROUT			; Output error string
	.ASCIZ	STR			;
	.ENDM	ERROR			;

	.MACRO	MSG,STR			;
	BSBW	BOO$FACMSG		; Output message
	.ASCIZ	\'STR'\			;
	.ENDM	MSG			;

;
;	Macro to output prompt string and read response into BOO$GT_COMBUF
;
	.MACRO	PROMPT,STR		;
	BSBW	PROMPT			;
	.ASCIZ	STR			;
	.ENDM	PROMPT			;

;
; Equated Symbols:
;
;
	IOSIZE = 127			; Do I/O in very large chunks
	MAXPGS = 16384			; Allow for 8Mb of memory
	DEBUG  = 1			; Assemble DEBUG code
	CR     = 13			; Character code for carriage return
	LF     = 10			; Character code for line feed

	RP_DT  = 24			; Offset to drive type register for massbus

	FPLA_VLOC = ^O155		; Offset to FPLA version number location
	FPLA = ^X08			; FPLA version number
	PCS_VLOC  = ^O152		; Offset to PCS version number location
	PCS = ^X01			; PCS version number
	WCSP_VLOC = ^O153		; Offset to WCS primary version location
	WCSP = ^X08			; WCS primary version number
	WCSS_VLOC = ^O154		; Offset to WCS secondary version location
	WCSS = ^X11			; WCS secondary version

 
RPB_SAVED_Q1	=	RPB$K_LENGTH	; Offset to saved RPB values
					;  BASE:+4 to BASE:+12
RPB_SAVED_Q2	=	RPB_SAVED_Q1+8	; Offset to saved RPB values
					;  BASE:+12 to BASE:+20
RPB_PHY_IOVEC	=	RPB_SAVED_Q2+8	; Offset to stored address of bootdriver
					;  in the ROLLEDOUT system (physical)
RPB_ROLLIN_CODE	=	RPB_PHY_IOVEC+4	; Offset to HALT instruction in ROLLIN
;
; Own Storage:
;
	.PSECT ZROLLIN,LONG
ROLLINSIZE::

	.PAGE
	.PSECT	AROLLIN,LONG
	.SBTTL	ROLLIN_MAIN - Main routine of ROLLIN
;++
;
; Functional Description:
;
; Calling Sequence:
;	CALLED BY THE PRIMARY BOOTSTRAP ROUTINE - VMB
;
; Input Parameters:
;	R10 - Base address of secondary bootstrap
;	R11 - Pointer to Restart Parameter Block
;	SP  - Stack pointer
;	PR$_SCBB - System Control Block Base (SCB)
;
;	Memory layout at start of secondary bootstrap:
;
;	+-----------------------------------------+ :BASE
;	!					  !
;	!	Restart Parameter Block (RPB)	  !
;	!					  !
;	+-----------------------------------------+ :BASE+^X200
;	!					  !
;	!	  Primary Bootstrap Code	  !
;	!					  !
;	!					  !
;	+-----------------------------------------+ :PR$_SCBB
;	!					  !
;	!	   System Control Block		  !
;	!					  !
;	+-----------------------------------------+ :PFNMAP
;	!					  !
;	!		PFN Bitmap		  !
;	!					  !
;	+-----------------------------------------+ :PFNMAP+^X800
;	!					  !
;	!	     Bootstrap Stack		  !
;	!					  !
;	+-----------------------------------------+ :(SP)
;	!					  !
;	!	 Secondary Bootstrap Code	  !
;	!					  !
;	+-----------------------------------------+
;
; Output Parameters:
;
;--

ROLLIN_MAIN::
	MOVL	R11,W^RPBBASE		; Save base addr of RPB for call back.
	MOVAB	W^DMPFILE,R7		; Get address of file descriptor
	MOVC3	#40,(R7),RPB$T_FILE(R11)	; Copy file name to RPB
	MOVL	RPB$L_IOVEC(R11),R5	; Save address of device independent 
					;  IO dirver (physical)
FILEOPEN:
;
;	R7 - Address of file name string
;	R10 - Address of first free memory after bootstrap
;	R11 - Address of Restart Parameter Block (RPB)
;
	PUSHAB	RPB$T_FILE+1(R11)	; Address of file name string
	MOVZBL	RPB$T_FILE(R11),-(SP)	; Count for string
	MOVAL	-(SP),R6		; Save pointer to file name descriptor
	PUSHAL	RPB$L_FILLBN(R11)	; Address of file statistics block
	PUSHAL	ROLLINSIZE(R10)		; Address of free space for index header.
	PUSHAL	ROLLINSIZE+512(R10)	; Address of space for file header.
	PUSHAL	4(R6)			; Address of file name descriptor
	PUSHAL	(R6)			; Address of phony channel
	CALLS	#7,W^FIL$OPENFILE	; Call FILEREAD to locate file
	BLBS	R0,10$			; Continue if successful open
	ERROR	</%ROLLIN-F-ERROR WHILE OPENING DUMPFILE/>
10$:	MOVQ	RPB$L_FILLBN(R11),R8	; Get logical block of dump file.
	TSTL	R8			; Check for contiguous
	BNEQ	20$			; Branch if file contiguous
	ERROR	</%ROLLIN-F-DUMPFILE NOT CONTIGUOUS/>
20$:
	MOVZBL	#1,R9			; Set to read the RPB block from file
	ADDL2	#3,R8			; Skip header and 2 errlog blocks.
	ROTL	#-9,R11,R0		; CALCULATE ANY OFFSET CAUSED BY
					; LOW MEMORY NOT USABLE.
	ADDL2	R0,R8			; ADD OFFSET TO LBN
	MOVL	R11,R6			; Set address of beginning of memory.
					; Read logical block 4 of SYSDUMP.DMP
					;  to BASE:+0
30$:	MOVZBL	#IOSIZE,R7		; Assume maximum transfer
	CMPL	R7,R9			; Minimize with file size
	BLEQ	40$			; Smaller than remaining file size
	MOVL	R9,R7			; Set to remaining file size
40$:	PUSHL	R11			; Base of RPB
	PUSHL	#0			; Set mode to physical
	PUSHL	#IO$_READLBLK		; Set function to read
	PUSHL	R8			; Set Logical Block Number
	ROTL	#9,R7,-(SP)		; Set transfer size in bytes
	PUSHL	R6			; Set buffer address
	ADDL	4(SP),R6		; Update buffer address
	ADDL	R7,R8			;  and block number
	CALLS	#6,@(R5)[R5]		; Perform read
	BLBS	R0,50$			; Continue if no error
	ERROR	</%ROLLIN-F-IO ERROR WHILE READING DUMPFILE , STS= />,R0
50$:
	CMPL	R11,RPB$L_BASE(R11)	; ASSURE THAT PHYSICAL CONFIGURATION
					;   HAS NOT CHANGED SINCE ROLLOUT.
	BEQL	55$			; BRANCH IF OK
	ERROR	</%ROLLIN-F-SYSTEM CONFIGURATION CHANGED SINCE ROLLOUT,BASE=(HEX)/>,RPB$L_BASE(R11)
55$:	SUBL	R7,R9			; Decrement blocks remaining
	BEQL	130$			; DONE
	JMP	30$			; Another transfer if not done
; Move the QIOVEC code to its corresponding location
;  in the ROLLEDOUT system
;
130$:	MOVC3	RPB$L_IOVECSZ(R11),(R5),-	; This operation distroys R0-R5
		@RPB_PHY_IOVEC(R11)
	MOVL	RPB_PHY_IOVEC(R11),R5	; Point R5 at new code
;
; Now restore the rest of physical memory using the ROLLIN code
; in the RPB and the bootdriver at its overlay position in
; physical memory.
;
	MOVAL	4(R11),R7		; Set up abbreviated SCB
	MOVZBL	S^#4,R9			;four vectors only!
140$:	MOVAL	RPB_ROLLIN_CODE(R11),(R7)+
	SOBGTR	R9,140$
;
	MOVL	RPB$L_SLR(R11),R7	; Calculate physical page size of system.
	MOVAL	@RPB$L_SBR(R11)[R7],R9	;assuming SPT in High Phy Memory
	ASHL	#-9,R9,R9		;no. of pages in ROLLEDOUT system.
	DECL	R9			;account for RPB already copied.
	MTPR	R11,#PR$_SCBB		; Point SCBB to abbreviated SCB
	MOVAL	^X200(R11),SP		; Point Stack to top of RPB
	JMP	RPB_ROLLIN_CODE-^X200+4(SP)	; Execute ROLLIN code.
;
;
;
	.PAGE
	.SBTTL	SYS$ASSIGN - Dummy assign device system service
;
; Functional Description:
;	SYS$ASSIGN is a dummy routine to satisfy the requirements of
;	FIL$OPENFILE.
;
; Input Parameters:
;	CHAN(AP) - Address at which to return channnel (RPB base address)
	CHAN	 = 8
;
; Output Parameters:
;	R0 - Completion status code
;
; Completion Codes:
;	SS$_NORMAL - Normal successful completion
;
;--
SYS$ASSIGN::	.WORD	0		; Null entry mask
	MOVL	RPBBASE,@CHAN(AP)	; Store RPB address as channel
	MOVZWL	#1,R0			; Return success
	RET				;

	.PAGE
	.SBTTL	Error message subroutine
;+
; Functional Description:
;	ERROUT outputs a descriptive error message to the console.
;
; Calling Sequence:
;	BSBW	ERROUT
;	.ASCIZ	/message/,STATUS
;
;-
ERROUT:	MOVL	(SP)+,R1		; Get pointer to message text
	CLRL	R11			; OUTPUT DEVICE IS CONSOLE
	BSBW	EXE$OUTCRLF
	BSBW	EXE$OUTZSTRING		; WRITE ERROR MESSAGE
	MOVL	(SP),R1			; GET STATUS CODE
	BEQL	10$			; BRANCH IF NO STATUS
	BSBW	EXE$OUTHEX		; PRINT IT
	BSBW	EXE$OUTCRLF

10$:	BRB 10$				; LOOP AND WAIT FOR CONSOLE HALT


	.PAGE
	.SBTTL	Miscellaneous constants and temps

FIL$GT_DDDEV::				; Default device string
	.ASCIC	/DDEV:/			;
FIL$GT_DDSTRING::			; Default directory string
	.ASCIC	/[SYSEXE]/		;

RPBBASE:.LONG	0			; Base address of RPB
;
DMPFILE:
	.ASCIC	/[SYSEXE]SYSDUMP.DMP/
;
	.END
