	TITLE	"BIOS.INI   HT68K BIOS: INITIALIZATION"
	PAGE
**************** SYSTEM INITIALIZATION *************************
	ORG	BIOS_CODE
	TST.B	SWAPREG		;SWAP ROM OUT OF PAGE ZERO
	MOVEM.L	#0FFFFH,MISCREG
	LEA	BIOS_STACK.L,A7
	MOVE.L	A7,USP
	LEA	BIOS_DATA.L,A6
	LEA	STACK_4.L,A4
	MOVE	#2700H,SR
;------------------------------- COPY VECTORS TO RAM
	LEA	BIOS_CONST.L,A0
	LEA	0,A1
	MOVE.W	#255,D0
VECOPY	MOVE.L	(A0)+,(A1)+
	DBRA	D0,VECOPY
;------------------------------- CLEAR BIOS VARIABLES
	MOVEA.L	#CLR_BEGIN,A0
	MOVE.W	#BIOS_STACK - 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
	CLR.B	MASK681(A6)		;MASK ALL DUART INTERRUPTS
	MOVE.B	MASK681(A6),IMRREG
	BSR	ICHANA			;INITIALIZE CHANNEL A
	BSR	ICHANB			;INITIALIZE CHANNEL B
 	MOVE.B	#OUTCONFIG,OPCRREG	;CONFIGURE OUTPUT PORT
	MOVE.B	#0FFH,OP0REG		;SET OUTPUT LINES
	MOVE.W	#TPRELOAD,D0		;SET TIMER PRELOAD
	MOVE.W	D0,D1
	LSR.W	#8,D1
	MOVE.B	D1,CTURREG
	MOVE.B	D0,CTLRREG
	MOVE.B	#AUXCNTRL,ACRREG	;SELECT TIMER MODE, INPUT INTERRUPT
	TST.B	STARTREG		;START TIMER
	ORI.B	#CTRBIT,MASK681(A6)	;ENABLE TIMER INTERRUPT
	MOVE.B	MASK681(A6),IMRREG
;------------------------------- SET UP DISK I/O VARS
	CLR.L	CURDRIV(A6)	;CURRENT DRIVE=NONE (FORCES RESTORE)
	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
;------------------------------- CLEAR INTERRUPTS
	CLR.B	STB1REG		;PRINTER
	MOVE.B	#CIRQCMD,COMMREG;FLOPPY
;------------------------------- ENABLE AUTOVECTOR INTERRUPTS
	MOVE.W	#SUPERSR,SR
;------------------------------- SAY HELLO
	LEA	STACK_4.L,A4
	LEA	BIOSMES,A0
	BSR	WRITESTR
	JMP	MONITOR	;*************************DEBUG************
;------------------------------- SET UP PRINTER PORT
	BSR	INIPRN
;------------------------------- SET UP DISK CONTROLLER
	LEA	BIOS_DATA.L,A2
	MOVE.L	#1,DRIVE(A2)
	BSR	RESTORE
;------------------------------- LOAD OPERATING SYSTEM
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	MONITOR		;EXIT TO MONITOR

**************** INITIALIZATION SUBROUTINES *************************
;------------------------------- INITIALIZE SERIAL CHANNEL A
ICHANA
	LEA	CRAREG,A0
	MOVE.B	#RESETMR,(A0)	;RESET CHANNEL A
	MOVE.B	#RESETRX,(A0)
	MOVE.B	#RESETTX,(A0)
	MOVE.B	#RESETER,(A0)
	MOVE.B	#RESETBR,(A0)
	MOVE.B	#MODE1,MRAREG	;SET PARITY, BITS/CHAR
	MOVE.B	#MODE2,MRAREG	;SET STOP BIT LENGTH
	MOVE.B	#BAUDA,CSRAREG	;SET BAUD
	MOVE.B	#SERIALON,(A0)	;ENABLE TRANSCEIVER
	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)
	LEA	VARS+MASK681,A0
	ORI.B	#RXABIT,(A0)	;ENABLE RECEIVER INTERRUPT
	MOVEQ	#TXABIT,D0
	TST.W	VARS+CONOUT+SIZE;IF TRANSMIT QUEUE NOT EMPTY
	BEQ.S	ICHA10
	MOVE.W	#5555H,70000H;===============DIAGNOSTIC
;======	OR.B	D0,(A0)		;  THEN ENABLE TRANSMITTER INTERRUPT
;======	BRA.S	ICHA20
ICHA10	NOT.B	D0		;  ELSE DISABLE TRANSMITTER INTERRUPT
	AND.B	D0,(A0)
ICHA20	MOVE.B	(A0),IMRREG	;SEND INTERRUPT MASK TO 68681
	RTS
;------------------------------- INITIALIZE SERIAL CHANNEL B
ICHANB
	LEA	CRBREG,A0
	MOVE.B	#RESETMR,(A0)	;RESET CHANNEL B
	MOVE.B	#RESETRX,(A0)
	MOVE.B	#RESETTX,(A0)
	MOVE.B	#RESETER,(A0)
	MOVE.B	#RESETBR,(A0)
	MOVE.B	#MODE1,MRBREG	;SET PARITY, BITS/CHAR
	MOVE.B	#MODE2,MRBREG	;SET STOP BIT LENGTH
	MOVE.B	#BAUDB,CSRBREG	;SET BAUD
	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)
	LEA	VARS+MASK681,A0
	ORI.B	#RXBBIT,(A0)	;ENABLE RECEIVER INTERRUPT
	MOVEQ	#TXBBIT,D0
	TST.W	VARS+AUXOUT+SIZE;IF TRANSMIT QUEUE NOT EMPTY
	BEQ.S	ICHB10
	MOVE.W	#0AAAAH,70002H;==========DIAGNOSTIC
;=====	OR.B	D0,(A0)		;  THEN ENABLE TRANSMITTER INTERRUPT
;=====	BRA.S	ICHB20
ICHB10	NOT.B	D0		;  ELSE DISABLE TRANSMITTER INTERRUPT
	AND.B	D0,(A0)
ICHB20	MOVE.B	(A0),IMRREG	;SEND INTERRUPT MASK TO 68681
	RTS
;------------------------------- INITIALIZE PRINTER PORT
INIPRN
	LEA	VARS+PRNOUT,A0	;RESET OUTPUT QUEUE
	CLR.B	HEAD(A0)
	CLR.B	TAIL(A0)
	CLR.W	SIZE(A0)
	SF	VARS+PRNACK	;NO ACKNOWLEDGE
	MOVE.B	#0,STB1REG	;PUT OUT NULL CHARACTER
	MOVE.B	#0,STB0REG	;/STROBE GOES ACTIVE
	RTS

	END	;BIOS.INI
