'*****************************************************************************
'*
'* INT 19H (BIOS) BOOTSTRAP LOADER
'*
'*****************************************************************************

//Type message to screen
procedure TypeMsg(s as string="", nextline as boolean=true)
 dim i as integer : if nextline then s=s+Chr(13)+Chr(10)
 cpu.AH=0xE : cpu.BH=0 : for i=1 to Length(s)
 cpu.AL=Asc(Mid(s,i,1)) : pc.CallEmuInt(0x10) : next i
end

//interrupt handler
public function EINT_19H as boolean

dim i as integer

//Set video mode 3 (TEXT 80x25x16)
cpu.AL=3 : cpu.AH=0 : pc.CallEmuInt(0x10)

//Title message
TypeMsg("Script PC x86 BIOS v.0.005 (25/06/2009)") 'title
TypeMsg 'empty line

//Config message
i=mem.Size/(1024*1024)
TypeMsg("RAM "+iif(i<=1,"640Kb",Str(i)+"Mb"),false)
if emuDrives.FDDCount>0 then TypeMsg(", FDD",false)
if emuDrives.HDDCount>0 then TypeMsg(", HDD",false)
TypeMsg(", Speaker") : TypeMsg

//Trying to load OS from FDD_0
if (emuDrives.FDD(0).Ready) and (emuDrives.FDD(0).Latched) then
  TypeMsg("Loading OS from FLOPPY disk... ",false)
  //Load boot sector
  cpu.AH=2 'function
  cpu.AL=1 'sector count
  cpu.DL=0 'floppy disk 0 (A)
  cpu.DH=0 'side 0
  cpu.CH=0 'track 0
  cpu.CL=1 'sector 1
  cpu.ES=0 : cpu.BX=0x7C00 'buffer in memory
  pc.CallEmuInt(0x13)
  if (cpu.AH=0) and (cpu.FLAGS.CF=false) then 'success
     TypeMsg("Ok!") : TypeMsg : cpu.CS=0 : cpu.IP=0x7C00 //execute boot sector
     result=true : exit
  else : TypeMsg("Error!") : TypeMsg : end if
end

//Trying to load OS from HDD_0
if (emuDrives.HDD(0).Ready) and (emuDrives.HDD(0).Latched) then
  TypeMsg("Loading OS from HARD disk... ",false)
  //Load boot sector
  cpu.AH=2 'function
  cpu.AL=1 'sector count
  cpu.DL=0x80 'hard disk 0 (C)
  cpu.DH=0 'side 0
  cpu.CH=0 'track 0
  cpu.CL=1 'sector 1
  cpu.ES=0 : cpu.BX=0x7C00 'buffer in memory
  pc.CallEmuInt(0x13)
  if (cpu.AH=0) and (cpu.FLAGS.CF=false) then 'success
     TypeMsg("Ok!") : TypeMsg :cpu.CS=0 : cpu.IP=0x7C00 //execute boot sector
     result=true : exit
  else : TypeMsg("Error!") : TypeMsg :end if
end

//If OS not loaded, show message
TypeMsg("Insert bootable floppy/hard disk to drive A/C and ""reset""...")
TypeMsg

//Write CLI+HLT to 0:7C00h 
mem.Word(0x7C00)=0xF4FA

//Execute CLI+HLT 
cpu.CS=0 : cpu.IP=0x7C00

result=false : end

