#include <sys/tss.h>
#include <ubixos/schedule.h>
#include <vmm/paging.h>
#include <lib/kmalloc.h>
#include <lib/bioscall.h>
#include <lib/string.h>
#include <sys/video.h>


asm (
  ".globl bios16Code\n"
  "bios16Code:      \n"
  "int $0x10        \n"
  "int $0x69        \n"
  );

  
int biosCall(int biosInt,int eax,int ebx,int ecx,int edx,int esi,int edi,int es,int ds) {
  short segment = 0x0,offset = 0x0;
  uInt32 tmpAddr = (uInt32)&bios16Code;
  kTask_t *newProcess = 0x0;

  offset = tmpAddr & 0xFFFF;
  segment = (tmpAddr >> 4) & 0xF000;

  newProcess = findTask();

  newProcess->tss.back_link    = 0x0;
  newProcess->tss.esp0         = (uInt32)kmalloc(4096,newProcess->id)+4096;
  newProcess->tss.ss0          = 0x10;
  newProcess->tss.esp1         = 0x0;
  newProcess->tss.ss1          = 0x0;
  newProcess->tss.esp2         = 0x0;
  newProcess->tss.ss2          = 0x0;
  newProcess->tss.cr3          = (uInt32)createVirtualSpace(newProcess->id); //(unsigned int)kernelPageDirectory;
  newProcess->tss.eip          =  offset & 0xFFFF;
  newProcess->tss.eflags       = 2 | EFLAG_IF | EFLAG_VM;
  newProcess->tss.eax          =     eax & 0xFFFF;
  newProcess->tss.ebx          =     ebx & 0xFFFF;
  newProcess->tss.ecx          =     ecx & 0xFFFF;
  newProcess->tss.edx          =     edx & 0xFFFF;
  newProcess->tss.esp          =  0x1000 & 0xFFFF;
  newProcess->tss.ebp          =  0x1000 & 0xFFFF;
  newProcess->tss.esi          =     esi & 0xFFFF;
  newProcess->tss.edi          =     edi & 0xFFFF;
  newProcess->tss.es           =      es & 0xFFFF;
  newProcess->tss.cs           = segment & 0xFFFF;
  newProcess->tss.ss           =  0x1000 & 0xFFFF;
  newProcess->tss.ds           =      ds & 0xFFFF;
  newProcess->tss.fs           =     0x0 & 0xFFFF;
  newProcess->tss.gs           =     0x0 & 0xFFFF;
  newProcess->tss.ldt          =     0x0 & 0xFFFF;
  newProcess->tss.trace_bitmap =     0x0 & 0xFFFF;
  //newProcess->tss.io_map       = sizeof(struct tssStruct)-8192;
  newProcess->oInfo.v86Task    = 0x1;
  //kmemset(&newProcess->tss.io_space,0,8192);
  setTaskStatus(newProcess->id, READY);

  while (newProcess->status != EXITING) sched_yield(); 

  return(1);
  }    
