'******************************************************************************
'* 
'* INIT OF MSX-1 (School/home 8-bit computer by ASCII and MS, 198x)
'*
'* Note: - This script always executed while emulation starting and then 
'*         "used" by other emulation scripts (to provide access to CPU etc.)
'*
'******************************************************************************

? "[INIT] Started emulation of MSX-1"

//Calculation of cartridge ROM sizes
public dim cart1 as object=emuDrives.ROM(0), cart2 as object=emuDrives.ROM(1)
public dim cart1size as integer, cart1offs as integer 'size and offs in KB
public dim cart2size as integer, cart2offs as integer 'size and offs in KB
if (cart1.Ready) and (cart1.Latched) then
   cart1size=cart1.Size/1024 
   if (cart1size*1024)<>cart1.size then cart1size=cart1size+1
else : cart1size=0 : end if
if (cart2.Ready) and (cart2.Latched) then
   cart2size=cart2.Size/1024 
   if (cart2size*1024)<>cart2.size then cart2size=cart2size+1
else : cart2size=0 : end if

//Configuring memory (64Kb r/w memory window and 4 slots x 64 Kb and cart1/2)
mem.Size=(5*64)*1024+cart1size*1024+cart2size*1024+2*8*1024 '2x8 KB of SRAM
mem.Limit=0xFFFF 

//Loading cartridge ROMs to memory
cart1offs=5*64 : cart2offs=cart1offs+cart1size
if cart1size>0 then : cart1offs=5*64
   mem.Pos=cart1offs*1024 : cart1.Pos=0 : mem.Copy(cart1.Object) 'load to memory
   cart1.Detected=true 
else : cart1offs=-1 : end if
if cart2size>0 then 
   if cart1size=0 then cart2offs=5*64 else cart2offs=cart1offs+cart1size+8 'SRAM
   mem.Pos=cart2offs*1024 : cart2.Pos=0 : mem.Copy(cart2.Object) 'load to memory
   cart2.Detected=true 
else : cart2offs=-1 : end if

'Assembling
public use object CPU_Z80 as CPU 'processor Z80
cpu.Frequency=3579545 '3,58Mhz
public use object "mboard" as MB : pc.AddDevice(MB) 'motherboard

