'******************************************************************************
'* 
'* COMBO-DEVICE IMPLEMENTATION v.1.0 (can be used by chipsets, buses, m/boards)
'*
'* Version history:
'*  - v.1.0 by WadiM (initial release)
'*
'* S/W Interface (to manupulate devices in user-friendly manner): 
'*  - AddDevice(device) - add device (integer result)
'*  - RemoveDevice(devindex) - remove device
'*  - RemoveAllDevices - remove all devices
'*  - DeviceCount -  count of devices (integer result)
'*
'* DEVICE Interface (implemented)
'*  - DEV_INIT(stream,EventFreq) - device initialization (boolean result)
'*  - DEV_DONE(stream) - device finalization
'*  - DEV_LOAD(stream) - load device state from stream (boolean result)
'*  - DEV_SAVE(stream) - save device state to stream (boolean result)
'*  - DEV_EXISTS(name,group) - check device existance (boolean result)
'*
'* DEVICES Interface (inherited)
'*
'****************************************************************************** 

//supported interfaces (device and device collection)
public use object DEVICE : public use object DEVICES

//devset name
DeviceName="UNKNOWN DEVICE SET" 
DebugName="UNK_DEVSET"

'--------------------------------- S/W Interface ------------------------------

//Add device
public function AddDevice(device as object) as integer
 result=DEVS_ADD(device) : if result<0 then 
 Error("Can not add device "+device.DeviceName) : end if
end

//Get device
public function GetDevice(devindex as integer) as object
 result=DEVS_GET(devindex)
end

//Remove device by index
public procedure RemoveDevice(devindex as integer)
 DEVS_REMOVE(devindex)
end

//Remove all devices
public procedure RemoveAllDevices
 dim i as integer : for i=0 to DEVS_COUNT-1 
 DEVS_REMOVE(i) : next
end

//Count of devices
public function DeviceCount as integer
 result=DEVS_COUNT
end

'------------------------------- DEVICE Interface -----------------------------

//Device initialization
public function DEV_INIT(stream as object,byref EventFreq as integer) as boolean
 dim i as integer, Freq as integer, device as object
 for i=0 to DEVS_COUNT-1 : device=DEVS_GET(i) : Freq=EventFreq 
 if not(device.DEV_INIT(stream,Freq)) then 
    Error("Can not initialize device "+device.DeviceName+" of "+DeviceName) 
//    Error(DeviceName) 
 else
 end if 
 if EventFreq<Freq then EventFreq=Freq : next 
 result=true
end

//Device finalization
public procedure DEV_DONE (stream as object)
 dim i as integer, device as object
 for i=0 to DEVS_COUNT-1 
 device=DEVS_GET(i) 
 device.DEV_DONE(stream) 
 next
end

//Load device state
public function DEV_LOAD(stream as object) as boolean 
 dim i as integer, device as object
 for i=0 to DEVS_COUNT-1 : device=DEVS_GET(i)
 if not(device.DEV_LOAD(stream)) then 
 Error("Can not load state of device "+device.DeviceName+" of "+DeviceName)
 end if : next : result=true
end

//Save device state
public function DEV_SAVE(stream as object) as boolean
 dim i as integer, device as object
 for i=0 to DEVS_COUNT-1 : device=DEVS_GET(i)
 if not(device.DEV_SAVE(stream)) then 
 Error("Can not save state of device "+device.DeviceName+" of "+DeviceName)
 end if : next : result=true
end

//Check device existance (empty params is not used)
public function DEV_EXISTS(name as string, group as string) as boolean
 dim i as integer, device as object
 for i=0 to DEVS_COUNT-1 : device=DEVS_GET(i)
 if device.DEV_EXISTS(name,group) then exit(true)
 next : result=false
end
