'******************************************************************************
'* 
'* PC/386 ISA MOTHERBOARD v.1.0 (MOTHERBOARD DEVICE)
'*
'* Supported platform/bus: X86
'*
'* Adapters: VGA
'* 
'* 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/386 Motherboard"
DebugName="PC386_MB"

//variables
dim i as integer

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

//VGA video adapter
public use object VID_VGA as VGA : AddDevice(VGA)
for i=0x3C0 to 0x3CF : pc.WritePort(i)=VGA.PORTS : pc.ReadPort(i)=VGA.PORTS : next
for i=0x3D0 to 0x3DF : pc.WritePort(i)=VGA.PORTS : pc.ReadPort(i)=VGA.PORTS : next
pc.WritePort(0x3B4)=VGA.PORTS : pc.ReadPort(0x3B4)=VGA.PORTS
pc.WritePort(0x3B5)=VGA.PORTS : pc.ReadPort(0x3B5)=VGA.PORTS
pc.WritePort(0x3BA)=VGA.PORTS : pc.ReadPort(0x3BA)=VGA.PORTS
for i=0xA0000/1024 to (0xBFFFF/1024)-1 //VGA video memory
 mem.KBReader(i)=VGA.ReadMemory : mem.KBWriter(i)=VGA.WriteMemory 
next

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

//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)

 'load ROM BIOS files
 s="bin\at386.bin" : mem.Bytes(0x100000-FileSize(s))=ArrayFile(s) 'main BIOS
 //s="bin\trident.bin" : mem.Bytes(0xC0000)=ArrayFile(s)  'VGA BIOS
 s="bin\et4000.bin" : mem.Bytes(0xC0000)=ArrayFile(s) 'VGA BIOS

 'some POST hacks (disable some checks)
 mem.Byte(0xF42A9)=0xEB 'checksum
 mem.Byte(0xF457E)=0xEB 'dma (step 21)
 mem.Word(0xFD62b)=0x9090 'kbc (step 27)

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

 'success 
 result=true
end
