;AutoVIDC module source - Change Log
;-----------------------------------
;
;VIDC Enhancer software control
;Best suited to Aux IO controlled VIDC Enhancers
;
;Author: Andreas Barth (1995)
;Author: Paul Vernon (Retro-Kit) (2012-2015)
;   www: http://www.retro-kit.co.uk
;
; Schaltet die Leitung AUX1 auf aktiv-1 in bestimmten Bildschirm modi.
; Das Originalprogramm musste unbedingt umgebaut werden, weil es nicht bei Mode 12 oder aehnlichen zugeschaltet hat und nicht konfigurierbar war.

; Version 0.99
; - Ruft auch bei Init den Modus ab.
; - Untersucht eine interne Tabelle und stellt danach Aux1 ein.

; Version 1.00
; - Befehle eingebaut um Konfiguration abzufragen und zu aendern.
; - Hardware wird geprueft, ob der richtige Rechner vorliegt.
;   Hardwarepruefung durch OS_ReadSysInfo 2 (PRM 1-722)
;   Gibt's nicht im RO2 --> alte Hardware automatisch.
;   Wenn vorhanden, dann muessen R0-R4 = 0 sein, sonst neue Hardware.

; Version 1.02 - Updated by Paul Vernon
; - Renamed to AutoVIDC+
; - Added in the ability to:
; -    1. Control up to two VIDC Enhancers at the same time.
; -    2. Store three seperate Modelists.
; -    3. Edit TWO of the Modelists
; - Uses the third ModeList under RISC OS 3 for MonitorTypes 3 and 4 to enhance non-VGA modes;apped to VGA ones.
; - This version provides full transparent control of two VIDC Enhancers to provide full A5000 screen mode capabilities.

; Version 1.03
; - Fixed an 'infinite loop' bug when applications created custom modes.

; Version 1.04
; - Added in the ability to:
; -    1. Actively turn the VIDC Enhancer boards off until the next mode change
; -    2. Re-initialise the VIDC Enhancer boards to check and start the correct VIDC Enhancer clock without a mode change.
; -    3. Include a way to set AutoVIDC+ to ignore specific modes - This allows modules created by CustomVDU to control the VIDC Enhancer boards without clashing with AutoVIDC+

; Version 1.05
; - Added the ability to completely suspend and re-enable AutoVIDC+
; - Added the command AutoVIDCStatus which reports Monitor Type, MODE and AutoVIDC+ status and VIDC clock speed.
; - Fixed a minor bug when setting the Aux IO lines ensuring the other line is disabled. This opens up the possibility of a latched VIDC Enhancer that could control three extra oscillators.
; - Moved the commands AutoVIDCSuspend and AutoVIDCReset
; - Altered the code so that it compiles correctly due to larger size of the module now. This involved changing all ADR dirtectives to ADRL and using ExtBas to patch the BASIC assembler.
; - Added the AutoVIDCTogglePOST command to toggle the VIDC POST check on and off. It toggles the correct bit but appears not to be implemented by RISC OS...

; Version 1.06 - never released
; - Added support for three Oscillators allowing 24MHz, 25.175MHz, 36MHz and an overclocked oscllator to be controlled. This requires a suitable VIDC Enhancer such as the Retro-Kit Ultra VIDC Enhancer.
; - Defaulted the overclocking ability to OFF so it has to be explicitly enabled by the user for it to work.
; - fixed a couple of minor output bugs in AutoVIDCStatus
; - Added in the SWI handler and a couple of SWI calls to allow control of the VIDC Enhancer to be delegated to a third party application.

; Version 2.00
; - Official release marking the allocation of the modname, *commands and SWI chunk base.
; - Reverted to Module Name AutoVIDC as this is now officially allocated by RISC OS Open Ltd.

; Version 2.01
; - Added the RISC OS clock selection feature. It checks the clock speed in the VIDC register and if all the MODE lists do not specify that MODE but AutoVIDC recognises the clock speed then it will automatically switch clock speeds.
; - Added * commands to enable and disable RISC OS clock selection
; - NOTE: RISC OS clock selection is only available from RISC OS2.01 up.
; - NOTE: The RISC OS clock selection feature could do away with MODE lists but RISC OS 2.00 would then be unsupported and if a user wanted to FORCE a specific clock speed, it couldn't be done.
; - Ensured that all access to LatchB is in SVC mode with IRQ and FIQ disabled.

; Version 2.02 - not released
; - Re-factored the source code and modified to compile with ExtAsm.
; - minor bug fixes 

; Version 2.03 - not released
;
; - many thanks to Jon Abbott and Steve Harrison for helping out and providing code
; - examples and ideas that have made AutoVIDC better than I could have hoped for
; :-)
;
; - Finally working on RISC OS 2. It seems RISC OS 2 support has never worked until now.
; - 26/32-bit neutral. This isn't really needed but it's nice to have :D
; - Added in a 25.175MHz mode list definition for RISC OS 2 as it has different screen geometry
; - added in IRQ/FIQ disabling code to protect R/W of the Aux IO latch hardware.

; Version 2.04
;
; - added in support for running on later machines with VIDC1(a) chips installed.
; - fixed RISC OS 3 25.175MHz MODE map by adding MODE 25 into the list and moving MODE 41-43 into the list from the VGA MODE map.
; - fixed the OS version detection code.
; - Built in hardware detection to detect VIDC1(a) machines that do and do not require  
;   a third party enhancer e.g. A540's and A5000's don't require one   
; - When setting the VIDC clock, where necessary, the Zero page location containing the
;   clock speed in kHz is updated to allow games that calculate sound playback do so
;   more accurately. >= RO3
; - Allow the user to set the speed of the over-clocking oscillator in kHz up-to 60000kHz (60MHz)
;
; - Now provides a standard way of setting the VIDC clock speed across all VIDC1 based Acorn machines.

