'******************************************************************************
'* 
'* PC/286-clone ISA MOTHERBOARD v.1.0 (MOTHERBOARD DEVICE)
'*
'* Supported platform/bus: X86
'*
'* Adapters: CGA
'* 
'* Version history:
'*  - v.1.0 by WadiM (initial emulation)
'*
'****************************************************************************** 

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

//parent PC/AT chipset implementation
public use object CHIPSET_PCAT

//motherboard name
DeviceName="PC/AT 286/AMI Motherboard"
DebugName="PC286_AMIMB"

//variables
dim i as integer

//objects
use object FILE_STREAM as file1 
use object FILE_STREAM as file2 

'--------------------------------- Adapters -----------------------------------

//CGA video adapter
public use object VID_CGA as CGA : AddISADevice(CGA,-1,-1)
pc.WritePort(0x3D4)=CGA.CRT_INDEX_PORT : pc.ReadPort(0x3D4)=CGA.CRT_INDEX_PORT
pc.WritePort(0x3D5)=CGA.CRT_DATA_PORT  : pc.ReadPort(0x3D5)=CGA.CRT_DATA_PORT
pc.WritePort(0x3D8)=CGA.MODE_PORT      : pc.ReadPort(0x3D8)=CGA.MODE_PORT
pc.WritePort(0x3D9)=CGA.COLOR_PORT     : pc.ReadPort(0x3D9)=CGA.COLOR_PORT
pc.WritePort(0x3DB)=CGA.PEN_RESET_PORT : pc.WritePort(0x3DC)=CGA.PEN_SET_PORT
pc.ReadPort(0x3DA)=CGA.STATE_PORT
for i=0xB8000/1024 to (0xBFFFF/1024)-1 //CGA video memory
 mem.KBReader(i)=CGA.ReadMemory : mem.KBWriter(i)=CGA.WriteMemory 
next

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

//Handler of non-phisycal memory reading (address bigger than memory size)
function ExternalMemoryReader(Addr as dword, byref Value as byte) as boolean
 if Addr>=0xFC0000 then //BIOS at top of address space
    dim pos as int64=mem.Pos : Value=mem.Byte(Addr-0xF00000)
    mem.pos=pos : result=true
 else : result=false : end if
end

//Device initialization
protected function DEV_INIT(stream as object,byref EventFreq as integer) as boolean
 dim i as integer, j as integer, s as string
 
 'parent call
 if not DEV_INIT(stream,EventFreq) then exit(false)

 'top of address space remapping
 mem.ExternalReader=ExternalMemoryReader

 'load main ROM BIOS files (byte-interleaved pairs)
 s="bin\at110387" : file1.Open(s+".0") : file2.Open(s+".1")
 if file1.Size<>file2.Size then Error("Sizes of paired ROM files must be equal")
 mem.Pos=0x100000-file1.Size*2 : for i=0 to file1.Size-1
 mem.Byte=file1.Byte : mem.Byte=file2.byte : next
 file1.Close : file2.Close

 'some POST hacks (disable some checks)
 mem.Byte(0xF8240)=0xEB 'checksum
 mem.Word(0xFF120)=0x9090 'dma (step 55)

 'mark non-conventional memory area as read-only 
 for i=0xA0000/1024 to 0xFFFFF/1024 : mem.KBAccess(i)=memReadOnly : next
 'mark CGA memory as read-write 
 for i=0xB8000/1024 to 0xBFFFF/1024 : mem.KBAccess(i)=memReadWrite : next

 'success 
 result=true
end
