/*
    Support.h, Copyright  (c) by Christian Conrad,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Topsy/Support.h,v $
 	Author(s):             Christian Conrad
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 2935 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-03-21 11:58:17 +0100 (Mit, 21 Mär 2007) $      by: $Author: baderma $
*/

#ifndef __SUPPORT_H
#define __SUPPORT_H

#include "Topsy.h"
#include "IOConsole.h"
#include <stdarg.h>

/*
 * Copy nbBytes from address srcAddress to address targetAddress.
 * (equivalent to UNIX memcpy(), new name to avoid confusion).
 */
void byteCopy( Address targetAddress, Address sourceAddress, unsigned long int nbBytes);

/* fill memory with zeros */
void zeroOut(Address target, unsigned long int size);

/* fill mem with 32 bit pattern */
void mem_set_uint32(Address targetAddress,
                    unsigned long pattern, unsigned long int nbBytes);

/* memcmp for Topsy */
int mem_cmp(register char *s, register char *t, unsigned long int len);

/* string functions (like in UNIX but with a _ after str */
/* NOTE: we do not care if you point to a valid pointer, that's your problem */
void str_cpy( char* target, char* source);

/* copies a string or at most the first size-1 bytes
 * and terminates it with 0 
 */
void str_ncpy(char* target, char* source, unsigned long int size);

/* test strings if they are equal
 */
int str_neql(char* s1, char* s2, unsigned long int len);
int str_eql(char* s1, char* s2);

/* str_ncmp(char *p1, char *p2, int pl);
   return values:
   p1 == p2 : 0
   p1 <  p2 : < 0
   p2 >  p2 : > 0
 */
int str_ncmp(register char *s, register char *t, unsigned long int len);
int str_cmp(char* s1, char* s2);

/*
 * return string length 
 */
unsigned long int str_len(char* s);

/* dump memory around address at with pre and post additional context */
void dumpMemory(unsigned char *at, unsigned int pre, unsigned int post);

/* Now the fun part ... printf */
/* this is far from complete, no floats and other freaky stuff ...  */
/* kernel must not use floats anyway... (gfa) */
/* but it acts like a real posix printf / vsnprintf. (cj) */
int vsnprintf(char *buffer, unsigned long len, char *fmt, va_list ap);

/* printf supports this flags: %, #, -, +, ' ', 0, . (precision) & field width */
/* 						types: diouXx, c, s, p, DOU, l & h (prefix to diouXx)  */
int vprintf(char *fmt, va_list ap);
int printf(char *fmt, ...);

/* I only support snprintf because sprintf is the direct way to hell */
int snprintf(char *buf, unsigned long len, char *fmt, ...);

/* support function (in assembler) to test and set memory
 * in one atomic step
 */
Boolean testAndSet(Boolean* lockvar);

#define DEBUG 1
/* printf style function if DEBUG is defined else it is a empty function */
void Debug(char *fmt, ...);

#endif
