;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;
;	D O R A D O   C o n t r o l   P r o g r a m
;
;		A n a l o g   I n t e r f a c e   C o d e
;
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

;	filed on DoradoAnalog.masm
;	E. McCreight
;	last modified July 30, 1980  11:31 AM

	.EXPORT	InitEnvironment
	.EXPORT	DoAllConversions,ADConvert
	.EXPORT	VoltageConversions,CurrentConversions
	.EXPORT	TemperatureConversions
	.EXPORT	VIConvSetup,DoCurrentConversion
	.EXPORT	NextBitTest

	.IMPORT	PowerOutOfSpec
	.IMPORT	VoltageTable,CurrentTable,EnvironmentLen
	.IMPORT	VDD,VEE,VTT,VCC
	.IMPORT	IDD,IEE,ITT,ICC
	.IMPORT	Thermometers
	.IMPORT	BaseTemp,CBTemp,CATemp
	.IMPORT	ProcLTemp,ProcHTemp,IFUTemp
	.IMPORT	MemCTemp,MemXTemp,MemDTemp
	.IMPORT	CurEnvironment,FirstEnvironment
	.IMPORT	MinEnvironment,MaxEnvironment
	.IMPORT	EclUpMuffler,ReadMuffler

	.SHORT	PowerOutOfSpec
	.SHORT	VoltageTable,CurrentTable,EnvironmentLen
	.SHORT	VDD,VEE,VTT,VCC
	.SHORT	IDD,IEE,ITT,ICC
	.SHORT	Thermometers
	.SHORT	BaseTemp,CBTemp,CATemp
	.SHORT	ProcLTemp,ProcHTemp,IFUTemp
	.SHORT	MemCTemp,MemXTemp,MemDTemp
	.SHORT	CurEnvironment,FirstEnvironment
	.SHORT	MinEnvironment,MaxEnvironment

	.IMPORT	InhibitMidas,EnableMidas
	.IMPORT	SetMufflerAddress,MufflerAddress
	.IMPORT	ThermomMufAddr,MaxThermom

	.PREDEFINE "MCS6502PREDEFS.SR"
	.GETNOLIST	"DoradoIO.mdefs"

;			V a r i a b l e s

	.LOC	AnalogData
	.SHORT	AnalogData

;	Analog To Digital Conversion Variables

ComparatorsPtr:	.BLK	2
ComparatorMask:	.BLK	1
NextBitTest:	.BLK	1
SettlingDelay:	.BLK	1
NormalIOffset:	.BLK	1
IOffset:	.BLK	1
CurrentOutOfSpec:	.BLK	1
VoltageOutOfSpec:	.BLK	1
ThermomIndex:	.BLK	1

	.LOC	AnalogCode

;			C o n s t a n t   T a b l e s

VoltageMaxTable:
	0e0	; 340 octal
	0e0
	0e0
	0e0

VoltageMinTable:
	0a0	; 240 octal
	0a0
	0a0
	0a0


CurrentMaxTable:
	0ff
	0ff
	0ff
	0ff

CurrentMinTable:
	0
	0
	0
	0

;	Subroutine to initialize the environment.

InitEnvironment:
	LDXI	EnvironmentLen-1

MoreEnvironment:
	LDAI	0
	STAX	MaxEnvironment
	LDAI	0ff
	STAX	MinEnvironment
	STAX	CurEnvironment
	DEX
	BPL	MoreEnvironment

	JSR	DoAllConversions

	LDXI	EnvironmentLen-1

MoreFirstEnvironment:
	LDAX	CurEnvironment
	STAX	FirstEnvironment
	DEX
	BPL	MoreFirstEnvironment
	RTS


;	Subroutine to do all A/D conversions and store
;	the results in memory

DoAllConversions:
	JSR	VoltageConversions
	JSR	CurrentConversions
	JSR	TemperatureConversions

	LDXI	EnvironmentLen-1

CheckEnvironmentLimits:
	LDAX	CurEnvironment
	CMPX	MinEnvironment
	BCS	MinOK
	STAX	MinEnvironment

MinOK:
	CMPI	0ff	; this is a special value
	BEQ	MaxOK

	CMPX	MaxEnvironment
	BCC	MaxOK
	STAX	MaxEnvironment

MaxOK:
	DEX
	BPL	CheckEnvironmentLimits

	LDA	CurrentOutOfSpec
	LSRA
	LSRA
	LSRA
	LSRA
	ORA	VoltageOutOfSpec
	STA	PowerOutOfSpec
	RTS

VoltageConversions:
	LDAI	0
	STA	VoltageOutOfSpec
	LDAI	CVEE
	JSR	VIConvSetup

AnotherVoltageConversion:
	JSR	ADConvert
	STAX	VoltageTable
	CMPX	VoltageMinTable
	BCC	VoltageIsOutOfSpec
	CMPX	VoltageMaxTable
	BCC	VoltageInSpec

VoltageIsOutOfSpec:
	SEC

VoltageInSpec:
	ROR	VoltageOutOfSpec

	ASL	ComparatorMask
	DEX
	BPL	AnotherVoltageConversion

	RTS


VIConvSetup:
	STA	ComparatorMask
	LDAI	Comparators↑-8
	STA	ComparatorsPtr+1
	LDAI	Comparators&0ff
	STA	ComparatorsPtr
	LDXI	3
	RTS

