'******************************************************************************
'* 
'* PC/AT ISA CHIPSET (CHIPSET DEVICE)       !!! WITH INT13 (FDD/HDD) HACK
'*
'* Supported platform/bus: X86
'*
'* Chipset, used in original x86 PC/AT (model 5170) and clones
'*
'* 2 PIC i8259, PIT i8253, 2 DMA i8237, FDC i8272, KBC i8042
'* 
'* Version history:
'*  2007,2008 - initial emulation (by WadiM)
'*
'****************************************************************************** 

//DEBUG.ON 'uncomment to enable debug messages (can be slow for hi-freq events)

//parent chipset implementation (w/o hacks, i.e. pure hardware)
public use object "set_at_h"

'------------------------------------ Hacks -----------------------------------

//HACK: Capture cpu's "int 13" to emulate FDD/HDD (because lack of h/w FDD emulation)
//Bootstap loader int19h used to detect change int13 handler to emulated one, 
//because BIOS already set interrupt vectors and OS not captured them yet

use module BIOS_SPCX86_PATH+"eint_13h" as INT13 : pc.CallEmuInt(0x13)=INT13.EINT_13H

public function EINT_19H as boolean
 dim pos as dword=shl(mem.Word(0x13*4+2),4)+mem.Word(0x13*4) //handler address
 if pos+5<mem.Size then : mem.pos=pos 'trying to change handler
  dim i=pos/1024 'remove posible write protetion (in case of ROM)
  dim a as byte=mem.KBAccess(i) : mem.KBAccess(i)=3 : dim b as byte
  if (i+1)*1024<mem.Size then : b=mem.KBAccess(i+1) : mem.KBAccess(i+1)=3 : end
  mem.Bytes=Array(0xF1,0x13)                        'EINT 13h
  mem.Bytes=Array(0xFB)                             'STI
  mem.Bytes=Array(0xCA,0x02,0x00)                   'RETF 0002h
  mem.KBAccess(i)=a : if (i+1)*1024<mem.Size then mem.KBAccess(i+1)=b
 end if
 result=false 'need real int19 processing
end

pc.CallInt(0x19)=EINT_19H //BMHACK

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

//Device initialization
public function DEV_INIT(stream as object,byref EventFreq as integer) as boolean
 
 'parent call
 if not DEV_INIT(stream,EventFreq) then exit(false)

 'success
 result=true

end


