	TITLE	"BIOS.INI   SAGE BIOS: INITIALIZATION"
	PAGE
**************** SYSTEM INITIALIZATION *************************
	ORG	BIOS_CODE
	MOVEM.L	#0FFFFH,MISCREG
	LEA	BIOS_STACK,A7
	MOVE.L	A7,USP
	LEA	BIOS_DATA,A6
	MOVE	#2700H,SR	;TURN OF ALL INTERRUPTS
;------------------------------- COPY VECTORS TO RAM
	LEA	BIOS_CONST.L,A0
	LEA	0,A1
	MOVE.W	#255,D0
VECOPY	MOVE.L	(A0)+,(A1)+
	DBRA	D0,VECOPY
;------------------------------- GET BOOTING DRIVE
	MOVE.W	DRIVE_VAR,D0
	MOVE.W 	D0,DRIVE(A6)
;------------------------------- CLEAR BIOS VARIABLES
	MOVEA.L	#CLR_BEGIN,A0
	MOVE.W	#BIOS_CONST - CLR_BEGIN - 1, D1
	CLR.L	D0
CLRMEM	MOVE.B	D0,(A0)+
	DBRA	D1,CLRMEM
;------------------------------- SET UP REAL TIME CLOCK
	LEA	BOOTIM(A6),A0
	MOVE.L	A0,TIMEPN(A6)
	LEA	BOODAT(A6),A1
	MOVE.L	A1,DATEPN(A6)
	LEA	BOOTMS(A6),A2
	MOVE.L	A2,MSECPN(A6)
	CLR.L	TICCNT(A6)
	CLR.L	(A0)		;00:00:00.00
	MOVE.L	#86010100H,(A1)	;JAN 1, 1986
	CLR.L	(A2)
;------------------------------- SET UP SERIAL I/O, PERIODIC INTERRUPTS
	MOVE.B	#092H,F8255		;SET A & B TO INPUT
	MOVE.B	#0F8H,DIPSWT+4		;C BITS TO OUTPUT
	MOVE.B	#082H,C8255+6		;SET A TO OUT, B TO IN
	MOVE.B	#030H,C8255+4		;SET C TO OUTPUT
	MOVE.B	#12H,P8259		;INITIALIZE 8259
	MOVE.B	#0,P8259+2
	MOVE.B	#0BFH,P8259+2		;INITIAL INTERUPT MASK
	MOVE.B	#0EH,F8255		;TURN OFF FLOPPY RESET STROBE
	MOVE.B	#34H,BRATE+6		;1ST COUNTER OF RTC MODE 2
	MOVE.B	#30H,TIMER+6		;SECOND COUNTER MODE 0
	CLR.B	D1
	MOVE.B	#2,BRATE		;SET UP FASTEST COUNT STAGE 2
	MOVE.B	D1,TIMER		;LOW COUNT SECOND STAGE
	MOVE.B	D1,BRATE		;HIGH COUNT 1ST STAGE
	MOVE.B	D1,TIMER		;HIGH COUNT SECOND STAGE
	MOVEQ	#64,D1
SETWT	DBF	D1,SETWT		;DO SETUP WAIT LOOP
	MOVE.W	#07FFFH,D0		;1/2 SEC COUNT
	MOVE.B	D0,BRATE		;LOW COUNT FIRST STAGE
	LSR.W	#8,D0
	MOVE.B	D0,BRATE		;HIGH COUNT FIRST STAGE
;------------------------------ END OF ROUTINES FROM SAGE BIOS

	BSR	ICHANA			;INITIALIZE CHANNEL A
	BSR	ICHANB			;INITIALIZE CHANNEL B
;------------------------------- SET UP PRINTER PORT
	BSR	INIPRN
;------------------------------- SET UP DISK I/O VARS
	LEA	CURTRAC(A6),A0
	MOVEQ	#3,D0		;FOR DRIVE 4 DOWNTO 1
INIDSK	CLR.B	0(A0,D0)	;  HEAD ON TRACK 00
	DBRA	D0,INIDSK
;------------------------------- ENABLE AUTOVECTOR INTERRUPTS
	MOVE.W	#SUPERSR,SR	;TURN ON INTERRUPTS
;------------------------------- SAY HELLO
	LEA	STACK_4.L,A4
	LEA	BIOSMES,A0
	BSR	WRITESTR
;------------------------------- LOAD OPERATING SYSTEM
LOOPCAR BSR	GETCON
	BSR	PUTCON
	JMP	LOOPCAR	

SYSTEM	BSR	LOADFAT
	TST.B	DSKERR(A6)
	BNE	DKABORT
	MOVE.L	#SYSNAM,FNAMPN(A6)
	MOVE.L	#OPSYS_CODE,LOADPN(A6)
	BSR	FINDFIL
	BEQ.S	LSYS
	TST.B	DSKERR(A6)
	BNE	DKABORT
	LEA	SYSNAM,A0
	BRA.S	NFABORT
LSYS	BSR	LOADFIL
	TST.B	DSKERR(A6)
	BNE.S	DKABORT
;------------------------------- LOAD COMMAND PROCESSOR
	MOVE.L	#COMM_CODE,D0
	BEQ.S	NONRESIDE
	MOVE.L	#COMNAM,FNAMPN(A6)
	MOVE.L	D0,LOADPN(A6)
	BSR	FINDFIL
	BEQ.S	LCOM
	TST.B	DSKERR(A6)
	BNE.S	DKABORT
	LEA	COMNAM,A0
	BRA.S	NFABORT