; Version 2.05
;
; - added an SWI to return the Sync.Polarity details for any given Mode and MonitorType
; - fixed an issue with correctly setting the Sync.Polarity when using the SetClock SWI on "native" enhanced VIDC1a based computers such as the A5000.

; Version 2.06
;
; - added an SWI to allow AutoVIDC to be re-initialised to match the current MODE without requiring a MODE change service call to be triggered.
; - Fix the DelegateControl SWI so that when control is returned to AutoVIDC, it calls the new re-initialise SWI also.

; Version 2.07
;
; - added in *MODE command to allow mode changes at the supervisor prompt
; - Introduced support for detecting WE VIDC Enhancers

; Version 2.08 - beta release
;
; - Added full support for WE "Super" VIDC Enhancer with Sync. Polarity
; - re-factored some source code to reduce module size
; - fixed a minor bug with the WE Enhancer code
; - fixed an issue with the AutoVIDCXXset command
;

; Version 2.09 - beta release
;
; - Added clock detection code
; - Added an SWI to query clock availability
; - Re-arranged output for AutoVIDCStatus 
;     - also indicates whether Aux IO VIDC Enhancer 
;       has been detected or is assumed
; - altered code to use more "local" labels
; - fixed bug in cleanup code for Native Enhancer support
; - removed two "unpredictable" pieces of code
; - Added/Removed TEMP/LOCK compiler directives
; - code optimised to reduce build size - thanks to Jon Abbott 
;     for the quick lesson in optimising ARM code :D
; - fixed a bug when enabling RO control after being disabled 
;     allowing the clock speed to be restored correctly
; - now initiates a call to Service_ModeChange *after* the module
;     has initialised. This addresses an issue where if QTM is
;     playing a module before AutoVIDC has initialised it now
;     correctly adjusts its timings after AutoVIDC has initialised.
; 

; Version 2.10 - full release
;
; - removed unused variable definition
; - added more comments in the code

; Version 2.11 
;
; - Added SWI AutoVIDC_EnhancerPresent
;	- Returns a value of 0 or 1 depending on whether an enhancer has been detected or not.
; - Added SWI AutoVIDC_EnhancerType
;	- Returns a value of 0, 1 or 2 for Acorn, Aux I/O or I2C enhancers respectively.
;	- Widened the range for detecting the clocks to address a problem with clock 
;          detection when in high bandwidth modes (e.g. 28) on all machines and
;	   on the A5000 in any mode.
;

; Version 2.12
; 
; - Increased delay of initiating AutoVIDC after module load from floppy.
;

; Version 2.13 - unreleased
;
; - changed all SWI to be X versions
; - changed all LDR/STR ..,[Rx] to [Rx,#0], to avoid post-indexing
; - _useWEVIDCEnhancer was setting Watford Enhancers to a non-existant clock, when selecting 25.175MHz
; - changed code that exits with an error to use an error exit function
; - changed help and syntax text to follow RISC OS conventions
; - changed the SWI jump table to use the normal convention
; - optimized _useAuxVIDCEnhancer
; - reordered code to remove auto-expansion
; - various code optimizations
; - Removed all TEMP/LOCK compiler directives
; - made vars and modeLists relative to R12
; - when it raised Service_ModeChange, it wasn't setting R2/R3
; - simplified the OS version detection
; - switched the DMA count code to hang off the SSBC IRQ hardware vector, instead of using a Sound Channel Handler
; - intermixed some local strings into the code, and aligned all other strings to avoid auto-expansion
; - getClockSpeedInkHz was corrupting R0, causing *AutoVIDCStatus to trigger an abort after calling AutoVIDC_SetClockSWI for the current VIDC clock speed when the Enhancer type is "Acorn VIDC Enhancer"

; Version 2.14
;
; - now ROM capable.  Moved vars and modeLists out of the Module
; - changed LatBShadow Page Zero references to be relative to 0, to avoid auto-expansion
; - moved duplicated IIC related code into functions
; - additional code optimizations
; - changed the machine detection from using the RAM speed, to checking the I/O chip type allowing AutoVIDC to work on overclocked machines
; - the Service handler no longer disables IRQ/FIQ
; - moved various flags into a bitwise flag variable
; - moved various strings in-line and changed to OS_WriteS
; - AutoVIDC_SetClockSWI no longer disables FIQ
; - removed the Service_ModeChange issued at the end of the Module init, it now determines which clock was active when it was loaded and sets it back avoiding the need for it
; - changed the Monitor/Mode detection to cover RO2 thru RO3.11
; - It no longer tries to guess the machine/OS capabilities.  It now tries changing the clock using Watford, Native OS, AuxIO in that order and uses the first that can actually change the clock...setting it's internal variables accordingly.
; - rewritten the Watford Super VIDC Enhancer code
; - VIDC clocks are now timed twice, with the lowest timing determining the clock speed.  Sample periods used and accuracy are as follows:
;   Clock      Sample period  Accuracy (+/-)
;   24Mhz      2560uS         5KHz
;   25.175MHz  2441uS         5KHz
;   36MHz      1407uS         10kHz
;   60MHz      1024uS         30kHz
; - changed AutoVIDC_SetClock SWI with R0=-1 to return clock 0 if it's running on RO2 (the OS doesn't track the VIDC clock speed)
; - Module is no longer 32bit compliant - it requires VIDC1, which is 26bit only
; - added a check for Arculator as it doesn't implement I2C correctly
; - added a check for Red Squirrel as it doesn't implement timers correctly