/* A crt0 to use with DDT */

.set	KSP,0		/* Kernel Stack Pointer */
.set	ISP,4		/* Interrupt Stack Pointer */
.set	SCBB,0x11	/* System Control Block Base register */
.set	MCESR,0x26	/* Machine Check Error Summary Register */
.set	SID,62		/* System ID register */
.set	MCHECK,0x4	/* Machine Check Vector */

.set	SIDmask,0x00ffffff /* Only the high byte has the processor type */
.set	uVAX2,0x08000000 /* SID for the uVAX-II */
.set	QBMAP,0x20088000 /* Start of Q-Bus mapping resigters on uVAX-II */
.set	QBMvalid,0x80000000 /* Valid bit in Q-Bus mapping register */
.set	QBMnum,0x2000	/* Number of Q-Bus mapping registers */
.set	Conmem,0x400	/* High memory to save for use by console program */
.set	bootmem,8192	/* memory to save for boot loader */

	.globl	start
	.globl	__start
	.globl	_main
	.globl	_JOBSA
	.globl	_DDT
	.globl	_scb
	.globl	_himem

start:
__start:
	.word	0
	/* If is a uVAX-II, set up a straight Q-bus map. */
	mfpr	$SID,r0
	bicl2	$SIDmask,r0
	cmpl	r0,$uVAX2
	bneq	3f
	clrl	r0
	movl	$QBMAP,r1
4:	movl	r0,(r1)[r0]
	bisl2	$QBMvalid,(r1)[r0]
	aoblss	$QBMnum,r0,4b

3:	movab	tmpstack,r0
	mtpr	r0,$ISP
	mtpr	r0,$KSP
	movab	_scb,r1
	mtpr	r1,$SCBB

	/* Get off the interrupt stack */
	pushl	$0x1f0000	/* A PSL at IPL 1f */
	pushal	size_mem	/* The PC to return to */
	rei
.align 2
size_mem:
	/* Size memory */
	movl	MCHECK+_scb,r1		/* Save machine check vector */
	movab	mcheck,MCHECK+_scb	/* Install own mcheck vector */
	clrl	r0
1:	tstl	(r0)			/* touch memory location */
	addl2	$512,r0			/* go through memory by pages */
	brb	1b

.align	2
mcheck:	mtpr	$0,$MCESR		/* Clear machine check error */
	subl2	$Conmem+bootmem,r0	/* Save memory for console program */
					/*   and boot loader */
	movl	r0,_himem		/* store address of first non-existant 
					 * page */
	movl	r1,MCHECK+_scb		/* restore machine check vector */

	/* get off interrupt stack and call DDT */
	pushl	$0x1f0000
	pushal	_DDT
	rei

_JOBSA:
	movl	_himem,sp		/* Stack at end of memory */
	mtpr	sp,$KSP
	mtpr	sp,$ISP
	clrl	ap
	clrl	fp
	pushl	$0
	pushl	$0
	pushl	$0
	calls	$3,_main
	halt

	.data
/* A temporary stack for startup */
	.align	2
	.space	100
tmpstack:
	.space	4

_himem:	.long	0	/* Address of first nonexistant memory page */
