'******************************************************************************
'* 
'* VECTOR-06C MOTHERBOARD (MOTHERBOARD DEVICE)
'*
'* Supported platform/bus: Vector-06C
'*
'* Version history:
'*  2009 - initial emulation (by WadiM)
'*
'****************************************************************************** 

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

//parent DEVSET implementation
public use object DEVSET

//motherboard name
DeviceName="Vector-06C Motherboard"
DebugName="VECT6C_MB"

//variables
dim i as integer

'---------------------------- Video device (VID) ------------------------------

//Video 
public use object "video" as vid : AddDevice(vid)
for i=0x8000/1024 to 0x9FFF/1024 //video memory page 0
    mem.KBReader(i)=vid.ReadMemory0 : mem.KBWriter(i)=vid.WriteMemory0 : next
for i=0xA000/1024 to 0xBFFF/1024 //video memory page 1
    mem.KBReader(i)=vid.ReadMemory1 : mem.KBWriter(i)=vid.WriteMemory1 : next
for i=0xC000/1024 to 0xDFFF/1024 //video memory page 2
    mem.KBReader(i)=vid.ReadMemory2 : mem.KBWriter(i)=vid.WriteMemory2 : next
for i=0xE000/1024 to 0xFFFF/1024 //video memory page 3
    mem.KBReader(i)=vid.ReadMemory3 : mem.KBWriter(i)=vid.WriteMemory3 : next

'--------------------------- Interval timer (PIT) -----------------------------

//PIT (programmable interval timer)
public use object PIT_I8253 as PIT : AddDevice(PIT)
AddDevice(PIT.Channel0) : PIT.Channel0.Freq=1500000 //1.5Mhz
AddDevice(PIT.Channel1) : PIT.Channel1.Freq=1500000 //1.5Mhz
AddDevice(PIT.Channel2) : PIT.Channel2.Freq=1500000 //1.5Mhz
pc.WritePort(0xB)=PIT.CH0_DATA_PORT : pc.ReadPort(0xB)=PIT.CH0_DATA_PORT
pc.WritePort(0xA)=PIT.CH1_DATA_PORT : pc.ReadPort(0xA)=PIT.CH1_DATA_PORT
pc.WritePort(0x9)=PIT.CH2_DATA_PORT : pc.ReadPort(0x9)=PIT.CH2_DATA_PORT
pc.WritePort(0x8)=PIT.MODE_PORT     : pc.ReadPort(0x8)=PIT.MODE_PORT

'------------------------------ IRQ Emulation ---------------------------------

//IRQ flag
dim IRQFlag as boolean=false

//Set IRQ
public function SetIRQ(Index as integer, State as boolean) as boolean
 //?? DebugPrefix;"SetIRQ(";Hex(Index,2);"h, ";State;")"
 IRQFlag=true
end

//Get IRQ
public function GetIRQ(Process as boolean,byref Address as dword) as integer
 if IRQFlag then
    result=7 : Address=0x38 : if Process then IRQFlag=false
 else : result=-1 : end if
 //?? DebugPrefix;"GetIRQ(";Process;")=";result
end

//Configure IRQs
pc.SetIRQ=SetIRQ : pc.GetIRQ=GetIRQ

/*dim PORT1 as byte=0 
property PORT_1(Value as byte) 
 PORT1=Value
end
function PORT_1 as byte : PORT1=not PORT1 : result=PORT1 : end
pc.WritePort(0x1)=PORT_1 : pc.ReadPort(0x1)=PORT_1*/

property PORT_3(Value as byte) 
 //if Value=0xFF then dbg.break
end
function PORT_3 as byte : result=0xFF : end
pc.WritePort(0x3)=PORT_3 : pc.ReadPort(0x3)=PORT_3

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

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

 'load ROM BIOS file 
 dim s as string="bin\vector06c.rom" 
 if not FileExists(s) then : s="bin\vector.epr"
 if not FileExists(s) then : s="bin\vector06c.rom"
 end if : end if
 mem.Bytes(0)=ArrayFile(s) 'main BIOS

 //mark first 16K of memory area as read-only (ROM)
 //for i=0 to 15 : mem.KBAccess(i)=memReadOnly : next
 'mark next 64K of memory area as read-write (RAM)
 for i=0 to 63 : mem.KBAccess(i)=memReadWrite : next

 'success 
 result=true
end

