;AutoVIDC module source - general functions...
;---------------------------------------------
;
;VIDC Enhancer software control
;For software controlled VIDC Enhancers
;
;Author: Andreas Barth (1995)
;Author: Paul Vernon (Retro-Kit) (2012-2013)
;   www: http://www.retro-kit.co.uk

;Exit:
; R0 = Screen mode
; R1 = Monitor Type
.getMonitorTypeAndMode				; exit R1=monitor type
	STMFD	R13!,{R2,R14}
	MOV	R0,#1
	SWI	XOS_ReadSysInfo
	BVC	_get_mode
						;fall-back method for RO2/3.00
	MOV	R0,#161				; Read CMOS
	MOV	R1,#133				; CMOS address for MonitorType
	SWI	XOS_Byte
	AND	R2,R2,#%01111100
	MOV	R1,R2,LSR#2

._get_mode
	STR	R1,[R13,#-4]!			;stack the monitor type

	MOV	R0,#135				; get the current Mode-number
	SWI	XOS_Byte
	MOV	R0,R2
        LDMFD	R13!,{R1-R2,PC}

;-------------------------------
;IIC functions
;
;In:
; R3 - value to write if a write operation

;Exit:
; R0 - corrupt for write, value returned for read
; R1,R2 - corrupt

.iic_control_write
	LDRB	R0,WEIICAddress
	B	iic_control

.iic_control_read
	LDRB	R0,WEIICAddress
	ORR	R0,R0,#1			; bit 0 set for read
.iic_control
	STMFD	R13!,{R3,R14}
	MOV	R1,R13				; use the stack for the transfer
	MOV	R2,#1
	SWI	XIIC_Control
	LDMFD	R13!,{R0,PC}

;Exit:
; R3=clock speed in KHz
.getClockSpeedInkHz				; exit R3=VIDC Clock speed in kHz
	STMFD	R13!,{R0-R1,R3,R14}
	ADR	R0,_vdublock			; Ok, lets grab the RISC OS clock speed
	ADD	R1,R13,#8			; overwrite R3 on stack
	SWI	XOS_ReadVduVariables		; PRM 1-703 (172)
        LDMFD	R13!,{R0-R1,R3,PC}

._vdublock
	DCD	&AC			;VIDC clock speed in kHz
	DCD	-1


.writeStringNewLine
	STMFD	R13!,{R14}
	SWI	XOS_Write0
	SWI	XOS_NewLine
	LDMFD	R13!,{PC}

.convertCardinal2
	STMFD	R13!,{R1-R2,R14}
	ADR	R1,buffer
	MOV	R2,#12
	SWI	XOS_ConvertCardinal2
	LDMFD	R13!,{R1-R2,PC}