LCOM	BSR	LOADFIL
	TST.B	DSKERR(A6)
	BNE.S	DKABORT
NONRESIDE
**************** SET UP DONE, START A PROGRAM ***********************
	JMP	OPSYS_CODE	;START HTPL OP SYS

**************** COULD NOT LOAD SYSTEM ******************************
NFABORT	BSR	WRITESTR	;WRITE FILENAME
	LEA	NFMES,A0
	BSR	WRITESTR	;WRITE FILE NOT FOUND
	BRA.S	ABORT
DKABORT	LEA	DKMES,A0	;WRITE DISK ERROR
	BSR	WRITESTR
ABORT	JMP	DEBUG		;EXIT TO MONITOR

**************** INITIALIZATION SUBROUTINES *************************
;------------------------------- INITIALIZE SERIAL CHANNEL A
ICHANA
	LEA	TERM_C,A0
	CLR.B	D0
	MOVE.B	D0,(A0)		;FIRST ZERO TO 8251A
	MOVE.B	#76H,BAUDC	;SQUARE ON 8253
	ROR.B	#8,D0		;DELAY OPERATON
	MOVE.B	D0,(A0)		;SECOND ZERO TO 8251A
	MOVE.B	DIPSWT,D1	;GET BAUD RATE FROM SWITCH
	ROR.B	#8,D0		;MORE DELAY
	MOVE.B	D0,(A0)		;THIRD ZERO OUT
	MOVE.B	#06EH,D0	;8N1
	BTST	#3,D1		;FOR PARITY SET
	BEQ.S	CHA1LP		;LOOP IF NOT
	MOVEQ	#7EH,D0		;SET PARITY
CHA1LP	LEA	RATES,A1
	MOVE.B	#40H,(A0)	;RESET 8251A
	ANDI.W	#7,D1		;CHECK BIT 0-2 OF DIPSWITCH
	ADDA	D1,A1		;MATCH BITS TO TABLE VALUE
	MOVE.B	(A1),D1
	MOVE.B	D1,BAUD1D	;LOW BIT BAUD RATE
	LSR.W	#8,D1
	MOVE.B	D0,(A0)		;MODE SELECT OF 8251( 16X CLOCK)
	MOVE.B	D1,BAUD1D	;ZERO
	ROR.B	#8,D0		;DELAY TIME
	MOVE.B	#025H,(A0)	;TX,RX,RTS,SET; ERRORS CLEARED
;
	LEA	VARS+CONIN,A0	;RESET INPUT QUEUE
	CLR.B	HEAD(A0)
	CLR.B	TAIL(A0)
	CLR.W	SIZE(A0)
	LEA	VARS+CONOUT,A0	;RESET OUTPUT QUEUE
	CLR.B	HEAD(A0)
	CLR.B	TAIL(A0)
	CLR.W	SIZE(A0)
	RTS

RATES	DC.B	2		;RESERVED BAUD RATE (19200)
	DC.B	128		;300 BAUD
	DC.B	64		;600
	DC.B	32		;1200
	DC.B	16		;2400
	DC.B	8		;4800
	DC.B	4		;9600
	DC.B	2		;19200

;------------------------------- INITIALIZE SERIAL CHANNEL B
ICHANB
	LEA	REM_C,A0
	CLR.B	D0
	MOVE.B	D0,(A0)		;FIRST ZERO TO 8251A
	MOVE.B	#0B6H,BAUDC	;SQUARE ON 8253
	ROR.B	#8,D0		;DELAY OPERATON
	MOVE.B	D0,(A0)		;SECOND ZERO TO 8251A
	ROR.B	#8,D0		;MORE DELAY
	MOVE.B	D0,(A0)		;THIRD ZERO OUT
	MOVE.B	#32,BAUD2D	;SET FOR 1200 BAUD
	ROR.B	#8,D0		;DELAY
	MOVE.B	#40H,(A0)	;RESET 8251
	MOVE.B	#0,BAUD2D	;HIGH BIT IS ALWAYS ZERO
	ROR.B	#8,D0		;DELAY
	MOVE.B	#4EH,(A0)	;MODE SELECT 16X CLOCK, 8N1
	ROR.B	#8,D0		;DELAY TIME
	MOVE.B	#027H,(A0)	;TX,RX,RTS,SET; 
;
;	MOVE.B	#SERIALON,(A0)	;ENABLE TRANSCEIVER
	LEA	VARS+AUXIN,A0	;RESET INPUT QUEUE
	CLR.B	HEAD(A0)
	CLR.B	TAIL(A0)
	CLR.W	SIZE(A0)
	LEA	VARS+AUXOUT,A0	;RESET OUTPUT QUEUE
	CLR.B	HEAD(A0)
	CLR.B	TAIL(A0)
	CLR.W	SIZE(A0)
	RTS
;------------------------------- INITIALIZE PRINTER PORT
INIPRN
	MOVE.B	#0CH,C8255+6	;DISABLE PORT INTERRUPTS
	BSET	#5,P8259+2
	LEA	VARS+PRNOUT,A0	;RESET OUTPUT QUEUE
	CLR.B	HEAD(A0)
	CLR.B	TAIL(A0)
	CLR.W	SIZE(A0)
	ST	VARS+PRNACK	;ACKNOWLEDGE READY FOR NEXT CHAR
	RTS

	END	;BIOS.INI
