'******************************************************************************
'* 
'* PC/X86 ISA BUS v.1.0 (SYSTEM BUS DEVICE)
'*
'* ISA is 8/16-bit system bus, introduced in early x86 PCs
'*
'* Supported platform/bus: X86
'* 
'* Version history:
'*  - v.1.0 by WadiM (initial emulation)
'*
'* S/W Interface (extended to manupulate ISA-bus devices): 
'*  - AddISADevice(device,irq,dma,port) - add ISA device (integer result)
'*
'* DEVICE Interface (inherited)
'* DEVICES Interface (inherited)
'*
'****************************************************************************** 

//parent device set implementation
public use object DEVSET

//bus name
DeviceName="PC/X86 ISA Bus"
DebugName="ISA_BUS"

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

//Add ISA-device
public function AddISADevice(device as object, irq as integer=-1, dma as integer=-1, _
                port as integer=-1) as integer

 dim i as integer, j as integer, temp_device as object

 //IRQ configuring
 if IRQ>=0 then 

    'configuring param
    j=device.DEVPARAMS.IndexOfName("IRQ")
    if j<0 then Error("IRQ not supported by device "+device.DeviceName)
    device.DEVPARAMS.Value(j)=irq 
    device.DEVPARAMS.Configured(j)=true

    'check and inform about irq conflicts
     for i=0 to DeviceCount-1 
      temp_device=GetDevice(i)
      j=temp_device.DEVPARAMS.IndexOfName("IRQ")
      if (j>=0) and (temp_device.DEVPARAMS.Configured(j)) and _
         (temp_device.DEVPARAMS.Value(j)=irq) then
         ? DebugPrefix+"Conflicting IRQ=";irq;" (";
         ? device.DeviceName;" with ";temp_device.DeviceName;")"
     end if : next

 end if

 //DMA configuring
 if DMA>=0 then 

    'configuring param
    j=device.DEVPARAMS.IndexOfName("DMA")
    if j<0 then Error("DMA not supported by device "+device.DeviceName)
    device.DEVPARAMS.Value(j)=dma : device.DEVPARAMS.Configured(j)=true

    'check and inform about dma conflicts
     for i=0 to DeviceCount-1 : temp_device=GetDevice(i)
      j=temp_device.DEVPARAMS.IndexOfName("DMA")
      if (j>=0) and (temp_device.DEVPARAMS.Configured(j)) and _
         (temp_device.DEVPARAMS.Value(j)=dma) then
         ? DebugPrefix+"Conflicting DMA=";dma;" (";
         ? device.DeviceName;" with ";temp_device.DeviceName;")"
     end if : next

 end if

 //PORT configuring
 if PORT>=0 then 

    'configuring param
    j=device.DEVPARAMS.IndexOfName("PORT")
    if j<0 then Error("PORT not supported by device "+device.DeviceName)
    device.DEVPARAMS.Value(j)=port : device.DEVPARAMS.Configured(j)=true

    'check and inform about dma conflicts
     for i=0 to DeviceCount-1 : temp_device=GetDevice(i)
      j=temp_device.DEVPARAMS.IndexOfName("PORT")
      if (j>=0) and (temp_device.DEVPARAMS.Configured(j)) and _
         (temp_device.DEVPARAMS.Value(j)=port) then
         ? DebugPrefix+"Conflicting PORT=";port;" (";
         ? device.DeviceName;" with ";temp_device.DeviceName;")"
     end if : next

 end if

 'add device to bus
 result=AddDevice(device) : if result<0 then 
 Error(DebugPrefix+"Can not add sub-device "+device.DeviceName) : end if

end