;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;
;	D O R A D O   B a s e B o a r d   D e b u g g e r
;
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

;	filed on DoradoBaseDebug.masm
;	E. McCreight
;	last modified December 7, 1981  10:46 AM

	.EXPORT	PacifyWatchdog,SetProblem,ClearProblem
	.EXPORT	CallPtr
	
	.IMPORT	PowerUpMinus5And12,PowerUpAllSupplies,PowerUpDisk
	.IMPORT	DisconnectMufMan
	.IMPORT	StdClockSpeedH,StdClockSpeedL
	.IMPORT	MidasSetup
	.IMPORT	InitEnvironment,DoAllConversions
	.IMPORT	SetManifold,Problems

	.PREDEFINE "MCS6502PREDEFS.SR"
	.GETNOLIST	"DoradoIO.mdefs"

;	Reset Pointers

	.LOC	0fffc
	.ADR	Reset
	.ADR	Interrupt

;	V a r i a b l e s

	.LOC	ResetData
	.SHORT	ResetData

CallPtr:
	.BLK	2

WatchdogTemp:
	.BLK	1


	.LOC	ResetCode

Interrupt:
	RTI

SetProblem:
ClearProblem:
	RTS

;	This is the reset code that gets run whenever the circuit
;	breakers are turned on or the little button on the front
;	panel is pushed.

Reset:
	CLD

;	Clear all the bytes in page 0.

	LDXI	0
	LDAI	0

ClearMoreBytes:
	STAX	0
	DEX
	BNE	ClearMoreBytes

;	Set up the stack and prepare for interrupts.

	CLD
	LDXI	0ff
	TXS
	JSR	MidasSetup

;	Set the data direction registers in the I/O ports, after first
;	setting some output registers so their critical bits will
;	remain high

	LDXI	0ff	; set for all output bits
	STX	DAC+DDR
	STX	MCPBusH+DDR

	INX	; set for all input bits
	STX	CPRegL+DDR
	STX	CPRegH+DDR

	LDXI	ISel	; set for mixtures
	STX	Comparators+DDR

	LDXI	MASync+Trap'
	STX	CPIBus+DDR

	LDXI	LampOff
	STX	MiscByte
	STX	MiscByte+DDR

	LDXI	80+MCPABus+MCPStrobe
	STX	MCPBusL+DDR

	LDXI	WatchdogDDRValue
	STX Watchdog+DDR

;	Now initialize the voltage, current, and temperature vectors.

	JSR	InitEnvironment

;	Power sequencing. First make sure our own supply is in spec.

RebootDorado:
	JSR	PacifyWatchdog
	JSR	DoAllConversions

CheckVCC:
	LDXI DisconnectMufMan
	JSR	SetManifold	; force IO reset

;	Now start up the Ecl supplies.

	LDXI	PowerUpAllSupplies
	JSR	SetManifold

SuppliesAllUp:

;	Set the standard clock speed

	LDXI	StdClockSpeedH
	JSR	SetManifold
	LDXI	StdClockSpeedL
	JSR	SetManifold

Continuous:
	JSR	DoAllConversions

	LDAI	0
	STA	Problems

	LDAI	0e0,#LowAddrByte	; standard place
	STA	CallPtr
	LDAI	0e0,#HighAddrByte
	STA	CallPtr+1

Spin:
	LDX	Problems	; this allows debugging subroutines, etc.
	BEQ	Routine0
	DEX
	BEQ	Routine1
	DEX
	BEQ  Routine2
	DEX
	BEQ  Routine3

	JSR	CallIt
	JSR	PacifyWatchdog
	JMP	Spin

;Run the subroutine 0,1,2,or 3, if the location in
;Problems is = to a 0,1,2 or 3. if Problems is larger than
;3 than CallPtr and CallPtr+1 should point to
;the subroutine that you are using for debugging.
;The location Problems is preset to E0.

CallIt:
	LDA	CallPtr+1		 
	PHA		
	LDA	CallPtr 
	PHA	
	PHP
	RTI
;Run this routine if Problems is=to a 0
;This routine will Pacify the watchdog timer and return to spin 

Routine0:
	JSR	DoAllConversions		 
	JSR  PacifyWatchdog
	JMP  Spin

;Run this routine if Problems is=to a 1
;This routine will increment the dac from 0 to 256

Routine1:
	INC	DAC						
	JSR  PacifyWatchdog	 
	JMP  Spin
	
;Run this routine if Problems is=to a 2
;This routine will turn all of the dac bits on and then off.

Routine2:
	LDAI 0
	STA  DAC
	LDAI 0ff
	STA  DAC	
	JSR  PacifyWatchdog	  
	JMP  Spin

;Run this routine if Problems is=to a 3	
;This routine will turn the lite on and if we exit midas the 
;watch dog timer should reboot the dorado and turn off the lite

Routine3:
	LDAI  0
	STA   MiscByte				 
	JMP   Routine3		 

	
OnceOnly:
	LDAI	0
	STA	Problems
	RTS

;	Let the watchdog timer know we're still alive by copying
;	WatchdogIn to WatchdogOut every once in a while.

PacifyWatchdog:
	LDA	Watchdog
	ANDI	WatchdogIn
	RORA	; carries WatchdogIn to WatchdogOut
	STA	WatchdogTemp
	SEI
	LDA	Watchdog
	ANDI	0ff-WatchdogOut
	ORA	WatchdogTemp
	STA	Watchdog
	CLI
	RTS

	.END