CurrentConversions:
	LDAI	0
	STA	CurrentOutOfSpec
	LDAI	CI
	JSR	VIConvSetup

	LDAI	IGndMask
	JSR	DoCurrentConversion
	STA	NormalIOffset

	LDAI	IVEEMask
	JSR	DoCurrentConversion
	STA	IOffset

	LDAI	IEEMask	; set the analog mpx

AnotherCurrentConversion:
	JSR	DoCurrentConversion
	SEC
	SBC	IOffset
	BCS	RecordCurrent

	LDAI	0	; current went negative, set to 0

RecordCurrent:
	STAX	CurrentTable
	CMPX	CurrentMinTable
	BCC	CurrentIsOutOfSpec
	CMPX	CurrentMaxTable
	BCC	CurrentInSpec

CurrentIsOutOfSpec:
	SEC

CurrentInSpec:
	ROR	CurrentOutOfSpec

	LDA	NormalIOffset
	STA	IOffset	; everything but IEE is Gnd-referenced

	LDA	Comparators	; leave new analog mpx addr in A
	ANDI	ISel
	SEC
	SBCI	IDelta	; leave result in A

	DEX
	BPL	AnotherCurrentConversion
	RTS


DoCurrentConversion:
	STA	Comparators
	LDAI	0b	; about 100 us
	STA	SettlingDelay

MoreCurrentSettlingTime:
	DEC	SettlingDelay
	BNE	MoreCurrentSettlingTime

	JSR	ADConvert
	RTS

;	There are two kinds of thermometers. One, typified by
;	that on the Base board, is directly sensible by an I/O
;	pin of the microcomputer. The other, typified by
;	that on the MemD board, is sensible only through the
;	muffler system, which is not always controllable by
;	the base board. In the latter case, if the base board is
;	not in control, the measurement is not recorded.

TemperatureConversions:
	LDA	VoltageOutOfSpec
	ANDI	70	; +5v, -2v, -5v must be OK
	BNE	UnknownTemp

	LDAI	MiscByte,#HighAddrByte	; do direct conversions
	STA	ComparatorsPtr+1
	LDAI	MiscByte,#LowAddrByte
	STA	ComparatorsPtr

	LDAI	TBaseTempSense
	JSR	ADConvertMaskInA
	STA	BaseTemp

	LDAI	TCBTempSense
	JSR	ADConvertMaskInA
	STA	CBTemp

	LDAI	CPIBus,#HighAddrByte	; now do indirect conversions
	STA	ComparatorsPtr+1
	LDAI	CPIBus,#LowAddrByte
	STA	ComparatorsPtr

	LDAI	DMuxData
	STA	ComparatorMask
	LDAI	MaxThermom
	STA	ThermomIndex

AnotherThermom:
	LDXI	EclUpMuffler
	JSR	ReadMuffler
	BCS	CantReadThermom	; Dorado running
	BEQ	CantReadThermom	; Backpanel muffler not connected

	LDA	ThermomIndex
	ASLA
	TAY
	LDAY	ThermomMufAddr+1
	STA	MufflerAddress+1
	LDAY	ThermomMufAddr
	STA	MufflerAddress

	JSR	SetMufflerAddress	; obtains CP bus control
	BCS	EnaCantReadThermom	; failed because Dorado was running

	JSR	ADConvert
	JSR	EnableMidas	; releases CP bus control

StoreTemp:
	LDX	ThermomIndex
	STAX	Thermometers

	DEC	ThermomIndex
	LDA	ThermomIndex
	CMPI	CATemp-Thermometers
	BPL	AnotherThermom

	RTS


EnaCantReadThermom:
	JSR	EnableMidas	; releases CP bus control

CantReadThermom:
	LDAI	0
	BEQ	StoreTemp


UnknownTemp:
	LDXI	MaxThermom
	LDAI	0

ZeroAnotherThermom:
	STAX	Thermometers
	DEX
	BPL	ZeroAnotherThermom

	RTS

;	Analog-to-Digital conversion subroutine. Assumes a stable
;	analog signal at a comparator input, an 8-bit DAC at
;	location DAC and a comparator in bit ComparatorMask of location
;	@ComparatorsPtr whose value is 1 if the DAC is more
;	positive than the analog input, and 0 otherwise.
;	Result returned
;	in register A. NextBitTest can serve as an interlock; non-zero
;	means don't mess with DAC.

ADConvertMaskInA:
	STA	ComparatorMask

ADConvert:
	JSR	InhibitMidas	; Midas code messes with DAC register
	LDA	DAC	; save Midas' DAC value
	PHA
	LDAI	0
	TAY
	STA	NextBitTest
	SEC
	BCS	TryNextADBit	; (unconditional)

NextADBit:
	EOR	NextBitTest

	STA	DAC
	LDAI	2	; 2 us
	STA	SettlingDelay	; 4 us

MoreSettlingTime:
	DEC	SettlingDelay	; 6 us
	BNE	MoreSettlingTime	; 3 us (usually)

;	*** Warning: Do not reverse the LDA and the AND. Because
;	of the asynchronism of @ComparatorsPtr, once
;	in a blue moon EQ is true while A#0. This may or may not
;	coincide with an interrupt; I don't know.
	LDA@Y	ComparatorsPtr	; 4 us before load
	AND	ComparatorMask
	BEQ	RecoverDAC

	LDA	NextBitTest

RecoverDAC:
	EOR	DAC

TryNextADBit:
	ROR	NextBitTest
	BNE	NextADBit

	TAY	; recover Midas' DAC register
	PLA
	STA	DAC
	TYA
	JMP	EnableMidas

	.END