/*

SIMPLE 16 BIT NASM AND C OPERATING SYSTEM

Author: Matthew W. Coan
Date: Mon Oct 31 06:44:36 EDT 2011

*/

/* CONSTANTS */
#define K_MEMORY_SIZE 512
#define BUFFER_SIZE 512

#define USER_SIZE 30
#define PASS_SIZE 30

#define PATH_SIZE 300
#define FILE_NAME_SIZE 30

#define RUN_STATE 1
#define WAIT_STATE 2

#define FILE_ARRAY_SIZE 10
#define PROC_ARRAY_SIZE 10
#define USER_ARRAY_SIZE 10

/* PROCESS STRUCT */
typedef struct _Proc {
   char name[FILE_NAME_SIZE];
   short state;
   char * code;
   short size;
   struct _Proc * next;
} Proc;

/* USER STRUCT */
typedef struct _User {
   char name[USER_SIZE];
   char pass[PASS_SIZE];
} User;

/* FILE STRUCT */
typedef struct _File {
   short sector;
   short drive;
   short head;
   short size;
   char path[PATH_SIZE];
   char name[FILE_NAME_SIZE];
   char r;
   char w;
   char x; 
} File;

/* MEMORY LIST */
typedef struct _Memory {
  short size;
  short used;
  char * memory;
  struct _Memory * next;
} Memory;

/* KERNEL INTERNAL CALLS */
extern void k_print(const char * msg);
extern short k_getch();
extern void k_readline(char * buffer);
extern void array_put(char * buffer, short i, short c);
extern short array_get(char * buffer, short i);
extern void readcmd();
extern void search();
extern short is_first();

/* SYSTEM CALLS */
extern short sys_open(const char * file);
extern void sys_read(short fd, char * buffer, short size);
extern void sys_write(short fd, char * buffer, short size);
extern void sys_close(short fd);
extern char * sys_malloc(short size);
extern void sys_free(char * p);
extern short sys_exec(const char * file);
extern void sys_exit(int code);
extern short sys_getpid();

/* MATH */
extern short short_lt(short arg1, short arg2);
extern short short_eq(short arg1, short arg2);
extern short short_neq(short arg1, short arg2);
extern short short_inc(short arg1);

/* GLOBAL KERNEL DATA */
User user_array[USER_ARRAY_SIZE];
File file_array[FILE_ARRAY_SIZE];
Proc proc_array[PROC_ARRAY_SIZE];
char buffer[BUFFER_SIZE];
Proc * p_proc;
short user_count;
short proc_count;
short file_count;
Memory * p_memory;
char * memory;
char * pointer;

/* KERNEL CODE */

void
bzero(char * buf1, short sz)
{
   short i;

   for(i = 0; short_lt(i,sz); i++)
      array_put(buf1, i, 0);
}

void
k_init()
{
   user_count = 0;
   user_count = 0;
   proc_count = 0;
   p_memory = 0;
   memory = 0;
   pointer = 0;
   p_proc = 0;
}

/* MAIN ENTRY POINT */
int
main()
{
   if(is_first()) {
      k_print(">>SimpleOS: Version: 1.0a\r\n");
      k_print("Author: Matthew W. Coan\r\n");
      k_print("\r\n");
      k_init();
      bzero(buffer, BUFFER_SIZE);
   }

   while(1) {
      k_print("SimpleOS> ");
      k_readline(buffer);
      k_print("\r\n");
      sys_exec(buffer);
      bzero(buffer, BUFFER_SIZE);
   }

   return 0;
}
