///////////////////////////////////////////////////////////////////////////////
//                            Lisaac OS Library                              //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program is distributed in the hope that it will be useful,         //
//   but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//   GNU General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name        := SYSTEM;

  - copyright   := "2003-2005 Jérome Boutet, 2003-2007 Benoit Sonntag";
  
  - comment     := "Generic System Object (methods).";

  - external    := `#include <time.h>`;

Section Public
  
  - is_ansi:BOOLEAN := TRUE;
  
  - exit code:INTEGER <- `exit(@code)`;
  
  - putb value:UINTEGER_8 to port:UINTEGER_16 <-
  // Write in port
  ( 
    `{ unsigned short val;
      val = @value;
      asm
      (
	"movw %0,%%dx \n\
	movw %1,%%ax     \n\
	outb %%al,%%dx  "
	: /* No output */ 
	:"r"(@port), "r"(val)
	:"%ax","%dx"
      );
    }`;
  );

  - itemb port:UINTEGER_16 :UINTEGER_8 <-
  // Read in port
  ( + result:UINTEGER_8;
    `{ unsigned short res;
      asm
      (
	"movw %1,%%dx \n\
	inb %%dx,%%al \n\
	movw %%ax,%0  "
	:"=r"(res)
	:"r"(@port)
	:"%ax","%dx"
      )`;
      result := `res`:UINTEGER_8;
    `}`;
    result
  );
       
  - get_universal_time:UINTEGER_64 <-
  (
    `(unsigned long long)time(NULL)`:UINTEGER_64
  );

  // Memory Management
  
  - get_begin_memory:UINTEGER_32 := realloc_c (0,get_capacity_memory);
  
  - get_capacity_memory:UINTEGER_32 := 10000000h; 
                                   //1000000h /*16Mo*/; //10000000h /*256MB*/;
  
Section SYSTEM,MEMORY
  
  - realloc_c (beg:UINTEGER_32,nb:INTEGER) :UINTEGER_32 <-
  ( + result:UINTEGER_32;
    result := `(unsigned long)realloc((void *)@beg,@nb+15)`:UINTEGER_32;
    ((beg != 0) && {result != beg}).if {
      MEMORY.print_nbx beg;
      '\n'.print;
      MEMORY.print_nbx result;
      '\n'.print;
      exit 1;
    };
    ? {(beg != 0) ->> {beg = result}};
    ? {result != 0};
    result
  );
  
Section ISAAC  
  
  - make <-
  // Isaac compatibility.
  (
    // Nothing.
  );