/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id: main.c,v 1.28 2003/01/10 16:47:26 reddawg Exp $

**************************************************************************************/

#include <ubixos/gdt.h>
#include <ubixos/tss.h>
#include <ubixos/idt.h>
#include <ubixos/schedule.h>
#include <ubixos/vitals.h>
#include <ubixos/exec.h>
#include <ubixos/idlethread.h>
#include <ubixos/selftune.h>
#include <ubixos/message.h>
#include <ubixos/signal.h>
#include <drivers/display.h>
#include <drivers/8259.h>
#include <drivers/fdc.h>
#include <vmm/memory.h>
#include <vmm/paging.h>
#include <ubixfs/ubixfs.h>
#include <urm/urm.h>
#include <lib/kstdio.h>

int main();
void testSignal();

extern union descriptorTableunion IDT[256];

descriptorTable(GDT,64) {
  {dummy:0},
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim)),
  standardDescriptor(0x0, 0xFFFFF, (dLdt)),
  standardDescriptor(0x6000, sizeof(struct tssStruct), (dTss)),
  standardDescriptor(0x1000, sizeof(struct tssStruct), (dTss)),
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dDpl3 + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl3)),
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dDpl2 + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl2)),
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dDpl1 + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl1)),  
  };

struct {
  unsigned short limit __attribute__ ((packed));
  union descriptorTableunion *gdt __attribute__ ((packed));
  } loadGdt = { (64 * sizeof(union descriptorTableunion) - 1), GDT };

/************************************************************************

Function: void _start();
Description: This Function Is The Kernels Entry Point
Notes:

************************************************************************/
asm(
  ".globl _start\n"
  "_start:      \n"
  "  lgdtl (loadGdt)   \n" // Load GDT
  "  mov $0x10,%ax    \n"
  "  mov %ax,%ds      \n" // Set Up Defaults
  "  mov %ax,%es      \n"
  "  mov %ax,%fs      \n"
  "  mov %ax,%gs      \n"
  "  mov %ax,%ss      \n"
  "  mov $0x2FFFF,%esp\n"
  "  mov $0x18,%ax    \n" // Set Up Dummy LDT
  "  lldt %ax         \n" // Load Dummy LDT
  "  mov $0x20,%ax    \n" // Set Up Dummy TSS
  "  ltr %ax          \n" // Loads Dummy TSS  
  "  call main        \n"
  "  hlt              \n" // Halt The System If We Happen To Get Here
  );

void testA() {
  uInt32 connectionId, messageId;
  uInt32 sender, cmd, size;
  uInt8 data[256];
  connectionCreate("TEST", &connectionId, 0);
  kprintf("I Am Application A! - Ticks [%i][%i][%i]\n",systemVitals->sysUptime,systemVitals->sysTicks,_current->cpuTime);
  while (1) {
    messageId = messageRecv(connectionId, &sender, &cmd, &size, data);
    kprintf("Thread A Got message: [%i][%i][0x%X][%i][%i]\n", connectionId, sender,cmd,size, data[0]);
    messageReply(connectionId, messageId, cmd, size, data);
    kprintf("Sent Message Reply To B\n");
    }
  return;
  }

void testB() {
  uInt32 connectionId, cmd, size;
  uInt8 data[256];
  uInt8 data1 = 0;
//  kprintf("[0x%X]\n",signal(69,testSignal));
  connectionOpen("TEST", &connectionId);
  data[0] = 1;
  while (1) {
    //kprintf("I Am Application B! - Ticks [%i][%i][%i]\n",systemVitals->sysUptime,systemVitals->sysTicks,_current->cpuTime);
    kprintf("I Am Application B! - Ticks [%i][%i]\n",systemVitals->sysUptime,systemVitals->sysTicks);
    cmd = 40;
    size = 1;
    messageSend(connectionId, &cmd, &size, data);
    data[0]++;
    kprintf("Task B Sent Message To Task A\n");
    //cmd++;
    //sendSignal(4,69);
    //sleep(20,0);
    }
  return;
  }

void testC() {
  uInt32 connectionId, cmd, size;
  connectionOpen("URM", &connectionId);
  while (1) {
    kprintf("I Am Application C! - Ticks [%i][%i][%i]\n",systemVitals->sysUptime,systemVitals->sysTicks,_current->cpuTime);
    cmd = 0x1;
    size = sizeof("Blah");
    messageSend(connectionId, &cmd, &size,"Blah");
    cmd++;
    sleep(5,0);
    }
  return;
  }

void testSignal() {
  while (1) {
    kprintf("This Is A Test Signal\n");
    }
  //_current->status = EMPTY;
  //while(1);
  }
  
/************************************************************************

Function: int main();
Description: This Function Is The Kernels Main Loop
Notes:

************************************************************************/  
int main() {
  clearScreen();      //Clear The Screen
  initVitals();       //Initialize The Systems Vitals
  initMemoryMap();    //Initialize The Memory Map
  initPagingSystem(); //Initialize Our Paging System
  init8259();         //Initialize PIC
  initIDT();          //Initialize The IDT
  initScheduler();    //Initialize Scheduler
  initFloppy();       //Initialize Floppy Controller
  initUbixFS();       //Initialize The UbixFS
  //initMouse();        //Initialize Mouse
  initPIT(1000);      //Initialize The Timer To 1000Hz
  execThread(idleThread,0x7FFF, "Idle Thread");
  //execThread(ubixOsResourceManage,0x6FFF,"UbixOS Resource Manager");
  execThread(selfTune,0x5FFF,"Self Tuning Thread");
  execThread(testSignal,0x4FFF,"Test Signal");
  //execThread(testA,0x4FFF,"testA");
  //execThread(testB,0x3FFF,"testB");
  //execThread(testC,0x2FFF,"testC");
  enableIrq(0);
  schedule();
  asm("hlt");
  return(0);
  }
