/*
    MemoryLayout.h, Copyright  (c) by George Fankhauser,
    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/Memory/mips/MemoryLayout.h,v $
 	Author(s):             George Fankhauser
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 2922 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
	
*/
#ifndef _MEMORYLAYOUT_H_
#define _MEMORYLAYOUT_H_

#include "cpu.h"

/* available memory */
#define PHYSMEM (1*1024*1024)
#define USERMEM (64*4*1024)

#define PHYSMEM_BASE 0x00000000 /* kernel direct mapped memory */
#define PHYSMEM_AVAIL PHYSMEM

/* PAGESIZE is the machines real page size */
#define PAGEBITS            	12
#define PAGESIZE            	(1 << PAGEBITS)
#define PAGEMASK            	(PAGESIZE - 1)
#define PAGEFRAMEMASK       	(~PAGEMASK)
#define PAGENUMBER(addr)    	(((unsigned long)(addr)) >> PAGEBITS)
#define PAGEREMAINDER(addr)		(((unsigned long)(addr)) & PAGEMASK)
#define PAGETOADDR(pagenr) 		((void *)(((unsigned long)(pagenr)) << PAGEBITS))
#define PAGEROUNDUP(addr) 		((((unsigned long)(addr)) + PAGEMASK) & PAGEFRAMEMASK )
#define PAGEROUNDDOWN(addr)		(((unsigned long)(addr)) & PAGEFRAMEMASK )
#define PAGETODMAPPED(pagenr)	((void *)PHYS_TO_K0SEG(PAGETOADDR((pagenr))))
#define DMAPPEDTOPAGE(addr)		((unsigned long)PAGENUMBER(K0SEG_TO_PHYS(addr)))
#define DMAPPEDTOPHYS(addr)		((void *)K0SEG_TO_PHYS(addr))

/* logical page size is the granularity of vm regions
 * this may be smaller on a direct mapped memory system. 
 * if paging is used LOGICAL and PHYSICAL have to be the same size 
 */
#define LOGICALPAGEBITS             8
#define LOGICALPAGESIZE            (1 << LOGICALPAGEBITS)
#define LOGICALPAGEFRAMEMASK       (0xffffffff << LOGICALPAGEBITS)
#define LOGICALPAGEMASK            (~LOGICALPAGEFRAMEMASK)
#define LOGICALPAGENUMBER(addr)    (((unsigned long)addr) >> LOGICALPAGEBITS)
#define LOGICALPAGEREMAINDER(addr) (((unsigned long)addr) & LOGICALPAGEMASK)

/* used as a boot and later as an exception stack */
/* We start at 1k so that there is enough space for the interrupt handlers */
#define EXCEPTIONSTACKSIZE	(PAGESIZE)
#define EXCEPTIONSTACKBOTTOM	(K0SEG_BASE)
#define EXCEPTIONSTACKTOP	(EXCEPTIONSTACKBOTTOM+EXCEPTIONSTACKSIZE-4)
/* exception context used to store pc, sp, ra and a0 in exceptions */
#define EXCEPTIONCONTEXTSIZE  16

#define BOOTSTACKSIZE	EXCEPTIONSTACKSIZE
#define BOOTSTACKBOTTOM	EXCEPTIONSTACKBOTTOM
#define BOOTSTACKTOP	EXCEPTIONSTACKTOP - EXCEPTIONCONTEXTSIZE

/* the steal region is in the kernel mapable region and has a virtual size of 4MB */
#define STEAL_START K2SEG_BASE
#define STEAL_SIZE  (4 * 1024 * 1024)
#endif
